forked from henkman/virgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virgo.c
393 lines (361 loc) · 9.42 KB
/
virgo.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
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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#define sb_free(a) ((a) ? HeapFree(GetProcessHeap(), 0, stb__sbraw(a)), 0 : 0)
#define sb_push(a, v) (stb__sbmaybegrow(a, 1), (a)[stb__sbn(a)++] = (v))
#define sb_count(a) ((a) ? stb__sbn(a) : 0)
#define stb__sbraw(a) ((int *)(a)-2)
#define stb__sbm(a) stb__sbraw(a)[0]
#define stb__sbn(a) stb__sbraw(a)[1]
#define stb__sbneedgrow(a, n) ((a) == 0 || stb__sbn(a) + (n) >= stb__sbm(a))
#define stb__sbmaybegrow(a, n) (stb__sbneedgrow(a, (n)) ? stb__sbgrow(a, n) : 0)
#define stb__sbgrow(a, n) ((a) = stb__sbgrowf((a), (n), sizeof(*(a))))
#ifndef MOD_NOREPEAT
#define MOD_NOREPEAT 0x4000
#endif
#define NUM_DESKTOPS 4
#define NUM_PINNED 8
typedef struct {
HWND *windows;
unsigned count;
HWND focus;
} Windows;
typedef struct {
NOTIFYICONDATA nid;
HBITMAP hBitmap;
HFONT hFont;
HWND hwnd;
HDC mdc;
unsigned bitmapWidth;
} Trayicon;
typedef struct {
unsigned current;
unsigned handle_hotkeys;
Windows desktops[NUM_DESKTOPS];
Trayicon trayicon;
HWND pinned[NUM_PINNED];
} Virgo;
static void save_current_desktops_focus(Windows* desktops) {
desktops->focus = GetForegroundWindow();
}
static unsigned is_valid_window(HWND hwnd);
static void restore_current_desktops_focus(Windows* desktops) {
if (!desktops->focus || !is_valid_window(desktops->focus)) {
return;
}
SetForegroundWindow(desktops->focus);
}
static void *stb__sbgrowf(void *arr, unsigned increment, unsigned itemsize)
{
unsigned dbl_cur = arr ? 2 * stb__sbm(arr) : 0;
unsigned min_needed = sb_count(arr) + increment;
unsigned m = dbl_cur > min_needed ? dbl_cur : min_needed;
unsigned *p;
if (arr) {
p = HeapReAlloc(GetProcessHeap(), 0, stb__sbraw(arr),
itemsize * m + sizeof(unsigned) * 2);
} else {
p = HeapAlloc(GetProcessHeap(), 0, itemsize * m + sizeof(unsigned) * 2);
}
if (p) {
if (!arr) {
p[1] = 0;
}
p[0] = m;
return p + 2;
} else {
ExitProcess(1);
return (void *)(2 * sizeof(unsigned));
}
}
static HICON trayicon_draw(Trayicon *t, char *text, unsigned len)
{
ICONINFO iconInfo;
HBITMAP hOldBitmap;
HFONT hOldFont;
hOldBitmap = (HBITMAP)SelectObject(t->mdc, t->hBitmap);
hOldFont = (HFONT)SelectObject(t->mdc, t->hFont);
TextOut(t->mdc, t->bitmapWidth / 4, 0, text, len);
SelectObject(t->mdc, hOldBitmap);
SelectObject(t->mdc, hOldFont);
iconInfo.fIcon = TRUE;
iconInfo.xHotspot = iconInfo.yHotspot = 0;
iconInfo.hbmMask = iconInfo.hbmColor = t->hBitmap;
return CreateIconIndirect(&iconInfo);
}
static void trayicon_init(Trayicon *t)
{
HDC hdc;
t->hwnd =
CreateWindowA("STATIC", "virgo", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
t->bitmapWidth = GetSystemMetrics(SM_CXSMICON);
t->nid.cbSize = sizeof(t->nid);
t->nid.hWnd = t->hwnd;
t->nid.uID = 100;
t->nid.uFlags = NIF_ICON;
hdc = GetDC(t->hwnd);
t->hBitmap = CreateCompatibleBitmap(hdc, t->bitmapWidth, t->bitmapWidth);
t->mdc = CreateCompatibleDC(hdc);
ReleaseDC(t->hwnd, hdc);
SetBkColor(t->mdc, RGB(0x00, 0x00, 0x00));
SetTextColor(t->mdc, RGB(0x00, 0xFF, 0x00));
t->hFont = CreateFont(-MulDiv(11, GetDeviceCaps(t->mdc, LOGPIXELSY), 72), 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("Arial"));
t->nid.hIcon = trayicon_draw(t, "1", 1);
Shell_NotifyIcon(NIM_ADD, &t->nid);
}
static void trayicon_set(Trayicon *t, unsigned number)
{
char snumber[2];
if (number > 9) {
return;
}
snumber[0] = number + '0';
snumber[1] = 0;
DestroyIcon(t->nid.hIcon);
t->nid.hIcon = trayicon_draw(t, snumber, 1);
Shell_NotifyIcon(NIM_MODIFY, &t->nid);
}
static void trayicon_deinit(Trayicon *t)
{
Shell_NotifyIcon(NIM_DELETE, &t->nid);
DestroyIcon(t->nid.hIcon);
DeleteObject(t->hBitmap);
DeleteObject(t->hFont);
DeleteDC(t->mdc);
DestroyWindow(t->hwnd);
}
static void windows_mod(Windows *wins, unsigned state)
{
unsigned i;
for (i = 0; i < wins->count; i++) {
ShowWindow(wins->windows[i], state);
}
}
static void windows_show(Windows *wins) { windows_mod(wins, SW_SHOW); }
static void windows_hide(Windows *wins) { windows_mod(wins, SW_HIDE); }
static void windows_add(Windows *wins, HWND hwnd)
{
if (wins->count >= sb_count(wins->windows)) {
sb_push(wins->windows, hwnd);
} else {
wins->windows[wins->count] = hwnd;
}
wins->count++;
}
static void windows_del(Windows *wins, HWND hwnd)
{
unsigned i, e;
for (i = 0; i < wins->count; i++) {
if (wins->windows[i] != hwnd) {
continue;
}
if (i != wins->count - 1) {
for (e = i; e < wins->count - 1; e++) {
wins->windows[e] = wins->windows[e + 1];
}
}
wins->count--;
break;
}
}
static unsigned is_valid_window(HWND hwnd)
{
WINDOWINFO wi;
wi.cbSize = sizeof(wi);
GetWindowInfo(hwnd, &wi);
return (wi.dwStyle & WS_VISIBLE) && !(wi.dwExStyle & WS_EX_TOOLWINDOW);
}
static void register_hotkey(unsigned id, unsigned mod, unsigned vk)
{
if (!RegisterHotKey(NULL, id, mod, vk)) {
MessageBox(NULL, "could not register hotkey", "error",
MB_ICONEXCLAMATION);
ExitProcess(1);
}
}
static unsigned virgo_is_pinned(Virgo *v, HWND hwnd) {
unsigned i;
for (i = 0; i < NUM_PINNED; ++i) {
if (v->pinned[i] == hwnd) {
return 1;
}
}
return 0;
}
static void virgo_toggle_pin(Virgo *v, HWND hwnd) {
unsigned i;
unsigned empty = NUM_PINNED;
for (i = 0; i < NUM_PINNED; ++i) {
if (v->pinned[i] == hwnd) {
v->pinned[i] = 0;
windows_add(&v->desktops[v->current], hwnd);
return;
}
if (!v->pinned[i]) {
empty = i;
}
}
if (empty == NUM_PINNED) {
MessageBox(NULL, "reached pinned windows limit", "error",
MB_ICONEXCLAMATION);
return;
}
v->pinned[empty] = hwnd;
windows_del(&v->desktops[v->current], hwnd);
}
static BOOL enum_func(HWND hwnd, LPARAM lParam)
{
unsigned i, e;
Virgo *v;
Windows *desk;
v = (Virgo *)lParam;
if (!is_valid_window(hwnd)) {
return 1;
}
for (i = 0; i < NUM_DESKTOPS; i++) {
desk = &(v->desktops[i]);
for (e = 0; e < desk->count; e++) {
if (desk->windows[e] == hwnd) {
return 1;
}
}
}
if (virgo_is_pinned(v, hwnd)) {
return 1;
}
windows_add(&(v->desktops[v->current]), hwnd);
return 1;
}
static void virgo_update(Virgo *v)
{
unsigned i, e;
Windows *desk;
HWND hwnd;
for (i = 0; i < NUM_DESKTOPS; i++) {
desk = &(v->desktops[i]);
for (e = 0; e < desk->count; e++) {
hwnd = desk->windows[e];
if (!GetWindowThreadProcessId(desk->windows[e], NULL)) {
windows_del(desk, hwnd);
}
}
}
for (i = 0; i < NUM_PINNED; i++) {
hwnd = v->pinned[i];
if (!GetWindowThreadProcessId(hwnd, NULL)) {
v->pinned[i] = 0;
}
}
desk = &v->desktops[v->current];
for (i = 0; i < desk->count; i++) {
hwnd = desk->windows[i];
if (!IsWindowVisible(hwnd)) {
windows_del(desk, hwnd);
}
}
EnumWindows((WNDENUMPROC)&enum_func, (LPARAM)v);
}
static void virgo_toggle_hotkeys(Virgo *v)
{
unsigned i;
v->handle_hotkeys = !v->handle_hotkeys;
if (v->handle_hotkeys) {
for (i = 0; i < NUM_DESKTOPS; i++) {
register_hotkey(i * 2, MOD_ALT | MOD_NOREPEAT, i + 1 + '0');
register_hotkey(i * 2 + 1, MOD_CONTROL | MOD_NOREPEAT, i + 1 + '0');
}
} else {
for (i = 0; i < NUM_DESKTOPS; i++) {
UnregisterHotKey(NULL, i * 2);
UnregisterHotKey(NULL, i * 2 + 1);
}
}
}
static void virgo_init(Virgo *v)
{
unsigned i;
v->handle_hotkeys = 1;
for (i = 0; i < NUM_DESKTOPS; i++) {
register_hotkey(i * 2, MOD_ALT | MOD_NOREPEAT, i + 1 + '0');
register_hotkey(i * 2 + 1, MOD_CONTROL | MOD_NOREPEAT, i + 1 + '0');
}
register_hotkey(i * 2, MOD_ALT | MOD_CONTROL | MOD_SHIFT | MOD_NOREPEAT, 'Q');
register_hotkey(i * 2 + 1, MOD_ALT | MOD_CONTROL | MOD_SHIFT | MOD_NOREPEAT, 'S');
register_hotkey(i * 2 + 2, MOD_ALT | MOD_CONTROL | MOD_SHIFT | MOD_NOREPEAT, 'T');
register_hotkey(i * 2 + 3, MOD_ALT | MOD_NOREPEAT, VK_LEFT);
register_hotkey(i * 2 + 4, MOD_ALT | MOD_NOREPEAT, VK_RIGHT);
trayicon_init(&v->trayicon);
}
static void virgo_deinit(Virgo *v)
{
unsigned i;
for (i = 0; i < NUM_DESKTOPS; i++) {
windows_show(&v->desktops[i]);
sb_free(v->desktops[i].windows);
}
trayicon_deinit(&v->trayicon);
}
static void virgo_move_to_desk(Virgo *v, unsigned desk)
{
HWND hwnd;
if (v->current == desk) {
return;
}
virgo_update(v);
hwnd = GetForegroundWindow();
if (virgo_is_pinned(v, hwnd)) {
return;
}
if (!hwnd || !is_valid_window(hwnd)) {
return;
}
windows_del(&v->desktops[v->current], hwnd);
windows_add(&v->desktops[desk], hwnd);
ShowWindow(hwnd, SW_HIDE);
}
static void virgo_go_to_desk(Virgo *v, unsigned desk)
{
if (v->current == desk) {
return;
}
virgo_update(v);
save_current_desktops_focus(&v->desktops[v->current]);
windows_hide(&v->desktops[v->current]);
windows_show(&v->desktops[desk]);
restore_current_desktops_focus(&v->desktops[desk]);
v->current = desk;
trayicon_set(&v->trayicon, v->current + 1);
}
void __main(void) __asm__("__main");
void __main(void)
{
Virgo v = {0};
MSG msg;
virgo_init(&v);
while (GetMessage(&msg, NULL, 0, 0)) {
if (msg.message != WM_HOTKEY) {
continue;
}
if (msg.wParam == NUM_DESKTOPS * 2) {
break;
}
if (msg.wParam == NUM_DESKTOPS * 2 + 1) {
virgo_toggle_hotkeys(&v);
} else if (msg.wParam == NUM_DESKTOPS * 2 + 2) {
virgo_toggle_pin(&v, GetForegroundWindow());
} else if (msg.wParam == NUM_DESKTOPS * 2 + 3) {
unsigned target_desk = (v.current - 1) % NUM_DESKTOPS;
virgo_go_to_desk(&v, target_desk);
} else if (msg.wParam == NUM_DESKTOPS * 2 + 4) {
unsigned target_desk = (v.current + 1) % NUM_DESKTOPS;
virgo_go_to_desk(&v, target_desk);
} else if (msg.wParam % 2 == 0) {
virgo_go_to_desk(&v, msg.wParam / 2);
} else {
virgo_move_to_desk(&v, (msg.wParam - 1) / 2);
}
}
virgo_deinit(&v);
ExitProcess(0);
}