char *use_dpy = NULL; char *auth_file = NULL; char *arg, *visual_str = NULL; char *logfile = NULL; char *passwdfile = NULL; char *blackout_string = NULL; char *remap_file = NULL; char *pointer_remap = NULL;
char *arg; char *remote_cmd = NULL; char *gui_string = NULL; int remote_query = 0;
int main(int argc, char* argv[]) {	XImage *fb;	int i;	int ev, er, maj, min;	char *use_dpy = NULL;	char *auth_file = NULL;	char *arg, *visual_str = NULL;	char *logfile = NULL;	char *passwdfile = NULL;	char *blackout_string = NULL;	char *remap_file = NULL;	char *pointer_remap = NULL;	int pw_loc = -1;	int vpw_loc = -1;	int dt = 0;	int bg = 0;	int got_rfbwait = 0;	int got_deferupdate = 0, got_defer = 0;	/* used to pass args we do not know about to rfbGetScreen(): */	int argc_vnc = 1; char *argv_vnc[128];	argv_vnc[0] = strdup(argv[0]);	check_rcfile(argc, argv);	/* kludge for the new array argv2 set in check_rcfile() */#	define argc argc2#	define argv argv2#	define CHECK_ARGC if (i >= argc-1) { \		fprintf(stderr, "not enough arguments for: %s\n", arg); \		exit(1); \	}	for (i=1; i < argc; i++) {		/* quick-n-dirty --option handling. */		arg = argv[i];		if (strstr(arg, "--") == arg) {			arg++;		}		if (!strcmp(arg, "-display")) {			CHECK_ARGC			use_dpy = argv[++i];		} else if (!strcmp(arg, "-id")) {			CHECK_ARGC			if (sscanf(argv[++i], "0x%x", &subwin) != 1) {				if (sscanf(argv[i], "%d", &subwin) != 1) {					fprintf(stderr, "bad -id arg: %s\n",					    argv[i]);					exit(1);				}			}		} else if (!strcmp(arg, "-sid")) {			rootshift = 1;			CHECK_ARGC			if (sscanf(argv[++i], "0x%x", &subwin) != 1) {				if (sscanf(argv[i], "%d", &subwin) != 1) {					fprintf(stderr, "bad -id arg: %s\n",					    argv[i]);					exit(1);				}			}		} else if (!strcmp(arg, "-scale")) {			int m, n;			char *p;			double f;			CHECK_ARGC			if ( (p = strchr(argv[++i], ':')) != NULL) {				/* options */				if (strstr(p+1, "nb") != NULL) {					scaling_noblend = 1;				}				if (strstr(p+1, "n4") != NULL) {					scaling_nomult4 = 1;				}				if (strstr(p+1, "in") != NULL) {					scaling_interpolate = 1;				}				if (strstr(p+1, "pad") != NULL) {					scaling_pad = 1;				}				*p = '\0';			}			if (strchr(argv[i], '.') != NULL) {				double test, diff, eps = 1.0e-7;				if (sscanf(argv[i], "%lf", &f) != 1) {					fprintf(stderr, "bad -scale arg: %s\n",					    argv[i]);					exit(1);				}				scale_fac = (double) f;				/* look for common fractions from small ints: */				for (n=2; n<=10; n++) {					for (m=1; m<n; m++) {						test = ((double) m)/ n;						diff = scale_fac - test;						if (-eps < diff && diff < eps) {							scale_numer = m;							scale_denom = n;							break;												}					}					if (scale_denom) {						break;					}				}			} else {				if (sscanf(argv[i], "%d/%d", &m, &n) != 2) {					fprintf(stderr, "bad -scale arg: %s\n",					    argv[i]);					exit(1);				}				scale_fac = ((double) m)/ n;				scale_numer = m;				scale_denom = n;			}			if (scale_fac == 1.0) {				if (! quiet) {					rfbLog("scaling disabled for factor "					    " %f\n", scale_fac);				}			} else {				scaling = 1;			}		} else if (!strcmp(arg, "-visual")) {			CHECK_ARGC			visual_str = argv[++i];		} else if (!strcmp(arg, "-overlay")) {			overlay = 1;		} else if (!strcmp(arg, "-overlay_nocursor")) {			overlay = 1;			overlay_cursor = 0;		} else if (!strcmp(arg, "-flashcmap")) {			flash_cmap = 1;		} else if (!strcmp(arg, "-notruecolor")) {			force_indexed_color = 1;		} else if (!strcmp(arg, "-viewonly")) {			view_only = 1;		} else if (!strcmp(arg, "-viewpasswd")) {			vpw_loc = i;			CHECK_ARGC			viewonly_passwd = strdup(argv[++i]);		} else if (!strcmp(arg, "-passwdfile")) {			CHECK_ARGC			passwdfile = argv[++i];		} else if (!strcmp(arg, "-storepasswd")) {			if (i+2 >= argc || rfbEncryptAndStorePasswd(argv[i+1],			    argv[i+2]) != 0) {				fprintf(stderr, "-storepasswd failed\n");				exit(1);			} else {				fprintf(stderr, "stored passwd in file %s\n",				    argv[i+2]);				exit(0);			}		} else if (!strcmp(arg, "-shared")) {			shared = 1;		} else if (!strcmp(arg, "-auth")) {			CHECK_ARGC			auth_file = argv[++i];		} else if (!strcmp(arg, "-allow")) {			CHECK_ARGC			allow_list = argv[++i];		} else if (!strcmp(arg, "-localhost")) {			allow_list = "127.0.0.1";		} else if (!strcmp(arg, "-accept")) {			CHECK_ARGC			accept_cmd = argv[++i];		} else if (!strcmp(arg, "-gone")) {			CHECK_ARGC			gone_cmd = argv[++i];		} else if (!strcmp(arg, "-once")) {			connect_once = 1;		} else if (!strcmp(arg, "-many")			|| !strcmp(arg, "-forever")) {			connect_once = 0;		} else if (!strcmp(arg, "-connect")) {			CHECK_ARGC			if (strchr(argv[++i], '/')) {				client_connect_file = argv[i];			} else {				client_connect = strdup(argv[i]);			}		} else if (!strcmp(arg, "-vncconnect")) {			vnc_connect = 1;		} else if (!strcmp(arg, "-novncconnect")) {			vnc_connect = 0;		} else if (!strcmp(arg, "-inetd")) {			inetd = 1;		} else if (!strcmp(arg, "-noshm")) {			using_shm = 0;		} else if (!strcmp(arg, "-flipbyteorder")) {			flip_byte_order = 1;		} else if (!strcmp(arg, "-modtweak")) {			use_modifier_tweak = 1;		} else if (!strcmp(arg, "-nomodtweak")) {			use_modifier_tweak = 0;		} else if (!strcmp(arg, "-isolevel3")) {			use_iso_level3 = 1;		} else if (!strcmp(arg, "-xkb")) {			use_xkb_modtweak = 1;		} else if (!strcmp(arg, "-skip_keycodes")) {			CHECK_ARGC			skip_keycodes = argv[++i];		} else if (!strcmp(arg, "-xkbcompat")) {			xkbcompat = 1;		} else if (!strcmp(arg, "-add_keysyms")) {			add_keysyms++;		} else if (!strcmp(arg, "-clear_mods")) {			clear_mods = 1;		} else if (!strcmp(arg, "-clear_keys")) {			clear_mods = 2;		} else if (!strcmp(arg, "-remap")) {			CHECK_ARGC			remap_file = argv[++i];		} else if (!strcmp(arg, "-blackout")) {			CHECK_ARGC			blackout_string = argv[++i];		} else if (!strcmp(arg, "-xinerama")) {			xinerama = 1;		} else if (!strcmp(arg, "-norc")) {			;	/* done above */		} else if (!strcmp(arg, "-rc")) {			i++;	/* done above */		} else if (!strcmp(arg, "-nobell")) {			watch_bell = 0;		} else if (!strcmp(arg, "-nofb")) {			nofb = 1;		} else if (!strcmp(arg, "-nosel")) {			watch_selection = 0;		} else if (!strcmp(arg, "-noprimary")) {			watch_primary = 0;		} else if (!strcmp(arg, "-cursor")) {			show_cursor = 1;			if (i >= argc-1) {				;			} else {				char *s = argv[i+1];				if (*s == 'X' || !strcmp(s, "default") ||				    !strcmp(s, "some") || !strcmp(s, "most")) {					multiple_cursors_mode = strdup(s);					i++;				}			}		} else if (!strcmp(arg, "-nocursor")) { 			show_cursor = 0;		} else if (!strcmp(arg, "-cursorpos")) {			cursor_pos_updates = 1;		} else if (!strcmp(arg, "-nocursorpos")) {			cursor_pos_updates = 0;		} else if (!strcmp(arg, "-nocursorshape")) {			cursor_shape_updates = 0;		} else if (!strcmp(arg, "-xwarppointer")) {			use_xwarppointer = 1;		} else if (!strcmp(arg, "-buttonmap")) {			CHECK_ARGC			pointer_remap = argv[++i];		} else if (!strcmp(arg, "-nodragging")) {			show_dragging = 0;		} else if (!strcmp(arg, "-input_skip")) {			CHECK_ARGC			ui_skip = atoi(argv[++i]);			if (! ui_skip) ui_skip = 1;		} else if (!strcmp(arg, "-old_pointer")) {			old_pointer = 1;		} else if (!strcmp(arg, "-old_pointer2")) {			old_pointer = 2;		} else if (!strcmp(arg, "-norepeat")) {			no_autorepeat = 1;		} else if (!strcmp(arg, "-repeat")) {			no_autorepeat = 0;		} else if (!strcmp(arg, "-onetile")) {			single_copytile = 1;		} else if (!strcmp(arg, "-debug_pointer")		    || !strcmp(arg, "-dp")) {			debug_pointer++;		} else if (!strcmp(arg, "-debug_keyboard")		    || !strcmp(arg, "-dk")) {			debug_keyboard++;		} else if (!strcmp(arg, "-defer")) {			CHECK_ARGC			defer_update = atoi(argv[++i]);			got_defer = 1;		} else if (!strcmp(arg, "-wait")) {			CHECK_ARGC			waitms = atoi(argv[++i]);		} else if (!strcmp(arg, "-nap")) {			take_naps = 1;#ifdef LIBVNCSERVER_HAVE_LIBPTHREAD		} else if (!strcmp(arg, "-threads")) {			use_threads = 1;		} else if (!strcmp(arg, "-nothreads")) {			use_threads = 0;#endif		} else if (!strcmp(arg, "-sigpipe")) {			CHECK_ARGC			if (!strcmp(argv[++i], "ignore"))  {				sigpipe = 1;			} else if (!strcmp(argv[i], "exit")) {				sigpipe = 2;			} else if (!strcmp(argv[i], "skip")) {				sigpipe = 0;			} else {				fprintf(stderr, "bad -sigpipe arg: %s, must "				    "be \"ignore\" or \"exit\"\n", argv[i]);				exit(1);			}		} else if (!strcmp(arg, "-fs")) {			CHECK_ARGC			fs_frac = atof(argv[++i]);		} else if (!strcmp(arg, "-gaps")) {			CHECK_ARGC			gaps_fill = atoi(argv[++i]);		} else if (!strcmp(arg, "-grow")) {			CHECK_ARGC			grow_fill = atoi(argv[++i]);		} else if (!strcmp(arg, "-fuzz")) {			CHECK_ARGC			tile_fuzz = atoi(argv[++i]);		} else if (!strcmp(arg, "-h") || !strcmp(arg, "-help")			|| !strcmp(arg, "-?")) {			print_help();		} else if (!strcmp(arg, "-V") || !strcmp(arg, "-version")) {			fprintf(stderr, "x11vnc: %s\n", lastmod);			exit(0);		} else if (!strcmp(arg, "-o") || !strcmp(arg, "-logfile")) {			CHECK_ARGC			logfile = argv[++i];		} else if (!strcmp(arg, "-q") || !strcmp(arg, "-quiet")) {			quiet = 1;#ifdef LIBVNCSERVER_HAVE_SETSID		} else if (!strcmp(arg, "-bg") || !strcmp(arg, "-background")) {			bg = 1;#endif		} else {			if (!strcmp(arg, "-desktop")) {				dt = 1;			}			if (!strcmp(arg, "-passwd")) {				pw_loc = i;			}			if (!strcmp(arg, "-rfbwait")) {				got_rfbwait = 1;			}			if (!strcmp(arg, "-deferupdate")) {				got_deferupdate = 1;			}			if (!strcmp(arg, "-rfbport")) {				got_rfbport = 1;			}			if (!strcmp(arg, "-alwaysshared ")) {				got_alwaysshared = 1;			}			if (!strcmp(arg, "-nevershared")) {				got_nevershared = 1;			}			/* otherwise copy it for libvncserver use below. */			if (argc_vnc < 100) {				argv_vnc[argc_vnc++] = strdup(arg);			}		}	}	if (logfile) {		int n;		if ((n = open(logfile, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {			fprintf(stderr, "error opening logfile: %s\n", logfile);			perror("open");			exit(1);		}		if (dup2(n, 2) < 0) {			fprintf(stderr, "dup2 failed\n");			perror("dup2");			exit(1);		}		if (n > 2) {			close(n);		}	}	if (! quiet && ! inetd) {		int i;		for (i=1; i < argc_vnc; i++) {			rfbLog("passing arg to libvncserver: %s\n", argv_vnc[i]);			if (!strcmp(argv_vnc[i], "-passwd")) {				i++;			}		}	}	/*	 * If -passwd was used, clear it out of argv.  This does not	 * work on all UNIX, have to use execvp() in general...	 */	if (pw_loc > 0) {		char *p = argv[pw_loc];				while (*p != '\0') {			*p++ = '\0';		}		if (pw_loc+1 < argc) {			p = argv[pw_loc+1];					while (*p != '\0') {				*p++ = '\0';			}		}	} else if (passwdfile) {		/* read passwd from file */		char line[1024];		FILE *in;		in = fopen(passwdfile, "r");		if (in == NULL) {			rfbLog("cannot open passwdfile: %s\n", passwdfile);			rfbLog("fopen");			exit(1);		}		if (fgets(line, 1024, in) != NULL) {			int len = strlen(line); 			if (len > 0 && line[len-1] == '\n') {				line[len-1] = '\0';			}			argv_vnc[argc_vnc++] = "-passwd";			argv_vnc[argc_vnc++] = strdup(line);			pw_loc = 100;	/* just for pw_loc check below */			if (fgets(line, 1024, in) != NULL) {				/* try to read viewonly passwd from file */				int ok = 0;				len = strlen(line); 				if (len > 0 && line[len-1] == '\n') {					line[len-1] = '\0';				}				if (strlen(line) > 0) {					char *p = line;					/* check for non-blank line */					while (*p != '\0') {						if (! isspace(*p)) {							ok = 1;						}						p++;					}				}				if (ok) {					viewonly_passwd = strdup(line);				} else {					rfbLog("*** not setting"					    " viewonly password to the 2nd"					    " line of %s. (blank or other"					    " problem)\n", passwdfile);				}			}		} else {			rfbLog("cannot read a line from passwdfile: %s\n",			    passwdfile);			exit(1);		}		fclose(in);	}	if (vpw_loc > 0) {		char *p = argv[vpw_loc];				while (*p != '\0') {			*p++ = '\0';		}		if (vpw_loc+1 < argc) {			p = argv[vpw_loc+1];					while (*p != '\0') {				*p++ = '\0';			}		}	} 	if (viewonly_passwd && pw_loc < 0) {		rfbLog("-passwd must be supplied when using -viewpasswd\n");		exit(1);	}	/* fixup settings that do not make sense */			if (use_threads && nofb && cursor_pos_updates) {		if (! quiet) {			rfbLog("disabling -threads under -nofb -cursorpos\n");		}		use_threads = 0;	}	if (tile_fuzz < 1) {		tile_fuzz = 1;	}	if (waitms < 0) {		waitms = 0;	}	if (inetd) {		shared = 0;		connect_once = 1;		bg = 0;	}	if (flip_byte_order && using_shm && ! quiet) {		rfbLog("warning: -flipbyte order only works with -noshm\n");	}	/* increase rfbwait if threaded */	if (use_threads && ! got_rfbwait) {		argv_vnc[argc_vnc++] = "-rfbwait";		argv_vnc[argc_vnc++] = "604800000"; /* one week... */	}	/* cursor shapes setup */#ifdef SOLARIS	if (show_cursor && ! overlay && overlay_cursor &&	    !strcmp(multiple_cursors_mode, "most")) {		overlay = 1;		if (! quiet) {			rfbLog("enabling -overlay mode to achieve "			    "'-cursor most'\n");			rfbLog("disable with: -overlay_nocursor.\n");		}	}#endif	if (overlay) {#ifdef SOLARIS		using_shm = 0;		if (flash_cmap && ! quiet) {			rfbLog("warning: -flashcmap may be incompatible "			    "with -overlay\n");		}				if (show_cursor && overlay_cursor) {			char *s = multiple_cursors_mode;			if (*s == 'X' || !strcmp(s, "some")) {				/*				 * user wants these modes, so disable fb cursor				 */				overlay_cursor = 0;			} else {				/*				 * "default" and "most", we turn off				 * show_cursor since it will automatically				 * be in the framebuffer.				 */				show_cursor = 0;			}		}#else		if (! quiet) {			rfbLog("disabling -overlay: only available on "			    "Solaris Xsun.\n");		}		overlay = 0; #endif	}	if (show_cursor) {		char *s = multiple_cursors_mode;		if (*s == 'X' || !strcmp(s, "some")) {			show_multiple_cursors = 1;		} else if (!strcmp(s, "most")) {			/* later check for XFIXES, for now "some" */			show_multiple_cursors = 1;		}	}	/* no framebuffer (Win2VNC) mode */	if (nofb) {		/* disable things that do not make sense */		using_shm = 0;		flash_cmap = 0;		show_cursor = 0;		show_multiple_cursors = 0;		overlay = 0;		if (! quiet) {			rfbLog("disabling -cursor, fb, shm, etc. in "			   "-nofb mode.\n");		}		if (! got_deferupdate && ! got_defer) {			/* reduce defer time under -nofb */			defer_update = defer_update_nofb;		}	}	/* check for OS with small shm limits */	if (using_shm && ! single_copytile) {		if (limit_shm()) {			single_copytile = 1;		}	}	if (! got_deferupdate) {		char tmp[40];		/* XXX not working yet in libvncserver */		sprintf(tmp, "%d", defer_update);		argv_vnc[argc_vnc++] = "-deferupdate";		argv_vnc[argc_vnc++] = strdup(tmp);	}	if (debug_pointer || debug_keyboard) {		if (bg || quiet) {			rfbLog("disabling -bg/-q under -debug_pointer"			    "/-debug_keyboard\n");			bg = 0;			quiet = 0;		}	}	if (! quiet) {		fprintf(stderr, "\n");		fprintf(stderr, "Settings:\n");		fprintf(stderr, " display:    %s\n", use_dpy ? use_dpy                    : "null");		fprintf(stderr, " subwin:     0x%x\n", subwin);		fprintf(stderr, " flashcmap:  %d\n", flash_cmap);		fprintf(stderr, " force_idx:  %d\n", force_indexed_color);		fprintf(stderr, " scaling:    %d %.5f\n", scaling, scale_fac);		fprintf(stderr, " visual:     %s\n", visual_str ? visual_str                    : "null");		fprintf(stderr, " overlay:    %d\n", overlay);		fprintf(stderr, " ovl_cursor: %d\n", overlay_cursor);		fprintf(stderr, " viewonly:   %d\n", view_only);		fprintf(stderr, " shared:     %d\n", shared);		fprintf(stderr, " conn_once:  %d\n", connect_once);		fprintf(stderr, " connect:    %s\n", client_connect		    ? client_connect : "null");		fprintf(stderr, " connectfile %s\n", client_connect_file		    ? client_connect_file : "null");		fprintf(stderr, " vnc_conn:   %d\n", vnc_connect);		fprintf(stderr, " authfile:   %s\n", auth_file ? auth_file                    : "null");		fprintf(stderr, " allow:      %s\n", allow_list ? allow_list                    : "null");		fprintf(stderr, " passfile:   %s\n", passwdfile ? passwdfile                    : "null");		fprintf(stderr, " accept:     %s\n", accept_cmd ? accept_cmd                    : "null");		fprintf(stderr, " gone:       %s\n", gone_cmd ? gone_cmd                    : "null");		fprintf(stderr, " inetd:      %d\n", inetd);		fprintf(stderr, " using_shm:  %d\n", using_shm);		fprintf(stderr, " flipbytes:  %d\n", flip_byte_order);		fprintf(stderr, " blackout:   %s\n", blackout_string		    ? blackout_string : "null");		fprintf(stderr, " xinerama:   %d\n", xinerama);		fprintf(stderr, " logfile:    %s\n", logfile ? logfile                    : "null");		fprintf(stderr, " bg:         %d\n", bg);		fprintf(stderr, " mod_tweak:  %d\n", use_modifier_tweak);		fprintf(stderr, " isolevel3:  %d\n", use_iso_level3);		fprintf(stderr, " xkb:        %d\n", use_xkb_modtweak);		fprintf(stderr, " skipkeys:   %s\n",		    skip_keycodes ? skip_keycodes : "null");		fprintf(stderr, " addkeysyms: %d\n", add_keysyms);		fprintf(stderr, " xkbcompat:  %d\n", xkbcompat);		fprintf(stderr, " clearmods:  %d\n", clear_mods);		fprintf(stderr, " remap:      %s\n", remap_file ? remap_file                    : "null");		fprintf(stderr, " nofb:       %d\n", nofb);		fprintf(stderr, " watchbell:  %d\n", watch_bell);		fprintf(stderr, " watchsel:   %d\n", watch_selection);		fprintf(stderr, " watchprim:  %d\n", watch_primary);		fprintf(stderr, " cursor:     %d\n", show_cursor);		fprintf(stderr, " root_curs:  %d\n", show_multiple_cursors);		fprintf(stderr, " curs_mode:  %s\n", multiple_cursors_mode		    ? multiple_cursors_mode : "null");		fprintf(stderr, " xwarpptr:   %d\n", use_xwarppointer);		fprintf(stderr, " cursorpos:  %d\n", cursor_pos_updates);		fprintf(stderr, " cursorshp:  %d\n", cursor_shape_updates);		fprintf(stderr, " buttonmap:  %s\n", pointer_remap		    ? pointer_remap : "null");		fprintf(stderr, " dragging:   %d\n", show_dragging);		fprintf(stderr, " old_ptr:    %d\n", old_pointer);		fprintf(stderr, " inputskip:  %d\n", ui_skip);		fprintf(stderr, " norepeat:   %d\n", no_autorepeat);		fprintf(stderr, " debug_ptr:  %d\n", debug_pointer);		fprintf(stderr, " debug_key:  %d\n", debug_keyboard);		fprintf(stderr, " defer:      %d\n", defer_update);		fprintf(stderr, " waitms:     %d\n", waitms);		fprintf(stderr, " take_naps:  %d\n", take_naps);		fprintf(stderr, " sigpipe:    %d\n", sigpipe);		fprintf(stderr, " threads:    %d\n", use_threads);		fprintf(stderr, " fs_frac:    %.2f\n", fs_frac);		fprintf(stderr, " onetile:    %d\n", single_copytile);		fprintf(stderr, " gaps_fill:  %d\n", gaps_fill);		fprintf(stderr, " grow_fill:  %d\n", grow_fill);		fprintf(stderr, " tile_fuzz:  %d\n", tile_fuzz);		fprintf(stderr, "\n");		rfbLog("x11vnc version: %s\n", lastmod);	} else {		rfbLogEnable(0);	}	/* open the X display: */	X_INIT;	if (auth_file) {		char *tmp;		int len = strlen("XAUTHORITY=") + strlen(auth_file) + 1;		tmp = (char *) malloc((size_t) len);		sprintf(tmp, "XAUTHORITY=%s", auth_file);		putenv(tmp);	}	if (watch_bell || use_xkb_modtweak) {		/* we need XKEYBOARD for these: */		use_xkb = 1;	}	if (xkbcompat) {		use_xkb = 0;	}#ifdef LIBVNCSERVER_HAVE_XKEYBOARD	/*	 * Disable XKEYBOARD before calling XOpenDisplay()	 * this should be used if there is ambiguity in the keymapping. 	 */	if (xkbcompat) {		Bool rc = XkbIgnoreExtension(True);		if (! quiet) {			rfbLog("disabling xkb extension. rc=%d\n", rc);		}		if (watch_bell) {			watch_bell = 0;			if (! quiet) rfbLog("disabling bell.\n");		}	}#else	use_xkb = 0;	watch_bell = 0;	use_xkb_modtweak = 0;#endif	if (use_dpy) {		dpy = XOpenDisplay(use_dpy);	} else if ( (use_dpy = getenv("DISPLAY")) ) {		dpy = XOpenDisplay(use_dpy);	} else {		dpy = XOpenDisplay("");	}	if (! dpy) {		rfbLog("XOpenDisplay failed (%s)\n", use_dpy ? use_dpy:"null");		exit(1);	} else if (use_dpy) {		if (! quiet) rfbLog("Using X display %s\n", use_dpy);	} else {		if (! quiet) rfbLog("Using default X display.\n");	}	scr = DefaultScreen(dpy);	rootwin = RootWindow(dpy, scr);	if (! dt) {		static char str[] = "-desktop";		argv_vnc[argc_vnc++] = str;		argv_vnc[argc_vnc++] = choose_title(use_dpy);	}	/* check for XTEST */	if (! XTestQueryExtension_wr(dpy, &ev, &er, &maj, &min)) {		rfbLog("warning: XTest extension not available, most user"		    " input\n");		rfbLog("(pointer and keyboard) will be discarded.\n");		xtest_present = 0;		rfbLog("no XTest extension, switching to -xwarppointer mode\n");		rfbLog("for pointer motion input.\n");		use_xwarppointer = 1;	}	/*	 * Window managers will often grab the display during resize, etc.	 * To avoid deadlock (our user resize input is not processed)	 * we tell the server to process our requests during all grabs:	 */	XTestGrabControl_wr(dpy, True);	/* check for MIT-SHM */	if (! nofb && ! XShmQueryExtension_wr(dpy)) {		if (! using_shm) {			if (! quiet) {				rfbLog("info: display does not support"				    " XShm.\n");			}		} else {			rfbLog("warning: XShm extension is not available.\n");			rfbLog("For best performance the X Display should be"			    " local. (i.e.\n");			rfbLog("the x11vnc and X server processes should be"			    " running on\n");			rfbLog("the same machine.)\n");#ifdef LIBVNCSERVER_HAVE_XSHM			rfbLog("Restart with -noshm to override this.\n");			exit(1);#else			rfbLog("Switching to -noshm mode.\n");			using_shm = 0;#endif		}	}	if (overlay) {	/* 	 * ideally we'd like to not have to cook up the visual variables	 * but rather let it all come out of XReadScreen(), however	 * there is no way to get a default visual out of it, so we	 * pretend -visual TrueColor:NN was supplied with NN usually 24.	 */#ifdef SOLARIS		char str[16];		XImage *xi;		Window twin = subwin ? subwin : rootwin;		xi = XReadScreen(dpy, twin, 0, 0, 8, 8, False);		sprintf(str, "TrueColor:%d", xi->depth);		if (xi->depth != 24 && ! quiet) {			fprintf(stderr, "warning XReadScreen() image has "			    "depth %d instead of 24.\n", xi->depth);		}		XDestroyImage(xi);		if (visual_str != NULL && ! quiet) {			fprintf(stderr, "warning: replacing '-visual %s' by "			    "'%s' for use with -overlay\n", visual_str, str);		}		visual_str = strdup(str);#endif	}	if (visual_str != NULL) {		set_visual(visual_str);	}#ifdef LIBVNCSERVER_HAVE_XKEYBOARD	/* check for XKEYBOARD */	if (use_xkb) {		initialize_xkb();	}	initialize_watch_bell();	if (!use_xkb && use_xkb_modtweak) {		if (! quiet) {			fprintf(stderr, "warning: disabling xkb modtweak."			    " XKEYBOARD ext. not present.\n");		}		use_xkb_modtweak = 0;	}#endif	/* set up parameters for subwin or non-subwin cases: */	if (! subwin) {		window = rootwin;		dpy_x = DisplayWidth(dpy, scr);		dpy_y = DisplayHeight(dpy, scr);		off_x = 0;		off_y = 0;		/* this may be overridden via visual_id below */		default_visual = DefaultVisual(dpy, scr);	} else {		/* experiment to share just one window */		XWindowAttributes attr;		window = (Window) subwin;		if ( ! XGetWindowAttributes(dpy, window, &attr) ) {			fprintf(stderr, "bad window: 0x%lx\n", window);			exit(1);		}		dpy_x = attr.width;		dpy_y = attr.height;		/* this may be overridden via visual_id below */		default_visual = attr.visual;		if (show_multiple_cursors) {			show_multiple_cursors = 0;			if (! quiet) {				fprintf(stderr, "disabling root cursor drawing"				    " for subwindow\n");			}		}		set_offset();	}	/* initialize depth to reasonable value, visual_id may override */	depth = DefaultDepth(dpy, scr);	if (visual_id) {		int n;		XVisualInfo vinfo_tmpl, *vinfo;		/*		 * we are in here from -visual or -overlay options		 * visual_id and visual_depth were set in set_visual().		 */		vinfo_tmpl.visualid = visual_id; 		vinfo = XGetVisualInfo(dpy, VisualIDMask, &vinfo_tmpl, &n);		if (vinfo == NULL || n == 0) {			fprintf(stderr, "could not match visual_id: 0x%x\n",			    (int) visual_id);			exit(1);		}		default_visual = vinfo->visual;		depth = vinfo->depth;		if (visual_depth) {			/* force it from -visual FooColor:NN */			depth = visual_depth;		}		if (! quiet) {			fprintf(stderr, "vis id:     0x%x\n",			    (int) vinfo->visualid);			fprintf(stderr, "vis scr:      %d\n", vinfo->screen);			fprintf(stderr, "vis depth     %d\n", vinfo->depth);			fprintf(stderr, "vis class     %d\n", vinfo->class);			fprintf(stderr, "vis rmask   0x%lx\n", vinfo->red_mask);			fprintf(stderr, "vis gmask   0x%lx\n", vinfo->green_mask);			fprintf(stderr, "vis bmask   0x%lx\n", vinfo->blue_mask);			fprintf(stderr, "vis cmap_sz   %d\n", vinfo->colormap_size);			fprintf(stderr, "vis b/rgb     %d\n", vinfo->bits_per_rgb);		}		XFree(vinfo);	}	if (! quiet) {		rfbLog("default visual ID: 0x%x\n",		    (int) XVisualIDFromVisual(default_visual));	}	if (nofb) {		/* 		 * For -nofb we do not allocate the framebuffer, so we		 * can save a few MB of memory. 		 */		fb = XCreateImage_wr(dpy, default_visual, depth, ZPixmap,		    0, NULL, dpy_x, dpy_y, BitmapPad(dpy), 0);	} else if (visual_id) {		/*		 * we need to call XCreateImage to supply the visual		 */		fb = XCreateImage_wr(dpy, default_visual, depth, ZPixmap,		    0, NULL, dpy_x, dpy_y, BitmapPad(dpy), 0);		fb->data = (char *) malloc(fb->bytes_per_line * fb->height);	} else {		fb = XGetImage_wr(dpy, window, 0, 0, dpy_x, dpy_y, AllPlanes,		    ZPixmap);		if (! quiet) {			rfbLog("Read initial data from X display into"			    " framebuffer.\n");		}	}	if (fb->bits_per_pixel == 24 && ! quiet) {		fprintf(stderr, "warning: 24 bpp may have poor"		    " performance.\n");	}	/*	 * n.b. we do not have to X_LOCK any X11 calls until watch_loop()	 * is called since we are single-threaded until then.	 */	initialize_screen(&argc_vnc, argv_vnc, fb);	initialize_tiles();	/* rectangular blackout regions */	if (blackout_string != NULL) {		initialize_blackout(blackout_string);	}	if (xinerama) {		initialize_xinerama();	}	if (blackouts) {		blackout_tiles();	}	initialize_shm();	/* also creates XImages when using_shm = 0 */	initialize_signals();	if (blackouts) {	/* blackout fb as needed. */		copy_screen();	}	if (use_modifier_tweak) {		initialize_modtweak();	}	if (remap_file != NULL) {		initialize_remap(remap_file);	}	initialize_pointer_map(pointer_remap);	clear_modifiers(1);	if (clear_mods == 1) {		clear_modifiers(0);	}	if (! inetd) {		if (! screen->port || screen->listenSock < 0) {			rfbLog("Error: could not obtain listening port.\n");			clean_up_exit(1);		}	}	if (! quiet) {		rfbLog("screen setup finished.\n");	}	if (screen->port) {		char *host = this_host();		int lport = screen->port;		if (host != NULL) {			/* note that vncviewer special cases 5900-5999 */			if (inetd) {				;	/* should not occur (port) */			} else if (quiet) {				if (lport >= 5900) {					fprintf(stderr, "The VNC desktop is "					    "%s:%d\n", host, lport - 5900);				} else {					fprintf(stderr, "The VNC desktop is "					    "%s:%d\n", host, lport);				}			} else if (lport >= 5900) {				rfbLog("The VNC desktop is %s:%d\n", host,				    lport - 5900);				if (lport >= 6000) {					rfbLog("possible aliases:  %s:%d, "					    "%s::%d\n", host, lport, host, lport);				}			} else {				rfbLog("The VNC desktop is %s:%d\n", host,				    lport);				rfbLog("possible alias:    %s::%d\n",				    host, lport);			}		}		fflush(stderr);			if (inetd) {			;	/* should not occur (port) */		} else {			fprintf(stdout, "PORT=%d\n", screen->port);		}		fflush(stdout);		}#if defined(LIBVNCSERVER_HAVE_FORK) && defined(LIBVNCSERVER_HAVE_SETSID)	if (bg) {		/* fork into the background now */		int p, n;		if ((p = fork()) > 0)  {			exit(0);		} else if (p == -1) {			fprintf(stderr, "could not fork\n");			perror("fork");			clean_up_exit(1);		}		if (setsid() == -1) {			fprintf(stderr, "setsid failed\n");			perror("setsid");			clean_up_exit(1);		}		/* adjust our stdio */		n = open("/dev/null", O_RDONLY);		dup2(n, 0);		dup2(n, 1);		if (! logfile) {			dup2(n, 2);		}		if (n > 2) {			close(n);		}	}#endif	watch_loop();	return(0);#undef argc#undef argv}