context.c 32.1 KB
Newer Older
1 2 3 4
/*
 * Tablet Context
 *
 * Copyright 2002 Patrik Stridvall
5
 * Copyright 2003 CodeWeavers, Aric Stewart
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22
 */

#include "config.h"
23 24
#include <stdio.h>
#include <stdlib.h>
25 26
#include <stdarg.h>

27 28
#include "windef.h"
#include "winerror.h"
29 30
#include "winbase.h"
#include "winuser.h"
31
#include "winnls.h"
32 33

#include "wintab.h"
34
#include "wintab_internal.h"
35 36 37 38 39

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(wintab32);

40 41 42 43 44 45 46 47
/*
 * Documentation found at
 * http://www.csl.sony.co.jp/projects/ar/restricted/wintabl.html
 */

static LPOPENCONTEXT gOpenContexts;
static HCTX gTopContext = (HCTX)0xc00;

48 49 50 51 52 53 54 55
static void LOGCONTEXTAtoW(const LOGCONTEXTA *in, LOGCONTEXTW *out)
{
    MultiByteToWideChar(CP_ACP, 0, in->lcName, -1, out->lcName, LCNAMELEN);
    out->lcName[LCNAMELEN - 1] = 0;
    /* we use the fact that the fields after lcName are the same in LOGCONTEXTA and W */
    memcpy(&out->lcOptions, &in->lcOptions, sizeof(LOGCONTEXTA) - FIELD_OFFSET(LOGCONTEXTA, lcOptions));
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
static void LOGCONTEXTWtoA(const LOGCONTEXTW *in, LOGCONTEXTA *out)
{
    WideCharToMultiByte(CP_ACP, 0, in->lcName, LCNAMELEN, out->lcName, LCNAMELEN, NULL, NULL);
    out->lcName[LCNAMELEN - 1] = 0;
    /* we use the fact that the fields after lcName are the same in LOGCONTEXTA and W */
    memcpy(&out->lcOptions, &in->lcOptions, sizeof(LOGCONTEXTW) - FIELD_OFFSET(LOGCONTEXTW, lcOptions));
}

static BOOL is_logcontext_category(UINT wCategory)
{
    return wCategory == WTI_DEFSYSCTX || wCategory == WTI_DEFCONTEXT || wCategory == WTI_DDCTXS;
}

static BOOL is_string_field(UINT wCategory, UINT nIndex)
{
    if (wCategory == WTI_INTERFACE && nIndex == IFC_WINTABID)
        return TRUE;
    if (is_logcontext_category(wCategory) && nIndex == CTX_NAME)
        return TRUE;
75 76
    if ((wCategory >= WTI_CURSORS && wCategory <= WTI_CURSORS + 9) &&
            (nIndex == CSR_NAME || nIndex == CSR_BTNNAMES))
77 78 79 80 81 82
        return TRUE;
    if (wCategory == WTI_DEVICES && (nIndex == DVC_NAME || nIndex == DVC_PNPID))
        return TRUE;
    return FALSE;
}

83
static const char* DUMPBITS(int x)
84
{
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    char buf[200];
    buf[0] = 0;
    if (x&PK_CONTEXT) strcat(buf,"PK_CONTEXT ");
    if (x&PK_STATUS) strcat(buf, "PK_STATUS ");
    if (x&PK_TIME) strcat(buf, "PK_TIME ");
    if (x&PK_CHANGED) strcat(buf, "PK_CHANGED ");
    if (x&PK_SERIAL_NUMBER) strcat(buf, "PK_SERIAL_NUMBER ");
    if (x&PK_CURSOR) strcat(buf, "PK_CURSOR ");
    if (x&PK_BUTTONS) strcat(buf, "PK_BUTTONS ");
    if (x&PK_X) strcat(buf, "PK_X ");
    if (x&PK_Y) strcat(buf, "PK_Y ");
    if (x&PK_Z) strcat(buf, "PK_Z ");
    if (x&PK_NORMAL_PRESSURE) strcat(buf, "PK_NORMAL_PRESSURE ");
    if (x&PK_TANGENT_PRESSURE) strcat(buf, "PK_TANGENT_PRESSURE ");
    if (x&PK_ORIENTATION) strcat(buf, "PK_ORIENTATION ");
    if (x&PK_ROTATION) strcat(buf, "PK_ROTATION ");
    return wine_dbg_sprintf("{%s}",buf);
102 103 104 105
}

static inline void DUMPPACKET(WTPACKET packet)
{
106
    TRACE("pkContext: %p pkStatus: 0x%x pkTime : 0x%x pkChanged: 0x%x pkSerialNumber: 0x%x pkCursor : %i pkButtons: %x pkX: %i pkY: %i pkZ: %i pkNormalPressure: %i pkTangentPressure: %i pkOrientation: (%i,%i,%i) pkRotation: (%i,%i,%i)\n",
107 108 109 110 111
          packet.pkContext, packet.pkStatus, packet.pkTime, packet.pkChanged, packet.pkSerialNumber,
          packet.pkCursor, packet.pkButtons, packet.pkX, packet.pkY, packet.pkZ,
          packet.pkNormalPressure, packet.pkTangentPressure,
          packet.pkOrientation.orAzimuth, packet.pkOrientation.orAltitude, packet.pkOrientation.orTwist,
          packet.pkRotation.roPitch, packet.pkRotation.roRoll, packet.pkRotation.roYaw);
112 113
}

114
static inline void DUMPCONTEXT(LOGCONTEXTW lc)
115
{
116 117 118 119 120 121 122 123 124 125 126 127 128
    TRACE("Name: %s, Options: %x, Status: %x, Locks: %x, MsgBase: %x, "
          "Device: %x, PktRate: %x, "
          "%x%s, %x%s, %x%s, "
          "BtnDnMask: %x, BtnUpMask: %x, "
          "InOrgX: %i, InOrgY: %i, InOrgZ: %i, "
          "InExtX: %i, InExtY: %i, InExtZ: %i, "
          "OutOrgX: %i, OutOrgY: %i, OutOrgZ: %i, "
          "OutExtX: %i, OutExtY: %i, OutExtZ: %i, "
          "SensX: %i, SensY: %i, SensZ: %i, "
          "SysMode: %i, "
          "SysOrgX: %i, SysOrgY: %i, "
          "SysExtX: %i, SysExtY: %i, "
          "SysSensX: %i, SysSensY: %i\n",
129 130 131 132 133 134 135 136 137
          wine_dbgstr_w(lc.lcName), lc.lcOptions, lc.lcStatus, lc.lcLocks, lc.lcMsgBase,
          lc.lcDevice, lc.lcPktRate, lc.lcPktData, DUMPBITS(lc.lcPktData),
          lc.lcPktMode, DUMPBITS(lc.lcPktMode), lc.lcMoveMask,
          DUMPBITS(lc.lcMoveMask), lc.lcBtnDnMask, lc.lcBtnUpMask,
          lc.lcInOrgX, lc.lcInOrgY, lc.lcInOrgZ, lc.lcInExtX, lc.lcInExtY,
          lc.lcInExtZ, lc.lcOutOrgX, lc.lcOutOrgY, lc.lcOutOrgZ, lc.lcOutExtX,
          lc.lcOutExtY, lc.lcOutExtZ, lc.lcSensX, lc.lcSensY, lc.lcSensZ, lc.lcSysMode,
          lc.lcSysOrgX, lc.lcSysOrgY, lc.lcSysExtX, lc.lcSysExtY, lc.lcSysSensX,
          lc.lcSysSensY);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
}


/* Find an open context given the handle */
static LPOPENCONTEXT TABLET_FindOpenContext(HCTX hCtx)
{
    LPOPENCONTEXT ptr = gOpenContexts;
    while (ptr)
    {
        if (ptr->handle == hCtx) return ptr;
        ptr = ptr->next;
    }
    return NULL;
}

153
static inline BOOL LoadTablet(void)
154
{
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    static enum {TI_START = 0, TI_OK, TI_FAIL} loaded = TI_START;

    if (loaded == TI_START)
    {
        TRACE("Initializing the tablet to hwnd %p\n",hwndDefault);

        if (pLoadTabletInfo && pLoadTabletInfo(hwndDefault))
        {
            loaded = TI_OK;
        }
        else
        {
            loaded = TI_FAIL;
            ERR("LoadTabletInfo(%p) failed\n", hwndDefault);
        }
    }

    return loaded == TI_OK;
173 174 175 176 177 178 179
}

int TABLET_PostTabletMessage(LPOPENCONTEXT newcontext, UINT msg, WPARAM wParam,
                             LPARAM lParam, BOOL send_always)
{
    if ((send_always) || (newcontext->context.lcOptions & CXO_MESSAGES))
    {
180
        TRACE("Posting message %x to %p\n",msg, newcontext->hwndOwner);
181 182 183 184 185
        return PostMessageA(newcontext->hwndOwner, msg, wParam, lParam);
    }
    return 0;
}

186
static inline DWORD ScaleForContext(DWORD In, LONG InOrg, LONG InExt, LONG OutOrg, LONG OutExt)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
{
    if (((InExt > 0 )&&(OutExt > 0)) || ((InExt<0) && (OutExt < 0)))
        return ((In - InOrg) * abs(OutExt) / abs(InExt)) + OutOrg;
    else
        return ((abs(InExt) - (In - InOrg))*abs(OutExt) / abs(InExt)) + OutOrg;
}

LPOPENCONTEXT AddPacketToContextQueue(LPWTPACKET packet, HWND hwnd)
{
    LPOPENCONTEXT ptr=NULL;

    EnterCriticalSection(&csTablet);

    ptr = gOpenContexts;
    while (ptr)
    {
        TRACE("Trying Queue %p (%p %p)\n", ptr->handle, hwnd, ptr->hwndOwner);

        if (ptr->hwndOwner == hwnd)
        {
            int tgt;
            if (!ptr->enabled)
            {
                ptr = ptr->next;
                continue;
            }

            tgt = ptr->PacketsQueued;

            packet->pkContext = ptr->handle;

            /* translate packet data to the context */

            /* Scale  as per documentation */
            packet->pkY = ScaleForContext(packet->pkY, ptr->context.lcInOrgY,
                                ptr->context.lcInExtY, ptr->context.lcOutOrgY,
                                ptr->context.lcOutExtY);

            packet->pkX = ScaleForContext(packet->pkX, ptr->context.lcInOrgX,
                                ptr->context.lcInExtX, ptr->context.lcOutOrgX,
                                ptr->context.lcOutExtX);

            /* flip the Y axis */
            if (ptr->context.lcOutExtY > 0)
                packet->pkY = ptr->context.lcOutExtY - packet->pkY;
232 233
            else if (ptr->context.lcOutExtY < 0)
                packet->pkY = abs(ptr->context.lcOutExtY + packet->pkY);
234 235 236

            DUMPPACKET(*packet);

237
            if (tgt == ptr->QueueSize)
238 239
            {
                TRACE("Queue Overflow %p\n",ptr->handle);
240
                ptr->PacketQueue[tgt-1].pkStatus |= TPS_QUEUE_ERR;
241 242 243 244
            }
            else
            {
                TRACE("Placed in queue %p index %i\n",ptr->handle,tgt);
245
                ptr->PacketQueue[tgt] = *packet;
246 247 248 249 250 251
                ptr->PacketsQueued++;

                if (ptr->ActiveCursor != packet->pkCursor)
                {
                    ptr->ActiveCursor = packet->pkCursor;
                    if (ptr->context.lcOptions & CXO_CSRMESSAGES)
252
                        TABLET_PostTabletMessage(ptr, _WT_CSRCHANGE(ptr->context.lcMsgBase),
253 254 255 256 257 258 259 260 261 262 263 264 265
                          (WPARAM)packet->pkSerialNumber, (LPARAM)ptr->handle,
                            FALSE);
                }
            }
            break;
         }
        ptr = ptr->next;
    }
    LeaveCriticalSection(&csTablet);
    TRACE("Done (%p)\n",ptr);
    return ptr;
}

266 267 268
/*
 * Flushes all packets from the queue.
 */
269
static inline void TABLET_FlushQueue(LPOPENCONTEXT context)
270 271 272 273
{
    context->PacketsQueued = 0;
}

274
static inline int CopyTabletData(LPVOID target, LPVOID src, INT size)
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 304
{
    memcpy(target,src,size);
    return(size);
}

static INT TABLET_FindPacket(LPOPENCONTEXT context, UINT wSerial,
                                LPWTPACKET *pkt)
{
    int loop;
    int index  = -1;
    for (loop = 0; loop < context->PacketsQueued; loop++)
        if (context->PacketQueue[loop].pkSerialNumber == wSerial)
        {
            index = loop;
            *pkt = &context->PacketQueue[loop];
            break;
        }

    TRACE("%i .. %i\n",context->PacketsQueued,index);

    return index;
}


static LPVOID TABLET_CopyPacketData(LPOPENCONTEXT context, LPVOID lpPkt,
                                    LPWTPACKET wtp)
{
    LPBYTE ptr;

    ptr = lpPkt;
305
    TRACE("Packet Bits %s\n",DUMPBITS(context->context.lcPktData));
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377

    if (context->context.lcPktData & PK_CONTEXT)
        ptr+=CopyTabletData(ptr,&wtp->pkContext,sizeof(HCTX));
    if (context->context.lcPktData & PK_STATUS)
        ptr+=CopyTabletData(ptr,&wtp->pkStatus,sizeof(UINT));
    if (context->context.lcPktData & PK_TIME)
        ptr+=CopyTabletData(ptr,&wtp->pkTime,sizeof(LONG));
    if (context->context.lcPktData & PK_CHANGED)
        ptr+=CopyTabletData(ptr,&wtp->pkChanged,sizeof(WTPKT));
    if (context->context.lcPktData & PK_SERIAL_NUMBER)
        ptr+=CopyTabletData(ptr,&wtp->pkChanged,sizeof(UINT));
    if (context->context.lcPktData & PK_CURSOR)
        ptr+=CopyTabletData(ptr,&wtp->pkCursor,sizeof(UINT));
    if (context->context.lcPktData & PK_BUTTONS)
        ptr+=CopyTabletData(ptr,&wtp->pkButtons,sizeof(DWORD));
    if (context->context.lcPktData & PK_X)
        ptr+=CopyTabletData(ptr,&wtp->pkX,sizeof(DWORD));
    if (context->context.lcPktData & PK_Y)
        ptr+=CopyTabletData(ptr,&wtp->pkY,sizeof(DWORD));
    if (context->context.lcPktData & PK_Z)
        ptr+=CopyTabletData(ptr,&wtp->pkZ,sizeof(DWORD));
    if (context->context.lcPktData & PK_NORMAL_PRESSURE)
        ptr+=CopyTabletData(ptr,&wtp->pkNormalPressure,sizeof(UINT));
    if (context->context.lcPktData & PK_TANGENT_PRESSURE)
        ptr+=CopyTabletData(ptr,&wtp->pkTangentPressure,sizeof(UINT));
    if (context->context.lcPktData & PK_ORIENTATION)
        ptr+=CopyTabletData(ptr,&wtp->pkOrientation,sizeof(ORIENTATION));
    if (context->context.lcPktData & PK_ROTATION)
        ptr+=CopyTabletData(ptr,&wtp->pkRotation,sizeof(ROTATION));

    /*TRACE("Copied %i bytes\n",(INT)ptr - (INT)lpPkt); */
    return ptr;
}

static VOID TABLET_BlankPacketData(LPOPENCONTEXT context, LPVOID lpPkt, INT n)
{
    int rc = 0;

    if (context->context.lcPktData & PK_CONTEXT)
        rc +=sizeof(HCTX);
    if (context->context.lcPktData & PK_STATUS)
        rc +=sizeof(UINT);
    if (context->context.lcPktData & PK_TIME)
        rc += sizeof(LONG);
    if (context->context.lcPktData & PK_CHANGED)
        rc += sizeof(WTPKT);
    if (context->context.lcPktData & PK_SERIAL_NUMBER)
        rc += sizeof(UINT);
    if (context->context.lcPktData & PK_CURSOR)
        rc += sizeof(UINT);
    if (context->context.lcPktData & PK_BUTTONS)
        rc += sizeof(DWORD);
    if (context->context.lcPktData & PK_X)
        rc += sizeof(DWORD);
    if (context->context.lcPktData & PK_Y)
        rc += sizeof(DWORD);
    if (context->context.lcPktData & PK_Z)
        rc += sizeof(DWORD);
    if (context->context.lcPktData & PK_NORMAL_PRESSURE)
        rc += sizeof(UINT);
    if (context->context.lcPktData & PK_TANGENT_PRESSURE)
        rc += sizeof(UINT);
    if (context->context.lcPktData & PK_ORIENTATION)
        rc += sizeof(ORIENTATION);
    if (context->context.lcPktData & PK_ROTATION)
        rc += sizeof(ROTATION);

    rc *= n;
    memset(lpPkt,0,rc);
}


378
static UINT WTInfoT(UINT wCategory, UINT nIndex, LPVOID lpOutput, BOOL bUnicode)
379
{
380
    UINT result;
381

382 383
    if (!LoadTablet()) return 0;

384
    TRACE("(%d, %d, %p, %d)\n", wCategory, nIndex, lpOutput, bUnicode);
385

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    /*
     *  Handle system extents here, as we can use user32.dll code to set them.
     */
    if(wCategory == WTI_DEFSYSCTX)
    {
        switch(nIndex)
        {
            case CTX_SYSEXTX:
                if(lpOutput != NULL)
                    *(LONG*)lpOutput = GetSystemMetrics(SM_CXSCREEN);
                return sizeof(LONG);
            case CTX_SYSEXTY:
                if(lpOutput != NULL)
                    *(LONG*)lpOutput = GetSystemMetrics(SM_CYSCREEN);
                return sizeof(LONG);
           /* No action, delegate to X11Drv */
        }
    }

405 406 407 408 409 410
    if (is_logcontext_category(wCategory) && nIndex == 0)
    {
        if (lpOutput)
        {
            LOGCONTEXTW buf;
            pWTInfoW(wCategory, nIndex, &buf);
411

412 413 414 415 416 417 418 419 420 421 422 423 424
            /*  Handle system extents here, as we can use user32.dll code to set them */
            if(wCategory == WTI_DEFSYSCTX && nIndex == 0)
            {
                buf.lcSysExtX = GetSystemMetrics(SM_CXSCREEN);
                buf.lcSysExtY = GetSystemMetrics(SM_CYSCREEN);
            }

            if (bUnicode)
                memcpy(lpOutput, &buf, sizeof(buf));
            else
                LOGCONTEXTWtoA(&buf, lpOutput);
        }

425
        result = bUnicode ? sizeof(LOGCONTEXTW) : sizeof(LOGCONTEXTA);
426 427
    }
    else if (is_string_field(wCategory, nIndex) && !bUnicode)
428
    {
429 430 431 432 433
        int size = pWTInfoW(wCategory, nIndex, NULL);
        WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, size);
        pWTInfoW(wCategory, nIndex, buf);
        result = WideCharToMultiByte(CP_ACP, 0, buf, size/sizeof(WCHAR), lpOutput, lpOutput ? 2*size : 0, NULL, NULL);
        HeapFree(GetProcessHeap(), 0, buf);
434
    }
435 436 437
    else
        result =  pWTInfoW(wCategory, nIndex, lpOutput);

438
    TRACE("returns %d\n", result);
439
    return result;
440 441 442
}

