Introduce __attribute__((unused)) (#defined to UNUSED_ATTR) to mark possibly unused...
[kugel-rb.git] / apps / plugins / rockboy / rockboy.c
blob156af74d64c2cb74a1cf569a9d5a8b3acca8ddb7
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_HOME;
162 options.START=BUTTON_SCROLL_BACK;
163 options.SELECT=BUTTON_SCROLL_FWD;
164 options.MENU=BUTTON_POWER;
165 options.LEFT=BUTTON_LEFT;
166 options.RIGHT=BUTTON_RIGHT;
168 #elif CONFIG_KEYPAD == SANSA_C200_PAD
169 options.UP=BUTTON_UP;
170 options.DOWN=BUTTON_DOWN;
172 options.A=BUTTON_SELECT;
173 options.B=BUTTON_REC;
174 options.START=BUTTON_VOL_DOWN;
175 options.SELECT=BUTTON_VOL_UP;
176 options.MENU=BUTTON_POWER;
178 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
179 options.UP=BUTTON_UP;
180 options.DOWN=BUTTON_DOWN;
182 options.A=BUTTON_PLAY;
183 options.B=BUTTON_REC;
184 options.START=BUTTON_SELECT;
185 options.SELECT=BUTTON_NONE;
186 options.MENU=BUTTON_POWER;
188 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
189 options.UP=BUTTON_SCROLL_UP;
190 options.DOWN=BUTTON_SCROLL_DOWN;
192 options.A=BUTTON_PLAY;
193 options.B=BUTTON_FF;
194 options.START=BUTTON_REW;
195 options.SELECT=BUTTON_NONE;
196 options.MENU=BUTTON_POWER;
198 #elif CONFIG_KEYPAD == MROBE500_PAD
199 options.MENU=BUTTON_POWER;
201 #elif CONFIG_KEYPAD == COWOND2_PAD
202 options.A=BUTTON_PLUS;
203 options.B=BUTTON_MINUS;
204 options.MENU=BUTTON_MENU;
206 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
207 options.UP=BUTTON_UP;
208 options.DOWN=BUTTON_DOWN;
210 options.A=BUTTON_VOL_UP;
211 options.B=BUTTON_VOL_DOWN;
212 options.START=BUTTON_PLAY;
213 options.SELECT=BUTTON_SELECT;
214 options.MENU=BUTTON_MENU;
215 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
216 options.UP=BUTTON_UP;
217 options.DOWN=BUTTON_DOWN;
219 options.A=BUTTON_CUSTOM;
220 options.B=BUTTON_PLAY;
221 options.START=BUTTON_BACK;
222 options.SELECT=BUTTON_SELECT;
223 options.MENU=BUTTON_MENU;
225 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
226 options.UP=BUTTON_UP;
227 options.DOWN=BUTTON_DOWN;
229 options.A=BUTTON_VOL_UP;
230 options.B=BUTTON_VOL_DOWN;
231 options.START=BUTTON_VIEW;
232 options.SELECT=BUTTON_SELECT;
233 options.MENU=BUTTON_MENU;
235 #elif CONFIG_KEYPAD == ONDAVX747_PAD
236 options.A=BUTTON_VOL_UP;
237 options.B=BUTTON_VOL_DOWN;
238 options.MENU=BUTTON_MENU;
240 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
241 options.UP=BUTTON_UP;
242 options.DOWN=BUTTON_DOWN;
244 options.A=BUTTON_PLAY;
245 options.B=BUTTON_FFWD;
246 options.START=BUTTON_REW;
247 options.SELECT=BUTTON_RIGHT;
248 options.MENU=BUTTON_LEFT;
250 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
251 options.UP=BUTTON_UP;
252 options.DOWN=BUTTON_DOWN;
254 options.A=BUTTON_PLAY;
255 options.B=BUTTON_REW;
256 options.START=BUTTON_REC;
257 options.SELECT=BUTTON_FFWD;
258 options.MENU=BUTTON_NONE;
260 #else
261 #error No Keymap Defined!
262 #endif
264 #ifdef HAVE_TOUCHSCREEN
265 options.UP=BUTTON_TOPMIDDLE;
266 options.DOWN=BUTTON_BOTTOMMIDDLE;
267 options.START=BUTTON_TOPRIGHT;
268 options.SELECT=BUTTON_CENTER;
269 #if CONFIG_KEYPAD == MROBE500_PAD
270 options.A=BUTTON_BOTTOMLEFT;
271 options.B=BUTTON_BOTTOMRIGHT;
272 #elif CONFIG_KEYPAD != COWOND2_PAD
273 options.A=BUTTON_BOTTOMLEFT;
274 options.B=BUTTON_BOTTOMRIGHT;
275 options.MENU=BUTTON_TOPLEFT;
276 #endif
277 #endif
279 options.maxskip=4;
280 options.fps=0;
281 options.showstats=0;
282 #if (LCD_WIDTH>=160) && (LCD_HEIGHT>=144)
283 options.scaling=0;
284 #else
285 options.scaling=1;
286 #endif
287 options.sound=1;
288 options.pal=0;
290 else
291 read(fd,&options, sizeof(options));
293 close(fd);
296 static void savesettings(void)
298 int fd;
299 char optionsave[sizeof(savedir)+sizeof(optionname)];
301 if(options.dirty)
303 options.dirty=0;
304 snprintf(optionsave, sizeof(optionsave), "%s/%s", savedir, optionname);
305 fd = open(optionsave, O_WRONLY|O_CREAT|O_TRUNC);
306 write(fd,&options, sizeof(options));
307 close(fd);
311 void doevents(void)
313 event_t ev;
314 int st;
316 ev_poll();
317 while (ev_getevent(&ev))
319 if (ev.type != EV_PRESS && ev.type != EV_RELEASE)
320 continue;
321 st = (ev.type != EV_RELEASE);
322 pad_set(ev.code, st);
326 static int gnuboy_main(const char *rom)
328 rb->lcd_puts(0,0,"Init video");
329 vid_init();
330 rb->lcd_puts(0,1,"Init sound");
331 pcm_init();
332 rb->lcd_puts(0,2,"Loading rom");
333 loader_init(rom);
334 if(shut)
335 return PLUGIN_ERROR;
336 rb->lcd_puts(0,3,"Emu reset");
337 emu_reset();
338 rb->lcd_puts(0,4,"Emu run");
339 rb->lcd_clear_display();
340 rb->lcd_update();
341 emu_run();
343 /* never reached */
344 return PLUGIN_OK;
347 /* this is the plugin entry point */
348 enum plugin_status plugin_start(UNUSED_ATTR const void* parameter)
350 PLUGIN_IRAM_INIT(rb)
352 rb->lcd_setfont(0);
354 rb->lcd_clear_display();
356 if (!parameter)
358 rb->splash(HZ*3, "Play gameboy ROM file! (.gb/.gbc)");
359 return PLUGIN_OK;
361 if(rb->audio_status())
363 audio_bufferbase = audio_bufferpointer
364 = rb->plugin_get_buffer(&audio_buffer_free);
365 plugbuf=true;
367 else
369 audio_bufferbase = audio_bufferpointer
370 = rb->plugin_get_audio_buffer(&audio_buffer_free);
371 plugbuf=false;
373 #if MEM <= 8 && !defined(SIMULATOR)
374 /* loaded as an overlay plugin, protect from overwriting ourselves */
375 if ((unsigned)(plugin_start_addr - (unsigned char *)audio_bufferbase)
376 < audio_buffer_free)
377 audio_buffer_free = plugin_start_addr - (unsigned char *)audio_bufferbase;
378 #endif
379 setoptions();
381 shut=0;
382 cleanshut=0;
384 #ifdef HAVE_WHEEL_POSITION
385 rb->wheel_send_events(false);
386 #endif
388 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
389 rb->lcd_set_mode(LCD_MODE_PAL256);
390 #endif
392 gnuboy_main(parameter);
394 #ifdef HAVE_WHEEL_POSITION
395 rb->wheel_send_events(true);
396 #endif
398 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
399 rb->lcd_set_mode(LCD_MODE_RGB565);
400 #endif
402 if(shut&&!cleanshut)
404 rb->splash(HZ/2, errormsg);
405 return PLUGIN_ERROR;
407 if(!rb->audio_status())
408 pcm_close();
410 rb->splash(HZ/2, "Closing Rockboy");
412 savesettings();
414 cleanup();
416 return PLUGIN_OK;