00001
00002
00003
00004
00005
00006
00007
00008 #include <qapplication.h>
00009 #include <qsqldatabase.h>
00010 #include <qfile.h>
00011 #include <qtextstream.h>
00012
00013 #include <iostream>
00014 using namespace std;
00015 #include <unistd.h>
00016 #include <fcntl.h>
00017 #include <signal.h>
00018
00019 #include "exitcodes.h"
00020 #include "mythcontext.h"
00021 #include "mythdbcon.h"
00022 #include "tv_play.h"
00023 #include "compat.h"
00024
00025 #include "lcdserver.h"
00026
00027 #define LCD_EXIT_DAEMONIZING_ERROR FRONTEND_EXIT_START-1
00028 #define LCD_EXIT_NO_TRANSCODE_SUPPORT FRONTEND_EXIT_START-2
00029
00030 int main(int argc, char **argv)
00031 {
00032 QApplication a(argc, argv, false);
00033 bool daemon_mode = false;
00034 int special_port = -1;
00035 QString startup_message = "";
00036 int message_time = 30;
00037 print_verbose_messages = VB_IMPORTANT;
00038 QString logfile = "";
00039
00040 debug_level = 0;
00041
00042
00043 for (int argpos = 1; argpos < a.argc(); ++argpos)
00044 {
00045
00046 if (!strcmp(a.argv()[argpos],"-d") ||
00047 !strcmp(a.argv()[argpos],"--daemon"))
00048 {
00049 daemon_mode = true;
00050 }
00051 else if (!strcmp(a.argv()[argpos],"-n") ||
00052 !strcmp(a.argv()[argpos],"--nodaemon"))
00053 {
00054 daemon_mode = false;
00055 }
00056 else if (!strcmp(a.argv()[argpos],"-p") ||
00057 !strcmp(a.argv()[argpos],"--port"))
00058 {
00059 if (a.argc() > argpos)
00060 {
00061 QString port_number = a.argv()[argpos+1];
00062 ++argpos;
00063 special_port = port_number.toInt();
00064 if (special_port < 1 || special_port > 65534)
00065 {
00066 VERBOSE(VB_IMPORTANT, "lcdserver: Bad port number");
00067 return FRONTEND_EXIT_INVALID_CMDLINE;
00068 }
00069 }
00070 else
00071 {
00072 VERBOSE(VB_IMPORTANT, "lcdserver: Missing argument to "
00073 "-p/--port option");
00074 return FRONTEND_EXIT_INVALID_CMDLINE;
00075 }
00076 }
00077 else if (!strcmp(a.argv()[argpos],"-m") ||
00078 !strcmp(a.argv()[argpos],"--startupmessage"))
00079 {
00080 if (a.argc() > argpos)
00081 {
00082 startup_message = a.argv()[argpos+1];
00083 ++argpos;
00084 }
00085 else
00086 {
00087 VERBOSE(VB_IMPORTANT, "lcdserver: Missing argument to "
00088 "-m/--startupmessage");
00089 return FRONTEND_EXIT_INVALID_CMDLINE;
00090 }
00091 }
00092 else if (!strcmp(a.argv()[argpos],"-t") ||
00093 !strcmp(a.argv()[argpos],"--messagetime"))
00094 {
00095 if (a.argc() > argpos)
00096 {
00097 QString sTime = a.argv()[argpos+1];
00098 ++argpos;
00099 message_time = sTime.toInt();
00100 if (message_time < 1 || message_time > 1000)
00101 {
00102 VERBOSE(VB_IMPORTANT, "lcdserver: Bad show message time");
00103 return FRONTEND_EXIT_INVALID_CMDLINE;
00104 }
00105 }
00106 else
00107 {
00108 VERBOSE(VB_IMPORTANT, "lcdserver: Missing argument to "
00109 "-t/--messagetime");
00110 return FRONTEND_EXIT_INVALID_CMDLINE;
00111 }
00112 }
00113 else if (!strcmp(a.argv()[argpos],"-v") ||
00114 !strcmp(a.argv()[argpos],"--verbose"))
00115 {
00116 if (a.argc()-1 > argpos)
00117 {
00118 if (parse_verbose_arg(a.argv()[argpos+1]) ==
00119 GENERIC_EXIT_INVALID_CMDLINE)
00120 return FRONTEND_EXIT_INVALID_CMDLINE;
00121
00122 ++argpos;
00123 }
00124 else
00125 {
00126 cerr << "Missing argument to -v/--verbose option\n";
00127 return FRONTEND_EXIT_INVALID_CMDLINE;
00128 }
00129 }
00130 else if (!strcmp(a.argv()[argpos],"-l") ||
00131 !strcmp(a.argv()[argpos],"--logfile"))
00132 {
00133 if (a.argc()-1 > argpos)
00134 {
00135 logfile = a.argv()[argpos+1];
00136 if (logfile.startsWith("-"))
00137 {
00138 cerr << "Invalid or missing argument to -l/--logfile option\n";
00139 return FRONTEND_EXIT_INVALID_CMDLINE;
00140 }
00141 else
00142 {
00143 ++argpos;
00144 }
00145 }
00146 else
00147 {
00148 cerr << "Missing argument to -l/--logfile option\n";
00149 return FRONTEND_EXIT_INVALID_CMDLINE;
00150 }
00151 }
00152 else if (!strcmp(a.argv()[argpos],"-x") ||
00153 !strcmp(a.argv()[argpos],"--debuglevel"))
00154 {
00155 if (a.argc() > argpos)
00156 {
00157 QString sTemp = a.argv()[argpos+1];
00158
00159 ++argpos;
00160 debug_level = sTemp.toInt();
00161 if (debug_level < 0 || debug_level > 10)
00162 {
00163 VERBOSE(VB_IMPORTANT, "lcdserver: Bad debug level");
00164 return FRONTEND_EXIT_INVALID_CMDLINE;
00165 }
00166 }
00167 else
00168 {
00169 VERBOSE(VB_IMPORTANT, "lcdserver: Missing argument to "
00170 "-x/--debuglevel");
00171 return FRONTEND_EXIT_INVALID_CMDLINE;
00172 }
00173 }
00174 else
00175 {
00176 cerr << "Invalid argument: " << a.argv()[argpos] << endl <<
00177 "Valid options are: " << endl <<
00178 "-p or --port number A port number to listen on (default is 6545) " << endl <<
00179 "-d or --daemon Runs lcd server as a daemon " << endl <<
00180 "-n or --nodaemon Does not run lcd server as a daemon (default)" << endl <<
00181 "-m or --startupmessage Message to show at startup" << endl <<
00182 "-t or --messagetime How long to show startup message (default 30 seconds)" << endl <<
00183 "-l or --logfile filename Writes STDERR and STDOUT messages to filename" << endl <<
00184 "-v or --verbose debug-level Use '-v help' for level info" << endl <<
00185 "-x or --debuglevel level Control how much debug messages to show" << endl <<
00186 " [number between 0 and 10] (default 0)" << endl;
00187 return FRONTEND_EXIT_INVALID_CMDLINE;
00188 }
00189 }
00190
00191
00192 int logfd = -1;
00193
00194 if (logfile != "")
00195 {
00196 logfd = open(logfile.ascii(), O_WRONLY|O_CREAT|O_APPEND, 0664);
00197
00198 if (logfd < 0)
00199 {
00200 perror("open(logfile)");
00201 return FRONTEND_EXIT_OPENING_LOGFILE_ERROR;
00202 }
00203 }
00204
00205 if (logfd != -1)
00206 {
00207
00208 dup2(logfd, 1);
00209 dup2(logfd, 2);
00210
00211
00212 if (logfd != 1 && logfd != 2)
00213 close(logfd);
00214 }
00215
00216 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
00217 cerr << "Unable to ignore SIGPIPE\n";
00218
00219
00220 if (daemon_mode)
00221 {
00222 if (daemon(0, 1) < 0)
00223 {
00224 VERBOSE(VB_IMPORTANT, "lcdserver: Failed to run as a daemon. "
00225 "Bailing out.");
00226 return LCD_EXIT_DAEMONIZING_ERROR;
00227 }
00228 cout << endl;
00229 }
00230
00231
00232 gContext = NULL;
00233 gContext = new MythContext(MYTH_BINARY_VERSION);
00234 if (!gContext->Init(false))
00235 {
00236 VERBOSE(VB_IMPORTANT, "lcdserver: Could not initialize myth context. "
00237 "Exiting.");
00238 return FRONTEND_EXIT_NO_MYTHCONTEXT;
00239 }
00240
00241 gContext->ConnectToMasterServer(false);
00242
00243
00244 int assigned_port = gContext->GetNumSetting("LCDServerPort", 6545);
00245 if (special_port > 0)
00246 {
00247 assigned_port = special_port;
00248 }
00249
00250 new LCDServer(assigned_port, startup_message, message_time);
00251
00252 a.exec();
00253
00254 delete gContext;
00255 return FRONTEND_EXIT_OK;
00256 }
00257