Rebase to SVN, and do a bit of cleanup as well as minor fixes.
[kugel-rb.git] / apps / plugins / frotz / input.c
blob296bffc529c199725a6e5905d1482f06c1792202
1 /* input.c - High level input functions
2 * Copyright (c) 1995-1997 Stefan Jokisch
4 * Changes for Rockbox copyright 2009 Torne Wuff
6 * This file is part of Frotz.
8 * Frotz is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Frotz is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
23 #include "frotz.h"
25 extern int save_undo (void);
27 extern zchar stream_read_key (zword, zword, bool);
28 extern zchar stream_read_input (int, zchar *, zword, zword, bool, bool);
30 extern void tokenise_line (zword, zword, zword, bool);
33 * is_terminator
35 * Check if the given key is an input terminator.
39 bool is_terminator (zchar key)
42 if (key == ZC_TIME_OUT)
43 return TRUE;
44 if (key == ZC_RETURN)
45 return TRUE;
46 if (key >= ZC_HKEY_MIN && key <= ZC_HKEY_MAX)
47 return TRUE;
49 if (h_terminating_keys != 0)
51 if (key >= ZC_ARROW_MIN && key <= ZC_MENU_CLICK) {
53 zword addr = h_terminating_keys;
54 zbyte c;
56 do {
57 LOW_BYTE (addr, c)
58 if (c == 255 || key == translate_from_zscii (c))
59 return TRUE;
60 addr++;
61 } while (c != 0);
65 return FALSE;
67 }/* is_terminator */
70 * z_make_menu, add or remove a menu and branch if successful.
72 * zargs[0] = number of menu
73 * zargs[1] = table of menu entries or 0 to remove menu
77 void z_make_menu (void)
80 /* This opcode was only used for the Macintosh version of Journey.
81 It controls menus with numbers greater than 2 (menus 0, 1 and 2
82 are system menus). Frotz doesn't implement menus yet. */
84 branch (FALSE);
86 }/* z_make_menu */
88 extern bool read_yes_or_no (const char *s);
91 * read_string
93 * Read a string from the current input stream.
97 void read_string (int max, zchar *buffer)
99 zchar key;
101 buffer[0] = 0;
103 do {
105 key = stream_read_input (max, buffer, 0, 0, FALSE, FALSE);
107 } while (key != ZC_RETURN);
109 }/* read_string */
112 * read_number
114 * Ask the user to type in a number and return it.
118 int read_number (void)
120 zchar buffer[6];
121 int value = 0;
122 int i;
124 read_string (5, buffer);
126 for (i = 0; buffer[i] != 0; i++)
127 if (buffer[i] >= '0' && buffer[i] <= '9')
128 value = 10 * value + buffer[i] - '0';
130 return value;
132 }/* read_number */
135 * z_read, read a line of input and (in V5+) store the terminating key.
137 * zargs[0] = address of text buffer
138 * zargs[1] = address of token buffer
139 * zargs[2] = timeout in tenths of a second (optional)
140 * zargs[3] = packed address of routine to be called on timeout
144 void z_read (void)
146 zchar buffer[INPUT_BUFFER_SIZE];
147 zword addr;
148 zchar key;
149 zbyte max, size;
150 zbyte c;
151 int i;
153 /* Supply default arguments */
155 if (zargc < 3)
156 zargs[2] = 0;
158 /* Get maximum input size */
160 addr = zargs[0];
162 LOW_BYTE (addr, max)
164 if (h_version <= V4)
165 max--;
167 if (max >= INPUT_BUFFER_SIZE)
168 max = INPUT_BUFFER_SIZE - 1;
170 /* Get initial input size */
172 if (h_version >= V5) {
173 addr++;
174 LOW_BYTE (addr, size)
175 } else size = 0;
177 /* Copy initial input to local buffer */
179 for (i = 0; i < size; i++) {
180 addr++;
181 LOW_BYTE (addr, c)
182 buffer[i] = translate_from_zscii (c);
185 buffer[i] = 0;
187 /* Draw status line for V1 to V3 games */
189 if (h_version <= V3)
190 z_show_status ();
192 /* Read input from current input stream */
194 key = stream_read_input (
195 max, buffer, /* buffer and size */
196 zargs[2], /* timeout value */
197 zargs[3], /* timeout routine */
198 TRUE, /* enable hot keys */
199 h_version == V6); /* no script in V6 */
201 if (key == ZC_BAD)
202 return;
204 /* Perform save_undo for V1 to V4 games */
206 if (h_version <= V4)
207 save_undo ();
209 /* Copy local buffer back to dynamic memory */
211 for (i = 0; buffer[i] != 0; i++) {
213 if (key == ZC_RETURN) {
215 if (buffer[i] >= 'A' && buffer[i] <= 'Z')
216 buffer[i] += 'a' - 'A';
217 if (buffer[i] >= 0xc0 && buffer[i] <= 0xde && buffer[i] != 0xd7)
218 buffer[i] += 0x20;
222 storeb ((zword) (zargs[0] + ((h_version <= V4) ? 1 : 2) + i), translate_to_zscii (buffer[i]));
226 /* Add null character (V1-V4) or write input length into 2nd byte */
228 if (h_version <= V4)
229 storeb ((zword) (zargs[0] + 1 + i), 0);
230 else
231 storeb ((zword) (zargs[0] + 1), i);
233 /* Tokenise line if a token buffer is present */
235 if (key == ZC_RETURN && zargs[1] != 0)
236 tokenise_line (zargs[0], zargs[1], 0, FALSE);
238 /* Store key */
240 if (h_version >= V5)
241 store (translate_to_zscii (key));
243 }/* z_read */
246 * z_read_char, read and store a key.
248 * zargs[0] = input device (must be 1)
249 * zargs[1] = timeout in tenths of a second (optional)
250 * zargs[2] = packed address of routine to be called on timeout
254 void z_read_char (void)
256 zchar key;
258 /* Supply default arguments */
260 if (zargc < 2)
261 zargs[1] = 0;
263 /* Read input from the current input stream */
265 key = stream_read_key (
266 zargs[1], /* timeout value */
267 zargs[2], /* timeout routine */
268 TRUE); /* enable hot keys */
270 if (key == ZC_BAD)
271 return;
273 /* Store key */
275 store (translate_to_zscii (key));
277 }/* z_read_char */
280 * z_read_mouse, write the current mouse status into a table.
282 * zargs[0] = address of table
286 void z_read_mouse (void)
288 zword btn;
290 /* Read the mouse position and which buttons are down */
292 btn = os_read_mouse ();
293 hx_mouse_y = mouse_y;
294 hx_mouse_x = mouse_x;
296 storew ((zword) (zargs[0] + 0), hx_mouse_y);
297 storew ((zword) (zargs[0] + 2), hx_mouse_x);
298 storew ((zword) (zargs[0] + 4), btn); /* mouse button bits */
299 storew ((zword) (zargs[0] + 6), 0); /* menu selection */
301 }/* z_read_mouse */