InputLIRC.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <QGuiApplication>
  2. #include <QDebug>
  3. #include "InputLIRC.h"
  4. #define DEFAULT_LIRC_ADDRESS "/run/lirc/lircd"
  5. ///////////////////////////////////////////////////////////////////////////////////////////////////
  6. InputLIRC::InputLIRC(QObject* parent) : InputBase(parent)
  7. {
  8. socket = new QLocalSocket(this);
  9. socketNotifier = NULL;
  10. connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this,
  11. SLOT(socketerror(QLocalSocket::LocalSocketError)));
  12. connect(socket, SIGNAL(connected()), this, SLOT(connected()));
  13. connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
  14. }
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////
  16. InputLIRC::~InputLIRC()
  17. {
  18. if (socket)
  19. disconnect();
  20. }
  21. ///////////////////////////////////////////////////////////////////////////////////////////////////
  22. bool InputLIRC::initInput()
  23. {
  24. return connectToLIRC();
  25. }
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. bool InputLIRC::connectToLIRC()
  28. {
  29. disconnect();
  30. socket->connectToServer(DEFAULT_LIRC_ADDRESS, QIODevice::ReadWrite);
  31. if (isConnected())
  32. {
  33. socketNotifier = new QSocketNotifier(socket->socketDescriptor(), QSocketNotifier::Read, this);
  34. connect(socketNotifier, SIGNAL(activated(int)), this, SLOT(read(int)));
  35. return true;
  36. }
  37. else
  38. return false;
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////////////////////////
  41. void InputLIRC::disconnectFromLIRC()
  42. {
  43. if (socket)
  44. socket->disconnectFromServer();
  45. }
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////
  47. bool InputLIRC::isConnected()
  48. {
  49. while (socket->state() == QLocalSocket::ConnectingState)
  50. {
  51. QGuiApplication::processEvents();
  52. }
  53. return (socket->state() == QLocalSocket::ConnectedState);
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////
  56. void InputLIRC::connected()
  57. {
  58. qInfo() << "LIRC socket connected ";
  59. }
  60. ///////////////////////////////////////////////////////////////////////////////////////////////////
  61. void InputLIRC::disconnected()
  62. {
  63. qInfo() << "LIRC socket disconnected ";
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////
  66. void InputLIRC::socketerror(QLocalSocket::LocalSocketError socketError)
  67. {
  68. qCritical() << "LIRC Socket Error : " << socketError;
  69. }
  70. ///////////////////////////////////////////////////////////////////////////////////////////////////
  71. void InputLIRC::read(int handle)
  72. {
  73. QString input;
  74. while ((input = socket->readLine()) != "")
  75. {
  76. QStringList cmdList = input.split(' ', QString::KeepEmptyParts);
  77. if (cmdList.size() == 4)
  78. {
  79. // grab the input data
  80. QString code = cmdList.at(0);
  81. QString repeat = cmdList.at(1);
  82. QString command = cmdList.at(2);
  83. QString remote = cmdList.at(3);
  84. int repeatCount = repeat.toInt();
  85. qInfo() << "LIRC Got Key : " << command << ", repeat count:" << repeatCount
  86. << ", from remote " << remote;
  87. // we dont want to have all the IR Bursts when we press a key
  88. // it makes GUI unusable
  89. if ((repeatCount % 3) == 0)
  90. {
  91. bool up = command.endsWith("_LIRCUP");
  92. emit receivedInput("LIRC", command, up ? InputBase::KeyUp : InputBase::KeyDown);
  93. }
  94. }
  95. else
  96. qCritical() << "Unknown LIRC input: " << input;
  97. }
  98. }