Ver código fonte

Input: Refactor up and down to use a state enum instead of a boolean

This makes it much more explicit in what's happening.
LongChair 8 anos atrás
pai
commit
40e52adc21

+ 6 - 8
src/input/InputCEC.cpp

@@ -183,9 +183,9 @@ void InputCECWorker::checkAdapter()
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-void InputCECWorker::sendReceivedInput(const QString &source, const QString &keycode, bool pressDown)
+void InputCECWorker::sendReceivedInput(const QString &source, const QString &keycode, InputBase::InputkeyState keyState)
 {
-  emit receivedInput(source, keycode, pressDown);
+  emit receivedInput(source, keycode, keyState);
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -269,8 +269,7 @@ int InputCECWorker::CecCommand(void *cbParam, const cec_command command)
   switch(command.opcode)
   {
     case CEC_OPCODE_PLAY:
-      cec->sendReceivedInput(CEC_INPUT_NAME, INPUT_KEY_PLAY);
-      cec->sendReceivedInput(CEC_INPUT_NAME, INPUT_KEY_PLAY, false);
+      cec->sendReceivedInput(CEC_INPUT_NAME, INPUT_KEY_PLAY, InputBase::KeyPressed);
       break;
 
     case CEC_OPCODE_DECK_CONTROL:
@@ -298,8 +297,7 @@ int InputCECWorker::CecCommand(void *cbParam, const cec_command command)
         {
           // We don't have up & down events for those special keys
           // so we just fake them
-          cec->sendReceivedInput(CEC_INPUT_NAME, keyCode);
-          cec->sendReceivedInput(CEC_INPUT_NAME, keyCode, false);
+          cec->sendReceivedInput(CEC_INPUT_NAME, keyCode, InputBase::KeyPressed);
         }
       }
       break;
@@ -324,7 +322,7 @@ int InputCECWorker::CecCommand(void *cbParam, const cec_command command)
         {
           // samsung Return key
           case CEC_USER_CONTROL_CODE_AN_RETURN:
-            cec->sendReceivedInput(CEC_INPUT_NAME, INPUT_KEY_BACK);
+            cec->sendReceivedInput(CEC_INPUT_NAME, INPUT_KEY_BACK, down ? InputBase::KeyDown : InputBase::KeyUp);
             return 1;
             break;
 
@@ -336,7 +334,7 @@ int InputCECWorker::CecCommand(void *cbParam, const cec_command command)
       cmdString = cec->getCommandString((cec_user_control_code)command.parameters[0]);
 
       if (!cmdString.isEmpty())
-        cec->sendReceivedInput(CEC_INPUT_NAME, cmdString, down);
+        cec->sendReceivedInput(CEC_INPUT_NAME, cmdString, down ? InputBase::KeyDown : InputBase::KeyUp);
     }
       break;
 

+ 2 - 2
src/input/InputCEC.h

@@ -40,7 +40,7 @@ public:
   ~InputCECWorker() override;
 
   Q_SLOT bool init();
-  Q_SIGNAL void receivedInput(const QString& source, const QString& keycode, bool pressDown);
+  Q_SIGNAL void receivedInput(const QString& source, const QString& keycode, InputBase::InputkeyState keyState);
 
 public slots:
   void checkAdapter();
@@ -52,7 +52,7 @@ private:
   void closeAdapter();
 
   QString getCommandString(cec_user_control_code code);
-  void sendReceivedInput(const QString& source, const QString& keycode, bool pressDown = true);
+  void sendReceivedInput(const QString& source, const QString& keycode, InputBase::InputkeyState keyState);
   QString getCommandParamsList(cec_command command);
 
   // libcec callbacks

+ 4 - 4
src/input/InputComponent.cpp

@@ -144,13 +144,13 @@ void InputComponent::handleAction(const QString& action)
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-void InputComponent::remapInput(const QString &source, const QString &keycode, bool pressDown)
+void InputComponent::remapInput(const QString &source, const QString &keycode, InputBase::InputkeyState keyState)
 {
-  QLOG_DEBUG() << "Input received: source:" << source << "keycode:" << keycode << "pressed:" << (pressDown ? "down" : "release");
+  QLOG_DEBUG() << "Input received: source:" << source << "keycode:" << keycode << ":" << keyState;
 
   emit receivedInput();
 
-  if (!pressDown)
+  if (keyState == InputBase::KeyUp)
   {
     m_autoRepeatTimer->stop();
     m_autoRepeatActions.clear();
@@ -206,7 +206,7 @@ void InputComponent::remapInput(const QString &source, const QString &keycode, b
     }
   }
 
-  if (!m_autoRepeatActions.isEmpty())
+  if (!m_autoRepeatActions.isEmpty() && keyState != InputBase::KeyPressed)
     m_autoRepeatTimer->start(INITAL_AUTOREPEAT_MSEC);
 
   if (!queuedActions.isEmpty())

+ 10 - 2
src/input/InputComponent.h

@@ -19,8 +19,16 @@ public:
   virtual bool initInput() = 0;
   virtual const char* inputName() = 0;
   
+  enum InputkeyState
+  {
+    KeyDown,
+    KeyUp,
+    KeyPressed
+  };
+  Q_ENUM(InputkeyState)
+
 signals:
-  void receivedInput(const QString& source, const QString& keycode, bool pressDown = true);
+  void receivedInput(const QString& source, const QString& keycode, InputkeyState keystate);
 };
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -106,7 +114,7 @@ signals:
   void hostInput(const QStringList& actions);
 
 private Q_SLOTS:
-  void remapInput(const QString& source, const QString& keycode, bool pressDown = true);
+  void remapInput(const QString& source, const QString& keycode, InputBase::InputkeyState keyState);
   
 private:
   explicit InputComponent(QObject *parent = nullptr);

+ 2 - 2
src/input/InputKeyboard.h

@@ -18,9 +18,9 @@ public:
   bool initInput() override { return true; }
   const char* inputName() override { return "Keyboard"; }
 
-  void keyPress(const QKeySequence& sequence, bool press)
+  void keyPress(const QKeySequence& sequence, InputkeyState keyState)
   {
-    emit receivedInput("Keyboard", sequence.toString(), press);
+    emit receivedInput("Keyboard", sequence.toString(), keyState);
   }
 
 private:

+ 1 - 1
src/input/InputLIRC.cpp

@@ -108,7 +108,7 @@ void InputLIRC::read(int handle)
       if ((repeatCount % 3) == 0)
       {
         bool up = (command.endsWith("_UP") && (command != "KEY_UP"));
-        emit receivedInput("LIRC", command, !up);
+        emit receivedInput("LIRC", command, up ? InputBase::KeyUp : InputBase::KeyDown);
       }
     }
     else

