1 /* input.c - High level input functions
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
23 extern int save_undo (void);
25 extern zchar stream_read_key (zword, zword, bool);
26 extern zchar stream_read_input (int, zchar *, zword, zword, bool, bool);
28 extern void tokenise_line (zword, zword, zword, bool);
29 zchar unicode_tolower (zchar);
34 * Check if the given key is an input terminator.
38 bool is_terminator (zchar key)
41 if (key == ZC_TIME_OUT)
45 if (key >= ZC_HKEY_MIN && key <= ZC_HKEY_MAX)
48 if (h_terminating_keys != 0)
50 if (key >= ZC_ARROW_MIN && key <= ZC_MENU_CLICK) {
52 zword addr = h_terminating_keys;
57 if (c == 255 || key == translate_from_zscii (c))
69 * z_make_menu, add or remove a menu and branch if successful.
71 * zargs[0] = number of menu
72 * zargs[1] = table of menu entries or 0 to remove menu
76 void z_make_menu (void)
79 /* This opcode was only used for the Macintosh version of Journey.
80 It controls menus with numbers greater than 2 (menus 0, 1 and 2
90 * Ask the user a question; return true if the answer is yes.
94 bool read_yes_or_no (const char *s)
99 print_string ("? (y/n) >");
101 key = stream_read_key (0, 0, FALSE);
103 if (key == 'y' || key == 'Y') {
104 print_string ("y\n");
107 print_string ("n\n");
111 }/* read_yes_or_no */
116 * Read a string from the current input stream.
120 void read_string (int max, zchar *buffer)
128 key = stream_read_input (max, buffer, 0, 0, FALSE, FALSE);
130 } while (key != ZC_RETURN);
137 * Ask the user to type in a number and return it.
141 int read_number (void)
147 read_string (5, buffer);
149 for (i = 0; buffer[i] != 0; i++)
150 if (buffer[i] >= '0' && buffer[i] <= '9')
151 value = 10 * value + buffer[i] - '0';
158 * z_read, read a line of input and (in V5+) store the terminating key.
160 * zargs[0] = address of text buffer
161 * zargs[1] = address of token buffer
162 * zargs[2] = timeout in tenths of a second (optional)
163 * zargs[3] = packed address of routine to be called on timeout
169 zchar buffer[INPUT_BUFFER_SIZE];
176 /* Supply default arguments */
181 /* Get maximum input size */
190 if (max >= INPUT_BUFFER_SIZE)
191 max = INPUT_BUFFER_SIZE - 1;
193 /* Get initial input size */
195 if (h_version >= V5) {
197 LOW_BYTE (addr, size)
200 /* Copy initial input to local buffer */
202 for (i = 0; i < size; i++) {
205 buffer[i] = translate_from_zscii (c);
210 /* Draw status line for V1 to V3 games */
215 /* Read input from current input stream */
217 key = stream_read_input (
218 max, buffer, /* buffer and size */
219 zargs[2], /* timeout value */
220 zargs[3], /* timeout routine */
221 FALSE, /* enable hot keys */
222 h_version == V6); /* no script in V6 */
227 /* Perform save_undo for V1 to V4 games */
232 /* Copy local buffer back to dynamic memory */
234 for (i = 0; buffer[i] != 0; i++) {
236 if (key == ZC_RETURN) {
238 buffer[i] = unicode_tolower (buffer[i]);
242 storeb ((zword) (zargs[0] + ((h_version <= V4) ? 1 : 2) + i), translate_to_zscii (buffer[i]));
246 /* Add null character (V1-V4) or write input length into 2nd byte */
249 storeb ((zword) (zargs[0] + 1 + i), 0);
251 storeb ((zword) (zargs[0] + 1), i);
253 /* Tokenise line if a token buffer is present */
255 if (key == ZC_RETURN && zargs[1] != 0)
256 tokenise_line (zargs[0], zargs[1], 0, FALSE);
261 store (translate_to_zscii (key));
266 * z_read_char, read and store a key.
268 * zargs[0] = input device (must be 1)
269 * zargs[1] = timeout in tenths of a second (optional)
270 * zargs[2] = packed address of routine to be called on timeout
274 void z_read_char (void)
278 /* Supply default arguments */
283 /* Read input from the current input stream */
285 key = stream_read_key (
286 zargs[1], /* timeout value */
287 zargs[2], /* timeout routine */
288 FALSE); /* enable hot keys */
295 store (translate_to_zscii (key));
300 * z_read_mouse, write the current mouse status into a table.
302 * zargs[0] = address of table
306 void z_read_mouse (void)
310 /* Read the mouse position, the last menu click
311 and which buttons are down */
313 btn = os_read_mouse ();
314 hx_mouse_y = mouse_y;
315 hx_mouse_x = mouse_x;
317 storew ((zword) (zargs[0] + 0), hx_mouse_y);
318 storew ((zword) (zargs[0] + 2), hx_mouse_x);
319 storew ((zword) (zargs[0] + 4), btn); /* mouse button bits */
320 storew ((zword) (zargs[0] + 6), menu_selected); /* menu selection */