From 358dd9b044f2d80b11a785c8ecd35a38bd67b4cf Mon Sep 17 00:00:00 2001 From: "K.Ohta" Date: Sat, 17 Oct 2015 03:35:08 +0900 Subject: [PATCH] [UI][Qt][Joystick] Manage Joysticks with GUID. --- source/src/qt/common/emu_input.cpp | 101 ++++++++++++++++++++++++++++++------- source/src/qt/common/qt_main.h | 8 ++- source/src/qt/gui/mainwidget.h | 1 - 3 files changed, 90 insertions(+), 20 deletions(-) diff --git a/source/src/qt/common/emu_input.cpp b/source/src/qt/common/emu_input.cpp index 8b5ba1f02..371df80fc 100644 --- a/source/src/qt/common/emu_input.cpp +++ b/source/src/qt/common/emu_input.cpp @@ -172,7 +172,7 @@ void EMU::update_input() } } lost_focus = false; -#if 1 + // update joystick status #ifdef USE_KEY_TO_JOY // emulate joystick #1 with keyboard @@ -206,7 +206,15 @@ void EMU::update_input() #endif #endif -#endif + // swap joystick buttons + if(config.swap_joy_buttons) { + for(int i = 0; i < joy_num && i < 2; i++) { + uint32 b0 = joy_status[i] & 0xaaaaaaa0; + uint32 b1 = joy_status[i] & 0x55555550; + joy_status[i] = (joy_status[i] & 0x0f) | (b0 >> 1) | (b1 << 1); + } + } + #if defined(USE_BUTTON) if(!press_flag && !release_flag) { int ii; @@ -631,17 +639,39 @@ void EMU::stop_auto_key() JoyThreadClass::JoyThreadClass(QObject *parent) : QThread(parent) { - int i; - for(i = 0; i < 2; i++) joyhandle[i] = SDL_JoystickOpen(i); + int i, j; joy_num = SDL_NumJoysticks(); - AGAR_DebugLog(AGAR_LOG_DEBUG, "JoyThread : Start."); - bRunThread = true; + for(i = 0; i < 16; i++) { + joyhandle[i] = NULL; + for(j = 0; j < 16; j++) guid_list[i].data[j] = 0; + for(j = 0; j < 16; j++) guid_assign[i].data[j] = 0; + names[i] = QString::fromUtf8(""); + } + + if(joy_num > 0) { + if(joy_num >= 16) joy_num = 16; + for(i = 0; i < joy_num; i++) { + joyhandle[i] = SDL_JoystickOpen(i); + if(joyhandle[i] != NULL) { + guid_list[i] = SDL_JoystickGetGUID(joyhandle[i]); + guid_assign[i] = SDL_JoystickGetGUID(joyhandle[i]); + names[i] = QString::fromUtf8(SDL_JoystickNameForIndex(i)); + AGAR_DebugLog(AGAR_LOG_DEBUG, "JoyThread : Joystick %d : %s.", i, names[i].toUtf8().data()); + } + } + AGAR_DebugLog(AGAR_LOG_DEBUG, "JoyThread : Start."); + bRunThread = true; + } else { + AGAR_DebugLog(AGAR_LOG_DEBUG, "JoyThread : Any joysticks were not connected."); + bRunThread = false; + } } - + + JoyThreadClass::~JoyThreadClass() { int i; - for(i = 0; i < 2; i++) { + for(i = 0; i < 16; i++) { if(joyhandle[i] != NULL) SDL_JoystickClose(joyhandle[i]); } AGAR_DebugLog(AGAR_LOG_DEBUG, "JoyThread : EXIT"); @@ -707,6 +737,26 @@ void JoyThreadClass::button_up(int index, unsigned int button) } // SDL Event Handler +bool JoyThreadClass::MatchJoyGUID(SDL_JoystickGUID *a, SDL_JoystickGUID *b) +{ + int i; + for(i = 0; i < 16; i++) { + if(a->data[i] != b->data[i]) return false; + } + return true; +} + +bool JoyThreadClass::CheckJoyGUID(SDL_JoystickGUID *a) +{ + int i; + bool b = false; + for(i = 0; i < 16; i++) { + if(a->data[i] != 0) b = true; + } + return b; +} + + bool JoyThreadClass::EventSDL(SDL_Event *eventQueue) { // SDL_Surface *p; @@ -715,6 +765,7 @@ bool JoyThreadClass::EventSDL(SDL_Event *eventQueue) int vk; uint32_t sym; uint32_t mod; + SDL_JoystickGUID guid; int i; if(eventQueue == NULL) return false; /* @@ -724,25 +775,39 @@ bool JoyThreadClass::EventSDL(SDL_Event *eventQueue) case SDL_JOYAXISMOTION: value = eventQueue->jaxis.value; i = eventQueue->jaxis.which; - if((i < 0) || (i > 1)) break; - - if(eventQueue->jaxis.axis == 0) { // X - x_axis_changed(i, value); - } else if(eventQueue->jaxis.axis == 1) { // Y - y_axis_changed(i, value); + guid = SDL_JoystickGetDeviceGUID(i); + if(!CheckJoyGUID(&guid)) break; + for(i = 0; i < 2; i++) { + if(MatchJoyGUID(&guid, &(guid_assign[i]))) { + if(eventQueue->jaxis.axis == 0) { // X + x_axis_changed(i, value); + } else if(eventQueue->jaxis.axis == 1) { // Y + y_axis_changed(i, value); + } + } } break; case SDL_JOYBUTTONDOWN: button = eventQueue->jbutton.button; i = eventQueue->jbutton.which; - if((i < 0) || (i > 1)) break; - button_down(i, button); + guid = SDL_JoystickGetDeviceGUID(i); + if(!CheckJoyGUID(&guid)) break; + for(i = 0; i < 2; i++) { + if(MatchJoyGUID(&guid, &(guid_assign[i]))) { + button_down(i, button); + } + } break; case SDL_JOYBUTTONUP: button = eventQueue->jbutton.button; i = eventQueue->jbutton.which; - if((i < 0) || (i > 1)) break; - button_up(i, button); + guid = SDL_JoystickGetDeviceGUID(i); + if(!CheckJoyGUID(&guid)) break; + for(i = 0; i < 2; i++) { + if(MatchJoyGUID(&guid, &(guid_assign[i]))) { + button_up(i, button); + } + } break; default: break; diff --git a/source/src/qt/common/qt_main.h b/source/src/qt/common/qt_main.h index 63bf15555..835af0b1a 100644 --- a/source/src/qt/common/qt_main.h +++ b/source/src/qt/common/qt_main.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include "sdl_cpuid.h" @@ -132,7 +133,10 @@ class JoyThreadClass : public QThread { private: int joy_num; SDL_Event event; - SDL_Joystick *joyhandle[2]; + SDL_Joystick *joyhandle[16]; + SDL_JoystickGUID guid_list[16]; + SDL_JoystickGUID guid_assign[16]; + QString names[16]; EMU *p_emu; protected: bool bRunThread; @@ -141,6 +145,8 @@ class JoyThreadClass : public QThread { void y_axis_changed(int, int); void button_down(int, unsigned int); void button_up(int, unsigned int); + bool CheckJoyGUID(SDL_JoystickGUID *a); + bool MatchJoyGUID(SDL_JoystickGUID *a, SDL_JoystickGUID *b); public: JoyThreadClass(QObject *parent = 0); ~JoyThreadClass(); diff --git a/source/src/qt/gui/mainwidget.h b/source/src/qt/gui/mainwidget.h index 396d8281b..9717d77fc 100644 --- a/source/src/qt/gui/mainwidget.h +++ b/source/src/qt/gui/mainwidget.h @@ -363,7 +363,6 @@ class Ui_MainWindow : public QMainWindow // Constructor class EmuThreadClass *hRunEmu; class DrawThreadClass *hDrawEmu; - class JoyThreadClass *hRunJoy; public: Ui_MainWindow(QWidget *parent = 0); -- 2.11.0