win_utils.c 16.8 KB
Newer Older
1
/*
2
   Copyright (C) 2002-2010 Karl J. Runge <runge@karlrunge.com> 
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
   All rights reserved.

This file is part of x11vnc.

x11vnc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

x11vnc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with x11vnc; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
or see <http://www.gnu.org/licenses/>.

In addition, as a special exception, Karl J. Runge
gives permission to link the code of its release of x11vnc with the
OpenSSL project's "OpenSSL" library (or with modified versions of it
that use the same license as the "OpenSSL" library), and distribute
the linked executables.  You must obey the GNU General Public License
in all respects for all of the code used other than "OpenSSL".  If you
modify this file, you may extend this exception to your version of the
file, but you are not obligated to do so.  If you do not wish to do
so, delete this exception statement from your version.
*/

runge's avatar
runge committed
33 34 35 36 37 38
/* -- win_utils.c -- */

#include "x11vnc.h"
#include "xinerama.h"
#include "winattr_t.h"
#include "cleanup.h"
39
#include "xwrappers.h"
runge's avatar
runge committed
40
#include "connections.h"
41
#include "xrandr.h"
runge's avatar
runge committed
42
#include "macosx.h"
runge's avatar
runge committed
43 44 45 46 47 48 49 50 51 52

winattr_t *stack_list = NULL;
int stack_list_len = 0;
int stack_list_num = 0;


Window parent_window(Window win, char **name);
int valid_window(Window win, XWindowAttributes *attr_ret, int bequiet);
Bool xtranslate(Window src, Window dst, int src_x, int src_y, int *dst_x,
    int *dst_y, Window *child, int bequiet);
53
int get_window_size(Window win, int *w, int *h);
runge's avatar
runge committed
54
void snapshot_stack_list(int free_only, double allowed_age);
55 56
int get_boff(void);
int get_bwin(void);
runge's avatar
runge committed
57 58
void update_stack_list(void);
Window query_pointer(Window start);
59
unsigned int mask_state(void);
runge's avatar
runge committed
60 61
int pick_windowid(unsigned long *num);
Window descend_pointer(int depth, Window start, char *name_info, int len);
62
void id_cmd(char *cmd);
runge's avatar
runge committed
63 64 65


Window parent_window(Window win, char **name) {
66
#if !NO_X11
runge's avatar
runge committed
67 68
	Window r, parent;
	Window *list;
69
	XErrorHandler old_handler;
runge's avatar
runge committed
70
	unsigned int nchild;
71
	int rc;
72
#endif
runge's avatar
runge committed
73 74 75 76

	if (name != NULL) {
		*name = NULL;
	}
77
	RAWFB_RET(None)
78 79
#if NO_X11
	nox11_exit(1);
80
	if (!name || !win) {}
81 82
	return None;
#else
runge's avatar
runge committed
83

84 85
	old_handler = XSetErrorHandler(trap_xerror);
	trapped_xerror = 0;
runge's avatar
runge committed
86
	rc = XQueryTree_wr(dpy, win, &r, &parent, &list, &nchild);
87 88 89 90
	XSetErrorHandler(old_handler);

	if (! rc || trapped_xerror) {
		trapped_xerror = 0;
runge's avatar
runge committed
91 92
		return None;
	}
93 94
	trapped_xerror = 0;

runge's avatar
runge committed
95
	if (list) {
runge's avatar
runge committed
96
		XFree_wr(list);
runge's avatar
runge committed
97 98 99 100 101
	}
	if (parent && name) {
		XFetchName(dpy, parent, name);
	}
	return parent;
102
#endif	/* NO_X11 */
runge's avatar
runge committed
103 104 105 106 107
}

