MythTV  master
mythtv/programs/mythlcdserver/main.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 #include <csignal>
9 #include <fcntl.h>
10 #include <iostream>
11 #include <unistd.h>
12 
13 #include <QCoreApplication>
14 #include <QFile>
15 
16 #include "exitcodes.h"
17 #include "mythcontext.h"
18 #include "mythdbcon.h"
19 #include "loggingserver.h"
20 #include "mythlogging.h"
21 #include "mythversion.h"
22 #include "tv_play.h"
23 #include "compat.h"
24 #include "mythtranslation.h"
25 #include "commandlineparser.h"
26 #include "signalhandling.h"
27 
28 #include "lcdserver.h"
29 
30 int main(int argc, char **argv)
31 {
32  int special_port = -1;
33  QString startup_message = ""; // default to no startup message
34  int message_time = 30; // time to display startup message
35 
36  // TODO: check if this can use LOG_*
37  debug_level = 0; // don't show any debug messages by default
38 
40  if (!cmdline.Parse(argc, argv))
41  {
44  }
45 
46  if (cmdline.toBool("showhelp"))
47  {
49  return GENERIC_EXIT_OK;
50  }
51 
52  if (cmdline.toBool("showversion"))
53  {
55  return GENERIC_EXIT_OK;
56  }
57 
58  QCoreApplication a(argc, argv);
59  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHLCDSERVER);
60 
61  int retval = cmdline.Daemonize();
62  if (retval != GENERIC_EXIT_OK)
63  return retval;
64 
65  bool daemonize = cmdline.toBool("daemon");
66  QString mask("general");
67  if ((retval = cmdline.ConfigureLogging(mask, daemonize)) != GENERIC_EXIT_OK)
68  return retval;
69 
70  if (cmdline.toBool("port"))
71  {
72  special_port = cmdline.toInt("port");
73  if (special_port < 1 || special_port > 65534)
74  {
75  LOG(VB_GENERAL, LOG_ERR, "lcdserver: Bad port number");
77  }
78  }
79  if (cmdline.toBool("message"))
80  startup_message = cmdline.toString("message");
81  if (cmdline.toBool("messagetime"))
82  {
83  message_time = cmdline.toInt("messagetime");
84  if (message_time < 1 || message_time > 1000)
85  {
86  LOG(VB_GENERAL, LOG_ERR, "lcdserver: Bad message duration");
88  }
89  }
90  if (cmdline.toBool("debug"))
91  {
92  debug_level = cmdline.toInt("debug");
93  if (debug_level < 0 || debug_level > 10)
94  {
95  LOG(VB_GENERAL, LOG_ERR, "lcdserver: Bad debug level");
97  }
98  }
99 
100 #ifndef _WIN32
101  QList<int> signallist;
102  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
103  << SIGILL;
104 #if ! CONFIG_DARWIN
105  signallist << SIGRTMIN;
106 #endif
107  SignalHandler::Init(signallist);
108  SignalHandler::SetHandler(SIGHUP, logSigHup);
109 #endif
110 
111  // Get the MythTV context and db hooks
113  if (!gContext->Init(false))
114  {
115  LOG(VB_GENERAL, LOG_ERR,
116  "lcdserver: Could not initialize MythContext. Exiting.");
119  }
120 
121  MythTranslation::load("mythfrontend");
122 
124 
125  // Figure out port to listen on
126  int assigned_port = gCoreContext->GetNumSetting("LCDServerPort", 6545);
127  if (special_port > 0)
128  {
129  assigned_port = special_port;
130  }
131 
132  auto *server = new LCDServer(assigned_port, startup_message, message_time);
133 
134  QCoreApplication::exec();
135 
136  delete server;
137  delete gContext;
138 
140 
141  return GENERIC_EXIT_OK;
142 }
MYTH_APPNAME_MYTHLCDSERVER
#define MYTH_APPNAME_MYTHLCDSERVER
Definition: mythcorecontext.h:30
GENERIC_EXIT_OK
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
MythCoreContext::ConnectToMasterServer
bool ConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
Definition: mythcorecontext.cpp:377
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
mythdbcon.h
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
GENERIC_EXIT_INVALID_CMDLINE
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
main
int main(int argc, char **argv)
Definition: mythtv/programs/mythlcdserver/main.cpp:30
mythversion.h
MythLCDServerCommandLineParser
Definition: mythlcdserver/commandlineparser.h:6
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1435
mythlogging.h
commandlineparser.h
signalhandling.h
compat.h
SIGHUP
#define SIGHUP
Definition: compat.h:215
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1263
mythtranslation.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1279
MythCommandLineParser::Daemonize
int Daemonize(void) const
Fork application into background, and detatch from terminal.
Definition: mythcommandlineparser.cpp:2767
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
MythCoreContext::GetNumSetting
int GetNumSetting(const QString &key, int defaultval=0)
Definition: mythcorecontext.cpp:933
MYTH_BINARY_VERSION
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:15
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythtv/programs/mythcommflag/main.cpp:76
LCDServer
Definition: lcdserver.h:27
MythCommandLineParser::toString
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2098
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:1941
lcdserver.h
mythcontext.h
debug_level
int debug_level
Definition: lcdserver.cpp:73
GENERIC_EXIT_NO_MYTHCONTEXT
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
SignalHandler::SetHandler
static void SetHandler(int signum, SigHandlerFunc handler)
Definition: signalhandling.cpp:138
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2592
MythTranslation::load
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
Definition: mythtranslation.cpp:37
exitcodes.h
MythCommandLineParser::toInt
int toInt(const QString &key) const
Returns stored QVariant as an integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:1963
loggingserver.h
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:64
MythContext::Init
bool Init(bool gui=true, bool promptForBackend=false, bool disableAutoDiscovery=false, bool ignoreDB=false)
Definition: mythcontext.cpp:1562
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:131
tv_play.h
SignalHandler::Init
static void Init(QList< int > &signallist, QObject *parent=nullptr)
Definition: signalhandling.cpp:124