InputComponent.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "QsLog.h"
  2. #include "InputComponent.h"
  3. #include "settings/SettingsComponent.h"
  4. #include "system/SystemComponent.h"
  5. #include "power/PowerComponent.h"
  6. #include "InputKeyboard.h"
  7. #include "InputSocket.h"
  8. #include "InputRoku.h"
  9. #ifdef Q_OS_MAC
  10. #include "apple/InputAppleRemote.h"
  11. #include "apple/InputAppleMediaKeys.h"
  12. #endif
  13. #ifdef HAVE_SDL
  14. #include "InputSDL.h"
  15. #endif
  16. #ifdef HAVE_LIRC
  17. #include "InputLIRC.h"
  18. #endif
  19. #ifdef HAVE_CEC
  20. #include "InputCEC.h"
  21. #endif
  22. #define LONG_HOLD_MSEC 500
  23. #define INITAL_AUTOREPEAT_MSEC 650
  24. // Synthetic key repeat events are emitted at 60ms intervals. The interval is
  25. // half of the fastest press-and-release time. Empirical key repeat intervals:
  26. // * Key hold on macOS wireless USB keyboard with fastest key repeat preference: ~35s
  27. // * Press and release on macOS wireless USB keyboard (like a meth monkey): ~120ms
  28. // * Key hold on macOS Apple TV IR remote + FLiRC (3.8 firmware) with fastest key repeat preference: ~35s
  29. // * Press and release on macOS Apple TV IR remote + FLiRC (3.8 firmware): ~150ms
  30. //
  31. #define AUTOREPEAT_MSEC 60
  32. ///////////////////////////////////////////////////////////////////////////////////////////////////
  33. InputComponent::InputComponent(QObject* parent) : ComponentBase(parent)
  34. {
  35. m_mappings = new InputMapping(this);
  36. }
  37. ///////////////////////////////////////////////////////////////////////////////////////////////////
  38. bool InputComponent::addInput(InputBase* base)
  39. {
  40. if (!base->initInput())
  41. {
  42. QLOG_WARN() << "Failed to init input:" << base->inputName();
  43. return false;
  44. }
  45. QLOG_INFO() << "Successfully inited input:" << base->inputName();
  46. m_inputs.push_back(base);
  47. // we connect to the provider receivedInput signal, then we check if the name
  48. // needs to be remaped in remapInput and then finally send it out to JS land.
  49. //
  50. connect(base, &InputBase::receivedInput, this, &InputComponent::remapInput);
  51. // for auto-repeating inputs
  52. //
  53. m_autoRepeatTimer = new QTimer(this);
  54. connect(m_autoRepeatTimer, &QTimer::timeout, [=]()
  55. {
  56. if (!m_autoRepeatActions.isEmpty())
  57. {
  58. QLOG_DEBUG() << "Emit input action (autorepeat):" << m_autoRepeatActions;
  59. emit hostInput(m_autoRepeatActions);
  60. }
  61. m_autoRepeatTimer->setInterval(AUTOREPEAT_MSEC);
  62. });
  63. return true;
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////
  66. bool InputComponent::componentInitialize()
  67. {
  68. // load our input mappings
  69. m_mappings->loadMappings();
  70. addInput(&InputKeyboard::Get());
  71. addInput(new InputSocket(this));
  72. addInput(new InputRoku(this));
  73. #ifdef Q_OS_MAC
  74. addInput(new InputAppleRemote(this));
  75. addInput(new InputAppleMediaKeys(this));
  76. #endif
  77. #ifdef HAVE_SDL
  78. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "sdlEnabled").toBool())
  79. addInput(new InputSDL(this));
  80. #endif
  81. #ifdef HAVE_LIRC
  82. addInput(new InputLIRC(this));
  83. #endif
  84. #ifdef HAVE_CEC
  85. if (SettingsComponent::Get().value(SETTINGS_SECTION_CEC, "enable").toBool())
  86. addInput(new InputCEC(this));
  87. #endif
  88. return true;
  89. }
  90. /////////////////////////////////////////////////////////////////////////////////////////
  91. void InputComponent::handleAction(const QString& action)
  92. {
  93. if (action.startsWith("host:"))
  94. {
  95. QStringList argList = action.mid(5).split(" ");
  96. QString hostCommand = argList.value(0);
  97. QString hostArguments;
  98. if (argList.size() > 1)
  99. {
  100. argList.pop_front();
  101. hostArguments = argList.join(" ");
  102. }
  103. QLOG_DEBUG() << "Got host command:" << hostCommand << "arguments:" << hostArguments;
  104. if (m_hostCommands.contains(hostCommand))
  105. {
  106. ReceiverSlot* recvSlot = m_hostCommands.value(hostCommand);
  107. if (recvSlot)
  108. {
  109. if (recvSlot->m_function)
  110. {
  111. QLOG_DEBUG() << "Invoking anonymous function";
  112. recvSlot->m_function();
  113. }
  114. else
  115. {
  116. QLOG_DEBUG() << "Invoking slot" << qPrintable(recvSlot->m_slot.data());
  117. QGenericArgument arg0 = QGenericArgument();
  118. if (recvSlot->m_hasArguments)
  119. arg0 = Q_ARG(const QString&, hostArguments);
  120. if (!QMetaObject::invokeMethod(recvSlot->m_receiver, recvSlot->m_slot.data(),
  121. Qt::AutoConnection, arg0))
  122. {
  123. QLOG_ERROR() << "Invoking slot" << qPrintable(recvSlot->m_slot.data()) << "failed!";
  124. }
  125. }
  126. }
  127. }
  128. else
  129. {
  130. QLOG_WARN() << "No such host command:" << hostCommand;
  131. }
  132. }
  133. }
  134. ///////////////////////////////////////////////////////////////////////////////////////////////////
  135. void InputComponent::remapInput(const QString &source, const QString &keycode, InputBase::InputkeyState keyState)
  136. {
  137. QLOG_DEBUG() << "Input received: source:" << source << "keycode:" << keycode << ":" << keyState;
  138. emit receivedInput();
  139. if (keyState == InputBase::KeyUp)
  140. {
  141. cancelAutoRepeat();
  142. if (!m_currentLongPressAction.isEmpty())
  143. {
  144. QString type;
  145. if (m_longHoldTimer.elapsed() > LONG_HOLD_MSEC)
  146. type = "long";
  147. else
  148. type = "short";
  149. QString action = m_currentLongPressAction.value(type).toString();
  150. m_currentLongPressAction.clear();
  151. QLOG_DEBUG() << "Emit input action (" + type + "):" << action;
  152. emit hostInput(QStringList{action});
  153. }
  154. return;
  155. }
  156. QStringList queuedActions;
  157. m_autoRepeatActions.clear();
  158. auto actions = m_mappings->mapToAction(source, keycode);
  159. for (auto action : actions)
  160. {
  161. if (action.type() == QVariant::String)
  162. {
  163. queuedActions.append(action.toString());
  164. m_autoRepeatActions.append(action.toString());
  165. }
  166. else if (action.type() == QVariant::Map)
  167. {
  168. QVariantMap map = action.toMap();
  169. if (map.contains("long"))
  170. {
  171. // Don't overwrite long actions if there was no key up event yet.
  172. // (It could be a key autorepeated by Qt.)
  173. if (m_currentLongPressAction.isEmpty())
  174. {
  175. m_longHoldTimer.start();
  176. m_currentLongPressAction = map;
  177. }
  178. }
  179. else if (map.contains("short"))
  180. {
  181. queuedActions.append(map.value("short").toString());
  182. }
  183. }
  184. else if (action.type() == QVariant::List)
  185. {
  186. queuedActions.append(action.toStringList());
  187. }
  188. }
  189. if (!m_autoRepeatActions.isEmpty() && keyState != InputBase::KeyPressed)
  190. m_autoRepeatTimer->start(INITAL_AUTOREPEAT_MSEC);
  191. if (!queuedActions.isEmpty())
  192. {
  193. if (SystemComponent::Get().isWebClientConnected())
  194. {
  195. QLOG_DEBUG() << "Emit input action:" << queuedActions;
  196. emit hostInput(queuedActions);
  197. }
  198. else
  199. {
  200. QLOG_DEBUG() << "Web Client has not connected, handling input in host instead.";
  201. executeActions(queuedActions);
  202. }
  203. }
  204. }
  205. /////////////////////////////////////////////////////////////////////////////////////////
  206. void InputComponent::executeActions(const QStringList& actions)
  207. {
  208. for (auto action : actions)
  209. handleAction(action);
  210. }
  211. /////////////////////////////////////////////////////////////////////////////////////////
  212. void InputComponent::registerHostCommand(const QString& command, QObject* receiver, const char* slot)
  213. {
  214. auto recvSlot = new ReceiverSlot;
  215. recvSlot->m_receiver = receiver;
  216. recvSlot->m_slot = QMetaObject::normalizedSignature(slot);
  217. recvSlot->m_hasArguments = false;
  218. QLOG_DEBUG() << "Adding host command:" << qPrintable(command) << "mapped to"
  219. << qPrintable(QString(receiver->metaObject()->className()) + "::" + recvSlot->m_slot);
  220. m_hostCommands.insert(command, recvSlot);
  221. auto slotWithArgs = QString("%1(QString)").arg(QString::fromLatin1(recvSlot->m_slot)).toLatin1();
  222. auto slotWithoutArgs = QString("%1()").arg(QString::fromLatin1(recvSlot->m_slot)).toLatin1();
  223. if (recvSlot->m_receiver->metaObject()->indexOfMethod(slotWithArgs.data()) != -1)
  224. {
  225. QLOG_DEBUG() << "Host command maps to method with an argument.";
  226. recvSlot->m_hasArguments = true;
  227. }
  228. else if (recvSlot->m_receiver->metaObject()->indexOfMethod(slotWithoutArgs.data()) != -1)
  229. {
  230. QLOG_DEBUG() << "Host command maps to method without arguments.";
  231. }
  232. else
  233. {
  234. QLOG_ERROR() << "Slot for host command missing, or has incorrect signature!";
  235. }
  236. }
  237. /////////////////////////////////////////////////////////////////////////////////////////
  238. void InputComponent::registerHostCommand(const QString& command, std::function<void(void)> function)
  239. {
  240. auto recvSlot = new ReceiverSlot;
  241. recvSlot->m_function = function;
  242. QLOG_DEBUG() << "Adding host command:" << qPrintable(command) << "mapped to anonymous function";
  243. m_hostCommands.insert(command, recvSlot);
  244. }
  245. /////////////////////////////////////////////////////////////////////////////////////////
  246. void InputComponent::cancelAutoRepeat()
  247. {
  248. m_autoRepeatTimer->stop();
  249. m_autoRepeatActions.clear();
  250. }