/***********************************************************************
443
 *		WTInfoA (WINTAB32.20)
444
 */
445
UINT WINAPI WTInfoA(UINT wCategory, UINT nIndex, LPVOID lpOutput)
446
{
447 448
    return WTInfoT(wCategory, nIndex, lpOutput, FALSE);
}
449 450


451 452 453 454 455 456
/***********************************************************************
 *		WTInfoW (WINTAB32.1020)
 */
UINT WINAPI WTInfoW(UINT wCategory, UINT nIndex, LPVOID lpOutput)
{
    return WTInfoT(wCategory, nIndex, lpOutput, TRUE);
457 458 459
}

/***********************************************************************
460
 *		WTOpenW (WINTAB32.2021)
461
 */
462
HCTX WINAPI WTOpenW(HWND hWnd, LPLOGCONTEXTW lpLogCtx, BOOL fEnable)
463
{
464
    LPOPENCONTEXT newcontext;
465

466 467
    if (!LoadTablet()) return 0;

468
    TRACE("hWnd=%p, lpLogCtx=%p, fEnable=%u\n", hWnd, lpLogCtx, fEnable);
469
    DUMPCONTEXT(*lpLogCtx);
470

471
    newcontext = HeapAlloc(GetProcessHeap(), 0 , sizeof(OPENCONTEXT));
472
    newcontext->context = *lpLogCtx;
473 474 475 476 477 478 479 480 481 482 483 484 485 486
    newcontext->hwndOwner = hWnd;
    newcontext->ActiveCursor = -1;
    newcontext->QueueSize = 10;
    newcontext->PacketsQueued = 0;
    newcontext->PacketQueue=HeapAlloc(GetProcessHeap(),0,sizeof(WTPACKET)*10);

    EnterCriticalSection(&csTablet);
    newcontext->handle = gTopContext++;
    newcontext->next = gOpenContexts;
    gOpenContexts = newcontext;
    LeaveCriticalSection(&csTablet);

    pAttachEventQueueToTablet(hWnd);

487
    TABLET_PostTabletMessage(newcontext, _WT_CTXOPEN(newcontext->context.lcMsgBase), (WPARAM)newcontext->handle,
488 489
                      newcontext->context.lcStatus, TRUE);

490 491 492 493 494 495 496 497 498 499 500
    if (fEnable)
    {
        newcontext->enabled = TRUE;
        /* TODO: Add to top of overlap order */
        newcontext->context.lcStatus = CXS_ONTOP;
    }
    else
    {
        newcontext->enabled = FALSE;
        newcontext->context.lcStatus = CXS_DISABLED;
    }
501

502
    TABLET_PostTabletMessage(newcontext, _WT_CTXOVERLAP(newcontext->context.lcMsgBase),
503 504 505 506
                            (WPARAM)newcontext->handle,
                            newcontext->context.lcStatus, TRUE);

    return newcontext->handle;
507 508 509
}

