From 5ed566cae08d4f39920bea81fd6bf2160f38348f Mon Sep 17 00:00:00 2001 From: Po Lu Date: Wed, 1 Jun 2022 08:57:08 +0800 Subject: [PATCH] Improve x_wait_for_cell_change * src/xterm.c (x_wait_for_cell_change): Keep processing events while still XPending. --- src/xterm.c | 51 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/xterm.c b/src/xterm.c index 7235a1e9599..186014b0b43 100644 --- a/src/xterm.c +++ b/src/xterm.c @@ -14849,13 +14849,19 @@ x_wait_for_cell_change (Lisp_Object cell, struct timespec timeout) fd_set fds; int fd, maxfd; #ifndef USE_GTK - int finish; + int finish, rc; XEvent event; + fd_set rfds; #endif struct input_event hold_quit; struct timespec current, at; at = timespec_add (current_timespec (), timeout); + FD_ZERO (&rfds); + +#ifndef USE_GTK + rc = 0; +#endif while (true) { @@ -14865,26 +14871,35 @@ x_wait_for_cell_change (Lisp_Object cell, struct timespec timeout) for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next) { + fd = ConnectionNumber (dpyinfo->display); + #ifndef USE_GTK - if (XPending (dpyinfo->display)) + if ((rc < 0 || FD_ISSET (fd, &rfds)) + /* If pselect failed, the erroring display's IO error + handler will eventually be called. */ + && XPending (dpyinfo->display)) { - EVENT_INIT (hold_quit); + while (XPending (dpyinfo->display)) + { + EVENT_INIT (hold_quit); - XNextEvent (dpyinfo->display, &event); - handle_one_xevent (dpyinfo, &event, - &finish, &hold_quit); + XNextEvent (dpyinfo->display, &event); + handle_one_xevent (dpyinfo, &event, + &finish, &hold_quit); - /* Make us quit now. */ - if (hold_quit.kind != NO_EVENT) - kbd_buffer_store_event (&hold_quit); + if (!NILP (XCAR (cell))) + return; - if (!NILP (XCAR (cell))) - return; + if (finish == X_EVENT_GOTO_OUT) + break; + + /* Make us quit now. */ + if (hold_quit.kind != NO_EVENT) + kbd_buffer_store_event (&hold_quit); + } } #endif - fd = XConnectionNumber (dpyinfo->display); - if (fd > maxfd) maxfd = fd; @@ -14929,10 +14944,14 @@ x_wait_for_cell_change (Lisp_Object cell, struct timespec timeout) timeout = timespec_sub (at, current); - /* We don't have to check the return of pselect, because if an - error occurs XPending will call the IO error handler, which - then brings us out of this loop. */ +#ifndef USE_GTK + rc = pselect (maxfd, &fds, NULL, NULL, &timeout, NULL); + + if (rc > 0) + rfds = fds; +#else pselect (maxfd, &fds, NULL, NULL, &timeout, NULL); +#endif } } -- 2.39.2