Commit | Line | Data |
---|---|---|
872870b2 | 1 | /* Function for handling the GLib event loop. |
95df8112 | 2 | |
ab422c4d | 3 | Copyright (C) 2009-2013 Free Software Foundation, Inc. |
872870b2 JD |
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 | |
a2332e8d | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
872870b2 | 19 | |
08a494a3 | 20 | #include <config.h> |
872870b2 | 21 | |
aefd87e1 PE |
22 | #include "xgselect.h" |
23 | ||
55a87246 | 24 | #ifdef HAVE_GLIB |
aefd87e1 | 25 | |
872870b2 JD |
26 | #include <glib.h> |
27 | #include <errno.h> | |
43aac990 | 28 | #include <timespec.h> |
7452b7bd | 29 | #include "frame.h" |
872870b2 | 30 | |
872870b2 | 31 | int |
d486344e | 32 | xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds, |
43aac990 | 33 | struct timespec const *timeout, sigset_t const *sigmask) |
872870b2 | 34 | { |
d486344e | 35 | fd_set all_rfds, all_wfds; |
43aac990 PE |
36 | struct timespec tmo; |
37 | struct timespec const *tmop = timeout; | |
872870b2 | 38 | |
b0572523 | 39 | GMainContext *context; |
872870b2 | 40 | int have_wfds = wfds != NULL; |
0f46bc75 PE |
41 | GPollFD gfds_buf[128]; |
42 | GPollFD *gfds = gfds_buf; | |
43 | int gfds_size = sizeof gfds_buf / sizeof *gfds_buf; | |
44 | int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1; | |
ff23cd9f | 45 | int i, nfds, tmo_in_millisec; |
0f46bc75 | 46 | USE_SAFE_ALLOCA; |
872870b2 | 47 | |
5de0e011 JD |
48 | /* Do not try to optimize with an initial check with g_main_context_pending |
49 | and a call to pselect if it returns false. If Gdk has a timeout for 0.01 | |
50 | second, and Emacs has a timeout for 1 second, g_main_context_pending will | |
51 | return false, but the timeout will be 1 second, thus missing the gdk | |
52 | timeout with a lot. */ | |
53 | ||
54 | context = g_main_context_default (); | |
b0572523 | 55 | |
ae1d87e2 | 56 | if (rfds) all_rfds = *rfds; |
872870b2 | 57 | else FD_ZERO (&all_rfds); |
ae1d87e2 | 58 | if (wfds) all_wfds = *wfds; |
872870b2 JD |
59 | else FD_ZERO (&all_wfds); |
60 | ||
0f46bc75 PE |
61 | n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec, |
62 | gfds, gfds_size); | |
63 | if (gfds_size < n_gfds) | |
64 | { | |
65 | SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds); | |
66 | gfds_size = n_gfds; | |
67 | n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec, | |
68 | gfds, gfds_size); | |
69 | } | |
872870b2 | 70 | |
41729b81 | 71 | for (i = 0; i < n_gfds; ++i) |
872870b2 JD |
72 | { |
73 | if (gfds[i].events & G_IO_IN) | |
74 | { | |
75 | FD_SET (gfds[i].fd, &all_rfds); | |
76 | if (gfds[i].fd > max_fds) max_fds = gfds[i].fd; | |
77 | } | |
78 | if (gfds[i].events & G_IO_OUT) | |
79 | { | |
80 | FD_SET (gfds[i].fd, &all_wfds); | |
81 | if (gfds[i].fd > max_fds) max_fds = gfds[i].fd; | |
82 | have_wfds = 1; | |
83 | } | |
84 | } | |
85 | ||
0f46bc75 PE |
86 | SAFE_FREE (); |
87 | ||
872870b2 JD |
88 | if (tmo_in_millisec >= 0) |
89 | { | |
43aac990 PE |
90 | tmo = make_timespec (tmo_in_millisec / 1000, |
91 | 1000 * 1000 * (tmo_in_millisec % 1000)); | |
92 | if (!timeout || timespec_cmp (tmo, *timeout) < 0) | |
d35af63c | 93 | tmop = &tmo; |
872870b2 JD |
94 | } |
95 | ||
97107e2e | 96 | fds_lim = max_fds + 1; |
d35af63c PE |
97 | nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL, |
98 | efds, tmop, sigmask); | |
872870b2 JD |
99 | |
100 | if (nfds < 0) | |
101 | retval = nfds; | |
41729b81 | 102 | else if (nfds > 0) |
872870b2 | 103 | { |
97107e2e | 104 | for (i = 0; i < fds_lim; ++i) |
872870b2 JD |
105 | { |
106 | if (FD_ISSET (i, &all_rfds)) | |
107 | { | |
108 | if (rfds && FD_ISSET (i, rfds)) ++retval; | |
109 | else ++our_fds; | |
110 | } | |
42d3022b J |
111 | else if (rfds) |
112 | FD_CLR (i, rfds); | |
113 | ||
872870b2 JD |
114 | if (have_wfds && FD_ISSET (i, &all_wfds)) |
115 | { | |
116 | if (wfds && FD_ISSET (i, wfds)) ++retval; | |
117 | else ++our_fds; | |
118 | } | |
42d3022b J |
119 | else if (wfds) |
120 | FD_CLR (i, wfds); | |
121 | ||
872870b2 JD |
122 | if (efds && FD_ISSET (i, efds)) |
123 | ++retval; | |
124 | } | |
125 | } | |
126 | ||
d35af63c | 127 | if (our_fds > 0 || (nfds == 0 && tmop == &tmo)) |
872870b2 | 128 | { |
41729b81 | 129 | |
872870b2 JD |
130 | /* If Gtk+ is in use eventually gtk_main_iteration will be called, |
131 | unless retval is zero. */ | |
132 | #ifdef USE_GTK | |
133 | if (retval == 0) | |
134 | #endif | |
135 | while (g_main_context_pending (context)) | |
136 | g_main_context_dispatch (context); | |
137 | ||
138 | /* To not have to recalculate timeout, return like this. */ | |
41729b81 | 139 | if (retval == 0) |
872870b2 JD |
140 | { |
141 | retval = -1; | |
142 | errno = EINTR; | |
143 | } | |
144 | } | |
145 | ||
146 | return retval; | |
147 | } | |
55a87246 | 148 | #endif /* HAVE_GLIB */ |