/***********************************************************************
510
 *		WTOpenA (WINTAB32.21)
511
 */
512
HCTX WINAPI WTOpenA(HWND hWnd, LPLOGCONTEXTA lpLogCtx, BOOL fEnable)
513
{
514
    LOGCONTEXTW logCtxW;
515

516 517
    LOGCONTEXTAtoW(lpLogCtx, &logCtxW);
    return WTOpenW(hWnd, &logCtxW, fEnable);
518 519 520 521 522 523 524
}

/***********************************************************************
 *		WTClose (WINTAB32.22)
 */
BOOL WINAPI WTClose(HCTX hCtx)
{
525
    LPOPENCONTEXT context,ptr;
526

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    TRACE("(%p)\n", hCtx);

    EnterCriticalSection(&csTablet);

    ptr = context = gOpenContexts;

    while (context && (context->handle != hCtx))
    {
        ptr = context;
        context = context->next;
    }
    if (!context)
    {
        LeaveCriticalSection(&csTablet);
        return TRUE;
    }

    if (context == gOpenContexts)
        gOpenContexts = context->next;
    else
        ptr->next = context->next;

    LeaveCriticalSection(&csTablet);

551
    TABLET_PostTabletMessage(context, _WT_CTXCLOSE(context->context.lcMsgBase), (WPARAM)context->handle,
552 553 554 555
                      context->context.lcStatus,TRUE);

    HeapFree(GetProcessHeap(),0,context->PacketQueue);
    HeapFree(GetProcessHeap(),0,context);
556 557 558 559 560 561 562 563 564

    return TRUE;
}

