FS#10075 - FUZE: QUIT Plugin by selecting BUTTON_HOME by Johannes Schwarz. Updates...
[kugel-rb/myfork.git] / apps / plugins / rockboy / rockboy.c
blob38f4fe1aa11f67339c2d0f3bda9182ab07c43c49
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Gameboy emulator based on gnuboy
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 "loader.h"
23 #include "rockmacros.h"
24 #include "input.h"
25 #include "emu.h"
26 #include "hw.h"
27 #include "pcm.h"
29 PLUGIN_HEADER
30 PLUGIN_IRAM_DECLARE
32 int shut,cleanshut;
33 char *errormsg;
35 #define optionname "options"
37 void die(char *message, ...)
39 shut=1;
40 errormsg=message;
43 struct options options;
45 void *audio_bufferbase;
46 void *audio_bufferpointer;
47 size_t audio_buffer_free;
49 void *my_malloc(size_t size)
51 void *alloc;
53 if (size + 4 > audio_buffer_free)
54 return 0;
55 alloc = audio_bufferpointer;
56 audio_bufferpointer += size + 4;
57 audio_buffer_free -= size + 4;
58 return alloc;
61 static void setoptions (void)
63 int fd;
64 DIR* dir;
65 char optionsave[sizeof(savedir)+sizeof(optionname)];
67 dir=rb->opendir(savedir);
68 if(!dir)
69 rb->mkdir(savedir);
70 else
71 rb->closedir(dir);
73 snprintf(optionsave, sizeof(optionsave), "%s/%s", savedir, optionname);
75 fd = open(optionsave, O_RDONLY);
76 if(fd < 0) /* no options to read, set defaults */
78 #ifdef HAVE_TOUCHSCREEN
79 options.LEFT=BUTTON_MIDLEFT;
80 options.RIGHT=BUTTON_MIDRIGHT;
81 #else
82 options.LEFT=BUTTON_LEFT;
83 options.RIGHT=BUTTON_RIGHT;
84 #endif
86 #if CONFIG_KEYPAD == IRIVER_H100_PAD
87 options.UP=BUTTON_UP;
88 options.DOWN=BUTTON_DOWN;
90 options.A=BUTTON_ON;
91 options.B=BUTTON_OFF;
92 options.START=BUTTON_REC;
93 options.SELECT=BUTTON_SELECT;
94 options.MENU=BUTTON_MODE;
96 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
97 options.UP=BUTTON_UP;
98 options.DOWN=BUTTON_DOWN;
100 options.A=BUTTON_REC;
101 options.B=BUTTON_MODE;
102 options.START=BUTTON_ON;
103 options.SELECT=BUTTON_SELECT;
104 options.MENU=BUTTON_OFF;
106 #elif CONFIG_KEYPAD == RECORDER_PAD
107 options.UP=BUTTON_UP;
108 options.DOWN=BUTTON_DOWN;
110 options.A=BUTTON_F1;
111 options.B=BUTTON_F2;
112 options.START=BUTTON_F3;
113 options.SELECT=BUTTON_PLAY;
114 options.MENU=BUTTON_OFF;
116 #elif CONFIG_KEYPAD == IPOD_4G_PAD
117 options.UP=BUTTON_MENU;
118 options.DOWN=BUTTON_PLAY;
120 options.A=BUTTON_NONE;
121 options.B=BUTTON_NONE;
122 options.START=BUTTON_SELECT;
123 options.SELECT=BUTTON_NONE;
124 options.MENU=(BUTTON_SELECT | BUTTON_REPEAT);
126 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
127 options.UP=BUTTON_UP;
128 options.DOWN=BUTTON_DOWN;
130 options.A=BUTTON_PLAY;
131 options.B=BUTTON_EQ;
132 options.START=BUTTON_MODE;
133 options.SELECT=(BUTTON_SELECT | BUTTON_REL);
134 options.MENU=(BUTTON_SELECT | BUTTON_REPEAT);
136 #elif CONFIG_KEYPAD == GIGABEAT_PAD
137 options.UP=BUTTON_UP;
138 options.DOWN=BUTTON_DOWN;
140 options.A=BUTTON_VOL_UP;
141 options.B=BUTTON_VOL_DOWN;
142 options.START=BUTTON_A;
143 options.SELECT=BUTTON_SELECT;
144 options.MENU=BUTTON_MENU;
146 #elif CONFIG_KEYPAD == SANSA_E200_PAD
147 options.UP=BUTTON_UP;
148 options.DOWN=BUTTON_DOWN;
150 options.A=BUTTON_SELECT;
151 options.B=BUTTON_REC;
152 options.START=BUTTON_SCROLL_BACK;
153 options.SELECT=BUTTON_SCROLL_FWD;
154 options.MENU=BUTTON_POWER;
156 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
157 options.UP=BUTTON_UP;
158 options.DOWN=BUTTON_DOWN;
160 options.A=BUTTON_SELECT;
161 options.B=BUTTON_LEFT;
162 options.START=BUTTON_SCROLL_BACK;
163 options.SELECT=BUTTON_SCROLL_FWD;
164 options.MENU=BUTTON_HOME;
166 #elif CONFIG_KEYPAD == SANSA_C200_PAD
167 options.UP=BUTTON_UP;
168 options.DOWN=BUTTON_DOWN;
170 options.A=BUTTON_SELECT;
171 options.B=BUTTON_REC;
172 options.START=BUTTON_VOL_DOWN;
173 options.SELECT=BUTTON_VOL_UP;
174 options.MENU=BUTTON_POWER;
176 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
177 options.UP=BUTTON_UP;
178 options.DOWN=BUTTON_DOWN;
180 options.A=BUTTON_PLAY;
181 options.B=BUTTON_REC;
182 options.START=BUTTON_SELECT;
183 options.SELECT=BUTTON_NONE;
184 options.MENU=BUTTON_POWER;
186 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
187 options.UP=BUTTON_SCROLL_UP;
188 options.DOWN=BUTTON_SCROLL_DOWN;
190 options.A=BUTTON_PLAY;
191 options.B=BUTTON_FF;
192 options.START=BUTTON_REW;
193 options.SELECT=BUTTON_NONE;
194 options.MENU=BUTTON_POWER;
196 #elif CONFIG_KEYPAD == MROBE500_PAD
197 options.UP=BUTTON_RC_PLAY;
198 options.DOWN=BUTTON_RC_DOWN;
199 options.LEFT=BUTTON_RC_REW;
200 options.RIGHT=BUTTON_RC_FF;
202 options.A=BUTTON_RC_VOL_DOWN;
203 options.B=BUTTON_RC_VOL_UP;
204 options.START=BUTTON_RC_HEART;
205 options.SELECT=BUTTON_RC_MODE;
206 options.MENU=BUTTON_POWER;
208 #elif CONFIG_KEYPAD == COWOND2_PAD
209 options.A=BUTTON_PLUS;
210 options.B=BUTTON_MINUS;
211 options.MENU=BUTTON_MENU;
213 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
214 options.UP=BUTTON_UP;
215 options.DOWN=BUTTON_DOWN;
217 options.A=BUTTON_VOL_UP;
218 options.B=BUTTON_VOL_DOWN;
219 options.START=BUTTON_PLAY;
220 options.SELECT=BUTTON_SELECT;
221 options.MENU=BUTTON_MENU;
222 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
223 options.UP=BUTTON_UP;
224 options.DOWN=BUTTON_DOWN;
226 options.A=BUTTON_CUSTOM;
227 options.B=BUTTON_PLAY;
228 options.START=BUTTON_BACK;
229 options.SELECT=BUTTON_SELECT;
230 options.MENU=BUTTON_MENU;
232 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
233 options.UP=BUTTON_UP;
234 options.DOWN=BUTTON_DOWN;
236 options.A=BUTTON_VOL_UP;
237 options.B=BUTTON_VOL_DOWN;
238 options.START=BUTTON_VIEW;
239 options.SELECT=BUTTON_SELECT;
240 options.MENU=BUTTON_MENU;
242 #elif CONFIG_KEYPAD == ONDAVX747_PAD
243 options.A=BUTTON_VOL_UP;
244 options.B=BUTTON_VOL_DOWN;
245 options.MENU=BUTTON_MENU;
246 #else
247 #error No Keymap Defined!
248 #endif
250 #ifdef HAVE_TOUCHSCREEN
251 options.UP=BUTTON_TOPMIDDLE;
252 options.DOWN=BUTTON_BOTTOMMIDDLE;
253 options.START=BUTTON_TOPRIGHT;
254 options.SELECT=BUTTON_CENTER;
255 #if CONFIG_KEYPAD != COWOND2_PAD
256 options.A=BUTTON_BOTTOMLEFT;
257 options.B=BUTTON_BOTTOMRIGHT;
258 options.MENU=BUTTON_TOPLEFT;
259 #endif
260 #endif
262 options.maxskip=4;
263 options.fps=0;
264 options.showstats=0;
265 #if (LCD_WIDTH>=160) && (LCD_HEIGHT>=144)
266 options.scaling=0;
267 #else
268 options.scaling=1;
269 #endif
270 options.sound=1;
271 options.pal=0;
273 else
274 read(fd,&options, sizeof(options));
276 close(fd);
279 static void savesettings(void)
281 int fd;
282 char optionsave[sizeof(savedir)+sizeof(optionname)];
284 if(options.dirty)
286 options.dirty=0;
287 snprintf(optionsave, sizeof(optionsave), "%s/%s", savedir, optionname);
288 fd = open(optionsave, O_WRONLY|O_CREAT|O_TRUNC);
289 write(fd,&options, sizeof(options));
290 close(fd);
294 void doevents(void)
296 event_t ev;
297 int st;
299 ev_poll();
300 while (ev_getevent(&ev))
302 if (ev.type != EV_PRESS && ev.type != EV_RELEASE)
303 continue;
304 st = (ev.type != EV_RELEASE);
305 pad_set(ev.code, st);
309 static int gnuboy_main(const char *rom)
311 rb->lcd_puts(0,0,"Init video");
312 vid_init();
313 rb->lcd_puts(0,1,"Init sound");
314 pcm_init();
315 rb->lcd_puts(0,2,"Loading rom");
316 loader_init(rom);
317 if(shut)
318 return PLUGIN_ERROR;
319 rb->lcd_puts(0,3,"Emu reset");
320 emu_reset();
321 rb->lcd_puts(0,4,"Emu run");
322 rb->lcd_clear_display();
323 rb->lcd_update();
324 emu_run();
326 /* never reached */
327 return PLUGIN_OK;
330 /* this is the plugin entry point */
331 enum plugin_status plugin_start(const void* parameter)
333 PLUGIN_IRAM_INIT(rb)
335 rb->lcd_setfont(0);
337 rb->lcd_clear_display();
339 if (!parameter)
341 rb->splash(HZ*3, "Play gameboy ROM file! (.gb/.gbc)");
342 return PLUGIN_OK;
344 if(rb->audio_status())
346 audio_bufferbase = audio_bufferpointer
347 = rb->plugin_get_buffer(&audio_buffer_free);
348 plugbuf=true;
350 else
352 audio_bufferbase = audio_bufferpointer
353 = rb->plugin_get_audio_buffer(&audio_buffer_free);
354 plugbuf=false;
356 #if MEM <= 8 && !defined(SIMULATOR)
357 /* loaded as an overlay plugin, protect from overwriting ourselves */
358 if ((unsigned)(plugin_start_addr - (unsigned char *)audio_bufferbase)
359 < audio_buffer_free)
360 audio_buffer_free = plugin_start_addr - (unsigned char *)audio_bufferbase;
361 #endif
362 setoptions();
364 shut=0;
365 cleanshut=0;
367 #ifdef HAVE_WHEEL_POSITION
368 rb->wheel_send_events(false);
369 #endif
371 gnuboy_main(parameter);
373 #ifdef HAVE_WHEEL_POSITION
374 rb->wheel_send_events(true);
375 #endif
377 if(shut&&!cleanshut)
379 rb->splash(HZ/2, errormsg);
380 return PLUGIN_ERROR;
382 if(!rb->audio_status())
383 pcm_close();
384 rb->splash(HZ/2, "Closing Rockboy");
386 savesettings();
388 cleanup();
390 return PLUGIN_OK;