0
|
1 /* Code shared between all event loops that use select() and have a
|
|
2 different input descriptor for each device.
|
|
3 Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
|
|
4 Copyright (C) 1995 Board of Trustees, University of Illinois.
|
|
5 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
6 Copyright (C) 1995, 1996 Ben Wing.
|
|
7
|
|
8 This file is part of XEmacs.
|
|
9
|
|
10 XEmacs is free software; you can redistribute it and/or modify it
|
|
11 under the terms of the GNU General Public License as published by the
|
|
12 Free Software Foundation; either version 2, or (at your option) any
|
|
13 later version.
|
|
14
|
|
15 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
18 for more details.
|
|
19
|
|
20 You should have received a copy of the GNU General Public License
|
|
21 along with XEmacs; see the file COPYING. If not, write to
|
|
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
23 Boston, MA 02111-1307, USA. */
|
|
24
|
|
25 /* Synched up with: Not in FSF. */
|
|
26
|
|
27 /* This file has been Mule-ized. */
|
|
28
|
|
29 #include <config.h>
|
|
30 #include "lisp.h"
|
|
31
|
|
32 #include "console-stream.h"
|
|
33 #include "console-tty.h"
|
|
34 #include "device.h"
|
|
35 #include "events.h"
|
|
36 #include "process.h"
|
|
37
|
|
38 #include "sysdep.h"
|
|
39 #include "sysproc.h" /* select stuff */
|
|
40 #include "systime.h"
|
|
41
|
153
|
42 #ifdef HAVE_GPM
|
|
43 #include "gpmevent.h"
|
|
44 #include <gpm.h>
|
|
45 #endif
|
|
46
|
0
|
47 /* Mask of bits indicating the descriptors that we wait for input on.
|
|
48 These work as follows:
|
|
49
|
|
50 input_wait_mask == mask of all file descriptors we select() on,
|
|
51 including TTY/stream console descriptors,
|
|
52 process descriptors, and the signal event pipe.
|
|
53 Only used in event-tty.c; event-Xt.c uses
|
|
54 XtAppAddInput(), and the call to select() is down in
|
|
55 the guts of Xt.
|
|
56
|
|
57 non_fake_input_wait_mask == same as input_wait_mask but minus the
|
|
58 signal event pipe. Also only used in
|
|
59 event-tty.c.
|
|
60
|
|
61 process_only_mask == only the process descriptors.
|
|
62
|
|
63 tty_only_mask == only the TTY/stream console descriptors.
|
|
64 */
|
|
65 SELECT_TYPE input_wait_mask, non_fake_input_wait_mask;
|
|
66 SELECT_TYPE process_only_mask, tty_only_mask;
|
|
67
|
|
68 /* This is used to terminate the select(), when an event came in
|
|
69 through a signal (e.g. window-change or C-g on controlling TTY). */
|
|
70 int signal_event_pipe[2];
|
|
71
|
|
72 static int signal_event_pipe_initialized;
|
|
73
|
|
74 int fake_event_occurred;
|
|
75
|
|
76 int
|
|
77 read_event_from_tty_or_stream_desc (struct Lisp_Event *event,
|
|
78 struct console *con, int fd)
|
|
79 {
|
|
80 unsigned char ch;
|
|
81 int nread;
|
|
82 Lisp_Object console = Qnil;
|
|
83
|
|
84 XSETCONSOLE (console, con);
|
|
85
|
153
|
86 #ifdef HAVE_GPM
|
|
87 if (fd == CONSOLE_TTY_MOUSE_FD (con)) {
|
|
88 return (handle_gpm_read (event,con,fd));
|
|
89 }
|
|
90 #endif
|
|
91
|
0
|
92 nread = read (fd, &ch, 1);
|
|
93 if (nread <= 0)
|
|
94 {
|
|
95 /* deleting the console might not be safe right now ... */
|
|
96 enqueue_magic_eval_event (io_error_delete_console, console);
|
|
97 /* but we definitely need to unselect it to avoid infinite
|
|
98 loops reading EOF's */
|
|
99 Fconsole_disable_input (console);
|
|
100 }
|
|
101 else
|
|
102 {
|
|
103 character_to_event (ch, event, con, 1);
|
|
104 event->channel = console;
|
|
105 return 1;
|
|
106 }
|
|
107 return 0;
|
|
108 }
|
|
109
|
|
110 void
|
|
111 signal_fake_event (void)
|
|
112 {
|
|
113 char byte = 0;
|
|
114 /* We do the write always. Formerly I tried to "optimize" this
|
|
115 by setting a flag indicating whether we're blocking and only
|
|
116 doing the write in that case, but there is a race condition
|
|
117 if the signal occurs after we've checked for the signal
|
|
118 occurrence (which could occur in many places throughout
|
|
119 an iteration of the command loop, e.g. in status_notify()),
|
|
120 but before we set the blocking flag.
|
|
121
|
|
122 This should be OK as long as write() is reentrant, which
|
|
123 I'm fairly sure it is since it's a system call. */
|
|
124
|
|
125 if (signal_event_pipe_initialized)
|
|
126 /* In case a signal comes through while we're dumping */
|
|
127 {
|
|
128 int old_errno = errno;
|
|
129 write (signal_event_pipe[1], &byte, 1);
|
|
130 errno = old_errno;
|
|
131 }
|
|
132 }
|
|
133
|
|
134 void
|
|
135 drain_signal_event_pipe (void)
|
|
136 {
|
|
137 char chars[128];
|
|
138 /* The input end of the pipe has been set to non-blocking. */
|
|
139 while (read (signal_event_pipe[0], chars, sizeof (chars)) > 0)
|
|
140 ;
|
|
141 }
|
|
142
|
|
143 int
|
|
144 event_stream_unixoid_select_console (struct console *con)
|
|
145 {
|
|
146 int infd;
|
|
147
|
|
148 if (CONSOLE_STREAM_P (con))
|
|
149 infd = fileno (CONSOLE_STREAM_DATA (con)->infd);
|
|
150 else
|
|
151 {
|
|
152 assert (CONSOLE_TTY_P (con));
|
|
153 infd = CONSOLE_TTY_DATA (con)->infd;
|
|
154 }
|
|
155
|
|
156 assert (infd >= 0);
|
|
157
|
|
158 FD_SET (infd, &input_wait_mask);
|
|
159 FD_SET (infd, &non_fake_input_wait_mask);
|
|
160 FD_SET (infd, &tty_only_mask);
|
|
161 return infd;
|
|
162 }
|
|
163
|
|
164 int
|
|
165 event_stream_unixoid_unselect_console (struct console *con)
|
|
166 {
|
|
167 int infd;
|
|
168
|
|
169 if (CONSOLE_STREAM_P (con))
|
|
170 infd = fileno (CONSOLE_STREAM_DATA (con)->infd);
|
|
171 else
|
|
172 {
|
|
173 assert (CONSOLE_TTY_P (con));
|
|
174 infd = CONSOLE_TTY_DATA (con)->infd;
|
|
175 }
|
|
176
|
|
177 assert (infd >= 0);
|
|
178
|
|
179 FD_CLR (infd, &input_wait_mask);
|
|
180 FD_CLR (infd, &non_fake_input_wait_mask);
|
|
181 FD_CLR (infd, &tty_only_mask);
|
|
182 return infd;
|
|
183 }
|
|
184
|
|
185 int
|
|
186 event_stream_unixoid_select_process (struct Lisp_Process *proc)
|
|
187 {
|
|
188 int infd, outfd;
|
|
189
|
|
190 get_process_file_descriptors (proc, &infd, &outfd);
|
|
191 assert (infd >= 0);
|
|
192
|
|
193 FD_SET (infd, &input_wait_mask);
|
|
194 FD_SET (infd, &non_fake_input_wait_mask);
|
|
195 FD_SET (infd, &process_only_mask);
|
|
196 return infd;
|
|
197 }
|
|
198
|
|
199 int
|
|
200 event_stream_unixoid_unselect_process (struct Lisp_Process *proc)
|
|
201 {
|
|
202 int infd, outfd;
|
|
203
|
|
204 get_process_file_descriptors (proc, &infd, &outfd);
|
|
205 assert (infd >= 0);
|
|
206
|
|
207 FD_CLR (infd, &input_wait_mask);
|
|
208 FD_CLR (infd, &non_fake_input_wait_mask);
|
|
209 FD_CLR (infd, &process_only_mask);
|
|
210 return infd;
|
|
211 }
|
|
212
|
|
213 int
|
|
214 poll_fds_for_input (SELECT_TYPE mask)
|
|
215 {
|
|
216 EMACS_TIME sometime;
|
|
217 EMACS_SELECT_TIME select_time;
|
|
218 SELECT_TYPE temp_mask;
|
|
219 int retval;
|
|
220
|
|
221 while (1)
|
|
222 {
|
|
223 EMACS_SET_SECS_USECS (sometime, 0, 0);
|
|
224 EMACS_TIME_TO_SELECT_TIME (sometime, select_time);
|
|
225 temp_mask = mask;
|
|
226 /* To effect a poll, tell select() to block for zero seconds. */
|
|
227 retval = select (MAXDESC, &temp_mask, 0, 0, &select_time);
|
|
228 if (retval >= 0)
|
|
229 return retval;
|
|
230 if (errno != EINTR)
|
|
231 {
|
|
232 /* Something went seriously wrong; don't abort since maybe
|
|
233 the TTY just died at the wrong time. */
|
|
234 fprintf (stderr, "xemacs: select failed: errno = %d\n", errno);
|
|
235 return 0;
|
|
236 }
|
|
237 /* else, we got interrupted by a signal, so try again. */
|
|
238 }
|
|
239
|
|
240 RETURN_NOT_REACHED(0) /* not reached */
|
|
241 }
|
|
242
|
|
243
|
|
244 void
|
|
245 init_event_unixoid (void)
|
|
246 {
|
|
247 /* Do this first; the init_event_*_late() functions
|
|
248 pay attention to it. */
|
|
249 if (pipe (signal_event_pipe) < 0)
|
|
250 {
|
|
251 perror ("XEmacs: can't open pipe");
|
|
252 exit (-1);
|
|
253 }
|
|
254 signal_event_pipe_initialized = 1;
|
185
|
255
|
0
|
256 /* Set it non-blocking so we can drain its output. */
|
|
257 set_descriptor_non_blocking (signal_event_pipe[0]);
|
185
|
258
|
0
|
259 /* Also set the write descriptor non-blocking so we don't
|
|
260 hang in case a long time passes between times when
|
|
261 we drain the pipe. */
|
|
262 set_descriptor_non_blocking (signal_event_pipe[1]);
|
185
|
263
|
0
|
264 /* WARNING: In order for the signal-event pipe to work correctly
|
|
265 and not cause lockups, the following need to be followed:
|
185
|
266
|
0
|
267 1) event_pending_p() must ignore input on the signal-event pipe.
|
|
268 2) As soon as next_event() notices input on the signal-event
|
|
269 pipe, it must drain it. */
|
|
270 FD_ZERO (&input_wait_mask);
|
|
271 FD_ZERO (&non_fake_input_wait_mask);
|
|
272 FD_ZERO (&process_only_mask);
|
|
273 FD_ZERO (&tty_only_mask);
|
185
|
274
|
0
|
275 FD_SET (signal_event_pipe[0], &input_wait_mask);
|
|
276 }
|