diff options
Diffstat (limited to 'tools/gsr-global-hotkeys/main.c')
-rw-r--r-- | tools/gsr-global-hotkeys/main.c | 72 |
1 files changed, 27 insertions, 45 deletions
diff --git a/tools/gsr-global-hotkeys/main.c b/tools/gsr-global-hotkeys/main.c index 9d7dd68..41e5ca5 100644 --- a/tools/gsr-global-hotkeys/main.c +++ b/tools/gsr-global-hotkeys/main.c @@ -2,42 +2,11 @@ /* C stdlib */ #include <stdio.h> -#include <stdint.h> #include <string.h> +#include <locale.h> /* POSIX */ #include <unistd.h> -#include <dlfcn.h> - -typedef struct { - uint32_t key; - uint32_t modifiers; /* keyboard_modkeys bitmask */ - const char *action; -} global_hotkey; - -#define NUM_GLOBAL_HOTKEYS 6 -static global_hotkey global_hotkeys[NUM_GLOBAL_HOTKEYS] = { - { .key = KEY_Z, .modifiers = KEYBOARD_MODKEY_LALT, .action = "show_hide" }, - { .key = KEY_F9, .modifiers = KEYBOARD_MODKEY_LALT, .action = "record" }, - { .key = KEY_F7, .modifiers = KEYBOARD_MODKEY_LALT, .action = "pause" }, - { .key = KEY_F8, .modifiers = KEYBOARD_MODKEY_LALT, .action = "stream" }, - { .key = KEY_F10, .modifiers = KEYBOARD_MODKEY_LALT | KEYBOARD_MODKEY_SHIFT, .action = "replay_start" }, - { .key = KEY_F10, .modifiers = KEYBOARD_MODKEY_LALT, .action = "replay_save" } -}; - -static bool on_key_callback(uint32_t key, uint32_t modifiers, int press_status, void *userdata) { - (void)userdata; - for(int i = 0; i < NUM_GLOBAL_HOTKEYS; ++i) { - if(key == global_hotkeys[i].key && modifiers == global_hotkeys[i].modifiers) { - if(press_status == 1) { /* 1 == Pressed */ - puts(global_hotkeys[i].action); - fflush(stdout); - } - return false; - } - } - return true; -} static void usage(void) { fprintf(stderr, "usage: gsr-global-hotkeys [--all|--virtual]\n"); @@ -46,13 +15,27 @@ static void usage(void) { fprintf(stderr, " --virtual Grab all virtual devices only.\n"); } -static int x_ignore_error(Display *display, XErrorEvent *ee) { - (void)display; - (void)ee; - return 0; +static bool is_gsr_global_hotkeys_already_running(void) { + FILE *f = fopen("/proc/bus/input/devices", "rb"); + if(!f) + return false; + + bool virtual_keyboard_running = false; + char line[1024]; + while(fgets(line, sizeof(line), f)) { + if(strstr(line, "gsr-ui virtual keyboard")) { + virtual_keyboard_running = true; + break; + } + } + + fclose(f); + return virtual_keyboard_running; } int main(int argc, char **argv) { + setlocale(LC_ALL, "C"); /* Sigh... stupid C */ + keyboard_grab_type grab_type = KEYBOARD_GRAB_TYPE_ALL; if(argc == 2) { const char *grab_type_arg = argv[1]; @@ -71,11 +54,10 @@ int main(int argc, char **argv) { return 1; } - Display *display = XOpenDisplay(NULL); - if(display) - XSetErrorHandler(x_ignore_error); - else - fprintf(stderr, "Warning: XOpenDisplay failed\n"); + if(is_gsr_global_hotkeys_already_running()) { + fprintf(stderr, "Error: gsr-global-hotkeys is already running\n"); + return 1; + } const uid_t user_id = getuid(); if(geteuid() != 0) { @@ -86,7 +68,7 @@ int main(int argc, char **argv) { } keyboard_event keyboard_ev; - if(!keyboard_event_init(&keyboard_ev, true, true, grab_type, display)) { + if(!keyboard_event_init(&keyboard_ev, true, grab_type)) { fprintf(stderr, "Error: failed to setup hotplugging and no keyboard input devices were found\n"); setuid(user_id); return 1; @@ -95,9 +77,9 @@ int main(int argc, char **argv) { fprintf(stderr, "Info: global hotkeys setup, waiting for hotkeys to be pressed\n"); for(;;) { - keyboard_event_poll_events(&keyboard_ev, -1, on_key_callback, NULL); - if(keyboard_event_stdout_has_failed(&keyboard_ev)) { - fprintf(stderr, "Info: stdout closed (parent process likely closed this process), exiting...\n"); + keyboard_event_poll_events(&keyboard_ev, -1); + if(keyboard_event_stdin_has_failed(&keyboard_ev)) { + fprintf(stderr, "Info: stdin closed (parent process likely closed this process), exiting...\n"); break; } } |