Doom plugin: In anticipation of moving to the EABI toolchain, because of its assumpti...
[kugel-rb.git] / apps / plugins / flipit.c
blobd4c5702bc0e6aa77ac36a2a56225e229fc47d52a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Vicentini Martin
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"
23 PLUGIN_HEADER
25 /* variable button definitions */
26 #if CONFIG_KEYPAD == RECORDER_PAD
27 #define FLIPIT_LEFT BUTTON_LEFT
28 #define FLIPIT_RIGHT BUTTON_RIGHT
29 #define FLIPIT_UP BUTTON_UP
30 #define FLIPIT_DOWN BUTTON_DOWN
31 #define FLIPIT_QUIT BUTTON_OFF
32 #define FLIPIT_SHUFFLE BUTTON_F1
33 #define FLIPIT_SOLVE BUTTON_F2
34 #define FLIPIT_STEP_BY_STEP BUTTON_F3
35 #define FLIPIT_TOGGLE BUTTON_PLAY
37 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
38 #define FLIPIT_LEFT BUTTON_LEFT
39 #define FLIPIT_RIGHT BUTTON_RIGHT
40 #define FLIPIT_UP BUTTON_UP
41 #define FLIPIT_DOWN BUTTON_DOWN
42 #define FLIPIT_QUIT BUTTON_OFF
43 #define FLIPIT_SHUFFLE BUTTON_F1
44 #define FLIPIT_SOLVE BUTTON_F2
45 #define FLIPIT_STEP_BY_STEP BUTTON_F3
46 #define FLIPIT_TOGGLE BUTTON_SELECT
48 #elif CONFIG_KEYPAD == PLAYER_PAD
49 #define FLIPIT_LEFT BUTTON_LEFT
50 #define FLIPIT_RIGHT BUTTON_RIGHT
51 #define FLIPIT_UP_PRE BUTTON_ON
52 #define FLIPIT_UP (BUTTON_ON | BUTTON_REL)
53 #define FLIPIT_DOWN BUTTON_MENU
54 #define FLIPIT_QUIT BUTTON_STOP
55 #define FLIPIT_SHUFFLE (BUTTON_ON | BUTTON_LEFT)
56 #define FLIPIT_SOLVE (BUTTON_ON | BUTTON_RIGHT)
57 #define FLIPIT_STEP_BY_STEP (BUTTON_ON | BUTTON_PLAY)
58 #define FLIPIT_TOGGLE BUTTON_PLAY
60 #elif CONFIG_KEYPAD == ONDIO_PAD
61 #define FLIPIT_LEFT BUTTON_LEFT
62 #define FLIPIT_RIGHT BUTTON_RIGHT
63 #define FLIPIT_UP BUTTON_UP
64 #define FLIPIT_DOWN BUTTON_DOWN
65 #define FLIPIT_QUIT BUTTON_OFF
66 #define FLIPIT_SHUFFLE (BUTTON_MENU | BUTTON_LEFT)
67 #define FLIPIT_SOLVE (BUTTON_MENU | BUTTON_UP)
68 #define FLIPIT_STEP_BY_STEP (BUTTON_MENU | BUTTON_RIGHT)
69 #define FLIPIT_TOGGLE_PRE BUTTON_MENU
70 #define FLIPIT_TOGGLE (BUTTON_MENU | BUTTON_REL)
72 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
73 (CONFIG_KEYPAD == IRIVER_H300_PAD)
74 #define FLIPIT_LEFT BUTTON_LEFT
75 #define FLIPIT_RIGHT BUTTON_RIGHT
76 #define FLIPIT_UP BUTTON_UP
77 #define FLIPIT_DOWN BUTTON_DOWN
78 #define FLIPIT_QUIT BUTTON_OFF
79 #define FLIPIT_SHUFFLE BUTTON_MODE
80 #define FLIPIT_SOLVE BUTTON_ON
81 #define FLIPIT_STEP_BY_STEP BUTTON_REC
82 #define FLIPIT_TOGGLE_PRE BUTTON_SELECT
83 #define FLIPIT_TOGGLE (BUTTON_SELECT | BUTTON_REL)
85 #define FLIPIT_RC_QUIT BUTTON_RC_STOP
87 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
88 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
89 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
91 #define FLIPIT_SCROLLWHEEL
92 #define FLIPIT_LEFT BUTTON_LEFT
93 #define FLIPIT_RIGHT BUTTON_RIGHT
94 #define FLIPIT_UP BUTTON_MENU
95 #define FLIPIT_DOWN BUTTON_PLAY
96 #define FLIPIT_NEXT BUTTON_SCROLL_FWD
97 #define FLIPIT_PREV BUTTON_SCROLL_BACK
98 #define FLIPIT_QUIT (BUTTON_SELECT | BUTTON_MENU)
99 #define FLIPIT_SHUFFLE (BUTTON_SELECT | BUTTON_LEFT)
100 #define FLIPIT_SOLVE (BUTTON_SELECT | BUTTON_PLAY)
101 #define FLIPIT_STEP_BY_STEP (BUTTON_SELECT | BUTTON_RIGHT)
102 #define FLIPIT_TOGGLE_PRE BUTTON_SELECT
103 #define FLIPIT_TOGGLE (BUTTON_SELECT | BUTTON_REL)
105 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
107 #define FLIPIT_LEFT BUTTON_LEFT
108 #define FLIPIT_RIGHT BUTTON_RIGHT
109 #define FLIPIT_UP BUTTON_UP
110 #define FLIPIT_DOWN BUTTON_DOWN
111 #define FLIPIT_QUIT BUTTON_POWER
112 #define FLIPIT_SHUFFLE BUTTON_REC
113 #define FLIPIT_SOLVE_PRE BUTTON_PLAY
114 #define FLIPIT_SOLVE (BUTTON_PLAY | BUTTON_REPEAT)
115 #define FLIPIT_STEP_PRE BUTTON_PLAY
116 #define FLIPIT_STEP_BY_STEP (BUTTON_PLAY | BUTTON_REL)
117 #define FLIPIT_TOGGLE BUTTON_SELECT
119 #elif CONFIG_KEYPAD == GIGABEAT_PAD
121 #define FLIPIT_LEFT BUTTON_LEFT
122 #define FLIPIT_RIGHT BUTTON_RIGHT
123 #define FLIPIT_UP BUTTON_UP
124 #define FLIPIT_DOWN BUTTON_DOWN
125 #define FLIPIT_QUIT BUTTON_POWER
126 #define FLIPIT_SHUFFLE BUTTON_MENU
127 #define FLIPIT_SOLVE BUTTON_VOL_UP
128 #define FLIPIT_STEP_BY_STEP BUTTON_VOL_DOWN
129 #define FLIPIT_TOGGLE BUTTON_SELECT
131 #elif CONFIG_KEYPAD == SANSA_E200_PAD
133 #define FLIPIT_SCROLLWHEEL
134 #define FLIPIT_LEFT BUTTON_LEFT
135 #define FLIPIT_RIGHT BUTTON_RIGHT
136 #define FLIPIT_UP BUTTON_UP
137 #define FLIPIT_DOWN BUTTON_DOWN
138 #define FLIPIT_NEXT BUTTON_SCROLL_FWD
139 #define FLIPIT_PREV BUTTON_SCROLL_BACK
140 #define FLIPIT_QUIT BUTTON_POWER
141 #define FLIPIT_SHUFFLE (BUTTON_REC | BUTTON_LEFT)
142 #define FLIPIT_SOLVE (BUTTON_REC | BUTTON_RIGHT)
143 #define FLIPIT_STEP_BY_STEP (BUTTON_REC | BUTTON_SELECT)
144 #define FLIPIT_TOGGLE BUTTON_SELECT
146 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
148 #define FLIPIT_SCROLLWHEEL
149 #define FLIPIT_LEFT BUTTON_LEFT
150 #define FLIPIT_RIGHT BUTTON_RIGHT
151 #define FLIPIT_UP BUTTON_UP
152 #define FLIPIT_DOWN BUTTON_DOWN
153 #define FLIPIT_NEXT BUTTON_SCROLL_FWD
154 #define FLIPIT_PREV BUTTON_SCROLL_BACK
155 #define FLIPIT_QUIT (BUTTON_HOME|BUTTON_REPEAT)
156 #define FLIPIT_SHUFFLE (BUTTON_SELECT | BUTTON_LEFT)
157 #define FLIPIT_SOLVE (BUTTON_SELECT | BUTTON_DOWN)
158 #define FLIPIT_STEP_BY_STEP (BUTTON_SELECT | BUTTON_RIGHT)
159 #define FLIPIT_TOGGLE_PRE BUTTON_SELECT
160 #define FLIPIT_TOGGLE (BUTTON_SELECT | BUTTON_REL)
162 #elif CONFIG_KEYPAD == SANSA_C200_PAD
164 #define FLIPIT_LEFT BUTTON_LEFT
165 #define FLIPIT_RIGHT BUTTON_RIGHT
166 #define FLIPIT_UP BUTTON_UP
167 #define FLIPIT_DOWN BUTTON_DOWN
168 #define FLIPIT_QUIT BUTTON_POWER
169 #define FLIPIT_SHUFFLE (BUTTON_REC | BUTTON_LEFT)
170 #define FLIPIT_SOLVE (BUTTON_REC | BUTTON_RIGHT)
171 #define FLIPIT_STEP_BY_STEP (BUTTON_REC | BUTTON_SELECT)
172 #define FLIPIT_TOGGLE BUTTON_SELECT
174 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
176 #define FLIPIT_LEFT BUTTON_LEFT
177 #define FLIPIT_RIGHT BUTTON_RIGHT
178 #define FLIPIT_UP BUTTON_UP
179 #define FLIPIT_DOWN BUTTON_DOWN
180 #define FLIPIT_QUIT BUTTON_POWER
181 #define FLIPIT_SHUFFLE (BUTTON_HOME | BUTTON_LEFT)
182 #define FLIPIT_SOLVE (BUTTON_HOME | BUTTON_RIGHT)
183 #define FLIPIT_STEP_BY_STEP (BUTTON_HOME | BUTTON_SELECT)
184 #define FLIPIT_TOGGLE BUTTON_SELECT
186 #elif CONFIG_KEYPAD == SANSA_M200_PAD
188 #define FLIPIT_LEFT BUTTON_LEFT
189 #define FLIPIT_RIGHT BUTTON_RIGHT
190 #define FLIPIT_UP BUTTON_UP
191 #define FLIPIT_DOWN BUTTON_DOWN
192 #define FLIPIT_QUIT BUTTON_POWER
193 #define FLIPIT_SHUFFLE (BUTTON_SELECT | BUTTON_LEFT)
194 #define FLIPIT_SOLVE (BUTTON_SELECT | BUTTON_RIGHT)
195 #define FLIPIT_STEP_BY_STEP (BUTTON_SELECT | BUTTON_UP)
196 #define FLIPIT_TOGGLE (BUTTON_SELECT | BUTTON_REL)
198 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
200 #define FLIPIT_LEFT BUTTON_LEFT
201 #define FLIPIT_RIGHT BUTTON_RIGHT
202 #define FLIPIT_UP BUTTON_SCROLL_UP
203 #define FLIPIT_DOWN BUTTON_SCROLL_DOWN
204 #define FLIPIT_QUIT BUTTON_POWER
205 #define FLIPIT_SHUFFLE (BUTTON_PLAY | BUTTON_LEFT)
206 #define FLIPIT_SOLVE (BUTTON_PLAY | BUTTON_RIGHT)
207 #define FLIPIT_STEP_BY_STEP (BUTTON_PLAY | BUTTON_SCROLL_UP)
208 #define FLIPIT_TOGGLE_PRE BUTTON_REW
209 #define FLIPIT_TOGGLE (BUTTON_REW | BUTTON_REL)
211 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
213 #define FLIPIT_LEFT BUTTON_LEFT
214 #define FLIPIT_RIGHT BUTTON_RIGHT
215 #define FLIPIT_UP BUTTON_UP
216 #define FLIPIT_DOWN BUTTON_DOWN
217 #define FLIPIT_QUIT BUTTON_BACK
218 #define FLIPIT_SHUFFLE BUTTON_MENU
219 #define FLIPIT_SOLVE BUTTON_VOL_UP
220 #define FLIPIT_STEP_BY_STEP BUTTON_VOL_DOWN
221 #define FLIPIT_TOGGLE BUTTON_SELECT
223 #elif CONFIG_KEYPAD == MROBE100_PAD
225 #define FLIPIT_LEFT BUTTON_LEFT
226 #define FLIPIT_RIGHT BUTTON_RIGHT
227 #define FLIPIT_UP BUTTON_UP
228 #define FLIPIT_DOWN BUTTON_DOWN
229 #define FLIPIT_QUIT BUTTON_POWER
230 #define FLIPIT_SHUFFLE BUTTON_MENU
231 #define FLIPIT_SOLVE BUTTON_PLAY
232 #define FLIPIT_STEP_BY_STEP BUTTON_DISPLAY
233 #define FLIPIT_TOGGLE BUTTON_SELECT
235 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
237 #define FLIPIT_LEFT BUTTON_RC_REW
238 #define FLIPIT_RIGHT BUTTON_RC_FF
239 #define FLIPIT_UP BUTTON_RC_VOL_UP
240 #define FLIPIT_DOWN BUTTON_RC_VOL_DOWN
241 #define FLIPIT_QUIT BUTTON_RC_REC
242 #define FLIPIT_SHUFFLE BUTTON_RC_MODE
243 #define FLIPIT_SOLVE_PRE BUTTON_RC_MENU
244 #define FLIPIT_SOLVE (BUTTON_RC_MENU|BUTTON_REPEAT)
245 #define FLIPIT_STEP_PRE BUTTON_RC_MENU
246 #define FLIPIT_STEP_BY_STEP (BUTTON_RC_MENU|BUTTON_REL)
247 #define FLIPIT_TOGGLE BUTTON_RC_PLAY
249 #define FLIPIT_RC_QUIT BUTTON_REC
251 #elif CONFIG_KEYPAD == COWON_D2_PAD
253 #define FLIPIT_QUIT BUTTON_POWER
255 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
257 #define FLIPIT_LEFT BUTTON_LEFT
258 #define FLIPIT_RIGHT BUTTON_RIGHT
259 #define FLIPIT_UP BUTTON_UP
260 #define FLIPIT_DOWN BUTTON_DOWN
261 #define FLIPIT_QUIT BUTTON_BACK
262 #define FLIPIT_SHUFFLE BUTTON_CUSTOM
263 #define FLIPIT_SOLVE BUTTON_PLAY
264 #define FLIPIT_STEP_BY_STEP BUTTON_MENU
265 #define FLIPIT_TOGGLE BUTTON_SELECT
267 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
269 #define FLIPIT_LEFT BUTTON_LEFT
270 #define FLIPIT_RIGHT BUTTON_RIGHT
271 #define FLIPIT_UP BUTTON_UP
272 #define FLIPIT_DOWN BUTTON_DOWN
273 #define FLIPIT_QUIT BUTTON_POWER
274 #define FLIPIT_SHUFFLE BUTTON_MENU
275 #define FLIPIT_SOLVE BUTTON_VIEW
276 #define FLIPIT_STEP_BY_STEP BUTTON_PLAYLIST
277 #define FLIPIT_TOGGLE BUTTON_SELECT
279 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
281 #define FLIPIT_LEFT BUTTON_PREV
282 #define FLIPIT_RIGHT BUTTON_NEXT
283 #define FLIPIT_UP BUTTON_UP
284 #define FLIPIT_DOWN BUTTON_DOWN
285 #define FLIPIT_QUIT BUTTON_POWER
286 #define FLIPIT_SHUFFLE BUTTON_MENU
287 #define FLIPIT_SOLVE BUTTON_RIGHT
288 #define FLIPIT_STEP_BY_STEP BUTTON_LEFT
289 #define FLIPIT_TOGGLE BUTTON_PLAY
291 #elif CONFIG_KEYPAD == ONDAVX747_PAD || CONFIG_KEYPAD == MROBE500_PAD
293 #define FLIPIT_QUIT BUTTON_POWER
295 #elif CONFIG_KEYPAD == ONDAVX777_PAD
297 #define FLIPIT_QUIT BUTTON_POWER
299 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
301 #define FLIPIT_LEFT BUTTON_LEFT
302 #define FLIPIT_RIGHT BUTTON_RIGHT
303 #define FLIPIT_UP BUTTON_UP
304 #define FLIPIT_DOWN BUTTON_DOWN
305 #define FLIPIT_QUIT BUTTON_REC
306 #define FLIPIT_SHUFFLE BUTTON_PLAY
307 #define FLIPIT_SOLVE BUTTON_FFWD
308 #define FLIPIT_STEP_BY_STEP (BUTTON_PLAY|BUTTON_UP)
309 #define FLIPIT_TOGGLE (BUTTON_PLAY|BUTTON_DOWN)
311 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
313 #define FLIPIT_LEFT BUTTON_PREV
314 #define FLIPIT_RIGHT BUTTON_NEXT
315 #define FLIPIT_UP BUTTON_UP
316 #define FLIPIT_DOWN BUTTON_DOWN
317 #define FLIPIT_QUIT BUTTON_REC
318 #define FLIPIT_SHUFFLE BUTTON_PLAY
319 #define FLIPIT_SOLVE BUTTON_MENU
320 #define FLIPIT_STEP_BY_STEP BUTTON_CANCEL
321 #define FLIPIT_TOGGLE BUTTON_OK
323 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
325 #define FLIPIT_LEFT BUTTON_VOL_DOWN
326 #define FLIPIT_RIGHT BUTTON_VOL_UP
327 #define FLIPIT_UP BUTTON_PREV
328 #define FLIPIT_DOWN BUTTON_NEXT
329 #define FLIPIT_QUIT (BUTTON_REC|BUTTON_PLAY)
330 #define FLIPIT_SHUFFLE BUTTON_SELECT
331 #define FLIPIT_SOLVE BUTTON_REC
332 #define FLIPIT_STEP_BY_STEP (BUTTON_PLAY|BUTTON_PREV)
333 #define FLIPIT_TOGGLE (BUTTON_PLAY|BUTTON_NEXT)
335 #else
336 #error No keymap defined!
337 #endif
339 #ifdef HAVE_TOUCHSCREEN
340 #ifndef FLIPIT_LEFT
341 #define FLIPIT_LEFT BUTTON_MIDLEFT
342 #endif
343 #ifndef FLIPIT_RIGHT
344 #define FLIPIT_RIGHT BUTTON_MIDRIGHT
345 #endif
346 #ifndef FLIPIT_UP
347 #define FLIPIT_UP BUTTON_TOPMIDDLE
348 #endif
349 #ifndef FLIPIT_DOWN
350 #define FLIPIT_DOWN BUTTON_BOTTOMMIDDLE
351 #endif
352 #ifndef FLIPIT_QUIT
353 #define FLIPIT_QUIT BUTTON_TOPLEFT
354 #endif
355 #ifndef FLIPIT_SHUFFLE
356 #define FLIPIT_SHUFFLE BUTTON_TOPRIGHT
357 #endif
358 #ifndef FLIPIT_SOLVE
359 #define FLIPIT_SOLVE BUTTON_BOTTOMLEFT
360 #endif
361 #ifndef FLIPIT_STEP_BY_STEP
362 #define FLIPIT_STEP_BY_STEP BUTTON_BOTTOMRIGHT
363 #endif
364 #ifndef FLIPIT_TOGGLE
365 #define FLIPIT_TOGGLE BUTTON_CENTER
366 #endif
367 #endif
369 static int spots[20];
370 static int toggle[20];
371 static int cursor_pos, moves;
373 #ifdef HAVE_LCD_BITMAP
375 #include "pluginbitmaps/flipit_cursor.h"
376 #include "pluginbitmaps/flipit_tokens.h"
378 #define PANEL_HEIGHT 12
379 #define TK_WIDTH BMPWIDTH_flipit_cursor
380 #define TK_HEIGHT BMPHEIGHT_flipit_cursor
381 #define TK_SPACE MAX(0, MIN((LCD_WIDTH - 5*TK_WIDTH)/4, \
382 (LCD_HEIGHT - PANEL_HEIGHT - 4*TK_HEIGHT)/4))
383 #define GRID_WIDTH (5*TK_WIDTH + 4*TK_SPACE)
384 #define GRID_LEFT ((LCD_WIDTH - GRID_WIDTH)/2)
385 #define GRID_HEIGHT (4*TK_HEIGHT + 4*TK_SPACE) /* includes grid-panel space */
386 #define GRID_TOP MAX(0, ((LCD_HEIGHT - PANEL_HEIGHT - GRID_HEIGHT)/2))
388 /* draw a spot at the coordinates (x,y), range of p is 0-19 */
389 static void draw_spot(int p)
391 rb->lcd_bitmap_part( flipit_tokens, 0, spots[p] * TK_HEIGHT,
392 STRIDE(SCREEN_MAIN, BMPWIDTH_flipit_tokens,
393 BMPHEIGHT_flipit_tokens),
394 GRID_LEFT + (p%5) * (TK_WIDTH+TK_SPACE),
395 GRID_TOP + (p/5) * (TK_HEIGHT+TK_SPACE),
396 TK_WIDTH, TK_HEIGHT );
399 /* draw the cursor at the current cursor position */
400 static void draw_cursor(void)
402 #ifdef HAVE_LCD_COLOR
403 rb->lcd_bitmap_transparent( flipit_cursor,
404 GRID_LEFT + (cursor_pos%5) * (TK_WIDTH+TK_SPACE),
405 GRID_TOP + (cursor_pos/5) * (TK_HEIGHT+TK_SPACE),
406 TK_WIDTH, TK_HEIGHT );
407 #else
408 rb->lcd_set_drawmode(DRMODE_FG);
409 rb->lcd_mono_bitmap( flipit_cursor,
410 GRID_LEFT + (cursor_pos%5) * (TK_WIDTH+TK_SPACE),
411 GRID_TOP + (cursor_pos/5) * (TK_HEIGHT+TK_SPACE),
412 TK_WIDTH, TK_HEIGHT );
413 rb->lcd_set_drawmode(DRMODE_SOLID);
414 #endif
417 /* draw the info panel ... duh */
418 static void draw_info_panel(void)
420 char s[32];
422 rb->lcd_set_drawmode( DRMODE_SOLID|DRMODE_INVERSEVID );
423 rb->lcd_fillrect( GRID_LEFT, GRID_TOP + 4*(TK_HEIGHT+TK_SPACE),
424 GRID_WIDTH, PANEL_HEIGHT );
425 rb->lcd_set_drawmode( DRMODE_SOLID );
426 rb->lcd_drawrect( GRID_LEFT, GRID_TOP + 4*(TK_HEIGHT+TK_SPACE),
427 GRID_WIDTH, PANEL_HEIGHT );
429 rb->snprintf( s, sizeof(s), "Flips: %d", moves );
430 rb->lcd_putsxy( (LCD_WIDTH - rb->lcd_getstringsize(s, NULL, NULL)) / 2,
431 GRID_TOP + 4*(TK_HEIGHT+TK_SPACE) + 2, s );
434 #else /* HAVE_LCD_CHARCELLS */
436 static const unsigned char tk_pat[4][7] = {
437 { 0x0e, 0x11, 0x0e, 0x00, 0x0e, 0x11, 0x0e }, /* white - white */
438 { 0x0e, 0x11, 0x0e, 0x00, 0x0e, 0x1f, 0x0e }, /* white - black */
439 { 0x0e, 0x1f, 0x0e, 0x00, 0x0e, 0x11, 0x0e }, /* black - white */
440 { 0x0e, 0x1f, 0x0e, 0x00, 0x0e, 0x1f, 0x0e } /* black - black */
443 static unsigned char cur_pat[7];
444 static unsigned long gfx_chars[5];
446 static void release_gfx(void)
448 int i;
450 for (i = 0; i < 5; i++)
451 if (gfx_chars[i])
452 rb->lcd_unlock_pattern(gfx_chars[i]);
455 static bool init_gfx(void)
457 int i;
459 for (i = 0; i < 5; i++) {
460 if ((gfx_chars[i] = rb->lcd_get_locked_pattern()) == 0) {
461 release_gfx();
462 return false;
465 for (i = 0; i < 4; i++)
466 rb->lcd_define_pattern(gfx_chars[i], tk_pat[i]);
467 return true;
470 /* draw a spot at the coordinates (x,y), range of p is 0-19 */
471 static void draw_spot(int p)
473 if ((p/5) & 1)
474 p -= 5;
476 rb->lcd_putc (p%5, p/10, gfx_chars[2*spots[p]+spots[p+5]]);
479 /* draw the cursor at the current cursor position */
480 static void draw_cursor(void)
482 if ((cursor_pos/5) & 1) {
483 rb->memcpy( cur_pat, tk_pat[2*spots[cursor_pos-5]+spots[cursor_pos]], 7 );
484 cur_pat[4] ^= 0x15;
485 cur_pat[6] ^= 0x11;
487 else {
488 rb->memcpy( cur_pat, tk_pat[2*spots[cursor_pos]+spots[cursor_pos+5]], 7 );
489 cur_pat[0] ^= 0x15;
490 cur_pat[2] ^= 0x11;
492 rb->lcd_define_pattern(gfx_chars[4], cur_pat);
493 rb->lcd_putc( cursor_pos%5, cursor_pos/10, gfx_chars[4] );
496 /* draw the info panel ... duh */
497 static void draw_info_panel(void)
499 char s[16];
501 rb->lcd_puts( 6, 0, "Flips" );
502 rb->snprintf( s, sizeof(s), "%d", moves );
503 rb->lcd_puts( 6, 1, s );
506 #endif /* LCD */
508 /* clear the cursor where it is */
509 static inline void clear_cursor(void)
511 draw_spot( cursor_pos );
514 /* check if the puzzle is finished */
515 static bool flipit_finished(void)
517 int i;
518 for (i=0; i<20; i++)
519 if (!spots[i])
520 return false;
521 clear_cursor();
522 return true;
525 /* draws the toggled spots */
526 static void flipit_toggle(void)
528 spots[cursor_pos] = 1-spots[cursor_pos];
529 toggle[cursor_pos] = 1-toggle[cursor_pos];
530 draw_spot(cursor_pos);
531 if (cursor_pos%5 > 0) {
532 spots[cursor_pos-1] = 1-spots[cursor_pos-1];
533 draw_spot(cursor_pos-1);
535 if (cursor_pos%5 < 4) {
536 spots[cursor_pos+1] = 1-spots[cursor_pos+1];
537 draw_spot(cursor_pos+1);
539 if (cursor_pos/5 > 0) {
540 spots[cursor_pos-5] = 1-spots[cursor_pos-5];
541 draw_spot(cursor_pos-5);
543 if (cursor_pos/5 < 3) {
544 spots[cursor_pos+5] = 1-spots[cursor_pos+5];
545 draw_spot(cursor_pos+5);
547 moves++;
549 draw_info_panel();
551 if (flipit_finished())
552 clear_cursor();
555 /* move the cursor in any direction */
556 static void move_cursor(int x, int y)
558 if (!(flipit_finished())) {
559 clear_cursor();
560 cursor_pos = ( x + 5 + cursor_pos%5 )%5
561 + ( ( y + 4 + cursor_pos/5 )%4 )*5;
562 draw_cursor();
564 rb->lcd_update();
567 /* initialize the board */
568 static void flipit_init(void)
570 int i;
572 rb->lcd_clear_display();
573 for (i=0; i<20; i++) {
574 spots[i]=1;
575 toggle[i]=1;
576 draw_spot(i);
578 rb->lcd_update();
580 for (i=0; i<20; i++) {
581 cursor_pos = (rb->rand() % 20);
582 flipit_toggle();
585 cursor_pos = 0;
586 draw_cursor();
587 moves = 0;
588 draw_info_panel();
589 rb->lcd_update();
592 /* the main game loop */
593 static bool flipit_loop(void)
595 int i;
596 int button;
597 int lastbutton = BUTTON_NONE;
599 flipit_init();
600 while(true) {
601 button = rb->button_get(true);
602 switch (button) {
603 #ifdef FLIPIT_RC_QUIT
604 case FLIPIT_RC_QUIT:
605 #endif
606 case FLIPIT_QUIT:
607 /* get out of here */
608 return PLUGIN_OK;
610 case FLIPIT_SHUFFLE:
611 /* mix up the pieces */
612 flipit_init();
613 break;
615 case FLIPIT_SOLVE:
616 #ifdef FLIPIT_SOLVE_PRE
617 if (lastbutton != FLIPIT_SOLVE_PRE)
618 break;
619 #endif
620 /* solve the puzzle */
621 if (!flipit_finished()) {
622 for (i=0; i<20; i++)
623 if (!toggle[i]) {
624 clear_cursor();
625 cursor_pos = i;
626 flipit_toggle();
627 draw_cursor();
628 rb->lcd_update();
629 rb->sleep(HZ*2/3);
632 break;
634 case FLIPIT_STEP_BY_STEP:
635 #ifdef FLIPIT_STEP_PRE
636 if (lastbutton != FLIPIT_STEP_PRE)
637 break;
638 #endif
639 if (!flipit_finished()) {
640 for (i=0; i<20; i++)
641 if (!toggle[i]) {
642 clear_cursor();
643 cursor_pos = i;
644 flipit_toggle();
645 draw_cursor();
646 rb->lcd_update();
647 break;
650 break;
652 case FLIPIT_TOGGLE:
653 #ifdef FLIPIT_TOGGLE_PRE
654 if (lastbutton != FLIPIT_TOGGLE_PRE)
655 break;
656 #endif
657 /* toggle the pieces */
658 if (!flipit_finished()) {
659 flipit_toggle();
660 draw_cursor();
661 rb->lcd_update();
663 break;
665 case FLIPIT_LEFT:
666 move_cursor(-1, 0);
667 break;
669 case FLIPIT_RIGHT:
670 move_cursor(1, 0);
671 break;
672 /*move cursor though the entire field*/
673 #ifdef FLIPIT_SCROLLWHEEL
674 case FLIPIT_PREV:
675 case FLIPIT_PREV|BUTTON_REPEAT:
676 if ((cursor_pos)%5 == 0) {
677 move_cursor(-1, -1);
679 else {
680 move_cursor(-1, 0);
682 break;
684 case FLIPIT_NEXT:
685 case FLIPIT_NEXT|BUTTON_REPEAT:
686 if ((cursor_pos+1)%5 == 0) {
687 move_cursor(1, 1);
689 else {
690 move_cursor(1, 0);
692 break;
693 #endif
694 case FLIPIT_UP:
695 #ifdef FLIPIT_UP_PRE
696 if (lastbutton != FLIPIT_UP_PRE)
697 break;
698 #endif
699 move_cursor(0, -1);
700 break;
702 case FLIPIT_DOWN:
703 move_cursor(0, 1);
704 break;
706 default:
707 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
708 return PLUGIN_USB_CONNECTED;
709 break;
711 if (button != BUTTON_NONE)
712 lastbutton = button;
716 /* called function from outside */
717 enum plugin_status plugin_start(const void* parameter)
719 int i, rc;
721 (void)parameter;
723 #ifdef HAVE_LCD_COLOR
724 rb->lcd_set_background(LCD_WHITE);
725 rb->lcd_set_foreground(LCD_BLACK);
726 #endif
728 #if LCD_DEPTH > 1
729 rb->lcd_set_backdrop(NULL);
730 #endif
732 rb->splash(HZ, "FlipIt!");
734 #ifdef HAVE_LCD_BITMAP
735 /* print instructions */
736 rb->lcd_clear_display();
737 rb->lcd_setfont(FONT_SYSFIXED);
738 #if CONFIG_KEYPAD == RECORDER_PAD
739 rb->lcd_putsxy(2, 8, "[OFF] to stop");
740 rb->lcd_putsxy(2, 18, "[PLAY] toggle");
741 rb->lcd_putsxy(2, 28, "[F1] shuffle");
742 rb->lcd_putsxy(2, 38, "[F2] solution");
743 rb->lcd_putsxy(2, 48, "[F3] step by step");
744 #elif CONFIG_KEYPAD == ONDIO_PAD
745 rb->lcd_putsxy(2, 8, "[OFF] to stop");
746 rb->lcd_putsxy(2, 18, "[MODE] toggle");
747 rb->lcd_putsxy(2, 28, "[M-LEFT] shuffle");
748 rb->lcd_putsxy(2, 38, "[M-UP] solution");
749 rb->lcd_putsxy(2, 48, "[M-RIGHT] step by step");
750 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
751 (CONFIG_KEYPAD == IRIVER_H300_PAD)
752 rb->lcd_putsxy(2, 8, "[STOP] to stop");
753 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
754 rb->lcd_putsxy(2, 28, "[MODE] shuffle");
755 rb->lcd_putsxy(2, 38, "[PLAY] solution");
756 rb->lcd_putsxy(2, 48, "[REC] step by step");
757 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
758 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
759 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
760 rb->lcd_putsxy(2, 8, "[S-MENU] to stop");
761 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
762 rb->lcd_putsxy(2, 28, "[S-LEFT] shuffle");
763 rb->lcd_putsxy(2, 38, "[S-PLAY] solution");
764 rb->lcd_putsxy(2, 48, "[S-RIGHT] step by step");
765 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
766 rb->lcd_putsxy(2, 8, "[POWER] to stop");
767 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
768 rb->lcd_putsxy(2, 28, "[REC] shuffle");
769 rb->lcd_putsxy(2, 38, "[PLAY..] solution");
770 rb->lcd_putsxy(2, 48, "[PLAY] step by step");
771 #elif CONFIG_KEYPAD == GIGABEAT_PAD
772 rb->lcd_putsxy(2, 8, "[POWER] to stop");
773 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
774 rb->lcd_putsxy(2, 28, "[MENU] shuffle");
775 rb->lcd_putsxy(2, 38, "[VOL+] solution");
776 rb->lcd_putsxy(2, 48, "[VOL-] step by step");
777 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
778 rb->lcd_putsxy(2, 8, "[POWER] to stop");
779 rb->lcd_putsxy(2, 18, "[REW] toggle");
780 rb->lcd_putsxy(2, 28, "[PL-LEFT] shuffle");
781 rb->lcd_putsxy(2, 38, "[PL-RIGHT] solution");
782 rb->lcd_putsxy(2, 48, "[PL-UP] step by step");
783 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
784 rb->lcd_putsxy(2, 8, "[BACK] to stop");
785 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
786 rb->lcd_putsxy(2, 28, "[MENU] shuffle");
787 rb->lcd_putsxy(2, 38, "[VOL+] solution");
788 rb->lcd_putsxy(2, 48, "[VOL-] step by step");
789 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
790 (CONFIG_KEYPAD == SANSA_C200_PAD)
791 rb->lcd_putsxy(2, 8, "[POWER] to stop");
792 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
793 rb->lcd_putsxy(2, 28, "[REC-LEFT] shuffle");
794 rb->lcd_putsxy(2, 38, "[REC-RIGHT] solution");
795 rb->lcd_putsxy(2, 48, "[REC-SEL] step by step");
796 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
797 rb->lcd_putsxy(2, 8, "[REC] to stop");
798 rb->lcd_putsxy(2, 18, "[PLAY] toggle");
799 rb->lcd_putsxy(2, 28, "[MODE] shuffle");
800 rb->lcd_putsxy(2, 38, "[MENU..] solution");
801 rb->lcd_putsxy(2, 48, "[MENU] step by step");
802 #endif
804 #ifdef HAVE_TOUCHSCREEN
805 rb->lcd_putsxy(2, 8, "[BOTTOMLEFT] to stop");
806 rb->lcd_putsxy(2, 18, "[CENTRE] toggle");
807 rb->lcd_putsxy(2, 28, "[TOPRIGHT] shuffle");
808 rb->lcd_putsxy(2, 38, "[BOTTOMLEFT] solution");
809 rb->lcd_putsxy(2, 48, "[BOTTOMRIGHT] step by step");
810 #endif
812 rb->lcd_update();
813 #else /* HAVE_LCD_CHARCELLS */
814 if (!init_gfx())
815 return PLUGIN_ERROR;
816 #endif
817 rb->button_get_w_tmo(HZ*3);
819 rb->lcd_clear_display();
820 draw_info_panel();
821 for (i=0; i<20; i++) {
822 spots[i]=1;
823 draw_spot(i);
825 rb->lcd_update();
826 rb->sleep(HZ*3/2);
827 rb->srand(*rb->current_tick);
829 rc = flipit_loop();
830 #ifdef HAVE_LCD_CHARCELLS
831 release_gfx();
832 #endif
833 return rc;