1 /* Function for handling the GLib event loop.
3 Copyright (C) 2009-2014 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/>. */
32 xg_select (int fds_lim
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*efds
,
33 struct timespec
const *timeout
, sigset_t
const *sigmask
)
35 fd_set all_rfds
, all_wfds
;
37 struct timespec
const *tmop
= timeout
;
39 GMainContext
*context
;
40 int have_wfds
= wfds
!= NULL
;
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;
45 int i
, nfds
, tmo_in_millisec
;
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. */
54 context
= g_main_context_default ();
56 if (rfds
) all_rfds
= *rfds
;
57 else FD_ZERO (&all_rfds
);
58 if (wfds
) all_wfds
= *wfds
;
59 else FD_ZERO (&all_wfds
);
61 n_gfds
= g_main_context_query (context
, G_PRIORITY_LOW
, &tmo_in_millisec
,
63 if (gfds_size
< n_gfds
)
65 SAFE_NALLOCA (gfds
, sizeof *gfds
, n_gfds
);
67 n_gfds
= g_main_context_query (context
, G_PRIORITY_LOW
, &tmo_in_millisec
,
71 for (i
= 0; i
< n_gfds
; ++i
)
73 if (gfds
[i
].events
& G_IO_IN
)
75 FD_SET (gfds
[i
].fd
, &all_rfds
);
76 if (gfds
[i
].fd
> max_fds
) max_fds
= gfds
[i
].fd
;
78 if (gfds
[i
].events
& G_IO_OUT
)
80 FD_SET (gfds
[i
].fd
, &all_wfds
);
81 if (gfds
[i
].fd
> max_fds
) max_fds
= gfds
[i
].fd
;
88 if (tmo_in_millisec
>= 0)
90 tmo
= make_timespec (tmo_in_millisec
/ 1000,
91 1000 * 1000 * (tmo_in_millisec
% 1000));
92 if (!timeout
|| timespec_cmp (tmo
, *timeout
) < 0)
96 fds_lim
= max_fds
+ 1;
97 nfds
= pselect (fds_lim
, &all_rfds
, have_wfds
? &all_wfds
: NULL
,
104 for (i
= 0; i
< fds_lim
; ++i
)
106 if (FD_ISSET (i
, &all_rfds
))
108 if (rfds
&& FD_ISSET (i
, rfds
)) ++retval
;
114 if (have_wfds
&& FD_ISSET (i
, &all_wfds
))
116 if (wfds
&& FD_ISSET (i
, wfds
)) ++retval
;
122 if (efds
&& FD_ISSET (i
, efds
))
127 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
128 unless retval is zero. */
132 while (g_main_context_pending (context
))
133 g_main_context_dispatch (context
);
135 /* To not have to recalculate timeout, return like this. */
136 if ((our_fds
> 0 || (nfds
== 0 && tmop
== &tmo
)) && (retval
== 0))
144 #endif /* HAVE_GLIB */