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