1 /* Function for handling the GLib event loop.
2 Copyright (C) 2009, 2010
3 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/>. */
22 #if defined (USE_GTK) || defined (HAVE_GCONF)
32 xg_select (max_fds
, rfds
, wfds
, efds
, timeout
)
39 SELECT_TYPE all_rfds
, all_wfds
;
40 EMACS_TIME tmo
, *tmop
= timeout
;
42 GMainContext
*context
= g_main_context_default ();
43 int have_wfds
= wfds
!= NULL
;
44 int n_gfds
= 0, our_tmo
= 0, retval
= 0, our_fds
= 0;
45 int prio
, i
, nfds
, tmo_in_millisec
;
47 if (rfds
) memcpy (&all_rfds
, rfds
, sizeof (all_rfds
));
48 else FD_ZERO (&all_rfds
);
49 if (wfds
) memcpy (&all_wfds
, wfds
, sizeof (all_rfds
));
50 else FD_ZERO (&all_wfds
);
52 /* Update event sources in GLib. */
53 g_main_context_pending (context
);
56 if (n_gfds
> gfds_size
)
58 while (n_gfds
> gfds_size
)
61 gfds
= xmalloc (sizeof (*gfds
) * gfds_size
);
64 n_gfds
= g_main_context_query (context
,
69 } while (n_gfds
> gfds_size
);
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
;
86 if (tmo_in_millisec
>= 0)
88 EMACS_SET_SECS_USECS (tmo
, tmo_in_millisec
/1000,
89 1000 * (tmo_in_millisec
% 1000));
90 if (!timeout
) our_tmo
= 1;
93 EMACS_TIME difference
;
95 EMACS_SUB_TIME (difference
, tmo
, *timeout
);
96 if (EMACS_TIME_NEG_P (difference
)) our_tmo
= 1;
99 if (our_tmo
) tmop
= &tmo
;
102 nfds
= select (max_fds
+1, &all_rfds
, have_wfds
? &all_wfds
: NULL
,
109 for (i
= 0; i
< max_fds
+1; ++i
)
111 if (FD_ISSET (i
, &all_rfds
))
113 if (rfds
&& FD_ISSET (i
, rfds
)) ++retval
;
116 if (have_wfds
&& FD_ISSET (i
, &all_wfds
))
118 if (wfds
&& FD_ISSET (i
, wfds
)) ++retval
;
121 if (efds
&& FD_ISSET (i
, efds
))
126 if (our_fds
> 0 || (nfds
== 0 && our_tmo
))
129 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
130 unless retval is zero. */
134 while (g_main_context_pending (context
))
135 g_main_context_dispatch (context
);
137 /* To not have to recalculate timeout, return like this. */
147 #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */
150 xgselect_initialize ()
152 #if defined (USE_GTK) || defined (HAVE_GCONF)
154 gfds
= xmalloc (sizeof (*gfds
)*gfds_size
);
155 #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */
158 /* arch-tag: c5873ee3-d1f6-44f9-9f3b-b14f70fd0e6a
159 (do not change this comment) */