InputComponent.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <QElapsedTimer>
  9. #include <functional>
  10. class InputBase : public QObject
  11. {
  12. Q_OBJECT
  13. public:
  14. explicit InputBase(QObject* parent = nullptr) : QObject(parent) { qRegisterMetaType<InputBase::InputkeyState>("InputkeyState"); }
  15. virtual bool initInput() = 0;
  16. virtual const char* inputName() = 0;
  17. enum InputkeyState
  18. {
  19. KeyDown,
  20. KeyUp,
  21. KeyPressed
  22. };
  23. Q_ENUM(InputkeyState)
  24. signals:
  25. void receivedInput(const QString& source, const QString& keycode, InputkeyState keystate);
  26. };
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. // well known keys
  29. ///////////////////////////////////////////////////////////////////////////////////////////////////
  30. #define INPUT_KEY_LEFT "KEY_LEFT"
  31. #define INPUT_KEY_RIGHT "KEY_RIGHT"
  32. #define INPUT_KEY_UP "KEY_UP"
  33. #define INPUT_KEY_DOWN "KEY_DOWN"
  34. #define INPUT_KEY_SELECT "KEY_SELECT"
  35. #define INPUT_KEY_MENU "KEY_MENU"
  36. #define INPUT_KEY_PLAY "KEY_PLAY"
  37. #define INPUT_KEY_PAUSE "KEY_PAUSE"
  38. #define INPUT_KEY_PLAY_PAUSE "KEY_PLAY_PAUSE"
  39. #define INPUT_KEY_STOP "KEY_STOP"
  40. #define INPUT_KEY_DOWN "KEY_DOWN"
  41. #define INPUT_KEY_BACK "KEY_BACK"
  42. #define INPUT_KEY_SEEKFWD "KEY_SEEKFWD"
  43. #define INPUT_KEY_SEEKBCK "KEY_SEEKBCK"
  44. #define INPUT_KEY_SUBTITLES "KEY_SUBTITLES"
  45. #define INPUT_KEY_INFO "KEY_INFO"
  46. #define INPUT_KEY_NEXT "KEY_NEXT"
  47. #define INPUT_KEY_PREV "KEY_PREV"
  48. #define INPUT_KEY_RED "KEY_RED"
  49. #define INPUT_KEY_GREEN "KEY_GREEN"
  50. #define INPUT_KEY_BLUE "KEY_BLUE"
  51. #define INPUT_KEY_YELLOW "KEY_YELLOW"
  52. #define INPUT_KEY_HOME "KEY_HOME"
  53. #define INPUT_KEY_0 "KEY_NUMERIC_0"
  54. #define INPUT_KEY_1 "KEY_NUMERIC_1"
  55. #define INPUT_KEY_2 "KEY_NUMERIC_2"
  56. #define INPUT_KEY_3 "KEY_NUMERIC_3"
  57. #define INPUT_KEY_4 "KEY_NUMERIC_4"
  58. #define INPUT_KEY_5 "KEY_NUMERIC_5"
  59. #define INPUT_KEY_6 "KEY_NUMERIC_6"
  60. #define INPUT_KEY_7 "KEY_NUMERIC_7"
  61. #define INPUT_KEY_8 "KEY_NUMERIC_8"
  62. #define INPUT_KEY_9 "KEY_NUMERIC_9"
  63. #define INPUT_KEY_GUIDE "KEY_GUIDE"
  64. #define INPUT_KEY_LEFT_LONG "KEY_LEFT_LONG"
  65. #define INPUT_KEY_RIGHT_LONG "KEY_RIGHT_LONG"
  66. #define INPUT_KEY_UP_LONG "KEY_UP_LONG"
  67. #define INPUT_KEY_DOWN_LONG "KEY_DOWN_LONG"
  68. #define INPUT_KEY_SELECT_LONG "KEY_SELECT_LONG"
  69. #define INPUT_KEY_MENU_LONG "KEY_MENU_LONG"
  70. #define INPUT_KEY_PLAY_LONG "KEY_PLAY_LONG"
  71. #define INPUT_KEY_DOWN_LONG "KEY_DOWN_LONG"
  72. struct ReceiverSlot
  73. {
  74. std::function<void(void)> m_function;
  75. QObject* m_receiver;
  76. QByteArray m_slot;
  77. bool m_hasArguments;
  78. };
  79. class InputComponent : public ComponentBase
  80. {
  81. Q_OBJECT
  82. DEFINE_SINGLETON(InputComponent);
  83. public:
  84. const char* componentName() override { return "input"; }
  85. bool componentExport() override { return true; }
  86. bool componentInitialize() override;
  87. void registerHostCommand(const QString& command, QObject* receiver, const char* slot);
  88. void registerHostCommand(const QString& command, std::function<void(void)> function);
  89. // Called by web to actually execute pending actions. This is done in reaction
  90. // to hostInput(). The actions parameter contains a list of actions which
  91. // should be actually dispatched.
  92. Q_INVOKABLE void executeActions(const QStringList& actions);
  93. void cancelAutoRepeat();
  94. void sendAction(const QString action);
  95. signals:
  96. // Always emitted when any input arrives
  97. void receivedInput();
  98. // Emitted when new input arrives. Each entry is an action that matches
  99. // in the keymap, such as "host:fullscreen".
  100. void hostInput(const QStringList& actions);
  101. private Q_SLOTS:
  102. void remapInput(const QString& source, const QString& keycode, InputBase::InputkeyState keyState);
  103. private:
  104. explicit InputComponent(QObject *parent = nullptr);
  105. bool addInput(InputBase* base);
  106. void handleAction(const QString& action);
  107. QHash<QString, ReceiverSlot*> m_hostCommands;
  108. QList<InputBase*> m_inputs;
  109. InputMapping* m_mappings;
  110. QTimer* m_autoRepeatTimer;
  111. QStringList m_autoRepeatActions;
  112. QVariantMap m_currentLongPressAction;
  113. QElapsedTimer m_longHoldTimer;
  114. };
  115. #endif // INPUTADAPTER_H