wintab.c 57.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * X11 tablet driver
 *
 * Copyright 2003 CodeWeavers (Aric Stewart)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23 24
 */

#include "config.h"
#include "wine/port.h"

#include <stdlib.h>
25
#include <stdarg.h>
26 27

#include "windef.h"
28 29
#include "winbase.h"
#include "winnls.h"
30
#include "x11drv.h"
31
#include "wine/library.h"
32
#include "wine/unicode.h"
33 34 35 36 37
#include "wine/debug.h"
#include "wintab.h"

WINE_DEFAULT_DEBUG_CHANNEL(wintab32);

38 39
#define WT_MAX_NAME_LEN 256

40 41
typedef struct tagWTI_CURSORS_INFO
{
42
    WCHAR   NAME[WT_MAX_NAME_LEN];
43 44 45 46 47 48 49 50 51 52 53 54 55
        /* a displayable zero-terminated string containing the name of the
         * cursor.
         */
    BOOL    ACTIVE;
        /* whether the cursor is currently connected. */
    WTPKT   PKTDATA;
        /* a bit mask indicating the packet data items supported when this
         * cursor is connected.
         */
    BYTE    BUTTONS;
        /* the number of buttons on this cursor. */
    BYTE    BUTTONBITS;
        /* the number of bits of raw button data returned by the hardware.*/
56 57
    DWORD   cchBTNNAMES;
    WCHAR   *BTNNAMES;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        /* a list of zero-terminated strings containing the names of the
         * cursor's buttons. The number of names in the list is the same as the
         * number of buttons on the cursor. The names are separated by a single
         * zero character; the list is terminated by two zero characters.
         */
    BYTE    BUTTONMAP[32];
        /* a 32 byte array of logical button numbers, one for each physical
         * button.
         */
    BYTE    SYSBTNMAP[32];
        /* a 32 byte array of button action codes, one for each logical
         * button.
         */
    BYTE    NPBUTTON;
        /* the physical button number of the button that is controlled by normal
         * pressure.
         */
    UINT    NPBTNMARKS[2];
        /* an array of two UINTs, specifying the button marks for the normal
         * pressure button. The first UINT contains the release mark; the second
         * contains the press mark.
         */
    UINT    *NPRESPONSE;
        /* an array of UINTs describing the pressure response curve for normal
         * pressure.
         */
    BYTE    TPBUTTON;
        /* the physical button number of the button that is controlled by
         * tangential pressure.
         */
    UINT    TPBTNMARKS[2];
        /* an array of two UINTs, specifying the button marks for the tangential
         * pressure button. The first UINT contains the release mark; the second
         * contains the press mark.
         */
    UINT    *TPRESPONSE;
        /* an array of UINTs describing the pressure response curve for
         * tangential pressure.
         */
    DWORD   PHYSID;
         /* a manufacturer-specific physical identifier for the cursor. This
          * value will distinguish the physical cursor from others on the same
          * device. This physical identifier allows applications to bind
          * functions to specific physical cursors, even if category numbers
          * change and multiple, otherwise identical, physical cursors are
          * present.
          */
    UINT    MODE;
        /* the cursor mode number of this cursor type, if this cursor type has
         * the CRC_MULTIMODE capability.
         */
    UINT    MINPKTDATA;
        /* the minimum set of data available from a physical cursor in this
         * cursor type, if this cursor type has the CRC_AGGREGATE capability.
         */
    UINT    MINBUTTONS;
        /* the minimum number of buttons of physical cursors in the cursor type,
         * if this cursor type has the CRC_AGGREGATE capability.
         */
    UINT    CAPABILITIES;
        /* flags indicating cursor capabilities, as defined below:
            CRC_MULTIMODE
                Indicates this cursor type describes one of several modes of a
                single physical cursor. Consecutive cursor type categories
                describe the modes; the CSR_MODE data item gives the mode number
                of each cursor type.
            CRC_AGGREGATE
                Indicates this cursor type describes several physical cursors
                that cannot be distinguished by software.
            CRC_INVERT
                Indicates this cursor type describes the physical cursor in its
                inverted orientation; the previous consecutive cursor type
                category describes the normal orientation.
         */
    UINT    TYPE;
        /* Manufacturer Unique id for the item type */
} WTI_CURSORS_INFO, *LPWTI_CURSORS_INFO;


typedef struct tagWTI_DEVICES_INFO
{
139
    WCHAR   NAME[WT_MAX_NAME_LEN];
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        /* a displayable null- terminated string describing the device,
         * manufacturer, and revision level.
         */
    UINT    HARDWARE;
        /* flags indicating hardware and driver capabilities, as defined
         * below:
            HWC_INTEGRATED:
                Indicates that the display and digitizer share the same surface.
            HWC_TOUCH
                Indicates that the cursor must be in physical contact with the
                device to report position.
            HWC_HARDPROX
                Indicates that device can generate events when the cursor is
                entering and leaving the physical detection range.
            HWC_PHYSID_CURSORS
                Indicates that device can uniquely identify the active cursor in
                hardware.
         */
    UINT    NCSRTYPES;
        /* the number of supported cursor types.*/
    UINT    FIRSTCSR;
        /* the first cursor type number for the device. */
    UINT    PKTRATE;
        /* the maximum packet report rate in Hertz. */
    WTPKT   PKTDATA;
        /* a bit mask indicating which packet data items are always available.*/
    WTPKT   PKTMODE;
        /* a bit mask indicating which packet data items are physically
         * relative, i.e., items for which the hardware can only report change,
         * not absolute measurement.
         */
    WTPKT   CSRDATA;
        /* a bit mask indicating which packet data items are only available when
         * certain cursors are connected. The individual cursor descriptions
         * must be consulted to determine which cursors return which data.
         */
    INT     XMARGIN;
    INT     YMARGIN;
    INT     ZMARGIN;
        /* the size of tablet context margins in tablet native coordinates, in
         * the x, y, and z directions, respectively.
         */
    AXIS    X;
    AXIS    Y;
    AXIS    Z;
        /* the tablet's range and resolution capabilities, in the x, y, and z
         * axes, respectively.
         */
    AXIS    NPRESSURE;
    AXIS    TPRESSURE;
        /* the tablet's range and resolution capabilities, for the normal and
         * tangential pressure inputs, respectively.
         */
    AXIS    ORIENTATION[3];
        /* a 3-element array describing the tablet's orientation range and
         * resolution capabilities.
         */
    AXIS    ROTATION[3];
        /* a 3-element array describing the tablet's rotation range and
         * resolution capabilities.
         */
201
    WCHAR   PNPID[WT_MAX_NAME_LEN];
202 203 204
        /* a null-terminated string containing the devices Plug and Play ID.*/
}   WTI_DEVICES_INFO, *LPWTI_DEVICES_INFO;

205 206 207 208 209 210 211 212 213 214

/***********************************************************************
 * WACOM WINTAB EXTENSIONS TO SUPPORT CSR_TYPE
 *   In Wintab 1.2, a CSR_TYPE feature was added.  This adds the
 *   ability to return a type of cursor on a tablet.
 *   Unfortunately, we cannot get the cursor type directly from X,
 *   and it is not specified directly anywhere.  So we virtualize
 *   the type here.  (This is unfortunate, the kernel module has
 *   the exact type, but we have no way of getting that module to
 *   pass us that type).
215 216 217 218 219 220 221 222 223 224
 *
 *   Reference linuxwacom driver project wcmCommon.c function
 *   idtotype for a much larger list of CSR_TYPE.
 *
 *   http://linuxwacom.cvs.sourceforge.net/linuxwacom/linuxwacom-prod/src/xdrv/wcmCommon.c?view=markup
 *
 *   The WTI_CURSORS_INFO.TYPE data is supposed to be used like this:
 *   (cursor.TYPE & 0x0F06) == target_cursor_type
 *   Reference: Section Unique ID
 *   http://www.wacomeng.com/devsupport/ibmpc/gddevpc.html
225 226 227 228 229
 */
