8to24.c 43.9 KB
Newer Older
1 2 3 4 5 6
/* -- 8to24.c -- */
#include "x11vnc.h"
#include "cleanup.h"
#include "scan.h"
#include "util.h"
#include "win_utils.h"
7
#include "xwrappers.h"
8 9

int multivis_count = 0;
10
int multivis_24count = 0;
11 12 13

void check_for_multivis(void);
void bpp8to24(int, int, int, int);
14
void mark_8bpp(int);
15

16 17 18 19 20 21 22
#if SKIP_8TO24
void check_for_multivis(void) {}
void bpp8to24(int x, int y, int z, int t) {}
void mark_8bpp(int x) {}
#else
/* lots... */

23
static void set_root_cmap(void);
24
static int check_pointer_in_depth24(void);
25
static void parse_cmap8to24(void);
26
static void set_poll_fb(void);
27
static int check_depth(Window win, Window top, int doall);
28
static int check_depth_win(Window win, Window top, XWindowAttributes *attr);
29 30 31 32 33 34 35
static XImage *p_xi(XImage *xi, Visual *visual, int win_depth, int *w);
static int poll_line(int x1, int x2, int y1, int n, sraRegionPtr mod);
static void poll_line_complement(int x1, int x2, int y1, sraRegionPtr mod);
static int poll_8bpp(sraRegionPtr, int);
static void poll_8bpp_complement(sraRegionPtr);
static void mark_rgn_rects(sraRegionPtr mod);
static int get_8bpp_regions(int validate);
36
static int get_cmap(int j, Colormap cmap);
37
static void do_8bpp_region(int n, sraRegionPtr mark);
38
static XImage *cmap_xi(XImage *xi, Window win, int win_depth);
39
static void transform_rect(sraRect rect, Window win, int win_depth, int cm);
40 41 42 43 44 45 46 47 48 49 50 51

/* struct for keeping info about the 8bpp windows: */
typedef struct window8 {
	Window win;
	Window top;
	int depth;
	int x, y;
	int w, h;
	int map_state;
	Colormap cmap;
	Bool map_installed;
	int fetched;
52
	double last_fetched;
53
	sraRegionPtr clip_region;
54 55
} window8bpp_t;

56 57 58 59 60 61
enum mark_8bpp_modes {
	MARK_8BPP_ALL = 0,
	MARK_8BPP_POINTER,
	MARK_8BPP_TOP
};

62 63
#define NCOLOR 256

64
static Colormap root_cmap = 0;
65
static unsigned int root_rgb[NCOLOR];
66

67
static void set_root_cmap(void) {
68
#if NO_X11
69
	RAWFB_RET_VOID
70 71
	return;
#else
72
	static time_t last_set = 0;
73
	time_t now = time(NULL);
74
	XWindowAttributes attr;
75 76
	static XColor color[NCOLOR];
	int redo = 0;
77

78 79
	RAWFB_RET_VOID

80 81
	if (now > last_set + 10) {
		redo = 1;
82
	}
83
	if (! root_cmap || redo) {
84
		X_LOCK;
85 86 87 88 89 90 91 92 93 94
		if (! valid_window(window, &attr, 1)) {
			X_UNLOCK;
			return;
		}
		if (attr.colormap) {
			int i, ncells = NCOLOR;
			for (i=0; i < ncells; i++) {
				color[i].pixel = i;
				color[i].pad = 0;
			}
95 96
			last_set = now;
			root_cmap = attr.colormap;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
			XQueryColors(dpy, root_cmap, color, ncells);
			for (i=0; i < ncells; i++) {
				unsigned int red, green, blue;
				/* strip out highest 8 bits of values: */
				red   = (color[i].red   & 0xff00) >> 8;
				green = (color[i].green & 0xff00) >> 8;
				blue  = (color[i].blue  & 0xff00) >> 8;

				/*
				 * the maxes should be at 255 already,
				 * but just in case...
				 */
				red   = (main_red_max   * red  )/255;
				green = (main_green_max * green)/255;
				blue  = (main_blue_max  * blue )/255;

				/* shift them over and or together for value */
				red   = red    << main_red_shift;
				green = green  << main_green_shift;
				blue  = blue   << main_blue_shift;

				/* store it in the array to be used later */
				root_rgb[i] = red | green | blue;
			}
121
		}
122
		X_UNLOCK;
123
	}
124
#endif	/* NO_X11 */
125 126
}

127
/* fixed size array.  Will primarily hold visible 8bpp windows */
128 129 130 131
#define MAX_8BPP_WINDOWS 64
static window8bpp_t windows_8bpp[MAX_8BPP_WINDOWS];

static int db24 = 0;
132 133
static int xgetimage_8to24 = 1;
static double poll_8to24_delay = POLL_8TO24_DELAY;
134 135
static double cache_win = 0.0;
static int level2_8to24 = 0;
136

137
static int check_pointer_in_depth24(void) {
138 139 140 141 142 143 144
	int tries = 0, in_24 = 0;
	XWindowAttributes attr;
	Window c, w;
	double now = dnow();

	c = window;

145 146
	RAWFB_RET(0)

147
	if (now > last_keyboard_time + 1.0 && now > last_pointer_time + 1.0) {
148
		return 0;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	}

	X_LOCK;
	while (c && tries++ < 3) {
		c = query_pointer(c);
		if (valid_window(c, &attr, 1)) 	{
			if (attr.depth == 24) {
				in_24 = 1;
				break;
			}
		}
	}
	X_UNLOCK;
	if (in_24) {
		int x1, y1, x2, y2;
		X_LOCK;
		xtranslate(c, window, 0, 0, &x1, &y1, &w, 1);
		X_UNLOCK;
		x2 = x1 + attr.width;
		y2 = y1 + attr.height;
		x1 = nfix(x1, dpy_x);
		y1 = nfix(y1, dpy_y);
171 172
		x2 = nfix(x2, dpy_x+1);
		y2 = nfix(y2, dpy_y+1);
173
		mark_rect_as_modified(x1, y1, x2, y2, 0);
174 175 176 177

if (db24 > 1) fprintf(stderr, "check_pointer_in_depth24 %d %d %d %d\n", x1, y1, x2, y2);

		return 1;
178
	}
179
	return 0;
180
}
181

182 183 184 185
static void parse_cmap8to24(void) {
	if (cmap8to24_str) {
		char *p, *str = strdup(cmap8to24_str);
		p = strtok(str, ",");
186
		/* defaults: */
187
		db24 = 0;
188 189
		xgetimage_8to24 = 1;
		poll_8to24_delay = POLL_8TO24_DELAY;
190 191
		level2_8to24 = 0;
		cache_win = 0.0;
192 193 194
		while (p) {
			if (strstr(p, "dbg=") == p) {
				db24 = atoi(p + strlen("dbg="));
195 196
			} else if (strstr(p, "poll=") == p) {
				poll_8to24_delay = atof(p + strlen("poll="));
197 198
			} else if (strstr(p, "cachewin=") == p) {
				cache_win = atof(p + strlen("cachewin="));
199 200
			} else if (!strcmp(p, "nogetimage")) {
				xgetimage_8to24 = 0;
201 202
			} else if (!strcmp(p, "level2")) {
				level2_8to24 = 1;
203 204 205 206 207 208 209 210
			}
			p = strtok(NULL, ",");
		}
		free(str);
	} else {
		if (getenv("DEBUG_8TO24") != NULL) {
			db24 = atoi(getenv("DEBUG_8TO24"));
		}
211 212
		if (getenv("NOXGETIMAGE_8TO24") != NULL) {
			xgetimage_8to24 = 0;
213
		}
214 215 216 217 218 219 220 221 222 223 224
	}
}

static char *poll8_fb = NULL, *poll24_fb = NULL;
static int poll8_fb_w = 0, poll8_fb_h = 0;
static int poll24_fb_w = 0, poll24_fb_h = 0;

static void pfb(int fac, char **fb, int *w, int *h)  {
	if (! *fb || *w != dpy_x || *h != dpy_y) {
		if (*fb) {
			free(*fb);
225
		}
226 227 228
		*fb = (char *) calloc(fac * dpy_x * dpy_y, 1);
		*w = dpy_x;
		*h = dpy_y;
229 230 231
	}
}

232 233 234 235 236 237 238 239
static void set_poll_fb(void) {
	/* create polling framebuffers or recreate if too small. */

	if (! xgetimage_8to24) {
		return;		/* this saves a bit of RAM */
	}
	pfb(4, &poll24_fb, &poll24_fb_w, &poll24_fb_h);
	pfb(1, &poll8_fb,  &poll8_fb_w,  &poll8_fb_h);
240 241
}

