From c07091b3900ad1d66bb9665a892bf93c685d031a Mon Sep 17 00:00:00 2001
From: runge <runge>
Date: Tue, 15 Jan 2008 04:27:11 +0000
Subject: [PATCH] x11vnc: -ping option, fix memory corruption in copy_tiles
 after xrandr resize.

---
 x11vnc/ChangeLog     |  4 +++
 x11vnc/README        | 59 +++++++++++++++++++++++---------------------
 x11vnc/help.c        | 27 ++++++++++----------
 x11vnc/options.c     |  1 +
 x11vnc/options.h     |  1 +
 x11vnc/scan.c        | 21 +++++++++++++---
 x11vnc/x11vnc.1      | 25 +++++++++++--------
 x11vnc/x11vnc.c      |  7 ++++++
 x11vnc/x11vnc_defs.c |  2 +-
 9 files changed, 91 insertions(+), 56 deletions(-)

diff --git a/x11vnc/ChangeLog b/x11vnc/ChangeLog
index 4f91276..c1c302c 100644
--- a/x11vnc/ChangeLog
+++ b/x11vnc/ChangeLog
@@ -1,3 +1,7 @@
+2008-01-14  Karl Runge <runge@karlrunge.com>
+	* x11vnc: -ping option, fix memory corruption in copy_tiles
+	  after xrandr resize.
+
 2007-12-16  Karl Runge <runge@karlrunge.com>
 	* x11vnc: setup remote-ctrl file by default on macosx. improve
 	  tkx11vnc wrt attaching to existing server in icon/tray mode. 
diff --git a/x11vnc/README b/x11vnc/README
index ecf8780..9611d30 100644
--- a/x11vnc/README
+++ b/x11vnc/README
@@ -1,5 +1,5 @@
 
-x11vnc README file                         Date: Sun Dec 16 13:21:20 EST 2007
+x11vnc README file                         Date: Mon Jan 14 22:59:36 EST 2008
 
 The following information is taken from these URLs:
 
@@ -1978,8 +1978,8 @@ typedef unsigned int in_addr_t;
    [361]http://www.archlinux.org/ Nokia 770 (.deb)
    [362]http://mike.saunby.googlepages.com/x11vncfornokia7702 Sharp
    Zaurus [363]http://www.focv.com/ Debian: (.deb)
-   [364]http://packages.debian.org/x11vnc (N.B: old and unmaintained;
-   better to compile from source) Solaris: (pkg)
+   [364]http://packages.debian.org/x11vnc (N.B: often unmaintained; might
+   be better to compile from source) Solaris: (pkg)
    [365]http://www.sunfreeware.com/ (N.B: very old; better to compile
    from source) OpenBSD: (.tgz) [366]http://www.openbsd.org/ (N.B: very
    old and unmaintained; better to compile from source)
@@ -11842,7 +11842,7 @@ x11vnc: a VNC server for real X displays
    Here are all of x11vnc command line options:
 % x11vnc -opts      (see below for -help long descriptions)
 
-x11vnc: allow VNC connections to real X11 displays. 0.9.4 lastmod: 2007-12-16
+x11vnc: allow VNC connections to real X11 displays. 0.9.4 lastmod: 2008-01-14
 
 x11vnc options:
   -display disp            -auth file               -N                     
@@ -11910,24 +11910,24 @@ x11vnc options:
   -allinput                -speeds rd,bw,lat        -wmdt string           
   -debug_pointer           -debug_keyboard          -defer time            
   -wait time               -wait_ui factor          -nowait_bog            
