Commit | Line | Data |
---|---|---|
872870b2 | 1 | /* Function for handling the GLib event loop. |
95df8112 | 2 | |
acaf905b | 3 | Copyright (C) 2009-2012 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 | |
0949d2b6 | 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 <setjmp.h> |
23 | #include "xgselect.h" | |
24 | ||
0949d2b6 | 25 | #if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS) |
aefd87e1 | 26 | |
872870b2 JD |
27 | #include <glib.h> |
28 | #include <errno.h> | |
29 | #include <setjmp.h> | |
b2f4d39f | 30 | #include "xterm.h" |
872870b2 | 31 | |
872870b2 | 32 | int |
ff23cd9f | 33 | xg_select (int fds_lim, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds, |
d35af63c | 34 | EMACS_TIME *timeout, sigset_t *sigmask) |
872870b2 JD |
35 | { |
36 | SELECT_TYPE all_rfds, all_wfds; | |
37 | EMACS_TIME tmo, *tmop = timeout; | |
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 | |
0f46bc75 PE |
48 | if (! (x_in_use |
49 | && g_main_context_pending (context = g_main_context_default ()))) | |
50 | return pselect (fds_lim, rfds, wfds, efds, timeout, sigmask); | |
b0572523 | 51 | |
872870b2 JD |
52 | if (rfds) memcpy (&all_rfds, rfds, sizeof (all_rfds)); |
53 | else FD_ZERO (&all_rfds); | |
54 | if (wfds) memcpy (&all_wfds, wfds, sizeof (all_rfds)); | |
55 | else FD_ZERO (&all_wfds); | |
56 | ||
0f46bc75 PE |
57 | n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec, |
58 | gfds, gfds_size); | |
59 | if (gfds_size < n_gfds) | |
60 | { | |
61 | SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds); | |
62 | gfds_size = n_gfds; | |
63 | n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec, | |
64 | gfds, gfds_size); | |
65 | } | |
872870b2 | 66 | |
41729b81 | 67 | for (i = 0; i < n_gfds; ++i) |
872870b2 JD |
68 | { |
69 | if (gfds[i].events & G_IO_IN) | |
70 | { | |
71 | FD_SET (gfds[i].fd, &all_rfds); | |
72 | if (gfds[i].fd > max_fds) max_fds = gfds[i].fd; | |
73 | } | |
74 | if (gfds[i].events & G_IO_OUT) | |
75 | { | |
76 | FD_SET (gfds[i].fd, &all_wfds); | |
77 | if (gfds[i].fd > max_fds) max_fds = gfds[i].fd; | |
78 | have_wfds = 1; | |
79 | } | |
80 | } | |
81 | ||
0f46bc75 PE |
82 | SAFE_FREE (); |
83 | ||
872870b2 JD |
84 | if (tmo_in_millisec >= 0) |
85 | { | |
e9a9ae03 PE |
86 | tmo = make_emacs_time (tmo_in_millisec / 1000, |
87 | 1000 * 1000 * (tmo_in_millisec % 1000)); | |
d35af63c PE |
88 | if (!timeout || EMACS_TIME_LT (tmo, *timeout)) |
89 | tmop = &tmo; | |
872870b2 JD |
90 | } |
91 | ||
97107e2e | 92 | fds_lim = max_fds + 1; |
d35af63c PE |
93 | nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL, |
94 | efds, tmop, sigmask); | |
872870b2 JD |
95 | |
96 | if (nfds < 0) | |
97 | retval = nfds; | |
41729b81 | 98 | else if (nfds > 0) |
872870b2 | 99 | { |
97107e2e | 100 | for (i = 0; i < fds_lim; ++i) |
872870b2 JD |
101 | { |
102 | if (FD_ISSET (i, &all_rfds)) | |
103 | { | |
104 | if (rfds && FD_ISSET (i, rfds)) ++retval; | |
105 | else ++our_fds; | |
106 | } | |
42d3022b J |
107 | else if (rfds) |
108 | FD_CLR (i, rfds); | |
109 | ||
872870b2 JD |
110 | if (have_wfds && FD_ISSET (i, &all_wfds)) |
111 | { | |
112 | if (wfds && FD_ISSET (i, wfds)) ++retval; | |
113 | else ++our_fds; | |
114 | } | |
42d3022b J |
115 | else if (wfds) |
116 | FD_CLR (i, wfds); | |
117 | ||
872870b2 JD |
118 | if (efds && FD_ISSET (i, efds)) |
119 | ++retval; | |
120 | } | |
121 | } | |
122 | ||
d35af63c | 123 | if (our_fds > 0 || (nfds == 0 && tmop == &tmo)) |
872870b2 | 124 | { |
41729b81 | 125 | |
872870b2 JD |
126 | /* If Gtk+ is in use eventually gtk_main_iteration will be called, |
127 | unless retval is zero. */ | |
128 | #ifdef USE_GTK | |
129 | if (retval == 0) | |
130 | #endif | |
131 | while (g_main_context_pending (context)) | |
132 | g_main_context_dispatch (context); | |
133 | ||
134 | /* To not have to recalculate timeout, return like this. */ | |
41729b81 | 135 | if (retval == 0) |
872870b2 JD |
136 | { |
137 | retval = -1; | |
138 | errno = EINTR; | |
139 | } | |
140 | } | |
141 | ||
142 | return retval; | |
143 | } | |
0949d2b6 | 144 | #endif /* USE_GTK || HAVE_GCONF || HAVE_GSETTINGS */ |