kis_tablet_support_win8.cpp 38.3 KB
Newer Older
1 2 3
/*
 * Copyright (c) 2017 Alvin Wong <alvinhochun@gmail.com>
 *
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Redistribution and use in source and binary forms, with or without
 - modification, are permitted provided that the following conditions
 - are met:
 -
 - 1. Redistributions of source code must retain the above copyright
 -    notice, this list of conditions and the following disclaimer.
 - 2. Redistributions in binary form must reproduce the above copyright
 -    notice, this list of conditions and the following disclaimer in the
 -    documentation and/or other materials provided with the distribution.
 -
 - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 */

// Get Windows 8 API prototypes and types
#ifdef WINVER
#  undef WINVER
#endif
#ifdef _WIN32_WINNT
#  undef _WIN32_WINNT
#endif
#define WINVER 0x0602
#define _WIN32_WINNT 0x0602

#include "kis_tablet_support_win8.h"

#include <QApplication>
#include <QDebug>
#include <QHash>
#include <QLibrary>
#include <QPointer>
#include <QTabletEvent>
#include <QVector>
#include <QWidget>
Alvin Wong's avatar
Alvin Wong committed
46
#include <QWindow>
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

#include <utility>

#include <kis_debug.h>

#include <windows.h>
#include <tpcshrd.h>

#ifndef Q_OS_WIN
#  error This file must not be compiled for non-Windows systems
#endif

namespace
{

class Win8PointerInputApi
{
#define WIN8_POINTER_INPUT_API_LIST(FUNC) \
    /* Pointer Input Functions */ \
    FUNC(GetPointerPenInfo) \
    FUNC(GetPointerPenInfoHistory) \
    FUNC(GetPointerType) \
    /* Pointer Device Functions */ \
70
    FUNC(GetPointerDevices) \
71
    /*FUNC(GetPointerDeviceProperties)*/ \
72
    FUNC(GetPointerDevice) \
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 139 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 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    FUNC(GetPointerDeviceRects) \
    /*FUNC(RegisterPointerDeviceNotifications)*/ \
    /* end */

    bool m_loaded;

public:

#define DEFINE_FP_FROM_WINAPI(func) \
    public: using p ## func ## _t = std::add_pointer<decltype(func)>::type; \
    private: p ## func ## _t m_p ## func = nullptr; \
    public: const p ## func ## _t &func = m_p ## func; // const fp ref to member

    WIN8_POINTER_INPUT_API_LIST(DEFINE_FP_FROM_WINAPI)

#undef DEFINE_FP_FROM_WINAPI

public:
    Win8PointerInputApi()
        : m_loaded(false)
    {
    }

    bool init() {
        if (m_loaded) {
            return true;
        }

        QLibrary user32Lib("user32");
        if (!user32Lib.load()) {
            qWarning() << "Failed to load user32.dll! This really should not happen.";
            return false;
        }

#define LOAD_AND_CHECK_FP_FROM_WINAPI(func) \
    m_p ## func = reinterpret_cast<p ## func ## _t>(user32Lib.resolve(#func)); \
    if (!m_p ## func) { \
        dbgTablet << "Failed to load function " #func " from user32.dll"; \
        return false; \
    }

        WIN8_POINTER_INPUT_API_LIST(LOAD_AND_CHECK_FP_FROM_WINAPI)

#undef LOAD_AND_CHECK_FP_FROM_WINAPI

        dbgTablet << "Loaded Windows 8 Pointer Input API functions";
        m_loaded = true;
        return true;
    }

    bool isLoaded() {
        return m_loaded;
    }

#undef WIN8_POINTER_INPUT_API_LIST
}; // class Win8PointerInputApi

Win8PointerInputApi api;

class PointerFlagsWrapper
{
    const POINTER_FLAGS f;

public:
    PointerFlagsWrapper(POINTER_FLAGS flags)
        : f(flags)
    {}

    static PointerFlagsWrapper fromPointerInfo(const POINTER_INFO &pointerInfo) {
        return PointerFlagsWrapper(pointerInfo.pointerFlags);
    }

    static PointerFlagsWrapper fromPenInfo(const POINTER_PEN_INFO &penInfo) {
        return fromPointerInfo(penInfo.pointerInfo);
    }

    bool isNew() const {
        return f & POINTER_FLAG_NEW;
    }

    bool isInRange() const {
        return f & POINTER_FLAG_INRANGE;
    }

    bool isInContact() const {
        return f & POINTER_FLAG_INCONTACT;
    }

    bool isFirstButtonDown() const {
        return f & POINTER_FLAG_FIRSTBUTTON;
    }

    bool isSecondButtonDown() const {
        return f & POINTER_FLAG_SECONDBUTTON;
    }

    bool isThirdButtonDown() const {
        return f & POINTER_FLAG_THIRDBUTTON;
    }

    bool isForthButtonDown() const {
        return f & POINTER_FLAG_FOURTHBUTTON;
    }

    bool isFifthButtonDown() const {
        return f & POINTER_FLAG_FIFTHBUTTON;
    }

    bool isPrimary() const {
        return f & POINTER_FLAG_PRIMARY;
    }

    bool isConfidence() const {
        return f & POINTER_FLAG_CONFIDENCE;
    }

    bool isCancelled() const {
        return f & POINTER_FLAG_CANCELED;
    }

    bool isDown() const {
        return f & POINTER_FLAG_DOWN;
    }

    bool isUpdate() const {
        return f & POINTER_FLAG_UPDATE;
    }

    bool isUp() const {
        return f & POINTER_FLAG_UP;
    }

    bool isWheel() const {
        return f & POINTER_FLAG_WHEEL;
    }

    bool isHWheel() const {
        return f & POINTER_FLAG_HWHEEL;
    }

    bool isCaptureChanged() const {
        return f & POINTER_FLAG_CAPTURECHANGED;
    }

    bool hasTransform() const {
        // mingw-w64 headers is missing this flag
        // return f & POINTER_FLAG_HASTRANSFORM;
        return f & 0x00400000;
    }
}; // class PointerFlagsWrapper

class PenFlagsWrapper
{
    const PEN_FLAGS f;

public:
    PenFlagsWrapper(PEN_FLAGS flags)
        : f(flags)
    {}

    static PenFlagsWrapper fromPenInfo(const POINTER_PEN_INFO &penInfo) {
        return PenFlagsWrapper(penInfo.penFlags);
    }

    bool isBarrelPressed() const {
        return f & PEN_FLAG_BARREL;
    }

    bool isInverted() const {
        return f & PEN_FLAG_INVERTED;
    }

    bool isEraserPressed() const {
        return f & PEN_FLAG_ERASER;
    }
}; // class PenFlagsWrapper

class PenMaskWrapper
{
    const PEN_MASK f;

public:
    PenMaskWrapper(PEN_MASK mask)
        :f(mask)
    {}

    static PenMaskWrapper fromPenInfo(const POINTER_PEN_INFO &penInfo) {
        return PenMaskWrapper(penInfo.penMask);
    }

    bool pressureValid() const {
        return f & PEN_MASK_PRESSURE;
    }

    bool rotationValid() const {
        return f & PEN_MASK_ROTATION;
    }

    bool tiltXValid() const {
        return f & PEN_MASK_TILT_X;
    }

    bool tiltYValid() const {
        return f & PEN_MASK_TILT_Y;
    }
}; // class PenMaskWrapper

struct PointerDeviceItem
{
    // HANDLE handle;
    // RECT pointerDeviceRect;
    // RECT displayRect;
    qreal himetricToPixelX;
    qreal himetricToPixelY;
    qreal pixelOffsetX;
    qreal pixelOffsetY;
289
    DISPLAYCONFIG_ROTATION deviceOrientation; // This is needed to fix tilt
290 291 292 293 294 295 296 297 298 299 300
};

QHash<HANDLE, PointerDeviceItem> penDevices;

struct PenPointerItem
{
    // int pointerId;
    // POINTER_PEN_INFO penInfo;
    HWND hwnd;
    HANDLE deviceHandle;
    QPointer<QWidget> activeWidget; // Current widget receiving events
Alvin Wong's avatar
Alvin Wong committed
301
    qreal oneOverDpr; // 1 / devicePixelRatio of activeWidget
302 303
    bool widgetIsCaptured; // Current widget is capturing a pen cown event
    bool widgetIsIgnored; // Pen events should be ignored until pen up
304
    bool widgetAcceptsPenEvent; // Whether the widget accepts pen events
305 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

    bool isCaptured() const {
        return widgetIsCaptured;
    }
};

QHash<int, PenPointerItem> penPointers;
// int primaryPenPointerId;

bool handlePointerMsg(const MSG &msg);

// extern "C" {
//
// LRESULT CALLBACK pointerDeviceNotificationsWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
// {
//     switch (uMsg) {
//     case WM_POINTERDEVICECHANGE:
//         dbgTablet << "I would want to handle this WM_POINTERDEVICECHANGE event, but ms just doesn't want me to use it";
//         dbgTablet << "  wParam:" << wParam;
//         dbgTablet << "  lParam:" << lParam;
//         return 0;
//     case WM_POINTERDEVICEINRANGE:
//         dbgTablet << "I would want to handle this WM_POINTERDEVICEINRANGE event, but ms just doesn't want me to use it";
//         dbgTablet << "  wParam:" << wParam;
//         dbgTablet << "  lParam:" << lParam;
//         return 0;
//     case WM_POINTERDEVICEOUTOFRANGE:
//         dbgTablet << "I would want to handle this WM_POINTERDEVICEOUTOFRANGE event, but ms just doesn't want me to use it";
//         dbgTablet << "  wParam:" << wParam;
//         dbgTablet << "  lParam:" << lParam;
//         return 0;
//     }
//     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
// }
//
// } // extern "C"

} // namespace

