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
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
);
65 * Start printing a "debugging" message.
69 void stream_mssg_on (void)
76 if (ostream_script
&& enable_scripting
)
86 * Stop printing a "debugging" message.
90 void stream_mssg_off (void)
97 if (ostream_script
&& enable_scripting
)
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)
118 switch ((short) zargs
[0]) {
120 case 1: ostream_screen
= TRUE
;
122 case -1: ostream_screen
= FALSE
;
124 case 2: if (!ostream_script
) script_open ();
126 case -2: if (ostream_script
) script_close ();
128 case 3: memory_open (zargs
[1], zargs
[2], zargc
>= 3);
130 case -3: memory_close ();
132 case 4: if (!ostream_record
) record_open ();
134 case -4: if (ostream_record
) record_close ();
139 }/* z_output_stream */
144 * Send a single character to the output stream.
148 void stream_char (zchar c
)
153 if (ostream_script
&& enable_scripting
)
161 * Send a string of characters to the output streams.
165 void stream_word (const zchar
*s
)
168 if (ostream_memory
&& !message
)
176 if (ostream_script
&& enable_scripting
)
186 * Send a newline to the output streams.
190 void stream_new_line (void)
193 if (ostream_memory
&& !message
)
201 if (ostream_script
&& enable_scripting
)
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)
220 if (zargs
[0] == 0 && istream_replay
)
222 if (zargs
[0] == 1 && !istream_replay
)
225 }/* z_input_stream */
230 * Read a single keystroke from the current input stream.
234 zchar
stream_read_key ( zword timeout
, zword routine
,
241 /* Read key from current input stream */
248 key
= replay_read_key ();
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 ())
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)
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
)
277 if (!handle_hot_key (key
))
288 }/* stream_read_key */
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
,
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
);
311 screen_erase_input (buf
);
313 /* Read input line from current input stream */
320 key
= replay_read_input (buf
);
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 ())
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)
343 /* Handle hot keys */
345 if (hot_keys
&& key
>= ZC_HKEY_MIN
&& key
<= ZC_HKEY_MAX
) {
347 if (!handle_hot_key (key
))
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
);
359 screen_write_input (buf
, key
);
361 /* Return terminating key */
365 }/* stream_read_input */