summaryrefslogtreecommitdiff
path: root/apps/plugins/frotz/stream.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/frotz/stream.c')
-rw-r--r--apps/plugins/frotz/stream.c365
1 files changed, 365 insertions, 0 deletions
diff --git a/apps/plugins/frotz/stream.c b/apps/plugins/frotz/stream.c
new file mode 100644
index 0000000000..4ccb44451c
--- /dev/null
+++ b/apps/plugins/frotz/stream.c
@@ -0,0 +1,365 @@
1/* stream.c - IO stream implementation
2 * Copyright (c) 1995-1997 Stefan Jokisch
3 *
4 * This file is part of Frotz.
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "frotz.h"
22
23extern bool handle_hot_key (zchar);
24
25extern bool validate_click (void);
26
27extern void replay_open (void);
28extern void replay_close (void);
29extern void memory_open (zword, zword, bool);
30extern void memory_close (void);
31extern void record_open (void);
32extern void record_close (void);
33extern void script_open (void);
34extern void script_close (void);
35
36extern void memory_word (const zchar *);
37extern void memory_new_line (void);
38extern void record_write_key (zchar);
39extern void record_write_input (const zchar *, zchar);
40extern void script_char (zchar);
41extern void script_word (const zchar *);
42extern void script_new_line (void);
43extern void script_write_input (const zchar *, zchar);
44extern void script_erase_input (const zchar *);
45extern void script_mssg_on (void);
46extern void script_mssg_off (void);
47extern void screen_char (zchar);
48extern void screen_word (const zchar *);
49extern void screen_new_line (void);
50extern void screen_write_input (const zchar *, zchar);
51extern void screen_erase_input (const zchar *);
52extern void screen_mssg_on (void);
53extern void screen_mssg_off (void);
54
55extern zchar replay_read_key (void);
56extern zchar replay_read_input (zchar *);
57extern zchar console_read_key (zword);
58extern zchar console_read_input (int, zchar *, zword, bool);
59
60extern int direct_call (zword);
61
62/*
63 * stream_mssg_on
64 *
65 * Start printing a "debugging" message.
66 *
67 */
68
69void stream_mssg_on (void)
70{
71
72 flush_buffer ();
73
74 if (ostream_screen)
75 screen_mssg_on ();
76 if (ostream_script && enable_scripting)
77 script_mssg_on ();
78
79 message = TRUE;
80
81}/* stream_mssg_on */
82
83/*
84 * stream_mssg_off
85 *
86 * Stop printing a "debugging" message.
87 *
88 */
89
90void stream_mssg_off (void)
91{
92
93 flush_buffer ();
94
95 if (ostream_screen)
96 screen_mssg_off ();
97 if (ostream_script && enable_scripting)
98 script_mssg_off ();
99
100 message = FALSE;
101
102}/* stream_mssg_off */
103
104/*
105 * z_output_stream, open or close an output stream.
106 *
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)
110 *
111 */
112
113void z_output_stream (void)
114{
115
116 flush_buffer ();
117
118 switch ((short) zargs[0]) {
119
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;
136
137 }
138
139}/* z_output_stream */
140
141/*
142 * stream_char
143 *
144 * Send a single character to the output stream.
145 *
146 */
147
148void stream_char (zchar c)
149{
150
151 if (ostream_screen)
152 screen_char (c);
153 if (ostream_script && enable_scripting)
154 script_char (c);
155
156}/* stream_char */
157
158/*
159 * stream_word
160 *
161 * Send a string of characters to the output streams.
162 *
163 */
164
165void stream_word (const zchar *s)
166{
167
168 if (ostream_memory && !message)
169
170 memory_word (s);
171
172 else {
173
174 if (ostream_screen)
175 screen_word (s);
176 if (ostream_script && enable_scripting)
177 script_word (s);
178
179 }
180
181}/* stream_word */
182
183/*
184 * stream_new_line
185 *
186 * Send a newline to the output streams.
187 *
188 */
189
190void stream_new_line (void)
191{
192
193 if (ostream_memory && !message)
194
195 memory_new_line ();
196
197 else {
198
199 if (ostream_screen)
200 screen_new_line ();
201 if (ostream_script && enable_scripting)
202 script_new_line ();
203
204 }
205
206}/* stream_new_line */
207
208/*
209 * z_input_stream, select an input stream.
210 *
211 * zargs[0] = input stream to be selected
212 *
213 */
214
215void z_input_stream (void)
216{
217
218 flush_buffer ();
219
220 if (zargs[0] == 0 && istream_replay)
221 replay_close ();
222 if (zargs[0] == 1 && !istream_replay)
223 replay_open ();
224
225}/* z_input_stream */
226
227/*
228 * stream_read_key
229 *
230 * Read a single keystroke from the current input stream.
231 *
232 */
233
234zchar stream_read_key ( zword timeout, zword routine,
235 bool hot_keys )
236{
237 zchar key = ZC_BAD;
238
239 flush_buffer ();
240
241 /* Read key from current input stream */
242
243continue_input:
244
245 do {
246
247 if (istream_replay)
248 key = replay_read_key ();
249 else
250 key = console_read_key (timeout);
251
252 } while (key == ZC_BAD);
253
254 /* Verify mouse clicks */
255
256 if (key == ZC_SINGLE_CLICK || key == ZC_DOUBLE_CLICK)
257 if (!validate_click ())
258 goto continue_input;
259
260 /* Copy key to the command file */
261
262 if (ostream_record && !istream_replay)
263 record_write_key (key);
264
265 /* Handle timeouts */
266
267 if (key == ZC_TIME_OUT)
268 if (direct_call (routine) == 0)
269 goto continue_input;
270
271 /* Handle hot keys */
272
273 if (hot_keys && key >= ZC_HKEY_MIN && key <= ZC_HKEY_MAX) {
274
275 if (h_version == V4 && key == ZC_HKEY_UNDO)
276 goto continue_input;
277 if (!handle_hot_key (key))
278 goto continue_input;
279
280 return ZC_BAD;
281
282 }
283
284 /* Return key */
285
286 return key;
287
288}/* stream_read_key */
289
290/*
291 * stream_read_input
292 *
293 * Read a line of input from the current input stream.
294 *
295 */
296
297zchar stream_read_input ( int max, zchar *buf,
298 zword timeout, zword routine,
299 bool hot_keys,
300 bool no_scripting )
301{
302 zchar key = ZC_BAD;
303
304 flush_buffer ();
305
306 /* Remove initial input from the transscript file or from the screen */
307
308 if (ostream_script && enable_scripting && !no_scripting)
309 script_erase_input (buf);
310 if (istream_replay)
311 screen_erase_input (buf);
312
313 /* Read input line from current input stream */
314
315continue_input:
316
317 do {
318
319 if (istream_replay)
320 key = replay_read_input (buf);
321 else
322 key = console_read_input (max, buf, timeout, key != ZC_BAD);
323
324 } while (key == ZC_BAD);
325
326 /* Verify mouse clicks */
327
328 if (key == ZC_SINGLE_CLICK || key == ZC_DOUBLE_CLICK)
329 if (!validate_click ())
330 goto continue_input;
331
332 /* Copy input line to the command file */
333
334 if (ostream_record && !istream_replay)
335 record_write_input (buf, key);
336
337 /* Handle timeouts */
338
339 if (key == ZC_TIME_OUT)
340 if (direct_call (routine) == 0)
341 goto continue_input;
342
343 /* Handle hot keys */
344
345 if (hot_keys && key >= ZC_HKEY_MIN && key <= ZC_HKEY_MAX) {
346
347 if (!handle_hot_key (key))
348 goto continue_input;
349
350 return ZC_BAD;
351
352 }
353
354 /* Copy input line to transscript file or to the screen */
355
356 if (ostream_script && enable_scripting && !no_scripting)
357 script_write_input (buf, key);
358 if (istream_replay)
359 screen_write_input (buf, key);
360
361 /* Return terminating key */
362
363 return key;
364
365}/* stream_read_input */