InputComponent.h 3.7 KB

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