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