-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwm.c
100 lines (76 loc) · 2.04 KB
/
wm.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
#include <windows.h>
#include "tiling.h"
#include "error.h"
#include "config.h"
#include "keyboard.h"
#include "messages.h"
#include "shared_mem.h"
#define EXIT_OK 0
#define EXIT_FAILED 1
int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prevInstance, PWSTR cmdLine, int cmdShow)
{
int exitCode = EXIT_FAILED;
HMODULE wmDll;
HHOOK hookShellProcHandle;
HANDLE currentlyRunningMutex = CreateMutexW(NULL, TRUE, L"Global\\LightWMIsCurrentlyRunning");
if (currentlyRunningMutex == NULL) {
reportWin32Error(L"Failed creating currently running mutex");
goto cleanup;
} else if (GetLastError() == ERROR_ALREADY_EXISTS) {
reportGeneralError(L"LightWM is already running, exiting");
goto cleanup;
}
SetProcessDPIAware();
if (!initializeKeyboardConfig()) {
reportGeneralError(L"Setup keyboard config");
goto cleanup;
}
if (!storeDwordInSharedMemory(GetCurrentThreadId())) {
reportGeneralError(L"Failed writing thread id to shared memory");
goto cleanup;
}
wmDll = LoadLibraryW(L"lightwm_dll");
if (wmDll == NULL) {
reportWin32Error(L"LoadLibrary of lightwm_dll");
goto cleanup;
}
FARPROC shellProc = GetProcAddress(wmDll, "ShellProc");
if (shellProc == NULL) {
reportWin32Error(L"GetProcAddress for ShellProc");
goto cleanup;
}
hookShellProcHandle = SetWindowsHookExW(WH_SHELL, (HOOKPROC)shellProc, wmDll, 0);
if (hookShellProcHandle == NULL) {
reportWin32Error(L"SetWindowsHookExW for shell hook");
goto cleanup;
}
tileWindows();
MSG msg;
while (GetMessage(&msg, (HWND)-1, 0, 0) != 0) {
switch (msg.message) {
case WM_HOTKEY:
if (msg.wParam == QUIT_LIGHTWM_HOTKEY_ID) {
exitCode = EXIT_OK;
goto cleanup;
}
handleHotkey(msg.wParam, msg.lParam);
break;
case LWM_WINDOW_EVENT:
tileWindows();
break;
}
}
cleanup:
if (currentlyRunningMutex != NULL) {
CloseHandle(currentlyRunningMutex);
}
cleanupKeyboard();
if (hookShellProcHandle) {
UnhookWindowsHookEx(hookShellProcHandle);
}
if (wmDll) {
FreeLibrary(wmDll);
}
cleanupMemoryMapFile();
return exitCode;
}