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