Browse Source

Adapt InputSDL to new InputComponent

Allows to have again a proper autorepeat
The analog values handling has been removed, because it's not used.
LongChair 9 years ago
parent
commit
ad8cfaa6cb
2 changed files with 17 additions and 77 deletions
  1. 17 76
      src/input/InputSDL.cpp
  2. 0 1
      src/input/InputSDL.h

+ 17 - 76
src/input/InputSDL.cpp

@@ -64,8 +64,6 @@ void InputSDLWorker::run()
 {
   QElapsedTimer polltimer;
 
-  QHash<int,int> repeats;
-
   while (true)
   {
     SDL_Event event;
@@ -85,29 +83,18 @@ void InputSDLWorker::run()
         {
           QLOG_DEBUG() << "SDL Got button down for button #" << event.jbutton.button
                        << " on Joystick #" << event.jbutton.which;
-          // emit that the button has gone down
-          emit receivedInput(nameForId(event.jbutton.which), QString("KEY_BUTTON_%1").arg(event.jbutton.button));
+
+          emit receivedInput(nameForId(event.jbutton.which), QString("KEY_BUTTON_%1").arg(event.jbutton.button, true));
           
-          // set up the repeat timer for this button
-          auto  repeatTimer = new QElapsedTimer();
-          m_buttonTimestamps[event.jbutton.button] = repeatTimer;
-          repeatTimer->start();
-          // reset the repeat count for this button
-          repeats[event.jbutton.button] = 0;
           break;
         }
 
         case SDL_JOYBUTTONUP:
         {
-          if (m_buttonTimestamps[event.jbutton.button])
-          {          
-            // remove the repeat timer for this button
-            delete m_buttonTimestamps[event.jbutton.button];
-            m_buttonTimestamps.remove(event.jbutton.button);
-          }
+          QLOG_DEBUG() << "SDL Got button up for button #" << event.jbutton.button
+                       << " on Joystick #" << event.jbutton.which;
 
-          // reset the repeat count
-          repeats[event.jbutton.button] = 0;
+          emit receivedInput(nameForId(event.jbutton.which), QString("KEY_BUTTON_%1").arg(event.jbutton.button), false);
 
           break;
         }
@@ -128,28 +115,6 @@ void InputSDLWorker::run()
 
         case SDL_JOYAXISMOTION:
         {
-          int deadband = 32768 * 0.15;
-
-          // handle the analog value push
-          // keep a dead band of 15%
-          if (std::abs(event.jaxis.value) > deadband)
-          {
-            // normalize the value
-            float normalizedvalue;
-            QString keyname;
-            if (event.jaxis.value > 0)
-            {
-              normalizedvalue = (float)(event.jaxis.value - deadband) / (float)(32767 - deadband);
-              keyname = QString("KEY_AXIS_%1_VAL_UP").arg(event.jaxis.axis);
-            }
-            else
-            {
-              normalizedvalue = (float)(std::abs(event.jaxis.value) - deadband) / (float)(32768 - deadband);
-              keyname = QString("KEY_AXIS_%1_VAL_DOWN").arg(event.jaxis.axis);
-            }
-
-            emit receivedInput(nameForId(event.jaxis.which), keyname, normalizedvalue);
-          }
 
           // handle the Digital conversion of the analog axis
           if (std::abs(event.jaxis.value) > 32768 / 2)
@@ -159,48 +124,25 @@ void InputSDLWorker::run()
               m_axisState[event.jaxis.axis] = 1;
 
               if (event.jaxis.value > 0)
-                emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_UP").arg(event.jaxis.axis));
+                emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_UP").arg(event.jaxis.axis), true);
               else
-                emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_DOWN").arg(event.jaxis.axis));
-
-              break;
+                emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_DOWN").arg(event.jaxis.axis), true);
             }
           }
+          else
+          {
+            if (m_axisState[event.jaxis.axis])
+            {
+              emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_UP").arg(event.jaxis.axis), false);
+              emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_DOWN").arg(event.jaxis.axis), false);
+            }
 
-          m_axisState[event.jaxis.axis] = 0;
-          break;
-        }
-      }
-    }
-
-    // check for long press and repeats
-    SDLTimeStampMapIterator it = m_buttonTimestamps.constBegin();
-    while (it != m_buttonTimestamps.constEnd())
-    {
-      if (it.value())
-      {
-        // repeat at a rate of SDL_BUTTON_REPEAT_RATE
-        if (repeats[it.key()] > 0 && m_buttonTimestamps[it.key()]->elapsed() > SDL_BUTTON_REPEAT_RATE)
-        {
-          QLOG_DEBUG() << QString("SDL Got button repeat(%1) for button #").arg(repeats[it.key()]) << event.jbutton.button
-                       << " on Joystick #" << event.jbutton.which;
-          emit receivedInput(nameForId(event.jbutton.which), QString("KEY_BUTTON_%1").arg(it.key()));
-
-          m_buttonTimestamps[it.key()]->restart();
-          it = m_buttonTimestamps.constBegin();
+            m_axisState[event.jaxis.axis] = 0;
+          }
 
-          repeats[it.key()]++;
-          continue;
-        }
-        else
-        {
-          // only start repeating if SDL_BUTTON_REPEAT_DELAY time has passed
-          if (m_buttonTimestamps[it.key()]->elapsed() > SDL_BUTTON_REPEAT_DELAY)
-            repeats[it.key()]++;
+          break;
         }
       }
-
-      it++;
     }
 
     // make the poll time fixed to SDL_POLL_TIME
@@ -222,7 +164,6 @@ void InputSDLWorker::refreshJoystickList()
   }
 
   m_joysticks.clear();
-  m_buttonTimestamps.clear();
 
   // list all the joysticks and open them
   int numJoysticks = SDL_NumJoysticks();

+ 0 - 1
src/input/InputSDL.h

@@ -46,7 +46,6 @@ private:
   void refreshJoystickList();
   QString nameForId(SDL_JoystickID id);
 
-  SDLTimeStampMap m_buttonTimestamps;
   SDLJoystickMap m_joysticks;
   QByteArray  m_axisState;
 };