]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkinput.c
Build Wintab support always on Windows. Don't require the Wintab SDK.
[~andy/gtk] / gdk / win32 / 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 /* This file should really be one level up, in the backend-independent
28  * GDK, and the x11/gdkinput.c could also be removed.
29  * 
30  * That stuff in x11/gdkinput.c which really *is* X11-dependent should
31  * be in x11/gdkinput-x11.c.
32  */
33
34 #include <config.h>
35
36 #include "gdkdisplay.h"
37 #include "gdkinput.h"
38 #include "gdkprivate.h"
39
40 #include "gdkinput-win32.h"
41
42 static GdkDeviceAxis gdk_input_core_axes[] = {
43   { GDK_AXIS_X, 0, 0 },
44   { GDK_AXIS_Y, 0, 0 }
45 };
46
47 /* Global variables  */
48
49 gint              _gdk_input_ignore_core;
50
51 GList            *_gdk_input_devices;
52 GList            *_gdk_input_windows;
53
54 void
55 _gdk_init_input_core (GdkDisplay *display)
56 {
57   display->core_pointer = g_object_new (GDK_TYPE_DEVICE, NULL);
58   
59   display->core_pointer->name = "Core Pointer";
60   display->core_pointer->source = GDK_SOURCE_MOUSE;
61   display->core_pointer->mode = GDK_MODE_SCREEN;
62   display->core_pointer->has_cursor = TRUE;
63   display->core_pointer->num_axes = 2;
64   display->core_pointer->axes = gdk_input_core_axes;
65   display->core_pointer->num_keys = 0;
66   display->core_pointer->keys = NULL;
67 }
68
69 static void
70 gdk_device_finalize (GObject *object)
71 {
72   g_error ("A GdkDevice object was finalized. This should not happen");
73 }
74
75 static void
76 gdk_device_class_init (GObjectClass *class)
77 {
78   class->finalize = gdk_device_finalize;
79 }
80
81 GType
82 gdk_device_get_type (void)
83 {
84   static GType object_type = 0;
85
86   if (!object_type)
87     {
88       static const GTypeInfo object_info =
89       {
90         sizeof (GdkDeviceClass),
91         (GBaseInitFunc) NULL,
92         (GBaseFinalizeFunc) NULL,
93         (GClassInitFunc) gdk_device_class_init,
94         NULL,           /* class_finalize */
95         NULL,           /* class_data */
96         sizeof (GdkDevicePrivate),
97         0,              /* n_preallocs */
98         (GInstanceInitFunc) NULL,
99       };
100       
101       object_type = g_type_register_static (G_TYPE_OBJECT,
102                                             "GdkDevice",
103                                             &object_info, 0);
104     }
105   
106   return object_type;
107 }
108
109 GList *
110 gdk_devices_list (void)
111 {
112   _gdk_input_wintab_init_check ();
113   return _gdk_input_devices;
114 }
115
116 GList *
117 gdk_display_list_devices (GdkDisplay *dpy)
118 {
119   _gdk_input_wintab_init_check ();
120   return _gdk_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 gboolean
175 gdk_device_get_history  (GdkDevice         *device,
176                          GdkWindow         *window,
177                          guint32            start,
178                          guint32            stop,
179                          GdkTimeCoord    ***events,
180                          gint              *n_events)
181 {
182   g_return_val_if_fail (window != NULL, FALSE);
183   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
184   g_return_val_if_fail (events != NULL, FALSE);
185   g_return_val_if_fail (n_events != NULL, FALSE);
186
187   if (n_events)
188     *n_events = 0;
189   if (events)
190     *events = NULL;
191
192   if (GDK_WINDOW_DESTROYED (window))
193     return FALSE;
194     
195   if (GDK_IS_CORE (device))
196     return FALSE;
197   else
198     return _gdk_device_get_history (device, window, start, stop, events, n_events);
199 }
200
201 GdkTimeCoord ** 
202 _gdk_device_allocate_history (GdkDevice *device,
203                               gint       n_events)
204 {
205   GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
206   gint i;
207
208   for (i=0; i<n_events; i++)
209     result[i] = g_malloc (sizeof (GdkTimeCoord) -
210                           sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->num_axes));
211
212   return result;
213 }
214
215 void 
216 gdk_device_free_history (GdkTimeCoord **events,
217                          gint           n_events)
218 {
219   gint i;
220   
221   for (i=0; i<n_events; i++)
222     g_free (events[i]);
223
224   g_free (events);
225 }
226
227 GdkInputWindow *
228 _gdk_input_window_find(GdkWindow *window)
229 {
230   GList *tmp_list;
231
232   for (tmp_list=_gdk_input_windows; tmp_list; tmp_list=tmp_list->next)
233     if (((GdkInputWindow *)(tmp_list->data))->window == window)
234       return (GdkInputWindow *)(tmp_list->data);
235
236   return NULL;      /* Not found */
237 }
238
239 /* FIXME: this routine currently needs to be called between creation
240    and the corresponding configure event (because it doesn't get the
241    root_relative_geometry).  This should work with
242    gtk_window_set_extension_events, but will likely fail in other
243    cases */
244
245 void
246 gdk_input_set_extension_events (GdkWindow *window, gint mask,
247                                 GdkExtensionMode mode)
248 {
249   GdkWindowObject *window_private;
250   GList *tmp_list;
251   GdkInputWindow *iw;
252
253   g_return_if_fail (window != NULL);
254   g_return_if_fail (GDK_IS_WINDOW (window));
255
256   window_private = (GdkWindowObject*) window;
257   if (GDK_WINDOW_DESTROYED (window))
258     return;
259
260   if (mode == GDK_EXTENSION_EVENTS_NONE)
261     mask = 0;
262
263   if (mask != 0)
264     {
265       _gdk_input_wintab_init_check ();
266
267       iw = g_new(GdkInputWindow,1);
268
269       iw->window = window;
270       iw->mode = mode;
271
272       iw->obscuring = NULL;
273       iw->num_obscuring = 0;
274       iw->grabbed = FALSE;
275
276       _gdk_input_windows = g_list_append(_gdk_input_windows,iw);
277       window_private->extension_events = mask;
278
279       /* Add enter window events to the event mask */
280       if (g_list_length (_gdk_input_devices) > 1)
281         gdk_window_set_events (window,
282                                gdk_window_get_events (window) | 
283                                GDK_ENTER_NOTIFY_MASK);
284     }
285   else
286     {
287       iw = _gdk_input_window_find (window);
288       if (iw)
289         {
290           _gdk_input_windows = g_list_remove(_gdk_input_windows,iw);
291           g_free(iw);
292         }
293
294       window_private->extension_events = 0;
295     }
296
297   for (tmp_list = _gdk_input_devices; tmp_list; tmp_list = tmp_list->next)
298     {
299       GdkDevicePrivate *gdkdev = tmp_list->data;
300
301       if (!GDK_IS_CORE (gdkdev))
302         {
303           if (mask != 0 && gdkdev->info.mode != GDK_MODE_DISABLED
304               && (gdkdev->info.has_cursor || mode == GDK_EXTENSION_EVENTS_ALL))
305             _gdk_input_enable_window (window,gdkdev);
306           else
307             _gdk_input_disable_window (window,gdkdev);
308         }
309     }
310 }
311
312 void
313 _gdk_input_window_destroy (GdkWindow *window)
314 {
315   GdkInputWindow *input_window;
316
317   input_window = _gdk_input_window_find (window);
318   g_return_if_fail (input_window != NULL);
319
320   _gdk_input_windows = g_list_remove (_gdk_input_windows,input_window);
321   g_free(input_window);
322 }
323
324 void
325 _gdk_input_exit (void)
326 {
327   GList *tmp_list;
328   GdkDevicePrivate *gdkdev;
329
330   for (tmp_list = _gdk_input_devices; tmp_list; tmp_list = tmp_list->next)
331     {
332       gdkdev = (GdkDevicePrivate *)(tmp_list->data);
333       if (!GDK_IS_CORE (gdkdev))
334         {
335           gdk_device_set_mode (&gdkdev->info, GDK_MODE_DISABLED);
336
337           g_free(gdkdev->info.name);
338           g_free(gdkdev->axes);
339           g_free(gdkdev->info.axes);
340           g_free(gdkdev->info.keys);
341           g_free(gdkdev);
342         }
343     }
344
345   g_list_free(_gdk_input_devices);
346
347   for (tmp_list = _gdk_input_windows; tmp_list; tmp_list = tmp_list->next)
348     g_free(tmp_list->data);
349
350   g_list_free(_gdk_input_windows);
351 }
352
353 gboolean
354 gdk_device_get_axis (GdkDevice  *device,
355                      gdouble    *axes,
356                      GdkAxisUse  use,
357                      gdouble    *value)
358 {
359   gint i;
360   
361   g_return_val_if_fail (device != NULL, FALSE);
362
363   if (axes == NULL)
364     return FALSE;
365   
366   for (i=0; i<device->num_axes; i++)
367     if (device->axes[i].use == use)
368       {
369         if (value)
370           *value = axes[i];
371         return TRUE;
372       }
373   
374   return FALSE;
375 }
376
377 gboolean
378 gdk_device_set_mode (GdkDevice   *device,
379                      GdkInputMode mode)
380 {
381   GList *tmp_list;
382   GdkDevicePrivate *gdkdev;
383   GdkInputMode old_mode;
384   GdkInputWindow *input_window;
385
386   if (GDK_IS_CORE (device))
387     return FALSE;
388
389   gdkdev = (GdkDevicePrivate *)device;
390
391   if (device->mode == mode)
392     return TRUE;
393
394   old_mode = device->mode;
395   device->mode = mode;
396
397   if (mode == GDK_MODE_WINDOW)
398     {
399       device->has_cursor = FALSE;
400       for (tmp_list = _gdk_input_windows; tmp_list; tmp_list = tmp_list->next)
401         {
402           input_window = (GdkInputWindow *)tmp_list->data;
403           if (input_window->mode != GDK_EXTENSION_EVENTS_CURSOR)
404             _gdk_input_enable_window (input_window->window, gdkdev);
405           else
406             if (old_mode != GDK_MODE_DISABLED)
407               _gdk_input_disable_window (input_window->window, gdkdev);
408         }
409     }
410   else if (mode == GDK_MODE_SCREEN)
411     {
412       device->has_cursor = TRUE;
413       for (tmp_list = _gdk_input_windows; tmp_list; tmp_list = tmp_list->next)
414         _gdk_input_enable_window (((GdkInputWindow *)tmp_list->data)->window,
415                                   gdkdev);
416     }
417   else  /* mode == GDK_MODE_DISABLED */
418     {
419       for (tmp_list = _gdk_input_windows; tmp_list; tmp_list = tmp_list->next)
420         {
421           input_window = (GdkInputWindow *)tmp_list->data;
422           if (old_mode != GDK_MODE_WINDOW ||
423               input_window->mode != GDK_EXTENSION_EVENTS_CURSOR)
424             _gdk_input_disable_window (input_window->window, gdkdev);
425         }
426     }
427
428   return TRUE;
429 }