]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkinput-x11.c
db716fa9f92d2d4368022b80def77469197e4cd9
[~andy/gtk] / gdk / x11 / gdkinput-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "gdkinputprivate.h"
28 #include "gdkinternals.h"
29 #include "gdkx.h"
30 #include "gdk.h"                /* For gdk_error_trap_push()/pop() */
31 #include "gdkdisplay-x11.h"
32
33 #include <string.h>
34
35 /* Forward declarations */
36 static GdkDevicePrivate *gdk_input_device_new            (GdkDisplay       *display,
37                                                           XDeviceInfo      *device,
38                                                           gint              include_core);
39 static void              gdk_input_translate_coordinates (GdkDevicePrivate *gdkdev,
40                                                           GdkInputWindow   *input_window,
41                                                           gint             *axis_data,
42                                                           gdouble          *axis_out,
43                                                           gdouble          *x_out,
44                                                           gdouble          *y_out);
45 static guint             gdk_input_translate_state       (guint             state,
46                                                           guint             device_state);
47
48 GdkDevicePrivate *
49 _gdk_input_find_device (GdkDisplay *display,
50                         guint32     id)
51 {
52   GList *tmp_list = GDK_DISPLAY_X11 (display)->input_devices;
53   GdkDevicePrivate *gdkdev;
54   while (tmp_list)
55     {
56       gdkdev = (GdkDevicePrivate *)(tmp_list->data);
57       if (gdkdev->deviceid == id)
58         return gdkdev;
59       tmp_list = tmp_list->next;
60     }
61   return NULL;
62 }
63
64 void
65 _gdk_input_get_root_relative_geometry(Display *display, Window w, int *x_ret, int *y_ret,
66                                       int *width_ret, int *height_ret)
67 {
68   Window root, parent, child;
69   Window *children;
70   guint nchildren;
71   gint x,y;
72   guint width, height;
73   guint border_widthc, depthc;
74    
75   XQueryTree (display, w, &root, &parent, &children, &nchildren);
76   if (children)
77     XFree(children);
78   
79   XGetGeometry (display, w, &root, &x, &y, &width, &height, &border_widthc, &depthc);
80
81   XTranslateCoordinates (display, w, root, 0, 0, &x, &y, &child);
82  
83   if (x_ret)
84     *x_ret = x;
85   if (y_ret)
86     *y_ret = y;
87   if (width_ret)
88     *width_ret = width;
89   if (height_ret)
90     *height_ret = height;
91 }
92
93 static GdkDevicePrivate *
94 gdk_input_device_new (GdkDisplay  *display,
95                       XDeviceInfo *device, 
96                       gint         include_core)
97 {
98   GdkDevicePrivate *gdkdev;
99   gchar *tmp_name;
100   XAnyClassPtr class;
101   gint i,j;
102
103   gdkdev = g_object_new (GDK_TYPE_DEVICE, NULL);
104
105   gdkdev->deviceid = device->id;
106   gdkdev->display = display;
107
108   if (device->name[0])
109     gdkdev->info.name = g_strdup (device->name);
110  else
111    /* XFree86 3.2 gives an empty name to the default core devices,
112       (fixed in 3.2A) */
113    gdkdev->info.name = g_strdup ("pointer");
114
115   gdkdev->info.mode = GDK_MODE_DISABLED;
116
117   /* Try to figure out what kind of device this is by its name -
118      could invite a very, very, long list... Lowercase name
119      for comparison purposes */
120
121   tmp_name = g_ascii_strdown (gdkdev->info.name, -1);
122   
123   if (!strcmp (tmp_name, "pointer"))
124     gdkdev->info.source = GDK_SOURCE_MOUSE;
125   else if (!strcmp (tmp_name, "wacom") ||
126            !strcmp (tmp_name, "pen"))
127     gdkdev->info.source = GDK_SOURCE_PEN;
128   else if (!strcmp (tmp_name, "eraser"))
129     gdkdev->info.source = GDK_SOURCE_ERASER;
130   else if (!strcmp (tmp_name, "cursor"))
131     gdkdev->info.source = GDK_SOURCE_CURSOR;
132   else
133     gdkdev->info.source = GDK_SOURCE_PEN;
134
135   g_free(tmp_name);
136
137   gdkdev->xdevice = NULL;
138
139   /* step through the classes */
140
141   gdkdev->info.num_axes = 0;
142   gdkdev->info.num_keys = 0;
143   gdkdev->info.axes = NULL;
144   gdkdev->info.keys = NULL;
145   gdkdev->axes = 0;
146   gdkdev->info.has_cursor = 0;
147   gdkdev->needs_update = FALSE;
148   gdkdev->claimed = FALSE;
149   gdkdev->button_state = 0;
150
151   class = device->inputclassinfo;
152   for (i=0;i<device->num_classes;i++) 
153     {
154       switch (class->class) {
155       case ButtonClass:
156         break;
157       case KeyClass:
158         {
159           XKeyInfo *xki = (XKeyInfo *)class;
160           /* Hack to catch XFree86 3.3.1 bug. Other devices better
161            * not have exactly 25 keys... 
162            */
163           if ((xki->min_keycode == 8) && (xki->max_keycode == 32))
164             {
165               gdkdev->info.num_keys = 32;
166               gdkdev->min_keycode = 1;
167             }
168           else
169             {
170               gdkdev->info.num_keys = xki->max_keycode - xki->min_keycode + 1;
171               gdkdev->min_keycode = xki->min_keycode;
172             }
173           gdkdev->info.keys = g_new (GdkDeviceKey, gdkdev->info.num_keys);
174
175           for (j=0; j<gdkdev->info.num_keys; j++)
176             {
177               gdkdev->info.keys[j].keyval = 0;
178               gdkdev->info.keys[j].modifiers = 0;
179             }
180
181           break;
182         }
183       case ValuatorClass:
184         {
185           XValuatorInfo *xvi = (XValuatorInfo *)class;
186           gdkdev->info.num_axes = xvi->num_axes;
187           gdkdev->axes = g_new (GdkAxisInfo, xvi->num_axes);
188           gdkdev->info.axes = g_new0 (GdkDeviceAxis, xvi->num_axes);
189           for (j=0;j<xvi->num_axes;j++)
190             {
191               gdkdev->axes[j].resolution = 
192                 gdkdev->axes[j].xresolution = xvi->axes[j].resolution;
193               gdkdev->axes[j].min_value =
194                 gdkdev->axes[j].xmin_value = xvi->axes[j].min_value;
195               gdkdev->axes[j].max_value =
196                 gdkdev->axes[j].xmax_value = xvi->axes[j].max_value;
197               gdkdev->info.axes[j].use = GDK_AXIS_IGNORE;
198             }
199           j=0;
200           if (j<xvi->num_axes)
201             gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_X);
202           if (j<xvi->num_axes)
203             gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_Y);
204           if (j<xvi->num_axes)
205             gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_PRESSURE);
206           if (j<xvi->num_axes)
207             gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_XTILT);
208           if (j<xvi->num_axes)
209             gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_YTILT);
210           if (j<xvi->num_axes)
211             gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_WHEEL);
212                        
213           break;
214         }
215       }
216       class = (XAnyClassPtr)(((char *)class) + class->length);
217     }
218   /* return NULL if no axes */
219   if (!gdkdev->info.num_axes || !gdkdev->axes ||
220       (!include_core && device->use == IsXPointer))
221     goto error;
222
223   if (device->use != IsXPointer)
224     {
225       gdk_error_trap_push ();
226       gdkdev->xdevice = XOpenDevice (GDK_DISPLAY_XDISPLAY (display),
227                                      gdkdev->deviceid);
228
229       /* return NULL if device is not ready */
230       if (gdk_error_trap_pop ())
231         goto error;
232     }
233
234   gdkdev->buttonpress_type = 0;
235   gdkdev->buttonrelease_type = 0;
236   gdkdev->keypress_type = 0;
237   gdkdev->keyrelease_type = 0;
238   gdkdev->motionnotify_type = 0;
239   gdkdev->proximityin_type = 0;
240   gdkdev->proximityout_type = 0;
241   gdkdev->changenotify_type = 0;
242
243   return gdkdev;
244
245  error:
246
247   g_free (gdkdev->info.name);
248   if (gdkdev->axes)
249     g_free (gdkdev->axes);
250   if (gdkdev->info.keys)
251     g_free (gdkdev->info.keys);
252   if (gdkdev->info.axes)
253     g_free (gdkdev->info.axes);
254   g_object_unref (gdkdev);
255   
256   return NULL;
257 }
258
259 void
260 _gdk_input_common_find_events(GdkWindow *window,
261                               GdkDevicePrivate *gdkdev,
262                               gint mask,
263                               XEventClass *classes,
264                               int *num_classes)
265 {
266   gint i;
267   XEventClass class;
268   
269   i = 0;
270   /* We have to track press and release events in pairs to keep
271      track of button state correctly and implement grabbing for
272      the gxi support. FIXME - is this needed any more since gxi is gone? */
273   if (mask & GDK_BUTTON_PRESS_MASK || mask & GDK_BUTTON_RELEASE_MASK)
274     {
275       DeviceButtonPress (gdkdev->xdevice, gdkdev->buttonpress_type,
276                              class);
277       if (class != 0)
278           classes[i++] = class;
279       DeviceButtonPressGrab (gdkdev->xdevice, 0, class);
280       if (class != 0)
281           classes[i++] = class;
282       DeviceButtonRelease (gdkdev->xdevice, gdkdev->buttonrelease_type,
283                            class);
284       if (class != 0)
285           classes[i++] = class;
286     }
287   if (mask & GDK_POINTER_MOTION_MASK)
288     {
289       DeviceMotionNotify  (gdkdev->xdevice, gdkdev->motionnotify_type, class);
290       if (class != 0)
291           classes[i++] = class;
292     }
293   else
294     if (mask & (GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
295                 GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_MOTION_MASK |
296                 GDK_POINTER_MOTION_HINT_MASK))
297       {
298         /* Make sure gdkdev->motionnotify_type is set */
299         DeviceMotionNotify  (gdkdev->xdevice, gdkdev->motionnotify_type, class);
300       }
301   if (mask & GDK_BUTTON1_MOTION_MASK)
302     {
303       DeviceButton1Motion  (gdkdev->xdevice, 0, class);
304       if (class != 0)
305           classes[i++] = class;
306     }
307   if (mask & GDK_BUTTON2_MOTION_MASK)
308     {
309       DeviceButton2Motion  (gdkdev->xdevice, 0, class);
310       if (class != 0)
311           classes[i++] = class;
312     }
313   if (mask & GDK_BUTTON3_MOTION_MASK)
314     {
315       DeviceButton3Motion  (gdkdev->xdevice, 0, class);
316       if (class != 0)
317           classes[i++] = class;
318     }
319   if (mask & GDK_BUTTON_MOTION_MASK)
320     {
321       DeviceButtonMotion  (gdkdev->xdevice, 0, class);
322       if (class != 0)
323           classes[i++] = class;
324     }
325   if (mask & GDK_POINTER_MOTION_HINT_MASK)
326     {
327       /* We'll get into trouble if the macros change, but at least we'll
328          know about it, and we avoid warnings now */
329       DevicePointerMotionHint (gdkdev->xdevice, 0, class);
330       if (class != 0)
331           classes[i++] = class;
332     }
333   if (mask & GDK_KEY_PRESS_MASK)
334     {
335       DeviceKeyPress (gdkdev->xdevice, gdkdev->keypress_type, class);
336       if (class != 0)
337           classes[i++] = class;
338     }
339   if (mask & GDK_KEY_RELEASE_MASK)
340     {
341       DeviceKeyRelease (gdkdev->xdevice, gdkdev->keyrelease_type, class);
342       if (class != 0)
343           classes[i++] = class;
344     }
345   if (mask & GDK_PROXIMITY_IN_MASK)
346     {
347       ProximityIn   (gdkdev->xdevice, gdkdev->proximityin_type, class);
348       if (class != 0)
349           classes[i++] = class;
350     }
351   if (mask & GDK_PROXIMITY_OUT_MASK)
352     {
353       ProximityOut  (gdkdev->xdevice, gdkdev->proximityout_type, class);
354       if (class != 0)
355           classes[i++] = class;
356     }
357
358   *num_classes = i;
359 }
360
361 void
362 _gdk_input_common_select_events(GdkWindow *window,
363                                 GdkDevicePrivate *gdkdev)
364 {
365   XEventClass classes[GDK_MAX_DEVICE_CLASSES];
366   gint num_classes;
367
368   if (gdkdev->info.mode == GDK_MODE_DISABLED)
369     _gdk_input_common_find_events(window, gdkdev, 0, classes, &num_classes);
370   else
371     _gdk_input_common_find_events(window, gdkdev, 
372                                   ((GdkWindowObject *)window)->extension_events,
373                                   classes, &num_classes);
374   
375   XSelectExtensionEvent (GDK_WINDOW_XDISPLAY (window),
376                          GDK_WINDOW_XWINDOW (window),
377                          classes, num_classes);
378 }
379
380 gint 
381 _gdk_input_common_init (GdkDisplay *display,
382                         gint        include_core)
383 {
384   char **extensions;
385   XDeviceInfo   *devices;
386   int num_devices;
387   int num_extensions, loop;
388   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
389
390   /* Init XInput extension */
391   
392   extensions = XListExtensions(display_x11->xdisplay, &num_extensions);
393   for (loop = 0; loop < num_extensions &&
394          (strcmp(extensions[loop], "XInputExtension") != 0); loop++);
395   XFreeExtensionList(extensions);
396   display_x11->input_devices = NULL;
397   if (loop < num_extensions)
398     {
399       /* XInput extension found */
400
401       devices = XListInputDevices(display_x11->xdisplay, &num_devices);
402   
403       for(loop=0; loop<num_devices; loop++)
404         {
405           GdkDevicePrivate *gdkdev = gdk_input_device_new(display,
406                                                           &devices[loop],
407                                                           include_core);
408           if (gdkdev)
409             display_x11->input_devices = g_list_append(display_x11->input_devices, gdkdev);
410         }
411       XFreeDeviceList(devices);
412     }
413
414   display_x11->input_devices = g_list_append (display_x11->input_devices, display->core_pointer);
415
416   return TRUE;
417 }
418
419 static void
420 gdk_input_translate_coordinates (GdkDevicePrivate *gdkdev,
421                                  GdkInputWindow   *input_window,
422                                  gint             *axis_data,
423                                  gdouble          *axis_out,
424                                  gdouble          *x_out,
425                                  gdouble          *y_out)
426 {
427   GdkWindowImplX11 *impl;
428   int i;
429   int x_axis = 0;
430   int y_axis = 0;
431
432   double device_width, device_height;
433   double x_offset, y_offset, x_scale, y_scale;
434
435   impl = GDK_WINDOW_IMPL_X11 (((GdkWindowObject *) input_window->window)->impl);
436
437   for (i=0; i<gdkdev->info.num_axes; i++)
438     {
439       switch (gdkdev->info.axes[i].use)
440         {
441         case GDK_AXIS_X:
442           x_axis = i;
443           break;
444         case GDK_AXIS_Y:
445           y_axis = i;
446           break;
447         default:
448           break;
449         }
450     }
451   
452   device_width = gdkdev->axes[x_axis].max_value - 
453                    gdkdev->axes[x_axis].min_value;
454   device_height = gdkdev->axes[y_axis].max_value - 
455                     gdkdev->axes[y_axis].min_value;
456
457   if (gdkdev->info.mode == GDK_MODE_SCREEN) 
458     {
459       x_scale = gdk_screen_get_width (gdk_drawable_get_screen (input_window->window)) / device_width;
460       y_scale = gdk_screen_get_height (gdk_drawable_get_screen (input_window->window)) / device_height;
461
462       x_offset = - input_window->root_x;
463       y_offset = - input_window->root_y;
464     }
465   else                          /* GDK_MODE_WINDOW */
466     {
467       double device_aspect = (device_height*gdkdev->axes[y_axis].resolution) /
468         (device_width*gdkdev->axes[x_axis].resolution);
469
470       if (device_aspect * impl->width >= impl->height)
471         {
472           /* device taller than window */
473           x_scale = impl->width / device_width;
474           y_scale = (x_scale * gdkdev->axes[x_axis].resolution)
475             / gdkdev->axes[y_axis].resolution;
476
477           x_offset = 0;
478           y_offset = -(device_height * y_scale - 
479                                impl->height)/2;
480         }
481       else
482         {
483           /* window taller than device */
484           y_scale = impl->height / device_height;
485           x_scale = (y_scale * gdkdev->axes[y_axis].resolution)
486             / gdkdev->axes[x_axis].resolution;
487
488           y_offset = 0;
489           x_offset = - (device_width * x_scale - impl->width)/2;
490         }
491     }
492
493   for (i=0; i<gdkdev->info.num_axes; i++)
494     {
495       switch (gdkdev->info.axes[i].use)
496         {
497         case GDK_AXIS_X:
498           axis_out[i] = x_offset + x_scale*axis_data[x_axis];
499           if (x_out)
500             *x_out = axis_out[i];
501           break;
502         case GDK_AXIS_Y:
503           axis_out[i] = y_offset + y_scale*axis_data[y_axis];
504           if (y_out)
505             *y_out = axis_out[i];
506           break;
507         default:
508           axis_out[i] =
509             (gdkdev->info.axes[i].max * (axis_data[i] - gdkdev->axes[i].min_value) +
510              gdkdev->info.axes[i].min * (gdkdev->axes[i].max_value - axis_data[i])) /
511             (gdkdev->axes[i].max_value - gdkdev->axes[i].min_value);
512           break;
513         }
514     }
515 }
516
517 /* combine the state of the core device and the device state
518  * into one - for now we do this in a simple-minded manner -
519  * we just take the keyboard portion of the core device and
520  * the button portion (all of?) the device state.
521  * Any button remapping should go on here.
522  */
523 static guint
524 gdk_input_translate_state(guint state, guint device_state)
525 {
526   return device_state | (state & 0xFF);
527 }
528
529
530 gint 
531 _gdk_input_common_other_event (GdkEvent         *event,
532                                XEvent           *xevent,
533                                GdkInputWindow   *input_window,
534                                GdkDevicePrivate *gdkdev)
535 {
536   if ((xevent->type == gdkdev->buttonpress_type) ||
537       (xevent->type == gdkdev->buttonrelease_type)) 
538     {
539       XDeviceButtonEvent *xdbe = (XDeviceButtonEvent *)(xevent);
540
541       if (xdbe->type == gdkdev->buttonpress_type)
542         {
543           event->button.type = GDK_BUTTON_PRESS;
544           gdkdev->button_state |= 1 << xdbe->button;
545         }
546       else
547         {
548           event->button.type = GDK_BUTTON_RELEASE;
549           gdkdev->button_state &= ~(1 << xdbe->button);
550         }
551       event->button.device = &gdkdev->info;
552       event->button.window = input_window->window;
553       event->button.time = xdbe->time;
554
555       event->button.axes = g_new (gdouble, gdkdev->info.num_axes);
556       gdk_input_translate_coordinates (gdkdev,input_window, xdbe->axis_data,
557                                        event->button.axes, 
558                                        &event->button.x,&event->button.y);
559       event->button.state = gdk_input_translate_state(xdbe->state,xdbe->device_state);
560       event->button.button = xdbe->button;
561
562       GDK_NOTE (EVENTS,
563         g_print ("button %s:\t\twindow: %ld  device: %ld  x,y: %f %f  button: %d\n",
564                  (event->button.type == GDK_BUTTON_PRESS) ? "press" : "release",
565                  xdbe->window,
566                  xdbe->deviceid,
567                  event->button.x, event->button.y,
568                  xdbe->button));
569
570       return TRUE;
571   }
572
573   if ((xevent->type == gdkdev->keypress_type) ||
574       (xevent->type == gdkdev->keyrelease_type))
575     {
576       XDeviceKeyEvent *xdke = (XDeviceKeyEvent *)(xevent);
577
578       GDK_NOTE (EVENTS,
579         g_print ("device key %s:\twindow: %ld  device: %ld  keycode: %d\n",
580                  (event->key.type == GDK_KEY_PRESS) ? "press" : "release",
581                  xdke->window,
582                  xdke->deviceid,
583                  xdke->keycode));
584
585       if (xdke->keycode < gdkdev->min_keycode ||
586           xdke->keycode >= gdkdev->min_keycode + gdkdev->info.num_keys)
587         {
588           g_warning ("Invalid device key code received");
589           return FALSE;
590         }
591       
592       event->key.keyval = gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].keyval;
593
594       if (event->key.keyval == 0) 
595         {
596           GDK_NOTE (EVENTS,
597             g_print ("\t\ttranslation - NONE\n"));
598           
599           return FALSE;
600         }
601
602       event->key.type = (xdke->type == gdkdev->keypress_type) ?
603         GDK_KEY_PRESS : GDK_KEY_RELEASE;
604
605       event->key.window = input_window->window;
606       event->key.time = xdke->time;
607
608       event->key.state = gdk_input_translate_state(xdke->state, xdke->device_state)
609         | gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].modifiers;
610
611       /* Add a string translation for the key event */
612       if ((event->key.keyval >= 0x20) && (event->key.keyval <= 0xFF))
613         {
614           event->key.length = 1;
615           event->key.string = g_new (gchar, 2);
616           event->key.string[0] = (gchar)event->key.keyval;
617           event->key.string[1] = 0;
618         }
619       else
620         {
621           event->key.length = 0;
622           event->key.string = g_new0 (gchar, 1);
623         }
624
625       GDK_NOTE (EVENTS,
626         g_print ("\t\ttranslation - keyval: %d modifiers: %#x\n",
627                  event->key.keyval,
628                  event->key.state));
629
630       return TRUE;
631     }
632
633   if (xevent->type == gdkdev->motionnotify_type) 
634     {
635       XDeviceMotionEvent *xdme = (XDeviceMotionEvent *)(xevent);
636
637       event->motion.device = &gdkdev->info;
638       
639       event->motion.axes = g_new (gdouble, gdkdev->info.num_axes);
640       gdk_input_translate_coordinates(gdkdev,input_window,xdme->axis_data,
641                                       event->motion.axes,
642                                       &event->motion.x,&event->motion.y);
643
644       event->motion.type = GDK_MOTION_NOTIFY;
645       event->motion.window = input_window->window;
646       event->motion.time = xdme->time;
647       event->motion.state = gdk_input_translate_state(xdme->state,
648                                                       xdme->device_state);
649       event->motion.is_hint = xdme->is_hint;
650
651       GDK_NOTE (EVENTS,
652         g_print ("motion notify:\t\twindow: %ld  device: %ld  x,y: %f %f  state %#4x  hint: %s\n",
653                  xdme->window,
654                  xdme->deviceid,
655                  event->motion.x, event->motion.y,
656                  event->motion.state,
657                  (xdme->is_hint) ? "true" : "false"));
658       
659       
660       return TRUE;
661     }
662
663   if (xevent->type == gdkdev->proximityin_type ||
664       xevent->type == gdkdev->proximityout_type)
665     {
666       XProximityNotifyEvent *xpne = (XProximityNotifyEvent *)(xevent);
667
668       event->proximity.device = &gdkdev->info;
669       event->proximity.type = (xevent->type == gdkdev->proximityin_type)?
670         GDK_PROXIMITY_IN:GDK_PROXIMITY_OUT;
671       event->proximity.window = input_window->window;
672       event->proximity.time = xpne->time;
673       
674       return TRUE;
675   }
676
677   return -1;                    /* wasn't one of our event types */
678 }
679
680 gboolean
681 _gdk_device_get_history (GdkDevice         *device,
682                          GdkWindow         *window,
683                          guint32            start,
684                          guint32            stop,
685                          GdkTimeCoord    ***events,
686                          gint              *n_events)
687 {
688   GdkTimeCoord **coords;
689   XDeviceTimeCoord *device_coords;
690   GdkInputWindow *input_window;
691   GdkDevicePrivate *gdkdev;
692   gint mode_return;
693   gint axis_count_return;
694   gint i;
695
696   gdkdev = (GdkDevicePrivate *)device;
697   input_window = _gdk_input_window_find (window);
698
699   g_return_val_if_fail (input_window != NULL, FALSE);
700
701   device_coords = XGetDeviceMotionEvents (GDK_WINDOW_XDISPLAY (window),
702                                           gdkdev->xdevice,
703                                           start, stop,
704                                           n_events, &mode_return,
705                                           &axis_count_return);
706
707   if (device_coords)
708     {
709       coords = _gdk_device_allocate_history (device, *n_events);
710       
711       for (i=0; i<*n_events; i++)
712         gdk_input_translate_coordinates (gdkdev, input_window,
713                                          device_coords[i].data,
714                                          coords[i]->axes, NULL, NULL);
715       XFreeDeviceMotionEvents (device_coords);
716
717       *events = coords;
718
719       return TRUE;
720     }
721   else
722     return FALSE;
723 }
724
725 void 
726 gdk_device_get_state (GdkDevice       *device,
727                       GdkWindow       *window,
728                       gdouble         *axes,
729                       GdkModifierType *mask)
730 {
731   gint i;
732
733   g_return_if_fail (device != NULL);
734   g_return_if_fail (GDK_IS_WINDOW (window));
735
736   if (GDK_IS_CORE (device))
737     {
738       gint x_int, y_int;
739       
740       gdk_window_get_pointer (window, &x_int, &y_int, mask);
741
742       if (axes)
743         {
744           axes[0] = x_int;
745           axes[1] = y_int;
746         }
747     }
748   else
749     {
750       GdkDevicePrivate *gdkdev;
751       GdkInputWindow *input_window;
752       XDeviceState *state;
753       XInputClass *input_class;
754       
755       if (mask)
756         gdk_window_get_pointer (window, NULL, NULL, mask);
757       
758       gdkdev = (GdkDevicePrivate *)device;
759       input_window = _gdk_input_window_find (window);
760       g_return_if_fail (input_window != NULL);
761
762       state = XQueryDeviceState (GDK_WINDOW_XDISPLAY (window),
763                                  gdkdev->xdevice);
764       input_class = state->data;
765       for (i=0; i<state->num_classes; i++)
766         {
767           switch (input_class->class)
768             {
769             case ValuatorClass:
770               if (axes)
771                 gdk_input_translate_coordinates (gdkdev, input_window,
772                                                  ((XValuatorState *)input_class)->valuators,
773                                                  axes, NULL, NULL);
774               break;
775               
776             case ButtonClass:
777               if (mask)
778                 {
779                   *mask &= 0xFF;
780                   if (((XButtonState *)input_class)->num_buttons > 0)
781                     *mask |= ((XButtonState *)input_class)->buttons[0] << 7;
782                   /* GDK_BUTTON1_MASK = 1 << 8, and button n is stored
783                    * in bit 1<<(n%8) in byte n/8. n = 1,2,... */
784                 }
785               break;
786             }
787           input_class = (XInputClass *)(((char *)input_class)+input_class->length);
788         }
789       XFreeDeviceState (state);
790     }
791 }