~ubuntu-branches/ubuntu/trusty/compiz/trusty

« back to all changes in this revision

Viewing changes to plugins/freewins/src/input.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2013-08-22 06:58:07 UTC
  • mto: This revision was merged to the branch mainline in revision 3352.
  • Revision ID: package-import@ubuntu.com-20130822065807-17nlzez0d30y09so
Tags: upstream-0.9.10+13.10.20130822
ImportĀ upstreamĀ versionĀ 0.9.10+13.10.20130822

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Compiz Fusion Freewins plugin
 
3
 *
 
4
 * input.cpp
 
5
 *
 
6
 * Copyright (C) 2007  Rodolfo Granata <warlock.cc@gmail.com>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License
 
10
 * as published by the Free Software Foundation; either version 2
 
11
 * of the License, or (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * Author(s):
 
19
 * Rodolfo Granata <warlock.cc@gmail.com>
 
20
 * Sam Spilsbury <smspillaz@gmail.com>
 
21
 *
 
22
 * Button binding support and Reset added by:
 
23
 * enigma_0Z <enigma.0ZA@gmail.com>
 
24
 *
 
25
 * Most of the input handling here is based on
 
26
 * the shelf plugin by
 
27
 *        : Kristian LyngstĆøl <kristian@bohemians.org>
 
28
 *        : Danny Baumann <maniac@opencompositing.org>
 
29
 *
 
30
 * Description:
 
31
 *
 
32
 * This plugin allows you to freely transform the texture of a window,
 
33
 * whether that be rotation or scaling to make better use of screen space
 
34
 * or just as a toy.
 
35
 *
 
36
 * Todo:
 
37
 *  - Fully implement an input redirection system by
 
38
 *    finding an inverse matrix, multiplying by it,
 
39
 *    translating to the actual window co-ords and
 
40
 *    XSendEvent() the co-ords to the actual window.
 
41
 *  - Code could be cleaner
 
42
 *  - Add timestep and speed options to animation
 
43
 *  - Add window hover-over info via paintOutput : i.e
 
44
 *    - Resize borders
 
45
 *    - 'Reset' Button
 
46
 *    - 'Scale' Button
 
47
 *    - 'Rotate' Button
 
48
 */
 
49
 
 
50
#include "freewins.h"
 
51
#include <cairo/cairo-xlib.h>
 
52
 
 
53
 
 
54
/* ------ Input Prevention -------------------------------------------*/
 
55
 
 
56
/* Shape the IPW
 
57
 * Thanks to Joel Bosveld (b0le)
 
58
 * for helping me with this section
 
59
 */
 
60
void
 
61
FWWindow::shapeIPW ()
 
62
{
 
63
    if (mInput)
 
64
    {
 
65
        Window xipw = mInput->ipw;
 
66
        CompWindow *ipw = screen->findWindow (xipw);
 
67
 
 
68
        if (ipw)
 
69
        {
 
70
            cairo_t                             *cr;
 
71
            int               width, height;
 
72
 
 
73
            width = mInputRect.width ();
 
74
            height = mInputRect.height ();
 
75
 
 
76
            Pixmap b = XCreatePixmap (screen->dpy (), xipw, width, height, 1);
 
77
 
 
78
            cairo_surface_t *bitmap =
 
79
                    cairo_xlib_surface_create_for_bitmap (screen->dpy (),
 
80
                                                          b,
 
81
                                                          DefaultScreenOfDisplay (screen->dpy ()),
 
82
                                                          width, height);
 
83
 
 
84
            cr = cairo_create (bitmap);
 
85
 
 
86
            cairo_save (cr);
 
87
            cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
 
88
            cairo_paint (cr);
 
89
            cairo_restore (cr);
 
90
 
 
91
            /* Move to our first corner (TopLeft) */
 
92
 
 
93
            cairo_move_to (cr,
 
94
                           mOutput.shapex1 - MIN(mInputRect.x1 (), mInputRect.x2 ()),
 
95
                           mOutput.shapey1 - MIN(mInputRect.y1 (), mInputRect.y2 ()));
 
96
 
 
97
            /* Line to TopRight */
 
98
 
 
99
            cairo_line_to (cr,
 
100
                           mOutput.shapex2 - MIN(mInputRect.x1 (), mInputRect.x2 ()),
 
101
                           mOutput.shapey2 - MIN(mInputRect.y1 (), mInputRect.y2 ()));
 
102
 
 
103
            /* Line to BottomRight */
 
104
 
 
105
            cairo_line_to (cr,
 
106
                           mOutput.shapex4 - MIN(mInputRect.x1 (), mInputRect.x2 ()),
 
107
                           mOutput.shapey4 - MIN(mInputRect.y1 (), mInputRect.y2 ()));
 
108
 
 
109
            /* Line to BottomLeft */
 
110
 
 
111
            cairo_line_to (cr,
 
112
                           mOutput.shapex3 - MIN(mInputRect.x1 (), mInputRect.x2 ()),
 
113
                           mOutput.shapey3 - MIN(mInputRect.y1 (), mInputRect.y2 ()));
 
114
 
 
115
            /* Line to TopLeft */
 
116
 
 
117
            cairo_line_to (cr,
 
118
                           mOutput.shapex1 - MIN(mInputRect.x1 (), mInputRect.x2 ()),
 
119
                           mOutput.shapey1 - MIN(mInputRect.y1 (), mInputRect.y2 ()));
 
120
 
 
121
            /* Ensure it's all closed up */
 
122
 
 
123
            cairo_close_path (cr);
 
124
 
 
125
            /* Fill in the box */
 
126
 
 
127
            cairo_set_source_rgb (cr, 1.0f, 1.0f, 1.0f);
 
128
            cairo_fill (cr);
 
129
 
 
130
            /* This takes the bitmap we just drew with cairo
 
131
             * and scans out the white bits (You can see these)
 
132
             * if you uncomment the following line after this
 
133
             * comment. Then, all the bits we drew on are clickable,
 
134
             * leaving us with a nice and neat window shape. Yummy.
 
135
             */
 
136
 
 
137
            /* XWriteBitmapFile (ipw->screen->display->display,
 
138
                                                                 "/path/to/your/image.bmp",
 
139
                                                                 b,
 
140
                                                                 ipw->width,
 
141
                                                                 ipw->height,
 
142
                                                                 -1, -1);  */
 
143
 
 
144
            XShapeCombineMask (screen->dpy (), xipw,
 
145
                               ShapeBounding,
 
146
                               0,
 
147
                               0,
 
148
                               b,
 
149
                               ShapeSet);
 
150
 
 
151
            XFreePixmap (screen->dpy (), b);
 
152
            cairo_surface_destroy (bitmap);
 
153
            cairo_destroy (cr);
 
154
        }
 
155
    }
 
156
}
 
157
 
 
158
void
 
159
FWWindow::saveInputShape (XRectangle **retRects,
 
160
                          int       *retCount,
 
161
                          int       *retOrdering)
 
162
{
 
163
    XRectangle *rects;
 
164
    int        count = 0, ordering;
 
165
    Display    *dpy = screen->dpy ();
 
166
 
 
167
    rects = XShapeGetRectangles (dpy, window->id (), ShapeInput, &count, &ordering);
 
168
 
 
169
    /* check if the returned shape exactly matches the window shape -
 
170
       if that is true, the window currently has no set input shape */
 
171
    if ((count == 1) &&
 
172
        (rects[0].x == -window->geometry ().border ()) &&
 
173
        (rects[0].y == -window->geometry ().border ()) &&
 
174
        (rects[0].width == (window->serverWidth () +
 
175
                            window->serverGeometry ().border ())) &&
 
176
        (rects[0].height == (window->serverHeight () +
 
177
                             window->serverGeometry (). border ())))
 
178
    {
 
179
        count = 0;
 
180
    }
 
181
    
 
182
    *retRects    = rects;
 
183
    *retCount    = count;
 
184
    *retOrdering = ordering;
 
185
}
 
186
 
 
187
void
 
188
FWScreen::addWindowToList (FWWindowInputInfo *info)
 
189
{
 
190
    mTransformedWindows.push_back (info);
 
191
}
 
192
 
 
193
void
 
194
FWScreen::removeWindowFromList (FWWindowInputInfo *info)
 
195
{
 
196
    mTransformedWindows.remove (info);
 
197
}
 
198
 
 
199
/* Adjust size and location of the input prevention window */
 
200
void
 
201
FWWindow::adjustIPW ()
 
202
{
 
203
    XWindowChanges xwc;
 
204
    Display        *dpy = screen->dpy ();
 
205
    float          f_width, f_height;
 
206
 
 
207
    if (!mInput || !mInput->ipw)
 
208
        return;
 
209
 
 
210
    f_width  = mInputRect.width ();
 
211
    f_height = mInputRect.height ();
 
212
 
 
213
    xwc.x          = mInputRect.x ();
 
214
    xwc.y          = mInputRect.y ();
 
215
    xwc.width      = (int) f_width;
 
216
    xwc.height     = (int) f_height;
 
217
    xwc.stack_mode = Below;
 
218
    /* XXX: This causes XConfigureWindow to break */
 
219
    //xwc.sibling    = window->id ();
 
220
 
 
221
    XMapWindow (dpy, mInput->ipw);
 
222
 
 
223
    XConfigureWindow (dpy, mInput->ipw,
 
224
                      CWStackMode | CWX | CWY | CWWidth | CWHeight,
 
225
                      &xwc);
 
226
 
 
227
    shapeIPW ();
 
228
}
 
229
 
 
230
void
 
231
FWScreen::adjustIPWStacking ()
 
232
{
 
233
 
 
234
    foreach (FWWindowInputInfo *run, mTransformedWindows)
 
235
    {
 
236
        if (!run->w->prev || run->w->prev->id () != run->ipw)
 
237
            FWWindow::get (run->w)->adjustIPW ();
 
238
    }
 
239
}
 
240
 
 
241
/* Create an input prevention window */
 
242
void
 
243
FWWindow::createIPW ()
 
244
{
 
245
    Window               ipw;
 
246
    XSetWindowAttributes attrib;
 
247
    XWindowChanges       xwc;
 
248
 
 
249
    if (!mInput || mInput->ipw)
 
250
        return;
 
251
 
 
252
    attrib.override_redirect = true;
 
253
    //attrib.event_mask        = 0;
 
254
 
 
255
    xwc.x = mInputRect.x ();
 
256
    xwc.y = mInputRect.y ();
 
257
    xwc.width = mInputRect.width ();
 
258
    xwc.height = mInputRect.height ();
 
259
 
 
260
    ipw = XCreateWindow (screen->dpy (),
 
261
                         screen->root (),
 
262
                         xwc.x, xwc.y, xwc.width, xwc.height, 0, CopyFromParent, InputOnly,
 
263
                         CopyFromParent, CWOverrideRedirect, &attrib);
 
264
 
 
265
    XMapWindow (screen->dpy (), ipw);
 
266
 
 
267
    //XConfigureWindow (screen->dpy (), ipw, CWStackMode | CWX | CWY | CWWidth | CWHeight, &xwc);
 
268
 
 
269
    mInput->ipw = ipw;
 
270
 
 
271
    //shapeIPW ();
 
272
}
 
273
 
 
274
FWWindowInputInfo::FWWindowInputInfo (CompWindow *window) :
 
275
    w (window),
 
276
    ipw (None),
 
277
    inputRects (NULL),
 
278
    nInputRects (0),
 
279
    inputRectOrdering (0),
 
280
    frameInputRects (NULL),
 
281
    frameNInputRects (0),
 
282
    frameInputRectOrdering (0)
 
283
{
 
284
}
 
285
 
 
286
FWWindowInputInfo::~FWWindowInputInfo ()
 
287
{
 
288
}
 
289
 
 
290
bool
 
291
FWWindow::handleWindowInputInfo ()
 
292
{
 
293
    FREEWINS_SCREEN (screen);
 
294
 
 
295
    if (!mTransformed && mInput)
 
296
    {
 
297
        if (mInput->ipw)
 
298
            XDestroyWindow (screen->dpy (), mInput->ipw);
 
299
 
 
300
        unshapeInput ();
 
301
        fws->removeWindowFromList (mInput);
 
302
 
 
303
        delete mInput;
 
304
        mInput = NULL;
 
305
 
 
306
        return false;
 
307
    }
 
308
    else if (mTransformed && !mInput)
 
309
    {
 
310
        mInput = new FWWindowInputInfo (window);
 
311
        if (!mInput)
 
312
            return false;
 
313
 
 
314
        shapeInput ();
 
315
        createIPW ();
 
316
        fws->addWindowToList (mInput);
 
317
    }
 
318
 
 
319
    return true;
 
320
}
 
321
 
 
322
/* Shape the input of the window when scaled.
 
323
 * Since the IPW will be dealing with the input, removing input
 
324
 * from the window entirely is a perfectly good solution. */
 
325
void
 
326
FWWindow::shapeInput ()
 
327
{
 
328
    Window     frame;
 
329
    Display    *dpy = screen->dpy();
 
330
 
 
331
    saveInputShape (&mInput->inputRects,
 
332
                    &mInput->nInputRects,
 
333
                    &mInput->inputRectOrdering);
 
334
 
 
335
    frame = window->frame();
 
336
    if (frame)
 
337
    {
 
338
        saveInputShape (&mInput->frameInputRects, &mInput->frameNInputRects,
 
339
                        &mInput->frameInputRectOrdering);
 
340
    }
 
341
    else
 
342
    {
 
343
        mInput->frameInputRects        = NULL;
 
344
        mInput->frameNInputRects       = -1;
 
345
        mInput->frameInputRectOrdering = 0;
 
346
    }
 
347
 
 
348
    /* clear shape */
 
349
    XShapeSelectInput (dpy, window->id(), NoEventMask);
 
350
    XShapeCombineRectangles  (dpy, window->id(), ShapeInput, 0, 0,
 
351
                              NULL, 0, ShapeSet, 0);
 
352
    
 
353
    if (frame)
 
354
        XShapeCombineRectangles  (dpy, window->frame(), ShapeInput, 0, 0,
 
355
                                  NULL, 0, ShapeSet, 0);
 
356
 
 
357
    XShapeSelectInput (dpy, window->id(), ShapeNotify);
 
358
}
 
359
 
 
360
/* Restores the shape of the window:
 
361
 * If the window had a custom shape defined by inputRects then we restore
 
362
 * that in order with XShapeCombineRectangles.
 
363
 * Most windows have no specific defined shape so we can restore it with
 
364
 * setting the shape to a 0x0 mask
 
365
 */
 
366
void
 
367
FWWindow::unshapeInput ()
 
368
{
 
369
    Display *dpy = screen->dpy ();
 
370
 
 
371
    if (mInput->nInputRects)
 
372
    {
 
373
        XShapeCombineRectangles (dpy, window->id(), ShapeInput, 0, 0,
 
374
                                 mInput->inputRects, mInput->nInputRects,
 
375
                                 ShapeSet, mInput->inputRectOrdering);
 
376
    }
 
377
    else
 
378
    {
 
379
        XShapeCombineMask (dpy, window->id(), ShapeInput, 0, 0, None, ShapeSet);
 
380
    }
 
381
 
 
382
    if (mInput->frameNInputRects >= 0)
 
383
    {
 
384
        if (mInput->frameNInputRects)
 
385
        {
 
386
            XShapeCombineRectangles (dpy, window->frame(), ShapeInput, 0, 0,
 
387
                                     mInput->frameInputRects,
 
388
                                     mInput->frameNInputRects,
 
389
                                     ShapeSet,
 
390
                                     mInput->frameInputRectOrdering);
 
391
        }
 
392
        else
 
393
        {
 
394
            XShapeCombineMask (dpy, window->frame(), ShapeInput, 0, 0, None, ShapeSet);
 
395
        }
 
396
    }
 
397
}