/* trapping utility to check for a valid window: */
int valid_window(Window win, XWindowAttributes *attr_ret, int bequiet) {
	XWindowAttributes attr, *pattr;
108 109
#if !NO_X11
	XErrorHandler old_handler;
runge's avatar
runge committed
110
	int ok = 0;
111
#endif
runge's avatar
runge committed
112 113 114 115 116 117 118 119 120 121

	if (attr_ret == NULL) {
		pattr = &attr;
	} else {
		pattr = attr_ret;
	}

	if (win == None) {
		return 0;
	}
122

runge's avatar
runge committed
123
#ifdef MACOSX
124
	if (macosx_console) {
runge's avatar
runge committed
125 126 127
		return macosx_valid_window(win, attr_ret);
	}
#endif
128

129
	RAWFB_RET(0)
130

131 132
#if NO_X11
	nox11_exit(1);
133
	if (!win || !attr_ret || !bequiet) {}
134 135
	return 0;
#else
runge's avatar
runge committed
136 137

	old_handler = XSetErrorHandler(trap_xerror);
138
	trapped_xerror = 0;
runge's avatar
runge committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152
	if (XGetWindowAttributes(dpy, win, pattr)) {
		ok = 1;
	}
	if (trapped_xerror && trapped_xerror_event) {
		if (! quiet && ! bequiet) {
			rfbLog("valid_window: trapped XError: %s (0x%lx)\n",
			    xerror_string(trapped_xerror_event), win);
		}
		ok = 0;
	}
	XSetErrorHandler(old_handler);
	trapped_xerror = 0;
	
	return ok;
153
#endif	/* NO_X11 */
runge's avatar
runge committed
154 155 156 157
}

Bool xtranslate(Window src, Window dst, int src_x, int src_y, int *dst_x,
    int *dst_y, Window *child, int bequiet) {
158
	XErrorHandler old_handler = NULL;
runge's avatar
runge committed
159 160
	Bool ok = False;

161
	RAWFB_RET(False)
162 163
#if NO_X11
	nox11_exit(1);
164 165
	if (!src || !dst || !src_x || !src_y || !dst_x || !dst_y || !child || !bequiet) {}
	if (!old_handler || !ok) {}
166 167
	return False;
#else
168

runge's avatar
runge committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	trapped_xerror = 0;
	old_handler = XSetErrorHandler(trap_xerror);
	if (XTranslateCoordinates(dpy, src, dst, src_x, src_y, dst_x,
	    dst_y, child)) {
		ok = True;
	}
	if (trapped_xerror && trapped_xerror_event) {
		if (! quiet && ! bequiet) {
			rfbLog("xtranslate: trapped XError: %s (0x%lx)\n",
			    xerror_string(trapped_xerror_event), src);
		}
		ok = False;
	}
	XSetErrorHandler(old_handler);
	trapped_xerror = 0;
	
	return ok;
186
#endif	/* NO_X11 */
runge's avatar
runge committed
187 188
}

189
int get_window_size(Window win, int *w, int *h) {
runge's avatar
runge committed
190 191 192
	XWindowAttributes attr;
	/* valid_window? */
	if (valid_window(win, &attr, 1)) {
193 194
		*w = attr.width;
		*h = attr.height;
runge's avatar
runge committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		return 1;
	} else {
		return 0;
	}
}

/*
 * For use in the -wireframe stuff, save the stacking order of the direct
 * children of the root window.  Ideally done before we send ButtonPress
 * to the X server.
 */
