1 /* stream.c - IO stream implementation
2 * Copyright (c) 1995-1997 Stefan Jokisch
4 * This file is part of Frotz.
6 * Frotz is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Frotz is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 zchar console_read_input (int max, zchar *buf, zword timeout, bool continued)
26 return os_read_line(max, buf, timeout, max, continued);
28 zchar console_read_key (zword timeout)
30 return os_read_key(timeout, 0);
33 /* extern bool handle_hot_key (zword); */
35 extern bool validate_click (void);
37 extern void replay_open (void);
38 extern void replay_close (void);
39 extern void memory_open (zword, zword, bool);
40 extern void memory_close (void);
41 extern void record_open (void);
42 extern void record_close (void);
43 extern void script_open (void);
44 extern void script_close (void);
46 extern void memory_word (const zchar *);
47 extern void memory_new_line (void);
48 extern void record_write_key (zchar);
49 extern void record_write_input (const zchar *, zchar);
50 extern void script_char (zchar);
51 extern void script_word (const zchar *);
52 extern void script_new_line (void);
53 extern void script_write_input (const zchar *, zchar);
54 extern void script_erase_input (const zchar *);
55 extern void script_mssg_on (void);
56 extern void script_mssg_off (void);
57 extern void screen_char (zchar);
58 extern void screen_word (const zchar *);
59 extern void screen_new_line (void);
60 extern void screen_write_input (const zchar *, zchar);
61 extern void screen_erase_input (const zchar *);
62 extern void screen_mssg_on (void);
63 extern void screen_mssg_off (void);
65 extern zword replay_read_key (void);
66 extern zword replay_read_input (zchar *);
68 extern int direct_call (zword);
73 * Write a single character to the scrollback buffer.
77 void scrollback_char (zchar c)
81 { scrollback_char (' '); scrollback_char (' '); scrollback_char (' '); return; }
83 { scrollback_char (' '); scrollback_char (' '); return; }
85 os_scrollback_char (c);
87 }/* scrollback_char */
92 * Write a string to the scrollback buffer.
96 void scrollback_word (const zchar *s)
100 for (i = 0; s[i] != 0; i++)
102 if (s[i] == ZC_NEW_FONT || s[i] == ZC_NEW_STYLE)
105 scrollback_char (s[i]);
107 }/* scrollback_word */
110 * scrollback_write_input
112 * Send an input line to the scrollback buffer.
116 void scrollback_write_input (const zchar *buf, zchar key)
120 for (i = 0; buf[i] != 0; i++)
121 scrollback_char (buf[i]);
123 if (key == ZC_RETURN)
124 scrollback_char ('\n');
126 }/* scrollback_write_input */
129 * scrollback_erase_input
131 * Remove an input line from the scrollback buffer.
135 void scrollback_erase_input (const zchar *buf)
140 for (i = 0, width = 0; buf[i] != 0; i++)
143 os_scrollback_erase (width);
145 }/* scrollback_erase_input */
150 * Start printing a "debugging" message.
154 void stream_mssg_on (void)
161 if (ostream_script && enable_scripting)
166 }/* stream_mssg_on */
171 * Stop printing a "debugging" message.
175 void stream_mssg_off (void)
182 if (ostream_script && enable_scripting)
187 }/* stream_mssg_off */
190 * z_output_stream, open or close an output stream.
192 * zargs[0] = stream to open (positive) or close (negative)
193 * zargs[1] = address to redirect output to (stream 3 only)
194 * zargs[2] = width of redirected output (stream 3 only, optional)
198 void z_output_stream (void)
203 switch ((short) zargs[0]) {
205 case 1: ostream_screen = TRUE;
207 case -1: ostream_screen = FALSE;
209 case 2: if (!ostream_script) script_open ();
211 case -2: if (ostream_script) script_close ();
213 case 3: memory_open (zargs[1], zargs[2], zargc >= 3);
215 case -3: memory_close ();
217 case 4: if (!ostream_record) record_open ();
219 case -4: if (ostream_record) record_close ();
224 }/* z_output_stream */
229 * Send a single character to the output stream.
233 void stream_char (zchar c)
238 if (ostream_script && enable_scripting)
240 if (enable_scripting)
248 * Send a string of characters to the output streams.
252 void stream_word (const zchar *s)
255 if (ostream_memory && !message)
263 if (ostream_script && enable_scripting)
265 if (enable_scripting)
274 * Send a newline to the output streams.
278 void stream_new_line (void)
281 if (ostream_memory && !message)
289 if (ostream_script && enable_scripting)
291 if (enable_scripting)
292 os_scrollback_char ('\n');
296 }/* stream_new_line */
299 * z_input_stream, select an input stream.
301 * zargs[0] = input stream to be selected
305 void z_input_stream (void)
310 if (zargs[0] == 0 && istream_replay)
312 if (zargs[0] == 1 && !istream_replay)
315 }/* z_input_stream */
320 * Read a single keystroke from the current input stream.
324 zchar stream_read_key ( zword timeout, zword routine, bool hot_keys )
330 /* Read key from current input stream */
337 key = replay_read_key ();
339 key = console_read_key (timeout);
341 } while (key == ZC_BAD);
343 /* Verify mouse clicks */
346 if (key == ZC_SINGLE_CLICK || key == ZC_DOUBLE_CLICK)
347 if (!validate_click ())
351 /* Copy key to the command file */
353 if (ostream_record && !istream_replay)
354 record_write_key (key);
356 /* Handle timeouts */
358 if (key == ZC_TIME_OUT)
359 if (direct_call (routine) == 0)
365 if (hot_keys && key >= ZC_HKEY_MIN && key <= ZC_HKEY_MAX) {
367 if (h_version == V4 && key == ZC_HKEY_UNDO)
369 if (!handle_hot_key (key))
381 }/* stream_read_key */
386 * Read a line of input from the current input stream.
390 zchar stream_read_input ( int max, zchar *buf,
391 zword timeout, zword routine,
396 bool no_scrollback = no_scripting;
398 if (h_version == V6 && story_id == UNKNOWN && !ostream_script)
399 no_scrollback = FALSE;
403 /* Remove initial input from the transscript file or from the screen */
405 if (ostream_script && enable_scripting && !no_scripting)
406 script_erase_input (buf);
407 //glkify if (enable_scripting && !no_scrollback)
408 //glkify scrollback_erase_input (buf);
409 //glkify if (istream_replay)
410 //glkify screen_erase_input (buf);
412 /* Read input line from current input stream */
419 key = replay_read_input (buf);
421 key = console_read_input (max, buf, timeout, key != ZC_BAD);
423 } while (key == ZC_BAD);
425 /* Verify mouse clicks */
428 if (key == ZC_SINGLE_CLICK || key == ZC_DOUBLE_CLICK)
429 if (!validate_click ())
433 /* Copy input line to the command file */
435 if (ostream_record && !istream_replay)
436 record_write_input (buf, key);
438 /* Handle timeouts */
440 if (key == ZC_TIME_OUT)
441 if (direct_call (routine) == 0)
447 if (hot_keys && key >= ZC_HKEY_MIN && key <= ZC_HKEY_MAX) {
449 if (!handle_hot_key (key))
457 /* Copy input line to transscript file or to the screen */
459 if (ostream_script && enable_scripting && !no_scripting)
460 script_write_input (buf, key);
461 //glkify if (enable_scripting && !no_scrollback)
462 //glkify scrollback_write_input (buf, key);
463 //glkify if (istream_replay)
464 //glkify screen_write_input (buf, key);
466 /* Return terminating key */
470 }/* stream_read_input */