]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkinput.c
f84404c014c134f29fbe4d97e792019c7421778e
[~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 <stdlib.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 #include "config.h"
31
32 #include "gdkx.h"
33 #include "gdkinput.h"
34 #include "gdkprivate.h"
35 #include "gdkinputprivate.h"
36 #include "gdkscreen-x11.h"
37 #include "gdkdisplay-x11.h"
38
39 static GdkDeviceAxis gdk_input_core_axes[] = {
40   { GDK_AXIS_X, 0, 0 },
41   { GDK_AXIS_Y, 0, 0 }
42 };
43
44 void
45 _gdk_init_input_core (GdkDisplay *display)
46 {
47   GdkDevicePrivate *private;
48   
49   display->core_pointer = g_object_new (GDK_TYPE_DEVICE, NULL);
50   private = (GdkDevicePrivate *)display->core_pointer;
51   
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;
60
61   private->display = display;
62 }
63
64 GType
65 gdk_device_get_type (void)
66 {
67   static GType object_type = 0;
68
69   if (!object_type)
70     {
71       static const GTypeInfo object_info =
72       {
73         sizeof (GdkDeviceClass),
74         (GBaseInitFunc) NULL,
75         (GBaseFinalizeFunc) NULL,
76         (GClassInitFunc) NULL,
77         NULL,           /* class_finalize */
78         NULL,           /* class_data */
79         sizeof (GdkDevicePrivate),
80         0,              /* n_preallocs */
81         (GInstanceInitFunc) NULL,
82       };
83       
84       object_type = g_type_register_static (G_TYPE_OBJECT,
85                                             "GdkDevice",
86                                             &object_info, 0);
87     }
88   
89   return object_type;
90 }
91
92 /**
93  * gdk_devices_list:
94  *
95  * Returns the list of available input devices for the default display.
96  * The list is statically allocated and should not be freed.
97  * 
98  * Return value: a list of #GdkDevice
99  **/
100 GList *
101 gdk_devices_list (void)
102 {
103   return gdk_display_list_devices (gdk_display_get_default ());
104 }
105
106 /**
107  * gdk_display_list_devices:
108  * @display : a #GdkDisplay
109  *
110  * Returns the list of available input devices attached to @display.
111  * The list is statically allocated and should not be freed.
112  * 
113  * Return value: a list of #GdkDevice
114  **/
115 GList * 
116 gdk_display_list_devices (GdkDisplay *display)
117 {
118   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
119   
120   return GDK_DISPLAY_X11 (display)->input_devices;
121 }
122
123 void
124 gdk_device_set_source (GdkDevice      *device,
125                        GdkInputSource  source)
126 {
127   g_return_if_fail (device != NULL);
128
129   device->source = source;
130 }
131
132 void
133 gdk_device_set_key (GdkDevice      *device,
134                     guint           index,
135                     guint           keyval,
136                     GdkModifierType modifiers)
137 {
138   g_return_if_fail (device != NULL);
139   g_return_if_fail (index < device->num_keys);
140
141   device->keys[index].keyval = keyval;
142   device->keys[index].modifiers = modifiers;
143 }
144
145 void
146 gdk_device_set_axis_use (GdkDevice   *device,
147                          guint        index,
148                          GdkAxisUse   use)
149 {
150   g_return_if_fail (device != NULL);
151   g_return_if_fail (index < device->num_axes);
152
153   device->axes[index].use = use;
154
155   switch (use)
156     {
157     case GDK_AXIS_X:
158     case GDK_AXIS_Y:
159       device->axes[index].min = 0.;
160       device->axes[index].max = 0.;
161       break;
162     case GDK_AXIS_XTILT:
163     case GDK_AXIS_YTILT:
164       device->axes[index].min = -1.;
165       device->axes[index].max = 1;
166       break;
167     default:
168       device->axes[index].min = 0.;
169       device->axes[index].max = 1;
170       break;
171     }
172 }
173
174 /**
175  * gdk_device_get_history:
176  * @device: a #GdkDevice
177  * @window: the window with respect to which which the event coordinates will be reported
178  * @start: starting timestamp for range of events to return
179  * @stop: ending timestamp for the range of events to return
180  * @events: location to store a newly-allocated array of #GdkTimeCoord, or %NULL
181  * @n_events: location to store the length of @events, or %NULL
182  * 
183  * Obtains the motion history for a device; given a starting and
184  * ending timestamp, return all events in the motion history for
185  * the device in the given range of time. Some windowing systems
186  * do not support motion history, in which case, %FALSE will
187  * be returned. (This is not distinguishable from the case where
188  * motion history is supported and no events were found.)
189  * 
190  * Return value: %TRUE if the windowing system supports motion history and
191  *  at least one event was found.
192  **/
193 gboolean
194 gdk_device_get_history  (GdkDevice         *device,
195                          GdkWindow         *window,
196                          guint32            start,
197                          guint32            stop,
198                          GdkTimeCoord    ***events,
199                          gint              *n_events)
200 {
201   GdkTimeCoord **coords = NULL;
202   gboolean result = FALSE;
203   int tmp_n_events = 0;
204   int i;
205
206   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
207
208   if (GDK_WINDOW_DESTROYED (window))
209     /* Nothing */ ;
210   else if (GDK_IS_CORE (device))
211     {
212       XTimeCoord *xcoords;
213       
214       xcoords = XGetMotionEvents (GDK_DRAWABLE_XDISPLAY (window),
215                                   GDK_DRAWABLE_XID (window),
216                                   start, stop, &tmp_n_events);
217       if (xcoords)
218         {
219           coords = _gdk_device_allocate_history (device, tmp_n_events);
220           for (i=0; i<tmp_n_events; i++)
221             {
222               coords[i]->time = xcoords[i].time;
223               coords[i]->axes[0] = xcoords[i].x;
224               coords[i]->axes[1] = xcoords[i].y;
225             }
226
227           XFree (xcoords);
228
229           result = TRUE;
230         }
231       else
232         result = FALSE;
233     }
234   else
235     result = _gdk_device_get_history (device, window, start, stop, &coords, &tmp_n_events);
236
237   if (n_events)
238     *n_events = tmp_n_events;
239   if (events)
240     *events = coords;
241   else if (coords)
242     gdk_device_free_history (coords, tmp_n_events);
243
244   return result;
245 }
246
247 GdkTimeCoord ** 
248 _gdk_device_allocate_history (GdkDevice *device,
249                               gint       n_events)
250 {
251   GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
252   gint i;
253
254   for (i=0; i<n_events; i++)
255     result[i] = g_malloc (sizeof (GdkTimeCoord) -
256                           sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->num_axes));
257
258   return result;
259 }
260
261 void 
262 gdk_device_free_history (GdkTimeCoord **events,
263                          gint           n_events)
264 {
265   gint i;
266   
267   for (i=0; i<n_events; i++)
268     g_free (events[i]);
269
270   g_free (events);
271 }
272
273 GdkInputWindow *
274 _gdk_input_window_find(GdkWindow *window)
275 {
276   GList *tmp_list;
277   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
278
279   for (tmp_list=display_x11->input_windows; tmp_list; tmp_list=tmp_list->next)
280     if (((GdkInputWindow *)(tmp_list->data))->window == window)
281       return (GdkInputWindow *)(tmp_list->data);
282
283   return NULL;      /* Not found */
284 }
285
286 /* FIXME: this routine currently needs to be called between creation
287    and the corresponding configure event (because it doesn't get the
288    root_relative_geometry).  This should work with
289    gtk_window_set_extension_events, but will likely fail in other
290    cases */
291
292 void
293 gdk_input_set_extension_events (GdkWindow *window, gint mask,
294                                 GdkExtensionMode mode)
295 {
296   GdkWindowObject *window_private;
297   GList *tmp_list;
298   GdkInputWindow *iw;
299   GdkDisplayX11 *display_x11;
300
301   g_return_if_fail (window != NULL);
302   g_return_if_fail (GDK_IS_WINDOW (window));
303
304   window_private = (GdkWindowObject*) window;
305   display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
306   if (GDK_WINDOW_DESTROYED (window))
307     return;
308
309   if (mode == GDK_EXTENSION_EVENTS_NONE)
310     mask = 0;
311
312   if (mask != 0)
313     {
314       iw = g_new(GdkInputWindow,1);
315
316       iw->window = window;
317       iw->mode = mode;
318
319       iw->obscuring = NULL;
320       iw->num_obscuring = 0;
321       iw->grabbed = FALSE;
322
323       display_x11->input_windows = g_list_append(display_x11->input_windows,iw);
324       window_private->extension_events = mask;
325
326       /* Add enter window events to the event mask */
327       /* FIXME, this is not needed for XINPUT_NONE */
328       gdk_window_set_events (window,
329                              gdk_window_get_events (window) | 
330                              GDK_ENTER_NOTIFY_MASK);
331     }
332   else
333     {
334       iw = _gdk_input_window_find (window);
335       if (iw)
336         {
337           display_x11->input_windows = g_list_remove(display_x11->input_windows,iw);
338           g_free(iw);
339         }
340
341       window_private->extension_events = 0;
342     }
343
344   for (tmp_list = display_x11->input_devices; tmp_list; tmp_list = tmp_list->next)
345     {
346       GdkDevicePrivate *gdkdev = tmp_list->data;
347
348       if (!GDK_IS_CORE (gdkdev))
349         {
350           if (mask != 0 && gdkdev->info.mode != GDK_MODE_DISABLED
351               && (gdkdev->info.has_cursor || mode == GDK_EXTENSION_EVENTS_ALL))
352             _gdk_input_enable_window (window,gdkdev);
353           else
354             _gdk_input_disable_window (window,gdkdev);
355         }
356     }
357 }
358
359 void
360 _gdk_input_window_destroy (GdkWindow *window)
361 {
362   GdkInputWindow *input_window;
363   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
364
365   input_window = _gdk_input_window_find (window);
366   g_return_if_fail (input_window != NULL);
367
368   display_x11->input_windows = g_list_remove (display_x11->input_windows, input_window);
369   g_free(input_window);
370 }
371
372 void
373 _gdk_input_exit (void)
374 {
375   GList *tmp_list;
376   GSList *display_list;
377   GdkDevicePrivate *gdkdev;
378
379   for (display_list = _gdk_displays ; display_list ; display_list = display_list->next)
380     {
381       GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display_list->data);
382       
383       for (tmp_list = display_x11->input_devices; tmp_list; tmp_list = tmp_list->next)
384         {
385           gdkdev = (GdkDevicePrivate *)(tmp_list->data);
386           if (!GDK_IS_CORE (gdkdev))
387             {
388               gdk_device_set_mode (&gdkdev->info, GDK_MODE_DISABLED);
389               
390               g_free(gdkdev->info.name);
391 #ifndef XINPUT_NONE       
392               g_free(gdkdev->axes);
393 #endif    
394               g_free(gdkdev->info.axes);
395               g_free(gdkdev->info.keys);
396               g_free(gdkdev);
397             }
398         }
399       
400       g_list_free(display_x11->input_devices);
401       
402       for (tmp_list = display_x11->input_windows; tmp_list; tmp_list = tmp_list->next)
403         g_free(tmp_list->data);
404       
405       g_list_free(display_x11->input_windows);
406     }
407 }
408
409 /**
410  * gdk_device_get_axis:
411  * @device: a #GdkDevice
412  * @axes: pointer to an array of axes
413  * @use: the use to look for
414  * @value: location to store the found value.
415  * 
416  * Interprets an array of double as axis values for a given device,
417  * and locates the value in the array for a given axis use.
418  * 
419  * Return value: %TRUE if the given axis use was found, otherwise %FALSE
420  **/
421 gboolean
422 gdk_device_get_axis (GdkDevice  *device,
423                      gdouble    *axes,
424                      GdkAxisUse  use,
425                      gdouble    *value)
426 {
427   gint i;
428   
429   g_return_val_if_fail (device != NULL, FALSE);
430
431   if (axes == NULL)
432     return FALSE;
433   
434   for (i=0; i<device->num_axes; i++)
435     if (device->axes[i].use == use)
436       {
437         if (value)
438           *value = axes[i];
439         return TRUE;
440       }
441   
442   return FALSE;
443 }