* Makefile.in (install-arch-indep): Remove unneeded chmod.
[emacs.git] / src / xgselect.c
blob8b5ee68e55b8ee043b5f33d7fc7d4bc08107a410
1 /* Function for handling the GLib event loop.
3 Copyright (C) 2009-2012 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) || defined (HAVE_GSETTINGS)
27 #include <glib.h>
28 #include <errno.h>
29 #include <setjmp.h>
31 static GPollFD *gfds;
32 static ptrdiff_t 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;
42 int have_wfds = wfds != NULL;
43 int n_gfds = 0, our_tmo = 0, retval = 0, our_fds = 0;
44 int i, nfds, fds_lim, tmo_in_millisec;
46 if (inhibit_window_system || !display_arg)
47 return select (max_fds, rfds, wfds, efds, timeout);
49 if (rfds) memcpy (&all_rfds, rfds, sizeof (all_rfds));
50 else FD_ZERO (&all_rfds);
51 if (wfds) memcpy (&all_wfds, wfds, sizeof (all_rfds));
52 else FD_ZERO (&all_wfds);
54 /* Update event sources in GLib. */
55 context = g_main_context_default ();
56 g_main_context_pending (context);
58 do {
59 if (n_gfds > gfds_size)
61 xfree (gfds);
62 gfds = xpalloc (0, &gfds_size, n_gfds - gfds_size, INT_MAX,
63 sizeof *gfds);
66 n_gfds = g_main_context_query (context,
67 G_PRIORITY_LOW,
68 &tmo_in_millisec,
69 gfds,
70 gfds_size);
71 } while (n_gfds > gfds_size);
73 for (i = 0; i < n_gfds; ++i)
75 if (gfds[i].events & G_IO_IN)
77 FD_SET (gfds[i].fd, &all_rfds);
78 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
80 if (gfds[i].events & G_IO_OUT)
82 FD_SET (gfds[i].fd, &all_wfds);
83 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
84 have_wfds = 1;
88 if (tmo_in_millisec >= 0)
90 EMACS_SET_SECS_USECS (tmo, tmo_in_millisec/1000,
91 1000 * (tmo_in_millisec % 1000));
92 if (!timeout) our_tmo = 1;
93 else
95 EMACS_TIME difference;
97 EMACS_SUB_TIME (difference, tmo, *timeout);
98 if (EMACS_TIME_NEG_P (difference)) our_tmo = 1;
101 if (our_tmo) tmop = &tmo;
104 fds_lim = max_fds + 1;
105 nfds = select (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL, efds, tmop);
107 if (nfds < 0)
108 retval = nfds;
109 else if (nfds > 0)
111 for (i = 0; i < fds_lim; ++i)
113 if (FD_ISSET (i, &all_rfds))
115 if (rfds && FD_ISSET (i, rfds)) ++retval;
116 else ++our_fds;
118 else if (rfds)
119 FD_CLR (i, rfds);
121 if (have_wfds && FD_ISSET (i, &all_wfds))
123 if (wfds && FD_ISSET (i, wfds)) ++retval;
124 else ++our_fds;
126 else if (wfds)
127 FD_CLR (i, wfds);
129 if (efds && FD_ISSET (i, efds))
130 ++retval;
134 if (our_fds > 0 || (nfds == 0 && our_tmo))
137 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
138 unless retval is zero. */
139 #ifdef USE_GTK
140 if (retval == 0)
141 #endif
142 while (g_main_context_pending (context))
143 g_main_context_dispatch (context);
145 /* To not have to recalculate timeout, return like this. */
146 if (retval == 0)
148 retval = -1;
149 errno = EINTR;
153 return retval;
155 #endif /* USE_GTK || HAVE_GCONF || HAVE_GSETTINGS */
157 void
158 xgselect_initialize (void)
160 #if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
161 gfds_size = 128;
162 gfds = xmalloc (sizeof (*gfds)*gfds_size);
163 #endif