OSDN Git Service

[UI][Joystick][Qt][SDL] Add support for joystick with separate thread (via SDL2).
authorK.Ohta <whatisthis.sowhat@gmail.com>
Wed, 14 Jan 2015 21:45:03 +0000 (06:45 +0900)
committerK.Ohta <whatisthis.sowhat@gmail.com>
Wed, 14 Jan 2015 21:45:03 +0000 (06:45 +0900)
12 files changed:
source/src/common.h
source/src/config.cpp
source/src/emu.cpp
source/src/emu.h
source/src/qt/common/emu_utils.cpp
source/src/qt/common/qt_gldraw.cpp
source/src/qt/common/qt_glevents.cpp
source/src/qt/common/qt_input.cpp
source/src/qt/common/qt_main.cpp
source/src/qt/common/qt_main.h
source/src/qt/common/sdl_sound.cpp
source/src/qt/gui/mainwidget.h

index 58158b7..352959e 100644 (file)
@@ -15,7 +15,7 @@
 #include <agar/core.h>\r
 #include <stdarg.h>\r
 #elif defined(_USE_QT)\r
-#include <SDL/SDL.h>\r
+#include <SDL2/SDL.h>\r
 #include <stdarg.h>\r
 #include <QtCore/QString>\r
 #include <QtCore/QFile>\r
index 0cff76d..d66d8b4 100644 (file)
@@ -16,7 +16,7 @@
 #endif\r
 \r
 #ifdef _USE_QT\r
-# include <SDL/SDL.h>\r
+//# include <SDL/SDL.h>\r
 #include <string>\r
 #include <vector>\r
 #include "fileio.h"\r
index 9504187..b4c9fc5 100644 (file)
@@ -16,7 +16,7 @@
 #include "agar_logger.h"\r
 #include <ctime>\r
 # elif defined(_USE_QT)\r
-#include <SDL/SDL.h>\r
+//#include <SDL/SDL.h>\r
 #include "qt_main.h"\r
 #include "agar_logger.h"\r
 #include <ctime>\r
index cba4e85..17938c7 100644 (file)
@@ -35,7 +35,7 @@
 #endif\r
 \r
 #elif defined(_USE_QT)\r
-# include <SDL/SDL.h>\r
+# include <SDL2/SDL.h>\r
 //# include "menuclasses.h"\r
 # include "mainwidget.h"\r
 # include "qt_gldraw.h"\r
@@ -672,6 +672,10 @@ private:
        \r
 public:\r
         bool bDrawLine[SCREEN_HEIGHT];\r
+        uint32_t *getJoyStatPtr(void) {\r
+          return joy_status;\r
+       }\r
+   \r
        // ----------------------------------------\r
        // initialize\r
        // ----------------------------------------\r
index 1bee87a..0f26144 100644 (file)
@@ -1,5 +1,5 @@
 
-#include <SDL/SDL.h>
+//#include <SDL/SDL.h>
 #include <stdio.h>
 #include <string>
 #include <vector>
index 6295017..7baa41f 100644 (file)
@@ -10,7 +10,7 @@
 
 #include <QtGui>
 #include <QtOpenGL/QGLWidget>
-#include <SDL/SDL.h>
+//#include <SDL/SDL.h>
 #ifdef _WINDOWS
 #include <GL/gl.h>
 #include <GL/glext.h>
index a0071f5..3f82069 100644 (file)
@@ -10,7 +10,7 @@
 
 #include <QtGui>
 #include <QtOpenGL/QGLWidget>
-#include <SDL/SDL.h>
+//#include <SDL/SDL.h>
 #ifdef _WINDOWS
 #include <GL/gl.h>
 #include <GL/glext.h>
index c1fded1..ac5dab1 100644 (file)
@@ -375,21 +375,20 @@ void EMU::initialize_input()
        memset(mouse_status, 0, sizeof(mouse_status));
        
        // initialize joysticks
-#if 0
-        joy_num = joyGetNumDevs();
-       for(int i = 0; i < joy_num && i < 2; i++) {
-               JOYCAPS joycaps;
-               if(joyGetDevCaps(i, &joycaps, sizeof(joycaps)) == JOYERR_NOERROR) {
-                       joy_mask[i] = (1 << joycaps.wNumButtons) - 1;
-               } else {
-                       joy_mask[i] = 0x0f; // 4buttons
-               }
-       }
-#else
-        joy_num = 0;
-#endif 
        // mouse emulation is disenabled
        mouse_enabled = false;
