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