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 <QtGlobal>
14 #include <QCoreApplication>
15 #include <QFile>
16 
17 #include "exitcodes.h"
18 #include "mythcontext.h"
19 #include "mythdbcon.h"
20 #include "loggingserver.h"
21 #include "mythlogging.h"
22 #include "mythversion.h"
23 #include "tv_play.h"
24 #include "compat.h"
25 #include "mythtranslation.h"
26 #include "commandlineparser.h"
27 #include "signalhandling.h"
28 
29 #include "lcdserver.h"
30 
31 int main(int argc, char **argv)
32 {
33  int special_port = -1;
34  QString startup_message = ""; // default to no startup message
35  std::chrono::seconds message_time = 30s; // time to display startup message
36 
37  // TODO: check if this can use LOG_*
38  debug_level = 0; // don't show any debug messages by default
39 
41  if (!cmdline.Parse(argc, argv))
42  {
45  }
46 
47  if (cmdline.toBool("showhelp"))
48  {
50  return GENERIC_EXIT_OK;
51  }
52 
53  if (cmdline.toBool("showversion"))
54  {
56  return GENERIC_EXIT_OK;
57  }
58 
59  QCoreApplication a(argc, argv);
60  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHLCDSERVER);
61 
62  int retval = cmdline.Daemonize();
63  if (retval != GENERIC_EXIT_OK)
64  return retval;
65 
66  bool daemonize = cmdline.toBool("daemon");
67  QString mask("general");
68  if ((retval = cmdline.ConfigureLogging(mask, daemonize)) != GENERIC_EXIT_OK)
69  return retval;
70 
71  if (cmdline.toBool("port"))
72  {
73  special_port = cmdline.toInt("port");
74  if (special_port < 1 || special_port > 65534)
75  {
76  LOG(VB_GENERAL, LOG_ERR, "lcdserver: Bad port number");
78  }
79  }
80  if (cmdline.toBool("message"))
81  startup_message = cmdline.toString("message");
82  if (cmdline.toBool("messagetime"))
83  {
84  message_time = std::chrono::seconds(cmdline.toInt("messagetime"));
85  if (message_time < 1s || message_time > 1000s)
86  {
87  LOG(VB_GENERAL, LOG_ERR, "lcdserver: Bad message duration");
89  }
90  }
91  if (cmdline.toBool("debug"))
92  {
93  debug_level = cmdline.toInt("debug");
94  if (debug_level < 0 || debug_level > 10)
95  {
96  LOG(VB_GENERAL, LOG_ERR, "lcdserver: Bad debug level");
98  }
99  }
100 
101 #ifndef _WIN32
102  QList<int> signallist;
103  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
104  << SIGILL;
105 #ifndef Q_OS_DARWIN
106  signallist << SIGRTMIN;
107 #endif
108  SignalHandler::Init(signallist);
109  SignalHandler::SetHandler(SIGHUP, logSigHup);
110 #endif
111 
112  // Get the MythTV context and db hooks
114  if (!gContext->Init(false))
115  {
116  LOG(VB_GENERAL, LOG_ERR,
117  "lcdserver: Could not initialize MythContext. Exiting.");
120  }
121 
122  MythTranslation::load("mythfrontend");
123 
125 
126  // Figure out port to listen on
127  int assigned_port = gCoreContext->GetNumSetting("LCDServerPort", 6545);
128  if (special_port > 0)
129  {
130  assigned_port = special_port;
131  }
132 
133  auto *server = new LCDServer(assigned_port, startup_message, message_time);
134 
135  QCoreApplication::exec();
136 
137  delete server;
138  delete gContext;
139 
141 
142  return GENERIC_EXIT_OK;
143 }
MYTH_APPNAME_MYTHLCDSERVER
#define MYTH_APPNAME_MYTHLCDSERVER
Definition: mythcorecontext.h:32
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:383
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:31
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:1449
mythlogging.h
commandlineparser.h
signalhandling.h
compat.h
SIGHUP
#define SIGHUP
Definition: compat.h:216
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1277
mythtranslation.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1293
MythCommandLineParser::Daemonize
int Daemonize(void) const
Fork application into background, and detatch from terminal.
Definition: mythcommandlineparser.cpp:2931
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:61
MythCoreContext::GetNumSetting
int GetNumSetting(const QString &key, int defaultval=0)
Definition: mythcorecontext.cpp:938
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:75
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:2252
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2095
lcdserver.h
mythcontext.h
debug_level
int debug_level
Definition: lcdserver.cpp:72
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:141
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2756
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:2117
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:1571
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:134
tv_play.h
SignalHandler::Init
static void Init(QList< int > &signallist, QObject *parent=nullptr)
Definition: signalhandling.cpp:127