#define CSR_TYPE_PEN        0x822
#define CSR_TYPE_ERASER     0x82a
#define CSR_TYPE_MOUSE_2D   0x007
#define CSR_TYPE_MOUSE_4D   0x094
230 231 232 233 234
/* CSR_TYPE_OTHER is a special value! assumed no real world signifigance
 * if a stylus type or eraser type eventually have this value
 * it'll be a bug.  As of 2008 05 21 we can be sure because
 * linux wacom lists all the known values and this isn't one of them */
#define CSR_TYPE_OTHER      0x000
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
typedef struct tagWTPACKET {
        HCTX pkContext;
        UINT pkStatus;
        LONG pkTime;
        WTPKT pkChanged;
        UINT pkSerialNumber;
        UINT pkCursor;
        DWORD pkButtons;
        DWORD pkX;
        DWORD pkY;
        DWORD pkZ;
        UINT pkNormalPressure;
        UINT pkTangentPressure;
        ORIENTATION pkOrientation;
        ROTATION pkRotation; /* 1.1 */
} WTPACKET, *LPWTPACKET;


254
#ifdef SONAME_LIBXI
255 256 257 258

#include <X11/Xlib.h>
#include <X11/extensions/XInput.h>

259 260 261 262 263 264 265
static int           motion_type;
static int           button_press_type;
static int           button_release_type;
static int           key_press_type;
static int           key_release_type;
static int           proximity_in_type;
static int           proximity_out_type;
266 267 268 269 270 271

static HWND          hwndTabletDefault;
static WTPACKET      gMsgPacket;
static DWORD         gSerial;
static INT           button_state[10];

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
/* Reference: http://www.wacomeng.com/devsupport/ibmpc/gddevpc.html
 *
 * Cursors come in sets of 3 normally
 * Cursor #0 = puck   device 1
 * Cursor #1 = stylus device 1
 * Cursor #2 = eraser device 1
 * Cursor #3 = puck   device 2
 * Cursor #4 = stylus device 2
 * Cursor #5 = eraser device 2
 * etc....
 *
 * A dual tracking/multimode tablet is one
 * that supports 2 independent cursors of the same or
 * different types simultaneously on a single tablet.
 * This makes our cursor layout potentially like this
 * Cursor #0 = puck   1 device 1
 * Cursor #1 = stylus 1 device 1
 * Cursor #2 = eraser 1 device 1
 * Cursor #3 = puck   2 device 1
 * Cursor #4 = stylus 2 device 1
 * Cursor #5 = eraser 2 device 1
 * Cursor #6 = puck   1 device 2
 * etc.....
 *
 * So with multimode tablets we could potentially need
 * 2 slots of the same type per tablet ie.
 * you are usuing 2 styluses at once so they would
 * get placed in Cursors #1 and Cursor #4
 *
 * Now say someone has 2 multimode tablets with 2 erasers each
 * now we would need Cursor #2, #5, #8, #11
 * So to support that we need CURSORMAX of 12 (0 to 11)
304
 * FIXME: we don't support more than 4 regular tablets or 2 multimode tablets */
305
#define             CURSORMAX 12
306

307
static LOGCONTEXTW      gSysContext;
308 309
static WTI_DEVICES_INFO gSysDevice;
static WTI_CURSORS_INFO gSysCursor[CURSORMAX];
310
static INT              gNumCursors; /* do NOT use this to iterate through gSysCursor slots */
311 312 313 314 315 316 317


/* XInput stuff */
static void *xinput_handle;

#define MAKE_FUNCPTR(f) static typeof(f) * p##f;
MAKE_FUNCPTR(XListInputDevices)
318
MAKE_FUNCPTR(XFreeDeviceList)
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
MAKE_FUNCPTR(XOpenDevice)
MAKE_FUNCPTR(XQueryDeviceState)
MAKE_FUNCPTR(XGetDeviceButtonMapping)
MAKE_FUNCPTR(XCloseDevice)
MAKE_FUNCPTR(XSelectExtensionEvent)
MAKE_FUNCPTR(XFreeDeviceState)
#undef MAKE_FUNCPTR

static INT X11DRV_XInput_Init(void)
{
    xinput_handle = wine_dlopen(SONAME_LIBXI, RTLD_NOW, NULL, 0);
    if (xinput_handle)
    {
#define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(xinput_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
        LOAD_FUNCPTR(XListInputDevices)
334
        LOAD_FUNCPTR(XFreeDeviceList)
335 336 337 338 339 340 341 342 343 344 345 346 347
        LOAD_FUNCPTR(XOpenDevice)
        LOAD_FUNCPTR(XGetDeviceButtonMapping)
        LOAD_FUNCPTR(XCloseDevice)
        LOAD_FUNCPTR(XSelectExtensionEvent)
        LOAD_FUNCPTR(XQueryDeviceState)
        LOAD_FUNCPTR(XFreeDeviceState)
#undef LOAD_FUNCPTR
        return 1;
    }
sym_not_found:
    return 0;
}

348 349 350 351 352
static int Tablet_ErrorHandler(Display *dpy, XErrorEvent *event, void* arg)
{
    return 1;
}

