Merge from emacs-23; up to 2010-06-10T12:56:11Z!michael.albinus@gmx.de.
[emacs.git] / src / xgselect.c
blob0d154f6496ad8e56c08a2f8cc59724cbbd60f618
1 /* Function for handling the GLib event loop.
3 Copyright (C) 2009-2011 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 <setjmp.h>
23 #include "xgselect.h"
25 #if defined (USE_GTK) || defined (HAVE_GCONF)
27 #include <glib.h>
28 #include <errno.h>
29 #include <setjmp.h>
31 static GPollFD *gfds;
32 static int gfds_size;
34 int
35 xg_select (int max_fds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
36 EMACS_TIME *timeout)
38 SELECT_TYPE all_rfds, all_wfds;
39 EMACS_TIME tmo, *tmop = timeout;
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;
44 int i, nfds, tmo_in_millisec;
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);
51 /* Update event sources in GLib. */
52 g_main_context_pending (context);
54 do {
55 if (n_gfds > gfds_size)
57 while (n_gfds > gfds_size)
58 gfds_size *= 2;
59 xfree (gfds);
60 gfds = xmalloc (sizeof (*gfds) * gfds_size);
63 n_gfds = g_main_context_query (context,
64 G_PRIORITY_LOW,
65 &tmo_in_millisec,
66 gfds,
67 gfds_size);
68 } while (n_gfds > gfds_size);
70 for (i = 0; i < n_gfds; ++i)
72 if (gfds[i].events & G_IO_IN)
74 FD_SET (gfds[i].fd, &all_rfds);
75 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
77 if (gfds[i].events & G_IO_OUT)
79 FD_SET (gfds[i].fd, &all_wfds);
80 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
81 have_wfds = 1;
85 if (tmo_in_millisec >= 0)
87 EMACS_SET_SECS_USECS (tmo, tmo_in_millisec/1000,
88 1000 * (tmo_in_millisec % 1000));
89 if (!timeout) our_tmo = 1;
90 else
92 EMACS_TIME difference;
94 EMACS_SUB_TIME (difference, tmo, *timeout);
95 if (EMACS_TIME_NEG_P (difference)) our_tmo = 1;
98 if (our_tmo) tmop = &tmo;
101 nfds = select (max_fds+1, &all_rfds, have_wfds ? &all_wfds : NULL,
102 efds, tmop);
104 if (nfds < 0)
105 retval = nfds;
106 else if (nfds > 0)
108 for (i = 0; i < max_fds+1; ++i)
110 if (FD_ISSET (i, &all_rfds))
112 if (rfds && FD_ISSET (i, rfds)) ++retval;
113 else ++our_fds;
115 else if (rfds)
116 FD_CLR (i, rfds);
118 if (have_wfds && FD_ISSET (i, &all_wfds))
120 if (wfds && FD_ISSET (i, wfds)) ++retval;
121 else ++our_fds;
123 else if (wfds)
124 FD_CLR (i, wfds);
126 if (efds && FD_ISSET (i, efds))
127 ++retval;
131 if (our_fds > 0 || (nfds == 0 && our_tmo))
134 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
135 unless retval is zero. */
136 #ifdef USE_GTK
137 if (retval == 0)
138 #endif
139 while (g_main_context_pending (context))
140 g_main_context_dispatch (context);
142 /* To not have to recalculate timeout, return like this. */
143 if (retval == 0)
145 retval = -1;
146 errno = EINTR;
150 return retval;
152 #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */
154 void
155 xgselect_initialize (void)
157 #if defined (USE_GTK) || defined (HAVE_GCONF)
158 gfds_size = 128;
159 gfds = xmalloc (sizeof (*gfds)*gfds_size);
160 #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */