Blender V5.3
GHOST_Wintab.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10/* Wacom's Wintab documentation is periodically offline, moved, and increasingly hidden away. You
11 * can find a (painstakingly) archived copy of the documentation at
12 * https://web.archive.org/web/20201122230125/https://developer-docs-legacy.wacom.com/display/DevDocs/Windows+Wintab+Documentation
13 */
14
15#pragma once
16
17#include <memory>
18#include <stdio.h>
19#include <vector>
20#include <wtypes.h>
21
22#include "GHOST_Types.hh"
23
24#include <wintab.h>
25/* PACKETDATA and PACKETMODE modify structs in pktdef.h, so make sure they come first. */
26#define PACKETDATA \
27 (PK_BUTTONS | PK_NORMAL_PRESSURE | PK_ORIENTATION | PK_CURSOR | PK_X | PK_Y | PK_TIME)
28#define PACKETMODE 0
29#include <pktdef.h>
30
31#if !defined(_MSVC_TRADITIONAL) || _MSVC_TRADITIONAL
32# define WINTAB_PRINTF(x, ...) \
33 { \
34 if (GHOST_Wintab::getDebug()) { \
35 printf(x, __VA_ARGS__); \
36 } \
37 } \
38 (void)0
39#else
40# define WINTAB_PRINTF(x, ...) \
41 { \
42 if (GHOST_Wintab::getDebug()) { \
43 printf(x, ##__VA_ARGS__); \
44 } \
45 } \
46 (void)0
47#endif
48
49/* Typedefs for Wintab functions to allow dynamic loading. */
50typedef UINT(API *GHOST_WIN32_WTInfo)(UINT, UINT, LPVOID);
51typedef BOOL(API *GHOST_WIN32_WTGet)(HCTX, LPLOGCONTEXTA);
52typedef BOOL(API *GHOST_WIN32_WTSet)(HCTX, LPLOGCONTEXTA);
53typedef HCTX(API *GHOST_WIN32_WTOpen)(HWND, LPLOGCONTEXTA, BOOL);
54typedef BOOL(API *GHOST_WIN32_WTClose)(HCTX);
55typedef int(API *GHOST_WIN32_WTPacketsGet)(HCTX, int, LPVOID);
56typedef int(API *GHOST_WIN32_WTQueueSizeGet)(HCTX);
57typedef BOOL(API *GHOST_WIN32_WTQueueSizeSet)(HCTX, int);
58typedef BOOL(API *GHOST_WIN32_WTEnable)(HCTX, BOOL);
59typedef BOOL(API *GHOST_WIN32_WTOverlap)(HCTX, BOOL);
60
61/* Typedefs for Wintab and Windows resource management. */
62typedef std::unique_ptr<std::remove_pointer_t<HMODULE>, decltype(&::FreeLibrary)> unique_hmodule;
63typedef std::unique_ptr<std::remove_pointer_t<HCTX>, GHOST_WIN32_WTClose> unique_hctx;
64
73
74class GHOST_Wintab {
75 public:
81 static GHOST_Wintab *loadWintab(HWND hwnd);
82
83 ~GHOST_Wintab();
84
88 void enable();
89
93 void disable();
94
98 void gainFocus();
99
103 void loseFocus();
104
108 void leaveRange();
109
113 void remapCoordinates();
114
122 void mapWintabToSysCoordinates(int x_in, int y_in, int &x_out, int &y_out);
123
127 void updateCursorInfo();
128
133 void processInfoChange(LPARAM lParam);
134
139 bool devicesPresent();
140
145 void getInput(std::vector<GHOST_WintabInfoWin32> &outWintabInfo);
146
151 bool trustCoordinates();
152
166 bool testCoordinates(int sysX, int sysY, int wtX, int wtY);
167
173
178 static void setDebug(bool debug);
179
184 static bool getDebug();
185
186 private:
188 unique_hmodule handle_;
190 GHOST_WIN32_WTInfo fp_info_ = nullptr;
191 [[maybe_unused]] GHOST_WIN32_WTGet fp_get_ = nullptr;
192 GHOST_WIN32_WTSet fp_set_ = nullptr;
193 GHOST_WIN32_WTPacketsGet fp_packets_get_ = nullptr;
194 GHOST_WIN32_WTEnable fp_enable_ = nullptr;
195 GHOST_WIN32_WTOverlap fp_overlap_ = nullptr;
196
198 unique_hctx context_;
200 bool enabled_ = false;
202 bool focused_ = false;
203
205 DWORD buttons_ = 0;
206
208 struct Range {
210 int org = 0;
212 int ext = 1;
213 };
214
216 struct Coord {
218 Range x = {};
220 Range y = {};
221 };
223 bool coord_trusted_ = false;
225 Coord tablet_coord_ = {};
227 Coord system_coord_ = {};
228
229 int max_pressure_ = 0;
230 int max_azimuth_ = 0;
231 int max_altitude_ = 0;
232
234 UINT num_devices_ = 0;
236 std::vector<PACKET> pkts_;
238 GHOST_TabletData last_tablet_data_ = GHOST_TABLET_DATA_NONE;
239
241 static bool debug_;
242
243 GHOST_Wintab(unique_hmodule handle,
247 GHOST_WIN32_WTPacketsGet packetsGet,
249 GHOST_WIN32_WTOverlap overlap,
250 unique_hctx hctx,
251 Coord tablet,
252 Coord system,
253 size_t queueSize);
254
261 GHOST_TButton mapWintabToGhostButton(UINT cursor, WORD physicalButton);
262
267 static GHOST_Wintab *loadWintabUnsafe(HWND hwnd);
268
273 static void modifyContext(LOGCONTEXT &lc);
274
281 static void extractCoordinates(LOGCONTEXT &lc, Coord &tablet, Coord &system);
282
283 /* Prints Wintab Context information. */
284 void printContextDebugInfo();
285};
GHOST_TEventType
@ GHOST_kEventCursorMove
GHOST_TButton
@ GHOST_kButtonMaskNone
BOOL(API *) GHOST_WIN32_WTOverlap(HCTX, BOOL)
BOOL(API *) GHOST_WIN32_WTGet(HCTX, LPLOGCONTEXTA)
std::unique_ptr< std::remove_pointer_t< HCTX >, GHOST_WIN32_WTClose > unique_hctx
HCTX(API *) GHOST_WIN32_WTOpen(HWND, LPLOGCONTEXTA, BOOL)
BOOL(API *) GHOST_WIN32_WTClose(HCTX)
BOOL(API *) GHOST_WIN32_WTQueueSizeSet(HCTX, int)
std::unique_ptr< std::remove_pointer_t< HMODULE >, decltype(&::FreeLibrary)> unique_hmodule
int(API *) GHOST_WIN32_WTPacketsGet(HCTX, int, LPVOID)
BOOL(API *) GHOST_WIN32_WTEnable(HCTX, BOOL)
int(API *) GHOST_WIN32_WTQueueSizeGet(HCTX)
BOOL(API *) GHOST_WIN32_WTSet(HCTX, LPLOGCONTEXTA)
UINT(API *) GHOST_WIN32_WTInfo(UINT, UINT, LPVOID)
unsigned long long int uint64_t
void mapWintabToSysCoordinates(int x_in, int y_in, int &x_out, int &y_out)
GHOST_TabletData getLastTabletData()
bool devicesPresent()
void getInput(std::vector< GHOST_WintabInfoWin32 > &outWintabInfo)
void processInfoChange(LPARAM lParam)
void updateCursorInfo()
void remapCoordinates()
static GHOST_Wintab * loadWintab(HWND hwnd)
bool trustCoordinates()
static bool getDebug()
bool testCoordinates(int sysX, int sysY, int wtX, int wtY)
static void setDebug(bool debug)
#define API(returnType)
GHOST_TEventType type
GHOST_TabletData tabletData