/***********************************************************************
 *		WTPacketsGet (WINTAB32.23)
 */
int WINAPI WTPacketsGet(HCTX hCtx, int cMaxPkts, LPVOID lpPkts)
{
565 566 567
    int limit;
    LPOPENCONTEXT context;
    LPVOID ptr = lpPkts;
568

569
    TRACE("(%p, %d, %p)\n", hCtx, cMaxPkts, lpPkts);
570

571 572
    if (!hCtx)
        return 0;
573 574 575 576

    EnterCriticalSection(&csTablet);

    context = TABLET_FindOpenContext(hCtx);
577 578 579 580 581
    if (!context)
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }
582 583 584

    if (lpPkts != NULL)
        TABLET_BlankPacketData(context,lpPkts,cMaxPkts);
585 586 587 588 589 590 591

    if (context->PacketsQueued == 0)
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }

592 593 594 595 596 597 598 599 600
    limit = min(cMaxPkts,context->PacketsQueued);

    if(ptr != NULL)
    {
        int i = 0;
        for(i = 0; i < limit; i++)
            ptr=TABLET_CopyPacketData(context ,ptr, &context->PacketQueue[i]);
    }

601 602 603

    if (limit < context->PacketsQueued)
    {
604 605
        memmove(context->PacketQueue, &context->PacketQueue[limit],
            (context->PacketsQueued - (limit))*sizeof(WTPACKET));
606 607 608 609 610 611 612
    }
    context->PacketsQueued -= limit;
    LeaveCriticalSection(&csTablet);

    TRACE("Copied %i packets\n",limit);

    return limit;
