From d08ea692771caa8e385412c2f992089672773e30 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 3 May 2025 12:03:43 +0200 Subject: Keep keyboard led when turning on global hotkeys, move files --- include/CursorTracker.hpp | 23 -------- include/CursorTracker/CursorTracker.hpp | 23 ++++++++ include/CursorTracker/CursorTrackerWayland.hpp | 43 +++++++++++++++ include/CursorTracker/CursorTrackerX11.hpp | 20 +++++++ include/CursorTrackerWayland.hpp | 43 --------------- include/CursorTrackerX11.hpp | 20 ------- include/GlobalHotkeys.hpp | 45 ---------------- include/GlobalHotkeys/GlobalHotkeys.hpp | 45 ++++++++++++++++ include/GlobalHotkeys/GlobalHotkeysJoystick.hpp | 70 +++++++++++++++++++++++++ include/GlobalHotkeys/GlobalHotkeysLinux.hpp | 34 ++++++++++++ include/GlobalHotkeys/GlobalHotkeysX11.hpp | 34 ++++++++++++ include/GlobalHotkeysJoystick.hpp | 70 ------------------------- include/GlobalHotkeysLinux.hpp | 34 ------------ include/GlobalHotkeysX11.hpp | 34 ------------ include/Overlay.hpp | 4 +- 15 files changed, 271 insertions(+), 271 deletions(-) delete mode 100644 include/CursorTracker.hpp create mode 100644 include/CursorTracker/CursorTracker.hpp create mode 100644 include/CursorTracker/CursorTrackerWayland.hpp create mode 100644 include/CursorTracker/CursorTrackerX11.hpp delete mode 100644 include/CursorTrackerWayland.hpp delete mode 100644 include/CursorTrackerX11.hpp delete mode 100644 include/GlobalHotkeys.hpp create mode 100644 include/GlobalHotkeys/GlobalHotkeys.hpp create mode 100644 include/GlobalHotkeys/GlobalHotkeysJoystick.hpp create mode 100644 include/GlobalHotkeys/GlobalHotkeysLinux.hpp create mode 100644 include/GlobalHotkeys/GlobalHotkeysX11.hpp delete mode 100644 include/GlobalHotkeysJoystick.hpp delete mode 100644 include/GlobalHotkeysLinux.hpp delete mode 100644 include/GlobalHotkeysX11.hpp (limited to 'include') diff --git a/include/CursorTracker.hpp b/include/CursorTracker.hpp deleted file mode 100644 index ff7374f..0000000 --- a/include/CursorTracker.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace gsr { - struct CursorInfo { - mgl::vec2i position; - std::string monitor_name; - }; - - class CursorTracker { - public: - CursorTracker() = default; - CursorTracker(const CursorTracker&) = delete; - CursorTracker& operator=(const CursorTracker&) = delete; - virtual ~CursorTracker() = default; - - virtual void update() = 0; - virtual std::optional get_latest_cursor_info() = 0; - }; -} \ No newline at end of file diff --git a/include/CursorTracker/CursorTracker.hpp b/include/CursorTracker/CursorTracker.hpp new file mode 100644 index 0000000..ff7374f --- /dev/null +++ b/include/CursorTracker/CursorTracker.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include + +namespace gsr { + struct CursorInfo { + mgl::vec2i position; + std::string monitor_name; + }; + + class CursorTracker { + public: + CursorTracker() = default; + CursorTracker(const CursorTracker&) = delete; + CursorTracker& operator=(const CursorTracker&) = delete; + virtual ~CursorTracker() = default; + + virtual void update() = 0; + virtual std::optional get_latest_cursor_info() = 0; + }; +} \ No newline at end of file diff --git a/include/CursorTracker/CursorTrackerWayland.hpp b/include/CursorTracker/CursorTrackerWayland.hpp new file mode 100644 index 0000000..1eeee83 --- /dev/null +++ b/include/CursorTracker/CursorTrackerWayland.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include "CursorTracker.hpp" +#include +#include + +struct wl_display; +struct wl_registry; +struct wl_output; +struct zxdg_output_manager_v1; +struct zxdg_output_v1; + +namespace gsr { + struct WaylandOutput { + uint32_t wl_name; + struct wl_output *output; + struct zxdg_output_v1 *xdg_output; + mgl::vec2i pos; + mgl::vec2i size; + int32_t transform; + std::string name; + }; + + class CursorTrackerWayland : public CursorTracker { + public: + CursorTrackerWayland(const char *card_path); + CursorTrackerWayland(const CursorTrackerWayland&) = delete; + CursorTrackerWayland& operator=(const CursorTrackerWayland&) = delete; + ~CursorTrackerWayland(); + + void update() override; + std::optional get_latest_cursor_info() override; + + std::vector monitors; + struct zxdg_output_manager_v1 *xdg_output_manager = nullptr; + private: + void set_monitor_outputs_from_xdg_output(struct wl_display *dpy); + private: + int drm_fd = -1; + mgl::vec2i latest_cursor_position; // Position of the cursor within the monitor + int latest_crtc_id = -1; + }; +} \ No newline at end of file diff --git a/include/CursorTracker/CursorTrackerX11.hpp b/include/CursorTracker/CursorTrackerX11.hpp new file mode 100644 index 0000000..66618c4 --- /dev/null +++ b/include/CursorTracker/CursorTrackerX11.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "CursorTracker.hpp" + +typedef struct _XDisplay Display; + +namespace gsr { + class CursorTrackerX11 : public CursorTracker { + public: + CursorTrackerX11(Display *dpy); + CursorTrackerX11(const CursorTrackerX11&) = delete; + CursorTrackerX11& operator=(const CursorTrackerX11&) = delete; + ~CursorTrackerX11() = default; + + void update() override {} + std::optional get_latest_cursor_info() override; + private: + Display *dpy = nullptr; + }; +} \ No newline at end of file diff --git a/include/CursorTrackerWayland.hpp b/include/CursorTrackerWayland.hpp deleted file mode 100644 index 1eeee83..0000000 --- a/include/CursorTrackerWayland.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include "CursorTracker.hpp" -#include -#include - -struct wl_display; -struct wl_registry; -struct wl_output; -struct zxdg_output_manager_v1; -struct zxdg_output_v1; - -namespace gsr { - struct WaylandOutput { - uint32_t wl_name; - struct wl_output *output; - struct zxdg_output_v1 *xdg_output; - mgl::vec2i pos; - mgl::vec2i size; - int32_t transform; - std::string name; - }; - - class CursorTrackerWayland : public CursorTracker { - public: - CursorTrackerWayland(const char *card_path); - CursorTrackerWayland(const CursorTrackerWayland&) = delete; - CursorTrackerWayland& operator=(const CursorTrackerWayland&) = delete; - ~CursorTrackerWayland(); - - void update() override; - std::optional get_latest_cursor_info() override; - - std::vector monitors; - struct zxdg_output_manager_v1 *xdg_output_manager = nullptr; - private: - void set_monitor_outputs_from_xdg_output(struct wl_display *dpy); - private: - int drm_fd = -1; - mgl::vec2i latest_cursor_position; // Position of the cursor within the monitor - int latest_crtc_id = -1; - }; -} \ No newline at end of file diff --git a/include/CursorTrackerX11.hpp b/include/CursorTrackerX11.hpp deleted file mode 100644 index 66618c4..0000000 --- a/include/CursorTrackerX11.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include "CursorTracker.hpp" - -typedef struct _XDisplay Display; - -namespace gsr { - class CursorTrackerX11 : public CursorTracker { - public: - CursorTrackerX11(Display *dpy); - CursorTrackerX11(const CursorTrackerX11&) = delete; - CursorTrackerX11& operator=(const CursorTrackerX11&) = delete; - ~CursorTrackerX11() = default; - - void update() override {} - std::optional get_latest_cursor_info() override; - private: - Display *dpy = nullptr; - }; -} \ No newline at end of file diff --git a/include/GlobalHotkeys.hpp b/include/GlobalHotkeys.hpp deleted file mode 100644 index 2927fa7..0000000 --- a/include/GlobalHotkeys.hpp +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace mgl { - class Event; -} - -namespace gsr { - enum HotkeyModifier : uint32_t { - HOTKEY_MOD_LSHIFT = 1 << 0, - HOTKEY_MOD_RSHIFT = 1 << 1, - HOTKEY_MOD_LCTRL = 1 << 2, - HOTKEY_MOD_RCTRL = 1 << 3, - HOTKEY_MOD_LALT = 1 << 4, - HOTKEY_MOD_RALT = 1 << 5, - HOTKEY_MOD_LSUPER = 1 << 6, - HOTKEY_MOD_RSUPER = 1 << 7 - }; - - struct Hotkey { - uint32_t key = 0; // X11 keysym - uint32_t modifiers = 0; // HotkeyModifier - }; - - using GlobalHotkeyCallback = std::function; - - class GlobalHotkeys { - public: - GlobalHotkeys() = default; - GlobalHotkeys(const GlobalHotkeys&) = delete; - GlobalHotkeys& operator=(const GlobalHotkeys&) = delete; - virtual ~GlobalHotkeys() = default; - - virtual bool bind_key_press(Hotkey hotkey, const std::string &id, GlobalHotkeyCallback callback) { (void)hotkey; (void)id; (void)callback; return false; } - virtual void unbind_key_press(const std::string &id) { (void)id; } - virtual void unbind_all_keys() {} - virtual bool bind_action(const std::string &id, GlobalHotkeyCallback callback) { (void)id; (void)callback; return false; }; - virtual void poll_events() = 0; - // Returns true if the event wasn't consumed (if the event didn't match a key that has been bound) - virtual bool on_event(mgl::Event &event) { (void)event; return true; } - }; -} \ No newline at end of file diff --git a/include/GlobalHotkeys/GlobalHotkeys.hpp b/include/GlobalHotkeys/GlobalHotkeys.hpp new file mode 100644 index 0000000..2927fa7 --- /dev/null +++ b/include/GlobalHotkeys/GlobalHotkeys.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include + +namespace mgl { + class Event; +} + +namespace gsr { + enum HotkeyModifier : uint32_t { + HOTKEY_MOD_LSHIFT = 1 << 0, + HOTKEY_MOD_RSHIFT = 1 << 1, + HOTKEY_MOD_LCTRL = 1 << 2, + HOTKEY_MOD_RCTRL = 1 << 3, + HOTKEY_MOD_LALT = 1 << 4, + HOTKEY_MOD_RALT = 1 << 5, + HOTKEY_MOD_LSUPER = 1 << 6, + HOTKEY_MOD_RSUPER = 1 << 7 + }; + + struct Hotkey { + uint32_t key = 0; // X11 keysym + uint32_t modifiers = 0; // HotkeyModifier + }; + + using GlobalHotkeyCallback = std::function; + + class GlobalHotkeys { + public: + GlobalHotkeys() = default; + GlobalHotkeys(const GlobalHotkeys&) = delete; + GlobalHotkeys& operator=(const GlobalHotkeys&) = delete; + virtual ~GlobalHotkeys() = default; + + virtual bool bind_key_press(Hotkey hotkey, const std::string &id, GlobalHotkeyCallback callback) { (void)hotkey; (void)id; (void)callback; return false; } + virtual void unbind_key_press(const std::string &id) { (void)id; } + virtual void unbind_all_keys() {} + virtual bool bind_action(const std::string &id, GlobalHotkeyCallback callback) { (void)id; (void)callback; return false; }; + virtual void poll_events() = 0; + // Returns true if the event wasn't consumed (if the event didn't match a key that has been bound) + virtual bool on_event(mgl::Event &event) { (void)event; return true; } + }; +} \ No newline at end of file diff --git a/include/GlobalHotkeys/GlobalHotkeysJoystick.hpp b/include/GlobalHotkeys/GlobalHotkeysJoystick.hpp new file mode 100644 index 0000000..4b266cb --- /dev/null +++ b/include/GlobalHotkeys/GlobalHotkeysJoystick.hpp @@ -0,0 +1,70 @@ +#pragma once + +#include "GlobalHotkeys.hpp" +#include "../Hotplug.hpp" +#include +#include +#include +#include + +namespace gsr { + static constexpr int max_js_poll_fd = 16; + + class GlobalHotkeysJoystick : public GlobalHotkeys { + class GlobalHotkeysJoystickHotplugDelegate; + public: + GlobalHotkeysJoystick() = default; + GlobalHotkeysJoystick(const GlobalHotkeysJoystick&) = delete; + GlobalHotkeysJoystick& operator=(const GlobalHotkeysJoystick&) = delete; + ~GlobalHotkeysJoystick() override; + + bool start(); + // Currently valid ids: + // save_replay + // save_1_min_replay + // save_10_min_replay + // take_screenshot + // toggle_record + // toggle_replay + // toggle_show + bool bind_action(const std::string &id, GlobalHotkeyCallback callback) override; + void poll_events() override; + private: + void read_events(); + void process_js_event(int fd, js_event &event); + bool add_device(const char *dev_input_filepath, bool print_error = true); + bool remove_device(const char *dev_input_filepath); + bool remove_poll_fd(int index); + // Returns -1 if not found + int get_poll_fd_index_by_dev_input_id(int dev_input_id) const; + private: + struct ExtraData { + int dev_input_id = 0; + }; + + std::unordered_map bound_actions_by_id; + std::thread read_thread; + + pollfd poll_fd[max_js_poll_fd]; + ExtraData extra_data[max_js_poll_fd]; + int num_poll_fd = 0; + int event_fd = -1; + int event_index = -1; + + bool playstation_button_pressed = false; + bool up_pressed = false; + bool down_pressed = false; + bool left_pressed = false; + bool right_pressed = false; + + bool save_replay = false; + bool save_1_min_replay = false; + bool save_10_min_replay = false; + bool take_screenshot = false; + bool toggle_record = false; + bool toggle_replay = false; + bool toggle_show = false; + int hotplug_poll_index = -1; + Hotplug hotplug; + }; +} \ No newline at end of file diff --git a/include/GlobalHotkeys/GlobalHotkeysLinux.hpp b/include/GlobalHotkeys/GlobalHotkeysLinux.hpp new file mode 100644 index 0000000..959d095 --- /dev/null +++ b/include/GlobalHotkeys/GlobalHotkeysLinux.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include "GlobalHotkeys.hpp" +#include +#include + +namespace gsr { + class GlobalHotkeysLinux : public GlobalHotkeys { + public: + enum class GrabType { + ALL, + VIRTUAL + }; + + GlobalHotkeysLinux(GrabType grab_type); + GlobalHotkeysLinux(const GlobalHotkeysLinux&) = delete; + GlobalHotkeysLinux& operator=(const GlobalHotkeysLinux&) = delete; + ~GlobalHotkeysLinux() override; + + bool start(); + bool bind_key_press(Hotkey hotkey, const std::string &id, GlobalHotkeyCallback callback) override; + void unbind_all_keys() override; + void poll_events() override; + private: + void close_fds(); + private: + pid_t process_id = 0; + int read_pipes[2]; + int write_pipes[2]; + FILE *read_file = nullptr; + std::unordered_map bound_actions_by_id; + GrabType grab_type; + }; +} \ No newline at end of file diff --git a/include/GlobalHotkeys/GlobalHotkeysX11.hpp b/include/GlobalHotkeys/GlobalHotkeysX11.hpp new file mode 100644 index 0000000..610399a --- /dev/null +++ b/include/GlobalHotkeys/GlobalHotkeysX11.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include "GlobalHotkeys.hpp" +#include +#include + +namespace gsr { + class GlobalHotkeysX11 : public GlobalHotkeys { + public: + GlobalHotkeysX11(); + GlobalHotkeysX11(const GlobalHotkeysX11&) = delete; + GlobalHotkeysX11& operator=(const GlobalHotkeysX11&) = delete; + ~GlobalHotkeysX11() override; + + // Hotkey key is a KeySym (XK_z for example) and modifiers is a bitmask of X11 modifier masks (for example ShiftMask | Mod1Mask) + bool bind_key_press(Hotkey hotkey, const std::string &id, GlobalHotkeyCallback callback) override; + void unbind_key_press(const std::string &id) override; + void unbind_all_keys() override; + void poll_events() override; + bool on_event(mgl::Event &event) override; + private: + // Returns true if a key bind has been registered for the hotkey + bool call_hotkey_callback(Hotkey hotkey) const; + private: + struct HotkeyData { + Hotkey hotkey; + GlobalHotkeyCallback callback; + }; + + Display *dpy = nullptr; + XEvent xev; + std::unordered_map bound_keys_by_id; + }; +} \ No newline at end of file diff --git a/include/GlobalHotkeysJoystick.hpp b/include/GlobalHotkeysJoystick.hpp deleted file mode 100644 index fde5be2..0000000 --- a/include/GlobalHotkeysJoystick.hpp +++ /dev/null @@ -1,70 +0,0 @@ -#pragma once - -#include "GlobalHotkeys.hpp" -#include "Hotplug.hpp" -#include -#include -#include -#include - -namespace gsr { - static constexpr int max_js_poll_fd = 16; - - class GlobalHotkeysJoystick : public GlobalHotkeys { - class GlobalHotkeysJoystickHotplugDelegate; - public: - GlobalHotkeysJoystick() = default; - GlobalHotkeysJoystick(const GlobalHotkeysJoystick&) = delete; - GlobalHotkeysJoystick& operator=(const GlobalHotkeysJoystick&) = delete; - ~GlobalHotkeysJoystick() override; - - bool start(); - // Currently valid ids: - // save_replay - // save_1_min_replay - // save_10_min_replay - // take_screenshot - // toggle_record - // toggle_replay - // toggle_show - bool bind_action(const std::string &id, GlobalHotkeyCallback callback) override; - void poll_events() override; - private: - void read_events(); - void process_js_event(int fd, js_event &event); - bool add_device(const char *dev_input_filepath, bool print_error = true); - bool remove_device(const char *dev_input_filepath); - bool remove_poll_fd(int index); - // Returns -1 if not found - int get_poll_fd_index_by_dev_input_id(int dev_input_id) const; - private: - struct ExtraData { - int dev_input_id = 0; - }; - - std::unordered_map bound_actions_by_id; - std::thread read_thread; - - pollfd poll_fd[max_js_poll_fd]; - ExtraData extra_data[max_js_poll_fd]; - int num_poll_fd = 0; - int event_fd = -1; - int event_index = -1; - - bool playstation_button_pressed = false; - bool up_pressed = false; - bool down_pressed = false; - bool left_pressed = false; - bool right_pressed = false; - - bool save_replay = false; - bool save_1_min_replay = false; - bool save_10_min_replay = false; - bool take_screenshot = false; - bool toggle_record = false; - bool toggle_replay = false; - bool toggle_show = false; - int hotplug_poll_index = -1; - Hotplug hotplug; - }; -} \ No newline at end of file diff --git a/include/GlobalHotkeysLinux.hpp b/include/GlobalHotkeysLinux.hpp deleted file mode 100644 index 959d095..0000000 --- a/include/GlobalHotkeysLinux.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include "GlobalHotkeys.hpp" -#include -#include - -namespace gsr { - class GlobalHotkeysLinux : public GlobalHotkeys { - public: - enum class GrabType { - ALL, - VIRTUAL - }; - - GlobalHotkeysLinux(GrabType grab_type); - GlobalHotkeysLinux(const GlobalHotkeysLinux&) = delete; - GlobalHotkeysLinux& operator=(const GlobalHotkeysLinux&) = delete; - ~GlobalHotkeysLinux() override; - - bool start(); - bool bind_key_press(Hotkey hotkey, const std::string &id, GlobalHotkeyCallback callback) override; - void unbind_all_keys() override; - void poll_events() override; - private: - void close_fds(); - private: - pid_t process_id = 0; - int read_pipes[2]; - int write_pipes[2]; - FILE *read_file = nullptr; - std::unordered_map bound_actions_by_id; - GrabType grab_type; - }; -} \ No newline at end of file diff --git a/include/GlobalHotkeysX11.hpp b/include/GlobalHotkeysX11.hpp deleted file mode 100644 index 610399a..0000000 --- a/include/GlobalHotkeysX11.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include "GlobalHotkeys.hpp" -#include -#include - -namespace gsr { - class GlobalHotkeysX11 : public GlobalHotkeys { - public: - GlobalHotkeysX11(); - GlobalHotkeysX11(const GlobalHotkeysX11&) = delete; - GlobalHotkeysX11& operator=(const GlobalHotkeysX11&) = delete; - ~GlobalHotkeysX11() override; - - // Hotkey key is a KeySym (XK_z for example) and modifiers is a bitmask of X11 modifier masks (for example ShiftMask | Mod1Mask) - bool bind_key_press(Hotkey hotkey, const std::string &id, GlobalHotkeyCallback callback) override; - void unbind_key_press(const std::string &id) override; - void unbind_all_keys() override; - void poll_events() override; - bool on_event(mgl::Event &event) override; - private: - // Returns true if a key bind has been registered for the hotkey - bool call_hotkey_callback(Hotkey hotkey) const; - private: - struct HotkeyData { - Hotkey hotkey; - GlobalHotkeyCallback callback; - }; - - Display *dpy = nullptr; - XEvent xev; - std::unordered_map bound_keys_by_id; - }; -} \ No newline at end of file diff --git a/include/Overlay.hpp b/include/Overlay.hpp index 5af839e..4cfab1d 100644 --- a/include/Overlay.hpp +++ b/include/Overlay.hpp @@ -6,10 +6,10 @@ #include "Config.hpp" #include "window_texture.h" #include "WindowUtils.hpp" -#include "GlobalHotkeysJoystick.hpp" +#include "GlobalHotkeys/GlobalHotkeysJoystick.hpp" #include "AudioPlayer.hpp" #include "RegionSelector.hpp" -#include "CursorTracker.hpp" +#include "CursorTracker/CursorTracker.hpp" #include #include -- cgit v1.2.3-70-g09d2