242 243 244 245 246
int MV_glob = 0;
int MV_count;
int MV_hit;
double MV_start;

247
void check_for_multivis(void) {
248
#if NO_X11
249
	RAWFB_RET_VOID
250 251
	return;
#else
252 253 254 255
	XWindowAttributes attr;
	int doall = 0;
	int k, i, cnt, diff;
	static int first = 1;
256 257
	static Window *stack_old = NULL;
	static int stack_old_len = 0;
258
	static double last_parse = 0.0;
259 260
	static double last_update = 0.0;
	static double last_clear = 0.0;
261
	static double last_poll = 0.0;
262 263 264
	static double last_fixup = 0.0;
	static double last_call = 0.0;
	static double last_query = 0.0;
265
	double now = dnow();
266
	double delay;
267

268 269
	RAWFB_RET_VOID

270
	if (now > last_parse + 1.0) {
271 272 273 274 275
		last_parse = now;
		parse_cmap8to24();
	}
if (db24 > 2) fprintf(stderr, " check_for_multivis: %.4f\n", now - last_call);
	last_call = now;
276 277 278 279 280 281 282 283 284 285

	if (first) {
		int i;
		/* initialize 8bpp window table: */
		for (i=0; i < MAX_8BPP_WINDOWS; i++) 	{
			windows_8bpp[i].win = None;
			windows_8bpp[i].top = None;
			windows_8bpp[i].map_state = IsUnmapped;
			windows_8bpp[i].cmap = (Colormap) 0;
			windows_8bpp[i].fetched = 0;
286
			windows_8bpp[i].last_fetched = -1.0;
287
			windows_8bpp[i].clip_region = NULL;
288
		}
289 290
		set_poll_fb();

291 292 293
		first = 0;
		doall = 1;	/* fetch everything first time */
	}
294

295 296 297 298
	if (wireframe_in_progress) {
		return;
	}

299
	set_root_cmap();
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

	/*
	 * allocate an "old stack" list of all toplevels.  we compare
	 * this to the current stack to guess stacking order changes.
	 */
	if (!stack_old || stack_old_len < stack_list_len) {
		int n = stack_list_len;
		if (n < 256) {
			n = 256;
		}
		if (stack_old) {
			free(stack_old);
		}
		stack_old = (Window *) malloc(n*sizeof(Window));
		stack_old_len = n;
	}

317
	/* fill the old stack with visible windows: */
318 319 320 321 322 323 324 325 326
	cnt = 0;
	for (k=0; k < stack_list_num; k++) {
		if (stack_list[k].valid &&
		    stack_list[k].map_state == IsViewable) {
			stack_old[cnt++] = stack_list[k].win;
		}
	}

	/* snapshot + update the current stacking order: */
327 328 329 330 331 332 333
	/* TUNABLE */
	if (poll_8to24_delay >= POLL_8TO24_DELAY) {
		delay = 3.0 * poll_8to24_delay;
	} else {
		delay = 3.0 * POLL_8TO24_DELAY;	/* 0.15 */
	}
	if (doall || now > last_update + delay) {
334
		snapshot_stack_list(0, 0.0);
335 336 337 338
		update_stack_list();
		last_update = now;
	}

339
	/* look for differences in the visible toplevels: */
340 341 342
	diff = 0;
	cnt = 0;
	for (k=0; k < stack_list_num; k++) {
343 344
		if (stack_list[k].valid && stack_list[k].map_state ==
		    IsViewable) {
345 346 347 348 349 350 351 352 353
			if (stack_old[cnt] != stack_list[k].win) {
				diff = 1;
				break;
			}
			cnt++;
		}
	}

	multivis_count = 0;
354
	multivis_24count = 0;
355 356 357 358 359 360 361 362 363 364

	/*
	 * every 10 seconds we try to clean out and also refresh the window
	 * info in the the 8bpp window table:
	 */
	if (now > last_clear + 10) {
		last_clear = now;
		X_LOCK;
		for (i=0; i < MAX_8BPP_WINDOWS; i++) {
			Window w = windows_8bpp[i].win;
365
			if (! valid_window(w, &attr, 1)) {
366 367 368 369 370 371
				/* catch windows that went away: */
				windows_8bpp[i].win = None;
				windows_8bpp[i].top = None;
				windows_8bpp[i].map_state = IsUnmapped;
				windows_8bpp[i].cmap = (Colormap) 0;
				windows_8bpp[i].fetched = 0;
372
				windows_8bpp[i].last_fetched = -1.0;
373 374 375 376 377
			}
		}
		X_UNLOCK;
	}

378 379 380 381 382 383
	MV_count = 0;
	MV_hit = 0;
	MV_start = dnow();

	set_root_cmap();

384
	/* loop over all toplevels, both 8 and 24 depths: */
385 386 387

	X_LOCK;	/* a giant lock around the whole activity */

388 389 390 391 392 393
	for (k=0; k < stack_list_num; k++) {
		Window r, parent;
		Window *list0;
		Status rc;
		unsigned int nc0;
		int i1;
394 395
		XErrorHandler old_handler;
		double delay;
396 397 398

		Window win = stack_list[k].win;

399 400 401 402 403 404 405 406
		/* TUNABLE */
		if (poll_8to24_delay >= POLL_8TO24_DELAY) {
			delay = 1.5 * poll_8to24_delay;
		} else {
			delay = 1.5 * POLL_8TO24_DELAY;	/* 0.075 */
		}

		if (now < last_query + delay) {
407 408 409
			break;
		}

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
		if (win == None) {
			continue;
		}

		if (stack_list[k].map_state != IsViewable) {
			int i;
			/*
			 * if the toplevel became unmapped, mark it
			 * for the children as well...
			 */
			for (i=0; i < MAX_8BPP_WINDOWS; i++) {
				if (windows_8bpp[i].top == win) {
					windows_8bpp[i].map_state =
					    stack_list[k].map_state;
				}
			}
		}

		if (check_depth(win, win, doall)) {
			/*
430
			 * returns 1 if no need to recurse down e.g. It
431 432 433 434 435 436
			 * is 8bpp and we assume all lower one are too.
			 */
			continue;
		}

		/* we recurse up to two levels down from stack_list windows */
437 438 439

		old_handler = XSetErrorHandler(trap_xerror);
		trapped_xerror = 0;
runge's avatar
runge committed
440
		rc = XQueryTree_wr(dpy, win, &r, &parent, &list0, &nc0);
441 442 443 444
		XSetErrorHandler(old_handler);

		if (! rc || trapped_xerror) {
			trapped_xerror = 0;
445 446
			continue;
		}
447
		trapped_xerror = 0;
448 449 450 451 452 453 454 455 456 457 458

		/* loop over grandchildren of rootwin: */
		for (i1=0; i1 < (int) nc0; i1++) {
			Window win1 = list0[i1];
			Window *list1;
			unsigned int nc1;
			int i2;

			if (check_depth(win1, win, doall)) {
				continue;
			}
459 460 461 462 463 464 465

			if (level2_8to24) {
				continue;
			}

			old_handler = XSetErrorHandler(trap_xerror);
			trapped_xerror = 0;
runge's avatar
runge committed
466
			rc = XQueryTree_wr(dpy, win1, &r, &parent, &list1, &nc1);
467 468 469 470
			XSetErrorHandler(old_handler);

			if (! rc || trapped_xerror) {
				trapped_xerror = 0;
471 472
				continue;
			}
473 474
			trapped_xerror = 0;

475 476 477 478 479 480 481 482 483 484
			/* loop over great-grandchildren of rootwin: */
			for (i2=0; i2< (int) nc1; i2++) {
				Window win2 = list1[i2];

				if (check_depth(win2, win, doall)) {
					continue;
				}
				/* more? Which wm does this? */
			}
			if (nc1) {
runge's avatar
runge committed
485
				XFree_wr(list1);
486 487 488
			}
		}
		if (nc0) {
runge's avatar
runge committed
489
			XFree_wr(list0);
490 491
		}
	}
492 493
	X_UNLOCK;

494 495
	last_query = dnow();

496 497 498
MV_glob += MV_count;
if (0) fprintf(stderr, "MV_count: %d hit: %d %.4f  %10.2f\n", MV_count, MV_hit, last_query - MV_start, MV_glob / (last_query - x11vnc_start));

499 500 501 502 503 504 505 506 507 508 509 510 511 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
	if (screen_fixup_8 > 0.0 && now > last_fixup + screen_fixup_8) {
		last_fixup = now;
		mark_8bpp(MARK_8BPP_ALL);
		last_poll = now;

	} else if (poll_8to24_delay > 0.0) {
		int area = -1;
		int validate = 0;

		if (diff && multivis_count) {
			validate = 1;
		}
		if (now > last_poll + poll_8to24_delay) {
			sraRegionPtr mod;

			last_poll = now;
			mod = sraRgnCreate();
			area = poll_8bpp(mod, validate);
			if (depth == 24) {
				poll_8bpp_complement(mod);
			}
			mark_rgn_rects(mod);
			sraRgnDestroy(mod);
		}
		if (0 && area < dpy_x * dpy_y / 2 && diff && multivis_count) {
			mark_8bpp(MARK_8BPP_POINTER);
			last_poll = now;
		}

	} else if (diff && multivis_count) {
		mark_8bpp(MARK_8BPP_ALL);
		last_poll = now;

	} else if (depth == 8 && multivis_24count) {
		static double last_check = 0.0;
		if (now > last_check + 0.4) {
			last_check = now;
			if (check_pointer_in_depth24()) {
				last_poll = now;
			}
		}
	}
541
if (0) fprintf(stderr, "done: %.4f\n", dnow() - last_query);
542
#endif	/* NO_X11 */
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
}

