winex11: The XIM callback is called with the X11 lock held.
[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
217 #define CSR_TYPE_PEN        0x822
218 #define CSR_TYPE_ERASER     0x82a
219 #define CSR_TYPE_MOUSE_2D   0x007
220 #define CSR_TYPE_MOUSE_4D   0x094
221
222
223 typedef struct tagWTPACKET {
224         HCTX pkContext;
225         UINT pkStatus;
226         LONG pkTime;
227         WTPKT pkChanged;
228         UINT pkSerialNumber;
229         UINT pkCursor;
230         DWORD pkButtons;
231         DWORD pkX;
232         DWORD pkY;
233         DWORD pkZ;
234         UINT pkNormalPressure;
235         UINT pkTangentPressure;
236         ORIENTATION pkOrientation;
237         ROTATION pkRotation; /* 1.1 */
238 } WTPACKET, *LPWTPACKET;
239
240
241 #ifdef SONAME_LIBXI
242
243 #include <X11/Xlib.h>
244 #include <X11/extensions/XInput.h>
245
246 static int           motion_type;
247 static int           button_press_type;
248 static int           button_release_type;
249 static int           key_press_type;
250 static int           key_release_type;
251 static int           proximity_in_type;
252 static int           proximity_out_type;
253
254 static HWND          hwndTabletDefault;
255 static WTPACKET      gMsgPacket;
256 static DWORD         gSerial;
257 static INT           button_state[10];
258
259 #define             CURSORMAX 10
260
261 static LOGCONTEXTW      gSysContext;
262 static WTI_DEVICES_INFO gSysDevice;
263 static WTI_CURSORS_INFO gSysCursor[CURSORMAX];
264 static INT              gNumCursors;
265
266
267 /* XInput stuff */
268 static void *xinput_handle;
269
270 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
271 MAKE_FUNCPTR(XListInputDevices)
272 MAKE_FUNCPTR(XFreeDeviceList)
273 MAKE_FUNCPTR(XOpenDevice)
274 MAKE_FUNCPTR(XQueryDeviceState)
275 MAKE_FUNCPTR(XGetDeviceButtonMapping)
276 MAKE_FUNCPTR(XCloseDevice)
277 MAKE_FUNCPTR(XSelectExtensionEvent)
278 MAKE_FUNCPTR(XFreeDeviceState)
279 #undef MAKE_FUNCPTR
280
281 static INT X11DRV_XInput_Init(void)
282 {
283     xinput_handle = wine_dlopen(SONAME_LIBXI, RTLD_NOW, NULL, 0);
284     if (xinput_handle)
285     {
286 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(xinput_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
287         LOAD_FUNCPTR(XListInputDevices)
288         LOAD_FUNCPTR(XFreeDeviceList)
289         LOAD_FUNCPTR(XOpenDevice)
290         LOAD_FUNCPTR(XGetDeviceButtonMapping)
291         LOAD_FUNCPTR(XCloseDevice)
292         LOAD_FUNCPTR(XSelectExtensionEvent)
293         LOAD_FUNCPTR(XQueryDeviceState)
294         LOAD_FUNCPTR(XFreeDeviceState)
295 #undef LOAD_FUNCPTR
296         return 1;
297     }
298 sym_not_found:
299     return 0;
300 }
301
302 static int Tablet_ErrorHandler(Display *dpy, XErrorEvent *event, void* arg)
303 {
304     return 1;
305 }
306
307 static int find_cursor_by_type(int cursor_type, int exclude)
308 {
309     int i;
310     for (i = 0; i < gNumCursors; i++)
311         if (i != exclude)
312             if (gSysCursor[i].TYPE == cursor_type)
313                 return i;
314
315     return -1;
316 }
317
318 static void swap_cursors(int a, int b)
319 {
320     WTI_CURSORS_INFO temp;
321     temp = gSysCursor[a];
322     gSysCursor[a] = gSysCursor[b];
323     gSysCursor[b] = temp;
324 }
325
326 /* Adobe Photoshop 7.0 relies on the eraser being cursor #2 or #5, and it assumes the stylus is 1.
327 **   If the X configuration is not set up that way, make it
328 */
329 static void Tablet_FixupCursors(void)
330 {
331     if (gNumCursors >= 1)
332         if (gSysCursor[1].TYPE != CSR_TYPE_PEN)
333         {
334             int stylus;
335             stylus = find_cursor_by_type(CSR_TYPE_PEN, 1);
336             if (stylus >= 0)
337             {
338                 swap_cursors(1, stylus);
339                 TRACE("Swapped cursor %d with stylus slot (1) for compatibility with older programs\n", stylus);
340             }
341         }
342
343     if (gNumCursors >= 2)
344         if (gSysCursor[2].TYPE != CSR_TYPE_ERASER)
345         {
346             int eraser;
347             eraser = find_cursor_by_type(CSR_TYPE_ERASER, 2);
348             if (eraser >= 0)
349             {
350                 swap_cursors(2, eraser);
351                 TRACE("Swapped cursor %d with eraser slot (2) for compatibility with older programs\n", eraser);
352             }
353         }
354 }
355
356 static void trace_axes(XValuatorInfoPtr val)
357 {
358     int i;
359     XAxisInfoPtr axis;
360
361     for (i = 0, axis = val->axes ; i < val->num_axes; i++, axis++)
362         TRACE("        Axis %d: [resolution %d|min_value %d|max_value %d]\n", i, axis->resolution, axis->min_value, axis->max_value);
363 }
364
365 BOOL match_token(const char *haystack, const char *needle)
366 {
367     const char *p, *q;
368     for (p = haystack; *p; )
369     {
370         while (*p && isspace(*p))
371             p++;
372         if (! *p)
373             break;
374
375         for (q = needle; *q && *p && tolower(*p) == tolower(*q); q++)
376             p++;
377         if (! *q && (isspace(*p) || !*p))
378             return TRUE;
379
380         while (*p && ! isspace(*p))
381             p++;
382     }
383     return FALSE;
384 }
385
386 /*    Determining if an X device is a Tablet style device is an imperfect science.
387 **  We rely on common conventions around device names as well as the type reported
388 **  by Wacom tablets.  This code will likely need to be expanded for alternate tablet types
389 */
390
391 static BOOL is_tablet_cursor(const char *name, const char *type)
392 {
393     int i;
394     static const char *tablet_cursor_whitelist[] = {
395         "wacom",
396         "wizardpen",
397         "acecad",
398         "tablet",
399         "cursor",
400         "stylus",
401         "eraser",
402         "pad",
403         NULL
404     };
405
406     for (i=0; tablet_cursor_whitelist[i] != NULL; i++) {
407         if (name && match_token(name, tablet_cursor_whitelist[i]))
408             return TRUE;
409         if (type && match_token(type, tablet_cursor_whitelist[i]))
410             return TRUE;
411     }
412     return FALSE;
413 }
414
415 static BOOL is_stylus(const char *name, const char *type)
416 {
417     int i;
418     static const char* tablet_stylus_whitelist[] = {
419         "stylus",
420         "wizardpen",
421         NULL
422     };
423
424     for (i=0; tablet_stylus_whitelist[i] != NULL; i++) {
425         if (name && match_token(name, tablet_stylus_whitelist[i]))
426             return TRUE;
427         if (type && match_token(type, tablet_stylus_whitelist[i]))
428             return TRUE;
429     }
430
431     return FALSE;
432 }
433
434 static BOOL is_eraser(const char *name, const char *type)
435 {
436     if (name && match_token(name, "eraser"))
437         return TRUE;
438     if (type && match_token(type, "eraser"))
439         return TRUE;
440     return FALSE;
441 }
442
443
444 /***********************************************************************
445  *             X11DRV_LoadTabletInfo (X11DRV.@)
446  */
447 void X11DRV_LoadTabletInfo(HWND hwnddefault)
448 {
449     const WCHAR SZ_CONTEXT_NAME[] = {'W','i','n','e',' ','T','a','b','l','e','t',' ','C','o','n','t','e','x','t',0};
450     const WCHAR SZ_DEVICE_NAME[] = {'W','i','n','e',' ','T','a','b','l','e','t',' ','D','e','v','i','c','e',0};
451     const WCHAR SZ_NON_PLUGINPLAY[] = {'n','o','n','-','p','l','u','g','i','n','p','l','a','y',0};
452
453     struct x11drv_thread_data *data = x11drv_thread_data();
454     int num_devices;
455     int loop;
456     int cursor_target;
457     XDeviceInfo *devices;
458     XDeviceInfo *target = NULL;
459     BOOL    axis_read_complete= FALSE;
460
461     XAnyClassPtr        any;
462     XButtonInfoPtr      Button;
463     XValuatorInfoPtr    Val;
464     XAxisInfoPtr        Axis;
465
466     XDevice *opendevice;
467
468     if (!X11DRV_XInput_Init())
469     {
470         ERR("Unable to initialize the XInput library.\n");
471         return;
472     }
473
474     hwndTabletDefault = hwnddefault;
475
476     /* Do base initialization */
477     strcpyW(gSysContext.lcName, SZ_CONTEXT_NAME);
478     strcpyW(gSysDevice.NAME, SZ_DEVICE_NAME);
479
480     gSysContext.lcOptions = CXO_SYSTEM;
481     gSysContext.lcLocks = CXL_INSIZE | CXL_INASPECT | CXL_MARGIN |
482                                CXL_SENSITIVITY | CXL_SYSOUT;
483
484     gSysContext.lcMsgBase= WT_DEFBASE;
485     gSysContext.lcDevice = 0;
486     gSysContext.lcPktData =
487         PK_CONTEXT | PK_STATUS | PK_SERIAL_NUMBER| PK_TIME | PK_CURSOR |
488         PK_BUTTONS |  PK_X | PK_Y | PK_NORMAL_PRESSURE | PK_ORIENTATION;
489     gSysContext.lcMoveMask=
490         PK_BUTTONS |  PK_X | PK_Y | PK_NORMAL_PRESSURE | PK_ORIENTATION;
491     gSysContext.lcStatus = CXS_ONTOP;
492     gSysContext.lcPktRate = 100;
493     gSysContext.lcBtnDnMask = 0xffffffff;
494     gSysContext.lcBtnUpMask = 0xffffffff;
495     gSysContext.lcSensX = 65536;
496     gSysContext.lcSensY = 65536;
497     gSysContext.lcSensX = 65536;
498     gSysContext.lcSensZ = 65536;
499     gSysContext.lcSysSensX= 65536;
500     gSysContext.lcSysSensY= 65536;
501
502     /* Device Defaults */
503     gSysDevice.HARDWARE = HWC_HARDPROX|HWC_PHYSID_CURSORS;
504     gSysDevice.FIRSTCSR= 0;
505     gSysDevice.PKTRATE = 100;
506     gSysDevice.PKTDATA =
507         PK_CONTEXT | PK_STATUS | PK_SERIAL_NUMBER| PK_TIME | PK_CURSOR |
508         PK_BUTTONS |  PK_X | PK_Y | PK_NORMAL_PRESSURE | PK_ORIENTATION;
509     strcpyW(gSysDevice.PNPID, SZ_NON_PLUGINPLAY);
510
511     wine_tsx11_lock();
512
513     cursor_target = -1;
514     devices = pXListInputDevices(data->display, &num_devices);
515     if (!devices)
516     {
517         WARN("XInput Extensions reported as not avalable\n");
518         wine_tsx11_unlock();
519         return;
520     }
521     TRACE("XListInputDevices reports %d devices\n", num_devices);
522     for (loop=0; loop < num_devices; loop++)
523     {
524         int class_loop;
525         char *device_type = devices[loop].type ? XGetAtomName(data->display, devices[loop].type) : NULL;
526         LPWTI_CURSORS_INFO cursor;
527
528         TRACE("Device %i:  [id %d|name %s|type %s|num_classes %d|use %d]\n",
529                 loop, (int) devices[loop].id, devices[loop].name, device_type ? device_type : "",
530                 devices[loop].num_classes, devices[loop].use );
531
532         switch (devices[loop].use)
533         {
534         case IsXExtensionDevice:
535 #ifdef IsXExtensionPointer
536         case IsXExtensionPointer:
537 #endif
538             TRACE("Is XExtension%s\n", (devices[loop].use == IsXExtensionDevice)? "Device":"Pointer");
539             cursor_target++;
540             target = &devices[loop];
541             cursor = &gSysCursor[cursor_target];
542
543             if (strlen(target->name) >= WT_MAX_NAME_LEN)
544             {
545                 ERR("Input device '%s' name too long - skipping\n", wine_dbgstr_a(target->name));
546                 cursor_target--;
547                 break;
548             }
549
550             X11DRV_expect_error(data->display, Tablet_ErrorHandler, NULL);
551             opendevice = pXOpenDevice(data->display,target->id);
552             if (!X11DRV_check_error() && opendevice)
553             {
554                 unsigned char map[32];
555                 int i;
556                 int shft = 0;
557
558                 X11DRV_expect_error(data->display,Tablet_ErrorHandler,NULL);
559                 cursor->BUTTONS = pXGetDeviceButtonMapping(data->display, opendevice, map, 32);
560                 if (X11DRV_check_error() || cursor->BUTTONS <= 0)
561                 {
562                     TRACE("No buttons, Non Tablet Device\n");
563                     pXCloseDevice(data->display, opendevice);
564                     cursor_target --;
565                     break;
566                 }
567
568                 for (i=0; i< cursor->BUTTONS; i++,shft++)
569                 {
570                     cursor->BUTTONMAP[i] = map[i];
571                     cursor->SYSBTNMAP[i] = (1<<shft);
572                 }
573                 pXCloseDevice(data->display, opendevice);
574             }
575             else
576             {
577                 WARN("Unable to open device %s\n",target->name);
578                 cursor_target --;
579                 break;
580             }
581             MultiByteToWideChar(CP_UNIXCP, 0, target->name, -1, cursor->NAME, WT_MAX_NAME_LEN);
582
583             if (! is_tablet_cursor(target->name, device_type))
584             {
585                 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",
586                      loop, devices[loop].name, device_type ? device_type : "");
587                 cursor_target --;
588                 break;
589             }
590
591             cursor->ACTIVE = 1;
592             cursor->PKTDATA = PK_TIME | PK_CURSOR | PK_BUTTONS |  PK_X | PK_Y |
593                               PK_NORMAL_PRESSURE | PK_TANGENT_PRESSURE |
594                               PK_ORIENTATION;
595
596             cursor->PHYSID = target->id;
597             cursor->NPBUTTON = 1;
598             cursor->NPBTNMARKS[0] = 0 ;
599             cursor->NPBTNMARKS[1] = 1 ;
600             cursor->CAPABILITIES = CRC_MULTIMODE;
601             if (is_stylus(target->name, device_type))
602                 cursor->TYPE = CSR_TYPE_PEN;
603             if (is_eraser(target->name, device_type))
604                 cursor->TYPE = CSR_TYPE_ERASER;
605
606
607             any = target->inputclassinfo;
608
609             for (class_loop = 0; class_loop < target->num_classes; class_loop++)
610             {
611                 switch (any->class)
612                 {
613
614                     case ValuatorClass:
615                         Val = (XValuatorInfoPtr) any;
616                         TRACE("    ValidatorInput %d: [class %d|length %d|num_axes %d|mode %d|motion_buffer %ld]\n",
617                                 class_loop, (int) Val->class, Val->length, Val->num_axes, Val->mode, Val->motion_buffer);
618                         if (TRACE_ON(wintab32))
619                             trace_axes(Val);
620
621                         /* FIXME:  This is imperfect; we compute our devices capabilities based upon the
622                         **         first pen type device we find.  However, a more correct implementation
623                         **         would require acquiring a wide variety of tablets and running through
624                         **         the various inputs to see what the values are.  Odds are that a
625                         **         more 'correct' algorithm would condense to this one anyway.
626                         */
627                         if (!axis_read_complete && cursor->TYPE == CSR_TYPE_PEN)
628                         {
629                             Axis = (XAxisInfoPtr) ((char *) Val + sizeof
630                                 (XValuatorInfo));
631
632                             if (Val->num_axes>=1)
633                             {
634                                 /* Axis 1 is X */
635                                 gSysDevice.X.axMin = Axis->min_value;
636                                 gSysDevice.X.axMax= Axis->max_value;
637                                 gSysDevice.X.axUnits = TU_INCHES;
638                                 gSysDevice.X.axResolution = Axis->resolution;
639                                 gSysContext.lcInOrgX = Axis->min_value;
640                                 gSysContext.lcSysOrgX = Axis->min_value;
641                                 gSysContext.lcInExtX = Axis->max_value;
642                                 gSysContext.lcSysExtX = Axis->max_value;
643                                 Axis++;
644                             }
645                             if (Val->num_axes>=2)
646                             {
647                                 /* Axis 2 is Y */
648                                 gSysDevice.Y.axMin = Axis->min_value;
649                                 gSysDevice.Y.axMax= Axis->max_value;
650                                 gSysDevice.Y.axUnits = TU_INCHES;
651                                 gSysDevice.Y.axResolution = Axis->resolution;
652                                 gSysContext.lcInOrgY = Axis->min_value;
653                                 gSysContext.lcSysOrgY = Axis->min_value;
654                                 gSysContext.lcInExtY = Axis->max_value;
655                                 gSysContext.lcSysExtY = Axis->max_value;
656                                 Axis++;
657                             }
658                             if (Val->num_axes>=3)
659                             {
660                                 /* Axis 3 is Normal Pressure */
661                                 gSysDevice.NPRESSURE.axMin = Axis->min_value;
662                                 gSysDevice.NPRESSURE.axMax= Axis->max_value;
663                                 gSysDevice.NPRESSURE.axUnits = TU_INCHES;
664                                 gSysDevice.NPRESSURE.axResolution =
665                                                         Axis->resolution;
666                                 Axis++;
667                             }
668                             if (Val->num_axes >= 5)
669                             {
670                                 /* Axis 4 and 5 are X and Y tilt */
671                                 XAxisInfoPtr        XAxis = Axis;
672                                 Axis++;
673                                 if (max (abs(Axis->max_value),
674                                          abs(XAxis->max_value)))
675                                 {
676                                     gSysDevice.ORIENTATION[0].axMin = 0;
677                                     gSysDevice.ORIENTATION[0].axMax = 3600;
678                                     gSysDevice.ORIENTATION[0].axUnits = TU_CIRCLE;
679                                     gSysDevice.ORIENTATION[0].axResolution
680                                                                 = CASTFIX32(3600);
681                                     gSysDevice.ORIENTATION[1].axMin = -1000;
682                                     gSysDevice.ORIENTATION[1].axMax = 1000;
683                                     gSysDevice.ORIENTATION[1].axUnits = TU_CIRCLE;
684                                     gSysDevice.ORIENTATION[1].axResolution
685                                                                 = CASTFIX32(3600);
686                                     Axis++;
687                                 }
688                             }
689                             axis_read_complete = TRUE;
690                         }
691                         break;
692                     case ButtonClass:
693                     {
694                         int cchBuf = 512;
695                         int cchPos = 0;
696                         int i;
697
698                         Button = (XButtonInfoPtr) any;
699                         TRACE("    ButtonInput %d: [class %d|length %d|num_buttons %d]\n",
700                                 class_loop, (int) Button->class, Button->length, Button->num_buttons);
701                         cursor->BTNNAMES = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*cchBuf);
702                         for (i = 0; i < cursor->BUTTONS; i++)
703                         {
704                             /* FIXME - these names are probably incorrect */
705                             int cch = strlenW(cursor->NAME) + 1;
706                             while (cch > cchBuf - cchPos - 1) /* we want one extra byte for the last NUL */
707                             {
708                                 cchBuf *= 2;
709                                 cursor->BTNNAMES = HeapReAlloc(GetProcessHeap(), 0, cursor->BTNNAMES, sizeof(WCHAR)*cchBuf);
710                             }
711
712                             strcpyW(cursor->BTNNAMES + cchPos, cursor->NAME);
713                             cchPos += cch;
714                         }
715                         cursor->BTNNAMES[cchPos++] = 0;
716                         cursor->BTNNAMES = HeapReAlloc(GetProcessHeap(), 0, cursor->BTNNAMES, sizeof(WCHAR)*cchPos);
717                         cursor->cchBTNNAMES = cchPos;
718                     }
719                     break;
720                 }
721                 any = (XAnyClassPtr) ((char*) any + any->length);
722             }
723             break;
724         }
725
726         XFree(device_type);
727     }
728     pXFreeDeviceList(devices);
729
730     if (axis_read_complete)
731     {
732         gSysDevice.NCSRTYPES = cursor_target+1;
733         gNumCursors = cursor_target+1;
734         Tablet_FixupCursors();
735     }
736     else
737         WARN("Did not find a valid stylus cursor with >= 5 axes, returning 0 valid devices.\n");
738
739     wine_tsx11_unlock();
740 }
741
742 static int figure_deg(int x, int y)
743 {
744     float angle;
745
746     angle = atan2((float)y, (float)x);
747     angle += M_PI_2;
748     if (angle <= 0)
749         angle += 2 * M_PI;
750
751     return (0.5 + (angle * 1800.0 / M_PI));
752 }
753
754 static int get_button_state(int curnum)
755 {
756     return button_state[curnum];
757 }
758
759 static void set_button_state(int curnum, XID deviceid)
760 {
761     struct x11drv_thread_data *data = x11drv_thread_data();
762     XDevice *device;
763     XDeviceState *state;
764     XInputClass  *class;
765     int loop;
766     int rc = 0;
767
768     wine_tsx11_lock();
769     device = pXOpenDevice(data->display,deviceid);
770     state = pXQueryDeviceState(data->display,device);
771
772     if (state)
773     {
774         class = state->data;
775         for (loop = 0; loop < state->num_classes; loop++)
776         {
777             if (class->class == ButtonClass)
778             {
779                 int loop2;
780                 XButtonState *button_state =  (XButtonState*)class;
781                 for (loop2 = 0; loop2 < button_state->num_buttons; loop2++)
782                 {
783                     if (button_state->buttons[loop2 / 8] & (1 << (loop2 % 8)))
784                     {
785                         rc |= (1<<loop2);
786                     }
787                 }
788             }
789             class = (XInputClass *) ((char *) class + class->length);
790         }
791     }
792     pXFreeDeviceState(state);
793     wine_tsx11_unlock();
794     button_state[curnum] = rc;
795 }
796
797 static int cursor_from_device(DWORD deviceid, LPWTI_CURSORS_INFO *cursorp)
798 {
799     int i;
800     for (i = 0; i < gNumCursors; i++)
801         if (gSysCursor[i].PHYSID == deviceid)
802         {
803             *cursorp = &gSysCursor[i];
804             return i;
805         }
806
807     ERR("Could not map device id %d to a cursor\n", (int) deviceid);
808     return -1;
809 }
810
811 static void motion_event( HWND hwnd, XEvent *event )
812 {
813     XDeviceMotionEvent *motion = (XDeviceMotionEvent *)event;
814     LPWTI_CURSORS_INFO cursor;
815     int curnum = cursor_from_device(motion->deviceid, &cursor);
816     if (curnum < 0)
817         return;
818
819     memset(&gMsgPacket,0,sizeof(WTPACKET));
820
821     TRACE("Received tablet motion event (%p); device id %d, cursor num %d\n",hwnd, (int) motion->deviceid, curnum);
822
823     /* Set cursor to inverted if cursor is the eraser */
824     gMsgPacket.pkStatus = (cursor->TYPE  == CSR_TYPE_ERASER ? TPS_INVERT:0);
825     gMsgPacket.pkTime = EVENT_x11_time_to_win32_time(motion->time);
826     gMsgPacket.pkSerialNumber = gSerial++;
827     gMsgPacket.pkCursor = curnum;
828     gMsgPacket.pkX = motion->axis_data[0];
829     gMsgPacket.pkY = motion->axis_data[1];
830     gMsgPacket.pkOrientation.orAzimuth = figure_deg(motion->axis_data[3],motion->axis_data[4]);
831     gMsgPacket.pkOrientation.orAltitude = ((1000 - 15 * max
832                                             (abs(motion->axis_data[3]),
833                                              abs(motion->axis_data[4])))
834                                            * (gMsgPacket.pkStatus & TPS_INVERT?-1:1));
835     gMsgPacket.pkNormalPressure = motion->axis_data[2];
836     gMsgPacket.pkButtons = get_button_state(curnum);
837     SendMessageW(hwndTabletDefault,WT_PACKET,0,(LPARAM)hwnd);
838 }
839
840 static void button_event( HWND hwnd, XEvent *event )
841 {
842     XDeviceButtonEvent *button = (XDeviceButtonEvent *) event;
843     LPWTI_CURSORS_INFO cursor;
844     int curnum = cursor_from_device(button->deviceid, &cursor);
845     if (curnum < 0)
846         return;
847
848     memset(&gMsgPacket,0,sizeof(WTPACKET));
849
850     TRACE("Received tablet button %s event\n", (event->type == button_press_type)?"press":"release");
851
852     /* Set cursor to inverted if cursor is the eraser */
853     gMsgPacket.pkStatus = (cursor->TYPE == CSR_TYPE_ERASER ? TPS_INVERT:0);
854     set_button_state(curnum, button->deviceid);
855     gMsgPacket.pkTime = EVENT_x11_time_to_win32_time(button->time);
856     gMsgPacket.pkSerialNumber = gSerial++;
857     gMsgPacket.pkCursor = curnum;
858     gMsgPacket.pkX = button->axis_data[0];
859     gMsgPacket.pkY = button->axis_data[1];
860     gMsgPacket.pkOrientation.orAzimuth = figure_deg(button->axis_data[3],button->axis_data[4]);
861     gMsgPacket.pkOrientation.orAltitude = ((1000 - 15 * max(abs(button->axis_data[3]),
862                                                             abs(button->axis_data[4])))
863                                            * (gMsgPacket.pkStatus & TPS_INVERT?-1:1));
864     gMsgPacket.pkNormalPressure = button->axis_data[2];
865     gMsgPacket.pkButtons = get_button_state(curnum);
866     SendMessageW(hwndTabletDefault,WT_PACKET,0,(LPARAM)hwnd);
867 }
868
869 static void key_event( HWND hwnd, XEvent *event )
870 {
871     if (event->type == key_press_type)
872         FIXME("Received tablet key press event\n");
873     else
874         FIXME("Received tablet key release event\n");
875 }
876
877 static void proximity_event( HWND hwnd, XEvent *event )
878 {
879     XProximityNotifyEvent *proximity = (XProximityNotifyEvent *) event;
880     LPWTI_CURSORS_INFO cursor;
881     int curnum = cursor_from_device(proximity->deviceid, &cursor);
882     if (curnum < 0)
883         return;
884
885     memset(&gMsgPacket,0,sizeof(WTPACKET));
886
887     TRACE("Received tablet proximity event\n");
888     /* Set cursor to inverted if cursor is the eraser */
889     gMsgPacket.pkStatus = (cursor->TYPE == CSR_TYPE_ERASER ? TPS_INVERT:0);
890     gMsgPacket.pkStatus |= (event->type==proximity_out_type)?TPS_PROXIMITY:0;
891     gMsgPacket.pkTime = EVENT_x11_time_to_win32_time(proximity->time);
892     gMsgPacket.pkSerialNumber = gSerial++;
893     gMsgPacket.pkCursor = curnum;
894     gMsgPacket.pkX = proximity->axis_data[0];
895     gMsgPacket.pkY = proximity->axis_data[1];
896     gMsgPacket.pkOrientation.orAzimuth = figure_deg(proximity->axis_data[3],proximity->axis_data[4]);
897     gMsgPacket.pkOrientation.orAltitude = ((1000 - 15 * max(abs(proximity->axis_data[3]),
898                                                             abs(proximity->axis_data[4])))
899                                            * (gMsgPacket.pkStatus & TPS_INVERT?-1:1));
900     gMsgPacket.pkNormalPressure = proximity->axis_data[2];
901     gMsgPacket.pkButtons = get_button_state(curnum);
902
903     SendMessageW(hwndTabletDefault, WT_PROXIMITY, (event->type == proximity_in_type), (LPARAM)hwnd);
904 }
905
906 /***********************************************************************
907  *              X11DRV_AttachEventQueueToTablet (X11DRV.@)
908  */
909 int X11DRV_AttachEventQueueToTablet(HWND hOwner)
910 {
911     struct x11drv_thread_data *data = x11drv_thread_data();
912     int             num_devices;
913     int             loop;
914     int             cur_loop;
915     XDeviceInfo     *devices;
916     XDeviceInfo     *target = NULL;
917     XDevice         *the_device;
918     XEventClass     event_list[7];
919     Window          win = X11DRV_get_whole_window( hOwner );
920
921     if (!win) return 0;
922
923     TRACE("Creating context for window %p (%lx)  %i cursors\n", hOwner, win, gNumCursors);
924
925     wine_tsx11_lock();
926     devices = pXListInputDevices(data->display, &num_devices);
927
928     X11DRV_expect_error(data->display,Tablet_ErrorHandler,NULL);
929     for (cur_loop=0; cur_loop < gNumCursors; cur_loop++)
930     {
931         char   cursorNameA[WT_MAX_NAME_LEN];
932         int    event_number=0;
933
934         /* the cursor name fits in the buffer because too long names are skipped */
935         WideCharToMultiByte(CP_UNIXCP, 0, gSysCursor[cur_loop].NAME, -1, cursorNameA, WT_MAX_NAME_LEN, NULL, NULL);
936         for (loop=0; loop < num_devices; loop ++)
937             if (strcmp(devices[loop].name, cursorNameA) == 0)
938                 target = &devices[loop];
939
940         TRACE("Opening cursor %i id %i\n",cur_loop,(INT)target->id);
941
942         the_device = pXOpenDevice(data->display, target->id);
943
944         if (!the_device)
945         {
946             WARN("Unable to Open device\n");
947             continue;
948         }
949
950         if (the_device->num_classes > 0)
951         {
952             DeviceKeyPress(the_device, key_press_type, event_list[event_number]);
953             if (key_press_type) event_number++;
954             DeviceKeyRelease(the_device, key_release_type, event_list[event_number]);
955             if (key_release_type) event_number++;
956             DeviceButtonPress(the_device, button_press_type, event_list[event_number]);
957             if (button_press_type) event_number++;
958             DeviceButtonRelease(the_device, button_release_type, event_list[event_number]);
959             if (button_release_type) event_number++;
960             DeviceMotionNotify(the_device, motion_type, event_list[event_number]);
961             if (motion_type) event_number++;
962             ProximityIn(the_device, proximity_in_type, event_list[event_number]);
963             if (proximity_in_type) event_number++;
964             ProximityOut(the_device, proximity_out_type, event_list[event_number]);
965             if (proximity_out_type) event_number++;
966
967             if (key_press_type) X11DRV_register_event_handler( key_press_type, key_event );
968             if (key_release_type) X11DRV_register_event_handler( key_release_type, key_event );
969             if (button_press_type) X11DRV_register_event_handler( button_press_type, button_event );
970             if (button_release_type) X11DRV_register_event_handler( button_release_type, button_event );
971             if (motion_type) X11DRV_register_event_handler( motion_type, motion_event );
972             if (proximity_in_type) X11DRV_register_event_handler( proximity_in_type, proximity_event );
973             if (proximity_out_type) X11DRV_register_event_handler( proximity_out_type, proximity_event );
974
975             pXSelectExtensionEvent(data->display, win, event_list, event_number);
976         }
977     }
978     XSync(data->display, False);
979     X11DRV_check_error();
980
981     if (NULL != devices) pXFreeDeviceList(devices);
982     wine_tsx11_unlock();
983     return 0;
984 }
985
986 /***********************************************************************
987  *              X11DRV_GetCurrentPacket (X11DRV.@)
988  */
989 int X11DRV_GetCurrentPacket(LPWTPACKET packet)
990 {
991     *packet = gMsgPacket;
992     return 1;
993 }
994
995
996 static inline int CopyTabletData(LPVOID target, LPCVOID src, INT size)
997 {
998     /*
999      * It is valid to call CopyTabletData with NULL.
1000      * This handles the WTInfo() case where lpOutput is null.
1001      */
1002     if(target != NULL)
1003         memcpy(target,src,size);
1004     return(size);
1005 }
1006
1007 /***********************************************************************
1008  *              X11DRV_WTInfoW (X11DRV.@)
1009  */
1010 UINT X11DRV_WTInfoW(UINT wCategory, UINT nIndex, LPVOID lpOutput)
1011 {
1012     /*
1013      * It is valid to call WTInfoA with lpOutput == NULL, as per standard.
1014      * lpOutput == NULL signifies the user only wishes
1015      * to find the size of the data.
1016      * NOTE:
1017      * From now on use CopyTabletData to fill lpOutput. memcpy will break
1018      * the code.
1019      */
1020     int rc = 0;
1021     LPWTI_CURSORS_INFO  tgtcursor;
1022     TRACE("(%u, %u, %p)\n", wCategory, nIndex, lpOutput);
1023
1024     switch(wCategory)
1025     {
1026         case 0:
1027             /* return largest necessary buffer */
1028             TRACE("%i cursors\n",gNumCursors);
1029             if (gNumCursors>0)
1030             {
1031                 FIXME("Return proper size\n");
1032                 rc = 200;
1033             }
1034             break;
1035         case WTI_INTERFACE:
1036             switch (nIndex)
1037             {
1038                 WORD version;
1039                 UINT num;
1040                 case IFC_WINTABID:
1041                 {
1042                     static const WCHAR driver[] = {'W','i','n','e',' ','W','i','n','t','a','b',' ','1','.','1',0};
1043                     rc = CopyTabletData(lpOutput, driver, (strlenW(driver) + 1) * sizeof(WCHAR));
1044                     break;
1045                 }
1046                 case IFC_SPECVERSION:
1047                     version = (0x01) | (0x01 << 8);
1048                     rc = CopyTabletData(lpOutput, &version,sizeof(WORD));
1049                     break;
1050                 case IFC_IMPLVERSION:
1051                     version = (0x00) | (0x01 << 8);
1052                     rc = CopyTabletData(lpOutput, &version,sizeof(WORD));
1053                     break;
1054                 case IFC_NDEVICES:
1055                     num = 1;
1056                     rc = CopyTabletData(lpOutput, &num,sizeof(num));
1057                     break;
1058                 case IFC_NCURSORS:
1059                     num = gNumCursors;
1060                     rc = CopyTabletData(lpOutput, &num,sizeof(num));
1061                     break;
1062                 default:
1063                     FIXME("WTI_INTERFACE unhandled index %i\n",nIndex);
1064                     rc = 0;
1065             }
1066             break;
1067         case WTI_DEFSYSCTX:
1068         case WTI_DDCTXS:
1069         case WTI_DEFCONTEXT:
1070             switch (nIndex)
1071             {
1072                 case 0:
1073                     rc = CopyTabletData(lpOutput, &gSysContext,
1074                             sizeof(LOGCONTEXTW));
1075                     break;
1076                 case CTX_NAME:
1077                     rc = CopyTabletData(lpOutput, &gSysContext.lcName,
1078                          (strlenW(gSysContext.lcName)+1) * sizeof(WCHAR));
1079                     break;
1080                 case CTX_OPTIONS:
1081                     rc = CopyTabletData(lpOutput, &gSysContext.lcOptions,
1082                                         sizeof(UINT));
1083                     break;
1084                 case CTX_STATUS:
1085                     rc = CopyTabletData(lpOutput, &gSysContext.lcStatus,
1086                                         sizeof(UINT));
1087                     break;
1088                 case CTX_LOCKS:
1089                     rc= CopyTabletData (lpOutput, &gSysContext.lcLocks,
1090                                         sizeof(UINT));
1091                     break;
1092                 case CTX_MSGBASE:
1093                     rc = CopyTabletData(lpOutput, &gSysContext.lcMsgBase,
1094                                         sizeof(UINT));
1095                     break;
1096                 case CTX_DEVICE:
1097                     rc = CopyTabletData(lpOutput, &gSysContext.lcDevice,
1098                                         sizeof(UINT));
1099                     break;
1100                 case CTX_PKTRATE:
1101                     rc = CopyTabletData(lpOutput, &gSysContext.lcPktRate,
1102                                         sizeof(UINT));
1103                     break;
1104                 case CTX_PKTDATA:
1105                     rc = CopyTabletData(lpOutput, &gSysContext.lcPktData,
1106                                         sizeof(WTPKT));
1107                     break;
1108                 case CTX_PKTMODE:
1109                     rc = CopyTabletData(lpOutput, &gSysContext.lcPktMode,
1110                                         sizeof(WTPKT));
1111                     break;
1112                 case CTX_MOVEMASK:
1113                     rc = CopyTabletData(lpOutput, &gSysContext.lcMoveMask,
1114                                         sizeof(WTPKT));
1115                     break;
1116                 case CTX_BTNDNMASK:
1117                     rc = CopyTabletData(lpOutput, &gSysContext.lcBtnDnMask,
1118                                         sizeof(DWORD));
1119                     break;
1120                 case CTX_BTNUPMASK:
1121                     rc = CopyTabletData(lpOutput, &gSysContext.lcBtnUpMask,
1122                                         sizeof(DWORD));
1123                     break;
1124                 case CTX_INORGX:
1125                     rc = CopyTabletData(lpOutput, &gSysContext.lcInOrgX,
1126                                         sizeof(LONG));
1127                     break;
1128                 case CTX_INORGY:
1129                     rc = CopyTabletData(lpOutput, &gSysContext.lcInOrgY,
1130                                         sizeof(LONG));
1131                     break;
1132                 case CTX_INORGZ:
1133                     rc = CopyTabletData(lpOutput, &gSysContext.lcInOrgZ,
1134                                         sizeof(LONG));
1135                     break;
1136                 case CTX_INEXTX:
1137                     rc = CopyTabletData(lpOutput, &gSysContext.lcInExtX,
1138                                         sizeof(LONG));
1139                     break;
1140                 case CTX_INEXTY:
1141                      rc = CopyTabletData(lpOutput, &gSysContext.lcInExtY,
1142                                         sizeof(LONG));
1143                     break;
1144                 case CTX_INEXTZ:
1145                      rc = CopyTabletData(lpOutput, &gSysContext.lcInExtZ,
1146                                         sizeof(LONG));
1147                     break;
1148                 case CTX_OUTORGX:
1149                      rc = CopyTabletData(lpOutput, &gSysContext.lcOutOrgX,
1150                                         sizeof(LONG));
1151                     break;
1152                 case CTX_OUTORGY:
1153                       rc = CopyTabletData(lpOutput, &gSysContext.lcOutOrgY,
1154                                         sizeof(LONG));
1155                     break;
1156                 case CTX_OUTORGZ:
1157                        rc = CopyTabletData(lpOutput, &gSysContext.lcOutOrgZ,
1158                                         sizeof(LONG));
1159                     break;
1160                case CTX_OUTEXTX:
1161                       rc = CopyTabletData(lpOutput, &gSysContext.lcOutExtX,
1162                                         sizeof(LONG));
1163                     break;
1164                 case CTX_OUTEXTY:
1165                        rc = CopyTabletData(lpOutput, &gSysContext.lcOutExtY,
1166                                         sizeof(LONG));
1167                     break;
1168                 case CTX_OUTEXTZ:
1169                        rc = CopyTabletData(lpOutput, &gSysContext.lcOutExtZ,
1170                                         sizeof(LONG));
1171                     break;
1172                 case CTX_SENSX:
1173                         rc = CopyTabletData(lpOutput, &gSysContext.lcSensX,
1174                                         sizeof(LONG));
1175                     break;
1176                 case CTX_SENSY:
1177                         rc = CopyTabletData(lpOutput, &gSysContext.lcSensY,
1178                                         sizeof(LONG));
1179                     break;
1180                 case CTX_SENSZ:
1181                         rc = CopyTabletData(lpOutput, &gSysContext.lcSensZ,
1182                                         sizeof(LONG));
1183                     break;
1184                 case CTX_SYSMODE:
1185                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysMode,
1186                                         sizeof(LONG));
1187                     break;
1188                 case CTX_SYSORGX:
1189                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysOrgX,
1190                                         sizeof(LONG));
1191                     break;
1192                 case CTX_SYSORGY:
1193                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysOrgY,
1194                                         sizeof(LONG));
1195                     break;
1196                 case CTX_SYSEXTX:
1197                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysExtX,
1198                                         sizeof(LONG));
1199                     break;
1200                 case CTX_SYSEXTY:
1201                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysExtY,
1202                                         sizeof(LONG));
1203                     break;
1204                 case CTX_SYSSENSX:
1205                         rc = CopyTabletData(lpOutput, &gSysContext.lcSysSensX,
1206                                         sizeof(LONG));
1207                     break;
1208                 case CTX_SYSSENSY:
1209                        rc = CopyTabletData(lpOutput, &gSysContext.lcSysSensY,
1210                                         sizeof(LONG));
1211                     break;
1212                 default:
1213                     FIXME("WTI_DEFSYSCTX unhandled index %i\n",nIndex);
1214                     rc = 0;
1215             }
1216             break;
1217         case WTI_CURSORS:
1218         case WTI_CURSORS+1:
1219         case WTI_CURSORS+2:
1220         case WTI_CURSORS+3:
1221         case WTI_CURSORS+4:
1222         case WTI_CURSORS+5:
1223         case WTI_CURSORS+6:
1224         case WTI_CURSORS+7:
1225         case WTI_CURSORS+8:
1226         case WTI_CURSORS+9:
1227             if (wCategory - WTI_CURSORS >= gNumCursors)
1228             {
1229                 rc = 0;
1230                 WARN("Requested cursor information for nonexistent cursor %d; only %d cursors\n",
1231                         wCategory - WTI_CURSORS, gNumCursors);
1232             }
1233             else
1234             {
1235                 tgtcursor = &gSysCursor[wCategory - WTI_CURSORS];
1236                 switch (nIndex)
1237                 {
1238                     case CSR_NAME:
1239                         rc = CopyTabletData(lpOutput, &tgtcursor->NAME,
1240                                             (strlenW(tgtcursor->NAME)+1) * sizeof(WCHAR));
1241                         break;
1242                     case CSR_ACTIVE:
1243                         rc = CopyTabletData(lpOutput,&tgtcursor->ACTIVE,
1244                                             sizeof(BOOL));
1245                         break;
1246                     case CSR_PKTDATA:
1247                         rc = CopyTabletData(lpOutput,&tgtcursor->PKTDATA,
1248                                             sizeof(WTPKT));
1249                         break;
1250                     case CSR_BUTTONS:
1251                         rc = CopyTabletData(lpOutput,&tgtcursor->BUTTONS,
1252                                             sizeof(BYTE));
1253                         break;
1254                     case CSR_BUTTONBITS:
1255                         rc = CopyTabletData(lpOutput,&tgtcursor->BUTTONBITS,
1256                                             sizeof(BYTE));
1257                         break;
1258                     case CSR_BTNNAMES:
1259                         FIXME("Button Names not returned correctly\n");
1260                         rc = CopyTabletData(lpOutput,&tgtcursor->BTNNAMES,
1261                                             tgtcursor->cchBTNNAMES*sizeof(WCHAR));
1262                         break;
1263                     case CSR_BUTTONMAP:
1264                         rc = CopyTabletData(lpOutput,&tgtcursor->BUTTONMAP,
1265                                             sizeof(BYTE)*32);
1266                         break;
1267                     case CSR_SYSBTNMAP:
1268                         rc = CopyTabletData(lpOutput,&tgtcursor->SYSBTNMAP,
1269                                             sizeof(BYTE)*32);
1270                         break;
1271                     case CSR_NPBTNMARKS:
1272                         rc = CopyTabletData(lpOutput,&tgtcursor->NPBTNMARKS,
1273                                             sizeof(UINT)*2);
1274                         break;
1275                     case CSR_NPBUTTON:
1276                         rc = CopyTabletData(lpOutput,&tgtcursor->NPBUTTON,
1277                                             sizeof(BYTE));
1278                         break;
1279                     case CSR_NPRESPONSE:
1280                         FIXME("Not returning CSR_NPRESPONSE correctly\n");
1281                         rc = 0;
1282                         break;
1283                     case CSR_TPBUTTON:
1284                         rc = CopyTabletData(lpOutput,&tgtcursor->TPBUTTON,
1285                                             sizeof(BYTE));
1286                         break;
1287                     case CSR_TPBTNMARKS:
1288                         rc = CopyTabletData(lpOutput,&tgtcursor->TPBTNMARKS,
1289                                             sizeof(UINT)*2);
1290                         break;
1291                     case CSR_TPRESPONSE:
1292                         FIXME("Not returning CSR_TPRESPONSE correctly\n");
1293                         rc = 0;
1294                         break;
1295                     case CSR_PHYSID:
1296                     {
1297                         DWORD id;
1298                         id = tgtcursor->PHYSID;
1299                         rc = CopyTabletData(lpOutput,&id,sizeof(DWORD));
1300                     }
1301                         break;
1302                     case CSR_MODE:
1303                         rc = CopyTabletData(lpOutput,&tgtcursor->MODE,sizeof(UINT));
1304                         break;
1305                     case CSR_MINPKTDATA:
1306                         rc = CopyTabletData(lpOutput,&tgtcursor->MINPKTDATA,
1307                             sizeof(UINT));
1308                         break;
1309                     case CSR_MINBUTTONS:
1310                         rc = CopyTabletData(lpOutput,&tgtcursor->MINBUTTONS,
1311                             sizeof(UINT));
1312                         break;
1313                     case CSR_CAPABILITIES:
1314                         rc = CopyTabletData(lpOutput,&tgtcursor->CAPABILITIES,
1315                             sizeof(UINT));
1316                         break;
1317                     case CSR_TYPE:
1318                         rc = CopyTabletData(lpOutput,&tgtcursor->TYPE,
1319                             sizeof(UINT));
1320                         break;
1321                     default:
1322                         FIXME("WTI_CURSORS unhandled index %i\n",nIndex);
1323                         rc = 0;
1324                 }
1325             }
1326             break;
1327         case WTI_DEVICES:
1328             switch (nIndex)
1329             {
1330                 case DVC_NAME:
1331                     rc = CopyTabletData(lpOutput,gSysDevice.NAME,
1332                                         (strlenW(gSysDevice.NAME)+1) * sizeof(WCHAR));
1333                     break;
1334                 case DVC_HARDWARE:
1335                     rc = CopyTabletData(lpOutput,&gSysDevice.HARDWARE,
1336                                         sizeof(UINT));
1337                     break;
1338                 case DVC_NCSRTYPES:
1339                     rc = CopyTabletData(lpOutput,&gSysDevice.NCSRTYPES,
1340                                         sizeof(UINT));
1341                     break;
1342                 case DVC_FIRSTCSR:
1343                     rc = CopyTabletData(lpOutput,&gSysDevice.FIRSTCSR,
1344                                         sizeof(UINT));
1345                     break;
1346                 case DVC_PKTRATE:
1347                     rc = CopyTabletData(lpOutput,&gSysDevice.PKTRATE,
1348                                         sizeof(UINT));
1349                     break;
1350                 case DVC_PKTDATA:
1351                     rc = CopyTabletData(lpOutput,&gSysDevice.PKTDATA,
1352                                         sizeof(WTPKT));
1353                     break;
1354                 case DVC_PKTMODE:
1355                     rc = CopyTabletData(lpOutput,&gSysDevice.PKTMODE,
1356                                         sizeof(WTPKT));
1357                     break;
1358                 case DVC_CSRDATA:
1359                     rc = CopyTabletData(lpOutput,&gSysDevice.CSRDATA,
1360                                         sizeof(WTPKT));
1361                     break;
1362                 case DVC_XMARGIN:
1363                     rc = CopyTabletData(lpOutput,&gSysDevice.XMARGIN,
1364                                         sizeof(INT));
1365                     break;
1366                 case DVC_YMARGIN:
1367                     rc = CopyTabletData(lpOutput,&gSysDevice.YMARGIN,
1368                                         sizeof(INT));
1369                     break;
1370                 case DVC_ZMARGIN:
1371                     rc = 0; /* unsupported */
1372                     /*
1373                     rc = CopyTabletData(lpOutput,&gSysDevice.ZMARGIN,
1374                                         sizeof(INT));
1375                     */
1376                     break;
1377                 case DVC_X:
1378                     rc = CopyTabletData(lpOutput,&gSysDevice.X,
1379                                         sizeof(AXIS));
1380                     break;
1381                 case DVC_Y:
1382                     rc = CopyTabletData(lpOutput,&gSysDevice.Y,
1383                                         sizeof(AXIS));
1384                     break;
1385                 case DVC_Z:
1386                     rc = 0; /* unsupported */
1387                     /*
1388                     rc = CopyTabletData(lpOutput,&gSysDevice.Z,
1389                                         sizeof(AXIS));
1390                     */
1391                     break;
1392                 case DVC_NPRESSURE:
1393                     rc = CopyTabletData(lpOutput,&gSysDevice.NPRESSURE,
1394                                         sizeof(AXIS));
1395                     break;
1396                 case DVC_TPRESSURE:
1397                     rc = 0; /* unsupported */
1398                     /*
1399                     rc = CopyTabletData(lpOutput,&gSysDevice.TPRESSURE,
1400                                         sizeof(AXIS));
1401                     */
1402                     break;
1403                 case DVC_ORIENTATION:
1404                     rc = CopyTabletData(lpOutput,&gSysDevice.ORIENTATION,
1405                                         sizeof(AXIS)*3);
1406                     break;
1407                 case DVC_ROTATION:
1408                     rc = 0; /* unsupported */
1409                     /*
1410                     rc = CopyTabletData(lpOutput,&gSysDevice.ROTATION,
1411                                         sizeof(AXIS)*3);
1412                     */
1413                     break;
1414                 case DVC_PNPID:
1415                     rc = CopyTabletData(lpOutput,gSysDevice.PNPID,
1416                                         (strlenW(gSysDevice.PNPID)+1)*sizeof(WCHAR));
1417                     break;
1418                 default:
1419                     FIXME("WTI_DEVICES unhandled index %i\n",nIndex);
1420                     rc = 0;
1421             }
1422             break;
1423         default:
1424             FIXME("Unhandled Category %i\n",wCategory);
1425     }
1426     return rc;
1427 }
1428
1429 #else /* SONAME_LIBXI */
1430
1431 /***********************************************************************
1432  *              AttachEventQueueToTablet (X11DRV.@)
1433  */
1434 int X11DRV_AttachEventQueueToTablet(HWND hOwner)
1435 {
1436     return 0;
1437 }
1438
1439 /***********************************************************************
1440  *              GetCurrentPacket (X11DRV.@)
1441  */
1442 int X11DRV_GetCurrentPacket(LPWTPACKET packet)
1443 {
1444     return 0;
1445 }
1446
1447 /***********************************************************************
1448  *              LoadTabletInfo (X11DRV.@)
1449  */
1450 void X11DRV_LoadTabletInfo(HWND hwnddefault)
1451 {
1452 }
1453
1454 /***********************************************************************
1455  *              WTInfoW (X11DRV.@)
1456  */
1457 UINT X11DRV_WTInfoW(UINT wCategory, UINT nIndex, LPVOID lpOutput)
1458 {
1459     return 0;
1460 }
1461
1462 #endif /* SONAME_LIBXI */