]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdevice-xi.c
b82ff18002d091db263b2ec6d7ea8f4c6006a058
[~andy/gtk] / gdk / x11 / gdkdevice-xi.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 <gdk/gdkwindow.h>
23 #include "gdkdeviceprivate.h"
24 #include "gdkdevice-xi.h"
25 #include "gdkprivate-x11.h"
26 #include "gdkintl.h"
27 #include "gdkx.h"
28
29 #define MAX_DEVICE_CLASSES 13
30
31 static GQuark quark_window_input_info = 0;
32
33 typedef struct
34 {
35   gdouble root_x;
36   gdouble root_y;
37 } GdkWindowInputInfo;
38
39 static void gdk_device_xi_constructed  (GObject *object);
40 static void gdk_device_xi_dispose      (GObject *object);
41
42 static void gdk_device_xi_set_property (GObject      *object,
43                                         guint         prop_id,
44                                         const GValue *value,
45                                         GParamSpec   *pspec);
46 static void gdk_device_xi_get_property (GObject      *object,
47                                         guint         prop_id,
48                                         GValue       *value,
49                                         GParamSpec   *pspec);
50
51 static gboolean gdk_device_xi_get_history (GdkDevice      *device,
52                                            GdkWindow      *window,
53                                            guint32         start,
54                                            guint32         stop,
55                                            GdkTimeCoord ***events,
56                                            guint          *n_events);
57
58 static void gdk_device_xi_get_state       (GdkDevice       *device,
59                                            GdkWindow       *window,
60                                            gdouble         *axes,
61                                            GdkModifierType *mask);
62 static void gdk_device_xi_set_window_cursor (GdkDevice *device,
63                                              GdkWindow *window,
64                                              GdkCursor *cursor);
65 static void gdk_device_xi_warp              (GdkDevice *device,
66                                              GdkScreen *screen,
67                                              gint       x,
68                                              gint       y);
69 static gboolean gdk_device_xi_query_state   (GdkDevice        *device,
70                                              GdkWindow        *window,
71                                              GdkWindow       **root_window,
72                                              GdkWindow       **child_window,
73                                              gint             *root_x,
74                                              gint             *root_y,
75                                              gint             *win_x,
76                                              gint             *win_y,
77                                              GdkModifierType  *mask);
78 static GdkGrabStatus gdk_device_xi_grab     (GdkDevice    *device,
79                                              GdkWindow    *window,
80                                              gboolean      owner_events,
81                                              GdkEventMask  event_mask,
82                                              GdkWindow    *confine_to,
83                                              GdkCursor    *cursor,
84                                              guint32       time_);
85 static void          gdk_device_xi_ungrab   (GdkDevice    *device,
86                                              guint32       time_);
87
88 static GdkWindow* gdk_device_xi_window_at_position (GdkDevice       *device,
89                                                     gint            *win_x,
90                                                     gint            *win_y,
91                                                     GdkModifierType *mask,
92                                                     gboolean         get_toplevel);
93
94 static void gdk_device_xi_select_window_events (GdkDevice    *device,
95                                                 GdkWindow    *window,
96                                                 GdkEventMask  mask);
97
98
99 G_DEFINE_TYPE (GdkDeviceXI, gdk_device_xi, GDK_TYPE_DEVICE)
100
101 enum {
102   PROP_0,
103   PROP_DEVICE_ID
104 };
105
106 static void
107 gdk_device_xi_class_init (GdkDeviceXIClass *klass)
108 {
109   GObjectClass *object_class = G_OBJECT_CLASS (klass);
110   GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
111
112   quark_window_input_info = g_quark_from_static_string ("gdk-window-input-info");
113
114   object_class->constructed = gdk_device_xi_constructed;
115   object_class->set_property = gdk_device_xi_set_property;
116   object_class->get_property = gdk_device_xi_get_property;
117   object_class->dispose = gdk_device_xi_dispose;
118
119   device_class->get_history = gdk_device_xi_get_history;
120   device_class->get_state = gdk_device_xi_get_state;
121   device_class->set_window_cursor = gdk_device_xi_set_window_cursor;
122   device_class->warp = gdk_device_xi_warp;
123   device_class->query_state = gdk_device_xi_query_state;
124   device_class->grab = gdk_device_xi_grab;
125   device_class->ungrab = gdk_device_xi_ungrab;
126   device_class->window_at_position = gdk_device_xi_window_at_position;
127   device_class->select_window_events = gdk_device_xi_select_window_events;
128
129   g_object_class_install_property (object_class,
130                                    PROP_DEVICE_ID,
131                                    g_param_spec_int ("device-id",
132                                                      P_("Device ID"),
133                                                      P_("Device ID"),
134                                                      0, G_MAXINT, 0,
135                                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
136 }
137
138 static void
139 gdk_device_xi_init (GdkDeviceXI *device)
140 {
141 }
142
143 static void
144 gdk_device_xi_constructed (GObject *object)
145 {
146   GdkDeviceXI *device;
147   GdkDisplay *display;
148
149   device = GDK_DEVICE_XI (object);
150   display = gdk_device_get_display (GDK_DEVICE (object));
151
152   gdk_error_trap_push ();
153   device->xdevice = XOpenDevice (GDK_DISPLAY_XDISPLAY (display),
154                                  device->device_id);
155
156   if (gdk_error_trap_pop ())
157     g_warning ("Device %s can't be opened", GDK_DEVICE (device)->name);
158
159   if (G_OBJECT_CLASS (gdk_device_xi_parent_class)->constructed)
160     G_OBJECT_CLASS (gdk_device_xi_parent_class)->constructed (object);
161 }
162
163 static void
164 gdk_device_xi_set_property (GObject      *object,
165                             guint         prop_id,
166                             const GValue *value,
167                             GParamSpec   *pspec)
168 {
169   GdkDeviceXI *device = GDK_DEVICE_XI (object);
170
171   switch (prop_id)
172     {
173     case PROP_DEVICE_ID:
174       device->device_id = g_value_get_int (value);
175       break;
176     default:
177       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178       break;
179     }
180 }
181
182 static void
183 gdk_device_xi_get_property (GObject    *object,
184                             guint       prop_id,
185                             GValue     *value,
186                             GParamSpec *pspec)
187 {
188   GdkDeviceXI *device = GDK_DEVICE_XI (object);
189
190   switch (prop_id)
191     {
192     case PROP_DEVICE_ID:
193       g_value_set_int (value, device->device_id);
194       break;
195     default:
196       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
197       break;
198     }
199 }
200
201 static void
202 gdk_device_xi_dispose (GObject *object)
203 {
204   GdkDeviceXI *device_xi;
205   GdkDisplay *display;
206
207   device_xi = GDK_DEVICE_XI (object);
208   display = gdk_device_get_display (GDK_DEVICE (device_xi));
209
210   if (device_xi->xdevice)
211     {
212       XCloseDevice (GDK_DISPLAY_XDISPLAY (display), device_xi->xdevice);
213       device_xi->xdevice = NULL;
214     }
215
216   if (device_xi->axis_data)
217     {
218       g_free (device_xi->axis_data);
219       device_xi->axis_data = NULL;
220     }
221
222   G_OBJECT_CLASS (gdk_device_xi_parent_class)->dispose (object);
223 }
224
225 static gboolean
226 gdk_device_xi_get_history (GdkDevice      *device,
227                            GdkWindow      *window,
228                            guint32         start,
229                            guint32         stop,
230                            GdkTimeCoord ***events,
231                            guint          *n_events)
232 {
233   GdkTimeCoord **coords;
234   XDeviceTimeCoord *device_coords;
235   GdkWindow *impl_window;
236   GdkDeviceXI *device_xi;
237   gint n_events_return;
238   gint mode_return;
239   gint axis_count_return;
240   gint i;
241
242   device_xi = GDK_DEVICE_XI (device);
243   impl_window = _gdk_window_get_impl_window (window);
244
245   device_coords = XGetDeviceMotionEvents (GDK_WINDOW_XDISPLAY (impl_window),
246                                           device_xi->xdevice,
247                                           start, stop,
248                                           &n_events_return,
249                                           &mode_return,
250                                           &axis_count_return);
251
252   if (!device_coords)
253     return FALSE;
254
255   *n_events = (guint) n_events_return;
256   coords = _gdk_device_allocate_history (device, *n_events);
257
258   for (i = 0; i < *n_events; i++)
259     {
260       coords[i]->time = device_coords[i].time;
261       gdk_device_xi_translate_axes (device, window,
262                                     device_coords[i].data,
263                                     coords[i]->axes,
264                                     NULL, NULL);
265     }
266
267   XFreeDeviceMotionEvents (device_coords);
268
269   *events = coords;
270
271   return TRUE;
272 }
273
274 static void
275 gdk_device_xi_get_state (GdkDevice       *device,
276                          GdkWindow       *window,
277                          gdouble         *axes,
278                          GdkModifierType *mask)
279 {
280   GdkDeviceXI *device_xi;
281   XDeviceState *state;
282   XInputClass *input_class;
283   gint i;
284
285   if (mask)
286     gdk_window_get_pointer (window, NULL, NULL, mask);
287
288   device_xi = GDK_DEVICE_XI (device);
289   state = XQueryDeviceState (GDK_WINDOW_XDISPLAY (window),
290                              device_xi->xdevice);
291   input_class = state->data;
292
293   for (i = 0; i < state->num_classes; i++)
294     {
295       switch (input_class->class)
296         {
297         case ValuatorClass:
298           if (axes)
299             gdk_device_xi_translate_axes (device, window,
300                                           ((XValuatorState *) input_class)->valuators,
301                                           axes, NULL, NULL);
302           break;
303
304         case ButtonClass:
305           if (mask)
306             {
307               *mask &= 0xFF;
308               if (((XButtonState *)input_class)->num_buttons > 0)
309                 *mask |= ((XButtonState *)input_class)->buttons[0] << 7;
310               /* GDK_BUTTON1_MASK = 1 << 8, and button n is stored
311                * in bit 1<<(n%8) in byte n/8. n = 1,2,... */
312             }
313           break;
314         }
315
316       input_class = (XInputClass *)(((char *)input_class)+input_class->length);
317     }
318
319   XFreeDeviceState (state);
320 }
321
322 static void
323 gdk_device_xi_set_window_cursor (GdkDevice *device,
324                                  GdkWindow *window,
325                                  GdkCursor *cursor)
326 {
327 }
328
329 static void
330 gdk_device_xi_warp (GdkDevice *device,
331                     GdkScreen *screen,
332                     gint       x,
333                     gint       y)
334 {
335 }
336
337 static void
338 find_events (GdkDevice    *device,
339              GdkEventMask  mask,
340              XEventClass  *classes,
341              int          *num_classes)
342 {
343   GdkDeviceXI *device_xi;
344   XEventClass class;
345   gint i;
346
347   device_xi = GDK_DEVICE_XI (device);
348   i = 0;
349
350   if (mask & GDK_BUTTON_PRESS_MASK)
351     {
352       DeviceButtonPress (device_xi->xdevice, device_xi->button_press_type, class);
353       if (class != 0)
354         classes[i++] = class;
355
356       DeviceButtonPressGrab (device_xi->xdevice, 0, class);
357       if (class != 0)
358         classes[i++] = class;
359     }
360
361   if (mask & GDK_BUTTON_RELEASE_MASK)
362     {
363       DeviceButtonRelease (device_xi->xdevice, device_xi->button_release_type, class);
364       if (class != 0)
365         classes[i++] = class;
366     }
367
368   if (mask & (GDK_POINTER_MOTION_MASK |
369               GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
370               GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_MOTION_MASK))
371     {
372       /* Make sure device->motionnotify_type is set */
373       DeviceMotionNotify (device_xi->xdevice, device_xi->motion_notify_type, class);
374       if (class != 0)
375           classes[i++] = class;
376       DeviceStateNotify (device_xi->xdevice, device_xi->state_notify_type, class);
377       if (class != 0)
378           classes[i++] = class;
379     }
380
381   if (mask & GDK_KEY_PRESS_MASK)
382     {
383       DeviceKeyPress (device_xi->xdevice, device_xi->key_press_type, class);
384       if (class != 0)
385         classes[i++] = class;
386     }
387
388   if (mask & GDK_KEY_RELEASE_MASK)
389     {
390       DeviceKeyRelease (device_xi->xdevice, device_xi->key_release_type, class);
391       if (class != 0)
392         classes[i++] = class;
393     }
394
395   if (mask & GDK_PROXIMITY_IN_MASK)
396     {
397       ProximityIn (device_xi->xdevice, device_xi->proximity_in_type, class);
398       if (class != 0)
399         classes[i++] = class;
400     }
401
402   if (mask & GDK_PROXIMITY_OUT_MASK)
403     {
404       ProximityOut (device_xi->xdevice, device_xi->proximity_out_type, class);
405       if (class != 0)
406         classes[i++] = class;
407     }
408
409   *num_classes = i;
410 }
411
412 static gboolean
413 gdk_device_xi_query_state (GdkDevice        *device,
414                            GdkWindow        *window,
415                            GdkWindow       **root_window,
416                            GdkWindow       **child_window,
417                            gint             *root_x,
418                            gint             *root_y,
419                            gint             *win_x,
420                            gint             *win_y,
421                            GdkModifierType  *mask)
422 {
423   return FALSE;
424 }
425
426 static GdkGrabStatus
427 gdk_device_xi_grab (GdkDevice    *device,
428                     GdkWindow    *window,
429                     gboolean      owner_events,
430                     GdkEventMask  event_mask,
431                     GdkWindow    *confine_to,
432                     GdkCursor    *cursor,
433                     guint32       time_)
434 {
435   XEventClass event_classes[MAX_DEVICE_CLASSES];
436   gint status, num_classes;
437   GdkDeviceXI *device_xi;
438
439   device_xi = GDK_DEVICE_XI (device);
440   find_events (device, event_mask, event_classes, &num_classes);
441
442   status = XGrabDevice (GDK_WINDOW_XDISPLAY (window),
443                         device_xi->xdevice,
444                         GDK_WINDOW_XWINDOW (window),
445                         owner_events,
446                         num_classes, event_classes,
447                         GrabModeAsync, GrabModeAsync,
448                         time_);
449
450   return _gdk_x11_convert_grab_status (status);
451 }
452
453 static void
454 gdk_device_xi_ungrab (GdkDevice *device,
455                       guint32    time_)
456 {
457   GdkDisplay *display;
458   GdkDeviceXI *device_xi;
459
460   device_xi = GDK_DEVICE_XI (device);
461   display = gdk_device_get_display (device);
462
463   XUngrabDevice (GDK_DISPLAY_XDISPLAY (device),
464                  device_xi->xdevice,
465                  time_);
466 }
467
468 static GdkWindow*
469 gdk_device_xi_window_at_position (GdkDevice       *device,
470                                   gint            *win_x,
471                                   gint            *win_y,
472                                   GdkModifierType *mask,
473                                   gboolean         get_toplevel)
474 {
475   return NULL;
476 }
477 static void
478 gdk_device_xi_select_window_events (GdkDevice    *device,
479                                     GdkWindow    *window,
480                                     GdkEventMask  event_mask)
481 {
482   XEventClass event_classes[MAX_DEVICE_CLASSES];
483   GdkDeviceXI *device_xi;
484   gint num_classes;
485
486   event_mask |= (GDK_PROXIMITY_IN_MASK |
487                  GDK_PROXIMITY_OUT_MASK);
488
489   device_xi = GDK_DEVICE_XI (device);
490   find_events (device, event_mask, event_classes, &num_classes);
491
492   XSelectExtensionEvent (GDK_WINDOW_XDISPLAY (window),
493                          GDK_WINDOW_XWINDOW (window),
494                          event_classes, num_classes);
495
496   if (event_mask)
497     {
498       GdkWindowInputInfo *info;
499
500       info = g_new0 (GdkWindowInputInfo, 1);
501       g_object_set_qdata_full (G_OBJECT (window),
502                                quark_window_input_info,
503                                info,
504                                (GDestroyNotify) g_free);
505     }
506   else
507     g_object_set_qdata (G_OBJECT (window),
508                         quark_window_input_info,
509                         NULL);
510 }
511
512 void
513 gdk_device_xi_update_window_info (GdkWindow *window)
514 {
515   GdkWindowInputInfo *info;
516   gint root_x, root_y;
517
518   info = g_object_get_qdata (G_OBJECT (window),
519                              quark_window_input_info);
520
521   if (!info)
522     return;
523
524   gdk_window_get_origin (window, &root_x, &root_y);
525   info->root_x = (gdouble) root_x;
526   info->root_y = (gdouble) root_y;
527 }
528
529 static gboolean
530 gdk_device_xi_get_window_info (GdkWindow *window,
531                                gdouble   *root_x,
532                                gdouble   *root_y)
533 {
534   GdkWindowInputInfo *info;
535
536   info = g_object_get_qdata (G_OBJECT (window),
537                              quark_window_input_info);
538
539   if (!info)
540     return FALSE;
541
542   *root_x = info->root_x;
543   *root_y = info->root_y;
544
545   return TRUE;
546 }
547
548 void
549 gdk_device_xi_update_axes (GdkDevice *device,
550                            gint       axes_count,
551                            gint       first_axis,
552                            gint      *axis_data)
553 {
554   GdkDeviceXI *device_xi;
555   int i;
556
557   device_xi = GDK_DEVICE_XI (device);
558   g_return_if_fail (first_axis >= 0 && first_axis + axes_count <= device->num_axes);
559
560   if (!device_xi->axis_data)
561     device_xi->axis_data = g_new0 (gint, device->num_axes);
562
563   for (i = 0; i < axes_count; i++)
564     device_xi->axis_data[first_axis + i] = axis_data[i];
565 }
566
567 void
568 gdk_device_xi_translate_axes (GdkDevice *device,
569                               GdkWindow *window,
570                               gint      *axis_data,
571                               gdouble   *axes,
572                               gdouble   *x,
573                               gdouble   *y)
574 {
575   GdkDeviceXI *device_xi;
576   GdkWindow *impl_window;
577   gdouble root_x, root_y;
578   gdouble temp_x, temp_y;
579   gint i;
580
581   device_xi = GDK_DEVICE_XI (device);
582   impl_window = _gdk_window_get_impl_window (window);
583   temp_x = temp_y = 0;
584
585   if (!gdk_device_xi_get_window_info (impl_window, &root_x, &root_y))
586     return;
587
588   for (i = 0; i < device->num_axes; i++)
589     {
590       GdkAxisUse use;
591
592       use = _gdk_device_get_axis_use (device, i);
593
594       switch (use)
595         {
596         case GDK_AXIS_X:
597         case GDK_AXIS_Y:
598           if (device->mode == GDK_MODE_WINDOW)
599             _gdk_device_translate_window_coord (device, window,
600                                                 i, axis_data[i],
601                                                 &axes[i]);
602           else
603             _gdk_device_translate_screen_coord (device, window,
604                                                 root_x, root_y,
605                                                 i, axis_data[i],
606                                                 &axes[i]);
607           if (use == GDK_AXIS_X)
608             temp_x = axes[i];
609           else if (use == GDK_AXIS_Y)
610             temp_y = axes[i];
611
612           break;
613         default:
614           _gdk_device_translate_axis (device, i, axis_data[i], &axes[i]);
615           break;
616         }
617     }
618
619   if (x)
620     *x = temp_x;
621
622   if (y)
623     *y = temp_y;
624 }