1 /*
2  *  Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
3  *
4  *  This is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This software is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this software; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
17  *  USA.
18  */
19 
20 /*
21  * args.c - argument processing.
22  */
23 
24 #include <sys/utsname.h>
25 #include <x2vnc.h>
26 
27 #define FLASHPORT  (5400)    /* Offset to listen for `flash' commands */
28 #define CLIENTPORT (5500)    /* Offset to listen for reverse connections */
29 #define SERVERPORT (5900)    /* Offset to server for regular connections */
30 
31 char *programName;
32 
33 char hostname[256];
34 int port;
35 
36 Bool listenSpecified = False;
37 int listenPort = 0, flashPort = 0;
38 
39 char *displayname = NULL;
40 
41 Bool shareDesktop = False;
42 
43 CARD32 explicitEncodings[MAX_ENCODINGS];
44 int nExplicitEncodings = 0;
45 Bool addCopyRect = True;
46 Bool addRRE = True;
47 Bool addCoRRE = True;
48 Bool addHextile = True;
49 
50 Bool useBGR233 = False;
51 Bool forceOwnCmap = False;
52 Bool forceTruecolour = False;
53 Bool resurface = False;
54 Bool reconnect = True;
55 int requestedDepth = 0;
56 float acceleration = 1.0;
57 
58 char *geometry = NULL;
59 
60 int wmDecorationWidth = 4;
61 int wmDecorationHeight = 24;
62 
63 char *passwdFile = NULL;
64 
65 int updateRequestPeriodms = 0;
66 
67 int updateRequestX = 0;
68 int updateRequestY = 0;
69 int updateRequestW = 0;
70 int updateRequestH = 0;
71 
72 int rawDelay = 0;
73 int copyRectDelay = 0;
74 
75 int debug=0;
76 Bool trimsel = False;
77 
78 Bool noblank = False;
79 int no_wakeup_delay = 0x7fffffff;
80 
usage()81 void usage()
82 {
83   fprintf(stderr,
84 	  "x2vnc version " VERSION ", Copyright (C) 2000 Fredrik Hubinette\n"
85 	  "Based on vncviewer which is copyright by AT&T\n"
86 	  "x2vnc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
87 	  "and you are welcome to redistribute it under certain conditions.\n"
88 	  "See the file COPYING in the x2vnc source for details.\n"
89 	  "\n"
90 	  "usage: %s [<options>] <host>:<display#>\n"
91 	  "       %s [<options>] -listen [<display#>]\n"
92 	  "\n"
93 	  "<options> are:\n"
94 	  "              [-display <display>]\n"
95           "              [-version]\n"
96           "              [-shared]\n"
97 	  "              [-north] [-south] [-east] [-west]\n"
98           "              [-hotkey key]\n"
99 	  "              [-passwdfile <passwd-file>]\n"
100 	  "              [-resurface]\n"
101 	  "              [-edgewidth width]\n"
102 	  "              [-desktop desktop]\n"
103 	  "              [-timeout seconds]\n"
104 	  "              [-wheelhack]\n"
105 	  "              [-navhack]\n"
106 	  "              [-reversewheel]\n"
107 	  "              [-scrolllines lines]\n"
108 	  "              [-mac]\n"
109 	  "              [-trimsel]\n"
110 	  "              [-noblank]\n"
111 	  "              [-lockdelay seconds]\n"
112 	  "              [-debug]\n"
113 	  "              [-accel multiplier]\n"
114 	  "              [-noreconnect]\n"
115 	  "              [-tunnel]\n"
116 	  "              [-via <host>]\n"
117 	  " Known extensions:"
118 #ifdef HAVE_XINERAMA
119 	  " Xinerama"
120 #endif
121 #if HAVE_MIT_SCREENSAVER
122 	  " MIT-Screensaver"
123 #endif
124 #ifdef HAVE_XIDLE
125 	  " Xidle"
126 #endif
127 #ifdef HAVE_XRANDR
128 	  " randr"
129 #endif
130 #ifdef HAVE_XF86DGA
131 	  " XFree86-DGA"
132 #endif
133 	  "\n"
134 	  ,programName,programName);
135   exit(1);
136 }
137 
processArgs(int argc,char ** argv)138 void processArgs(int argc, char **argv)
139 {
140   int i;
141   Bool argumentSpecified = False;
142 
143   programName = argv[0];
144 
145   for (i = 1; i < argc; i++) {
146     if (strcmp(argv[i],"-display") == 0) {
147 
148       if (++i >= argc) usage();
149       displayname = argv[i];
150 
151     } else if (strcmp(argv[i],"-east") == 0) {
152       edge=EDGE_EAST;
153     } else if (strcmp(argv[i],"-west") == 0) {
154       edge=EDGE_WEST;
155     } else if (strcmp(argv[i],"-north") == 0) {
156       edge=EDGE_NORTH;
157     } else if (strcmp(argv[i],"-south") == 0) {
158       edge=EDGE_SOUTH;
159     } else if (strcmp(argv[i],"-mac") == 0) {
160       extern int mac_mode;
161       mac_mode=1;
162     } else if (strcmp(argv[i],"-nowheel") == 0) {
163       extern int emulate_wheel;
164       emulate_wheel=0;
165     } else if (strcmp(argv[i],"-wheelhack") == 0) {
166       extern int emulate_wheel;
167       emulate_wheel=1;
168     } else if (strcmp(argv[i],"-navhack") == 0) {
169       extern int emulate_nav;
170       emulate_nav=1;
171     } else if (strcmp(argv[i],"-reversewheel") == 0) {
172       extern int wheel_button_up;
173       wheel_button_up=5;
174     } else if (strcmp(argv[i],"-resurface") == 0) {
175       resurface=True;
176     } else if (strcmp(argv[i],"-noreconnect") == 0) {
177       reconnect=False;
178     } else if (strcmp(argv[i],"-shared") == 0) {
179       shareDesktop = True;
180     } else if (strcmp(argv[i],"-edgewidth") == 0) {
181       extern int edge_width;
182       if (++i >= argc) usage();
183       edge_width = atoi(argv[i]);
184       if(edge_width < 0)
185       {
186 	fprintf(stderr,"x2vnc: -edgewidth cannot be less than 0\n");
187 	exit(1);
188       }
189     } else if (strcmp(argv[i],"-desktop") == 0) {
190       extern int requested_desktop;
191       if (++i >= argc) usage();
192       requested_desktop = atoi(argv[i]);
193     } else if (strcmp(argv[i],"-timeout") == 0) {
194       extern long grab_timeout_delay;
195       if (++i >= argc) usage();
196       grab_timeout_delay = atoi(argv[i]);
197       if(grab_timeout_delay < 0)
198       {
199 	fprintf(stderr,"x2vnc: -timeout cannot be less than 0\n");
200 	exit(1);
201       }
202     } else if (strcmp(argv[i],"-lockdelay") == 0) {
203       if (++i >= argc) usage();
204       no_wakeup_delay = atoi(argv[i]);
205     } else if (strcmp(argv[i],"-accel") == 0) {
206       if (++i >= argc) usage();
207       acceleration = atof(argv[i]);
208     } else if (strcmp(argv[i],"-scrolllines") == 0) {
209       extern int scroll_lines;
210       if (++i >= argc) usage();
211       scroll_lines = atoi(argv[i]);
212       if(scroll_lines < 1)
213       {
214 	fprintf(stderr,"x2vnc: -scrollines cannot be less than 1\n");
215 	exit(1);
216       }
217     } else if (strcmp(argv[i],"-hotkey") == 0) {
218       extern void sethotkey(char *);
219       if (++i >= argc) usage();
220       sethotkey(argv[i]);
221     } else if (strcmp(argv[i],"-passwd") == 0) {
222       if (++i >= argc) usage();
223       passwdFile = argv[i];
224     } else if (strcmp(argv[i],"-passwdfile") == 0) {
225       if (++i >= argc) usage();
226       passwdFile = argv[i];
227     } else if (strcmp(argv[i],"-debug") == 0) {
228       debug++;
229     } else if (strcmp(argv[i],"-trimsel") == 0) {
230       trimsel = True;
231     } else if (strcmp(argv[i],"-noblank") == 0) {
232       noblank = True;
233     } else if (strcmp(argv[i],"-tunnel") == 0) {
234       useSSHTunnel=True;
235     } else if (strcmp(argv[i],"-via") == 0) {
236       if (++i >= argc) usage();
237       useSSHGateway = argv[i];
238       useSSHTunnel=True;
239     } else if (strcmp(argv[i],"-listen") == 0) {
240       if (argumentSpecified) usage();
241 
242       listenSpecified = True;
243       if (++i < argc) {
244 	listenPort = CLIENTPORT+atoi(argv[i]);
245 	flashPort = FLASHPORT+atoi(argv[i]);
246       }
247 
248     } else if (argv[i][0] != '-') {
249 
250       if (argumentSpecified || listenSpecified) usage();
251 
252       argumentSpecified = True;
253 
254       if (sscanf(argv[i], "%[^:]:%d", hostname, &port) != 2) usage();
255 
256       if (port < 100)
257 	port += SERVERPORT;
258 
259     } else {
260       usage();
261     }
262   }
263 
264   if (listenSpecified) {
265     if (listenPort == 0) {
266       char *display;
267       char *colonPos;
268       struct utsname hostinfo;
269 
270       display = XDisplayName(displayname);
271       colonPos = strchr(display, ':');
272 
273       uname(&hostinfo);
274 
275       if (colonPos && ((colonPos == display) ||
276 		       (strncmp(hostinfo.nodename, display,
277 				strlen(hostinfo.nodename)) == 0))) {
278 
279 	listenPort = CLIENTPORT+atoi(colonPos+1);
280 	flashPort = FLASHPORT+atoi(colonPos+1);
281 
282       } else {
283 	fprintf(stderr,"%s: cannot work out which display number to "
284 		"listen on.\n", programName);
285 		fprintf(stderr,
286 			"Please specify explicitly with -listen <num>\n");
287 		exit(1);
288 	    }
289 	}
290 
291     } else if (!argumentSpecified) {
292 
293 	usage();
294 
295     }
296 }
297