wok-undigest rev 469

fbvnc: remove screen size limit
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Sep 11 09:35:17 2011 +0200 (2011-09-11)
parents 20593bf0cf84
children 5c5dbcecf5c7
files fbvnc/stuff/fbvnc.u
line diff
     1.1 --- a/fbvnc/stuff/fbvnc.u	Sun Sep 11 00:14:04 2011 +0200
     1.2 +++ b/fbvnc/stuff/fbvnc.u	Sun Sep 11 09:35:17 2011 +0200
     1.3 @@ -86,13 +86,13 @@
     1.4   }
     1.5  --- fbvnc.c
     1.6  +++ fbvnc.c
     1.7 -@@ -36,10 +36,11 @@
     1.8 +@@ -36,13 +36,15 @@
     1.9   
    1.10   #define VNC_PORT		"5900"
    1.11   
    1.12  -#define MAXRES			(1 << 21)
    1.13  -#define MIN(a, b)		((a) < (b) ? (a) : (b))
    1.14 -+#define MAXRES			(1 << 14)
    1.15 ++#define MAXRES			(1 << 12)
    1.16   
    1.17   static int cols, rows;
    1.18  +static int srv_cols, srv_rows;
    1.19 @@ -100,12 +100,17 @@
    1.20   static int mr, mc;		/* mouse position */
    1.21   
    1.22   static char buf[MAXRES];
    1.23 -@@ -62,15 +63,22 @@
    1.24 ++#define MAXPIX		(MAXRES/sizeof(fbval_t))
    1.25 + 
    1.26 + static int vnc_connect(char *addr, char *port)
    1.27 + {
    1.28 +@@ -61,22 +63,28 @@
    1.29 + 
    1.30   	if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) == -1) {
    1.31   		close(fd);
    1.32 - 		freeaddrinfo(addrinfo);
    1.33 +-		freeaddrinfo(addrinfo);
    1.34  -		return -1;
    1.35 -+		return -2;
    1.36 ++		fd = -2;
    1.37   	}
    1.38   	freeaddrinfo(addrinfo);
    1.39   	return fd;
    1.40 @@ -115,15 +120,23 @@
    1.41  +static struct rgb_conv format;
    1.42   static int vnc_init(int fd)
    1.43   {
    1.44 +-	char vncver[] = "RFB 003.003\n";
    1.45  +	static struct { int bpp, fmt; } fmt[] = {
    1.46  +		{ 32, 0x0888 },  { 16, 0x0565 },  { 8, 0x0233 }, { -1, 0 }
    1.47  +	};
    1.48 - 	char vncver[] = "RFB 003.003\n";
    1.49 ++	char vncver[12];
    1.50  +	int i;
    1.51  +
    1.52   	struct vnc_client_init clientinit;
    1.53   	struct vnc_server_init serverinit;
    1.54   	struct vnc_client_pixelfmt pixfmt_cmd;
    1.55 + 	int connstat = VNC_CONN_FAILED;
    1.56 + 
    1.57 +-	write(fd, vncver, 12);
    1.58 ++	write(fd, "RFB 003.003\n", 12);
    1.59 + 	read(fd, vncver, 12);
    1.60 + 
    1.61 + 	read(fd, &connstat, sizeof(connstat));
    1.62  @@ -88,68 +96,71 @@
    1.63   	write(fd, &clientinit, sizeof(clientinit));
    1.64   	read(fd, &serverinit, sizeof(serverinit));
    1.65 @@ -219,64 +232,125 @@
    1.66  -		}
    1.67  -		fb_set(y + i, x, slice, w);
    1.68  +	fbval_t slice[MAXRES];
    1.69 -+	int j, k;
    1.70 ++	unsigned char *byte = (unsigned char *) slice;
    1.71 ++	int j;
    1.72  +	int mode = FBM_BPP(fb_mode());
    1.73 -+	for (j = k = 0; j < w; j++, k += mode, s += Bpp) {
    1.74 ++	for (j = 0; j < w; j++, byte += mode, s += Bpp) {
    1.75  +		fbval_t c = * (fbval_t *) s;
    1.76  +		int r = (c & format.rmax) << format.rskp;
    1.77  +		int g = ((c >> format.gshr) & format.gmax) << format.gskp;
    1.78  +		int b = ((c >> format.bshr) & format.bmax) << format.bskp;
    1.79 -+		unsigned char *byte = (unsigned char *) slice;
    1.80 -+		* (fbval_t *) (byte + k) = FB_VAL(r, g, b);
    1.81 ++		* (fbval_t *) byte = FB_VAL(r, g, b);
    1.82   	}
    1.83  +	fb_set(y, x, slice, w);
    1.84   }
    1.85   
    1.86   static void xread(int fd, void *buf, int len)
    1.87 -@@ -181,18 +192,37 @@
    1.88 - 		xread(fd, msg + 1, sizeof(*fbup) - 1);
    1.89 - 		n = ntohs(fbup->n);
    1.90 +@@ -159,54 +170,79 @@
    1.91 + 	while (nr < len && (n = read(fd, buf + nr, len - nr)) > 0)
    1.92 + 		nr += n;
    1.93 + 	if (nr < len) {
    1.94 +-		printf("partial vnc read!\n");
    1.95 +-		exit(1);
    1.96 ++		fprintf(stderr,"partial vnc read!\n");
    1.97 ++		exit(99);
    1.98 + 	}
    1.99 + }
   1.100 + 
   1.101 ++static void skip(int fd, int len)
   1.102 ++{
   1.103 ++	int n;
   1.104 ++	while (len > 0 && (n = read(fd, buf, MIN(len, sizeof(buf)))) > 0)
   1.105 ++		len -= n;
   1.106 ++}
   1.107 ++
   1.108 + static int vnc_event(int fd)
   1.109 + {
   1.110 + 	struct vnc_rect uprect;
   1.111 +-	char msg[1 << 12];
   1.112 +-	struct vnc_server_fbup *fbup = (void *) msg;
   1.113 +-	struct vnc_server_cuttext *cuttext = (void *) msg;
   1.114 +-	struct vnc_server_colormap *colormap = (void *) msg;
   1.115 +-	int j;
   1.116 +-	int n;
   1.117 ++	union {
   1.118 ++		struct vnc_server_fbup fbup;
   1.119 ++		struct vnc_server_cuttext cuttext;
   1.120 ++		struct vnc_server_colormap colormap;
   1.121 ++	} msg;
   1.122 ++	int j, n;
   1.123 + 
   1.124 +-	if (read(fd, msg, 1) != 1)
   1.125 ++	if (read(fd, &msg.fbup.type, 1) != 1)
   1.126 + 		return -1;
   1.127 +-	switch (msg[0]) {
   1.128 ++	switch (msg.fbup.type) {
   1.129 + 	case VNC_SERVER_FBUP:
   1.130 +-		xread(fd, msg + 1, sizeof(*fbup) - 1);
   1.131 +-		n = ntohs(fbup->n);
   1.132 ++		xread(fd, &msg.fbup.pad, sizeof(msg.fbup) - 1);
   1.133 ++		n = ntohs(msg.fbup.n);
   1.134   		for (j = 0; j < n; j++) {
   1.135  -			int x, y, w, h;
   1.136 -+			int x, y, w, h, l;
   1.137 -+			char *p;
   1.138 ++			int x, y, w, h, l, i;
   1.139   			xread(fd, &uprect, sizeof(uprect));
   1.140 ++			if (uprect.enc != 0) {
   1.141 ++				fprintf(stderr,"Encoding not RAW: %d\n",
   1.142 ++					ntohl(uprect.enc));
   1.143 ++				return -1;
   1.144 ++			}
   1.145   			x = ntohs(uprect.x);
   1.146   			y = ntohs(uprect.y);
   1.147   			w = ntohs(uprect.w);
   1.148   			h = ntohs(uprect.h);
   1.149  -			if (x >= cols || x + w > cols)
   1.150 -+			if (ntohl(uprect.enc) != 0) {
   1.151 -+				fprintf(stderr,"Encoding not RAW: %d\n",
   1.152 -+					ntohl(uprect.enc));
   1.153 - 				return -1;
   1.154 +-				return -1;
   1.155  -			if (y >= rows || y + h > rows)
   1.156 -+			}
   1.157 -+			if (w * Bpp > MAXRES) {
   1.158 -+				fprintf(stderr,"too much pixels: %dx%d, max %d\n",
   1.159 -+					w, Bpp, MAXRES);
   1.160 - 				return -1;
   1.161 +-				return -1;
   1.162  -			xread(fd, buf, w * h);
   1.163  -			drawfb(buf, x, y, w, h);
   1.164 -+			}
   1.165 -+			p = buf;
   1.166  +			x -= oc;
   1.167  +			y -= or;
   1.168 ++			i = 0;
   1.169  +			l = MIN(w, cols - x);
   1.170  +			if (x < 0) {
   1.171  +				l = MIN(w + x, cols);
   1.172 -+				p -= x * Bpp;
   1.173 ++				i = -x;
   1.174  +				x = 0;
   1.175  +			}
   1.176 -+			for (w *= Bpp; h--; y++) {
   1.177 -+				xread(fd, buf, w);
   1.178 -+				if (y >= 0 && y < rows && l > 0)
   1.179 -+					drawfb(p, x, y, l);
   1.180 ++			for (; h--; y++) {
   1.181 ++				int a, b, c = i;
   1.182 ++				for (a = b = 0; w > b; b += a, c = 0) {
   1.183 ++					a = MIN(w - b, MAXPIX);
   1.184 ++					xread(fd, buf, a * Bpp);
   1.185 ++					if (y >= 0 && y < rows && l > b)
   1.186 ++						drawfb(buf + (c * Bpp), x + b,
   1.187 ++						       y, MIN(a, l - b) - c);
   1.188 ++				}
   1.189  +			}
   1.190   		}
   1.191   		break;
   1.192   	case VNC_SERVER_BELL:
   1.193 -@@ -217,12 +247,31 @@
   1.194 + 		break;
   1.195 + 	case VNC_SERVER_CUTTEXT:
   1.196 +-		xread(fd, msg + 1, sizeof(*cuttext) - 1);
   1.197 +-		xread(fd, buf, ntohl(cuttext->len));
   1.198 ++		xread(fd, &msg.cuttext.pad1, sizeof(msg.cuttext) - 1);
   1.199 ++		skip(fd, ntohl(msg.cuttext.len));
   1.200 + 		break;
   1.201 + 	case VNC_SERVER_COLORMAP:
   1.202 +-		xread(fd, msg + 1, sizeof(*colormap) - 1);
   1.203 +-		xread(fd, buf, ntohs(colormap->n) * 3 * 2);
   1.204 ++		xread(fd, &msg.colormap.pad, sizeof(msg.colormap) - 1);
   1.205 ++		skip(fd, ntohs(msg.colormap.n) * 3 * 2);
   1.206 + 		break;
   1.207 + 	default:
   1.208 +-		fprintf(stderr, "unknown vnc msg: %d\n", msg[0]);
   1.209 ++		fprintf(stderr, "unknown vnc msg: %d\n", msg.fbup.type);
   1.210 + 		return -1;
   1.211 + 	}
   1.212 + 	return 0;
   1.213 +@@ -217,12 +253,31 @@
   1.214   	char ie[3];
   1.215   	struct vnc_client_ratevent me = {VNC_CLIENT_RATEVENT};
   1.216   	int mask = 0;
   1.217 @@ -310,7 +384,7 @@
   1.218   	if (ie[0] & 0x01)
   1.219   		mask |= VNC_BUTTON1_MASK;
   1.220   	if (ie[0] & 0x04)
   1.221 -@@ -233,6 +282,8 @@
   1.222 +@@ -233,6 +288,8 @@
   1.223   	me.x = htons(mc);
   1.224   	me.mask = mask;
   1.225   	write(fd, &me, sizeof(me));
   1.226 @@ -319,7 +393,7 @@
   1.227   	return 0;
   1.228   }
   1.229   
   1.230 -@@ -292,12 +343,11 @@
   1.231 +@@ -292,12 +349,11 @@
   1.232   			k = 0xff0d;
   1.233   			break;
   1.234   		case 0x0c:	/* ^L: redraw */
   1.235 @@ -334,7 +408,7 @@
   1.236   			mod[nmod++] = 0xffe1;
   1.237   		if (k >= 1 && k <= 26) {
   1.238   			k = 'a' + k - 1;
   1.239 -@@ -339,7 +389,7 @@
   1.240 +@@ -339,40 +395,42 @@
   1.241   	write(STDIN_FILENO, show, strlen(show));
   1.242   }
   1.243   
   1.244 @@ -343,9 +417,14 @@
   1.245   {
   1.246   	struct pollfd ufds[3];
   1.247   	int pending = 0;
   1.248 -@@ -350,29 +400,31 @@
   1.249 - 	ufds[1].events = POLLIN;
   1.250 + 	int err;
   1.251 + 	ufds[0].fd = kbd_fd;
   1.252 +-	ufds[0].events = POLLIN;
   1.253 + 	ufds[1].fd = vnc_fd;
   1.254 +-	ufds[1].events = POLLIN;
   1.255   	ufds[2].fd = rat_fd;
   1.256 ++	ufds[0].events =
   1.257 ++	ufds[1].events =
   1.258   	ufds[2].events = POLLIN;
   1.259  -	if (vnc_refresh(vnc_fd, 0))
   1.260  -		return;
   1.261 @@ -379,7 +458,7 @@
   1.262   }
   1.263   
   1.264   int main(int argc, char * argv[])
   1.265 -@@ -380,27 +432,38 @@
   1.266 +@@ -380,27 +438,38 @@
   1.267   	char *port = VNC_PORT;
   1.268   	char *host = "127.0.0.1";
   1.269   	struct termios ti;