]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkdevicemanager-win32.c
Add gdk_frame_clock_begin/end_updating()
[~andy/gtk] / gdk / win32 / gdkdevicemanager-win32.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <math.h>
23
24 #include <gdk/gdk.h>
25 #include "gdkwin32.h"
26 #include "gdkprivate-win32.h"
27 #include "gdkdevicemanager-win32.h"
28 #include "gdkdeviceprivate.h"
29 #include "gdkdevice-win32.h"
30 #include "gdkdevice-virtual.h"
31 #include "gdkdevice-wintab.h"
32 #include "gdkdisplayprivate.h"
33
34 #define WINTAB32_DLL "Wintab32.dll"
35
36 #define PACKETDATA (PK_CONTEXT | PK_CURSOR | PK_BUTTONS | PK_X | PK_Y  | PK_NORMAL_PRESSURE | PK_ORIENTATION)
37 /* We want everything in absolute mode */
38 #define PACKETMODE (0)
39 #include <pktdef.h>
40
41 #define DEBUG_WINTAB 1          /* Verbose debug messages enabled */
42 #define TWOPI (2 * G_PI)
43
44 static GList     *wintab_contexts = NULL;
45 static GdkWindow *wintab_window = NULL;
46 extern gint       _gdk_input_ignore_core;
47
48 typedef UINT (WINAPI *t_WTInfoA) (UINT a, UINT b, LPVOID c);
49 typedef UINT (WINAPI *t_WTInfoW) (UINT a, UINT b, LPVOID c);
50 typedef BOOL (WINAPI *t_WTEnable) (HCTX a, BOOL b);
51 typedef HCTX (WINAPI *t_WTOpenA) (HWND a, LPLOGCONTEXTA b, BOOL c);
52 typedef BOOL (WINAPI *t_WTGetA) (HCTX a, LPLOGCONTEXTA b);
53 typedef BOOL (WINAPI *t_WTSetA) (HCTX a, LPLOGCONTEXTA b);
54 typedef BOOL (WINAPI *t_WTOverlap) (HCTX a, BOOL b);
55 typedef BOOL (WINAPI *t_WTPacket) (HCTX a, UINT b, LPVOID c);
56 typedef int (WINAPI *t_WTQueueSizeSet) (HCTX a, int b);
57
58 static t_WTInfoA p_WTInfoA;
59 static t_WTInfoW p_WTInfoW;
60 static t_WTEnable p_WTEnable;
61 static t_WTOpenA p_WTOpenA;
62 static t_WTGetA p_WTGetA;
63 static t_WTSetA p_WTSetA;
64 static t_WTOverlap p_WTOverlap;
65 static t_WTPacket p_WTPacket;
66 static t_WTQueueSizeSet p_WTQueueSizeSet;
67
68
69 static void    gdk_device_manager_win32_finalize    (GObject *object);
70 static void    gdk_device_manager_win32_constructed (GObject *object);
71
72 static GList * gdk_device_manager_win32_list_devices (GdkDeviceManager *device_manager,
73                                                       GdkDeviceType     type);
74 static GdkDevice * gdk_device_manager_win32_get_client_pointer (GdkDeviceManager *device_manager);
75
76
77 G_DEFINE_TYPE (GdkDeviceManagerWin32, gdk_device_manager_win32, GDK_TYPE_DEVICE_MANAGER)
78
79 static void
80 gdk_device_manager_win32_class_init (GdkDeviceManagerWin32Class *klass)
81 {
82   GdkDeviceManagerClass *device_manager_class = GDK_DEVICE_MANAGER_CLASS (klass);
83   GObjectClass *object_class = G_OBJECT_CLASS (klass);
84
85   object_class->finalize = gdk_device_manager_win32_finalize;
86   object_class->constructed = gdk_device_manager_win32_constructed;
87   device_manager_class->list_devices = gdk_device_manager_win32_list_devices;
88   device_manager_class->get_client_pointer = gdk_device_manager_win32_get_client_pointer;
89 }
90
91 static GdkDevice *
92 create_pointer (GdkDeviceManager *device_manager, 
93                 GType g_type,
94                 const char *name,
95                 GdkDeviceType type)
96 {
97   return g_object_new (g_type,
98                        "name", name,
99                        "type", type,
100                        "input-source", GDK_SOURCE_MOUSE,
101                        "input-mode", GDK_MODE_SCREEN,
102                        "has-cursor", type == GDK_DEVICE_TYPE_MASTER,
103                        "display", _gdk_display,
104                        "device-manager", device_manager,
105                        NULL);
106 }
107
108 static GdkDevice *
109 create_keyboard (GdkDeviceManager *device_manager, 
110                  GType g_type,
111                  const char *name,
112                  GdkDeviceType type)
113 {
114   return g_object_new (g_type,
115                        "name", name,
116                        "type", type,
117                        "input-source", GDK_SOURCE_KEYBOARD,
118                        "input-mode", GDK_MODE_SCREEN,
119                        "has-cursor", FALSE,
120                        "display", _gdk_display,
121                        "device-manager", device_manager,
122                        NULL);
123 }
124
125 static void
126 gdk_device_manager_win32_init (GdkDeviceManagerWin32 *device_manager_win32)
127 {
128 }
129
130 static void
131 gdk_device_manager_win32_finalize (GObject *object)
132 {
133   GdkDeviceManagerWin32 *device_manager_win32;
134
135   device_manager_win32 = GDK_DEVICE_MANAGER_WIN32 (object);
136
137   g_object_unref (device_manager_win32->core_pointer);
138   g_object_unref (device_manager_win32->core_keyboard);
139
140   G_OBJECT_CLASS (gdk_device_manager_win32_parent_class)->finalize (object);
141 }
142
143 #if DEBUG_WINTAB
144
145 static void
146 print_lc(LOGCONTEXT *lc)
147 {
148   g_print ("lcName = %s\n", lc->lcName);
149   g_print ("lcOptions =");
150   if (lc->lcOptions & CXO_SYSTEM) g_print (" CXO_SYSTEM");
151   if (lc->lcOptions & CXO_PEN) g_print (" CXO_PEN");
152   if (lc->lcOptions & CXO_MESSAGES) g_print (" CXO_MESSAGES");
153   if (lc->lcOptions & CXO_MARGIN) g_print (" CXO_MARGIN");
154   if (lc->lcOptions & CXO_MGNINSIDE) g_print (" CXO_MGNINSIDE");
155   if (lc->lcOptions & CXO_CSRMESSAGES) g_print (" CXO_CSRMESSAGES");
156   g_print ("\n");
157   g_print ("lcStatus =");
158   if (lc->lcStatus & CXS_DISABLED) g_print (" CXS_DISABLED");
159   if (lc->lcStatus & CXS_OBSCURED) g_print (" CXS_OBSCURED");
160   if (lc->lcStatus & CXS_ONTOP) g_print (" CXS_ONTOP");
161   g_print ("\n");
162   g_print ("lcLocks =");
163   if (lc->lcLocks & CXL_INSIZE) g_print (" CXL_INSIZE");
164   if (lc->lcLocks & CXL_INASPECT) g_print (" CXL_INASPECT");
165   if (lc->lcLocks & CXL_SENSITIVITY) g_print (" CXL_SENSITIVITY");
166   if (lc->lcLocks & CXL_MARGIN) g_print (" CXL_MARGIN");
167   g_print ("\n");
168   g_print ("lcMsgBase = %#x, lcDevice = %#x, lcPktRate = %d\n",
169           lc->lcMsgBase, lc->lcDevice, lc->lcPktRate);
170   g_print ("lcPktData =");
171   if (lc->lcPktData & PK_CONTEXT) g_print (" PK_CONTEXT");
172   if (lc->lcPktData & PK_STATUS) g_print (" PK_STATUS");
173   if (lc->lcPktData & PK_TIME) g_print (" PK_TIME");
174   if (lc->lcPktData & PK_CHANGED) g_print (" PK_CHANGED");
175   if (lc->lcPktData & PK_SERIAL_NUMBER) g_print (" PK_SERIAL_NUMBER");
176   if (lc->lcPktData & PK_CURSOR) g_print (" PK_CURSOR");
177   if (lc->lcPktData & PK_BUTTONS) g_print (" PK_BUTTONS");
178   if (lc->lcPktData & PK_X) g_print (" PK_X");
179   if (lc->lcPktData & PK_Y) g_print (" PK_Y");
180   if (lc->lcPktData & PK_Z) g_print (" PK_Z");
181   if (lc->lcPktData & PK_NORMAL_PRESSURE) g_print (" PK_NORMAL_PRESSURE");
182   if (lc->lcPktData & PK_TANGENT_PRESSURE) g_print (" PK_TANGENT_PRESSURE");
183   if (lc->lcPktData & PK_ORIENTATION) g_print (" PK_ORIENTATION");
184   if (lc->lcPktData & PK_ROTATION) g_print (" PK_ROTATION");
185   g_print ("\n");
186   g_print ("lcPktMode =");
187   if (lc->lcPktMode & PK_CONTEXT) g_print (" PK_CONTEXT");
188   if (lc->lcPktMode & PK_STATUS) g_print (" PK_STATUS");
189   if (lc->lcPktMode & PK_TIME) g_print (" PK_TIME");
190   if (lc->lcPktMode & PK_CHANGED) g_print (" PK_CHANGED");
191   if (lc->lcPktMode & PK_SERIAL_NUMBER) g_print (" PK_SERIAL_NUMBER");
192   if (lc->lcPktMode & PK_CURSOR) g_print (" PK_CURSOR");
193   if (lc->lcPktMode & PK_BUTTONS) g_print (" PK_BUTTONS");
194   if (lc->lcPktMode & PK_X) g_print (" PK_X");
195   if (lc->lcPktMode & PK_Y) g_print (" PK_Y");
196   if (lc->lcPktMode & PK_Z) g_print (" PK_Z");
197   if (lc->lcPktMode & PK_NORMAL_PRESSURE) g_print (" PK_NORMAL_PRESSURE");
198   if (lc->lcPktMode & PK_TANGENT_PRESSURE) g_print (" PK_TANGENT_PRESSURE");
199   if (lc->lcPktMode & PK_ORIENTATION) g_print (" PK_ORIENTATION");
200   if (lc->lcPktMode & PK_ROTATION) g_print (" PK_ROTATION");
201   g_print ("\n");
202   g_print ("lcMoveMask =");
203   if (lc->lcMoveMask & PK_CONTEXT) g_print (" PK_CONTEXT");
204   if (lc->lcMoveMask & PK_STATUS) g_print (" PK_STATUS");
205   if (lc->lcMoveMask & PK_TIME) g_print (" PK_TIME");
206   if (lc->lcMoveMask & PK_CHANGED) g_print (" PK_CHANGED");
207   if (lc->lcMoveMask & PK_SERIAL_NUMBER) g_print (" PK_SERIAL_NUMBER");
208   if (lc->lcMoveMask & PK_CURSOR) g_print (" PK_CURSOR");
209   if (lc->lcMoveMask & PK_BUTTONS) g_print (" PK_BUTTONS");
210   if (lc->lcMoveMask & PK_X) g_print (" PK_X");
211   if (lc->lcMoveMask & PK_Y) g_print (" PK_Y");
212   if (lc->lcMoveMask & PK_Z) g_print (" PK_Z");
213   if (lc->lcMoveMask & PK_NORMAL_PRESSURE) g_print (" PK_NORMAL_PRESSURE");
214   if (lc->lcMoveMask & PK_TANGENT_PRESSURE) g_print (" PK_TANGENT_PRESSURE");
215   if (lc->lcMoveMask & PK_ORIENTATION) g_print (" PK_ORIENTATION");
216   if (lc->lcMoveMask & PK_ROTATION) g_print (" PK_ROTATION");
217   g_print ("\n");
218   g_print ("lcBtnDnMask = %#x, lcBtnUpMask = %#x\n",
219           (guint) lc->lcBtnDnMask, (guint) lc->lcBtnUpMask);
220   g_print ("lcInOrgX = %ld, lcInOrgY = %ld, lcInOrgZ = %ld\n",
221           lc->lcInOrgX, lc->lcInOrgY, lc->lcInOrgZ);
222   g_print ("lcInExtX = %ld, lcInExtY = %ld, lcInExtZ = %ld\n",
223           lc->lcInExtX, lc->lcInExtY, lc->lcInExtZ);
224   g_print ("lcOutOrgX = %ld, lcOutOrgY = %ld, lcOutOrgZ = %ld\n",
225           lc->lcOutOrgX, lc->lcOutOrgY, lc->lcOutOrgZ);
226   g_print ("lcOutExtX = %ld, lcOutExtY = %ld, lcOutExtZ = %ld\n",
227           lc->lcOutExtX, lc->lcOutExtY, lc->lcOutExtZ);
228   g_print ("lcSensX = %g, lcSensY = %g, lcSensZ = %g\n",
229           lc->lcSensX / 65536., lc->lcSensY / 65536., lc->lcSensZ / 65536.);
230   g_print ("lcSysMode = %d\n", lc->lcSysMode);
231   g_print ("lcSysOrgX = %d, lcSysOrgY = %d\n",
232           lc->lcSysOrgX, lc->lcSysOrgY);
233   g_print ("lcSysExtX = %d, lcSysExtY = %d\n",
234           lc->lcSysExtX, lc->lcSysExtY);
235   g_print ("lcSysSensX = %g, lcSysSensY = %g\n",
236           lc->lcSysSensX / 65536., lc->lcSysSensY / 65536.);
237 }
238
239 static void
240 print_cursor (int index)
241 {
242   int size;
243   int i;
244   char *name;
245   BOOL active;
246   WTPKT wtpkt;
247   BYTE buttons;
248   BYTE buttonbits;
249   char *btnnames;
250   char *p;
251   BYTE buttonmap[32];
252   BYTE sysbtnmap[32];
253   BYTE npbutton;
254   UINT npbtnmarks[2];
255   UINT *npresponse;
256   BYTE tpbutton;
257   UINT tpbtnmarks[2];
258   UINT *tpresponse;
259   DWORD physid;
260   UINT mode;
261   UINT minpktdata;
262   UINT minbuttons;
263   UINT capabilities;
264
265   size = (*p_WTInfoA) (WTI_CURSORS + index, CSR_NAME, NULL);
266   name = g_malloc (size + 1);
267   (*p_WTInfoA) (WTI_CURSORS + index, CSR_NAME, name);
268   g_print ("NAME: %s\n", name);
269   (*p_WTInfoA) (WTI_CURSORS + index, CSR_ACTIVE, &active);
270   g_print ("ACTIVE: %s\n", active ? "YES" : "NO");
271   (*p_WTInfoA) (WTI_CURSORS + index, CSR_PKTDATA, &wtpkt);
272   g_print ("PKTDATA: %#x:", (guint) wtpkt);
273 #define BIT(x) if (wtpkt & PK_##x) g_print (" " #x)
274   BIT (CONTEXT);
275   BIT (STATUS);
276   BIT (TIME);
277   BIT (CHANGED);
278   BIT (SERIAL_NUMBER);
279   BIT (BUTTONS);
280   BIT (X);
281   BIT (Y);
282   BIT (Z);
283   BIT (NORMAL_PRESSURE);
284   BIT (TANGENT_PRESSURE);
285   BIT (ORIENTATION);
286   BIT (ROTATION);
287 #undef BIT
288   g_print ("\n");
289   (*p_WTInfoA) (WTI_CURSORS + index, CSR_BUTTONS, &buttons);
290   g_print ("BUTTONS: %d\n", buttons);
291   (*p_WTInfoA) (WTI_CURSORS + index, CSR_BUTTONBITS, &buttonbits);
292   g_print ("BUTTONBITS: %d\n", buttonbits);
293   size = (*p_WTInfoA) (WTI_CURSORS + index, CSR_BTNNAMES, NULL);
294   g_print ("BTNNAMES:");
295   if (size > 0)
296     {
297       btnnames = g_malloc (size + 1);
298       (*p_WTInfoA) (WTI_CURSORS + index, CSR_BTNNAMES, btnnames);
299       p = btnnames;
300       while (*p)
301         {
302           g_print (" %s", p);
303           p += strlen (p) + 1;
304         }
305     }
306   g_print ("\n");
307   (*p_WTInfoA) (WTI_CURSORS + index, CSR_BUTTONMAP, buttonmap);
308   g_print ("BUTTONMAP:");
309   for (i = 0; i < buttons; i++)
310     g_print (" %d", buttonmap[i]);
311   g_print ("\n");
312   (*p_WTInfoA) (WTI_CURSORS + index, CSR_SYSBTNMAP, sysbtnmap);
313   g_print ("SYSBTNMAP:");
314   for (i = 0; i < buttons; i++)
315     g_print (" %d", sysbtnmap[i]);
316   g_print ("\n");
317   (*p_WTInfoA) (WTI_CURSORS + index, CSR_NPBUTTON, &npbutton);
318   g_print ("NPBUTTON: %d\n", npbutton);
319   (*p_WTInfoA) (WTI_CURSORS + index, CSR_NPBTNMARKS, npbtnmarks);
320   g_print ("NPBTNMARKS: %d %d\n", npbtnmarks[0], npbtnmarks[1]);
321   size = (*p_WTInfoA) (WTI_CURSORS + index, CSR_NPRESPONSE, NULL);
322   g_print ("NPRESPONSE:");
323   if (size > 0)
324     {
325       npresponse = g_malloc (size);
326       (*p_WTInfoA) (WTI_CURSORS + index, CSR_NPRESPONSE, npresponse);
327       for (i = 0; i < size / sizeof (UINT); i++)
328         g_print (" %d", npresponse[i]);
329     }
330   g_print ("\n");
331   (*p_WTInfoA) (WTI_CURSORS + index, CSR_TPBUTTON, &tpbutton);
332   g_print ("TPBUTTON: %d\n", tpbutton);
333   (*p_WTInfoA) (WTI_CURSORS + index, CSR_TPBTNMARKS, tpbtnmarks);
334   g_print ("TPBTNMARKS: %d %d\n", tpbtnmarks[0], tpbtnmarks[1]);
335   size = (*p_WTInfoA) (WTI_CURSORS + index, CSR_TPRESPONSE, NULL);
336   g_print ("TPRESPONSE:");
337   if (size > 0)
338     {
339       tpresponse = g_malloc (size);
340       (*p_WTInfoA) (WTI_CURSORS + index, CSR_TPRESPONSE, tpresponse);
341       for (i = 0; i < size / sizeof (UINT); i++)
342         g_print (" %d", tpresponse[i]);
343     }
344   g_print ("\n");
345   (*p_WTInfoA) (WTI_CURSORS + index, CSR_PHYSID, &physid);
346   g_print ("PHYSID: %#x\n", (guint) physid);
347   (*p_WTInfoA) (WTI_CURSORS + index, CSR_CAPABILITIES, &capabilities);
348   g_print ("CAPABILITIES: %#x:", capabilities);
349 #define BIT(x) if (capabilities & CRC_##x) g_print (" " #x)
350   BIT (MULTIMODE);
351   BIT (AGGREGATE);
352   BIT (INVERT);
353 #undef BIT
354   g_print ("\n");
355   if (capabilities & CRC_MULTIMODE)
356     {
357       (*p_WTInfoA) (WTI_CURSORS + index, CSR_MODE, &mode);
358       g_print ("MODE: %d\n", mode);
359     }
360   if (capabilities & CRC_AGGREGATE)
361     {
362       (*p_WTInfoA) (WTI_CURSORS + index, CSR_MINPKTDATA, &minpktdata);
363       g_print ("MINPKTDATA: %d\n", minpktdata);
364       (*p_WTInfoA) (WTI_CURSORS + index, CSR_MINBUTTONS, &minbuttons);
365       g_print ("MINBUTTONS: %d\n", minbuttons);
366     }
367 }
368 #endif
369
370 void
371 _gdk_input_wintab_init_check (GdkDeviceManager *_device_manager)
372 {
373   GdkDeviceManagerWin32 *device_manager = (GdkDeviceManagerWin32 *)_device_manager;
374   static gboolean wintab_initialized = FALSE;
375   GdkDeviceWintab *device;
376   GdkWindowAttr wa;
377   WORD specversion;
378   HCTX *hctx;
379   UINT ndevices, ncursors, ncsrtypes, firstcsr, hardware;
380   BOOL active;
381   DWORD physid;
382   AXIS axis_x, axis_y, axis_npressure, axis_or[3];
383   int i, devix, cursorix, num_axes = 0;
384   wchar_t devname[100], csrname[100];
385   gchar *devname_utf8, *csrname_utf8, *device_name;
386   BOOL defcontext_done;
387   HMODULE wintab32;
388   char *wintab32_dll_path;
389   char dummy;
390   int n, k;
391
392   if (wintab_initialized)
393     return;
394
395   wintab_initialized = TRUE;
396
397   wintab_contexts = NULL;
398
399   if (_gdk_input_ignore_wintab)
400     return;
401
402   n = GetSystemDirectory (&dummy, 0);
403
404   if (n <= 0)
405     return;
406
407   wintab32_dll_path = g_malloc (n + 1 + strlen (WINTAB32_DLL));
408   k = GetSystemDirectory (wintab32_dll_path, n);
409   
410   if (k == 0 || k > n)
411     {
412       g_free (wintab32_dll_path);
413       return;
414     }
415
416   if (!G_IS_DIR_SEPARATOR (wintab32_dll_path[strlen (wintab32_dll_path) -1]))
417     strcat (wintab32_dll_path, G_DIR_SEPARATOR_S);
418   strcat (wintab32_dll_path, WINTAB32_DLL);
419
420   if ((wintab32 = LoadLibrary (wintab32_dll_path)) == NULL)
421     return;
422
423   if ((p_WTInfoA = (t_WTInfoA) GetProcAddress (wintab32, "WTInfoA")) == NULL)
424     return;
425   if ((p_WTInfoW = (t_WTInfoW) GetProcAddress (wintab32, "WTInfoW")) == NULL)
426     return;
427   if ((p_WTEnable = (t_WTEnable) GetProcAddress (wintab32, "WTEnable")) == NULL)
428     return;
429   if ((p_WTOpenA = (t_WTOpenA) GetProcAddress (wintab32, "WTOpenA")) == NULL)
430     return;
431   if ((p_WTGetA = (t_WTGetA) GetProcAddress (wintab32, "WTGetA")) == NULL)
432     return;
433   if ((p_WTSetA = (t_WTSetA) GetProcAddress (wintab32, "WTSetA")) == NULL)
434     return;
435   if ((p_WTOverlap = (t_WTOverlap) GetProcAddress (wintab32, "WTOverlap")) == NULL)
436     return;
437   if ((p_WTPacket = (t_WTPacket) GetProcAddress (wintab32, "WTPacket")) == NULL)
438     return;
439   if ((p_WTQueueSizeSet = (t_WTQueueSizeSet) GetProcAddress (wintab32, "WTQueueSizeSet")) == NULL)
440     return;
441
442   if (!(*p_WTInfoA) (0, 0, NULL))
443     return;
444
445   (*p_WTInfoA) (WTI_INTERFACE, IFC_SPECVERSION, &specversion);
446   GDK_NOTE (INPUT, g_print ("Wintab interface version %d.%d\n",
447                             HIBYTE (specversion), LOBYTE (specversion)));
448   (*p_WTInfoA) (WTI_INTERFACE, IFC_NDEVICES, &ndevices);
449   (*p_WTInfoA) (WTI_INTERFACE, IFC_NCURSORS, &ncursors);
450 #if DEBUG_WINTAB
451   GDK_NOTE (INPUT, g_print ("NDEVICES: %d, NCURSORS: %d\n",
452                             ndevices, ncursors));
453 #endif
454   /* Create a dummy window to receive wintab events */
455   wa.wclass = GDK_INPUT_OUTPUT;
456   wa.event_mask = GDK_ALL_EVENTS_MASK;
457   wa.width = 2;
458   wa.height = 2;
459   wa.x = -100;
460   wa.y = -100;
461   wa.window_type = GDK_WINDOW_TOPLEVEL;
462   if ((wintab_window = gdk_window_new (NULL, &wa, GDK_WA_X|GDK_WA_Y)) == NULL)
463     {
464       g_warning ("gdk_input_wintab_init: gdk_window_new failed");
465       return;
466     }
467   g_object_ref (wintab_window);
468
469   for (devix = 0; devix < ndevices; devix++)
470     {
471       LOGCONTEXT lc;
472
473       /* We open the Wintab device (hmm, what if there are several, or
474        * can there even be several, probably not?) as a system
475        * pointing device, i.e. it controls the normal Windows
476        * cursor. This seems much more natural.
477        */
478
479       (*p_WTInfoW) (WTI_DEVICES + devix, DVC_NAME, devname);
480       devname_utf8 = g_utf16_to_utf8 (devname, -1, NULL, NULL, NULL);
481 #ifdef DEBUG_WINTAB
482       GDK_NOTE (INPUT, (g_print("Device %d: %s\n", devix, devname_utf8)));
483 #endif
484       (*p_WTInfoA) (WTI_DEVICES + devix, DVC_NCSRTYPES, &ncsrtypes);
485       (*p_WTInfoA) (WTI_DEVICES + devix, DVC_FIRSTCSR, &firstcsr);
486       (*p_WTInfoA) (WTI_DEVICES + devix, DVC_HARDWARE, &hardware);
487       (*p_WTInfoA) (WTI_DEVICES + devix, DVC_X, &axis_x);
488       (*p_WTInfoA) (WTI_DEVICES + devix, DVC_Y, &axis_y);
489       (*p_WTInfoA) (WTI_DEVICES + devix, DVC_NPRESSURE, &axis_npressure);
490       (*p_WTInfoA) (WTI_DEVICES + devix, DVC_ORIENTATION, axis_or);
491
492       defcontext_done = FALSE;
493       if (HIBYTE (specversion) > 1 || LOBYTE (specversion) >= 1)
494         {
495           /* Try to get device-specific default context */
496           /* Some drivers, e.g. Aiptek, don't provide this info */
497           if ((*p_WTInfoA) (WTI_DSCTXS + devix, 0, &lc) > 0)
498             defcontext_done = TRUE;
499 #if DEBUG_WINTAB
500           if (defcontext_done)
501             GDK_NOTE (INPUT, (g_print("Using device-specific default context\n")));
502           else
503             GDK_NOTE (INPUT, (g_print("Note: Driver did not provide device specific default context info despite claiming to support version 1.1\n")));
504 #endif
505         }
506
507       if (!defcontext_done)
508         (*p_WTInfoA) (WTI_DEFSYSCTX, 0, &lc);
509 #if DEBUG_WINTAB
510       GDK_NOTE (INPUT, (g_print("Default context:\n"), print_lc(&lc)));
511 #endif
512       lc.lcOptions |= CXO_MESSAGES | CXO_CSRMESSAGES;
513       lc.lcStatus = 0;
514       lc.lcMsgBase = WT_DEFBASE;
515       lc.lcPktRate = 0;
516       lc.lcPktData = PACKETDATA;
517       lc.lcPktMode = PACKETMODE;
518       lc.lcMoveMask = PACKETDATA;
519       lc.lcBtnUpMask = lc.lcBtnDnMask = ~0;
520       lc.lcOutOrgX = axis_x.axMin;
521       lc.lcOutOrgY = axis_y.axMin;
522       lc.lcOutExtX = axis_x.axMax - axis_x.axMin + 1;
523       lc.lcOutExtY = axis_y.axMax - axis_y.axMin + 1;
524       lc.lcOutExtY = -lc.lcOutExtY; /* We want Y growing downward */
525 #if DEBUG_WINTAB
526       GDK_NOTE (INPUT, (g_print("context for device %d:\n", devix),
527                         print_lc(&lc)));
528 #endif
529       hctx = g_new (HCTX, 1);
530       if ((*hctx = (*p_WTOpenA) (GDK_WINDOW_HWND (wintab_window), &lc, TRUE)) == NULL)
531         {
532           g_warning ("gdk_input_wintab_init: WTOpen failed");
533           return;
534         }
535       GDK_NOTE (INPUT, g_print ("opened Wintab device %d %p\n",
536                                 devix, *hctx));
537
538       wintab_contexts = g_list_append (wintab_contexts, hctx);
539 #if 0
540       (*p_WTEnable) (*hctx, TRUE);
541 #endif
542       (*p_WTOverlap) (*hctx, TRUE);
543
544 #if DEBUG_WINTAB
545       GDK_NOTE (INPUT, (g_print("context for device %d after WTOpen:\n", devix),
546                         print_lc(&lc)));
547 #endif
548       /* Increase packet queue size to reduce the risk of lost packets.
549        * According to the specs, if the function fails we must try again
550        * with a smaller queue size.
551        */
552       GDK_NOTE (INPUT, g_print("Attempting to increase queue size\n"));
553       for (i = 128; i >= 1; i >>= 1)
554         {
555           if ((*p_WTQueueSizeSet) (*hctx, i))
556             {
557               GDK_NOTE (INPUT, g_print("Queue size set to %d\n", i));
558               break;
559             }
560         }
561       if (!i)
562         GDK_NOTE (INPUT, g_print("Whoops, no queue size could be set\n"));
563       for (cursorix = firstcsr; cursorix < firstcsr + ncsrtypes; cursorix++)
564         {
565 #ifdef DEBUG_WINTAB
566           GDK_NOTE (INPUT, (g_print("Cursor %d:\n", cursorix), print_cursor (cursorix)));
567 #endif
568           active = FALSE;
569           (*p_WTInfoA) (WTI_CURSORS + cursorix, CSR_ACTIVE, &active);
570           if (!active)
571             continue;
572
573           /* Wacom tablets seem to report cursors corresponding to
574            * nonexistent pens or pucks. At least my ArtPad II reports
575            * six cursors: a puck, pressure stylus and eraser stylus,
576            * and then the same three again. I only have a
577            * pressure-sensitive pen. The puck instances, and the
578            * second instances of the styluses report physid zero. So
579            * at least for Wacom, skip cursors with physid zero.
580            */
581           (*p_WTInfoA) (WTI_CURSORS + cursorix, CSR_PHYSID, &physid);
582           if (wcscmp (devname, L"WACOM Tablet") == 0 && physid == 0)
583             continue;
584
585           (*p_WTInfoW) (WTI_CURSORS + cursorix, CSR_NAME, csrname);
586           csrname_utf8 = g_utf16_to_utf8 (csrname, -1, NULL, NULL, NULL);
587           device_name = g_strconcat (devname_utf8, " ", csrname_utf8, NULL);
588
589           device = g_object_new (GDK_TYPE_DEVICE_WINTAB,
590                                  "name", device_name,
591                                  "type", GDK_DEVICE_TYPE_FLOATING,
592                                  "input-source", GDK_SOURCE_PEN,
593                                  "input-mode", GDK_MODE_SCREEN,
594                                  "has-cursor", lc.lcOptions & CXO_SYSTEM,
595                                  "display", _gdk_display,
596                                  "device-manager", device_manager,
597                                  NULL);
598
599           device->sends_core = lc.lcOptions & CXO_SYSTEM;
600           if (device->sends_core)
601             {
602               _gdk_device_set_associated_device (device_manager->system_pointer, GDK_DEVICE (device));
603               _gdk_device_add_slave (device_manager->core_pointer, GDK_DEVICE (device));
604             }
605
606           g_free (csrname_utf8);
607
608           device->hctx = *hctx;
609           device->cursor = cursorix;
610           (*p_WTInfoA) (WTI_CURSORS + cursorix, CSR_PKTDATA, &device->pktdata);
611
612           if (device->pktdata & PK_X)
613             {
614               _gdk_device_add_axis (GDK_DEVICE (device),
615                                     GDK_NONE,
616                                     GDK_AXIS_X,
617                                     axis_x.axMin,
618                                     axis_x.axMax,
619                                     axis_x.axResolution / 65535);
620               num_axes++;
621             }
622
623           if (device->pktdata & PK_Y)
624             {
625               _gdk_device_add_axis (GDK_DEVICE (device),
626                                     GDK_NONE,
627                                     GDK_AXIS_Y,
628                                     axis_y.axMin,
629                                     axis_y.axMax,
630                                     axis_y.axResolution / 65535);
631               num_axes++;
632             }
633
634
635           if (device->pktdata & PK_NORMAL_PRESSURE)
636             {
637               _gdk_device_add_axis (GDK_DEVICE (device),
638                                     GDK_NONE,
639                                     GDK_AXIS_PRESSURE,
640                                     axis_npressure.axMin,
641                                     axis_npressure.axMax,
642                                     axis_npressure.axResolution / 65535);
643               num_axes++;
644             }
645
646           /* The wintab driver for the Wacom ArtPad II reports
647            * PK_ORIENTATION in CSR_PKTDATA, but the tablet doesn't
648            * actually sense tilt. Catch this by noticing that the
649            * orientation axis's azimuth resolution is zero.
650            */
651           if ((device->pktdata & PK_ORIENTATION) && axis_or[0].axResolution == 0)
652             {
653               device->orientation_axes[0] = axis_or[0];
654               device->orientation_axes[1] = axis_or[1];
655
656               /* Wintab gives us aximuth and altitude, which
657                * we convert to x and y tilt in the -1000..1000 range
658                */
659               _gdk_device_add_axis (GDK_DEVICE (device),
660                                     GDK_NONE,
661                                     GDK_AXIS_XTILT,
662                                     -1000,
663                                     1000,
664                                     1000);
665
666               _gdk_device_add_axis (GDK_DEVICE (device),
667                                     GDK_NONE,
668                                     GDK_AXIS_YTILT,
669                                     -1000,
670                                     1000,
671                                     1000);
672               num_axes += 2;
673             }
674
675           device->last_axis_data = g_new (gint, num_axes);
676
677           GDK_NOTE (INPUT, g_print ("device: (%d) %s axes: %d\n",
678                                     cursorix,
679                                     device_name,
680                                     num_axes));
681
682 #if 0
683           for (i = 0; i < gdkdev->info.num_axes; i++)
684             GDK_NOTE (INPUT, g_print ("... axis %d: %d--%d@%d\n",
685                                       i,
686                                       gdkdev->axes[i].min_value,
687                                       gdkdev->axes[i].max_value,
688                                       gdkdev->axes[i].resolution));
689 #endif
690
691           device_manager->wintab_devices = g_list_append (device_manager->wintab_devices,
692                                                           device);
693
694           g_free (device_name);
695         }
696
697       g_free (devname_utf8);
698     }
699 }
700
701 static void
702 gdk_device_manager_win32_constructed (GObject *object)
703 {
704   GdkDeviceManagerWin32 *device_manager;
705
706   device_manager = GDK_DEVICE_MANAGER_WIN32 (object);
707   device_manager->core_pointer = 
708     create_pointer (GDK_DEVICE_MANAGER (device_manager),
709                     GDK_TYPE_DEVICE_VIRTUAL,
710                     "Virtual Core Pointer",
711                     GDK_DEVICE_TYPE_MASTER);
712   device_manager->system_pointer = 
713     create_pointer (GDK_DEVICE_MANAGER (device_manager),
714                     GDK_TYPE_DEVICE_WIN32,
715                     "System Aggregated Pointer",
716                     GDK_DEVICE_TYPE_SLAVE);
717   _gdk_device_virtual_set_active (device_manager->core_pointer,
718                                   device_manager->system_pointer);
719   _gdk_device_set_associated_device (device_manager->system_pointer, device_manager->core_pointer);
720   _gdk_device_add_slave (device_manager->core_pointer, device_manager->system_pointer);
721
722   device_manager->core_keyboard = 
723     create_keyboard (GDK_DEVICE_MANAGER (device_manager),
724                      GDK_TYPE_DEVICE_VIRTUAL,
725                      "Virtual Core Keyboard",
726                      GDK_DEVICE_TYPE_MASTER);
727   device_manager->system_keyboard = 
728     create_keyboard (GDK_DEVICE_MANAGER (device_manager),
729                     GDK_TYPE_DEVICE_WIN32,
730                      "System Aggregated Keyboard",
731                      GDK_DEVICE_TYPE_SLAVE);
732   _gdk_device_virtual_set_active (device_manager->core_keyboard,
733                                   device_manager->system_keyboard);
734   _gdk_device_set_associated_device (device_manager->system_keyboard, device_manager->core_keyboard);
735   _gdk_device_add_slave (device_manager->core_keyboard, device_manager->system_keyboard);
736
737   _gdk_device_set_associated_device (device_manager->core_pointer, device_manager->core_keyboard);
738   _gdk_device_set_associated_device (device_manager->core_keyboard, device_manager->core_pointer);
739 }
740
741 static GList *
742 gdk_device_manager_win32_list_devices (GdkDeviceManager *device_manager,
743                                        GdkDeviceType     type)
744 {
745   GdkDeviceManagerWin32 *device_manager_win32;
746   GList *devices = NULL, *l;
747
748   device_manager_win32 = (GdkDeviceManagerWin32 *) device_manager;
749
750   if (type == GDK_DEVICE_TYPE_MASTER)
751     {
752       devices = g_list_prepend (devices, device_manager_win32->core_keyboard);
753       devices = g_list_prepend (devices, device_manager_win32->core_pointer);
754     }
755   else 
756     {
757       if (type == GDK_DEVICE_TYPE_SLAVE)
758         {
759           devices = g_list_prepend (devices, device_manager_win32->system_keyboard);
760           devices = g_list_prepend (devices, device_manager_win32->system_pointer);
761         }
762
763       for (l = device_manager_win32->wintab_devices; l != NULL; l = l->next)
764         {
765           GdkDevice *device = l->data;
766
767           if (gdk_device_get_device_type (device) == type)
768             devices = g_list_prepend (devices, device);
769         }
770     }
771
772   return g_list_reverse (devices);
773 }
774
775 static GdkDevice *
776 gdk_device_manager_win32_get_client_pointer (GdkDeviceManager *device_manager)
777 {
778   GdkDeviceManagerWin32 *device_manager_win32;
779
780   device_manager_win32 = (GdkDeviceManagerWin32 *) device_manager;
781   return device_manager_win32->core_pointer;
782 }
783
784 void
785 _gdk_input_set_tablet_active (void)
786 {
787   GList *tmp_list;
788   HCTX *hctx;
789
790   /* Bring the contexts to the top of the overlap order when one of the
791    * application's windows is activated */
792
793   if (!wintab_contexts)
794     return; /* No tablet devices found, or Wintab not initialized yet */
795
796   GDK_NOTE (INPUT, g_print ("_gdk_input_set_tablet_active: "
797                             "Bringing Wintab contexts to the top of the overlap order\n"));
798
799   tmp_list = wintab_contexts;
800
801   while (tmp_list)
802     {
803       hctx = (HCTX *) (tmp_list->data);
804       (*p_WTOverlap) (*hctx, TRUE);
805       tmp_list = tmp_list->next;
806     }
807 }
808
809 static void
810 decode_tilt (gint   *axis_data,
811              AXIS   *axes,
812              PACKET *packet)
813 {
814   double az, el;
815
816   /* As I don't have a tilt-sensing tablet,
817    * I cannot test this code.
818    */
819   az = TWOPI * packet->pkOrientation.orAzimuth /
820     (axes[0].axResolution / 65536.);
821   el = TWOPI * packet->pkOrientation.orAltitude /
822     (axes[1].axResolution / 65536.);
823
824   /* X tilt */
825   axis_data[0] = cos (az) * cos (el) * 1000;
826   /* Y tilt */
827   axis_data[1] = sin (az) * cos (el) * 1000;
828 }
829
830 /*
831  * Get the currently active keyboard modifiers (ignoring the mouse buttons)
832  * We could use gdk_window_get_pointer but that function does a lot of other
833  * expensive things besides getting the modifiers. This code is somewhat based
834  * on build_pointer_event_state from gdkevents-win32.c
835  */
836 static guint
837 get_modifier_key_state (void)
838 {
839   guint state;
840
841   state = 0;
842   /* High-order bit is up/down, low order bit is toggled/untoggled */
843   if (GetKeyState (VK_CONTROL) < 0)
844     state |= GDK_CONTROL_MASK;
845   if (GetKeyState (VK_SHIFT) < 0)
846     state |= GDK_SHIFT_MASK;
847   if (GetKeyState (VK_MENU) < 0)
848     state |= GDK_MOD1_MASK;
849   if (GetKeyState (VK_CAPITAL) & 0x1)
850     state |= GDK_LOCK_MASK;
851
852   return state;
853 }
854
855 static GdkDeviceWintab *
856 _gdk_device_manager_find_wintab_device (HCTX hctx,
857                                         UINT cursor)
858 {
859   GdkDeviceManagerWin32 *device_manager;
860   GdkDeviceWintab *device;
861   GList *tmp_list;
862
863   device_manager = GDK_DEVICE_MANAGER_WIN32 (gdk_display_get_device_manager (_gdk_display));
864   tmp_list = device_manager->wintab_devices;
865
866   while (tmp_list)
867     {
868       device = tmp_list->data;
869       tmp_list = tmp_list->next;
870
871       if (device->hctx == hctx &&
872           device->cursor == cursor)
873         return device;
874     }
875
876   return NULL;
877 }
878
879 gboolean
880 _gdk_input_other_event (GdkEvent  *event,
881                         MSG       *msg,
882                         GdkWindow *window)
883 {
884   GdkDeviceManagerWin32 *device_manager;
885   GdkDisplay *display;
886   GdkDeviceWintab *source_device = NULL;
887   GdkDeviceGrabInfo *last_grab;
888   GdkEventMask masktest;
889   guint key_state;
890   POINT pt;
891
892   PACKET packet;
893   gint root_x, root_y;
894   gint num_axes;
895   gint x, y;
896   guint translated_buttons, button_diff, button_mask;
897   /* Translation from tablet button state to GDK button state for
898    * buttons 1-3 - swap button 2 and 3.
899    */
900   static guint button_map[8] = {0, 1, 4, 5, 2, 3, 6, 7};
901
902   if (event->any.window != wintab_window)
903     {
904       g_warning ("_gdk_input_other_event: not wintab_window?");
905       return FALSE;
906     }
907
908   device_manager = GDK_DEVICE_MANAGER_WIN32 (gdk_display_get_device_manager (_gdk_display));
909
910   window = gdk_window_at_pointer (&x, &y);
911   if (window == NULL)
912     window = _gdk_root;
913
914   g_object_ref (window);
915   display = gdk_window_get_display (window);
916
917   GDK_NOTE (EVENTS_OR_INPUT,
918             g_print ("_gdk_input_other_event: window=%p %+d%+d\n",
919                GDK_WINDOW_HWND (window), x, y));
920
921   if (msg->message == WT_PACKET || msg->message == WT_CSRCHANGE)
922     {
923       if (!(*p_WTPacket) ((HCTX) msg->lParam, msg->wParam, &packet))
924         return FALSE;
925     }
926
927   switch (msg->message)
928     {
929     case WT_PACKET:
930       /* Don't produce any button or motion events while a window is being
931        * moved or resized, see bug #151090.
932        */
933       if (_modal_operation_in_progress)
934         {
935           GDK_NOTE (EVENTS_OR_INPUT, g_print ("... ignored when moving/sizing\n"));
936           return FALSE;
937         }
938
939       if ((source_device = _gdk_device_manager_find_wintab_device ((HCTX) msg->lParam,
940                                                                    packet.pkCursor)) == NULL)
941         return FALSE;
942
943       if (gdk_device_get_mode (GDK_DEVICE (source_device)) == GDK_MODE_DISABLED)
944         return FALSE;
945
946       last_grab = _gdk_display_get_last_device_grab (_gdk_display, GDK_DEVICE (source_device));
947
948       if (last_grab && last_grab->window)
949         {
950           g_object_unref (window);
951
952           window = g_object_ref (last_grab->window);
953         }
954
955       if (window == _gdk_root)
956         {
957           GDK_NOTE (EVENTS_OR_INPUT, g_print ("... is root\n"));
958           return FALSE;
959         }
960
961       num_axes = 0;
962       if (source_device->pktdata & PK_X)
963         source_device->last_axis_data[num_axes++] = packet.pkX;
964       if (source_device->pktdata & PK_Y)
965         source_device->last_axis_data[num_axes++] = packet.pkY;
966       if (source_device->pktdata & PK_NORMAL_PRESSURE)
967         source_device->last_axis_data[num_axes++] = packet.pkNormalPressure;
968       if (source_device->pktdata & PK_ORIENTATION)
969         {
970           decode_tilt (source_device->last_axis_data + num_axes,
971                        source_device->orientation_axes, &packet);
972           num_axes += 2;
973         }
974
975       translated_buttons = button_map[packet.pkButtons & 0x07] | (packet.pkButtons & ~0x07);
976
977       if (translated_buttons != source_device->button_state)
978         {
979           /* At least one button has changed state so produce a button event
980            * If more than one button has changed state (unlikely),
981            * just care about the first and act on the next the next time
982            * we get a packet
983            */
984           button_diff = translated_buttons ^ source_device->button_state;
985
986           /* Gdk buttons are numbered 1.. */
987           event->button.button = 1;
988
989           for (button_mask = 1; button_mask != 0x80000000;
990                button_mask <<= 1, event->button.button++)
991             {
992               if (button_diff & button_mask)
993                 {
994                   /* Found a button that has changed state */
995                   break;
996                 }
997             }
998
999           if (!(translated_buttons & button_mask))
1000             {
1001               event->any.type = GDK_BUTTON_RELEASE;
1002               masktest = GDK_BUTTON_RELEASE_MASK;
1003             }
1004           else
1005             {
1006               event->any.type = GDK_BUTTON_PRESS;
1007               masktest = GDK_BUTTON_PRESS_MASK;
1008             }
1009           source_device->button_state ^= button_mask;
1010         }
1011       else
1012         {
1013           event->any.type = GDK_MOTION_NOTIFY;
1014           masktest = GDK_POINTER_MOTION_MASK;
1015           if (source_device->button_state & (1 << 0))
1016             masktest |= GDK_BUTTON_MOTION_MASK | GDK_BUTTON1_MOTION_MASK;
1017           if (source_device->button_state & (1 << 1))
1018             masktest |= GDK_BUTTON_MOTION_MASK | GDK_BUTTON2_MOTION_MASK;
1019           if (source_device->button_state & (1 << 2))
1020             masktest |= GDK_BUTTON_MOTION_MASK | GDK_BUTTON3_MOTION_MASK;
1021         }
1022
1023       /* Now we can check if the window wants the event, and
1024        * propagate if necessary.
1025        */
1026       while ((gdk_window_get_device_events (window, GDK_DEVICE (source_device)) & masktest) == 0 &&
1027              (gdk_device_get_device_type (GDK_DEVICE (source_device)) == GDK_DEVICE_TYPE_SLAVE &&
1028               (gdk_window_get_events (window) & masktest) == 0))
1029         {
1030           GDK_NOTE (EVENTS_OR_INPUT, g_print ("... not selected\n"));
1031
1032           if (window->parent == GDK_WINDOW (_gdk_root) ||
1033               window->parent == NULL)
1034             return FALSE;
1035
1036           pt.x = x;
1037           pt.y = y;
1038           ClientToScreen (GDK_WINDOW_HWND (window), &pt);
1039           g_object_unref (window);
1040           window = window->parent;
1041           g_object_ref (window);
1042           ScreenToClient (GDK_WINDOW_HWND (window), &pt);
1043           x = pt.x;
1044           y = pt.y;
1045           GDK_NOTE (EVENTS_OR_INPUT, g_print ("... propagating to %p %+d%+d\n",
1046                                               GDK_WINDOW_HWND (window), x, y));
1047         }
1048
1049       event->any.window = window;
1050       key_state = get_modifier_key_state ();
1051       if (event->any.type == GDK_BUTTON_PRESS ||
1052           event->any.type == GDK_BUTTON_RELEASE)
1053         {
1054           event->button.time = _gdk_win32_get_next_tick (msg->time);
1055           if (source_device->sends_core)
1056             gdk_event_set_device (event, device_manager->core_pointer);
1057           gdk_event_set_source_device (event, GDK_DEVICE (source_device));
1058
1059           event->button.axes = g_new (gdouble, num_axes);
1060           gdk_window_get_origin (window, &root_x, &root_y);
1061
1062           _gdk_device_wintab_translate_axes (source_device,
1063                                              window,
1064                                              event->button.axes,
1065                                              &event->button.x,
1066                                              &event->button.y);
1067
1068           event->button.x_root = event->button.x + root_x;
1069           event->button.y_root = event->button.y + root_y;
1070
1071           event->button.state =
1072             key_state | ((source_device->button_state << 8)
1073                          & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK
1074                             | GDK_BUTTON3_MASK | GDK_BUTTON4_MASK
1075                             | GDK_BUTTON5_MASK));
1076
1077           GDK_NOTE (EVENTS_OR_INPUT,
1078                     g_print ("WINTAB button %s:%d %g,%g\n",
1079                              (event->button.type == GDK_BUTTON_PRESS ?
1080                               "press" : "release"),
1081                              event->button.button,
1082                              event->button.x, event->button.y));
1083         }
1084       else
1085         {
1086           event->motion.time = _gdk_win32_get_next_tick (msg->time);
1087           event->motion.is_hint = FALSE;
1088           gdk_event_set_device (event, device_manager->core_pointer);
1089           gdk_event_set_source_device (event, GDK_DEVICE (source_device));
1090
1091           event->motion.axes = g_new (gdouble, num_axes);
1092           gdk_window_get_origin (window, &root_x, &root_y);
1093
1094           _gdk_device_wintab_translate_axes (source_device,
1095                                              window,
1096                                              event->motion.axes,
1097                                              &event->motion.x,
1098                                              &event->motion.y);
1099
1100           event->motion.x_root = event->motion.x + root_x;
1101           event->motion.y_root = event->motion.y + root_y;
1102
1103           event->motion.state =
1104             key_state | ((source_device->button_state << 8)
1105                          & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK
1106                             | GDK_BUTTON3_MASK | GDK_BUTTON4_MASK
1107                             | GDK_BUTTON5_MASK));
1108
1109           GDK_NOTE (EVENTS_OR_INPUT,
1110                     g_print ("WINTAB motion: %g,%g\n",
1111                              event->motion.x, event->motion.y));
1112         }
1113       return TRUE;
1114
1115     case WT_CSRCHANGE:
1116       if ((source_device = _gdk_device_manager_find_wintab_device ((HCTX) msg->lParam,
1117                                                                    packet.pkCursor)) == NULL)
1118         return FALSE;
1119
1120       if (gdk_device_get_mode (GDK_DEVICE (source_device)) == GDK_MODE_DISABLED)
1121         return FALSE;
1122
1123       if (source_device->sends_core)
1124         {
1125           _gdk_device_virtual_set_active (device_manager->core_pointer, GDK_DEVICE (source_device));
1126           _gdk_input_ignore_core = TRUE;
1127         }
1128
1129       return FALSE;
1130
1131     case WT_PROXIMITY:
1132       if (LOWORD (msg->lParam) == 0)
1133         {
1134           _gdk_input_ignore_core = FALSE;
1135           _gdk_device_virtual_set_active (device_manager->core_pointer,
1136                                           device_manager->system_pointer);
1137         }
1138
1139       return FALSE;
1140     }
1141
1142   return FALSE;
1143 }