From 8d4252ef329e3e79b0cc4346aa6afc5664a3380f Mon Sep 17 00:00:00 2001 From: jackun Date: Fri, 22 Jul 2022 16:35:12 +0300 Subject: [PATCH] X11 keyboard poller thread --- src/keybinds.cpp | 5 ----- src/keybinds.h | 4 +--- src/shared_x11.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/keybinds.cpp b/src/keybinds.cpp index 16f7d078..ef8d6507 100644 --- a/src/keybinds.cpp +++ b/src/keybinds.cpp @@ -12,11 +12,6 @@ void check_keybinds(struct overlay_params& params, uint32_t vendorID){ auto elapsedReloadCfg = now - reload_cfg_press; auto elapsedUpload = now - last_upload_press; - static Clock::time_point last_check; - if (now - last_check < 100ms) - return; - last_check = now; - auto keyPressDelay = 400ms; if (elapsedF2 >= keyPressDelay && diff --git a/src/keybinds.h b/src/keybinds.h index 83dff612..05e2102f 100644 --- a/src/keybinds.h +++ b/src/keybinds.h @@ -12,6 +12,7 @@ typedef unsigned long KeySym; #endif Clock::time_point last_f2_press, toggle_fps_limit_press , last_f12_press, reload_cfg_press, last_upload_press; +extern char keys_return[32]; #if defined(HAVE_X11) bool keys_are_pressed(const std::vector& keys) { @@ -19,11 +20,8 @@ bool keys_are_pressed(const std::vector& keys) { if (!init_x11()) return false; - char keys_return[32]; size_t pressed = 0; - g_x11->XQueryKeymap(get_xdisplay(), keys_return); - for (KeySym ks : keys) { KeyCode kc2 = g_x11->XKeysymToKeycode(get_xdisplay(), ks); diff --git a/src/shared_x11.cpp b/src/shared_x11.cpp index ec2b25c5..ffd23cf7 100644 --- a/src/shared_x11.cpp +++ b/src/shared_x11.cpp @@ -2,13 +2,52 @@ #include #include #include +#include +#include #include #include "shared_x11.h" #include "loaders/loader_x11.h" +using namespace std::chrono_literals; + static std::unique_ptr> display; +char keys_return[32]{}; + +class x11_poller +{ + std::thread thread; + bool quit; + + void poll() + { + while (!quit) + { + libx11->XQueryKeymap(get_xdisplay(), keys_return); + std::this_thread::sleep_for(10ms); + } + } + +public: + std::shared_ptr libx11; + x11_poller(std::shared_ptr x) : libx11(x) {} + + void start() + { + stop(); + quit = false; + thread = std::thread(&x11_poller::poll, this); + } + + void stop() + { + quit = true; + if (thread.joinable()) + thread.join(); + } +}; bool init_x11() { + std::shared_ptr poller; static bool failed = false; if (failed) return false; @@ -24,13 +63,17 @@ bool init_x11() { const char *displayid = getenv("DISPLAY"); if (displayid) { - auto local_x11 = g_x11; + poller = std::make_shared( g_x11 ); display = { g_x11->XOpenDisplay(displayid), - [local_x11](Display* dpy) { + [poller](Display* dpy) { + poller->stop(); if (dpy) - local_x11->XCloseDisplay(dpy); + poller->libx11->XCloseDisplay(dpy); } }; + + if (display) + poller->start(); } failed = !display;