#define VW_CACHE_MAX 1024
static XWindowAttributes vw_cache_attr[VW_CACHE_MAX];
static Window vw_cache_win[VW_CACHE_MAX];

static void set_attr(XWindowAttributes *attr, int j) {
	memcpy((void *) (vw_cache_attr+j), (void *) attr,
	    sizeof(XWindowAttributes));
}
#if 0
static int get_attr(XWindowAttributes *attr, int j) {
	memcpy((void *) attr, (void *) (vw_cache_attr+j),
	    sizeof(XWindowAttributes));
	return 1;
}
#endif

static XWindowAttributes wattr;

static XWindowAttributes *vw_lookup(Window win) {
	static double last_purge = 0.0;
	double now;
	int i, j, k;

	if (win == None) {
		return NULL;
	}

	now = dnow();
	if (now > last_purge + cache_win) {
		last_purge = now;
		for (i=0; i<VW_CACHE_MAX; i++) {
			vw_cache_win[i] = None;
		}
	}

	j = -1;
	k = -1;
	for (i=0; i<VW_CACHE_MAX; i++) {
		if (vw_cache_win[i] == win) {
			j = i;
			break;
		} else if (vw_cache_win[i] == None) {
			k = i;
			break;
		}
	}

	if (j >= 0) {
MV_hit++;
		return vw_cache_attr+j;
		
	} else if (k >= 0) {
		XWindowAttributes attr2;
		int rc = valid_window(win, &attr2, 1);
		if (rc) {
			vw_cache_win[k] = win;
			set_attr(&attr2, k);
			return vw_cache_attr+k;
		} else {
			return NULL;
		}
	} else {
		/* Full */
		int rc = valid_window(win, &wattr, 1);
		if (rc) {
			return &wattr;
		} else {
			return NULL;
		}
	}
615 616 617
}

static int check_depth(Window win, Window top, int doall) {
618
	XWindowAttributes attr, *pattr;
619 620

	/* first see if it is (still) a valid window: */
621 622 623 624 625 626 627 628 629 630 631 632
MV_count++;

	if (cache_win > 0.0) {
		pattr = vw_lookup(win);
		if (pattr == NULL) {
			return 1;	/* indicate done */
		}
	} else {
		if (! valid_window(win, &attr, 1)) {
			return 1;	/* indicate done */
		}
		pattr = &attr;
633
	}
634 635

	if (! doall && pattr->map_state != IsViewable) {
636
		/*
637 638 639
		 * store results anyway...  this may lead to table
		 * filling up, but currently this allows us to update
		 * state of onetime mapped windows.
640
		 */
641
		check_depth_win(win, top, pattr);
642
		return 1;	/* indicate done */
643
	} else if (check_depth_win(win, top, pattr)) {
644 645 646 647 648 649
		return 1;	/* indicate done */
	} else {
		return 0;	/* indicate not done */
	}
}

650
static int check_depth_win(Window win, Window top, XWindowAttributes *attr) {
651
	int store_it = 0;
652
	/*
653 654
	 * only store windows with depth not equal to the default visual's
	 * depth note some windows can have depth == 0 ... (skip them).
655
	 */
656 657
	if (attr->depth > 0) {
		if (depth == 24 && attr->depth != 24) {
658
			store_it = 1;
659
		} else if (depth == 8 && root_cmap && attr->colormap !=
660 661 662 663
		    root_cmap) {
			store_it = 1;
		}
	}
664

665
	if (store_it) {
666 667
		int i, j = -1, none = -1, nomap = -1;
		int new = 0;
668
		if (attr->map_state == IsViewable) {
669
			/* count the visible ones: */
670
			multivis_count++;
671
			if (attr->depth == 24) {
672 673
				multivis_24count++;
			}
674
if (db24 > 1) fprintf(stderr, "multivis: 0x%lx %d\n", win, attr->depth);
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		}

		/* try to find a table slot for this window: */
		for (i=0; i < MAX_8BPP_WINDOWS; i++) {
			if (none < 0 && windows_8bpp[i].win == None) {
				/* found first None */
				none = i;
			}
			if (windows_8bpp[i].win == win) {
				/* found myself */
				j = i;
				break;
			}
			if (nomap < 0 && windows_8bpp[i].win != None &&
			    windows_8bpp[i].map_state != IsViewable) {
				/* found first unmapped */
				nomap = i;
			}
		}
		if (j < 0) {
695
			if (attr->map_state != IsViewable) {
696
				/* no slot and not visible: not worth keeping */
697 698 699 700 701 702 703 704 705 706 707 708
				return 1;
			} else if (none >= 0) {
				/* put it in the first None slot */
				j = none;
				new = 1;
			} else if (nomap >=0) {
				/* put it in the first unmapped slot */
				j = nomap;
			}
			/* otherwise we cannot store it... */
		}

709
if (db24 > 1) fprintf(stderr, "multivis: 0x%lx ms: %d j: %d no: %d nm: %d dep=%d\n", win, attr->map_state, j, none, nomap, attr->depth);
710 711 712 713 714

		/* store if if we found a slot j: */
		if (j >= 0) {
			Window w;
			int x, y;
715
			int now_vis = 0;
716

717
			if (attr->map_state == IsViewable &&
718 719 720
			    windows_8bpp[j].map_state != IsViewable) {
				now_vis = 1;
			} 
721
if (db24 > 1) fprintf(stderr, "multivis: STORE 0x%lx j: %3d ms: %d dep=%d\n", win, j, attr->map_state, attr->depth);
722 723
			windows_8bpp[j].win = win;
			windows_8bpp[j].top = top;
724 725 726 727 728 729
			windows_8bpp[j].depth = attr->depth;
			windows_8bpp[j].map_state = attr->map_state;
			windows_8bpp[j].cmap = attr->colormap;
			windows_8bpp[j].map_installed = attr->map_installed;
			windows_8bpp[j].w = attr->width;
			windows_8bpp[j].h = attr->height;
730
			windows_8bpp[j].fetched = 1;
731
			windows_8bpp[j].last_fetched = dnow();
732 733 734 735 736 737

			/* translate x y to be WRT the root window (not parent) */
			xtranslate(win, window, 0, 0, &x, &y, &w, 1);
			windows_8bpp[j].x = x;
			windows_8bpp[j].y = y;

738 739
			if (new || now_vis) {
if (db24) fprintf(stderr, "new/now_vis: 0x%lx %d/%d\n", win, new, now_vis);
740
				/* mark it immediately if a new one: */
741 742 743 744
				X_UNLOCK;	/* dont forget the giant lock */
				mark_rect_as_modified(x, y, x + attr->width,
				    y + attr->height, 0);
				X_LOCK;
745 746 747 748 749 750
			}
		} else {
			/*
			 * Error: could not find a slot.
			 * perhaps keep age and expire old ones??
			 */
751
if (db24) fprintf(stderr, "multivis: CANNOT STORE 0x%lx j=%d\n", win, j);
752
			for (i=0; i < MAX_8BPP_WINDOWS; i++) {
753
if (db24 > 1) fprintf(stderr, "          ------------ 0x%lx i=%d\n", windows_8bpp[i].win, i);
754 755 756 757 758 759 760 761
			}
			
		}
		return 1;
	}
	return 0;
}

762
static XImage *p_xi(XImage *xi, Visual *visual, int win_depth, int *w) {
763 764
	RAWFB_RET(NULL)

765
#if NO_X11
766
	if (!xi || !visual || !win_depth || !w) {}
767 768
	return NULL;
#else
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	if (xi == NULL || *w < dpy_x) {
		char *d;
		if (xi) {
			XDestroyImage(xi);
		}
		if (win_depth == 8) {
			d = (char *) malloc(dpy_x * 1);
		} else {
			d = (char *) malloc(dpy_x * 4);
		}
		*w = dpy_x;
		xi = XCreateImage(dpy, visual, win_depth, ZPixmap, 0, d,
		    dpy_x, 1, 8, 0);
	}
	return xi;
784
#endif	/* NO_X11 */
785 786 787
}

