if (hack_val) {
if (debug_xevents) {
void check_xevents(void) {	XEvent xev;	int have_clients = 0;	static int sent_some_sel = 0;	static time_t last_request = 0;	static time_t last_call = 0;	static time_t last_bell = 0;	static time_t last_init_check = 0;	static time_t last_sync = 0;	time_t now = time(0);	if (! dpy) {	/* raw_fb hack */		return;	}	if (now > last_init_check+1) {		last_init_check = now;		initialize_xevents();	}	if (screen && screen->clientHead) {		have_clients = 1;	}	X_LOCK;	/*	 * There is a bug where we have to wait before sending text to	 * the client... so instead of sending right away we wait a	 * the few seconds.	 */	if (have_clients && watch_selection && !sent_some_sel	    && now > last_client + sel_waittime) {		if (XGetSelectionOwner(dpy, XA_PRIMARY) == None) {			cutbuffer_send();		}		sent_some_sel = 1;	}	if (! have_clients) {		/*		 * If we don't have clients we can miss the X server		 * going away until a client connects.		 */		static time_t last_X_ping = 0;		if (now > last_X_ping + 5) {			last_X_ping = now;			XGetSelectionOwner(dpy, XA_PRIMARY);		}	}	if (no_autorepeat && have_clients && no_repeat_countdown) {		static time_t last_check = 0;		if (now > last_check + 1 && ! view_only) {			last_check = now;			X_UNLOCK;			if (get_autorepeat_state() != 0) {				int n = no_repeat_countdown - 1;				if (n >= 0) {					rfbLog("Battling with something for "					    "-norepeat!! (%d resets left)\n",n);				} else {					rfbLog("Battling with something for "					    "-norepeat!!\n");				}				if (no_repeat_countdown > 0) {					no_repeat_countdown--;				}				autorepeat(1);				autorepeat(0);			}			X_LOCK;		}	}	if (now > last_call+1) {		/* we only check these once a second or so. */		int n = 0;		while (XCheckTypedEvent(dpy, MappingNotify, &xev)) {			XRefreshKeyboardMapping((XMappingEvent *) &xev);			n++;		}		if (n && use_modifier_tweak) {			X_UNLOCK;			initialize_modtweak();			X_LOCK;		}		if (xtrap_base_event_type) {			int base = xtrap_base_event_type;			while (XCheckTypedEvent(dpy, base, &xev)) {				;			}		}		if (xtest_base_event_type) {			int base = xtest_base_event_type;			while (XCheckTypedEvent(dpy, base, &xev)) {				;			}		}	}	if (last_sendevent && (now > last_sendevent+1 || now % 10 == 0)) {		/*		 * we can get ClientMessage from our XSendEvent() call in 		 * selection_request().		 */		while (XCheckTypedEvent(dpy, ClientMessage, &xev)) {			;		}		last_sendevent = 0;	}	/* check for CUT_BUFFER0 and VNC_CONNECT changes: */	if (XCheckTypedEvent(dpy, PropertyNotify, &xev)) {		if (xev.type == PropertyNotify) {			if (xev.xproperty.atom == XA_CUT_BUFFER0) {				/*				 * Go retrieve CUT_BUFFER0 and send it.				 *				 * set_cutbuffer is a flag to try to avoid				 * processing our own cutbuffer changes.				 */				if (have_clients && watch_selection				    && ! set_cutbuffer) {					cutbuffer_send();					sent_some_sel = 1;				}				set_cutbuffer = 0;			} else if (vnc_connect && vnc_connect_prop != None		    	    && xev.xproperty.atom == vnc_connect_prop) {					/*				 * Go retrieve VNC_CONNECT string.				 */				read_vnc_connect_prop();			}		}	}#if LIBVNCSERVER_HAVE_LIBXRANDR	if (xrandr) {		check_xrandr_event("check_xevents");	}#endif#if LIBVNCSERVER_HAVE_LIBXFIXES	if (xfixes_present && use_xfixes && xfixes_base_event_type) {		if (XCheckTypedEvent(dpy, xfixes_base_event_type +		    XFixesCursorNotify, &xev)) {			got_xfixes_cursor_notify++;		}	}#endif	/* check for our PRIMARY request notification: */	if (watch_primary) {		if (XCheckTypedEvent(dpy, SelectionNotify, &xev)) {			if (xev.type == SelectionNotify &&			    xev.xselection.requestor == selwin &&			    xev.xselection.selection == XA_PRIMARY &&			    xev.xselection.property != None &&			    xev.xselection.target == XA_STRING) {				/* go retrieve PRIMARY and check it */				if (now > last_client + sel_waittime				    || sent_some_sel) {					selection_send(&xev);				}			}		}		if (now > last_request) {			/*			 * Every second or two, request PRIMARY, unless we			 * already own it or there is no owner or we have			 * no clients.			 * TODO: even at this low rate we should look into			 * and performance problems in odds cases, etc.			 */			last_request = now;			if (! own_selection && have_clients &&			    XGetSelectionOwner(dpy, XA_PRIMARY) != None) {				XConvertSelection(dpy, XA_PRIMARY, XA_STRING,				    XA_STRING, selwin, CurrentTime);			}		}	}	if (own_selection) {		/* we own PRIMARY, see if someone requested it: */		if (XCheckTypedEvent(dpy, SelectionRequest, &xev)) {			if (xev.type == SelectionRequest &&			    xev.xselectionrequest.selection == XA_PRIMARY) {				selection_request(&xev);			}		}		/* we own PRIMARY, see if we no longer own it: */		if (XCheckTypedEvent(dpy, SelectionClear, &xev)) {			if (xev.type == SelectionClear &&			    xev.xselectionclear.selection == XA_PRIMARY) {				own_selection = 0;				if (xcut_str) {					free(xcut_str);					xcut_str = NULL;				}			}		}	}	if (watch_bell || now > last_bell+1) {		last_bell = now;		check_bell_event();	}#ifndef DEBUG_XEVENTS#define DEBUG_XEVENTS 1#endif#if DEBUG_XEVENTS	if (hack_val) {		static time_t last_check = 0;		if (now > last_check + 1) {			XEvent xevs[400];			int i, tot = XEventsQueued(dpy, QueuedAlready);			last_check = now;			if (tot) {		    		fprintf(stderr, "Total events queued: %d\n",				    tot);			}			for (i=1; i<300; i++) {				int k, n = 0;				while (XCheckTypedEvent(dpy, i, xevs+n)) {					if (++n >= 400) {						break;					}				}				if (n) {					fprintf(stderr, "%d events of type %d "					    "queued\n", n, i);				}				for (k=n-1; k >= 0; k--) {					XPutBackEvent(dpy, xevs+k);				}			}		}	}#endif	if (now > last_sync + 1200) {		/* kludge for any remaining event leaks */		int bugout = use_xdamage ? 500 : 50;		if (last_sync != 0) {			int qlen = XEventsQueued(dpy, QueuedAlready);			if (qlen >= bugout) {				rfbLog("event leak: %d queued, "				    " calling XSync(dpy, True)\n", qlen);  				XSync(dpy, True);			}		}		last_sync = now;	}	X_UNLOCK;	last_call = now;}