]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdevice-xi2.c
Inclusion cleanups in sources
[~andy/gtk] / gdk / x11 / gdkdevice-xi2.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include "gdkdevice-xi2.h"
23
24 #include "gdkintl.h"
25 #include "gdkx.h"
26
27 #include <X11/extensions/XInput2.h>
28
29 struct _GdkDeviceXI2Private
30 {
31   int device_id;
32 };
33
34 static void gdk_device_xi2_get_property (GObject      *object,
35                                          guint         prop_id,
36                                          GValue       *value,
37                                          GParamSpec   *pspec);
38 static void gdk_device_xi2_set_property (GObject      *object,
39                                          guint         prop_id,
40                                          const GValue *value,
41                                          GParamSpec   *pspec);
42
43 static void gdk_device_xi2_get_state (GdkDevice       *device,
44                                       GdkWindow       *window,
45                                       gdouble         *axes,
46                                       GdkModifierType *mask);
47 static void gdk_device_xi2_set_window_cursor (GdkDevice *device,
48                                               GdkWindow *window,
49                                               GdkCursor *cursor);
50 static void gdk_device_xi2_warp (GdkDevice *device,
51                                  GdkScreen *screen,
52                                  gint       x,
53                                  gint       y);
54 static gboolean gdk_device_xi2_query_state (GdkDevice        *device,
55                                             GdkWindow        *window,
56                                             GdkWindow       **root_window,
57                                             GdkWindow       **child_window,
58                                             gint             *root_x,
59                                             gint             *root_y,
60                                             gint             *win_x,
61                                             gint             *win_y,
62                                             GdkModifierType  *mask);
63
64 static GdkGrabStatus gdk_device_xi2_grab   (GdkDevice     *device,
65                                             GdkWindow     *window,
66                                             gboolean       owner_events,
67                                             GdkEventMask   event_mask,
68                                             GdkWindow     *confine_to,
69                                             GdkCursor     *cursor,
70                                             guint32        time_);
71 static void          gdk_device_xi2_ungrab (GdkDevice     *device,
72                                             guint32        time_);
73
74 static GdkWindow * gdk_device_xi2_window_at_position (GdkDevice       *device,
75                                                       gint            *win_x,
76                                                       gint            *win_y,
77                                                       GdkModifierType *mask,
78                                                       gboolean         get_toplevel);
79 static void  gdk_device_xi2_select_window_events (GdkDevice    *device,
80                                                   GdkWindow    *window,
81                                                   GdkEventMask  event_mask);
82
83
84 G_DEFINE_TYPE (GdkDeviceXI2, gdk_device_xi2, GDK_TYPE_DEVICE)
85
86 enum {
87   PROP_0,
88   PROP_DEVICE_ID
89 };
90
91 static void
92 gdk_device_xi2_class_init (GdkDeviceXI2Class *klass)
93 {
94   GObjectClass *object_class = G_OBJECT_CLASS (klass);
95   GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
96
97   object_class->get_property = gdk_device_xi2_get_property;
98   object_class->set_property = gdk_device_xi2_set_property;
99
100   device_class->get_state = gdk_device_xi2_get_state;
101   device_class->set_window_cursor = gdk_device_xi2_set_window_cursor;
102   device_class->warp = gdk_device_xi2_warp;
103   device_class->query_state = gdk_device_xi2_query_state;
104   device_class->grab = gdk_device_xi2_grab;
105   device_class->ungrab = gdk_device_xi2_ungrab;
106   device_class->window_at_position = gdk_device_xi2_window_at_position;
107   device_class->select_window_events = gdk_device_xi2_select_window_events;
108
109   g_object_class_install_property (object_class,
110                                    PROP_DEVICE_ID,
111                                    g_param_spec_int ("device-id",
112                                                      P_("Device ID"),
113                                                      P_("Device identifier"),
114                                                      0, G_MAXINT, 0,
115                                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
116
117   g_type_class_add_private (object_class, sizeof (GdkDeviceXI2Private));
118 }
119
120 static void
121 gdk_device_xi2_init (GdkDeviceXI2 *device)
122 {
123   GdkDeviceXI2Private *priv;
124
125   device->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
126                                                      GDK_TYPE_DEVICE_XI2,
127                                                      GdkDeviceXI2Private);
128 }
129
130 static void
131 gdk_device_xi2_get_property (GObject    *object,
132                              guint       prop_id,
133                              GValue     *value,
134                              GParamSpec *pspec)
135 {
136   GdkDeviceXI2Private *priv;
137
138   priv = GDK_DEVICE_XI2 (object)->priv;
139
140   switch (prop_id)
141     {
142     case PROP_DEVICE_ID:
143       g_value_set_int (value, priv->device_id);
144       break;
145     default:
146       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147       break;
148     }
149 }
150
151 static void
152 gdk_device_xi2_set_property (GObject      *object,
153                              guint         prop_id,
154                              const GValue *value,
155                              GParamSpec   *pspec)
156 {
157   GdkDeviceXI2Private *priv;
158
159   priv = GDK_DEVICE_XI2 (object)->priv;
160
161   switch (prop_id)
162     {
163     case PROP_DEVICE_ID:
164       priv->device_id = g_value_get_int (value);
165       break;
166     default:
167       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168       break;
169     }
170 }
171
172 static void
173 gdk_device_xi2_get_state (GdkDevice       *device,
174                           GdkWindow       *window,
175                           gdouble         *axes,
176                           GdkModifierType *mask)
177 {
178   GdkDeviceXI2Private *priv;
179   GdkDisplay *display;
180   XIDeviceInfo *info;
181   gint i, j, ndevices;
182
183   priv = GDK_DEVICE_XI2 (device)->priv;
184   display = gdk_device_get_display (device);
185
186   if (axes)
187     {
188       info = XIQueryDevice(GDK_DISPLAY_XDISPLAY (display),
189                            priv->device_id, &ndevices);
190
191       for (i = 0, j = 0; i < info->num_classes; i++)
192         {
193           XIAnyClassInfo *class_info = info->classes[i];
194           GdkAxisUse use;
195           gdouble value;
196
197           if (class_info->type != XIValuatorClass)
198             continue;
199
200           value = ((XIValuatorClassInfo *) class_info)->value;
201           use = _gdk_device_get_axis_use (device, j);
202
203           switch (use)
204             {
205             case GDK_AXIS_X:
206             case GDK_AXIS_Y:
207             case GDK_AXIS_IGNORE:
208               if (device->mode == GDK_MODE_WINDOW)
209                 _gdk_device_translate_window_coord (device, window, j, value, &axes[j]);
210               else
211                 {
212                   gint root_x, root_y;
213
214                   /* FIXME: Maybe root coords chaching should happen here */
215                   gdk_window_get_origin (window, &root_x, &root_y);
216                   _gdk_device_translate_screen_coord (device, window,
217                                                       root_x, root_y,
218                                                       j, value,
219                                                       &axes[j]);
220                 }
221               break;
222             default:
223               _gdk_device_translate_axis (device, j, value, &axes[j]);
224               break;
225             }
226
227           j++;
228         }
229
230       XIFreeDeviceInfo (info);
231     }
232
233   if (mask)
234     gdk_device_xi2_query_state (device, window,
235                                 NULL, NULL,
236                                 NULL, NULL,
237                                 NULL, NULL,
238                                 mask);
239 }
240
241 static void
242 gdk_device_xi2_set_window_cursor (GdkDevice *device,
243                                   GdkWindow *window,
244                                   GdkCursor *cursor)
245 {
246   GdkDeviceXI2Private *priv;
247   GdkCursorPrivate *cursor_private;
248
249   priv = GDK_DEVICE_XI2 (device)->priv;
250
251   /* Non-master devices don't have a cursor */
252   if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
253     return;
254
255   if (cursor)
256     {
257       cursor_private = (GdkCursorPrivate*) cursor;
258
259       XIDefineCursor (GDK_WINDOW_XDISPLAY (window),
260                       priv->device_id,
261                       GDK_WINDOW_XWINDOW (window),
262                       cursor_private->xcursor);
263     }
264   else
265     XIUndefineCursor (GDK_WINDOW_XDISPLAY (window),
266                       priv->device_id,
267                       GDK_WINDOW_XWINDOW (window));
268 }
269
270 static void
271 gdk_device_xi2_warp (GdkDevice *device,
272                      GdkScreen *screen,
273                      gint       x,
274                      gint       y)
275 {
276   GdkDeviceXI2Private *priv;
277   Window dest;
278
279   priv = GDK_DEVICE_XI2 (device)->priv;
280   dest = GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen));
281
282   XIWarpPointer (GDK_SCREEN_XDISPLAY (screen),
283                  priv->device_id,
284                  None, dest,
285                  0, 0, 0, 0, x, y);
286 }
287
288 static gboolean
289 gdk_device_xi2_query_state (GdkDevice        *device,
290                             GdkWindow        *window,
291                             GdkWindow       **root_window,
292                             GdkWindow       **child_window,
293                             gint             *root_x,
294                             gint             *root_y,
295                             gint             *win_x,
296                             gint             *win_y,
297                             GdkModifierType  *mask)
298 {
299   GdkDisplay *display;
300   GdkDeviceXI2Private *priv;
301   Window xroot_window, xchild_window;
302   gdouble xroot_x, xroot_y, xwin_x, xwin_y;
303   XIButtonState button_state;
304   XIModifierState mod_state;
305   XIGroupState group_state;
306
307   if (!window || GDK_WINDOW_DESTROYED (window))
308     return FALSE;
309
310   priv = GDK_DEVICE_XI2 (device)->priv;
311   display = gdk_window_get_display (window);
312
313   if (!XIQueryPointer (GDK_WINDOW_XDISPLAY (window),
314                        priv->device_id,
315                        GDK_WINDOW_XID (window),
316                        &xroot_window,
317                        &xchild_window,
318                        &xroot_x,
319                        &xroot_y,
320                        &xwin_x,
321                        &xwin_y,
322                        &button_state,
323                        &mod_state,
324                        &group_state))
325     {
326       return FALSE;
327     }
328
329   if (root_window)
330     *root_window = gdk_window_lookup_for_display (display, xroot_window);
331
332   if (child_window)
333     *child_window = gdk_window_lookup_for_display (display, xchild_window);
334
335   if (root_x)
336     *root_x = (gint) xroot_x;
337
338   if (root_y)
339     *root_y = (gint) xroot_y;
340
341   if (win_x)
342     *win_x = (gint) xwin_x;
343
344   if (win_y)
345     *win_y = (gint) xwin_y;
346
347   if (mask)
348     *mask = gdk_device_xi2_translate_state (&mod_state, &button_state);
349
350   return TRUE;
351 }
352
353 static GdkGrabStatus
354 gdk_device_xi2_grab (GdkDevice    *device,
355                      GdkWindow    *window,
356                      gboolean      owner_events,
357                      GdkEventMask  event_mask,
358                      GdkWindow    *confine_to,
359                      GdkCursor    *cursor,
360                      guint32       time_)
361 {
362   GdkDeviceXI2Private *priv;
363   GdkDisplay *display;
364   XIEventMask mask;
365   Window xwindow;
366   Cursor xcursor;
367   int status;
368
369   priv = GDK_DEVICE_XI2 (device)->priv;
370   display = gdk_device_get_display (device);
371
372   /* FIXME: confine_to is actually unused */
373
374   xwindow = GDK_WINDOW_XID (window);
375
376   if (!cursor)
377     xcursor = None;
378   else
379     {
380       _gdk_x11_cursor_update_theme (cursor);
381       xcursor = ((GdkCursorPrivate *) cursor)->xcursor;
382     }
383
384   mask.deviceid = priv->device_id;
385   mask.mask = gdk_device_xi2_translate_event_mask (event_mask, &mask.mask_len);
386
387   status = XIGrabDevice (GDK_DISPLAY_XDISPLAY (display),
388                          priv->device_id,
389                          xwindow,
390                          time_,
391                          xcursor,
392                          GrabModeAsync, GrabModeAsync,
393                          owner_events,
394                          &mask);
395
396   g_free (mask.mask);
397
398   return _gdk_x11_convert_grab_status (status);
399 }
400
401 static void
402 gdk_device_xi2_ungrab (GdkDevice *device,
403                        guint32    time_)
404 {
405   GdkDeviceXI2Private *priv;
406   GdkDisplay *display;
407
408   priv = GDK_DEVICE_XI2 (device)->priv;
409   display = gdk_device_get_display (device);
410
411   XIUngrabDevice (GDK_DISPLAY_XDISPLAY (display),
412                   priv->device_id,
413                   time_);
414 }
415
416 static GdkWindow *
417 gdk_device_xi2_window_at_position (GdkDevice       *device,
418                                    gint            *win_x,
419                                    gint            *win_y,
420                                    GdkModifierType *mask,
421                                    gboolean         get_toplevel)
422 {
423   GdkDeviceXI2Private *priv;
424   GdkDisplay *display;
425   GdkScreen *screen;
426   Display *xdisplay;
427   GdkWindow *window;
428   Window xwindow, root, child, last = None;
429   gdouble xroot_x, xroot_y, xwin_x, xwin_y;
430   XIButtonState button_state;
431   XIModifierState mod_state;
432   XIGroupState group_state;
433
434   priv = GDK_DEVICE_XI2 (device)->priv;
435   display = gdk_device_get_display (device);
436   screen = gdk_display_get_default_screen (display);
437
438   /* This function really only works if the mouse pointer is held still
439    * during its operation. If it moves from one leaf window to another
440    * than we'll end up with inaccurate values for win_x, win_y
441    * and the result.
442    */
443   gdk_x11_display_grab (display);
444
445   xdisplay = GDK_SCREEN_XDISPLAY (screen);
446   xwindow = GDK_SCREEN_XROOTWIN (screen);
447
448   XIQueryPointer (xdisplay,
449                   priv->device_id,
450                   xwindow,
451                   &root, &child,
452                   &xroot_x, &xroot_y,
453                   &xwin_x, &xwin_y,
454                   &button_state,
455                   &mod_state,
456                   &group_state);
457
458   if (root == xwindow)
459     xwindow = child;
460   else
461     xwindow = root;
462
463   while (xwindow)
464     {
465       last = xwindow;
466       XIQueryPointer (xdisplay,
467                       priv->device_id,
468                       xwindow,
469                       &root, &xwindow,
470                       &xroot_x, &xroot_y,
471                       &xwin_x, &xwin_y,
472                       &button_state,
473                       &mod_state,
474                       &group_state);
475
476       if (get_toplevel && last != root &&
477           (window = gdk_window_lookup_for_display (display, last)) != NULL &&
478           GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
479         {
480           xwindow = last;
481           break;
482         }
483     }
484
485   gdk_x11_display_ungrab (display);
486
487   window = gdk_window_lookup_for_display (display, last);
488
489   if (win_x)
490     *win_x = (window) ? (gint) xwin_x : -1;
491
492   if (win_y)
493     *win_y = (window) ? (gint) xwin_y : -1;
494
495   if (mask)
496     *mask = gdk_device_xi2_translate_state (&mod_state, &button_state);
497
498   return window;
499 }
500
501 static void
502 gdk_device_xi2_select_window_events (GdkDevice    *device,
503                                      GdkWindow    *window,
504                                      GdkEventMask  event_mask)
505 {
506   GdkDeviceXI2Private *priv;
507   XIEventMask evmask;
508
509   priv = GDK_DEVICE_XI2 (device)->priv;
510
511   evmask.deviceid = priv->device_id;
512   evmask.mask = gdk_device_xi2_translate_event_mask (event_mask, &evmask.mask_len);
513
514   XISelectEvents (GDK_WINDOW_XDISPLAY (window),
515                   GDK_WINDOW_XWINDOW (window),
516                   &evmask, 1);
517
518   g_free (evmask.mask);
519 }
520
521 guchar *
522 gdk_device_xi2_translate_event_mask (GdkEventMask  event_mask,
523                                      int          *len)
524 {
525   guchar *mask;
526
527   *len = XIMaskLen (XI_LASTEVENT);
528   mask = g_new0 (guchar, *len);
529
530   if (event_mask & GDK_POINTER_MOTION_MASK ||
531       event_mask & GDK_POINTER_MOTION_HINT_MASK)
532     XISetMask (mask, XI_Motion);
533
534   if (event_mask & GDK_BUTTON_MOTION_MASK ||
535       event_mask & GDK_BUTTON1_MOTION_MASK ||
536       event_mask & GDK_BUTTON2_MOTION_MASK ||
537       event_mask & GDK_BUTTON3_MOTION_MASK)
538     {
539       XISetMask (mask, XI_ButtonPress);
540       XISetMask (mask, XI_ButtonRelease);
541       XISetMask (mask, XI_Motion);
542     }
543
544   if (event_mask & GDK_SCROLL_MASK)
545     {
546       XISetMask (mask, XI_ButtonPress);
547       XISetMask (mask, XI_ButtonRelease);
548     }
549
550   if (event_mask & GDK_BUTTON_PRESS_MASK)
551     XISetMask (mask, XI_ButtonPress);
552
553   if (event_mask & GDK_BUTTON_RELEASE_MASK)
554     XISetMask (mask, XI_ButtonRelease);
555
556   if (event_mask & GDK_KEY_PRESS_MASK)
557     XISetMask (mask, XI_KeyPress);
558
559   if (event_mask & GDK_KEY_RELEASE_MASK)
560     XISetMask (mask, XI_KeyRelease);
561
562   if (event_mask & GDK_ENTER_NOTIFY_MASK)
563     XISetMask (mask, XI_Enter);
564
565   if (event_mask & GDK_LEAVE_NOTIFY_MASK)
566     XISetMask (mask, XI_Leave);
567
568   if (event_mask & GDK_FOCUS_CHANGE_MASK)
569     {
570       XISetMask (mask, XI_FocusIn);
571       XISetMask (mask, XI_FocusOut);
572     }
573
574   return mask;
575 }
576
577 guint
578 gdk_device_xi2_translate_state (XIModifierState *mods_state,
579                                 XIButtonState   *buttons_state)
580 {
581   guint state = 0;
582
583   if (mods_state)
584     state = (guint) mods_state->effective;
585
586   if (buttons_state)
587     {
588       gint len, i;
589
590       /* We're only interested in the first 5 buttons */
591       len = MIN (5, buttons_state->mask_len * 8);
592
593       for (i = 0; i < len; i++)
594         {
595           if (!XIMaskIsSet (buttons_state->mask, i))
596             continue;
597
598           switch (i)
599             {
600             case 1:
601               state |= GDK_BUTTON1_MASK;
602               break;
603             case 2:
604               state |= GDK_BUTTON2_MASK;
605               break;
606             case 3:
607               state |= GDK_BUTTON3_MASK;
608               break;
609             case 4:
610               state |= GDK_BUTTON4_MASK;
611               break;
612             case 5:
613               state |= GDK_BUTTON5_MASK;
614               break;
615             default:
616               break;
617             }
618         }
619     }
620
621   return state;
622 }