* lisp/profiler.el (profiler-start): Don't prompt for choice when there
[emacs.git] / src / xgselect.c
blob04ca00274e8ac821512636d016bcbdb215d7764c
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>
30 #include "xterm.h"
32 static GPollFD *gfds;
33 static ptrdiff_t gfds_size;
35 int
36 xg_select (int fds_lim, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
37 EMACS_TIME *timeout, sigset_t *sigmask)
39 SELECT_TYPE all_rfds, all_wfds;
40 EMACS_TIME tmo, *tmop = timeout;
42 GMainContext *context;
43 int have_wfds = wfds != NULL;
44 int n_gfds = 0, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
45 int i, nfds, tmo_in_millisec;
47 if (!x_in_use)
48 return pselect (fds_lim, rfds, wfds, efds, tmop, sigmask);
50 if (rfds) memcpy (&all_rfds, rfds, sizeof (all_rfds));
51 else FD_ZERO (&all_rfds);
52 if (wfds) memcpy (&all_wfds, wfds, sizeof (all_rfds));
53 else FD_ZERO (&all_wfds);
55 /* Update event sources in GLib. */
56 context = g_main_context_default ();
57 g_main_context_pending (context);
59 do {
60 if (n_gfds > gfds_size)
62 xfree (gfds);
63 gfds = xpalloc (0, &gfds_size, n_gfds - gfds_size, INT_MAX,
64 sizeof *gfds);
67 n_gfds = g_main_context_query (context,
68 G_PRIORITY_LOW,
69 &tmo_in_millisec,
70 gfds,
71 gfds_size);
72 } while (n_gfds > gfds_size);
74 for (i = 0; i < n_gfds; ++i)
76 if (gfds[i].events & G_IO_IN)
78 FD_SET (gfds[i].fd, &all_rfds);
79 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
81 if (gfds[i].events & G_IO_OUT)
83 FD_SET (gfds[i].fd, &all_wfds);
84 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
85 have_wfds = 1;
89 if (tmo_in_millisec >= 0)
91 tmo = make_emacs_time (tmo_in_millisec / 1000,
92 1000 * 1000 * (tmo_in_millisec % 1000));
93 if (!timeout || EMACS_TIME_LT (tmo, *timeout))
94 tmop = &tmo;
97 fds_lim = max_fds + 1;
98 nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL,
99 efds, tmop, sigmask);
101 if (nfds < 0)
102 retval = nfds;
103 else if (nfds > 0)
105 for (i = 0; i < fds_lim; ++i)
107 if (FD_ISSET (i, &all_rfds))
109 if (rfds && FD_ISSET (i, rfds)) ++retval;
110 else ++our_fds;
112 else if (rfds)
113 FD_CLR (i, rfds);
115 if (have_wfds && FD_ISSET (i, &all_wfds))
117 if (wfds && FD_ISSET (i, wfds)) ++retval;
118 else ++our_fds;
120 else if (wfds)
121 FD_CLR (i, wfds);
123 if (efds && FD_ISSET (i, efds))
124 ++retval;
128 if (our_fds > 0 || (nfds == 0 && tmop == &tmo))
131 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
132 unless retval is zero. */
133 #ifdef USE_GTK
134 if (retval == 0)
135 #endif
136 while (g_main_context_pending (context))
137 g_main_context_dispatch (context);
139 /* To not have to recalculate timeout, return like this. */
140 if (retval == 0)
142 retval = -1;
143 errno = EINTR;
147 return retval;
149 #endif /* USE_GTK || HAVE_GCONF || HAVE_GSETTINGS */
151 void
152 xgselect_initialize (void)
154 #if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
155 gfds_size = 128;
156 gfds = xmalloc (gfds_size * sizeof *gfds);
157 #endif