Change joystick button to save replay. Add joystick buttons for more actions.

This commit is contained in:
dec05eba
2025-03-17 22:04:02 +01:00
parent f9e1e3ec26
commit a6bd165d97
5 changed files with 71 additions and 22 deletions

4
TODO
View File

@@ -142,3 +142,7 @@ Allow using a hotkey such as printscreen or any other non-alphanumeric key witho
Use x11 shm instead of XGetImage (https://stackoverflow.com/questions/43442675/how-to-use-xshmgetimage-and-xshmputimage). Use x11 shm instead of XGetImage (https://stackoverflow.com/questions/43442675/how-to-use-xshmgetimage-and-xshmputimage).
Add a hotkey to record/stream/replay/screenshot region. Add a hotkey to record/stream/replay/screenshot region.
Do xi grab for keys as well. Otherwise the ui cant be used for keyboard input if a program has grabbed the keyboard, and there could possibly be a game that grabs the keyboard as well.
Make inactive buttons gray (in dropdown boxes and in the front page with save, etc when replay is not running).

View File

@@ -3,10 +3,8 @@
#include "GlobalHotkeys.hpp" #include "GlobalHotkeys.hpp"
#include "Hotplug.hpp" #include "Hotplug.hpp"
#include <unordered_map> #include <unordered_map>
#include <optional>
#include <thread> #include <thread>
#include <poll.h> #include <poll.h>
#include <mglpp/system/Clock.hpp>
#include <linux/joystick.h> #include <linux/joystick.h>
namespace gsr { namespace gsr {
@@ -21,6 +19,11 @@ namespace gsr {
~GlobalHotkeysJoystick() override; ~GlobalHotkeysJoystick() override;
bool start(); bool start();
// Currently valid ids:
// save_replay
// take_screenshot
// toggle_record
// toggle_replay
bool bind_action(const std::string &id, GlobalHotkeyCallback callback) override; bool bind_action(const std::string &id, GlobalHotkeyCallback callback) override;
void poll_events() override; void poll_events() override;
private: private:
@@ -45,9 +48,12 @@ namespace gsr {
int event_fd = -1; int event_fd = -1;
int event_index = -1; int event_index = -1;
mgl::Clock double_click_clock; bool playstation_button_pressed = false;
std::optional<double> prev_time_clicked;
bool save_replay = false; bool save_replay = false;
bool take_screenshot = false;
bool toggle_record = false;
bool toggle_replay = false;
int hotplug_poll_index = -1; int hotplug_poll_index = -1;
Hotplug hotplug; Hotplug hotplug;
}; };

View File

@@ -6,7 +6,12 @@
#include <sys/eventfd.h> #include <sys/eventfd.h>
namespace gsr { namespace gsr {
static constexpr double double_click_timeout_seconds = 0.33; static constexpr int button_pressed = 1;
static constexpr int playstation_button = 10;
static constexpr int x_button = 0;
static constexpr int circle_button = 1;
static constexpr int triangle_button = 2;
static constexpr int square_button = 3;
// Returns -1 on error // Returns -1 on error
static int get_js_dev_input_id_from_filepath(const char *dev_input_filepath) { static int get_js_dev_input_id_from_filepath(const char *dev_input_filepath) {
@@ -99,6 +104,27 @@ namespace gsr {
if(it != bound_actions_by_id.end()) if(it != bound_actions_by_id.end())
it->second("save_replay"); it->second("save_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");
}
} }
void GlobalHotkeysJoystick::read_events() { void GlobalHotkeysJoystick::read_events() {
@@ -156,22 +182,17 @@ namespace gsr {
if((event.type & JS_EVENT_BUTTON) == 0) if((event.type & JS_EVENT_BUTTON) == 0)
return; return;
if(event.number == 8 && event.value == 1) { if(event.number == playstation_button) {
const double now = double_click_clock.get_elapsed_time_seconds(); playstation_button_pressed = event.value == button_pressed;
if(!prev_time_clicked.has_value()) { } else if(playstation_button_pressed && event.value == button_pressed) {
prev_time_clicked = now; if(event.number == circle_button)
return; save_replay = true;
} else if(event.number == triangle_button)
take_screenshot = true;
if(prev_time_clicked.has_value()) { else if(event.number == square_button)
const bool double_clicked = (now - prev_time_clicked.value()) < double_click_timeout_seconds; toggle_record = true;
if(double_clicked) { else if(event.number == x_button)
save_replay = true; toggle_replay = true;
prev_time_clicked.reset();
} else {
prev_time_clicked = now;
}
}
} }
} }

View File

@@ -339,6 +339,21 @@ namespace gsr {
overlay->save_replay(); overlay->save_replay();
}); });
global_hotkeys_js->bind_action("take_screenshot", [overlay](const std::string &id) {
fprintf(stderr, "pressed %s\n", id.c_str());
overlay->take_screenshot();
});
global_hotkeys_js->bind_action("toggle_record", [overlay](const std::string &id) {
fprintf(stderr, "pressed %s\n", id.c_str());
overlay->toggle_record();
});
global_hotkeys_js->bind_action("toggle_replay", [overlay](const std::string &id) {
fprintf(stderr, "pressed %s\n", id.c_str());
overlay->toggle_replay();
});
return global_hotkeys_js; return global_hotkeys_js;
} }

View File

@@ -340,7 +340,10 @@ namespace gsr {
list_ptr->add_widget(create_record_hotkey_options()); list_ptr->add_widget(create_record_hotkey_options());
list_ptr->add_widget(create_stream_hotkey_options()); list_ptr->add_widget(create_stream_hotkey_options());
list_ptr->add_widget(create_screenshot_hotkey_options()); list_ptr->add_widget(create_screenshot_hotkey_options());
list_ptr->add_widget(std::make_unique<Label>(&get_theme().body_font, "Double-click the controller share button to save a replay", get_color_theme().text_color)); list_ptr->add_widget(std::make_unique<Label>(&get_theme().body_font, "Press the PlayStation button and square to start/stop recording", get_color_theme().text_color));
list_ptr->add_widget(std::make_unique<Label>(&get_theme().body_font, "Press the PlayStation button and X to start/stop replay", get_color_theme().text_color));
list_ptr->add_widget(std::make_unique<Label>(&get_theme().body_font, "Press the PlayStation button and circle to save a replay", get_color_theme().text_color));
list_ptr->add_widget(std::make_unique<Label>(&get_theme().body_font, "Press the PlayStation button and triangle to take a screenshot", get_color_theme().text_color));
list_ptr->add_widget(create_hotkey_control_buttons()); list_ptr->add_widget(create_hotkey_control_buttons());
return subsection; return subsection;
} }