+        joy_num = SDL_NumJoysticks();
+        for(int i = 0; i < joy_num && i < 2; i++) {
+          //SDL_Joystick *joycaps = SDL_JoystickOpen(i);
+          //if(joycaps != NULL) {
+          //   joy_mask[i] = (1 << SDL_JoystickNumButtons(joycaps)) - 1;
+          //   SDL_JoystickClose(joycaps);
+          //} else {
+             joy_mask[i] = 0x0f; // 4buttons
+          //}
+  
+       }
+
        
        // initialize keycode convert table
        FILEIO* fio = new FILEIO();
@@ -433,6 +432,7 @@ void EMU::release_input()
 #endif
 }
 
+
 void EMU::update_input()
 {
 
@@ -469,21 +469,8 @@ void EMU::update_input()
                }
        }
        lost_focus = false;
-#if 0  
+#if 1  
        // update joystick status
-       memset(joy_status, 0, sizeof(joy_status));
-       for(int i = 0; i < joy_num && i < 2; i++) {
-               JOYINFOEX joyinfo;
-               joyinfo.dwSize = sizeof(JOYINFOEX);
-               joyinfo.dwFlags = JOY_RETURNALL;
-               if(joyGetPosEx(i, &joyinfo) == JOYERR_NOERROR) {
-                       if(joyinfo.dwYpos < 0x3fff) joy_status[i] |= 0x01;      // up
-                       if(joyinfo.dwYpos > 0xbfff) joy_status[i] |= 0x02;      // down
-                       if(joyinfo.dwXpos < 0x3fff) joy_status[i] |= 0x04;      // left
-                       if(joyinfo.dwXpos > 0xbfff) joy_status[i] |= 0x08;      // right
-                       joy_status[i] |= ((joyinfo.dwButtons & joy_mask[i]) << 4);
-               }
-       }
 #ifdef USE_KEY_TO_JOY
        // emulate joystick #1 with keyboard
        if(key_status[0x26]) joy_status[0] |= 0x01;     // up
index 6640717..ca5d7c4 100644 (file)
@@ -20,7 +20,7 @@
 #include "qt_main.h"\r
 #include "agar_logger.h"\r
 \r
-#include <SDL/SDL.h>\r
+//#include <SDL/SDL.h>\r
 //#include <agar/dev.h>\r
 \r
 \r
@@ -49,7 +49,6 @@ static int close_notified = 0;
 \r
 const int screen_mode_width[]  = {320, 320, 640, 640, 800, 1024, 1280, 1280, 1440, 1440, 1600, 1600, 1920, 1920, 0};\r
 const int screen_mode_height[] = {200, 240, 400, 480, 600, 768,  800,  960,  900,  1080, 1000, 1200, 1080, 1400, 0};\r
-bool bRunJoyThread = false;\r
 \r
 // timing control\r
 #define MAX_SKIP_FRAMES 10\r
@@ -160,20 +159,95 @@ void EmuThread(void *p)
   } while(1);\r
 }\r
 \r
