summaryrefslogtreecommitdiff
path: root/compiz/Apply-this-patch-second-instead-of-0002.patch
blob: eb43ad256da0c713c8f328d3ad76a89c015a292a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
From 40943030c021df26857a0e7e8b2d4cd94f2849b5 Mon Sep 17 00:00:00 2001
From: Sam Spilsbury <Sam@XPS-SUSE.site>
Date: Thu, 19 Mar 2009 14:04:11 +0900
Subject: [PATCH] Apply this patch second instead of 0002

---
 src/devices.c |  480 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 480 insertions(+), 0 deletions(-)
 create mode 100644 src/devices.c

diff --git a/src/devices.c b/src/devices.c
new file mode 100644
index 0000000..e27828e
--- /dev/null
+++ b/src/devices.c
@@ -0,0 +1,480 @@
+/*
+ * Copyright © 2008 University of South Australia
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * the authors not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior permission.
+ * The authors make no representations about the suitability of this
+ * software for any purpose. It is provided "as is" without express or
+ * implied warranty.
+ *
+ * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
+ * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Author: Peter Hutterer <peter@cs.unisa.edu.au>
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "../config.h"
+#endif
+
+#include <string.h>
+
+#include <compiz-core.h>
+
+extern Bool inHandleEvent;
+
+static Bool
+addDevice(CompDisplay *dpy, XDeviceInfo *info)
+{
+    int           ndevices = 0,
+                  cls;
+    CompDevice    *d;
+    XAnyClassInfo *any;
+
+    if (info->use >= IsXExtensionDevice) /* don't care about SDs */
+        return FALSE;
+
+    if (!dpy->devices)
+    {
+	dpy->devices = calloc(2, sizeof(CompDevice));
+	if (!dpy->devices)
+	    return FALSE;
+	ndevices = 1;
+	d = dpy->devices;
+    } else
+    {
+	d = dpy->devices;
+	while(d->id != -1)
+	{
+	    d++;
+	    ndevices++;
+	}
+	dpy->devices =                   /* + new device, + termination */
+	    realloc(dpy->devices, (ndevices + 2) * sizeof(CompDevice));
+        if (!dpy->devices)
+            return FALSE;
+
+        d = &dpy->devices[ndevices];
+    }
+
+
+    d->dev = XOpenDevice(dpy->display, info->id);
+    d->id = info->id;
+    d->use = info->use;
+    d->maxGrab = 0;
+    d->grabSize = 0;
+    d->grabs = NULL;
+    d->pointerX = 0;
+    d->pointerY = 0;
+    fprintf(stderr, "initializing button grabs\n");
+    d->buttonGrab  = 0;
+    d->nButtonGrab = 0;
+    d->keyGrab     = 0;
+    d->nKeyGrab    = 0;
+    ndevices++;
+
+    if (info->use == IsXPointer)
+    {
+        Window root, child;
+        int winx, winy, mask;
+        XQueryDevicePointer(dpy->display, d->dev,
+                            RootWindow(dpy->display, 0),
+                            &root, &child,
+                            &d->pointerX, &d->pointerY,
+                            &winx, &winy, &mask);
+	DeviceMotionNotify (d->dev,
+                            dpy->xi_motion,
+                            d->cls_motion);
+	DeviceButtonPress  (d->dev,
+                            dpy->xi_btpress,
+                            d->cls_btpress);
+	DeviceButtonRelease(d->dev,
+		            dpy->xi_btrelease,
+                            d->cls_btrelease);
+        DeviceEnterNotify  (d->dev,
+                            dpy->xi_enter,
+                            d->cls_enter);
+        DeviceLeaveNotify  (d->dev,
+                            dpy->xi_leave,
+                            d->cls_leave);
+	compLogMessage("devices", CompLogLevelDebug,
+                       "Found pointer %s.\n", info->name);
+    } else if (info->use == IsXKeyboard)
+    {
+	DeviceKeyPress  (d->dev,
+                         dpy->xi_kpress,
+                         d->cls_kpress);
+	DeviceKeyRelease(d->dev,
+                         dpy->xi_krelease,
+                         d->cls_krelease);
+        DeviceFocusIn   (d->dev,
+                         dpy->xi_focusin,
+                         d->cls_focusin);
+        DeviceFocusOut  (d->dev,
+                         dpy->xi_focusout,
+                         d->cls_focusout);
+	compLogMessage("devices", CompLogLevelDebug,
+                       "Found keyboard %s.\n", info->name);
+    }
+
+    any = info->inputclassinfo;
+    for (cls = 0; cls < info->num_classes; cls++)
+    {
+        if (any->class == AttachClass)
+        {
+            d->paired = ((XAttachInfoPtr)any)->attached;
+            break;
+        }
+        any = (XAnyClassPtr)((char*)any + any->length);
+    }
+
+    /* sort-of NULL-terminate the list */
+    d[1].id = -1;
+    d[1].use = -1;
+
+    return TRUE;
+}
+
+Bool
+compInitDeviceList(CompDisplay *display)
+{
+    int                 ninfo, i;
+    XDeviceInfo         *info = NULL;
+    XExtensionVersion   *ext;
+    int                 ret = TRUE;
+
+    display->devices      = NULL;
+    display->xi_btpress   = 0;
+    display->xi_btrelease = 0;
+    display->xi_motion    = 0;
+    display->xi_kpress    = 0;
+    display->xi_krelease  = 0;
+
+    ext = XQueryInputVersion(display->display, XI_2_Major, XI_2_Minor);
+    if (ext->major_version < XI_2_Major)
+    {
+        ret = FALSE;
+        compLogMessage("devices", CompLogLevelWarn,
+                   "XI 2 not available.\n");
+        goto unwind;
+    }
+
+    info = XListInputDevices(display->display, &ninfo);
+
+    for (i = 0; i < ninfo; i++)
+    {
+        if (info[i].use < IsXExtensionDevice)
+	    addDevice(display, &info[i]);
+    }
+
+unwind:
+    XFree(ext);
+    XFreeDeviceList(info);
+    return ret;
+}
+
+/**
+ * Add the device with the given deviceid to compiz' internal device list.
+ */
+Bool
+compAddDevice(CompDisplay *d, int deviceid)
+{
+    XDeviceInfo         *info = NULL;
+    int			ninfo, i, ret = FALSE;
+
+    info = XListInputDevices(d->display, &ninfo);
+
+    for (i = 0; i < ninfo; i++)
+    {
+        if (info[i].id == deviceid)
+        {
+            ret = addDevice(d, &info[i]);
+            break;
+        }
+    }
+
+    XFreeDeviceList(info);
+    return ret;
+}
+
+void
+compRemoveDevice(CompDisplay *dpy, int deviceid)
+{
+    CompDevice *d = dpy->devices;
+    CompDevice *remove = NULL;
+    int        idx, ndevices = 0;
+
+    while(d && d->id != -1)
+    {
+        if (d->id == deviceid)
+        {
+            remove = d;
+            idx = ndevices;
+        }
+        ndevices++;
+        d++;
+    }
+
+    /* Reminder: the -1 termination device isn't counted in ndevices */
+    if (remove)
+    {
+        XCloseDevice(dpy->display, remove->dev);
+        memmove(remove, &remove[1], (ndevices - idx) * sizeof(CompDevice));
+        dpy->devices = realloc(dpy->devices, ndevices * sizeof(CompDevice));
+    }
+}
+
+
+
+/**
+ * Find and return the device with the given id or NULL if the id is invalid.
+ */
+CompDevice*
+compFindDeviceById(CompDisplay *d, int id)
+{
+    CompDevice *ret = d->devices;
+
+    while(ret && ret->id != -1)
+    {
+        if (ret->id == id)
+            return ret;
+        ret++;
+    }
+
+    return NULL;
+}
+
+
+int
+pushDeviceGrab(CompScreen *s,
+               CompDevice *device,
+               Cursor cursor,
+               const char *name)
+{
+    CompDevice *ptr, *kbd;
+
+    if (device->use == IsXPointer)
+    {
+        ptr = device;
+        kbd = compFindDeviceById(s->display, device->paired);
+    } else
+    {
+        ptr = compFindDeviceById(s->display, device->paired);
+        kbd = device;
+    }
+
+    if (ptr->maxGrab == 0)
+    {
+        int status;
+        status = XExtendedGrabDevice(s->display->display,
+                                     ptr->dev,
+                                     s->grabWindow,
+                                     GrabModeAsync,
+                                     FALSE,
+                                     NULL,
+                                     cursor,
+                                     3, &ptr->cls_btpress,
+                                     0, NULL);
+
+        if (status == GrabSuccess)
+        {
+            status = XGrabDevice(s->display->display, kbd->dev,
+                                 s->grabWindow, FALSE,
+                                 2, &kbd->cls_kpress,
+                                 GrabModeAsync, GrabModeAsync, CurrentTime);
+            if (status != GrabSuccess)
+            {
+                XUngrabDevice(s->display->display, ptr->dev, CurrentTime);
+                return 0;
+            }
+        } else
+            return 0;
+    }
+    else
+        return 0;
+
+    if (ptr->grabSize <= ptr->maxGrab)
+    {
+        ptr->grabs = realloc(ptr->grabs,
+                                sizeof(CompGrab) * (ptr->maxGrab + 1));
+        if (!ptr->grabs)
+            return 0;
+        ptr->grabSize = ptr->maxGrab + 1;
+    }
+
+    /* In theory, ptr & kbd grabs should always be in sync, so we only update
+     * ptr's grab fields */
+    ptr->grabs[ptr->maxGrab].active = TRUE;
+    ptr->grabs[ptr->maxGrab].name   = name;
+    ptr->maxGrab++;
+    return ptr->maxGrab;
+}
+
+void
+removeDeviceGrab(CompScreen *s, CompDevice *device, int index,
+                 XPoint *restorePointer)
+{
+    CompDevice *ptr, *kbd;
+    int maxGrab;
+    index--;
+
+    if (device->use == IsXPointer)
+    {
+        ptr = device;
+        kbd = compFindDeviceById(s->display, device->paired);
+    } else
+    {
+        ptr = compFindDeviceById(s->display, device->paired);
+        kbd = device;
+    }
+
+    ptr->grabs[index].active = FALSE;
+    for (maxGrab = ptr->maxGrab; maxGrab; maxGrab--)
+        if (ptr->grabs[maxGrab - 1].active)
+            break;
+
+    if (maxGrab != ptr->maxGrab)
+    {
+        /* FIXME: ChangeActivePointerGrab equivalent? */
+        if (!maxGrab)
+        {
+            if (restorePointer)
+                warpDevicePointer(s, ptr,
+                                  restorePointer->x - ptr->pointerX,
+                                  restorePointer->y - ptr->pointerY);
+            XUngrabDevice(s->display->display, ptr->dev, CurrentTime);
+            XUngrabDevice(s->display->display, kbd->dev, CurrentTime);
+        }
+
+        ptr->maxGrab = maxGrab;
+    }
+}
+
+void
+warpDevicePointer(CompScreen *s, CompDevice *dev, int dx, int dy)
+{
+    CompDisplay *display = s->display;
+
+    dev->pointerX += dx;
+    dev->pointerX += dy;
+
+    if (dev->pointerX >= s->width)
+        dev->pointerX = s->width - 1;
+    else if (dev->pointerX < 0)
+        dev->pointerX = 0;
+
+    if (dev->pointerY >= s->height)
+        dev->pointerY = s->height - 1;
+    else if (dev->pointerY < 0)
+        dev->pointerY = 0;
+
+    XWarpDevicePointer(display->display,
+                       dev->dev,
+                       None, s->root,
+                       0, 0, 0, 0,
+                       dx, dy);
+    XSync(display->display, FALSE);
+
+    /* TODO: XCheckMaskEvent equivalent */
+
+    if (!inHandleEvent)
+    {
+        dev->lastPointerX = dev->pointerX;
+        dev->lastPointerY = dev->pointerY;
+    }
+}
+
+/* otherScreenGrabExist takes a series of strings terminated by a NULL.
+   It returns TRUE if a grab exists but it is NOT held by one of the
+   plugins listed, returns FALSE otherwise. */
+
+Bool
+otherDeviceGrabExist (CompDevice *dev, ...)
+{
+    va_list ap;
+    char *name;
+    int i;
+
+    for (i = 0; dev && i < dev->maxGrab; i++)
+    {
+        if (dev->grabs[i].active)
+        {
+            va_start(ap, dev);
+            name = va_arg (ap, char *);
+            while(name)
+            {
+		if (strcmp(name, dev->grabs[i].name) == 0)
+		    break;
+
+                name = va_arg(ap, char*);
+            }
+            va_end(ap);
+            if (!name)
+                return TRUE;
+        }
+    }
+    return FALSE;
+}
+
+/* Register for all XI events (including presence) on all root windows */
+void
+compRegisterXIEvents(CompDisplay *d, Window w)
+{
+    XEventClass *allclasses = NULL;
+    int nclasses = 0;
+    int idx = 0;
+    CompDevice *dev;
+    XEventClass cls_presence;
+
+    nclasses = 1; /* device presence */
+
+    dev = d->devices;
+    while(dev->id != -1)
+    {
+        nclasses += 4; /* button press/rel and enter/leave,
+                          or key press/rel and focus in/out,
+                          depending on dev->use */
+        dev++;
+    }
+    allclasses = malloc(nclasses * sizeof(XEventClass));
+
+    if (!allclasses)
+        return;
+
+    DevicePresence(d->display, d->xi_presence, cls_presence);
+    allclasses[idx++] = cls_presence;
+
+    dev = d->devices;
+    while(dev->id != -1)
+    {
+
+        if (dev->use == IsXPointer)
+        {
+            allclasses[idx++] = dev->cls_btpress;
+            allclasses[idx++] = dev->cls_btrelease;
+            allclasses[idx++] = dev->cls_enter;
+            allclasses[idx++] = dev->cls_leave;
+
+        } else if (dev->use == IsXKeyboard)
+        {
+            allclasses[idx++] = dev->cls_kpress;
+            allclasses[idx++] = dev->cls_krelease;
+            allclasses[idx++] = dev->cls_focusin;
+            allclasses[idx++] = dev->cls_focusout;
+        }
+        dev++;
+    }
+
+    XSelectExtensionEvent(d->display, w, allclasses, nclasses);
+    free(allclasses);
+}
-- 
1.5.6