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