static int poll_line(int x1, int x2, int y1, int n, sraRegionPtr mod) {
788
#if NO_X11
789
	RAWFB_RET(1)
790 791 792
	if (!x1 || !x2 || !y1 || !n || !mod) {}
	return 1;
#else
793 794 795 796 797 798 799 800 801 802 803 804
	int fac, n_off, w, xo, yo;
	char *poll_fb, *dst, *src;
	int w2, xl, xh, stride = 32;
	int inrun = 0, rx1 = -1, rx2 = -1;

	static XImage *xi8 = NULL, *xi24 = NULL, *xi_r;
	static int xi8_w = 0, xi24_w = 0;

	XErrorHandler old_handler = NULL;
	XImage *xi;
	Window c, win = windows_8bpp[n].win;

805 806 807 808 809
	static XWindowAttributes attr;
	static Window last_win = None; 
	static double last_time = 0.0;
	double now;

810 811 812 813
	sraRegionPtr rect;
	int mx1, mx2, my1, my2;
	int ns = NSCAN/2;

814 815
	RAWFB_RET(1)

816 817 818 819 820 821 822 823 824 825 826
	if (win == None) {
		return 1;
	}
	if (windows_8bpp[n].map_state != IsViewable) {
		return 1;
	}
	if (! xgetimage_8to24) {
		return 1;
	}

	X_LOCK;
827 828 829 830 831 832 833 834 835 836 837
	now = dnow();
	if (last_win != None && win == last_win && now < last_time + 0.5) {
		;	/* use previous attr */
	} else {
		if (! valid_window(win, &attr, 1)) {
			X_UNLOCK;
			last_win = None;
			return 0;
		}
		last_time = now;
		last_win = win;
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
	}

	if (attr.depth != 8 && attr.depth != 24) {
		X_UNLOCK;
		return 1;
	} else if (attr.depth == 8) {
		xi = xi8 = p_xi(xi8, attr.visual, 8, &xi8_w);

		poll_fb = poll8_fb;
		fac = 1;
		n_off = poll8_fb_w * y1 + x1;
	} else {
		xi = xi24 = p_xi(xi24, attr.visual, 24, &xi24_w);

		poll_fb = poll24_fb;
		fac = 4;
		n_off = poll24_fb_w * y1 + x1;
	}

857 858 859 860 861 862
	old_handler = XSetErrorHandler(trap_xerror);
	trapped_xerror = 0;

	/* xtranslate() not used to save two XSetErrorHandler calls */
	XTranslateCoordinates(dpy, win, window, 0, 0, &xo, &yo, &c);

863 864 865 866
	xo = x1 - xo;
	yo = y1 - yo;
	w = x2 - x1;

867
	if (trapped_xerror || xo < 0 || yo < 0 || xo + w > attr.width) {
868
if (db24 > 2) fprintf(stderr, "avoid bad match...\n");
869 870
		XSetErrorHandler(old_handler);
		trapped_xerror = 0;
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
		X_UNLOCK;
		return 0;
	}

	trapped_xerror = 0;
	xi_r = XGetSubImage(dpy, win, xo, yo, w, 1, AllPlanes, ZPixmap, xi,
	    0, 0);
	XSetErrorHandler(old_handler);
	X_UNLOCK;

	if (! xi_r || trapped_xerror) {
		trapped_xerror = 0;
		return 0;
	}
	trapped_xerror = 0;

	src = xi->data;
	dst = poll_fb + fac * n_off;

	inrun = 0;

	xl = x1;
	while (xl < x2) {
		xh = xl + stride;
		if (xh > x2) {
			xh = x2;
		}
		w2 = xh - xl;
		if (memcmp(dst, src, fac * w2)) {
			if (inrun) {
				rx2 = xh;
			} else {
				rx1 = xl;
				rx2 = xh;
				inrun = 1;
			}
		} else {
			if (inrun) {
				mx1 = rx1;
				mx2 = rx2;
				my1 = nfix(y1 - ns, dpy_y);
				my2 = nfix(y1 + ns, dpy_y+1);

				rect = sraRgnCreateRect(mx1, my1, mx2, my2);
				sraRgnOr(mod, rect);
				sraRgnDestroy(rect);	
				inrun = 0;
			}
		}

		xl += stride;
		dst += fac * stride;
		src += fac * stride;
	}

	if (inrun) {
		mx1 = rx1;
		mx2 = rx2;
		my1 = nfix(y1 - ns, dpy_y);
		my2 = nfix(y1 + ns, dpy_y+1);

		rect = sraRgnCreateRect(mx1, my1, mx2, my2);
		sraRgnOr(mod, rect);
		sraRgnDestroy(rect);	
	}
	return 1;
937
#endif	/* NO_X11 */
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
}

static void poll_line_complement(int x1, int x2, int y1, sraRegionPtr mod) {
	int n_off, w, xl, xh, stride = 32;
	char *dst, *src;
	int inrun = 0, rx1 = -1, rx2 = -1;
	sraRegionPtr rect;
	int mx1, mx2, my1, my2;
	int ns = NSCAN/2;

	if (depth != 24) {
		return;
	}
	if (! cmap8to24_fb) {
		return;
	}
	if (! xgetimage_8to24) {
		return;
	}

	n_off = main_bytes_per_line * y1 + 4 * x1;

	src = main_fb + n_off;
	dst = cmap8to24_fb + n_off;

	inrun = 0;

	xl = x1;
	while (xl < x2) {
		xh = xl + stride;
		if (xh > x2) {
			xh = x2;
		}
		w = xh - xl;
		if (memcmp(dst, src, 4 * w)) {
			if (inrun) {
				rx2 = xh;
			} else {
				rx1 = xl;
				rx2 = xh;
				inrun = 1;
			}
		} else {
			if (inrun) {
				mx1 = rx1;
				mx2 = rx2;
				my1 = nfix(y1 - ns, dpy_y);
				my2 = nfix(y1 + ns, dpy_y+1);

				rect = sraRgnCreateRect(mx1, my1, mx2, my2);
				sraRgnOr(mod, rect);
				sraRgnDestroy(rect);	

				inrun = 0;
			}
		}

		xl += stride;
		dst += 4 * stride;
		src += 4 * stride;
	}

	if (inrun) {
		mx1 = rx1;
		mx2 = rx2;
		my1 = nfix(y1 - ns, dpy_y);
		my2 = nfix(y1 + ns, dpy_y+1);

		rect = sraRgnCreateRect(mx1, my1, mx2, my2);
		sraRgnOr(mod, rect);
		sraRgnDestroy(rect);	

		inrun = 0;
	}
}

1014
#define CMAPMAX 64
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
static Colormap cmaps[CMAPMAX];
static int ncmaps;

static int poll_8bpp(sraRegionPtr mod, int validate) {
	int i, y, ysh, map_count;
	static int ycnt = 0;
	sraRegionPtr line;
	sraRect rect;
	sraRectangleIterator *iter;
	int br = 0, area = 0;
	static double last_call = 0.0;
	
	map_count = get_8bpp_regions(validate);

if (db24 > 1) fprintf(stderr, "poll_8bpp mc: %d\n", map_count);

	if (! map_count) {
		return 0;
	}

	set_poll_fb();

	ysh = scanlines[(ycnt++) % NSCAN];
if (db24 > 2) fprintf(stderr, "poll_8bpp: ysh: %2d  %.4f\n", ysh, dnow() - last_call);
	last_call = dnow();

	for (i=0; i < MAX_8BPP_WINDOWS; i++) {
		sraRegionPtr reg = windows_8bpp[i].clip_region;

		if (! reg || sraRgnEmpty(reg)) {
			continue;
		}
		y = ysh;
		while (y < dpy_y) {
			line = sraRgnCreateRect(0, y, dpy_x, y+1);

			if (sraRgnAnd(line, reg)) {
				iter = sraRgnGetIterator(line);
				while (sraRgnIteratorNext(iter, &rect)) {
					if (! poll_line(rect.x1, rect.x2,
					    rect.y1, i, mod)) {
						br = 1;
						break;	/* exception */
					}
				}
				sraRgnReleaseIterator(iter);
			}

			sraRgnDestroy(line);
			y += NSCAN;
			if (br) break;
		}
		if (br) break;
	}

	iter = sraRgnGetIterator(mod);
	while (sraRgnIteratorNext(iter, &rect)) {
		area += nabs((rect.x2 - rect.x1)*(rect.y2 - rect.y1));
	}
	sraRgnReleaseIterator(iter);

	return area;
}

