Merge from emacs-24; up to 2012-12-30T19:34:25Z!jan.h.d@swipnet.se
[bpt/emacs.git] / src / xgselect.c
1 /* Function for handling the GLib event loop.
2
3 Copyright (C) 2009-2013 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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/>. */
19
20 #include <config.h>
21
22 #include "xgselect.h"
23
24 #ifdef HAVE_GLIB
25
26 #include <glib.h>
27 #include <errno.h>
28 #include "frame.h"
29
30 int
31 xg_select (int fds_lim, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
32 EMACS_TIME *timeout, sigset_t *sigmask)
33 {
34 SELECT_TYPE all_rfds, all_wfds;
35 EMACS_TIME tmo, *tmop = timeout;
36
37 GMainContext *context;
38 int have_wfds = wfds != NULL;
39 GPollFD gfds_buf[128];
40 GPollFD *gfds = gfds_buf;
41 int gfds_size = sizeof gfds_buf / sizeof *gfds_buf;
42 int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
43 int i, nfds, tmo_in_millisec;
44 USE_SAFE_ALLOCA;
45
46 /* Do not try to optimize with an initial check with g_main_context_pending
47 and a call to pselect if it returns false. If Gdk has a timeout for 0.01
48 second, and Emacs has a timeout for 1 second, g_main_context_pending will
49 return false, but the timeout will be 1 second, thus missing the gdk
50 timeout with a lot. */
51
52 context = g_main_context_default ();
53
54 if (rfds) all_rfds = *rfds;
55 else FD_ZERO (&all_rfds);
56 if (wfds) all_wfds = *wfds;
57 else FD_ZERO (&all_wfds);
58
59 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
60 gfds, gfds_size);
61 if (gfds_size < n_gfds)
62 {
63 SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds);
64 gfds_size = n_gfds;
65 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
66 gfds, gfds_size);
67 }
68
69 for (i = 0; i < n_gfds; ++i)
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 SAFE_FREE ();
85
86 if (tmo_in_millisec >= 0)
87 {
88 tmo = make_emacs_time (tmo_in_millisec / 1000,
89 1000 * 1000 * (tmo_in_millisec % 1000));
90 if (!timeout || EMACS_TIME_LT (tmo, *timeout))
91 tmop = &tmo;
92 }
93
94 fds_lim = max_fds + 1;
95 nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL,
96 efds, tmop, sigmask);
97
98 if (nfds < 0)
99 retval = nfds;
100 else if (nfds > 0)
101 {
102 for (i = 0; i < fds_lim; ++i)
103 {
104 if (FD_ISSET (i, &all_rfds))
105 {
106 if (rfds && FD_ISSET (i, rfds)) ++retval;
107 else ++our_fds;
108 }
109 else if (rfds)
110 FD_CLR (i, rfds);
111
112 if (have_wfds && FD_ISSET (i, &all_wfds))
113 {
114 if (wfds && FD_ISSET (i, wfds)) ++retval;
115 else ++our_fds;
116 }
117 else if (wfds)
118 FD_CLR (i, wfds);
119
120 if (efds && FD_ISSET (i, efds))
121 ++retval;
122 }
123 }
124
125 if (our_fds > 0 || (nfds == 0 && tmop == &tmo))
126 {
127
128 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
129 unless retval is zero. */
130 #ifdef USE_GTK
131 if (retval == 0)
132 #endif
133 while (g_main_context_pending (context))
134 g_main_context_dispatch (context);
135
136 /* To not have to recalculate timeout, return like this. */
137 if (retval == 0)
138 {
139 retval = -1;
140 errno = EINTR;
141 }
142 }
143
144 return retval;
145 }
146 #endif /* HAVE_GLIB */