bool KisTabletSupportWin8::isAvailable()
{
    // Just try loading the APIs
    return api.init();
}

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 378 379 380 381 382
bool KisTabletSupportWin8::isPenDeviceAvailable()
{
    if (!api.init()) {
        return false;
    }
    UINT32 deviceCount = 0;
    if (!api.GetPointerDevices(&deviceCount, nullptr)) {
        dbgTablet << "GetPointerDevices failed";
        return false;
    }
    if (deviceCount == 0) {
        dbgTablet << "No pointer devices";
        return false;
    }
    QVector<POINTER_DEVICE_INFO> devices(deviceCount);
    if (!api.GetPointerDevices(&deviceCount, devices.data())) {
        dbgTablet << "GetPointerDevices failed";
        return false;
    }
    bool hasPenDevice = false;
    Q_FOREACH (const POINTER_DEVICE_INFO &device, devices) {
        dbgTablet << "Found pointer device" << static_cast<void *>(device.device)
                  << QString::fromWCharArray(device.productString)
                  << "type:" << device.pointerDeviceType;
        if (device.pointerDeviceType == POINTER_DEVICE_TYPE_INTEGRATED_PEN ||
            device.pointerDeviceType == POINTER_DEVICE_TYPE_EXTERNAL_PEN) {
            hasPenDevice = true;
        }
    }
    dbgTablet << "hasPenDevice:" << hasPenDevice;
    return hasPenDevice;
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
bool KisTabletSupportWin8::init()
{
    return api.init();
}

// void KisTabletSupportWin8::registerPointerDeviceNotifications()
// {
//     const wchar_t *className = L"w8PointerMsgWindow";
//     HINSTANCE hInst = static_cast<HINSTANCE>(GetModuleHandleW(nullptr));
//     WNDCLASSEXW wc;
//     wc.cbSize = sizeof(WNDCLASSEXW);
//     wc.style = 0;
//     wc.lpfnWndProc = pointerDeviceNotificationsWndProc;
//     wc.cbClsExtra = 0;
//     wc.cbWndExtra = 0;
//     wc.hInstance = hInst;
//     wc.hCursor = 0;
//     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
//     wc.hIcon = 0;
//     wc.hIconSm = 0;
//     wc.lpszMenuName = 0;
//     wc.lpszClassName = className;
//
//     if (RegisterClassEx(&wc)) {
//         HWND hwnd = CreateWindowEx(0, className, nullptr, 0, 0, 0, 0, 0, HWND_MESSAGE, nullptr, hInst, nullptr);
//         api.RegisterPointerDeviceNotifications(hwnd, TRUE);
//     } else {
//         dbgTablet << "Cannot register dummy window";
//     }
// }

bool KisTabletSupportWin8::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
    if (!result) {
        // I don't know why this even happens, but it actually does
        // And the same event is sent in again with result != nullptr
        return false;
    }

    // This is only installed on Windows so there is no reason to check eventType
    MSG &msg = *static_cast<MSG *>(message);

    switch (msg.message) {
    case WM_POINTERDOWN:
    case WM_POINTERUP:
    case WM_POINTERENTER:
    case WM_POINTERLEAVE:
    case WM_POINTERUPDATE:
    case WM_POINTERCAPTURECHANGED:
        {
            bool handled = handlePointerMsg(msg);
            if (handled) {
                *result = 0;
                return true;
            }
            break;
        }
    case WM_TABLET_QUERYSYSTEMGESTURESTATUS:
        *result = 0;
        return true;
    }

    Q_UNUSED(eventType)
    return false;
}

namespace {

QDebug operator<<(QDebug debug, const POINT &pt)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << '(' << pt.x << ", " << pt.y << ')';

    return debug;
}

QDebug operator<<(QDebug debug, const RECT &rect)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << '(' << rect.left << ", " << rect.top << ", " << rect.right << ", " << rect.bottom << ')';

    return debug;
}

467
bool registerOrUpdateDevice(HANDLE deviceHandle, const RECT &pointerDeviceRect, const RECT &displayRect, const DISPLAYCONFIG_ROTATION deviceOrientation)
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
{
    bool isPreviouslyRegistered = penDevices.contains(deviceHandle);
    PointerDeviceItem &deviceItem = penDevices[deviceHandle];
    PointerDeviceItem oldDeviceItem = deviceItem;
    // deviceItem.handle = deviceHandle;
    deviceItem.himetricToPixelX =
            static_cast<qreal>(displayRect.right - displayRect.left)
            / (pointerDeviceRect.right - pointerDeviceRect.left);
    deviceItem.himetricToPixelY =
            static_cast<qreal>(displayRect.bottom - displayRect.top)
            / (pointerDeviceRect.bottom - pointerDeviceRect.top);
    deviceItem.pixelOffsetX = static_cast<qreal>(displayRect.left)
            - deviceItem.himetricToPixelX * pointerDeviceRect.left;
    deviceItem.pixelOffsetY = static_cast<qreal>(displayRect.top)
            - deviceItem.himetricToPixelY * pointerDeviceRect.top;
483
    deviceItem.deviceOrientation = deviceOrientation;
484 485 486 487 488
    if (!isPreviouslyRegistered) {
        dbgTablet << "Registered pen device" << deviceHandle
                  << "with displayRect" << displayRect
                  << "and deviceRect" << pointerDeviceRect
                  << "scale" << deviceItem.himetricToPixelX << deviceItem.himetricToPixelY
489 490
                  << "offset" << deviceItem.pixelOffsetX << deviceItem.pixelOffsetY
                  << "orientation" << deviceItem.deviceOrientation;
491 492 493
    } else if (deviceItem.himetricToPixelX != oldDeviceItem.himetricToPixelX
            || deviceItem.himetricToPixelY != oldDeviceItem.himetricToPixelY
            || deviceItem.pixelOffsetX != oldDeviceItem.pixelOffsetX
494 495
            || deviceItem.pixelOffsetY != oldDeviceItem.pixelOffsetY
            || deviceItem.deviceOrientation != oldDeviceItem.deviceOrientation) {
496 497 498 499
        dbgTablet << "Updated pen device" << deviceHandle
                  << "with displayRect" << displayRect
                  << "and deviceRect" << pointerDeviceRect
                  << "scale" << deviceItem.himetricToPixelX << deviceItem.himetricToPixelY
500 501
                  << "offset" << deviceItem.pixelOffsetX << deviceItem.pixelOffsetY
                  << "orientation" << deviceItem.deviceOrientation;
502 503 504 505 506 507 508 509 510 511 512
    }
    return true;
}

bool registerOrUpdateDevice(HANDLE deviceHandle)
{
    RECT pointerDeviceRect, displayRect;
    if (!api.GetPointerDeviceRects(deviceHandle, &pointerDeviceRect, &displayRect)) {
        dbgTablet << "GetPointerDeviceRects failed";
        return false;
    }
513 514 515 516 517 518 519
    POINTER_DEVICE_INFO pointerDeviceInfo;
    if (!api.GetPointerDevice(deviceHandle, &pointerDeviceInfo)) {
        dbgTablet << "GetPointerDevice failed";
        return false;
    }
    return registerOrUpdateDevice(deviceHandle, pointerDeviceRect, displayRect,
            static_cast<DISPLAYCONFIG_ROTATION>(pointerDeviceInfo.displayOrientation));
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
}

