-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTouchpadTablet.cpp
760 lines (686 loc) · 21.7 KB
/
TouchpadTablet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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
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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
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
719
720
721
722
723
724
725
726
727
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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <windows.h>
#include <stdio.h>
#include <hidsdi.h>
#include <hidusage.h>
#include <vector>
#include <unordered_map>
#include <optional>
#include "resource.h"
#define WMAPP_NOTIFYCALLBACK (WM_APP + 1)
#define HID_USAGE_DIGITIZER_CONTACT_ID 0x51
#define HID_USAGE_DIGITIZER_CONTACT_COUNT 0x54
#define DEBUG_MODE 0
HWND hwnd;
HINSTANCE hInstance;
WNDCLASSEX wc;
NOTIFYICONDATA nid = {};
RECT bounds = { -1, -1, -1, -1 };
// units in mm. defaults are taken from my laptop
float width = 110;
float height = 51;
float awidth = 110;
float aheight = 51;
int swidth = GetSystemMetrics(SM_CXSCREEN);
int sheight = GetSystemMetrics(SM_CYSCREEN);
// Contact information parsed from the HID report descriptor.
struct contact_info
{
USHORT link;
};
// The data for a touch event.
struct contact
{
contact_info info;
ULONG id;
POINT point;
};
// Wrapper for malloc with unique_ptr semantics, to allow
// for variable-sized structures.
struct free_deleter { void operator()(void* ptr) { free(ptr); } };
template<typename T> using malloc_ptr = std::unique_ptr<T, free_deleter>;
// Device information, such as touch area bounds and HID offsets.
// This can be reused across HID events, so we only have to parse
// this info once.
struct device_info
{
malloc_ptr<_HIDP_PREPARSED_DATA> preparsedData; // HID internal data
USHORT linkContactCount = 0; // Link collection for number of contacts present
std::vector<contact_info> contactInfo; // Link collection and touch area for each contact
};
// Caches per-device info for better performance
static std::unordered_map<HANDLE, device_info> g_devices;
// Holds the current primary touch point ID
static thread_local ULONG t_primaryContactID;
// Allocates a malloc_ptr with the given size. The size must be
// greater than or equal to sizeof(T).
template<typename T>
static malloc_ptr<T>
make_malloc(size_t size)
{
T* ptr = (T*)malloc(size);
if (ptr == nullptr) {
throw std::bad_alloc();
}
return malloc_ptr<T>(ptr);
}
// C-style printf for debug output.
#if DEBUG_MODE
static void
vfdebugf(FILE* f, const char* fmt, va_list args)
{
vfprintf(f, fmt, args);
putc('\n', f);
}
static void
debugf(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vfdebugf(stderr, fmt, args);
va_end(args);
}
#else
#define debugf(...) ((void)0)
#endif
std::vector<std::string> split(const std::string& s, char delim) {
std::stringstream ss(s);
std::string item;
std::vector<std::string> elems;
while (std::getline(ss, item, delim)) {
elems.push_back(std::move(item));
}
return elems;
}
// Taken from Windows 7 SDK
BOOL AddNotificationIcon()
{
nid.cbSize = sizeof(nid);
nid.uID = 1;
nid.hWnd = hwnd;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
nid.uCallbackMessage = WMAPP_NOTIFYCALLBACK;
Shell_NotifyIcon(NIM_ADD, &nid);
// NOTIFYICON_VERSION_4 is prefered
nid.uVersion = NOTIFYICON_VERSION_4;
return Shell_NotifyIcon(NIM_SETVERSION, &nid);
}
// Taken from Windows 7 SDK
void ShowContextMenu(HWND hwnd, POINT pt)
{
HMENU hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU1));
if (hMenu)
{
HMENU hSubMenu = GetSubMenu(hMenu, 0);
if (hSubMenu)
{
// our window must be foreground before calling TrackPopupMenu or the menu will not disappear when the user clicks away
SetForegroundWindow(hwnd);
// respect menu drop alignment
UINT uFlags = TPM_RIGHTBUTTON;
if (GetSystemMetrics(SM_MENUDROPALIGNMENT) != 0)
{
uFlags |= TPM_RIGHTALIGN;
}
else
{
uFlags |= TPM_LEFTALIGN;
}
TrackPopupMenuEx(hSubMenu, uFlags, pt.x, pt.y, hwnd, NULL);
}
DestroyMenu(hMenu);
}
}
// On exit
void Clean() {
Shell_NotifyIcon(NIM_DELETE, &nid);
PostQuitMessage(0);
}
// Registers the specified window to receive touchpad HID events.
static void RegisterTouchpadInput()
{
RAWINPUTDEVICE dev;
dev.usUsagePage = HID_USAGE_PAGE_DIGITIZER;
dev.usUsage = HID_USAGE_DIGITIZER_TOUCH_PAD;
dev.dwFlags = RIDEV_INPUTSINK;
dev.hwndTarget = hwnd;
if (!RegisterRawInputDevices(&dev, 1, sizeof(RAWINPUTDEVICE))) {
throw;
}
}
// Reads the raw input header for the given raw input handle.
static RAWINPUTHEADER GetRawInputHeader(HRAWINPUT hInput)
{
RAWINPUTHEADER hdr;
UINT size = sizeof(hdr);
if (GetRawInputData(hInput, RID_HEADER, &hdr, &size, sizeof(RAWINPUTHEADER)) == (UINT)-1) {
throw;
}
return hdr;
}
// Reads the raw input data for the given raw input handle.
static malloc_ptr<RAWINPUT> GetRawInput(HRAWINPUT hInput, RAWINPUTHEADER hdr)
{
malloc_ptr<RAWINPUT> input = make_malloc<RAWINPUT>(hdr.dwSize);
UINT size = hdr.dwSize;
if (GetRawInputData(hInput, RID_INPUT, input.get(), &size, sizeof(RAWINPUTHEADER)) == (UINT)-1) {
throw;
}
return input;
}
// Gets info about a raw input device.
static RID_DEVICE_INFO GetRawInputDeviceInfo(HANDLE hDevice)
{
RID_DEVICE_INFO info;
info.cbSize = sizeof(RID_DEVICE_INFO);
UINT size = sizeof(RID_DEVICE_INFO);
if (GetRawInputDeviceInfoW(hDevice, RIDI_DEVICEINFO, &info, &size) == (UINT)-1) {
throw;
}
return info;
}
// Reads the preparsed HID report descriptor for the device
// that generated the given raw input.
static malloc_ptr<_HIDP_PREPARSED_DATA> GetHidPreparsedData(HANDLE hDevice)
{
UINT size = 0;
if (GetRawInputDeviceInfoW(hDevice, RIDI_PREPARSEDDATA, nullptr, &size) == (UINT)-1) {
throw;
}
malloc_ptr<_HIDP_PREPARSED_DATA> preparsedData = make_malloc<_HIDP_PREPARSED_DATA>(size);
if (GetRawInputDeviceInfoW(hDevice, RIDI_PREPARSEDDATA, preparsedData.get(), &size) == (UINT)-1) {
throw;
}
return preparsedData;
}
// Returns all input button caps for the given preparsed
// HID report descriptor.
static std::vector<HIDP_BUTTON_CAPS> GetHidInputButtonCaps(PHIDP_PREPARSED_DATA preparsedData)
{
NTSTATUS status;
HIDP_CAPS caps;
status = HidP_GetCaps(preparsedData, &caps);
if (status != HIDP_STATUS_SUCCESS) {
throw;
}
USHORT numCaps = caps.NumberInputButtonCaps;
std::vector<HIDP_BUTTON_CAPS> buttonCaps(numCaps);
status = HidP_GetButtonCaps(HidP_Input, &buttonCaps[0], &numCaps, preparsedData);
if (status != HIDP_STATUS_SUCCESS) {
throw;
}
buttonCaps.resize(numCaps);
return buttonCaps;
}
// Returns all input value caps for the given preparsed
// HID report descriptor.
static std::vector<HIDP_VALUE_CAPS> GetHidInputValueCaps(PHIDP_PREPARSED_DATA preparsedData)
{
NTSTATUS status;
HIDP_CAPS caps;
status = HidP_GetCaps(preparsedData, &caps);
if (status != HIDP_STATUS_SUCCESS) {
throw;
}
USHORT numCaps = caps.NumberInputValueCaps;
std::vector<HIDP_VALUE_CAPS> valueCaps(numCaps);
status = HidP_GetValueCaps(HidP_Input, &valueCaps[0], &numCaps, preparsedData);
if (status != HIDP_STATUS_SUCCESS) {
throw;
}
valueCaps.resize(numCaps);
return valueCaps;
}
// Reads the pressed status of a single HID report button.
static bool GetHidUsageButton(
HIDP_REPORT_TYPE reportType,
USAGE usagePage,
USHORT linkCollection,
USAGE usage,
PHIDP_PREPARSED_DATA preparsedData,
PBYTE report,
ULONG reportLen)
{
ULONG numUsages = HidP_MaxUsageListLength(
reportType,
usagePage,
preparsedData);
std::vector<USAGE> usages(numUsages);
NTSTATUS status = HidP_GetUsages(
reportType,
usagePage,
linkCollection,
&usages[0],
&numUsages,
preparsedData,
(PCHAR)report,
reportLen);
if (status != HIDP_STATUS_SUCCESS) {
throw;
}
usages.resize(numUsages);
return std::find(usages.begin(), usages.end(), usage) != usages.end();
}
// Reads a single HID report value in logical units.
static ULONG GetHidUsageLogicalValue(
HIDP_REPORT_TYPE reportType,
USAGE usagePage,
USHORT linkCollection,
USAGE usage,
PHIDP_PREPARSED_DATA preparsedData,
PBYTE report,
ULONG reportLen)
{
ULONG value;
NTSTATUS status = HidP_GetUsageValue(
reportType,
usagePage,
linkCollection,
usage,
&value,
preparsedData,
(PCHAR)report,
reportLen);
if (status != HIDP_STATUS_SUCCESS) {
throw;
}
return value;
}
// Reads a single HID report value in physical units.
static LONG GetHidUsagePhysicalValue(
HIDP_REPORT_TYPE reportType,
USAGE usagePage,
USHORT linkCollection,
USAGE usage,
PHIDP_PREPARSED_DATA preparsedData,
PBYTE report,
ULONG reportLen)
{
LONG value;
NTSTATUS status = HidP_GetScaledUsageValue(
reportType,
usagePage,
linkCollection,
usage,
&value,
preparsedData,
(PCHAR)report,
reportLen);
if (status != HIDP_STATUS_SUCCESS) {
return -1;
}
return value;
}
// Gets the device info associated with the given raw input. Uses the
// cached info if available; otherwise parses the HID report descriptor
// and stores it into the cache.
static device_info& GetDeviceInfo(HANDLE hDevice)
{
if (g_devices.count(hDevice)) {
return g_devices.at(hDevice);
}
device_info dev;
std::optional<USHORT> linkContactCount;
dev.preparsedData = GetHidPreparsedData(hDevice);
// Struct to hold our parser state
struct contact_info_tmp
{
bool hasContactID = false;
bool hasTip = false;
bool hasX = false;
bool hasY = false;
};
std::unordered_map<USHORT, contact_info_tmp> contacts;
// Get the touch area for all the contacts. Also make sure that each one
// is actually a contact, as specified by:
// https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/windows-precision-touchpad-required-hid-top-level-collections
for (const HIDP_VALUE_CAPS& cap : GetHidInputValueCaps(dev.preparsedData.get())) {
if (cap.IsRange || !cap.IsAbsolute) {
continue;
}
if (cap.UsagePage == HID_USAGE_PAGE_GENERIC) {
if (cap.NotRange.Usage == HID_USAGE_GENERIC_X) {
contacts[cap.LinkCollection].hasX = true;
}
else if (cap.NotRange.Usage == HID_USAGE_GENERIC_Y) {
contacts[cap.LinkCollection].hasY = true;
}
}
else if (cap.UsagePage == HID_USAGE_PAGE_DIGITIZER) {
if (cap.NotRange.Usage == HID_USAGE_DIGITIZER_CONTACT_COUNT) {
linkContactCount = cap.LinkCollection;
}
else if (cap.NotRange.Usage == HID_USAGE_DIGITIZER_CONTACT_ID) {
contacts[cap.LinkCollection].hasContactID = true;
}
}
}
for (const HIDP_BUTTON_CAPS& cap : GetHidInputButtonCaps(dev.preparsedData.get())) {
if (cap.UsagePage == HID_USAGE_PAGE_DIGITIZER) {
if (cap.NotRange.Usage == HID_USAGE_DIGITIZER_TIP_SWITCH) {
contacts[cap.LinkCollection].hasTip = true;
}
}
}
if (!linkContactCount.has_value()) {
throw std::runtime_error("No contact count usage found");
}
dev.linkContactCount = linkContactCount.value();
for (const auto& kvp : contacts) {
USHORT link = kvp.first;
const contact_info_tmp& info = kvp.second;
if (info.hasContactID && info.hasTip && info.hasX && info.hasY) {
debugf("Contact for device %p: link=%d",
hDevice,
link);
dev.contactInfo.push_back({ link});
}
}
return g_devices[hDevice] = std::move(dev);
}
// Reads all touch contact points from a raw input event.
static std::vector<contact> GetContacts(device_info& dev, RAWINPUT* input)
{
std::vector<contact> contacts;
DWORD sizeHid = input->data.hid.dwSizeHid;
DWORD count = input->data.hid.dwCount;
BYTE* rawData = input->data.hid.bRawData;
if (count == 0) {
debugf("Raw input contained no HID events");
return contacts;
}
ULONG numContacts = GetHidUsageLogicalValue(
HidP_Input,
HID_USAGE_PAGE_DIGITIZER,
dev.linkContactCount,
HID_USAGE_DIGITIZER_CONTACT_COUNT,
dev.preparsedData.get(),
rawData,
sizeHid);
if (numContacts > dev.contactInfo.size()) {
debugf("Device reported more contacts (%u) than we have links (%zu)", numContacts, dev.contactInfo.size());
numContacts = (ULONG)dev.contactInfo.size();
}
// It's a little ambiguous as to whether contact count includes
// released contacts. I interpreted the specs as a yes, but this
// may require additional testing.
for (ULONG i = 0; i < numContacts; ++i) {
contact_info& info = dev.contactInfo[i];
bool tip = GetHidUsageButton(
HidP_Input,
HID_USAGE_PAGE_DIGITIZER,
info.link,
HID_USAGE_DIGITIZER_TIP_SWITCH,
dev.preparsedData.get(),
rawData,
sizeHid);
if (!tip) {
debugf("Contact has tip = 0, ignoring");
continue;
}
ULONG id = GetHidUsageLogicalValue(
HidP_Input,
HID_USAGE_PAGE_DIGITIZER,
info.link,
HID_USAGE_DIGITIZER_CONTACT_ID,
dev.preparsedData.get(),
rawData,
sizeHid);
LONG x = GetHidUsagePhysicalValue(
HidP_Input,
HID_USAGE_PAGE_GENERIC,
info.link,
HID_USAGE_GENERIC_X,
dev.preparsedData.get(),
rawData,
sizeHid);
LONG y = GetHidUsagePhysicalValue(
HidP_Input,
HID_USAGE_PAGE_GENERIC,
info.link,
HID_USAGE_GENERIC_Y,
dev.preparsedData.get(),
rawData,
sizeHid);
if (x != -1 && y != -1)
contacts.push_back({ info, id, { x, y } });
}
return contacts;
}
// Returns the primary contact for a given list of contacts. This is
// necessary since we are mapping potentially many touches to a single
// mouse position. Currently this just stores a global contact ID and
// uses that as the primary contact.
static contact GetPrimaryContact(const std::vector<contact>& contacts)
{
for (const contact& contact : contacts) {
if (contact.id == t_primaryContactID) {
return contact;
}
}
t_primaryContactID = contacts[0].id;
return contacts[0];
}
void WriteCalibration() {
std::ofstream calib;
calib.open("tpcalib.dat");
calib << bounds.left << std::endl;
calib << bounds.right << std::endl;
calib << bounds.top << std::endl;
calib << bounds.bottom << std::endl;
calib.close();
}
void ReadCalibration() {
int i = 0;
std::ifstream input("tpcalib.dat");
if (input.good()) {
for (std::string line; std::getline(input, line); )
{
switch (i) {
case 0:
bounds.left = std::stol(line.c_str());
break;
case 1:
bounds.right = std::stol(line.c_str());
break;
case 2:
bounds.top = std::stol(line.c_str());
break;
case 3:
bounds.bottom = std::stol(line.c_str());
break;
}
i++;
}
debugf("Loaded calibration %d %d %d %d", bounds.left, bounds.right, bounds.top, bounds.bottom);
}
else {
MessageBox(hwnd, "Calibrate touchpad by touching each corner after clicking ok", "TouchpadTablet", MB_OK | MB_ICONQUESTION);
}
}
void HandleCalibration(LONG x, LONG y) {
if (x < bounds.left || bounds.left == -1) {
bounds.left = x;
WriteCalibration();
}
if (x > bounds.right || bounds.right == -1) {
bounds.right = x;
WriteCalibration();
}
if (y < bounds.top || bounds.top == -1) {
bounds.top = y;
WriteCalibration();
}
if (y > bounds.bottom || bounds.bottom == -1) {
bounds.bottom = y;
WriteCalibration();
}
}
void ReadConfig() {
std::string line;
std::ifstream input("config.txt");
if (input.good()) {
for (std::string line; std::getline(input, line); )
{
if (line[0] == '#')
continue;
std::vector<std::string> s = split(line, '=');
if (s.size() == 2) {
if (s[0] == "Width")
width = std::stof(s[1].c_str());
else if (s[0] == "Height")
height = std::stof(s[1].c_str());
else if (s[0] == "AreaWidth")
awidth = std::stof(s[1].c_str());
else if (s[0] == "AreaHeight")
aheight = std::stof(s[1].c_str());
}
}
debugf("Loaded config.txt");
}
}
// Handles a WM_INPUT event
static void HandleRawInput(WPARAM* wParam, LPARAM* lParam)
{
INPUT event = { 0 };
HRAWINPUT hInput = (HRAWINPUT)*lParam;
RAWINPUTHEADER hdr = GetRawInputHeader(hInput);
device_info& dev = GetDeviceInfo(hdr.hDevice);
malloc_ptr<RAWINPUT> input = GetRawInput(hInput, hdr);
std::vector<contact> contacts = GetContacts(dev, input.get());
float newx = ((float)bounds.right - (float)bounds.left) * ((float)awidth / (float)width);
float newy = ((float)bounds.bottom - (float)bounds.top) * ((float)aheight / (float)height);
for (const contact& contact : contacts) {
HandleCalibration(contact.point.x, contact.point.y);
}
if (contacts.empty()) {
debugf("Found no contacts in input event");
return;
}
contact contact = GetPrimaryContact(contacts);
debugf("%d %d", contact.point.x, contact.point.y);
double x = (contact.point.x - bounds.left - ((((float)bounds.right - (float)bounds.left) - newx) / 2)) * ((float)swidth / newx);
double y = (contact.point.y - bounds.top - ((((float)bounds.bottom - (float)bounds.top) - newy) / 2)) * ((float)sheight / newy);
event.type = INPUT_MOUSE;
event.mi.dx = (long)((x * 65536) / swidth);
event.mi.dy = (long)((y * 65536) / sheight);
event.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
SendInput(1, &event, sizeof(INPUT));
}
BOOL HasPrecisionTouchpad() {
std::vector<RAWINPUTDEVICELIST> devices(64);
while (true) {
UINT numDevices = (UINT)devices.size();
UINT ret = GetRawInputDeviceList(&devices[0], &numDevices, sizeof(RAWINPUTDEVICELIST));
if (ret != (UINT)-1) {
devices.resize(ret);
break;
}
else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
devices.resize(numDevices);
}
else {
return false;
}
}
for (RAWINPUTDEVICELIST dev : devices) {
RID_DEVICE_INFO info;
info.cbSize = sizeof(RID_DEVICE_INFO);
UINT size = sizeof(RID_DEVICE_INFO);
if (GetRawInputDeviceInfoW(dev.hDevice, RIDI_DEVICEINFO, &info, &size) == (UINT)-1) {
continue;
}
if (info.dwType == RIM_TYPEHID &&
info.hid.usUsagePage == HID_USAGE_PAGE_DIGITIZER &&
info.hid.usUsage == HID_USAGE_DIGITIZER_TOUCH_PAD) {
device_info& info = GetDeviceInfo(dev.hDevice);
if (!info.contactInfo.empty()) {
debugf("Detected touchpad with handle %p, %zu", dev.hDevice, info.contactInfo.size());
return true;
}
else
break;
}
}
return false;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg)
{
case WMAPP_NOTIFYCALLBACK:
if (LOWORD(lParam) == WM_CONTEXTMENU)
{
POINT const pt = { LOWORD(wParam), HIWORD(wParam) };
ShowContextMenu(hwnd, pt);
}
break;
case WM_INPUT:
HandleRawInput(&wParam, &lParam);
break;
case WM_COMMAND:
if (LOWORD(wParam) == ID_EXIT_EXIT)
Clean();
break;
case WM_DESTROY:
Clean();
break;
default:
return DefWindowProc(hwnd, Msg, wParam, lParam);
}
return 0;
}
static void
StartDebugMode()
{
#if DEBUG_MODE
FreeConsole();
AllocConsole();
#pragma warning(push)
#pragma warning(disable:4996)
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
#pragma warning(pop)
#endif
}
int APIENTRY wWinMain(_In_ HINSTANCE _hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
hInstance = _hInstance;
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = "UWU_CLASS";
if (RegisterClassEx(&wc)) {
hwnd = CreateWindowEx(0, "UWU_CLASS", "UWU", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
StartDebugMode();
if (!HasPrecisionTouchpad()) {
debugf("No precision touchpad detected");
MessageBox(NULL, "No precision touchpad detected", "TouchpadTablet", MB_OK | MB_ICONERROR);
Clean();
}
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); // Reduce input lag
AddNotificationIcon();
ReadConfig();
ReadCalibration();
RegisterTouchpadInput();
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}