aboutsummaryrefslogtreecommitdiff
path: root/src/GlobalHotkeys
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2025-05-03 12:03:43 +0200
committerdec05eba <dec05eba@protonmail.com>2025-05-03 12:03:43 +0200
commitd08ea692771caa8e385412c2f992089672773e30 (patch)
tree994c05673d90b130e25d8bc25c6c365f607134db /src/GlobalHotkeys
parent180a3b73dbab2f586c53f9e5f044ab88aca95014 (diff)
Keep keyboard led when turning on global hotkeys, move files
Diffstat (limited to 'src/GlobalHotkeys')
-rw-r--r--src/GlobalHotkeys/GlobalHotkeysJoystick.cpp317
-rw-r--r--src/GlobalHotkeys/GlobalHotkeysLinux.cpp275
-rw-r--r--src/GlobalHotkeys/GlobalHotkeysX11.cpp201
3 files changed, 793 insertions, 0 deletions
diff --git a/src/GlobalHotkeys/GlobalHotkeysJoystick.cpp b/src/GlobalHotkeys/GlobalHotkeysJoystick.cpp
new file mode 100644
index 0000000..b3b21c8
--- /dev/null
+++ b/src/GlobalHotkeys/GlobalHotkeysJoystick.cpp
@@ -0,0 +1,317 @@
+#include "../../include/GlobalHotkeys/GlobalHotkeysJoystick.hpp"
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/eventfd.h>
+
+namespace gsr {
+ static constexpr int button_pressed = 1;
+ static constexpr int cross_button = 0;
+ static constexpr int triangle_button = 2;
+ static constexpr int options_button = 9;
+ static constexpr int playstation_button = 10;
+ static constexpr int axis_up_down = 7;
+ static constexpr int axis_left_right = 6;
+
+ // Returns -1 on error
+ static int get_js_dev_input_id_from_filepath(const char *dev_input_filepath) {
+ if(strncmp(dev_input_filepath, "/dev/input/js", 13) != 0)
+ return -1;
+
+ int dev_input_id = -1;
+ if(sscanf(dev_input_filepath + 13, "%d", &dev_input_id) == 1)
+ return dev_input_id;
+ return -1;
+ }
+
+ GlobalHotkeysJoystick::~GlobalHotkeysJoystick() {
+ if(event_fd > 0) {
+ const uint64_t exit = 1;
+ write(event_fd, &exit, sizeof(exit));
+ }
+
+ if(read_thread.joinable())
+ read_thread.join();
+
+ if(event_fd > 0)
+ close(event_fd);
+
+ for(int i = 0; i < num_poll_fd; ++i) {
+ close(poll_fd[i].fd);
+ }
+ }
+
+ bool GlobalHotkeysJoystick::start() {
+ if(num_poll_fd > 0)
+ return false;
+
+ event_fd = eventfd(0, 0);
+ if(event_fd <= 0)
+ return false;
+
+ event_index = num_poll_fd;
+ poll_fd[num_poll_fd] = {
+ event_fd,
+ POLLIN,
+ 0
+ };
+ extra_data[num_poll_fd] = {
+ -1
+ };
+ ++num_poll_fd;
+
+ if(!hotplug.start()) {
+ fprintf(stderr, "Warning: failed to setup hotplugging\n");
+ } else {
+ hotplug_poll_index = num_poll_fd;
+ poll_fd[num_poll_fd] = {
+ hotplug.steal_fd(),
+ POLLIN,
+ 0
+ };
+ extra_data[num_poll_fd] = {
+ -1
+ };
+ ++num_poll_fd;
+ }
+
+ char dev_input_path[128];
+ for(int i = 0; i < 8; ++i) {
+ snprintf(dev_input_path, sizeof(dev_input_path), "/dev/input/js%d", i);
+ add_device(dev_input_path, false);
+ }
+
+ if(num_poll_fd == 0)
+ fprintf(stderr, "Info: no joysticks found, assuming they might be connected later\n");
+
+ read_thread = std::thread(&GlobalHotkeysJoystick::read_events, this);
+ return true;
+ }
+
+ bool GlobalHotkeysJoystick::bind_action(const std::string &id, GlobalHotkeyCallback callback) {
+ if(num_poll_fd == 0)
+ return false;
+ return bound_actions_by_id.insert(std::make_pair(id, std::move(callback))).second;
+ }
+
+ void GlobalHotkeysJoystick::poll_events() {
+ if(num_poll_fd == 0)
+ return;
+
+ if(save_replay) {
+ save_replay = false;
+ auto it = bound_actions_by_id.find("save_replay");
+ if(it != bound_actions_by_id.end())
+ it->second("save_replay");
+ }
+
+ if(save_1_min_replay) {
+ save_1_min_replay = false;
+ auto it = bound_actions_by_id.find("save_1_min_replay");
+ if(it != bound_actions_by_id.end())
+ it->second("save_1_min_replay");
+ }
+
+ if(save_10_min_replay) {
+ save_10_min_replay = false;
+ auto it = bound_actions_by_id.find("save_10_min_replay");
+ if(it != bound_actions_by_id.end())
+ it->second("save_10_min_replay");
+ }
+
+ if(take_screenshot) {
+ take_screenshot = false;
+ auto it = bound_actions_by_id.find("take_screenshot");
+ if(it != bound_actions_by_id.end())
+ it->second("take_screenshot");
+ }
+
+ if(toggle_record) {
+ toggle_record = false;
+ auto it = bound_actions_by_id.find("toggle_record");
+ if(it != bound_actions_by_id.end())
+ it->second("toggle_record");
+ }
+
+ if(toggle_replay) {
+ toggle_replay = false;
+ auto it = bound_actions_by_id.find("toggle_replay");
+ if(it != bound_actions_by_id.end())
+ it->second("toggle_replay");
+ }
+
+ if(toggle_show) {
+ toggle_show = false;
+ auto it = bound_actions_by_id.find("toggle_show");
+ if(it != bound_actions_by_id.end())
+ it->second("toggle_show");
+ }
+ }
+
+ void GlobalHotkeysJoystick::read_events() {
+ js_event event;
+ while(poll(poll_fd, num_poll_fd, -1) > 0) {
+ for(int i = 0; i < num_poll_fd; ++i) {
+ if(poll_fd[i].revents & (POLLHUP|POLLERR|POLLNVAL)) {
+ if(i == event_index)
+ goto done;
+
+ if(remove_poll_fd(i))
+ --i; // This item was removed so we want to repeat the same index to continue to the next item
+
+ continue;
+ }
+
+ if(!(poll_fd[i].revents & POLLIN))
+ continue;
+
+ if(i == event_index) {
+ goto done;
+ } else if(i == hotplug_poll_index) {
+ hotplug.process_event_data(poll_fd[i].fd, [&](HotplugAction hotplug_action, const char *devname) {
+ char dev_input_filepath[1024];
+ snprintf(dev_input_filepath, sizeof(dev_input_filepath), "/dev/%s", devname);
+ switch(hotplug_action) {
+ case HotplugAction::ADD: {
+ // Cant open the /dev/input device immediately or it fails.
+ // TODO: Remove this hack when a better solution is found.
+ usleep(50 * 1000);
+ add_device(dev_input_filepath);
+ break;
+ }
+ case HotplugAction::REMOVE: {
+ if(remove_device(dev_input_filepath))
+ --i; // This item was removed so we want to repeat the same index to continue to the next item
+ break;
+ }
+ }
+ });
+ } else {
+ process_js_event(poll_fd[i].fd, event);
+ }
+ }
+ }
+
+ done:
+ ;
+ }
+
+ void GlobalHotkeysJoystick::process_js_event(int fd, js_event &event) {
+ if(read(fd, &event, sizeof(event)) != sizeof(event))
+ return;
+
+ if((event.type & JS_EVENT_BUTTON) == JS_EVENT_BUTTON) {
+ switch(event.number) {
+ case playstation_button: {
+ playstation_button_pressed = event.value == button_pressed;
+ break;
+ }
+ case options_button: {
+ if(playstation_button_pressed && event.value == button_pressed)
+ toggle_show = true;
+ break;
+ }
+ case cross_button: {
+ if(playstation_button_pressed && event.value == button_pressed)
+ save_1_min_replay = true;
+ break;
+ }
+ case triangle_button: {
+ if(playstation_button_pressed && event.value == button_pressed)
+ save_10_min_replay = true;
+ break;
+ }
+ }
+ } else if((event.type & JS_EVENT_AXIS) == JS_EVENT_AXIS && playstation_button_pressed) {
+ const int trigger_threshold = 16383;
+ const bool prev_up_pressed = up_pressed;
+ const bool prev_down_pressed = down_pressed;
+ const bool prev_left_pressed = left_pressed;
+ const bool prev_right_pressed = right_pressed;
+
+ if(event.number == axis_up_down) {
+ up_pressed = event.value <= -trigger_threshold;
+ down_pressed = event.value >= trigger_threshold;
+ } else if(event.number == axis_left_right) {
+ left_pressed = event.value <= -trigger_threshold;
+ right_pressed = event.value >= trigger_threshold;
+ }
+
+ if(up_pressed && !prev_up_pressed)
+ take_screenshot = true;
+ else if(down_pressed && !prev_down_pressed)
+ save_replay = true;
+ else if(left_pressed && !prev_left_pressed)
+ toggle_record = true;
+ else if(right_pressed && !prev_right_pressed)
+ toggle_replay = true;
+ }
+ }
+
+ bool GlobalHotkeysJoystick::add_device(const char *dev_input_filepath, bool print_error) {
+ if(num_poll_fd >= max_js_poll_fd) {
+ fprintf(stderr, "Warning: failed to add joystick device %s, too many joysticks have been added\n", dev_input_filepath);
+ return false;
+ }
+
+ const int dev_input_id = get_js_dev_input_id_from_filepath(dev_input_filepath);
+ if(dev_input_id == -1)
+ return false;
+
+ const int fd = open(dev_input_filepath, O_RDONLY);
+ if(fd <= 0) {
+ if(print_error)
+ fprintf(stderr, "Error: failed to add joystick %s, error: %s\n", dev_input_filepath, strerror(errno));
+ return false;
+ }
+
+ poll_fd[num_poll_fd] = {
+ fd,
+ POLLIN,
+ 0
+ };
+
+ extra_data[num_poll_fd] = {
+ dev_input_id
+ };
+
+ ++num_poll_fd;
+ fprintf(stderr, "Info: added joystick: %s\n", dev_input_filepath);
+ return true;
+ }
+
+ bool GlobalHotkeysJoystick::remove_device(const char *dev_input_filepath) {
+ const int dev_input_id = get_js_dev_input_id_from_filepath(dev_input_filepath);
+ if(dev_input_id == -1)
+ return false;
+
+ const int poll_fd_index = get_poll_fd_index_by_dev_input_id(dev_input_id);
+ if(poll_fd_index == -1)
+ return false;
+
+ fprintf(stderr, "Info: removed joystick: %s\n", dev_input_filepath);
+ return remove_poll_fd(poll_fd_index);
+ }
+
+ bool GlobalHotkeysJoystick::remove_poll_fd(int index) {
+ if(index < 0 || index >= num_poll_fd)
+ return false;
+
+ close(poll_fd[index].fd);
+ for(int i = index + 1; i < num_poll_fd; ++i) {
+ poll_fd[i - 1] = poll_fd[i];
+ extra_data[i - 1] = extra_data[i];
+ }
+ --num_poll_fd;
+ return true;
+ }
+
+ int GlobalHotkeysJoystick::get_poll_fd_index_by_dev_input_id(int dev_input_id) const {
+ for(int i = 0; i < num_poll_fd; ++i) {
+ if(dev_input_id == extra_data[i].dev_input_id)
+ return i;
+ }
+ return -1;
+ }
+}
diff --git a/src/GlobalHotkeys/GlobalHotkeysLinux.cpp b/src/GlobalHotkeys/GlobalHotkeysLinux.cpp
new file mode 100644
index 0000000..a56bbc6
--- /dev/null
+++ b/src/GlobalHotkeys/GlobalHotkeysLinux.cpp
@@ -0,0 +1,275 @@
+#include "../../include/GlobalHotkeys/GlobalHotkeysLinux.hpp"
+#include <sys/wait.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <string.h>
+
+extern "C" {
+#include <mgl/mgl.h>
+}
+#include <X11/Xlib.h>
+#include <X11/keysym.h>
+#include <linux/input-event-codes.h>
+
+#define PIPE_READ 0
+#define PIPE_WRITE 1
+
+namespace gsr {
+ static const char* grab_type_to_arg(GlobalHotkeysLinux::GrabType grab_type) {
+ switch(grab_type) {
+ case GlobalHotkeysLinux::GrabType::ALL: return "--all";
+ case GlobalHotkeysLinux::GrabType::VIRTUAL: return "--virtual";
+ }
+ return "--all";
+ }
+
+ static inline uint8_t x11_keycode_to_linux_keycode(uint8_t code) {
+ return code - 8;
+ }
+
+ static std::vector<uint8_t> modifiers_to_linux_keys(uint32_t modifiers) {
+ std::vector<uint8_t> result;
+ if(modifiers & HOTKEY_MOD_LSHIFT)
+ result.push_back(KEY_LEFTSHIFT);
+ if(modifiers & HOTKEY_MOD_RSHIFT)
+ result.push_back(KEY_RIGHTSHIFT);
+ if(modifiers & HOTKEY_MOD_LCTRL)
+ result.push_back(KEY_LEFTCTRL);
+ if(modifiers & HOTKEY_MOD_RCTRL)
+ result.push_back(KEY_RIGHTCTRL);
+ if(modifiers & HOTKEY_MOD_LALT)
+ result.push_back(KEY_LEFTALT);
+ if(modifiers & HOTKEY_MOD_RALT)
+ result.push_back(KEY_RIGHTALT);
+ if(modifiers & HOTKEY_MOD_LSUPER)
+ result.push_back(KEY_LEFTMETA);
+ if(modifiers & HOTKEY_MOD_RSUPER)
+ result.push_back(KEY_RIGHTMETA);
+ return result;
+ }
+
+ static std::string linux_keys_to_command_string(const uint8_t *keys, size_t size) {
+ std::string result;
+ for(size_t i = 0; i < size; ++i) {
+ if(!result.empty())
+ result += "+";
+ result += std::to_string(keys[i]);
+ }
+ return result;
+ }
+
+ static bool x11_key_is_alpha_numerical(KeySym keysym) {
+ return (keysym >= XK_A && keysym <= XK_Z) || (keysym >= XK_a && keysym <= XK_z) || (keysym >= XK_0 && keysym <= XK_9);
+ }
+
+ GlobalHotkeysLinux::GlobalHotkeysLinux(GrabType grab_type) : grab_type(grab_type) {
+ for(int i = 0; i < 2; ++i) {
+ read_pipes[i] = -1;
+ write_pipes[i] = -1;
+ }
+ }
+
+ GlobalHotkeysLinux::~GlobalHotkeysLinux() {
+ if(write_pipes[PIPE_WRITE] > 0) {
+ char command[32];
+ const int command_size = snprintf(command, sizeof(command), "exit\n");
+ if(write(write_pipes[PIPE_WRITE], command, command_size) != command_size) {
+ fprintf(stderr, "Error: GlobalHotkeysLinux::~GlobalHotkeysLinux: failed to write command to gsr-global-hotkeys, error: %s\n", strerror(errno));
+ close_fds();
+ }
+ } else {
+ close_fds();
+ }
+
+ if(process_id > 0) {
+ int status;
+ waitpid(process_id, &status, 0);
+ }
+
+ close_fds();
+ }
+
+ void GlobalHotkeysLinux::close_fds() {
+ for(int i = 0; i < 2; ++i) {
+ if(read_pipes[i] > 0) {
+ close(read_pipes[i]);
+ read_pipes[i] = -1;
+ }
+
+ if(write_pipes[i] > 0) {
+ close(write_pipes[i]);
+ write_pipes[i] = -1;
+ }
+ }
+
+ if(read_file) {
+ fclose(read_file);
+ read_file = nullptr;
+ }
+ }
+
+ bool GlobalHotkeysLinux::start() {
+ const char *grab_type_arg = grab_type_to_arg(grab_type);
+ const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
+ const char *user_homepath = getenv("HOME");
+ if(!user_homepath)
+ user_homepath = "/tmp";
+
+ if(process_id > 0)
+ return false;
+
+ if(pipe(read_pipes) == -1)
+ return false;
+
+ if(pipe(write_pipes) == -1) {
+ for(int i = 0; i < 2; ++i) {
+ close(read_pipes[i]);
+ read_pipes[i] = -1;
+ }
+ return false;
+ }
+
+ const pid_t pid = vfork();
+ if(pid == -1) {
+ perror("Failed to vfork");
+ for(int i = 0; i < 2; ++i) {
+ close(read_pipes[i]);
+ close(write_pipes[i]);
+ read_pipes[i] = -1;
+ write_pipes[i] = -1;
+ }
+ return false;
+ } else if(pid == 0) { /* child */
+ dup2(read_pipes[PIPE_WRITE], STDOUT_FILENO);
+ for(int i = 0; i < 2; ++i) {
+ close(read_pipes[i]);
+ }
+
+ dup2(write_pipes[PIPE_READ], STDIN_FILENO);
+ for(int i = 0; i < 2; ++i) {
+ close(write_pipes[i]);
+ }
+
+ if(inside_flatpak) {
+ const char *args[] = { "flatpak-spawn", "--host", "/var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/bin/kms-server-proxy", "launch-gsr-global-hotkeys", user_homepath, grab_type_arg, nullptr };
+ execvp(args[0], (char* const*)args);
+ } else {
+ const char *args[] = { "gsr-global-hotkeys", grab_type_arg, nullptr };
+ execvp(args[0], (char* const*)args);
+ }
+
+ perror("gsr-global-hotkeys");
+ _exit(127);
+ } else { /* parent */
+ process_id = pid;
+
+ close(read_pipes[PIPE_WRITE]);
+ read_pipes[PIPE_WRITE] = -1;
+
+ close(write_pipes[PIPE_READ]);
+ write_pipes[PIPE_READ] = -1;
+
+ fcntl(read_pipes[PIPE_READ], F_SETFL, fcntl(read_pipes[PIPE_READ], F_GETFL) | O_NONBLOCK);
+ read_file = fdopen(read_pipes[PIPE_READ], "r");
+ if(read_file)
+ read_pipes[PIPE_READ] = -1;
+ else
+ fprintf(stderr, "fdopen failed for read, error: %s\n", strerror(errno));
+ }
+
+ return true;
+ }
+
+ bool GlobalHotkeysLinux::bind_key_press(Hotkey hotkey, const std::string &id, GlobalHotkeyCallback callback) {
+ if(process_id <= 0)
+ return false;
+
+ if(bound_actions_by_id.find(id) != bound_actions_by_id.end())
+ return false;
+
+ if(id.find(' ') != std::string::npos || id.find('\n') != std::string::npos) {
+ fprintf(stderr, "Error: GlobalHotkeysLinux::bind_key_press: id \"%s\" contains either space or newline\n", id.c_str());
+ return false;
+ }
+
+ if(hotkey.key == 0) {
+ //fprintf(stderr, "Error: GlobalHotkeysLinux::bind_key_press: hotkey requires a key\n");
+ return false;
+ }
+
+ if(hotkey.modifiers == 0 && x11_key_is_alpha_numerical(hotkey.key)) {
+ //fprintf(stderr, "Error: GlobalHotkeysLinux::bind_key_press: hotkey requires a modifier\n");
+ return false;
+ }
+
+ mgl_context *context = mgl_get_context();
+ Display *display = (Display*)context->connection;
+ const uint8_t keycode = x11_keycode_to_linux_keycode(XKeysymToKeycode(display, hotkey.key));
+ const std::vector<uint8_t> modifiers = modifiers_to_linux_keys(hotkey.modifiers);
+ const std::string modifiers_command = linux_keys_to_command_string(modifiers.data(), modifiers.size());
+
+ char command[256];
+ int command_size = 0;
+ if(modifiers_command.empty())
+ command_size = snprintf(command, sizeof(command), "bind %s %d\n", id.c_str(), (int)keycode);
+ else
+ command_size = snprintf(command, sizeof(command), "bind %s %d+%s\n", id.c_str(), (int)keycode, modifiers_command.c_str());
+
+ if(write(write_pipes[PIPE_WRITE], command, command_size) != command_size) {
+ fprintf(stderr, "Error: GlobalHotkeysLinux::bind_key_press: failed to write command to gsr-global-hotkeys, error: %s\n", strerror(errno));
+ return false;
+ }
+
+ bound_actions_by_id[id] = std::move(callback);
+ return true;
+ }
+
+ void GlobalHotkeysLinux::unbind_all_keys() {
+ if(process_id <= 0)
+ return;
+
+ if(bound_actions_by_id.empty())
+ return;
+
+ char command[32];
+ const int command_size = snprintf(command, sizeof(command), "unbind_all\n");
+ if(write(write_pipes[PIPE_WRITE], command, command_size) != command_size) {
+ fprintf(stderr, "Error: GlobalHotkeysLinux::unbind_all_keys: failed to write command to gsr-global-hotkeys, error: %s\n", strerror(errno));
+ }
+ bound_actions_by_id.clear();
+ }
+
+ void GlobalHotkeysLinux::poll_events() {
+ if(process_id <= 0) {
+ //fprintf(stderr, "error: GlobalHotkeysLinux::poll_events failed, process has not been started yet. Use GlobalHotkeysLinux::start to start the process first\n");
+ return;
+ }
+
+ if(!read_file) {
+ //fprintf(stderr, "error: GlobalHotkeysLinux::poll_events failed, read file hasn't opened\n");
+ return;
+ }
+
+ std::string action;
+ char buffer[256];
+ while(true) {
+ char *line = fgets(buffer, sizeof(buffer), read_file);
+ if(!line)
+ break;
+
+ int line_len = strlen(line);
+ if(line_len == 0)
+ continue;
+
+ if(line[line_len - 1] == '\n') {
+ line[line_len - 1] = '\0';
+ --line_len;
+ }
+
+ action = line;
+ auto it = bound_actions_by_id.find(action);
+ if(it != bound_actions_by_id.end())
+ it->second(action);
+ }
+ }
+}
diff --git a/src/GlobalHotkeys/GlobalHotkeysX11.cpp b/src/GlobalHotkeys/GlobalHotkeysX11.cpp
new file mode 100644
index 0000000..bc79ce8
--- /dev/null
+++ b/src/GlobalHotkeys/GlobalHotkeysX11.cpp
@@ -0,0 +1,201 @@
+#include "../../include/GlobalHotkeys/GlobalHotkeysX11.hpp"
+#include <X11/keysym.h>
+#include <mglpp/window/Event.hpp>
+#include <assert.h>
+
+namespace gsr {
+ static bool x_failed = false;
+ static int xerror_grab_error(Display*, XErrorEvent*) {
+ x_failed = true;
+ return 0;
+ }
+
+ static unsigned int x11_get_numlock_mask(Display *dpy) {
+ unsigned int numlockmask = 0;
+ KeyCode numlock_keycode = XKeysymToKeycode(dpy, XK_Num_Lock);
+ XModifierKeymap *modmap = XGetModifierMapping(dpy);
+ if(modmap) {
+ for(int i = 0; i < 8; ++i) {
+ for(int j = 0; j < modmap->max_keypermod; ++j) {
+ if(modmap->modifiermap[i * modmap->max_keypermod + j] == numlock_keycode)
+ numlockmask = (1 << i);
+ }
+ }
+ XFreeModifiermap(modmap);
+ }
+ return numlockmask;
+ }
+
+ static KeySym mgl_key_to_key_sym(mgl::Keyboard::Key key) {
+ switch(key) {
+ case mgl::Keyboard::Z: return XK_z;
+ case mgl::Keyboard::F7: return XK_F7;
+ case mgl::Keyboard::F8: return XK_F8;
+ case mgl::Keyboard::F9: return XK_F9;
+ case mgl::Keyboard::F10: return XK_F10;
+ default: return None;
+ }
+ }
+
+ static uint32_t mgl_key_modifiers_to_x11_modifier_mask(const mgl::Event::KeyEvent &key_event) {
+ uint32_t mask = 0;
+ if(key_event.shift)
+ mask |= ShiftMask;
+ if(key_event.control)
+ mask |= ControlMask;
+ if(key_event.alt)
+ mask |= Mod1Mask;
+ if(key_event.system)
+ mask |= Mod4Mask;
+ return mask;
+ }
+
+ static uint32_t modifiers_to_x11_modifiers(uint32_t modifiers) {
+ uint32_t result = 0;
+ if(modifiers & HOTKEY_MOD_LSHIFT)
+ result |= ShiftMask;
+ if(modifiers & HOTKEY_MOD_RSHIFT)
+ result |= ShiftMask;
+ if(modifiers & HOTKEY_MOD_LCTRL)
+ result |= ControlMask;
+ if(modifiers & HOTKEY_MOD_RCTRL)
+ result |= ControlMask;
+ if(modifiers & HOTKEY_MOD_LALT)
+ result |= Mod1Mask;
+ if(modifiers & HOTKEY_MOD_RALT)
+ result |= Mod5Mask;
+ if(modifiers & HOTKEY_MOD_LSUPER)
+ result |= Mod4Mask;
+ if(modifiers & HOTKEY_MOD_RSUPER)
+ result |= Mod4Mask;
+ return result;
+ }
+
+ GlobalHotkeysX11::GlobalHotkeysX11() {
+ dpy = XOpenDisplay(NULL);
+ if(!dpy)
+ fprintf(stderr, "GlobalHotkeysX11 error: failed to connect to X11 server, global hotkeys wont be available\n");
+ }
+
+ GlobalHotkeysX11::~GlobalHotkeysX11() {
+ if(dpy) {
+ XCloseDisplay(dpy);
+ dpy = nullptr;
+ }
+ }
+
+ bool GlobalHotkeysX11::bind_key_press(Hotkey hotkey, const std::string &id, GlobalHotkeyCallback callback) {
+ if(!dpy)
+ return false;
+
+ auto it = bound_keys_by_id.find(id);
+ if(it != bound_keys_by_id.end())
+ return false;
+
+ x_failed = false;
+ XErrorHandler prev_xerror = XSetErrorHandler(xerror_grab_error);
+
+ const uint32_t modifiers_x11 = modifiers_to_x11_modifiers(hotkey.modifiers);
+ unsigned int numlock_mask = x11_get_numlock_mask(dpy);
+ unsigned int modifiers[] = { 0, LockMask, numlock_mask, numlock_mask|LockMask };
+ for(int i = 0; i < 4; ++i) {
+ XGrabKey(dpy, XKeysymToKeycode(dpy, hotkey.key), modifiers_x11 | modifiers[i], DefaultRootWindow(dpy), False, GrabModeAsync, GrabModeAsync);
+ }
+ XSync(dpy, False);
+
+ if(x_failed) {
+ for(int i = 0; i < 4; ++i) {
+ XUngrabKey(dpy, XKeysymToKeycode(dpy, hotkey.key), modifiers_x11 | modifiers[i], DefaultRootWindow(dpy));
+ }
+ XSync(dpy, False);
+ XSetErrorHandler(prev_xerror);
+ return false;
+ } else {
+ XSetErrorHandler(prev_xerror);
+ bound_keys_by_id[id] = { hotkey, std::move(callback) };
+ return true;
+ }
+ }
+
+ void GlobalHotkeysX11::unbind_key_press(const std::string &id) {
+ if(!dpy)
+ return;
+
+ auto it = bound_keys_by_id.find(id);
+ if(it == bound_keys_by_id.end())
+ return;
+
+ x_failed = false;
+ XErrorHandler prev_xerror = XSetErrorHandler(xerror_grab_error);
+
+ const uint32_t modifiers_x11 = modifiers_to_x11_modifiers(it->second.hotkey.modifiers);
+ unsigned int numlock_mask = x11_get_numlock_mask(dpy);
+ unsigned int modifiers[] = { 0, LockMask, numlock_mask, numlock_mask|LockMask };
+ for(int i = 0; i < 4; ++i) {
+ XUngrabKey(dpy, XKeysymToKeycode(dpy, it->second.hotkey.key), modifiers_x11 | modifiers[i], DefaultRootWindow(dpy));
+ }
+ XSync(dpy, False);
+
+ XSetErrorHandler(prev_xerror);
+ bound_keys_by_id.erase(id);
+ }
+
+ void GlobalHotkeysX11::unbind_all_keys() {
+ if(!dpy)
+ return;
+
+ x_failed = false;
+ XErrorHandler prev_xerror = XSetErrorHandler(xerror_grab_error);
+
+ unsigned int numlock_mask = x11_get_numlock_mask(dpy);
+ unsigned int modifiers[] = { 0, LockMask, numlock_mask, numlock_mask|LockMask };
+ for(auto it = bound_keys_by_id.begin(); it != bound_keys_by_id.end();) {
+ const uint32_t modifiers_x11 = modifiers_to_x11_modifiers(it->second.hotkey.modifiers);
+ for(int i = 0; i < 4; ++i) {
+ XUngrabKey(dpy, XKeysymToKeycode(dpy, it->second.hotkey.key), modifiers_x11 | modifiers[i], DefaultRootWindow(dpy));
+ }
+ }
+ bound_keys_by_id.clear();
+ XSync(dpy, False);
+
+ XSetErrorHandler(prev_xerror);
+ }
+
+ void GlobalHotkeysX11::poll_events() {
+ if(!dpy)
+ return;
+
+ while(XPending(dpy)) {
+ XNextEvent(dpy, &xev);
+ if(xev.type == KeyPress) {
+ const KeySym key_sym = XLookupKeysym(&xev.xkey, 0);
+ call_hotkey_callback({ (uint32_t)key_sym, xev.xkey.state });
+ }
+ }
+ }
+
+ bool GlobalHotkeysX11::on_event(mgl::Event &event) {
+ if(event.type != mgl::Event::KeyPressed)
+ return true;
+
+ // Note: not all keys are mapped in mgl_key_to_key_sym. If more hotkeys are added or changed then add the key mapping there
+ const KeySym key_sym = mgl_key_to_key_sym(event.key.code);
+ const uint32_t modifiers = mgl_key_modifiers_to_x11_modifier_mask(event.key);
+ return !call_hotkey_callback(Hotkey{(uint32_t)key_sym, modifiers});
+ }
+
+ static unsigned int key_state_without_locks(unsigned int key_state) {
+ return key_state & ~(Mod2Mask|LockMask);
+ }
+
+ bool GlobalHotkeysX11::call_hotkey_callback(Hotkey hotkey) const {
+ const uint32_t modifiers_x11 = modifiers_to_x11_modifiers(hotkey.modifiers);
+ for(const auto &[key, val] : bound_keys_by_id) {
+ if(val.hotkey.key == hotkey.key && key_state_without_locks(modifiers_to_x11_modifiers(val.hotkey.modifiers)) == key_state_without_locks(modifiers_x11)) {
+ val.callback(key);
+ return true;
+ }
+ }
+ return false;
+ }
+} \ No newline at end of file