QTabletEvent makeProximityTabletEvent(const QEvent::Type eventType, const POINTER_PEN_INFO &penInfo)
{
    PenFlagsWrapper penFlags = PenFlagsWrapper::fromPenInfo(penInfo);
    QTabletEvent::PointerType pointerType = penFlags.isInverted() ? QTabletEvent::Eraser : QTabletEvent::Pen;
    const QPointF emptyPoint;
    return QTabletEvent(
        eventType, // type
        emptyPoint, // pos
        emptyPoint, // globalPos
        QTabletEvent::Stylus, // device
        pointerType, // pointerType
        0, // pressure
        0, // xTilt
        0, // yTilt
        0, // tangentialPressure
        0, // rotation
        0, // z
        Qt::NoModifier, // keyState
        reinterpret_cast<qint64>(penInfo.pointerInfo.sourceDevice), // uniqueID
        Qt::NoButton, // button
        (Qt::MouseButtons)0 // buttons
    );
}

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
// void rotateTiltAngles(int &tiltX, int &tiltY, const DISPLAYCONFIG_ROTATION orientation) {
//     int newTiltX, newTiltY;
//     switch (orientation) {
//     case DISPLAYCONFIG_ROTATION_ROTATE90:
//         newTiltX = -tiltY;
//         newTiltY = tiltX;
//         break;
//     case DISPLAYCONFIG_ROTATION_ROTATE180:
//         newTiltX = -tiltX;
//         newTiltY = -tiltY;
//         break;
//     case DISPLAYCONFIG_ROTATION_ROTATE270:
//         newTiltX = tiltY;
//         newTiltY = -tiltX;
//         break;
//     case DISPLAYCONFIG_ROTATION_IDENTITY:
//     default:
//         newTiltX = tiltX;
//         newTiltY = tiltY;
//         break;
//     }
//     tiltX = newTiltX;
//     tiltY = newTiltY;
// }
570

Alvin Wong's avatar
Alvin Wong committed
571
QTabletEvent makePositionalTabletEvent(const QWidget *targetWidget, const QEvent::Type eventType, const POINTER_PEN_INFO &penInfo, const PointerDeviceItem &deviceItem, const PenPointerItem &penPointerItem)
572 573 574 575 576 577
{
    PenFlagsWrapper penFlags = PenFlagsWrapper::fromPenInfo(penInfo);
    PointerFlagsWrapper pointerFlags = PointerFlagsWrapper::fromPenInfo(penInfo);
    PenMaskWrapper penMask = PenMaskWrapper::fromPenInfo(penInfo);

    const QPointF globalPosF(
Alvin Wong's avatar
Alvin Wong committed
578 579
        (deviceItem.himetricToPixelX * penInfo.pointerInfo.ptHimetricLocationRaw.x + deviceItem.pixelOffsetX) * penPointerItem.oneOverDpr,
        (deviceItem.himetricToPixelY * penInfo.pointerInfo.ptHimetricLocationRaw.y + deviceItem.pixelOffsetY) * penPointerItem.oneOverDpr
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    );
    const QPoint globalPos = globalPosF.toPoint();
    const QPoint localPos = targetWidget->mapFromGlobal(globalPos);
    const QPointF delta = globalPosF - globalPos;
    const QPointF localPosF = localPos + delta;

    const QTabletEvent::PointerType pointerType = penFlags.isInverted() ? QTabletEvent::Eraser : QTabletEvent::Pen;

    Qt::MouseButton mouseButton;
    if (eventType == QEvent::TabletPress) {
        if (penInfo.pointerInfo.ButtonChangeType == POINTER_CHANGE_SECONDBUTTON_DOWN) {
            mouseButton = Qt::RightButton;
        } else {
            KIS_SAFE_ASSERT_RECOVER(penInfo.pointerInfo.ButtonChangeType == POINTER_CHANGE_FIRSTBUTTON_DOWN) {
                qWarning() << "WM_POINTER* sent unknown ButtonChangeType" << penInfo.pointerInfo.ButtonChangeType;
            }
            mouseButton = Qt::LeftButton;
        }
    } else if (eventType == QEvent::TabletRelease) {
        if (penInfo.pointerInfo.ButtonChangeType == POINTER_CHANGE_SECONDBUTTON_UP) {
            mouseButton = Qt::RightButton;
        } else {
            KIS_SAFE_ASSERT_RECOVER(penInfo.pointerInfo.ButtonChangeType == POINTER_CHANGE_FIRSTBUTTON_UP) {
                qWarning() << "WM_POINTER* sent unknown ButtonChangeType" << penInfo.pointerInfo.ButtonChangeType;
            }
            mouseButton = Qt::LeftButton;
        }
    } else {
        mouseButton = Qt::NoButton;
    }

    Qt::MouseButtons mouseButtons;
    if (pointerFlags.isFirstButtonDown()) {
        mouseButtons |= Qt::LeftButton;
    }
    if (pointerFlags.isSecondButtonDown()) {
        mouseButtons |= Qt::RightButton;
    }

619 620 621 622 623 624 625
    int tiltX = 0, tiltY = 0;
    if (penMask.tiltXValid()) {
        tiltX = qBound(-60, penInfo.tiltX, 60);
    }
    if (penMask.tiltYValid()) {
        tiltY = qBound(-60, penInfo.tiltY, 60);
    }
626
    // rotateTiltAngles(tiltX, tiltY, deviceItem.deviceOrientation);
627

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
    int rotation = 0;
    if (penMask.rotationValid()) {
        rotation = 360 - penInfo.rotation; // Flip direction and convert to signed int
        if (rotation > 180) {
            rotation -= 360;
        }
    }

    return QTabletEvent(
        eventType, // type
        localPosF, // pos
        globalPosF, // globalPos
        QTabletEvent::Stylus, // device
        pointerType, // pointerType
        penMask.pressureValid() ? static_cast<qreal>(penInfo.pressure) / 1024 : 0, // pressure
643 644
        tiltX, // xTilt
        tiltY, // yTilt
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
        0, // tangentialPressure
        rotation, // rotation
        0, // z
        QApplication::queryKeyboardModifiers(), // keyState
        reinterpret_cast<qint64>(penInfo.pointerInfo.sourceDevice), // uniqueID
        mouseButton, // button
        mouseButtons // buttons
    );
}

