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
|
#include<stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <unistd.h>
#define VERSION "0.0.1"
typedef struct Command {
char* command;
unsigned int mode;
int persist;
} Command;
typedef struct Key {
unsigned int mod;
KeySym key;
Command command;
} Key;
typedef struct ModeProperties {
char* label;
} ModeProperties;
#define cmd(c) (Command) { c, -1, False }
#define mode(m, p) (Command) { NULL, m, p }
#include "config.h"
#define LENGTH(X) (sizeof X / sizeof X[0])
#define CLEANMASK(mask) (mask & ~LockMask & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
int current_mode = -1;
int is_mode_persistent = 0;
void bind_key(Display *dpy, Window win, unsigned int mod, KeySym key) {
int keycode = XKeysymToKeycode(dpy, key);
XGrabKey(dpy, keycode, mod, win, False, GrabModeAsync, GrabModeAsync);
}
void unbind_key(Display *dpy, Window win, unsigned int mod, KeySym key) {
int keycode = XKeysymToKeycode(dpy, key);
XUngrabKey(dpy, keycode, mod, win);
}
int error_handler(Display *disp, XErrorEvent *xe) {
if (xe->error_code == BadAccess) {
printf("daemonic: [BadAccess] Cant grab key binding. Already grabbed\n");
return 0;
}
printf("daemonic: Something went wrong\n");
return 1;
}
void spawn(char ** command) {
if (fork() == 0) {
setsid();
execvp(command[0], command);
fprintf(stderr, "dwm: execvp %s", command[0]);
perror(" failed");
exit(0);
}
}
void run(Display* dpy, Window win, Command command) {
Key mode_key;
unsigned int i;
if (command.command) {
char* cmd[] = {shell, "-c", command.command, NULL};
spawn(cmd);
} else if(command.mode != -1) {
current_mode = command.mode;
is_mode_persistent = command.persist;
if (modes[current_mode] && current_mode < MODE_SIZE) {
for (i = 0; i < LENGTH(modes[current_mode]); i++) {
mode_key = modes[current_mode][i];
bind_key(dpy, win, mode_key.mod, mode_key.key);
}
}
// Bind an escape key to quit mode
bind_key(dpy, win, 0, XK_Escape);
}
}
void keypress(Display *dpy, Window win, XKeyEvent *ev) {
unsigned int i;
Key mode_key;
KeySym keysym = XKeycodeToKeysym(dpy, (KeyCode) ev->keycode, 0);
if (current_mode == -1) {
// Bind all the normal mode keys
for (i = 0; i < LENGTH(keys); i++) {
if (keysym == keys[i].key && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)) {
run(dpy, win, keys[i].command);
}
}
} else {
// Escape key
is_mode_persistent = is_mode_persistent && ev->keycode != 9;
if (modes[current_mode] && current_mode < MODE_SIZE) {
// Check if key is in mode and execute
for (i = 0; i < LENGTH(modes[current_mode]); i++) {
mode_key = modes[current_mode][i];
if (keysym == mode_key.key && CLEANMASK(mode_key.mod) == CLEANMASK(ev->state)) {
run(dpy, win, mode_key.command);
}
}
if (!is_mode_persistent) {
// Unbind mode related keys
for (i = 0; i < LENGTH(modes[current_mode]); i++) {
mode_key = modes[current_mode][i];
unbind_key(dpy, win, mode_key.mod, mode_key.key);
}
// Unbind escape key
unbind_key(dpy, win, 0, XK_Escape);
}
}
if (!is_mode_persistent) {
current_mode = -1;
is_mode_persistent = False;
}
}
}
char* get_mode_label() {
if (current_mode == -1) return "";
ModeProperties props = mode_properties[current_mode];
return props.label;
}
void help_menu(char* x) {
printf("Usage: %s [-vh]\n", x);
}
int main(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, "vh")) != -1) {
switch (opt) {
case 'v': printf("%s\n", VERSION); return 0;
/*case 'm': printf("%s\n", get_mode_label()); return 0;*/
case 'h':
default:
help_menu(argv[0]);
return opt != 'h';
}
}
XSetErrorHandler(error_handler);
int running = 1, i = 0;
Display *dpy = XOpenDisplay(0);
Window root = DefaultRootWindow(dpy);
// Grab keys
for (i = 0; i < LENGTH(keys); i++) {
bind_key(dpy, root, keys[i].mod, keys[i].key);
}
XSelectInput(dpy, root, KeyPressMask);
/* main event loop */
XEvent ev;
XSync(dpy, False);
while (running && !XNextEvent(dpy, &ev)) {
switch (ev.type) {
case KeyPress: {
keypress(dpy, root, &ev.xkey);
break;
}
}
}
XCloseDisplay(dpy);
}
|