forked from nir9/lightwm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.c
113 lines (97 loc) · 3.26 KB
/
keyboard.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
#include <Windows.h>
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
#include "keyboard.h"
#include "debug.h"
UINT getModifier(const char *value);
UINT getKeyCode(const char *value);
void addKeyboardKeybind(enum Action action, UINT modifier, UINT keyCode);
BOOL initializeKeyboardConfig(const ConfigItems *configItems) {
assert(configItems != NULL);
assert(configItems->configItem != NULL);
assert(configItems->configItemsCount != 0);
for (size_t i = 1; i <= configItems->configItemsCount; i++) {
ADD_KEYBOARD_KEYBIND(i, WORKSPACE_1);
ADD_KEYBOARD_KEYBIND(i, WORKSPACE_2);
ADD_KEYBOARD_KEYBIND(i, WORKSPACE_3);
ADD_KEYBOARD_KEYBIND(i, WORKSPACE_4);
ADD_KEYBOARD_KEYBIND(i, WINDOW_UP);
ADD_KEYBOARD_KEYBIND(i, WINDOW_DOWN);
ADD_KEYBOARD_KEYBIND(i, WINDOW_LEFT);
ADD_KEYBOARD_KEYBIND(i, WINDOW_RIGHT);
}
return TRUE;
}
void cleanupKeyboard() {
UnregisterHotKey(NULL, WORKSPACE_1);
UnregisterHotKey(NULL, WORKSPACE_2);
UnregisterHotKey(NULL, WORKSPACE_3);
UnregisterHotKey(NULL, WORKSPACE_4);
UnregisterHotKey(NULL, WINDOW_UP);
UnregisterHotKey(NULL, WINDOW_DOWN);
UnregisterHotKey(NULL, WINDOW_LEFT);
UnregisterHotKey(NULL, WINDOW_RIGHT);
DEBUG_PRINT("Unregistered all hotkeys");
}
UINT getModifier(const char *value) {
if (strncmp(value, "alt", 3) == 0) {
return MOD_ALT;
}
if (strncmp(value, "win", 3) == 0) {
return MOD_WIN;
}
if (strncmp(value, "ctrl", 4) == 0) {
return MOD_CONTROL;
}
if (strncmp(value, "shift", 5) == 0) {
return MOD_SHIFT;
}
return MOD_ALT; //TODO Throw an error here
}
UINT getKeyCode(const char *value) {
DEBUG_PRINT("GetKeyCode char value '%c'", value[strlen(value) - 1]);
return VkKeyScanEx(value[strlen(value) - 1], GetKeyboardLayout(0));
}
void addKeyboardKeybind(enum Action action, UINT modifier, UINT keyCode) {
if (!RegisterHotKey(NULL, action, modifier | MOD_NOREPEAT, keyCode)) {
if (GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED) {
puts("Warn: Hotkey already registerd\n");
return;
}
MessageBox(NULL, "Failed to register hotkey.", "Error", MB_OK | MB_ICONERROR);
}
DEBUG_PRINT("Registered %s hotkey", ACTION_STRINGS[action]);
}
LRESULT handleHotkey(WPARAM wparam, LPARAM lparam) {
switch (wparam) {
case WORKSPACE_1:
DEBUG_PRINT("Switch to workspace 1");
break;
case WORKSPACE_2:
DEBUG_PRINT("Switch to workspace 2");
break;
case WORKSPACE_3:
DEBUG_PRINT("Switch to workspace 3");
break;
case WORKSPACE_4:
DEBUG_PRINT("Switch to workspace 4");
break;
case WINDOW_UP:
DEBUG_PRINT("Highlight window above");
break;
case WINDOW_DOWN:
DEBUG_PRINT("Highlight window below");
break;
case WINDOW_LEFT:
DEBUG_PRINT("Highlight window left");
break;
case WINDOW_RIGHT:
DEBUG_PRINT("Highlight window right");
break;
default:
DEBUG_PRINT("Unhandled hotkey message! Hotkey ID: %lli", wparam);
break;
}
return ERROR_SUCCESS;
}