Do not create thread to handle SDL joystick events in Qt build.

Caused crashes on MacOS Sierra (10.12).
This commit is contained in:
zhyk 2017-02-01 14:19:18 +08:00
parent 8c185b2f3a
commit 0629054783
3 changed files with 16 additions and 36 deletions

View File

@ -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);
@ -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) {
@ -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);
}
}
}

View File

@ -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;
};

View File

@ -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));
@ -334,6 +334,9 @@ void MainUI::initializeGL()
void MainUI::paintGL()
{
#ifdef SDL
SDL_PumpEvents();
#endif
updateAccelerometer();
UpdateInputState(&input_state);
time_update();