aboutsummaryrefslogtreecommitdiff
path: root/src/hotkey.c
blob: d34d9a0e048b1463f3ecbf72ac1c39e0ffd1f7be (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
#include "hotkey.h"
#include "locale.h"
#include "parse.h"

#include <string.h>
#include <pthread.h>

#define internal static
#define local_persist static

internal bool
execute_hotkey(struct hotkey *hotkey)
{
    local_persist char arg[] = "-c";
    local_persist char *shell = NULL;
    if(!shell)
    {
        char *env_shell = getenv("SHELL");
        shell = env_shell ? env_shell : "/bin/bash";
    }

    int cpid = fork();
    if(cpid == 0)
    {
        char *exec[] = { shell, arg, hotkey->command, NULL};
        int status_code = execvp(exec[0], exec);
        exit(status_code);
    }

    return true;
}

internal bool
compare_cmd(struct hotkey *a, struct hotkey *b)
{
    if(has_flags(a, Hotkey_Flag_Cmd)) {
        return (has_flags(b, Hotkey_Flag_LCmd) ||
                has_flags(b, Hotkey_Flag_RCmd) ||
                has_flags(b, Hotkey_Flag_Cmd));
    } else {
        return ((has_flags(a, Hotkey_Flag_LCmd) == has_flags(b, Hotkey_Flag_LCmd)) &&
                (has_flags(a, Hotkey_Flag_RCmd) == has_flags(b, Hotkey_Flag_RCmd)) &&
                (has_flags(a, Hotkey_Flag_Cmd) == has_flags(b, Hotkey_Flag_Cmd)));
    }
}

internal bool
compare_shift(struct hotkey *a, struct hotkey *b)
{
    if(has_flags(a, Hotkey_Flag_Shift)) {
        return (has_flags(b, Hotkey_Flag_LShift) ||
                has_flags(b, Hotkey_Flag_RShift) ||
                has_flags(b, Hotkey_Flag_Shift));
    } else {
        return ((has_flags(a, Hotkey_Flag_LShift) == has_flags(b, Hotkey_Flag_LShift)) &&
                (has_flags(a, Hotkey_Flag_RShift) == has_flags(b, Hotkey_Flag_RShift)) &&
                (has_flags(a, Hotkey_Flag_Shift) == has_flags(b, Hotkey_Flag_Shift)));
    }
}

internal bool
compare_alt(struct hotkey *a, struct hotkey *b)
{
    if(has_flags(a, Hotkey_Flag_Alt)) {
        return (has_flags(b, Hotkey_Flag_LAlt) ||
                has_flags(b, Hotkey_Flag_RAlt) ||
                has_flags(b, Hotkey_Flag_Alt));
    } else {
        return ((has_flags(a, Hotkey_Flag_LAlt) == has_flags(b, Hotkey_Flag_LAlt)) &&
                (has_flags(a, Hotkey_Flag_RAlt) == has_flags(b, Hotkey_Flag_RAlt)) &&
                (has_flags(a, Hotkey_Flag_Alt) == has_flags(b, Hotkey_Flag_Alt)));
    }
}

internal bool
compare_ctrl(struct hotkey *a, struct hotkey *b)
{
    if(has_flags(a, Hotkey_Flag_Control)) {
        return (has_flags(b, Hotkey_Flag_LControl) ||
                has_flags(b, Hotkey_Flag_RControl) ||
                has_flags(b, Hotkey_Flag_Control));
    } else {
        return ((has_flags(a, Hotkey_Flag_LControl) == has_flags(b, Hotkey_Flag_LControl)) &&
                (has_flags(a, Hotkey_Flag_RControl) == has_flags(b, Hotkey_Flag_RControl)) &&
                (has_flags(a, Hotkey_Flag_Control) == has_flags(b, Hotkey_Flag_Control)));
    }
}

internal inline bool
same_hotkey(struct hotkey *a, struct hotkey *b)
{
    if(a && b) {
        return compare_cmd(a, b) &&
               compare_shift(a, b) &&
               compare_alt(a, b) &&
               compare_ctrl(a, b) &&
               a->key == b->key;
    }

    return false;
}

internal bool
find_hotkey(struct hotkey *seek, struct hotkey **result, struct hotkey *hotkeys)
{
    struct hotkey *hotkey = hotkeys;
    while(hotkey) {
        if(same_hotkey(hotkey, seek)) {
            *result = hotkey;
            return true;
        }

        hotkey = hotkey->next;
    }

    return false;
}

void free_hotkeys(struct hotkey *hotkeys)
{
    struct hotkey *next, *hotkey = hotkeys;
    while(hotkey) {
        next = hotkey->next;
        free(hotkey->command);
        free(hotkey);
        hotkey = next;
    }
}

bool find_and_exec_hotkey(struct hotkey *eventkey, struct hotkey *hotkeys)
{
    bool result = false;
    struct hotkey *hotkey = NULL;
    if(find_hotkey(eventkey, &hotkey, hotkeys)) {
        if(execute_hotkey(hotkey)) {
            result = has_flags(hotkey, Hotkey_Flag_Passthrough) ? false : true;
        }
    }

    return result;
}

struct hotkey
cgevent_to_hotkey(CGEventFlags flags, uint32_t key)
{
    struct hotkey eventkey = {};
    eventkey.key = key;

    if((flags & Event_Mask_Cmd) == Event_Mask_Cmd) {
        bool left = (flags & Event_Mask_LCmd) == Event_Mask_LCmd;
        bool right = (flags & Event_Mask_RCmd) == Event_Mask_RCmd;

        if(left)            add_flags(&eventkey, Hotkey_Flag_LCmd);
        if(right)           add_flags(&eventkey, Hotkey_Flag_RCmd);
        if(!left && !right) add_flags(&eventkey, Hotkey_Flag_Cmd);
    }

    if((flags & Event_Mask_Shift) == Event_Mask_Shift) {
        bool left = (flags & Event_Mask_LShift) == Event_Mask_LShift;
        bool right = (flags & Event_Mask_RShift) == Event_Mask_RShift;

        if(left)            add_flags(&eventkey, Hotkey_Flag_LShift);
        if(right)           add_flags(&eventkey, Hotkey_Flag_RShift);
        if(!left && !right) add_flags(&eventkey, Hotkey_Flag_Shift);
    }

    if((flags & Event_Mask_Alt) == Event_Mask_Alt) {
        bool left = (flags & Event_Mask_LAlt) == Event_Mask_LAlt;
        bool right = (flags & Event_Mask_RAlt) == Event_Mask_RAlt;

        if(left)            add_flags(&eventkey, Hotkey_Flag_LAlt);
        if(right)           add_flags(&eventkey, Hotkey_Flag_RAlt);
        if(!left && !right) add_flags(&eventkey, Hotkey_Flag_Alt);
    }

    if((flags & Event_Mask_Control) == Event_Mask_Control) {
        bool left = (flags & Event_Mask_LControl) == Event_Mask_LControl;
        bool right = (flags & Event_Mask_RControl) == Event_Mask_RControl;

        if(left)            add_flags(&eventkey, Hotkey_Flag_LControl);
        if(right)           add_flags(&eventkey, Hotkey_Flag_RControl);
        if(!left && !right) add_flags(&eventkey, Hotkey_Flag_Control);
    }

    return eventkey;
}