static void poll_8bpp_complement(sraRegionPtr mod) {
	int i, y, ysh;
	static int ycnt = 0;
	sraRegionPtr disp, line;
	sraRect rect;
	sraRectangleIterator *iter;

	disp = sraRgnCreateRect(0, 0, dpy_x, dpy_y);

	ysh = scanlines[(ycnt++) % NSCAN];

	for (i=0; i < MAX_8BPP_WINDOWS; i++) {
		sraRegionPtr reg = windows_8bpp[i].clip_region;

		if (! reg) {
			continue;
		}
		if (windows_8bpp[i].map_state != IsViewable) {
			continue;	
		}
		sraRgnSubtract(disp, reg);
	}
1101

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
	y = ysh;
	while (y < dpy_y) {
		line = sraRgnCreateRect(0, y, dpy_x, y+1);

		if (sraRgnAnd(line, disp)) {
			iter = sraRgnGetIterator(line);
			while (sraRgnIteratorNext(iter, &rect)) {
				poll_line_complement(rect.x1, rect.x2,
				    rect.y1, mod);
			}
			sraRgnReleaseIterator(iter);
		}

		sraRgnDestroy(line);

		y += NSCAN;
	}

	sraRgnDestroy(disp);
}

static void mark_rgn_rects(sraRegionPtr mod) {
	sraRect rect;
	sraRectangleIterator *iter;
	int area = 0;

	if (sraRgnEmpty(mod)) {
		return;
	}
	
	iter = sraRgnGetIterator(mod);
	while (sraRgnIteratorNext(iter, &rect)) {
		mark_rect_as_modified(rect.x1, rect.y1, rect.x2, rect.y2, 0);
		area += nabs((rect.x2 - rect.x1)*(rect.y2 - rect.y1));
	}
	sraRgnReleaseIterator(iter);

if (db24 > 1) fprintf(stderr, " mark_rgn_rects area: %d\n", area);
}

static int get_8bpp_regions(int validate) {
1143

1144 1145
	XWindowAttributes attr;
	int i, k, mapcount = 0;
1146 1147 1148

	/* initialize color map list */
	ncmaps = 0;
1149
	for (i=0; i < CMAPMAX; i++) {
1150 1151 1152 1153 1154 1155
		cmaps[i] = (Colormap) 0;
	}

	/* loop over the table of 8bpp windows: */
	for (i=0; i < MAX_8BPP_WINDOWS; i++) {
		sraRegionPtr tmp_reg, tmp_reg2;
1156
		Window c, w = windows_8bpp[i].win;
1157 1158
		int x, y;

1159 1160
		if (windows_8bpp[i].clip_region) {
			sraRgnDestroy(windows_8bpp[i].clip_region);	
1161
		}
1162
		windows_8bpp[i].clip_region = NULL;
1163 1164 1165 1166 1167

		if (w == None) {
			continue;
		}

1168
if (db24 > 1) fprintf(stderr, "get_8bpp_regions: 0x%lx ms=%d dep=%d i=%d\n", w, windows_8bpp[i].map_state, windows_8bpp[i].depth, i);
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
		if (validate) {
			/*
			 * this could be slow: validating 8bpp windows each
			 * time...
			 */

			X_LOCK;
			if (! valid_window(w, &attr, 1)) {
				X_UNLOCK;
				windows_8bpp[i].win = None;
				windows_8bpp[i].top = None;
				windows_8bpp[i].map_state = IsUnmapped;
				windows_8bpp[i].cmap = (Colormap) 0;
				windows_8bpp[i].fetched = 0;
1183
				windows_8bpp[i].last_fetched = -1.0;
1184 1185 1186
				continue;
			}
			X_UNLOCK;
1187 1188 1189 1190 1191 1192 1193 1194

			windows_8bpp[i].depth = attr.depth;
			windows_8bpp[i].map_state = attr.map_state;
			windows_8bpp[i].cmap = attr.colormap;
			windows_8bpp[i].map_installed = attr.map_installed;
			windows_8bpp[i].w = attr.width;
			windows_8bpp[i].h = attr.height;
			windows_8bpp[i].fetched = 1;
1195
			windows_8bpp[i].last_fetched = dnow();
1196

1197 1198 1199 1200 1201 1202 1203
			if (attr.map_state != IsViewable) {
				continue;
			}

			X_LOCK;
			xtranslate(w, window, 0, 0, &x, &y, &c, 1);
			X_UNLOCK;
1204 1205
			windows_8bpp[i].x = x;
			windows_8bpp[i].y = y;
1206

1207
		} else {
1208
			/* this will be faster: no call to X server: */
1209 1210 1211
			if (windows_8bpp[i].map_state != IsViewable) {
				continue;
			}
1212
			attr.depth = windows_8bpp[i].depth;
1213 1214
			attr.map_state = windows_8bpp[i].map_state;
			attr.colormap = windows_8bpp[i].cmap;
1215 1216 1217 1218 1219 1220
			attr.map_installed = windows_8bpp[i].map_installed;
			attr.width = windows_8bpp[i].w;
			attr.height = windows_8bpp[i].h;

			x =  windows_8bpp[i].x; 
			y =  windows_8bpp[i].y; 
1221
		}
1222 1223

		mapcount++;
1224 1225

		/* tmp region for this 8bpp rectangle: */
1226
		tmp_reg = sraRgnCreateRect(nfix(x, dpy_x), nfix(y, dpy_y),
1227
		    nfix(x + attr.width, dpy_x+1), nfix(y + attr.height, dpy_y+1));
1228 1229 1230 1231 1232 1233

		/* loop over all toplevels, top to bottom clipping: */
		for (k = stack_list_num - 1; k >= 0; k--) {
			Window swin = stack_list[k].win;
			int sx, sy, sw, sh;

1234
if (db24 > 1 && stack_list[k].map_state == IsViewable) fprintf(stderr, "Stack win: 0x%lx %d iv=%d\n", swin, k, stack_list[k].map_state);
1235 1236

			if (swin == windows_8bpp[i].top) {
1237
				/* found our top level: we skip the rest. */
1238
if (db24 > 1) fprintf(stderr, "found top: 0x%lx %d iv=%d\n", swin, k, stack_list[k].map_state);
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
				break;
			}
			if (stack_list[k].map_state != IsViewable) {
				/* skip unmapped ones: */
				continue;
			}

			/* make a temp rect for this toplevel: */
			sx = stack_list[k].x;
			sy = stack_list[k].y;
			sw = stack_list[k].width;
			sh = stack_list[k].height;

1252 1253
if (db24 > 1) fprintf(stderr, "subtract:  0x%lx %d -- %d %d %d %d\n", swin, k, sx, sy, sw, sh);

1254
			tmp_reg2 = sraRgnCreateRect(nfix(sx, dpy_x),
1255 1256
			    nfix(sy, dpy_y), nfix(sx + sw, dpy_x+1),
			    nfix(sy + sh, dpy_y+1));
1257

1258
			/* subtract it from the 8bpp window region */
1259
			sraRgnSubtract(tmp_reg, tmp_reg2);
1260

1261
			sraRgnDestroy(tmp_reg2);
1262 1263 1264 1265

			if (sraRgnEmpty(tmp_reg)) {
				break;
			}
1266 1267 1268 1269 1270 1271 1272 1273 1274
		}

		if (sraRgnEmpty(tmp_reg)) {
			/* skip this 8bpp if completely clipped away: */
			sraRgnDestroy(tmp_reg);
			continue;
		}

		/* otherwise, store any new colormaps: */
1275
		if (ncmaps < CMAPMAX && attr.colormap != (Colormap) 0) {
1276
			int m, seen = 0;
1277 1278
			for (m=0; m < ncmaps; m++) {
				if (cmaps[m] == attr.colormap) {
1279
					seen = 1;
1280 1281 1282
					break;
				}
			}
1283
			if (! seen && attr.depth == 8) {
1284 1285 1286 1287 1288
				/* store only new ones: */
				cmaps[ncmaps++] = attr.colormap;
			}
		}

1289
		windows_8bpp[i].clip_region = tmp_reg;
1290 1291
	}

1292 1293
	return mapcount;
}
1294

1295 1296 1297 1298
static XColor color[CMAPMAX][NCOLOR];
static unsigned int rgb[CMAPMAX][NCOLOR];
static int cmap_failed[CMAPMAX];
int histo[256];
1299