-void JoyThreadClass::run()\r
+\r
+bool  EventSDL(SDL_Event *eventQueue)\r
 {\r
-  bRunJoyThread = TRUE;\r
+//     SDL_Surface *p;\r
+        Sint16 value;\r
+        uint32_t *joy_status;\r
+        uint8_t button;\r
+        int i;\r
+        if(eventQueue == NULL) return;\r
+       /*\r
+        * JoyStickなどはSDLが管理する\r
+        */\r
+//     AG_SDL_GetNextEvent(void *obj, AG_DriverEvent *dev)\r
+        if(emu == NULL) return false;\r
+       if(SDL_WasInit(SDL_INIT_JOYSTICK) != 0) {\r
+//        while (SDL_PollEvent(eventQueue))\r
+//        {\r
+             switch (eventQueue->type){\r
+                case SDL_JOYAXISMOTION:\r
+                     value = eventQueue->jaxis.value;\r
+                     i = eventQueue->jaxis.which;\r
+                     if((i < 0) || (i > 1)) break;\r
+\r
+                     emu->LockVM();\r
+                  joy_status = emu->getJoyStatPtr();\r
+                  if(eventQueue->jaxis.axis == 0) { // X\r
+                     if(value < -8192) {joy_status[i] |= 0x04; joy_status[i] &= 0xfffffff7;} // left\r
+                     if(value > 8192)  {joy_status[i] |= 0x08; joy_status[i] &= 0xfffffffb;}  // right\r
+                  } else if(eventQueue->jaxis.axis == 1) { // Y\r
+                     if(value < -8192) {joy_status[i] |= 0x01; joy_status[i] &= 0xfffffffd;}// up\r
+                     if(value > 8192)  {joy_status[i] |= 0x02; joy_status[i] &= 0xfffffffe;}// down;\r
+                  }\r
+                  emu->UnlockVM();\r
+                  break;\r
+              case SDL_JOYBUTTONDOWN:\r
+                     button = eventQueue->jbutton.button;\r
+                     i = eventQueue->jbutton.which;\r
+                     if((i < 0) || (i > 1)) break;\r
+                   emu->LockVM();\r
+                   joy_status = emu->getJoyStatPtr();\r
+                   joy_status[i] |= (1 << (button + 4));\r
+                   emu->UnlockVM();\r
+                   break;\r
+              case SDL_JOYBUTTONUP:\r
+                     button = eventQueue->jbutton.button;\r
+                     i = eventQueue->jbutton.which;\r
+                     if((i < 0) || (i > 1)) break;\r
+                   emu->LockVM();\r
+                   joy_status = emu->getJoyStatPtr();\r
+                   joy_status[i] &= ~(1 << (button + 4));\r
+                   emu->UnlockVM();\r
+                   break;\r
+              default: // Is right Ignoring?\r
+                    break;\r
+             }\r
+          }\r
+//     }\r
+   return TRUE;\r
+}\r
+\r
+\r
+\r
+//void JoyThreadClass::run()\r
+void JoyThread(void *p)\r
+{\r
+  int joy_num;\r
+  int i;\r
+  SDL_Joystick *joyhandle[2] = {NULL, NULL};\r
+  SDL_Event event;\r
+  for(i = 0; i < 2; i++) joyhandle[i] = SDL_JoystickOpen(i);\r
+  joy_num = SDL_NumJoysticks();\r
+  uint32 *joy_status = NULL;\r
   do {\r
-//     rMainWindow->getGraphicsView()->updateGL();\r
-    // Event Handling for joystick\r
-    SDL_Delay(10); // Right?\r
+       if(rMainWindow->GetJoyThreadEnabled() != true) {\r
+         for(i = 0; i < 2; i++) {\r
+            if(joyhandle[i] != NULL) SDL_JoystickClose(joyhandle[i]);\r
+         }\r
+         //exit(0);\r
+         return;\r
+       }\r
+     if(SDL_WaitEventTimeout(&event, 10) == 1) EventSDL(&event);\r
   } while(1);\r
 }\r
+   \r
 \r
 //EmuThreadClass *hEmuThread;\r
-JoyThreadClass *pJoyThread;\r
+//JoyThreadClass *pJoyThread;\r
 //QThread *hEmuThread;\r
-QThread *hJoyThread;\r
+//QThread *hJoyThread;\r
 \r
 // Important Flags\r
 AGAR_CPUID *pCpuID;\r
@@ -355,18 +429,9 @@ void Ui_MainWindow::OnMainWindowClosed(void)
       }\r
     }\r
 #endif\r
+    StopJoyThread();\r
     StopEmuThread();\r
