MythTV master
mythlcdserver.cpp
Go to the documentation of this file.
1/*
2 main.cpp
3
4 Starting point for the myth lcd server daemon
5
6*/
7
8// c/c++
9#include <csignal>
10#include <fcntl.h>
11#include <iostream>
12#include <unistd.h>
13
14// Qt
15#include <QtGlobal>
16#include <QCoreApplication>
17#include <QFile>
18
19// MythTV
20#include "libmyth/mythcontext.h"
21#include "libmythbase/compat.h"
28#include "libmythbase/mythversion.h"
29#include "libmythtv/tv_play.h"
30
31// mythlcdserver
32#include "lcdserver.h"
34
35int main(int argc, char **argv)
36{
37 int special_port = -1;
38 QString startup_message = ""; // default to no startup message
39 std::chrono::seconds message_time = 30s; // time to display startup message
40
41 // TODO: check if this can use LOG_*
42 debug_level = 0; // don't show any debug messages by default
43
45 if (!cmdline.Parse(argc, argv))
46 {
49 }
50
51 if (cmdline.toBool("showhelp"))
52 {
54 return GENERIC_EXIT_OK;
55 }
56
57 if (cmdline.toBool("showversion"))
58 {
60 return GENERIC_EXIT_OK;
61 }
62
63 QCoreApplication a(argc, argv);
64 QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHLCDSERVER);
65
66 int retval = cmdline.Daemonize();
67 if (retval != GENERIC_EXIT_OK)
68 return retval;
69
70 bool daemonize = cmdline.toBool("daemon");
71 QString mask("general");
72 retval = cmdline.ConfigureLogging(mask, daemonize);
73 if (retval != GENERIC_EXIT_OK)
74 return retval;
75
76 if (cmdline.toBool("port"))
77 {
78 special_port = cmdline.toInt("port");
79 if (special_port < 1 || special_port > 65534)
80 {
81 LOG(VB_GENERAL, LOG_ERR, "lcdserver: Bad port number");
83 }
84 }
85 if (cmdline.toBool("message"))
86 startup_message = cmdline.toString("message");
87 if (cmdline.toBool("messagetime"))
88 {
89 message_time = std::chrono::seconds(cmdline.toInt("messagetime"));
90 if (message_time < 1s || message_time > 1000s)
91 {
92 LOG(VB_GENERAL, LOG_ERR, "lcdserver: Bad message duration");
94 }
95 }
96 if (cmdline.toBool("debug"))
97 {
98 debug_level = cmdline.toInt("debug");
99 if (debug_level < 0 || debug_level > 10)
100 {
101 LOG(VB_GENERAL, LOG_ERR, "lcdserver: Bad debug level");
103 }
104 }
105
106 // Get the MythTV context and db hooks
107 MythContext context {MYTH_BINARY_VERSION};
108 if (!context.Init(false))
109 {
110 LOG(VB_GENERAL, LOG_ERR,
111 "lcdserver: Could not initialize MythContext. Exiting.");
113 }
114
115 MythTranslation::load("mythfrontend");
116
118
119 // Figure out port to listen on
120 int assigned_port = gCoreContext->GetNumSetting("LCDServerPort", 6545);
121 if (special_port > 0)
122 {
123 assigned_port = special_port;
124 }
125
126 auto *server = new LCDServer(assigned_port, startup_message, message_time);
127
128 QCoreApplication::exec();
129
130 delete server;
131
132 return GENERIC_EXIT_OK;
133}
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
int toInt(const QString &key) const
Returns stored QVariant as an integer, falling to default if not provided.
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
int Daemonize(void) const
Fork application into background, and detatch from terminal.
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
static void PrintVersion(void)
Print application version information.
void PrintHelp(void) const
Print command line option help.
Startup context for MythTV.
Definition: mythcontext.h:20
bool ConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
int GetNumSetting(const QString &key, int defaultval=0)
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
@ GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:16
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:13
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:18
int debug_level
Definition: lcdserver.cpp:76
static constexpr const char * MYTH_APPNAME_MYTHLCDSERVER
Definition: mythappname.h:15
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
int main(int argc, char **argv)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythCommFlagCommandLineParser cmdline