22 pixel high Nimbus-like font, containing only digits and the period; intended for...
[kugel-rb.git] / apps / plugins / frotz / stream.c
blob4ccb44451cbe627c8ec869469a372f8faa19995e
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include "frotz.h"
23 extern bool handle_hot_key (zchar);
25 extern bool validate_click (void);
27 extern void replay_open (void);
28 extern void replay_close (void);
29 extern void memory_open (zword, zword, bool);
30 extern void memory_close (void);
31 extern void record_open (void);
32 extern void record_close (void);
33 extern void script_open (void);
34 extern void script_close (void);
36 extern void memory_word (const zchar *);
37 extern void memory_new_line (void);
38 extern void record_write_key (zchar);
39 extern void record_write_input (const zchar *, zchar);
40 extern void script_char (zchar);
41 extern void script_word (const zchar *);
42 extern void script_new_line (void);
43 extern void script_write_input (const zchar *, zchar);
44 extern void script_erase_input (const zchar *);
45 extern void script_mssg_on (void);
46 extern void script_mssg_off (void);
47 extern void screen_char (zchar);
48 extern void screen_word (const zchar *);
49 extern void screen_new_line (void);
50 extern void screen_write_input (const zchar *, zchar);
51 extern void screen_erase_input (const zchar *);
52 extern void screen_mssg_on (void);
53 extern void screen_mssg_off (void);
55 extern zchar replay_read_key (void);
56 extern zchar replay_read_input (zchar *);
57 extern zchar console_read_key (zword);
58 extern zchar console_read_input (int, zchar *, zword, bool);
60 extern int direct_call (zword);
63 * stream_mssg_on
65 * Start printing a "debugging" message.
69 void stream_mssg_on (void)
72 flush_buffer ();
74 if (ostream_screen)
75 screen_mssg_on ();
76 if (ostream_script && enable_scripting)
77 script_mssg_on ();
79 message = TRUE;
81 }/* stream_mssg_on */
84 * stream_mssg_off
86 * Stop printing a "debugging" message.
90 void stream_mssg_off (void)
93 flush_buffer ();
95 if (ostream_screen)
96 screen_mssg_off ();
97 if (ostream_script && enable_scripting)
98 script_mssg_off ();
100 message = FALSE;
102 }/* stream_mssg_off */
105 * z_output_stream, open or close an output stream.
107 * zargs[0] = stream to open (positive) or close (negative)
108 * zargs[1] = address to redirect output to (stream 3 only)
109 * zargs[2] = width of redirected output (stream 3 only, optional)
113 void z_output_stream (void)
116 flush_buffer ();
118 switch ((short) zargs[0]) {
120 case 1: ostream_screen = TRUE;
121 break;
122 case -1: ostream_screen = FALSE;
123 break;
124 case 2: if (!ostream_script) script_open ();
125 break;
126 case -2: if (ostream_script) script_close ();
127 break;
128 case 3: memory_open (zargs[1], zargs[2], zargc >= 3);
129 break;
130 case -3: memory_close ();
131 break;
132 case 4: if (!ostream_record) record_open ();
133 break;
134 case -4: if (ostream_record) record_close ();
135 break;
139 }/* z_output_stream */
142 * stream_char
144 * Send a single character to the output stream.
148 void stream_char (zchar c)
151 if (ostream_screen)
152 screen_char (c);
153 if (ostream_script && enable_scripting)
154 script_char (c);
156 }/* stream_char */
159 * stream_word
161 * Send a string of characters to the output streams.
165 void stream_word (const zchar *s)
168 if (ostream_memory && !message)
170 memory_word (s);
172 else {
174 if (ostream_screen)
175 screen_word (s);
176 if (ostream_script && enable_scripting)
177 script_word (s);
181 }/* stream_word */
184 * stream_new_line
186 * Send a newline to the output streams.
190 void stream_new_line (void)
193 if (ostream_memory && !message)
195 memory_new_line ();
197 else {
199 if (ostream_screen)
200 screen_new_line ();
201 if (ostream_script && enable_scripting)
202 script_new_line ();
206 }/* stream_new_line */
209 * z_input_stream, select an input stream.
211 * zargs[0] = input stream to be selected
215 void z_input_stream (void)
218 flush_buffer ();
220 if (zargs[0] == 0 && istream_replay)
221 replay_close ();
222 if (zargs[0] == 1 && !istream_replay)
223 replay_open ();
225 }/* z_input_stream */
228 * stream_read_key
230 * Read a single keystroke from the current input stream.
234 zchar stream_read_key ( zword timeout, zword routine,
235 bool hot_keys )
237 zchar key = ZC_BAD;
239 flush_buffer ();
241 /* Read key from current input stream */
243 continue_input:
245 do {
247 if (istream_replay)
248 key = replay_read_key ();
249 else
250 key = console_read_key (timeout);
252 } while (key == ZC_BAD);
254 /* Verify mouse clicks */
256 if (key == ZC_SINGLE_CLICK || key == ZC_DOUBLE_CLICK)
257 if (!validate_click ())
258 goto continue_input;
260 /* Copy key to the command file */
262 if (ostream_record && !istream_replay)
263 record_write_key (key);
265 /* Handle timeouts */
267 if (key == ZC_TIME_OUT)
268 if (direct_call (routine) == 0)
269 goto continue_input;
271 /* Handle hot keys */
273 if (hot_keys && key >= ZC_HKEY_MIN && key <= ZC_HKEY_MAX) {
275 if (h_version == V4 && key == ZC_HKEY_UNDO)
276 goto continue_input;
277 if (!handle_hot_key (key))
278 goto continue_input;
280 return ZC_BAD;
284 /* Return key */
286 return key;
288 }/* stream_read_key */
291 * stream_read_input
293 * Read a line of input from the current input stream.
297 zchar stream_read_input ( int max, zchar *buf,
298 zword timeout, zword routine,
299 bool hot_keys,
300 bool no_scripting )
302 zchar key = ZC_BAD;
304 flush_buffer ();
306 /* Remove initial input from the transscript file or from the screen */
308 if (ostream_script && enable_scripting && !no_scripting)
309 script_erase_input (buf);
310 if (istream_replay)
311 screen_erase_input (buf);
313 /* Read input line from current input stream */
315 continue_input:
317 do {
319 if (istream_replay)
320 key = replay_read_input (buf);
321 else
322 key = console_read_input (max, buf, timeout, key != ZC_BAD);
324 } while (key == ZC_BAD);
326 /* Verify mouse clicks */
328 if (key == ZC_SINGLE_CLICK || key == ZC_DOUBLE_CLICK)
329 if (!validate_click ())
330 goto continue_input;
332 /* Copy input line to the command file */
334 if (ostream_record && !istream_replay)
335 record_write_input (buf, key);
337 /* Handle timeouts */
339 if (key == ZC_TIME_OUT)
340 if (direct_call (routine) == 0)
341 goto continue_input;
343 /* Handle hot keys */
345 if (hot_keys && key >= ZC_HKEY_MIN && key <= ZC_HKEY_MAX) {
347 if (!handle_hot_key (key))
348 goto continue_input;
350 return ZC_BAD;
354 /* Copy input line to transscript file or to the screen */
356 if (ostream_script && enable_scripting && !no_scripting)
357 script_write_input (buf, key);
358 if (istream_replay)
359 screen_write_input (buf, key);
361 /* Return terminating key */
363 return key;
365 }/* stream_read_input */