1300
static int get_cmap(int j, Colormap cmap) {
1301
#if NO_X11
1302
	RAWFB_RET(0)
1303 1304 1305
	if (!j || !cmap) {}
	return 0;
#else
1306 1307 1308
	int i, ncells;
	XErrorHandler old_handler = NULL;

1309 1310
	RAWFB_RET(0)

1311
	if (0) {
1312 1313 1314 1315
		/* not working properly for depth 24... */
		X_LOCK;
		ncells = CellsOfScreen(ScreenOfDisplay(dpy, scr));
		X_UNLOCK;
1316
	} else {
1317
		ncells = NCOLOR;
1318 1319
	}
if (db24 > 1) fprintf(stderr, "get_cmap: %d 0x%x\n", j, (unsigned int) cmap);
1320

1321 1322 1323 1324 1325 1326 1327
	/* ncells should "always" be 256. */
	if (ncells > NCOLOR) {
		ncells = NCOLOR;
	} else if (ncells == 8) {
		/* hmmm. see set_colormap() */
		ncells = NCOLOR;
	}
1328

1329 1330 1331 1332 1333
	/* initialize XColor array: */
	for (i=0; i < ncells; i++) {
		color[j][i].pixel = i;
		color[j][i].pad = 0;
	}
1334

1335 1336 1337 1338 1339 1340 1341
	/* try to query the colormap, trap errors */
	X_LOCK;
	trapped_xerror = 0;
	old_handler = XSetErrorHandler(trap_xerror);
	XQueryColors(dpy, cmap, color[j], ncells);
	XSetErrorHandler(old_handler);
	X_UNLOCK;
1342

1343 1344 1345 1346 1347
	if (trapped_xerror) {
		trapped_xerror = 0;
		return 0;
	}
	trapped_xerror = 0;
1348

1349 1350 1351 1352 1353 1354 1355
	/* now map each index to depth 24 RGB */
	for (i=0; i < ncells; i++) {
		unsigned int red, green, blue;
		/* strip out highest 8 bits of values: */
		red   = (color[j][i].red   & 0xff00) >> 8;
		green = (color[j][i].green & 0xff00) >> 8;
		blue  = (color[j][i].blue  & 0xff00) >> 8;
1356

1357 1358 1359 1360 1361 1362 1363
		/*
		 * the maxes should be at 255 already,
		 * but just in case...
		 */
		red   = (main_red_max   * red  )/255;
		green = (main_green_max * green)/255;
		blue  = (main_blue_max  * blue )/255;
1364

1365
if (db24 > 2) fprintf(stderr, " cmap[%02d][%03d]: %03d %03d %03d  0x%08x \n", j, i, red, green, blue, ( red << main_red_shift | green << main_green_shift | blue << main_blue_shift));
1366

1367 1368 1369 1370
		/* shift them over and or together for value */
		red   = red    << main_red_shift;
		green = green  << main_green_shift;
		blue  = blue   << main_blue_shift;
1371

1372
		/* store it in the array to be used later */
1373 1374 1375
		rgb[j][i] = red | green | blue;
	}
	return 1;
1376
#endif	/* NO_X11 */
1377
}
1378

1379 1380 1381 1382 1383
static void do_8bpp_region(int n, sraRegionPtr mark) {
	int k, cm = -1, failed = 0;
	sraRectangleIterator *iter;
	sraRegionPtr clip;
	sraRect rect;
1384

1385 1386 1387 1388 1389 1390 1391 1392 1393
	if (! windows_8bpp[n].clip_region) {
		return;
	}
	if (windows_8bpp[n].win == None) {
		return;
	}
	if (windows_8bpp[n].map_state != IsViewable) {
		return;
	}
1394
if (db24 > 1) fprintf(stderr, "ncmaps: %d\n", ncmaps);
1395

1396 1397 1398 1399 1400
	/* see if XQueryColors failed: */
	for (k=0; k<ncmaps; k++) {
		if (windows_8bpp[n].cmap == cmaps[k]) {
			cm = k;
			if (cmap_failed[k]) {
1401 1402
				failed = 1;
			}
1403
			break;
1404
		}
1405
	}
1406

1407 1408 1409 1410 1411
	if (windows_8bpp[n].depth == 8) {	/* 24 won't have a cmap */
		if (failed || cm == -1) {
			return;
		}
	}
1412

1413 1414
	clip = sraRgnCreateRgn(mark);
	sraRgnAnd(clip, windows_8bpp[n].clip_region);
1415

1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
	/* loop over the rectangles making up region */
	iter = sraRgnGetIterator(clip);
	while (sraRgnIteratorNext(iter, &rect)) {
		if (rect.x1 > rect.x2) {
			int tmp = rect.x2;
			rect.x2 = rect.x1;
			rect.x1 = tmp;
		}
		if (rect.y1 > rect.y2) {
			int tmp = rect.y2;
			rect.y2 = rect.y1;
			rect.y1 = tmp;
1428
		}
1429

1430 1431
		transform_rect(rect, windows_8bpp[n].win,
		    windows_8bpp[n].depth, cm);
1432
	}
1433 1434 1435
	sraRgnReleaseIterator(iter);
	sraRgnDestroy(clip);
}
1436

1437
static XImage *cmap_xi(XImage *xi, Window win, int win_depth) {
1438
#if NO_X11
1439
	if (!xi || !win || !win_depth) {}
1440 1441
	return NULL;
#else
1442 1443 1444
	XWindowAttributes attr;
	char *d;

1445 1446 1447
	if (xi) {
		XDestroyImage(xi);
	}
1448
	if (! dpy || ! valid_window(win, &attr, 1)) {
1449 1450 1451 1452 1453
		return (XImage *) NULL;
	}
	if (attr.depth != win_depth) {
		return (XImage *) NULL;
	} else if (win_depth == 8) {
1454 1455 1456
		d = (char *) malloc(dpy_x * dpy_y * 1);
	} else if (win_depth == 24) {
		d = (char *) malloc(dpy_x * dpy_y * 4);
1457
	} else {
1458
		return (XImage *) NULL;
1459
	}
1460
	return XCreateImage(dpy, attr.visual, win_depth, ZPixmap, 0, d, dpy_x,
1461
	    dpy_y, 8, 0);
1462
#endif	/* NO_X11 */
1463 1464 1465 1466
}