void snapshot_stack_list(int free_only, double allowed_age) {
	static double last_snap = 0.0, last_free = 0.0;
	double now; 
	int num, rc, i, j;
	unsigned int ui;
	Window r, w;
	Window *list;

	if (! stack_list) {
		stack_list = (winattr_t *) malloc(256*sizeof(winattr_t));
		stack_list_num = 0;
		stack_list_len = 256;
	}

	dtime0(&now);
	if (free_only) {
		/* we really don't free it, just reset to zero windows */
		stack_list_num = 0;
		last_free = now;
		return;
	}

	if (stack_list_num && now < last_snap + allowed_age) {
		return;
	}

	stack_list_num = 0;
	last_free = now;

235 236 237 238 239
#ifdef MACOSX
	if (! macosx_console) {
		RAWFB_RET_VOID
	}
#else
240
	RAWFB_RET_VOID
runge's avatar
runge committed
241
#endif
242

runge's avatar
runge committed
243
#if NO_X11 && !defined(MACOSX)
244
	num = rc = i = j = 0;	/* compiler warnings */
runge's avatar
runge committed
245 246 247
	ui = 0;
	r = w = None;
	list = NULL;
248 249
	return;
#else
250

runge's avatar
runge committed
251
	X_LOCK;
252
	/* no need to trap error since rootwin */
runge's avatar
runge committed
253
	rc = XQueryTree_wr(dpy, rootwin, &r, &w, &list, &ui);
runge's avatar
runge committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	num = (int) ui;

	if (! rc) {
		stack_list_num = 0;
		last_free = now;
		last_snap = 0.0;
		X_UNLOCK;
		return;
	}

	last_snap = now;
	if (num > stack_list_len + blackouts) {
		int n = 2*num;
		free(stack_list);
		stack_list = (winattr_t *) malloc(n*sizeof(winattr_t));
		stack_list_len = n;
	}
	j = 0;
	for (i=0; i<num; i++) {
		stack_list[j].win = list[i];
		stack_list[j].fetched = 0;
		stack_list[j].valid = 0;
		stack_list[j].time = now;
		j++;
	}
	for (i=0; i<blackouts; i++) {
280
		stack_list[j].win = get_boff() + 1;
runge's avatar
runge committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
		stack_list[j].fetched = 1;
		stack_list[j].valid = 1;
		stack_list[j].x = blackr[i].x1;
		stack_list[j].y = blackr[i].y1;
		stack_list[j].width  = blackr[i].x2 - blackr[i].x1;
		stack_list[j].height = blackr[i].y2 - blackr[i].y1;
		stack_list[j].time = now;
		stack_list[j].map_state = IsViewable;
		stack_list[j].rx = -1;
		stack_list[j].ry = -1;
		j++;

if (0) fprintf(stderr, "blackr: %d %dx%d+%d+%d\n", i,
	stack_list[j-1].width, stack_list[j-1].height,
	stack_list[j-1].x, stack_list[j-1].y);

	}
	stack_list_num = num + blackouts;
	if (debug_wireframe > 1) {
		fprintf(stderr, "snapshot_stack_list: num=%d len=%d\n",
		    stack_list_num, stack_list_len);
	}

runge's avatar
runge committed
304
	XFree_wr(list);
runge's avatar
runge committed
305
	X_UNLOCK;
306
#endif	/* NO_X11 */
runge's avatar
runge committed
307 308
}

309 310 311 312 313 314 315 316 317 318 319 320
int get_boff(void) {
	if (macosx_console) {
		return 0x1000000;
	} else {
		return 0;		
	}
}

int get_bwin(void) {
	return 10;		
}

runge's avatar
runge committed
321 322 323 324
void update_stack_list(void) {
	int k;
	double now;
	XWindowAttributes attr;
325
	int boff, bwin;
runge's avatar
runge committed
326 327 328 329 330 331 332 333 334

	if (! stack_list) {
		return;
	}
	if (! stack_list_num) {
		return;
	}

	dtime0(&now);
335 336 337

	boff = get_boff();
	bwin = get_bwin();
runge's avatar
runge committed
338
	
339
	X_LOCK;
runge's avatar
runge committed
340 341
	for (k=0; k < stack_list_num; k++) {
		Window win = stack_list[k].win;
342
		if (win != None && boff <= (int) win && (int) win < boff + bwin) {
runge's avatar
runge committed
343 344 345 346 347 348 349 350 351
			;	/* special, blackout */
		} else if (!valid_window(win, &attr, 1)) {
			stack_list[k].valid = 0;
		} else {
			stack_list[k].valid = 1;
			stack_list[k].x = attr.x;
			stack_list[k].y = attr.y;
			stack_list[k].width = attr.width;
			stack_list[k].height = attr.height;
352
			stack_list[k].border_width = attr.border_width;
runge's avatar
runge committed
353 354 355 356 357 358 359 360 361 362 363 364
			stack_list[k].depth = attr.depth;
			stack_list[k].class = attr.class;
			stack_list[k].backing_store = attr.backing_store;
			stack_list[k].map_state = attr.map_state;

			/* root_x, root_y not used for stack_list usage: */
			stack_list[k].rx = -1;
			stack_list[k].ry = -1;
		}
		stack_list[k].fetched = 1;
		stack_list[k].time = now;
	}
365
	X_UNLOCK;
runge's avatar
runge committed
366 367 368 369
if (0) fprintf(stderr, "update_stack_list[%d]: %.4f  %.4f\n", stack_list_num, now - x11vnc_start, dtime(&now));
}

