1 /* Function for handling the GLib event loop.
3 Copyright (C) 2009-2013 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/>. */
24 #if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
31 xg_select (int fds_lim
, SELECT_TYPE
*rfds
, SELECT_TYPE
*wfds
, SELECT_TYPE
*efds
,
32 EMACS_TIME
*timeout
, sigset_t
*sigmask
)
34 SELECT_TYPE all_rfds
, all_wfds
;
35 EMACS_TIME tmo
, *tmop
= timeout
;
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
;
47 && g_main_context_pending (context
= g_main_context_default ())))
48 return pselect (fds_lim
, rfds
, wfds
, efds
, timeout
, sigmask
);
50 if (rfds
) all_rfds
= *rfds
;
51 else FD_ZERO (&all_rfds
);
52 if (wfds
) all_wfds
= *wfds
;
53 else FD_ZERO (&all_wfds
);
55 n_gfds
= g_main_context_query (context
, G_PRIORITY_LOW
, &tmo_in_millisec
,
57 if (gfds_size
< n_gfds
)
59 SAFE_NALLOCA (gfds
, sizeof *gfds
, n_gfds
);
61 n_gfds
= g_main_context_query (context
, G_PRIORITY_LOW
, &tmo_in_millisec
,
65 for (i
= 0; i
< n_gfds
; ++i
)
67 if (gfds
[i
].events
& G_IO_IN
)
69 FD_SET (gfds
[i
].fd
, &all_rfds
);
70 if (gfds
[i
].fd
> max_fds
) max_fds
= gfds
[i
].fd
;
72 if (gfds
[i
].events
& G_IO_OUT
)
74 FD_SET (gfds
[i
].fd
, &all_wfds
);
75 if (gfds
[i
].fd
> max_fds
) max_fds
= gfds
[i
].fd
;
82 if (tmo_in_millisec
>= 0)
84 tmo
= make_emacs_time (tmo_in_millisec
/ 1000,
85 1000 * 1000 * (tmo_in_millisec
% 1000));
86 if (!timeout
|| EMACS_TIME_LT (tmo
, *timeout
))
90 fds_lim
= max_fds
+ 1;
91 nfds
= pselect (fds_lim
, &all_rfds
, have_wfds
? &all_wfds
: NULL
,
98 for (i
= 0; i
< fds_lim
; ++i
)
100 if (FD_ISSET (i
, &all_rfds
))
102 if (rfds
&& FD_ISSET (i
, rfds
)) ++retval
;
108 if (have_wfds
&& FD_ISSET (i
, &all_wfds
))
110 if (wfds
&& FD_ISSET (i
, wfds
)) ++retval
;
116 if (efds
&& FD_ISSET (i
, efds
))
121 if (our_fds
> 0 || (nfds
== 0 && tmop
== &tmo
))
124 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
125 unless retval is zero. */
129 while (g_main_context_pending (context
))
130 g_main_context_dispatch (context
);
132 /* To not have to recalculate timeout, return like this. */
142 #endif /* USE_GTK || HAVE_GCONF || HAVE_GSETTINGS */