/* Function for handling the GLib event loop.
-Copyright (C) 2009-2013 Free Software Foundation, Inc.
+Copyright (C) 2009-2014 Free Software Foundation, Inc.
This file is part of GNU Emacs.
#include "xgselect.h"
-#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
+#ifdef HAVE_GLIB
#include <glib.h>
#include <errno.h>
-#include "xterm.h"
+#include <stdbool.h>
+#include <timespec.h>
#include "frame.h"
int
-xg_select (int fds_lim, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
- EMACS_TIME *timeout, sigset_t *sigmask)
+xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds,
+ struct timespec const *timeout, sigset_t const *sigmask)
{
- SELECT_TYPE all_rfds, all_wfds;
- EMACS_TIME tmo, *tmop = timeout;
+ fd_set all_rfds, all_wfds;
+ struct timespec tmo;
+ struct timespec const *tmop = timeout;
GMainContext *context;
int have_wfds = wfds != NULL;
int gfds_size = sizeof gfds_buf / sizeof *gfds_buf;
int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
int i, nfds, tmo_in_millisec;
+ bool need_to_dispatch;
USE_SAFE_ALLOCA;
- if (! (window_system_available (NULL)
- && g_main_context_pending (context = g_main_context_default ())))
- return pselect (fds_lim, rfds, wfds, efds, timeout, sigmask);
+ /* Do not try to optimize with an initial check with g_main_context_pending
+ and a call to pselect if it returns false. If Gdk has a timeout for 0.01
+ second, and Emacs has a timeout for 1 second, g_main_context_pending will
+ return false, but the timeout will be 1 second, thus missing the gdk
+ timeout with a lot. */
+
+ context = g_main_context_default ();
if (rfds) all_rfds = *rfds;
else FD_ZERO (&all_rfds);
if (tmo_in_millisec >= 0)
{
- tmo = make_emacs_time (tmo_in_millisec / 1000,
- 1000 * 1000 * (tmo_in_millisec % 1000));
- if (!timeout || EMACS_TIME_LT (tmo, *timeout))
+ tmo = make_timespec (tmo_in_millisec / 1000,
+ 1000 * 1000 * (tmo_in_millisec % 1000));
+ if (!timeout || timespec_cmp (tmo, *timeout) < 0)
tmop = &tmo;
}
}
}
- if (our_fds > 0 || (nfds == 0 && tmop == &tmo))
- {
-
- /* If Gtk+ is in use eventually gtk_main_iteration will be called,
- unless retval is zero. */
+ /* If Gtk+ is in use eventually gtk_main_iteration will be called,
+ unless retval is zero. */
#ifdef USE_GTK
- if (retval == 0)
+ need_to_dispatch = retval == 0;
+#else
+ need_to_dispatch = true;
#endif
- while (g_main_context_pending (context))
- g_main_context_dispatch (context);
+ if (need_to_dispatch)
+ {
+ int pselect_errno = errno;
+ while (g_main_context_pending (context))
+ g_main_context_dispatch (context);
+ errno = pselect_errno;
+ }
- /* To not have to recalculate timeout, return like this. */
- if (retval == 0)
- {
- retval = -1;
- errno = EINTR;
- }
+ /* To not have to recalculate timeout, return like this. */
+ if ((our_fds > 0 || (nfds == 0 && tmop == &tmo)) && (retval == 0))
+ {
+ retval = -1;
+ errno = EINTR;
}
return retval;
}
-#endif /* USE_GTK || HAVE_GCONF || HAVE_GSETTINGS */
+#endif /* HAVE_GLIB */