+ 7 - 4
src/input/InputRoku.cpp

@@ -202,10 +202,13 @@ void InputRoku::handleKeyPress(QHttpRequest* request, QHttpResponse* response)
   }
 
   auto url = request->url().toString();
-  if (url.startsWith("/keydown/") || url.startsWith("/keypress/"))
-    emit receivedInput("roku", pathsplit.value(2), true);
-  if (url.startsWith("/keyup/") || url.startsWith("/keypress/"))
-    emit receivedInput("roku", pathsplit.value(2), false);
+  if (url.startsWith("/keydown/"))
+    emit receivedInput("roku", pathsplit.value(2), KeyDown);
+  else if (url.startsWith("/keyup/"))
+    emit receivedInput("roku", pathsplit.value(2), KeyUp);
+  else if (url.startsWith("/keypress/"))
+    emit receivedInput("roku", pathsplit.value(2), KeyPressed);
+
 
   response->setStatusCode(qhttp::ESTATUS_OK);
   response->end();

+ 6 - 6
src/input/InputSDL.cpp

@@ -80,13 +80,13 @@ void InputSDLWorker::run()
 
         case SDL_JOYBUTTONDOWN:
         {
-          emit receivedInput(nameForId(event.jbutton.which), QString("KEY_BUTTON_%1").arg(event.jbutton.button, true));
+          emit receivedInput(nameForId(event.jbutton.which), QString("KEY_BUTTON_%1").arg(event.jbutton.button), InputBase::KeyDown);
           break;
         }
 
         case SDL_JOYBUTTONUP:
         {
-          emit receivedInput(nameForId(event.jbutton.which), QString("KEY_BUTTON_%1").arg(event.jbutton.button), false);
+          emit receivedInput(nameForId(event.jbutton.which), QString("KEY_BUTTON_%1").arg(event.jbutton.button), InputBase::KeyUp);
           break;
         }
 
@@ -137,7 +137,7 @@ void InputSDLWorker::run()
 
           m_lastHat = hatName;
 
-          emit receivedInput(nameForId(event.jhat.which), hatName, pressed);
+          emit receivedInput(nameForId(event.jhat.which), hatName, pressed ? InputBase::KeyDown : InputBase::KeyUp);
 
           break;
         }
@@ -155,18 +155,18 @@ void InputSDLWorker::run()
             bool up = value < 0;
             if (!m_axisState.contains(axis))
             {
-              emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_%2").arg(axis).arg(up ? "UP" : "DOWN"), true);
+              emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_%2").arg(axis).arg(up ? "UP" : "DOWN"), InputBase::KeyDown);
               m_axisState.insert(axis, up);
             }
             else if (m_axisState.value(axis) != up)
             {
-              emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_%2").arg(axis).arg(m_axisState.value(axis) ? "UP" : "DOWN"), false);
+              emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_%2").arg(axis).arg(m_axisState.value(axis) ? "UP" : "DOWN"), InputBase::KeyUp);
               m_axisState.remove(axis);
             }
           }
           else if (std::abs(value) < 10000 && m_axisState.contains(axis)) // back to the center.
           {
-            emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_%2").arg(axis).arg(m_axisState.value(axis) ? "UP" : "DOWN"), false);
+            emit receivedInput(nameForId(event.jaxis.which), QString("KEY_AXIS_%1_%2").arg(axis).arg(m_axisState.value(axis) ? "UP" : "DOWN"), InputBase::KeyUp);
             m_axisState.remove(axis);
           }
           break;

