Fix description of 'C-z' in User manual
[emacs.git] / src / xgselect.c
blobac88afdd54bd448dd58affca291d33aa99c9ae26
1 /* Function for handling the GLib event loop.
3 Copyright (C) 2009-2016 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 #include "xgselect.h"
24 #ifdef HAVE_GLIB
26 #include <glib.h>
27 #include <errno.h>
28 #include <stdbool.h>
29 #include "blockinput.h"
30 #include "systime.h"
32 /* `xg_select' is a `pselect' replacement. Why do we need a separate function?
33 1. Timeouts. Glib and Gtk rely on timer events. If we did pselect
34 with a greater timeout then the one scheduled by Glib, we would
35 not allow Glib to process its timer events. We want Glib to
36 work smoothly, so we need to reduce our timeout to match Glib.
37 2. Descriptors. Glib may listen to more file descriptors than we do.
38 So we add Glib descriptors to our pselect pool, but we don't change
39 the value returned by the function. The return value matches only
40 the descriptors passed as arguments, making it compatible with
41 plain pselect. */
43 int
44 xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds,
45 struct timespec const *timeout, sigset_t const *sigmask)
47 fd_set all_rfds, all_wfds;
48 struct timespec tmo;
49 struct timespec const *tmop = timeout;
51 GMainContext *context;
52 bool have_wfds = wfds != NULL;
53 GPollFD gfds_buf[128];
54 GPollFD *gfds = gfds_buf;
55 int gfds_size = ARRAYELTS (gfds_buf);
56 int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
57 bool context_acquired = false;
58 int i, nfds, tmo_in_millisec;
59 bool need_to_dispatch;
60 USE_SAFE_ALLOCA;
62 context = g_main_context_default ();
63 context_acquired = g_main_context_acquire (context);
64 /* FIXME: If we couldn't acquire the context, we just silently proceed
65 because this function handles more than just glib file descriptors.
66 Note that, as implemented, this failure is completely silent: there is
67 no feedback to the caller. */
69 if (rfds) all_rfds = *rfds;
70 else FD_ZERO (&all_rfds);
71 if (wfds) all_wfds = *wfds;
72 else FD_ZERO (&all_wfds);
74 n_gfds = (context_acquired
75 ? g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
76 gfds, gfds_size)
77 : -1);
79 if (gfds_size < n_gfds)
81 SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds);
82 gfds_size = n_gfds;
83 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
84 gfds, gfds_size);
87 for (i = 0; i < n_gfds; ++i)
89 if (gfds[i].events & G_IO_IN)
91 FD_SET (gfds[i].fd, &all_rfds);
92 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
94 if (gfds[i].events & G_IO_OUT)
96 FD_SET (gfds[i].fd, &all_wfds);
97 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
98 have_wfds = true;
102 SAFE_FREE ();
104 if (n_gfds >= 0 && tmo_in_millisec >= 0)
106 tmo = make_timespec (tmo_in_millisec / 1000,
107 1000 * 1000 * (tmo_in_millisec % 1000));
108 if (!timeout || timespec_cmp (tmo, *timeout) < 0)
109 tmop = &tmo;
112 fds_lim = max_fds + 1;
113 nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL,
114 efds, tmop, sigmask);
116 if (nfds < 0)
117 retval = nfds;
118 else if (nfds > 0)
120 for (i = 0; i < fds_lim; ++i)
122 if (FD_ISSET (i, &all_rfds))
124 if (rfds && FD_ISSET (i, rfds)) ++retval;
125 else ++our_fds;
127 else if (rfds)
128 FD_CLR (i, rfds);
130 if (have_wfds && FD_ISSET (i, &all_wfds))
132 if (wfds && FD_ISSET (i, wfds)) ++retval;
133 else ++our_fds;
135 else if (wfds)
136 FD_CLR (i, wfds);
138 if (efds && FD_ISSET (i, efds))
139 ++retval;
143 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
144 unless retval is zero. */
145 #ifdef USE_GTK
146 need_to_dispatch = retval == 0;
147 #else
148 need_to_dispatch = true;
149 #endif
150 if (need_to_dispatch)
152 int pselect_errno = errno;
153 /* Prevent g_main_dispatch recursion, that would occur without
154 block_input wrapper, because event handlers call
155 unblock_input. Event loop recursion was causing Bug#15801. */
156 block_input ();
157 while (g_main_context_pending (context))
158 g_main_context_dispatch (context);
159 unblock_input ();
160 errno = pselect_errno;
163 if (context_acquired)
164 g_main_context_release (context);
166 /* To not have to recalculate timeout, return like this. */
167 if ((our_fds > 0 || (nfds == 0 && tmop == &tmo)) && (retval == 0))
169 retval = -1;
170 errno = EINTR;
173 return retval;
175 #endif /* HAVE_GLIB */