353 354 355 356 357 358 359 360 361
static void trace_axes(XValuatorInfoPtr val)
{
    int i;
    XAxisInfoPtr axis;

    for (i = 0, axis = val->axes ; i < val->num_axes; i++, axis++)
        TRACE("        Axis %d: [resolution %d|min_value %d|max_value %d]\n", i, axis->resolution, axis->min_value, axis->max_value);
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
BOOL match_token(const char *haystack, const char *needle)
{
    const char *p, *q;
    for (p = haystack; *p; )
    {
        while (*p && isspace(*p))
            p++;
        if (! *p)
            break;

        for (q = needle; *q && *p && tolower(*p) == tolower(*q); q++)
            p++;
        if (! *q && (isspace(*p) || !*p))
            return TRUE;

        while (*p && ! isspace(*p))
            p++;
    }
    return FALSE;
}

/*    Determining if an X device is a Tablet style device is an imperfect science.
**  We rely on common conventions around device names as well as the type reported
**  by Wacom tablets.  This code will likely need to be expanded for alternate tablet types
386 387 388 389 390 391
**
**    Wintab refers to any device that interacts with the tablet as a cursor,
**  (stylus, eraser, tablet mouse, airbrush, etc)
**  this is not to be confused with wacom x11 configuration "cursor" device.
**  Wacoms x11 config "cursor" refers to its device slot (which we mirror with
**  our gSysCursors) for puck like devices (tablet mice essentially).
392 393
*/

394
static BOOL is_tablet_cursor(const char *name, const char *type)
395
{
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    int i;
    static const char *tablet_cursor_whitelist[] = {
        "wacom",
        "wizardpen",
        "acecad",
        "tablet",
        "cursor",
        "stylus",
        "eraser",
        "pad",
        NULL
    };

    for (i=0; tablet_cursor_whitelist[i] != NULL; i++) {
        if (name && match_token(name, tablet_cursor_whitelist[i]))
            return TRUE;
        if (type && match_token(type, tablet_cursor_whitelist[i]))
            return TRUE;
    }
415 416 417 418 419
    return FALSE;
}

static BOOL is_stylus(const char *name, const char *type)
{
420 421 422 423
    int i;
    static const char* tablet_stylus_whitelist[] = {
        "stylus",
        "wizardpen",
424
        "acecad",
425 426 427 428 429 430 431 432 433 434
        NULL
    };

    for (i=0; tablet_stylus_whitelist[i] != NULL; i++) {
        if (name && match_token(name, tablet_stylus_whitelist[i]))
            return TRUE;
        if (type && match_token(type, tablet_stylus_whitelist[i]))
            return TRUE;
    }

435 436 437 438 439 440 441 442 443 444 445 446
    return FALSE;
}

static BOOL is_eraser(const char *name, const char *type)
{
    if (name && match_token(name, "eraser"))
        return TRUE;
    if (type && match_token(type, "eraser"))
        return TRUE;
    return FALSE;
}

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
/* cursors are placed in gSysCursor rows depending on their type
 * see CURSORMAX comments for more detail */
static BOOL add_system_cursor(LPWTI_CURSORS_INFO cursor)
{
    UINT offset = 0;

    if (cursor->TYPE == CSR_TYPE_PEN)
        offset = 1;
    else if (cursor->TYPE == CSR_TYPE_ERASER)
        offset = 2;

    for (; offset < CURSORMAX; offset += 3)
    {
        if (!gSysCursor[offset].ACTIVE)
        {
            gSysCursor[offset] = *cursor;
            ++gNumCursors;
            return TRUE;
        }
    }

    return FALSE;
}

471 472 473 474 475 476 477 478 479 480 481 482
static void disable_system_cursors(void)
{
    UINT i;

    for (i = 0; i < CURSORMAX; ++i)
    {
        gSysCursor[i].ACTIVE = 0;
    }

    gNumCursors = 0;
}

483

484 485 486
/***********************************************************************
 *             X11DRV_LoadTabletInfo (X11DRV.@)
 */
487 488
void X11DRV_LoadTabletInfo(HWND hwnddefault)
{
489 490 491 492
    const WCHAR SZ_CONTEXT_NAME[] = {'W','i','n','e',' ','T','a','b','l','e','t',' ','C','o','n','t','e','x','t',0};
    const WCHAR SZ_DEVICE_NAME[] = {'W','i','n','e',' ','T','a','b','l','e','t',' ','D','e','v','i','c','e',0};
    const WCHAR SZ_NON_PLUGINPLAY[] = {'n','o','n','-','p','l','u','g','i','n','p','l','a','y',0};

493
    struct x11drv_thread_data *data = x11drv_init_thread_data();
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    int num_devices;
    int loop;
    XDeviceInfo *devices;
    XDeviceInfo *target = NULL;
    BOOL    axis_read_complete= FALSE;

    XAnyClassPtr        any;
    XButtonInfoPtr      Button;
    XValuatorInfoPtr    Val;
    XAxisInfoPtr        Axis;

    XDevice *opendevice;

    if (!X11DRV_XInput_Init())
    {
Austin English's avatar
Austin English committed
509
        ERR("Unable to initialize the XInput library.\n");
510 511 512 513 514
        return;
    }

    hwndTabletDefault = hwnddefault;

Austin English's avatar
Austin English committed
515
    /* Do base initialization */
516 517
    strcpyW(gSysContext.lcName, SZ_CONTEXT_NAME);
    strcpyW(gSysDevice.NAME, SZ_DEVICE_NAME);
518

519
    gSysContext.lcOptions = CXO_SYSTEM;
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    gSysContext.lcLocks = CXL_INSIZE | CXL_INASPECT | CXL_MARGIN |
                               CXL_SENSITIVITY | CXL_SYSOUT;

    gSysContext.lcMsgBase= WT_DEFBASE;
    gSysContext.lcDevice = 0;
    gSysContext.lcPktData =
        PK_CONTEXT | PK_STATUS | PK_SERIAL_NUMBER| PK_TIME | PK_CURSOR |
        PK_BUTTONS |  PK_X | PK_Y | PK_NORMAL_PRESSURE | PK_ORIENTATION;
    gSysContext.lcMoveMask=
        PK_BUTTONS |  PK_X | PK_Y | PK_NORMAL_PRESSURE | PK_ORIENTATION;
    gSysContext.lcStatus = CXS_ONTOP;
    gSysContext.lcPktRate = 100;
    gSysContext.lcBtnDnMask = 0xffffffff;
    gSysContext.lcBtnUpMask = 0xffffffff;
    gSysContext.lcSensX = 65536;
    gSysContext.lcSensY = 65536;
    gSysContext.lcSensX = 65536;
    gSysContext.lcSensZ = 65536;
    gSysContext.lcSysSensX= 65536;
    gSysContext.lcSysSensY= 65536;

541 542 543
    /* initialize cursors */
    disable_system_cursors();

544 545 546 547 548 549 550
    /* Device Defaults */
    gSysDevice.HARDWARE = HWC_HARDPROX|HWC_PHYSID_CURSORS;
    gSysDevice.FIRSTCSR= 0;
    gSysDevice.PKTRATE = 100;
    gSysDevice.PKTDATA =
        PK_CONTEXT | PK_STATUS | PK_SERIAL_NUMBER| PK_TIME | PK_CURSOR |
        PK_BUTTONS |  PK_X | PK_Y | PK_NORMAL_PRESSURE | PK_ORIENTATION;
551
    strcpyW(gSysDevice.PNPID, SZ_NON_PLUGINPLAY);
552 553 554 555 556 557

    wine_tsx11_lock();

    devices = pXListInputDevices(data->display, &num_devices);
    if (!devices)
    {
558
        WARN("XInput Extensions reported as not avalable\n");
559 560 561
        wine_tsx11_unlock();
        return;
    }
562
    TRACE("XListInputDevices reports %d devices\n", num_devices);
563 564 565
    for (loop=0; loop < num_devices; loop++)
    {
        int class_loop;
566
        char *device_type = devices[loop].type ? XGetAtomName(data->display, devices[loop].type) : NULL;
567
        WTI_CURSORS_INFO cursor;
568

569
        TRACE("Device %i:  [id %d|name %s|type %s|num_classes %d|use %d]\n",
570
                loop, (int) devices[loop].id, devices[loop].name, device_type ? device_type : "",
571
                devices[loop].num_classes, devices[loop].use );
572

573 574 575 576 577 578
        switch (devices[loop].use)
        {
        case IsXExtensionDevice:
#ifdef IsXExtensionPointer
        case IsXExtensionPointer:
#endif
579 580 581 582
#ifdef IsXExtensionKeyboard
	case IsXExtensionKeyboard:
#endif
            TRACE("Is XExtension: Device, Keyboard, or Pointer\n");
583 584
            target = &devices[loop];

585 586 587
            if (strlen(target->name) >= WT_MAX_NAME_LEN)
            {
                ERR("Input device '%s' name too long - skipping\n", wine_dbgstr_a(target->name));
588
                break;
589 590
            }

591
            X11DRV_expect_error(data->display, Tablet_ErrorHandler, NULL);
592
            opendevice = pXOpenDevice(data->display,target->id);
593
            if (!X11DRV_check_error() && opendevice)
594 595 596 597 598
            {
                unsigned char map[32];
                int i;
                int shft = 0;

599
                X11DRV_expect_error(data->display,Tablet_ErrorHandler,NULL);
600 601
                cursor.BUTTONS = pXGetDeviceButtonMapping(data->display, opendevice, map, 32);
                if (X11DRV_check_error() || cursor.BUTTONS <= 0)
602 603 604
                {
                    TRACE("No buttons, Non Tablet Device\n");
                    pXCloseDevice(data->display, opendevice);
605
                    break;
606
                }
607

608
                for (i=0; i< cursor.BUTTONS; i++,shft++)
609
                {
610 611
                    cursor.BUTTONMAP[i] = map[i];
                    cursor.SYSBTNMAP[i] = (1<<shft);
612 613 614 615 616 617
                }
                pXCloseDevice(data->display, opendevice);
            }
            else
            {
                WARN("Unable to open device %s\n",target->name);
618
                break;
619
            }
620
            MultiByteToWideChar(CP_UNIXCP, 0, target->name, -1, cursor.NAME, WT_MAX_NAME_LEN);
621

622
            if (! is_tablet_cursor(target->name, device_type))
623
            {
624
                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",
625
                     loop, devices[loop].name, device_type ? device_type : "");
626
                break;
627 628
            }

629 630
            cursor.ACTIVE = 1;
            cursor.PKTDATA = PK_TIME | PK_CURSOR | PK_BUTTONS |  PK_X | PK_Y |
631 632 633
                              PK_NORMAL_PRESSURE | PK_TANGENT_PRESSURE |
                              PK_ORIENTATION;

634 635 636 637 638
            cursor.PHYSID = target->id;
            cursor.NPBUTTON = 1;
            cursor.NPBTNMARKS[0] = 0 ;
            cursor.NPBTNMARKS[1] = 1 ;
            cursor.CAPABILITIES = CRC_MULTIMODE;
639 640

            /* prefer finding TYPE_PEN(most capable) */
641
            if (is_stylus(target->name, device_type))
642
                cursor.TYPE = CSR_TYPE_PEN;
643
            else if (is_eraser(target->name, device_type))
644
                cursor.TYPE = CSR_TYPE_ERASER;
645
            else
646
                cursor.TYPE = CSR_TYPE_OTHER;
647

648
            any = target->inputclassinfo;
649 650 651 652 653

            for (class_loop = 0; class_loop < target->num_classes; class_loop++)
            {
                switch (any->class)
                {
654

655
                    case ValuatorClass:
656 657 658 659 660 661
                        Val = (XValuatorInfoPtr) any;
                        TRACE("    ValidatorInput %d: [class %d|length %d|num_axes %d|mode %d|motion_buffer %ld]\n",
                                class_loop, (int) Val->class, Val->length, Val->num_axes, Val->mode, Val->motion_buffer);
                        if (TRACE_ON(wintab32))
                            trace_axes(Val);

662 663 664 665 666 667
                        /* FIXME:  This is imperfect; we compute our devices capabilities based upon the
                        **         first pen type device we find.  However, a more correct implementation
                        **         would require acquiring a wide variety of tablets and running through
                        **         the various inputs to see what the values are.  Odds are that a
                        **         more 'correct' algorithm would condense to this one anyway.
                        */
668
                        if (!axis_read_complete && cursor.TYPE == CSR_TYPE_PEN)
669 670 671 672 673 674 675 676 677
                        {
                            Axis = (XAxisInfoPtr) ((char *) Val + sizeof
                                (XValuatorInfo));

                            if (Val->num_axes>=1)
                            {
                                /* Axis 1 is X */
                                gSysDevice.X.axMin = Axis->min_value;
                                gSysDevice.X.axMax= Axis->max_value;
678
                                gSysDevice.X.axUnits = TU_INCHES;
679 680 681 682 683 684 685 686 687 688 689 690
                                gSysDevice.X.axResolution = Axis->resolution;
                                gSysContext.lcInOrgX = Axis->min_value;
                                gSysContext.lcSysOrgX = Axis->min_value;
                                gSysContext.lcInExtX = Axis->max_value;
                                gSysContext.lcSysExtX = Axis->max_value;
                                Axis++;
                            }
                            if (Val->num_axes>=2)
                            {
                                /* Axis 2 is Y */
                                gSysDevice.Y.axMin = Axis->min_value;
                                gSysDevice.Y.axMax= Axis->max_value;
691
                                gSysDevice.Y.axUnits = TU_INCHES;
692 693 694 695 696 697 698 699 700 701 702 703
                                gSysDevice.Y.axResolution = Axis->resolution;
                                gSysContext.lcInOrgY = Axis->min_value;
                                gSysContext.lcSysOrgY = Axis->min_value;
                                gSysContext.lcInExtY = Axis->max_value;
                                gSysContext.lcSysExtY = Axis->max_value;
                                Axis++;
                            }
                            if (Val->num_axes>=3)
                            {
                                /* Axis 3 is Normal Pressure */
                                gSysDevice.NPRESSURE.axMin = Axis->min_value;
                                gSysDevice.NPRESSURE.axMax= Axis->max_value;
704
                                gSysDevice.NPRESSURE.axUnits = TU_INCHES;
705 706 707 708 709 710 711 712 713 714 715 716 717 718
                                gSysDevice.NPRESSURE.axResolution =
                                                        Axis->resolution;
                                Axis++;
                            }
                            if (Val->num_axes >= 5)
                            {
                                /* Axis 4 and 5 are X and Y tilt */
                                XAxisInfoPtr        XAxis = Axis;
                                Axis++;
                                if (max (abs(Axis->max_value),
                                         abs(XAxis->max_value)))
                                {
                                    gSysDevice.ORIENTATION[0].axMin = 0;
                                    gSysDevice.ORIENTATION[0].axMax = 3600;
719 720 721
                                    gSysDevice.ORIENTATION[0].axUnits = TU_CIRCLE;
                                    gSysDevice.ORIENTATION[0].axResolution
                                                                = CASTFIX32(3600);
722 723
                                    gSysDevice.ORIENTATION[1].axMin = -1000;
                                    gSysDevice.ORIENTATION[1].axMax = 1000;
724 725 726
                                    gSysDevice.ORIENTATION[1].axUnits = TU_CIRCLE;
                                    gSysDevice.ORIENTATION[1].axResolution
                                                                = CASTFIX32(3600);
727 728 729 730 731 732 733 734
                                    Axis++;
                                }
                            }
                            axis_read_complete = TRUE;
                        }
                        break;
                    case ButtonClass:
                    {
735 736
                        int cchBuf = 512;
                        int cchPos = 0;
737 738 739
                        int i;

                        Button = (XButtonInfoPtr) any;
740 741
                        TRACE("    ButtonInput %d: [class %d|length %d|num_buttons %d]\n",
                                class_loop, (int) Button->class, Button->length, Button->num_buttons);
742 743
                        cursor.BTNNAMES = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*cchBuf);
                        for (i = 0; i < cursor.BUTTONS; i++)
744
                        {
745
                            /* FIXME - these names are probably incorrect */
746
                            int cch = strlenW(cursor.NAME) + 1;
747 748 749
                            while (cch > cchBuf - cchPos - 1) /* we want one extra byte for the last NUL */
                            {
                                cchBuf *= 2;
750
                                cursor.BTNNAMES = HeapReAlloc(GetProcessHeap(), 0, cursor.BTNNAMES, sizeof(WCHAR)*cchBuf);
751 752
                            }

753
                            strcpyW(cursor.BTNNAMES + cchPos, cursor.NAME);
754
                            cchPos += cch;
755
                        }
756 757 758
                        cursor.BTNNAMES[cchPos++] = 0;
                        cursor.BTNNAMES = HeapReAlloc(GetProcessHeap(), 0, cursor.BTNNAMES, sizeof(WCHAR)*cchPos);
                        cursor.cchBTNNAMES = cchPos;
759 760
                    }
                    break;
761
                } /* switch any->class */
762
                any = (XAnyClassPtr) ((char*) any + any->length);
763 764 765
            } /* for class_loop */
            if (!add_system_cursor(&cursor))
                FIXME("Skipping this cursor due to lack of system cursor slots.\n");
766
            break;
767
        } /* switch devices.use */
768
        XFree(device_type);
769
    } /* for XListInputDevices */
