/
hotkeyman.c
397 lines (351 loc) · 12 KB
/
hotkeyman.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*******************************************************************************
* hotkeyman
******************************************************************************/
#ifdef _PLAT_WNDS
#include <windows.h>
#else
#include "xhklib.h"
#include "wnds_keydefs.h"
#endif // _PLAT_WNDS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hotkeyman.h"
#include "hkutils.h"
#define HK_CONF_FILENAME "hotkeymanrc"
// #############################################################################
// Declarations
// #############################################################################
// Hotkey manager
struct HotkeyManager
{
hklist* hotkeys;
char* config_file_name;
int current_id;
#ifndef _PLAT_WNDS
xhkConfig* hkconfig;
#endif // not _PLAT_WNDS
bool quit_flag;
int hk_id_quit;
int hk_id_refresh;
};
char* hotkeymanager_get_config_path(const char* file_name);
#ifndef _PLAT_WNDS
void hotkeymanager_handle_hotkey_cb(xhkEvent event, void* arg1, void* arg2,
void* arg3);
#endif // _PLAT_WNDS
// #############################################################################
// Interface functions
// #############################################################################
// -----------------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------------
HotkeyManager* hotkeymanager_create()
{
HotkeyManager* hkman = (HotkeyManager*) malloc(sizeof(HotkeyManager));
hkman->hotkeys = hklist_create(1);
hkman->config_file_name = hotkeymanager_get_config_path(HK_CONF_FILENAME);
hkman->current_id = 0;
#ifndef _PLAT_WNDS
hkman->hkconfig = xhkInit(NULL);
#endif // not _PLAT_WNDS
hkman->quit_flag = false;
hkman->hk_id_quit = -1;
hkman->hk_id_refresh = -1;
return hkman;
}
// -----------------------------------------------------------------------------
// Destructor
// -----------------------------------------------------------------------------
void hotkeymanager_free(HotkeyManager* hkman)
{
free(hkman->config_file_name);
#ifndef _PLAT_WNDS
xhkClose(hkman->hkconfig);
#endif // not _PLAT_WNDS
hklist_destroy(hkman->hotkeys);
free(hkman);
}
// -----------------------------------------------------------------------------
// Process hotkeys
// -----------------------------------------------------------------------------
void hotkeymanager_process_hotkeys_internal(HotkeyManager* hkman, int hotkey_id)
{
int i = 0;
hklist* current_item = hkman->hotkeys;
hklog("Hotkey triggered (id: %d)\n", hotkey_id);
// iterate over list of registered hotkeys
do
{
if (hotkey_id == current_item->id)
{
hotkeymanager_handle_hotkey(hkman, current_item->id,
current_item->command);
// appropriate command found -> break
break;
}
// get next item
current_item = current_item->next;
} while (current_item);
}
// -----------------------------------------------------------------------------
// Process hotkeys
// -----------------------------------------------------------------------------
void hotkeymanager_process_hotkeys(HotkeyManager* hkman)
{
#ifdef _PLAT_WNDS
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.message == WM_HOTKEY)
hotkeymanager_process_hotkeys_internal(hkman, msg.wParam);
// exit hotkeyman if quit flag is true
if (hkman->quit_flag)
break;
}
#else
while (true)
{
xhkPollKeys(hkman->hkconfig, 1);
if (hkman->quit_flag)
break;
}
#endif // _PLAT_WNDS
}
// -----------------------------------------------------------------------------
// Handle hotkey
// -----------------------------------------------------------------------------
bool hotkeymanager_handle_hotkey(HotkeyManager* hkman, int hotkey_id,
char* command)
{
if (hotkey_id == hkman->hk_id_quit)
{
// quit hotkeyman
hkman->quit_flag = true;
hklog("Exit hotkeyman ...\n");
}
else if (hotkey_id == hkman->hk_id_refresh)
{
// refresh hotkeys
hklog("Refreshing hotkeys ...\n");
if (!hotkeymanager_refresh_hotkeys(hkman))
{
hklog("ERROR: Failed to refresh hotkeys!\n");
hklog(HK_ERR_TERMINATE);
hkman->quit_flag = true;
return false;
}
}
else
{
hklog("Running command: '%s'\n", command);
#ifdef _PLAT_WNDS
PROCESS_INFORMATION proc_info;
STARTUPINFO startup_info;
// initialize struct with 0
memset(&startup_info, 0, sizeof(STARTUPINFO));
// run command as new process
if (!CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL,
&startup_info, &proc_info))
hklog("ERROR: Creating process failed! (code: %d)\n", GetLastError());
#else
int exit_code = system(command);
if (exit_code != 0)
hklog("ERROR: Creating process failed! (code: %d)\n", exit_code);
#endif // _PLAT_WNDS
}
return true;
}
// -----------------------------------------------------------------------------
// Register hotkeys
// -----------------------------------------------------------------------------
bool hotkeymanager_register_hotkeys(HotkeyManager* hkman)
{
hklist* item = hkman->hotkeys;
do
{
// ignore hotkeys with id = -1
if (item->id != -1)
{
// register hotkey
#ifdef _PLAT_WNDS
if (RegisterHotKey(NULL, item->id, item->mod, item->vk))
#else
if (xhkBindKey(hkman->hkconfig, 0, item->vk, hkconvert(item->mod),
xhkKeyPress, &hotkeymanager_handle_hotkey_cb, hkman,
(void*) item->id, 0) == 0)
#endif // _PLAT_WNDS
hklog("Successfully registered hotkey (id: %d)\n", item->id);
else
{
hklog("WARNING: Unable to register hotkey (id: %d)\n", item->id);
return false;
}
}
// get next item
item = item->next;
} while (item);
return true;
}
// -----------------------------------------------------------------------------
// Set default hotkeys
// -----------------------------------------------------------------------------
void hotkeymanager_append_default_hotkeys(HotkeyManager* hkman)
{
int new_id = hkman->current_id++;
hklist* item = hklist_append(hkman->hotkeys);
hklist_set_item(item, new_id, "", MOD_ALT | MOD_SHIFT | MOD_CONTROL, 'Q');
hkman->hk_id_quit = new_id;
new_id = hkman->current_id++;
item = hklist_append(item);
hklist_set_item(item, new_id, "", MOD_ALT | MOD_CONTROL, 'R');
hkman->hk_id_refresh = new_id;
}
// -----------------------------------------------------------------------------
// Unregister hotkeys
// -----------------------------------------------------------------------------
bool hotkeymanager_unregister_hotkeys(HotkeyManager* hkman)
{
hklist* item = hkman->hotkeys->next; // get first item
while (item)
{
#ifdef _PLAT_WNDS
if (!UnregisterHotKey(NULL, item->id))
#else
if (xhkUnBindKey(hkman->hkconfig, 0, item->vk, hkconvert(item->mod),
xhkKeyPress) == -1)
#endif // _PLAT_WNDS
return false;
else
hklog("Successfully unregistered hotkey (id: %d)\n", item->id);
// get next item
item = item->next;
}
return true;
}
// -----------------------------------------------------------------------------
// Refresh hotkeys
// -----------------------------------------------------------------------------
bool hotkeymanager_refresh_hotkeys(HotkeyManager* hkman)
{
if (!hotkeymanager_unregister_hotkeys(hkman))
{
hklog("ERROR: Failed to unregister hotkeys!\n");
return false;
}
hklist_destroy(hkman->hotkeys);
hkman->hotkeys = hklist_create(1);
hkman->current_id = 0;
hkman->hk_id_quit = -1;
hkman->hk_id_refresh = -1;
hotkeymanager_append_default_hotkeys(hkman);
if (hotkeymanager_read_hotkeys_form_file(hkman) &&
hotkeymanager_register_hotkeys(hkman))
return true;
else
return false;
}
// -----------------------------------------------------------------------------
// Read hotkeys from hotkeyman configuration file.
// Default: hotkeyman.conf
// -----------------------------------------------------------------------------
bool hotkeymanager_read_hotkeys_form_file(HotkeyManager* hkman)
{
FILE* hk_file = fopen(hkman->config_file_name, "r");
if (hk_file == NULL)
{
hklog("ERROR: Unable to open file '%s'\n", hkman->config_file_name);
return false;
}
char* key = malloc(sizeof(char) * 512);
char* value = malloc(sizeof(char) * 512);
hklist* item = hkman->hotkeys;
bool new_stmt = true;
// strip trailing and leading whitespaces ('\n' and ' ')
while (fscanf(hk_file, " \n %[^= ] = \"%[^\"]\" \n", key, value) == 2)
{
// check for new statement
if (new_stmt)
{
// new statement appeard -> append new hotkey-list item
item = hklist_append(item);
// current statement is active -> no new statement is expected
new_stmt = false;
hkman->current_id++;
item->id = hkman->current_id;
}
if (strcmp("cmd", key) == 0)
// set command
hklist_set_item_command_attrib(item, value);
else if (strcmp("keys", key) == 0)
{
unsigned int mod1 = 0;
unsigned int mod2 = 0;
unsigned int mod3 = 0;
char vk = -1;
// parse value
if (sscanf(value, "%x %x %x %c", &mod1, &mod2, &mod3, &vk) == 4)
{
// set keys
item->mod = mod1 | mod2 | mod3;
item->vk = vk;
}
else
{
hklog("ERROR: Value of key '%s' does not fit the specified"\
" format!\n");
return false;
}
}
else
hklog("WARNING: The key '%s' is not specified!\n", key);
// get next char
char c = fgetc(hk_file);
if (c == ';')
// end of statement reached -> new statement is expected
new_stmt = true;
else
// restore char (end of statement not yet reached)
ungetc(c, hk_file);
}
free(key);
free(value);
fclose(hk_file);
return true;
}
// #############################################################################
// Internal functions
// #############################################################################
// -----------------------------------------------------------------------------
// Determine the configuration path
// -----------------------------------------------------------------------------
char* hotkeymanager_get_config_path(const char* file_name)
{
char* result;
const char* home;
home = getenv("HOME");
if (home)
{
int size = strlen(home) + strlen(file_name) + 3;
result = malloc(size);
memset(result, 0, size);
strcpy(result, home);
strcat(result, "/.");
strcat(result, file_name);
}
else
result = strdup(file_name);
return result;
}
#ifndef _PLAT_WNDS
// -----------------------------------------------------------------------------
// Callback: Handle keypress in X11
// -----------------------------------------------------------------------------
void hotkeymanager_handle_hotkey_cb(xhkEvent event, void* arg1, void* arg2,
void* arg3)
{
HotkeyManager* hkman = (HotkeyManager*) arg1;
int id = (int) arg2;
hotkeymanager_process_hotkeys_internal(hkman, id);
}
#endif // not _PLAT_WNDS