Window query_pointer(Window start) {
370 371
	int rx, ry;
#if !NO_X11
372
	Window r, c;	/* compiler warnings */
373
	int wx, wy;
runge's avatar
runge committed
374
	unsigned int mask;
375
#endif
376 377 378

#ifdef MACOSX
	if (macosx_console) {
379
		macosx_get_cursor_pos(&rx, &ry);
380 381 382
	}
#endif

383
	RAWFB_RET(None)
384

385
#if NO_X11
runge's avatar
runge committed
386
	if (!start) { rx = ry = 0; }
387 388
	return None;
#else
runge's avatar
runge committed
389 390 391
	if (start == None) {
		start = rootwin;
	}
runge's avatar
runge committed
392
	if (XQueryPointer_wr(dpy, start, &r, &c, &rx, &ry, &wx, &wy, &mask)) {
runge's avatar
runge committed
393 394 395 396
		return c;
	} else {
		return None;
	}
397
#endif	/* NO_X11 */
runge's avatar
runge committed
398 399
}

400
unsigned int mask_state(void) {
401 402 403 404
#if NO_X11
	RAWFB_RET(0)
	return 0;
#else
405 406 407 408 409 410
	Window r, c;	
	int rx, ry, wx, wy;
	unsigned int mask;

	RAWFB_RET(0)

runge's avatar
runge committed
411
	if (XQueryPointer_wr(dpy, rootwin, &r, &c, &rx, &ry, &wx, &wy, &mask)) {
412 413 414 415
		return mask;
	} else {
		return 0;
	}
416
#endif	/* NO_X11 */
417 418
}

runge's avatar
runge committed
419 420 421 422 423
int pick_windowid(unsigned long *num) {
	char line[512];
	int ok = 0, n = 0, msec = 10, secmax = 15;
	FILE *p;

424 425
	RAWFB_RET(0)

runge's avatar
runge committed
426 427 428
	if (use_dpy) {
		set_env("DISPLAY", use_dpy);
	}
429
	/* id */
430
	if (no_external_cmds || !cmd_ok("id")) {
runge's avatar
runge committed
431 432 433 434 435 436
		rfbLogEnable(1);
		rfbLog("cannot run external commands in -nocmds mode:\n");
		rfbLog("   \"%s\"\n", "xwininfo");
		rfbLog("   exiting.\n");
		clean_up_exit(1);
	}
437
	close_exec_fds();
runge's avatar
runge committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	p = popen("xwininfo", "r");

	if (! p) {
		return 0;
	}

	fprintf(stderr, "\n");
	fprintf(stderr, "  Please select the window for x11vnc to poll\n");
	fprintf(stderr, "  by clicking the mouse in that window.\n");
	fprintf(stderr, "\n");

	while (msec * n++ < 1000 * secmax) {
		unsigned long tmp;
		char *q;
		fd_set set;
		struct timeval tv;

		if (screen && screen->clientHead) {
			/* they may be doing the pointer-pick thru vnc: */
			int nfds;
			tv.tv_sec = 0;
			tv.tv_usec = msec * 1000;
			FD_ZERO(&set);
			FD_SET(fileno(p), &set);

			nfds = select(fileno(p)+1, &set, NULL, NULL, &tv);
			
			if (nfds == 0 || nfds < 0) {
				/* 
				 * select timedout or error.
				 * note this rfbPE takes about 30ms too:
				 */
				rfbPE(-1);
471
				XFlush_wr(dpy);
runge's avatar
runge committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
				continue;
			}
		}
		
		if (fgets(line, 512, p) == NULL) {
			break;
		}
		q = strstr(line, " id: 0x"); 
		if (q) {
			q += 5;
			if (sscanf(q, "0x%lx ", &tmp) == 1) {
				ok = 1;
				*num = tmp;
				fprintf(stderr, "  Picked: 0x%lx\n\n", tmp);
				break;
			}
		}
	}
	pclose(p);
	return ok;
}

