From 873bcd6d5d1d571caa3aa355fef2c9a987ebb4ca Mon Sep 17 00:00:00 2001 From: Po Lu Date: Wed, 1 Jun 2022 20:52:00 +0800 Subject: [PATCH] Minor fixes to x_next_event_from_any_display * src/xterm.c (x_next_event_from_any_display): Don't call XPending unless there is input to be read on the connection, and don't call ConnectionNumber twice. --- src/xterm.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/xterm.c b/src/xterm.c index 484637807aa..214419a3360 100644 --- a/src/xterm.c +++ b/src/xterm.c @@ -10292,8 +10292,11 @@ static void x_next_event_from_any_display (XEvent *event) { struct x_display_info *dpyinfo; - fd_set fds; - int fd, maxfd; + fd_set fds, rfds; + int fd, maxfd, rc; + + rc = 0; + FD_ZERO (&rfds); while (true) { @@ -10303,27 +10306,31 @@ x_next_event_from_any_display (XEvent *event) for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next) { - if (XPending (dpyinfo->display)) + fd = ConnectionNumber (dpyinfo->display); + + if ((rc < 0 || FD_ISSET (fd, &rfds)) + && XPending (dpyinfo->display)) { XNextEvent (dpyinfo->display, event); return; } - fd = XConnectionNumber (dpyinfo->display); - if (fd > maxfd) maxfd = fd; eassert (fd < FD_SETSIZE); - FD_SET (XConnectionNumber (dpyinfo->display), &fds); + FD_SET (fd, &fds); } eassert (maxfd >= 0); - /* We don't have to check the return of pselect, because if an + /* Continue to read input even if pselect fails, because if an error occurs XPending will call the IO error handler, which then brings us out of this loop. */ - pselect (maxfd + 1, &fds, NULL, NULL, NULL, NULL); + rc = pselect (maxfd + 1, &fds, NULL, NULL, NULL, NULL); + + if (rc >= 0) + rfds = fds; } } -- 2.39.2