A bit of adapting.
[Rockbox.git] / apps / plugins / solitaire.c
blob89b3966dfde50b0eb019aefc64693722c708881e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004-2007 Antoine Cellerier <dionoea @t videolan d.t org>
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "plugin.h"
21 #include "playback_control.h"
22 #include "configfile.h"
23 #include "button.h"
24 #include "lcd.h"
25 #include "oldmenuapi.h"
27 #ifdef HAVE_LCD_BITMAP
29 PLUGIN_HEADER
31 static struct plugin_api* rb;
32 #define min(a,b) (a<b?a:b)
34 /**
35 * Key definitions
38 #if CONFIG_KEYPAD == RECORDER_PAD
39 # define SOL_QUIT BUTTON_OFF
40 # define SOL_UP BUTTON_UP
41 # define SOL_DOWN BUTTON_DOWN
42 # define SOL_LEFT BUTTON_LEFT
43 # define SOL_RIGHT BUTTON_RIGHT
44 # define SOL_MOVE BUTTON_ON
45 # define SOL_DRAW BUTTON_F1
46 # define SOL_REM2CUR BUTTON_PLAY
47 # define SOL_CUR2STACK BUTTON_F2
48 # define SOL_REM2STACK BUTTON_F3
49 # define HK_MOVE "ON"
50 # define HK_DRAW "F1"
51 # define HK_REM2CUR "PLAY"
52 # define HK_CUR2STACK "F2"
53 # define HK_REM2STACK "F3"
55 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
56 # define SOL_QUIT BUTTON_OFF
57 # define SOL_UP BUTTON_UP
58 # define SOL_DOWN BUTTON_DOWN
59 # define SOL_LEFT BUTTON_LEFT
60 # define SOL_RIGHT BUTTON_RIGHT
61 # define SOL_MOVE BUTTON_ON
62 # define SOL_DRAW BUTTON_F1
63 # define SOL_REM2CUR BUTTON_SELECT
64 # define SOL_CUR2STACK BUTTON_F2
65 # define SOL_REM2STACK BUTTON_F3
66 # define HK_MOVE "ON"
67 # define HK_DRAW "F1"
68 # define HK_REM2CUR "SELECT"
69 # define HK_CUR2STACK "F2"
70 # define HK_REM2STACK "F3"
72 #elif CONFIG_KEYPAD == ONDIO_PAD
73 # define SOL_QUIT BUTTON_OFF
74 # define SOL_UP_PRE BUTTON_UP
75 # define SOL_UP (BUTTON_UP | BUTTON_REL)
76 # define SOL_DOWN_PRE BUTTON_DOWN
77 # define SOL_DOWN (BUTTON_DOWN | BUTTON_REL)
78 # define SOL_LEFT_PRE BUTTON_LEFT
79 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
80 # define SOL_RIGHT_PRE BUTTON_RIGHT
81 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
82 # define SOL_MOVE_PRE BUTTON_MENU
83 # define SOL_MOVE (BUTTON_MENU | BUTTON_REL)
84 # define SOL_DRAW_PRE BUTTON_MENU
85 # define SOL_DRAW (BUTTON_MENU | BUTTON_REPEAT)
86 # define SOL_REM2CUR_PRE BUTTON_DOWN
87 # define SOL_REM2CUR (BUTTON_DOWN | BUTTON_REPEAT)
88 # define SOL_CUR2STACK_PRE BUTTON_UP
89 # define SOL_CUR2STACK (BUTTON_UP | BUTTON_REPEAT)
90 # define SOL_REM2STACK_PRE BUTTON_RIGHT
91 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
92 # define HK_MOVE "MODE"
93 # define HK_DRAW "MODE.."
94 # define HK_REM2CUR "DOWN.."
95 # define HK_CUR2STACK "UP.."
96 # define HK_REM2STACK "RIGHT.."
98 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
99 (CONFIG_KEYPAD == IRIVER_H300_PAD)
100 # define SOL_QUIT BUTTON_OFF
101 # define SOL_UP BUTTON_UP
102 # define SOL_DOWN BUTTON_DOWN
103 # define SOL_LEFT BUTTON_LEFT
104 # define SOL_RIGHT BUTTON_RIGHT
105 # define SOL_MOVE_PRE BUTTON_SELECT
106 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
107 # define SOL_DRAW BUTTON_MODE
108 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_ON)
109 # define SOL_CUR2STACK_PRE BUTTON_SELECT
110 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
111 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_ON)
112 # define SOL_REM BUTTON_REC
113 # define SOL_RC_QUIT BUTTON_RC_STOP
114 # define HK_MOVE "NAVI"
115 # define HK_DRAW "A-B"
116 # define HK_REM2CUR "PLAY+LEFT"
117 # define HK_CUR2STACK "NAVI.."
118 # define HK_REM2STACK "PLAY+RIGHT"
120 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
121 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
122 # define SOL_QUIT (BUTTON_SELECT | BUTTON_MENU)
123 # define SOL_UP BUTTON_SCROLL_BACK
124 # define SOL_DOWN BUTTON_SCROLL_FWD
125 # define SOL_LEFT_PRE BUTTON_LEFT
126 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
127 # define SOL_RIGHT_PRE BUTTON_RIGHT
128 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
129 # define SOL_MOVE_PRE BUTTON_SELECT
130 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
131 # define SOL_DRAW_PRE BUTTON_MENU
132 # define SOL_DRAW (BUTTON_MENU | BUTTON_REL)
133 # define SOL_REM2CUR BUTTON_PLAY
134 # define SOL_CUR2STACK_PRE BUTTON_MENU
135 # define SOL_CUR2STACK (BUTTON_MENU | BUTTON_REPEAT)
136 # define SOL_REM2STACK_PRE BUTTON_RIGHT
137 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
138 # define HK_UD "SCROLL U/D"
139 # define HK_MOVE "SELECT"
140 # define HK_DRAW "MENU"
141 # define HK_REM2CUR "PLAY"
142 # define HK_CUR2STACK "MENU.."
143 # define HK_REM2STACK "RIGHT.."
145 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
146 # define SOL_QUIT BUTTON_POWER
147 # define SOL_UP BUTTON_UP
148 # define SOL_DOWN BUTTON_DOWN
149 # define SOL_LEFT BUTTON_LEFT
150 # define SOL_RIGHT BUTTON_RIGHT
151 # define SOL_MOVE_PRE BUTTON_SELECT
152 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
153 # define SOL_DRAW_PRE BUTTON_PLAY
154 # define SOL_DRAW (BUTTON_PLAY | BUTTON_REL)
155 # define SOL_REM2CUR_PRE BUTTON_PLAY
156 # define SOL_REM2CUR (BUTTON_PLAY | BUTTON_REPEAT)
157 # define SOL_CUR2STACK_PRE BUTTON_SELECT
158 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
159 # define SOL_REM2STACK BUTTON_REC
160 # define HK_MOVE "SELECT"
161 # define HK_DRAW "PLAY"
162 # define HK_REM2CUR "PLAY.."
163 # define HK_CUR2STACK "SELECT.."
164 # define HK_REM2STACK "REC"
166 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
167 # define SOL_QUIT BUTTON_POWER
168 # define SOL_UP BUTTON_UP
169 # define SOL_DOWN BUTTON_DOWN
170 # define SOL_LEFT BUTTON_LEFT
171 # define SOL_RIGHT BUTTON_RIGHT
172 # define SOL_MOVE_PRE BUTTON_SELECT
173 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
174 # define SOL_DRAW BUTTON_MENU
175 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_A)
176 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
177 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_A)
178 # define HK_MOVE "SELECT"
179 # define HK_DRAW "MENU"
180 # define HK_REM2CUR "A+LEFT"
181 # define HK_CUR2STACK "SELECT.."
182 # define HK_REM2STACK "A+RIGHT"
184 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
185 # define SOL_QUIT BUTTON_POWER
186 # define SOL_UP BUTTON_UP
187 # define SOL_DOWN BUTTON_DOWN
188 # define SOL_LEFT BUTTON_SCROLL_UP
189 # define SOL_RIGHT BUTTON_SCROLL_DOWN
190 # define SOL_MOVE BUTTON_SELECT
191 # define SOL_DRAW BUTTON_REC
192 # define SOL_REM2CUR BUTTON_LEFT
193 # define SOL_CUR2STACK_PRE BUTTON_REC
194 # define SOL_CUR2STACK (BUTTON_REC | BUTTON_RIGHT)
195 # define SOL_REM2STACK BUTTON_RIGHT
196 # define HK_MOVE "SELECT"
197 # define HK_DRAW "REC"
198 # define HK_REM2CUR "LEFT"
199 # define HK_CUR2STACK "DOUBLE SELECT"
200 # define HK_REM2STACK "RIGHT"
202 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
203 # define SOL_QUIT BUTTON_POWER
204 # define SOL_UP BUTTON_UP
205 # define SOL_DOWN BUTTON_DOWN
206 # define SOL_LEFT BUTTON_LEFT
207 # define SOL_RIGHT BUTTON_RIGHT
208 # define SOL_MOVE_PRE BUTTON_SELECT
209 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
210 # define SOL_DRAW BUTTON_VOL_DOWN
211 # define SOL_REM2CUR BUTTON_REC
212 # define SOL_CUR2STACK_PRE BUTTON_SELECT
213 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
214 # define SOL_REM2STACK BUTTON_VOL_UP
215 # define HK_MOVE "SELECT"
216 # define HK_DRAW "REC"
217 # define HK_REM2CUR "LEFT"
218 # define HK_CUR2STACK "DOUBLE SELECT"
219 # define HK_REM2STACK "RIGHT"
221 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
222 # define SOL_QUIT BUTTON_POWER
223 # define SOL_UP BUTTON_SCROLL_UP
224 # define SOL_DOWN BUTTON_SCROLL_DOWN
225 # define SOL_LEFT_PRE BUTTON_LEFT
226 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
227 # define SOL_RIGHT_PRE BUTTON_RIGHT
228 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
229 # define SOL_MOVE BUTTON_PLAY
230 # define SOL_DRAW_PRE BUTTON_LEFT
231 # define SOL_DRAW (BUTTON_LEFT | BUTTON_REPEAT)
232 # define SOL_REM2CUR BUTTON_FF
233 # define SOL_CUR2STACK BUTTON_REW
234 # define SOL_REM2STACK_PRE BUTTON_RIGHT
235 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
236 # define HK_MOVE "PLAY"
237 # define HK_DRAW "LEFT.."
238 # define HK_REM2CUR "FF"
239 # define HK_CUR2STACK "REW"
240 # define HK_REM2STACK "RIGHT.."
242 #else
243 # error "Unknown keypad"
244 #endif
246 #define HK_LR "LEFT/RIGHT"
247 #ifndef HK_UD
248 # define HK_UD "UP/DOWN"
249 #endif
252 * Help strings
255 static int helplines;
256 static int displaylines;
258 static char helptext[] =
259 /* Use single spaces only! Close each line with one \0. */
260 "-- Navigation --\0"
261 HK_LR ": Move the cursor to the previous/ next column.\0"
262 HK_UD ": Move the cursor up/ down in the column.\0"
263 HK_MOVE ": Select cards, move cards, reveal hidden cards...\0"
264 HK_DRAW ": Deselect a card if it was selected. Else draw new card(s) from the remains stack.\0"
265 "-- Shortcuts --\0"
266 HK_REM2CUR ": Put the card on top of the remains stack on top of the cursor.\0"
267 HK_CUR2STACK ": Put the card under the cursor on one of the 4 final stacks.\0"
268 HK_REM2STACK ": Put the card on top of the remains stack on one of the 4 final stacks.\0"
272 * Misc constants, graphics and other defines
275 #include "card_back.h"
276 #include "card_deck.h"
277 #include "solitaire_suitsi.h"
279 #define CARD_GFX_WIDTH BMPWIDTH_card_back
280 #define CARD_GFX_HEIGHT BMPHEIGHT_card_back
281 #define CARD_WIDTH (BMPWIDTH_card_back+2)
282 #define CARD_HEIGHT (BMPHEIGHT_card_back+2)
284 #if LCD_WIDTH >= 320
285 # define MARGIN 4
286 # define LARGE_CARD
287 # define SYMBOL_HEIGHT 12
288 #elif LCD_WIDTH >= 220
289 # define MARGIN 3
290 # define LARGE_CARD
291 # define SYMBOL_HEIGHT 12
292 #elif LCD_WIDTH >= 160
293 # define MARGIN 2
294 # define SYMBOL_HEIGHT 11
295 #elif LCD_WIDTH >= 128
296 # define MARGIN 1
297 # define SYMBOL_HEIGHT 10
298 #else
299 # define MARGIN 0
300 # define SYMBOL_HEIGHT 8
301 #endif
303 #define CARD_START (CARD_HEIGHT+2*MARGIN+1)
305 /* background color */
306 #ifdef HAVE_LCD_COLOR
307 # define BACKGROUND_COLOR LCD_RGBPACK(0,157,0)
308 # define FRAME_COLOR LCD_RGBPACK(23,119,218)
309 #elif LCD_DEPTH > 1
310 # define BACKGROUND_COLOR LCD_WHITE
311 # define FRAME_COLOR LCD_BLACK
312 #endif
315 #define CONFIG_FILENAME "sol.cfg"
317 #define NOT_A_CARD -1
319 /* number of cards per suit */
320 #define CARDS_PER_SUIT 13
322 /* number of suits */
323 #define SUITS 4
325 #define NUM_CARDS ( CARDS_PER_SUIT * SUITS )
327 /* number of columns */
328 #define COL_NUM 7
330 /* pseudo column numbers to be used for cursor coordinates */
331 /* columns COL_NUM to COL_NUM + SUITS - 1 correspond to the final stacks */
332 #define STACKS_COL COL_NUM
333 /* column COL_NUM + SUITS corresponds to the remains' stack */
334 #define REM_COL (STACKS_COL + SUITS)
336 #define NOT_A_COL -1
338 typedef struct
340 signed char suit;
341 signed char num;
342 bool known : 1;
343 bool used : 1; /* this is what is used when dealing cards */
344 signed char next;
345 } card_t;
349 * LCD card drawing routines
352 static void draw_cursor( int x, int y )
354 rb->lcd_set_drawmode( DRMODE_COMPLEMENT );
355 rb->lcd_fillrect( x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
356 #ifdef LARGE_CARD
357 rb->lcd_drawpixel( x+1, y+1 );
358 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
359 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
360 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
361 #endif
362 rb->lcd_set_drawmode( DRMODE_SOLID );
365 /* Draw a card's border, select it if it's selected and draw the cursor
366 * if the cursor is currently over the card */
367 static void draw_card_ext( int x, int y, bool selected, bool cursor )
369 #if LCD_DEPTH > 1
370 int oldfg = rb->lcd_get_foreground();
372 rb->lcd_set_foreground( LCD_BLACK );
373 #endif
374 #ifdef LARGE_CARD
375 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y );
376 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y+CARD_HEIGHT-1 );
377 rb->lcd_vline( x, y+2, y+CARD_HEIGHT-3 );
378 rb->lcd_vline( x+CARD_WIDTH-1, y+2, y+CARD_HEIGHT-3 );
379 rb->lcd_drawpixel( x+1, y+1 );
380 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
381 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
382 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
383 #else
384 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y );
385 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y+CARD_HEIGHT-1 );
386 rb->lcd_vline( x, y+1, y+CARD_HEIGHT-2 );
387 rb->lcd_vline( x+CARD_WIDTH-1, y+1, y+CARD_HEIGHT-2 );
388 #endif
390 if( selected )
392 #if LCD_DEPTH > 1
393 rb->lcd_set_foreground( FRAME_COLOR );
394 #endif
395 rb->lcd_drawrect( x+1, y+1, CARD_WIDTH-2, CARD_HEIGHT-2 );
396 #ifdef LARGE_CARD
397 rb->lcd_drawrect( x+2, y+2, CARD_WIDTH-4, CARD_HEIGHT-4 );
398 #endif
400 #if LCD_DEPTH > 1
401 rb->lcd_set_foreground( oldfg );
402 #endif
404 if( cursor )
406 draw_cursor( x, y );
410 /* Draw a card's inner graphics */
411 static void draw_card( card_t *card, int x, int y,
412 bool selected, bool cursor )
414 if( card->known )
416 rb->lcd_bitmap_part( card_deck, CARD_GFX_WIDTH * card->num,
417 CARD_GFX_HEIGHT * card->suit, BMPWIDTH_card_deck,
418 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
420 else
422 rb->lcd_bitmap( card_back, x+1, y+1,
423 CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
425 draw_card_ext( x, y, selected, cursor );
428 /* Draw an empty stack */
429 static void draw_empty_stack( int s, int x, int y, bool cursor )
431 rb->lcd_bitmap_part( solitaire_suitsi, 0,
432 CARD_GFX_HEIGHT * s, BMPWIDTH_solitaire_suitsi,
433 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
435 draw_card_ext( x, y, false, cursor );
439 * Help
442 static void init_help(void)
444 int lines = 0;
445 int w_space, w, h;
446 int para_len;
447 char *para = helptext;
449 rb->lcd_getstringsize(" ", &w_space, &h);
450 displaylines = LCD_HEIGHT / h;
451 para_len = rb->strlen(para);
453 while (para_len)
455 bool first = true;
456 int x = 0;
457 char *next, *store;
459 next = rb->strtok_r(para, " ", &store);
460 while (next)
462 rb->lcd_getstringsize(next, &w, NULL);
463 if (!first)
465 if (x + w > LCD_WIDTH)
467 lines++;
468 x = 0;
470 else
471 next[-1] = ' '; /* re-concatenate string */
473 else
474 first = false;
476 x += w + w_space;
477 next = rb->strtok_r(NULL, " ", &store);
480 lines++;
481 para += para_len + 1;
482 para_len = rb->strlen(para);
484 helplines = lines;
487 enum help { HELP_QUIT, HELP_USB };
489 /* help for the not so intuitive interface */
490 enum help solitaire_help( void )
492 int start = 0;
493 int button;
494 int lastbutton = BUTTON_NONE;
495 bool fixed = (displaylines >= helplines);
497 while( true )
499 char *text = helptext;
500 int line = fixed ? (displaylines - helplines) / 2 : 0;
501 int i;
503 rb->lcd_clear_display();
505 for (i = 0; i < start + displaylines; i++)
507 if (i >= start)
508 rb->lcd_puts(0, line++, text);
509 text += rb->strlen(text) + 1;
511 rb->lcd_update();
513 button = rb->button_get( true );
514 switch( button )
516 case SOL_UP:
517 #ifdef SOL_UP_PRE
518 if( lastbutton != SOL_UP_PRE )
519 break;
520 #else
521 case SOL_UP|BUTTON_REPEAT:
522 #endif
523 if (!fixed && start > 0)
524 start--;
525 break;
527 case SOL_DOWN:
528 #ifdef SOL_DOWN_PRE
529 if( lastbutton != SOL_DOWN_PRE )
530 break;
531 #else
532 case SOL_DOWN|BUTTON_REPEAT:
533 #endif
534 if (!fixed && start < helplines - displaylines)
535 start++;
536 break;
538 #ifdef SOL_RC_QUIT
539 case SOL_RC_QUIT:
540 #endif
541 case SOL_QUIT:
542 return HELP_QUIT;
544 default:
545 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
546 return HELP_USB;
547 break;
549 if( button != BUTTON_NONE )
550 lastbutton = button;
555 * Custom menu / options
558 #define CFGFILE_VERSION 0
560 struct sol_config {
561 int draw_type;
562 int auto_unhide;
565 struct sol_config sol_disk = {0, 0};
566 struct sol_config sol;
568 static struct configdata config[] = {
569 { TYPE_INT, 0, 1, &sol_disk.draw_type, "draw_type", NULL, NULL },
570 { TYPE_INT, 0, 1, &sol_disk.auto_unhide, "auto_unhide", NULL, NULL }
573 char draw_option_string[32];
574 char unhide_option_string[32];
576 static void create_draw_option_string(void)
578 if (sol.draw_type == 0)
579 rb->strcpy(draw_option_string, "Draw Three Cards");
580 else
581 rb->strcpy(draw_option_string, "Draw One Card");
584 static void create_unhide_option_string(void)
586 if (sol.auto_unhide == 0)
587 rb->strcpy(unhide_option_string, "Unhide manually");
588 else
589 rb->strcpy(unhide_option_string, "Unhide automatically");
592 void solitaire_init(void);
594 /* menu return codes */
595 enum { MENU_RESUME, MENU_SAVE_AND_QUIT, MENU_QUIT, MENU_USB };
597 int solitaire_menu(bool in_game)
599 int m;
600 int result = -1;
601 int i = 0;
603 struct menu_item items[7];
605 if( in_game )
607 items[i++].desc = "Resume Game";
608 items[i++].desc = "Restart Game";
610 else
612 items[i++].desc = "Start Game";
613 items[i++].desc = draw_option_string;
615 items[i++].desc = unhide_option_string;
616 items[i++].desc = "Help";
617 items[i++].desc = "Audio Playback";
618 if( in_game )
620 items[i++].desc = "Save and Quit";
622 items[i++].desc = "Quit";
624 create_draw_option_string();
625 create_unhide_option_string();
626 m = menu_init(rb, items, i, NULL, NULL, NULL, NULL);
627 while (result < 0)
629 switch (menu_show(m))
631 case MENU_SELECTED_EXIT:
632 result = MENU_RESUME;
633 break;
635 case MENU_ATTACHED_USB:
636 result = MENU_USB;
637 break;
639 case 0:
640 result = MENU_RESUME;
641 break;
643 case 1:
644 if (in_game)
646 solitaire_init();
647 result = MENU_RESUME;
649 else
651 sol.draw_type = (sol.draw_type + 1) % 2;
652 create_draw_option_string();
654 break;
656 case 2:
657 sol.auto_unhide = (sol.auto_unhide + 1) % 2;
658 create_unhide_option_string();
659 break;
661 case 3:
662 rb->lcd_setmargins(0, 0);
663 if (solitaire_help() == HELP_USB)
664 result = MENU_USB;
665 break;
667 case 4:
668 playback_control(rb);
669 break;
671 case 5:
672 if( in_game )
673 result = MENU_SAVE_AND_QUIT;
674 else
675 result = MENU_QUIT;
676 break;
678 case 6:
679 result = MENU_QUIT;
680 break;
683 menu_exit(m);
684 rb->lcd_setmargins(0, 0);
685 return result;
689 * Global variables
692 /* player's cursor */
693 int cur_card;
694 /* player's cursor column num */
695 int cur_col;
697 /* selected card */
698 int sel_card;
700 /* the deck */
701 card_t deck[ NUM_CARDS ];
703 /* the remaining cards */
704 /* first card of the remains' stack */
705 int rem;
706 /* upper visible card from the remains' stack */
707 int cur_rem;
708 /* number of cards drawn from the remains stack - 1 */
709 int count_rem;
710 /* number of cards per draw of the remains' stack */
711 int cards_per_draw;
713 /* the 7 game columns */
714 int cols[COL_NUM];
715 /* the 4 final stacks */
716 int stacks[SUITS];
719 * Card handling routines
722 int next_random_card( card_t *deck )
724 int i,r;
726 r = rb->rand()%(NUM_CARDS)+1;
727 i = 0;
729 while( r>0 )
731 i = (i + 1)%(NUM_CARDS);
732 if( !deck[i].used ) r--;
735 deck[i].used = true;
737 return i;
741 /* initialize the game */
742 void solitaire_init( void )
745 int c;
746 int i, j;
748 /* number of cards that are drawn on the remains' stack (by pressing F2) */
749 if( sol.draw_type == 0 )
751 cards_per_draw = 3;
753 else
755 cards_per_draw = 1;
758 /* init deck */
759 for( i=0; i<SUITS; i++ )
761 for( j=0; j<CARDS_PER_SUIT; j++ )
763 #define card deck[i*CARDS_PER_SUIT+j]
764 card.suit = i;
765 card.num = j;
766 card.known = true;
767 card.used = false;
768 card.next = NOT_A_CARD;
769 #undef card
773 /* deal the cards ... */
774 /* ... in the columns */
775 for( i=0; i<COL_NUM; i++ )
777 c = NOT_A_CARD;
778 for( j=0; j<=i; j++ )
780 if( c == NOT_A_CARD )
782 cols[i] = next_random_card( deck );
783 c = cols[i];
785 else
787 deck[c].next = next_random_card( deck );
788 c = deck[c].next;
790 if( j < i )
791 deck[c].known = false;
795 /* ... shuffle what's left of the deck */
796 rem = next_random_card(deck);
797 c = rem;
799 for( i=1; i < NUM_CARDS - COL_NUM * (COL_NUM + 1)/2; i++ )
801 deck[c].next = next_random_card( deck );
802 c = deck[c].next;
805 /* we now finished dealing the cards. The game can start ! (at last) */
807 /* init the stack */
808 for( i = 0; i<SUITS; i++ )
810 stacks[i] = NOT_A_CARD;
813 /* the cursor starts on upper left card */
814 cur_card = cols[0];
815 cur_col = 0;
817 /* no card is selected */
818 sel_card = NOT_A_CARD;
820 /* init the remainder */
821 cur_rem = NOT_A_CARD;
823 count_rem = -1;
826 /* find the column number in which 'card' can be found */
827 int find_card_col( int card )
829 int i;
830 int c;
832 if( card == NOT_A_CARD ) return NOT_A_COL;
834 for( i=0; i<COL_NUM; i++ )
836 c = cols[i];
837 while( c != NOT_A_CARD )
839 if( c == card ) return i;
840 c = deck[c].next;
844 for( i=0; i<SUITS; i++ )
846 c = stacks[i];
847 while( c != NOT_A_CARD )
849 if( c == card ) return STACKS_COL + i;
850 c = deck[c].next;
854 return REM_COL;
857 /* find the card preceding 'card' */
858 /* if it doesn't exist, return NOT_A_CARD */
859 int find_prev_card( int card ){
860 int i;
862 for( i=0; i < NUM_CARDS; i++ )
864 if( deck[i].next == card ) return i;
867 return NOT_A_CARD;
870 /* find the last card of a given column */
871 int find_last_card( int col )
873 int c;
875 if( col < COL_NUM )
877 c = cols[col];
879 else if( col < REM_COL )
881 c = stacks[col - STACKS_COL];
883 else
885 c = cur_rem;
888 if(c == NOT_A_CARD)
889 return c;
890 else
892 while(deck[c].next != NOT_A_CARD)
893 c = deck[c].next;
894 return c;
898 enum move { MOVE_OK, MOVE_NOT_OK };
900 enum move move_card( int dest_col, int src_card )
902 /* the column on which to take src_card */
903 int src_col;
905 /* the last card of dest_col */
906 int dest_card;
908 /* the card under src_card */
909 int src_card_prev;
911 /* you can't move no card (at least, it doesn't have any consequence) */
912 if( src_card == NOT_A_CARD ) return MOVE_NOT_OK;
913 /* you can't put a card back on the remains' stack */
914 if( dest_col == REM_COL ) return MOVE_NOT_OK;
915 /* you can't move an unknown card */
916 if( !deck[src_card].known ) return MOVE_NOT_OK;
918 src_col = find_card_col( src_card );
919 dest_card = find_last_card( dest_col );
920 src_card_prev = find_prev_card( src_card );
922 /* you can't move more than one card at a time from the final stack */
923 /* to the rest of the game */
924 if( src_col >= COL_NUM && src_col < REM_COL
925 && deck[src_card].next != NOT_A_CARD )
927 return MOVE_NOT_OK;
930 /* if we (that means dest) are on one of the 7 columns ... */
931 if( dest_col < COL_NUM )
933 /* ... check is we are on an empty color and that the src is a king */
934 if( dest_card == NOT_A_CARD
935 && deck[src_card].num == CARDS_PER_SUIT - 1 )
937 /* this is a winning combination */
938 cols[dest_col] = src_card;
940 /* ... or check if the cards follow one another and have
941 * different colorsuit */
942 else if(( deck[dest_card].suit + deck[src_card].suit)%2==1
943 && deck[dest_card].num == deck[src_card].num + 1 )
945 /* this is a winning combination */
946 deck[dest_card].next = src_card;
948 /* ... or, humpf, well that's not good news */
949 else
951 /* this is not a winning combination */
952 return MOVE_NOT_OK;
955 /* if we are on one of the 4 final stacks ... */
956 else if( dest_col < REM_COL )
958 /* ... check if we are on an empty stack... */
959 if( dest_card == NOT_A_CARD )
961 /* ... and the src is an ace and this is the correct final stack */
962 if( deck[src_card].num == 0
963 && deck[src_card].suit == dest_col - STACKS_COL )
965 /* this is a winning combination */
966 stacks[dest_col - STACKS_COL] = src_card;
968 else
970 /* this is not a winning combination */
971 return MOVE_NOT_OK;
974 else /* non-empty stack */
976 /* ...check if the cards follow one another, have the same suit and
977 * {that src has no .next element or is from the remains' stack} */
978 if( deck[dest_card].suit == deck[src_card].suit
979 && deck[dest_card].num + 1 == deck[src_card].num
980 && (deck[src_card].next == NOT_A_CARD || src_col == REM_COL) )
982 /* this is a winning combination */
983 deck[dest_card].next = src_card;
985 /* ... or, well that's not good news */
986 else
988 /* this is not a winning combination */
989 return MOVE_NOT_OK;
993 /* if we are on the remains' stack */
994 else
996 /* you can't move a card back to the remains' stack */
997 return MOVE_NOT_OK;
1000 /* if the src card is from the remains' stack, we don't want to take
1001 * the following cards */
1002 if( src_col == REM_COL )
1004 /* if src card is the first card from the stack */
1005 if( src_card_prev == NOT_A_CARD )
1007 rem = deck[src_card].next;
1009 /* if src card is not the first card from the stack */
1010 else
1012 deck[src_card_prev].next = deck[src_card].next;
1014 deck[src_card].next = NOT_A_CARD;
1015 cur_rem = src_card_prev;
1016 count_rem--;
1018 /* if the src card is from somewhere else, just take everything */
1019 else
1021 if( src_card_prev == NOT_A_CARD )
1023 if( src_col < COL_NUM )
1025 cols[src_col] = NOT_A_CARD;
1027 else
1029 stacks[src_col - STACKS_COL] = NOT_A_CARD;
1032 else
1034 deck[src_card_prev].next = NOT_A_CARD;
1035 if (sol.auto_unhide)
1037 deck[src_card_prev].known = true;
1041 sel_card = NOT_A_CARD;
1042 /* tada ! */
1043 return MOVE_OK;
1046 enum { SOLITAIRE_WIN, SOLITAIRE_SAVE_AND_QUIT, SOLITAIRE_QUIT, SOLITAIRE_USB };
1049 * Bouncing cards at the end of the game
1052 #define BC_ACCEL ((1<<16)*LCD_HEIGHT/128)
1053 #define BC_MYSPEED (6*BC_ACCEL)
1054 #define BC_MXSPEED (6*LCD_HEIGHT/128)
1056 int bouncing_cards( void )
1058 int i, j, x, vx, y, fp_y, fp_vy, button;
1060 /* flush the button queue */
1061 while( ( button = rb->button_get( false ) ) != BUTTON_NONE )
1063 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1064 return SOLITAIRE_USB;
1067 /* fun stuff :) */
1068 for( i = CARDS_PER_SUIT-1; i>=0; i-- )
1070 for( j = 0; j < SUITS; j++ )
1072 x = LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*j+j+1;
1073 fp_y = MARGIN<<16;
1075 #if LCD_WIDTH > 200
1076 vx = rb->rand() % (4*BC_MXSPEED/3-2) - BC_MXSPEED;
1077 if( vx >= -1 )
1078 vx += 3;
1079 #else
1080 vx = rb->rand() % (4*BC_MXSPEED/3) - BC_MXSPEED;
1081 if( vx >= 0 )
1082 vx++;
1083 #endif
1085 fp_vy = -rb->rand() % BC_MYSPEED;
1087 while( x < LCD_WIDTH && x + CARD_WIDTH > 0 )
1089 fp_vy += BC_ACCEL;
1090 x += vx;
1091 fp_y += fp_vy;
1092 if( fp_y >= (LCD_HEIGHT-CARD_HEIGHT) << 16 )
1094 fp_vy = -fp_vy*4/5;
1095 fp_y = (LCD_HEIGHT-CARD_HEIGHT) << 16;
1097 y = fp_y >> 16;
1098 draw_card( &deck[j*CARDS_PER_SUIT+i], x, y,
1099 false, false );
1100 rb->lcd_update_rect( x<0?0:x, y<0?0:y,
1101 CARD_WIDTH, CARD_HEIGHT );
1103 button = rb->button_get_w_tmo( 2 );
1104 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1105 return SOLITAIRE_USB;
1106 if( button == SOL_QUIT || button == SOL_MOVE )
1107 return SOLITAIRE_WIN;
1111 return SOLITAIRE_WIN;
1115 * Game save/load routines
1117 void get_save_filename( char *buf )
1119 char *s;
1120 rb->strcpy( buf, rb->plugin_get_current_filename() );
1121 s = rb->strrchr( buf, '/' ) + 1;
1122 *s = '\0';
1123 rb->strcat( s, "sol.save" );
1126 int open_save_file( int flags )
1128 char buf[MAX_PATH];
1129 get_save_filename( buf );
1130 return rb->open( buf, flags );
1133 void delete_save_file( void )
1135 char buf[MAX_PATH];
1136 get_save_filename( buf );
1137 rb->remove( buf );
1140 #ifdef write
1141 # undef write
1142 #endif
1143 int save_write( int fd, const void *buf, size_t count, int *checksum )
1145 size_t i;
1146 if( rb->PREFIX(write)( fd, buf, count ) < (ssize_t)count )
1147 return 1;
1148 for( i = 0; i < count; i++ )
1149 *checksum += (int)(((const char *)buf)[i]);
1150 return 0;
1153 #ifdef read
1154 # undef read
1155 #endif
1156 int save_read( int fd, void *buf, size_t count, int *checksum )
1158 size_t i;
1159 if( rb->PREFIX(read)( fd, buf, count ) < (ssize_t)count )
1160 return 1;
1161 for( i = 0; i < count; i++ )
1162 *checksum -= (int)(((const char *)buf)[i]);
1163 return 0;
1166 int save_game( void )
1168 int fd = open_save_file( O_CREAT|O_WRONLY|O_TRUNC );
1169 int checksum = 42;
1170 if( fd < 0 )
1171 return -1;
1172 if( save_write( fd, &cur_card, sizeof( int ), &checksum )
1173 || save_write( fd, &cur_col, sizeof( int ), &checksum )
1174 || save_write( fd, &sel_card, sizeof( int ), &checksum )
1175 || save_write( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1176 || save_write( fd, &rem, sizeof( int ), &checksum )
1177 || save_write( fd, &cur_rem, sizeof( int ), &checksum )
1178 || save_write( fd, &count_rem, sizeof( int ), &checksum )
1179 || save_write( fd, &cards_per_draw, sizeof( int ), &checksum )
1180 || save_write( fd, cols, COL_NUM * sizeof( int ), &checksum )
1181 || save_write( fd, stacks, SUITS * sizeof( int ), &checksum )
1182 || ( rb->PREFIX(write)( fd, &checksum, sizeof( int ) ) < (ssize_t)(sizeof( int ) ) ) )
1184 rb->close( fd );
1185 rb->splash( 2*HZ, "Error while saving game. Aborting." );
1186 return -2;
1188 rb->close( fd );
1189 return 0;
1192 int load_game( void )
1194 int fd = open_save_file( O_RDONLY );
1195 int checksum;
1196 if( fd < 0 )
1197 return -1;
1198 if( ( rb->PREFIX(lseek)( fd, -sizeof( int ), SEEK_END ) == -((ssize_t)sizeof( int ))-1 )
1199 || ( rb->PREFIX(read)( fd, &checksum, sizeof( int ) ) < ((ssize_t)sizeof( int )) )
1200 || ( rb->PREFIX(lseek)( fd, 0, SEEK_SET ) == -1 )
1201 || save_read( fd, &cur_card, sizeof( int ), &checksum )
1202 || save_read( fd, &cur_col, sizeof( int ), &checksum )
1203 || save_read( fd, &sel_card, sizeof( int ), &checksum )
1204 || save_read( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1205 || save_read( fd, &rem, sizeof( int ), &checksum )
1206 || save_read( fd, &cur_rem, sizeof( int ), &checksum )
1207 || save_read( fd, &count_rem, sizeof( int ), &checksum )
1208 || save_read( fd, &cards_per_draw, sizeof( int ), &checksum )
1209 || save_read( fd, cols, COL_NUM * sizeof( int ), &checksum )
1210 || save_read( fd, stacks, SUITS * sizeof( int ), &checksum ) )
1212 rb->close( fd );
1213 rb->splash( 2*HZ, "Error while loading saved game. Aborting." );
1214 delete_save_file();
1215 return -2;
1217 rb->close( fd );
1218 if( checksum != 42 )
1220 rb->splash( 2*HZ, "Save file was corrupted. Aborting." );
1221 delete_save_file();
1222 return -3;
1224 return 0;
1228 * The main game loop
1230 * If skipmenu is defined to SOLITAIRE_QUIT, the menu will be skipped and
1231 * game will resume.
1234 int solitaire( int skipmenu )
1237 int i,j;
1238 int button, lastbutton = 0;
1239 int c,h,prevcard;
1240 int biggest_col_length;
1242 rb->srand( *rb->current_tick );
1243 if( skipmenu != SOLITAIRE_QUIT )
1245 switch( solitaire_menu(false) )
1247 case MENU_QUIT:
1248 return SOLITAIRE_QUIT;
1250 case MENU_USB:
1251 return SOLITAIRE_USB;
1253 solitaire_init();
1256 while( true )
1258 rb->lcd_clear_display();
1260 /* get the biggest column length so that display can be "optimized" */
1261 biggest_col_length = 0;
1263 for(i=0;i<COL_NUM;i++)
1265 j = 0;
1266 c = cols[i];
1268 if( c != NOT_A_CARD )
1270 while( true )
1272 /* don't include the last card in the column length. */
1273 if( deck[c].next == NOT_A_CARD )
1275 break; /* no successor: get outta here. */
1277 else
1279 if( deck[c].known )
1280 j += 2;
1281 else
1282 j++;
1284 c = deck[c].next;
1286 /* make column distinguishable from an empty column,
1287 * and avoid division by zero while displaying */
1288 if( j == 0 )
1289 j = 1;
1291 if( j > biggest_col_length )
1292 biggest_col_length = j;
1295 /* check if there are cards remaining in the game. */
1296 /* if there aren't any, that means you won :) */
1297 if( biggest_col_length == 0 && rem == NOT_A_CARD )
1299 rb->splash( HZ, "You Won :)" );
1300 return bouncing_cards();
1303 /* draw the columns */
1304 for( i = 0; i < COL_NUM; i++ )
1306 c = cols[i];
1307 j = CARD_START;
1308 while( true )
1310 if( c == NOT_A_CARD )
1312 /* draw the cursor on empty columns */
1313 if( cur_col == i )
1315 draw_cursor( MARGIN + i * (CARD_WIDTH
1316 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1317 j );
1319 break;
1322 draw_card( &deck[c], MARGIN + i * (CARD_WIDTH
1323 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1324 j, c == sel_card, c == cur_card );
1326 h = c;
1327 c = deck[c].next;
1328 if( c == NOT_A_CARD ) break;
1330 /* This is where we change the spacing between cards so that
1331 * they don't overflow out of the LCD */
1332 if( h == cur_card )
1333 j += SYMBOL_HEIGHT;
1334 else if( deck[h].known )
1335 j += min( SYMBOL_HEIGHT,
1336 2 * (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1337 / biggest_col_length );
1338 else
1339 j += min( SYMBOL_HEIGHT / 2,
1340 (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1341 / biggest_col_length );
1345 /* draw the stacks */
1346 for( i=0; i<SUITS; i++ )
1348 c = find_last_card( STACKS_COL + i );
1350 if( c != NOT_A_CARD )
1352 draw_card( &deck[c],
1353 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1354 MARGIN,
1355 c == sel_card, cur_col == STACKS_COL + i );
1357 else
1359 draw_empty_stack( i,
1360 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1361 MARGIN, cur_col == STACKS_COL + i );
1365 /* draw the remains */
1366 if( rem != NOT_A_CARD &&
1367 ( cur_rem == NOT_A_CARD || deck[cur_rem].next != NOT_A_CARD ) )
1369 /* gruik ! (we want to display a card back) */
1370 deck[rem].known = false;
1371 draw_card( &deck[rem], MARGIN, MARGIN, false, false );
1372 deck[rem].known = true;
1375 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1377 if( count_rem < 0 )
1379 prevcard = rem;
1380 count_rem = 0;
1381 while( prevcard != cur_rem && count_rem < cards_per_draw-1 )
1383 prevcard = deck[prevcard].next;
1384 count_rem++;
1387 prevcard = cur_rem;
1388 j = CARD_WIDTH+2*MARGIN+1;
1389 for( i = 0; i < count_rem; i++ )
1390 prevcard = find_prev_card(prevcard);
1391 for( i = 0; i <= count_rem; i++ )
1393 draw_card( &deck[prevcard], j,
1394 MARGIN, sel_card == prevcard,
1395 cur_card == prevcard );
1396 prevcard = deck[prevcard].next;
1397 j += CARD_WIDTH/2;
1400 if( ( cur_rem == NOT_A_CARD || rem == NOT_A_CARD )
1401 && cur_col == REM_COL )
1403 draw_cursor( CARD_WIDTH+2*MARGIN+1, MARGIN );
1406 rb->lcd_update();
1408 /* what to do when a key is pressed ... */
1409 button = rb->button_get( true );
1410 #if (CONFIG_KEYPAD == SANSA_E200_PAD)
1411 if (button&(BUTTON_SCROLL_UP|BUTTON_SCROLL_DOWN))
1412 button = button & (~BUTTON_REPEAT);
1413 #endif
1414 switch( button )
1416 /* move cursor to the last card of the previous column
1417 * or to the previous final stack
1418 * or to the remains stack */
1419 case SOL_RIGHT:
1420 #ifdef SOL_RIGHT_PRE
1421 if( lastbutton != SOL_RIGHT_PRE )
1422 break;
1423 #endif
1424 if( cur_col >= COL_NUM )
1426 cur_col = 0;
1428 else if( cur_col == COL_NUM - 1 )
1430 cur_col = REM_COL;
1432 else
1434 cur_col = (cur_col+1)%(REM_COL+1);
1436 if(cur_col == REM_COL)
1438 cur_card = cur_rem;
1439 break;
1441 cur_card = find_last_card( cur_col );
1442 break;
1444 /* move cursor to the last card of the next column
1445 * or to the next final stack
1446 * or to the remains stack */
1447 case SOL_LEFT:
1448 #ifdef SOL_LEFT_PRE
1449 if( lastbutton != SOL_LEFT_PRE )
1450 break;
1451 #endif
1452 if( cur_col == 0 )
1454 cur_col = REM_COL;
1456 else if( cur_col >= COL_NUM )
1458 cur_col = COL_NUM - 1;
1460 else
1462 cur_col = (cur_col + REM_COL)%(REM_COL+1);
1464 if( cur_col == REM_COL )
1466 cur_card = cur_rem;
1467 break;
1469 cur_card = find_last_card( cur_col );
1470 break;
1472 /* move cursor to card that's bellow */
1473 case SOL_DOWN:
1474 #ifdef SOL_DOWN_PRE
1475 if( lastbutton != SOL_DOWN_PRE )
1476 break;
1477 #else
1478 case SOL_DOWN|BUTTON_REPEAT:
1479 #endif
1480 if( cur_col >= COL_NUM )
1482 cur_col = (cur_col - COL_NUM + 1)%(SUITS + 1) + COL_NUM;
1483 if( cur_col == REM_COL )
1485 cur_card = cur_rem;
1487 else
1489 cur_card = find_last_card( cur_col );
1491 break;
1493 if( cur_card == NOT_A_CARD ) break;
1494 if( deck[cur_card].next != NOT_A_CARD )
1496 cur_card = deck[cur_card].next;
1498 else
1500 cur_card = cols[cur_col];
1501 while( !deck[ cur_card].known
1502 && deck[cur_card].next != NOT_A_CARD )
1504 cur_card = deck[cur_card].next;
1507 break;
1509 /* move cursor to card that's above */
1510 case SOL_UP:
1511 #ifdef SOL_UP_PRE
1512 if( lastbutton != SOL_UP_PRE )
1513 break;
1514 #else
1515 case SOL_UP|BUTTON_REPEAT:
1516 #endif
1517 if( cur_col >= COL_NUM )
1519 cur_col = (cur_col - COL_NUM + SUITS)%(SUITS + 1) + COL_NUM;
1520 if( cur_col == REM_COL )
1522 cur_card = cur_rem;
1524 else
1526 cur_card = find_last_card( cur_col );
1528 break;
1530 if( cur_card == NOT_A_CARD ) break;
1531 do {
1532 cur_card = find_prev_card( cur_card );
1533 if( cur_card == NOT_A_CARD )
1535 cur_card = find_last_card( cur_col );
1537 } while( deck[cur_card].next != NOT_A_CARD
1538 && !deck[cur_card].known );
1539 break;
1541 /* Try to put card under cursor on one of the stacks */
1542 case SOL_CUR2STACK:
1543 #ifdef SOL_CUR2STACK_PRE
1544 if( lastbutton != SOL_CUR2STACK_PRE )
1545 break;
1546 #endif
1547 move_card( deck[cur_card].suit + STACKS_COL, cur_card );
1548 break;
1550 /* Move cards arround, Uncover cards, ... */
1551 case SOL_MOVE:
1552 #ifdef SOL_MOVE_PRE
1553 if( lastbutton != SOL_MOVE_PRE )
1554 break;
1555 #endif
1557 if( sel_card == NOT_A_CARD )
1559 if( cur_card != NOT_A_CARD )
1561 if( deck[cur_card].next == NOT_A_CARD
1562 && !deck[cur_card].known )
1564 /* reveal a hidden card */
1565 deck[cur_card].known = true;
1567 else if( cur_col == REM_COL && cur_rem == NOT_A_CARD )
1569 break;
1571 else
1573 /* select a card */
1574 sel_card = cur_card;
1578 else if( sel_card == cur_card )
1580 /* unselect card or try putting card on
1581 * one of the 4 stacks */
1582 if( move_card( deck[sel_card].suit + COL_NUM, sel_card )
1583 == MOVE_OK && cur_col == REM_COL )
1585 cur_card = cur_rem;
1587 sel_card = NOT_A_CARD;
1589 else
1591 /* try moving cards */
1592 /* the code in the else seems to not like moveing kings
1593 so if the selected card is a king do it the simple way */
1594 if (deck[sel_card].num == CARDS_PER_SUIT - 1)
1596 if (move_card( cur_col, sel_card ) == MOVE_NOT_OK)
1597 sel_card = NOT_A_CARD;
1599 else
1601 int retval;
1602 do {
1603 retval = move_card( cur_col, sel_card );
1604 if (retval == MOVE_NOT_OK)
1606 sel_card = find_prev_card(sel_card);
1608 } while ((retval == MOVE_NOT_OK) && (sel_card != NOT_A_CARD));
1611 break;
1613 /* If the card on the top of the remains can be put where
1614 * the cursor is, go ahead */
1615 case SOL_REM2CUR:
1616 #ifdef SOL_REM2CUR_PRE
1617 if( lastbutton != SOL_REM2CUR_PRE )
1618 break;
1619 #endif
1620 move_card( cur_col, cur_rem );
1621 break;
1623 /* If the card on top of the remains can be put on one
1624 * of the stacks, do so */
1625 case SOL_REM2STACK:
1626 #ifdef SOL_REM2STACK_PRE
1627 if( lastbutton != SOL_REM2STACK_PRE )
1628 break;
1629 #endif
1630 move_card( deck[cur_rem].suit + COL_NUM, cur_rem );
1631 break;
1633 #ifdef SOL_REM
1634 case SOL_REM:
1635 if( sel_card != NOT_A_CARD )
1637 /* unselect selected card */
1638 sel_card = NOT_A_CARD;
1639 break;
1641 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1643 sel_card = cur_rem;
1644 break;
1646 break;
1647 #endif
1649 /* unselect selected card or ...
1650 * draw new cards from the remains of the deck */
1651 case SOL_DRAW:
1652 #ifdef SOL_DRAW_PRE
1653 if( lastbutton != SOL_DRAW_PRE )
1654 break;
1655 #endif
1656 if( sel_card != NOT_A_CARD )
1658 /* unselect selected card */
1659 sel_card = NOT_A_CARD;
1660 break;
1662 if( rem != NOT_A_CARD )
1664 int cur_rem_old = cur_rem;
1665 count_rem = -1;
1666 /* draw new cards form the remains of the deck */
1667 if( cur_rem == NOT_A_CARD )
1669 /*if the cursor card is null*/
1670 cur_rem = rem;
1671 i = cards_per_draw - 1;
1672 count_rem++;
1674 else
1676 i = cards_per_draw;
1679 while( i > 0 && deck[cur_rem].next != NOT_A_CARD )
1681 cur_rem = deck[cur_rem].next;
1682 i--;
1683 count_rem++;
1685 /* test if any cards are really left on
1686 * the remains' stack */
1687 if( i == cards_per_draw )
1689 cur_rem = NOT_A_CARD;
1690 count_rem = -1;
1692 /* if cursor was on remains' stack when new cards were
1693 * drawn, put cursor on top of remains' stack */
1694 if( cur_col == REM_COL && cur_card == cur_rem_old )
1696 cur_card = cur_rem;
1697 sel_card = NOT_A_CARD;
1700 break;
1702 /* Show the menu */
1703 #ifdef SOL_RC_QUIT
1704 case SOL_RC_QUIT:
1705 #endif
1706 case SOL_QUIT:
1707 switch( solitaire_menu(true) )
1709 case MENU_SAVE_AND_QUIT:
1710 return SOLITAIRE_SAVE_AND_QUIT;
1712 case MENU_QUIT:
1713 return SOLITAIRE_QUIT;
1715 case MENU_USB:
1716 return SOLITAIRE_USB;
1718 break;
1720 case SYS_POWEROFF:
1721 return SOLITAIRE_SAVE_AND_QUIT;
1723 default:
1724 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1725 return SOLITAIRE_USB;
1726 break;
1729 if( button != BUTTON_NONE )
1730 lastbutton = button;
1732 /* fix incoherences concerning cur_col and cur_card */
1733 c = find_card_col( cur_card );
1734 if( c != NOT_A_COL && c != cur_col )
1735 cur_card = find_last_card( cur_col );
1737 if( cur_card == NOT_A_CARD
1738 && find_last_card( cur_col ) != NOT_A_CARD )
1739 cur_card = find_last_card( cur_col );
1744 * Plugin entry point
1747 enum plugin_status plugin_start( struct plugin_api* api, void* parameter )
1749 int result;
1751 /* plugin init */
1752 (void)parameter;
1753 rb = api;
1755 rb->splash( HZ, "Welcome to Solitaire!" );
1757 configfile_init(rb);
1758 configfile_load(CONFIG_FILENAME, config,
1759 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1760 rb->memcpy(&sol, &sol_disk, sizeof(sol)); /* copy to running config */
1762 if( load_game() == 0 )
1764 rb->splash( HZ, "Resuming saved game." );
1765 result = SOLITAIRE_QUIT;
1767 else
1768 result = SOLITAIRE_WIN;
1770 init_help();
1772 /* play the game :)
1773 * Keep playing if a game was won (that means display the menu after
1774 * winning instead of quiting) */
1775 while( ( result = solitaire( result ) ) == SOLITAIRE_WIN );
1777 if( result == SOLITAIRE_QUIT )
1778 delete_save_file();
1779 else /* result == SOLITAIRE_USB || result == SOLITAIRE_SAVE_AND_QUIT */
1780 save_game();
1782 if (rb->memcmp(&sol, &sol_disk, sizeof(sol))) /* save settings if changed */
1784 rb->memcpy(&sol_disk, &sol, sizeof(sol));
1785 configfile_save(CONFIG_FILENAME, config,
1786 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1789 /* Exit the plugin */
1790 return ( result == SOLITAIRE_USB ) ? PLUGIN_USB_CONNECTED : PLUGIN_OK;
1793 #endif