-  -slow_fb time            -xrefresh time           -readtimeout n         
-  -nap                     -nonap                   -sb time               
-  -nofbpm                  -fbpm                    -nodpms                
-  -dpms                    -forcedpms               -clientdpms            
-  -noserverdpms            -noultraext              -noxdamage             
-  -xd_area A               -xd_mem f                -sigpipe string        
-  -threads                 -nothreads               -fs f                  
-  -gaps n                  -grow n                  -fuzz n                
-  -debug_tiles             -snapfb                  -rawfb string          
-  -freqtab file            -pipeinput cmd           -macnodim              
-  -macnosleep              -macnosaver              -macnowait             
-  -macwheel n              -macnoswap               -macnoresize           
-  -maciconanim n           -macmenu                 -gui [gui-opts]        
-  -remote command          -query variable          -QD variable           
-  -sync                    -noremote                -yesremote             
-  -unsafe                  -safer                   -privremote            
-  -nocmds                  -allowedcmds list        -deny_all              
-
+  -slow_fb time            -xrefresh time           -nap                   
+  -nonap                   -sb time                 -readtimeout n         
+  -ping n                  -nofbpm                  -fbpm                  
+  -nodpms                  -dpms                    -forcedpms             
+  -clientdpms              -noserverdpms            -noultraext            
+  -noxdamage               -xd_area A               -xd_mem f              
+  -sigpipe string          -threads                 -nothreads             
+  -fs f                    -gaps n                  -grow n                
+  -fuzz n                  -debug_tiles             -snapfb                
+  -rawfb string            -freqtab file            -pipeinput cmd         
+  -macnodim                -macnosleep              -macnosaver            
+  -macnowait               -macwheel n              -macnoswap             
+  -macnoresize             -maciconanim n           -macmenu               
+  -gui [gui-opts]          -remote command          -query variable        
+  -QD variable             -sync                    -noremote              
+  -yesremote               -unsafe                  -safer                 
+  -privremote              -nocmds                  -allowedcmds list      
+  -deny_all              
 
 libvncserver options:
 -rfbport port          TCP port for RFB protocol
@@ -11961,7 +11961,7 @@ libvncserver-tight-extension options:
 
 % x11vnc -help
 
-x11vnc: allow VNC connections to real X11 displays. 0.9.4 lastmod: 2007-12-16
+x11vnc: allow VNC connections to real X11 displays. 0.9.4 lastmod: 2008-01-14
 
 (type "x11vnc -opts" to just list the options.)
 
@@ -14956,16 +14956,19 @@ t
                        to repaint themselves.  Use this only if applications
                        misbehave by not repainting themselves properly.
                        See also -noxdamage.
--readtimeout n         Set libvncserver rfbMaxClientWait to n seconds. On
-                       slow links that take a long time to paint the first
-                       screen libvncserver may hit the timeout and drop the
-                       connection.  Default: 20 seconds.
 -nap                   Monitor activity and if it is low take longer naps
 -nonap                 between screen polls to really cut down load when idle.
                        Default: take naps
 -sb time               Time in seconds after NO activity (e.g. screen blank)
                        to really throttle down the screen polls (i.e. sleep
-                       for about 1.5 secs). Use 0 to disable.  Default: 60
+                       for about 1.5 secs). Use 0 to disable.  Default: 20
+
+-readtimeout n         Set libvncserver rfbMaxClientWait to n seconds. On
+                       slow links that take a long time to paint the first
+                       screen libvncserver may hit the timeout and drop the
+                       connection.  Default: 60 seconds.
+-ping n                Send a 1x1 framebuffer update to all clients every n
+                       seconds (e.g. to try to keep a network connection alive)
 
 -nofbpm                If the system supports the FBPM (Frame Buffer Power
 -fbpm                  Management) extension (i.e. some Sun systems), then
