1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #include "gdkinputprivate.h"
28 #include "gdkinternals.h"
30 #include "gdk.h" /* For gdk_error_trap_push()/pop() */
34 /* Forward declarations */
35 static GdkDevicePrivate *gdk_input_device_new(XDeviceInfo *device,
37 static void gdk_input_translate_coordinates(GdkDevicePrivate *gdkdev,
38 GdkInputWindow *input_window,
43 static guint gdk_input_translate_state(guint state, guint device_state);
45 /* Global variables */
48 gdk_input_find_device (guint32 id)
50 GList *tmp_list = gdk_input_devices;
51 GdkDevicePrivate *gdkdev;
54 gdkdev = (GdkDevicePrivate *)(tmp_list->data);
55 if (gdkdev->deviceid == id)
57 tmp_list = tmp_list->next;
63 gdk_input_get_root_relative_geometry(Display *dpy, Window w, int *x_ret, int *y_ret,
64 int *width_ret, int *height_ret)
66 Window root, parent, child;
71 guint border_widthc, depthc;
73 XQueryTree (dpy, w, &root, &parent, &children, &nchildren);
77 XGetGeometry (dpy, w, &root, &x, &y, &width, &height, &border_widthc, &depthc);
79 XTranslateCoordinates (dpy, w, root, 0, 0, &x, &y, &child);
91 static GdkDevicePrivate *
92 gdk_input_device_new (XDeviceInfo *device, gint include_core)
94 GdkDevicePrivate *gdkdev;
99 gdkdev = g_new(GdkDevicePrivate,1);
101 gdkdev->deviceid = device->id;
103 gdkdev->info.name = g_strdup (device->name);
105 /* XFree86 3.2 gives an empty name to the default core devices,
107 gdkdev->info.name = g_strdup ("pointer");
109 gdkdev->info.mode = GDK_MODE_DISABLED;
111 /* Try to figure out what kind of device this is by its name -
112 could invite a very, very, long list... Lowercase name
113 for comparison purposes */
115 tmp_name = g_strdup (gdkdev->info.name);
116 g_strdown (tmp_name);
118 if (!strcmp (tmp_name, "pointer"))
119 gdkdev->info.source = GDK_SOURCE_MOUSE;
120 else if (!strcmp (tmp_name, "wacom") ||
121 !strcmp (tmp_name, "pen"))
122 gdkdev->info.source = GDK_SOURCE_PEN;
123 else if (!strcmp (tmp_name, "eraser"))
124 gdkdev->info.source = GDK_SOURCE_ERASER;
125 else if (!strcmp (tmp_name, "cursor"))
126 gdkdev->info.source = GDK_SOURCE_CURSOR;
128 gdkdev->info.source = GDK_SOURCE_PEN;
132 gdkdev->xdevice = NULL;
134 /* step through the classes */
136 gdkdev->info.num_axes = 0;
137 gdkdev->info.num_keys = 0;
138 gdkdev->info.axes = NULL;
139 gdkdev->info.keys = NULL;
141 gdkdev->info.has_cursor = 0;
142 gdkdev->needs_update = FALSE;
143 gdkdev->claimed = FALSE;
144 gdkdev->button_state = 0;
146 class = device->inputclassinfo;
147 for (i=0;i<device->num_classes;i++)
149 switch (class->class) {
154 XKeyInfo *xki = (XKeyInfo *)class;
155 /* Hack to catch XFree86 3.3.1 bug. Other devices better
156 * not have exactly 25 keys...
158 if ((xki->min_keycode == 8) && (xki->max_keycode == 32))
160 gdkdev->info.num_keys = 32;
161 gdkdev->min_keycode = 1;
165 gdkdev->info.num_keys = xki->max_keycode - xki->min_keycode + 1;
166 gdkdev->min_keycode = xki->min_keycode;
168 gdkdev->info.keys = g_new (GdkDeviceKey, gdkdev->info.num_keys);
170 for (j=0; j<gdkdev->info.num_keys; j++)
172 gdkdev->info.keys[j].keyval = 0;
173 gdkdev->info.keys[j].modifiers = 0;
180 XValuatorInfo *xvi = (XValuatorInfo *)class;
181 gdkdev->info.num_axes = xvi->num_axes;
182 gdkdev->axes = g_new (GdkAxisInfo, xvi->num_axes);
183 gdkdev->info.axes = g_new0 (GdkDeviceAxis, xvi->num_axes);
184 for (j=0;j<xvi->num_axes;j++)
186 gdkdev->axes[j].resolution =
187 gdkdev->axes[j].xresolution = xvi->axes[j].resolution;
188 gdkdev->axes[j].min_value =
189 gdkdev->axes[j].xmin_value = xvi->axes[j].min_value;
190 gdkdev->axes[j].max_value =
191 gdkdev->axes[j].xmax_value = xvi->axes[j].max_value;
192 gdkdev->info.axes[j].use = GDK_AXIS_IGNORE;
196 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_X);
198 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_Y);
200 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_PRESSURE);
202 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_XTILT);
204 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_YTILT);
206 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_WHEEL);
211 class = (XAnyClassPtr)(((char *)class) + class->length);
213 /* return NULL if no axes */
214 if (!gdkdev->info.num_axes || !gdkdev->axes ||
215 (!include_core && device->use == IsXPointer))
218 if (device->use != IsXPointer)
220 gdk_error_trap_push ();
221 gdkdev->xdevice = XOpenDevice(gdk_display, gdkdev->deviceid);
223 /* return NULL if device is not ready */
224 if (gdk_error_trap_pop ())
228 gdkdev->buttonpress_type = 0;
229 gdkdev->buttonrelease_type = 0;
230 gdkdev->keypress_type = 0;
231 gdkdev->keyrelease_type = 0;
232 gdkdev->motionnotify_type = 0;
233 gdkdev->proximityin_type = 0;
234 gdkdev->proximityout_type = 0;
235 gdkdev->changenotify_type = 0;
241 g_free (gdkdev->info.name);
243 g_free (gdkdev->axes);
244 if (gdkdev->info.keys)
245 g_free (gdkdev->info.keys);
246 if (gdkdev->info.axes)
247 g_free (gdkdev->info.axes);
254 gdk_input_common_find_events(GdkWindow *window,
255 GdkDevicePrivate *gdkdev,
257 XEventClass *classes,
264 /* We have to track press and release events in pairs to keep
265 track of button state correctly and implement grabbing for
267 if (mask & GDK_BUTTON_PRESS_MASK || mask & GDK_BUTTON_RELEASE_MASK)
269 DeviceButtonPress (gdkdev->xdevice, gdkdev->buttonpress_type,
272 classes[i++] = class;
273 DeviceButtonPressGrab (gdkdev->xdevice, 0, class);
275 classes[i++] = class;
276 DeviceButtonRelease (gdkdev->xdevice, gdkdev->buttonrelease_type,
279 classes[i++] = class;
281 if (mask & GDK_POINTER_MOTION_MASK)
283 DeviceMotionNotify (gdkdev->xdevice, gdkdev->motionnotify_type, class);
285 classes[i++] = class;
288 if (mask & (GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
289 GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_MOTION_MASK |
290 GDK_POINTER_MOTION_HINT_MASK))
292 /* Make sure gdkdev->motionnotify_type is set */
293 DeviceMotionNotify (gdkdev->xdevice, gdkdev->motionnotify_type, class);
295 if (mask & GDK_BUTTON1_MOTION_MASK)
297 DeviceButton1Motion (gdkdev->xdevice, 0, class);
299 classes[i++] = class;
301 if (mask & GDK_BUTTON2_MOTION_MASK)
303 DeviceButton2Motion (gdkdev->xdevice, 0, class);
305 classes[i++] = class;
307 if (mask & GDK_BUTTON3_MOTION_MASK)
309 DeviceButton3Motion (gdkdev->xdevice, 0, class);
311 classes[i++] = class;
313 if (mask & GDK_BUTTON_MOTION_MASK)
315 DeviceButtonMotion (gdkdev->xdevice, 0, class);
317 classes[i++] = class;
319 if (mask & GDK_POINTER_MOTION_HINT_MASK)
321 /* We'll get into trouble if the macros change, but at least we'll
322 know about it, and we avoid warnings now */
323 DevicePointerMotionHint (gdkdev->xdevice, 0, class);
325 classes[i++] = class;
327 if (mask & GDK_KEY_PRESS_MASK)
329 DeviceKeyPress (gdkdev->xdevice, gdkdev->keypress_type, class);
331 classes[i++] = class;
333 if (mask & GDK_KEY_RELEASE_MASK)
335 DeviceKeyRelease (gdkdev->xdevice, gdkdev->keyrelease_type, class);
337 classes[i++] = class;
339 if (mask & GDK_PROXIMITY_IN_MASK)
341 ProximityIn (gdkdev->xdevice, gdkdev->proximityin_type, class);
343 classes[i++] = class;
345 if (mask & GDK_PROXIMITY_OUT_MASK)
347 ProximityOut (gdkdev->xdevice, gdkdev->proximityout_type, class);
349 classes[i++] = class;
356 gdk_input_common_select_events(GdkWindow *window,
357 GdkDevicePrivate *gdkdev)
359 XEventClass classes[GDK_MAX_DEVICE_CLASSES];
362 if (gdkdev->info.mode == GDK_MODE_DISABLED)
363 gdk_input_common_find_events(window, gdkdev, 0, classes, &num_classes);
365 gdk_input_common_find_events(window, gdkdev,
366 ((GdkWindowObject *)window)->extension_events,
367 classes, &num_classes);
369 XSelectExtensionEvent (gdk_display,
370 GDK_WINDOW_XWINDOW(window),
371 classes, num_classes);
375 gdk_input_common_init(gint include_core)
378 XDeviceInfo *devices;
380 int num_extensions, loop;
381 Display *display = gdk_display;
383 /* Init XInput extension */
385 extensions = XListExtensions(display, &num_extensions);
386 for (loop = 0; loop < num_extensions &&
387 (strcmp(extensions[loop], "XInputExtension") != 0); loop++);
388 XFreeExtensionList(extensions);
389 gdk_input_devices = NULL;
390 if (loop < num_extensions)
392 /* XInput extension found */
394 devices = XListInputDevices(display, &num_devices);
396 for(loop=0; loop<num_devices; loop++)
398 GdkDevicePrivate *gdkdev = gdk_input_device_new(&devices[loop],
401 gdk_input_devices = g_list_append(gdk_input_devices, gdkdev);
403 XFreeDeviceList(devices);
406 gdk_input_devices = g_list_append (gdk_input_devices, gdk_core_pointer);
412 gdk_input_translate_coordinates (GdkDevicePrivate *gdkdev,
413 GdkInputWindow *input_window,
419 GdkWindowImplX11 *impl;
424 double device_width, device_height;
425 double x_offset, y_offset, x_scale, y_scale;
427 impl = GDK_WINDOW_IMPL_X11 (((GdkWindowObject *) input_window->window)->impl);
429 for (i=0; i<gdkdev->info.num_axes; i++)
431 switch (gdkdev->info.axes[i].use)
444 device_width = gdkdev->axes[x_axis].max_value -
445 gdkdev->axes[x_axis].min_value;
446 device_height = gdkdev->axes[y_axis].max_value -
447 gdkdev->axes[y_axis].min_value;
449 if (gdkdev->info.mode == GDK_MODE_SCREEN)
451 x_scale = gdk_screen_width() / device_width;
452 y_scale = gdk_screen_height() / device_height;
454 x_offset = - input_window->root_x;
455 y_offset = - input_window->root_y;
457 else /* GDK_MODE_WINDOW */
459 double device_aspect = (device_height*gdkdev->axes[y_axis].resolution) /
460 (device_width*gdkdev->axes[x_axis].resolution);
462 if (device_aspect * impl->width >= impl->height)
464 /* device taller than window */
465 x_scale = impl->width / device_width;
466 y_scale = (x_scale * gdkdev->axes[x_axis].resolution)
467 / gdkdev->axes[y_axis].resolution;
470 y_offset = -(device_height * y_scale -
475 /* window taller than device */
476 y_scale = impl->height / device_height;
477 x_scale = (y_scale * gdkdev->axes[y_axis].resolution)
478 / gdkdev->axes[x_axis].resolution;
481 x_offset = - (device_width * x_scale - impl->width)/2;
485 for (i=0; i<gdkdev->info.num_axes; i++)
487 switch (gdkdev->info.axes[i].use)
490 axis_out[i] = x_offset + x_scale*axis_data[x_axis];
492 *x_out = axis_out[i];
495 axis_out[i] = y_offset + y_scale*axis_data[y_axis];
497 *y_out = axis_out[i];
501 (gdkdev->info.axes[i].max * (axis_data[i] - gdkdev->axes[i].min_value) +
502 gdkdev->info.axes[i].min * (gdkdev->axes[i].max_value - axis_data[i])) /
503 (gdkdev->axes[i].max_value - gdkdev->axes[i].min_value);
509 /* combine the state of the core device and the device state
510 * into one - for now we do this in a simple-minded manner -
511 * we just take the keyboard portion of the core device and
512 * the button portion (all of?) the device state.
513 * Any button remapping should go on here.
516 gdk_input_translate_state(guint state, guint device_state)
518 return device_state | (state & 0xFF);
523 gdk_input_common_other_event (GdkEvent *event,
525 GdkInputWindow *input_window,
526 GdkDevicePrivate *gdkdev)
528 if ((xevent->type == gdkdev->buttonpress_type) ||
529 (xevent->type == gdkdev->buttonrelease_type))
531 XDeviceButtonEvent *xdbe = (XDeviceButtonEvent *)(xevent);
533 if (xdbe->type == gdkdev->buttonpress_type)
535 event->button.type = GDK_BUTTON_PRESS;
536 gdkdev->button_state |= 1 << xdbe->button;
540 event->button.type = GDK_BUTTON_RELEASE;
541 gdkdev->button_state &= ~(1 << xdbe->button);
543 event->button.device = &gdkdev->info;
544 event->button.window = input_window->window;
545 event->button.time = xdbe->time;
547 event->button.axes = g_new (gdouble, gdkdev->info.num_axes);
548 gdk_input_translate_coordinates (gdkdev,input_window, xdbe->axis_data,
550 &event->button.x,&event->button.y);
551 event->button.state = gdk_input_translate_state(xdbe->state,xdbe->device_state);
552 event->button.button = xdbe->button;
555 g_print ("button %s:\t\twindow: %ld device: %ld x,y: %f %f button: %d\n",
556 (event->button.type == GDK_BUTTON_PRESS) ? "press" : "release",
559 event->button.x, event->button.y,
565 if ((xevent->type == gdkdev->keypress_type) ||
566 (xevent->type == gdkdev->keyrelease_type))
568 XDeviceKeyEvent *xdke = (XDeviceKeyEvent *)(xevent);
571 g_print ("device key %s:\twindow: %ld device: %ld keycode: %d\n",
572 (event->key.type == GDK_KEY_PRESS) ? "press" : "release",
577 if (xdke->keycode < gdkdev->min_keycode ||
578 xdke->keycode >= gdkdev->min_keycode + gdkdev->info.num_keys)
580 g_warning ("Invalid device key code received");
584 event->key.keyval = gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].keyval;
586 if (event->key.keyval == 0)
589 g_print ("\t\ttranslation - NONE\n"));
594 event->key.type = (xdke->type == gdkdev->keypress_type) ?
595 GDK_KEY_PRESS : GDK_KEY_RELEASE;
597 event->key.window = input_window->window;
598 event->key.time = xdke->time;
600 event->key.state = gdk_input_translate_state(xdke->state, xdke->device_state)
601 | gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].modifiers;
603 /* Add a string translation for the key event */
604 if ((event->key.keyval >= 0x20) && (event->key.keyval <= 0xFF))
606 event->key.length = 1;
607 event->key.string = g_new (gchar, 2);
608 event->key.string[0] = (gchar)event->key.keyval;
609 event->key.string[1] = 0;
613 event->key.length = 0;
614 event->key.string = g_new0 (gchar, 1);
618 g_print ("\t\ttranslation - keyval: %d modifiers: %#x\n",
625 if (xevent->type == gdkdev->motionnotify_type)
627 XDeviceMotionEvent *xdme = (XDeviceMotionEvent *)(xevent);
629 event->motion.device = &gdkdev->info;
631 event->motion.axes = g_new (gdouble, gdkdev->info.num_axes);
632 gdk_input_translate_coordinates(gdkdev,input_window,xdme->axis_data,
634 &event->motion.x,&event->motion.y);
636 event->motion.type = GDK_MOTION_NOTIFY;
637 event->motion.window = input_window->window;
638 event->motion.time = xdme->time;
639 event->motion.state = gdk_input_translate_state(xdme->state,
641 event->motion.is_hint = xdme->is_hint;
644 g_print ("motion notify:\t\twindow: %ld device: %ld x,y: %f %f state %#4x hint: %s\n",
647 event->motion.x, event->motion.y,
649 (xdme->is_hint) ? "true" : "false"));
655 if (xevent->type == gdkdev->proximityin_type ||
656 xevent->type == gdkdev->proximityout_type)
658 XProximityNotifyEvent *xpne = (XProximityNotifyEvent *)(xevent);
660 event->proximity.device = &gdkdev->info;
661 event->proximity.type = (xevent->type == gdkdev->proximityin_type)?
662 GDK_PROXIMITY_IN:GDK_PROXIMITY_OUT;
663 event->proximity.window = input_window->window;
664 event->proximity.time = xpne->time;
669 return -1; /* wasn't one of our event types */
673 _gdk_device_get_history (GdkDevice *device,
677 GdkTimeCoord ***events,
680 GdkTimeCoord **coords;
681 XDeviceTimeCoord *device_coords;
682 GdkInputWindow *input_window;
683 GdkDevicePrivate *gdkdev;
685 gint axis_count_return;
688 gdkdev = (GdkDevicePrivate *)device;
689 input_window = gdk_input_window_find (window);
691 g_return_val_if_fail (input_window != NULL, FALSE);
693 device_coords = XGetDeviceMotionEvents (gdk_display,
696 n_events, &mode_return,
701 coords = _gdk_device_allocate_history (device, *n_events);
703 for (i=0; i<*n_events; i++)
704 gdk_input_translate_coordinates (gdkdev, input_window,
705 device_coords[i].data,
706 coords[i]->axes, NULL, NULL);
707 XFreeDeviceMotionEvents (device_coords);
723 gdk_device_get_state (GdkDevice *device,
726 GdkModifierType *mask)
730 g_return_if_fail (device != NULL);
731 g_return_if_fail (GDK_IS_WINDOW (window));
733 if (GDK_IS_CORE (device))
737 gdk_window_get_pointer (window, &x_int, &y_int, mask);
747 GdkDevicePrivate *gdkdev;
748 GdkInputWindow *input_window;
750 XInputClass *input_class;
753 gdk_window_get_pointer (window, NULL, NULL, mask);
755 gdkdev = (GdkDevicePrivate *)device;
756 input_window = gdk_input_window_find (window);
757 g_return_if_fail (input_window != NULL);
759 state = XQueryDeviceState (gdk_display, gdkdev->xdevice);
760 input_class = state->data;
761 for (i=0; i<state->num_classes; i++)
763 switch (input_class->class)
767 gdk_input_translate_coordinates (gdkdev, input_window,
768 ((XValuatorState *)input_class)->valuators,
776 if (((XButtonState *)input_class)->num_buttons > 0)
777 *mask |= ((XButtonState *)input_class)->buttons[0] << 7;
778 /* GDK_BUTTON1_MASK = 1 << 8, and button n is stored
779 * in bit 1<<(n%8) in byte n/8. n = 1,2,... */
783 input_class = (XInputClass *)(((char *)input_class)+input_class->length);
785 XFreeDeviceState (state);