From 8e26419f8a4db1f822018296021d5f5dd7b0915c Mon Sep 17 00:00:00 2001 From: rock88 Date: Sun, 3 May 2020 20:24:20 +0300 Subject: [PATCH] Support left/right mouse button --- src/decoders/video_decoder.c | 1 + src/moonlight_libretro.c | 8 ++-- src/moonlight_libretro_wrapper.cpp | 3 ++ src/moonlight_libretro_wrapper.h | 4 +- src/ui/windows/StreamWindow.cpp | 60 ++++++++++++++++++++++++++---- 5 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/decoders/video_decoder.c b/src/decoders/video_decoder.c index 6d6cabb..fe80545 100644 --- a/src/decoders/video_decoder.c +++ b/src/decoders/video_decoder.c @@ -32,6 +32,7 @@ static int video_decoder_setup(int videoFormat, int width, int height, int redra static void video_decoder_cleanup() { ffmpeg_destroy(); + frame = NULL; } static int video_decoder_submit_decode_unit(PDECODE_UNIT decodeUnit) { diff --git a/src/moonlight_libretro.c b/src/moonlight_libretro.c index 1fd79d4..9eaa256 100644 --- a/src/moonlight_libretro.c +++ b/src/moonlight_libretro.c @@ -123,10 +123,6 @@ static void update_variables(void) { } void retro_run(void) { -// bool updated = false; -// if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) -// update_variables(); - moonlight_libretro_wrapper_init(width, height); // Handle inputs @@ -159,6 +155,10 @@ void retro_run(void) { moonlight_libretro_wrapper_handle_mouse_button(0, 0, 0); } + for (int i = 0; i < RETROK_LAST; i++) { + keyboard_state[i] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, i); + } + // Draw glBindFramebuffer(RARCH_GL_FRAMEBUFFER, hw_render.get_current_framebuffer()); diff --git a/src/moonlight_libretro_wrapper.cpp b/src/moonlight_libretro_wrapper.cpp index 66a363c..7e358ea 100644 --- a/src/moonlight_libretro_wrapper.cpp +++ b/src/moonlight_libretro_wrapper.cpp @@ -3,6 +3,7 @@ #include "GameStreamClient.hpp" #include "gl_render.h" #include +#include "libretro.h" #include static bool moonlight_is_initialized = false; @@ -10,6 +11,8 @@ static bool moonlight_is_initialized = false; static Application* app; static std::string working_dir; +int16_t keyboard_state[RETROK_LAST]; + void moonlight_libretro_wrapper_preinit() { OpenSSL_add_all_algorithms(); curl_global_init(CURL_GLOBAL_ALL); diff --git a/src/moonlight_libretro_wrapper.h b/src/moonlight_libretro_wrapper.h index 0d42c7d..b0bcc1c 100644 --- a/src/moonlight_libretro_wrapper.h +++ b/src/moonlight_libretro_wrapper.h @@ -3,9 +3,11 @@ #ifdef __cplusplus #define EXTERN extern "C" #else -#define EXTERN +#define EXTERN extern #endif +EXTERN int16_t keyboard_state[]; + EXTERN void moonlight_libretro_wrapper_preinit(); EXTERN void moonlight_libretro_wrapper_init(int width, int height); EXTERN void moonlight_libretro_wrapper_set_working_dir(const char* dir); diff --git a/src/ui/windows/StreamWindow.cpp b/src/ui/windows/StreamWindow.cpp index e9086ee..41f1bba 100644 --- a/src/ui/windows/StreamWindow.cpp +++ b/src/ui/windows/StreamWindow.cpp @@ -5,14 +5,25 @@ #include "video_decoder.h" #include "audio_decoder.h" #include "nanovg.h" +#include "moonlight_libretro_wrapper.h" +#include "libretro.h" #include +static bool pressed = false, sent_l_press = false, sent_r_press = false; +static int current_mouse_x = 0, current_mouse_y = 0; +static int start_mouse_x = 0, start_mouse_y = 0; + using namespace nanogui; StreamWindow::StreamWindow(Widget *parent, const std::string &address, int app_id): Widget(parent) { m_address = address; m_app_id = app_id; + pressed = sent_l_press = sent_r_press = false; + current_mouse_x = parent->width() / 2; + current_mouse_y = parent->height() / 2; + start_mouse_x = start_mouse_y = 0; + LiInitializeStreamConfiguration(&m_config); int h = 720; @@ -47,27 +58,42 @@ void StreamWindow::setup_stream() { LiStartConnection(&m_data.serverInfo, &m_config, NULL, &video_decoder_callbacks, &audio_decoder_callbacks, NULL, 0, NULL, 0); async([this] { - m_loader->dispose(); + if (m_loader) { + m_loader->dispose(); + m_loader = NULL; + } }); }); - + } void StreamWindow::draw(NVGcontext *ctx) { nvgSave(ctx); gl_render_setup(1280, 720); - + if (frame != NULL) { gl_render_draw(frame->data); } nvgRestore(ctx); + + // TODO: Get out of here... + if (keyboard_state[RETROK_q]) { + LiStopConnection(); + + async([this]{ + if (m_loader) { + m_loader->dispose(); + m_loader = NULL; + } + + auto app = static_cast(screen()); + app->pop_window(); + }); + } } -static bool pressed = false; -static int start_mouse_x = 0, start_mouse_y = 0; - bool StreamWindow::mouse_button_event(const nanogui::Vector2i &p, int button, bool down, int modifiers) { if (button == 0) { if (down && !pressed) { @@ -75,7 +101,22 @@ bool StreamWindow::mouse_button_event(const nanogui::Vector2i &p, int button, bo start_mouse_y = p.y(); } pressed = down; - //LiSendMouseButtonEvent(down ? BUTTON_ACTION_PRESS : BUTTON_ACTION_RELEASE, BUTTON_LEFT); + + if (pressed && keyboard_state[RETROK_LCTRL]) { + sent_l_press = true; + LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_LEFT); + } else if (sent_l_press) { + sent_l_press = false; + LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_LEFT); + } + + if (pressed && keyboard_state[RETROK_LALT]) { + sent_r_press = true; + LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_RIGHT); + } else if (sent_r_press) { + sent_r_press = false; + LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_RIGHT); + } } return true; } @@ -83,7 +124,10 @@ bool StreamWindow::mouse_button_event(const nanogui::Vector2i &p, int button, bo bool StreamWindow::mouse_motion_event(const Vector2i &p, const Vector2i &rel, int button, int modifiers) { #if defined(__LAKKA_SWITCH__) || defined(__APPLE__) if (pressed) { - LiSendMouseMoveEvent(p.x() - start_mouse_x, p.y() - start_mouse_y); + current_mouse_x = current_mouse_x + p.x() - start_mouse_x; + current_mouse_y = current_mouse_y + p.y() - start_mouse_y; + + LiSendMousePositionEvent(current_mouse_x, current_mouse_y, width(), height()); start_mouse_x = p.x(); start_mouse_y = p.y();