613 614 615 616 617 618 619
}

/***********************************************************************
 *		WTPacket (WINTAB32.24)
 */
BOOL WINAPI WTPacket(HCTX hCtx, UINT wSerial, LPVOID lpPkt)
{
620 621
    int rc = 0;
    LPOPENCONTEXT context;
622
    LPWTPACKET wtp = NULL;
623

624
    TRACE("(%p, %d, %p)\n", hCtx, wSerial, lpPkt);
625

626 627
    if (!hCtx)
        return 0;
628 629 630 631

    EnterCriticalSection(&csTablet);

    context = TABLET_FindOpenContext(hCtx);
632 633 634 635 636
    if (!context)
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }
637 638 639 640 641 642 643 644 645 646

    rc = TABLET_FindPacket(context ,wSerial, &wtp);

    if (rc >= 0)
    {
        if (lpPkt)
           TABLET_CopyPacketData(context ,lpPkt, wtp);

        if ((rc+1) < context->QueueSize)
        {
647 648
            memmove(context->PacketQueue, &context->PacketQueue[rc+1],
                (context->PacketsQueued - (rc+1))*sizeof(WTPACKET));
649 650 651 652 653 654 655
        }
        context->PacketsQueued -= (rc+1);
    }
    LeaveCriticalSection(&csTablet);

    TRACE("Returning %i\n",rc+1);
    return rc+1;
