InputCEC.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include "QsLog.h"
  2. #include "InputCEC.h"
  3. #include "settings/SettingsComponent.h"
  4. static QMap<int, QString> cecKeyMap { \
  5. { CEC_USER_CONTROL_CODE_SELECT , INPUT_KEY_SELECT } , \
  6. { CEC_USER_CONTROL_CODE_UP , INPUT_KEY_UP } , \
  7. { CEC_USER_CONTROL_CODE_DOWN , INPUT_KEY_DOWN } , \
  8. { CEC_USER_CONTROL_CODE_LEFT , INPUT_KEY_LEFT } , \
  9. { CEC_USER_CONTROL_CODE_RIGHT , INPUT_KEY_RIGHT } , \
  10. { CEC_USER_CONTROL_CODE_SETUP_MENU , INPUT_KEY_MENU } , \
  11. { CEC_USER_CONTROL_CODE_PLAY , INPUT_KEY_PLAY } , \
  12. { CEC_USER_CONTROL_CODE_PAUSE , INPUT_KEY_PAUSE } , \
  13. { CEC_USER_CONTROL_CODE_STOP , INPUT_KEY_STOP } , \
  14. { CEC_USER_CONTROL_CODE_EXIT , INPUT_KEY_BACK } , \
  15. { CEC_USER_CONTROL_CODE_FAST_FORWARD , INPUT_KEY_SEEKFWD } , \
  16. { CEC_USER_CONTROL_CODE_REWIND , INPUT_KEY_SEEKBCK } , \
  17. { CEC_USER_CONTROL_CODE_DISPLAY_INFORMATION , INPUT_KEY_INFO } , \
  18. { CEC_USER_CONTROL_CODE_FORWARD , INPUT_KEY_NEXT } , \
  19. { CEC_USER_CONTROL_CODE_BACKWARD , INPUT_KEY_PREV } , \
  20. { CEC_USER_CONTROL_CODE_F1_BLUE , INPUT_KEY_BLUE } , \
  21. { CEC_USER_CONTROL_CODE_F2_RED , INPUT_KEY_RED } , \
  22. { CEC_USER_CONTROL_CODE_F3_GREEN , INPUT_KEY_GREEN } , \
  23. { CEC_USER_CONTROL_CODE_F4_YELLOW , INPUT_KEY_YELLOW } , \
  24. { CEC_USER_CONTROL_CODE_SUB_PICTURE, INPUT_KEY_SUBTITLES } , \
  25. { CEC_USER_CONTROL_CODE_ROOT_MENU, INPUT_KEY_HOME }, \
  26. { CEC_USER_CONTROL_CODE_NUMBER0, INPUT_KEY_0 } , \
  27. { CEC_USER_CONTROL_CODE_NUMBER1, INPUT_KEY_1 } , \
  28. { CEC_USER_CONTROL_CODE_NUMBER2, INPUT_KEY_2 } , \
  29. { CEC_USER_CONTROL_CODE_NUMBER3, INPUT_KEY_3 } , \
  30. { CEC_USER_CONTROL_CODE_NUMBER4, INPUT_KEY_4 } , \
  31. { CEC_USER_CONTROL_CODE_NUMBER5, INPUT_KEY_5 } , \
  32. { CEC_USER_CONTROL_CODE_NUMBER6, INPUT_KEY_6 } , \
  33. { CEC_USER_CONTROL_CODE_NUMBER7, INPUT_KEY_7 } , \
  34. { CEC_USER_CONTROL_CODE_NUMBER8, INPUT_KEY_8 } , \
  35. { CEC_USER_CONTROL_CODE_NUMBER9, INPUT_KEY_9 } , \
  36. { CEC_USER_CONTROL_CODE_ELECTRONIC_PROGRAM_GUIDE, INPUT_KEY_GUIDE } , \
  37. };
  38. //////////////////////////////////////////////////////////////////////////////////////////////////
  39. InputCEC::InputCEC(QObject *parent) : InputBase(parent)
  40. {
  41. m_configuration.Clear();
  42. m_callbacks.Clear();
  43. m_adapter = NULL;
  44. m_verboseLogging = false;
  45. // setup adapter check timer
  46. m_timer.setInterval(1000);
  47. m_timer.setSingleShot(false);
  48. connect(&m_timer, &QTimer::timeout, this, &InputCEC::checkAdapter);
  49. }
  50. //////////////////////////////////////////////////////////////////////////////////////////////////
  51. bool InputCEC::initInput()
  52. {
  53. m_configuration.Clear();
  54. m_callbacks.Clear();
  55. m_verboseLogging = SettingsComponent::Get().value(SETTINGS_SECTION_CEC, "verbose_logging").toBool();
  56. m_configuration.clientVersion = LIBCEC_VERSION_CURRENT;
  57. strcpy(m_configuration.strDeviceName, "Plex");
  58. m_configuration.bActivateSource = 0;
  59. m_callbacks.CBCecLogMessage = &CecLogMessage;
  60. m_callbacks.CBCecKeyPress = &CecKeyPress;
  61. m_callbacks.CBCecCommand = &CecCommand;
  62. m_callbacks.CBCecAlert = &CecAlert;
  63. m_configuration.callbackParam = this;
  64. m_configuration.callbacks = &m_callbacks;
  65. m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
  66. m_configuration.bAutodetectAddress = CEC_DEFAULT_SETTING_AUTODETECT_ADDRESS;
  67. m_configuration.iPhysicalAddress = CEC_PHYSICAL_ADDRESS_TV;
  68. m_configuration.baseDevice = CECDEVICE_AUDIOSYSTEM;
  69. m_configuration.bActivateSource = 1;
  70. m_configuration.iHDMIPort = (quint8)SettingsComponent::Get().value(SETTINGS_SECTION_CEC, "hdmiport").toInt();
  71. // open libcec
  72. m_adapter = (ICECAdapter*)CECInitialise(&m_configuration);
  73. if (!m_adapter)
  74. {
  75. QLOG_ERROR() << "Unable to initialize libCEC.";
  76. return false;
  77. }
  78. QLOG_INFO() << "libCEC was successfully initialized, found version"
  79. << m_configuration.serverVersion;
  80. // init video on targets that need this
  81. m_adapter->InitVideoStandalone();
  82. // start the adapter check timer
  83. m_timer.start();
  84. return true;
  85. }
  86. ///////////////////////////////////////////////////////////////////////////////////////////////////
  87. InputCEC::~InputCEC()
  88. {
  89. m_timer.stop();
  90. close();
  91. }
  92. //////////////////////////////////////////////////////////////////////////////////////////////////
  93. void InputCEC::close()
  94. {
  95. if (m_adapter)
  96. {
  97. QLOG_DEBUG() << "Closing libCEC.";
  98. closeAdapter();
  99. CECDestroy(m_adapter);
  100. }
  101. }
  102. //////////////////////////////////////////////////////////////////////////////////////////////////
  103. bool InputCEC::openAdapter()
  104. {
  105. bool ret = false;
  106. m_lock.lock();
  107. // try to find devices
  108. cec_adapter devices[10];
  109. int devicesCount = m_adapter->FindAdapters(devices, 10, NULL);
  110. if (devicesCount > 0)
  111. {
  112. // list devices
  113. QLOG_INFO() << "libCEC found" << devicesCount << "CEC adapters.";
  114. // open first adapter
  115. m_adapterPort = devices[0].comm;
  116. if (m_adapter->Open(m_adapterPort.toStdString().c_str()))
  117. {
  118. QLOG_INFO() << "Device " << devices[0].path << "was successfully openned";
  119. ret = true;
  120. }
  121. else
  122. {
  123. QLOG_ERROR() << "Opening device" << devices[0].path << "failed";
  124. ret = false;
  125. }
  126. }
  127. m_lock.unlock();
  128. return ret;
  129. }
  130. //////////////////////////////////////////////////////////////////////////////////////////////////
  131. void InputCEC::closeAdapter()
  132. {
  133. m_lock.lock();
  134. m_adapterPort = "";
  135. m_lock.unlock();
  136. }
  137. //////////////////////////////////////////////////////////////////////////////////////////////////
  138. void InputCEC::reopenAdapter()
  139. {
  140. closeAdapter();
  141. }
  142. ///////////////////////////////////////////////////////////////////////////////////////////////////
  143. void InputCEC::checkAdapter()
  144. {
  145. if (m_adapterPort.isEmpty())
  146. {
  147. if (m_adapter)
  148. m_adapter->Close();
  149. openAdapter();
  150. }
  151. }
  152. ///////////////////////////////////////////////////////////////////////////////////////////////////
  153. void InputCEC::sendReceivedInput(const QString &source, const QString &keycode, float amount)
  154. {
  155. emit receivedInput(source, keycode, amount);
  156. }
  157. ///////////////////////////////////////////////////////////////////////////////////////////////////
  158. QString InputCEC::getCommandString(cec_user_control_code code, unsigned int duration)
  159. {
  160. QString key = cecKeyMap[code];
  161. if (!key.isEmpty() && (duration > CEC_LONGPRESS_DURATION))
  162. {
  163. key += "_LONG";
  164. }
  165. return key;
  166. }
  167. ///////////////////////////////////////////////////////////////////////////////////////////////////
  168. int InputCEC::CecLogMessage(void* cbParam, const cec_log_message message)
  169. {
  170. InputCEC *cec = (InputCEC*)cbParam;
  171. switch (message.level)
  172. {
  173. case CEC_LOG_ERROR:
  174. QLOG_ERROR() << "libCEC ERROR:" << message.message;
  175. break;
  176. case CEC_LOG_WARNING:
  177. QLOG_WARN() << "libCEC WARNING:" << message.message;
  178. break;
  179. case CEC_LOG_NOTICE:
  180. QLOG_INFO() << "libCEC NOTICE:" << message.message;
  181. break;
  182. case CEC_LOG_DEBUG:
  183. if (cec->m_verboseLogging)
  184. {
  185. QLOG_DEBUG() << "libCEC DEBUG:" << message.message;
  186. }
  187. break;
  188. case CEC_LOG_TRAFFIC:
  189. break;
  190. default:
  191. break;
  192. }
  193. return 0;
  194. }
  195. ///////////////////////////////////////////////////////////////////////////////////////////////////
  196. int InputCEC::CecKeyPress(void *cbParam, const cec_keypress key)
  197. {
  198. InputCEC *cec = (InputCEC*)cbParam;
  199. if (cec && key.duration == 0)
  200. {
  201. QString cmdString = cec->getCommandString(key.keycode, key.duration);
  202. if (!cmdString.isEmpty())
  203. cec->sendReceivedInput(CEC_INPUT_NAME, cmdString);
  204. }
  205. return 0;
  206. }
  207. ///////////////////////////////////////////////////////////////////////////////////////////////////
  208. int InputCEC::CecCommand(void *cbParam, const cec_command command)
  209. {
  210. InputCEC *cec = (InputCEC*)cbParam;
  211. switch(command.opcode)
  212. {
  213. case CEC_OPCODE_PLAY:
  214. cec->sendReceivedInput(CEC_INPUT_NAME, INPUT_KEY_PLAY);
  215. break;
  216. case CEC_OPCODE_DECK_CONTROL:
  217. if (command.parameters.size)
  218. {
  219. switch(command.parameters[0])
  220. {
  221. case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
  222. cec->sendReceivedInput(CEC_INPUT_NAME, INPUT_KEY_SEEKFWD);
  223. break;
  224. case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
  225. cec->sendReceivedInput(CEC_INPUT_NAME, INPUT_KEY_SEEKBCK);
  226. break;
  227. case CEC_DECK_CONTROL_MODE_STOP:
  228. cec->sendReceivedInput(CEC_INPUT_NAME, INPUT_KEY_STOP);
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. break;
  235. case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
  236. if (command.parameters.size)
  237. {
  238. switch(command.parameters[0])
  239. {
  240. // samsung Return key
  241. case CEC_USER_CONTROL_CODE_AN_RETURN:
  242. cec->sendReceivedInput(CEC_INPUT_NAME, INPUT_KEY_BACK);
  243. break;
  244. default:
  245. break;
  246. }
  247. }
  248. break;
  249. case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
  250. case CEC_OPCODE_USER_CONTROL_PRESSED:
  251. // ignore those commands as they are handled in CecKeypress
  252. break;
  253. case CEC_OPCODE_GIVE_OSD_NAME:
  254. case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
  255. // ignore those known commands (only pollng from TV)
  256. break;
  257. default:
  258. QLOG_DEBUG() << "Unhandled CEC command " << command.opcode;
  259. if (command.parameters.size)
  260. {
  261. for (int i=0; i<command.parameters.size; i++)
  262. QLOG_DEBUG() << command.parameters.size << QString("parameters -> [%1]= %2").arg(i).arg(command.parameters[i]);
  263. }
  264. break;
  265. }
  266. return 1;
  267. }
  268. ///////////////////////////////////////////////////////////////////////////////////////////////////
  269. int InputCEC::CecAlert(void *cbParam, const libcec_alert type, const libcec_parameter param)
  270. {
  271. bool reopen = false;
  272. switch (type)
  273. {
  274. case CEC_ALERT_SERVICE_DEVICE:
  275. QLOG_ERROR() << "libCEC : Alert CEC_ALERT_SERVICE_DEVICE";
  276. break;
  277. case CEC_ALERT_CONNECTION_LOST:
  278. QLOG_ERROR() << "libCEC : Alert CEC_ALERT_CONNECTION_LOST";
  279. reopen = true;
  280. break;
  281. case CEC_ALERT_PERMISSION_ERROR:
  282. QLOG_ERROR() << "libCEC : Alert CEC_ALERT_PERMISSION_ERROR";
  283. reopen = true;
  284. break;
  285. case CEC_ALERT_PORT_BUSY:
  286. QLOG_ERROR() << "libCEC : Alert CEC_ALERT_PORT_BUSY";
  287. reopen = true;
  288. break;
  289. default:
  290. break;
  291. }
  292. if (reopen)
  293. {
  294. QLOG_DEBUG() << "libCEC : Reopenning adapter";
  295. InputCEC *cec = (InputCEC*)cbParam;
  296. if (cec)
  297. cec->reopenAdapter();
  298. }
  299. return 0;
  300. }