Fix red in bootloaders
[maemo-rb.git] / apps / plugins / flipit.c
blobfe383c139484d609fe9975c1312d7f6b7dc0cefa
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 == COWOND2_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 == ONDAVX747_PAD || CONFIG_KEYPAD == MROBE500_PAD
281 #define FLIPIT_QUIT BUTTON_POWER
283 #else
284 #error No keymap defined!
285 #endif
287 #ifdef HAVE_TOUCHSCREEN
288 #ifndef FLIPIT_LEFT
289 #define FLIPIT_LEFT BUTTON_MIDLEFT
290 #endif
291 #ifndef FLIPIT_RIGHT
292 #define FLIPIT_RIGHT BUTTON_MIDRIGHT
293 #endif
294 #ifndef FLIPIT_UP
295 #define FLIPIT_UP BUTTON_TOPMIDDLE
296 #endif
297 #ifndef FLIPIT_DOWN
298 #define FLIPIT_DOWN BUTTON_BOTTOMMIDDLE
299 #endif
300 #ifndef FLIPIT_QUIT
301 #define FLIPIT_QUIT BUTTON_TOPLEFT
302 #endif
303 #ifndef FLIPIT_SHUFFLE
304 #define FLIPIT_SHUFFLE BUTTON_TOPRIGHT
305 #endif
306 #ifndef FLIPIT_SOLVE
307 #define FLIPIT_SOLVE BUTTON_BOTTOMLEFT
308 #endif
309 #ifndef FLIPIT_STEP_BY_STEP
310 #define FLIPIT_STEP_BY_STEP BUTTON_BOTTOMRIGHT
311 #endif
312 #ifndef FLIPIT_TOGGLE
313 #define FLIPIT_TOGGLE BUTTON_CENTER
314 #endif
315 #endif
317 static int spots[20];
318 static int toggle[20];
319 static int cursor_pos, moves;
321 #ifdef HAVE_LCD_BITMAP
323 #include "pluginbitmaps/flipit_cursor.h"
324 #include "pluginbitmaps/flipit_tokens.h"
326 #define PANEL_HEIGHT 12
327 #define TK_WIDTH BMPWIDTH_flipit_cursor
328 #define TK_HEIGHT BMPHEIGHT_flipit_cursor
329 #define TK_SPACE MAX(0, MIN((LCD_WIDTH - 5*TK_WIDTH)/4, \
330 (LCD_HEIGHT - PANEL_HEIGHT - 4*TK_HEIGHT)/4))
331 #define GRID_WIDTH (5*TK_WIDTH + 4*TK_SPACE)
332 #define GRID_LEFT ((LCD_WIDTH - GRID_WIDTH)/2)
333 #define GRID_HEIGHT (4*TK_HEIGHT + 4*TK_SPACE) /* includes grid-panel space */
334 #define GRID_TOP MAX(0, ((LCD_HEIGHT - PANEL_HEIGHT - GRID_HEIGHT)/2))
336 /* draw a spot at the coordinates (x,y), range of p is 0-19 */
337 static void draw_spot(int p)
339 rb->lcd_bitmap_part( flipit_tokens, 0, spots[p] * TK_HEIGHT, TK_WIDTH,
340 GRID_LEFT + (p%5) * (TK_WIDTH+TK_SPACE),
341 GRID_TOP + (p/5) * (TK_HEIGHT+TK_SPACE),
342 TK_WIDTH, TK_HEIGHT );
345 /* draw the cursor at the current cursor position */
346 static void draw_cursor(void)
348 #ifdef HAVE_LCD_COLOR
349 rb->lcd_bitmap_transparent( flipit_cursor,
350 GRID_LEFT + (cursor_pos%5) * (TK_WIDTH+TK_SPACE),
351 GRID_TOP + (cursor_pos/5) * (TK_HEIGHT+TK_SPACE),
352 TK_WIDTH, TK_HEIGHT );
353 #else
354 rb->lcd_set_drawmode(DRMODE_FG);
355 rb->lcd_mono_bitmap( flipit_cursor,
356 GRID_LEFT + (cursor_pos%5) * (TK_WIDTH+TK_SPACE),
357 GRID_TOP + (cursor_pos/5) * (TK_HEIGHT+TK_SPACE),
358 TK_WIDTH, TK_HEIGHT );
359 rb->lcd_set_drawmode(DRMODE_SOLID);
360 #endif
363 /* draw the info panel ... duh */
364 static void draw_info_panel(void)
366 char s[32];
368 rb->lcd_set_drawmode( DRMODE_SOLID|DRMODE_INVERSEVID );
369 rb->lcd_fillrect( GRID_LEFT, GRID_TOP + 4*(TK_HEIGHT+TK_SPACE),
370 GRID_WIDTH, PANEL_HEIGHT );
371 rb->lcd_set_drawmode( DRMODE_SOLID );
372 rb->lcd_drawrect( GRID_LEFT, GRID_TOP + 4*(TK_HEIGHT+TK_SPACE),
373 GRID_WIDTH, PANEL_HEIGHT );
375 rb->snprintf( s, sizeof(s), "Flips: %d", moves );
376 rb->lcd_putsxy( (LCD_WIDTH - rb->lcd_getstringsize(s, NULL, NULL)) / 2,
377 GRID_TOP + 4*(TK_HEIGHT+TK_SPACE) + 2, s );
380 #else /* HAVE_LCD_CHARCELLS */
382 static const unsigned char tk_pat[4][7] = {
383 { 0x0e, 0x11, 0x0e, 0x00, 0x0e, 0x11, 0x0e }, /* white - white */
384 { 0x0e, 0x11, 0x0e, 0x00, 0x0e, 0x1f, 0x0e }, /* white - black */
385 { 0x0e, 0x1f, 0x0e, 0x00, 0x0e, 0x11, 0x0e }, /* black - white */
386 { 0x0e, 0x1f, 0x0e, 0x00, 0x0e, 0x1f, 0x0e } /* black - black */
389 static unsigned char cur_pat[7];
390 static unsigned long gfx_chars[5];
392 static void release_gfx(void)
394 int i;
396 for (i = 0; i < 5; i++)
397 if (gfx_chars[i])
398 rb->lcd_unlock_pattern(gfx_chars[i]);
401 static bool init_gfx(void)
403 int i;
405 for (i = 0; i < 5; i++) {
406 if ((gfx_chars[i] = rb->lcd_get_locked_pattern()) == 0) {
407 release_gfx();
408 return false;
411 for (i = 0; i < 4; i++)
412 rb->lcd_define_pattern(gfx_chars[i], tk_pat[i]);
413 return true;
416 /* draw a spot at the coordinates (x,y), range of p is 0-19 */
417 static void draw_spot(int p)
419 if ((p/5) & 1)
420 p -= 5;
422 rb->lcd_putc (p%5, p/10, gfx_chars[2*spots[p]+spots[p+5]]);
425 /* draw the cursor at the current cursor position */
426 static void draw_cursor(void)
428 if ((cursor_pos/5) & 1) {
429 rb->memcpy( cur_pat, tk_pat[2*spots[cursor_pos-5]+spots[cursor_pos]], 7 );
430 cur_pat[4] ^= 0x15;
431 cur_pat[6] ^= 0x11;
433 else {
434 rb->memcpy( cur_pat, tk_pat[2*spots[cursor_pos]+spots[cursor_pos+5]], 7 );
435 cur_pat[0] ^= 0x15;
436 cur_pat[2] ^= 0x11;
438 rb->lcd_define_pattern(gfx_chars[4], cur_pat);
439 rb->lcd_putc( cursor_pos%5, cursor_pos/10, gfx_chars[4] );
442 /* draw the info panel ... duh */
443 static void draw_info_panel(void)
445 char s[16];
447 rb->lcd_puts( 6, 0, "Flips" );
448 rb->snprintf( s, sizeof(s), "%d", moves );
449 rb->lcd_puts( 6, 1, s );
452 #endif /* LCD */
454 /* clear the cursor where it is */
455 static inline void clear_cursor(void)
457 draw_spot( cursor_pos );
460 /* check if the puzzle is finished */
461 static bool flipit_finished(void)
463 int i;
464 for (i=0; i<20; i++)
465 if (!spots[i])
466 return false;
467 clear_cursor();
468 return true;
471 /* draws the toggled spots */
472 static void flipit_toggle(void)
474 spots[cursor_pos] = 1-spots[cursor_pos];
475 toggle[cursor_pos] = 1-toggle[cursor_pos];
476 draw_spot(cursor_pos);
477 if (cursor_pos%5 > 0) {
478 spots[cursor_pos-1] = 1-spots[cursor_pos-1];
479 draw_spot(cursor_pos-1);
481 if (cursor_pos%5 < 4) {
482 spots[cursor_pos+1] = 1-spots[cursor_pos+1];
483 draw_spot(cursor_pos+1);
485 if (cursor_pos/5 > 0) {
486 spots[cursor_pos-5] = 1-spots[cursor_pos-5];
487 draw_spot(cursor_pos-5);
489 if (cursor_pos/5 < 3) {
490 spots[cursor_pos+5] = 1-spots[cursor_pos+5];
491 draw_spot(cursor_pos+5);
493 moves++;
495 draw_info_panel();
497 if (flipit_finished())
498 clear_cursor();
501 /* move the cursor in any direction */
502 static void move_cursor(int x, int y)
504 if (!(flipit_finished())) {
505 clear_cursor();
506 cursor_pos = ( x + 5 + cursor_pos%5 )%5
507 + ( ( y + 4 + cursor_pos/5 )%4 )*5;
508 draw_cursor();
510 rb->lcd_update();
513 /* initialize the board */
514 static void flipit_init(void)
516 int i;
518 rb->lcd_clear_display();
519 for (i=0; i<20; i++) {
520 spots[i]=1;
521 toggle[i]=1;
522 draw_spot(i);
524 rb->lcd_update();
526 for (i=0; i<20; i++) {
527 cursor_pos = (rb->rand() % 20);
528 flipit_toggle();
531 cursor_pos = 0;
532 draw_cursor();
533 moves = 0;
534 draw_info_panel();
535 rb->lcd_update();
538 /* the main game loop */
539 static bool flipit_loop(void)
541 int i;
542 int button;
543 int lastbutton = BUTTON_NONE;
545 flipit_init();
546 while(true) {
547 button = rb->button_get(true);
548 switch (button) {
549 #ifdef FLIPIT_RC_QUIT
550 case FLIPIT_RC_QUIT:
551 #endif
552 case FLIPIT_QUIT:
553 /* get out of here */
554 return PLUGIN_OK;
556 case FLIPIT_SHUFFLE:
557 /* mix up the pieces */
558 flipit_init();
559 break;
561 case FLIPIT_SOLVE:
562 #ifdef FLIPIT_SOLVE_PRE
563 if (lastbutton != FLIPIT_SOLVE_PRE)
564 break;
565 #endif
566 /* solve the puzzle */
567 if (!flipit_finished()) {
568 for (i=0; i<20; i++)
569 if (!toggle[i]) {
570 clear_cursor();
571 cursor_pos = i;
572 flipit_toggle();
573 draw_cursor();
574 rb->lcd_update();
575 rb->sleep(HZ*2/3);
578 break;
580 case FLIPIT_STEP_BY_STEP:
581 #ifdef FLIPIT_STEP_PRE
582 if (lastbutton != FLIPIT_STEP_PRE)
583 break;
584 #endif
585 if (!flipit_finished()) {
586 for (i=0; i<20; i++)
587 if (!toggle[i]) {
588 clear_cursor();
589 cursor_pos = i;
590 flipit_toggle();
591 draw_cursor();
592 rb->lcd_update();
593 break;
596 break;
598 case FLIPIT_TOGGLE:
599 #ifdef FLIPIT_TOGGLE_PRE
600 if (lastbutton != FLIPIT_TOGGLE_PRE)
601 break;
602 #endif
603 /* toggle the pieces */
604 if (!flipit_finished()) {
605 flipit_toggle();
606 draw_cursor();
607 rb->lcd_update();
609 break;
611 case FLIPIT_LEFT:
612 move_cursor(-1, 0);
613 break;
615 case FLIPIT_RIGHT:
616 move_cursor(1, 0);
617 break;
618 /*move cursor though the entire field*/
619 #ifdef FLIPIT_SCROLLWHEEL
620 case FLIPIT_PREV:
621 case FLIPIT_PREV|BUTTON_REPEAT:
622 if ((cursor_pos)%5 == 0) {
623 move_cursor(-1, -1);
625 else {
626 move_cursor(-1, 0);
628 break;
630 case FLIPIT_NEXT:
631 case FLIPIT_NEXT|BUTTON_REPEAT:
632 if ((cursor_pos+1)%5 == 0) {
633 move_cursor(1, 1);
635 else {
636 move_cursor(1, 0);
638 break;
639 #endif
640 case FLIPIT_UP:
641 #ifdef FLIPIT_UP_PRE
642 if (lastbutton != FLIPIT_UP_PRE)
643 break;
644 #endif
645 move_cursor(0, -1);
646 break;
648 case FLIPIT_DOWN:
649 move_cursor(0, 1);
650 break;
652 default:
653 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
654 return PLUGIN_USB_CONNECTED;
655 break;
657 if (button != BUTTON_NONE)
658 lastbutton = button;
662 /* called function from outside */
663 enum plugin_status plugin_start(const void* parameter)
665 int i, rc;
667 (void)parameter;
669 #ifdef HAVE_LCD_COLOR
670 rb->lcd_set_background(LCD_WHITE);
671 rb->lcd_set_foreground(LCD_BLACK);
672 #endif
674 #if LCD_DEPTH > 1
675 rb->lcd_set_backdrop(NULL);
676 #endif
678 rb->splash(HZ, "FlipIt!");
680 #ifdef HAVE_LCD_BITMAP
681 /* print instructions */
682 rb->lcd_clear_display();
683 rb->lcd_setfont(FONT_SYSFIXED);
684 #if CONFIG_KEYPAD == RECORDER_PAD
685 rb->lcd_putsxy(2, 8, "[OFF] to stop");
686 rb->lcd_putsxy(2, 18, "[PLAY] toggle");
687 rb->lcd_putsxy(2, 28, "[F1] shuffle");
688 rb->lcd_putsxy(2, 38, "[F2] solution");
689 rb->lcd_putsxy(2, 48, "[F3] step by step");
690 #elif CONFIG_KEYPAD == ONDIO_PAD
691 rb->lcd_putsxy(2, 8, "[OFF] to stop");
692 rb->lcd_putsxy(2, 18, "[MODE] toggle");
693 rb->lcd_putsxy(2, 28, "[M-LEFT] shuffle");
694 rb->lcd_putsxy(2, 38, "[M-UP] solution");
695 rb->lcd_putsxy(2, 48, "[M-RIGHT] step by step");
696 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
697 (CONFIG_KEYPAD == IRIVER_H300_PAD)
698 rb->lcd_putsxy(2, 8, "[STOP] to stop");
699 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
700 rb->lcd_putsxy(2, 28, "[MODE] shuffle");
701 rb->lcd_putsxy(2, 38, "[PLAY] solution");
702 rb->lcd_putsxy(2, 48, "[REC] step by step");
703 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
704 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
705 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
706 rb->lcd_putsxy(2, 8, "[S-MENU] to stop");
707 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
708 rb->lcd_putsxy(2, 28, "[S-LEFT] shuffle");
709 rb->lcd_putsxy(2, 38, "[S-PLAY] solution");
710 rb->lcd_putsxy(2, 48, "[S-RIGHT] step by step");
711 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
712 rb->lcd_putsxy(2, 8, "[POWER] to stop");
713 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
714 rb->lcd_putsxy(2, 28, "[REC] shuffle");
715 rb->lcd_putsxy(2, 38, "[PLAY..] solution");
716 rb->lcd_putsxy(2, 48, "[PLAY] step by step");
717 #elif CONFIG_KEYPAD == GIGABEAT_PAD
718 rb->lcd_putsxy(2, 8, "[POWER] to stop");
719 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
720 rb->lcd_putsxy(2, 28, "[MENU] shuffle");
721 rb->lcd_putsxy(2, 38, "[VOL+] solution");
722 rb->lcd_putsxy(2, 48, "[VOL-] step by step");
723 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
724 rb->lcd_putsxy(2, 8, "[POWER] to stop");
725 rb->lcd_putsxy(2, 18, "[REW] toggle");
726 rb->lcd_putsxy(2, 28, "[PL-LEFT] shuffle");
727 rb->lcd_putsxy(2, 38, "[PL-RIGHT] solution");
728 rb->lcd_putsxy(2, 48, "[PL-UP] step by step");
729 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
730 rb->lcd_putsxy(2, 8, "[BACK] to stop");
731 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
732 rb->lcd_putsxy(2, 28, "[MENU] shuffle");
733 rb->lcd_putsxy(2, 38, "[VOL+] solution");
734 rb->lcd_putsxy(2, 48, "[VOL-] step by step");
735 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
736 (CONFIG_KEYPAD == SANSA_C200_PAD)
737 rb->lcd_putsxy(2, 8, "[POWER] to stop");
738 rb->lcd_putsxy(2, 18, "[SELECT] toggle");
739 rb->lcd_putsxy(2, 28, "[REC-LEFT] shuffle");
740 rb->lcd_putsxy(2, 38, "[REC-RIGHT] solution");
741 rb->lcd_putsxy(2, 48, "[REC-SEL] step by step");
742 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
743 rb->lcd_putsxy(2, 8, "[REC] to stop");
744 rb->lcd_putsxy(2, 18, "[PLAY] toggle");
745 rb->lcd_putsxy(2, 28, "[MODE] shuffle");
746 rb->lcd_putsxy(2, 38, "[MENU..] solution");
747 rb->lcd_putsxy(2, 48, "[MENU] step by step");
748 #endif
750 #ifdef HAVE_TOUCHSCREEN
751 rb->lcd_putsxy(2, 8, "[BOTTOMLEFT] to stop");
752 rb->lcd_putsxy(2, 18, "[CENTRE] toggle");
753 rb->lcd_putsxy(2, 28, "[TOPRIGHT] shuffle");
754 rb->lcd_putsxy(2, 38, "[BOTTOMLEFT] solution");
755 rb->lcd_putsxy(2, 48, "[BOTTOMRIGHT] step by step");
756 #endif
758 rb->lcd_update();
759 #else /* HAVE_LCD_CHARCELLS */
760 if (!init_gfx())
761 return PLUGIN_ERROR;
762 #endif
763 rb->button_get_w_tmo(HZ*3);
765 rb->lcd_clear_display();
766 draw_info_panel();
767 for (i=0; i<20; i++) {
768 spots[i]=1;
769 draw_spot(i);
771 rb->lcd_update();
772 rb->sleep(HZ*3/2);
773 rb->srand(*rb->current_tick);
775 rc = flipit_loop();
776 #ifdef HAVE_LCD_CHARCELLS
777 release_gfx();
778 #endif
779 return rc;