Support SDL GameController

This commit is contained in:
Florian Märkl 2019-08-18 17:15:13 +02:00
commit 84756ba8ef
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
8 changed files with 391 additions and 9 deletions

View file

@ -0,0 +1,96 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_CONTROLLERMANAGER_H
#define CHIAKI_CONTROLLERMANAGER_H
#include <chiaki/controller.h>
#include <QObject>
#include <QSet>
#include <QMap>
#include <QString>
#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
#include <SDL.h>
#endif
class Controller;
class ControllerManager : public QObject
{
Q_OBJECT
friend class Controller;
private:
#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
QSet<SDL_JoystickID> available_controllers;
#endif
QMap<int, Controller *> open_controllers;
void ControllerClosed(Controller *controller);
private slots:
void UpdateAvailableControllers();
void HandleEvents();
void ControllerEvent(int device_id);
public:
static ControllerManager *GetInstance();
ControllerManager(QObject *parent = nullptr);
~ControllerManager();
QList<int> GetAvailableControllers();
Controller *OpenController(int device_id);
signals:
void AvailableControllersUpdated();
};
class Controller : public QObject
{
Q_OBJECT
friend class ControllerManager;
private:
Controller(int device_id, ControllerManager *manager);
void UpdateState();
ControllerManager *manager;
int id;
#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
SDL_GameController *controller;
#endif
public:
~Controller();
bool IsConnected();
int GetDeviceID();
QString GetName();
ChiakiControllerState GetState();
signals:
void StateChanged();
};
#endif // CHIAKI_CONTROLLERMANAGER_H

View file

@ -21,6 +21,7 @@
#include "videodecoder.h"
#include "exception.h"
#include "sessionlog.h"
#include "controllermanager.h"
#include <QObject>
#include <QImage>
@ -68,6 +69,7 @@ class StreamSession : public QObject
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
QGamepad *gamepad;
#endif
Controller *controller;
ChiakiControllerState keyboard_state;
@ -93,6 +95,7 @@ class StreamSession : public QObject
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
QGamepad *GetGamepad() { return gamepad; }
#endif
Controller *GetController() { return controller; }
VideoDecoder *GetVideoDecoder() { return &video_decoder; }
void HandleKeyboardEvent(QKeyEvent *event);
@ -102,9 +105,7 @@ class StreamSession : public QObject
void SessionQuit(ChiakiQuitReason reason, const QString &reason_str);
private slots:
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
void UpdateGamepads();
#endif
void SendFeedbackState();
};