xgselect.c 3.79 KB
Newer Older
1
/* Function for handling the GLib event loop.
2

3
Copyright (C) 2009-2013 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

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

Dan Nicolaescu's avatar
Dan Nicolaescu committed
20
#include <config.h>
21

22 23
#include "xgselect.h"

24
#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
25

26 27
#include <glib.h>
#include <errno.h>
28
#include "xterm.h"
29 30

int
31
xg_select (int fds_lim, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
32
	   EMACS_TIME *timeout, sigset_t *sigmask)
33 34 35 36
{
  SELECT_TYPE all_rfds, all_wfds;
  EMACS_TIME tmo, *tmop = timeout;

37
  GMainContext *context;
38
  int have_wfds = wfds != NULL;
39 40 41 42
  GPollFD gfds_buf[128];
  GPollFD *gfds = gfds_buf;
  int gfds_size = sizeof gfds_buf / sizeof *gfds_buf;
  int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
43
  int i, nfds, tmo_in_millisec;
44
  USE_SAFE_ALLOCA;
45

46 47 48
  if (! (x_in_use
	 && g_main_context_pending (context = g_main_context_default ())))
    return pselect (fds_lim, rfds, wfds, efds, timeout, sigmask);
49

50
  if (rfds) all_rfds = *rfds;
51
  else FD_ZERO (&all_rfds);
52
  if (wfds) all_wfds = *wfds;
53 54
  else FD_ZERO (&all_wfds);

55 56 57 58 59 60 61 62 63
  n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
				 gfds, gfds_size);
  if (gfds_size < n_gfds)
    {
      SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds);
      gfds_size = n_gfds;
      n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
				     gfds, gfds_size);
    }
64

65
  for (i = 0; i < n_gfds; ++i)
66 67 68 69 70 71 72 73 74 75 76 77 78 79
    {
      if (gfds[i].events & G_IO_IN)
        {
          FD_SET (gfds[i].fd, &all_rfds);
          if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
        }
      if (gfds[i].events & G_IO_OUT)
        {
          FD_SET (gfds[i].fd, &all_wfds);
          if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
          have_wfds = 1;
        }
    }

80 81
  SAFE_FREE ();

82 83
  if (tmo_in_millisec >= 0)
    {
84 85
      tmo = make_emacs_time (tmo_in_millisec / 1000,
			     1000 * 1000 * (tmo_in_millisec % 1000));
86 87
      if (!timeout || EMACS_TIME_LT (tmo, *timeout))
	tmop = &tmo;
88 89
    }

90
  fds_lim = max_fds + 1;
91 92
  nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL,
		  efds, tmop, sigmask);
93 94 95

  if (nfds < 0)
    retval = nfds;
96
  else if (nfds > 0)
97
    {
98
      for (i = 0; i < fds_lim; ++i)
99 100 101 102 103 104
        {
          if (FD_ISSET (i, &all_rfds))
            {
              if (rfds && FD_ISSET (i, rfds)) ++retval;
              else ++our_fds;
            }
105 106 107
          else if (rfds)
            FD_CLR (i, rfds);

108 109 110 111 112
          if (have_wfds && FD_ISSET (i, &all_wfds))
            {
              if (wfds && FD_ISSET (i, wfds)) ++retval;
              else ++our_fds;
            }
113 114 115
          else if (wfds)
            FD_CLR (i, wfds);

116 117 118 119 120
          if (efds && FD_ISSET (i, efds))
            ++retval;
        }
    }

121
  if (our_fds > 0 || (nfds == 0 && tmop == &tmo))
122
    {
123

124 125 126 127 128 129 130 131 132
      /* If Gtk+ is in use eventually gtk_main_iteration will be called,
         unless retval is zero.  */
#ifdef USE_GTK
      if (retval == 0)
#endif
        while (g_main_context_pending (context))
          g_main_context_dispatch (context);

      /* To not have to recalculate timeout, return like this.  */
133
      if (retval == 0)
134 135 136 137 138 139 140 141
        {
          retval = -1;
          errno = EINTR;
        }
    }

  return retval;
}
142
#endif /* USE_GTK || HAVE_GCONF || HAVE_GSETTINGS */