aboutsummaryrefslogtreecommitdiff
path: root/src/skhd.c
blob: 853af0f0124ebef02ecede3be58207440f59afee (plain) (blame)
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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <getopt.h>
#include <signal.h>
#include <string.h>

#include <Carbon/Carbon.h>

#include "hotload.h"
#include "event_tap.h"
#include "locale.h"
#include "tokenize.h"
#include "parse.h"
#include "hotkey.h"

#include "hotload.c"
#include "event_tap.c"
#include "locale.c"
#include "tokenize.c"
#include "parse.c"
#include "hotkey.c"

#define internal static
extern bool CGSIsSecureEventInputSet();
#define secure_keyboard_entry_enabled CGSIsSecureEventInputSet

internal unsigned major_version = 0;
internal unsigned minor_version = 0;
internal unsigned patch_version = 2;
internal struct hotkey *hotkeys;
internal char *config_file;

internal void
error(const char *format, ...)
{
    va_list args;
    va_start(args, format);
    vfprintf(stderr, format, args);
    va_end(args);
    exit(EXIT_FAILURE);
}

internal bool
watched_io_file(struct hotloader *hotloader, char *absolutepath)
{
    bool success = false;
    for(unsigned index = 0; success == 0 && index < hotloader->watch_count; ++index) {
        struct watched_file *watch_info = &hotloader->watch_list[index];

        char *directory = file_directory(absolutepath);
        char *filename = file_name(absolutepath);

        if(strcmp(watch_info->directory, directory) == 0) {
            if(strcmp(watch_info->filename, filename) == 0) {
                success = true;
            }
        }

        free(filename);
        free(directory);
    }

    return success;
}

internal HOTLOADER_CALLBACK(hotloader_handler)
{
    struct hotloader *hotloader = (struct hotloader *) context;

    char **files = (char **) paths;
    for(unsigned index = 0; index < count; ++index) {
        char *absolutepath = files[index];
        if(watched_io_file(hotloader, absolutepath)) {
            /* TODO(koekeishiya): We sometimes get two events upon file save.
             * Filter the duplicated event or something ?? */
            struct parser parser;
            if(parser_init(&parser, absolutepath)) {
                free_hotkeys(hotkeys);
                hotkeys = parse_config(&parser);
                parser_destroy(&parser);
            }
        }
    }
}

internal EVENT_TAP_CALLBACK(key_handler)
{
    switch(type)
    {
        case kCGEventTapDisabledByTimeout:
        case kCGEventTapDisabledByUserInput:
        {
            printf("skhd: restarting event-tap\n");
            struct event_tap *event_tap = (struct event_tap *) reference;
            CGEventTapEnable(event_tap->handle, 1);
        } break;
        case kCGEventKeyDown:
        {
            uint32_t flags = CGEventGetFlags(event);
            uint32_t key = CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
            struct hotkey eventkey = cgevent_to_hotkey(flags, key);
            if(find_and_exec_hotkey(&eventkey, hotkeys)) {
                return NULL;
            }
        } break;
        default: {} break;
    }

    return event;
}

internal bool
parse_arguments(int argc, char **argv)
{
    int option;
    const char *short_option = "vc:";
    struct option long_option[] =
    {
        { "version", no_argument, NULL, 'v' },
        { "config", required_argument, NULL, 'c' },
        { NULL, 0, NULL, 0 }
    };

    while((option = getopt_long(argc, argv, short_option, long_option, NULL)) != -1) {
        switch(option)
        {
            case 'v':
            {
                printf("skhd version %d.%d.%d\n", major_version, minor_version, patch_version);
                return true;
            } break;
            case 'c':
            {
                config_file = strdup(optarg);
            } break;
        }
    }

    return false;
}

internal bool
check_privileges()
{
    bool result = false;
    const void *keys[] = { kAXTrustedCheckOptionPrompt };
    const void *values[] = { kCFBooleanTrue };

    CFDictionaryRef options;
    options = CFDictionaryCreate(kCFAllocatorDefault,
                                 keys, values, sizeof(keys) / sizeof(*keys),
                                 &kCFCopyStringDictionaryKeyCallBacks,
                                 &kCFTypeDictionaryValueCallBacks);

    result = AXIsProcessTrustedWithOptions(options);
    CFRelease(options);

    return result;
}

internal void
set_config_path()
{
    if(!config_file) {
        char *home = getenv("HOME");
        if(home) {
            int length = strlen(home) + strlen("/.skhdrc");
            config_file = (char *) malloc(length + 1);
            strcpy(config_file, home);
            strcat(config_file, "/.skhdrc");
        } else {
            config_file = strdup(".skhdrc");
        }
    }
}

int main(int argc, char **argv)
{
    if(parse_arguments(argc, argv)) {
        return EXIT_SUCCESS;
    }

    if(secure_keyboard_entry_enabled()) {
        error("skhd: secure keyboard entry is enabled! abort..\n");
    }

    if(!check_privileges()) {
        error("skhd: must be run with accessibility access.\n");
    }

    signal(SIGCHLD, SIG_IGN);
    set_config_path();
    printf("skhd: using config '%s'\n", config_file);

    struct parser parser;
    if(parser_init(&parser, config_file)) {
        hotkeys = parse_config(&parser);
        parser_destroy(&parser);
    } else {
        error("skhd: could not open file '%s'\n", config_file);
    }

    struct event_tap event_tap;
    event_tap.mask = (1 << kCGEventKeyDown);
    event_tap_begin(&event_tap, key_handler);

    struct hotloader hotloader = {};
    hotloader_add_file(&hotloader, config_file);
    hotloader_begin(&hotloader, hotloader_handler);

    CFRunLoopRun();

    return EXIT_SUCCESS;
}