diff --git a/x11vnc/help.c b/x11vnc/help.c
index 96004b8..edacb02 100644
--- a/x11vnc/help.c
+++ b/x11vnc/help.c
@@ -774,7 +774,7 @@ void print_help(int mode) {
 "                       associated with it does apply as normal.\n"
 "\n"
 "                       There are also some utilities for testing password\n"
-"                       if [list] starts with the \"%\" character.  See the\n"
+"                       if [list] starts with the \"%%\" character.  See the\n"
 "                       quick_pw() function in the source for details.\n"
 "\n"
 "-unixpw_nis [list]     As -unixpw above, however do not use su(1) but rather\n"
@@ -2214,14 +2214,6 @@ void print_help(int mode) {
 "                       Keysym to the X server's keyboard mapping on an unused\n"
 "                       key.  Added Keysyms will be removed periodically and\n"
 "                       also when x11vnc exits.  Default: %s\n"
-#if 0
-"-xkbcompat             Ignore the XKEYBOARD extension.  Use as a workaround for\n"
-"                       some keyboard mapping problems.  E.g. if you are using\n"
-"                       an international keyboard (AltGr or ISO_Level3_Shift),\n"
-"                       and the OS or keyboard where the VNC viewer is run\n"
-"                       is not identical to that of the X server, and you are\n"
-"                       having problems typing some keys.  Implies -nobell.\n"
-#endif
 "-clear_mods            At startup and exit clear the modifier keys by sending\n"
 "                       KeyRelease for each one. The Lock modifiers are skipped.\n"
 "                       Used to clear the state if the display was accidentally\n"
@@ -3029,10 +3021,6 @@ void print_help(int mode) {
 "                       to repaint themselves.  Use this only if applications\n"
 "                       misbehave by not repainting themselves properly.\n"
 "                       See also -noxdamage.\n"
-"-readtimeout n         Set libvncserver rfbMaxClientWait to n seconds. On\n"
-"                       slow links that take a long time to paint the first\n"
-"                       screen libvncserver may hit the timeout and drop the\n"
-"                       connection.  Default: %d seconds.\n"
 "-nap                   Monitor activity and if it is low take longer naps\n"
 "-nonap                 between screen polls to really cut down load when idle.\n"
 "                       Default: %s\n"
@@ -3040,6 +3028,13 @@ void print_help(int mode) {
 "                       to really throttle down the screen polls (i.e. sleep\n"
 "                       for about 1.5 secs). Use 0 to disable.  Default: %d\n"
 "\n"
+"-readtimeout n         Set libvncserver rfbMaxClientWait to n seconds. On\n"
+"                       slow links that take a long time to paint the first\n"
+"                       screen libvncserver may hit the timeout and drop the\n"
+"                       connection.  Default: %d seconds.\n"
+"-ping n                Send a 1x1 framebuffer update to all clients every n\n"
+"                       seconds (e.g. to try to keep a network connection alive)\n"
+"\n"
 "-nofbpm                If the system supports the FBPM (Frame Buffer Power\n"
 "-fbpm                  Management) extension (i.e. some Sun systems), then\n"
 "                       prevent the video h/w from going into a reduced power\n"
@@ -4277,6 +4272,7 @@ void print_help(int mode) {
 		fprintf(stderr, "\n");
 		exit(1);
 	}
+#if 1
 	fprintf(stderr, help, lastmod,
 		POLL_8TO24_DELAY,
 		scaling_copyrect ? ":cr":":nocr",
@@ -4306,8 +4302,8 @@ void print_help(int mode) {
 		defer_update,
 		waitms,
 		wait_ui,
-		rfbMaxClientWait/1000,
 		take_naps ? "take naps":"no naps",
+		rfbMaxClientWait/1000,
 		screen_blank,
 		watch_fbpm ? "-nofbpm":"-fbpm",
 		watch_dpms ? "-nodpms":"-dpms",
@@ -4320,6 +4316,9 @@ void print_help(int mode) {
 		accept_remote_cmds ? "-yesremote":"-noremote",
 		""
 	);
+#else
+	fprintf(stderr, "%s", help);
+#endif
 
 	rfbUsage();
 #endif
diff --git a/x11vnc/options.c b/x11vnc/options.c
index c3dc295..d1a285d 100644
--- a/x11vnc/options.c
+++ b/x11vnc/options.c
@@ -110,6 +110,7 @@ int inetd = 0;			/* spawned from inetd(8) */
 #endif
 int tightfilexfer = TIGHTFILEXFER; 
 int first_conn_timeout = 0;	/* -timeout */
+int ping_interval = 0;		/* -ping */
 int flash_cmap = 0;		/* follow installed colormaps */
 int shift_cmap = 0;		/* ncells < 256 and needs shift of pixel values */
 int force_indexed_color = 0;	/* whether to force indexed color for 8bpp */
diff --git a/x11vnc/options.h b/x11vnc/options.h
index 560c184..1bfd664 100644
--- a/x11vnc/options.h
+++ b/x11vnc/options.h
@@ -85,6 +85,7 @@ extern int begin_viewonly;
 extern int inetd;
 extern int tightfilexfer; 
 extern int first_conn_timeout;
+extern int ping_interval;
 extern int flash_cmap;
 extern int shift_cmap;
 extern int force_indexed_color;
diff --git a/x11vnc/scan.c b/x11vnc/scan.c
index 5dbefe9..23fd32d 100644
--- a/x11vnc/scan.c
+++ b/x11vnc/scan.c
@@ -1659,8 +1659,8 @@ static void mark_hint(hint_t hint) {
  * devices are optimized for write, not read, so we are limited by the
  * read bandwidth, sometimes only 5 MB/sec on otherwise fast hardware.
  */
-static int *first_line = NULL, *last_line;
-static unsigned short *left_diff, *right_diff;
+static int *first_line = NULL, *last_line = NULL;
+static unsigned short *left_diff = NULL, *right_diff = NULL;
 
 static int copy_tiles(int tx, int ty, int nt) {
 	int x, y, line;
@@ -1670,14 +1670,23 @@ static int copy_tiles(int tx, int ty, int nt) {
 	int pixelsize = bpp/8;
 	int first_min, last_max;
 	int first_x = -1, last_x = -1;
+	static int prev_ntiles_x = -1;
 
 	char *src, *dst, *s_src, *s_dst, *m_src, *m_dst;
 	char *h_src, *h_dst;
 	if (unixpw_in_progress) return 0;
 
-	if (! first_line) {
+	if (ntiles_x != prev_ntiles_x && first_line != NULL) {
+		free(first_line);	first_line = NULL;
+		free(last_line);	last_line = NULL;
+		free(left_diff);	left_diff = NULL;
+		free(right_diff);	right_diff = NULL;
+	}
+
+	if (first_line == NULL) {
 		/* allocate arrays first time in. */
 		int n = ntiles_x + 1;
+		rfbLog("copy_tiles: allocating first_line at size %d\n", n);
 		first_line = (int *) malloc((size_t) (n * sizeof(int)));
 		last_line  = (int *) malloc((size_t) (n * sizeof(int)));
 		left_diff  = (unsigned short *)
@@ -1685,6 +1694,7 @@ static int copy_tiles(int tx, int ty, int nt) {
 		right_diff = (unsigned short *)
 			malloc((size_t) (n * sizeof(unsigned short)));
 	}
+	prev_ntiles_x = ntiles_x;
 
 	x = tx * tile_x;
 	y = ty * tile_y;
@@ -3354,6 +3364,11 @@ if (tile_count) fprintf(stderr, "XX copytile: %.4f  tile_count: %d\n", dnow() -
 	} else if (use_openssl && !tile_diffs) {
 		ping_clients(0);
 	}
+	/* -ping option: */
+	if (ping_interval) {
+		int td = ping_interval > 0 ? ping_interval : -ping_interval;
+		ping_clients(-td);
+	}
 
 
 	nap_check(tile_diffs);
diff --git a/x11vnc/x11vnc.1 b/x11vnc/x11vnc.1
index 171e5a6..0da57fd 100644
--- a/x11vnc/x11vnc.1
+++ b/x11vnc/x11vnc.1
@@ -1,8 +1,8 @@
 .\" This file was automatically generated from x11vnc -help output.
-.TH X11VNC "1" "December 2007" "x11vnc " "User Commands"
+.TH X11VNC "1" "January 2008" "x11vnc " "User Commands"
 .SH NAME
 x11vnc - allow VNC connections to real X11 displays
-         version: 0.9.4, lastmod: 2007-12-16
+         version: 0.9.4, lastmod: 2008-01-14
 .SH SYNOPSIS
 .B x11vnc
 [OPTION]...
@@ -3546,13 +3546,6 @@ to repaint themselves.  Use this only if applications
 misbehave by not repainting themselves properly.
 See also \fB-noxdamage.\fR
 .PP
-\fB-readtimeout\fR \fIn\fR
-.IP
-Set libvncserver rfbMaxClientWait to n seconds. On
-slow links that take a long time to paint the first
-screen libvncserver may hit the timeout and drop the
-connection.  Default: 20 seconds.
-.PP
 \fB-nap,\fR \fB-nonap\fR
 .IP
 Monitor activity and if it is low take longer naps
@@ -3563,7 +3556,19 @@ Default: take naps
 .IP
 Time in seconds after NO activity (e.g. screen blank)
 to really throttle down the screen polls (i.e. sleep
-for about 1.5 secs). Use 0 to disable.  Default: 60
+for about 1.5 secs). Use 0 to disable.  Default: 20
+.PP
+\fB-readtimeout\fR \fIn\fR
+.IP
+Set libvncserver rfbMaxClientWait to n seconds. On
+slow links that take a long time to paint the first
+screen libvncserver may hit the timeout and drop the
+connection.  Default: 60 seconds.
+.PP
+\fB-ping\fR \fIn\fR
+.IP
+Send a 1x1 framebuffer update to all clients every n
+seconds (e.g. to try to keep a network connection alive)
 .PP
 \fB-nofbpm,\fR \fB-fbpm\fR
 .IP
diff --git a/x11vnc/x11vnc.c b/x11vnc/x11vnc.c
index df2c11b..e5b0589 100644
--- a/x11vnc/x11vnc.c
+++ b/x11vnc/x11vnc.c
@@ -1873,6 +1873,7 @@ static void print_settings(int try_http, int bg, char *gui_str) {
 	fprintf(stderr, " shared:     %d\n", shared);
 	fprintf(stderr, " conn_once:  %d\n", connect_once);
 	fprintf(stderr, " timeout:    %d\n", first_conn_timeout);
+	fprintf(stderr, " ping:       %d\n", ping_interval);
 	fprintf(stderr, " inetd:      %d\n", inetd);
 	fprintf(stderr, " tightfilexfer:   %d\n", tightfilexfer);
 	fprintf(stderr, " http:       %d\n", try_http);
@@ -2265,6 +2266,9 @@ char msg2[] =
 	if (raw_fb_str && !macosx_console) {
 		return;
 	}
+	if (quiet) {
+		return;
+	}
 	if (nofb) {
 		return;
 	}
@@ -3221,6 +3225,9 @@ int main(int argc, char* argv[]) {
 		} else if (!strcmp(arg, "-readtimeout")) {
 			CHECK_ARGC
 			rfbMaxClientWait = atoi(argv[++i]) * 1000;
+		} else if (!strcmp(arg, "-ping")) {
+			CHECK_ARGC
+			ping_interval = atoi(argv[++i]);
 		} else if (!strcmp(arg, "-nap")) {
 			take_naps = 1;
 		} else if (!strcmp(arg, "-nonap")) {
diff --git a/x11vnc/x11vnc_defs.c b/x11vnc/x11vnc_defs.c
index a3d53c0..a54be39 100644
--- a/x11vnc/x11vnc_defs.c
+++ b/x11vnc/x11vnc_defs.c
@@ -15,7 +15,7 @@ int xtrap_base_event_type = 0;
 int xdamage_base_event_type = 0;
 
 /*               date +'lastmod: %Y-%m-%d' */
-char lastmod[] = "0.9.4 lastmod: 2007-12-16";
+char lastmod[] = "0.9.4 lastmod: 2008-01-14";
 
 /* X display info */
 
-- 
2.18.1