Window descend_pointer(int depth, Window start, char *name_info, int len) {
495 496 497 498 499
#if NO_X11
	RAWFB_RET(None)
	if (!depth || !start || !name_info || !len) {}
	return None;
#else
500
	Window r, c, clast = None;
runge's avatar
runge committed
501 502 503 504 505 506 507 508 509
	int i, rx, ry, wx, wy;
	int written = 0, filled = 0;
	char *store = NULL;
	unsigned int m;
	static XClassHint *classhint = NULL;
	static char *nm_cache = NULL;
	static int nm_cache_len = 0;
	static Window prev_start = None;

510 511
	RAWFB_RET(None)

runge's avatar
runge committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	if (! classhint) {
		classhint = XAllocClassHint();
	}

	if (! nm_cache) {
		nm_cache = (char *) malloc(1024);
		nm_cache_len = 1024;
		nm_cache[0] = '\0';
	}
	if (name_info && nm_cache_len < len) {
		if (nm_cache) {
			free(nm_cache);
		}
		nm_cache_len = 2*len;
		nm_cache = (char *) malloc(nm_cache_len);
	}

	if (name_info) {
		if (start != None && start == prev_start) {
			store = NULL;
			strncpy(name_info, nm_cache, len);
		} else {
			store = name_info;
			name_info[0] = '\0';
		}
	}

	if (start != None) {
		c = start;
		if (name_info) {
			prev_start = start;
		}
	} else {
		c = rootwin;	
	}

	for (i=0; i<depth; i++) {
		clast = c;
		if (store && ! filled) {
			char *name;
			if (XFetchName(dpy, clast, &name) && name != NULL) {
				int l = strlen(name);
				if (written + l+2 < len) {
					strcat(store, "^^");
					written += 2;
					strcat(store, name);
					written += l;
				} else {
					filled = 1;
				}
runge's avatar
runge committed
562
				XFree_wr(name);
runge's avatar
runge committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
			}
		}
		if (store && classhint && ! filled) {
			classhint->res_name = NULL;
			classhint->res_class = NULL;
			if (XGetClassHint(dpy, clast, classhint)) {
				int l = 0;
				if (classhint->res_class) {
					l += strlen(classhint->res_class); 
				}
				if (classhint->res_name) {
					l += strlen(classhint->res_name); 
				}
				if (written + l+4 < len) {
					strcat(store, "##");
					if (classhint->res_class) {
						strcat(store,
						    classhint->res_class);
					}
					strcat(store, "++");
					if (classhint->res_name) {
						strcat(store,
						    classhint->res_name);
					}
					written += l+4;
				} else {
					filled = 1;
				}
				if (classhint->res_class) {
runge's avatar
runge committed
592
					XFree_wr(classhint->res_class);
runge's avatar
runge committed
593 594
				}
				if (classhint->res_name) {
runge's avatar
runge committed
595
					XFree_wr(classhint->res_name);
runge's avatar
runge committed
596 597 598
				}
			}
		}
runge's avatar
runge committed
599
		if (! XQueryPointer_wr(dpy, c, &r, &c, &rx, &ry, &wx, &wy, &m)) {
runge's avatar
runge committed
600 601 602 603 604 605 606 607 608 609 610
			break;
		}
		if (! c) {
			break;
		}
	}
	if (start != None && name_info) {
		strncpy(nm_cache, name_info, nm_cache_len);
	}

	return clast;
611
#endif	/* NO_X11 */
runge's avatar
runge committed
612 613
}

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
void id_cmd(char *cmd) {
	int rc, dx = 0, dy = 0, dw = 0, dh = 0;
	int x0, y0, w0, h0;
	int x, y, w, h, do_move = 0, do_resize = 0;
	int disp_x = DisplayWidth(dpy, scr);
	int disp_y = DisplayHeight(dpy, scr);
	Window win = subwin;
	XWindowAttributes attr;
	XErrorHandler old_handler = NULL;
	Window twin;

	if (!cmd || !strcmp(cmd, "")) { 
		return;
	}
	if (strstr(cmd, "win=") == cmd) {
		if (! scan_hexdec(cmd + strlen("win="), &win)) {
			rfbLog("id_cmd: incorrect win= hex/dec number: %s\n", cmd);
			return;
		} else {
			char *q = strchr(cmd, ':');
			if (!q) {
				rfbLog("id_cmd: incorrect win=...: hex/dec number: %s\n", cmd);
				return;
			}
			rfbLog("id_cmd:%s set window id to 0x%lx\n", cmd, win);
			cmd = q+1;
		}
	}
	if (!win) {
		rfbLog("id_cmd:%s not in sub-window mode or no win=0xNNNN.\n", cmd);
		return;
	}
#if !NO_X11
	X_LOCK;
	if (!valid_window(win, &attr, 1)) {
		X_UNLOCK;
		return;
	}
	w0 = w = attr.width;
	h0 = h = attr.height;
	old_handler = XSetErrorHandler(trap_xerror);
	trapped_xerror = 0;
	XTranslateCoordinates(dpy, win, rootwin, 0, 0, &x, &y, &twin);
	x0 = x;
	y0 = y;
	if (strstr(cmd, "move:") == cmd) {
		if (sscanf(cmd, "move:%d%d", &dx, &dy) == 2) {
			x = x + dx;
			y = y + dy;
			do_move = 1;
		}
	} else if (strstr(cmd, "resize:") == cmd) {
		if (sscanf(cmd, "resize:%d%d", &dw, &dh) == 2) {
			w = w + dw;
			h = h + dh;
			do_move = 1;
			do_resize = 1;
		}
	} else if (strstr(cmd, "geom:") == cmd) {
		if (parse_geom(cmd+strlen("geom:"), &w, &h, &x, &y, disp_x, disp_y)) {
			do_move = 1;
			do_resize = 1;
			if (w <= 0) {
				w = w0;
			}
			if (h <= 0) {
				h = h0;
			}
			if (scaling && getenv("X11VNC_APPSHARE_ACTIVE")) {
				x /= scale_fac_x;
				y /= scale_fac_y;
			}
		}
	} else if (!strcmp(cmd, "raise")) {
		rc = XRaiseWindow(dpy, win);
		rfbLog("id_cmd:%s rc=%d\n", cmd, rc);
	} else if (!strcmp(cmd, "lower")) {
		rc = XLowerWindow(dpy, win);
		rfbLog("id_cmd:%s rc=%d\n", cmd, rc);
	} else if (!strcmp(cmd, "map")) {
		rc= XMapRaised(dpy, win);
		rfbLog("id_cmd:%s rc=%d\n", cmd, rc);
	} else if (!strcmp(cmd, "unmap")) {
		rc= XUnmapWindow(dpy, win);
		rfbLog("id_cmd:%s rc=%d\n", cmd, rc);
	} else if (!strcmp(cmd, "iconify")) {
		rc= XIconifyWindow(dpy, win, scr);
		rfbLog("id_cmd:%s rc=%d\n", cmd, rc);
	} else if (strstr(cmd, "wm_name:") == cmd) {
		rc= XStoreName(dpy, win, cmd+strlen("wm_name:"));
		rfbLog("id_cmd:%s rc=%d\n", cmd, rc);
	} else if (strstr(cmd, "icon_name:") == cmd) {
		rc= XSetIconName(dpy, win, cmd+strlen("icon_name:"));
		rfbLog("id_cmd:%s rc=%d\n", cmd, rc);
	} else if (!strcmp(cmd, "wm_delete")) {
		XClientMessageEvent ev;
		memset(&ev, 0, sizeof(ev));
		ev.type = ClientMessage;
		ev.send_event = True;
		ev.display = dpy;
		ev.window = win;
		ev.message_type = XInternAtom(dpy, "WM_PROTOCOLS", False);
		ev.format = 32;
		ev.data.l[0] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
		rc = XSendEvent(dpy, win, False, 0, (XEvent *) &ev);
		rfbLog("id_cmd:%s rc=%d\n", cmd, rc);
	} else {
		rfbLog("id_cmd:%s unrecognized command.\n", cmd);
	}
	if (do_move || do_resize) {
		if (w >= disp_x) {
			w = disp_x - 4;
		}
		if (h >= disp_y) {
			h = disp_y - 4;
		}
		if (w < 1) {
			w = 1;
		}
		if (h < 1) {
			h = 1;
		}
		if (x + w > disp_x) {
			x = disp_x - w - 1;
		}
		if (y + h > disp_y) {
			y = disp_y - h - 1;
		}
		if (x < 0) {
			x = 1;
		}
		if (y < 0) {
			y = 1;
		}
		rc = 0;
		rc += XMoveWindow(dpy, win, x, y);
		off_x = x;
		off_y = y;

		rc += XResizeWindow(dpy, win, w, h);

		rfbLog("id_cmd:%s rc=%d dx=%d dy=%d dw=%d dh=%d %dx%d+%d+%d -> %dx%d+%d+%d\n",
		    cmd, rc, dx, dy, dw, dh, w0, h0, x0, y0, w, h, x, h);
	}
	XSync(dpy, False);
	XSetErrorHandler(old_handler);
	if (trapped_xerror) {
		rfbLog("id_cmd:%s trapped_xerror.\n", cmd);
	}
	trapped_xerror = 0;
	if (do_resize) {
		rfbLog("id_cmd:%s calling check_xrandr_event.\n", cmd);
		check_xrandr_event("id_cmd");
	}
	X_UNLOCK;
#endif
}