Fix an off by one error in c-guess-basic-syntax CASE 5B.1.
[bpt/emacs.git] / src / xgselect.c
CommitLineData
872870b2 1/* Function for handling the GLib event loop.
95df8112 2
acaf905b 3Copyright (C) 2009-2012 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
0949d2b6 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 <setjmp.h>
23#include "xgselect.h"
24
0949d2b6 25#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
aefd87e1 26
872870b2
JD
27#include <glib.h>
28#include <errno.h>
29#include <setjmp.h>
872870b2
JD
30
31static GPollFD *gfds;
0065d054 32static ptrdiff_t gfds_size;
872870b2
JD
33
34int
e5447b22
JB
35xg_select (int max_fds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
36 EMACS_TIME *timeout)
872870b2
JD
37{
38 SELECT_TYPE all_rfds, all_wfds;
39 EMACS_TIME tmo, *tmop = timeout;
40
41 GMainContext *context = g_main_context_default ();
42 int have_wfds = wfds != NULL;
43 int n_gfds = 0, our_tmo = 0, retval = 0, our_fds = 0;
41729b81 44 int i, nfds, tmo_in_millisec;
872870b2
JD
45
46 if (rfds) memcpy (&all_rfds, rfds, sizeof (all_rfds));
47 else FD_ZERO (&all_rfds);
48 if (wfds) memcpy (&all_wfds, wfds, sizeof (all_rfds));
49 else FD_ZERO (&all_wfds);
50
51 /* Update event sources in GLib. */
52 g_main_context_pending (context);
53
54 do {
41729b81 55 if (n_gfds > gfds_size)
872870b2 56 {
872870b2 57 xfree (gfds);
0065d054
PE
58 gfds = xpalloc (0, &gfds_size, n_gfds - gfds_size, INT_MAX,
59 sizeof *gfds);
872870b2
JD
60 }
61
62 n_gfds = g_main_context_query (context,
63 G_PRIORITY_LOW,
64 &tmo_in_millisec,
65 gfds,
66 gfds_size);
67 } while (n_gfds > gfds_size);
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
84 if (tmo_in_millisec >= 0)
85 {
86 EMACS_SET_SECS_USECS (tmo, tmo_in_millisec/1000,
87 1000 * (tmo_in_millisec % 1000));
88 if (!timeout) our_tmo = 1;
89 else
90 {
91 EMACS_TIME difference;
41729b81 92
872870b2
JD
93 EMACS_SUB_TIME (difference, tmo, *timeout);
94 if (EMACS_TIME_NEG_P (difference)) our_tmo = 1;
95 }
96
97 if (our_tmo) tmop = &tmo;
98 }
99
100 nfds = select (max_fds+1, &all_rfds, have_wfds ? &all_wfds : NULL,
101 efds, tmop);
102
103 if (nfds < 0)
104 retval = nfds;
41729b81 105 else if (nfds > 0)
872870b2
JD
106 {
107 for (i = 0; i < max_fds+1; ++i)
108 {
109 if (FD_ISSET (i, &all_rfds))
110 {
111 if (rfds && FD_ISSET (i, rfds)) ++retval;
112 else ++our_fds;
113 }
42d3022b
J
114 else if (rfds)
115 FD_CLR (i, rfds);
116
872870b2
JD
117 if (have_wfds && FD_ISSET (i, &all_wfds))
118 {
119 if (wfds && FD_ISSET (i, wfds)) ++retval;
120 else ++our_fds;
121 }
42d3022b
J
122 else if (wfds)
123 FD_CLR (i, wfds);
124
872870b2
JD
125 if (efds && FD_ISSET (i, efds))
126 ++retval;
127 }
128 }
129
130 if (our_fds > 0 || (nfds == 0 && our_tmo))
131 {
41729b81 132
872870b2
JD
133 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
134 unless retval is zero. */
135#ifdef USE_GTK
136 if (retval == 0)
137#endif
138 while (g_main_context_pending (context))
139 g_main_context_dispatch (context);
140
141 /* To not have to recalculate timeout, return like this. */
41729b81 142 if (retval == 0)
872870b2
JD
143 {
144 retval = -1;
145 errno = EINTR;
146 }
147 }
148
149 return retval;
150}
0949d2b6 151#endif /* USE_GTK || HAVE_GCONF || HAVE_GSETTINGS */
872870b2
JD
152
153void
971de7fb 154xgselect_initialize (void)
872870b2 155{
0949d2b6 156#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
872870b2
JD
157 gfds_size = 128;
158 gfds = xmalloc (sizeof (*gfds)*gfds_size);
0949d2b6 159#endif
872870b2 160}