-\r
-    bRunJoyThread = false;\r
-    if(hJoyThread != NULL) {\r
-      hJoyThread->terminate();\r
-//      do {\r
-       SDL_Delay(50);\r
-//     if(hJoyThread->finished()) break;\r
-//      } while(1);\r
-      delete hJoyThread;\r
-      hJoyThread = NULL;\r
-    }\r
+   \r
     // release emulation core\r
     if(emu) {\r
       SDL_Delay(50);\r
@@ -409,39 +474,7 @@ bool InitInstance(int argc, char *argv[])
 #endif\r
 \r
 \r
-BOOL EventSDL(void)\r
-{\r
-//     SDL_Surface *p;\r
-       SDL_Event eventQueue;\r
-\r
-       /*\r
-        * JoyStickなどはSDLが管理する\r
-        */\r
-//     AG_SDL_GetNextEvent(void *obj, AG_DriverEvent *dev)\r
 \r
-       if(SDL_WasInit(SDL_INIT_JOYSTICK) != 0) {\r
-               while (SDL_PollEvent(&eventQueue))\r
-               {\r
-                       switch (eventQueue.type)\r
-                       {\r
-                       case SDL_JOYAXISMOTION: /* JS */\r
-                               //OnMoveJoy(&eventQueue);\r
-                               break;\r
-                       case SDL_JOYBUTTONDOWN:\r
-                               //OnPressJoy(&eventQueue);\r
-                               break;\r
-                       case SDL_JOYBUTTONUP:\r
-                               //OnReleaseJoy(&eventQueue);\r
-                               break;\r
-                       default: // Is right Ignoring?\r
-                         //                            ConvertSDLEvent(drv, &eventQueue, &event);\r
-                         //      if(!EventGuiSingle(drv, &event)) return FALSE;\r
-                               break;\r
-                       }\r
-               }\r
-       }\r
-       return TRUE;\r
-}\r
 \r
 int MainLoop(int argc, char *argv[])\r
 {\r
@@ -517,6 +550,7 @@ int MainLoop(int argc, char *argv[])
   // Launch Emulator loop\r
 #if 1\r
   rMainWindow->LaunchEmuThread(EmuThread);\r
+  rMainWindow->LaunchJoyThread(JoyThread);\r
   //hEmuThread = new QThread();\r
   //pEmuThread->property("Emu Thread");\r
   //pEmuThread->moveToThread(hEmuThread);\r
@@ -524,7 +558,7 @@ int MainLoop(int argc, char *argv[])
 //                      rMainWindow->getGraphicsView(), rMainWindow->getGraphicsView()->update_screenChanged(int));\r
   //hEmuThread->start();\r
   // Launch JoystickClass\r
-  bRunJoyThread = false;\r
+//  bRunJoyThread = false;\r
   //pJoyThread = new JoyThreadClass();\r
   //hJoyThread = new QThread();\r
   //hJoyThread->property("SDL Joy Thread");\r
index cbc9511..19d4227 100644 (file)
@@ -12,7 +12,7 @@
 
 #include <string>
 #include <qthread.h>
-#include <SDL/SDL.h>
+#include <SDL2/SDL.h>
 #include "sdl_cpuid.h"
 #include "simd_types.h"
 #include "common.h"
index ba839f2..844498d 100644 (file)
@@ -3,7 +3,7 @@
  * (C) 2014-12-30 K.Ohta <whatisthis.sowhat@gmail.com>
  */
 
-#include <SDL/SDL.h>
+//#include <SDL/SDL.h>
 #include <ctime>
 #include "emu.h"
 #include "vm/vm.h"
index d9db165..82dc25b 100644 (file)
@@ -285,6 +285,8 @@ class Ui_MainWindow : public QMainWindow
      // Constructor
     SDL_Thread *hRunEmuThread;
     bool bRunEmuThread;
+    SDL_Thread *hRunJoyThread;
+    bool bRunJoyThread;
 public:
  Ui_MainWindow(QWidget *parent = 0);
   ~Ui_MainWindow();
@@ -300,14 +302,29 @@ public:
       hRunEmuThread = NULL;
     }
   }
-   void LaunchEmuThread(int (*fn)(void *))
+   void StopJoyThread(void) {
+    bRunJoyThread = false;
+    if(hRunJoyThread != NULL) {
+      SDL_WaitThread(hRunJoyThread, NULL);
+      hRunJoyThread = NULL;
+    }
+  }
+  void LaunchEmuThread(int (*fn)(void *))
   {
     bRunEmuThread = true;
-    hRunEmuThread = SDL_CreateThread(fn, (void *)this);
+    hRunEmuThread = SDL_CreateThread(fn, "CSP_EmuThread", (void *)this);
   }
    bool GetEmuThreadEnabled(void) {
        return bRunEmuThread;
   }
+   void LaunchJoyThread(int (*fn)(void *))
+  {
+    bRunJoyThread = true;
+    hRunJoyThread = SDL_CreateThread(fn, "CSP_JoyThread", (void *)this);
+  }
+   bool GetJoyThreadEnabled(void) {
+       return bRunJoyThread;
+  }
   // Getting important widgets.
    QMainWindow *getWindow(void) { return MainWindow; }
    QMenuBar    *getMenuBar(void) { return menubar;}