bool sendProximityTabletEvent(const QEvent::Type eventType, const POINTER_PEN_INFO &penInfo)
{
    KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(
        eventType == QEvent::TabletEnterProximity || eventType == QEvent::TabletLeaveProximity,
        false
    );
    QTabletEvent ev = makeProximityTabletEvent(eventType, penInfo);
    ev.setAccepted(false);
    ev.setTimestamp(penInfo.pointerInfo.dwTime);
    QCoreApplication::sendEvent(qApp, &ev);
    return ev.isAccepted();
}

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
void synthesizeMouseEvent(const QTabletEvent &ev, const POINTER_PEN_INFO &penInfo)
{
    // Update the cursor position
    BOOL result = SetCursorPos(penInfo.pointerInfo.ptPixelLocationRaw.x, penInfo.pointerInfo.ptPixelLocationRaw.y);
    if (!result) {
        dbgInput << "SetCursorPos failed, err" << GetLastError();
        return;
    }
    // Send mousebutton down/up events. Windows stores the button state.
    DWORD inputDataFlags = 0;
    switch (ev.type()) {
    case QEvent::TabletPress:
        switch (ev.button()) {
        case Qt::LeftButton:
            inputDataFlags = MOUSEEVENTF_LEFTDOWN;
            break;
        case Qt::RightButton:
            inputDataFlags = MOUSEEVENTF_RIGHTDOWN;
            break;
        default:
            return;
        }
        break;
    case QEvent::TabletRelease:
        switch (ev.button()) {
        case Qt::LeftButton:
            inputDataFlags = MOUSEEVENTF_LEFTUP;
            break;
        case Qt::RightButton:
            inputDataFlags = MOUSEEVENTF_RIGHTUP;
            break;
        default:
            return;
        }
        break;
    case QEvent::TabletMove:
    default:
        return;
    }
    INPUT inputData = {};
    inputData.type = INPUT_MOUSE;
    inputData.mi.dwFlags = inputDataFlags;
    inputData.mi.dwExtraInfo = 0xFF515700 | 0x01; // https://msdn.microsoft.com/en-us/library/windows/desktop/ms703320%28v=vs.85%29.aspx
    UINT result2 = SendInput(1, &inputData, sizeof(inputData));
    if (result2 != 1) {
        dbgInput << "SendInput failed, err" << GetLastError();
        return;
    }
}

bool sendPositionalTabletEvent(QWidget *target, const QEvent::Type eventType, const POINTER_PEN_INFO &penInfo, const PointerDeviceItem &device, const PenPointerItem &penPointerItem, const bool shouldSynthesizeMouseEvent)
719 720 721 722 723
{
    KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(
        eventType == QEvent::TabletMove || eventType == QEvent::TabletPress || eventType == QEvent::TabletRelease,
        false
    );
Alvin Wong's avatar
Alvin Wong committed
724
    QTabletEvent ev = makePositionalTabletEvent(target, eventType, penInfo, device, penPointerItem);
725 726 727
    ev.setAccepted(false);
    ev.setTimestamp(penInfo.pointerInfo.dwTime);
    QCoreApplication::sendEvent(target, &ev);
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
    if (!shouldSynthesizeMouseEvent) {
        // For pen update with multiple updates, only the last update should
        // be used to synthesize a mouse event.
        return false;
    }
    // This is some specialized code to handle synthesizing of mouse events from
    // the pen events. Issues being:
    // 1. We must accept the pointer down/up and the intermediate update events
    //    to indicate that we want all the pen pointer events for painting,
    //    otherwise Windows may do weird stuff and skip passing pointer events.
    // 2. Some Qt and Krita code uses QCursor::pos() which calls GetCursorPos to
    //    get the cursor position. This doesn't work nicely before ver 1709 and
    //    doesn't work at all on ver 1709 if the pen events are handled, so we
    //    need some way to nudge the cursor on the OS level.
    // It appears that using the same way (as in synthesizeMouseEvent) to nudge
    // the cursor does work fine for when painting on canvas (which handles
    // the QTabletEvent), but not for other widgets because it introduces a lag
    // with mouse move events on move start and immediately after mouse down.
    // The resolution is to simulate mouse movement with our own code only for
    // handled pen events, which is what the following code does.
    if (ev.type() == QEvent::TabletMove && ev.buttons() == Qt::NoButton) {
        // Let Windows synthesize mouse hover events
        return false;
    }
    if (ev.type() == QEvent::TabletPress && !ev.isAccepted()) {
        // On pen down event, if the widget doesn't handle the event, let
        // Windows translate the event to touch, mouse or whatever
        return false;
    }
    if (ev.type() != QEvent::TabletPress && !penPointerItem.widgetAcceptsPenEvent) {
        // For other events, if the previous pen down event wasn't handled by
        // the widget, continue to let Windows translate the event
        return false;
    }
    // Otherwise, we synthesize our mouse events
    synthesizeMouseEvent(ev, penInfo);
    return true; // and tell Windows that we do want the pen events.
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
}

