428
|
1 /* The event_stream interface for tty's.
|
|
2 Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
|
|
3 Copyright (C) 1995 Sun Microsystems, Inc.
|
1268
|
4 Copyright (C) 1995, 2002, 2003 Ben Wing.
|
428
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Not in FSF. */
|
|
24
|
|
25 #include <config.h>
|
|
26 #include "lisp.h"
|
|
27
|
|
28 #include "device.h"
|
872
|
29 #include "console-tty-impl.h"
|
428
|
30 #include "events.h"
|
|
31 #include "frame.h"
|
|
32 #include "process.h"
|
|
33
|
|
34 #include "sysproc.h"
|
|
35 #include "syswait.h"
|
|
36 #include "systime.h"
|
|
37
|
|
38 /* Mask of bits indicating the descriptors that we wait for input on */
|
|
39 extern SELECT_TYPE input_wait_mask, non_fake_input_wait_mask;
|
|
40 extern SELECT_TYPE process_only_mask, tty_only_mask;
|
|
41
|
|
42 static struct event_stream *tty_event_stream;
|
|
43
|
1292
|
44 #ifdef WIN32_ANY
|
|
45 extern int mswindows_is_blocking;
|
|
46 #endif
|
|
47
|
428
|
48
|
|
49 /************************************************************************/
|
|
50 /* timeout events */
|
|
51 /************************************************************************/
|
|
52
|
|
53 /* The pending timers are stored in an ordered list, where the first timer
|
|
54 on the list is the first one to fire. Times recorded here are
|
|
55 absolute. */
|
|
56 static struct low_level_timeout *tty_timer_queue;
|
|
57
|
|
58 static int
|
|
59 emacs_tty_add_timeout (EMACS_TIME thyme)
|
|
60 {
|
|
61 return add_low_level_timeout (&tty_timer_queue, thyme);
|
|
62 }
|
|
63
|
|
64 static void
|
|
65 emacs_tty_remove_timeout (int id)
|
|
66 {
|
|
67 remove_low_level_timeout (&tty_timer_queue, id);
|
|
68 }
|
|
69
|
|
70 static void
|
440
|
71 tty_timeout_to_emacs_event (Lisp_Event *emacs_event)
|
428
|
72 {
|
934
|
73 /* timeout events have nil as channel */
|
|
74 SET_EVENT_TYPE (emacs_event, timeout_event);
|
|
75 SET_EVENT_TIMESTAMP_ZERO (emacs_event); /* #### */
|
1268
|
76 SET_EVENT_TIMEOUT_INTERVAL_ID (emacs_event,
|
|
77 pop_low_level_timeout (&tty_timer_queue, 0));
|
1204
|
78 SET_EVENT_TIMEOUT_FUNCTION (emacs_event, Qnil);
|
|
79 SET_EVENT_TIMEOUT_OBJECT (emacs_event, Qnil);
|
428
|
80 }
|
|
81
|
|
82
|
|
83
|
|
84 static int
|
1268
|
85 emacs_tty_event_pending_p (int how_many)
|
428
|
86 {
|
1268
|
87 if (!how_many)
|
428
|
88 {
|
|
89 EMACS_TIME sometime;
|
|
90 /* see if there's a pending timeout. */
|
|
91 EMACS_GET_TIME (sometime);
|
|
92 if (tty_timer_queue &&
|
|
93 EMACS_TIME_EQUAL_OR_GREATER (sometime, tty_timer_queue->time))
|
|
94 return 1;
|
1268
|
95
|
|
96 return poll_fds_for_input (non_fake_input_wait_mask);
|
428
|
97 }
|
|
98
|
1268
|
99 /* #### Not right! We need to *count* the number of pending events, which
|
|
100 means we need to have a dispatch queue and drain the pending events,
|
|
101 using drain_tty_devices(). */
|
|
102 return poll_fds_for_input (tty_only_mask);
|
428
|
103 }
|
|
104
|
|
105 static void
|
440
|
106 emacs_tty_next_event (Lisp_Event *emacs_event)
|
428
|
107 {
|
|
108 while (1)
|
|
109 {
|
|
110 int ndesc;
|
|
111 int i;
|
|
112 SELECT_TYPE temp_mask = input_wait_mask;
|
|
113 EMACS_TIME time_to_block;
|
|
114 EMACS_SELECT_TIME select_time_to_block, *pointer_to_this;
|
|
115
|
3541
|
116 if (!NILP (dispatch_event_queue))
|
|
117 {
|
|
118 Lisp_Object event, event2;
|
|
119 event2 = wrap_event (emacs_event);
|
|
120 event = dequeue_dispatch_event ();
|
|
121 Fcopy_event (event, event2);
|
|
122 Fdeallocate_event (event);
|
|
123 return;
|
|
124 }
|
|
125
|
428
|
126 if (!get_low_level_timeout_interval (tty_timer_queue, &time_to_block))
|
|
127 /* no timer events; block indefinitely */
|
|
128 pointer_to_this = 0;
|
|
129 else
|
|
130 {
|
|
131 EMACS_TIME_TO_SELECT_TIME (time_to_block, select_time_to_block);
|
|
132 pointer_to_this = &select_time_to_block;
|
|
133 }
|
|
134
|
1292
|
135 #ifdef WIN32_ANY
|
|
136 mswindows_is_blocking = 1;
|
|
137 #endif
|
428
|
138 ndesc = select (MAXDESC, &temp_mask, 0, 0, pointer_to_this);
|
1292
|
139 #ifdef WIN32_ANY
|
|
140 mswindows_is_blocking = 0;
|
|
141 #endif
|
428
|
142 if (ndesc > 0)
|
|
143 {
|
|
144 /* Look for a TTY event */
|
|
145 for (i = 0; i < MAXDESC; i++)
|
|
146 {
|
|
147 /* To avoid race conditions (among other things, an infinite
|
|
148 loop when called from Fdiscard_input()), we must return
|
|
149 user events ahead of process events. */
|
|
150 if (FD_ISSET (i, &temp_mask) && FD_ISSET (i, &tty_only_mask))
|
|
151 {
|
1204
|
152 struct console *c = find_tty_or_stream_console_from_fd (i);
|
428
|
153
|
|
154 assert (c);
|
771
|
155 if (read_event_from_tty_or_stream_desc (emacs_event, c))
|
428
|
156 return;
|
|
157 }
|
|
158 }
|
|
159
|
|
160 /* Look for a process event */
|
|
161 for (i = 0; i < MAXDESC; i++)
|
|
162 {
|
|
163 if (FD_ISSET (i, &temp_mask) && FD_ISSET (i, &process_only_mask))
|
|
164 {
|
|
165 Lisp_Object process;
|
440
|
166 Lisp_Process *p = get_process_from_usid (FD_TO_USID(i));
|
428
|
167
|
|
168 assert (p);
|
793
|
169 process = wrap_process (p);
|
934
|
170 set_event_type (emacs_event, process_event);
|
|
171 /* process events have nil as channel */
|
|
172 SET_EVENT_TIMESTAMP_ZERO (emacs_event); /* #### */
|
1204
|
173 SET_EVENT_PROCESS_PROCESS (emacs_event, process);
|
428
|
174 return;
|
|
175 }
|
|
176 }
|
|
177
|
|
178 /* We might get here when a fake event came through a signal. */
|
|
179 /* Return a dummy event, so that a cycle of the command loop will
|
|
180 occur. */
|
|
181 drain_signal_event_pipe ();
|
934
|
182 set_event_type (emacs_event, eval_event);
|
|
183 /* eval events have nil as channel */
|
1204
|
184 SET_EVENT_EVAL_FUNCTION (emacs_event, Qidentity);
|
|
185 SET_EVENT_EVAL_OBJECT (emacs_event, Qnil);
|
428
|
186 return;
|
|
187 }
|
|
188 else if (ndesc == 0) /* timeout fired */
|
|
189 {
|
|
190 tty_timeout_to_emacs_event (emacs_event);
|
|
191 return;
|
|
192 }
|
|
193 }
|
|
194 }
|
|
195
|
|
196 static void
|
2286
|
197 emacs_tty_format_magic_event (Lisp_Event *UNUSED (emacs_event),
|
|
198 Lisp_Object UNUSED (pstream))
|
788
|
199 {
|
|
200 /* Nothing to do currently */
|
|
201 }
|
|
202
|
|
203 static int
|
2286
|
204 emacs_tty_compare_magic_event (Lisp_Event *UNUSED (e1),
|
|
205 Lisp_Event *UNUSED (e2))
|
788
|
206 {
|
|
207 return 1;
|
|
208 }
|
|
209
|
|
210 static Hashcode
|
2286
|
211 emacs_tty_hash_magic_event (Lisp_Event *UNUSED (e))
|
788
|
212 {
|
|
213 return 0;
|
|
214 }
|
|
215
|
|
216 static void
|
2286
|
217 emacs_tty_handle_magic_event (Lisp_Event *UNUSED (emacs_event))
|
428
|
218 {
|
|
219 /* Nothing to do currently */
|
|
220 }
|
|
221
|
|
222
|
|
223 static void
|
853
|
224 emacs_tty_select_process (Lisp_Process *process, int doin, int doerr)
|
428
|
225 {
|
853
|
226 int infd, errfd;
|
|
227
|
|
228 event_stream_unixoid_select_process (process, doin, doerr, &infd, &errfd);
|
428
|
229 }
|
|
230
|
|
231 static void
|
853
|
232 emacs_tty_unselect_process (Lisp_Process *process, int doin, int doerr)
|
428
|
233 {
|
853
|
234 int infd, errfd;
|
|
235
|
|
236 event_stream_unixoid_unselect_process (process, doin, doerr, &infd, &errfd);
|
428
|
237 }
|
|
238
|
|
239 static void
|
|
240 emacs_tty_select_console (struct console *con)
|
|
241 {
|
|
242 event_stream_unixoid_select_console (con);
|
|
243 }
|
|
244
|
|
245 static void
|
|
246 emacs_tty_unselect_console (struct console *con)
|
|
247 {
|
|
248 event_stream_unixoid_unselect_console (con);
|
|
249 }
|
|
250
|
|
251 static void
|
1204
|
252 emacs_tty_drain_queue (void)
|
428
|
253 {
|
1204
|
254 drain_tty_devices ();
|
428
|
255 }
|
|
256
|
853
|
257 static void
|
|
258 emacs_tty_create_io_streams (void* inhandle, void* outhandle,
|
|
259 void *errhandle, Lisp_Object* instream,
|
|
260 Lisp_Object* outstream,
|
|
261 Lisp_Object* errstream,
|
|
262 USID* in_usid,
|
|
263 USID* err_usid,
|
|
264 int flags)
|
428
|
265 {
|
853
|
266 event_stream_unixoid_create_io_streams
|
|
267 (inhandle, outhandle, errhandle, instream, outstream,
|
|
268 errstream, in_usid, err_usid, flags);
|
428
|
269 }
|
|
270
|
853
|
271 static void
|
|
272 emacs_tty_delete_io_streams (Lisp_Object instream,
|
|
273 Lisp_Object outstream,
|
|
274 Lisp_Object errstream,
|
|
275 USID* in_usid,
|
|
276 USID* err_usid)
|
428
|
277 {
|
853
|
278 event_stream_unixoid_delete_io_streams
|
|
279 (instream, outstream, errstream, in_usid, err_usid);
|
428
|
280 }
|
|
281
|
|
282
|
|
283 /************************************************************************/
|
|
284 /* initialization */
|
|
285 /************************************************************************/
|
|
286
|
|
287 void
|
|
288 reinit_vars_of_event_tty (void)
|
|
289 {
|
1204
|
290 tty_event_stream = xnew_and_zero (struct event_stream);
|
428
|
291
|
|
292 tty_event_stream->event_pending_p = emacs_tty_event_pending_p;
|
|
293 tty_event_stream->next_event_cb = emacs_tty_next_event;
|
|
294 tty_event_stream->handle_magic_event_cb = emacs_tty_handle_magic_event;
|
788
|
295 tty_event_stream->format_magic_event_cb = emacs_tty_format_magic_event;
|
|
296 tty_event_stream->compare_magic_event_cb= emacs_tty_compare_magic_event;
|
|
297 tty_event_stream->hash_magic_event_cb = emacs_tty_hash_magic_event;
|
428
|
298 tty_event_stream->add_timeout_cb = emacs_tty_add_timeout;
|
|
299 tty_event_stream->remove_timeout_cb = emacs_tty_remove_timeout;
|
|
300 tty_event_stream->select_console_cb = emacs_tty_select_console;
|
|
301 tty_event_stream->unselect_console_cb = emacs_tty_unselect_console;
|
|
302 tty_event_stream->select_process_cb = emacs_tty_select_process;
|
|
303 tty_event_stream->unselect_process_cb = emacs_tty_unselect_process;
|
1204
|
304 tty_event_stream->drain_queue_cb = emacs_tty_drain_queue;
|
853
|
305 tty_event_stream->create_io_streams_cb = emacs_tty_create_io_streams;
|
|
306 tty_event_stream->delete_io_streams_cb = emacs_tty_delete_io_streams;
|
428
|
307 }
|
|
308
|
|
309 void
|
|
310 vars_of_event_tty (void)
|
|
311 {
|
|
312 }
|
|
313
|
|
314 void
|
|
315 init_event_tty_late (void)
|
|
316 {
|
|
317 event_stream = tty_event_stream;
|
|
318 }
|