diff options
author | Torne Wuff <torne@wolfpuppy.org.uk> | 2010-01-17 22:15:13 +0000 |
---|---|---|
committer | Torne Wuff <torne@wolfpuppy.org.uk> | 2010-01-17 22:15:13 +0000 |
commit | 7f28c94eda576e3f972fc05468188986f2e45885 (patch) | |
tree | e03b94613028d16855a5d3df0f4853e077931214 /apps/plugins/frotz/frotz.c | |
parent | 563f2602f471208cb8544a36539a79dcceaad643 (diff) | |
download | rockbox-7f28c94eda576e3f972fc05468188986f2e45885.tar.gz rockbox-7f28c94eda576e3f972fc05468188986f2e45885.zip |
New plugin: frotz, a Z-machine interpreter, for playing interactive fiction.
The interpreter more or less passes all the tests in the z-machine test suite.
It should build for every target except Archos (for which it is disabled).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24267 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/frotz/frotz.c')
-rw-r--r-- | apps/plugins/frotz/frotz.c | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/apps/plugins/frotz/frotz.c b/apps/plugins/frotz/frotz.c new file mode 100644 index 0000000000..96029b85cb --- /dev/null +++ b/apps/plugins/frotz/frotz.c | |||
@@ -0,0 +1,314 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2009 Torne Wuff | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include "plugin.h" | ||
22 | #include "dumb_frotz.h" | ||
23 | #include "lib/pluginlib_exit.h" | ||
24 | #include "lib/pluginlib_actions.h" | ||
25 | |||
26 | PLUGIN_HEADER | ||
27 | |||
28 | extern int frotz_main(void); | ||
29 | extern bool hot_key_quit(void); | ||
30 | |||
31 | f_setup_t f_setup; | ||
32 | extern char save_name[], auxilary_name[], script_name[], command_name[]; | ||
33 | extern int story_fp, sfp, rfp, pfp; | ||
34 | |||
35 | static struct viewport vp[NB_SCREENS]; | ||
36 | |||
37 | static void atexit_cleanup(void); | ||
38 | |||
39 | enum plugin_status plugin_start(const void* parameter) | ||
40 | { | ||
41 | int i; | ||
42 | char* ext; | ||
43 | |||
44 | PLUGINLIB_EXIT_INIT_ATEXIT(atexit_cleanup); | ||
45 | |||
46 | if (!parameter) | ||
47 | return PLUGIN_ERROR; | ||
48 | |||
49 | rb->lcd_setfont(FONT_SYSFIXED); | ||
50 | #if LCD_DEPTH > 1 | ||
51 | rb->lcd_set_backdrop(NULL); | ||
52 | #endif | ||
53 | rb->lcd_clear_display(); | ||
54 | |||
55 | FOR_NB_SCREENS(i) | ||
56 | rb->viewport_set_defaults(&vp[i], i); | ||
57 | |||
58 | story_name = parameter; | ||
59 | strcpy(save_name, story_name); | ||
60 | ext = rb->strrchr(save_name, '.'); | ||
61 | if (ext) | ||
62 | *ext = '\0'; | ||
63 | strcpy(auxilary_name, save_name); | ||
64 | strcpy(script_name, save_name); | ||
65 | strcpy(command_name, save_name); | ||
66 | rb->strlcat(save_name, ".sav", MAX_PATH); | ||
67 | rb->strlcat(auxilary_name, ".aux", MAX_PATH); | ||
68 | rb->strlcat(script_name, ".scr", MAX_PATH); | ||
69 | rb->strlcat(command_name, ".rec", MAX_PATH); | ||
70 | |||
71 | frotz_main(); | ||
72 | |||
73 | return PLUGIN_OK; | ||
74 | } | ||
75 | |||
76 | void atexit_cleanup() | ||
77 | { | ||
78 | if (story_fp != -1) | ||
79 | fclose(story_fp); | ||
80 | if (sfp != -1) | ||
81 | fclose(sfp); | ||
82 | if (rfp != -1) | ||
83 | fclose(rfp); | ||
84 | if (pfp != -1) | ||
85 | fclose(pfp); | ||
86 | } | ||
87 | |||
88 | MENUITEM_STRINGLIST(ingame_menu, "Frotz", NULL, "Resume", "Undo", "Restart", | ||
89 | "Toggle input recording", "Play back input", | ||
90 | "Debug options", "Exit"); | ||
91 | |||
92 | zchar menu(void) | ||
93 | { | ||
94 | switch (rb->do_menu(&ingame_menu, NULL, vp, true)) | ||
95 | { | ||
96 | case 0: | ||
97 | default: | ||
98 | dumb_dump_screen(); | ||
99 | return ZC_BAD; | ||
100 | case 1: | ||
101 | return ZC_HKEY_UNDO; | ||
102 | case 2: | ||
103 | return ZC_HKEY_RESTART; | ||
104 | case 3: | ||
105 | return ZC_HKEY_RECORD; | ||
106 | case 4: | ||
107 | return ZC_HKEY_PLAYBACK; | ||
108 | case 5: | ||
109 | return ZC_HKEY_DEBUG; | ||
110 | case 6: | ||
111 | return ZC_HKEY_QUIT; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | const struct button_mapping* plugin_contexts[]={generic_actions}; | ||
116 | |||
117 | void wait_for_key() | ||
118 | { | ||
119 | int action; | ||
120 | |||
121 | dumb_show_screen(false); | ||
122 | |||
123 | for (;;) | ||
124 | { | ||
125 | action = pluginlib_getaction(TIMEOUT_BLOCK, | ||
126 | plugin_contexts, 1); | ||
127 | switch (action) | ||
128 | { | ||
129 | case PLA_QUIT: | ||
130 | hot_key_quit(); | ||
131 | break; | ||
132 | |||
133 | case PLA_FIRE: | ||
134 | return; | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | |||
139 | zchar do_input(int timeout, bool show_cursor) | ||
140 | { | ||
141 | int action; | ||
142 | long timeout_at; | ||
143 | zchar menu_ret; | ||
144 | |||
145 | dumb_show_screen(show_cursor); | ||
146 | |||
147 | /* Convert timeout (tenths of a second) to ticks */ | ||
148 | if (timeout > 0) | ||
149 | timeout = (timeout * HZ) / 10; | ||
150 | else | ||
151 | timeout = TIMEOUT_BLOCK; | ||
152 | |||
153 | timeout_at = *rb->current_tick + timeout; | ||
154 | |||
155 | for (;;) | ||
156 | { | ||
157 | action = pluginlib_getaction(timeout, | ||
158 | plugin_contexts, 1); | ||
159 | switch (action) | ||
160 | { | ||
161 | case PLA_QUIT: | ||
162 | return ZC_HKEY_QUIT; | ||
163 | |||
164 | case PLA_MENU: | ||
165 | menu_ret = menu(); | ||
166 | if (menu_ret != ZC_BAD) | ||
167 | return menu_ret; | ||
168 | timeout_at = *rb->current_tick + timeout; | ||
169 | break; | ||
170 | |||
171 | case PLA_FIRE: | ||
172 | return ZC_RETURN; | ||
173 | |||
174 | case PLA_START: | ||
175 | return ZC_BAD; | ||
176 | |||
177 | default: | ||
178 | if (timeout != TIMEOUT_BLOCK && | ||
179 | !TIME_BEFORE(*rb->current_tick, timeout_at)) | ||
180 | return ZC_TIME_OUT; | ||
181 | } | ||
182 | } | ||
183 | } | ||
184 | |||
185 | zchar os_read_key(int timeout, bool show_cursor) | ||
186 | { | ||
187 | int r; | ||
188 | char inputbuf[5]; | ||
189 | short key; | ||
190 | zchar zkey; | ||
191 | |||
192 | for(;;) | ||
193 | { | ||
194 | zkey = do_input(timeout, show_cursor); | ||
195 | if (zkey != ZC_BAD) | ||
196 | return zkey; | ||
197 | |||
198 | inputbuf[0] = '\0'; | ||
199 | r = rb->kbd_input(inputbuf, 5); | ||
200 | rb->lcd_setfont(FONT_SYSFIXED); | ||
201 | dumb_dump_screen(); | ||
202 | if (!r) | ||
203 | { | ||
204 | rb->utf8decode(inputbuf, &key); | ||
205 | if (key > 0 && key < 256) | ||
206 | return (zchar)key; | ||
207 | } | ||
208 | } | ||
209 | } | ||
210 | |||
211 | zchar os_read_line(int max, zchar *buf, int timeout, int width, int continued) | ||
212 | { | ||
213 | (void)continued; | ||
214 | int r; | ||
215 | char inputbuf[256]; | ||
216 | const char *in; | ||
217 | char *out; | ||
218 | short key; | ||
219 | zchar zkey; | ||
220 | |||
221 | for(;;) | ||
222 | { | ||
223 | zkey = do_input(timeout, true); | ||
224 | if (zkey != ZC_BAD) | ||
225 | return zkey; | ||
226 | |||
227 | if (max > width) | ||
228 | max = width; | ||
229 | strcpy(inputbuf, buf); | ||
230 | r = rb->kbd_input(inputbuf, 256); | ||
231 | rb->lcd_setfont(FONT_SYSFIXED); | ||
232 | dumb_dump_screen(); | ||
233 | if (!r) | ||
234 | { | ||
235 | in = inputbuf; | ||
236 | out = buf; | ||
237 | while (*in && max) | ||
238 | { | ||
239 | in = rb->utf8decode(in, &key); | ||
240 | if (key > 0 && key < 256) | ||
241 | { | ||
242 | *out++ = key; | ||
243 | max--; | ||
244 | } | ||
245 | } | ||
246 | *out = '\0'; | ||
247 | os_display_string(buf); | ||
248 | return ZC_RETURN; | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | |||
253 | bool read_yes_or_no(const char *s) | ||
254 | { | ||
255 | char message_line[50]; | ||
256 | const char *message_lines[] = {message_line}; | ||
257 | const struct text_message message = {message_lines, 1}; | ||
258 | |||
259 | rb->strlcpy(message_line, s, 49); | ||
260 | rb->strcat(message_line, "?"); | ||
261 | |||
262 | if (rb->gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES) | ||
263 | return TRUE; | ||
264 | else | ||
265 | return FALSE; | ||
266 | } | ||
267 | |||
268 | zchar os_read_mouse(void) | ||
269 | { | ||
270 | return 0; | ||
271 | } | ||
272 | |||
273 | int os_read_file_name(char *file_name, const char *default_name, int flag) | ||
274 | { | ||
275 | (void)flag; | ||
276 | strcpy(file_name, default_name); | ||
277 | return TRUE; | ||
278 | } | ||
279 | |||
280 | void os_beep(int volume) | ||
281 | { | ||
282 | rb->splashf(HZ/2, "[%s-PITCHED BEEP]", (volume==1) ? "HIGH" : "LOW"); | ||
283 | } | ||
284 | |||
285 | static unsigned char unget_buf; | ||
286 | static int unget_file; | ||
287 | |||
288 | int ungetc(int c, int f) | ||
289 | { | ||
290 | unget_file = f; | ||
291 | unget_buf = c; | ||
292 | return c; | ||
293 | } | ||
294 | |||
295 | int fgetc(int f) | ||
296 | { | ||
297 | unsigned char cb; | ||
298 | if (unget_file == f) | ||
299 | { | ||
300 | unget_file = -1; | ||
301 | return unget_buf; | ||
302 | } | ||
303 | if (rb->read(f, &cb, 1) != 1) | ||
304 | return EOF; | ||
305 | return cb; | ||
306 | } | ||
307 | |||
308 | int fputc(int c, int f) | ||
309 | { | ||
310 | unsigned char cb = c; | ||
311 | if (rb->write(f, &cb, 1) != 1) | ||
312 | return EOF; | ||
313 | return cb; | ||
314 | } | ||