bool handlePenEnterMsg(const POINTER_PEN_INFO &penInfo)
{
    PointerFlagsWrapper pointerFlags = PointerFlagsWrapper::fromPenInfo(penInfo);
    if (!pointerFlags.isPrimary()) {
        // Don't handle non-primary pointer messages for now
        dbgTablet << "Pointer" << penInfo.pointerInfo.pointerId
                  << "of device" << penInfo.pointerInfo.sourceDevice
                  << "is not flagged PRIMARY";
        return false;
    }

    // Update the device scaling factors here
    // It doesn't cost much to recalculate anyway
    // This ensures that the screen resolution changes are reflected
    // WM_POINTERDEVICECHANGE might be useful for this, but its docs are too unclear to use
    registerOrUpdateDevice(penInfo.pointerInfo.sourceDevice);
    // TODO: Need a way to remove from device registration when devices are changed

    // We now only handle one pointer at a time, so just clear the pointer registration
    penPointers.clear();

    int pointerId = penInfo.pointerInfo.pointerId;
    PenPointerItem penPointerItem;
    penPointerItem.hwnd = penInfo.pointerInfo.hwndTarget;
    penPointerItem.deviceHandle = penInfo.pointerInfo.sourceDevice;
    penPointerItem.activeWidget = nullptr;
Alvin Wong's avatar
Alvin Wong committed
793
    penPointerItem.oneOverDpr = 1.0;
794 795
    penPointerItem.widgetIsCaptured = false;
    penPointerItem.widgetIsIgnored = false;
796
    penPointerItem.widgetAcceptsPenEvent = false;
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
    // penPointerItem.pointerId = pointerId;

    penPointers.insert(pointerId, penPointerItem);
    // primaryPenPointerId = pointerId;

    // penEnter
    sendProximityTabletEvent(QEvent::TabletEnterProximity, penInfo);

    return false;
}

bool handlePenLeaveMsg(const POINTER_PEN_INFO &penInfo)
{
    if (!penPointers.contains(penInfo.pointerInfo.pointerId)) {
        dbgTablet << "Pointer" << penInfo.pointerInfo.pointerId << "wasn't being handled";
        return false;
    }
    if (!penDevices.contains(penInfo.pointerInfo.sourceDevice)) {
        dbgTablet << "Device is gone from the registration???";
        // TODO: re-register device?
        penPointers.remove(penInfo.pointerInfo.pointerId);
        return false;
    }

    // penLeave
    sendProximityTabletEvent(QEvent::TabletLeaveProximity, penInfo);

    penPointers.remove(penInfo.pointerInfo.pointerId);

    return false;
}

829
bool handleSinglePenUpdate(PenPointerItem &penPointerItem, const POINTER_PEN_INFO &penInfo, const PointerDeviceItem &device, const bool shouldSynthesizeMouseEvent)
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
{
    QWidget *targetWidget;
    if (penPointerItem.isCaptured()) {
        if (penPointerItem.widgetIsIgnored) {
            return false;
        }
        targetWidget = penPointerItem.activeWidget;
        if (!targetWidget) {
            return false;
        }
    } else {
        QWidget *hwndWidget = QWidget::find(reinterpret_cast<WId>(penInfo.pointerInfo.hwndTarget));
        if (!hwndWidget) {
            dbgTablet << "HWND cannot be mapped to QWidget (what?)";
            return false;
        }
        {
            // Check popup / modal widget
            QWidget *modalWidget = QApplication::activePopupWidget();
            if (!modalWidget) {
                modalWidget = QApplication::activeModalWidget();
            }
            if (modalWidget && modalWidget != hwndWidget && !modalWidget->isAncestorOf(hwndWidget)) {
                return false;
            }
        }
Alvin Wong's avatar
Alvin Wong committed
856 857 858 859 860 861 862 863 864 865 866 867
        {
            QWindow *topLevelWindow = hwndWidget->windowHandle();
            if (topLevelWindow) {
                penPointerItem.oneOverDpr = 1.0 / topLevelWindow->devicePixelRatio();
            } else {
                penPointerItem.oneOverDpr = 1.0 / qApp->devicePixelRatio();
            }
        }
        QPoint posInHwndWidget = hwndWidget->mapFromGlobal(QPoint(
            static_cast<int>(penInfo.pointerInfo.ptPixelLocationRaw.x * penPointerItem.oneOverDpr),
            static_cast<int>(penInfo.pointerInfo.ptPixelLocationRaw.y * penPointerItem.oneOverDpr)
        ));
868 869 870 871 872 873 874 875
        targetWidget = hwndWidget->childAt(posInHwndWidget);
        if (!targetWidget) {
            // dbgTablet << "No childQWidget at cursor position";
            targetWidget = hwndWidget;
        }
        // penPointerItem.activeWidget = targetWidget;
    }

876
    bool handled = sendPositionalTabletEvent(targetWidget, QEvent::TabletMove, penInfo, device, penPointerItem, shouldSynthesizeMouseEvent);
877 878 879 880 881 882 883
    return handled;
}