770
    pXFreeDeviceList(devices);
771 772

    if (axis_read_complete)
773 774
        gSysDevice.NCSRTYPES = gNumCursors;
    else
775
    {
776 777
        disable_system_cursors();
        WARN("Did not find a valid stylus, unable to determine system context parameters. Wintab is disabled.\n");
778 779
    }

780
    wine_tsx11_unlock();
781 782 783 784
}

static int figure_deg(int x, int y)
{
785
    float angle;
786

787 788 789 790
    angle = atan2((float)y, (float)x);
    angle += M_PI_2;
    if (angle <= 0)
	angle += 2 * M_PI;
791

792
    return (0.5 + (angle * 1800.0 / M_PI));
793 794
}

795
static int get_button_state(int curnum)
796
{
797
    return button_state[curnum];
798 799
}

800
static void set_button_state(int curnum, XID deviceid)
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
{
    struct x11drv_thread_data *data = x11drv_thread_data();
    XDevice *device;
    XDeviceState *state;
    XInputClass  *class;
    int loop;
    int rc = 0;

    wine_tsx11_lock();
    device = pXOpenDevice(data->display,deviceid);
    state = pXQueryDeviceState(data->display,device);

    if (state)
    {
        class = state->data;
        for (loop = 0; loop < state->num_classes; loop++)
        {
            if (class->class == ButtonClass)
            {
                int loop2;
                XButtonState *button_state =  (XButtonState*)class;
822
                for (loop2 = 0; loop2 < button_state->num_buttons; loop2++)
823 824 825
                {
                    if (button_state->buttons[loop2 / 8] & (1 << (loop2 % 8)))
                    {
826
                        rc |= (1<<loop2);
827 828 829 830 831 832 833 834
                    }
                }
            }
            class = (XInputClass *) ((char *) class + class->length);
        }
    }
    pXFreeDeviceState(state);
    wine_tsx11_unlock();
835 836 837 838 839 840
    button_state[curnum] = rc;
}

