]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkinput.c
Add headers. Add section about ActiveIMM.
[~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 "gdkinput.h"
37 #include "gdkprivate.h"
38
39 /* When ther necessary stuff is in
40  * gdkinput.h, gdkinternals.h and
41  * gdkprivate.h, these includes shouldn't be here.
42  */
43
44 #include <windows.h>
45 #ifdef HAVE_WINTAB
46 #include <wintab.h>
47 #endif
48 #include "gdkinput-win32.h"
49
50 static GdkDeviceAxis gdk_input_core_axes[] = {
51   { GDK_AXIS_X, 0, 0 },
52   { GDK_AXIS_Y, 0, 0 }
53 };
54
55 /* Global variables  */
56
57 GDKVAR GdkDevice *_gdk_core_pointer = NULL;
58  
59 gint              _gdk_input_ignore_core;
60
61 GList            *_gdk_input_devices;
62 GList            *_gdk_input_windows;
63
64 void
65 _gdk_init_input_core (void)
66 {
67   _gdk_core_pointer = g_object_new (GDK_TYPE_DEVICE, NULL);
68   
69   _gdk_core_pointer->name = "Core Pointer";
70   _gdk_core_pointer->source = GDK_SOURCE_MOUSE;
71   _gdk_core_pointer->mode = GDK_MODE_SCREEN;
72   _gdk_core_pointer->has_cursor = TRUE;
73   _gdk_core_pointer->num_axes = 2;
74   _gdk_core_pointer->axes = gdk_input_core_axes;
75   _gdk_core_pointer->num_keys = 0;
76   _gdk_core_pointer->keys = NULL;
77 }
78
79 static void
80 gdk_device_finalize (GObject *object)
81 {
82   g_error ("A GdkDevice object was finalized. This should not happen");
83 }
84
85 static void
86 gdk_device_class_init (GObjectClass *class)
87 {
88   class->finalize = gdk_device_finalize;
89 }
90
91 GType
92 gdk_device_get_type (void)
93 {
94   static GType object_type = 0;
95
96   if (!object_type)
97     {
98       static const GTypeInfo object_info =
99       {
100         sizeof (GdkDeviceClass),
101         (GBaseInitFunc) NULL,
102         (GBaseFinalizeFunc) NULL,
103         (GClassInitFunc) gdk_device_class_init,
104         NULL,           /* class_finalize */
105         NULL,           /* class_data */
106         sizeof (GdkDevicePrivate),
107         0,              /* n_preallocs */
108         (GInstanceInitFunc) NULL,
109       };
110       
111       object_type = g_type_register_static (G_TYPE_OBJECT,
112                                             "GdkDevice",
113                                             &object_info, 0);
114     }
115   
116   return object_type;
117 }
118
119 GList *
120 gdk_devices_list (void)
121 {
122   return _gdk_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 gboolean
177 gdk_device_get_history  (GdkDevice         *device,
178                          GdkWindow         *window,
179                          guint32            start,
180                          guint32            stop,
181                          GdkTimeCoord    ***events,
182                          gint              *n_events)
183 {
184   g_return_val_if_fail (window != NULL, FALSE);
185   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
186   g_return_val_if_fail (events != NULL, FALSE);
187   g_return_val_if_fail (n_events != NULL, FALSE);
188
189   *n_events = 0;
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       iw = g_new(GdkInputWindow,1);
266
267       iw->window = window;
268       iw->mode = mode;
269
270       iw->obscuring = NULL;
271       iw->num_obscuring = 0;
272       iw->grabbed = FALSE;
273
274       _gdk_input_windows = g_list_append(_gdk_input_windows,iw);
275       window_private->extension_events = mask;
276
277       /* Add enter window events to the event mask */
278       if (g_list_length (_gdk_input_devices) > 1)
279         gdk_window_set_events (window,
280                                gdk_window_get_events (window) | 
281                                GDK_ENTER_NOTIFY_MASK);
282     }
283   else
284     {
285       iw = gdk_input_window_find (window);
286       if (iw)
287         {
288           _gdk_input_windows = g_list_remove(_gdk_input_windows,iw);
289           g_free(iw);
290         }
291
292       window_private->extension_events = 0;
293     }
294
295   for (tmp_list = _gdk_input_devices; tmp_list; tmp_list = tmp_list->next)
296     {
297       GdkDevicePrivate *gdkdev = tmp_list->data;
298
299       if (!GDK_IS_CORE (gdkdev))
300         {
301           if (mask != 0 && gdkdev->info.mode != GDK_MODE_DISABLED
302               && (gdkdev->info.has_cursor || mode == GDK_EXTENSION_EVENTS_ALL))
303             _gdk_input_enable_window (window,gdkdev);
304           else
305             _gdk_input_disable_window (window,gdkdev);
306         }
307     }
308 }
309
310 void
311 gdk_input_window_destroy (GdkWindow *window)
312 {
313   GdkInputWindow *input_window;
314
315   input_window = gdk_input_window_find (window);
316   g_return_if_fail (input_window != NULL);
317
318   _gdk_input_windows = g_list_remove (_gdk_input_windows,input_window);
319   g_free(input_window);
320 }
321
322 void
323 _gdk_input_exit (void)
324 {
325   GList *tmp_list;
326   GdkDevicePrivate *gdkdev;
327
328   for (tmp_list = _gdk_input_devices; tmp_list; tmp_list = tmp_list->next)
329     {
330       gdkdev = (GdkDevicePrivate *)(tmp_list->data);
331       if (!GDK_IS_CORE (gdkdev))
332         {
333           gdk_device_set_mode (&gdkdev->info, GDK_MODE_DISABLED);
334
335           g_free(gdkdev->info.name);
336           g_free(gdkdev->axes);
337           g_free(gdkdev->info.axes);
338           g_free(gdkdev->info.keys);
339           g_free(gdkdev);
340         }
341     }
342
343   g_list_free(_gdk_input_devices);
344
345   for (tmp_list = _gdk_input_windows; tmp_list; tmp_list = tmp_list->next)
346     g_free(tmp_list->data);
347
348   g_list_free(_gdk_input_windows);
349 }
350
351 gboolean
352 gdk_device_get_axis (GdkDevice  *device,
353                      gdouble    *axes,
354                      GdkAxisUse  use,
355                      gdouble    *value)
356 {
357   gint i;
358   
359   g_return_val_if_fail (device != NULL, FALSE);
360
361   if (axes == NULL)
362     return FALSE;
363   
364   for (i=0; i<device->num_axes; i++)
365     if (device->axes[i].use == use)
366       {
367         if (value)
368           *value = axes[i];
369         return TRUE;
370       }
371   
372   return FALSE;
373 }
374
375 gboolean
376 gdk_device_set_mode (GdkDevice   *device,
377                      GdkInputMode mode)
378 {
379   GList *tmp_list;
380   GdkDevicePrivate *gdkdev;
381   GdkInputMode old_mode;
382   GdkInputWindow *input_window;
383
384   if (GDK_IS_CORE (device))
385     return FALSE;
386
387   gdkdev = (GdkDevicePrivate *)device;
388
389   if (device->mode == mode)
390     return TRUE;
391
392   old_mode = device->mode;
393   device->mode = mode;
394
395   if (mode == GDK_MODE_WINDOW)
396     {
397       device->has_cursor = FALSE;
398       for (tmp_list = _gdk_input_windows; tmp_list; tmp_list = tmp_list->next)
399         {
400           input_window = (GdkInputWindow *)tmp_list->data;
401           if (input_window->mode != GDK_EXTENSION_EVENTS_CURSOR)
402             _gdk_input_enable_window (input_window->window, gdkdev);
403           else
404             if (old_mode != GDK_MODE_DISABLED)
405               _gdk_input_disable_window (input_window->window, gdkdev);
406         }
407     }
408   else if (mode == GDK_MODE_SCREEN)
409     {
410       device->has_cursor = TRUE;
411       for (tmp_list = _gdk_input_windows; tmp_list; tmp_list = tmp_list->next)
412         _gdk_input_enable_window (((GdkInputWindow *)tmp_list->data)->window,
413                                   gdkdev);
414     }
415   else  /* mode == GDK_MODE_DISABLED */
416     {
417       for (tmp_list = _gdk_input_windows; tmp_list; tmp_list = tmp_list->next)
418         {
419           input_window = (GdkInputWindow *)tmp_list->data;
420           if (old_mode != GDK_MODE_WINDOW ||
421               input_window->mode != GDK_EXTENSION_EVENTS_CURSOR)
422             _gdk_input_disable_window (input_window->window, gdkdev);
423         }
424     }
425
426   return TRUE;
427 }