Commit | Line | Data |
---|---|---|
872870b2 | 1 | /* Function for handling the GLib event loop. |
95df8112 GM |
2 | |
3 | Copyright (C) 2009-2011 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 | |
18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
08a494a3 | 20 | #include <config.h> |
872870b2 JD |
21 | |
22 | #if defined (USE_GTK) || defined (HAVE_GCONF) | |
23 | #include <glib.h> | |
24 | #include <errno.h> | |
25 | #include <setjmp.h> | |
26 | #include "xgselect.h" | |
27 | ||
28 | static GPollFD *gfds; | |
29 | static int gfds_size; | |
30 | ||
31 | int | |
e5447b22 JB |
32 | xg_select (int max_fds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds, |
33 | EMACS_TIME *timeout) | |
872870b2 JD |
34 | { |
35 | SELECT_TYPE all_rfds, all_wfds; | |
36 | EMACS_TIME tmo, *tmop = timeout; | |
37 | ||
38 | GMainContext *context = g_main_context_default (); | |
39 | int have_wfds = wfds != NULL; | |
40 | int n_gfds = 0, our_tmo = 0, retval = 0, our_fds = 0; | |
41 | int prio, i, nfds, tmo_in_millisec; | |
42 | ||
43 | if (rfds) memcpy (&all_rfds, rfds, sizeof (all_rfds)); | |
44 | else FD_ZERO (&all_rfds); | |
45 | if (wfds) memcpy (&all_wfds, wfds, sizeof (all_rfds)); | |
46 | else FD_ZERO (&all_wfds); | |
47 | ||
48 | /* Update event sources in GLib. */ | |
49 | g_main_context_pending (context); | |
50 | ||
51 | do { | |
52 | if (n_gfds > gfds_size) | |
53 | { | |
54 | while (n_gfds > gfds_size) | |
55 | gfds_size *= 2; | |
56 | xfree (gfds); | |
57 | gfds = xmalloc (sizeof (*gfds) * gfds_size); | |
58 | } | |
59 | ||
60 | n_gfds = g_main_context_query (context, | |
61 | G_PRIORITY_LOW, | |
62 | &tmo_in_millisec, | |
63 | gfds, | |
64 | gfds_size); | |
65 | } while (n_gfds > gfds_size); | |
66 | ||
67 | for (i = 0; i < n_gfds; ++i) | |
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 | ||
82 | if (tmo_in_millisec >= 0) | |
83 | { | |
84 | EMACS_SET_SECS_USECS (tmo, tmo_in_millisec/1000, | |
85 | 1000 * (tmo_in_millisec % 1000)); | |
86 | if (!timeout) our_tmo = 1; | |
87 | else | |
88 | { | |
89 | EMACS_TIME difference; | |
90 | ||
91 | EMACS_SUB_TIME (difference, tmo, *timeout); | |
92 | if (EMACS_TIME_NEG_P (difference)) our_tmo = 1; | |
93 | } | |
94 | ||
95 | if (our_tmo) tmop = &tmo; | |
96 | } | |
97 | ||
98 | nfds = select (max_fds+1, &all_rfds, have_wfds ? &all_wfds : NULL, | |
99 | efds, tmop); | |
100 | ||
101 | if (nfds < 0) | |
102 | retval = nfds; | |
103 | else if (nfds > 0) | |
104 | { | |
105 | for (i = 0; i < max_fds+1; ++i) | |
106 | { | |
107 | if (FD_ISSET (i, &all_rfds)) | |
108 | { | |
109 | if (rfds && FD_ISSET (i, rfds)) ++retval; | |
110 | else ++our_fds; | |
111 | } | |
42d3022b J |
112 | else if (rfds) |
113 | FD_CLR (i, rfds); | |
114 | ||
872870b2 JD |
115 | if (have_wfds && FD_ISSET (i, &all_wfds)) |
116 | { | |
117 | if (wfds && FD_ISSET (i, wfds)) ++retval; | |
118 | else ++our_fds; | |
119 | } | |
42d3022b J |
120 | else if (wfds) |
121 | FD_CLR (i, wfds); | |
122 | ||
872870b2 JD |
123 | if (efds && FD_ISSET (i, efds)) |
124 | ++retval; | |
125 | } | |
126 | } | |
127 | ||
128 | if (our_fds > 0 || (nfds == 0 && our_tmo)) | |
129 | { | |
130 | ||
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); | |
138 | ||
139 | /* To not have to recalculate timeout, return like this. */ | |
140 | if (retval == 0) | |
141 | { | |
142 | retval = -1; | |
143 | errno = EINTR; | |
144 | } | |
145 | } | |
146 | ||
147 | return retval; | |
148 | } | |
149 | #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */ | |
150 | ||
151 | void | |
971de7fb | 152 | xgselect_initialize (void) |
872870b2 JD |
153 | { |
154 | #if defined (USE_GTK) || defined (HAVE_GCONF) | |
155 | gfds_size = 128; | |
156 | gfds = xmalloc (sizeof (*gfds)*gfds_size); | |
157 | #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */ | |
158 | } | |
159 |