static int cursor_from_device(DWORD deviceid, LPWTI_CURSORS_INFO *cursorp)
{
    int i;
841 842
    for (i = 0; i < CURSORMAX; i++)
        if (gSysCursor[i].ACTIVE && gSysCursor[i].PHYSID == deviceid)
843 844 845 846 847 848 849
        {
            *cursorp = &gSysCursor[i];
            return i;
        }

    ERR("Could not map device id %d to a cursor\n", (int) deviceid);
    return -1;
850 851
}

852
static void motion_event( HWND hwnd, XEvent *event )
853
{
854
    XDeviceMotionEvent *motion = (XDeviceMotionEvent *)event;
855 856 857 858
    LPWTI_CURSORS_INFO cursor;
    int curnum = cursor_from_device(motion->deviceid, &cursor);
    if (curnum < 0)
        return;
859

860 861
    memset(&gMsgPacket,0,sizeof(WTPACKET));

862
    TRACE("Received tablet motion event (%p); device id %d, cursor num %d\n",hwnd, (int) motion->deviceid, curnum);
863 864

    /* Set cursor to inverted if cursor is the eraser */
865
    gMsgPacket.pkStatus = (cursor->TYPE  == CSR_TYPE_ERASER ? TPS_INVERT:0);
866 867
    gMsgPacket.pkTime = EVENT_x11_time_to_win32_time(motion->time);
    gMsgPacket.pkSerialNumber = gSerial++;
868
    gMsgPacket.pkCursor = curnum;
869 870 871 872 873 874 875 876
    gMsgPacket.pkX = motion->axis_data[0];
    gMsgPacket.pkY = motion->axis_data[1];
    gMsgPacket.pkOrientation.orAzimuth = figure_deg(motion->axis_data[3],motion->axis_data[4]);
    gMsgPacket.pkOrientation.orAltitude = ((1000 - 15 * max
                                            (abs(motion->axis_data[3]),
                                             abs(motion->axis_data[4])))
                                           * (gMsgPacket.pkStatus & TPS_INVERT?-1:1));
    gMsgPacket.pkNormalPressure = motion->axis_data[2];
877
    gMsgPacket.pkButtons = get_button_state(curnum);
878
    SendMessageW(hwndTabletDefault,WT_PACKET,gMsgPacket.pkSerialNumber,(LPARAM)hwnd);
879 880 881 882 883
}

static void button_event( HWND hwnd, XEvent *event )
{
    XDeviceButtonEvent *button = (XDeviceButtonEvent *) event;
884 885 886 887
    LPWTI_CURSORS_INFO cursor;
    int curnum = cursor_from_device(button->deviceid, &cursor);
    if (curnum < 0)
        return;
888 889 890 891 892 893

    memset(&gMsgPacket,0,sizeof(WTPACKET));

    TRACE("Received tablet button %s event\n", (event->type == button_press_type)?"press":"release");

    /* Set cursor to inverted if cursor is the eraser */
894
    gMsgPacket.pkStatus = (cursor->TYPE == CSR_TYPE_ERASER ? TPS_INVERT:0);
895
    set_button_state(curnum, button->deviceid);
896 897
    gMsgPacket.pkTime = EVENT_x11_time_to_win32_time(button->time);
    gMsgPacket.pkSerialNumber = gSerial++;
898
    gMsgPacket.pkCursor = curnum;
899 900 901 902 903 904 905
    gMsgPacket.pkX = button->axis_data[0];
    gMsgPacket.pkY = button->axis_data[1];
    gMsgPacket.pkOrientation.orAzimuth = figure_deg(button->axis_data[3],button->axis_data[4]);
    gMsgPacket.pkOrientation.orAltitude = ((1000 - 15 * max(abs(button->axis_data[3]),
                                                            abs(button->axis_data[4])))
                                           * (gMsgPacket.pkStatus & TPS_INVERT?-1:1));
    gMsgPacket.pkNormalPressure = button->axis_data[2];
906
    gMsgPacket.pkButtons = get_button_state(curnum);
907
    SendMessageW(hwndTabletDefault,WT_PACKET,gMsgPacket.pkSerialNumber,(LPARAM)hwnd);
908 909 910 911 912
}

static void key_event( HWND hwnd, XEvent *event )
{
    if (event->type == key_press_type)
913 914
        FIXME("Received tablet key press event\n");
    else
915 916
        FIXME("Received tablet key release event\n");
}
917

918 919 920
static void proximity_event( HWND hwnd, XEvent *event )
{
    XProximityNotifyEvent *proximity = (XProximityNotifyEvent *) event;
921 922
    LPWTI_CURSORS_INFO cursor;
    int curnum = cursor_from_device(proximity->deviceid, &cursor);
923 924 925 926
    LPARAM proximity_info;

    TRACE("hwnd=%p\n", hwnd);

927 928
    if (curnum < 0)
        return;
929 930 931 932

    memset(&gMsgPacket,0,sizeof(WTPACKET));

    /* Set cursor to inverted if cursor is the eraser */
933
    gMsgPacket.pkStatus = (cursor->TYPE == CSR_TYPE_ERASER ? TPS_INVERT:0);
934 935 936
    gMsgPacket.pkStatus |= (event->type==proximity_out_type)?TPS_PROXIMITY:0;
    gMsgPacket.pkTime = EVENT_x11_time_to_win32_time(proximity->time);
    gMsgPacket.pkSerialNumber = gSerial++;
937
    gMsgPacket.pkCursor = curnum;
938 939 940 941 942 943 944
    gMsgPacket.pkX = proximity->axis_data[0];
    gMsgPacket.pkY = proximity->axis_data[1];
    gMsgPacket.pkOrientation.orAzimuth = figure_deg(proximity->axis_data[3],proximity->axis_data[4]);
    gMsgPacket.pkOrientation.orAltitude = ((1000 - 15 * max(abs(proximity->axis_data[3]),
                                                            abs(proximity->axis_data[4])))
                                           * (gMsgPacket.pkStatus & TPS_INVERT?-1:1));
    gMsgPacket.pkNormalPressure = proximity->axis_data[2];
945
    gMsgPacket.pkButtons = get_button_state(curnum);
946

947 948 949 950 951 952 953 954 955 956
    /* FIXME: LPARAM loword is true when cursor entering context, false when leaving context
     * This needs to be handled here or in wintab32.  Using the proximity_in_type is not correct
     * but kept for now.
     * LPARAM hiword is "non-zero when the cursor is leaving or entering hardware proximity"
     * WPARAM contains context handle.
     * HWND to HCTX is handled by wintab32.
     */
    proximity_info = MAKELPARAM((event->type == proximity_in_type),
                     (event->type == proximity_in_type) || (event->type == proximity_out_type));
    SendMessageW(hwndTabletDefault, WT_PROXIMITY, (WPARAM)hwnd, proximity_info);
957 958
}

