forked from nir9/lightwm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wm.c
149 lines (116 loc) · 2.76 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
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
#include <Windows.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include "tiling.h"
#include "error.h"
#include "keyboard.h"
#include "config.h"
#include "messages.h"
#include "debug.h"
HMODULE wmDll;
HHOOK hookShellProcHandle;
void cleanupObjects() {
cleanupKeyboard();
cleanupConfigReader();
if (hookShellProcHandle) {
UnhookWindowsHookEx(hookShellProcHandle);
}
if (wmDll) {
FreeLibrary(wmDll);
}
}
void ctrlc(int sig) {
cleanupObjects();
puts("Exiting");
exit(ERROR_SUCCESS);
}
LPVOID createAddressSharedMemory() {
// Create a shared memory region
HANDLE hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
sizeof(DWORD),
"lightwmthreadid"
);
if (hMapFile == NULL) {
DEBUG_PRINT("Could not create file mapping object (%lu).", GetLastError());
return NULL;
}
LPVOID lpMapAddress = MapViewOfFile(
hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
sizeof(DWORD)
);
if (lpMapAddress == NULL) {
DEBUG_PRINT("Could not map view of file (%lu).", GetLastError());
CloseHandle(hMapFile);
return NULL;
}
return lpMapAddress;
}
int main() {
SetProcessDPIAware();
//Create shared memory
LPVOID sharedMemoryAddress = createAddressSharedMemory();
if(sharedMemoryAddress == NULL) {
reportWin32Error(L"Create Shared Memory");
goto cleanup;
}
DWORD dwThreadId = GetCurrentThreadId();
DEBUG_PRINT("Lightwm.exe thread id: %lu", dwThreadId);
CopyMemory((PVOID)sharedMemoryAddress, &dwThreadId, sizeof(DWORD));
wmDll = LoadLibraryW(L"lightwm_dll");
if (wmDll == NULL) {
reportWin32Error(L"LoadLibrary of wm_dll");
return ERROR_MOD_NOT_FOUND;
}
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;
}
signal(SIGINT, ctrlc);
if(!loadConfigFile(NULL))
{
reportWin32Error(L"Load config file");
goto cleanup;
}
if(!initializeKeyboardConfig(getConfigItems()))
{
reportWin32Error(L"Setup keyboard config");
goto cleanup;
}
// Handle a message loop
tileWindows();
MSG msg;
while (GetMessage(&msg, (HWND)-1, 0, 0) != 0) {
switch(msg.message)
{
case WM_HOTKEY:
const LRESULT ret = handleHotkey(msg.wParam, msg.lParam);
if(ret != ERROR_SUCCESS) {
DEBUG_PRINT("HotKey was unhandled! Ret: %lli", ret);
}
break;
case LWM_WINDOW_EVENT:
tileWindows();
DEBUG_PRINT("LWM_WINDOW_EVENT Message handled");
break;
default:
break;
}
}
cleanup:
cleanupObjects();
return EXIT_FAILURE;
}