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/.
29 #include <X11/Xutil.h>
34 #include "gdkprivate.h"
35 #include "gdkinputprivate.h"
36 #include "gdkscreen-x11.h"
37 #include "gdkdisplay-x11.h"
39 static GdkDeviceAxis gdk_input_core_axes[] = {
45 _gdk_init_input_core (GdkDisplay *display)
47 GdkDevicePrivate *private;
49 display->core_pointer = g_object_new (GDK_TYPE_DEVICE, NULL);
50 private = (GdkDevicePrivate *)display->core_pointer;
52 display->core_pointer->name = "Core Pointer";
53 display->core_pointer->source = GDK_SOURCE_MOUSE;
54 display->core_pointer->mode = GDK_MODE_SCREEN;
55 display->core_pointer->has_cursor = TRUE;
56 display->core_pointer->num_axes = 2;
57 display->core_pointer->axes = gdk_input_core_axes;
58 display->core_pointer->num_keys = 0;
59 display->core_pointer->keys = NULL;
61 private->display = display;
65 gdk_device_get_type (void)
67 static GType object_type = 0;
71 static const GTypeInfo object_info =
73 sizeof (GdkDeviceClass),
75 (GBaseFinalizeFunc) NULL,
76 (GClassInitFunc) NULL,
77 NULL, /* class_finalize */
78 NULL, /* class_data */
79 sizeof (GdkDevicePrivate),
81 (GInstanceInitFunc) NULL,
84 object_type = g_type_register_static (G_TYPE_OBJECT,
95 * Returns the list of available input devices for the default display.
96 * The list is statically allocated and should not be freed.
98 * Return value: a list of #GdkDevice
101 gdk_devices_list (void)
103 return gdk_display_list_devices (gdk_get_default_display ());
107 * gdk_display_list_devices:
108 * @display : a #GdkDisplay
110 * Returns the list of available input devices attached to @display.
111 * The list is statically allocated and should not be freed.
113 * Return value: a list of #GdkDevice
116 gdk_display_list_devices (GdkDisplay *display)
118 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
120 return GDK_DISPLAY_X11 (display)->input_devices;
124 gdk_device_set_source (GdkDevice *device,
125 GdkInputSource source)
127 g_return_if_fail (device != NULL);
129 device->source = source;
133 gdk_device_set_key (GdkDevice *device,
136 GdkModifierType modifiers)
138 g_return_if_fail (device != NULL);
139 g_return_if_fail (index < device->num_keys);
141 device->keys[index].keyval = keyval;
142 device->keys[index].modifiers = modifiers;
146 gdk_device_set_axis_use (GdkDevice *device,
150 g_return_if_fail (device != NULL);
151 g_return_if_fail (index < device->num_axes);
153 device->axes[index].use = use;
159 device->axes[index].min = 0.;
160 device->axes[index].max = 0.;
164 device->axes[index].min = -1.;
165 device->axes[index].max = 1;
168 device->axes[index].min = 0.;
169 device->axes[index].max = 1;
175 gdk_device_get_history (GdkDevice *device,
179 GdkTimeCoord ***events,
182 GdkTimeCoord **coords;
185 g_return_val_if_fail (window != NULL, FALSE);
186 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
187 g_return_val_if_fail (events != NULL, FALSE);
188 g_return_val_if_fail (n_events != NULL, FALSE);
193 if (GDK_WINDOW_DESTROYED (window))
196 if (GDK_IS_CORE (device))
200 xcoords = XGetMotionEvents (GDK_DRAWABLE_XDISPLAY (window),
201 GDK_DRAWABLE_XID (window),
202 start, stop, n_events);
205 coords = _gdk_device_allocate_history (device, *n_events);
206 for (i=0; i<*n_events; i++)
208 coords[i]->time = xcoords[i].time;
209 coords[i]->axes[0] = xcoords[i].x;
210 coords[i]->axes[1] = xcoords[i].y;
222 return _gdk_device_get_history (device, window, start, stop, events, n_events);
226 _gdk_device_allocate_history (GdkDevice *device,
229 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
232 for (i=0; i<n_events; i++)
233 result[i] = g_malloc (sizeof (GdkTimeCoord) -
234 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->num_axes));
240 gdk_device_free_history (GdkTimeCoord **events,
245 for (i=0; i<n_events; i++)
252 gdk_input_window_find(GdkWindow *window)
255 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
257 for (tmp_list=display_x11->input_windows; tmp_list; tmp_list=tmp_list->next)
258 if (((GdkInputWindow *)(tmp_list->data))->window == window)
259 return (GdkInputWindow *)(tmp_list->data);
261 return NULL; /* Not found */
264 /* FIXME: this routine currently needs to be called between creation
265 and the corresponding configure event (because it doesn't get the
266 root_relative_geometry). This should work with
267 gtk_window_set_extension_events, but will likely fail in other
271 gdk_input_set_extension_events (GdkWindow *window, gint mask,
272 GdkExtensionMode mode)
274 GdkWindowObject *window_private;
277 GdkDisplayX11 *display_x11;
279 g_return_if_fail (window != NULL);
280 g_return_if_fail (GDK_IS_WINDOW (window));
282 window_private = (GdkWindowObject*) window;
283 display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
284 if (GDK_WINDOW_DESTROYED (window))
287 if (mode == GDK_EXTENSION_EVENTS_NONE)
292 iw = g_new(GdkInputWindow,1);
297 iw->obscuring = NULL;
298 iw->num_obscuring = 0;
301 display_x11->input_windows = g_list_append(display_x11->input_windows,iw);
302 window_private->extension_events = mask;
304 /* Add enter window events to the event mask */
305 /* FIXME, this is not needed for XINPUT_NONE */
306 gdk_window_set_events (window,
307 gdk_window_get_events (window) |
308 GDK_ENTER_NOTIFY_MASK);
312 iw = gdk_input_window_find (window);
315 display_x11->input_windows = g_list_remove(display_x11->input_windows,iw);
319 window_private->extension_events = 0;
322 for (tmp_list = display_x11->input_devices; tmp_list; tmp_list = tmp_list->next)
324 GdkDevicePrivate *gdkdev = tmp_list->data;
326 if (!GDK_IS_CORE (gdkdev))
328 if (mask != 0 && gdkdev->info.mode != GDK_MODE_DISABLED
329 && (gdkdev->info.has_cursor || mode == GDK_EXTENSION_EVENTS_ALL))
330 _gdk_input_enable_window (window,gdkdev);
332 _gdk_input_disable_window (window,gdkdev);
338 gdk_input_window_destroy (GdkWindow *window)
340 GdkInputWindow *input_window;
341 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
343 input_window = gdk_input_window_find (window);
344 g_return_if_fail (input_window != NULL);
346 display_x11->input_windows = g_list_remove (display_x11->input_windows, input_window);
347 g_free(input_window);
351 _gdk_input_exit (void)
354 GSList *display_list;
355 GdkDevicePrivate *gdkdev;
357 for (display_list = _gdk_displays ; display_list ; display_list = display_list->next)
359 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display_list->data);
361 for (tmp_list = display_x11->input_devices; tmp_list; tmp_list = tmp_list->next)
363 gdkdev = (GdkDevicePrivate *)(tmp_list->data);
364 if (!GDK_IS_CORE (gdkdev))
366 gdk_device_set_mode (&gdkdev->info, GDK_MODE_DISABLED);
368 g_free(gdkdev->info.name);
370 g_free(gdkdev->axes);
372 g_free(gdkdev->info.axes);
373 g_free(gdkdev->info.keys);
378 g_list_free(display_x11->input_devices);
380 for (tmp_list = display_x11->input_windows; tmp_list; tmp_list = tmp_list->next)
381 g_free(tmp_list->data);
383 g_list_free(display_x11->input_windows);
388 * gdk_device_get_axis:
389 * @device: a #GdkDevice
390 * @axes: pointer to an array of axes
391 * @use: the use to look for
392 * @value: location to store the found value.
394 * Interprets an array of double as axis values for a given device,
395 * and locates the value in the array for a given axis use.
397 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
400 gdk_device_get_axis (GdkDevice *device,
407 g_return_val_if_fail (device != NULL, FALSE);
412 for (i=0; i<device->num_axes; i++)
413 if (device->axes[i].use == use)