Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix joystick support for Qt build #9268

Merged
merged 2 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,16 @@ elseif(USING_QT_UI)
include_directories(${CMAKE_CURRENT_BINARY_DIR} Qt Qt/Debugger)
set(nativeExtraLibs ${nativeExtraLibs} Qt5::Multimedia Qt5::OpenGL Qt5::Gui Qt5::Core)
set(TargetBin PPSSPPQt)

# Enable SDL if found
if (SDL2_FOUND)
add_definitions(-DSDL)
set(nativeExtra ${nativeExtra}
SDL/SDLJoystick.h
SDL/SDLJoystick.cpp)
set(nativeExtraLibs ${nativeExtraLibs} SDL2::SDL2)
endif()

elseif(TARGET SDL2::SDL2)
set(TargetBin PPSSPPSDL)
# Require SDL
Expand Down
35 changes: 10 additions & 25 deletions SDL/SDLJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@

using namespace std;

extern "C" {
int SDLJoystickThreadWrapper(void *SDLJoy){
SDLJoystick *stick = static_cast<SDLJoystick *>(SDLJoy);
stick->runLoop();
return 0;
}
static int SDLJoystickEventHandlerWrapper(void* userdata, SDL_Event* event)
{
static_cast<SDLJoystick *>(userdata)->ProcessInput(*event);
return 0;
}

SDLJoystick::SDLJoystick(bool init_SDL ): thread(NULL), running(true) {
SDLJoystick::SDLJoystick(bool init_SDL ) : registeredAsEventHandler(false) {
if (init_SDL) {
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
Expand Down Expand Up @@ -81,20 +79,17 @@ void SDLJoystick::setUpController(int deviceIndex) {
}

SDLJoystick::~SDLJoystick() {
if (thread) {
running = false;
SDL_Event evt;
evt.type = SDL_USEREVENT;
SDL_PushEvent(&evt);
SDL_WaitThread(thread,0);
if (registeredAsEventHandler) {
SDL_DelEventWatch(SDLJoystickEventHandlerWrapper, this);
}
for (auto & controller : controllers) {
SDL_GameControllerClose(controller);
}
}

void SDLJoystick::startEventLoop() {
thread = SDL_CreateThread(SDLJoystickThreadWrapper, "joystick",static_cast<void *>(this));
void SDLJoystick::registerEventHandler() {
SDL_AddEventWatch(SDLJoystickEventHandlerWrapper, this);
registeredAsEventHandler = true;
}

keycode_t SDLJoystick::getKeycodeForButton(SDL_GameControllerButton button) {
Expand Down Expand Up @@ -199,13 +194,3 @@ int SDLJoystick::getDeviceIndex(int instanceId) {
}
return it->second;
}

void SDLJoystick::runLoop() {
while (running) {
SDL_Event evt;
int res = SDL_WaitEvent(&evt);
if (res) {
ProcessInput(evt);
}
}
}
12 changes: 2 additions & 10 deletions SDL/SDLJoystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,20 @@
#include "net/resolve.h"
#include "base/NativeApp.h"

extern "C" {
int SDLJoystickThreadWrapper(void *SDLJoy);
}

class SDLJoystick{
friend int ::SDLJoystickThreadWrapper(void *);
public:
SDLJoystick(bool init_SDL = false);
~SDLJoystick();

void startEventLoop();
void registerEventHandler();
void ProcessInput(SDL_Event &event);

private:

void runLoop();
void setUpController(int deviceIndex);
void setUpControllers();
keycode_t getKeycodeForButton(SDL_GameControllerButton button);
int getDeviceIndex(int instanceId);
SDL_Thread *thread ;
bool running ;
bool registeredAsEventHandler;
std::vector<SDL_GameController *> controllers;
std::map<int, int> controllerDeviceMap;
};
5 changes: 4 additions & 1 deletion ext/native/base/QtMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static int mainInternal(QApplication &a)

#ifdef SDL
SDLJoystick joy(true);
joy.startEventLoop();
joy.registerEventHandler();
SDL_Init(SDL_INIT_AUDIO);
SDL_AudioSpec fmt, ret_fmt;
memset(&fmt, 0, sizeof(fmt));
Expand Down Expand Up @@ -334,6 +334,9 @@ void MainUI::initializeGL()

void MainUI::paintGL()
{
#ifdef SDL
SDL_PumpEvents();
#endif
updateAccelerometer();
UpdateInputState(&input_state);
time_update();
Expand Down