959 960 961
/***********************************************************************
 *		X11DRV_AttachEventQueueToTablet (X11DRV.@)
 */
962 963
int X11DRV_AttachEventQueueToTablet(HWND hOwner)
{
964
    struct x11drv_thread_data *data = x11drv_init_thread_data();
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
    int             num_devices;
    int             loop;
    int             cur_loop;
    XDeviceInfo     *devices;
    XDeviceInfo     *target = NULL;
    XDevice         *the_device;
    XEventClass     event_list[7];
    Window          win = X11DRV_get_whole_window( hOwner );

    if (!win) return 0;

    TRACE("Creating context for window %p (%lx)  %i cursors\n", hOwner, win, gNumCursors);

    wine_tsx11_lock();
    devices = pXListInputDevices(data->display, &num_devices);

981
    X11DRV_expect_error(data->display,Tablet_ErrorHandler,NULL);
982
    for (cur_loop=0; cur_loop < CURSORMAX; cur_loop++)
983
    {
984
        char   cursorNameA[WT_MAX_NAME_LEN];
985 986
        int    event_number=0;

987 988
        if (!gSysCursor[cur_loop].ACTIVE) continue;

989 990
        /* the cursor name fits in the buffer because too long names are skipped */
        WideCharToMultiByte(CP_UNIXCP, 0, gSysCursor[cur_loop].NAME, -1, cursorNameA, WT_MAX_NAME_LEN, NULL, NULL);
991
        for (loop=0; loop < num_devices; loop ++)
992
            if (strcmp(devices[loop].name, cursorNameA) == 0)
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
                target = &devices[loop];

        TRACE("Opening cursor %i id %i\n",cur_loop,(INT)target->id);

        the_device = pXOpenDevice(data->display, target->id);

        if (!the_device)
        {
            WARN("Unable to Open device\n");
            continue;
        }

        if (the_device->num_classes > 0)
        {
1007
            DeviceKeyPress(the_device, key_press_type, event_list[event_number]);
1008
            if (key_press_type) event_number++;
1009
            DeviceKeyRelease(the_device, key_release_type, event_list[event_number]);
1010
            if (key_release_type) event_number++;
1011
            DeviceButtonPress(the_device, button_press_type, event_list[event_number]);
1012
            if (button_press_type) event_number++;
1013
            DeviceButtonRelease(the_device, button_release_type, event_list[event_number]);
1014
            if (button_release_type) event_number++;
1015
            DeviceMotionNotify(the_device, motion_type, event_list[event_number]);
1016
            if (motion_type) event_number++;
1017
            ProximityIn(the_device, proximity_in_type, event_list[event_number]);
1018
            if (proximity_in_type) event_number++;
1019
            ProximityOut(the_device, proximity_out_type, event_list[event_number]);
1020
            if (proximity_out_type) event_number++;
1021 1022 1023 1024 1025 1026 1027 1028 1029

            if (key_press_type) X11DRV_register_event_handler( key_press_type, key_event );
            if (key_release_type) X11DRV_register_event_handler( key_release_type, key_event );
            if (button_press_type) X11DRV_register_event_handler( button_press_type, button_event );
            if (button_release_type) X11DRV_register_event_handler( button_release_type, button_event );
            if (motion_type) X11DRV_register_event_handler( motion_type, motion_event );
            if (proximity_in_type) X11DRV_register_event_handler( proximity_in_type, proximity_event );
            if (proximity_out_type) X11DRV_register_event_handler( proximity_out_type, proximity_event );

1030
            pXSelectExtensionEvent(data->display, win, event_list, event_number);
1031 1032
        }
    }
1033 1034
    XSync(data->display, False);
    X11DRV_check_error();
1035

1036
    if (NULL != devices) pXFreeDeviceList(devices);
1037 1038 1039 1040
    wine_tsx11_unlock();
    return 0;
}

1041 1042 1043
/***********************************************************************
 *		X11DRV_GetCurrentPacket (X11DRV.@)
 */
1044
int X11DRV_GetCurrentPacket(LPWTPACKET packet)
1045
{
1046
    *packet = gMsgPacket;
1047 1048 1049 1050
    return 1;
}


1051
static inline int CopyTabletData(LPVOID target, LPCVOID src, INT size)
1052
{
1053 1054 1055 1056 1057 1058
    /*
     * It is valid to call CopyTabletData with NULL.
     * This handles the WTInfo() case where lpOutput is null.
     */
    if(target != NULL)
        memcpy(target,src,size);
1059 1060 1061 1062
    return(size);
}

/***********************************************************************
1063
 *		X11DRV_WTInfoW (X11DRV.@)
1064
 */
1065
UINT X11DRV_WTInfoW(UINT wCategory, UINT nIndex, LPVOID lpOutput)
1066
{
1067 1068 1069 1070 1071 1072 1073 1074
    /*
     * It is valid to call WTInfoA with lpOutput == NULL, as per standard.
     * lpOutput == NULL signifies the user only wishes
     * to find the size of the data.
     * NOTE:
     * From now on use CopyTabletData to fill lpOutput. memcpy will break
     * the code.
     */
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    int rc = 0;
    LPWTI_CURSORS_INFO  tgtcursor;
    TRACE("(%u, %u, %p)\n", wCategory, nIndex, lpOutput);

    switch(wCategory)
    {
        case 0:
            /* return largest necessary buffer */
            TRACE("%i cursors\n",gNumCursors);
            if (gNumCursors>0)
            {
                FIXME("Return proper size\n");
1087
                rc = 200;
1088 1089 1090 1091 1092 1093
            }
            break;
        case WTI_INTERFACE:
            switch (nIndex)
            {
                WORD version;
1094
                UINT num;
1095
                case IFC_WINTABID:
1096 1097 1098
                {
                    static const WCHAR driver[] = {'W','i','n','e',' ','W','i','n','t','a','b',' ','1','.','1',0};
                    rc = CopyTabletData(lpOutput, driver, (strlenW(driver) + 1) * sizeof(WCHAR));
1099
                    break;
1100
                }
1101 1102 1103 1104 1105 1106 1107 1108
                case IFC_SPECVERSION:
                    version = (0x01) | (0x01 << 8);
                    rc = CopyTabletData(lpOutput, &version,sizeof(WORD));
                    break;
                case IFC_IMPLVERSION:
                    version = (0x00) | (0x01 << 8);
                    rc = CopyTabletData(lpOutput, &version,sizeof(WORD));
                    break;
1109 1110 1111 1112 1113 1114 1115 1116
                case IFC_NDEVICES:
                    num = 1;
                    rc = CopyTabletData(lpOutput, &num,sizeof(num));
                    break;
                case IFC_NCURSORS:
                    num = gNumCursors;
                    rc = CopyTabletData(lpOutput, &num,sizeof(num));
                    break;
1117 1118 1119 1120
                default:
                    FIXME("WTI_INTERFACE unhandled index %i\n",nIndex);
                    rc = 0;
            }
1121
            break;
1122 1123 1124 1125 1126 1127
        case WTI_DEFSYSCTX:
        case WTI_DDCTXS:
        case WTI_DEFCONTEXT:
            switch (nIndex)
            {
                case 0:
1128
                    rc = CopyTabletData(lpOutput, &gSysContext,
1129
                            sizeof(LOGCONTEXTW));
1130 1131
                    break;
                case CTX_NAME:
1132
                    rc = CopyTabletData(lpOutput, gSysContext.lcName,
1133
                         (strlenW(gSysContext.lcName)+1) * sizeof(WCHAR));
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
                    break;
                case CTX_OPTIONS:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcOptions,
                                        sizeof(UINT));
                    break;
                case CTX_STATUS:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcStatus,
                                        sizeof(UINT));
                    break;
                case CTX_LOCKS:
                    rc= CopyTabletData (lpOutput, &gSysContext.lcLocks,
                                        sizeof(UINT));
                    break;
                case CTX_MSGBASE:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcMsgBase,
                                        sizeof(UINT));
                    break;
                case CTX_DEVICE:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcDevice,
                                        sizeof(UINT));
                    break;
                case CTX_PKTRATE:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcPktRate,
                                        sizeof(UINT));
                    break;
