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