]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdevice-xi2.c
x11: Simplify XI2 mods state
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include "gdkx11device-xi2.h"
21 #include "gdkdeviceprivate.h"
22
23 #include "gdkintl.h"
24 #include "gdkasync.h"
25 #include "gdkprivate-x11.h"
26
27 #include <stdlib.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 #include <X11/extensions/XInput2.h>
31
32
33 typedef struct _ScrollValuator ScrollValuator;
34
35 struct _ScrollValuator
36 {
37   guint n_valuator       : 4;
38   guint direction        : 4;
39   guint last_value_valid : 1;
40   gdouble last_value;
41 };
42
43 struct _GdkX11DeviceXI2
44 {
45   GdkDevice parent_instance;
46
47   gint device_id;
48   GArray *scroll_valuators;
49 };
50
51 struct _GdkX11DeviceXI2Class
52 {
53   GdkDeviceClass parent_class;
54 };
55
56 G_DEFINE_TYPE (GdkX11DeviceXI2, gdk_x11_device_xi2, GDK_TYPE_DEVICE)
57
58
59 static void gdk_x11_device_xi2_finalize     (GObject      *object);
60 static void gdk_x11_device_xi2_get_property (GObject      *object,
61                                              guint         prop_id,
62                                              GValue       *value,
63                                              GParamSpec   *pspec);
64 static void gdk_x11_device_xi2_set_property (GObject      *object,
65                                              guint         prop_id,
66                                              const GValue *value,
67                                              GParamSpec   *pspec);
68
69 static void gdk_x11_device_xi2_get_state (GdkDevice       *device,
70                                           GdkWindow       *window,
71                                           gdouble         *axes,
72                                           GdkModifierType *mask);
73 static void gdk_x11_device_xi2_set_window_cursor (GdkDevice *device,
74                                                   GdkWindow *window,
75                                                   GdkCursor *cursor);
76 static void gdk_x11_device_xi2_warp (GdkDevice *device,
77                                      GdkScreen *screen,
78                                      gint       x,
79                                      gint       y);
80 static gboolean gdk_x11_device_xi2_query_state (GdkDevice        *device,
81                                                 GdkWindow        *window,
82                                                 GdkWindow       **root_window,
83                                                 GdkWindow       **child_window,
84                                                 gint             *root_x,
85                                                 gint             *root_y,
86                                                 gint             *win_x,
87                                                 gint             *win_y,
88                                                 GdkModifierType  *mask);
89
90 static GdkGrabStatus gdk_x11_device_xi2_grab   (GdkDevice     *device,
91                                                 GdkWindow     *window,
92                                                 gboolean       owner_events,
93                                                 GdkEventMask   event_mask,
94                                                 GdkWindow     *confine_to,
95                                                 GdkCursor     *cursor,
96                                                 guint32        time_);
97 static void          gdk_x11_device_xi2_ungrab (GdkDevice     *device,
98                                                 guint32        time_);
99
100 static GdkWindow * gdk_x11_device_xi2_window_at_position (GdkDevice       *device,
101                                                           gint            *win_x,
102                                                           gint            *win_y,
103                                                           GdkModifierType *mask,
104                                                           gboolean         get_toplevel);
105 static void  gdk_x11_device_xi2_select_window_events (GdkDevice    *device,
106                                                       GdkWindow    *window,
107                                                       GdkEventMask  event_mask);
108
109
110 enum {
111   PROP_0,
112   PROP_DEVICE_ID
113 };
114
115 static void
116 gdk_x11_device_xi2_class_init (GdkX11DeviceXI2Class *klass)
117 {
118   GObjectClass *object_class = G_OBJECT_CLASS (klass);
119   GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
120
121   object_class->finalize = gdk_x11_device_xi2_finalize;
122   object_class->get_property = gdk_x11_device_xi2_get_property;
123   object_class->set_property = gdk_x11_device_xi2_set_property;
124
125   device_class->get_state = gdk_x11_device_xi2_get_state;
126   device_class->set_window_cursor = gdk_x11_device_xi2_set_window_cursor;
127   device_class->warp = gdk_x11_device_xi2_warp;
128   device_class->query_state = gdk_x11_device_xi2_query_state;
129   device_class->grab = gdk_x11_device_xi2_grab;
130   device_class->ungrab = gdk_x11_device_xi2_ungrab;
131   device_class->window_at_position = gdk_x11_device_xi2_window_at_position;
132   device_class->select_window_events = gdk_x11_device_xi2_select_window_events;
133
134   g_object_class_install_property (object_class,
135                                    PROP_DEVICE_ID,
136                                    g_param_spec_int ("device-id",
137                                                      P_("Device ID"),
138                                                      P_("Device identifier"),
139                                                      0, G_MAXINT, 0,
140                                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
141 }
142
143 static void
144 gdk_x11_device_xi2_init (GdkX11DeviceXI2 *device)
145 {
146   device->scroll_valuators = g_array_new (FALSE, FALSE, sizeof (ScrollValuator));
147 }
148
149 static void
150 gdk_x11_device_xi2_finalize (GObject *object)
151 {
152   GdkX11DeviceXI2 *device = GDK_X11_DEVICE_XI2 (object);
153
154   g_array_free (device->scroll_valuators, TRUE);
155
156   G_OBJECT_CLASS (gdk_x11_device_xi2_parent_class)->finalize (object);
157 }
158
159 static void
160 gdk_x11_device_xi2_get_property (GObject    *object,
161                                  guint       prop_id,
162                                  GValue     *value,
163                                  GParamSpec *pspec)
164 {
165   GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (object);
166
167   switch (prop_id)
168     {
169     case PROP_DEVICE_ID:
170       g_value_set_int (value, device_xi2->device_id);
171       break;
172     default:
173       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174       break;
175     }
176 }
177
178 static void
179 gdk_x11_device_xi2_set_property (GObject      *object,
180                                  guint         prop_id,
181                                  const GValue *value,
182                                  GParamSpec   *pspec)
183 {
184   GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (object);
185
186   switch (prop_id)
187     {
188     case PROP_DEVICE_ID:
189       device_xi2->device_id = g_value_get_int (value);
190       break;
191     default:
192       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193       break;
194     }
195 }
196
197 static void
198 gdk_x11_device_xi2_get_state (GdkDevice       *device,
199                               GdkWindow       *window,
200                               gdouble         *axes,
201                               GdkModifierType *mask)
202 {
203   GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
204
205   if (axes)
206     {
207       GdkDisplay *display;
208       XIDeviceInfo *info;
209       gint i, j, ndevices;
210
211       display = gdk_device_get_display (device);
212
213       gdk_x11_display_error_trap_push (display);
214       info = XIQueryDevice (GDK_DISPLAY_XDISPLAY (display),
215                             device_xi2->device_id, &ndevices);
216       gdk_x11_display_error_trap_pop_ignored (display);
217
218       for (i = 0, j = 0; info && i < info->num_classes; i++)
219         {
220           XIAnyClassInfo *class_info = info->classes[i];
221           GdkAxisUse use;
222           gdouble value;
223
224           if (class_info->type != XIValuatorClass)
225             continue;
226
227           value = ((XIValuatorClassInfo *) class_info)->value;
228           use = gdk_device_get_axis_use (device, j);
229
230           switch (use)
231             {
232             case GDK_AXIS_X:
233             case GDK_AXIS_Y:
234             case GDK_AXIS_IGNORE:
235               if (gdk_device_get_mode (device) == GDK_MODE_WINDOW)
236                 _gdk_device_translate_window_coord (device, window, j, value, &axes[j]);
237               else
238                 {
239                   gint root_x, root_y;
240
241                   /* FIXME: Maybe root coords chaching should happen here */
242                   gdk_window_get_origin (window, &root_x, &root_y);
243                   _gdk_device_translate_screen_coord (device, window,
244                                                       root_x, root_y,
245                                                       j, value,
246                                                       &axes[j]);
247                 }
248               break;
249             default:
250               _gdk_device_translate_axis (device, j, value, &axes[j]);
251               break;
252             }
253
254           j++;
255         }
256
257       if (info)
258         XIFreeDeviceInfo (info);
259     }
260
261   if (mask)
262     gdk_x11_device_xi2_query_state (device, window,
263                                     NULL, NULL,
264                                     NULL, NULL,
265                                     NULL, NULL,
266                                     mask);
267 }
268
269 static void
270 gdk_x11_device_xi2_set_window_cursor (GdkDevice *device,
271                                       GdkWindow *window,
272                                       GdkCursor *cursor)
273 {
274   GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
275
276   /* Non-master devices don't have a cursor */
277   if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
278     return;
279
280   if (cursor)
281     XIDefineCursor (GDK_WINDOW_XDISPLAY (window),
282                     device_xi2->device_id,
283                     GDK_WINDOW_XID (window),
284                     gdk_x11_cursor_get_xcursor (cursor));
285   else
286     XIUndefineCursor (GDK_WINDOW_XDISPLAY (window),
287                       device_xi2->device_id,
288                       GDK_WINDOW_XID (window));
289 }
290
291 static void
292 gdk_x11_device_xi2_warp (GdkDevice *device,
293                          GdkScreen *screen,
294                          gint       x,
295                          gint       y)
296 {
297   GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
298   Window dest;
299
300   dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
301
302   XIWarpPointer (GDK_SCREEN_XDISPLAY (screen),
303                  device_xi2->device_id,
304                  None, dest,
305                  0, 0, 0, 0, x, y);
306 }
307
308 static gboolean
309 gdk_x11_device_xi2_query_state (GdkDevice        *device,
310                                 GdkWindow        *window,
311                                 GdkWindow       **root_window,
312                                 GdkWindow       **child_window,
313                                 gint             *root_x,
314                                 gint             *root_y,
315                                 gint             *win_x,
316                                 gint             *win_y,
317                                 GdkModifierType  *mask)
318 {
319   GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
320   GdkDisplay *display;
321   GdkScreen *default_screen;
322   Window xroot_window, xchild_window;
323   gdouble xroot_x, xroot_y, xwin_x, xwin_y;
324   XIButtonState button_state;
325   XIModifierState mod_state;
326   XIGroupState group_state;
327
328   if (!window || GDK_WINDOW_DESTROYED (window))
329     return FALSE;
330
331   display = gdk_window_get_display (window);
332   default_screen = gdk_display_get_default_screen (display);
333
334   if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
335     {
336       if (!XIQueryPointer (GDK_WINDOW_XDISPLAY (window),
337                            device_xi2->device_id,
338                            GDK_WINDOW_XID (window),
339                            &xroot_window,
340                            &xchild_window,
341                            &xroot_x, &xroot_y,
342                            &xwin_x, &xwin_y,
343                            &button_state,
344                            &mod_state,
345                            &group_state))
346         return FALSE;
347     }
348   else
349     {
350       XSetWindowAttributes attributes;
351       Display *xdisplay;
352       Window xwindow, w;
353
354       /* FIXME: untrusted clients not multidevice-safe */
355       xdisplay = GDK_SCREEN_XDISPLAY (default_screen);
356       xwindow = GDK_SCREEN_XROOTWIN (default_screen);
357
358       w = XCreateWindow (xdisplay, xwindow, 0, 0, 1, 1, 0,
359                          CopyFromParent, InputOnly, CopyFromParent,
360                          0, &attributes);
361       XIQueryPointer (xdisplay, device_xi2->device_id,
362                       w,
363                       &xroot_window,
364                       &xchild_window,
365                       &xroot_x, &xroot_y,
366                       &xwin_x, &xwin_y,
367                       &button_state,
368                       &mod_state,
369                       &group_state);
370       XDestroyWindow (xdisplay, w);
371     }
372
373   if (root_window)
374     *root_window = gdk_x11_window_lookup_for_display (display, xroot_window);
375
376   if (child_window)
377     *child_window = gdk_x11_window_lookup_for_display (display, xchild_window);
378
379   if (root_x)
380     *root_x = (gint) xroot_x;
381
382   if (root_y)
383     *root_y = (gint) xroot_y;
384
385   if (win_x)
386     *win_x = (gint) xwin_x;
387
388   if (win_y)
389     *win_y = (gint) xwin_y;
390
391   if (mask)
392     *mask = _gdk_x11_device_xi2_translate_state (&mod_state, &button_state, &group_state);
393
394   free (button_state.mask);
395
396   return TRUE;
397 }
398
399 static GdkGrabStatus
400 gdk_x11_device_xi2_grab (GdkDevice    *device,
401                          GdkWindow    *window,
402                          gboolean      owner_events,
403                          GdkEventMask  event_mask,
404                          GdkWindow    *confine_to,
405                          GdkCursor    *cursor,
406                          guint32       time_)
407 {
408   GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
409   GdkX11DeviceManagerXI2 *device_manager_xi2;
410   GdkDisplay *display;
411   XIEventMask mask;
412   Window xwindow;
413   Cursor xcursor;
414   gint status;
415
416   display = gdk_device_get_display (device);
417   device_manager_xi2 = GDK_X11_DEVICE_MANAGER_XI2 (gdk_display_get_device_manager (display));
418
419   /* FIXME: confine_to is actually unused */
420
421   xwindow = GDK_WINDOW_XID (window);
422
423   if (!cursor)
424     xcursor = None;
425   else
426     {
427       _gdk_x11_cursor_update_theme (cursor);
428       xcursor = gdk_x11_cursor_get_xcursor (cursor);
429     }
430
431   mask.deviceid = device_xi2->device_id;
432   mask.mask = _gdk_x11_device_xi2_translate_event_mask (device_manager_xi2,
433                                                         event_mask,
434                                                         &mask.mask_len);
435
436 #ifdef G_ENABLE_DEBUG
437   if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
438     status = GrabSuccess;
439   else
440 #endif
441   status = XIGrabDevice (GDK_DISPLAY_XDISPLAY (display),
442                          device_xi2->device_id,
443                          xwindow,
444                          time_,
445                          xcursor,
446                          GrabModeAsync, GrabModeAsync,
447                          owner_events,
448                          &mask);
449
450   g_free (mask.mask);
451
452   _gdk_x11_display_update_grab_info (display, device, status);
453
454   return _gdk_x11_convert_grab_status (status);
455 }
456
457 static void
458 gdk_x11_device_xi2_ungrab (GdkDevice *device,
459                            guint32    time_)
460 {
461   GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
462   GdkDisplay *display;
463   gulong serial;
464
465   display = gdk_device_get_display (device);
466   serial = NextRequest (GDK_DISPLAY_XDISPLAY (display));
467
468   XIUngrabDevice (GDK_DISPLAY_XDISPLAY (display), device_xi2->device_id, time_);
469
470   _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
471 }
472
473 static GdkWindow *
474 gdk_x11_device_xi2_window_at_position (GdkDevice       *device,
475                                        gint            *win_x,
476                                        gint            *win_y,
477                                        GdkModifierType *mask,
478                                        gboolean         get_toplevel)
479 {
480   GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
481   GdkDisplay *display;
482   GdkScreen *screen;
483   Display *xdisplay;
484   GdkWindow *window;
485   Window xwindow, root, child, last = None;
486   gdouble xroot_x, xroot_y, xwin_x, xwin_y;
487   XIButtonState button_state = { 0 };
488   XIModifierState mod_state;
489   XIGroupState group_state;
490
491   display = gdk_device_get_display (device);
492   screen = gdk_display_get_default_screen (display);
493
494   /* This function really only works if the mouse pointer is held still
495    * during its operation. If it moves from one leaf window to another
496    * than we'll end up with inaccurate values for win_x, win_y
497    * and the result.
498    */
499   gdk_x11_display_grab (display);
500
501   xdisplay = GDK_SCREEN_XDISPLAY (screen);
502   xwindow = GDK_SCREEN_XROOTWIN (screen);
503
504   if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
505     {
506       XIQueryPointer (xdisplay,
507                       device_xi2->device_id,
508                       xwindow,
509                       &root, &child,
510                       &xroot_x, &xroot_y,
511                       &xwin_x, &xwin_y,
512                       &button_state,
513                       &mod_state,
514                       &group_state);
515
516       if (root == xwindow)
517         xwindow = child;
518       else
519         xwindow = root;
520     }
521   else
522     {
523       gint i, screens, width, height;
524       GList *toplevels, *list;
525       Window pointer_window, root, child;
526
527       /* FIXME: untrusted clients case not multidevice-safe */
528       pointer_window = None;
529       screens = gdk_display_get_n_screens (display);
530
531       for (i = 0; i < screens; ++i)
532         {
533           screen = gdk_display_get_screen (display, i);
534           toplevels = gdk_screen_get_toplevel_windows (screen);
535           for (list = toplevels; list != NULL; list = g_list_next (list))
536             {
537               window = GDK_WINDOW (list->data);
538               xwindow = GDK_WINDOW_XID (window);
539
540               /* Free previous button mask, if any */
541               g_free (button_state.mask);
542
543               gdk_x11_display_error_trap_push (display);
544               XIQueryPointer (xdisplay,
545                               device_xi2->device_id,
546                               xwindow,
547                               &root, &child,
548                               &xroot_x, &xroot_y,
549                               &xwin_x, &xwin_y,
550                               &button_state,
551                               &mod_state,
552                               &group_state);
553               if (gdk_x11_display_error_trap_pop (display))
554                 continue;
555               if (child != None)
556                 {
557                   pointer_window = child;
558                   break;
559                 }
560               gdk_window_get_geometry (window, NULL, NULL, &width, &height);
561               if (xwin_x >= 0 && xwin_y >= 0 && xwin_x < width && xwin_y < height)
562                 {
563                   /* A childless toplevel, or below another window? */
564                   XSetWindowAttributes attributes;
565                   Window w;
566
567                   free (button_state.mask);
568
569                   w = XCreateWindow (xdisplay, xwindow, (int)xwin_x, (int)xwin_y, 1, 1, 0,
570                                      CopyFromParent, InputOnly, CopyFromParent,
571                                      0, &attributes);
572                   XMapWindow (xdisplay, w);
573                   XIQueryPointer (xdisplay,
574                                   device_xi2->device_id,
575                                   xwindow,
576                                   &root, &child,
577                                   &xroot_x, &xroot_y,
578                                   &xwin_x, &xwin_y,
579                                   &button_state,
580                                   &mod_state,
581                                   &group_state);
582                   XDestroyWindow (xdisplay, w);
583                   if (child == w)
584                     {
585                       pointer_window = xwindow;
586                       break;
587                     }
588                 }
589             }
590
591           g_list_free (toplevels);
592           if (pointer_window != None)
593             break;
594         }
595
596       xwindow = pointer_window;
597     }
598
599   while (xwindow)
600     {
601       last = xwindow;
602       free (button_state.mask);
603
604       gdk_x11_display_error_trap_push (display);
605       XIQueryPointer (xdisplay,
606                       device_xi2->device_id,
607                       xwindow,
608                       &root, &xwindow,
609                       &xroot_x, &xroot_y,
610                       &xwin_x, &xwin_y,
611                       &button_state,
612                       &mod_state,
613                       &group_state);
614       if (gdk_x11_display_error_trap_pop (display))
615         break;
616
617       if (get_toplevel && last != root &&
618           (window = gdk_x11_window_lookup_for_display (display, last)) != NULL &&
619           GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
620         {
621           xwindow = last;
622           break;
623         }
624     }
625
626   gdk_x11_display_ungrab (display);
627
628   window = gdk_x11_window_lookup_for_display (display, last);
629
630   if (win_x)
631     *win_x = (window) ? (gint) xwin_x : -1;
632
633   if (win_y)
634     *win_y = (window) ? (gint) xwin_y : -1;
635
636   if (mask)
637     *mask = _gdk_x11_device_xi2_translate_state (&mod_state, &button_state, &group_state);
638
639   free (button_state.mask);
640
641   return window;
642 }
643
644 static void
645 gdk_x11_device_xi2_select_window_events (GdkDevice    *device,
646                                          GdkWindow    *window,
647                                          GdkEventMask  event_mask)
648 {
649   GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
650   GdkX11DeviceManagerXI2 *device_manager_xi2;
651   GdkDisplay *display;
652   XIEventMask evmask;
653
654   display = gdk_device_get_display (device);
655   device_manager_xi2 = GDK_X11_DEVICE_MANAGER_XI2 (gdk_display_get_device_manager (display));
656
657   evmask.deviceid = device_xi2->device_id;
658   evmask.mask = _gdk_x11_device_xi2_translate_event_mask (device_manager_xi2,
659                                                           event_mask,
660                                                           &evmask.mask_len);
661
662   XISelectEvents (GDK_WINDOW_XDISPLAY (window),
663                   GDK_WINDOW_XID (window),
664                   &evmask, 1);
665
666   g_free (evmask.mask);
667 }
668
669 guchar *
670 _gdk_x11_device_xi2_translate_event_mask (GdkX11DeviceManagerXI2 *device_manager_xi2,
671                                           GdkEventMask            event_mask,
672                                           gint                   *len)
673 {
674   guchar *mask;
675   gint minor;
676
677   g_object_get (device_manager_xi2, "minor", &minor, NULL);
678
679   *len = XIMaskLen (XI_LASTEVENT);
680   mask = g_new0 (guchar, *len);
681
682   if (event_mask & GDK_POINTER_MOTION_MASK ||
683       event_mask & GDK_POINTER_MOTION_HINT_MASK)
684     XISetMask (mask, XI_Motion);
685
686   if (event_mask & GDK_BUTTON_MOTION_MASK ||
687       event_mask & GDK_BUTTON1_MOTION_MASK ||
688       event_mask & GDK_BUTTON2_MOTION_MASK ||
689       event_mask & GDK_BUTTON3_MOTION_MASK)
690     {
691       XISetMask (mask, XI_ButtonPress);
692       XISetMask (mask, XI_ButtonRelease);
693       XISetMask (mask, XI_Motion);
694     }
695
696   if (event_mask & GDK_SCROLL_MASK)
697     {
698       XISetMask (mask, XI_ButtonPress);
699       XISetMask (mask, XI_ButtonRelease);
700     }
701
702   if (event_mask & GDK_BUTTON_PRESS_MASK)
703     XISetMask (mask, XI_ButtonPress);
704
705   if (event_mask & GDK_BUTTON_RELEASE_MASK)
706     XISetMask (mask, XI_ButtonRelease);
707
708   if (event_mask & GDK_KEY_PRESS_MASK)
709     XISetMask (mask, XI_KeyPress);
710
711   if (event_mask & GDK_KEY_RELEASE_MASK)
712     XISetMask (mask, XI_KeyRelease);
713
714   if (event_mask & GDK_ENTER_NOTIFY_MASK)
715     XISetMask (mask, XI_Enter);
716
717   if (event_mask & GDK_LEAVE_NOTIFY_MASK)
718     XISetMask (mask, XI_Leave);
719
720   if (event_mask & GDK_FOCUS_CHANGE_MASK)
721     {
722       XISetMask (mask, XI_FocusIn);
723       XISetMask (mask, XI_FocusOut);
724     }
725
726 #ifdef XINPUT_2_2
727   /* XInput 2.2 includes multitouch support */
728   if (minor >= 2 &&
729       event_mask & GDK_TOUCH_MASK)
730     {
731       XISetMask (mask, XI_TouchBegin);
732       XISetMask (mask, XI_TouchUpdate);
733       XISetMask (mask, XI_TouchEnd);
734     }
735 #endif /* XINPUT_2_2 */
736
737   return mask;
738 }
739
740 guint
741 _gdk_x11_device_xi2_translate_state (XIModifierState *mods_state,
742                                      XIButtonState   *buttons_state,
743                                      XIGroupState    *group_state)
744 {
745   guint state = 0;
746
747   if (mods_state)
748     state = mods_state->effective;
749
750   if (buttons_state)
751     {
752       gint len, i;
753
754       /* We're only interested in the first 5 buttons */
755       len = MIN (5, buttons_state->mask_len * 8);
756
757       for (i = 0; i < len; i++)
758         {
759           if (!XIMaskIsSet (buttons_state->mask, i))
760             continue;
761
762           switch (i)
763             {
764             case 1:
765               state |= GDK_BUTTON1_MASK;
766               break;
767             case 2:
768               state |= GDK_BUTTON2_MASK;
769               break;
770             case 3:
771               state |= GDK_BUTTON3_MASK;
772               break;
773             case 4:
774               state |= GDK_BUTTON4_MASK;
775               break;
776             case 5:
777               state |= GDK_BUTTON5_MASK;
778               break;
779             default:
780               break;
781             }
782         }
783     }
784
785   if (group_state)
786     state |= (group_state->effective) << 13;
787
788   return state;
789 }
790
791 void
792 _gdk_x11_device_xi2_add_scroll_valuator (GdkX11DeviceXI2    *device,
793                                          guint               n_valuator,
794                                          GdkScrollDirection  direction)
795 {
796   ScrollValuator scroll;
797
798   g_return_if_fail (GDK_IS_X11_DEVICE_XI2 (device));
799   g_return_if_fail (n_valuator < gdk_device_get_n_axes (GDK_DEVICE (device)));
800
801   scroll.n_valuator = n_valuator;
802   scroll.direction = direction;
803   scroll.last_value_valid = FALSE;
804
805   g_array_append_val (device->scroll_valuators, scroll);
806 }
807
808 gboolean
809 _gdk_x11_device_xi2_get_scroll_delta (GdkX11DeviceXI2    *device,
810                                       guint               n_valuator,
811                                       gdouble             valuator_value,
812                                       GdkScrollDirection *direction_ret,
813                                       gdouble            *delta_ret)
814 {
815   guint i;
816
817   g_return_val_if_fail (GDK_IS_X11_DEVICE_XI2 (device), FALSE);
818   g_return_val_if_fail (n_valuator < gdk_device_get_n_axes (GDK_DEVICE (device)), FALSE);
819
820   for (i = 0; i < device->scroll_valuators->len; i++)
821     {
822       ScrollValuator *scroll;
823
824       scroll = &g_array_index (device->scroll_valuators, ScrollValuator, i);
825
826       if (scroll->n_valuator == n_valuator)
827         {
828           if (direction_ret)
829             *direction_ret = scroll->direction;
830
831           if (delta_ret)
832             *delta_ret = 0;
833
834           if (scroll->last_value_valid)
835             {
836               if (delta_ret)
837                 *delta_ret = valuator_value - scroll->last_value;
838
839               scroll->last_value = valuator_value;
840             }
841           else
842             {
843               scroll->last_value = valuator_value;
844               scroll->last_value_valid = TRUE;
845             }
846
847           return TRUE;
848         }
849     }
850
851   return FALSE;
852 }
853
854 void
855 _gdk_device_xi2_reset_scroll_valuators (GdkX11DeviceXI2 *device)
856 {
857   guint i;
858
859   for (i = 0; i < device->scroll_valuators->len; i++)
860     {
861       ScrollValuator *scroll;
862
863       scroll = &g_array_index (device->scroll_valuators, ScrollValuator, i);
864       scroll->last_value_valid = FALSE;
865     }
866 }
867
868 gint
869 _gdk_x11_device_xi2_get_id (GdkX11DeviceXI2 *device)
870 {
871   g_return_val_if_fail (GDK_IS_X11_DEVICE_XI2 (device), 0);
872
873   return device->device_id;
874 }