New unwind-protect flavors to better type-check C callbacks.
[bpt/emacs.git] / src / xgselect.c
CommitLineData
872870b2 1/* Function for handling the GLib event loop.
95df8112 2
ab422c4d 3Copyright (C) 2009-2013 Free Software Foundation, Inc.
872870b2
JD
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
a2332e8d 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
872870b2 19
08a494a3 20#include <config.h>
872870b2 21
aefd87e1
PE
22#include "xgselect.h"
23
55a87246 24#ifdef HAVE_GLIB
aefd87e1 25
872870b2
JD
26#include <glib.h>
27#include <errno.h>
7452b7bd 28#include "frame.h"
872870b2 29
872870b2 30int
ff23cd9f 31xg_select (int fds_lim, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
d35af63c 32 EMACS_TIME *timeout, sigset_t *sigmask)
872870b2
JD
33{
34 SELECT_TYPE all_rfds, all_wfds;
35 EMACS_TIME tmo, *tmop = timeout;
36
b0572523 37 GMainContext *context;
872870b2 38 int have_wfds = wfds != NULL;
0f46bc75
PE
39 GPollFD gfds_buf[128];
40 GPollFD *gfds = gfds_buf;
41 int gfds_size = sizeof gfds_buf / sizeof *gfds_buf;
42 int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
ff23cd9f 43 int i, nfds, tmo_in_millisec;
0f46bc75 44 USE_SAFE_ALLOCA;
872870b2 45
5de0e011
JD
46 /* Do not try to optimize with an initial check with g_main_context_pending
47 and a call to pselect if it returns false. If Gdk has a timeout for 0.01
48 second, and Emacs has a timeout for 1 second, g_main_context_pending will
49 return false, but the timeout will be 1 second, thus missing the gdk
50 timeout with a lot. */
51
52 context = g_main_context_default ();
b0572523 53
ae1d87e2 54 if (rfds) all_rfds = *rfds;
872870b2 55 else FD_ZERO (&all_rfds);
ae1d87e2 56 if (wfds) all_wfds = *wfds;
872870b2
JD
57 else FD_ZERO (&all_wfds);
58
0f46bc75
PE
59 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
60 gfds, gfds_size);
61 if (gfds_size < n_gfds)
62 {
63 SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds);
64 gfds_size = n_gfds;
65 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
66 gfds, gfds_size);
67 }
872870b2 68
41729b81 69 for (i = 0; i < n_gfds; ++i)
872870b2
JD
70 {
71 if (gfds[i].events & G_IO_IN)
72 {
73 FD_SET (gfds[i].fd, &all_rfds);
74 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
75 }
76 if (gfds[i].events & G_IO_OUT)
77 {
78 FD_SET (gfds[i].fd, &all_wfds);
79 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
80 have_wfds = 1;
81 }
82 }
83
0f46bc75
PE
84 SAFE_FREE ();
85
872870b2
JD
86 if (tmo_in_millisec >= 0)
87 {
e9a9ae03
PE
88 tmo = make_emacs_time (tmo_in_millisec / 1000,
89 1000 * 1000 * (tmo_in_millisec % 1000));
d35af63c
PE
90 if (!timeout || EMACS_TIME_LT (tmo, *timeout))
91 tmop = &tmo;
872870b2
JD
92 }
93
97107e2e 94 fds_lim = max_fds + 1;
d35af63c
PE
95 nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL,
96 efds, tmop, sigmask);
872870b2
JD
97
98 if (nfds < 0)
99 retval = nfds;
41729b81 100 else if (nfds > 0)
872870b2 101 {
97107e2e 102 for (i = 0; i < fds_lim; ++i)
872870b2
JD
103 {
104 if (FD_ISSET (i, &all_rfds))
105 {
106 if (rfds && FD_ISSET (i, rfds)) ++retval;
107 else ++our_fds;
108 }
42d3022b
J
109 else if (rfds)
110 FD_CLR (i, rfds);
111
872870b2
JD
112 if (have_wfds && FD_ISSET (i, &all_wfds))
113 {
114 if (wfds && FD_ISSET (i, wfds)) ++retval;
115 else ++our_fds;
116 }
42d3022b
J
117 else if (wfds)
118 FD_CLR (i, wfds);
119
872870b2
JD
120 if (efds && FD_ISSET (i, efds))
121 ++retval;
122 }
123 }
124
d35af63c 125 if (our_fds > 0 || (nfds == 0 && tmop == &tmo))
872870b2 126 {
41729b81 127
872870b2
JD
128 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
129 unless retval is zero. */
130#ifdef USE_GTK
131 if (retval == 0)
132#endif
133 while (g_main_context_pending (context))
134 g_main_context_dispatch (context);
135
136 /* To not have to recalculate timeout, return like this. */
41729b81 137 if (retval == 0)
872870b2
JD
138 {
139 retval = -1;
140 errno = EINTR;
141 }
142 }
143
144 return retval;
145}
55a87246 146#endif /* HAVE_GLIB */