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