InputComponent.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef INPUTADAPTER_H
  2. #define INPUTADAPTER_H
  3. #include <QThread>
  4. #include <QVariantMap>
  5. #include <QTimer>
  6. #include <QTime>
  7. #include "ComponentManager.h"
  8. #include "InputMapping.h"
  9. class InputBase : public QObject
  10. {
  11. Q_OBJECT
  12. public:
  13. InputBase(QObject* parent = nullptr) : QObject(parent) { }
  14. virtual bool initInput() = 0;
  15. virtual const char* inputName() = 0;
  16. signals:
  17. void receivedInput(const QString& source, const QString& keycode, bool pressDown = true);
  18. };
  19. ///////////////////////////////////////////////////////////////////////////////////////////////////
  20. // well known keys
  21. ///////////////////////////////////////////////////////////////////////////////////////////////////
  22. #define INPUT_KEY_LEFT "KEY_LEFT"
  23. #define INPUT_KEY_RIGHT "KEY_RIGHT"
  24. #define INPUT_KEY_UP "KEY_UP"
  25. #define INPUT_KEY_DOWN "KEY_DOWN"
  26. #define INPUT_KEY_SELECT "KEY_SELECT"
  27. #define INPUT_KEY_MENU "KEY_MENU"
  28. #define INPUT_KEY_PLAY "KEY_PLAY"
  29. #define INPUT_KEY_PAUSE "KEY_PAUSE"
  30. #define INPUT_KEY_STOP "KEY_STOP"
  31. #define INPUT_KEY_DOWN "KEY_DOWN"
  32. #define INPUT_KEY_BACK "KEY_BACK"
  33. #define INPUT_KEY_SEEKFWD "KEY_SEEKFWD"
  34. #define INPUT_KEY_SEEKBCK "KEY_SEEKBCK"
  35. #define INPUT_KEY_SUBTITLES "KEY_SUBTITLES"
  36. #define INPUT_KEY_INFO "KEY_INFO"
  37. #define INPUT_KEY_NEXT "KEY_NEXT"
  38. #define INPUT_KEY_PREV "KEY_PREV"
  39. #define INPUT_KEY_RED "KEY_RED"
  40. #define INPUT_KEY_GREEN "KEY_GREEN"
  41. #define INPUT_KEY_BLUE "KEY_BLUE"
  42. #define INPUT_KEY_YELLOW "KEY_YELLOW"
  43. #define INPUT_KEY_HOME "KEY_HOME"
  44. #define INPUT_KEY_0 "KEY_NUMERIC_0"
  45. #define INPUT_KEY_1 "KEY_NUMERIC_1"
  46. #define INPUT_KEY_2 "KEY_NUMERIC_2"
  47. #define INPUT_KEY_3 "KEY_NUMERIC_3"
  48. #define INPUT_KEY_4 "KEY_NUMERIC_4"
  49. #define INPUT_KEY_5 "KEY_NUMERIC_5"
  50. #define INPUT_KEY_6 "KEY_NUMERIC_6"
  51. #define INPUT_KEY_7 "KEY_NUMERIC_7"
  52. #define INPUT_KEY_8 "KEY_NUMERIC_8"
  53. #define INPUT_KEY_9 "KEY_NUMERIC_9"
  54. #define INPUT_KEY_GUIDE "KEY_GUIDE"
  55. #define INPUT_KEY_LEFT_LONG "KEY_LEFT_LONG"
  56. #define INPUT_KEY_RIGHT_LONG "KEY_RIGHT_LONG"
  57. #define INPUT_KEY_UP_LONG "KEY_UP_LONG"
  58. #define INPUT_KEY_DOWN_LONG "KEY_DOWN_LONG"
  59. #define INPUT_KEY_SELECT_LONG "KEY_SELECT_LONG"
  60. #define INPUT_KEY_MENU_LONG "KEY_MENU_LONG"
  61. #define INPUT_KEY_PLAY_LONG "KEY_PLAY_LONG"
  62. #define INPUT_KEY_DOWN_LONG "KEY_DOWN_LONG"
  63. struct ReceiverSlot
  64. {
  65. QObject* receiver;
  66. QByteArray slot;
  67. bool hasArguments;
  68. };
  69. class InputComponent : public ComponentBase
  70. {
  71. Q_OBJECT
  72. DEFINE_SINGLETON(InputComponent);
  73. public:
  74. virtual const char* componentName() { return "input"; }
  75. virtual bool componentExport() { return true; }
  76. virtual bool componentInitialize();
  77. void registerHostCommand(const QString& command, QObject* receiver, const char* slot);
  78. signals:
  79. void receivedAction(const QString& action);
  80. private Q_SLOTS:
  81. void remapInput(const QString& source, const QString& keycode, bool pressDown = true);
  82. private:
  83. InputComponent(QObject *parent = nullptr);
  84. bool addInput(InputBase* base);
  85. void handleAction(const QString& action, bool autoRepeat = true);
  86. QHash<QString, ReceiverSlot*> m_hostCommands;
  87. QList<InputBase*> m_inputs;
  88. InputMapping* m_mappings;
  89. QTimer* m_autoRepeatTimer;
  90. QVariantMap m_currentLongPressAction;
  91. qint32 m_currentActionCount;
  92. QTime m_longHoldTimer;
  93. QString m_currentAction;
  94. };
  95. #endif // INPUTADAPTER_H