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/.
31 #include <X11/Xutil.h>
35 #include "gdkprivate.h"
36 #include "gdkinputprivate.h"
37 #include "gdkscreen-x11.h"
38 #include "gdkdisplay-x11.h"
41 static GdkDeviceAxis gdk_input_core_axes[] = {
47 _gdk_init_input_core (GdkDisplay *display)
49 GdkDevicePrivate *private;
51 display->core_pointer = g_object_new (GDK_TYPE_DEVICE, NULL);
52 private = (GdkDevicePrivate *)display->core_pointer;
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;
63 private->display = display;
66 static void gdk_device_class_init (GdkDeviceClass *klass);
67 static void gdk_device_dispose (GObject *object);
69 static gpointer gdk_device_parent_class = NULL;
72 gdk_device_get_type (void)
74 static GType object_type = 0;
78 const GTypeInfo object_info =
80 sizeof (GdkDeviceClass),
82 (GBaseFinalizeFunc) NULL,
83 (GClassInitFunc) gdk_device_class_init,
84 NULL, /* class_finalize */
85 NULL, /* class_data */
86 sizeof (GdkDevicePrivate),
88 (GInstanceInitFunc) NULL,
91 object_type = g_type_register_static (G_TYPE_OBJECT,
92 g_intern_static_string ("GdkDevice"),
100 gdk_device_class_init (GdkDeviceClass *klass)
102 GObjectClass *object_class = G_OBJECT_CLASS (klass);
104 gdk_device_parent_class = g_type_class_peek_parent (klass);
106 object_class->dispose = gdk_device_dispose;
110 gdk_device_dispose (GObject *object)
112 GdkDevicePrivate *gdkdev = (GdkDevicePrivate *) object;
114 if (gdkdev->display && !GDK_IS_CORE (gdkdev))
119 XCloseDevice (GDK_DISPLAY_XDISPLAY (gdkdev->display), gdkdev->xdevice);
120 gdkdev->xdevice = NULL;
122 g_free (gdkdev->axes);
123 g_free (gdkdev->axis_data);
125 gdkdev->axis_data = NULL;
126 #endif /* !XINPUT_NONE */
128 g_free (gdkdev->info.name);
129 g_free (gdkdev->info.keys);
130 g_free (gdkdev->info.axes);
132 gdkdev->info.name = NULL;
133 gdkdev->info.keys = NULL;
134 gdkdev->info.axes = NULL;
137 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
143 * Returns the list of available input devices for the default display.
144 * The list is statically allocated and should not be freed.
146 * Return value: a list of #GdkDevice
149 gdk_devices_list (void)
151 return gdk_display_list_devices (gdk_display_get_default ());
155 * gdk_display_list_devices:
156 * @display: a #GdkDisplay
158 * Returns the list of available input devices attached to @display.
159 * The list is statically allocated and should not be freed.
161 * Return value: a list of #GdkDevice
166 gdk_display_list_devices (GdkDisplay *display)
168 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
170 return GDK_DISPLAY_X11 (display)->input_devices;
174 gdk_device_set_source (GdkDevice *device,
175 GdkInputSource source)
177 g_return_if_fail (device != NULL);
179 device->source = source;
183 gdk_device_set_key (GdkDevice *device,
186 GdkModifierType modifiers)
188 g_return_if_fail (device != NULL);
189 g_return_if_fail (index < device->num_keys);
191 device->keys[index].keyval = keyval;
192 device->keys[index].modifiers = modifiers;
196 gdk_device_set_axis_use (GdkDevice *device,
200 g_return_if_fail (device != NULL);
201 g_return_if_fail (index < device->num_axes);
203 device->axes[index].use = use;
209 device->axes[index].min = 0.;
210 device->axes[index].max = 0.;
214 device->axes[index].min = -1.;
215 device->axes[index].max = 1;
218 device->axes[index].min = 0.;
219 device->axes[index].max = 1;
225 impl_coord_in_window (GdkWindow *window,
229 GdkWindowObject *priv = (GdkWindowObject *)window;
231 if (impl_x < priv->abs_x ||
232 impl_x > priv->abs_x + priv->width)
234 if (impl_y < priv->abs_y ||
235 impl_y > priv->abs_y + priv->height)
241 * gdk_device_get_history:
242 * @device: a #GdkDevice
243 * @window: the window with respect to which which the event coordinates will be reported
244 * @start: starting timestamp for range of events to return
245 * @stop: ending timestamp for the range of events to return
246 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
247 * @n_events: location to store the length of @events, or %NULL
249 * Obtains the motion history for a device; given a starting and
250 * ending timestamp, return all events in the motion history for
251 * the device in the given range of time. Some windowing systems
252 * do not support motion history, in which case, %FALSE will
253 * be returned. (This is not distinguishable from the case where
254 * motion history is supported and no events were found.)
256 * Return value: %TRUE if the windowing system supports motion history and
257 * at least one event was found.
260 gdk_device_get_history (GdkDevice *device,
264 GdkTimeCoord ***events,
267 GdkTimeCoord **coords = NULL;
268 GdkWindow *impl_window;
269 gboolean result = FALSE;
270 int tmp_n_events = 0;
272 g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), FALSE);
274 impl_window = _gdk_window_get_impl_window (window);
276 if (GDK_WINDOW_DESTROYED (window))
278 else if (GDK_IS_CORE (device))
282 xcoords = XGetMotionEvents (GDK_DRAWABLE_XDISPLAY (window),
283 GDK_DRAWABLE_XID (impl_window),
284 start, stop, &tmp_n_events);
287 GdkWindowObject *priv = (GdkWindowObject *)window;
290 coords = _gdk_device_allocate_history (device, tmp_n_events);
293 for (i = 0; i < tmp_n_events; i++)
295 if (impl_coord_in_window (window, xcoords[i].x, xcoords[i].y))
297 coords[j]->time = xcoords[i].time;
298 coords[j]->axes[0] = xcoords[i].x - priv->abs_x;
299 coords[j]->axes[1] = xcoords[i].y - priv->abs_y;
306 /* free the events we allocated too much */
307 for (i = j; i < tmp_n_events; i++)
315 if (tmp_n_events > 0)
321 gdk_device_free_history (coords, tmp_n_events);
327 result = _gdk_device_get_history (device, window, start, stop, &coords, &tmp_n_events);
330 *n_events = tmp_n_events;
335 gdk_device_free_history (coords, tmp_n_events);
341 _gdk_device_allocate_history (GdkDevice *device,
344 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
347 for (i=0; i<n_events; i++)
348 result[i] = g_malloc (sizeof (GdkTimeCoord) -
349 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->num_axes));
355 * gdk_device_free_history:
356 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
357 * @n_events: the length of the array.
359 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
362 gdk_device_free_history (GdkTimeCoord **events,
367 for (i=0; i<n_events; i++)
374 unset_extension_events (GdkWindow *window)
376 GdkWindowObject *window_private;
377 GdkWindowObject *impl_window;
378 GdkDisplayX11 *display_x11;
381 window_private = (GdkWindowObject*) window;
382 impl_window = (GdkWindowObject *)_gdk_window_get_impl_window (window);
383 iw = impl_window->input_window;
385 display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
387 if (window_private->extension_events != 0)
389 g_assert (iw != NULL);
390 g_assert (g_list_find (iw->windows, window) != NULL);
392 iw->windows = g_list_remove (iw->windows, window);
393 if (iw->windows == NULL)
395 impl_window->input_window = NULL;
396 display_x11->input_windows = g_list_remove (display_x11->input_windows, iw);
401 window_private->extension_events = 0;
405 gdk_input_set_extension_events (GdkWindow *window, gint mask,
406 GdkExtensionMode mode)
408 GdkWindowObject *window_private;
409 GdkWindowObject *impl_window;
411 GdkDisplayX11 *display_x11;
416 g_return_if_fail (window != NULL);
417 g_return_if_fail (GDK_WINDOW_IS_X11 (window));
419 window_private = (GdkWindowObject*) window;
420 display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
421 if (GDK_WINDOW_DESTROYED (window))
424 impl_window = (GdkWindowObject *)_gdk_window_get_impl_window (window);
426 if (mode == GDK_EXTENSION_EVENTS_ALL && mask != 0)
427 mask |= GDK_ALL_DEVICES_MASK;
429 if (mode == GDK_EXTENSION_EVENTS_NONE)
432 iw = impl_window->input_window;
438 iw = g_new0 (GdkInputWindow,1);
440 iw->impl_window = (GdkWindow *)impl_window;
445 display_x11->input_windows = g_list_append (display_x11->input_windows, iw);
448 /* we might not receive ConfigureNotify so get the root_relative_geometry
449 * now, just in case */
450 _gdk_input_get_root_relative_geometry (window, &iw->root_x, &iw->root_y);
451 #endif /* !XINPUT_NONE */
452 impl_window->input_window = iw;
455 if (window_private->extension_events == 0)
456 iw->windows = g_list_append (iw->windows, window);
457 window_private->extension_events = mask;
461 unset_extension_events (window);
465 for (tmp_list = display_x11->input_devices; tmp_list; tmp_list = tmp_list->next)
467 GdkDevicePrivate *gdkdev = tmp_list->data;
469 if (!GDK_IS_CORE (gdkdev))
470 _gdk_input_select_events ((GdkWindow *)impl_window, gdkdev);
472 #endif /* !XINPUT_NONE */
476 _gdk_input_window_destroy (GdkWindow *window)
478 unset_extension_events (window);
482 * gdk_device_get_axis:
483 * @device: a #GdkDevice
484 * @axes: pointer to an array of axes
485 * @use: the use to look for
486 * @value: location to store the found value.
488 * Interprets an array of double as axis values for a given device,
489 * and locates the value in the array for a given axis use.
491 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
494 gdk_device_get_axis (GdkDevice *device,
501 g_return_val_if_fail (device != NULL, FALSE);
506 for (i=0; i<device->num_axes; i++)
507 if (device->axes[i].use == use)
517 #define __GDK_INPUT_C__
518 #include "gdkaliasdef.c"