+ 1 - 1
src/input/InputSDL.h

@@ -40,7 +40,7 @@ public slots:
   void close();
 
 signals:
-  void receivedInput(const QString& source, const QString& keycode, bool pressDown = true);
+  void receivedInput(const QString& source, const QString& keycode, InputBase::InputkeyState keyState);
 
 private:
   void refreshJoystickList();

+ 1 - 1
src/input/InputSocket.cpp

@@ -38,5 +38,5 @@ void InputSocket::messageReceived(const QVariant& message)
   QLOG_DEBUG() << "Input from client:" << map.value("client").toString() << " - " <<
                map.value("source").toString() << map.value("keycode").toString();
 
-  emit receivedInput(map.value("source").toString(), map.value("keycode").toString(), 1);
+  emit receivedInput(map.value("source").toString(), map.value("keycode").toString(), KeyPressed);
 }

+ 21 - 23
src/input/apple/InputAppleMediaKeys.mm

@@ -43,30 +43,28 @@
 
   QString keyPressed;
 
-  if (keyIsPressed) {
-    switch (keyCode) {
-      case NX_KEYTYPE_PLAY:
-        keyPressed = INPUT_KEY_PLAY;
-        break;
-      case NX_KEYTYPE_FAST:
-        keyPressed = "KEY_FAST";
-        break;
-      case NX_KEYTYPE_REWIND:
-        keyPressed = "KEY_REWIND";
-        break;
-      case NX_KEYTYPE_NEXT:
-        keyPressed = INPUT_KEY_NEXT;
-        break;
-      case NX_KEYTYPE_PREVIOUS:
-        keyPressed = INPUT_KEY_PREV;
-        break;
-      default:
-        break;
-        // More cases defined in hidsystem/ev_keymap.h
-    }
-
-    emit input->receivedInput("AppleMediaKeys", keyPressed);
+  switch (keyCode) {
+    case NX_KEYTYPE_PLAY:
+      keyPressed = INPUT_KEY_PLAY;
+      break;
+    case NX_KEYTYPE_FAST:
+      keyPressed = "KEY_FAST";
+      break;
+    case NX_KEYTYPE_REWIND:
+      keyPressed = "KEY_REWIND";
+      break;
+    case NX_KEYTYPE_NEXT:
+      keyPressed = INPUT_KEY_NEXT;
+      break;
+    case NX_KEYTYPE_PREVIOUS:
+      keyPressed = INPUT_KEY_PREV;
+      break;
+    default:
+      break;
+      // More cases defined in hidsystem/ev_keymap.h
   }
+
+  emit input->receivedInput("AppleMediaKeys", keyPressed, keyIsPressed ? InputBase::KeyDown : InputBase::KeyUp);
 }
 
 @end

+ 1 - 1
src/input/apple/InputAppleRemote.mm

@@ -56,7 +56,7 @@ void InputAppleRemote::remoteButtonEvent(quint8 code, bool pressed, const QStrin
   else
     eventName = QString::number(code);
 
-  emit receivedInput("AppleRemote", eventName, pressed);
+  emit receivedInput("AppleRemote", eventName, pressed ? KeyDown : KeyUp);
 }
 
 /////////////////////////////////////////////////////////////////////////////////////////

+ 7 - 2
src/ui/EventFilter.cpp

@@ -31,7 +31,12 @@ bool EventFilter::eventFilter(QObject* watched, QEvent* event)
     // keyboard buttons to different events.
     //
 
-    bool pressed = event->type() == QEvent::KeyPress;
+    InputBase::InputkeyState keystatus;
+
+    if (event->type() == QEvent::KeyPress)
+      keystatus = InputBase::KeyDown;
+    else
+      keystatus = InputBase::KeyUp;
 
     QKeyEvent* kevent = dynamic_cast<QKeyEvent*>(event);
     if (kevent)
@@ -41,7 +46,7 @@ bool EventFilter::eventFilter(QObject* watched, QEvent* event)
       {
         // We ignore the KeypadModifier here since it's practically useless
         QKeySequence key(kevent->key() | (kevent->modifiers() &= ~Qt::KeypadModifier));
-        InputKeyboard::Get().keyPress(key, pressed);
+        InputKeyboard::Get().keyPress(key, keystatus);
         return true;
       }
     }