-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
97 lines (73 loc) · 2.6 KB
/
main.c
File metadata and controls
97 lines (73 loc) · 2.6 KB
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
#include <ApplicationServices/ApplicationServices.h>
#include <stdint.h>
#include "deps/parson/parson.h"
#include "types.h"
struct keyblock blockedKeys[32];
size_t numBlockedKeys;
CGEventRef filterKeysCallback(CGEventTapProxy proxy, CGEventType type,
CGEventRef event, void *refcon) {
CGKeyCode keyCode =
(CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
size_t i;
double timeElapsed;
for (i = 0; i < numBlockedKeys; i += 1) {
if (keyCode == (CGKeyCode)blockedKeys[i].keyCode) {
gettimeofday(&blockedKeys[i].start, NULL);
timeElapsed =
(blockedKeys[i].start.tv_sec - blockedKeys[i].oldStart.tv_sec) *
1000.0;
timeElapsed +=
(blockedKeys[i].start.tv_usec - blockedKeys[i].oldStart.tv_usec) /
1000.0;
gettimeofday(&blockedKeys[i].oldStart, NULL);
if (timeElapsed < blockedKeys[i].delay) {
return NULL;
} else {
return event;
}
}
}
return event;
}
bool load_settings() {
JSON_Value *root_value = json_parse_file("/usr/local/etc/keyblock/settings.json");
if (json_value_get_type(root_value) != JSONObject) {
return false;
}
JSON_Object *root_obj = json_value_get_object(root_value);
JSON_Array *keys = json_object_get_array(root_obj, "keys");
size_t i;
JSON_Object *key;
numBlockedKeys = json_array_get_count(keys);
for (i = 0; i < numBlockedKeys; i += 1) {
key = json_array_get_object(keys, i);
blockedKeys[i].keyCode = (int)json_object_dotget_number(key, "keyCode");
blockedKeys[i].delay = (int)json_object_dotget_number(key, "delay");
gettimeofday(&blockedKeys[i].start, NULL);
gettimeofday(&blockedKeys[i].oldStart, NULL);
}
json_value_free(root_value);
return true;
}
int main(void) {
if (load_settings() == false) {
printf("Cannot find/parse settings.json\n");
return EXIT_FAILURE;
}
// Create the event tap before any pre-existing event taps
CFMachPortRef eventTap = CGEventTapCreate(
kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault,
CGEventMaskBit(kCGEventKeyDown), filterKeysCallback, NULL);
if (!eventTap) {
printf("Failed to create event tap. Must run with root privileges.\n");
return EXIT_FAILURE;
}
printf("Event tap created. Intercepting events...\n");
CFRunLoopSourceRef runLoopSource =
CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource,
kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
return EXIT_SUCCESS;
}