-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.c
More file actions
505 lines (448 loc) · 15.7 KB
/
Copy pathcli.c
File metadata and controls
505 lines (448 loc) · 15.7 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
* cli.c - Command line interface: argument parsing and subcommands.
*/
#include "cli.h"
#include "config.h"
#include "daemon.h"
#include "keys.h"
#include "presets.h"
#include "protocol.h"
#include "tui.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#define SERVICE_NAME "matebook-gestures.service"
#define UNIT_PATH "/etc/systemd/system/" SERVICE_NAME
#define UDEV_PATH "/etc/udev/rules.d/99-matebook-gestures.rules"
#define OLD_SERVICE "matebook-sliders.service"
static const char UNIT_TEMPLATE[] =
"[Unit]\n"
"Description=MateBook touchpad gestures (edge sliders, knuckle knocks, corner presses)\n"
"Documentation=https://github.com/dalzyu/matebook-gestures\n"
"StartLimitIntervalSec=30\n"
"StartLimitBurst=3\n"
"\n"
"[Service]\n"
"Type=simple\n"
"ExecStart=%s\n"
"ExecReload=/bin/kill -HUP $MAINPID\n"
"Restart=on-failure\n"
"RestartSec=2\n"
"Nice=-20\n"
"NoNewPrivileges=yes\n"
"PrivateTmp=yes\n"
"ProtectHome=yes\n"
"ProtectSystem=full\n"
"\n"
"[Install]\n"
"WantedBy=multi-user.target\n";
static const char UDEV_TEMPLATE[] =
"# Goodix GXTP7863 haptic touchpad gesture hidraw node\n"
"KERNEL==\"hidraw*\", KERNELS==\"*27C6:01E0*\", GROUP=\"input\", MODE=\"0660\", TAG+=\"uaccess\"\n"
"\n"
"# uinput virtual keyboard device\n"
"KERNEL==\"uinput\", GROUP=\"input\", MODE=\"0660\", TAG+=\"uaccess\"\n";
/* ---- helpers ------------------------------------------------------------ */
static void require_root(const char *what)
{
if (geteuid() != 0) {
fprintf(stderr, "%s needs root: sudo matebook-gestures %s\n",
what, what);
exit(1);
}
}
static int run_command(const char *const argv[])
{
pid_t pid = fork();
if (pid < 0) return -1;
if (pid == 0) {
execvp(argv[0], (char *const *)argv);
_exit(127);
}
int status;
if (waitpid(pid, &status, 0) < 0)
return -1;
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
}
static int systemctl(const char *arg1, const char *arg2,
const char *arg3, const char *arg4)
{
const char *argv[7] = { "systemctl" };
int n = 1;
if (arg1) argv[n++] = arg1;
if (arg2) argv[n++] = arg2;
if (arg3) argv[n++] = arg3;
if (arg4) argv[n++] = arg4;
argv[n] = NULL;
return run_command(argv);
}
static const char *find_executable(void)
{
/* Resolve /proc/self/exe */
static char exe[256];
ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
if (n > 0) {
exe[n] = '\0';
return exe;
}
return "matebook-gestures";
}
/* ---- subcommands -------------------------------------------------------- */
static int cmd_run(const char *cfg_path, int argc, char **argv)
{
int debug = 0;
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "--debug")) debug = 1;
}
return daemon_run(cfg_path, debug);
}
static int cmd_config(const char *cfg_path)
{
if (geteuid() == 0 || strcmp(cfg_path, CONFIG_PATH) != 0)
return tui_run(cfg_path);
fprintf(stderr, "system configuration requires administrator privileges\n");
const char *exe = find_executable();
execlp("sudo", "sudo", exe, "--config", cfg_path, "config",
(char *)NULL);
perror("sudo");
return 1;
}
static int cmd_status(const char *cfg_path)
{
config cfg;
cfg_load(&cfg, cfg_path);
char devpath[64];
int found = find_touchpad(devpath, sizeof(devpath));
pid_t daemon_pid = daemon_find_pid();
bool config_present = access(cfg_path, F_OK) == 0;
bool uinput_present = access("/dev/uinput", F_OK) == 0;
bool rule_present = access(UDEV_PATH, F_OK) == 0;
bool healthy = found == 0 && daemon_pid > 0 && config_present &&
uinput_present && rule_present && cfg.nwarnings == 0;
printf("matebook-gestures %s\n", VERSION);
if (found == 0)
printf(" touchpad : %s\n", devpath);
else
printf(" touchpad : NOT FOUND (%s %s:%s)\n", HID_NAME, HID_VENDOR, HID_PRODUCT);
if (daemon_pid > 0)
printf(" daemon : running (pid %d)\n", daemon_pid);
else
printf(" daemon : NOT RUNNING\n");
printf(" config : %s%s\n", cfg_path,
config_present ? "" : " (missing -- using defaults)");
printf(" sensitivity: %s\n",
cfg_get(&cfg, "general", "click-sensitivity"));
printf(" vibration : %s\n",
cfg_get(&cfg, "general", "vibration-intensity"));
printf(" direct ctl: %s (experimental)\n",
cfg_get_bool(&cfg, "direct-control", "enabled", false)
? "enabled" : "disabled");
printf(" trackball : %s\n",
cfg_get_bool(&cfg, "direct-control", "trackball", false)
? "enabled" : "disabled");
printf(" setup : uinput %s, udev rule %s\n",
uinput_present ? "present" : "MISSING",
rule_present ? "installed" : "MISSING");
for (int i = 0; i < cfg.nwarnings; i++)
printf(" warning : %s\n", cfg.warnings[i]);
printf(" health : %s\n", healthy ? "OK" : "NEEDS ATTENTION");
if (!healthy) {
if (found != 0)
printf(" fix : touchpad not detected; check hardware support\n");
if (daemon_pid <= 0)
printf(" fix : sudo systemctl start matebook-gestures\n");
if (!uinput_present || !rule_present)
printf(" fix : sudo matebook-gestures install\n");
if (!config_present) {
if (!strcmp(cfg_path, CONFIG_PATH))
printf(" fix : matebook-gestures config\n");
else
printf(" fix : matebook-gestures --config %s config\n",
cfg_path);
} else if (cfg.nwarnings > 0) {
if (!strcmp(cfg_path, CONFIG_PATH))
printf(" fix : matebook-gestures config\n");
else
printf(" fix : matebook-gestures --config %s config\n",
cfg_path);
}
}
printf("\n %-28s %-4s %s\n", "GESTURE", "ON", "BINDING");
for (int i = 0; i < GESTURE_COUNT; i++) {
const gesture_def *g = &GESTURES[i];
bool on = cfg_get_bool(&cfg, g->section, "enabled", true);
if (g->kind == GKIND_TICK) {
const char *inc = cfg_get(&cfg, g->section, "increase");
const char *dec = cfg_get(&cfg, g->section, "decrease");
printf(" %-28s %-4s %s / %s\n", g->title, on ? "on" : "off",
inc && inc[0] ? inc : "none", dec && dec[0] ? dec : "none");
} else {
const char *k = cfg_get(&cfg, g->section, "keys");
printf(" %-28s %-4s %s\n", g->title, on ? "on" : "off",
k && k[0] ? k : "none");
}
}
cfg_free_warnings(&cfg);
return 0;
}
static int cmd_events(const char *cfg_path, int raw)
{
config cfg;
cfg_load(&cfg, cfg_path);
engine eng;
memset(&eng, 0, sizeof(eng));
engine_init(&eng, &cfg);
char devpath[64];
int fd = daemon_open_touchpad(devpath, sizeof(devpath));
if (fd < 0) {
cfg_free_warnings(&cfg);
return 1;
}
printf("reading %s -- gesture away, ^C to stop\n", devpath);
fflush(stdout);
uint8_t buf[64];
for (;;) {
ssize_t n = read(fd, buf, sizeof(buf));
if (n <= 0) {
close(fd);
cfg_free_warnings(&cfg);
return n == 0 ? 1 : 0;
}
if (raw && is_gesture_report(buf, (int)n)) {
printf("raw:");
for (int i = 0; i < (int)n; i++) printf(" %02x", buf[i]);
printf("\n");
fflush(stdout);
}
action act;
if (engine_handle(&eng, buf, (int)n, &act)) {
/* Build binding string */
char binding[256] = "(no binding)";
if (act.ncodes > 0) {
int pos = 0;
for (int i = 0; i < act.ncodes; i++) {
const char *nm = keycode_name(act.codes[i]);
if (i > 0) binding[pos++] = '+';
pos += snprintf(binding + pos, sizeof(binding) - pos,
"%s", nm ? nm : "?");
}
}
char label[128];
engine_action_label(&act, label, sizeof(label));
printf("%-40s -> %s\n", label, binding);
fflush(stdout);
}
}
}
static int cmd_keys(void)
{
/* Print unique key names sorted (they already are in KEY_TABLE) */
for (int i = 0; i < KEY_TABLE_LEN; i++)
printf("%s\n", KEY_TABLE[i].name);
return 0;
}
static int cmd_preset_list(void)
{
for (int i = 0; i < PRESET_COUNT; i++)
printf("%-12s %s\n", PRESETS[i].name, PRESETS[i].summary);
return 0;
}
static int cmd_preset_show(const char *name)
{
config cfg;
cfg_default(&cfg);
if (cfg_apply_preset(&cfg, name) < 0) {
fprintf(stderr, "unknown preset '%s' (see: matebook-gestures preset list)\n", name);
return 1;
}
char *text = cfg_dumps(&cfg);
if (text) { fputs(text, stdout); free(text); }
return 0;
}
static int cmd_install(const char *cfg_path)
{
require_root("install");
/* Write default config if missing */
if (access(cfg_path, F_OK) != 0) {
config cfg;
cfg_default(&cfg);
cfg_save(&cfg, cfg_path);
printf("wrote default config to %s\n", cfg_path);
}
/* Write systemd unit pointing to matebook-gestures-daemon */
const char *exe = find_executable();
char daemon_exe[512];
snprintf(daemon_exe, sizeof(daemon_exe), "%s-daemon", exe);
char unit[2048];
snprintf(unit, sizeof(unit), UNIT_TEMPLATE, daemon_exe);
FILE *f = fopen(UNIT_PATH, "w");
if (!f) { perror("fopen " UNIT_PATH); return 1; }
fputs(unit, f);
fclose(f);
printf("wrote %s\n", UNIT_PATH);
/* Write udev rules */
FILE *uf = fopen(UDEV_PATH, "w");
if (uf) {
fputs(UDEV_TEMPLATE, uf);
fclose(uf);
printf("wrote %s\n", UDEV_PATH);
/* Reload and trigger udev rules */
pid_t upid = fork();
if (upid == 0) {
execlp("udevadm", "udevadm", "control", "--reload-rules", (char *)NULL);
_exit(127);
}
waitpid(upid, NULL, 0);
upid = fork();
if (upid == 0) {
execlp("udevadm", "udevadm", "trigger", (char *)NULL);
_exit(127);
}
waitpid(upid, NULL, 0);
} else {
perror("fopen " UDEV_PATH);
}
/* Load uinput before starting the service. */
if (access("/dev/uinput", F_OK) != 0) {
const char *const argv[] = { "modprobe", "uinput", NULL };
if (run_command(argv) != 0)
fprintf(stderr, "warning: could not load the uinput kernel module\n");
}
/* Retire old service if present */
char old_path[256];
snprintf(old_path, sizeof(old_path), "/etc/systemd/system/%s", OLD_SERVICE);
if (access(old_path, F_OK) == 0) {
systemctl("disable", "--now", OLD_SERVICE, NULL);
printf("disabled old %s\n", OLD_SERVICE);
}
systemctl("daemon-reload", NULL, NULL, NULL);
systemctl("enable", SERVICE_NAME, NULL, NULL);
systemctl("restart", SERVICE_NAME, NULL, NULL);
systemctl("--no-pager", "--full", "status", SERVICE_NAME);
printf("\ninstalled. configure with: matebook-gestures config\n");
return 0;
}
static int cmd_uninstall(const char *cfg_path, int purge)
{
require_root("uninstall");
systemctl("disable", "--now", SERVICE_NAME, NULL);
char old_path[256];
snprintf(old_path, sizeof(old_path), "/etc/systemd/system/%s", OLD_SERVICE);
if (access(old_path, F_OK) == 0) {
systemctl("disable", "--now", OLD_SERVICE, NULL);
}
unlink(UNIT_PATH);
printf("removed %s\n", UNIT_PATH);
unlink(UDEV_PATH);
printf("removed %s\n", UDEV_PATH);
/* Reload systemd and udev */
systemctl("daemon-reload", NULL, NULL, NULL);
pid_t upid = fork();
if (upid == 0) {
execlp("udevadm", "udevadm", "control", "--reload-rules", (char *)NULL);
_exit(127);
}
waitpid(upid, NULL, 0);
if (purge) {
unlink(cfg_path);
printf("purged config %s\n", cfg_path);
}
printf("\nuninstalled successfully.\n");
return 0;
}
/* ---- usage -------------------------------------------------------------- */
static void usage(void)
{
fprintf(stderr,
"matebook-gestures %s\n"
"Firmware touchpad gestures for Huawei MateBook on Linux.\n\n"
"Usage:\n"
" matebook-gestures run [--debug] Run the gesture daemon (foreground)\n"
" matebook-gestures config Interactive configuration TUI (prompts for sudo)\n"
" matebook-gestures status Health check, settings, and bindings\n"
" matebook-gestures events [--raw] Watch decoded gesture events live\n"
" matebook-gestures keys List valid key names for bindings\n"
" matebook-gestures preset list List available presets\n"
" matebook-gestures preset show NAME Show a preset's bindings\n"
" matebook-gestures install Install + start systemd service (root)\n"
" matebook-gestures uninstall [--purge] Remove the systemd service (root)\n"
"\nOptions:\n"
" --config PATH Config file (default: " CONFIG_PATH ")\n"
" --version Show version\n"
" --help Show this help\n\n"
"Quick start: sudo matebook-gestures install && matebook-gestures config\n",
VERSION);
}
/* ---- main dispatch (called from main.c) -------------------------------- */
int cli_main(int argc, char **argv)
{
const char *cfg_path = CONFIG_PATH;
int i = 1;
/* Parse global options */
while (i < argc && argv[i][0] == '-') {
if (!strcmp(argv[i], "--config") && i + 1 < argc) {
cfg_path = argv[++i];
i++;
} else if (!strcmp(argv[i], "--version")) {
printf("matebook-gestures %s\n", VERSION);
return 0;
} else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
usage();
return 0;
} else {
fprintf(stderr, "unknown option: %s\n", argv[i]);
usage();
return 1;
}
}
if (i >= argc) {
usage();
return 1;
}
const char *cmd = argv[i++];
if (!strcmp(cmd, "run"))
return cmd_run(cfg_path, argc - i, argv + i);
if (!strcmp(cmd, "config"))
return cmd_config(cfg_path);
if (!strcmp(cmd, "status"))
return cmd_status(cfg_path);
if (!strcmp(cmd, "events")) {
int raw = 0;
for (int j = i; j < argc; j++)
if (!strcmp(argv[j], "--raw")) raw = 1;
return cmd_events(cfg_path, raw);
}
if (!strcmp(cmd, "keys"))
return cmd_keys();
if (!strcmp(cmd, "preset")) {
if (i >= argc) {
fprintf(stderr, "usage: matebook-gestures preset {list|show} [NAME]\n");
return 1;
}
const char *verb = argv[i++];
if (!strcmp(verb, "list"))
return cmd_preset_list();
if (i >= argc) {
fprintf(stderr, "preset %s needs a name (see: matebook-gestures preset list)\n", verb);
return 1;
}
const char *name = argv[i];
if (!strcmp(verb, "show"))
return cmd_preset_show(name);
fprintf(stderr, "unknown preset action: %s\n", verb);
return 1;
}
if (!strcmp(cmd, "install"))
return cmd_install(cfg_path);
if (!strcmp(cmd, "uninstall")) {
int purge = 0;
for (int j = i; j < argc; j++)
if (!strcmp(argv[j], "--purge")) purge = 1;
return cmd_uninstall(cfg_path, purge);
}
fprintf(stderr, "unknown command: %s\n", cmd);
usage();
return 1;
}