static void transform_rect(sraRect rect, Window win, int win_depth, int cm) {
1467
#if NO_X11
1468
	RAWFB_RET_VOID
1469 1470 1471
	if (!rect.x1 || !win || !win_depth || !cm) {}
	return;
#else
1472 1473 1474 1475 1476 1477

	char *src, *dst, *poll;
	unsigned int *ui;
	unsigned char *uc;
	int ps, pixelsize = bpp/8;
	int poll_Bpl;
1478

1479
	int do_getimage = xgetimage_8to24;
1480
	int line, n_off, j, h, w;
1481 1482 1483
	unsigned int hi, idx;
	XWindowAttributes attr;
	XErrorHandler old_handler = NULL;
1484

1485
if (db24 > 1) fprintf(stderr, "transform %4d %4d %4d %4d cm: %d\n", rect.x1, rect.y1, rect.x2, rect.y2, cm);
1486

1487 1488
	RAWFB_RET_VOID

runge's avatar
runge committed
1489 1490 1491
	attr.width = 0;
	attr.height = 0;

1492
	/* now transform the pixels in this rectangle: */
1493
	n_off = main_bytes_per_line * rect.y1 + pixelsize * rect.x1;
1494

1495 1496
	h = rect.y2 - rect.y1;
	w = rect.x2 - rect.x1;
1497

1498
	if (depth == 8) {
1499 1500
		/* need to fetch depth 24 data. */
		do_getimage = 1;
1501
	}
1502

1503
#if 0
1504 1505
	if (do_getimage) {
		X_LOCK;
1506
		vw = valid_window(win, &attr, 1);
1507 1508 1509 1510
		X_UNLOCK;
	}

	if (do_getimage && vw) {
1511 1512 1513
#else
	if (do_getimage) {
#endif
1514 1515
		static XImage *xi_8  = NULL;
		static XImage *xi_24 = NULL;
1516
		XImage *xi = NULL, *xi_r;
1517 1518 1519
		Window c;
		unsigned int wu, hu;
		int xo, yo;
1520

1521 1522 1523 1524
		wu = (unsigned int) w;
		hu = (unsigned int) h;

		X_LOCK;
1525 1526
#define GETSUBIMAGE
#ifdef GETSUBIMAGE
1527 1528 1529
		if (win_depth == 8) {
			if (xi_8 == NULL || xi_8->width != dpy_x ||
			    xi_8->height != dpy_y) {
1530
				xi_8 = cmap_xi(xi_8, win, 8);
1531
			}
1532 1533 1534 1535
			xi = xi_8;
		} else if (win_depth == 24) {
			if (xi_24 == NULL || xi_24->width != dpy_x ||
			    xi_24->height != dpy_y) {
1536
				xi_24 = cmap_xi(xi_24, win, 24);
1537
			}
1538
			xi = xi_24;
1539 1540 1541
		}
#endif

1542
		old_handler = XSetErrorHandler(trap_xerror);
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
		trapped_xerror = 0;

		XTranslateCoordinates(dpy, win, window, 0, 0, &xo, &yo, &c);

		xo = rect.x1 - xo;
		yo = rect.y1 - yo;

if (db24 > 1) fprintf(stderr, "xywh: %d %d %d %d vs. %d %d\n", xo, yo, w, h, attr.width, attr.height);

		if (trapped_xerror || xo < 0 || yo < 0) {
		        /* w > attr.width || h > attr.height */
			XSetErrorHandler(old_handler);
			X_UNLOCK;
			trapped_xerror = 0;
if (db24 > 1) fprintf(stderr, "skipping due to potential bad match...\n");
			return;
		}
		trapped_xerror = 0;

1562
#ifndef GETSUBIMAGE
1563 1564
		xi = XGetImage(dpy, win, xo, yo, wu, hu, AllPlanes, ZPixmap);
		xi_r = xi;
1565
#else
1566
		xi_r = XGetSubImage(dpy, win, xo, yo, wu, hu, AllPlanes,
1567 1568
		    ZPixmap, xi, 0, 0);
#endif
1569 1570
		XSetErrorHandler(old_handler);
		X_UNLOCK;
1571

1572
		if (! xi_r || trapped_xerror) {
1573 1574 1575 1576 1577 1578 1579
			trapped_xerror = 0;
if (db24 > 1) fprintf(stderr, "xi-fail: 0x%p trap=%d  %d %d %d %d\n", (void *)xi, trapped_xerror, xo, yo, w, h);
			return;
		} else {
if (db24 > 1) fprintf(stderr, "xi: 0x%p  %d %d %d %d -- %d %d\n", (void *)xi, xo, yo, w, h, xi->width, xi->height);
		}
		trapped_xerror = 0;
1580

1581
		if (xi->depth != 8 && xi->depth != 24) {
1582
#ifndef GETSUBIMAGE
1583 1584 1585
			X_LOCK;
			XDestroyImage(xi);
			X_UNLOCK;
1586
#endif
1587
if (db24) fprintf(stderr, "xi: wrong depth: %d\n", xi->depth);
1588 1589 1590
			return;
		}

1591 1592
		set_poll_fb();

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
		if (xi->depth == 8) {
			int ps1, ps2, fac;

			if (depth == 8) {
				ps1 = 1;
				ps2 = 4;
				fac = 4;
			} else {
				ps1 = 1;
				ps2 = pixelsize;
				fac = 1;
			}
1605

1606 1607
			src = xi->data;
			dst = cmap8to24_fb + fac * n_off;
1608

1609 1610
			poll = poll8_fb + poll8_fb_w * rect.y1 + rect.x1;
			poll_Bpl = poll8_fb_w * 1;
1611

1612
			/* line by line ... */
1613
			for (line = 0; line < h; line++) {
1614
				/* pixel by pixel... */
1615
				for (j = 0; j < w; j++) {
1616 1617 1618

					uc = (unsigned char *) (src + ps1 * j);
					ui = (unsigned int *)  (dst + ps2 * j);
1619

1620 1621
					idx = (int) (*uc);

1622 1623 1624
					*ui = rgb[cm][idx];

					*(poll + ps1 * j) = *uc;
1625
				}
1626 1627
				src += xi->bytes_per_line;
				dst += main_bytes_per_line * fac;
1628
				poll += poll_Bpl;
1629 1630 1631 1632 1633 1634 1635
			}
		} else if (xi->depth == 24) {
			/* line by line ... */
			int ps1 = 4, fac;
			if (depth == 8) {
				fac = 4;
			} else {
1636
				fac = 1;	/* will not happen */
1637
			}
1638

1639 1640 1641
			src = xi->data;
			dst = cmap8to24_fb + fac * n_off;

1642 1643 1644
			poll = poll24_fb + (poll24_fb_w * rect.y1 + rect.x1)*4;
			poll_Bpl = poll24_fb_w * 4;

1645
			for (line = 0; line < h; line++) {
1646 1647 1648
				memcpy(dst,  src, w * ps1);
				memcpy(poll, src, w * ps1);

1649 1650
				src += xi->bytes_per_line;
				dst += main_bytes_per_line * fac;
1651
				poll += poll_Bpl;
1652 1653 1654
			}
		}

1655
#ifndef GETSUBIMAGE
1656 1657 1658
		X_LOCK;
		XDestroyImage(xi);
		X_UNLOCK;
1659
#endif
1660 1661 1662 1663 1664 1665

	} else if (! do_getimage) {
		int fac;

		if (depth == 8) {
			/* cooked up depth 24 TrueColor  */
1666
			/* but currently disabled (high bits no useful?) */
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
			ps = 4;
			fac = 4;
			src = cmap8to24_fb + 4 * n_off;
		} else {
			ps = pixelsize;
			fac = 1;
			src = cmap8to24_fb + n_off;
		}
		
		/* line by line ... */
		for (line = 0; line < h; line++) {
			/* pixel by pixel... */
			for (j = 0; j < w; j++) {

				/* grab 32 bit value */
				ui = (unsigned int *) (src + ps * j);

				/* extract top 8 bits (FIXME: masks?) */
				hi = (*ui) & 0xff000000; 

				/* map to lookup index; rewrite pixel */
				idx = hi >> 24;
1689
				*ui = hi | rgb[cm][idx];
1690 1691 1692 1693
			}
			src += main_bytes_per_line * fac;
		}
	}
1694
#endif	/* NO_X11 */
1695 1696 1697 1698 1699 1700 1701
}

void bpp8to24(int x1, int y1, int x2, int y2) {
	char *src, *dst;
	unsigned char *uc;
	unsigned int *ui;
	int idx, pixelsize = bpp/8;
1702
	int line, k, i, j, h, w;
1703
	int n_off;
1704
	sraRegionPtr rect;
1705 1706
	int validate = 1;
	static int last_map_count = 0, call_count = 0;
1707 1708
	static double last_get_8bpp_validate = 0.0;
	static double last_snapshot = 0.0;
1709 1710
	double now;
	double dt, d0 = 0.0, t2;
1711

1712 1713
	RAWFB_RET_VOID

1714
	if (! cmap8to24 || ! cmap8to24_fb) {
1715 1716 1717 1718
		/* hmmm, why were we called? */
		return;
	}

1719
if (db24 > 1) fprintf(stderr, "bpp8to24 %d %d %d %d %.4f\n", x1, y1, x2, y2, dnow() - last_get_8bpp_validate);
1720

1721 1722 1723 1724 1725
	call_count++;

	/* clip to display just in case: */
	x1 = nfix(x1, dpy_x);
	y1 = nfix(y1, dpy_y);
1726 1727
	x2 = nfix(x2, dpy_x+1);
	y2 = nfix(y2, dpy_y+1);
1728

1729 1730 1731 1732 1733 1734 1735
	if (wireframe_in_progress) {
		/*
		 * draw_box() manages cmap8to24_fb for us so we get out as
		 * soon as we can.  No need to cp main_fb -> cmap8to24_fb.
		 */
		return;
	}
1736

1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
	/* copy from main_fb to cmap8to24_fb regardless of 8bpp windows: */

	h = y2 - y1;
	w = x2 - x1;

	if (depth == 8) {
		/* need to cook up to depth 24 TrueColor  */
		/* pixelsize = 1 */

		n_off = main_bytes_per_line * y1 + pixelsize * x1;

		src = main_fb + n_off;
		dst = cmap8to24_fb + 4 * n_off;

		set_root_cmap();
1752
		if (root_cmap) {
1753
			int ps1 = 1, ps2 = 4;
1754 1755 1756
#if 0
			unsigned int hi;
#endif
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767

			/* line by line ... */
			for (line = 0; line < h; line++) {
				/* pixel by pixel... */
				for (j = 0; j < w; j++) {

					uc = (unsigned char *) (src + ps1 * j);
					ui = (unsigned int *)  (dst + ps2 * j);

					idx = (int) (*uc);

1768
#if 0
1769 1770 1771 1772 1773
					if (do_hibits) {
						hi = idx << 24;
						*ui = hi | rgb[0][idx];
					} else {
					}
1774
#endif
1775
					*ui = root_rgb[idx];
1776 1777
if (db24 > 2) histo[idx]++;
				}
1778 1779
				src += main_bytes_per_line;
				dst += main_bytes_per_line * 4;
1780 1781
			}
		}
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
		
	} else if (depth == 24) {
		/* pixelsize = 4 */
		n_off = main_bytes_per_line * y1 + pixelsize * x1;

		src = main_fb      + n_off;
		dst = cmap8to24_fb + n_off;

		/* otherwise, the pixel data as is */
		for (line = 0; line < h; line++) {
			memcpy(dst, src, w * pixelsize);
			src += main_bytes_per_line;
			dst += main_bytes_per_line;
		}
	}

1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
	if (last_map_count > MAX_8BPP_WINDOWS/4) {
		/* table is filling up... skip validating sometimes: */
		int skip = 3;
		if (last_map_count > MAX_8BPP_WINDOWS/2) {
			skip = 6;
		} else if (last_map_count > 3*MAX_8BPP_WINDOWS/4) {
			skip = 12;
		}
		if (call_count % skip != 0) {
			validate = 0;
		}
	}

if (db24 > 2) {for(i=0;i<256;i++){histo[i]=0;}}

1813
	now = dnow();
1814
	dt = now - last_get_8bpp_validate;
1815 1816
	/* TUNABLES  */
	if (dt < 0.003) {
1817 1818 1819
		;	/* XXX does this still give painting errors? */
	} else {
		int snapit = 0;
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
		double delay1, delay2, delay3;
		if (poll_8to24_delay >= POLL_8TO24_DELAY) {
			delay1 = 1.0 * poll_8to24_delay;
			delay2 = 2.0 * poll_8to24_delay;
			delay3 = 10. * poll_8to24_delay;
		} else {
			delay1 = 1.0 * POLL_8TO24_DELAY;	/* 0.05 */
			delay2 = 2.0 * POLL_8TO24_DELAY;	/* 0.1  */
			delay3 = 10. * POLL_8TO24_DELAY;	/* 0.5  */
		}
		if (cache_win > 1.0) {
			delay2 *= 2;
			delay3 *= 2;
		}
		if (dt < delay1) {
1835 1836 1837
			validate = 0;
		}
		if (last_map_count) {
1838
			if (now > last_snapshot + delay2) {
1839 1840 1841
				snapit = 1;
			}
		} else {
1842
			if (now > last_snapshot + delay3) {
1843 1844 1845 1846 1847 1848 1849
				snapit = 1;
			}
		}

		if (snapit) {
			/* less problems if we update the stack frequently */
			snapshot_stack_list(0, 0.0);
1850
if (0) fprintf(stderr, "SNAP time: %.4f\n", dnow() - now);
1851
			update_stack_list();
1852 1853
			last_snapshot = dnow();
if (0) fprintf(stderr, "UPDA time: %.4f\n", last_snapshot - now);
1854 1855
		}

1856
if (0) t2 = dnow();
1857 1858
		last_map_count = get_8bpp_regions(validate);
		if (validate) {
1859
			last_get_8bpp_validate = dnow();
1860
		}
1861
if (0) fprintf(stderr, "get8bpp-%d: %.4f\n", validate, dnow() - t2);
1862
	}
1863
if (db24) d0 = dnow();
1864

1865 1866 1867
if (db24 > 1) fprintf(stderr, "bpp8to24 w=%d h=%d m=%p c=%p r=%p ncmaps=%d\n", w, h, main_fb, cmap8to24_fb, rfb_fb, ncmaps);

	/*
1868 1869
	 * now go back and transform and 8bpp regions to TrueColor in
	 * cmap8to24_fb.
1870
	 */
1871 1872 1873 1874 1875
	if (last_map_count && (ncmaps || depth == 8)) {
		int i, j;
		int win[MAX_8BPP_WINDOWS];
		int did[MAX_8BPP_WINDOWS];
		int count = 0;
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886

		/*
		 * first, grab all of the associated colormaps from the
		 * X server.  Hopefully just 1 or 2...
		 */
		for (j=0; j<ncmaps; j++) {
			if (! get_cmap(j, cmaps[j])) {
				cmap_failed[j] = 1;
			} else {
				cmap_failed[j] = 0;
			}
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
if (db24 > 2) fprintf(stderr, "cmap %d  %.4f\n", (int) cmaps[j], dnow() - d0);
		}
		for (i=0; i < MAX_8BPP_WINDOWS; i++) {
			sraRegionPtr reg = windows_8bpp[i].clip_region;
			if (reg) {
				rect = sraRgnCreateRect(x1, y1, x2, y2);
				if (sraRgnAnd(rect, reg)) {
					win[count] = i;
					did[count++] = 0;
				}
				sraRgnDestroy(rect);
			}
1899 1900
		}

1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
		if (count) {

			rect = sraRgnCreateRect(x1, y1, x2, y2);
			/* try to apply lower windows first */
			for (k=0; k < stack_list_num; k++) {
				Window swin = stack_list[k].win;
				for (j=0; j<count; j++) {
					i = win[j];
					if (did[j]) {
						continue;
					}
					if (windows_8bpp[i].top == swin) {
						do_8bpp_region(i, rect);
						did[j] = 1;
						break;
					}
				}
1918
			}
1919 1920 1921 1922 1923 1924
			for (j=0; j<count; j++) {
				if (! did[j]) {
					i = win[j];
					do_8bpp_region(i, rect);
					did[j] = 1;
				}
1925
			}
1926
			sraRgnDestroy(rect);
1927
		}
1928
	}
1929
if (0) fprintf(stderr, "done time: %.4f\n", dnow() - d0);
1930

1931
if (db24 > 2) {for(i=0; i<256;i++) {fprintf(stderr, " cmap histo[%03d] %d\n", i, histo[i]);}}
1932 1933
}

1934
void mark_8bpp(int mode) {
1935
	int i, cnt = 0;
1936
	Window top = None;
1937

1938 1939
	RAWFB_RET_VOID

1940 1941 1942
	if (! cmap8to24 || !cmap8to24_fb) {
		return;
	}
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
	
	if (mode == MARK_8BPP_TOP) {
		int k;
		for (k = stack_list_num - 1; k >= 0; k--) {
			Window swin = stack_list[k].win;
			for (i=0; i < MAX_8BPP_WINDOWS; i++) {
				if (windows_8bpp[i].win == None) {
					continue;
				}
				if (windows_8bpp[i].map_state != IsViewable) {
					continue;
				}
				if (swin == windows_8bpp[i].top) {
					top = swin;
					break;
				}
			}
			if (top != None) {
				break;
			}
		}
	}
1965 1966 1967 1968 1969

	/* for each mapped 8bpp window, mark it changed: */

	for (i=0; i < MAX_8BPP_WINDOWS; i++) {
		int x1, y1, x2, y2, w, h, f = 32;
1970

1971 1972 1973 1974 1975
		f = 0;	/* skip fuzz, may bring in other windows... */

		if (windows_8bpp[i].win == None) {
			continue;
		}
1976 1977 1978 1979 1980
		if (mode == MARK_8BPP_TOP) {
			if (windows_8bpp[i].top != top) {
				continue;
			}
		}
1981
		if (windows_8bpp[i].map_state != IsViewable) {
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
			XWindowAttributes attr;
			int vw;

			X_LOCK;
			vw = valid_window(windows_8bpp[i].win, &attr, 1);
			X_UNLOCK;
			if (vw) {
				if (attr.map_state != IsViewable) {
					continue;
				}
			} else {
				continue;
			}
1995 1996 1997 1998 1999 2000 2001 2002 2003
		}

		x1 = windows_8bpp[i].x;
		y1 = windows_8bpp[i].y;
		w  = windows_8bpp[i].w;
		h  = windows_8bpp[i].h;

		x2 = x1 + w;
		y2 = y1 + h;
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015

		if (mode == MARK_8BPP_POINTER) {
			int b = 32;	/* apply some fuzz for wm border */
			if (cursor_x < x1 - b || cursor_y < y1 - b) {
				continue;
			}
			if (cursor_x > x2 + b || cursor_y > y2 + b) {
				continue;
			}
		}

		/* apply fuzz f around each one; constrain to screen */
2016 2017
		x1 = nfix(x1 - f, dpy_x);
		y1 = nfix(y1 - f, dpy_y);
2018 2019
		x2 = nfix(x2 + f, dpy_x+1);
		y2 = nfix(y2 + f, dpy_y+1);
2020

2021
if (db24 > 1) fprintf(stderr, "mark_8bpp: 0x%lx %d %d %d %d\n", windows_8bpp[i].win, x1, y1, x2, y2);
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031

		mark_rect_as_modified(x1, y1, x2, y2, 0);
		cnt++;
	}
	if (cnt) {
		/* push it to viewers if possible. */
		rfbPE(-1);
	}
}

2032 2033
#endif /* SKIP_8TO24 */