MCPcopy Create free account
hub / github.com/cafali/SnapKey / main

Function main

SnapKey.cpp:102–159  ·  view source on GitHub ↗

Main entry

Source from the content-addressed store, hash-verified

100
101// Main entry
102int main() {
103 if (!LoadConfig("config.cfg")) {
104 return 1;
105 }
106
107 hMutex = CreateMutex(NULL, TRUE, TEXT("SnapKeyMutex"));
108 if (GetLastError() == ERROR_ALREADY_EXISTS) {
109 MessageBox(NULL, TEXT("SnapKey is already running!"), TEXT("SnapKey"), MB_ICONINFORMATION | MB_OK);
110 return 1;
111 }
112
113 WNDCLASSEX wc = {0};
114 wc.cbSize = sizeof(WNDCLASSEX);
115 wc.lpfnWndProc = WndProc;
116 wc.hInstance = GetModuleHandle(NULL);
117 wc.lpszClassName = TEXT("SnapKeyClass");
118
119 if (!RegisterClassEx(&wc)) {
120 MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error"), MB_ICONEXCLAMATION | MB_OK);
121 ReleaseMutex(hMutex);
122 CloseHandle(hMutex);
123 return 1;
124 }
125
126 HWND hwnd = CreateWindowEx(0, wc.lpszClassName, TEXT("SnapKey"), WS_OVERLAPPEDWINDOW,
127 CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
128 NULL, NULL, wc.hInstance, NULL);
129
130 if (hwnd == NULL) {
131 MessageBox(NULL, TEXT("Window Creation Failed!"), TEXT("Error"), MB_ICONEXCLAMATION | MB_OK);
132 ReleaseMutex(hMutex);
133 CloseHandle(hMutex);
134 return 1;
135 }
136
137 InitNotifyIconData(hwnd);
138
139 hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, 0);
140 if (hHook == NULL) {
141 MessageBox(NULL, TEXT("Failed to install hook!"), TEXT("Error"), MB_ICONEXCLAMATION | MB_OK);
142 ReleaseMutex(hMutex);
143 CloseHandle(hMutex);
144 return 1;
145 }
146
147 MSG msg;
148 while (GetMessage(&msg, NULL, 0, 0)) {
149 TranslateMessage(&msg);
150 DispatchMessage(&msg);
151 }
152
153 UnhookWindowsHookEx(hHook);
154 Shell_NotifyIcon(NIM_DELETE, &nid);
155 ReleaseMutex(hMutex);
156 CloseHandle(hMutex);
157
158 return 0;
159}

Callers

nothing calls this directly

Calls 2

LoadConfigFunction · 0.85
InitNotifyIconDataFunction · 0.85

Tested by

no test coverage detected