Skip to content

Commit

Permalink
WSS: Dispatch local sdl events
Browse files Browse the repository at this point in the history
  • Loading branch information
univrsal committed Jul 10, 2024
1 parent 58f04de commit c166322
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/hook/gamepad_hook_helper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "gamepad_hook_helper.hpp"
#include "../util/log.h"
#include "../util/config.hpp"
#include "../network/websocket_server.hpp"
#include <input_data.hpp>
#include <thread>

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
Expand Down
17 changes: 14 additions & 3 deletions src/network/websocket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, double[GAMEPAD_AXIS_MAX]> 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";
Expand All @@ -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);
Expand Down

0 comments on commit c166322

Please sign in to comment.