Get rid of some platform-specific functions examining window
[bpt/emacs.git] / src / xgselect.c
CommitLineData
872870b2 1/* Function for handling the GLib event loop.
95df8112 2
ab422c4d 3Copyright (C) 2009-2013 Free Software Foundation, Inc.
872870b2
JD
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
a2332e8d 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
872870b2 19
08a494a3 20#include <config.h>
872870b2 21
aefd87e1
PE
22#include "xgselect.h"
23
0949d2b6 24#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
aefd87e1 25
872870b2
JD
26#include <glib.h>
27#include <errno.h>
b2f4d39f 28#include "xterm.h"
7452b7bd 29#include "frame.h"
872870b2 30
872870b2 31int
ff23cd9f 32xg_select (int fds_lim, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
d35af63c 33 EMACS_TIME *timeout, sigset_t *sigmask)
872870b2
JD
34{
35 SELECT_TYPE all_rfds, all_wfds;
36 EMACS_TIME tmo, *tmop = timeout;
37
b0572523 38 GMainContext *context;
872870b2 39 int have_wfds = wfds != NULL;
0f46bc75
PE
40 GPollFD gfds_buf[128];
41 GPollFD *gfds = gfds_buf;
42 int gfds_size = sizeof gfds_buf / sizeof *gfds_buf;
43 int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
ff23cd9f 44 int i, nfds, tmo_in_millisec;
0f46bc75 45 USE_SAFE_ALLOCA;
872870b2 46
7452b7bd 47 if (! (window_system_available (NULL)
0f46bc75
PE
48 && g_main_context_pending (context = g_main_context_default ())))
49 return pselect (fds_lim, rfds, wfds, efds, timeout, sigmask);
b0572523 50
ae1d87e2 51 if (rfds) all_rfds = *rfds;
872870b2 52 else FD_ZERO (&all_rfds);
ae1d87e2 53 if (wfds) all_wfds = *wfds;
872870b2
JD
54 else FD_ZERO (&all_wfds);
55
0f46bc75
PE
56 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
57 gfds, gfds_size);
58 if (gfds_size < n_gfds)
59 {
60 SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds);
61 gfds_size = n_gfds;
62 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
63 gfds, gfds_size);
64 }
872870b2 65
41729b81 66 for (i = 0; i < n_gfds; ++i)
872870b2
JD
67 {
68 if (gfds[i].events & G_IO_IN)
69 {
70 FD_SET (gfds[i].fd, &all_rfds);
71 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
72 }
73 if (gfds[i].events & G_IO_OUT)
74 {
75 FD_SET (gfds[i].fd, &all_wfds);
76 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
77 have_wfds = 1;
78 }
79 }
80
0f46bc75
PE
81 SAFE_FREE ();
82
872870b2
JD
83 if (tmo_in_millisec >= 0)
84 {
e9a9ae03
PE
85 tmo = make_emacs_time (tmo_in_millisec / 1000,
86 1000 * 1000 * (tmo_in_millisec % 1000));
d35af63c
PE
87 if (!timeout || EMACS_TIME_LT (tmo, *timeout))
88 tmop = &tmo;
872870b2
JD
89 }
90
97107e2e 91 fds_lim = max_fds + 1;
d35af63c
PE
92 nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL,
93 efds, tmop, sigmask);
872870b2
JD
94
95 if (nfds < 0)
96 retval = nfds;
41729b81 97 else if (nfds > 0)
872870b2 98 {
97107e2e 99 for (i = 0; i < fds_lim; ++i)
872870b2
JD
100 {
101 if (FD_ISSET (i, &all_rfds))
102 {
103 if (rfds && FD_ISSET (i, rfds)) ++retval;
104 else ++our_fds;
105 }
42d3022b
J
106 else if (rfds)
107 FD_CLR (i, rfds);
108
872870b2
JD
109 if (have_wfds && FD_ISSET (i, &all_wfds))
110 {
111 if (wfds && FD_ISSET (i, wfds)) ++retval;
112 else ++our_fds;
113 }
42d3022b
J
114 else if (wfds)
115 FD_CLR (i, wfds);
116
872870b2
JD
117 if (efds && FD_ISSET (i, efds))
118 ++retval;
119 }
120 }
121
d35af63c 122 if (our_fds > 0 || (nfds == 0 && tmop == &tmo))
872870b2 123 {
41729b81 124
872870b2
JD
125 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
126 unless retval is zero. */
127#ifdef USE_GTK
128 if (retval == 0)
129#endif
130 while (g_main_context_pending (context))
131 g_main_context_dispatch (context);
132
133 /* To not have to recalculate timeout, return like this. */
41729b81 134 if (retval == 0)
872870b2
JD
135 {
136 retval = -1;
137 errno = EINTR;
138 }
139 }
140
141 return retval;
142}
0949d2b6 143#endif /* USE_GTK || HAVE_GCONF || HAVE_GSETTINGS */