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
|
#define _GNU_SOURCE
#include <X11/XKBlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#define MAX_BINDS 256
#define MAX_CMD_LEN 512
#define SOCK_PATH "/tmp/piss.sock"
typedef struct {
unsigned int mod;
KeyCode keycode;
char cmd[MAX_CMD_LEN];
} Binding;
static Display *dpy;
static Window root;
static Binding bindings[MAX_BINDS];
static int bind_count = 0;
unsigned int parse_modifiers(const char *str) {
unsigned int mod = 0;
if (strcasestr(str, "super") || strcasestr(str, "mod4"))
mod |= Mod4Mask;
if (strcasestr(str, "shift"))
mod |= ShiftMask;
if (strcasestr(str, "ctrl") || strcasestr(str, "control"))
mod |= ControlMask;
if (strcasestr(str, "alt") || strcasestr(str, "mod1"))
mod |= Mod1Mask;
return mod;
}
void grab_key(unsigned int mod, KeyCode kc) {
unsigned int numlock = Mod2Mask, capslock = LockMask;
XGrabKey(dpy, kc, mod, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, kc, mod | numlock, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, kc, mod | capslock, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, kc, mod | numlock | capslock, root, True, GrabModeAsync,
GrabModeAsync);
}
void add_binding(char *msg) {
if (bind_count >= MAX_BINDS)
return;
char *sep = strstr(msg, "|||");
if (!sep)
return;
*sep = '\0';
char *combo = msg, *cmd = sep + 3;
char *last_plus = strrchr(combo, '+');
if (!last_plus)
return;
*last_plus = '\0';
char *key_name = last_plus + 1;
while (*key_name == ' ')
key_name++;
KeySym sym = XStringToKeysym(key_name);
if (sym == NoSymbol)
return;
bindings[bind_count].mod = parse_modifiers(combo);
bindings[bind_count].keycode = XKeysymToKeycode(dpy, sym);
strncpy(bindings[bind_count].cmd, cmd, MAX_CMD_LEN - 1);
grab_key(bindings[bind_count].mod, bindings[bind_count].keycode);
bind_count++;
XSync(dpy, False);
}
int client_mode(int retry, char *combo, char *cmd) {
struct sockaddr_un addr = {.sun_family = AF_UNIX};
strncpy(addr.sun_path, SOCK_PATH, sizeof(addr.sun_path) - 1);
while (1) {
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
if (combo && cmd) {
char buf[MAX_CMD_LEN * 2];
snprintf(buf, sizeof(buf), "%s|||%s", combo, cmd);
write(sock, buf, strlen(buf));
}
close(sock);
return 0;
}
close(sock);
if (!retry)
return 1;
usleep(50000);
}
}
int main(int argc, char *argv[]) {
if (argc >= 2 && strcmp(argv[1], "wait") == 0)
return client_mode(1, NULL, NULL);
if (argc >= 4 && strcmp(argv[1], "bind") == 0)
return client_mode(0, argv[2], argv[3]);
dpy = XOpenDisplay(NULL);
if (!dpy)
return 1;
root = DefaultRootWindow(dpy);
XSelectInput(dpy, root, KeyPressMask);
int serv = socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un addr = {.sun_family = AF_UNIX};
strncpy(addr.sun_path, SOCK_PATH, sizeof(addr.sun_path) - 1);
unlink(SOCK_PATH);
bind(serv, (struct sockaddr *)&addr, sizeof(addr));
listen(serv, 10);
char path[512];
ssize_t len = readlink("/proc/self/exe", path, sizeof(path) - 1);
if (len != -1) {
path[len] = '\0';
setenv("PISS_BIN", path, 1);
}
if (fork() == 0) {
char *cfg = (argc > 1) ? argv[1] : NULL;
if (!cfg) {
static char def[512];
snprintf(def, 511, "%s/.config/piss/pissrc", getpwuid(getuid())->pw_dir);
cfg = def;
}
// Use execvp so the shebang (#! /usr/bin/lua) is respected
char *args[] = {cfg, NULL};
execvp(cfg, args);
perror("execvp");
exit(1);
}
int xfd = ConnectionNumber(dpy);
while (1) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(xfd, &fds);
FD_SET(serv, &fds);
int max = (xfd > serv) ? xfd : serv;
if (select(max + 1, &fds, NULL, NULL, NULL) > 0) {
if (FD_ISSET(serv, &fds)) {
int cli = accept(serv, NULL, NULL);
char buf[MAX_CMD_LEN * 2] = {0};
if (read(cli, buf, sizeof(buf) - 1) > 0)
add_binding(buf);
close(cli);
}
while (XPending(dpy)) {
XEvent ev;
XNextEvent(dpy, &ev);
if (ev.type == KeyPress) {
unsigned int state = ev.xkey.state & ~(Mod2Mask | LockMask);
for (int i = 0; i < bind_count; i++) {
if (bindings[i].keycode == ev.xkey.keycode &&
bindings[i].mod == state) {
if (fork() == 0) {
setsid();
execl("/bin/sh", "sh", "-c", bindings[i].cmd, NULL);
exit(0);
}
}
}
}
}
}
}
return 0;
}
|