Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / plugins / frotz / frotz.c
blobf64431f14eb8c7084664ee31dffcb0401a469947
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Torne Wuff
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "dumb_frotz.h"
23 #include "lib/pluginlib_exit.h"
24 #include "lib/pluginlib_actions.h"
26 PLUGIN_HEADER
28 extern int frotz_main(void);
29 extern bool hot_key_quit(void);
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;
35 static struct viewport vp[NB_SCREENS];
37 static void atexit_cleanup(void);
39 enum plugin_status plugin_start(const void* parameter)
41 int i;
42 char* ext;
44 PLUGINLIB_EXIT_INIT_ATEXIT(atexit_cleanup);
46 if (!parameter)
47 return PLUGIN_ERROR;
49 rb->lcd_setfont(FONT_SYSFIXED);
50 #if LCD_DEPTH > 1
51 rb->lcd_set_backdrop(NULL);
52 #endif
53 rb->lcd_clear_display();
55 FOR_NB_SCREENS(i)
56 rb->viewport_set_defaults(&vp[i], i);
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);
71 frotz_main();
73 return PLUGIN_OK;
76 void atexit_cleanup()
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);
88 MENUITEM_STRINGLIST(ingame_menu, "Frotz", NULL, "Resume", "Undo", "Restart",
89 "Toggle input recording", "Play back input",
90 "Debug options", "Exit");
92 zchar menu(void)
94 switch (rb->do_menu(&ingame_menu, NULL, vp, true))
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;
115 const struct button_mapping* plugin_contexts[]={generic_actions};
117 void wait_for_key()
119 int action;
121 dumb_show_screen(false);
123 for (;;)
125 action = pluginlib_getaction(TIMEOUT_BLOCK,
126 plugin_contexts, 1);
127 switch (action)
129 case PLA_QUIT:
130 hot_key_quit();
131 break;
133 case PLA_FIRE:
134 return;
139 zchar do_input(int timeout, bool show_cursor)
141 int action;
142 long timeout_at;
143 zchar menu_ret;
145 dumb_show_screen(show_cursor);
147 /* Convert timeout (tenths of a second) to ticks */
148 if (timeout > 0)
149 timeout = (timeout * HZ) / 10;
150 else
151 timeout = TIMEOUT_BLOCK;
153 timeout_at = *rb->current_tick + timeout;
155 for (;;)
157 action = pluginlib_getaction(timeout,
158 plugin_contexts, 1);
159 switch (action)
161 case PLA_QUIT:
162 return ZC_HKEY_QUIT;
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;
171 case PLA_FIRE:
172 return ZC_RETURN;
174 case PLA_START:
175 return ZC_BAD;
177 default:
178 if (timeout != TIMEOUT_BLOCK &&
179 !TIME_BEFORE(*rb->current_tick, timeout_at))
180 return ZC_TIME_OUT;
185 zchar os_read_key(int timeout, bool show_cursor)
187 int r;
188 char inputbuf[5];
189 short key;
190 zchar zkey;
192 for(;;)
194 zkey = do_input(timeout, show_cursor);
195 if (zkey != ZC_BAD)
196 return zkey;
198 inputbuf[0] = '\0';
199 r = rb->kbd_input(inputbuf, 5);
200 rb->lcd_setfont(FONT_SYSFIXED);
201 dumb_dump_screen();
202 if (!r)
204 rb->utf8decode(inputbuf, &key);
205 if (key > 0 && key < 256)
206 return (zchar)key;
211 zchar os_read_line(int max, zchar *buf, int timeout, int width, int continued)
213 (void)continued;
214 int r;
215 char inputbuf[256];
216 const char *in;
217 char *out;
218 short key;
219 zchar zkey;
221 for(;;)
223 zkey = do_input(timeout, true);
224 if (zkey != ZC_BAD)
225 return zkey;
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)
235 in = inputbuf;
236 out = buf;
237 while (*in && max)
239 in = rb->utf8decode(in, &key);
240 if (key > 0 && key < 256)
242 *out++ = key;
243 max--;
246 *out = '\0';
247 os_display_string(buf);
248 return ZC_RETURN;
253 bool read_yes_or_no(const char *s)
255 char message_line[50];
256 const char *message_lines[] = {message_line};
257 const struct text_message message = {message_lines, 1};
259 rb->strlcpy(message_line, s, 49);
260 rb->strcat(message_line, "?");
262 if (rb->gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
263 return TRUE;
264 else
265 return FALSE;
268 zchar os_read_mouse(void)
270 return 0;
273 int os_read_file_name(char *file_name, const char *default_name, int flag)
275 (void)flag;
276 strcpy(file_name, default_name);
277 return TRUE;
280 void os_beep(int volume)
282 rb->splashf(HZ/2, "[%s-PITCHED BEEP]", (volume==1) ? "HIGH" : "LOW");
285 static unsigned char unget_buf;
286 static int unget_file;
288 int frotz_ungetc(int c, int f)
290 unget_file = f;
291 unget_buf = c;
292 return c;
295 int frotz_fgetc(int f)
297 unsigned char cb;
298 if (unget_file == f)
300 unget_file = -1;
301 return unget_buf;
303 if (rb->read(f, &cb, 1) != 1)
304 return EOF;
305 return cb;
308 int frotz_fputc(int c, int f)
310 unsigned char cb = c;
311 if (rb->write(f, &cb, 1) != 1)
312 return EOF;
313 return cb;