bool handlePenUpdateMsg(const POINTER_PEN_INFO &penInfo)
{
    auto currentPointerIt = penPointers.find(penInfo.pointerInfo.pointerId);
    if (currentPointerIt == penPointers.end()) {
Alvin Wong's avatar
Alvin Wong committed
884
        // dbgTablet << "Pointer" << penInfo.pointerInfo.pointerId << "wasn't being handled";
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
        return false;
    }

    const auto devIt = penDevices.find(penInfo.pointerInfo.sourceDevice);
    if (devIt == penDevices.end()) {
        dbgTablet << "Device not registered???";
        return false;
    }

    // UINT32 entriesCount = 0;
    // if (!api.GetPointerPenInfoHistory(penInfo.pointerInfo.pointerId, &entriesCount, nullptr)) {
    //     dbgTablet << "GetPointerPenInfoHistory (getting count) failed";
    //     return false;
    // }
    UINT32 entriesCount = penInfo.pointerInfo.historyCount;
    // dbgTablet << "entriesCount:" << entriesCount;
901
    bool handled = false;
902 903 904 905 906 907 908 909 910 911
    if (entriesCount != 1) {
        QVector<POINTER_PEN_INFO> penInfoArray(entriesCount);
        if (!api.GetPointerPenInfoHistory(penInfo.pointerInfo.pointerId, &entriesCount, penInfoArray.data())) {
            dbgTablet << "GetPointerPenInfoHistory failed";
            return false;
        }
        bool handled = false;
        // The returned array is in reverse chronological order
        const auto rbegin = penInfoArray.rbegin();
        const auto rend = penInfoArray.rend();
912
        const auto rlast = rend - 1; // Only synthesize mouse event for the last one
913
        for (auto it = rbegin; it != rend; ++it) {
914
            handled |= handleSinglePenUpdate(*currentPointerIt, *it, *devIt, it == rlast); // Bitwise OR doesn't short circuit
915 916
        }
    } else {
917
        handled = handleSinglePenUpdate(*currentPointerIt, penInfo, *devIt, true);
918
    }
919
    return handled;
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
}

bool handlePenDownMsg(const POINTER_PEN_INFO &penInfo)
{
    // PointerFlagsWrapper pointerFlags = PointerFlagsWrapper::fromPenInfo(penInfo);
    // if (!pointerFlags.isPrimary()) {
    //     // Don't handle non-primary pointer messages for now
    //     return false;
    // }
    auto currentPointerIt = penPointers.find(penInfo.pointerInfo.pointerId);
    if (currentPointerIt == penPointers.end()) {
        dbgTablet << "Pointer" << penInfo.pointerInfo.pointerId << "wasn't being handled";
        return false;
    }
    currentPointerIt->hwnd = penInfo.pointerInfo.hwndTarget; // They *should* be the same, but just in case
    QWidget *hwndWidget = QWidget::find(reinterpret_cast<WId>(penInfo.pointerInfo.hwndTarget));
    if (!hwndWidget) {
        dbgTablet << "HWND cannot be mapped to QWidget (what?)";
        return false;
    }
Alvin Wong's avatar
Alvin Wong committed
940 941 942 943 944 945 946 947 948 949 950 951
    {
        QWindow *topLevelWindow = hwndWidget->windowHandle();
        if (topLevelWindow) {
            currentPointerIt->oneOverDpr = 1.0 / topLevelWindow->devicePixelRatio();
        } else {
            currentPointerIt->oneOverDpr = 1.0 / qApp->devicePixelRatio();
        }
    }
    QPoint posInHwndWidget = hwndWidget->mapFromGlobal(QPoint(
        static_cast<int>(penInfo.pointerInfo.ptPixelLocationRaw.x * currentPointerIt->oneOverDpr),
        static_cast<int>(penInfo.pointerInfo.ptPixelLocationRaw.y * currentPointerIt->oneOverDpr)
    ));
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
    QWidget *targetWidget = hwndWidget->childAt(posInHwndWidget);
    if (!targetWidget) {
        dbgTablet << "No childQWidget at cursor position";
        targetWidget = hwndWidget;
    }

    currentPointerIt->activeWidget = targetWidget;
    currentPointerIt->widgetIsCaptured = true;
    // dbgTablet << "QWidget" << targetWidget->windowTitle() << "is capturing pointer" << penInfo.pointerInfo.pointerId;
    {
        // Check popup / modal widget
        QWidget *modalWidget = QApplication::activePopupWidget();
        if (!modalWidget) {
            modalWidget = QApplication::activeModalWidget();
        }
        if (modalWidget && modalWidget != hwndWidget && !modalWidget->isAncestorOf(hwndWidget)) {
            currentPointerIt->widgetIsIgnored = true;
            dbgTablet << "Pointer" << penInfo.pointerInfo.pointerId << "is being captured but will be ignored";
            return false;
        }
    }

    // penDown
    const auto devIt = penDevices.find(penInfo.pointerInfo.sourceDevice);
    if (devIt == penDevices.end()) {
        dbgTablet << "Device not registered???";
        return false;
    }

981 982
    bool handled = sendPositionalTabletEvent(targetWidget, QEvent::TabletPress, penInfo, *devIt, *currentPointerIt, true);
    currentPointerIt->widgetAcceptsPenEvent = handled;
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
    if (!handled) {
        // dbgTablet << "QWidget did not handle tablet down event";
    }
    return handled;
}