1159 1160 1161 1162
                case CTX_PKTDATA:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcPktData,
                                        sizeof(WTPKT));
                    break;
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
                case CTX_PKTMODE:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcPktMode,
                                        sizeof(WTPKT));
                    break;
                case CTX_MOVEMASK:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcMoveMask,
                                        sizeof(WTPKT));
                    break;
                case CTX_BTNDNMASK:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcBtnDnMask,
                                        sizeof(DWORD));
                    break;
                case CTX_BTNUPMASK:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcBtnUpMask,
                                        sizeof(DWORD));
                    break;
                case CTX_INORGX:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcInOrgX,
                                        sizeof(LONG));
                    break;
                case CTX_INORGY:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcInOrgY,
                                        sizeof(LONG));
                    break;
                case CTX_INORGZ:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcInOrgZ,
                                        sizeof(LONG));
                    break;
                case CTX_INEXTX:
                    rc = CopyTabletData(lpOutput, &gSysContext.lcInExtX,
                                        sizeof(LONG));
                    break;
                case CTX_INEXTY:
                     rc = CopyTabletData(lpOutput, &gSysContext.lcInExtY,
                                        sizeof(LONG));
                    break;
                case CTX_INEXTZ:
                     rc = CopyTabletData(lpOutput, &gSysContext.lcInExtZ,
                                        sizeof(LONG));
                    break;
                case CTX_OUTORGX:
                     rc = CopyTabletData(lpOutput, &gSysContext.lcOutOrgX,
                                        sizeof(LONG));
                    break;
                case CTX_OUTORGY:
                      rc = CopyTabletData(lpOutput, &gSysContext.lcOutOrgY,
                                        sizeof(LONG));
                    break;
                case CTX_OUTORGZ:
                       rc = CopyTabletData(lpOutput, &gSysContext.lcOutOrgZ,
                                        sizeof(LONG));
                    break;
               case CTX_OUTEXTX:
                      rc = CopyTabletData(lpOutput, &gSysContext.lcOutExtX,
                                        sizeof(LONG));
                    break;
                case CTX_OUTEXTY:
                       rc = CopyTabletData(lpOutput, &gSysContext.lcOutExtY,
                                        sizeof(LONG));
                    break;
                case CTX_OUTEXTZ:
                       rc = CopyTabletData(lpOutput, &gSysContext.lcOutExtZ,
                                        sizeof(LONG));
                    break;
                case CTX_SENSX:
                        rc = CopyTabletData(lpOutput, &gSysContext.lcSensX,
                                        sizeof(LONG));
                    break;
                case CTX_SENSY:
                        rc = CopyTabletData(lpOutput, &gSysContext.lcSensY,
                                        sizeof(LONG));
                    break;
                case CTX_SENSZ:
                        rc = CopyTabletData(lpOutput, &gSysContext.lcSensZ,
                                        sizeof(LONG));
                    break;
                case CTX_SYSMODE:
                        rc = CopyTabletData(lpOutput, &gSysContext.lcSysMode,
                                        sizeof(LONG));
                    break;
                case CTX_SYSORGX:
                        rc = CopyTabletData(lpOutput, &gSysContext.lcSysOrgX,
                                        sizeof(LONG));
                    break;
                case CTX_SYSORGY:
                        rc = CopyTabletData(lpOutput, &gSysContext.lcSysOrgY,
                                        sizeof(LONG));
                    break;
                case CTX_SYSEXTX:
                        rc = CopyTabletData(lpOutput, &gSysContext.lcSysExtX,
                                        sizeof(LONG));
                    break;
                case CTX_SYSEXTY:
                        rc = CopyTabletData(lpOutput, &gSysContext.lcSysExtY,
                                        sizeof(LONG));
                    break;
                case CTX_SYSSENSX:
                        rc = CopyTabletData(lpOutput, &gSysContext.lcSysSensX,
                                        sizeof(LONG));
                    break;
                case CTX_SYSSENSY:
                       rc = CopyTabletData(lpOutput, &gSysContext.lcSysSensY,
                                        sizeof(LONG));
                    break;
                default:
                    FIXME("WTI_DEFSYSCTX unhandled index %i\n",nIndex);
                    rc = 0;
            }
            break;
        case WTI_CURSORS:
        case WTI_CURSORS+1:
        case WTI_CURSORS+2:
        case WTI_CURSORS+3:
        case WTI_CURSORS+4:
        case WTI_CURSORS+5:
        case WTI_CURSORS+6:
        case WTI_CURSORS+7:
        case WTI_CURSORS+8:
        case WTI_CURSORS+9:
1282 1283 1284 1285
        case WTI_CURSORS+10:
        case WTI_CURSORS+11:
        /* CURSORMAX == 12 */
        /* FIXME: dynamic cursor support */
1286 1287 1288
            /* Apps will poll different slots to detect what cursors are available
             * if there isn't a cursor for this slot return 0 */
            if (!gSysCursor[wCategory - WTI_CURSORS].ACTIVE)
