* doc/misc/efaq.texi: Rename from faq.texi, to match its output files.
[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,
f462f075 32 EMACS_TIME const *timeout, sigset_t const *sigmask)
872870b2
JD
33{
34 SELECT_TYPE all_rfds, all_wfds;
f462f075
PE
35 EMACS_TIME tmo;
36 EMACS_TIME const *tmop = timeout;
872870b2 37
b0572523 38 GMainContext *context;
872870b2 39 int have_wfds = wfds != NULL;
0f46bc75
PE
40 GPollFD gfds_buf[128];
41 GPollFD *gfds = gfds_buf;
42 int gfds_size = sizeof gfds_buf / sizeof *gfds_buf;
43 int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
ff23cd9f 44 int i, nfds, tmo_in_millisec;
0f46bc75 45 USE_SAFE_ALLOCA;
872870b2 46
5de0e011
JD
47 /* Do not try to optimize with an initial check with g_main_context_pending
48 and a call to pselect if it returns false. If Gdk has a timeout for 0.01
49 second, and Emacs has a timeout for 1 second, g_main_context_pending will
50 return false, but the timeout will be 1 second, thus missing the gdk
51 timeout with a lot. */
52
53 context = g_main_context_default ();
b0572523 54
ae1d87e2 55 if (rfds) all_rfds = *rfds;
872870b2 56 else FD_ZERO (&all_rfds);
ae1d87e2 57 if (wfds) all_wfds = *wfds;
872870b2
JD
58 else FD_ZERO (&all_wfds);
59
0f46bc75
PE
60 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
61 gfds, gfds_size);
62 if (gfds_size < n_gfds)
63 {
64 SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds);
65 gfds_size = n_gfds;
66 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
67 gfds, gfds_size);
68 }
872870b2 69
41729b81 70 for (i = 0; i < n_gfds; ++i)
872870b2
JD
71 {
72 if (gfds[i].events & G_IO_IN)
73 {
74 FD_SET (gfds[i].fd, &all_rfds);
75 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
76 }
77 if (gfds[i].events & G_IO_OUT)
78 {
79 FD_SET (gfds[i].fd, &all_wfds);
80 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
81 have_wfds = 1;
82 }
83 }
84
0f46bc75
PE
85 SAFE_FREE ();
86
872870b2
JD
87 if (tmo_in_millisec >= 0)
88 {
e9a9ae03
PE
89 tmo = make_emacs_time (tmo_in_millisec / 1000,
90 1000 * 1000 * (tmo_in_millisec % 1000));
d35af63c
PE
91 if (!timeout || EMACS_TIME_LT (tmo, *timeout))
92 tmop = &tmo;
872870b2
JD
93 }
94
97107e2e 95 fds_lim = max_fds + 1;
d35af63c
PE
96 nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL,
97 efds, tmop, sigmask);
872870b2
JD
98
99 if (nfds < 0)
100 retval = nfds;
41729b81 101 else if (nfds > 0)
872870b2 102 {
97107e2e 103 for (i = 0; i < fds_lim; ++i)
872870b2
JD
104 {
105 if (FD_ISSET (i, &all_rfds))
106 {
107 if (rfds && FD_ISSET (i, rfds)) ++retval;
108 else ++our_fds;
109 }
42d3022b
J
110 else if (rfds)
111 FD_CLR (i, rfds);
112
872870b2
JD
113 if (have_wfds && FD_ISSET (i, &all_wfds))
114 {
115 if (wfds && FD_ISSET (i, wfds)) ++retval;
116 else ++our_fds;
117 }
42d3022b
J
118 else if (wfds)
119 FD_CLR (i, wfds);
120
872870b2
JD
121 if (efds && FD_ISSET (i, efds))
122 ++retval;
123 }
124 }
125
d35af63c 126 if (our_fds > 0 || (nfds == 0 && tmop == &tmo))
872870b2 127 {
41729b81 128
872870b2
JD
129 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
130 unless retval is zero. */
131#ifdef USE_GTK
132 if (retval == 0)
133#endif
134 while (g_main_context_pending (context))
135 g_main_context_dispatch (context);
136
137 /* To not have to recalculate timeout, return like this. */
41729b81 138 if (retval == 0)
872870b2
JD
139 {
140 retval = -1;
141 errno = EINTR;
142 }
143 }
144
145 return retval;
146}
55a87246 147#endif /* HAVE_GLIB */