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
|
|
116 if (!get_low_level_timeout_interval (tty_timer_queue, &time_to_block))
|
|
117 /* no timer events; block indefinitely */
|
|
118 pointer_to_this = 0;
|
|
119 else
|
|
120 {
|
|
121 EMACS_TIME_TO_SELECT_TIME (time_to_block, select_time_to_block);
|
|
122 pointer_to_this = &select_time_to_block;
|
|
123 }
|
|
124
|
1292
|
125 #ifdef WIN32_ANY
|
|
126 mswindows_is_blocking = 1;
|
|
127 #endif
|
428
|
128 ndesc = select (MAXDESC, &temp_mask, 0, 0, pointer_to_this);
|
1292
|
129 #ifdef WIN32_ANY
|
|
130 mswindows_is_blocking = 0;
|
|
131 #endif
|
428
|
132 if (ndesc > 0)
|
|
133 {
|
|
134 /* Look for a TTY event */
|
|
135 for (i = 0; i < MAXDESC; i++)
|
|
136 {
|
|
137 /* To avoid race conditions (among other things, an infinite
|
|
138 loop when called from Fdiscard_input()), we must return
|
|
139 user events ahead of process events. */
|
|
140 if (FD_ISSET (i, &temp_mask) && FD_ISSET (i, &tty_only_mask))
|
|
141 {
|
1204
|
142 struct console *c = find_tty_or_stream_console_from_fd (i);
|
428
|
143
|
|
144 assert (c);
|
771
|
145 if (read_event_from_tty_or_stream_desc (emacs_event, c))
|
428
|
146 return;
|
|
147 }
|
|
148 }
|
|
149
|
|
150 /* Look for a process event */
|
|
151 for (i = 0; i < MAXDESC; i++)
|
|
152 {
|
|
153 if (FD_ISSET (i, &temp_mask) && FD_ISSET (i, &process_only_mask))
|
|
154 {
|
|
155 Lisp_Object process;
|
440
|
156 Lisp_Process *p = get_process_from_usid (FD_TO_USID(i));
|
428
|
157
|
|
158 assert (p);
|
793
|
159 process = wrap_process (p);
|
934
|
160 set_event_type (emacs_event, process_event);
|
|
161 /* process events have nil as channel */
|
|
162 SET_EVENT_TIMESTAMP_ZERO (emacs_event); /* #### */
|
1204
|
163 SET_EVENT_PROCESS_PROCESS (emacs_event, process);
|
428
|
164 return;
|
|
165 }
|
|
166 }
|
|
167
|
|
168 /* We might get here when a fake event came through a signal. */
|
|
169 /* Return a dummy event, so that a cycle of the command loop will
|
|
170 occur. */
|
|
171 drain_signal_event_pipe ();
|
934
|
172 set_event_type (emacs_event, eval_event);
|
|
173 /* eval events have nil as channel */
|
1204
|
174 SET_EVENT_EVAL_FUNCTION (emacs_event, Qidentity);
|
|
175 SET_EVENT_EVAL_OBJECT (emacs_event, Qnil);
|
428
|
176 return;
|
|
177 }
|
|
178 else if (ndesc == 0) /* timeout fired */
|
|
179 {
|
|
180 tty_timeout_to_emacs_event (emacs_event);
|
|
181 return;
|
|
182 }
|
|
183 }
|
|
184 }
|
|
185
|
|
186 static void
|
2286
|
187 emacs_tty_format_magic_event (Lisp_Event *UNUSED (emacs_event),
|
|
188 Lisp_Object UNUSED (pstream))
|
788
|
189 {
|
|
190 /* Nothing to do currently */
|
|
191 }
|
|
192
|
|
193 static int
|
2286
|
194 emacs_tty_compare_magic_event (Lisp_Event *UNUSED (e1),
|
|
195 Lisp_Event *UNUSED (e2))
|
788
|
196 {
|
|
197 return 1;
|
|
198 }
|
|
199
|
|
200 static Hashcode
|
2286
|
201 emacs_tty_hash_magic_event (Lisp_Event *UNUSED (e))
|
788
|
202 {
|
|
203 return 0;
|
|
204 }
|
|
205
|
|
206 static void
|
2286
|
207 emacs_tty_handle_magic_event (Lisp_Event *UNUSED (emacs_event))
|
428
|
208 {
|
|
209 /* Nothing to do currently */
|
|
210 }
|
|
211
|
|
212
|
|
213 static void
|
853
|
214 emacs_tty_select_process (Lisp_Process *process, int doin, int doerr)
|
428
|
215 {
|
853
|
216 int infd, errfd;
|
|
217
|
|
218 event_stream_unixoid_select_process (process, doin, doerr, &infd, &errfd);
|
428
|
219 }
|
|
220
|
|
221 static void
|
853
|
222 emacs_tty_unselect_process (Lisp_Process *process, int doin, int doerr)
|
428
|
223 {
|
853
|
224 int infd, errfd;
|
|
225
|
|
226 event_stream_unixoid_unselect_process (process, doin, doerr, &infd, &errfd);
|
428
|
227 }
|
|
228
|
|
229 static void
|
|
230 emacs_tty_select_console (struct console *con)
|
|
231 {
|
|
232 event_stream_unixoid_select_console (con);
|
|
233 }
|
|
234
|
|
235 static void
|
|
236 emacs_tty_unselect_console (struct console *con)
|
|
237 {
|
|
238 event_stream_unixoid_unselect_console (con);
|
|
239 }
|
|
240
|
|
241 static void
|
1204
|
242 emacs_tty_drain_queue (void)
|
428
|
243 {
|
1204
|
244 drain_tty_devices ();
|
428
|
245 }
|
|
246
|
853
|
247 static void
|
|
248 emacs_tty_create_io_streams (void* inhandle, void* outhandle,
|
|
249 void *errhandle, Lisp_Object* instream,
|
|
250 Lisp_Object* outstream,
|
|
251 Lisp_Object* errstream,
|
|
252 USID* in_usid,
|
|
253 USID* err_usid,
|
|
254 int flags)
|
428
|
255 {
|
853
|
256 event_stream_unixoid_create_io_streams
|
|
257 (inhandle, outhandle, errhandle, instream, outstream,
|
|
258 errstream, in_usid, err_usid, flags);
|
428
|
259 }
|
|
260
|
853
|
261 static void
|
|
262 emacs_tty_delete_io_streams (Lisp_Object instream,
|
|
263 Lisp_Object outstream,
|
|
264 Lisp_Object errstream,
|
|
265 USID* in_usid,
|
|
266 USID* err_usid)
|
428
|
267 {
|
853
|
268 event_stream_unixoid_delete_io_streams
|
|
269 (instream, outstream, errstream, in_usid, err_usid);
|
428
|
270 }
|
|
271
|
|
272
|
|
273 /************************************************************************/
|
|
274 /* initialization */
|
|
275 /************************************************************************/
|
|
276
|
|
277 void
|
|
278 reinit_vars_of_event_tty (void)
|
|
279 {
|
1204
|
280 tty_event_stream = xnew_and_zero (struct event_stream);
|
428
|
281
|
|
282 tty_event_stream->event_pending_p = emacs_tty_event_pending_p;
|
|
283 tty_event_stream->next_event_cb = emacs_tty_next_event;
|
|
284 tty_event_stream->handle_magic_event_cb = emacs_tty_handle_magic_event;
|
788
|
285 tty_event_stream->format_magic_event_cb = emacs_tty_format_magic_event;
|
|
286 tty_event_stream->compare_magic_event_cb= emacs_tty_compare_magic_event;
|
|
287 tty_event_stream->hash_magic_event_cb = emacs_tty_hash_magic_event;
|
428
|
288 tty_event_stream->add_timeout_cb = emacs_tty_add_timeout;
|
|
289 tty_event_stream->remove_timeout_cb = emacs_tty_remove_timeout;
|
|
290 tty_event_stream->select_console_cb = emacs_tty_select_console;
|
|
291 tty_event_stream->unselect_console_cb = emacs_tty_unselect_console;
|
|
292 tty_event_stream->select_process_cb = emacs_tty_select_process;
|
|
293 tty_event_stream->unselect_process_cb = emacs_tty_unselect_process;
|
1204
|
294 tty_event_stream->drain_queue_cb = emacs_tty_drain_queue;
|
853
|
295 tty_event_stream->create_io_streams_cb = emacs_tty_create_io_streams;
|
|
296 tty_event_stream->delete_io_streams_cb = emacs_tty_delete_io_streams;
|
428
|
297 }
|
|
298
|
|
299 void
|
|
300 vars_of_event_tty (void)
|
|
301 {
|
|
302 }
|
|
303
|
|
304 void
|
|
305 init_event_tty_late (void)
|
|
306 {
|
|
307 event_stream = tty_event_stream;
|
|
308 }
|