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
|
|
44
|
|
45 /************************************************************************/
|
|
46 /* timeout events */
|
|
47 /************************************************************************/
|
|
48
|
|
49 /* The pending timers are stored in an ordered list, where the first timer
|
|
50 on the list is the first one to fire. Times recorded here are
|
|
51 absolute. */
|
|
52 static struct low_level_timeout *tty_timer_queue;
|
|
53
|
|
54 static int
|
|
55 emacs_tty_add_timeout (EMACS_TIME thyme)
|
|
56 {
|
|
57 return add_low_level_timeout (&tty_timer_queue, thyme);
|
|
58 }
|
|
59
|
|
60 static void
|
|
61 emacs_tty_remove_timeout (int id)
|
|
62 {
|
|
63 remove_low_level_timeout (&tty_timer_queue, id);
|
|
64 }
|
|
65
|
|
66 static void
|
440
|
67 tty_timeout_to_emacs_event (Lisp_Event *emacs_event)
|
428
|
68 {
|
934
|
69 /* timeout events have nil as channel */
|
|
70 SET_EVENT_TYPE (emacs_event, timeout_event);
|
|
71 SET_EVENT_TIMESTAMP_ZERO (emacs_event); /* #### */
|
1268
|
72 SET_EVENT_TIMEOUT_INTERVAL_ID (emacs_event,
|
|
73 pop_low_level_timeout (&tty_timer_queue, 0));
|
1204
|
74 SET_EVENT_TIMEOUT_FUNCTION (emacs_event, Qnil);
|
|
75 SET_EVENT_TIMEOUT_OBJECT (emacs_event, Qnil);
|
428
|
76 }
|
|
77
|
|
78
|
|
79
|
|
80 static int
|
1268
|
81 emacs_tty_event_pending_p (int how_many)
|
428
|
82 {
|
1268
|
83 if (!how_many)
|
428
|
84 {
|
|
85 EMACS_TIME sometime;
|
|
86 /* see if there's a pending timeout. */
|
|
87 EMACS_GET_TIME (sometime);
|
|
88 if (tty_timer_queue &&
|
|
89 EMACS_TIME_EQUAL_OR_GREATER (sometime, tty_timer_queue->time))
|
|
90 return 1;
|
1268
|
91
|
|
92 return poll_fds_for_input (non_fake_input_wait_mask);
|
428
|
93 }
|
|
94
|
1268
|
95 /* #### Not right! We need to *count* the number of pending events, which
|
|
96 means we need to have a dispatch queue and drain the pending events,
|
|
97 using drain_tty_devices(). */
|
|
98 return poll_fds_for_input (tty_only_mask);
|
428
|
99 }
|
|
100
|
|
101 static void
|
440
|
102 emacs_tty_next_event (Lisp_Event *emacs_event)
|
428
|
103 {
|
|
104 while (1)
|
|
105 {
|
|
106 int ndesc;
|
|
107 int i;
|
|
108 SELECT_TYPE temp_mask = input_wait_mask;
|
|
109 EMACS_TIME time_to_block;
|
|
110 EMACS_SELECT_TIME select_time_to_block, *pointer_to_this;
|
|
111
|
|
112 if (!get_low_level_timeout_interval (tty_timer_queue, &time_to_block))
|
|
113 /* no timer events; block indefinitely */
|
|
114 pointer_to_this = 0;
|
|
115 else
|
|
116 {
|
|
117 EMACS_TIME_TO_SELECT_TIME (time_to_block, select_time_to_block);
|
|
118 pointer_to_this = &select_time_to_block;
|
|
119 }
|
|
120
|
|
121 ndesc = select (MAXDESC, &temp_mask, 0, 0, pointer_to_this);
|
|
122 if (ndesc > 0)
|
|
123 {
|
|
124 /* Look for a TTY event */
|
|
125 for (i = 0; i < MAXDESC; i++)
|
|
126 {
|
|
127 /* To avoid race conditions (among other things, an infinite
|
|
128 loop when called from Fdiscard_input()), we must return
|
|
129 user events ahead of process events. */
|
|
130 if (FD_ISSET (i, &temp_mask) && FD_ISSET (i, &tty_only_mask))
|
|
131 {
|
1204
|
132 struct console *c = find_tty_or_stream_console_from_fd (i);
|
428
|
133
|
|
134 assert (c);
|
771
|
135 if (read_event_from_tty_or_stream_desc (emacs_event, c))
|
428
|
136 return;
|
|
137 }
|
|
138 }
|
|
139
|
|
140 /* Look for a process event */
|
|
141 for (i = 0; i < MAXDESC; i++)
|
|
142 {
|
|
143 if (FD_ISSET (i, &temp_mask) && FD_ISSET (i, &process_only_mask))
|
|
144 {
|
|
145 Lisp_Object process;
|
440
|
146 Lisp_Process *p = get_process_from_usid (FD_TO_USID(i));
|
428
|
147
|
|
148 assert (p);
|
793
|
149 process = wrap_process (p);
|
934
|
150 set_event_type (emacs_event, process_event);
|
|
151 /* process events have nil as channel */
|
|
152 SET_EVENT_TIMESTAMP_ZERO (emacs_event); /* #### */
|
1204
|
153 SET_EVENT_PROCESS_PROCESS (emacs_event, process);
|
428
|
154 return;
|
|
155 }
|
|
156 }
|
|
157
|
|
158 /* We might get here when a fake event came through a signal. */
|
|
159 /* Return a dummy event, so that a cycle of the command loop will
|
|
160 occur. */
|
|
161 drain_signal_event_pipe ();
|
934
|
162 set_event_type (emacs_event, eval_event);
|
|
163 /* eval events have nil as channel */
|
1204
|
164 SET_EVENT_EVAL_FUNCTION (emacs_event, Qidentity);
|
|
165 SET_EVENT_EVAL_OBJECT (emacs_event, Qnil);
|
428
|
166 return;
|
|
167 }
|
|
168 else if (ndesc == 0) /* timeout fired */
|
|
169 {
|
|
170 tty_timeout_to_emacs_event (emacs_event);
|
|
171 return;
|
|
172 }
|
|
173 }
|
|
174 }
|
|
175
|
|
176 static void
|
788
|
177 emacs_tty_format_magic_event (Lisp_Event *emacs_event, Lisp_Object pstream)
|
|
178 {
|
|
179 /* Nothing to do currently */
|
|
180 }
|
|
181
|
|
182 static int
|
|
183 emacs_tty_compare_magic_event (Lisp_Event *e1, Lisp_Event *e2)
|
|
184 {
|
|
185 return 1;
|
|
186 }
|
|
187
|
|
188 static Hashcode
|
|
189 emacs_tty_hash_magic_event (Lisp_Event *e)
|
|
190 {
|
|
191 return 0;
|
|
192 }
|
|
193
|
|
194 static void
|
440
|
195 emacs_tty_handle_magic_event (Lisp_Event *emacs_event)
|
428
|
196 {
|
|
197 /* Nothing to do currently */
|
|
198 }
|
|
199
|
|
200
|
|
201 static void
|
853
|
202 emacs_tty_select_process (Lisp_Process *process, int doin, int doerr)
|
428
|
203 {
|
853
|
204 int infd, errfd;
|
|
205
|
|
206 event_stream_unixoid_select_process (process, doin, doerr, &infd, &errfd);
|
428
|
207 }
|
|
208
|
|
209 static void
|
853
|
210 emacs_tty_unselect_process (Lisp_Process *process, int doin, int doerr)
|
428
|
211 {
|
853
|
212 int infd, errfd;
|
|
213
|
|
214 event_stream_unixoid_unselect_process (process, doin, doerr, &infd, &errfd);
|
428
|
215 }
|
|
216
|
|
217 static void
|
|
218 emacs_tty_select_console (struct console *con)
|
|
219 {
|
|
220 event_stream_unixoid_select_console (con);
|
|
221 }
|
|
222
|
|
223 static void
|
|
224 emacs_tty_unselect_console (struct console *con)
|
|
225 {
|
|
226 event_stream_unixoid_unselect_console (con);
|
|
227 }
|
|
228
|
|
229 static void
|
1204
|
230 emacs_tty_drain_queue (void)
|
428
|
231 {
|
1204
|
232 drain_tty_devices ();
|
428
|
233 }
|
|
234
|
853
|
235 static void
|
|
236 emacs_tty_create_io_streams (void* inhandle, void* outhandle,
|
|
237 void *errhandle, Lisp_Object* instream,
|
|
238 Lisp_Object* outstream,
|
|
239 Lisp_Object* errstream,
|
|
240 USID* in_usid,
|
|
241 USID* err_usid,
|
|
242 int flags)
|
428
|
243 {
|
853
|
244 event_stream_unixoid_create_io_streams
|
|
245 (inhandle, outhandle, errhandle, instream, outstream,
|
|
246 errstream, in_usid, err_usid, flags);
|
428
|
247 }
|
|
248
|
853
|
249 static void
|
|
250 emacs_tty_delete_io_streams (Lisp_Object instream,
|
|
251 Lisp_Object outstream,
|
|
252 Lisp_Object errstream,
|
|
253 USID* in_usid,
|
|
254 USID* err_usid)
|
428
|
255 {
|
853
|
256 event_stream_unixoid_delete_io_streams
|
|
257 (instream, outstream, errstream, in_usid, err_usid);
|
428
|
258 }
|
|
259
|
|
260
|
|
261 /************************************************************************/
|
|
262 /* initialization */
|
|
263 /************************************************************************/
|
|
264
|
|
265 void
|
|
266 reinit_vars_of_event_tty (void)
|
|
267 {
|
1204
|
268 tty_event_stream = xnew_and_zero (struct event_stream);
|
428
|
269
|
|
270 tty_event_stream->event_pending_p = emacs_tty_event_pending_p;
|
|
271 tty_event_stream->next_event_cb = emacs_tty_next_event;
|
|
272 tty_event_stream->handle_magic_event_cb = emacs_tty_handle_magic_event;
|
788
|
273 tty_event_stream->format_magic_event_cb = emacs_tty_format_magic_event;
|
|
274 tty_event_stream->compare_magic_event_cb= emacs_tty_compare_magic_event;
|
|
275 tty_event_stream->hash_magic_event_cb = emacs_tty_hash_magic_event;
|
428
|
276 tty_event_stream->add_timeout_cb = emacs_tty_add_timeout;
|
|
277 tty_event_stream->remove_timeout_cb = emacs_tty_remove_timeout;
|
|
278 tty_event_stream->select_console_cb = emacs_tty_select_console;
|
|
279 tty_event_stream->unselect_console_cb = emacs_tty_unselect_console;
|
|
280 tty_event_stream->select_process_cb = emacs_tty_select_process;
|
|
281 tty_event_stream->unselect_process_cb = emacs_tty_unselect_process;
|
1204
|
282 tty_event_stream->drain_queue_cb = emacs_tty_drain_queue;
|
853
|
283 tty_event_stream->create_io_streams_cb = emacs_tty_create_io_streams;
|
|
284 tty_event_stream->delete_io_streams_cb = emacs_tty_delete_io_streams;
|
428
|
285 }
|
|
286
|
|
287 void
|
|
288 vars_of_event_tty (void)
|
|
289 {
|
|
290 reinit_vars_of_event_tty ();
|
|
291 }
|
|
292
|
|
293 void
|
|
294 init_event_tty_late (void)
|
|
295 {
|
|
296 event_stream = tty_event_stream;
|
|
297 }
|