bool handlePenUpMsg(const POINTER_PEN_INFO &penInfo)
{
    auto currentPointerIt = penPointers.find(penInfo.pointerInfo.pointerId);
    if (currentPointerIt == penPointers.end()) {
        dbgTablet << "Pointer" << penInfo.pointerInfo.pointerId << "wasn't being handled";
        return false;
    }
    PenPointerItem &penPointerItem = *currentPointerIt;

    if (!penPointerItem.isCaptured()) {
        dbgTablet << "Pointer wasn't captured";
        return false;
    }
    if (penPointerItem.widgetIsIgnored) {
        penPointerItem.widgetIsCaptured = false;
        penPointerItem.widgetIsIgnored = false;
        return false;
    }

    // penUp
    QWidget *targetWidget = penPointerItem.activeWidget;
    if (!targetWidget) {
        dbgTablet << "Previously captured target has been deleted";
        penPointerItem.widgetIsCaptured = false;
        return false;
    }

    const auto devIt = penDevices.find(penInfo.pointerInfo.sourceDevice);
    if (devIt == penDevices.end()) {
        dbgTablet << "Device not registered???";
        return false;
    }

1022
    bool handled = sendPositionalTabletEvent(targetWidget, QEvent::TabletRelease, penInfo, *devIt, penPointerItem, true);
1023 1024 1025

    // dbgTablet << "QWidget" << currentPointerIt->activeWidget->windowTitle() << "is releasing capture to pointer" << penInfo.pointerInfo.pointerId;
    penPointerItem.widgetIsCaptured = false;
1026
    penPointerItem.widgetAcceptsPenEvent = false;
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
    return handled;
}

bool handlePointerMsg(const MSG &msg)
{
    if (!api.isLoaded()) {
        qWarning() << "Windows 8 Pointer Input API functions not loaded";
        return false;
    }

    int pointerId = GET_POINTERID_WPARAM(msg.wParam);
    POINTER_INPUT_TYPE pointerType;
    if (!api.GetPointerType(pointerId, &pointerType)) {
        dbgTablet << "GetPointerType failed";
        return false;
    }
    if (pointerType != PT_PEN) {
Alvin Wong's avatar
Alvin Wong committed
1044
        // dbgTablet << "pointerType" << pointerType << "is not PT_PEN";
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
        return false;
    }

    POINTER_PEN_INFO penInfo;
    if (!api.GetPointerPenInfo(pointerId, &penInfo)) {
        dbgTablet << "GetPointerPenInfo failed";
        return false;
    }

    switch (msg.message) {
    case WM_POINTERDOWN:
        // dbgTablet << "WM_POINTERDOWN";
        break;
    case WM_POINTERUP:
        // dbgTablet << "WM_POINTERUP";
        break;
    case WM_POINTERENTER:
        // dbgTablet << "WM_POINTERENTER";
        break;
    case WM_POINTERLEAVE:
        // dbgTablet << "WM_POINTERLEAVE";
        break;
    case WM_POINTERUPDATE:
        // dbgTablet << "WM_POINTERUPDATE";
        break;
    case WM_POINTERCAPTURECHANGED:
        // dbgTablet << "WM_POINTERCAPTURECHANGED";
        break;
    default:
        dbgTablet << "I missed this message: " << msg.message;
        break;
    }
    // dbgTablet << "  hwnd:        " << penInfo.pointerInfo.hwndTarget;
    // dbgTablet << "  msg hwnd:    " << msg.hwnd;
    // dbgTablet << "  pointerId:   " << pointerId;
    // dbgTablet << "  sourceDevice:" << penInfo.pointerInfo.sourceDevice;
    // dbgTablet << "  pointerFlags:" << penInfo.pointerInfo.pointerFlags;
    // dbgTablet << "  btnChgType:  " << penInfo.pointerInfo.ButtonChangeType;
    // dbgTablet << "  penFlags:    " << penInfo.penFlags;
    // dbgTablet << "  penMask:     " << penInfo.penMask;
    // dbgTablet << "  pressure:    " << penInfo.pressure;
    // dbgTablet << "  rotation:    " << penInfo.rotation;
    // dbgTablet << "  tiltX:       " << penInfo.tiltX;
    // dbgTablet << "  tiltY:       " << penInfo.tiltY;
    // dbgTablet << "  ptPixelLocationRaw:   " << penInfo.pointerInfo.ptPixelLocationRaw;
    // dbgTablet << "  ptHimetricLocationRaw:" << penInfo.pointerInfo.ptHimetricLocationRaw;
    // RECT pointerDeviceRect, displayRect;
    // if (!api.GetPointerDeviceRects(penInfo.pointerInfo.sourceDevice, &pointerDeviceRect, &displayRect)) {
    //     dbgTablet << "GetPointerDeviceRects failed";
    //     return false;
    // }
    // dbgTablet << "  pointerDeviceRect:" << pointerDeviceRect;
    // dbgTablet << "  displayRect:" << displayRect;
    // dbgTablet << "  scaled X:" << static_cast<qreal>(penInfo.pointerInfo.ptHimetricLocationRaw.x) / (pointerDeviceRect.right - pointerDeviceRect.left) * (displayRect.right - displayRect.left);
    // dbgTablet << "  scaled Y:" << static_cast<qreal>(penInfo.pointerInfo.ptHimetricLocationRaw.y) / (pointerDeviceRect.bottom - pointerDeviceRect.top) * (displayRect.bottom - displayRect.top);

    switch (msg.message) {
    case WM_POINTERDOWN:
        return handlePenDownMsg(penInfo);
    case WM_POINTERUP:
        return handlePenUpMsg(penInfo);
    case WM_POINTERENTER:
        return handlePenEnterMsg(penInfo);
    case WM_POINTERLEAVE:
        return handlePenLeaveMsg(penInfo);
    case WM_POINTERUPDATE:
1111
        return handlePenUpdateMsg(penInfo);
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
    case WM_POINTERCAPTURECHANGED:
        // TODO: Should this event be handled?
        dbgTablet << "FIXME: WM_POINTERCAPTURECHANGED isn't handled";
        break;
    }

    return false;
}

} // namespace