656 657 658 659 660 661 662
}

/***********************************************************************
 *		WTEnable (WINTAB32.40)
 */
BOOL WINAPI WTEnable(HCTX hCtx, BOOL fEnable)
{
663
    LPOPENCONTEXT context;
664

665
    TRACE("hCtx=%p, fEnable=%u\n", hCtx, fEnable);
666

667
    if (!hCtx) return FALSE;
668 669 670

    EnterCriticalSection(&csTablet);
    context = TABLET_FindOpenContext(hCtx);
671 672 673 674 675 676
    if (!context)
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }

677 678 679 680 681 682
    /* if we want to enable and it is not enabled then */
    if(fEnable && !context->enabled)
    {
        context->enabled = TRUE;
        /* TODO: Add to top of overlap order */
        context->context.lcStatus = CXS_ONTOP;
683 684 685 686
        TABLET_PostTabletMessage(context,
            _WT_CTXOVERLAP(context->context.lcMsgBase),
            (WPARAM)context->handle,
            context->context.lcStatus, TRUE);
687 688 689 690 691 692 693
    }
    /* if we want to disable and it is not disabled then */
    else if (!fEnable && context->enabled)
    {
        context->enabled = FALSE;
        /* TODO: Remove from overlap order?? needs a test */
        context->context.lcStatus = CXS_DISABLED;
694
        TABLET_FlushQueue(context);
695 696 697 698
        TABLET_PostTabletMessage(context,
            _WT_CTXOVERLAP(context->context.lcMsgBase),
            (WPARAM)context->handle,
            context->context.lcStatus, TRUE);
699
    }
700 701 702
    LeaveCriticalSection(&csTablet);

    return TRUE;
703 704 705 706
}

/***********************************************************************
 *		WTOverlap (WINTAB32.41)
707 708
 *
 *		Move context to top or bottom of overlap order
709 710 711
 */
BOOL WINAPI WTOverlap(HCTX hCtx, BOOL fToTop)
{
712 713 714 715 716 717 718 719
    LPOPENCONTEXT context;

    TRACE("hCtx=%p, fToTop=%u\n", hCtx, fToTop);

    if (!hCtx) return FALSE;

    EnterCriticalSection(&csTablet);
    context = TABLET_FindOpenContext(hCtx);
720 721 722 723 724 725
    if (!context)
    {
        LeaveCriticalSection(&csTablet);
        return FALSE;
    }

726 727
    /* if we want to send to top and it's not already there */
    if (fToTop && context->context.lcStatus != CXS_ONTOP)
728 729 730 731
    {
        /* TODO: Move context to top of overlap order */
        FIXME("Not moving context to top of overlap order\n");
        context->context.lcStatus = CXS_ONTOP;
732 733 734 735
        TABLET_PostTabletMessage(context,
            _WT_CTXOVERLAP(context->context.lcMsgBase),
            (WPARAM)context->handle,
            context->context.lcStatus, TRUE);
736
    }
737
    else if (!fToTop)
738 739 740 741
    {
        /* TODO: Move context to bottom of overlap order */
        FIXME("Not moving context to bottom of overlap order\n");
        context->context.lcStatus = CXS_OBSCURED;
742 743 744 745
        TABLET_PostTabletMessage(context,
            _WT_CTXOVERLAP(context->context.lcMsgBase),
            (WPARAM)context->handle,
            context->context.lcStatus, TRUE);
746 747
    }
    LeaveCriticalSection(&csTablet);
748

749
    return TRUE;
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
}

/***********************************************************************
 *		WTConfig (WINTAB32.61)
 */
BOOL WINAPI WTConfig(HCTX hCtx, HWND hWnd)
{
    FIXME("(%p, %p): stub\n", hCtx, hWnd);

    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);

    return FALSE;
}

/***********************************************************************
 *		WTGetA (WINTAB32.61)
 */
BOOL WINAPI WTGetA(HCTX hCtx, LPLOGCONTEXTA lpLogCtx)
{
769
    LPOPENCONTEXT context;
770

771
    TRACE("(%p, %p)\n", hCtx, lpLogCtx);
772

773 774 775 776
    if (!hCtx) return 0;

    EnterCriticalSection(&csTablet);
    context = TABLET_FindOpenContext(hCtx);
777 778 779 780 781 782
    if (!context)
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }

783
    LOGCONTEXTWtoA(&context->context, lpLogCtx);
784 785 786
    LeaveCriticalSection(&csTablet);

    return TRUE;
787 788 789 790 791 792 793
}

/***********************************************************************
 *		WTGetW (WINTAB32.1061)
 */
BOOL WINAPI WTGetW(HCTX hCtx, LPLOGCONTEXTW lpLogCtx)
{
794
    LPOPENCONTEXT context;
795

796
    TRACE("(%p, %p)\n", hCtx, lpLogCtx);
797

798 799 800 801
    if (!hCtx) return 0;

    EnterCriticalSection(&csTablet);
    context = TABLET_FindOpenContext(hCtx);
802 803 804 805 806 807
    if (!context)
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }

808 809 810 811
    memmove(lpLogCtx,&context->context,sizeof(LOGCONTEXTW));
    LeaveCriticalSection(&csTablet);

    return TRUE;
812 813 814 815 816 817 818
}

/***********************************************************************
 *		WTSetA (WINTAB32.62)
 */
