MythTV  master
mythinputdevicehandler.cpp
Go to the documentation of this file.
1 // Qt
2 #include <QCoreApplication>
3 #include <QKeyEvent>
4 #include <QDir>
5 
6 // MythTV
7 #include "libmythbase/mythconfig.h"
8 #include "libmythbase/mythdb.h"
9 #include "libmythbase/mythdirs.h"
11 
12 #include "mythmainwindow.h"
13 #include "mythinputdevicehandler.h"
14 
15 #ifdef USE_JOYSTICK_MENU
16 #include "devices/jsmenu.h"
17 #include "devices/jsmenuevent.h"
18 #endif
19 
20 #ifdef USING_APPLEREMOTE
22 #endif
23 
24 #ifdef USE_LIRC
25 #include "devices/lirc.h"
26 #endif
27 
28 #if defined (USE_LIRC) || defined (USING_APPLEREMOTE)
29 #include "devices/lircevent.h"
30 #endif
31 
32 #define LOC QString("InputHandler: ")
33 
42  : m_parent(Parent)
43 {
45 }
46 
48 {
50 }
51 
53 {
54  LOG(VB_GENERAL, LOG_INFO, LOC + "Starting");
55 
56 #ifdef USE_LIRC
57  if (!m_lircThread)
58  {
59  QString config = GetConfDir() + "/lircrc";
60  if (!QFile::exists(config))
61  config = QDir::homePath() + "/.lircrc";
62 
63  // lircd socket moved from /dev/ to /var/run/lirc/ in lirc 0.8.6
64  QString socket = "/dev/lircd";
65  if (!QFile::exists(socket))
66  socket = "/var/run/lirc/lircd";
67 
68  m_lircThread = new LIRC(this, GetMythDB()->GetSetting("LircSocket", socket), "mythtv", config);
69  if (m_lircThread->Init())
70  {
72  }
73  else
74  {
76  m_lircThread = nullptr;
77  }
78  }
79 #endif
80 
81 #ifdef USE_JOYSTICK_MENU
82  if (!m_joystickThread)
83  {
84  QString config = GetConfDir() + "/joystickmenurc";
86  if (m_joystickThread->Init(config))
88  }
89 #endif
90 
91 #ifdef USING_APPLEREMOTE
93  {
96 
100  {
101  m_appleRemote->start();
102  }
103  else
104  {
105  // start listening failed, no remote receiver present
106  delete m_appleRemote;
107  delete m_appleRemoteListener;
108  m_appleRemote = nullptr;
109  m_appleRemoteListener = nullptr;
110  }
111  }
112 #endif
113 }
114 
115 void MythInputDeviceHandler::Stop([[maybe_unused]] bool Finishing /* = true */)
116 {
117  LOG(VB_GENERAL, LOG_INFO, LOC + "Stopping");
118 
119 #ifdef USING_LIBCEC
120  if (Finishing)
121  m_cecAdapter.Close();
122 #endif
123 
124 #ifdef USING_APPLEREMOTE
125  if (Finishing)
126  {
127  delete m_appleRemote;
128  delete m_appleRemoteListener;
129  m_appleRemote = nullptr;
130  m_appleRemoteListener = nullptr;
131  }
132 #endif
133 
134 #ifdef USE_JOYSTICK_MENU
135  if (m_joystickThread && Finishing)
136  {
138  {
141  }
142  delete m_joystickThread;
143  m_joystickThread = nullptr;
144  }
145 #endif
146 
147 #ifdef USE_LIRC
148  if (m_lircThread)
149  {
151  m_lircThread = nullptr;
152  }
153 #endif
154 }
155 
157 {
158  Stop(false);
159  Start();
160 }
161 
162 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
164 {
165  if (!Event)
166  return;
167 
168 #ifdef USING_APPLEREMOTE
169  if (m_appleRemote)
170  {
171  if (Event->type() == QEvent::WindowActivate)
173  if (Event->type() == QEvent::WindowDeactivate)
175  }
176 #endif
177 }
178 
179 void MythInputDeviceHandler::Action([[maybe_unused]] const QString &Action)
180 {
181 #ifdef USING_LIBCEC
182  m_cecAdapter.Action(Action);
183 #endif
184 }
185 
187 {
188  if (Ignore)
189  LOG(VB_GENERAL, LOG_INFO, LOC + "Locking input devices");
190  else
191  LOG(VB_GENERAL, LOG_INFO, LOC + "Unlocking input devices");
192  m_ignoreKeys = Ignore;
193 #ifdef USING_LIBCEC
194  m_cecAdapter.IgnoreKeys(Ignore);
195 #endif
196 }
197 
199 {
200 #ifdef USING_LIBCEC
201  // Open any adapter after the window has been created to ensure we capture
202  // the EDID if available - and hence get a more accurate Physical Address.
203  // This will close any existing adapter in the event that the window has been re-init'ed.
204  m_cecAdapter.Open(m_parent);
205 #endif
206 }
207 
208 void MythInputDeviceHandler::customEvent([[maybe_unused]] QEvent* Event)
209 {
210  if (m_ignoreKeys)
211  return;
212 
213  QScopedPointer<QKeyEvent> key { new QKeyEvent(QEvent::KeyPress, 0, Qt::NoModifier) };
214  QObject* target = nullptr;
215  QString error;
216 
217 #ifdef USE_JOYSTICK_MENU
219  {
220  auto *jke = dynamic_cast<JoystickKeycodeEvent *>(Event);
221  if (!jke)
222  return;
223 
224  int keycode = jke->key();
225  if (keycode)
226  {
227  key.reset(new QKeyEvent(jke->keyAction(), keycode, jke->keyModifiers()));
228  target = m_parent->GetTarget(*key);
229  }
230  else
231  {
232  error = jke->getJoystickMenuText();
233  }
234  }
235 #endif
236 
237 #if defined(USE_LIRC) || defined(USING_APPLEREMOTE)
238  if (Event->type() == LircKeycodeEvent::kEventType)
239  {
240  auto *lke = dynamic_cast<LircKeycodeEvent *>(Event);
241  if (!lke)
242  return;
243 
244  if (LircKeycodeEvent::kLIRCInvalidKeyCombo == lke->modifiers())
245  {
246  error = lke->lirctext();
247  }
248  else
249  {
250  key.reset(new QKeyEvent(lke->keytype(), lke->key(), lke->modifiers(), lke->text()));
251  target = m_parent->GetTarget(*key);
252  }
253  }
254 #endif
255 
256  if (!error.isEmpty())
257  {
258  LOG(VB_GENERAL, LOG_WARNING, LOC +
259  QString("Attempt to convert key sequence '%1' to a Qt key sequence failed.").arg(error));
260  }
261  else if (target)
262  {
265  return;
266  QCoreApplication::sendEvent(target, key.data());
267  }
268 }
AppleRemote::Get
static AppleRemote * Get()
Definition: AppleRemote.cpp:41
AppleRemoteListener
Definition: AppleRemoteListener.h:6
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:283
LOC
#define LOC
Definition: mythinputdevicehandler.cpp:32
MythInputDeviceHandler::MainWindowReady
void MainWindowReady(void)
Definition: mythinputdevicehandler.cpp:198
MythInputDeviceHandler::customEvent
void customEvent(QEvent *Event) override
Definition: mythinputdevicehandler.cpp:208
jsmenu.h
error
static void error(const char *str,...)
Definition: vbi.cpp:36
mythdb.h
MythInputDeviceHandler::MythInputDeviceHandler
MythInputDeviceHandler(MythMainWindow *Parent)
Definition: mythinputdevicehandler.cpp:41
MThread::wait
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:300
jsmenuevent.h
MythInputDeviceHandler::Action
void Action(const QString &Action)
Definition: mythinputdevicehandler.cpp:179
MythInputDeviceHandler::Start
void Start(void)
Definition: mythinputdevicehandler.cpp:52
JoystickKeycodeEvent
Definition: jsmenuevent.h:16
LircKeycodeEvent::kEventType
static const Type kEventType
Definition: lircevent.h:27
MythInputDeviceHandler::m_ignoreKeys
bool m_ignoreKeys
Definition: mythinputdevicehandler.h:41
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
mythinputdevicehandler.h
GetMythDB
MythDB * GetMythDB(void)
Definition: mythdb.cpp:50
mythdirs.h
MythInputDeviceHandler::Stop
void Stop(bool Finishing=true)
Definition: mythinputdevicehandler.cpp:115
Action
An action (for this plugin) consists of a description, and a set of key sequences.
Definition: action.h:40
JoystickKeycodeEvent::key
int key() const
Definition: jsmenuevent.h:28
mythlogging.h
GetConfDir
QString GetConfDir(void)
Definition: mythdirs.cpp:256
Event
Event details.
Definition: zmdefines.h:26
JoystickKeycodeEvent::kEventType
static const Type kEventType
Definition: jsmenuevent.h:32
LIRC::deleteLater
virtual void deleteLater(void)
Definition: lirc.cpp:101
AppleRemoteListener.h
MythInputDeviceHandler::m_appleRemoteListener
AppleRemoteListener * m_appleRemoteListener
Definition: mythinputdevicehandler.h:56
MythInputDeviceHandler::Reset
void Reset(void)
Definition: mythinputdevicehandler.cpp:156
LircKeycodeEvent::kLIRCInvalidKeyCombo
static const unsigned kLIRCInvalidKeyCombo
Definition: lircevent.h:29
JoystickMenuThread::Stop
void Stop(void)
Definition: jsmenu.h:99
MythInputDeviceHandler::m_joystickThread
JoystickMenuThread * m_joystickThread
Definition: mythinputdevicehandler.h:52
MythInputDeviceHandler::m_appleRemote
AppleRemote * m_appleRemote
Definition: mythinputdevicehandler.h:57
MythInputDeviceHandler::~MythInputDeviceHandler
~MythInputDeviceHandler() override
Definition: mythinputdevicehandler.cpp:47
AppleRemote::startListening
void startListening()
Definition: AppleRemote.cpp:73
LircKeycodeEvent
Definition: lircevent.h:12
MythMainWindow::GetTarget
QObject * GetTarget(QKeyEvent &Key)
Definition: mythmainwindow.cpp:2082
MythMainWindow::IsScreensaverAsleep
static bool IsScreensaverAsleep()
Definition: mythmainwindow.cpp:595
lirc.h
MThread::isRunning
bool isRunning(void) const
Definition: mthread.cpp:263
LIRC::Init
bool Init(void)
Definition: lirc.cpp:166
AppleRemote::stopListening
void stopListening()
Definition: AppleRemote.cpp:95
JoystickMenuThread::Init
bool Init(QString &config_file)
Initialise the class variables with values from the config file.
Definition: jsmenu.cpp:77
JoystickMenuThread
Main object for injecting key strokes based on joystick movements.
Definition: jsmenu.h:87
MythMainWindow::ResetScreensaver
static void ResetScreensaver()
Definition: mythmainwindow.cpp:589
MythInputDeviceHandler::Event
void Event(QEvent *Event) const
Definition: mythinputdevicehandler.cpp:163
AppleRemote::isListeningToRemote
bool isListeningToRemote()
Definition: AppleRemote.cpp:63
LIRC
Interface between mythtv and lircd.
Definition: lirc.h:25
mythmainwindow.h
MythInputDeviceHandler::m_lircThread
LIRC * m_lircThread
Definition: mythinputdevicehandler.h:44
AppleRemote::setListener
void setListener(Listener *listener)
Definition: AppleRemote.cpp:68
MythMainWindow
Definition: mythmainwindow.h:28
lircevent.h
LIRC::start
virtual void start(void)
Definition: lirc.cpp:325
MythInputDeviceHandler::IgnoreKeys
void IgnoreKeys(bool Ignore)
Definition: mythinputdevicehandler.cpp:186
MythInputDeviceHandler::m_parent
MythMainWindow * m_parent
Definition: mythinputdevicehandler.h:40