From f3be4ed22a5849fd6951a51945f24df677d3a9ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Thu, 2 Jul 2020 22:34:57 +0200 Subject: [PATCH] Integrate Setsu in GUI --- gui/CMakeLists.txt | 4 ++ gui/include/streamsession.h | 12 +++++ gui/src/controllermanager.cpp | 3 +- gui/src/streamsession.cpp | 95 ++++++++++++++++++++++++++++++++++- lib/src/controller.c | 16 ++++-- 5 files changed, 124 insertions(+), 6 deletions(-) diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 210e513..7072b83 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -83,6 +83,10 @@ if(CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER) target_link_libraries(chiaki SDL2::SDL2) target_compile_definitions(chiaki PRIVATE CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER) endif() +if(CHIAKI_ENABLE_SETSU) + target_link_libraries(chiaki setsu) + target_compile_definitions(chiaki PRIVATE CHIAKI_GUI_ENABLE_SETSU) +endif() set_target_properties(chiaki PROPERTIES MACOSX_BUNDLE TRUE diff --git a/gui/include/streamsession.h b/gui/include/streamsession.h index b6a5b29..94399ad 100644 --- a/gui/include/streamsession.h +++ b/gui/include/streamsession.h @@ -21,6 +21,10 @@ #include #include +#if CHIAKI_GUI_ENABLE_SETSU +#include +#endif + #include "videodecoder.h" #include "exception.h" #include "sessionlog.h" @@ -69,6 +73,11 @@ class StreamSession : public QObject ChiakiOpusDecoder opus_decoder; Controller *controller; +#if CHIAKI_GUI_ENABLE_SETSU + Setsu *setsu; + QMap, uint8_t> setsu_ids; + ChiakiControllerState setsu_state; +#endif ChiakiControllerState keyboard_state; @@ -83,6 +92,9 @@ class StreamSession : public QObject void PushAudioFrame(int16_t *buf, size_t samples_count); void PushVideoSample(uint8_t *buf, size_t buf_size); void Event(ChiakiEvent *event); +#if CHIAKI_GUI_ENABLE_SETSU + void HandleSetsuEvent(SetsuEvent *event); +#endif private slots: void InitAudio(unsigned int channels, unsigned int rate); diff --git a/gui/src/controllermanager.cpp b/gui/src/controllermanager.cpp index 224f555..2a52ea6 100644 --- a/gui/src/controllermanager.cpp +++ b/gui/src/controllermanager.cpp @@ -199,7 +199,8 @@ QString Controller::GetName() ChiakiControllerState Controller::GetState() { - ChiakiControllerState state = {}; + ChiakiControllerState state; + chiaki_controller_state_set_idle(&state); #ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER if(!controller) return state; diff --git a/gui/src/streamsession.cpp b/gui/src/streamsession.cpp index a0c85a1..dd4104a 100644 --- a/gui/src/streamsession.cpp +++ b/gui/src/streamsession.cpp @@ -27,6 +27,8 @@ #include #include +#define SETSU_UPDATE_INTERVAL_MS 4 + StreamSessionConnectInfo::StreamSessionConnectInfo(Settings *settings, QString host, QByteArray regist_key, QByteArray morning) { key_map = settings->GetControllerMappingForDecoding(); @@ -44,6 +46,7 @@ static void AudioSettingsCb(uint32_t channels, uint32_t rate, void *user); static void AudioFrameCb(int16_t *buf, size_t samples_count, void *user); static bool VideoSampleCb(uint8_t *buf, size_t buf_size, void *user); static void EventCb(ChiakiEvent *event, void *user); +static void SessionSetsuCb(SetsuEvent *event, void *user); StreamSession::StreamSession(const StreamSessionConnectInfo &connect_info, QObject *parent) : QObject(parent), @@ -70,7 +73,7 @@ StreamSession::StreamSession(const StreamSessionConnectInfo &connect_info, QObje throw ChiakiException("Morning invalid"); memcpy(chiaki_connect_info.morning, connect_info.morning.constData(), sizeof(chiaki_connect_info.morning)); - memset(&keyboard_state, 0, sizeof(keyboard_state)); + chiaki_controller_state_set_idle(&keyboard_state); ChiakiErrorCode err = chiaki_session_init(&session, &chiaki_connect_info, log.GetChiakiLog()); if(err != CHIAKI_ERR_SUCCESS) @@ -88,6 +91,16 @@ StreamSession::StreamSession(const StreamSessionConnectInfo &connect_info, QObje connect(ControllerManager::GetInstance(), &ControllerManager::AvailableControllersUpdated, this, &StreamSession::UpdateGamepads); #endif +#if CHIAKI_GUI_ENABLE_SETSU + chiaki_controller_state_set_idle(&setsu_state); + setsu = setsu_new(); + auto timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, [this]{ + setsu_poll(setsu, SessionSetsuCb, this); + }); + timer->start(SETSU_UPDATE_INTERVAL_MS); +#endif + key_map = connect_info.key_map; UpdateGamepads(); } @@ -100,6 +113,9 @@ StreamSession::~StreamSession() #if CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER delete controller; #endif +#if CHIAKI_GUI_ENABLE_SETSU + setsu_free(setsu); +#endif } void StreamSession::Start() @@ -217,13 +233,18 @@ void StreamSession::UpdateGamepads() void StreamSession::SendFeedbackState() { - ChiakiControllerState state = {}; + ChiakiControllerState state; + chiaki_controller_state_set_idle(&state); #if CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER if(controller) state = controller->GetState(); #endif +#if CHIAKI_GUI_ENABLE_SETSU + chiaki_controller_state_or(&state, &state, &setsu_state); +#endif + chiaki_controller_state_or(&state, &state, &keyboard_state); chiaki_session_set_controller_state(&session, &state); } @@ -275,6 +296,8 @@ void StreamSession::Event(ChiakiEvent *event) { switch(event->type) { + case CHIAKI_EVENT_CONNECTED: + break; case CHIAKI_EVENT_QUIT: emit SessionQuit(event->quit.reason, event->quit.reason_str ? QString::fromUtf8(event->quit.reason_str) : QString()); break; @@ -284,6 +307,63 @@ void StreamSession::Event(ChiakiEvent *event) } } +#if CHIAKI_GUI_ENABLE_SETSU +void StreamSession::HandleSetsuEvent(SetsuEvent *event) +{ + if(!setsu) + return; + switch(event->type) + { + case SETSU_EVENT_DEVICE_ADDED: + setsu_connect(setsu, event->path); + break; + case SETSU_EVENT_DEVICE_REMOVED: + for(auto it=setsu_ids.begin(); it!=setsu_ids.end();) + { + if(it.key().first == event->path) + { + chiaki_controller_state_stop_touch(&setsu_state, it.value()); + setsu_ids.erase(it++); + } + else + it++; + } + SendFeedbackState(); + break; + case SETSU_EVENT_DOWN: + break; + case SETSU_EVENT_UP: + for(auto it=setsu_ids.begin(); it!=setsu_ids.end(); it++) + { + if(it.key().first == setsu_device_get_path(event->dev) && it.key().second == event->tracking_id) + { + chiaki_controller_state_stop_touch(&setsu_state, it.value()); + setsu_ids.erase(it); + break; + } + } + SendFeedbackState(); + break; + case SETSU_EVENT_POSITION: { + QPair k = { setsu_device_get_path(event->dev), event->tracking_id }; + auto it = setsu_ids.find(k); + if(it == setsu_ids.end()) + { + int8_t cid = chiaki_controller_state_start_touch(&setsu_state, event->x, event->y); + if(cid >= 0) + setsu_ids[k] = (uint8_t)cid; + else + break; + } + else + chiaki_controller_state_set_touch_pos(&setsu_state, it.value(), event->x, event->y); + SendFeedbackState(); + break; + } + } +} +#endif + class StreamSessionPrivate { public: @@ -295,6 +375,9 @@ class StreamSessionPrivate static void PushAudioFrame(StreamSession *session, int16_t *buf, size_t samples_count) { session->PushAudioFrame(buf, samples_count); } static void PushVideoSample(StreamSession *session, uint8_t *buf, size_t buf_size) { session->PushVideoSample(buf, buf_size); } static void Event(StreamSession *session, ChiakiEvent *event) { session->Event(event); } +#if CHIAKI_GUI_ENABLE_SETSU + static void HandleSetsuEvent(StreamSession *session, SetsuEvent *event) { session->HandleSetsuEvent(event); } +#endif }; static void AudioSettingsCb(uint32_t channels, uint32_t rate, void *user) @@ -321,3 +404,11 @@ static void EventCb(ChiakiEvent *event, void *user) auto session = reinterpret_cast(user); StreamSessionPrivate::Event(session, event); } + +#if CHIAKI_GUI_ENABLE_SETSU +static void SessionSetsuCb(SetsuEvent *event, void *user) +{ + auto session = reinterpret_cast(user); + StreamSessionPrivate::HandleSetsuEvent(session, event); +} +#endif diff --git a/lib/src/controller.c b/lib/src/controller.c index a50cac4..222b380 100644 --- a/lib/src/controller.c +++ b/lib/src/controller.c @@ -22,10 +22,19 @@ CHIAKI_EXPORT void chiaki_controller_state_set_idle(ChiakiControllerState *state) { state->buttons = 0; + state->l2_state = 0; + state->r2_state = 0; state->left_x = 0; state->left_y = 0; state->right_x = 0; state->right_y = 0; + state->touch_id_next = 0; + for(size_t i=0; itouches[i].id = -1; + state->touches[i].x = 0; + state->touches[i].y = 0; + } } CHIAKI_EXPORT int8_t chiaki_controller_state_start_touch(ChiakiControllerState *state, uint16_t x, uint16_t y) @@ -38,7 +47,7 @@ CHIAKI_EXPORT int8_t chiaki_controller_state_start_touch(ChiakiControllerState * state->touch_id_next = (state->touch_id_next + 1) & TOUCH_ID_MASK; state->touches[i].x = x; state->touches[i].y = y; - break; + return state->touches[i].id; } } return -1; @@ -87,13 +96,14 @@ CHIAKI_EXPORT void chiaki_controller_state_or(ChiakiControllerState *out, Chiaki out->touch_id_next = 0; for(size_t i=0; itouches[i].id >= 0 ? &a->touches[i] : b->touches[i].id >= 0 ? &b->touches[i] : NULL; + ChiakiControllerTouch *touch = a->touches[i].id >= 0 ? &a->touches[i] : (b->touches[i].id >= 0 ? &b->touches[i] : NULL); if(!touch) { out->touches[i].id = -1; out->touches[i].x = out->touches[i].y = 0; continue; } - out->touches[i] = *touch; + if(touch != &out->touches[i]) + out->touches[i] = *touch; } }