forked from misterchaos/hotcorner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotcorner.c
340 lines (282 loc) · 9.76 KB
/
hotcorner.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
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
#define WIN32_LEAN_AND_MEAN
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "USER32")
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")
#define KEYDOWN(k) ((k)&0x80)
// This is a **very** minimal hotcorner app, written in C. Maybe its not the
// optimal way to do this, but it works for me.
//
// Zero state is stored anywhere, no registry keys or configuration files.
//
// - If you want to configure something, edit the code.
// - If you want to uninstall it, just delete it.
//
// Tavis Ormandy <[email protected]> December, 2016
//
// https://github.com/taviso/hotcorner
//
// If the mouse enters this rectangle, activate the hot corner function.
// There are some hints about changing corners here
// https://github.com/taviso/hotcorner/issues/7#issuecomment-269367351
static const RECT kHotCorner_topleft = {
.top = -20,
.left = -20,
.right = +20,
.bottom = +20,
};
static RECT kHotCorner_downleft;
static RECT kHotCorner_downRight;
// Input to inject when corner activated (Win+Tab by default).
static const INPUT kCornerInput_topleft[] = {
{INPUT_KEYBOARD, .ki = {VK_LWIN, .dwFlags = 0}},
{INPUT_KEYBOARD, .ki = {VK_TAB, .dwFlags = 0}},
{INPUT_KEYBOARD, .ki = {VK_TAB, .dwFlags = KEYEVENTF_KEYUP}},
{INPUT_KEYBOARD, .ki = {VK_LWIN, .dwFlags = KEYEVENTF_KEYUP}},
};
static const INPUT kCornerInput_downleft[] = {
{INPUT_KEYBOARD, .ki = {VK_LCONTROL, .dwFlags = 0}},
{INPUT_KEYBOARD, .ki = {VK_LWIN, .dwFlags = 0}},
{INPUT_KEYBOARD, .ki = {VK_LEFT, .dwFlags = 0}},
{INPUT_KEYBOARD, .ki = {VK_LCONTROL, .dwFlags = KEYEVENTF_KEYUP}},
{INPUT_KEYBOARD, .ki = {VK_LWIN, .dwFlags = KEYEVENTF_KEYUP}},
{INPUT_KEYBOARD, .ki = {VK_LEFT, .dwFlags = KEYEVENTF_KEYUP}},
};
static const INPUT kCornerInput_downRight[] = {
{INPUT_KEYBOARD, .ki = {VK_LCONTROL, .dwFlags = 0}},
{INPUT_KEYBOARD, .ki = {VK_LWIN, .dwFlags = 0}},
{INPUT_KEYBOARD, .ki = {VK_RIGHT, .dwFlags = 0}},
{INPUT_KEYBOARD, .ki = {VK_LCONTROL, .dwFlags = KEYEVENTF_KEYUP}},
{INPUT_KEYBOARD, .ki = {VK_LWIN, .dwFlags = KEYEVENTF_KEYUP}},
{INPUT_KEYBOARD, .ki = {VK_RIGHT, .dwFlags = KEYEVENTF_KEYUP}},
};
// activeHotCorner will be kCornerInput_topleft or kCornerInput_downleft
static INPUT **activekCornerInput;
// How long cursor has to linger in the kHotCorner RECT to trigger input.
static const DWORD kHotDelay = 300;
// You can exit the application using the hot key CTRL+ALT+C by default, if it
// interferes with some application you're using (e.g. a full screen game).
static const DWORD kHotKeyModifiers = MOD_CONTROL | MOD_ALT;
static const DWORD kHotKey = 'C';
static HANDLE CornerThread = INVALID_HANDLE_VALUE;
// This thread runs when the cursor enters the hot corner, and waits to see if the cursor stays in the corner.
// If the mouse leaves while we're waiting, the thread is just terminated.
static DWORD WINAPI CornerHotFunc_TopLeft(LPVOID lpParameter)
{
BYTE KeyState[256];
POINT Point;
Sleep(kHotDelay);
// Check if a mouse putton is pressed, maybe a drag operation?
if (GetKeyState(VK_LBUTTON) < 0 || GetKeyState(VK_RBUTTON) < 0)
{
return 0;
}
// Check if any modifier keys are pressed.
if (GetKeyboardState(KeyState))
{
if (KEYDOWN(KeyState[VK_SHIFT]) || KEYDOWN(KeyState[VK_CONTROL]) || KEYDOWN(KeyState[VK_MENU]) || KEYDOWN(KeyState[VK_LWIN]) || KEYDOWN(KeyState[VK_RWIN]))
{
return 0;
}
}
// Verify the corner is still hot
if (GetCursorPos(&Point) == FALSE)
{
return 1;
}
// Check co-ordinates.
if (PtInRect(&kHotCorner_topleft, Point))
{
#pragma warning(suppress : 4090)
if (SendInput(_countof(kCornerInput_topleft), kCornerInput_topleft, sizeof(INPUT)) != _countof(kCornerInput_topleft))
{
return 1;
}
}
return 0;
}
// This thread runs when the cursor enters the hot corner, and waits to see if the cursor stays in the corner.
// If the mouse leaves while we're waiting, the thread is just terminated.
static DWORD WINAPI CornerHotFunc_DownLeft(LPVOID lpParameter)
{
BYTE KeyState[256];
POINT Point;
Sleep(10);
// Check if a mouse putton is pressed, maybe a drag operation?
if (GetKeyState(VK_LBUTTON) < 0 || GetKeyState(VK_RBUTTON) < 0)
{
return 0;
}
// Check if any modifier keys are pressed.
if (GetKeyboardState(KeyState))
{
if (KEYDOWN(KeyState[VK_SHIFT]) || KEYDOWN(KeyState[VK_CONTROL]) || KEYDOWN(KeyState[VK_MENU]) || KEYDOWN(KeyState[VK_LWIN]) || KEYDOWN(KeyState[VK_RWIN]))
{
return 0;
}
}
// Verify the corner is still hot
if (GetCursorPos(&Point) == FALSE)
{
return 1;
}
#pragma warning(suppress : 4090)
if (SendInput(_countof(kCornerInput_downleft), kCornerInput_downleft, sizeof(INPUT)) != _countof(kCornerInput_downleft))
{
return 1;
}
return 0;
}
// This thread runs when the cursor enters the hot corner, and waits to see if the cursor stays in the corner.
// If the mouse leaves while we're waiting, the thread is just terminated.
static DWORD WINAPI CornerHotFunc_DownRight(LPVOID lpParameter)
{
BYTE KeyState[256];
POINT Point;
Sleep(10);
// Check if a mouse button is pressed, maybe a drag operation?
if (GetKeyState(VK_LBUTTON) < 0 || GetKeyState(VK_RBUTTON) < 0)
{
return 0;
}
// Check if any modifier keys are pressed.
if (GetKeyboardState(KeyState))
{
if (KEYDOWN(KeyState[VK_SHIFT]) || KEYDOWN(KeyState[VK_CONTROL]) || KEYDOWN(KeyState[VK_MENU]) || KEYDOWN(KeyState[VK_LWIN]) || KEYDOWN(KeyState[VK_RWIN]))
{
return 0;
}
}
// Verify the corner is still hot
if (GetCursorPos(&Point) == FALSE)
{
return 1;
}
#pragma warning(suppress : 4090)
if (SendInput(_countof(kCornerInput_downRight), kCornerInput_downRight, sizeof(INPUT)) != _countof(kCornerInput_downRight))
{
return 1;
}
return 0;
}
static LRESULT CALLBACK MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
MSLLHOOKSTRUCT *evt = (MSLLHOOKSTRUCT *)lParam;
// If the mouse hasn't moved, we're done.
if (wParam != WM_MOUSEMOVE)
goto finish;
// Check if the cursor is hot or cold.
if (!(PtInRect(&kHotCorner_topleft, evt->pt) || PtInRect(&kHotCorner_downleft, evt->pt) || PtInRect(&kHotCorner_downRight, evt->pt)))
{
// The corner is cold, and was cold before.
if (CornerThread == INVALID_HANDLE_VALUE)
goto finish;
// The corner is cold, but was previously hot.
TerminateThread(CornerThread, 0);
CloseHandle(CornerThread);
// Reset state.
CornerThread = INVALID_HANDLE_VALUE;
goto finish;
}
// The corner is hot, check if it was already hot.
if (CornerThread != INVALID_HANDLE_VALUE)
{
goto finish;
}
// Check if a mouse button is pressed, maybe a drag operation?
if (GetKeyState(VK_LBUTTON) < 0 || GetKeyState(VK_RBUTTON) < 0)
{
goto finish;
}
// select activekCornerInput
if (PtInRect(&kHotCorner_topleft, evt->pt))
{
// The corner is hot, and was previously cold. Here we start a thread to
// monitor if the mouse lingers.
printf("top left activated\n");
CornerThread = CreateThread(NULL, 0, CornerHotFunc_TopLeft, NULL, 0, NULL);
}
else if (PtInRect(&kHotCorner_downleft, evt->pt))
{
// The corner is hot, and was previously cold. Here we start a thread to
// monitor if the mouse lingers.
printf("down left activated\n");
CornerThread = CreateThread(NULL, 0, CornerHotFunc_DownLeft, NULL, 0, NULL);
}
else if (PtInRect(&kHotCorner_downRight, evt->pt))
{
// The corner is hot, and was previously cold. Here we start a thread to
// monitor if the mouse lingers.
printf("down right activated\n");
CornerThread = CreateThread(NULL, 0, CornerHotFunc_DownRight, NULL, 0, NULL);
}
finish:
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int getConfigY()
{
FILE *fp;
fp = fopen(".\\configy.txt", "r");
if (fp == NULL)
{
return GetSystemMetrics(SM_CYSCREEN);
}
int screenY;
fscanf(fp, "%d", &screenY);
fclose(fp);
if (screenY <= 0)
{
return GetSystemMetrics(SM_CYSCREEN);
}
return screenY;
}
int getConfigX()
{
FILE *fp;
fp = fopen(".\\configx.txt", "r");
if (fp == NULL)
{
return GetSystemMetrics(SM_CXSCREEN);
}
int screenX;
fscanf(fp, "%d", &screenX);
printf("x = %d", screenX);
fclose(fp);
if (screenX <= 0)
{
printf("te %d", GetSystemMetrics(SM_CXSCREEN));
return GetSystemMetrics(SM_CXSCREEN);
}
return screenX;
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
HHOOK MouseHook;
// get screen size y
kHotCorner_downleft.bottom = getConfigY();
kHotCorner_downleft.top = kHotCorner_downleft.bottom - 20;
kHotCorner_downleft.bottom += 20;
kHotCorner_downleft.left = -20;
kHotCorner_downleft.right = +20;
// get screen size x
kHotCorner_downRight.bottom = getConfigY();
kHotCorner_downRight.top = kHotCorner_downRight.bottom - 20;
kHotCorner_downRight.bottom += 20;
kHotCorner_downRight.left = getConfigX() - 20;
kHotCorner_downRight.right = getConfigX() + 20;
if (!(MouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, NULL, 0)))
return 1;
RegisterHotKey(NULL, 1, kHotKeyModifiers, kHotKey);
while (GetMessage(&Msg, NULL, 0, 0))
{
if (Msg.message == WM_HOTKEY)
{
break;
}
DispatchMessage(&Msg);
}
UnhookWindowsHookEx(MouseHook);
return Msg.wParam;
}