From c1663229912a4c77b466886db11d22bf9d690b55 Mon Sep 17 00:00:00 2001 From: univrsal Date: Wed, 10 Jul 2024 22:05:09 +0200 Subject: [PATCH] WSS: Dispatch local sdl events --- src/hook/gamepad_hook_helper.cpp | 8 +++++--- src/network/websocket_server.cpp | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/hook/gamepad_hook_helper.cpp b/src/hook/gamepad_hook_helper.cpp index f411eddd..ec52429c 100644 --- a/src/hook/gamepad_hook_helper.cpp +++ b/src/hook/gamepad_hook_helper.cpp @@ -1,5 +1,7 @@ #include "gamepad_hook_helper.hpp" #include "../util/log.h" +#include "../util/config.hpp" +#include "../network/websocket_server.hpp" #include #include @@ -40,9 +42,7 @@ inline void sdl_init() SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); SDL_SetHint(SDL_HINT_LINUX_JOYSTICK_DEADZONES, "1"); - - if (SDL_WasInit(0) == INIT_FLAGS_FOR_SDL || - SDL_Init(INIT_FLAGS_FOR_SDL) < 0) { + if (SDL_WasInit(0) == INIT_FLAGS_FOR_SDL || SDL_Init(INIT_FLAGS_FOR_SDL) < 0) { berr("Couldn't initialize SDL: %s\n", SDL_GetError()); return; } @@ -152,6 +152,8 @@ void gamepads::event_loop() /* Process all currently pending events */ while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1) { + if (!io_config::io_window_filters.input_blocked()) + wss::dispatch_sdl_event(&event, "local", &local_data::data); switch (event.type) { case SDL_CONTROLLERDEVICEADDED: char fmt[512]; diff --git a/src/network/websocket_server.cpp b/src/network/websocket_server.cpp index f3e3def3..7338725c 100644 --- a/src/network/websocket_server.cpp +++ b/src/network/websocket_server.cpp @@ -118,22 +118,31 @@ void dispatch_sdl_event(const SDL_Event *e, const std::string &source_name, inpu { if (!mg::can_queue_message()) return; + static thread_local std::unordered_map last_axis; QJsonObject obj; obj["event_source"] = source_name.c_str(); obj["device_index"] = e->cdevice.which; obj["time"] = int(e->cdevice.timestamp); - + double axis{}; switch (e->type) { case SDL_CONTROLLERDEVICEADDED: + obj["event_type"] = "controller_device_added"; + obj["device_name"] = utf8_to_qt(SDL_GameControllerNameForIndex(e->cdevice.which)); break; case SDL_CONTROLLERDEVICEREMOVED: + obj["event_type"] = "controller_device_removed"; + // we don't have the name anymore break; - case SDL_CONTROLLERAXISMOTION: + // ignore small axis values + axis = double(e->caxis.value / double(INT16_MAX)); + if (std::abs(axis - last_axis[source_name][e->caxis.axis]) < 0.003) + return; + last_axis[source_name][e->caxis.axis] = axis; obj["event_type"] = "controller_axis_motion"; obj["virtual_code"] = e->caxis.axis; - obj["virtual_value"] = e->caxis.value / float(INT16_MAX); + obj["virtual_value"] = axis; break; case SDL_CONTROLLERBUTTONDOWN: obj["event_type"] = "controller_button_down"; @@ -145,6 +154,8 @@ void dispatch_sdl_event(const SDL_Event *e, const std::string &source_name, inpu obj["virtual_code"] = e->cbutton.button; obj["virtual_value"] = e->cbutton.state; break; + default: + return; /* ignore other events */ } auto n = data->remote_gamepad_names.find(e->cdevice.which);