BOOL WINAPI WTSetA(HCTX hCtx, LPLOGCONTEXTA lpLogCtx)
{
819
    LPOPENCONTEXT context;
820

821
    TRACE("hCtx=%p, lpLogCtx=%p\n", hCtx, lpLogCtx);
822

823 824 825 826 827 828 829
    if (!hCtx || !lpLogCtx) return FALSE;

    /* TODO: if cur process not owner of hCtx only modify
     * attribs not locked by owner */

    EnterCriticalSection(&csTablet);
    context = TABLET_FindOpenContext(hCtx);
830 831 832 833 834 835
    if (!context)
    {
        LeaveCriticalSection(&csTablet);
        return FALSE;
    }

836 837 838 839
    LOGCONTEXTAtoW(lpLogCtx, &context->context);
    LeaveCriticalSection(&csTablet);

    return TRUE;
840 841 842 843 844 845 846
}

/***********************************************************************
 *		WTSetW (WINTAB32.1062)
 */
BOOL WINAPI WTSetW(HCTX hCtx, LPLOGCONTEXTW lpLogCtx)
{
847
    LPOPENCONTEXT context;
848

849
    TRACE("hCtx=%p, lpLogCtx=%p\n", hCtx, lpLogCtx);
850

851 852 853 854 855 856 857
    if (!hCtx || !lpLogCtx) return FALSE;

    /* TODO: if cur process not hCtx owner only modify
     * attribs not locked by owner */

    EnterCriticalSection(&csTablet);
    context = TABLET_FindOpenContext(hCtx);
858 859 860 861 862 863
    if (!context)
    {
        LeaveCriticalSection(&csTablet);
        return FALSE;
    }

864 865 866 867
    memmove(&context->context, lpLogCtx, sizeof(LOGCONTEXTW));
    LeaveCriticalSection(&csTablet);

    return TRUE;
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
}

/***********************************************************************
 *		WTExtGet (WINTAB32.63)
 */
BOOL WINAPI WTExtGet(HCTX hCtx, UINT wExt, LPVOID lpData)
{
    FIXME("(%p, %u, %p): stub\n", hCtx, wExt, lpData);

    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);

    return FALSE;
}

/***********************************************************************
 *		WTExtSet (WINTAB32.64)
 */
BOOL WINAPI WTExtSet(HCTX hCtx, UINT wExt, LPVOID lpData)
{
    FIXME("(%p, %u, %p): stub\n", hCtx, wExt, lpData);

    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);

    return FALSE;
}

/***********************************************************************
 *		WTSave (WINTAB32.65)
 */
BOOL WINAPI WTSave(HCTX hCtx, LPVOID lpSaveInfo)
{
    FIXME("(%p, %p): stub\n", hCtx, lpSaveInfo);

    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);

    return FALSE;
}

/***********************************************************************
 *		WTRestore (WINTAB32.66)
 */
HCTX WINAPI WTRestore(HWND hWnd, LPVOID lpSaveInfo, BOOL fEnable)
{
    FIXME("(%p, %p, %u): stub\n", hWnd, lpSaveInfo, fEnable);

    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);

    return 0;
}

/***********************************************************************
 *		WTPacketsPeek (WINTAB32.80)
 */
int WINAPI WTPacketsPeek(HCTX hCtx, int cMaxPkts, LPVOID lpPkts)
{
923 924 925
    int limit;
    LPOPENCONTEXT context;
    LPVOID ptr = lpPkts;
926

927
    TRACE("(%p, %d, %p)\n", hCtx, cMaxPkts, lpPkts);
928

929 930 931 932 933 934
    if (!hCtx || !lpPkts) return 0;

    EnterCriticalSection(&csTablet);

    context = TABLET_FindOpenContext(hCtx);

935
    if (!context || context->PacketsQueued == 0)
936 937 938 939 940 941 942 943 944 945 946
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }

    for (limit = 0; limit < cMaxPkts && limit < context->PacketsQueued; limit++)
        ptr = TABLET_CopyPacketData(context ,ptr, &context->PacketQueue[limit]);

    LeaveCriticalSection(&csTablet);
    TRACE("Copied %i packets\n",limit);
    return limit;
947 948 949 950 951 952 953 954
}

/***********************************************************************
 *		WTDataGet (WINTAB32.81)
 */
int WINAPI WTDataGet(HCTX hCtx, UINT wBegin, UINT wEnd,
		     int cMaxPkts, LPVOID lpPkts, LPINT lpNPkts)
{
955 956
    LPOPENCONTEXT context;
    LPVOID ptr = lpPkts;
957 958 959
    INT bgn = 0;
    INT end = 0;
    INT num = 0;
960 961

    TRACE("(%p, %u, %u, %d, %p, %p)\n",
962 963
	  hCtx, wBegin, wEnd, cMaxPkts, lpPkts, lpNPkts);

964
    if (!hCtx) return 0;
965

966 967 968 969
    EnterCriticalSection(&csTablet);

    context = TABLET_FindOpenContext(hCtx);

970
    if (!context || context->PacketsQueued == 0)
971 972 973 974 975 976 977 978 979 980 981 982 983 984
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }

    while (bgn < context->PacketsQueued &&
           context->PacketQueue[bgn].pkSerialNumber != wBegin)
        bgn++;

    end = bgn;
    while (end < context->PacketsQueued &&
           context->PacketQueue[end].pkSerialNumber != wEnd)
        end++;

985
    if ((bgn == end) && (end == context->PacketsQueued))