1289 1290 1291 1292 1293
                rc = 0;
            else
            {
                tgtcursor = &gSysCursor[wCategory - WTI_CURSORS];
                switch (nIndex)
1294
                {
1295
                    case CSR_NAME:
1296
                        rc = CopyTabletData(lpOutput, tgtcursor->NAME,
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
                                            (strlenW(tgtcursor->NAME)+1) * sizeof(WCHAR));
                        break;
                    case CSR_ACTIVE:
                        rc = CopyTabletData(lpOutput,&tgtcursor->ACTIVE,
                                            sizeof(BOOL));
                        break;
                    case CSR_PKTDATA:
                        rc = CopyTabletData(lpOutput,&tgtcursor->PKTDATA,
                                            sizeof(WTPKT));
                        break;
                    case CSR_BUTTONS:
                        rc = CopyTabletData(lpOutput,&tgtcursor->BUTTONS,
                                            sizeof(BYTE));
                        break;
                    case CSR_BUTTONBITS:
                        rc = CopyTabletData(lpOutput,&tgtcursor->BUTTONBITS,
                                            sizeof(BYTE));
                        break;
                    case CSR_BTNNAMES:
                        FIXME("Button Names not returned correctly\n");
                        rc = CopyTabletData(lpOutput,&tgtcursor->BTNNAMES,
                                            tgtcursor->cchBTNNAMES*sizeof(WCHAR));
                        break;
                    case CSR_BUTTONMAP:
1321
                        rc = CopyTabletData(lpOutput,tgtcursor->BUTTONMAP,
1322 1323 1324
                                            sizeof(BYTE)*32);
                        break;
                    case CSR_SYSBTNMAP:
1325
                        rc = CopyTabletData(lpOutput,tgtcursor->SYSBTNMAP,
1326 1327 1328
                                            sizeof(BYTE)*32);
                        break;
                    case CSR_NPBTNMARKS:
1329
                        rc = CopyTabletData(lpOutput,tgtcursor->NPBTNMARKS,
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
                                            sizeof(UINT)*2);
                        break;
                    case CSR_NPBUTTON:
                        rc = CopyTabletData(lpOutput,&tgtcursor->NPBUTTON,
                                            sizeof(BYTE));
                        break;
                    case CSR_NPRESPONSE:
                        FIXME("Not returning CSR_NPRESPONSE correctly\n");
                        rc = 0;
                        break;
                    case CSR_TPBUTTON:
                        rc = CopyTabletData(lpOutput,&tgtcursor->TPBUTTON,
                                            sizeof(BYTE));
                        break;
                    case CSR_TPBTNMARKS:
1345
                        rc = CopyTabletData(lpOutput,tgtcursor->TPBTNMARKS,
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
                                            sizeof(UINT)*2);
                        break;
                    case CSR_TPRESPONSE:
                        FIXME("Not returning CSR_TPRESPONSE correctly\n");
                        rc = 0;
                        break;
                    case CSR_PHYSID:
                    {
                        DWORD id;
                        id = tgtcursor->PHYSID;
                        rc = CopyTabletData(lpOutput,&id,sizeof(DWORD));
                    }
                        break;
                    case CSR_MODE:
                        rc = CopyTabletData(lpOutput,&tgtcursor->MODE,sizeof(UINT));
                        break;
                    case CSR_MINPKTDATA:
                        rc = CopyTabletData(lpOutput,&tgtcursor->MINPKTDATA,
                            sizeof(UINT));
                        break;
                    case CSR_MINBUTTONS:
                        rc = CopyTabletData(lpOutput,&tgtcursor->MINBUTTONS,
                            sizeof(UINT));
                        break;
                    case CSR_CAPABILITIES:
                        rc = CopyTabletData(lpOutput,&tgtcursor->CAPABILITIES,
                            sizeof(UINT));
                        break;
                    case CSR_TYPE:
                        rc = CopyTabletData(lpOutput,&tgtcursor->TYPE,
                            sizeof(UINT));
                        break;
                    default:
                        FIXME("WTI_CURSORS unhandled index %i\n",nIndex);
                        rc = 0;
1381 1382 1383 1384 1385 1386 1387 1388
                }
            }
            break;
        case WTI_DEVICES:
            switch (nIndex)
            {
                case DVC_NAME:
                    rc = CopyTabletData(lpOutput,gSysDevice.NAME,
1389
                                        (strlenW(gSysDevice.NAME)+1) * sizeof(WCHAR));
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
                    break;
                case DVC_HARDWARE:
                    rc = CopyTabletData(lpOutput,&gSysDevice.HARDWARE,
                                        sizeof(UINT));
                    break;
                case DVC_NCSRTYPES:
                    rc = CopyTabletData(lpOutput,&gSysDevice.NCSRTYPES,
                                        sizeof(UINT));
                    break;
                case DVC_FIRSTCSR:
                    rc = CopyTabletData(lpOutput,&gSysDevice.FIRSTCSR,
                                        sizeof(UINT));
                    break;
                case DVC_PKTRATE:
                    rc = CopyTabletData(lpOutput,&gSysDevice.PKTRATE,
                                        sizeof(UINT));
                    break;
                case DVC_PKTDATA:
                    rc = CopyTabletData(lpOutput,&gSysDevice.PKTDATA,
                                        sizeof(WTPKT));
                    break;
                case DVC_PKTMODE:
                    rc = CopyTabletData(lpOutput,&gSysDevice.PKTMODE,
                                        sizeof(WTPKT));
                    break;
                case DVC_CSRDATA:
                    rc = CopyTabletData(lpOutput,&gSysDevice.CSRDATA,
                                        sizeof(WTPKT));
                    break;
                case DVC_XMARGIN:
                    rc = CopyTabletData(lpOutput,&gSysDevice.XMARGIN,
                                        sizeof(INT));
                    break;
                case DVC_YMARGIN:
                    rc = CopyTabletData(lpOutput,&gSysDevice.YMARGIN,
                                        sizeof(INT));
                    break;
                case DVC_ZMARGIN:
                    rc = 0; /* unsupported */
                    /*
                    rc = CopyTabletData(lpOutput,&gSysDevice.ZMARGIN,
                                        sizeof(INT));
                    */
                    break;
                case DVC_X:
                    rc = CopyTabletData(lpOutput,&gSysDevice.X,
                                        sizeof(AXIS));
                    break;
                case DVC_Y:
                    rc = CopyTabletData(lpOutput,&gSysDevice.Y,
                                        sizeof(AXIS));
                    break;
                case DVC_Z:
                    rc = 0; /* unsupported */
                    /*
                    rc = CopyTabletData(lpOutput,&gSysDevice.Z,
                                        sizeof(AXIS));
                    */
                    break;
                case DVC_NPRESSURE:
                    rc = CopyTabletData(lpOutput,&gSysDevice.NPRESSURE,
                                        sizeof(AXIS));
                    break;
                case DVC_TPRESSURE:
                    rc = 0; /* unsupported */
                    /*
                    rc = CopyTabletData(lpOutput,&gSysDevice.TPRESSURE,
                                        sizeof(AXIS));
                    */
                    break;
                case DVC_ORIENTATION:
1461
                    rc = CopyTabletData(lpOutput,gSysDevice.ORIENTATION,
1462
                                        sizeof(AXIS)*3);
1463 1464 1465 1466
                    break;
                case DVC_ROTATION:
                    rc = 0; /* unsupported */
                    /*
1467 1468
                    rc = CopyTabletData(lpOutput,&gSysDevice.ROTATION,
                                        sizeof(AXIS)*3);
1469 1470 1471 1472
                    */
                    break;
                case DVC_PNPID:
                    rc = CopyTabletData(lpOutput,gSysDevice.PNPID,
1473
                                        (strlenW(gSysDevice.PNPID)+1)*sizeof(WCHAR));
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
                    break;
                default:
                    FIXME("WTI_DEVICES unhandled index %i\n",nIndex);
                    rc = 0;
            }
            break;
        default:
            FIXME("Unhandled Category %i\n",wCategory);
    }
    return rc;
}

1486
#else /* SONAME_LIBXI */
1487

1488 1489 1490
/***********************************************************************
 *		AttachEventQueueToTablet (X11DRV.@)
 */
1491 1492 1493 1494 1495
int X11DRV_AttachEventQueueToTablet(HWND hOwner)
{
    return 0;
}

1496 1497 1498
/***********************************************************************
 *		GetCurrentPacket (X11DRV.@)
 */
1499
int X11DRV_GetCurrentPacket(LPWTPACKET packet)
1500 1501 1502 1503
{
    return 0;
}

1504 1505 1506
/***********************************************************************
 *		LoadTabletInfo (X11DRV.@)
 */
1507 1508 1509 1510
void X11DRV_LoadTabletInfo(HWND hwnddefault)
{
}

1511
/***********************************************************************
1512
 *		WTInfoW (X11DRV.@)
1513
 */
1514
UINT X11DRV_WTInfoW(UINT wCategory, UINT nIndex, LPVOID lpOutput)
1515 1516 1517 1518
{
    return 0;
}

1519
#endif /* SONAME_LIBXI */