Prepare new maemo release
[maemo-rb.git] / apps / plugins / frotz / frotz.c
blobb4ec8854d28b505098111921587fee72a3f8dbf2
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"
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 char* ext;
43 atexit(atexit_cleanup);
45 if (!parameter)
46 return PLUGIN_ERROR;
48 rb->lcd_setfont(FONT_SYSFIXED);
49 #if LCD_DEPTH > 1
50 rb->lcd_set_backdrop(NULL);
51 #endif
52 rb->lcd_clear_display();
54 FOR_NB_SCREENS(i)
55 rb->viewport_set_defaults(&vp[i], i);
57 story_name = parameter;
58 strcpy(save_name, story_name);
59 ext = rb->strrchr(save_name, '.');
60 if (ext)
61 *ext = '\0';
62 strcpy(auxilary_name, save_name);
63 strcpy(script_name, save_name);
64 strcpy(command_name, save_name);
65 rb->strlcat(save_name, ".sav", MAX_PATH);
66 rb->strlcat(auxilary_name, ".aux", MAX_PATH);
67 rb->strlcat(script_name, ".scr", MAX_PATH);
68 rb->strlcat(command_name, ".rec", MAX_PATH);
70 frotz_main();
72 return PLUGIN_OK;
75 void atexit_cleanup()
77 if (story_fp != -1)
78 fclose(story_fp);
79 if (sfp != -1)
80 fclose(sfp);
81 if (rfp != -1)
82 fclose(rfp);
83 if (pfp != -1)
84 fclose(pfp);
87 MENUITEM_STRINGLIST(ingame_menu, "Frotz", NULL, "Resume", "Undo", "Restart",
88 "Toggle input recording", "Play back input",
89 "Debug options", "Exit");
91 zchar menu(void)
93 switch (rb->do_menu(&ingame_menu, NULL, vp, true))
95 case 0:
96 default:
97 dumb_dump_screen();
98 return ZC_BAD;
99 case 1:
100 return ZC_HKEY_UNDO;
101 case 2:
102 return ZC_HKEY_RESTART;
103 case 3:
104 return ZC_HKEY_RECORD;
105 case 4:
106 return ZC_HKEY_PLAYBACK;
107 case 5:
108 return ZC_HKEY_DEBUG;
109 case 6:
110 return ZC_HKEY_QUIT;
114 const struct button_mapping* plugin_contexts[]={pla_main_ctx};
116 void wait_for_key()
118 int action;
120 dumb_show_screen(false);
122 for (;;)
124 action = pluginlib_getaction(TIMEOUT_BLOCK, plugin_contexts,
125 ARRAYLEN(plugin_contexts));
126 switch (action)
128 case PLA_EXIT:
129 hot_key_quit();
130 break;
132 case PLA_SELECT:
133 return;
138 zchar do_input(int timeout, bool show_cursor)
140 int action;
141 long timeout_at;
142 zchar menu_ret;
144 dumb_show_screen(show_cursor);
146 /* Convert timeout (tenths of a second) to ticks */
147 if (timeout > 0)
148 timeout = (timeout * HZ) / 10;
149 else
150 timeout = TIMEOUT_BLOCK;
152 timeout_at = *rb->current_tick + timeout;
154 for (;;)
156 action = pluginlib_getaction(timeout, plugin_contexts,
157 ARRAYLEN(plugin_contexts));
158 switch (action)
160 case PLA_EXIT:
161 return ZC_HKEY_QUIT;
163 case PLA_CANCEL:
164 menu_ret = menu();
165 if (menu_ret != ZC_BAD)
166 return menu_ret;
167 timeout_at = *rb->current_tick + timeout;
168 break;
170 case PLA_SELECT:
171 return ZC_RETURN;
173 case PLA_DOWN:
174 return ZC_BAD;
176 default:
177 if (timeout != TIMEOUT_BLOCK &&
178 !TIME_BEFORE(*rb->current_tick, timeout_at))
179 return ZC_TIME_OUT;
184 zchar os_read_key(int timeout, bool show_cursor)
186 int r;
187 char inputbuf[5];
188 short key;
189 zchar zkey;
191 for(;;)
193 zkey = do_input(timeout, show_cursor);
194 if (zkey != ZC_BAD)
195 return zkey;
197 inputbuf[0] = '\0';
198 r = rb->kbd_input(inputbuf, 5);
199 rb->lcd_setfont(FONT_SYSFIXED);
200 dumb_dump_screen();
201 if (!r)
203 rb->utf8decode(inputbuf, &key);
204 if (key > 0 && key < 256)
205 return (zchar)key;
210 zchar os_read_line(int max, zchar *buf, int timeout, int width, int continued)
212 (void)continued;
213 int r;
214 char inputbuf[256];
215 const char *in;
216 char *out;
217 short key;
218 zchar zkey;
220 for(;;)
222 zkey = do_input(timeout, true);
223 if (zkey != ZC_BAD)
224 return zkey;
226 if (max > width)
227 max = width;
228 strcpy(inputbuf, buf);
229 r = rb->kbd_input(inputbuf, 256);
230 rb->lcd_setfont(FONT_SYSFIXED);
231 dumb_dump_screen();
232 if (!r)
234 in = inputbuf;
235 out = buf;
236 while (*in && max)
238 in = rb->utf8decode(in, &key);
239 if (key > 0 && key < 256)
241 *out++ = key;
242 max--;
245 *out = '\0';
246 os_display_string(buf);
247 return ZC_RETURN;
252 bool read_yes_or_no(const char *s)
254 char message_line[50];
255 const char *message_lines[] = {message_line};
256 const struct text_message message = {message_lines, 1};
258 rb->strlcpy(message_line, s, 49);
259 rb->strcat(message_line, "?");
261 if (rb->gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
262 return TRUE;
263 else
264 return FALSE;
267 zchar os_read_mouse(void)
269 return 0;
272 int os_read_file_name(char *file_name, const char *default_name, int flag)
274 (void)flag;
275 strcpy(file_name, default_name);
276 return TRUE;
279 void os_beep(int volume)
281 rb->splashf(HZ/2, "[%s-PITCHED BEEP]", (volume==1) ? "HIGH" : "LOW");
284 static unsigned char unget_buf;
285 static int unget_file;
287 int frotz_ungetc(int c, int f)
289 unget_file = f;
290 unget_buf = c;
291 return c;
294 int frotz_fgetc(int f)
296 unsigned char cb;
297 if (unget_file == f)
299 unget_file = -1;
300 return unget_buf;
302 if (rb->read(f, &cb, 1) != 1)
303 return EOF;
304 return cb;
307 int frotz_fputc(int c, int f)
309 unsigned char cb = c;
310 if (rb->write(f, &cb, 1) != 1)
311 return EOF;
312 return cb;