winex11: Add window data structure locking to the window region functions.
[wine] / dlls / winex11.drv / wintab.c
1 /*
2  * X11 tablet driver
3  *
4  * Copyright 2003 CodeWeavers (Aric Stewart)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdlib.h>
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "x11drv.h"
31 #include "wine/library.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
34 #include "wintab.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(wintab32);
37
38 #define WT_MAX_NAME_LEN 256
39
40 typedef struct tagWTI_CURSORS_INFO
41 {
42     WCHAR   NAME[WT_MAX_NAME_LEN];
43         /* a displayable zero-terminated string containing the name of the
44          * cursor.
45          */
46     BOOL    ACTIVE;
47         /* whether the cursor is currently connected. */
48     WTPKT   PKTDATA;
49         /* a bit mask indicating the packet data items supported when this
50          * cursor is connected.
51          */
52     BYTE    BUTTONS;
53         /* the number of buttons on this cursor. */
54     BYTE    BUTTONBITS;
55         /* the number of bits of raw button data returned by the hardware.*/
56     DWORD   cchBTNNAMES;
57     WCHAR   *BTNNAMES;
58         /* a list of zero-terminated strings containing the names of the
59          * cursor's buttons. The number of names in the list is the same as the
60          * number of buttons on the cursor. The names are separated by a single
61          * zero character; the list is terminated by two zero characters.
62          */
63     BYTE    BUTTONMAP[32];
64         /* a 32 byte array of logical button numbers, one for each physical
65          * button.
66          */
67     BYTE    SYSBTNMAP[32];
68         /* a 32 byte array of button action codes, one for each logical
69          * button.
70          */
71     BYTE    NPBUTTON;
72         /* the physical button number of the button that is controlled by normal
73          * pressure.
74          */
75     UINT    NPBTNMARKS[2];
76         /* an array of two UINTs, specifying the button marks for the normal
77          * pressure button. The first UINT contains the release mark; the second
78          * contains the press mark.
79          */
80     UINT    *NPRESPONSE;
81         /* an array of UINTs describing the pressure response curve for normal
82          * pressure.
83          */
84     BYTE    TPBUTTON;
85         /* the physical button number of the button that is controlled by
86          * tangential pressure.
87          */
88     UINT    TPBTNMARKS[2];
89         /* an array of two UINTs, specifying the button marks for the tangential
90          * pressure button. The first UINT contains the release mark; the second
91          * contains the press mark.
92          */
93     UINT    *TPRESPONSE;
94         /* an array of UINTs describing the pressure response curve for
95          * tangential pressure.
96          */
97     DWORD   PHYSID;
98          /* a manufacturer-specific physical identifier for the cursor. This
99           * value will distinguish the physical cursor from others on the same
100           * device. This physical identifier allows applications to bind
101           * functions to specific physical cursors, even if category numbers
102           * change and multiple, otherwise identical, physical cursors are
103           * present.
104           */
105     UINT    MODE;
106         /* the cursor mode number of this cursor type, if this cursor type has
107          * the CRC_MULTIMODE capability.
108          */
109     UINT    MINPKTDATA;
110         /* the minimum set of data available from a physical cursor in this
111          * cursor type, if this cursor type has the CRC_AGGREGATE capability.
112          */
113     UINT    MINBUTTONS;
114         /* the minimum number of buttons of physical cursors in the cursor type,
115          * if this cursor type has the CRC_AGGREGATE capability.
116          */
117     UINT    CAPABILITIES;
118         /* flags indicating cursor capabilities, as defined below:
119             CRC_MULTIMODE
120                 Indicates this cursor type describes one of several modes of a
121                 single physical cursor. Consecutive cursor type categories
122                 describe the modes; the CSR_MODE data item gives the mode number
123                 of each cursor type.
124             CRC_AGGREGATE
125                 Indicates this cursor type describes several physical cursors
126                 that cannot be distinguished by software.
127             CRC_INVERT
128                 Indicates this cursor type describes the physical cursor in its
129                 inverted orientation; the previous consecutive cursor type
130                 category describes the normal orientation.
131          */
132     UINT    TYPE;
133         /* Manufacturer Unique id for the item type */
134 } WTI_CURSORS_INFO, *LPWTI_CURSORS_INFO;
135
136
137 typedef struct tagWTI_DEVICES_INFO
138 {
139     WCHAR   NAME[WT_MAX_NAME_LEN];
140         /* a displayable null- terminated string describing the device,
141          * manufacturer, and revision level.
142          */
143     UINT    HARDWARE;
144         /* flags indicating hardware and driver capabilities, as defined
145          * below:
146             HWC_INTEGRATED:
147                 Indicates that the display and digitizer share the same surface.
148             HWC_TOUCH
149                 Indicates that the cursor must be in physical contact with the
150                 device to report position.
151             HWC_HARDPROX
152                 Indicates that device can generate events when the cursor is
153                 entering and leaving the physical detection range.
154             HWC_PHYSID_CURSORS
155                 Indicates that device can uniquely identify the active cursor in
156                 hardware.
157          */
158     UINT    NCSRTYPES;
159         /* the number of supported cursor types.*/
160     UINT    FIRSTCSR;
161         /* the first cursor type number for the device. */
162     UINT    PKTRATE;
163         /* the maximum packet report rate in Hertz. */
164     WTPKT   PKTDATA;
165         /* a bit mask indicating which packet data items are always available.*/
166     WTPKT   PKTMODE;
167         /* a bit mask indicating which packet data items are physically
168          * relative, i.e., items for which the hardware can only report change,
169          * not absolute measurement.
170          */
171     WTPKT   CSRDATA;
172         /* a bit mask indicating which packet data items are only available when
173          * certain cursors are connected. The individual cursor descriptions
174          * must be consulted to determine which cursors return which data.
175          */
176     INT     XMARGIN;
177     INT     YMARGIN;
178     INT     ZMARGIN;
179         /* the size of tablet context margins in tablet native coordinates, in
180          * the x, y, and z directions, respectively.
181          */
182     AXIS    X;
183     AXIS    Y;
184     AXIS    Z;
185         /* the tablet's range and resolution capabilities, in the x, y, and z
186          * axes, respectively.
187          */
188     AXIS    NPRESSURE;
189     AXIS    TPRESSURE;
190         /* the tablet's range and resolution capabilities, for the normal and
191          * tangential pressure inputs, respectively.
192          */
193     AXIS    ORIENTATION[3];
194         /* a 3-element array describing the tablet's orientation range and
195          * resolution capabilities.
196          */
197     AXIS    ROTATION[3];
198         /* a 3-element array describing the tablet's rotation range and
199          * resolution capabilities.
200          */
201     WCHAR   PNPID[WT_MAX_NAME_LEN];
202         /* a null-terminated string containing the devices Plug and Play ID.*/
203 }   WTI_DEVICES_INFO, *LPWTI_DEVICES_INFO;
204
205
206 /***********************************************************************
207  * WACOM WINTAB EXTENSIONS TO SUPPORT CSR_TYPE
208  *   In Wintab 1.2, a CSR_TYPE feature was added.  This adds the
209  *   ability to return a type of cursor on a tablet.
210  *   Unfortunately, we cannot get the cursor type directly from X,
211  *   and it is not specified directly anywhere.  So we virtualize
212  *   the type here.  (This is unfortunate, the kernel module has
213  *   the exact type, but we have no way of getting that module to
214  *   pass us that type).
215  *
216  *   Reference linuxwacom driver project wcmCommon.c function
217  *   idtotype for a much larger list of CSR_TYPE.
218  *
219  *   http://linuxwacom.cvs.sourceforge.net/linuxwacom/linuxwacom-prod/src/xdrv/wcmCommon.c?view=markup
220  *
221  *   The WTI_CURSORS_INFO.TYPE data is supposed to be used like this:
222  *   (cursor.TYPE & 0x0F06) == target_cursor_type
223  *   Reference: Section Unique ID
224  *   http://www.wacomeng.com/devsupport/ibmpc/gddevpc.html
225  */
226 #define CSR_TYPE_PEN        0x822
227 #define CSR_TYPE_ERASER     0x82a
228 #define CSR_TYPE_MOUSE_2D   0x007
229 #define CSR_TYPE_MOUSE_4D   0x094
230 /* CSR_TYPE_OTHER is a special value! assumed no real world significance
231  * if a stylus type or eraser type eventually have this value
232  * it'll be a bug.  As of 2008 05 21 we can be sure because
233  * linux wacom lists all the known values and this isn't one of them */
234 #define CSR_TYPE_OTHER      0x000
235
236 typedef struct tagWTPACKET {
237         HCTX pkContext;
238         UINT pkStatus;
239         LONG pkTime;
240         WTPKT pkChanged;
241         UINT pkSerialNumber;
242         UINT pkCursor;
243         DWORD pkButtons;
244         DWORD pkX;
245         DWORD pkY;
246         DWORD pkZ;
247         UINT pkNormalPressure;
248         UINT pkTangentPressure;
249         ORIENTATION pkOrientation;
250         ROTATION pkRotation; /* 1.1 */
251 } WTPACKET, *LPWTPACKET;
252
253
254 #ifdef SONAME_LIBXI
255
256 #include <X11/Xlib.h>
257 #include <X11/extensions/XInput.h>
258
259 static int           motion_type;
260 static int           button_press_type;
261 static int           button_release_type;
262 static int           key_press_type;
263 static int           key_release_type;
264 static int           proximity_in_type;
265 static int           proximity_out_type;
266
267 static HWND          hwndTabletDefault;
268 static WTPACKET      gMsgPacket;
269 static DWORD         gSerial;
270
271 /* Reference: http://www.wacomeng.com/devsupport/ibmpc/gddevpc.html
272  *
273  * Cursors come in sets of 3 normally
274  * Cursor #0 = puck   device 1
275  * Cursor #1 = stylus device 1
276  * Cursor #2 = eraser device 1
277  * Cursor #3 = puck   device 2
278  * Cursor #4 = stylus device 2
279  * Cursor #5 = eraser device 2
280  * etc....
281  *
282  * A dual tracking/multimode tablet is one
283  * that supports 2 independent cursors of the same or
284  * different types simultaneously on a single tablet.
285  * This makes our cursor layout potentially like this
286  * Cursor #0 = puck   1 device 1
287  * Cursor #1 = stylus 1 device 1
288  * Cursor #2 = eraser 1 device 1
289  * Cursor #3 = puck   2 device 1
290  * Cursor #4 = stylus 2 device 1
291  * Cursor #5 = eraser 2 device 1
292  * Cursor #6 = puck   1 device 2
293  * etc.....
294  *
295  * So with multimode tablets we could potentially need
296  * 2 slots of the same type per tablet i.e.
297  * you are using 2 styluses at once so they would
298  * get placed in Cursors #1 and Cursor #4
299  *
300  * Now say someone has 2 multimode tablets with 2 erasers each
301  * now we would need Cursor #2, #5, #8, #11
302  * So to support that we need CURSORMAX of 12 (0 to 11)
303  * FIXME: we don't support more than 4 regular tablets or 2 multimode tablets */
304 #define             CURSORMAX 12
305 static INT           button_state[CURSORMAX];
306
307 static LOGCONTEXTW      gSysContext;
308 static WTI_DEVICES_INFO gSysDevice;
309 static WTI_CURSORS_INFO gSysCursor[CURSORMAX];
310 static INT              gNumCursors; /* do NOT use this to iterate through gSysCursor slots */
311
312
313 /* XInput stuff */
314 static void *xinput_handle;
315
316 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
317 MAKE_FUNCPTR(XListInputDevices)
318 MAKE_FUNCPTR(XFreeDeviceList)
319 MAKE_FUNCPTR(XOpenDevice)
320 MAKE_FUNCPTR(XQueryDeviceState)
321 MAKE_FUNCPTR(XGetDeviceButtonMapping)
322 MAKE_FUNCPTR(XCloseDevice)
323 MAKE_FUNCPTR(XSelectExtensionEvent)
324 MAKE_FUNCPTR(XFreeDeviceState)
325 #undef MAKE_FUNCPTR
326
327 static INT X11DRV_XInput_Init(void)
328 {
329     xinput_handle = wine_dlopen(SONAME_LIBXI, RTLD_NOW, NULL, 0);
330     if (xinput_handle)
331     {
332 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(xinput_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
333         LOAD_FUNCPTR(XListInputDevices)
334         LOAD_FUNCPTR(XFreeDeviceList)
335         LOAD_FUNCPTR(XOpenDevice)
336         LOAD_FUNCPTR(XGetDeviceButtonMapping)
337         LOAD_FUNCPTR(XCloseDevice)
338         LOAD_FUNCPTR(XSelectExtensionEvent)
339         LOAD_FUNCPTR(XQueryDeviceState)
340         LOAD_FUNCPTR(XFreeDeviceState)
341 #undef LOAD_FUNCPTR
342         return 1;
343     }
344 sym_not_found:
345     return 0;
346 }
347
348 static int Tablet_ErrorHandler(Display *dpy, XErrorEvent *event, void* arg)
349 {
350     return 1;
351 }
352
353 static void trace_axes(XValuatorInfoPtr val)
354 {
355     int i;
356     XAxisInfoPtr axis;
357
358     for (i = 0, axis = val->axes ; i < val->num_axes; i++, axis++)
359         TRACE("        Axis %d: [resolution %d|min_value %d|max_value %d]\n", i, axis->resolution, axis->min_value, axis->max_value);
360 }
361
362 static BOOL match_token(const char *haystack, const char *needle)
363 {
364     const char *p, *q;
365     for (p = haystack; *p; )
366     {
367         while (*p && isspace(*p))
368             p++;
369         if (! *p)
370             break;
371
372         for (q = needle; *q && *p && tolower(*p) == tolower(*q); q++)
373             p++;
374         if (! *q && (isspace(*p) || !*p))
375             return TRUE;
376
377         while (*p && ! isspace(*p))
378             p++;
379     }
380     return FALSE;
381 }
382
383 /*    Determining if an X device is a Tablet style device is an imperfect science.
384 **  We rely on common conventions around device names as well as the type reported
385 **  by Wacom tablets.  This code will likely need to be expanded for alternate tablet types
386 **
387 **    Wintab refers to any device that interacts with the tablet as a cursor,
388 **  (stylus, eraser, tablet mouse, airbrush, etc)
389 **  this is not to be confused with wacom x11 configuration "cursor" device.
390 **  Wacoms x11 config "cursor" refers to its device slot (which we mirror with
391 **  our gSysCursors) for puck like devices (tablet mice essentially).
392 */
393
394 static BOOL is_tablet_cursor(const char *name, const char *type)
395 {
396     int i;
397     static const char *tablet_cursor_whitelist[] = {
398         "wacom",
399         "wizardpen",
400         "acecad",
401         "tablet",
402         "cursor",
403         "stylus",
404         "eraser",
405         "pad",
406         NULL
407     };
408
409     for (i=0; tablet_cursor_whitelist[i] != NULL; i++) {
410         if (name && match_token(name, tablet_cursor_whitelist[i]))
411             return TRUE;
412         if (type && match_token(type, tablet_cursor_whitelist[i]))
413             return TRUE;
414     }
415     return FALSE;
416 }
417
418 static BOOL is_stylus(const char *name, const char *type)
419 {
420     int i;
421     static const char* tablet_stylus_whitelist[] = {
422         "stylus",
423         "wizardpen",
424         "acecad",
425         NULL
426     };
427
428     for (i=0; tablet_stylus_whitelist[i] != NULL; i++) {
429         if (name && match_token(name, tablet_stylus_whitelist[i]))
430             return TRUE;
431         if (type && match_token(type, tablet_stylus_whitelist[i]))
432             return TRUE;
433     }
434
435     return FALSE;
436 }
437
438 static BOOL is_eraser(const char *name, const char *type)
439 {
440     if (name && match_token(name, "eraser"))
441         return TRUE;
442     if (type && match_token(type, "eraser"))
443         return TRUE;
444     return FALSE;
445 }
446
447 /* cursors are placed in gSysCursor rows depending on their type
448  * see CURSORMAX comments for more detail */
449 static BOOL add_system_cursor(LPWTI_CURSORS_INFO cursor)
450 {
451     UINT offset = 0;
452
453     if (cursor->TYPE == CSR_TYPE_PEN)
454         offset = 1;
455     else if (cursor->TYPE == CSR_TYPE_ERASER)
456         offset = 2;
457
458     for (; offset < CURSORMAX; offset += 3)
459     {
460         if (!gSysCursor[offset].ACTIVE)
461         {
462             gSysCursor[offset] = *cursor;
463             ++gNumCursors;
464             return TRUE;
465         }
466     }
467
468     return FALSE;
469 }
470
471 static void disable_system_cursors(void)
472 {
473     UINT i;
474
475     for (i = 0; i < CURSORMAX; ++i)
476     {
477         gSysCursor[i].ACTIVE = 0;
478     }
479
480     gNumCursors = 0;
481 }
482
483
484 /***********************************************************************
485  *             X11DRV_LoadTabletInfo (X11DRV.@)
486  */
487 BOOL CDECL X11DRV_LoadTabletInfo(HWND hwnddefault)
488 {
489     const WCHAR SZ_CONTEXT_NAME[] = {'W','i','n','e',' ','T','a','b','l','e','t',' ','C','o','n','t','e','x','t',0};
490     const WCHAR SZ_DEVICE_NAME[] = {'W','i','n','e',' ','T','a','b','l','e','t',' ','D','e','v','i','c','e',0};
491     const WCHAR SZ_NON_PLUGINPLAY[] = {'n','o','n','-','p','l','u','g','i','n','p','l','a','y',0};
492
493     struct x11drv_thread_data *data = x11drv_init_thread_data();
494     int num_devices;
495     int loop;
496     XDeviceInfo *devices;
497     XDeviceInfo *target = NULL;
498     BOOL    axis_read_complete= FALSE;
499
500     XAnyClassPtr        any;
501     XButtonInfoPtr      Button;
502     XValuatorInfoPtr    Val;
503     XAxisInfoPtr        Axis;
504
505     XDevice *opendevice;
506
507     if (!X11DRV_XInput_Init())
508     {
509         ERR("Unable to initialize the XInput library.\n");
510         return FALSE;
511     }
512
513     hwndTabletDefault = hwnddefault;
514
515     /* Do base initialization */
516     strcpyW(gSysContext.lcName, SZ_CONTEXT_NAME);
517     strcpyW(gSysDevice.NAME, SZ_DEVICE_NAME);
518
519     gSysContext.lcOptions = CXO_SYSTEM;
520     gSysContext.lcLocks = CXL_INSIZE | CXL_INASPECT | CXL_MARGIN |
521                                CXL_SENSITIVITY | CXL_SYSOUT;
522
523     gSysContext.lcMsgBase= WT_DEFBASE;
524     gSysContext.lcDevice = 0;
525     gSysContext.lcPktData =
526         PK_CONTEXT | PK_STATUS | PK_SERIAL_NUMBER| PK_TIME | PK_CURSOR |
527         PK_BUTTONS |  PK_X | PK_Y | PK_NORMAL_PRESSURE | PK_ORIENTATION;
528     gSysContext.lcMoveMask=
529         PK_BUTTONS |  PK_X | PK_Y | PK_NORMAL_PRESSURE | PK_ORIENTATION;
530     gSysContext.lcStatus = CXS_ONTOP;
531     gSysContext.lcPktRate = 100;
532     gSysContext.lcBtnDnMask = 0xffffffff;
533     gSysContext.lcBtnUpMask = 0xffffffff;
534     gSysContext.lcSensX = 65536;
535     gSysContext.lcSensY = 65536;
536     gSysContext.lcSensX = 65536;
537     gSysContext.lcSensZ = 65536;
538     gSysContext.lcSysSensX= 65536;
539     gSysContext.lcSysSensY= 65536;
540
541     /* initialize cursors */
542     disable_system_cursors();
543
544     /* Device Defaults */
545     gSysDevice.HARDWARE = HWC_HARDPROX|HWC_PHYSID_CURSORS;
546     gSysDevice.FIRSTCSR= 0;
547     gSysDevice.PKTRATE = 100;
548     gSysDevice.PKTDATA =
549         PK_CONTEXT | PK_STATUS | PK_SERIAL_NUMBER| PK_TIME | PK_CURSOR |
550         PK_BUTTONS |  PK_X | PK_Y | PK_NORMAL_PRESSURE | PK_ORIENTATION;
551     strcpyW(gSysDevice.PNPID, SZ_NON_PLUGINPLAY);
552
553     devices = pXListInputDevices(data->display, &num_devices);
554     if (!devices)
555     {
556         WARN("XInput Extensions reported as not available\n");
557         return FALSE;
558     }
559     TRACE("XListInputDevices reports %d devices\n", num_devices);
560     for (loop=0; loop < num_devices; loop++)
561     {
562         int class_loop;
563         char *device_type = devices[loop].type ? XGetAtomName(data->display, devices[loop].type) : NULL;
564         WTI_CURSORS_INFO cursor;
565
566         TRACE("Device %i:  [id %d|name %s|type %s|num_classes %d|use %d]\n",
567                 loop, (int) devices[loop].id, devices[loop].name, device_type ? device_type : "",
568                 devices[loop].num_classes, devices[loop].use );
569
570         switch (devices[loop].use)
571         {
572         case IsXExtensionDevice:
573 #ifdef IsXExtensionPointer
574         case IsXExtensionPointer:
575 #endif
576 #ifdef IsXExtensionKeyboard
577         case IsXExtensionKeyboard:
578 #endif
579             TRACE("Is XExtension: Device, Keyboard, or Pointer\n");
580             target = &devices[loop];
581
582             if (strlen(target->name) >= WT_MAX_NAME_LEN)
583             {
584                 ERR("Input device '%s' name too long - skipping\n", wine_dbgstr_a(target->name));
585                 break;
586             }
587
588             X11DRV_expect_error(data->display, Tablet_ErrorHandler, NULL);
589             opendevice = pXOpenDevice(data->display,target->id);
590             if (!X11DRV_check_error() && opendevice)
591             {
592                 unsigned char map[32];
593                 int i;
594                 int shft = 0;
595
596                 X11DRV_expect_error(data->display,Tablet_ErrorHandler,NULL);
597                 cursor.BUTTONS = pXGetDeviceButtonMapping(data->display, opendevice, map, 32);
598                 if (X11DRV_check_error() || cursor.BUTTONS <= 0)
599                 {
600                     TRACE("No buttons, Non Tablet Device\n");
601                     pXCloseDevice(data->display, opendevice);
602                     break;
603                 }
604
605                 for (i=0; i< cursor.BUTTONS; i++,shft++)
606                 {
607                     cursor.BUTTONMAP[i] = map[i];
608                     cursor.SYSBTNMAP[i] = (1<<shft);
609                 }
610                 pXCloseDevice(data->display, opendevice);
611             }
612             else
613             {
614                 WARN("Unable to open device %s\n",target->name);
615                 break;
616             }
617             MultiByteToWideChar(CP_UNIXCP, 0, target->name, -1, cursor.NAME, WT_MAX_NAME_LEN);
618
619             if (! is_tablet_cursor(target->name, device_type))
620             {
621                 WARN("Skipping device %d [name %s|type %s]; not apparently a tablet cursor type device.  If this is wrong, please report it to wine-devel@winehq.org\n",
622                      loop, devices[loop].name, device_type ? device_type : "");
623                 break;
624             }
625
626             cursor.ACTIVE = 1;
627             cursor.PKTDATA = PK_TIME | PK_CURSOR | PK_BUTTONS |  PK_X | PK_Y |
628                               PK_NORMAL_PRESSURE | PK_TANGENT_PRESSURE |
629                               PK_ORIENTATION;
630
631             cursor.PHYSID = target->id;
632             cursor.NPBUTTON = 1;
633             cursor.NPBTNMARKS[0] = 0 ;
634             cursor.NPBTNMARKS[1] = 1 ;
635             cursor.CAPABILITIES = CRC_MULTIMODE;
636
637             /* prefer finding TYPE_PEN(most capable) */
638             if (is_stylus(target->name, device_type))
639                 cursor.TYPE = CSR_TYPE_PEN;
640             else if (is_eraser(target->name, device_type))
641                 cursor.TYPE = CSR_TYPE_ERASER;
642             else
643                 cursor.TYPE = CSR_TYPE_OTHER;
644
645             any = target->inputclassinfo;
646
647             for (class_loop = 0; class_loop < target->num_classes; class_loop++)
648             {
649                 switch (any->class)
650                 {
651
652                     case ValuatorClass:
653                         Val = (XValuatorInfoPtr) any;
654                         TRACE("    ValidatorInput %d: [class %d|length %d|num_axes %d|mode %d|motion_buffer %ld]\n",
655                                 class_loop, (int) Val->class, Val->length, Val->num_axes, Val->mode, Val->motion_buffer);
656                         if (TRACE_ON(wintab32))
657                             trace_axes(Val);
658
659                         /* FIXME:  This is imperfect; we compute our devices capabilities based upon the
660                         **         first pen type device we find.  However, a more correct implementation
661                         **         would require acquiring a wide variety of tablets and running through
662                         **         the various inputs to see what the values are.  Odds are that a
663                         **         more 'correct' algorithm would condense to this one anyway.
664                         */
665                         if (!axis_read_complete && cursor.TYPE == CSR_TYPE_PEN)
666                         {
667                             Axis = (XAxisInfoPtr) ((char *) Val + sizeof
668                                 (XValuatorInfo));
669
670                             if (Val->num_axes>=1)
671                             {
672                                 /* Axis 1 is X */
673                                 gSysDevice.X.axMin = Axis->min_value;
674                                 gSysDevice.X.axMax= Axis->max_value;
675                                 gSysDevice.X.axUnits = TU_INCHES;
676                                 gSysDevice.X.axResolution = Axis->resolution;
677                                 gSysContext.lcInOrgX = Axis->min_value;
678                                 gSysContext.lcSysOrgX = Axis->min_value;
679                                 gSysContext.lcInExtX = Axis->max_value;
680                                 gSysContext.lcSysExtX = Axis->max_value;
681                                 Axis++;
682                             }
683                             if (Val->num_axes>=2)
684                             {
685                                 /* Axis 2 is Y */
686                                 gSysDevice.Y.axMin = Axis->min_value;
687                                 gSysDevice.Y.axMax= Axis->max_value;
688                                 gSysDevice.Y.axUnits = TU_INCHES;
689                                 gSysDevice.Y.axResolution = Axis->resolution;
690                                 gSysContext.lcInOrgY = Axis->min_value;
691                                 gSysContext.lcSysOrgY = Axis->min_value;
692                                 gSysContext.lcInExtY = Axis->max_value;
693                                 gSysContext.lcSysExtY = Axis->max_value;
694                                 Axis++;
695                             }
696                             if (Val->num_axes>=3)
697                             {
698                                 /* Axis 3 is Normal Pressure */
699                                 gSysDevice.NPRESSURE.axMin = Axis->min_value;
700                                 gSysDevice.NPRESSURE.axMax= Axis->max_value;
701                                 gSysDevice.NPRESSURE.axUnits = TU_INCHES;
702                                 gSysDevice.NPRESSURE.axResolution =
703                                                         Axis->resolution;
704                                 Axis++;
705                             }
706                             if (Val->num_axes >= 5)
707                             {
708                                 /* Axis 4 and 5 are X and Y tilt */
709                                 XAxisInfoPtr        XAxis = Axis;
710                                 Axis++;
711                                 if (max (abs(Axis->max_value),
712                                          abs(XAxis->max_value)))
713                                 {
714                                     gSysDevice.ORIENTATION[0].axMin = 0;
715                                     gSysDevice.ORIENTATION[0].axMax = 3600;
716                                     gSysDevice.ORIENTATION[0].axUnits = TU_CIRCLE;
717                                     gSysDevice.ORIENTATION[0].axResolution
718                                                                 = CASTFIX32(3600);
719                                     gSysDevice.ORIENTATION[1].axMin = -1000;
720                                     gSysDevice.ORIENTATION[1].axMax = 1000;
721                                     gSysDevice.ORIENTATION[1].axUnits = TU_CIRCLE;
722                                     gSysDevice.ORIENTATION[1].axResolution
723                                                                 = CASTFIX32(3600);
724                                     Axis++;
725                                 }
726                             }
727                             axis_read_complete = TRUE;
728                         }
729                         break;
730                     case ButtonClass:
731                     {
732                         int cchBuf = 512;
733                         int cchPos = 0;
734                         int i;
735
736                         Button = (XButtonInfoPtr) any;
737                         TRACE("    ButtonInput %d: [class %d|length %d|num_buttons %d]\n",
738                                 class_loop, (int) Button->class, Button->length, Button->num_buttons);
739                         cursor.BTNNAMES = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*cchBuf);
740                         for (i = 0; i < cursor.BUTTONS; i++)
741                         {
742                             /* FIXME - these names are probably incorrect */
743                             int cch = strlenW(cursor.NAME) + 1;
744                             while (cch > cchBuf - cchPos - 1) /* we want one extra byte for the last NUL */
745                             {
746                                 cchBuf *= 2;
747                                 cursor.BTNNAMES = HeapReAlloc(GetProcessHeap(), 0, cursor.BTNNAMES, sizeof(WCHAR)*cchBuf);
748                             }
749
750                             strcpyW(cursor.BTNNAMES + cchPos, cursor.NAME);
751                             cchPos += cch;
752                         }
753                         cursor.BTNNAMES[cchPos++] = 0;
754                         cursor.BTNNAMES = HeapReAlloc(GetProcessHeap(), 0, cursor.BTNNAMES, sizeof(WCHAR)*cchPos);
755                         cursor.cchBTNNAMES = cchPos;
756                     }
757                     break;
758                 } /* switch any->class */
759                 any = (XAnyClassPtr) ((char*) any + any->length);
760             } /* for class_loop */
761             if (!add_system_cursor(&cursor))
762                 FIXME("Skipping this cursor due to lack of system cursor slots.\n");
763             break;
764         } /* switch devices.use */
765         XFree(device_type);
766     } /* for XListInputDevices */
767     pXFreeDeviceList(devices);
768
769     if (axis_read_complete)
770         gSysDevice.NCSRTYPES = gNumCursors;
771     else
772     {
773         disable_system_cursors();
774         WARN("Did not find a valid stylus, unable to determine system context parameters. Wintab is disabled.\n");
775     }
776
777     return TRUE;
778 }
779
780 static int figure_deg(int x, int y)
781 {
782     float angle;
783
784     angle = atan2((float)y, (float)x);
785     angle += M_PI_2;
786     if (angle <= 0)
787         angle += 2 * M_PI;
788
789     return (0.5 + (angle * 1800.0 / M_PI));
790 }
791
792 static int get_button_state(int curnum)
793 {
794     return button_state[curnum];
795 }
796
797 static void set_button_state(int curnum, XID deviceid)
798 {
799     struct x11drv_thread_data *data = x11drv_thread_data();
800     XDevice *device;
801     XDeviceState *state;
802     XInputClass  *class;
803     int loop;
804     int rc = 0;
805
806     device = pXOpenDevice(data->display,deviceid);
807     state = pXQueryDeviceState(data->display,device);
808
809     if (state)
810     {
811         class = state->data;
812         for (loop = 0; loop < state->num_classes; loop++)
813         {
814             if (class->class == ButtonClass)
815             {
816                 int loop2;
817                 XButtonState *button_state =  (XButtonState*)class;
818                 for (loop2 = 0; loop2 < button_state->num_buttons; loop2++)
819                 {
820                     if (button_state->buttons[loop2 / 8] & (1 << (loop2 % 8)))
821                     {
822                         rc |= (1<<loop2);
823                     }
824                 }
825             }
826             class = (XInputClass *) ((char *) class + class->length);
827         }
828     }
829     pXFreeDeviceState(state);
830     button_state[curnum] = rc;
831 }
832
833 static int cursor_from_device(DWORD deviceid, LPWTI_CURSORS_INFO *cursorp)
834 {
835     int i;
836     for (i = 0; i < CURSORMAX; i++)
837         if (gSysCursor[i].ACTIVE && gSysCursor[i].PHYSID == deviceid)
838         {
839             *cursorp = &gSysCursor[i];
840             return i;
841         }
842
843     ERR("Could not map device id %d to a cursor\n", (int) deviceid);
844     return -1;
845 }
846
847 static void motion_event( HWND hwnd, XEvent *event )
848 {
849     XDeviceMotionEvent *motion = (XDeviceMotionEvent *)event;
850     LPWTI_CURSORS_INFO cursor;
851     int curnum = cursor_from_device(motion->deviceid, &cursor);
852     if (curnum < 0)
853         return;
854
855     memset(&gMsgPacket,0,sizeof(WTPACKET));
856
857     TRACE("Received tablet motion event (%p); device id %d, cursor num %d\n",hwnd, (int) motion->deviceid, curnum);
858
859     /* Set cursor to inverted if cursor is the eraser */
860     gMsgPacket.pkStatus = (cursor->TYPE  == CSR_TYPE_ERASER ? TPS_INVERT:0);
861     gMsgPacket.pkTime = EVENT_x11_time_to_win32_time(motion->time);
862     gMsgPacket.pkSerialNumber = gSerial++;
863     gMsgPacket.pkCursor = curnum;
864     gMsgPacket.pkX = motion->axis_data[0];
865     gMsgPacket.pkY = motion->axis_data[1];
866     gMsgPacket.pkOrientation.orAzimuth = figure_deg(motion->axis_data[3],motion->axis_data[4]);
867     gMsgPacket.pkOrientation.orAltitude = ((1000 - 15 * max
868                                             (abs(motion->axis_data[3]),
869                                              abs(motion->axis_data[4])))
870                                            * (gMsgPacket.pkStatus & TPS_INVERT?-1:1));
871     gMsgPacket.pkNormalPressure = motion->axis_data[2];
872     gMsgPacket.pkButtons = get_button_state(curnum);
873     SendMessageW(hwndTabletDefault,WT_PACKET,gMsgPacket.pkSerialNumber,(LPARAM)hwnd);
874 }
875
876 static void button_event( HWND hwnd, XEvent *event )
877 {
878     XDeviceButtonEvent *button = (XDeviceButtonEvent *) event;
879     LPWTI_CURSORS_INFO cursor;
880     int curnum = cursor_from_device(button->deviceid, &cursor);
881     if (curnum < 0)
882         return;
883
884     memset(&gMsgPacket,0,sizeof(WTPACKET));
885
886     TRACE("Received tablet button %s event\n", (event->type == button_press_type)?"press":"release");
887
888     /* Set cursor to inverted if cursor is the eraser */
889     gMsgPacket.pkStatus = (cursor->TYPE == CSR_TYPE_ERASER ? TPS_INVERT:0);
890     set_button_state(curnum, button->deviceid);
891     gMsgPacket.pkTime = EVENT_x11_time_to_win32_time(button->time);
892     gMsgPacket.pkSerialNumber = gSerial++;
893     gMsgPacket.pkCursor = curnum;
894     gMsgPacket.pkX = button->axis_data[0];
895     gMsgPacket.pkY = button->axis_data[1];
896     gMsgPacket.pkOrientation.orAzimuth = figure_deg(button->axis_data[3],button->axis_data[4]);
897     gMsgPacket.pkOrientation.orAltitude = ((1000 - 15 * max(abs(button->axis_data[3]),
898                                                             abs(button->axis_data[4])))
899                                            * (gMsgPacket.pkStatus & TPS_INVERT?-1:1));
900     gMsgPacket.pkNormalPressure = button->axis_data[2];
901     gMsgPacket.pkButtons = get_button_state(curnum);
902     SendMessageW(hwndTabletDefault,WT_PACKET,gMsgPacket.pkSerialNumber,(LPARAM)hwnd);
903 }
904
905 static void key_event( HWND hwnd, XEvent *event )
906 {
907     if (event->type == key_press_type)
908         FIXME("Received tablet key press event\n");
909     else
910         FIXME("Received tablet key release event\n");
911 }
912
913 static void proximity_event( HWND hwnd, XEvent *event )
914 {
915     XProximityNotifyEvent *proximity = (XProximityNotifyEvent *) event;
916     LPWTI_CURSORS_INFO cursor;
917     int curnum = cursor_from_device(proximity->deviceid, &cursor);
918     LPARAM proximity_info;
919
920     TRACE("hwnd=%p\n", hwnd);
921
922     if (curnum < 0)
923         return;
924
925     memset(&gMsgPacket,0,sizeof(WTPACKET));
926
927     /* Set cursor to inverted if cursor is the eraser */
928     gMsgPacket.pkStatus = (cursor->TYPE == CSR_TYPE_ERASER ? TPS_INVERT:0);
929     gMsgPacket.pkStatus |= (event->type==proximity_out_type)?TPS_PROXIMITY:0;
930     gMsgPacket.pkTime = EVENT_x11_time_to_win32_time(proximity->time);
931     gMsgPacket.pkSerialNumber = gSerial++;
932     gMsgPacket.pkCursor = curnum;
933     gMsgPacket.pkX = proximity->axis_data[0];
934     gMsgPacket.pkY = proximity->axis_data[1];
935     gMsgPacket.pkOrientation.orAzimuth = figure_deg(proximity->axis_data[3],proximity->axis_data[4]);
936     gMsgPacket.pkOrientation.orAltitude = ((1000 - 15 * max(abs(proximity->axis_data[3]),
937                                                             abs(proximity->axis_data[4])))
938                                            * (gMsgPacket.pkStatus & TPS_INVERT?-1:1));
939     gMsgPacket.pkNormalPressure = proximity->axis_data[2];
940     gMsgPacket.pkButtons = get_button_state(curnum);
941
942     /* FIXME: LPARAM loword is true when cursor entering context, false when leaving context
943      * This needs to be handled here or in wintab32.  Using the proximity_in_type is not correct
944      * but kept for now.
945      * LPARAM hiword is "non-zero when the cursor is leaving or entering hardware proximity"
946      * WPARAM contains context handle.
947      * HWND to HCTX is handled by wintab32.
948      */
949     proximity_info = MAKELPARAM((event->type == proximity_in_type),
950                      (event->type == proximity_in_type) || (event->type == proximity_out_type));
951     SendMessageW(hwndTabletDefault, WT_PROXIMITY, (WPARAM)hwnd, proximity_info);
952 }
953
954 /***********************************************************************
955  *              X11DRV_AttachEventQueueToTablet (X11DRV.@)
956  */
957 int CDECL X11DRV_AttachEventQueueToTablet(HWND hOwner)
958 {
959     struct x11drv_thread_data *data = x11drv_init_thread_data();
960     int             num_devices;
961     int             loop;
962     int             cur_loop;
963     XDeviceInfo     *devices;
964     XDeviceInfo     *target = NULL;
965     XDevice         *the_device;
966     XEventClass     event_list[7];
967     Window          win = X11DRV_get_whole_window( hOwner );
968
969     if (!win || !xinput_handle) return 0;
970
971     TRACE("Creating context for window %p (%lx)  %i cursors\n", hOwner, win, gNumCursors);
972
973     devices = pXListInputDevices(data->display, &num_devices);
974
975     X11DRV_expect_error(data->display,Tablet_ErrorHandler,NULL);
976     for (cur_loop=0; cur_loop < CURSORMAX; cur_loop++)
977     {
978         char   cursorNameA[WT_MAX_NAME_LEN];
979         int    event_number=0;
980
981         if (!gSysCursor[cur_loop].ACTIVE) continue;
982
983         /* the cursor name fits in the buffer because too long names are skipped */
984         WideCharToMultiByte(CP_UNIXCP, 0, gSysCursor[cur_loop].NAME, -1, cursorNameA, WT_MAX_NAME_LEN, NULL, NULL);
985         for (loop=0; loop < num_devices; loop ++)
986             if (strcmp(devices[loop].name, cursorNameA) == 0)
987                 target = &devices[loop];
988         if (!target) {
989             WARN("Cursor Name %s not found in list of targets.\n", cursorNameA);
990             continue;
991         }
992
993         TRACE("Opening cursor %i id %i\n",cur_loop,(INT)target->id);
994
995         the_device = pXOpenDevice(data->display, target->id);
996
997         if (!the_device)
998         {
999             WARN("Unable to Open device\n");
1000             continue;
1001         }
1002
1003         if (the_device->num_classes > 0)
1004         {
1005             DeviceKeyPress(the_device, key_press_type, event_list[event_number]);
1006             if (key_press_type) event_number++;
1007             DeviceKeyRelease(the_device, key_release_type, event_list[event_number]);
1008             if (key_release_type) event_number++;
1009             DeviceButtonPress(the_device, button_press_type, event_list[event_number]);
1010             if (button_press_type) event_number++;
1011             DeviceButtonRelease(the_device, button_release_type, event_list[event_number]);
1012             if (button_release_type) event_number++;
1013             DeviceMotionNotify(the_device, motion_type, event_list[event_number]);
1014             if (motion_type) event_number++;
1015             ProximityIn(the_device, proximity_in_type, event_list[event_number]);
1016             if (proximity_in_type) event_number++;
1017             ProximityOut(the_device, proximity_out_type, event_list[event_number]);
1018             if (proximity_out_type) event_number++;
1019
1020             if (key_press_type)
1021                 X11DRV_register_event_handler( key_press_type, key_event, "XInput KeyPress" );
1022             if (key_release_type)
1023                 X11DRV_register_event_handler( key_release_type, key_event, "XInput KeyRelease" );
1024             if (button_press_type)
1025                 X11DRV_register_event_handler( button_press_type, button_event, "XInput ButtonPress" );
1026             if (button_release_type)
1027                 X11DRV_register_event_handler( button_release_type, button_event, "XInput ButtonRelease" );
1028             if (motion_type)
1029                 X11DRV_register_event_handler( motion_type, motion_event, "XInput MotionNotify" );
1030             if (proximity_in_type)
1031                 X11DRV_register_event_handler( proximity_in_type, proximity_event, "XInput ProximityIn" );
1032             if (proximity_out_type)
1033                 X11DRV_register_event_handler( proximity_out_type, proximity_event, "XInput ProximityOut" );
1034
1035             pXSelectExtensionEvent(data->display, win, event_list, event_number);
1036         }
1037     }
1038     XSync(data->display, False);
1039     X11DRV_check_error();
1040
1041     if (NULL != devices) pXFreeDeviceList(devices);
1042     return 0;
1043 }
1044
1045 /***********************************************************************
1046  *              X11DRV_GetCurrentPacket (X11DRV.@)
1047  */
1048 int CDECL X11DRV_GetCurrentPacket(LPWTPACKET packet)
1049 {
1050     *packet = gMsgPacket;
1051     return 1;
1052 }
1053
1054
1055 static inline int CopyTabletData(LPVOID target, LPCVOID src, INT size)
1056 {
1057     /*
1058      * It is valid to call CopyTabletData with NULL.
1059      * This handles the WTInfo() case where lpOutput is null.
1060      */
1061     if(target != NULL)
1062         memcpy(target,src,size);
1063     return(size);
1064 }
1065
1066 /***********************************************************************
1067  *              X11DRV_WTInfoW (X11DRV.@)
1068  */
1069 UINT CDECL X11DRV_WTInfoW(UINT wCategory, UINT nIndex, LPVOID lpOutput)
1070 {
1071     /*
1072      * It is valid to call WTInfoA with lpOutput == NULL, as per standard.
1073      * lpOutput == NULL signifies the user only wishes
1074      * to find the size of the data.
1075      * NOTE:
1076      * From now on use CopyTabletData to fill lpOutput. memcpy will break
1077      * the code.
1078      */
1079     int rc = 0;
1080     LPWTI_CURSORS_INFO  tgtcursor;
1081     TRACE("(%u, %u, %p)\n", wCategory, nIndex, lpOutput);
1082
1083     if (!xinput_handle) return 0;
1084
1085     switch(wCategory)
1086     {
1087         case 0:
1088             /* return largest necessary buffer */
1089             TRACE("%i cursors\n",gNumCursors);
1090             if (gNumCursors>0)
1091             {
1092                 FIXME("Return proper size\n");
1093                 rc = 200;
1094             }
1095             break;
1096         case WTI_INTERFACE:
1097             switch (nIndex)
1098             {
1099                 WORD version;
1100                 UINT num;
1101                 case IFC_WINTABID:
1102                 {
1103                     static const WCHAR driver[] = {'W','i','n','e',' ','W','i','n','t','a','b',' ','1','.','1',0};
1104                     rc = CopyTabletData(lpOutput, driver, (strlenW(driver) + 1) * sizeof(WCHAR));
1105                     break;
1106                 }
1107                 case IFC_SPECVERSION:
1108                     version = (0x01) | (0x01 << 8);
1109                     rc = CopyTabletData(lpOutput, &version,sizeof(WORD));
1110                     break;
1111                 case IFC_IMPLVERSION:
1112                     version = (0x00) | (0x01 << 8);
1113                     rc = CopyTabletData(lpOutput, &version,sizeof(WORD));
1114                     break;
1115                 case IFC_NDEVICES:
1116                     num = 1;
1117                     rc = CopyTabletData(lpOutput, &num,sizeof(num));
1118                     break;
1119                 case IFC_NCURSORS:
1120                     num = gNumCursors;
1121                     rc = CopyTabletData(lpOutput, &num,sizeof(num));
1122                     break;
1123                 default:
1124                     FIXME("WTI_INTERFACE unhandled index %i\n",nIndex);
1125                     rc = 0;
1126             }
1127             break;
1128         case WTI_DEFSYSCTX:
1129         case WTI_DDCTXS:
1130         case WTI_DEFCONTEXT:
1131             switch (nIndex)
1132             {
1133                 case 0:
1134                     /* report 0 if wintab is disabled */
1135                     if (0 == gNumCursors)
1136                         rc = 0;
1137                     else
1138                         rc = CopyTabletData(lpOutput, &gSysContext,
1139                                 sizeof(LOGCONTEXTW));
1140                     break;
1141                 case CTX_NAME:
1142                     rc = CopyTabletData(lpOutput, gSysContext.lcName,
1143                          (strlenW(gSysContext.lcName)+1) * sizeof(WCHAR));
1144                     break;
1145                 case CTX_OPTIONS:
1146                     rc = CopyTabletData(lpOutput, &gSysContext.lcOptions,
1147                                         sizeof(UINT));
1148                     break;
1149                 case CTX_STATUS:
1150                     rc = CopyTabletData(lpOutput, &gSysContext.lcStatus,
1151                                         sizeof(UINT));
1152                     break;
1153                 case CTX_LOCKS:
1154                     rc= CopyTabletData (lpOutput, &gSysContext.lcLocks,
1155                                         sizeof(UINT));
1156                     break;
1157                 case CTX_MSGBASE:
1158                     rc = CopyTabletData(lpOutput, &gSysContext.lcMsgBase,
1159                                         sizeof(UINT));
1160                     break;
1161                 case CTX_DEVICE:
1162                     rc = CopyTabletData(lpOutput, &gSysContext.lcDevice,
1163                                         sizeof(UINT));
1164                     break;
1165                 case CTX_PKTRATE:
1166                     rc = CopyTabletData(lpOutput, &gSysContext.lcPktRate,
1167                                         sizeof(UINT));
1168                     break;
1169                 case CTX_PKTDATA:
1170                     rc = CopyTabletData(lpOutput, &gSysContext.lcPktData,
1171                                         sizeof(WTPKT));
1172                     break;
1173                 case CTX_PKTMODE:
1174                     rc = CopyTabletData(lpOutput, &gSysContext.lcPktMode,
1175                                         sizeof(WTPKT));
1176                     break;
1177                 case CTX_MOVEMASK:
1178                     rc = CopyTabletData(lpOutput, &gSysContext.lcMoveMask,
1179                                         sizeof(WTPKT));
1180                     break;
1181                 case CTX_BTNDNMASK:
1182                     rc = CopyTabletData(lpOutput, &gSysContext.lcBtnDnMask,
1183                                         sizeof(DWORD));
1184                     break;
1185                 case CTX_BTNUPMASK:
1186                     rc = CopyTabletData(lpOutput, &gSysContext.lcBtnUpMask,
1187                                         sizeof(DWORD));
1188                     break;
1189                 case CTX_INORGX:
1190                     rc = CopyTabletData(lpOutput, &gSysContext.lcInOrgX,
1191                                         sizeof(LONG));
1192                     break;
1193                 case CTX_INORGY:
1194                     rc = CopyTabletData(lpOutput, &gSysContext.lcInOrgY,
1195                                         sizeof(LONG));
1196                     break;
1197                 case CTX_INORGZ:
1198                     rc = CopyTabletData(lpOutput, &gSysContext.lcInOrgZ,
1199                                         sizeof(LONG));
1200                     break;
1201                 case CTX_INEXTX:
1202                     rc = CopyTabletData(lpOutput, &gSysContext.lcInExtX,
1203                                         sizeof(LONG));
1204                     break;
1205                 case CTX_INEXTY:
1206                      rc = CopyTabletData(lpOutput, &gSysContext.lcInExtY,
1207                                         sizeof(LONG));
1208                     break;
1209                 case CTX_INEXTZ:
1210                      rc = CopyTabletData(lpOutput, &gSysContext.lcInExtZ,
1211                                         sizeof(LONG));
1212                     break;
1213                 case CTX_OUTORGX:
1214                      rc = CopyTabletData(lpOutput, &gSysContext.lcOutOrgX,
1215                                         sizeof(LONG));
1216                     break;
1217                 case CTX_OUTORGY:
1218                       rc = CopyTabletData(lpOutput, &gSysContext.lcOutOrgY,
1219                                         sizeof(LONG));
1220                     break;
1221                 case CTX_OUTORGZ:
1222                        rc = CopyTabletData(lpOutput, &gSysContext.lcOutOrgZ,
1223                                         sizeof(LONG));
1224                     break;
1225                case CTX_OUTEXTX:
1226                       rc = CopyTabletData(lpOutput, &gSysContext.lcOutExtX,
1227                                         sizeof(LONG));
1228                     break;
1229                 case CTX_OUTEXTY:
1230                        rc = CopyTabletData(lpOutput, &gSysContext.lcOutExtY,
1231                                         sizeof(LONG));
1232                     break;
1233                 case CTX_OUTEXTZ:
1234                        rc = CopyTabletData(lpOutput, &gSysContext.lcOutExtZ,
1235                                         sizeof(LONG));
1236                     break;
1237                 case CTX_SENSX:
1238                         rc = CopyTabletData(lpOutput, &gSysContext.lcSensX,
1239                                         sizeof(LONG));
1240                     break;
1241                 case CTX_SENSY:
1242                         rc = CopyTabletData(lpOutput, &gSysContext.lcSensY,
1243                                         sizeof(LONG));
1244                     break;
1245                 case CTX_SENSZ:
1246                         rc = CopyTabletData(lpOutput, &gSysContext.lcSensZ,
1247                                         sizeof(LONG));
1248                     break;
1249                 case CTX_SYSMODE:
1250                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysMode,
1251                                         sizeof(LONG));
1252                     break;
1253                 case CTX_SYSORGX:
1254                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysOrgX,
1255                                         sizeof(LONG));
1256                     break;
1257                 case CTX_SYSORGY:
1258                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysOrgY,
1259                                         sizeof(LONG));
1260                     break;
1261                 case CTX_SYSEXTX:
1262                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysExtX,
1263                                         sizeof(LONG));
1264                     break;
1265                 case CTX_SYSEXTY:
1266                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysExtY,
1267                                         sizeof(LONG));
1268                     break;
1269                 case CTX_SYSSENSX:
1270                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysSensX,
1271                                         sizeof(LONG));
1272                     break;
1273                 case CTX_SYSSENSY:
1274                        rc = CopyTabletData(lpOutput, &gSysContext.lcSysSensY,
1275                                         sizeof(LONG));
1276                     break;
1277                 default:
1278                     FIXME("WTI_DEFSYSCTX unhandled index %i\n",nIndex);
1279                     rc = 0;
1280             }
1281             break;
1282         case WTI_CURSORS:
1283         case WTI_CURSORS+1:
1284         case WTI_CURSORS+2:
1285         case WTI_CURSORS+3:
1286         case WTI_CURSORS+4:
1287         case WTI_CURSORS+5:
1288         case WTI_CURSORS+6:
1289         case WTI_CURSORS+7:
1290         case WTI_CURSORS+8:
1291         case WTI_CURSORS+9:
1292         case WTI_CURSORS+10:
1293         case WTI_CURSORS+11:
1294         /* CURSORMAX == 12 */
1295         /* FIXME: dynamic cursor support */
1296             /* Apps will poll different slots to detect what cursors are available
1297              * if there isn't a cursor for this slot return 0 */
1298             if (!gSysCursor[wCategory - WTI_CURSORS].ACTIVE)
1299                 rc = 0;
1300             else
1301             {
1302                 tgtcursor = &gSysCursor[wCategory - WTI_CURSORS];
1303                 switch (nIndex)
1304                 {
1305                     case CSR_NAME:
1306                         rc = CopyTabletData(lpOutput, tgtcursor->NAME,
1307                                             (strlenW(tgtcursor->NAME)+1) * sizeof(WCHAR));
1308                         break;
1309                     case CSR_ACTIVE:
1310                         rc = CopyTabletData(lpOutput,&tgtcursor->ACTIVE,
1311                                             sizeof(BOOL));
1312                         break;
1313                     case CSR_PKTDATA:
1314                         rc = CopyTabletData(lpOutput,&tgtcursor->PKTDATA,
1315                                             sizeof(WTPKT));
1316                         break;
1317                     case CSR_BUTTONS:
1318                         rc = CopyTabletData(lpOutput,&tgtcursor->BUTTONS,
1319                                             sizeof(BYTE));
1320                         break;
1321                     case CSR_BUTTONBITS:
1322                         rc = CopyTabletData(lpOutput,&tgtcursor->BUTTONBITS,
1323                                             sizeof(BYTE));
1324                         break;
1325                     case CSR_BTNNAMES:
1326                         FIXME("Button Names not returned correctly\n");
1327                         rc = CopyTabletData(lpOutput,&tgtcursor->BTNNAMES,
1328                                             tgtcursor->cchBTNNAMES*sizeof(WCHAR));
1329                         break;
1330                     case CSR_BUTTONMAP:
1331                         rc = CopyTabletData(lpOutput,tgtcursor->BUTTONMAP,
1332                                             sizeof(BYTE)*32);
1333                         break;
1334                     case CSR_SYSBTNMAP:
1335                         rc = CopyTabletData(lpOutput,tgtcursor->SYSBTNMAP,
1336                                             sizeof(BYTE)*32);
1337                         break;
1338                     case CSR_NPBTNMARKS:
1339                         rc = CopyTabletData(lpOutput,tgtcursor->NPBTNMARKS,
1340                                             sizeof(UINT)*2);
1341                         break;
1342                     case CSR_NPBUTTON:
1343                         rc = CopyTabletData(lpOutput,&tgtcursor->NPBUTTON,
1344                                             sizeof(BYTE));
1345                         break;
1346                     case CSR_NPRESPONSE:
1347                         FIXME("Not returning CSR_NPRESPONSE correctly\n");
1348                         rc = 0;
1349                         break;
1350                     case CSR_TPBUTTON:
1351                         rc = CopyTabletData(lpOutput,&tgtcursor->TPBUTTON,
1352                                             sizeof(BYTE));
1353                         break;
1354                     case CSR_TPBTNMARKS:
1355                         rc = CopyTabletData(lpOutput,tgtcursor->TPBTNMARKS,
1356                                             sizeof(UINT)*2);
1357                         break;
1358                     case CSR_TPRESPONSE:
1359                         FIXME("Not returning CSR_TPRESPONSE correctly\n");
1360                         rc = 0;
1361                         break;
1362                     case CSR_PHYSID:
1363                     {
1364                         DWORD id;
1365                         id = tgtcursor->PHYSID;
1366                         rc = CopyTabletData(lpOutput,&id,sizeof(DWORD));
1367                     }
1368                         break;
1369                     case CSR_MODE:
1370                         rc = CopyTabletData(lpOutput,&tgtcursor->MODE,sizeof(UINT));
1371                         break;
1372                     case CSR_MINPKTDATA:
1373                         rc = CopyTabletData(lpOutput,&tgtcursor->MINPKTDATA,
1374                             sizeof(UINT));
1375                         break;
1376                     case CSR_MINBUTTONS:
1377                         rc = CopyTabletData(lpOutput,&tgtcursor->MINBUTTONS,
1378                             sizeof(UINT));
1379                         break;
1380                     case CSR_CAPABILITIES:
1381                         rc = CopyTabletData(lpOutput,&tgtcursor->CAPABILITIES,
1382                             sizeof(UINT));
1383                         break;
1384                     case CSR_TYPE:
1385                         rc = CopyTabletData(lpOutput,&tgtcursor->TYPE,
1386                             sizeof(UINT));
1387                         break;
1388                     default:
1389                         FIXME("WTI_CURSORS unhandled index %i\n",nIndex);
1390                         rc = 0;
1391                 }
1392             }
1393             break;
1394         case WTI_DEVICES:
1395             switch (nIndex)
1396             {
1397                 case DVC_NAME:
1398                     rc = CopyTabletData(lpOutput,gSysDevice.NAME,
1399                                         (strlenW(gSysDevice.NAME)+1) * sizeof(WCHAR));
1400                     break;
1401                 case DVC_HARDWARE:
1402                     rc = CopyTabletData(lpOutput,&gSysDevice.HARDWARE,
1403                                         sizeof(UINT));
1404                     break;
1405                 case DVC_NCSRTYPES:
1406                     rc = CopyTabletData(lpOutput,&gSysDevice.NCSRTYPES,
1407                                         sizeof(UINT));
1408                     break;
1409                 case DVC_FIRSTCSR:
1410                     rc = CopyTabletData(lpOutput,&gSysDevice.FIRSTCSR,
1411                                         sizeof(UINT));
1412                     break;
1413                 case DVC_PKTRATE:
1414                     rc = CopyTabletData(lpOutput,&gSysDevice.PKTRATE,
1415                                         sizeof(UINT));
1416                     break;
1417                 case DVC_PKTDATA:
1418                     rc = CopyTabletData(lpOutput,&gSysDevice.PKTDATA,
1419                                         sizeof(WTPKT));
1420                     break;
1421                 case DVC_PKTMODE:
1422                     rc = CopyTabletData(lpOutput,&gSysDevice.PKTMODE,
1423                                         sizeof(WTPKT));
1424                     break;
1425                 case DVC_CSRDATA:
1426                     rc = CopyTabletData(lpOutput,&gSysDevice.CSRDATA,
1427                                         sizeof(WTPKT));
1428                     break;
1429                 case DVC_XMARGIN:
1430                     rc = CopyTabletData(lpOutput,&gSysDevice.XMARGIN,
1431                                         sizeof(INT));
1432                     break;
1433                 case DVC_YMARGIN:
1434                     rc = CopyTabletData(lpOutput,&gSysDevice.YMARGIN,
1435                                         sizeof(INT));
1436                     break;
1437                 case DVC_ZMARGIN:
1438                     rc = 0; /* unsupported */
1439                     /*
1440                     rc = CopyTabletData(lpOutput,&gSysDevice.ZMARGIN,
1441                                         sizeof(INT));
1442                     */
1443                     break;
1444                 case DVC_X:
1445                     rc = CopyTabletData(lpOutput,&gSysDevice.X,
1446                                         sizeof(AXIS));
1447                     break;
1448                 case DVC_Y:
1449                     rc = CopyTabletData(lpOutput,&gSysDevice.Y,
1450                                         sizeof(AXIS));
1451                     break;
1452                 case DVC_Z:
1453                     rc = 0; /* unsupported */
1454                     /*
1455                     rc = CopyTabletData(lpOutput,&gSysDevice.Z,
1456                                         sizeof(AXIS));
1457                     */
1458                     break;
1459                 case DVC_NPRESSURE:
1460                     rc = CopyTabletData(lpOutput,&gSysDevice.NPRESSURE,
1461                                         sizeof(AXIS));
1462                     break;
1463                 case DVC_TPRESSURE:
1464                     rc = 0; /* unsupported */
1465                     /*
1466                     rc = CopyTabletData(lpOutput,&gSysDevice.TPRESSURE,
1467                                         sizeof(AXIS));
1468                     */
1469                     break;
1470                 case DVC_ORIENTATION:
1471                     rc = CopyTabletData(lpOutput,gSysDevice.ORIENTATION,
1472                                         sizeof(AXIS)*3);
1473                     break;
1474                 case DVC_ROTATION:
1475                     rc = 0; /* unsupported */
1476                     /*
1477                     rc = CopyTabletData(lpOutput,&gSysDevice.ROTATION,
1478                                         sizeof(AXIS)*3);
1479                     */
1480                     break;
1481                 case DVC_PNPID:
1482                     rc = CopyTabletData(lpOutput,gSysDevice.PNPID,
1483                                         (strlenW(gSysDevice.PNPID)+1)*sizeof(WCHAR));
1484                     break;
1485                 default:
1486                     FIXME("WTI_DEVICES unhandled index %i\n",nIndex);
1487                     rc = 0;
1488             }
1489             break;
1490         default:
1491             FIXME("Unhandled Category %i\n",wCategory);
1492     }
1493     return rc;
1494 }
1495
1496 #else /* SONAME_LIBXI */
1497
1498 /***********************************************************************
1499  *              AttachEventQueueToTablet (X11DRV.@)
1500  */
1501 int CDECL X11DRV_AttachEventQueueToTablet(HWND hOwner)
1502 {
1503     return 0;
1504 }
1505
1506 /***********************************************************************
1507  *              GetCurrentPacket (X11DRV.@)
1508  */
1509 int CDECL X11DRV_GetCurrentPacket(LPWTPACKET packet)
1510 {
1511     return 0;
1512 }
1513
1514 /***********************************************************************
1515  *              LoadTabletInfo (X11DRV.@)
1516  */
1517 BOOL CDECL X11DRV_LoadTabletInfo(HWND hwnddefault)
1518 {
1519     return FALSE;
1520 }
1521
1522 /***********************************************************************
1523  *              WTInfoW (X11DRV.@)
1524  */
1525 UINT CDECL X11DRV_WTInfoW(UINT wCategory, UINT nIndex, LPVOID lpOutput)
1526 {
1527     return 0;
1528 }
1529
1530 #endif /* SONAME_LIBXI */