-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynthPointer.c
168 lines (132 loc) · 5.09 KB
/
SynthPointer.c
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
#define _WIN32_WINNT_WIN10
#define NTDDI_VERSION NTDDI_WIN10_RS5
#include "SynthPointer.h"
#include <stdio.h>
#include <windows.h>
typedef enum {
PEN_STATE_MASK = (POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_DOWN | POINTER_FLAG_UP | POINTER_FLAG_UPDATE),
PEN_HOVER = (POINTER_FLAG_INRANGE | POINTER_FLAG_UPDATE),
PEN_DOWN = (POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_DOWN),
PEN_CONTACT = (POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_UPDATE),
PEN_UP = (POINTER_FLAG_INRANGE | POINTER_FLAG_UP),
PEN_ENDHOVER = (POINTER_FLAG_UPDATE),
} PEN_STATES;
HSYNTHETICPOINTERDEVICE CreateSynthPointer() {
HSYNTHETICPOINTERDEVICE device = CreateSyntheticPointerDevice(PT_PEN, 1, POINTER_FEEDBACK_DEFAULT);
if (device == NULL) {
_handleError();
}
return device;
}
POINTER_TYPE_INFO *GetDefaultPointerTypeInfo() {
POINTER_TYPE_INFO *typeInfo = (POINTER_TYPE_INFO *)malloc(sizeof(POINTER_TYPE_INFO));
POINTER_PEN_INFO penInfo = {0};
POINTER_INFO info = {0};
info.pointerType = PT_PEN;
penInfo.pointerInfo = info;
typeInfo->type = PT_PEN;
typeInfo->penInfo = penInfo;
return typeInfo;
}
void HoverMove(HSYNTHETICPOINTERDEVICE device, POINTER_TYPE_INFO *info, float x, float y, int screenId, BOOL buttonPressed) {
DEVMODE devMode;
devMode.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
int screenWidth = devMode.dmPelsWidth;
int screenHeight = devMode.dmPelsHeight;
int screenX = (int)(x * screenWidth);
int screenY = (int)(y * screenHeight);
POINT global = _ScreenToGlobal(screenX, screenY, screenId);
info->penInfo.pointerInfo.ptPixelLocation.x = global.x;
info->penInfo.pointerInfo.ptPixelLocation.y = global.y;
info->penInfo.pointerInfo.pointerFlags = PEN_HOVER;
info->penInfo.penFlags = (buttonPressed) ? PEN_FLAG_BARREL : PEN_FLAG_NONE;
_injectPointer(device, info);
}
void ContactMove(HSYNTHETICPOINTERDEVICE device, POINTER_TYPE_INFO *info, float x, float y, int screenId, BOOL buttonPressed, UINT32 pressure) {
DEVMODE devMode;
devMode.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
int screenWidth = devMode.dmPelsWidth;
int screenHeight = devMode.dmPelsHeight;
int screenX = (int)(x * screenWidth);
int screenY = (int)(y * screenHeight);
POINT global = _ScreenToGlobal(screenX, screenY, screenId);
info->penInfo.pointerInfo.ptPixelLocation.x = global.x;
info->penInfo.pointerInfo.ptPixelLocation.y = global.y;
info->penInfo.pointerInfo.pointerFlags = PEN_CONTACT;
info->penInfo.penFlags = (buttonPressed) ? PEN_FLAG_BARREL : PEN_FLAG_NONE;
info->penInfo.penMask = PEN_MASK_PRESSURE;
info->penInfo.pressure = pressure;
_injectPointer(device, info);
}
void HoverExit(HSYNTHETICPOINTERDEVICE device, POINTER_TYPE_INFO *info) {
info->penInfo.pointerInfo.pointerFlags = PEN_ENDHOVER;
_injectPointer(device, info);
}
void Down(HSYNTHETICPOINTERDEVICE device, POINTER_TYPE_INFO *info) {
info->penInfo.pointerInfo.pointerFlags = PEN_DOWN;
_injectPointer(device, info);
}
void Up(HSYNTHETICPOINTERDEVICE device, POINTER_TYPE_INFO *info) {
info->penInfo.pointerInfo.pointerFlags = PEN_UP;
_injectPointer(device, info);
}
void ScreenCount(int *count) {
*count = GetSystemMetrics(SM_CMONITORS);
}
typedef struct {
int screenID;
POINT offset;
} _ScreenData;
static int _CurrentEnumScreen = 0;
static POINT _globalOrigin = {0, 0};
BOOL CALLBACK _MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
_ScreenData *data = (_ScreenData *)dwData;
if (lprcMonitor->left < _globalOrigin.x) {
_globalOrigin.x = lprcMonitor->left;
}
if (lprcMonitor->top < _globalOrigin.y) {
_globalOrigin.y = lprcMonitor->top;
}
if (_CurrentEnumScreen == data->screenID) {
data->offset.x = lprcMonitor->left;
data->offset.y = lprcMonitor->top;
};
_CurrentEnumScreen++;
return TRUE;
}
POINT _ScreenToGlobal(int screenX, int screenY, int screen) {
if (screen < 0 || screen >= GetSystemMetrics(SM_CMONITORS)) {
POINT p = {screenX, screenY};
return p;
}
_globalOrigin.x = 0;
_globalOrigin.y = 0;
_CurrentEnumScreen = 0;
_ScreenData data = {screen, {0, 0}};
data.screenID = screen;
EnumDisplayMonitors(NULL, NULL, _MonitorEnumProc, (LPARAM)&data);
POINT ret = {screenX + data.offset.x - _globalOrigin.x, screenY + data.offset.y - _globalOrigin.y};
return ret;
}
void _injectPointer(HSYNTHETICPOINTERDEVICE device, POINTER_TYPE_INFO *info) {
if (!InjectSyntheticPointerInput(device, info, 1)) {
_handleError();
}
}
void _handleError() {
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
printf("Error %lu: %s\n", dw, (char *)lpMsgBuf);
LocalFree(lpMsgBuf);
}