add --listen command line option
[presencevnc] / src / main.cpp
1 /*
2     Presence VNC
3     Copyright (C) 2010 Christian Pulvermacher
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #include "mainwindow.h"
20 #include "rfb/rfbclient.h"
21
22 #include <QApplication>
23 #include <QString>
24
25 #include <iostream>
26
27
28 const QString APPNAME("Presence VNC");
29
30 void printHelp() {
31         std::cout << "Usage: " << qPrintable(QCoreApplication::arguments().at(0)) << " [options] [URL [quality]]\n"
32
33                 << "\nOptions:\n"
34                 << " --help\t\t\t Print this text and exit.\n"
35                 << " --listen [port]\t Listen for incoming connections on given port, or 5500 if omitted. Cannot be used with an URL.\n"
36                 << " --viewonly\t\t Don't send mouse/keyboard input to remote desktop. This is only useful if you also supply a URL, or --listen.\n"
37
38                 << "\nURLs:\n"
39                 << " vnc://:password@server:display\n\n"
40                 << " Password and display can be omitted, e.g. vnc://server is a valid URL.\n"
41                 << " Optionally, you can define the quality as a second argument (1-3, where 1 is the best). Default is 2.\n";
42 }
43
44 int main(int argc, char *argv[])
45 {
46         QCoreApplication::setOrganizationName(APPNAME);
47         QCoreApplication::setApplicationName(APPNAME);
48
49         QApplication app(argc, argv);
50
51         QString url;
52         int quality = 2;
53         int listen_port = 0; //only used if >0
54         bool view_only = false;
55
56         QStringList arguments = QCoreApplication::arguments();
57         for(int i = 1; i < arguments.count(); i++) {
58                 if(arguments.at(i) == "--help") {
59                         printHelp();
60
61                         return 0;
62                 } else if(arguments.at(i) == "--listen") {
63                         if(arguments.count() <= i+1 or arguments.at(i+1).startsWith("--")) {
64                                 //no argument found, use default
65                                 listen_port = LISTEN_PORT_OFFSET;
66                         } else {
67                                 //next argument should be a port number
68                                 listen_port = arguments.at(i+1).toInt();
69                                 if(listen_port < 1 or listen_port > 65535) {
70                                         std::cerr << "\"" << qPrintable(arguments.at(i+1)) << "\" is not a valid port number!\n\n";
71                                         return 1;
72                                 }
73
74                                 //everything ok
75                                 i++;
76                         }
77                 } else if(arguments.at(i) == "--viewonly") {
78                         view_only = true;
79                 } else { //not a valid command line option, should be the url
80                         url = arguments.at(i);
81
82                         //check url
83                         if(!url.startsWith("vnc://")) {
84                                 std::cerr << "\"" << qPrintable(url) << "\" is not a valid command line option!\n\n";
85                                 printHelp();
86
87                                 return 1;
88                         }
89
90                         if(arguments.count() > i+1) { //TODO: having a --quality option would make more sense.
91                                 int arg = arguments.at(i+1).toInt();
92                                 if(1 <= arg and arg <= 3) { //check if arg is valid, might also be another option
93                                         quality = arg;
94                                         i++;
95                                 }
96                         }
97                 }
98         }
99
100         if(!url.isNull() and listen_port > 0) {
101                 std::cerr << "--listen cannot be used with an URL!\n";
102                 return 1;
103         }
104
105         MainWindow main(url, quality, listen_port, view_only);
106         main.show();
107         return app.exec();
108 }