update.
[chise/xemacs-chise.git.1] / src / console-stream.c
1 /* Stream device functions.
2    Copyright (C) 1995 Free Software Foundation, Inc.
3    Copyright (C) 1996 Ben Wing.
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* Synched up with: Not in FSF. */
23
24 /* This file has been Mule-ized. */
25
26 /* Written by Ben Wing. */
27
28 #include <config.h>
29 #include "lisp.h"
30
31 #include "console-stream.h"
32 #include "console-tty.h"
33 #include "events.h"
34 #include "frame.h"
35 #include "redisplay.h"
36 #include "sysdep.h"
37 #include "sysfile.h"
38 #include "window.h"
39
40 DEFINE_CONSOLE_TYPE (stream);
41
42 Lisp_Object Vterminal_console;
43 Lisp_Object Vterminal_device;
44 Lisp_Object Vterminal_frame;
45
46 Lisp_Object Vstdio_str;
47
48 static void
49 stream_init_console (struct console *con, Lisp_Object params)
50 {
51   Lisp_Object tty = CONSOLE_CONNECTION (con);
52   struct stream_console *stream_con;
53
54   if (CONSOLE_STREAM_DATA (con) == NULL)
55     CONSOLE_STREAM_DATA (con) = xnew (struct stream_console);
56
57   stream_con = CONSOLE_STREAM_DATA (con);
58
59   stream_con->needs_newline = 0;
60
61   /* Open the specified console */
62   if (NILP (tty) || internal_equal (tty, Vstdio_str, 0))
63     {
64       stream_con->in  = stdin;
65       stream_con->out = stdout;
66       stream_con->err = stderr;
67     }
68   else
69     {
70       CHECK_STRING (tty);
71       stream_con->in = stream_con->out = stream_con->err =
72         /* #### We don't currently do coding-system translation on
73            this descriptor. */
74         fopen ((char *) XSTRING_DATA (tty), READ_PLUS_TEXT);
75       if (!stream_con->in)
76         error ("Unable to open tty %s", XSTRING_DATA (tty));
77     }
78 }
79
80 static void
81 stream_init_device (struct device *d, Lisp_Object params)
82 {
83   struct console *con = XCONSOLE (DEVICE_CONSOLE (d));
84
85   DEVICE_INFD  (d) = fileno (CONSOLE_STREAM_DATA (con)->in);
86   DEVICE_OUTFD (d) = fileno (CONSOLE_STREAM_DATA (con)->out);
87   init_baud_rate (d);
88   init_one_device (d);
89 }
90
91 static int
92 stream_initially_selected_for_input (struct console *con)
93 {
94   return noninteractive && initialized;
95 }
96
97 extern int stdout_needs_newline;
98
99 static void
100 stream_delete_console (struct console *con)
101 {
102   struct stream_console *stream_con = CONSOLE_STREAM_DATA (con);
103   if (stream_con)
104     {
105       if (/* stream_con->needs_newline */
106           stdout_needs_newline) /* #### clean this up */
107         {
108           fputc ('\n', stream_con->out);
109           fflush (stream_con->out);
110         }
111       if (stream_con->in != stdin)
112         fclose (stream_con->in);
113
114       xfree (stream_con);
115       CONSOLE_STREAM_DATA (con) = NULL;
116     }
117 }
118
119 Lisp_Object
120 stream_semi_canonicalize_console_connection (Lisp_Object connection,
121                                              Error_behavior errb)
122 {
123   return NILP (connection) ? Vstdio_str : connection;
124 }
125
126 Lisp_Object
127 stream_canonicalize_console_connection (Lisp_Object connection,
128                                         Error_behavior errb)
129 {
130   if (NILP (connection) || internal_equal (connection, Vstdio_str, 0))
131     return Vstdio_str;
132
133   if (!ERRB_EQ (errb, ERROR_ME))
134     {
135       if (!STRINGP (connection))
136         return Qunbound;
137     }
138   else
139     CHECK_STRING (connection);
140
141   return Ffile_truename (connection, Qnil);
142 }
143
144 Lisp_Object
145 stream_semi_canonicalize_device_connection (Lisp_Object connection,
146                                             Error_behavior errb)
147 {
148   return stream_semi_canonicalize_console_connection (connection, errb);
149 }
150
151 Lisp_Object
152 stream_canonicalize_device_connection (Lisp_Object connection,
153                                        Error_behavior errb)
154 {
155   return stream_canonicalize_console_connection (connection, errb);
156 }
157
158 \f
159 static void
160 stream_init_frame_1 (struct frame *f, Lisp_Object props)
161 {
162 #if 0
163   struct device *d = XDEVICE (FRAME_DEVICE (f));
164   if (!NILP (DEVICE_FRAME_LIST (d)))
165     error ("Only one frame allowed on stream devices");
166 #endif
167   f->name = build_string ("stream");
168   f->height = 80;
169   f->width = 24;
170   f->visible = 0; /* so redisplay doesn't try to do anything */
171 }
172
173 \f
174 static int
175 stream_text_width (struct frame *f, struct face_cachel *cachel,
176                    const Charc *str, Charcount len)
177 {
178   return len;
179 }
180
181 static int
182 stream_left_margin_width (struct window *w)
183 {
184   return 0;
185 }
186
187 static int
188 stream_right_margin_width (struct window *w)
189 {
190   return 0;
191 }
192
193 static int
194 stream_divider_height (void)
195 {
196   return 1;
197 }
198
199 static int
200 stream_eol_cursor_width (void)
201 {
202   return 1;
203 }
204
205 static void
206 stream_output_display_block (struct window *w, struct display_line *dl,
207                              int block, int start, int end,
208                              int start_pixpos, int cursor_start,
209                              int cursor_width, int cursor_height)
210 {
211 }
212
213 static void
214 stream_clear_region (Lisp_Object window, struct device* d, struct frame * f,
215                   face_index findex, int x, int y,
216                   int width, int height, Lisp_Object fcolor, Lisp_Object bcolor,
217                   Lisp_Object background_pixmap)
218 {
219 }
220
221 static int
222 stream_flash (struct device *d)
223 {
224   return 0; /* sorry can't do it */
225 }
226
227 static void
228 stream_ring_bell (struct device *d, int volume, int pitch, int duration)
229 {
230   struct console *c = XCONSOLE (DEVICE_CONSOLE (d));
231   fputc (07, CONSOLE_STREAM_DATA (c)->out);
232   fflush (CONSOLE_STREAM_DATA (c)->out);
233 }
234
235 \f
236 /************************************************************************/
237 /*                            initialization                            */
238 /************************************************************************/
239
240 void
241 console_type_create_stream (void)
242 {
243   INITIALIZE_CONSOLE_TYPE (stream, "stream", "console-stream-p");
244
245   /* console methods */
246   CONSOLE_HAS_METHOD (stream, init_console);
247   CONSOLE_HAS_METHOD (stream, initially_selected_for_input);
248   CONSOLE_HAS_METHOD (stream, delete_console);
249   CONSOLE_HAS_METHOD (stream, canonicalize_console_connection);
250   CONSOLE_HAS_METHOD (stream, canonicalize_device_connection);
251   CONSOLE_HAS_METHOD (stream, semi_canonicalize_console_connection);
252   CONSOLE_HAS_METHOD (stream, semi_canonicalize_device_connection);
253
254   /* device methods */
255   CONSOLE_HAS_METHOD (stream, init_device);
256
257   /* frame methods */
258   CONSOLE_HAS_METHOD (stream, init_frame_1);
259
260   /* redisplay methods */
261   CONSOLE_HAS_METHOD (stream, left_margin_width);
262   CONSOLE_HAS_METHOD (stream, right_margin_width);
263   CONSOLE_HAS_METHOD (stream, text_width);
264   CONSOLE_HAS_METHOD (stream, output_display_block);
265   CONSOLE_HAS_METHOD (stream, divider_height);
266   CONSOLE_HAS_METHOD (stream, eol_cursor_width);
267   CONSOLE_HAS_METHOD (stream, clear_region);
268   CONSOLE_HAS_METHOD (stream, flash);
269   CONSOLE_HAS_METHOD (stream, ring_bell);
270 }
271
272 void
273 reinit_console_type_create_stream (void)
274 {
275   REINITIALIZE_CONSOLE_TYPE (stream);
276 }
277
278 void
279 vars_of_console_stream (void)
280 {
281   DEFVAR_LISP ("terminal-console", &Vterminal_console /*
282 The initial console object, which represents XEmacs' stdout.
283 */ );
284   Vterminal_console = Qnil;
285
286   DEFVAR_LISP ("terminal-device", &Vterminal_device /*
287 The initial device object, which represents XEmacs' stdout.
288 */ );
289   Vterminal_device = Qnil;
290
291   DEFVAR_LISP ("terminal-frame", &Vterminal_frame /*
292 The initial frame object, which represents XEmacs' stdout.
293 */ );
294   Vterminal_frame = Qnil;
295
296   /* Moved from console-tty.c */
297   Vstdio_str = build_string ("stdio");
298   staticpro (&Vstdio_str);
299 }
300
301 #ifndef PDUMP
302 void
303 init_console_stream (int reinit)
304 {
305   /* This function can GC */
306   if (!initialized)
307     {
308       Vterminal_device = Fmake_device (Qstream, Qnil, Qnil);
309       Vterminal_console = Fdevice_console (Vterminal_device);
310       Vterminal_frame = Fmake_frame (Qnil, Vterminal_device);
311       minibuf_window = XFRAME (Vterminal_frame)->minibuffer_window;
312     }
313   else
314     {
315       /* Re-initialize the FILE fields of the console. */
316       stream_init_console (XCONSOLE (Vterminal_console), Qnil);
317       if (noninteractive)
318         event_stream_select_console (XCONSOLE (Vterminal_console));
319     }
320 }
321
322 #else
323
324 void
325 init_console_stream (int reinit)
326 {
327   /* This function can GC */
328   if (!reinit)
329     {
330       Vterminal_device = Fmake_device (Qstream, Qnil, Qnil);
331       Vterminal_console = Fdevice_console (Vterminal_device);
332       Vterminal_frame = Fmake_frame (Qnil, Vterminal_device);
333       minibuf_window = XFRAME (Vterminal_frame)->minibuffer_window;
334     }
335   if (initialized)
336     {
337       stream_init_console (XCONSOLE (Vterminal_console), Qnil);
338       if (noninteractive)
339         event_stream_select_console (XCONSOLE (Vterminal_console));
340     }
341 }
342 #endif