-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
56 lines (44 loc) · 1.68 KB
/
main.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
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h>
#include <stdlib.h>
#include <unistd.h>
// from: https://gist.github.com/atr000/387590
#define RSHIFT_DOWN 0x20104ull
#define MODIFIER_UP 0x100ull
// The following callback method is invoked on every keypress.
CGEventRef CGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
CGKeyCode keyCode = (CGKeyCode) CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
CGEventFlags modifiers = CGEventGetFlags(event);
(void)keyCode;
if (modifiers == RSHIFT_DOWN) {
system("/usr/bin/curl http://10.10.10.10:4444/toggle-usb-switch --max-time 3 -o /dev/null 1> /dev/null 2>/dev/null &");
}
return event;
}
int main(int argc, const char *argv[]) {
fprintf(stderr, "I am: %d : %d\n", getuid(), geteuid());
if (argc > 1) {
fprintf(stderr, "Version: 1.0.0\n");
exit(0);
}
// Create an event tap to retrieve keypresses.
CGEventMask eventMask = /*CGEventMaskBit(kCGEventKeyDown) |*/ CGEventMaskBit(kCGEventFlagsChanged);
CFMachPortRef eventTap = CGEventTapCreate(
kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, CGEventCallback, NULL
);
// Exit the program if unable to create the event tap.
if (!eventTap) {
fprintf(stderr, "ERROR: Unable to create event tap.\n");
exit(1);
}
// Create a run loop source and add enable the event tap.
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
return 0;
}