double spin_fac = 1.0, scroll_fac = 2.0;
double spin_fac = 1.0, scroll_fac = 2.0, noscroll_fac = 0.75;
int check_xrecord_keys(void) {	static int last_wx, last_wy, last_ww, last_wh;	double spin = 0.0, tm, tnow;	int scr_cnt = 0, input = 0, scroll_rep;	int get_out, got_one = 0, flush1 = 0, flush2 = 0;	int gk, gk0, ret = 0, db = debug_scroll;	int fail = 0;	int link, latency, netrate;	static double last_key_scroll = 0.0;	static double persist_start = 0.0;	static double last_bdpush = 0.0;	static int persist_count = 0;	double last_scroll, scroll_persist = scr_key_persist;	double spin_fac = 1.0, scroll_fac = 2.0;	double max_spin, max_long_spin = 0.3;	double set_repeat_in;	static double set_repeat = 0.0;	set_repeat_in = set_repeat;	set_repeat = 0.0;	get_out = 1;	if (got_keyboard_input) {		get_out = 0;	} 	dtime0(&tnow);	if (tnow < last_key_scroll + scroll_persist) {		get_out = 0;	}	if (set_repeat_in > 0.0 && tnow < last_key_scroll + set_repeat_in) {		get_out = 0;	}	if (get_out) {		persist_start = 0.0;		persist_count = 0;		last_bdpush = 0.0;		if (xrecording) {			xrecord_watch(0, SCR_KEY);		}		return 0;	}	scroll_rep = scrollability(xrecord_ptr_window, SCR_NONE) + 1;	max_spin = scr_key_time;	if (set_repeat_in > 0.0 && tnow < last_key_scroll + 2*set_repeat_in) {		max_spin = 2 * set_repeat_in;	} else if (tnow < last_key_scroll + scroll_persist) {		max_spin = 1.25*(tnow - last_key_scroll);	} else if (tnow < last_key_to_button_remap_time + scroll_persist) {		/* mostly a hack I use for testing -remap key -> btn4/btn5 */		max_spin = scroll_persist;	} else if (xrecord_scroll_keysym(last_rfb_keysym)) {		spin_fac = scroll_fac;	}	if (max_spin > max_long_spin) {		max_spin = max_long_spin;	}	/* XXX use this somehow  */if (0)	link = link_rate(&latency, &netrate);	gk = gk0 = got_keyboard_input;	dtime0(&tm);if (db) fprintf(stderr, "check_xrecord_keys: BEGIN LOOP: scr_ev_cnt: "    "%d max: %.3f  %.4f\n", scr_ev_cnt, max_spin, tm - x11vnc_start);	while (1) {		if (scr_ev_cnt) {			got_one = 1;			scrollability(xrecord_ptr_window, SCR_SUCCESS);			scroll_rep = 2;			dtime0(&last_scroll);			last_key_scroll = last_scroll;			scr_cnt++;			break;		}		X_LOCK;		flush1 = 1;		XFlush(dpy);		X_UNLOCK;		if (set_repeat_in > 0.0) {			max_keyrepeat_time = set_repeat_in;		}		if (use_threads) {			usleep(1000);		} else {			rfbCFD(1000);		}		spin += dtime(&tm);		X_LOCK;		if (got_keyboard_input > gk) {			gk = got_keyboard_input;			input++;			if (set_repeat_in) {				;			} else if (xrecord_scroll_keysym(last_rfb_keysym)) {				spin_fac = scroll_fac;			}if (0 || db) fprintf(stderr, "check_xrecord: more keys: %.3f  0x%x "    " %.4f  %s  %s\n", spin, last_rfb_keysym, last_rfb_keytime - x11vnc_start,    last_rfb_down ? "down":"up  ", last_rfb_key_accepted ? "accept":"skip");			flush2 = 1;			XFlush(dpy);		}		SCR_LOCK;		XRecordProcessReplies(rdpy_data);		SCR_UNLOCK;		X_UNLOCK;		if (spin >= max_spin * spin_fac) {if (0 || db) fprintf(stderr, "check_xrecord: SPIN-OUT: %.3f/%.3f\n", spin,    max_spin * spin_fac);			fail = 1;			break;		}	}	max_keyrepeat_time = 0.0;	if (scr_ev_cnt) {		int dret, ev = scr_ev_cnt - 1;		int bdx, bdy, bdskinny, bdpush = 0;		double max_age = 0.25, age, tm, dt;		static double last_scr_ev = 0.0;		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;		/* assume scrollbar on rhs: */		bdx = last_wx + last_ww + 3;		bdy = last_wy + last_wh/2;		bdskinny = 32;					if (persist_start == 0.0) {			bdpush = 0;		} else {			set_bdpush(SCR_KEY, &last_bdpush, &bdpush);		}		dtime0(&tm);		age = max_age;		dret = push_scr_ev(&age, SCR_KEY, bdpush, bdx, bdy, bdskinny);		dt = dtime(&tm);		ret = 1 + dret;		scr_ev_cnt = 0;		if (ret == 2 && xrecord_scroll_keysym(last_rfb_keysym)) {			int repeating;			double time_lo = 1.0/max_keyrepeat_lo;			double time_hi = 1.0/max_keyrepeat_hi;			double rate = typing_rate(0.0, &repeating);if (0 || db) fprintf(stderr, "Typing: dt: %.4f rate: %.1f\n", dt, rate);			if (repeating) {				/* n.b. the "quantum" is about 1/30 sec. */				max_keyrepeat_time = 1.0*dt;				if (max_keyrepeat_time > time_lo ||				    max_keyrepeat_time < time_hi) {					max_keyrepeat_time = 0.0;				} else {					set_repeat = max_keyrepeat_time;if (0 || db) fprintf(stderr, "set max_keyrepeat_time: %.2f\n", max_keyrepeat_time);				}			}		}		last_scr_ev = dnow();	}	if ((got_one && ret < 2) || persist_count) {		set_xdamage_mark(last_wx, last_wy, last_ww, last_wh);	}	if (fail) {		scrollability(xrecord_ptr_window, SCR_FAIL);	}	if (xrecording) {		if (ret < 2) {			xrecord_watch(0, SCR_KEY);		}	}	if (ret == 2) {		if (persist_start == 0.0) {			dtime(&persist_start);			last_bdpush = persist_start;		}	} else {		persist_start = 0.0;		last_bdpush = 0.0;	}	/* since we've flushed it, we might as well avoid -input_skip */	if (flush1 || flush2) {		got_keyboard_input = 0;		got_pointer_input = 0;	}	return ret;}