Minor fixes to the docs.
[bpt/emacs.git] / src / xgselect.c
CommitLineData
872870b2 1/* Function for handling the GLib event loop.
95df8112 2
ba318903 3Copyright (C) 2009-2014 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>
e6e8a5eb 28#include <stdbool.h>
43aac990 29#include <timespec.h>
7452b7bd 30#include "frame.h"
872870b2 31
872870b2 32int
d486344e 33xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds,
43aac990 34 struct timespec const *timeout, sigset_t const *sigmask)
872870b2 35{
3a31cae4 36 fd_set all_rfds, all_wfds;
43aac990
PE
37 struct timespec tmo;
38 struct timespec const *tmop = timeout;
872870b2 39
b0572523 40 GMainContext *context;
3a31cae4 41 int have_wfds = wfds != NULL;
0f46bc75
PE
42 GPollFD gfds_buf[128];
43 GPollFD *gfds = gfds_buf;
44 int gfds_size = sizeof gfds_buf / sizeof *gfds_buf;
3a31cae4 45 int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
ff23cd9f 46 int i, nfds, tmo_in_millisec;
e6e8a5eb 47 bool need_to_dispatch;
0f46bc75 48 USE_SAFE_ALLOCA;
872870b2 49
5de0e011
JD
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. */
55
56 context = g_main_context_default ();
b0572523 57
ae1d87e2 58 if (rfds) all_rfds = *rfds;
872870b2 59 else FD_ZERO (&all_rfds);
ae1d87e2 60 if (wfds) all_wfds = *wfds;
872870b2
JD
61 else FD_ZERO (&all_wfds);
62
0f46bc75
PE
63 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
64 gfds, gfds_size);
65 if (gfds_size < n_gfds)
66 {
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);
71 }
872870b2 72
41729b81 73 for (i = 0; i < n_gfds; ++i)
3a31cae4
PE
74 {
75 if (gfds[i].events & G_IO_IN)
76 {
77 FD_SET (gfds[i].fd, &all_rfds);
78 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
79 }
80 if (gfds[i].events & G_IO_OUT)
81 {
82 FD_SET (gfds[i].fd, &all_wfds);
83 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
84 have_wfds = 1;
85 }
86 }
872870b2 87
0f46bc75
PE
88 SAFE_FREE ();
89
872870b2
JD
90 if (tmo_in_millisec >= 0)
91 {
43aac990
PE
92 tmo = make_timespec (tmo_in_millisec / 1000,
93 1000 * 1000 * (tmo_in_millisec % 1000));
94 if (!timeout || timespec_cmp (tmo, *timeout) < 0)
d35af63c 95 tmop = &tmo;
872870b2
JD
96 }
97
3a31cae4
PE
98 fds_lim = max_fds + 1;
99 nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL,
100 efds, tmop, sigmask);
872870b2
JD
101
102 if (nfds < 0)
103 retval = nfds;
3a31cae4 104 else if (nfds > 0)
872870b2 105 {
97107e2e 106 for (i = 0; i < fds_lim; ++i)
872870b2 107 {
3a31cae4
PE
108 if (FD_ISSET (i, &all_rfds))
109 {
110 if (rfds && FD_ISSET (i, rfds)) ++retval;
111 else ++our_fds;
112 }
113 else if (rfds)
114 FD_CLR (i, rfds);
115
116 if (have_wfds && FD_ISSET (i, &all_wfds))
117 {
118 if (wfds && FD_ISSET (i, wfds)) ++retval;
119 else ++our_fds;
120 }
121 else if (wfds)
122 FD_CLR (i, wfds);
123
124 if (efds && FD_ISSET (i, efds))
125 ++retval;
872870b2
JD
126 }
127 }
128
86d2bf49
MA
129 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
130 unless retval is zero. */
872870b2 131#ifdef USE_GTK
e6e8a5eb
PE
132 need_to_dispatch = retval == 0;
133#else
134 need_to_dispatch = true;
872870b2 135#endif
e6e8a5eb
PE
136 if (need_to_dispatch)
137 {
138 int pselect_errno = errno;
139 while (g_main_context_pending (context))
140 g_main_context_dispatch (context);
141 errno = pselect_errno;
142 }
872870b2 143
86d2bf49 144 /* To not have to recalculate timeout, return like this. */
3a31cae4 145 if ((our_fds > 0 || (nfds == 0 && tmop == &tmo)) && (retval == 0))
86d2bf49
MA
146 {
147 retval = -1;
148 errno = EINTR;
872870b2
JD
149 }
150
151 return retval;
152}
55a87246 153#endif /* HAVE_GLIB */