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