InputComponent.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. ///////////////////////////////////////////////////////////////////////////////////////////////////
  25. InputComponent::InputComponent(QObject* parent) : ComponentBase(parent), m_autoRepeatCount(0)
  26. {
  27. m_mappings = new InputMapping(this);
  28. }
  29. ///////////////////////////////////////////////////////////////////////////////////////////////////
  30. bool InputComponent::addInput(InputBase* base)
  31. {
  32. if (!base->initInput())
  33. {
  34. QLOG_WARN() << "Failed to init input:" << base->inputName();
  35. return false;
  36. }
  37. QLOG_INFO() << "Successfully inited input:" << base->inputName();
  38. m_inputs.push_back(base);
  39. // we connect to the provider receivedInput signal, then we check if the name
  40. // needs to be remaped in remapInput and then finally send it out to JS land.
  41. //
  42. connect(base, &InputBase::receivedInput, this, &InputComponent::remapInput);
  43. // for auto-repeating inputs
  44. //
  45. m_autoRepeatTimer = new QTimer(this);
  46. connect(m_autoRepeatTimer, &QTimer::timeout, [=]()
  47. {
  48. if (!m_autoRepeatActions.isEmpty())
  49. {
  50. m_autoRepeatCount ++;
  51. QLOG_DEBUG() << "Emit input action (autorepeat):" << m_autoRepeatActions;
  52. emit hostInput(m_autoRepeatActions);
  53. }
  54. qint32 multiplier = qMin(5, qMax(1, m_autoRepeatCount / 5));
  55. m_autoRepeatTimer->setInterval(100 / multiplier);
  56. });
  57. return true;
  58. }
  59. ///////////////////////////////////////////////////////////////////////////////////////////////////
  60. bool InputComponent::componentInitialize()
  61. {
  62. // load our input mappings
  63. m_mappings->loadMappings();
  64. addInput(&InputKeyboard::Get());
  65. addInput(new InputSocket(this));
  66. addInput(new InputRoku(this));
  67. #ifdef Q_OS_MAC
  68. addInput(new InputAppleRemote(this));
  69. addInput(new InputAppleMediaKeys(this));
  70. #endif
  71. #ifdef HAVE_SDL
  72. addInput(new InputSDL(this));
  73. #endif
  74. #ifdef HAVE_LIRC
  75. addInput(new InputLIRC(this));
  76. #endif
  77. #ifdef HAVE_CEC
  78. if (SettingsComponent::Get().value(SETTINGS_SECTION_CEC, "enable").toBool())
  79. addInput(new InputCEC(this));
  80. #endif
  81. return true;
  82. }
  83. /////////////////////////////////////////////////////////////////////////////////////////
  84. void InputComponent::handleAction(const QString& action)
  85. {
  86. if (action.startsWith("host:"))
  87. {
  88. QStringList argList = action.mid(5).split(" ");
  89. QString hostCommand = argList.value(0);
  90. QString hostArguments;
  91. if (argList.size() > 1)
  92. {
  93. argList.pop_front();
  94. hostArguments = argList.join(" ");
  95. }
  96. QLOG_DEBUG() << "Got host command:" << hostCommand << "arguments:" << hostArguments;
  97. if (m_hostCommands.contains(hostCommand))
  98. {
  99. ReceiverSlot* recvSlot = m_hostCommands.value(hostCommand);
  100. if (recvSlot)
  101. {
  102. if (recvSlot->m_function)
  103. {
  104. QLOG_DEBUG() << "Invoking anonymous function";
  105. recvSlot->m_function();
  106. }
  107. else
  108. {
  109. QLOG_DEBUG() << "Invoking slot" << qPrintable(recvSlot->m_slot.data());
  110. QGenericArgument arg0 = QGenericArgument();
  111. if (recvSlot->m_hasArguments)
  112. arg0 = Q_ARG(const QString&, hostArguments);
  113. QMetaObject::invokeMethod(recvSlot->m_receiver, recvSlot->m_slot.data(),
  114. Qt::AutoConnection, arg0);
  115. }
  116. }
  117. }
  118. else
  119. {
  120. QLOG_WARN() << "No such host command:" << hostCommand;
  121. }
  122. }
  123. }
  124. ///////////////////////////////////////////////////////////////////////////////////////////////////
  125. void InputComponent::remapInput(const QString &source, const QString &keycode, InputBase::InputkeyState keyState)
  126. {
  127. QLOG_DEBUG() << "Input received: source:" << source << "keycode:" << keycode << ":" << keyState;
  128. emit receivedInput();
  129. if (keyState == InputBase::KeyUp)
  130. {
  131. m_autoRepeatTimer->stop();
  132. m_autoRepeatActions.clear();
  133. m_autoRepeatCount = 0;
  134. if (!m_currentLongPressAction.isEmpty())
  135. {
  136. QString type;
  137. if (m_longHoldTimer.elapsed() > LONG_HOLD_MSEC)
  138. type = "long";
  139. else
  140. type = "short";
  141. QString action = m_currentLongPressAction.value(type).toString();
  142. m_currentLongPressAction.clear();
  143. QLOG_DEBUG() << "Emit input action (" + type + "):" << action;
  144. emit hostInput(QStringList{action});
  145. }
  146. return;
  147. }
  148. QStringList queuedActions;
  149. m_autoRepeatActions.clear();
  150. auto actions = m_mappings->mapToAction(source, keycode);
  151. for (auto action : actions)
  152. {
  153. if (action.type() == QVariant::String)
  154. {
  155. queuedActions.append(action.toString());
  156. m_autoRepeatActions.append(action.toString());
  157. }
  158. else if (action.type() == QVariant::Map)
  159. {
  160. QVariantMap map = action.toMap();
  161. if (map.contains("long"))
  162. {
  163. // Don't overwrite long actions if there was no key up event yet.
  164. // (It could be a key autorepeated by Qt.)
  165. if (m_currentLongPressAction.isEmpty())
  166. {
  167. m_longHoldTimer.start();
  168. m_currentLongPressAction = map;
  169. }
  170. }
  171. else if (map.contains("short"))
  172. {
  173. queuedActions.append(map.value("short").toString());
  174. }
  175. }
  176. }
  177. if (!m_autoRepeatActions.isEmpty() && keyState != InputBase::KeyPressed)
  178. m_autoRepeatTimer->start(INITAL_AUTOREPEAT_MSEC);
  179. if (!queuedActions.isEmpty())
  180. {
  181. QLOG_DEBUG() << "Emit input action:" << queuedActions;
  182. emit hostInput(queuedActions);
  183. }
  184. }
  185. /////////////////////////////////////////////////////////////////////////////////////////
  186. void InputComponent::executeActions(const QStringList& actions)
  187. {
  188. for (auto action : actions)
  189. handleAction(action);
  190. }
  191. /////////////////////////////////////////////////////////////////////////////////////////
  192. void InputComponent::registerHostCommand(const QString& command, QObject* receiver, const char* slot)
  193. {
  194. auto recvSlot = new ReceiverSlot;
  195. recvSlot->m_receiver = receiver;
  196. recvSlot->m_slot = QMetaObject::normalizedSignature(slot);
  197. recvSlot->m_hasArguments = false;
  198. QLOG_DEBUG() << "Adding host command:" << qPrintable(command) << "mapped to"
  199. << qPrintable(QString(receiver->metaObject()->className()) + "::" + recvSlot->m_slot);
  200. m_hostCommands.insert(command, recvSlot);
  201. auto slotWithArgs = QString("%1(QString)").arg(QString::fromLatin1(recvSlot->m_slot)).toLatin1();
  202. auto slotWithoutArgs = QString("%1()").arg(QString::fromLatin1(recvSlot->m_slot)).toLatin1();
  203. if (recvSlot->m_receiver->metaObject()->indexOfMethod(slotWithArgs.data()) != -1)
  204. {
  205. QLOG_DEBUG() << "Host command maps to method with an argument.";
  206. recvSlot->m_hasArguments = true;
  207. }
  208. else if (recvSlot->m_receiver->metaObject()->indexOfMethod(slotWithoutArgs.data()) != -1)
  209. {
  210. QLOG_DEBUG() << "Host command maps to method without arguments.";
  211. }
  212. else
  213. {
  214. QLOG_ERROR() << "Slot for host command missing, or has incorrect signature!";
  215. }
  216. }
  217. /////////////////////////////////////////////////////////////////////////////////////////
  218. void InputComponent::registerHostCommand(const QString& command, std::function<void(void)> function)
  219. {
  220. auto recvSlot = new ReceiverSlot;
  221. recvSlot->m_function = function;
  222. QLOG_DEBUG() << "Adding host command:" << qPrintable(command) << "mapped to anonymous function";
  223. m_hostCommands.insert(command, recvSlot);
  224. }