Add CHIAKI_GUI_ENABLE_QT_GAMEPAD option

This commit is contained in:
Florian Märkl 2019-07-10 09:41:11 +02:00
commit 3f76782b6c
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
3 changed files with 64 additions and 30 deletions

View file

@ -1,9 +1,14 @@
option(CHIAKI_GUI_ENABLE_QT_GAMEPAD "Use QtGamepad for Input" ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Multimedia Gamepad)
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Multimedia)
if(CHIAKI_GUI_ENABLE_QT_GAMEPAD)
find_package(Qt5 REQUIRED COMPONENTS Gamepad)
endif()
find_package(FFMPEG REQUIRED COMPONENTS avcodec)
@ -29,4 +34,8 @@ target_include_directories(chiaki PRIVATE include)
target_link_libraries(chiaki chiaki-lib)
target_link_libraries(chiaki FFMPEG::avcodec)
target_link_libraries(chiaki Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Multimedia Qt5::Gamepad)
target_link_libraries(chiaki Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Multimedia)
if(CHIAKI_GUI_ENABLE_QT_GAMEPAD)
target_link_libraries(chiaki Qt5::Gamepad)
target_compile_definitions(chiaki CHIAKI_GUI_ENABLE_QT_GAMEPAD)
endif()

View file

@ -25,7 +25,10 @@
#include <chiaki/session.h>
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
class QGamepad;
#endif
class QAudioOutput;
class QIODevice;
@ -38,7 +41,9 @@ class StreamSession : public QObject
private:
ChiakiSession session;
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
QGamepad *gamepad;
#endif
VideoDecoder video_decoder;
@ -52,14 +57,18 @@ class StreamSession : public QObject
explicit StreamSession(const QString &host, const QString &registkey, const QString &ostype, const QString &auth, const QString &morning, const QString &did, QObject *parent = nullptr);
~StreamSession();
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
QGamepad *GetGamepad() { return gamepad; }
#endif
VideoDecoder *GetVideoDecoder() { return &video_decoder; }
signals:
void CurrentImageUpdated();
private slots:
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
void UpdateGamepads();
#endif
void SendFeedbackState();
};

View file

@ -19,9 +19,12 @@
#include <chiaki/base64.h>
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
#include <QGamepadManager>
#include <QAudioOutput>
#include <QGamepad>
#endif
#include <QAudioOutput>
#include <QDebug>
@ -29,8 +32,10 @@ static void AudioFrameCb(int16_t *buf, size_t samples_count, void *user);
static void VideoSampleCb(uint8_t *buf, size_t buf_size, void *user);
StreamSession::StreamSession(const QString &host, const QString &registkey, const QString &ostype, const QString &auth, const QString &morning, const QString &did, QObject *parent)
: QObject(parent),
gamepad(nullptr)
: QObject(parent)
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
,gamepad(nullptr)
#endif
{
QByteArray host_str = host.toUtf8();
QByteArray registkey_str = registkey.toUtf8();
@ -88,16 +93,22 @@ StreamSession::StreamSession(const QString &host, const QString &registkey, cons
chiaki_session_set_video_sample_cb(&session, VideoSampleCb, this);
chiaki_session_start(&session);
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
connect(QGamepadManager::instance(), &QGamepadManager::connectedGamepadsChanged, this, &StreamSession::UpdateGamepads);
UpdateGamepads();
#endif
}
StreamSession::~StreamSession()
{
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
delete gamepad;
#endif
chiaki_session_join(&session);
chiaki_session_fini(&session);
}
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
void StreamSession::UpdateGamepads()
{
if(!gamepad || !gamepad->isConnected())
@ -139,13 +150,15 @@ void StreamSession::UpdateGamepads()
SendFeedbackState();
}
#endif
void StreamSession::SendFeedbackState()
{
if(!gamepad)
return;
ChiakiControllerState state;
state.buttons = 0;
ChiakiControllerState state = {};
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
if(gamepad)
{
state.buttons |= gamepad->buttonA() ? CHIAKI_CONTROLLER_BUTTON_CROSS : 0;
state.buttons |= gamepad->buttonB() ? CHIAKI_CONTROLLER_BUTTON_MOON : 0;
state.buttons |= gamepad->buttonX() ? CHIAKI_CONTROLLER_BUTTON_BOX : 0;
@ -167,6 +180,9 @@ void StreamSession::SendFeedbackState()
state.left_y = static_cast<int16_t>(gamepad->axisLeftY() * 0x7fff);
state.right_x = static_cast<int16_t>(gamepad->axisRightX() * 0x7fff);
state.right_y = static_cast<int16_t>(gamepad->axisRightY() * 0x7fff);
}
#endif
chiaki_session_set_controller_state(&session, &state);
}