Rearrange pointer logging
[emacs.git] / src / xgselect.c
blob5f71ff8401464e5c1043ce9abab2b260b97445bf
1 /* Function for handling the GLib event loop.
3 Copyright (C) 2009-2014 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
10 (at 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 <timespec.h>
30 #include "frame.h"
32 int
33 xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds,
34 struct timespec const *timeout, sigset_t const *sigmask)
36 fd_set all_rfds, all_wfds;
37 struct timespec tmo;
38 struct timespec const *tmop = timeout;
40 GMainContext *context;
41 int have_wfds = wfds != NULL;
42 GPollFD gfds_buf[128];
43 GPollFD *gfds = gfds_buf;
44 int gfds_size = ARRAYELTS (gfds_buf);
45 int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
46 int i, nfds, tmo_in_millisec;
47 bool need_to_dispatch;
48 USE_SAFE_ALLOCA;
50 /* Do not try to optimize with an initial check with g_main_context_pending
51 and a call to pselect if it returns false. If Gdk has a timeout for 0.01
52 second, and Emacs has a timeout for 1 second, g_main_context_pending will
53 return false, but the timeout will be 1 second, thus missing the gdk
54 timeout with a lot. */
56 context = g_main_context_default ();
58 if (rfds) all_rfds = *rfds;
59 else FD_ZERO (&all_rfds);
60 if (wfds) all_wfds = *wfds;
61 else FD_ZERO (&all_wfds);
63 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
64 gfds, gfds_size);
65 if (gfds_size < n_gfds)
67 SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds);
68 gfds_size = n_gfds;
69 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
70 gfds, gfds_size);
73 for (i = 0; i < n_gfds; ++i)
75 if (gfds[i].events & G_IO_IN)
77 FD_SET (gfds[i].fd, &all_rfds);
78 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
80 if (gfds[i].events & G_IO_OUT)
82 FD_SET (gfds[i].fd, &all_wfds);
83 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
84 have_wfds = 1;
88 SAFE_FREE ();
90 if (tmo_in_millisec >= 0)
92 tmo = make_timespec (tmo_in_millisec / 1000,
93 1000 * 1000 * (tmo_in_millisec % 1000));
94 if (!timeout || timespec_cmp (tmo, *timeout) < 0)
95 tmop = &tmo;
98 fds_lim = max_fds + 1;
99 nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL,
100 efds, tmop, sigmask);
102 if (nfds < 0)
103 retval = nfds;
104 else if (nfds > 0)
106 for (i = 0; i < fds_lim; ++i)
108 if (FD_ISSET (i, &all_rfds))
110 if (rfds && FD_ISSET (i, rfds)) ++retval;
111 else ++our_fds;
113 else if (rfds)
114 FD_CLR (i, rfds);
116 if (have_wfds && FD_ISSET (i, &all_wfds))
118 if (wfds && FD_ISSET (i, wfds)) ++retval;
119 else ++our_fds;
121 else if (wfds)
122 FD_CLR (i, wfds);
124 if (efds && FD_ISSET (i, efds))
125 ++retval;
129 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
130 unless retval is zero. */
131 #ifdef USE_GTK
132 need_to_dispatch = retval == 0;
133 #else
134 need_to_dispatch = true;
135 #endif
136 if (need_to_dispatch)
138 int pselect_errno = errno;
139 while (g_main_context_pending (context))
140 g_main_context_dispatch (context);
141 errno = pselect_errno;
144 /* To not have to recalculate timeout, return like this. */
145 if ((our_fds > 0 || (nfds == 0 && tmop == &tmo)) && (retval == 0))
147 retval = -1;
148 errno = EINTR;
151 return retval;
153 #endif /* HAVE_GLIB */