986 987 988 989 990 991
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }

    for (num = bgn; num <= end; num++)
992
        ptr = TABLET_CopyPacketData(context ,ptr, &context->PacketQueue[num]);
993 994 995

    /* remove read packets */
    if ((end+1) < context->PacketsQueued)
996 997
        memmove( &context->PacketQueue[bgn], &context->PacketQueue[end+1],
                (context->PacketsQueued - (end+1)) * sizeof (WTPACKET));
998 999 1000 1001 1002 1003 1004

    context->PacketsQueued -= ((end-bgn)+1);
    *lpNPkts = ((end-bgn)+1);

    LeaveCriticalSection(&csTablet);
    TRACE("Copied %i packets\n",*lpNPkts);
    return (end - bgn)+1;
1005 1006 1007 1008 1009 1010 1011 1012
}

/***********************************************************************
 *		WTDataPeek (WINTAB32.82)
 */
int WINAPI WTDataPeek(HCTX hCtx, UINT wBegin, UINT wEnd,
		      int cMaxPkts, LPVOID lpPkts, LPINT lpNPkts)
{
1013 1014
    LPOPENCONTEXT context;
    LPVOID ptr = lpPkts;
1015 1016 1017
    INT bgn = 0;
    INT end = 0;
    INT num = 0;
1018 1019

    TRACE("(%p, %u, %u, %d, %p, %p)\n",
1020 1021
	  hCtx, wBegin, wEnd, cMaxPkts, lpPkts, lpNPkts);

1022
    if (!hCtx || !lpPkts) return 0;
1023

1024 1025 1026 1027
    EnterCriticalSection(&csTablet);

    context = TABLET_FindOpenContext(hCtx);

1028
    if (!context || context->PacketsQueued == 0)
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }

    while (bgn < context->PacketsQueued &&
           context->PacketQueue[bgn].pkSerialNumber != wBegin)
        bgn++;

    end = bgn;
    while (end < context->PacketsQueued &&
           context->PacketQueue[end].pkSerialNumber != wEnd)
        end++;

    if (bgn == context->PacketsQueued ||  end == context->PacketsQueued)
    {
1045
        TRACE("%i %i %i\n", bgn, end, context->PacketsQueued);
1046 1047 1048 1049 1050
        LeaveCriticalSection(&csTablet);
        return 0;
    }

    for (num = bgn; num <= end; num++)
1051
        ptr = TABLET_CopyPacketData(context ,ptr, &context->PacketQueue[num]);
1052 1053 1054 1055 1056 1057

    *lpNPkts = ((end-bgn)+1);
    LeaveCriticalSection(&csTablet);

    TRACE("Copied %i packets\n",*lpNPkts);
    return (end - bgn)+1;
1058 1059 1060 1061 1062 1063 1064
}

/***********************************************************************
 *		WTQueuePacketsEx (WINTAB32.200)
 */
BOOL WINAPI WTQueuePacketsEx(HCTX hCtx, UINT *lpOld, UINT *lpNew)
{
1065
    LPOPENCONTEXT context;
1066

1067 1068 1069 1070 1071 1072 1073 1074
    TRACE("(%p, %p, %p)\n", hCtx, lpOld, lpNew);

    if (!hCtx) return 0;

    EnterCriticalSection(&csTablet);

    context = TABLET_FindOpenContext(hCtx);

1075
    if (context && context->PacketsQueued)
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    {
        *lpOld = context->PacketQueue[0].pkSerialNumber;
        *lpNew = context->PacketQueue[context->PacketsQueued-1].pkSerialNumber;
    }
    else
    {
        TRACE("No packets\n");
        LeaveCriticalSection(&csTablet);
        return FALSE;
    }
    LeaveCriticalSection(&csTablet);
1087 1088 1089 1090 1091 1092 1093 1094 1095

    return TRUE;
}

/***********************************************************************
 *		WTQueueSizeGet (WINTAB32.84)
 */
int WINAPI WTQueueSizeGet(HCTX hCtx)
{
1096
    LPOPENCONTEXT context;
1097 1098
    int queueSize = 0;

1099
    TRACE("(%p)\n", hCtx);
1100

1101
    if (!hCtx) return 0;
1102

1103 1104
    EnterCriticalSection(&csTablet);
    context = TABLET_FindOpenContext(hCtx);
1105 1106
    if (context)
        queueSize = context->QueueSize;
1107
    LeaveCriticalSection(&csTablet);
1108
    return queueSize;
1109 1110 1111 1112 1113 1114 1115
}

/***********************************************************************
 *		WTQueueSizeSet (WINTAB32.85)
 */
BOOL WINAPI WTQueueSizeSet(HCTX hCtx, int nPkts)
{
1116
    LPOPENCONTEXT context;
1117

1118
    TRACE("(%p, %d)\n", hCtx, nPkts);
1119

1120 1121 1122 1123 1124
    if (!hCtx) return 0;

    EnterCriticalSection(&csTablet);

    context = TABLET_FindOpenContext(hCtx);
1125 1126 1127 1128 1129
    if (!context)
    {
        LeaveCriticalSection(&csTablet);
        return 0;
    }
1130 1131 1132 1133 1134 1135 1136 1137

    context->PacketQueue = HeapReAlloc(GetProcessHeap(), 0,
                        context->PacketQueue, sizeof(WTPACKET)*nPkts);

    context->QueueSize = nPkts;
    LeaveCriticalSection(&csTablet);

    return nPkts;
1138
}