00001
00002 #include <cstdlib>
00003
00004
00005 #include <sys/types.h>
00006
00007
00008 #include <iostream>
00009 using namespace std;
00010
00011
00012 #include <qapplication.h>
00013 #include <qsocketdevice.h>
00014 #include <qstring.h>
00015 #include <qcstring.h>
00016 #include <qfile.h>
00017 #include <qhostaddress.h>
00018
00019
00020 #include "exitcodes.h"
00021 #include "compat.h"
00022
00023 const QString kalert =
00024 "<mythnotify version=\"1\">\n"
00025 " <container name=\"notify_alert_text\">\n"
00026 " <textarea name=\"notify_text\">\n"
00027 " <value>%alert_text%</value>\n"
00028 " </textarea>\n"
00029 " </container>\n"
00030 "</mythnotify>";
00031
00032 const QString kscroll =
00033 "<mythnotify version=\"1\" displaytime=\"-1\">\n"
00034 " <container name=\"news_scroller\">\n"
00035 " <textarea name=\"text_scroll\">\n"
00036 " <value>%scroll_text%</value>\n"
00037 " </textarea>\n"
00038 " </container>\n"
00039 "</mythnotify>";
00040
00041 const QString kcid =
00042 "<mythnotify version=\"1\">\n"
00043 " <container name=\"notify_cid_info\">\n"
00044 " <textarea name=\"notify_cid_line\">\n"
00045 " <value>LINE #%caller_line%</value>\n"
00046 " </textarea>\n"
00047 " <textarea name=\"notify_cid_name\">\n"
00048 " <value>NAME: %caller_name%</value>\n"
00049 " </textarea>\n"
00050 " <textarea name=\"notify_cid_num\">\n"
00051 " <value>NUM : %caller_number%</value>\n"
00052 " </textarea>\n"
00053 " <textarea name=\"notify_cid_dt\">\n"
00054 " <value>DATE: %caller_date% TIME : %caller_time%</value>\n"
00055 " </textarea>\n"
00056 " </container>\n"
00057 "</mythnotify>";
00058
00059 void printHelp(void)
00060 {
00061 cout << "\nUsage: mythtvosd --template=<name> [OPTION]\n";
00062 cout << "A utility to put items on the mythTV OSD.\n\n";
00063 cout << " --template=<name> : (required) template name or xml file to send\n";
00064 cout << " (--file=cid.xml)\n";
00065 cout << " Valid template names are 'cid', 'alert',\n";
00066 cout << " and 'scroller'\n";
00067 cout << " --replace=\"value\" : (optional) 'replace' can be any word present in the\n";
00068 cout << " template\n";
00069 cout << " : 'cid' has: caller_line, caller_name, caller_number\n";
00070 cout << " caller_date, and caller_time\n";
00071 cout << " : 'alert' has: alert_text\n";
00072 cout << " : 'scroller' has: scroll_text\n";
00073 cout << " --udpport=\"value\" : (optional) UDP port to sent to (--udpport=6948)\n";
00074 cout << " --bcastaddr\"value\" : (optional) IP address to send to\n";
00075 cout << " (--bcastaddr=127.0.0.1)\n";
00076 cout << " --verbose : (optional) extra debug information\n";
00077 cout << " --help : (optional) this text\n";
00078
00079 cout << "\nAn XML file or template name is required - it will be read into memory. Then\n";
00080 cout << "each %%name%% item will be replaced with the given 'value'.\n";
00081 }
00082
00083 int main(int argc, char *argv[])
00084 {
00085 QApplication a(argc, argv, false);
00086
00087 QHostAddress address;
00088 address.setAddress("255.255.255.255");
00089 unsigned short port = 6948;
00090
00091 QString message = "";
00092
00093 bool verbose = false;
00094
00095 QString templatearg = "";
00096
00097 if (a.argc() == 0)
00098 {
00099 printHelp();
00100 return TVOSD_EXIT_OK;
00101 }
00102
00103 for (int argpos = 1; argpos < a.argc(); ++argpos)
00104 {
00105 QString arg = a.argv()[argpos];
00106
00107 if (arg.startsWith("-h") || arg.startsWith("--help"))
00108 {
00109 printHelp();
00110 return TVOSD_EXIT_OK;
00111 }
00112
00113 if (arg.startsWith("--template="))
00114 {
00115 templatearg = arg.section('=', 1);
00116 break;
00117 }
00118 }
00119
00120 if (templatearg == "")
00121 {
00122 cerr << "--template=<value> argument is required\n"
00123 << " <value> can be 'alert', 'cid', 'scroller', or a "
00124 << "filename of a template\n";
00125 return TVOSD_EXIT_INVALID_CMDLINE;
00126 }
00127
00128 if (templatearg == "cid")
00129 message = kcid;
00130 else if (templatearg == "alert")
00131 message = kalert;
00132 else if (templatearg == "scroller")
00133 message = kscroll;
00134 else
00135 {
00136 QFile mfile(templatearg);
00137 if (!mfile.open(IO_ReadOnly))
00138 {
00139 cerr << "Unable to open template file '" << templatearg << "'\n";
00140 return TVOSD_EXIT_NO_TEMPLATE;
00141 }
00142
00143 QByteArray mdata = mfile.readAll();
00144 message = QString(mdata);
00145 }
00146
00147 for (int argpos = 1; argpos < a.argc(); ++argpos)
00148 {
00149 QString arg = a.argv()[argpos];
00150
00151 if (arg.startsWith("--help") || arg.startsWith("--template"))
00152 continue;
00153
00154 if (arg.startsWith("--verbose"))
00155 {
00156 verbose = true;
00157 continue;
00158 }
00159
00160 if (arg.startsWith("--udpport"))
00161 {
00162 port = arg.section("=", 1).toUShort();
00163 if (port == 0)
00164 port = 6948;
00165 continue;
00166 }
00167
00168 if (arg.startsWith("--bcastaddr"))
00169 {
00170 QString newaddr = arg.section("=", 1);
00171 if (!newaddr.isEmpty())
00172 address.setAddress(newaddr);
00173 continue;
00174 }
00175
00176 QString name = arg.section("=", 0, 0);
00177 name.replace("--", "");
00178
00179 QString value = QString::fromLocal8Bit(arg.section("=", 1));
00180 if (verbose)
00181 {
00182
00183 cerr << "name: " << name
00184 << " -- value: " << value.local8Bit() << endl;
00185 }
00186
00187 name.append("%");
00188 name.prepend("%");
00189
00190 message.replace(name, value);
00191 }
00192
00193 if (verbose)
00194 cout << "output:\n" << message.local8Bit() << endl;
00195
00196 QSocketDevice sock(QSocketDevice::Datagram);
00197
00198 int yes = 1;
00199 if (setsockopt(sock.socket(), SOL_SOCKET, SO_BROADCAST, &yes, sizeof(int))
00200 < 0)
00201 {
00202 perror("Set broadcast");
00203 return TVOSD_EXIT_SOCKET_ERROR;
00204 }
00205
00206 QCString utf8 = message.utf8();
00207 int size = utf8.length();
00208
00209 if (sock.writeBlock(utf8.data(), size, address, port) < 0)
00210 {
00211 perror("sendto");
00212 }
00213 else
00214 {
00215 cout << "Sent UDP/XML packet to IP " << address.toString()
00216 << " and port: " << port << endl;
00217 }
00218
00219 return TVOSD_EXIT_OK;
00220 }
00221