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 "mythconfig.h"
8 #include "mythlogging.h"
9 #include "mythdb.h"
10 #include "mythdirs.h"
11 #include "mythmainwindow.h"
12 #include "mythinputdevicehandler.h"
13 
14 #ifdef USE_JOYSTICK_MENU
15 #include "devices/jsmenu.h"
16 #include "devices/jsmenuevent.h"
17 #endif
18 
19 #ifdef USING_APPLEREMOTE
21 #endif
22 
23 #ifdef USE_LIRC
24 #include "devices/lirc.h"
25 #endif
26 
27 #if defined (USE_LIRC) || defined (USING_APPLEREMOTE)
28 #include "devices/lircevent.h"
29 #endif
30 
31 #define LOC QString("InputHandler: ")
32 
41  : m_parent(Parent)
42 {
44 }
45 
47 {
49 }
50 
52 {
53  LOG(VB_GENERAL, LOG_INFO, LOC + "Starting");
54 
55 #ifdef USE_LIRC
56  if (!m_lircThread)
57  {
58  QString config = GetConfDir() + "/lircrc";
59  if (!QFile::exists(config))
60  config = QDir::homePath() + "/.lircrc";
61 
62  // lircd socket moved from /dev/ to /var/run/lirc/ in lirc 0.8.6
63  QString socket = "/dev/lircd";
64  if (!QFile::exists(socket))
65  socket = "/var/run/lirc/lircd";
66 
67  m_lircThread = new LIRC(this, GetMythDB()->GetSetting("LircSocket", socket), "mythtv", config);
68  if (m_lircThread->Init())
69  {
71  }
72  else
73  {
75  m_lircThread = nullptr;
76  }
77  }
78 #endif
79 
80 #ifdef USE_JOYSTICK_MENU
81  if (!m_joystickThread)
82  {
83  QString config = GetConfDir() + "/joystickmenurc";
85  if (m_joystickThread->Init(config))
87  }
88 #endif
89 
90 #ifdef USING_APPLEREMOTE
92  {
95 
99  {
100  m_appleRemote->start();
101  }
102  else
103  {
104  // start listening failed, no remote receiver present
105  delete m_appleRemote;
106  delete m_appleRemoteListener;
107  m_appleRemote = nullptr;
108  m_appleRemoteListener = nullptr;
109  }
110  }
111 #endif
112 }
113 
114 void MythInputDeviceHandler::Stop(bool Finishing /* = true */)
115 {
116  Q_UNUSED(Finishing) // depending on #ifdefs
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 
180 {
181 #ifdef USING_LIBCEC
182  m_cecAdapter.Action(Action);
183 #else
184  (void) Action;
185 #endif
186 }
187 
189 {
190  if (Ignore)
191  LOG(VB_GENERAL, LOG_INFO, LOC + "Locking input devices");
192  else
193  LOG(VB_GENERAL, LOG_INFO, LOC + "Unlocking input devices");
194  m_ignoreKeys = Ignore;
195 #ifdef USING_LIBCEC
196  m_cecAdapter.IgnoreKeys(Ignore);
197 #endif
198 }
199 
201 {
202 #ifdef USING_LIBCEC
203  // Open any adapter after the window has been created to ensure we capture
204  // the EDID if available - and hence get a more accurate Physical Address.
205  // This will close any existing adapter in the event that the window has been re-init'ed.
206  m_cecAdapter.Open(m_parent);
207 #endif
208 }
209 
211 {
212  Q_UNUSED(Event) // depending on #ifdefs
213 
214  if (m_ignoreKeys)
215  return;
216 
217  QScopedPointer<QKeyEvent> key { new QKeyEvent(QEvent::KeyPress, 0, Qt::NoModifier) };
218  QObject* target = nullptr;
219  QString error;
220 
221 #ifdef USE_JOYSTICK_MENU
223  {
224  auto *jke = dynamic_cast<JoystickKeycodeEvent *>(Event);
225  if (!jke)
226  return;
227 
228  int keycode = jke->key();
229  if (keycode)
230  {
231  key.reset(new QKeyEvent(jke->keyAction(), keycode, jke->keyModifiers()));
232  target = m_parent->GetTarget(*key);
233  }
234  else
235  {
236  error = jke->getJoystickMenuText();
237  }
238  }
239 #endif
240 
241 #if defined(USE_LIRC) || defined(USING_APPLEREMOTE)
242  if (Event->type() == LircKeycodeEvent::kEventType)
243  {
244  auto *lke = dynamic_cast<LircKeycodeEvent *>(Event);
245  if (!lke)
246  return;
247 
248  if (LircKeycodeEvent::kLIRCInvalidKeyCombo == lke->modifiers())
249  {
250  error = lke->lirctext();
251  }
252  else
253  {
254  key.reset(new QKeyEvent(lke->keytype(), lke->key(), lke->modifiers(), lke->text()));
255  target = m_parent->GetTarget(*key);
256  }
257  }
258 #endif
259 
260  if (!error.isEmpty())
261  {
262  LOG(VB_GENERAL, LOG_WARNING, LOC +
263  QString("Attempt to convert key sequence '%1' to a Qt key sequence failed.").arg(error));
264  }
265  else if (target)
266  {
269  return;
270  QCoreApplication::sendEvent(target, key.data());
271  }
272 }
AppleRemote::Get
static AppleRemote * Get()
Definition: AppleRemote.cpp:36
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:286
LOC
#define LOC
Definition: mythinputdevicehandler.cpp:31
MythInputDeviceHandler::MainWindowReady
void MainWindowReady(void)
Definition: mythinputdevicehandler.cpp:200
MythInputDeviceHandler::customEvent
void customEvent(QEvent *Event) override
Definition: mythinputdevicehandler.cpp:210
jsmenu.h
error
static void error(const char *str,...)
Definition: vbi.cpp:42
mythdb.h
MythInputDeviceHandler::MythInputDeviceHandler
MythInputDeviceHandler(MythMainWindow *Parent)
Definition: mythinputdevicehandler.cpp:40
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:303
jsmenuevent.h
MythInputDeviceHandler::Action
void Action(const QString &Action)
Definition: mythinputdevicehandler.cpp:179
MythInputDeviceHandler::Start
void Start(void)
Definition: mythinputdevicehandler.cpp:51
JoystickKeycodeEvent
Definition: jsmenuevent.h:16
MythInputDeviceHandler::m_ignoreKeys
bool m_ignoreKeys
Definition: mythinputdevicehandler.h:41
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
mythinputdevicehandler.h
GetMythDB
MythDB * GetMythDB(void)
Definition: mythdb.cpp:46
mythdirs.h
MythInputDeviceHandler::Stop
void Stop(bool Finishing=true)
Definition: mythinputdevicehandler.cpp:114
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:224
Event
Event details.
Definition: zmdefines.h:26
LIRC::deleteLater
virtual void deleteLater(void)
Definition: lirc.cpp:92
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:94
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:46
AppleRemote::startListening
void startListening()
Definition: AppleRemote.cpp:68
LircKeycodeEvent
Definition: lircevent.h:12
MythMainWindow::GetTarget
QObject * GetTarget(QKeyEvent &Key)
Definition: mythmainwindow.cpp:2055
MythMainWindow::IsScreensaverAsleep
static bool IsScreensaverAsleep()
Definition: mythmainwindow.cpp:594
lirc.h
JoystickKeycodeEvent::kEventType
static Type kEventType
Definition: jsmenuevent.h:32
MThread::isRunning
bool isRunning(void) const
Definition: mthread.cpp:266
LIRC::Init
bool Init(void)
Definition: lirc.cpp:157
LircKeycodeEvent::kEventType
static Type kEventType
Definition: lircevent.h:27
AppleRemote::stopListening
void stopListening()
Definition: AppleRemote.cpp:90
JoystickMenuThread::Init
bool Init(QString &config_file)
Initialise the class variables with values from the config file.
Definition: jsmenu.cpp:72
JoystickMenuThread
Main object for injecting key strokes based on joystick movements.
Definition: jsmenu.h:82
MythMainWindow::ResetScreensaver
static void ResetScreensaver()
Definition: mythmainwindow.cpp:588
MythInputDeviceHandler::Event
void Event(QEvent *Event) const
Definition: mythinputdevicehandler.cpp:163
AppleRemote::isListeningToRemote
bool isListeningToRemote()
Definition: AppleRemote.cpp:58
LIRC
Interface between mythtv and lircd.
Definition: lirc.h:27
mythmainwindow.h
MythInputDeviceHandler::m_lircThread
LIRC * m_lircThread
Definition: mythinputdevicehandler.h:44
AppleRemote::setListener
void setListener(Listener *listener)
Definition: AppleRemote.cpp:63
MythMainWindow
Definition: mythmainwindow.h:35
lircevent.h
LIRC::start
virtual void start(void)
Definition: lirc.cpp:315
MythInputDeviceHandler::IgnoreKeys
void IgnoreKeys(bool Ignore)
Definition: mythinputdevicehandler.cpp:188
MythInputDeviceHandler::m_parent
MythMainWindow * m_parent
Definition: mythinputdevicehandler.h:40