if (db) fprintf(stderr, "check_xrecord_mouse: BEGIN LOOP: scr_ev_cnt: %d max: %.3f\n", scr_ev_cnt, max_spin[scroll_rep]);
if (db) fprintf(stderr, "check_xrecord_mouse: BEGIN LOOP: scr_ev_cnt: " "%d max: %.3f  %.4f\n", scr_ev_cnt, max_spin[scroll_rep], tm - x11vnc_start);
int check_xrecord_mouse(void) {	static int last_wx, last_wy, last_ww, last_wh;	double spin = 0.0, tm, tnow;	int i, scr_cnt = 0, input = 0, scroll_rep;	int get_out, got_one = 0, flush1 = 0, flush2 = 0;	int gp, gp0, ret = 0, db = debug_scroll;	int gk, gk0;	int fail = 0;	int link, latency, netrate;	int start_x, start_y, last_x, last_y;	static double last_mouse_scroll = 0.0;	double last_scroll;	double max_spin[3], max_long[3], persist[3];	double flush1_time = 0.01;	static double last_flush = 0.0;	double last_bdpush = 0.0, button_up_time = 0.0;	int button_mask_save;	int already_down = 0, max_ptr_eat = 20;	static int want_back_in = 0;	int came_back_in;	get_out = 1;	if (button_mask) {		get_out = 0;	} 	if (want_back_in) {		get_out = 0;	} 	dtime0(&tnow);if (0) fprintf(stderr, "check_xrecord_mouse: IN xrecording: %d\n", xrecording);	if (get_out) {		if (xrecording) {			xrecord_watch(0, SCR_MOUSE);		}		return 0;	}	scroll_rep = scrollability(xrecord_ptr_window, SCR_NONE) + 1;	if (button_mask_prev) {		already_down = 1;	}	if (want_back_in) {		came_back_in = 1;	} else {		came_back_in = 0;	}	want_back_in = 0;	/*	 * set up times for the various "reputations"	 *	 * 0 => -1, has been tried but never found a scroll.	 * 1 =>  0, has not been tried.	 * 2 => +1, has been tried and found a scroll.	 */	/* first spin-out time (no events) */	max_spin[0] = 1*scr_mouse_time;	max_spin[1] = 2*scr_mouse_time;	max_spin[2] = 4*scr_mouse_time;	if (!already_down) {		for (i=0; i<3; i++) {			max_spin[i] *= 1.5;		}	}	/* max time between events */	persist[0] = 1*scr_mouse_persist;	persist[1] = 2*scr_mouse_persist;	persist[2] = 4*scr_mouse_persist;	/* absolute max time in the loop */	max_long[0] = scr_mouse_maxtime;	max_long[1] = scr_mouse_maxtime;	max_long[2] = scr_mouse_maxtime;	pointer_flush_delay = scr_mouse_pointer_delay;	/* slow links: */	link = link_rate(&latency, &netrate);	if (link == LR_DIALUP) {		for (i=0; i<3; i++) {			max_spin[i] *= 2.0;		}		pointer_flush_delay *= 2;	} else if (link == LR_BROADBAND) {		pointer_flush_delay *= 2;	}	gp = gp0 = got_pointer_input;	gk = gk0 = got_keyboard_input;	dtime0(&tm);	/*	 * this is used for border pushes (bdpush) to guess location	 * of scrollbar (region rects containing this point are pushed).	 */	last_x = start_x = cursor_x;	last_y = start_y = cursor_y;if (db) fprintf(stderr, "check_xrecord_mouse: BEGIN LOOP: scr_ev_cnt: %d max: %.3f\n",    scr_ev_cnt, max_spin[scroll_rep]);	while (1) {		double spin_check;		if (scr_ev_cnt) {			int dret, ev = scr_ev_cnt - 1;			int bdpush = 0, bdx, bdy, bdskinny;			double tm, dt;			got_one = 1;			scrollability(xrecord_ptr_window, SCR_SUCCESS);			scroll_rep = 2;			scr_cnt++;			dtime0(&last_scroll);			last_mouse_scroll = last_scroll;			if (last_bdpush == 0.0) {				last_bdpush = last_scroll;			}			bdx = start_x;			bdy = start_y;			bdskinny = 32;						set_bdpush(SCR_MOUSE, &last_bdpush, &bdpush);			dtime0(&tm);			dret = push_scr_ev(0.35, SCR_MOUSE, bdpush, bdx,			    bdy, bdskinny);			ret = 1 + dret;			dt = dtime(&tm);if (db) fprintf(stderr, "  dret: %d  scr_ev_cnt: %d dt: %.4f\n",	dret, scr_ev_cnt, dt);			last_wx = scr_ev[ev].win_x;			last_wy = scr_ev[ev].win_y;			last_ww = scr_ev[ev].win_w;			last_wh = scr_ev[ev].win_h;			scr_ev_cnt = 0;			if (! dret) {				break;			}			if (0 && button_up_time > 0.0) {				/* we only take 1 more event with button up */if (db) fprintf(stderr, "check_xrecord: BUTTON_UP_SCROLL: %.3f\n", spin);				break;			}		}		if (! flush1) {			if (! already_down || (!scr_cnt && spin>flush1_time)) {				flush1 = 1;				X_LOCK;				XFlush(dpy);				X_UNLOCK;				dtime0(&last_flush);			}		}		if (use_threads) {			usleep(1000);		} else {			rfbCFD(1000);			rfbCFD(0);		}		spin += dtime(&tm);		if (got_pointer_input > gp) {			flush2 = 1;			input += eat_pointer(max_ptr_eat, 1);			gp = got_pointer_input;		}		if (got_keyboard_input > gk) {			gk = got_keyboard_input;			input++;		}		X_LOCK;		SCR_LOCK;		XRecordProcessReplies(rdpy_data);		SCR_UNLOCK;		X_UNLOCK;		if (! input) {			spin_check = 1.5 * max_spin[scroll_rep];		} else {			spin_check = max_spin[scroll_rep];		}		if (button_up_time > 0.0) {			if (tm > button_up_time + max_spin[scroll_rep]) {if (db) fprintf(stderr, "check_xrecord: SPIN-OUT-BUTTON_UP: %.3f/%.3f\n", spin, tm - button_up_time);				break;			}		} else if (!scr_cnt) {			if (spin >= spin_check) {if (db) fprintf(stderr, "check_xrecord: SPIN-OUT-1: %.3f/%.3f\n", spin, spin_check);				fail = 1;				break;			}		} else {			if (tm >= last_scroll + persist[scroll_rep]) {if (db) fprintf(stderr, "check_xrecord: SPIN-OUT-2: %.3f/%.3f\n", spin, tm - last_scroll);				break;			}		}		if (spin >= max_long[scroll_rep]) {if (db) fprintf(stderr, "check_xrecord: SPIN-OUT-3: %.3f/%.3f\n", spin, max_long[scroll_rep]);			break;		}		if (! button_mask) {			int doflush = 0;			if (button_up_time > 0.0) {				;			} else if (came_back_in) {				dtime0(&button_up_time);				doflush = 1;			} else if (last_x == cursor_x && last_y == cursor_y) {if (db) fprintf(stderr, "check_xrecord: BUTTON-UP:  %.3f/%.3f %d/%d %d/%d\n", spin, max_long[scroll_rep], last_x, last_y, cursor_x, cursor_y);				break;			} else {if (db) fprintf(stderr, "check_xrecord: BUTTON-UP-KEEP-GOING:  %.3f/%.3f %d/%d %d/%d\n", spin, max_long[scroll_rep], last_x, last_y, cursor_x, cursor_y);				doflush = 1;				dtime0(&button_up_time);			}			if (doflush) {				flush1 = 1;				X_LOCK;				XFlush(dpy);				X_UNLOCK;				dtime0(&last_flush);			}		}		last_x = cursor_x;		last_y = cursor_y;	}	if (got_one) {		set_xdamage_mark(last_wx, last_wy, last_ww, last_wh);	}	if (fail) {		scrollability(xrecord_ptr_window, SCR_FAIL);	}	/* flush any remaining pointer events. */	button_mask_save = button_mask;	pointer_queued_sent = 0;	last_x = cursor_x;	last_y = cursor_y;	pointer(-1, 0, 0, NULL);	pointer_flush_delay = 0.0;	if (xrecording && pointer_queued_sent && button_mask_save &&	    (last_x != cursor_x || last_y != cursor_y) ) {if (db) fprintf(stderr, "  pointer() push yields events on: ret=%d\n", ret);		if (ret == 2) {if (db) fprintf(stderr, "  we decide to send ret=3\n");			want_back_in = 1;			ret = 3;			flush2 = 1;		} else {			if (ret) {				ret = 1;			} else {				ret = 0;			}			xrecord_watch(0, SCR_MOUSE);		}	} else {		if (ret) {			ret = 1;		} else {			ret = 0;		}		if (xrecording) {			xrecord_watch(0, SCR_MOUSE);		}	}	if (flush2) {		X_LOCK;		XFlush(dpy);		XFlush(rdpy_ctrl);		X_UNLOCK;		flush2 = 1;		dtime0(&last_flush);if (db) fprintf(stderr, "FLUSH-2\n");	}	/* since we've flushed it, we might as well avoid -input_skip */	if (flush1 || flush2) {		got_keyboard_input = 0;		got_pointer_input = 0;	}	if (ret) {		return ret;	} else if (scr_cnt) {		return 1;	} else {		return 0;	}}