-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
175 lines (150 loc) · 4.74 KB
/
main.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
#include <windows.h>
#include <winuser.h>
#include <stdlib.h>
#include <stdio.h>
#include <shellapi.h>
#include <stdbool.h>
#define NAME "sxhkd-win32"
#define LENGTH(x) (sizeof x / sizeof x[0])
typedef union {
int i;
unsigned int ui;
float f;
void *v;
} Arg;
typedef struct {
unsigned int mod;
unsigned int key;
void (*func)(const Arg *);
const Arg arg;
} Key;
#include "debug.h"
#include "actions.h"
#include "config.h"
HWND dwmhwnd;
#ifdef VDA_FEATURES
HMODULE VDA;
typedef int(*GoToDesktopNumber)(int);
typedef int(*MoveWindowToDesktopNumber)(HWND, int);
typedef int(*GetCurrentDesktop)();
typedef int(*GetWindowDesktopNumber)(HWND);
typedef int(*GetCurrentDesktopNumber)();
typedef int(*IsWindowOnCurrentVirtualDesktop)(HWND);
GetCurrentDesktop GetCurrentDesktopFunc;
MoveWindowToDesktopNumber MoveWindowToDesktopNumberFunc;
GoToDesktopNumber GoToDesktopNumberFunc;
GetWindowDesktopNumber GetWindowDesktopNumberFunc;
GetCurrentDesktopNumber GetCurrentDesktopNumberFunc;
IsWindowOnCurrentVirtualDesktop IsWindowOnCurrentVirtualDesktopFunc;
HWND lastActive[10]; // 0 to 9 index
#endif
#include "actions.c"
void cleanup() {
for (int i = 0; i < LENGTH(keys); i++) UnregisterHotKey(dwmhwnd, i + 1);
DestroyWindow(dwmhwnd);
#ifdef VDA_FEATURES
FreeLibrary(VDA);
#endif
}
void die(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
dbg(errstr, ap);
va_end(ap);
cleanup();
exit(EXIT_FAILURE);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
break;
case WM_CLOSE:
cleanup();
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_HOTKEY:
if (wParam > 0 && wParam <= LENGTH(keys)) {
dbg("\nHotkey was pressed!\n");
keys[wParam - 1].func(&(keys[wParam - 1].arg));
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
void setup(HINSTANCE hInstance) {
WNDCLASSEX winClass;
winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = 0;
winClass.lpfnWndProc = WndProc;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
winClass.hInstance = hInstance;
winClass.hIcon = NULL;
winClass.hIconSm = NULL;
winClass.hCursor = NULL;
winClass.hbrBackground = NULL;
winClass.lpszMenuName = NULL;
winClass.lpszClassName = NAME;
if (!RegisterClassEx(&winClass)) die("Error registering window class\n");
dwmhwnd = CreateWindowEx(0, NAME, NAME, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, NULL);
if (!dwmhwnd) die("Error creating window\n");
for (int i = 0; i < LENGTH(keys); i++) RegisterHotKey(dwmhwnd, i + 1, keys[i].mod, keys[i].key);
}
#ifdef VDA_FEATURES
BOOL CALLBACK initEnumCall(HWND hwnd, LPARAM lParam) {
char windowTitle[256];
GetWindowTextA(hwnd, windowTitle, sizeof(windowTitle));
if (IsWindowVisible(hwnd) && windowTitle[0] != '\0') {
if(lastActive[GetWindowDesktopNumberFunc(hwnd)]!=NULL) return TRUE;
printf("windowTitle: %s %d\n", windowTitle, GetWindowDesktopNumberFunc(hwnd));
lastActive[GetWindowDesktopNumberFunc(hwnd)]=hwnd;
}
return TRUE;
}
HWND initLastActive(){
EnumWindows(initEnumCall, 0);
return topmost;
}
#endif
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
#ifdef DEBUG
extern char ** __argv;
extern int __argc;
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
freopen("CONOUT$", "wb", stdout);
freopen("CONOUT$", "wb", stderr);
}
#endif
dbg("Hello!! : ))\n");
#ifdef VDA_FEATURES
char* libName = "\\VirtualDesktopAccessor.dll";
TCHAR exeFullPath[MAX_PATH + 1] = { 0 };
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
char* lastSlash = strrchr(exeFullPath, '\\');
if (lastSlash != NULL) *(lastSlash + 1) = '\0';
strcat_s(exeFullPath, sizeof exeFullPath, libName);
HINSTANCE VDA = LoadLibrary(exeFullPath);
if (VDA == NULL) printf("Failed to load the DLL, VD Features will not work\n");
MoveWindowToDesktopNumberFunc = (MoveWindowToDesktopNumber)GetProcAddress(VDA, "MoveWindowToDesktopNumber");
GoToDesktopNumberFunc = (GoToDesktopNumber)GetProcAddress(VDA, "GoToDesktopNumber");
GetCurrentDesktopFunc = (GetCurrentDesktop)GetProcAddress(VDA, "GetCurrentDesktopNumber");
GetWindowDesktopNumberFunc = (GetWindowDesktopNumber)GetProcAddress(VDA, "GetWindowDesktopNumber");
GetCurrentDesktopNumberFunc = (GetCurrentDesktopNumber)GetProcAddress(VDA, "GetCurrentDesktopNumber");
IsWindowOnCurrentVirtualDesktopFunc = (IsWindowOnCurrentVirtualDesktop)GetProcAddress(VDA, "IsWindowOnCurrentVirtualDesktop");
for(int i=0; i<10; i++) lastActive[i]=NULL;
// init with topmost windows of all VDs is essential for when not launched at startup
initLastActive();
#endif
MSG msg;
setup(hInstance);
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
cleanup();
return msg.wParam;
}