/
mythsystemevent.cpp
433 lines (372 loc) · 15.7 KB
/
mythsystemevent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <QApplication>
#include <QFileInfo>
#include <QRunnable>
#include <QThreadPool>
#include "mythcorecontext.h"
#include "mythsystem.h"
#include "mythsystemevent.h"
#include "mythverbose.h"
#include "programinfo.h"
#include "remoteutil.h"
#include "exitcodes.h"
#define LOC QString("MythSystemEventHandler: ")
#define LOC_ERR QString("MythSystemEventHandler ERROR: ")
#define LOC_WARN QString("MythSystemEventHandler WARNING: ")
/** \class SystemEventThread
* \brief QRunnable class for running MythSystemEvent handler commands
*
* The SystemEventThread class runs a system event handler command in
* non-blocking mode. The commands are run in the QThreadPool::globalInstance,
* but we release and reserve the thread inside ::run() so that long-running
* commands to not block other short-running commands from executing if we
* hit QThreadPool::maxThreadCount().
*/
class SystemEventThread : public QRunnable
{
public:
/** \fn SystemEventThread::SystemEventThread(const QString cmd,
QString eventName)
* \brief Constructor for creating a SystemEventThread
* \param cmd Command line to run for this System Event
* \param eventName Optional System Event name for this command
*/
SystemEventThread(const QString cmd, QString eventName = "")
: m_command(cmd), m_event(eventName) {};
/** \fn SystemEventThread::run()
* \brief Runs the System Event handler command
*
* Overrides QRunnable::run()
*/
void run()
{
uint flags = kMSDontBlockInputDevs;
m_event.detach();
m_command.detach();
QThreadPool::globalInstance()->releaseThread();
uint result = myth_system(m_command, flags);
QThreadPool::globalInstance()->reserveThread();
if (result != GENERIC_EXIT_OK)
VERBOSE(VB_IMPORTANT, LOC_WARN + QString("Command '%1' returned %2")
.arg(m_command).arg(result));
if (m_event.isEmpty())
return;
RemoteSendMessage(QString("SYSTEM_EVENT_RESULT %1 SENDER %2 RESULT %3")
.arg(m_event).arg(gCoreContext->GetHostName())
.arg(result));
}
private:
// Private data storage
QString m_command;
QString m_event;
};
/** \fn MythSystemEventHandler::MythSystemEventHandler(void)
* \brief Null Constructor
*
* Adds this object as a gContext event listener.
*/
MythSystemEventHandler::MythSystemEventHandler(void)
{
gCoreContext->addListener(this);
}
/** \fn MythSystemEventHandler::~MythSystemEventHandler()
* \brief Destructor
*
* Removes this object as a gContext event listener.
*/
MythSystemEventHandler::~MythSystemEventHandler()
{
gCoreContext->removeListener(this);
}
/** \fn MythSystemEventHandler::SubstituteMatches(const QStringList &tokens,
QString &command)
* \brief Substitutes %MATCH% variables in given command line.
* \sa ProgramInfo::SubstituteMatches(QString &str)
*
* Subsitutes values for %MATCH% type variables in given command string.
* Some of these matches come from the tokens list passed in and some
* may come from a ProgramInfo if a chanid and starttime are specified
* in the tokens list.
*
* \param tokens Const QStringList containing token list passed with event.
* \param command Command line containing %MATCH% variables to be substituted.
*/
void MythSystemEventHandler::SubstituteMatches(const QStringList &tokens,
QString &command)
{
if (command.isEmpty())
return;
VERBOSE(VB_FILE+VB_EXTRA, LOC + QString("SubstituteMatches: BEFORE: %1")
.arg(command));
QString args;
uint chanid = 0;
QDateTime recstartts;
QString sender;
QStringList::const_iterator it = tokens.begin();
++it;
command.replace(QString("%EVENTNAME%"), *it);
++it;
while (it != tokens.end())
{
if (!args.isEmpty())
args += " ";
args += *it;
// Check for some token names that we substitute one for one as
// %MATCH% type variables.
if ((*it == "CARDID") ||
(*it == "HOSTNAME") ||
(*it == "SECS") ||
(*it == "SENDER") ||
(*it == "PATH"))
{
QString token = *it;
if (++it == tokens.end())
break;
if (token == "SENDER")
sender = *it;
// The following string is broken up on purpose to indicate
// what we're replacing is the token surrounded by percent signs
command.replace(QString("%" "%1" "%").arg(token), *it);
if (!args.isEmpty())
args += " ";
args += *it;
}
// Remember any chanid and starttime so we can lookup info about
// the recording from the database.
if (*it == "CHANID")
{
if (++it == tokens.end())
break;
chanid = (*it).toUInt();
if (!args.isEmpty())
args += " ";
args += *it;
}
if (*it == "STARTTIME")
{
if (++it == tokens.end())
break;
recstartts = QDateTime::fromString(*it, Qt::ISODate);
if (!args.isEmpty())
args += " ";
args += *it;
}
++it;
}
command.replace(QString("%ARGS%"), args);
ProgramInfo pginfo(chanid, recstartts);
bool pginfo_loaded = pginfo.GetChanID();
if (!pginfo_loaded)
{
RecordingInfo::LoadStatus status;
pginfo = RecordingInfo(chanid, recstartts, false, 0, &status);
pginfo_loaded = RecordingInfo::kFoundProgram == status;
}
if (pginfo_loaded)
{
pginfo.SubstituteMatches(command);
}
else
{
command.replace(QString("%CHANID%"), QString::number(chanid));
command.replace(QString("%STARTTIME%"),
recstartts.toString("yyyyMMddhhmmss"));
command.replace(QString("%STARTTIMEISO%"),
recstartts.toString(Qt::ISODate));
}
command.replace(QString("%VERBOSELEVEL%"),
QString("%1").arg(print_verbose_messages));
VERBOSE(VB_FILE+VB_EXTRA, LOC + QString("SubstituteMatches: AFTER : %1")
.arg(command));
}
/** \fn MythSystemEventHandler::EventNameToSetting(const QString &name)
* \brief Convert an MythSystemEvent name to a database setting name
*
* Converts an underscored, all-capital-letters system event name of the form
* NET_CTRL_CONNECTED to the corresponding CamelCase database setting
* name EventCmdNetCtrlConnected.
*
* \param name Const QString containing System Event name to convert
*/
QString MythSystemEventHandler::EventNameToSetting(const QString &name)
{
QString result("EventCmd");
QStringList parts = name.toLower().split('_', QString::SkipEmptyParts);
QStringList::Iterator it = parts.begin();
while (it != parts.end())
{
result += (*it).left(1).toUpper();
result += (*it).mid(1);
++it;
}
return result;
}
/** \fn MythSystemEventHandler::customEvent(QEvent *e)
* \brief Custom Event handler for receiving and processing System Events
* \sa MythSystemEventHandler::SubstituteMatches(const QStringList &tokens,
QString &command)
* MythSystemEventHandler::EventNameToSetting(const QString &name)
*
* This function listens for SYSTEM_EVENT messages and fires off any
* necessary event handler commands. In addition to SYSTEM_EVENT messages,
* this code also forwards GLOBAL_SYSTEM_EVENT which may have been sent
* by the local system via code that does not have access to the master
* backend connection to send events on its own. One example is the
* code that sends KEY_xx system events.
*
* \param e Pointer to QEvent containing event to handle
*/
void MythSystemEventHandler::customEvent(QEvent *e)
{
if ((MythEvent::Type)(e->type()) == MythEvent::MythEventMessage)
{
MythEvent *me = (MythEvent *)e;
QString msg = me->Message().simplified();
if (msg == "CLEAR_SETTINGS_CACHE")
msg = "SYSTEM_EVENT SETTINGS_CACHE_CLEARED";
// Listen for any GLOBAL_SYSTEM_EVENT messages and resend to
// the master backend as regular SYSTEM_EVENT messages.
if (msg.startsWith("GLOBAL_SYSTEM_EVENT "))
{
RemoteSendMessage(msg.mid(7) + QString(" SENDER %1")
.arg(gCoreContext->GetHostName()));
return;
}
if ((!msg.startsWith("SYSTEM_EVENT ")) &&
(!msg.startsWith("LOCAL_SYSTEM_EVENT ")))
return;
QStringList tokens = msg.split(' ', QString::SkipEmptyParts);
// Return if this event is for another host
if ((tokens.size() >= 4) &&
(tokens[2] == "HOST") &&
(tokens[3] != gCoreContext->GetHostName()))
return;
QString cmd;
// See if this system has a command that runs for all system events
cmd = gCoreContext->GetSetting("EventCmdAll");
if (!cmd.isEmpty())
{
SubstituteMatches(tokens, cmd);
SystemEventThread *eventThread = new SystemEventThread(cmd);
QThreadPool::globalInstance()->start(eventThread);
}
// Check for an EventCmd for this particular event
cmd = gCoreContext->GetSetting(EventNameToSetting(tokens[1]));
if (!cmd.isEmpty())
{
SubstituteMatches(tokens, cmd);
SystemEventThread *eventThread =
new SystemEventThread(cmd, tokens[1]);
QThreadPool::globalInstance()->start(eventThread);
}
}
}
/****************************************************************************/
/** \fn SendMythSystemEvent(const QString msg)
* \brief Send the given message as a System Event
* \param msg Const QString to send as a System Event
*/
void SendMythSystemEvent(const QString msg)
{
RemoteSendMessage(QString("SYSTEM_EVENT %1 SENDER %2")
.arg(msg).arg(gCoreContext->GetHostName()));
}
/** \fn SendMythSystemRecEvent(const QString msg, const RecordingInfo *pginfo)
* \brief Sends a System Event for an in-progress recording
* \sa SendMythSystemEvent(const QString msg)
* \param msg Const QString to send as a System Event
* \param pginfo Pointer to a RecordingInfo containing info on a recording
*/
void SendMythSystemRecEvent(const QString msg, const RecordingInfo *pginfo)
{
if (pginfo)
SendMythSystemEvent(QString("%1 CARDID %2 CHANID %3 STARTTIME %4")
.arg(msg).arg(pginfo->GetCardID())
.arg(pginfo->GetChanID())
.arg(pginfo->GetRecordingStartTime(ISODate)));
else
VERBOSE(VB_IMPORTANT, LOC_ERR + "SendMythSystemRecEvent() called with "
"empty RecordingInfo");
}
/** \fn SendMythSystemPlayEvent(const QString msg, const ProgramInfo *pginfo)
* \brief Sends a System Event for a previously recorded program
* \sa SendMythSystemEvent(const QString msg)
* \param msg Const QString to send as a System Event
* \param pginfo Pointer to a RecordingInfo containing info on a recording
*/
void SendMythSystemPlayEvent(const QString msg, const ProgramInfo *pginfo)
{
if (pginfo)
SendMythSystemEvent(
QString("%1 HOSTNAME %2 CHANID %3 STARTTIME %4")
.arg(msg).arg(gCoreContext->GetHostName())
.arg(pginfo->GetChanID())
.arg(pginfo->GetRecordingStartTime(ISODate)));
else
VERBOSE(VB_IMPORTANT, LOC_ERR + "SendMythSystemPlayEvent() called with "
"empty ProgramInfo");
}
/** \fn SendMythSystemHostEvent(const QString msg, const QString &hostname,
const QString args)
* \brief Sends a System Event to a specific host
* \param msg Const QString to send as a System Event
* \param hostname Const QString to send the event to
* \param args Const QString containing other info to send in the event
*/
void SendMythSystemHostEvent(const QString msg, const QString &hostname,
const QString args)
{
SendMythSystemEvent(QString("%1 HOST %2 %3")
.arg(msg).arg(hostname).arg(args));
}
/****************************************************************************/
/** \fn MythSystemEventEditor::MythSystemEventEditor(MythScreenStack *parent,
const char *name)
* \brief Constructor for the MythSystemEvent settings editor
*
* Populates the settings name list with the System Event settings names
* and sets the title of the RawSettingsEditor screen to indicate that
* we are editting the System Event Commands.
*
* \param parent Parent screen stack for this window
* \param name Name of this window
*/
MythSystemEventEditor::MythSystemEventEditor(MythScreenStack *parent,
const char *name)
: RawSettingsEditor(parent, name)
{
m_title = tr("System Event Command Editor");
m_settings["EventCmdRecPending"] = tr("Recording pending");
m_settings["EventCmdRecStarted"] = tr("Recording started");
m_settings["EventCmdRecFinished"] = tr("Recording finished");
m_settings["EventCmdRecDeleted"] = tr("Recording deleted");
m_settings["EventCmdRecExpired"] = tr("Recording expired");
m_settings["EventCmdLivetvStarted"] = tr("LiveTV started");
m_settings["EventCmdPlayStarted"] = tr("Playback started");
m_settings["EventCmdPlayStopped"] = tr("Playback stopped");
m_settings["EventCmdPlayPaused"] = tr("Playback paused");
m_settings["EventCmdPlayUnpaused"] = tr("Playback unpaused");
m_settings["EventCmdPlayChanged"] = tr("Playback program changed");
m_settings["EventCmdMasterStarted"] = tr("Master backend started");
m_settings["EventCmdMasterShutdown"] = tr("Master backend shutdown");
m_settings["EventCmdClientConnected"] = tr("Client connected to master backend");
m_settings["EventCmdClientDisconnected"] = tr("Client disconnected from master backend");
m_settings["EventCmdSlaveConnected"] = tr("Slave backend connected to master");
m_settings["EventCmdSlaveDisconnected"] = tr("Slave backend disconnected from master");
m_settings["EventCmdNetCtrlConnected"] = tr("Network Control client connected");
m_settings["EventCmdNetCtrlDisconnected"] = tr("Network Control client disconnected");
m_settings["EventCmdMythfilldatabaseRan"] = tr("mythfilldatabase ran");
m_settings["EventCmdSchedulerRan"] = tr("Scheduler ran");
m_settings["EventCmdSettingsCacheCleared"] = tr("Settings cache cleared");
m_settings["EventCmdKey01"] = tr("Keystroke event #1");
m_settings["EventCmdKey02"] = tr("Keystroke event #2");
m_settings["EventCmdKey03"] = tr("Keystroke event #3");
m_settings["EventCmdKey04"] = tr("Keystroke event #4");
m_settings["EventCmdKey05"] = tr("Keystroke event #5");
m_settings["EventCmdKey06"] = tr("Keystroke event #6");
m_settings["EventCmdKey07"] = tr("Keystroke event #7");
m_settings["EventCmdKey08"] = tr("Keystroke event #8");
m_settings["EventCmdKey09"] = tr("Keystroke event #9");
m_settings["EventCmdKey10"] = tr("Keystroke event #10");
m_settings["EventCmdAll"] = tr("Any event");
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */