Debian Codegraph @

gdk/x11/gdkinput.c

raw
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 "config.h"
28
29 #include <stdlib.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32
33 #include "gdkx.h"
34 #include "gdkinput.h"
35 #include "gdkprivate.h"
36 #include "gdkinputprivate.h"
37 #include "gdkscreen-x11.h"
38 #include "gdkdisplay-x11.h"
39 #include "gdkalias.h"
40
41 static GdkDeviceAxis gdk_input_core_axes[] = {
42 { GDK_AXIS_X, 0, 0 },
43 { GDK_AXIS_Y, 0, 0 }
44 };
45
46 void
47 _gdk_init_input_core (GdkDisplay *display)
48 {
49 GdkDevicePrivate *private;
50
51 display->core_pointer = g_object_new (GDK_TYPE_DEVICE, NULL);
52 private = (GdkDevicePrivate *)display->core_pointer;
53
54 display->core_pointer->name = "Core Pointer";
55 display->core_pointer->source = GDK_SOURCE_MOUSE;
56 display->core_pointer->mode = GDK_MODE_SCREEN;
57 display->core_pointer->has_cursor = TRUE;
58 display->core_pointer->num_axes = 2;
59 display->core_pointer->axes = gdk_input_core_axes;
60 display->core_pointer->num_keys = 0;
61 display->core_pointer->keys = NULL;
62
63 private->display = display;
64 }
65
66 static void gdk_device_class_init (GdkDeviceClass *klass);
67 static void gdk_device_dispose (GObject *object);
68
69 static gpointer gdk_device_parent_class = NULL;
70
71 GType
72 gdk_device_get_type (void)
73 {
74 static GType object_type = 0;
75
76 if (!object_type)
77 {
78 const GTypeInfo object_info =
79 {
80 sizeof (GdkDeviceClass),
81 (GBaseInitFunc) NULL,
82 (GBaseFinalizeFunc) NULL,
83 (GClassInitFunc) gdk_device_class_init,
84 NULL, /* class_finalize */
85 NULL, /* class_data */
86 sizeof (GdkDevicePrivate),
87 0, /* n_preallocs */
88 (GInstanceInitFunc) NULL,
89 };
90
91 object_type = g_type_register_static (G_TYPE_OBJECT,
92 g_intern_static_string ("GdkDevice"),
93 &object_info, 0);
94 }
95
96 return object_type;
97 }
98
99 static void
100 gdk_device_class_init (GdkDeviceClass *klass)
101 {
102 GObjectClass *object_class = G_OBJECT_CLASS (klass);
103
104 gdk_device_parent_class = g_type_class_peek_parent (klass);
105
106 object_class->dispose = gdk_device_dispose;
107 }
108
109 static void
110 gdk_device_dispose (GObject *object)
111 {
112 GdkDevicePrivate *gdkdev = (GdkDevicePrivate *) object;
113
114 if (gdkdev->display && !GDK_IS_CORE (gdkdev))
115 {
116 #ifndef XINPUT_NONE
117 if (gdkdev->xdevice)
118 {
119 XCloseDevice (GDK_DISPLAY_XDISPLAY (gdkdev->display), gdkdev->xdevice);
120 gdkdev->xdevice = NULL;
121 }
122 g_free (gdkdev->axes);
123 g_free (gdkdev->axis_data);
124 gdkdev->axes = NULL;
125 gdkdev->axis_data = NULL;
126 #endif /* !XINPUT_NONE */
127
128 g_free (gdkdev->info.name);
129 g_free (gdkdev->info.keys);
130 g_free (gdkdev->info.axes);
131
132 gdkdev->info.name = NULL;
133 gdkdev->info.keys = NULL;
134 gdkdev->info.axes = NULL;
135 }
136
137 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
138 }
139
140 /**
141 * gdk_devices_list:
142 *
143 * Returns the list of available input devices for the default display.
144 * The list is statically allocated and should not be freed.
145 *
146 * Return value: (transfer none) (element-type GdkDevice): a list of #GdkDevice
147 **/
148 GList *
149 gdk_devices_list (void)
150 {
151 return gdk_display_list_devices (gdk_display_get_default ());
152 }
153
154 /**
155 * gdk_display_list_devices:
156 * @display: a #GdkDisplay
157 *
158 * Returns the list of available input devices attached to @display.
159 * The list is statically allocated and should not be freed.
160 *
161 * Return value: a list of #GdkDevice
162 *
163 * Since: 2.2
164 **/
165 GList *
166 gdk_display_list_devices (GdkDisplay *display)
167 {
168 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
169
170 return GDK_DISPLAY_X11 (display)->input_devices;
171 }
172
173 /**
174 * gdk_device_get_name:
175 * @device: a #GdkDevice
176 *
177 * Determines the name of the device.
178 *
179 * Return value: a name
180 *
181 * Since: 2.22
182 **/
183 const gchar *
184 gdk_device_get_name (GdkDevice *device)
185 {
186 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
187
188 return device->name;
189 }
190
191 /**
192 * gdk_device_get_source:
193 * @device: a #GdkDevice
194 *
195 * Determines the type of the device.
196 *
197 * Return value: a #GdkInputSource
198 *
199 * Since: 2.22
200 **/
201 GdkInputSource
202 gdk_device_get_source (GdkDevice *device)
203 {
204 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
205
206 return device->source;
207 }
208
209 /**
210 * gdk_device_get_mode:
211 * @device: a #GdkDevice
212 *
213 * Determines the mode of the device.
214 *
215 * Return value: a #GdkInputSource
216 *
217 * Since: 2.22
218 **/
219 GdkInputMode
220 gdk_device_get_mode (GdkDevice *device)
221 {
222 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
223
224 return device->mode;
225 }
226
227 /**
228 * gdk_device_get_has_cursor:
229 * @device: a #GdkDevice
230 *
231 * Determines whether the pointer follows device motion.
232 *
233 * Return value: %TRUE if the pointer follows device motion
234 *
235 * Since: 2.22
236 **/
237 gboolean
238 gdk_device_get_has_cursor (GdkDevice *device)
239 {
240 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
241
242 return device->has_cursor;
243 }
244
245 void
246 gdk_device_set_source (GdkDevice *device,
247 GdkInputSource source)
248 {
249 g_return_if_fail (device != NULL);
250
251 device->source = source;
252 }
253
254 /**
255 * gdk_device_get_key:
256 * @device: a #GdkDevice.
257 * @index: the index of the macro button to get.
258 * @keyval: return value for the keyval.
259 * @modifiers: return value for modifiers.
260 *
261 * If @index has a valid keyval, this function will
262 * fill in @keyval and @modifiers with the keyval settings.
263 *
264 * Since: 2.22
265 **/
266 void
267 gdk_device_get_key (GdkDevice *device,
268 guint index,
269 guint *keyval,
270 GdkModifierType *modifiers)
271 {
272 g_return_if_fail (GDK_IS_DEVICE (device));
273 g_return_if_fail (index < device->num_keys);
274
275 if (!device->keys[index].keyval &&
276 !device->keys[index].modifiers)
277 return;
278
279 if (keyval)
280 *keyval = device->keys[index].keyval;
281
282 if (modifiers)
283 *modifiers = device->keys[index].modifiers;
284 }
285
286 void
287 gdk_device_set_key (GdkDevice *device,
288 guint index,
289 guint keyval,
290 GdkModifierType modifiers)
291 {
292 g_return_if_fail (device != NULL);
293 g_return_if_fail (index < device->num_keys);
294
295 device->keys[index].keyval = keyval;
296 device->keys[index].modifiers = modifiers;
297 }
298
299 /**
300 * gdk_device_get_axis_use:
301 * @device: a #GdkDevice.
302 * @index: the index of the axis.
303 *
304 * Returns the axis use for @index.
305 *
306 * Returns: a #GdkAxisUse specifying how the axis is used.
307 *
308 * Since: 2.22
309 **/
310 GdkAxisUse
311 gdk_device_get_axis_use (GdkDevice *device,
312 guint index)
313 {
314 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
315 g_return_val_if_fail (index < device->num_axes, GDK_AXIS_IGNORE);
316
317 return device->axes[index].use;
318 }
319
320 /**
321 * gdk_device_get_n_keys:
322 * @device: a #GdkDevice.
323 *
324 * Gets the number of keys of a device.
325 *
326 * Returns: the number of keys of @device
327 *
328 * Since: 2.24
329 **/
330 gint
331 gdk_device_get_n_keys (GdkDevice *device)
332 {
333 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
334
335 return device->num_keys;
336 }
337
338 /**
339 * gdk_device_get_n_axes:
340 * @device: a #GdkDevice.
341 *
342 * Gets the number of axes of a device.
343 *
344 * Returns: the number of axes of @device
345 *
346 * Since: 2.22
347 **/
348 gint
349 gdk_device_get_n_axes (GdkDevice *device)
350 {
351 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
352
353 return device->num_axes;
354 }
355
356 void
357 gdk_device_set_axis_use (GdkDevice *device,
358 guint index,
359 GdkAxisUse use)
360 {
361 g_return_if_fail (device != NULL);
362 g_return_if_fail (index < device->num_axes);
363
364 device->axes[index].use = use;
365
366 switch (use)
367 {
368 case GDK_AXIS_X:
369 case GDK_AXIS_Y:
370 device->axes[index].min = 0.;
371 device->axes[index].max = 0.;
372 break;
373 case GDK_AXIS_XTILT:
374 case GDK_AXIS_YTILT:
375 device->axes[index].min = -1.;
376 device->axes[index].max = 1;
377 break;
378 default:
379 device->axes[index].min = 0.;
380 device->axes[index].max = 1;
381 break;
382 }
383 }
384
385 static gboolean
386 impl_coord_in_window (GdkWindow *window,
387 int impl_x,
388 int impl_y)
389 {
390 GdkWindowObject *priv = (GdkWindowObject *)window;
391
392 if (impl_x < priv->abs_x ||
393 impl_x > priv->abs_x + priv->width)
394 return FALSE;
395 if (impl_y < priv->abs_y ||
396 impl_y > priv->abs_y + priv->height)
397 return FALSE;
398 return TRUE;
399 }
400
401 /**
402 * gdk_device_get_history:
403 * @device: a #GdkDevice
404 * @window: the window with respect to which which the event coordinates will be reported
405 * @start: starting timestamp for range of events to return
406 * @stop: ending timestamp for the range of events to return
407 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
408 * @n_events: location to store the length of @events, or %NULL
409 *
410 * Obtains the motion history for a device; given a starting and
411 * ending timestamp, return all events in the motion history for
412 * the device in the given range of time. Some windowing systems
413 * do not support motion history, in which case, %FALSE will
414 * be returned. (This is not distinguishable from the case where
415 * motion history is supported and no events were found.)
416 *
417 * Return value: %TRUE if the windowing system supports motion history and
418 * at least one event was found.
419 **/
420 gboolean
421 gdk_device_get_history (GdkDevice *device,
422 GdkWindow *window,
423 guint32 start,
424 guint32 stop,
425 GdkTimeCoord ***events,
426 gint *n_events)
427 {
428 GdkTimeCoord **coords = NULL;
429 GdkWindow *impl_window;
430 gboolean result = FALSE;
431 int tmp_n_events = 0;
432
433 g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), FALSE);
434
435 impl_window = _gdk_window_get_impl_window (window);
436
437 if (GDK_WINDOW_DESTROYED (window))
438 /* Nothing */ ;
439 else if (GDK_IS_CORE (device))
440 {
441 XTimeCoord *xcoords;
442
443 xcoords = XGetMotionEvents (GDK_DRAWABLE_XDISPLAY (window),
444 GDK_DRAWABLE_XID (impl_window),
445 start, stop, &tmp_n_events);
446 if (xcoords)
447 {
448 GdkWindowObject *priv = (GdkWindowObject *)window;
449 int i, j;
450
451 coords = _gdk_device_allocate_history (device, tmp_n_events);
452 j = 0;
453
454 for (i = 0; i < tmp_n_events; i++)
455 {
456 if (impl_coord_in_window (window, xcoords[i].x, xcoords[i].y))
457 {
458 coords[j]->time = xcoords[i].time;
459 coords[j]->axes[0] = xcoords[i].x - priv->abs_x;
460 coords[j]->axes[1] = xcoords[i].y - priv->abs_y;
461 j++;
462 }
463 }
464
465 XFree (xcoords);
466
467 /* free the events we allocated too much */
468 for (i = j; i < tmp_n_events; i++)
469 {
470 g_free (coords[i]);
471 coords[i] = NULL;
472 }
473
474 tmp_n_events = j;
475
476 if (tmp_n_events > 0)
477 {
478 result = TRUE;
479 }
480 else
481 {
482 gdk_device_free_history (coords, tmp_n_events);
483 coords = NULL;
484 }
485 }
486 }
487 else
488 result = _gdk_device_get_history (device, window, start, stop, &coords, &tmp_n_events);
489
490 if (n_events)
491 *n_events = tmp_n_events;
492
493 if (events)
494 *events = coords;
495 else if (coords)
496 gdk_device_free_history (coords, tmp_n_events);
497
498 return result;
499 }
500
501 GdkTimeCoord **
502 _gdk_device_allocate_history (GdkDevice *device,
503 gint n_events)
504 {
505 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
506 gint i;
507
508 for (i=0; i<n_events; i++)
509 result[i] = g_malloc (sizeof (GdkTimeCoord) -
510 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->num_axes));
511
512 return result;
513 }
514
515 /**
516 * gdk_device_free_history:
517 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
518 * @n_events: the length of the array.
519 *
520 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
521 */
522 void
523 gdk_device_free_history (GdkTimeCoord **events,
524 gint n_events)
525 {
526 gint i;
527
528 for (i=0; i<n_events; i++)
529 g_free (events[i]);
530
531 g_free (events);
532 }
533
534 static void
535 unset_extension_events (GdkWindow *window)
536 {
537 GdkWindowObject *window_private;
538 GdkWindowObject *impl_window;
539 GdkDisplayX11 *display_x11;
540 GdkInputWindow *iw;
541
542 window_private = (GdkWindowObject*) window;
543 impl_window = (GdkWindowObject *)_gdk_window_get_impl_window (window);
544 iw = impl_window->input_window;
545
546 display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
547
548 if (window_private->extension_events != 0)
549 {
550 g_assert (iw != NULL);
551 g_assert (g_list_find (iw->windows, window) != NULL);
552
553 iw->windows = g_list_remove (iw->windows, window);
554 if (iw->windows == NULL)
555 {
556 impl_window->input_window = NULL;
557 display_x11->input_windows = g_list_remove (display_x11->input_windows, iw);
558 g_free (iw);
559 }
560 }
561
562 window_private->extension_events = 0;
563 }
564
565 void
566 gdk_input_set_extension_events (GdkWindow *window, gint mask,
567 GdkExtensionMode mode)
568 {
569 GdkWindowObject *window_private;
570 GdkWindowObject *impl_window;
571 GdkInputWindow *iw;
572 GdkDisplayX11 *display_x11;
573 #ifndef XINPUT_NONE
574 GList *tmp_list;
575 #endif
576
577 g_return_if_fail (window != NULL);
578 g_return_if_fail (GDK_WINDOW_IS_X11 (window));
579
580 window_private = (GdkWindowObject*) window;
581 display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
582 if (GDK_WINDOW_DESTROYED (window))
583 return;
584
585 impl_window = (GdkWindowObject *)_gdk_window_get_impl_window (window);
586
587 if (mode == GDK_EXTENSION_EVENTS_ALL && mask != 0)
588 mask |= GDK_ALL_DEVICES_MASK;
589
590 if (mode == GDK_EXTENSION_EVENTS_NONE)
591 mask = 0;
592
593 iw = impl_window->input_window;
594
595 if (mask != 0)
596 {
597 if (!iw)
598 {
599 iw = g_new0 (GdkInputWindow,1);
600
601 iw->impl_window = (GdkWindow *)impl_window;
602
603 iw->windows = NULL;
604 iw->grabbed = FALSE;
605
606 display_x11->input_windows = g_list_append (display_x11->input_windows, iw);
607
608 #ifndef XINPUT_NONE
609 /* we might not receive ConfigureNotify so get the root_relative_geometry
610 * now, just in case */
611 _gdk_input_get_root_relative_geometry (window, &iw->root_x, &iw->root_y);
612 #endif /* !XINPUT_NONE */
613 impl_window->input_window = iw;
614 }
615
616 if (window_private->extension_events == 0)
617 iw->windows = g_list_append (iw->windows, window);
618 window_private->extension_events = mask;
619 }
620 else
621 {
622 unset_extension_events (window);
623 }
624
625 #ifndef XINPUT_NONE
626 for (tmp_list = display_x11->input_devices; tmp_list; tmp_list = tmp_list->next)
627 {
628 GdkDevicePrivate *gdkdev = tmp_list->data;
629
630 if (!GDK_IS_CORE (gdkdev))
631 _gdk_input_select_events ((GdkWindow *)impl_window, gdkdev);
632 }
633 #endif /* !XINPUT_NONE */
634 }
635
636 void
637 _gdk_input_window_destroy (GdkWindow *window)
638 {
639 unset_extension_events (window);
640 }
641
642 /**
643 * gdk_device_get_axis:
644 * @device: a #GdkDevice
645 * @axes: pointer to an array of axes
646 * @use: the use to look for
647 * @value: location to store the found value.
648 *
649 * Interprets an array of double as axis values for a given device,
650 * and locates the value in the array for a given axis use.
651 *
652 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
653 **/
654 gboolean
655 gdk_device_get_axis (GdkDevice *device,
656 gdouble *axes,
657 GdkAxisUse use,
658 gdouble *value)
659 {
660 gint i;
661
662 g_return_val_if_fail (device != NULL, FALSE);
663
664 if (axes == NULL)
665 return FALSE;
666
667 for (i=0; i<device->num_axes; i++)
668 if (device->axes[i].use == use)
669 {
670 if (value)
671 *value = axes[i];
672 return TRUE;
673 }
674
675 return FALSE;
676 }
677
678 #define __GDK_INPUT_C__
679 #include "gdkaliasdef.c"