]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkinput.c
Merge in Gdk-custom.c introspection annotations
[~andy/gtk] / gdk / x11 / gdkinput.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 "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: 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 void
174 gdk_device_set_source (GdkDevice      *device,
175                        GdkInputSource  source)
176 {
177   g_return_if_fail (device != NULL);
178
179   device->source = source;
180 }
181
182 void
183 gdk_device_set_key (GdkDevice      *device,
184                     guint           index,
185                     guint           keyval,
186                     GdkModifierType modifiers)
187 {
188   g_return_if_fail (device != NULL);
189   g_return_if_fail (index < device->num_keys);
190
191   device->keys[index].keyval = keyval;
192   device->keys[index].modifiers = modifiers;
193 }
194
195 void
196 gdk_device_set_axis_use (GdkDevice   *device,
197                          guint        index,
198                          GdkAxisUse   use)
199 {
200   g_return_if_fail (device != NULL);
201   g_return_if_fail (index < device->num_axes);
202
203   device->axes[index].use = use;
204
205   switch (use)
206     {
207     case GDK_AXIS_X:
208     case GDK_AXIS_Y:
209       device->axes[index].min = 0.;
210       device->axes[index].max = 0.;
211       break;
212     case GDK_AXIS_XTILT:
213     case GDK_AXIS_YTILT:
214       device->axes[index].min = -1.;
215       device->axes[index].max = 1;
216       break;
217     default:
218       device->axes[index].min = 0.;
219       device->axes[index].max = 1;
220       break;
221     }
222 }
223
224 static gboolean
225 impl_coord_in_window (GdkWindow *window,
226                       int impl_x,
227                       int impl_y)
228 {
229   GdkWindowObject *priv = (GdkWindowObject *)window;
230
231   if (impl_x < priv->abs_x ||
232       impl_x > priv->abs_x + priv->width)
233     return FALSE;
234   if (impl_y < priv->abs_y ||
235       impl_y > priv->abs_y + priv->height)
236     return FALSE;
237   return TRUE;
238 }
239
240 /**
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
248  *
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.)
255  *
256  * Return value: %TRUE if the windowing system supports motion history and
257  *  at least one event was found.
258  **/
259 gboolean
260 gdk_device_get_history  (GdkDevice         *device,
261                          GdkWindow         *window,
262                          guint32            start,
263                          guint32            stop,
264                          GdkTimeCoord    ***events,
265                          gint              *n_events)
266 {
267   GdkTimeCoord **coords = NULL;
268   GdkWindow *impl_window;
269   gboolean result = FALSE;
270   int tmp_n_events = 0;
271
272   g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), FALSE);
273
274   impl_window = _gdk_window_get_impl_window (window);
275
276   if (GDK_WINDOW_DESTROYED (window))
277     /* Nothing */ ;
278   else if (GDK_IS_CORE (device))
279     {
280       XTimeCoord *xcoords;
281
282       xcoords = XGetMotionEvents (GDK_DRAWABLE_XDISPLAY (window),
283                                   GDK_DRAWABLE_XID (impl_window),
284                                   start, stop, &tmp_n_events);
285       if (xcoords)
286         {
287           GdkWindowObject *priv = (GdkWindowObject *)window;
288           int i, j;
289
290           coords = _gdk_device_allocate_history (device, tmp_n_events);
291           j = 0;
292
293           for (i = 0; i < tmp_n_events; i++)
294             {
295               if (impl_coord_in_window (window, xcoords[i].x, xcoords[i].y))
296                 {
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;
300                   j++;
301                 }
302             }
303
304           XFree (xcoords);
305
306           /* free the events we allocated too much */
307           for (i = j; i < tmp_n_events; i++)
308             {
309               g_free (coords[i]);
310               coords[i] = NULL;
311             }
312
313           tmp_n_events = j;
314
315           if (tmp_n_events > 0)
316             {
317               result = TRUE;
318             }
319           else
320             {
321               gdk_device_free_history (coords, tmp_n_events);
322               coords = NULL;
323             }
324         }
325     }
326   else
327     result = _gdk_device_get_history (device, window, start, stop, &coords, &tmp_n_events);
328
329   if (n_events)
330     *n_events = tmp_n_events;
331
332   if (events)
333     *events = coords;
334   else if (coords)
335     gdk_device_free_history (coords, tmp_n_events);
336
337   return result;
338 }
339
340 GdkTimeCoord **
341 _gdk_device_allocate_history (GdkDevice *device,
342                               gint       n_events)
343 {
344   GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
345   gint i;
346
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));
350
351   return result;
352 }
353
354 /**
355  * gdk_device_free_history:
356  * @events: (inout) (transfer none): an array of #GdkTimeCoord.
357  * @n_events: the length of the array.
358  *
359  * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
360  */
361 void
362 gdk_device_free_history (GdkTimeCoord **events,
363                          gint           n_events)
364 {
365   gint i;
366
367   for (i=0; i<n_events; i++)
368     g_free (events[i]);
369
370   g_free (events);
371 }
372
373 static void
374 unset_extension_events (GdkWindow *window)
375 {
376   GdkWindowObject *window_private;
377   GdkWindowObject *impl_window;
378   GdkDisplayX11 *display_x11;
379   GdkInputWindow *iw;
380
381   window_private = (GdkWindowObject*) window;
382   impl_window = (GdkWindowObject *)_gdk_window_get_impl_window (window);
383   iw = impl_window->input_window;
384
385   display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
386
387   if (window_private->extension_events != 0)
388     {
389       g_assert (iw != NULL);
390       g_assert (g_list_find (iw->windows, window) != NULL);
391
392       iw->windows = g_list_remove (iw->windows, window);
393       if (iw->windows == NULL)
394         {
395           impl_window->input_window = NULL;
396           display_x11->input_windows = g_list_remove (display_x11->input_windows, iw);
397           g_free (iw);
398         }
399     }
400
401   window_private->extension_events = 0;
402 }
403
404 void
405 gdk_input_set_extension_events (GdkWindow *window, gint mask,
406                                 GdkExtensionMode mode)
407 {
408   GdkWindowObject *window_private;
409   GdkWindowObject *impl_window;
410   GdkInputWindow *iw;
411   GdkDisplayX11 *display_x11;
412 #ifndef XINPUT_NONE
413   GList *tmp_list;
414 #endif
415
416   g_return_if_fail (window != NULL);
417   g_return_if_fail (GDK_WINDOW_IS_X11 (window));
418
419   window_private = (GdkWindowObject*) window;
420   display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
421   if (GDK_WINDOW_DESTROYED (window))
422     return;
423
424   impl_window = (GdkWindowObject *)_gdk_window_get_impl_window (window);
425
426   if (mode == GDK_EXTENSION_EVENTS_ALL && mask != 0)
427     mask |= GDK_ALL_DEVICES_MASK;
428
429   if (mode == GDK_EXTENSION_EVENTS_NONE)
430     mask = 0;
431
432   iw = impl_window->input_window;
433
434   if (mask != 0)
435     {
436       if (!iw)
437         {
438           iw = g_new0 (GdkInputWindow,1);
439
440           iw->impl_window = (GdkWindow *)impl_window;
441
442           iw->windows = NULL;
443           iw->grabbed = FALSE;
444
445           display_x11->input_windows = g_list_append (display_x11->input_windows, iw);
446
447 #ifndef XINPUT_NONE
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;
453         }
454
455       if (window_private->extension_events == 0)
456         iw->windows = g_list_append (iw->windows, window);
457       window_private->extension_events = mask;
458     }
459   else
460     {
461       unset_extension_events (window);
462     }
463
464 #ifndef XINPUT_NONE
465   for (tmp_list = display_x11->input_devices; tmp_list; tmp_list = tmp_list->next)
466     {
467       GdkDevicePrivate *gdkdev = tmp_list->data;
468
469       if (!GDK_IS_CORE (gdkdev))
470         _gdk_input_select_events ((GdkWindow *)impl_window, gdkdev);
471     }
472 #endif /* !XINPUT_NONE */
473 }
474
475 void
476 _gdk_input_window_destroy (GdkWindow *window)
477 {
478   unset_extension_events (window);
479 }
480
481 /**
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.
487  *
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.
490  *
491  * Return value: %TRUE if the given axis use was found, otherwise %FALSE
492  **/
493 gboolean
494 gdk_device_get_axis (GdkDevice  *device,
495                      gdouble    *axes,
496                      GdkAxisUse  use,
497                      gdouble    *value)
498 {
499   gint i;
500
501   g_return_val_if_fail (device != NULL, FALSE);
502
503   if (axes == NULL)
504     return FALSE;
505
506   for (i=0; i<device->num_axes; i++)
507     if (device->axes[i].use == use)
508       {
509         if (value)
510           *value = axes[i];
511         return TRUE;
512       }
513
514   return FALSE;
515 }
516
517 #define __GDK_INPUT_C__
518 #include "gdkaliasdef.c"