InputComponent.cpp 8.9 KB

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