Fix red in bootloaders
[maemo-rb.git] / apps / plugins / solitaire.c
blob47b1b25fa05b600e6b8f5d7b4e07217e9d6519f7
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 * 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 ****************************************************************************/
22 #include "plugin.h"
23 #include "lib/playback_control.h"
24 #include "lib/configfile.h"
25 #include "button.h"
26 #include "lcd.h"
28 #ifdef HAVE_LCD_BITMAP
30 PLUGIN_HEADER
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_BACK
189 # define SOL_RIGHT BUTTON_SCROLL_FWD
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_FUZE_PAD)
203 # define SOL_QUIT (BUTTON_HOME|BUTTON_REPEAT)
204 # define SOL_UP BUTTON_UP
205 # define SOL_DOWN BUTTON_DOWN
206 # define SOL_LEFT BUTTON_SCROLL_BACK
207 # define SOL_RIGHT BUTTON_SCROLL_FWD
208 # define SOL_MOVE (BUTTON_SELECT|BUTTON_REL)
209 # define SOL_DRAW (BUTTON_HOME|BUTTON_REL)
210 # define SOL_REM2CUR BUTTON_LEFT
211 # define SOL_CUR2STACK_PRE (BUTTON_RIGHT | BUTTON_REPEAT)
212 # define SOL_CUR2STACK BUTTON_RIGHT
213 # define SOL_REM2STACK (BUTTON_LEFT|BUTTON_REPEAT)
214 # define SOL_REM2STACK_PRE BUTTON_LEFT
215 # define HK_MOVE "SHORT SELECT"
216 # define HK_DRAW "SHORT HOME"
217 # define HK_REM2CUR "LEFT"
218 # define HK_CUR2STACK "DOUBLE SELECT"
219 # define HK_REM2STACK "LEFT"
221 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
222 # define SOL_QUIT BUTTON_POWER
223 # define SOL_UP BUTTON_UP
224 # define SOL_DOWN BUTTON_DOWN
225 # define SOL_LEFT BUTTON_LEFT
226 # define SOL_RIGHT BUTTON_RIGHT
227 # define SOL_MOVE_PRE BUTTON_SELECT
228 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
229 # define SOL_DRAW BUTTON_VOL_DOWN
230 # define SOL_REM2CUR BUTTON_REC
231 # define SOL_CUR2STACK_PRE BUTTON_SELECT
232 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
233 # define SOL_REM2STACK BUTTON_VOL_UP
234 # define HK_MOVE "SELECT"
235 # define HK_DRAW "REC"
236 # define HK_REM2CUR "LEFT"
237 # define HK_CUR2STACK "DOUBLE SELECT"
238 # define HK_REM2STACK "RIGHT"
240 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
241 # define SOL_QUIT BUTTON_POWER
242 # define SOL_UP BUTTON_UP
243 # define SOL_DOWN BUTTON_DOWN
244 # define SOL_LEFT BUTTON_LEFT
245 # define SOL_RIGHT BUTTON_RIGHT
246 # define SOL_MOVE_PRE BUTTON_SELECT
247 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
248 # define SOL_DRAW BUTTON_HOME
249 # define SOL_REM2CUR BUTTON_VOL_DOWN
250 # define SOL_CUR2STACK_PRE BUTTON_SELECT
251 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
252 # define SOL_REM2STACK BUTTON_VOL_UP
253 # define HK_MOVE "SELECT"
254 # define HK_DRAW "HOME"
255 # define HK_REM2CUR "LEFT"
256 # define HK_CUR2STACK "DOUBLE SELECT"
257 # define HK_REM2STACK "RIGHT"
259 #elif CONFIG_KEYPAD == SANSA_M200_PAD
260 # define SOL_QUIT BUTTON_POWER
261 # define SOL_UP BUTTON_UP
262 # define SOL_DOWN BUTTON_DOWN
263 # define SOL_LEFT BUTTON_LEFT
264 # define SOL_RIGHT BUTTON_RIGHT
265 # define SOL_MOVE_PRE BUTTON_SELECT
266 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
267 # define SOL_DRAW (BUTTON_SELECT | BUTTON_UP)
268 # define SOL_REM2CUR BUTTON_VOL_DOWN
269 # define SOL_CUR2STACK_PRE BUTTON_SELECT
270 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
271 # define SOL_REM2STACK BUTTON_VOL_UP
272 # define HK_MOVE "SELECT"
273 # define HK_DRAW "SELECT + UP"
274 # define HK_REM2CUR "LEFT"
275 # define HK_CUR2STACK "DOUBLE SELECT"
276 # define HK_REM2STACK "RIGHT"
278 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
279 # define SOL_QUIT BUTTON_POWER
280 # define SOL_UP BUTTON_SCROLL_UP
281 # define SOL_DOWN BUTTON_SCROLL_DOWN
282 # define SOL_LEFT_PRE BUTTON_LEFT
283 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
284 # define SOL_RIGHT_PRE BUTTON_RIGHT
285 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
286 # define SOL_MOVE BUTTON_PLAY
287 # define SOL_DRAW_PRE BUTTON_LEFT
288 # define SOL_DRAW (BUTTON_LEFT | BUTTON_REPEAT)
289 # define SOL_REM2CUR BUTTON_FF
290 # define SOL_CUR2STACK BUTTON_REW
291 # define SOL_REM2STACK_PRE BUTTON_RIGHT
292 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
293 # define HK_MOVE "PLAY"
294 # define HK_DRAW "LEFT.."
295 # define HK_REM2CUR "FF"
296 # define HK_CUR2STACK "REW"
297 # define HK_REM2STACK "RIGHT.."
299 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
300 # define SOL_QUIT BUTTON_BACK
301 # define SOL_UP BUTTON_UP
302 # define SOL_DOWN BUTTON_DOWN
303 # define SOL_LEFT BUTTON_LEFT
304 # define SOL_RIGHT BUTTON_RIGHT
305 # define SOL_MOVE_PRE BUTTON_SELECT
306 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
307 # define SOL_DRAW BUTTON_MENU
308 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_SELECT)
309 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
310 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_SELECT)
311 # define HK_MOVE "SELECT"
312 # define HK_DRAW "MENU"
313 # define HK_REM2CUR "SELECT+LEFT"
314 # define HK_CUR2STACK "SELECT.."
315 # define HK_REM2STACK "SELECT+RIGHT"
317 #elif (CONFIG_KEYPAD == MROBE100_PAD)
318 # define SOL_QUIT BUTTON_POWER
319 # define SOL_UP BUTTON_UP
320 # define SOL_DOWN BUTTON_DOWN
321 # define SOL_LEFT BUTTON_LEFT
322 # define SOL_RIGHT BUTTON_RIGHT
323 # define SOL_MOVE_PRE BUTTON_SELECT
324 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
325 # define SOL_DRAW BUTTON_MENU
326 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_DISPLAY)
327 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
328 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_DISPLAY)
329 # define HK_MOVE "SELECT"
330 # define HK_DRAW "MENU"
331 # define HK_REM2CUR "DISPLAY+LEFT"
332 # define HK_CUR2STACK "SELECT.."
333 # define HK_REM2STACK "DISPLAY+RIGHT"
335 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
336 # define SOL_QUIT BUTTON_RC_REC
337 # define SOL_UP BUTTON_RC_VOL_UP
338 # define SOL_DOWN BUTTON_RC_VOL_DOWN
339 # define SOL_LEFT BUTTON_RC_REW
340 # define SOL_RIGHT BUTTON_RC_FF
341 # define SOL_MOVE BUTTON_RC_PLAY
342 # define SOL_DRAW_PRE BUTTON_RC_MENU
343 # define SOL_DRAW (BUTTON_RC_MENU | BUTTON_REL)
344 # define SOL_REM2CUR_PRE BUTTON_RC_MENU
345 # define SOL_REM2CUR (BUTTON_RC_MENU | BUTTON_REPEAT)
346 # define SOL_CUR2STACK_PRE BUTTON_RC_MODE
347 # define SOL_CUR2STACK (BUTTON_RC_MODE | BUTTON_REL)
348 # define SOL_REM2STACK_PRE BUTTON_RC_MODE
349 # define SOL_REM2STACK (BUTTON_RC_MODE | BUTTON_REPEAT)
350 # define HK_LR "REW/FF"
351 # define HK_UD "VOL UP/DOWN"
352 # define HK_MOVE "PLAY"
353 # define HK_DRAW "MENU"
354 # define HK_REM2CUR "MENU.."
355 # define HK_CUR2STACK "MODE"
356 # define HK_REM2STACK "MODE.."
358 #elif (CONFIG_KEYPAD == COWOND2_PAD)
359 # define SOL_QUIT BUTTON_POWER
361 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
362 # define SOL_QUIT BUTTON_BACK
363 # define SOL_UP BUTTON_UP
364 # define SOL_DOWN BUTTON_DOWN
365 # define SOL_LEFT BUTTON_LEFT
366 # define SOL_RIGHT BUTTON_RIGHT
367 # define SOL_MOVE_PRE BUTTON_SELECT
368 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
369 # define SOL_DRAW BUTTON_MENU
370 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_SELECT)
371 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
372 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_SELECT)
373 # define HK_MOVE "MIDDLE"
374 # define HK_DRAW "MENU"
375 # define HK_REM2CUR "PLAY+LEFT"
376 # define HK_CUR2STACK "PLAY.."
377 # define HK_REM2STACK "PLAY+RIGHT"
379 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
380 # define SOL_QUIT BUTTON_POWER
381 # define SOL_UP BUTTON_UP
382 # define SOL_DOWN BUTTON_DOWN
383 # define SOL_LEFT BUTTON_LEFT
384 # define SOL_RIGHT BUTTON_RIGHT
385 # define SOL_MOVE_PRE BUTTON_SELECT
386 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
387 # define SOL_DRAW BUTTON_MENU
388 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_VIEW)
389 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
390 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_VIEW)
391 # define HK_MOVE "SELECT"
392 # define HK_DRAW "MENU"
393 # define HK_REM2CUR "VIEW+LEFT"
394 # define HK_CUR2STACK "SELECT.."
395 # define HK_REM2STACK "VIEW+RIGHT"
397 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || CONFIG_KEYPAD == MROBE500_PAD
398 # define SOL_QUIT BUTTON_POWER
400 #else
401 #error No keymap defined!
402 #endif
404 #ifdef HAVE_TOUCHSCREEN
405 //#ifndef SOL_QUIT
406 //# define SOL_QUIT BUTTON_TOPLEFT
407 //endif
408 #ifndef SOL_UP
409 # define SOL_UP BUTTON_TOPMIDDLE
410 #endif
411 #ifndef SOL_DOWN
412 # define SOL_DOWN BUTTON_BOTTOMMIDDLE
413 #endif
414 #ifndef SOL_LEFT
415 # define SOL_LEFT BUTTON_MIDLEFT
416 #endif
417 #ifndef SOL_RIGHT
418 # define SOL_RIGHT BUTTON_MIDRIGHT
419 #endif
420 #ifndef SOL_MOVE
421 # define SOL_MOVE BUTTON_CENTER
422 # define HK_MOVE "CENTRE"
423 #endif
424 #ifndef SOL_DRAW
425 # define SOL_DRAW BUTTON_TOPLEFT
426 # define HK_DRAW "TOPLEFT"
427 #endif
428 #ifndef SOL_REM2CUR
429 # define SOL_REM2CUR BUTTON_TOPRIGHT
430 # define HK_REM2CUR "TOPRIGHT"
431 #endif
432 #ifndef SOL_CUR2STACK
433 # define SOL_CUR2STACK BUTTON_BOTTOMLEFT
434 # define HK_CUR2STACK "BOTTOMLEFT"
435 #endif
436 #ifndef SOL_REM2STACK
437 # define SOL_REM2STACK BUTTON_BOTTOMRIGHT
438 # define HK_REM2STACK "BOTTOMRIGHT"
439 #endif
441 #endif
443 #ifndef HK_LR
444 # define HK_LR "LEFT/RIGHT"
445 #endif
446 #ifndef HK_UD
447 # define HK_UD "UP/DOWN"
448 #endif
451 * Help strings
454 static int helplines;
455 static int displaylines;
457 static char helptext[] =
458 /* Use single spaces only! Close each line with one \0. */
459 "-- Navigation --\0"
460 HK_LR ": Move the cursor to the previous/ next column.\0"
461 HK_UD ": Move the cursor up/ down in the column.\0"
462 HK_MOVE ": Select cards, move cards, reveal hidden cards...\0"
463 HK_DRAW ": Deselect a card if it was selected. Else draw new card(s) from the remains stack.\0"
464 "-- Shortcuts --\0"
465 HK_REM2CUR ": Put the card on top of the remains stack on top of the cursor.\0"
466 HK_CUR2STACK ": Put the card under the cursor on one of the 4 final stacks.\0"
467 HK_REM2STACK ": Put the card on top of the remains stack on one of the 4 final stacks.\0"
471 * Misc constants, graphics and other defines
474 #include "pluginbitmaps/card_back.h"
475 #include "pluginbitmaps/card_deck.h"
476 #include "pluginbitmaps/solitaire_suitsi.h"
478 #define CARD_GFX_WIDTH BMPWIDTH_card_back
479 #define CARD_GFX_HEIGHT BMPHEIGHT_card_back
480 #define CARD_WIDTH (BMPWIDTH_card_back+2)
481 #define CARD_HEIGHT (BMPHEIGHT_card_back+2)
483 #if LCD_WIDTH >= 320
484 # define MARGIN 4
485 # define LARGE_CARD
486 # define SYMBOL_HEIGHT 12
487 #elif LCD_WIDTH >= 220
488 # define MARGIN 3
489 # define LARGE_CARD
490 # define SYMBOL_HEIGHT 12
491 #elif LCD_WIDTH >= 160
492 # define MARGIN 2
493 # define SYMBOL_HEIGHT 11
494 #elif LCD_WIDTH >= 128
495 # define MARGIN 1
496 # define SYMBOL_HEIGHT 10
497 #else
498 # define MARGIN 0
499 # define SYMBOL_HEIGHT 8
500 #endif
502 #define CARD_START (CARD_HEIGHT+2*MARGIN+1)
504 /* background color */
505 #ifdef HAVE_LCD_COLOR
506 # define BACKGROUND_COLOR LCD_RGBPACK(0,157,0)
507 # define FRAME_COLOR LCD_RGBPACK(23,119,218)
508 #elif LCD_DEPTH > 1
509 # define BACKGROUND_COLOR LCD_WHITE
510 # define FRAME_COLOR LCD_BLACK
511 #endif
514 #define CONFIG_FILENAME "sol.cfg"
516 #define NOT_A_CARD -1
518 /* number of cards per suit */
519 #define CARDS_PER_SUIT 13
521 /* number of suits */
522 #define SUITS 4
524 #define NUM_CARDS ( CARDS_PER_SUIT * SUITS )
526 /* number of columns */
527 #define COL_NUM 7
529 /* pseudo column numbers to be used for cursor coordinates */
530 /* columns COL_NUM to COL_NUM + SUITS - 1 correspond to the final stacks */
531 #define STACKS_COL COL_NUM
532 /* column COL_NUM + SUITS corresponds to the remains' stack */
533 #define REM_COL (STACKS_COL + SUITS)
535 #define NOT_A_COL -1
537 typedef struct
539 signed char suit;
540 signed char num;
541 bool known : 1;
542 bool used : 1; /* this is what is used when dealing cards */
543 signed char next;
544 } card_t;
548 * LCD card drawing routines
551 static void draw_cursor( int x, int y )
553 rb->lcd_set_drawmode( DRMODE_COMPLEMENT );
554 rb->lcd_fillrect( x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
555 #ifdef LARGE_CARD
556 rb->lcd_drawpixel( x+1, y+1 );
557 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
558 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
559 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
560 #endif
561 rb->lcd_set_drawmode( DRMODE_SOLID );
564 /* Draw a card's border, select it if it's selected and draw the cursor
565 * if the cursor is currently over the card */
566 static void draw_card_ext( int x, int y, bool selected, bool cursor )
568 #if LCD_DEPTH > 1
569 int oldfg = rb->lcd_get_foreground();
571 rb->lcd_set_foreground( LCD_BLACK );
572 #endif
573 #ifdef LARGE_CARD
574 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y );
575 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y+CARD_HEIGHT-1 );
576 rb->lcd_vline( x, y+2, y+CARD_HEIGHT-3 );
577 rb->lcd_vline( x+CARD_WIDTH-1, y+2, y+CARD_HEIGHT-3 );
578 rb->lcd_drawpixel( x+1, y+1 );
579 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
580 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
581 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
582 #else
583 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y );
584 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y+CARD_HEIGHT-1 );
585 rb->lcd_vline( x, y+1, y+CARD_HEIGHT-2 );
586 rb->lcd_vline( x+CARD_WIDTH-1, y+1, y+CARD_HEIGHT-2 );
587 #endif
589 if( selected )
591 #if LCD_DEPTH > 1
592 rb->lcd_set_foreground( FRAME_COLOR );
593 #endif
594 rb->lcd_drawrect( x+1, y+1, CARD_WIDTH-2, CARD_HEIGHT-2 );
595 #ifdef LARGE_CARD
596 rb->lcd_drawrect( x+2, y+2, CARD_WIDTH-4, CARD_HEIGHT-4 );
597 #endif
599 #if LCD_DEPTH > 1
600 rb->lcd_set_foreground( oldfg );
601 #endif
603 if( cursor )
605 draw_cursor( x, y );
609 /* Draw a card's inner graphics */
610 static void draw_card( card_t *card, int x, int y,
611 bool selected, bool cursor )
613 if( card->known )
615 rb->lcd_bitmap_part( card_deck, CARD_GFX_WIDTH * card->num,
616 CARD_GFX_HEIGHT * card->suit, BMPWIDTH_card_deck,
617 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
619 else
621 rb->lcd_bitmap( card_back, x+1, y+1,
622 CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
624 draw_card_ext( x, y, selected, cursor );
627 /* Draw an empty stack */
628 static void draw_empty_stack( int s, int x, int y, bool cursor )
630 rb->lcd_bitmap_part( solitaire_suitsi, 0,
631 CARD_GFX_HEIGHT * s, BMPWIDTH_solitaire_suitsi,
632 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
634 draw_card_ext( x, y, false, cursor );
638 * Help
641 static void init_help(void)
643 int lines = 0;
644 int w_space, w, h;
645 int para_len;
646 char *para = helptext;
648 rb->lcd_getstringsize(" ", &w_space, &h);
649 displaylines = LCD_HEIGHT / h;
650 para_len = rb->strlen(para);
652 while (para_len)
654 bool first = true;
655 int x = 0;
656 char *next, *store;
658 next = rb->strtok_r(para, " ", &store);
659 while (next)
661 rb->lcd_getstringsize(next, &w, NULL);
662 if (!first)
664 if (x + w > LCD_WIDTH)
666 lines++;
667 x = 0;
669 else
670 next[-1] = ' '; /* re-concatenate string */
672 else
673 first = false;
675 x += w + w_space;
676 next = rb->strtok_r(NULL, " ", &store);
679 lines++;
680 para += para_len + 1;
681 para_len = rb->strlen(para);
683 helplines = lines;
686 enum help { HELP_QUIT, HELP_USB };
688 /* help for the not so intuitive interface */
689 enum help solitaire_help( void )
691 int start = 0;
692 int button;
693 int lastbutton = BUTTON_NONE;
694 bool fixed = (displaylines >= helplines);
696 while( true )
698 char *text = helptext;
699 int line = fixed ? (displaylines - helplines) / 2 : 0;
700 int i;
702 rb->lcd_clear_display();
704 for (i = 0; i < start + displaylines; i++)
706 if (i >= start)
707 rb->lcd_puts(0, line++, text);
708 text += rb->strlen(text) + 1;
710 rb->lcd_update();
712 button = rb->button_get( true );
713 switch( button )
715 case SOL_UP:
716 #ifdef SOL_UP_PRE
717 if( lastbutton != SOL_UP_PRE )
718 break;
719 #else
720 case SOL_UP|BUTTON_REPEAT:
721 #endif
722 if (!fixed && start > 0)
723 start--;
724 break;
726 case SOL_DOWN:
727 #ifdef SOL_DOWN_PRE
728 if( lastbutton != SOL_DOWN_PRE )
729 break;
730 #else
731 case SOL_DOWN|BUTTON_REPEAT:
732 #endif
733 if (!fixed && start < helplines - displaylines)
734 start++;
735 break;
737 #ifdef SOL_RC_QUIT
738 case SOL_RC_QUIT:
739 #endif
740 case SOL_QUIT:
741 return HELP_QUIT;
743 default:
744 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
745 return HELP_USB;
746 break;
748 if( button != BUTTON_NONE )
749 lastbutton = button;
751 rb->yield();
756 * Custom menu / options
759 #define CFGFILE_VERSION 0
761 struct sol_config {
762 int draw_type;
765 struct sol_config sol_disk = {0};
766 struct sol_config sol;
768 static struct configdata config[] = {
769 { TYPE_INT, 0, 1, { .int_p = &sol_disk.draw_type }, "draw_type", NULL }
772 static const struct opt_items drawcards[2] = {
773 { "Draw Three Cards", -1 },
774 { "Draw One Card", -1 },
777 void solitaire_init(void);
779 /* menu return codes */
780 enum { MENU_RESUME, MENU_SAVE_AND_QUIT, MENU_QUIT, MENU_USB };
782 static bool _ingame;
783 int solitaire_menu_cb(int action, const struct menu_item_ex *this_item)
785 int i = (intptr_t)this_item;
786 if( action == ACTION_REQUEST_MENUITEM )
788 if((!_ingame && (i==0 || i==5)) || ( _ingame && i==2 ))
789 return ACTION_EXIT_MENUITEM;
791 return action;
794 int solitaire_menu(bool in_game)
796 int selected = 0;
797 int result = -1;
799 MENUITEM_STRINGLIST(menu, "Solitaire Menu", solitaire_menu_cb,
800 "Resume Game", "Start New Game",
801 "Draw Cards Option",
802 "Help", "Playback Control",
803 "Save and Quit", "Quit");
804 _ingame = in_game;
806 while (result < 0)
808 switch (rb->do_menu(&menu, &selected, NULL, false))
810 default:
811 result = MENU_RESUME;
812 break;
814 case MENU_ATTACHED_USB:
815 result = MENU_USB;
816 break;
818 case 0:
819 result = MENU_RESUME;
820 break;
822 case 1:
823 solitaire_init();
824 result = MENU_RESUME;
825 break;
827 case 2:
828 if (rb->set_option("Draw Cards Option", &sol.draw_type,
829 INT, drawcards, 2, NULL))
830 result = MENU_USB;
831 break;
833 case 3:
834 if (solitaire_help() == HELP_USB)
835 result = MENU_USB;
836 break;
838 case 4:
839 playback_control(NULL);
840 break;
842 case 5:
843 result = MENU_SAVE_AND_QUIT;
844 break;
846 case 6:
847 result = MENU_QUIT;
848 break;
851 return result;
855 * Global variables
858 /* player's cursor */
859 int cur_card;
860 /* player's cursor column num */
861 int cur_col;
863 /* selected card */
864 int sel_card;
866 /* the deck */
867 card_t deck[ NUM_CARDS ];
869 /* the remaining cards */
870 /* first card of the remains' stack */
871 int rem;
872 /* upper visible card from the remains' stack */
873 int cur_rem;
874 /* number of cards drawn from the remains stack - 1 */
875 int count_rem;
876 /* number of cards per draw of the remains' stack */
877 int cards_per_draw;
879 /* the 7 game columns */
880 int cols[COL_NUM];
881 /* the 4 final stacks */
882 int stacks[SUITS];
885 * Card handling routines
888 int next_random_card( card_t *deck )
890 int i,r;
892 r = rb->rand()%(NUM_CARDS)+1;
893 i = 0;
895 while( r>0 )
897 i = (i + 1)%(NUM_CARDS);
898 if( !deck[i].used ) r--;
901 deck[i].used = true;
903 return i;
907 /* initialize the game */
908 void solitaire_init( void )
911 int c;
912 int i, j;
914 /* number of cards that are drawn on the remains' stack (by pressing F2) */
915 if( sol.draw_type == 0 )
917 cards_per_draw = 3;
919 else
921 cards_per_draw = 1;
924 /* init deck */
925 for( i=0; i<SUITS; i++ )
927 for( j=0; j<CARDS_PER_SUIT; j++ )
929 #define card deck[i*CARDS_PER_SUIT+j]
930 card.suit = i;
931 card.num = j;
932 card.known = true;
933 card.used = false;
934 card.next = NOT_A_CARD;
935 #undef card
939 /* deal the cards ... */
940 /* ... in the columns */
941 for( i=0; i<COL_NUM; i++ )
943 c = NOT_A_CARD;
944 for( j=0; j<=i; j++ )
946 if( c == NOT_A_CARD )
948 cols[i] = next_random_card( deck );
949 c = cols[i];
951 else
953 deck[c].next = next_random_card( deck );
954 c = deck[c].next;
956 if( j < i )
957 deck[c].known = false;
961 /* ... shuffle what's left of the deck */
962 rem = next_random_card(deck);
963 c = rem;
965 for( i=1; i < NUM_CARDS - COL_NUM * (COL_NUM + 1)/2; i++ )
967 deck[c].next = next_random_card( deck );
968 c = deck[c].next;
971 /* we now finished dealing the cards. The game can start ! (at last) */
973 /* init the stack */
974 for( i = 0; i<SUITS; i++ )
976 stacks[i] = NOT_A_CARD;
979 /* the cursor starts on upper left card */
980 cur_card = cols[0];
981 cur_col = 0;
983 /* no card is selected */
984 sel_card = NOT_A_CARD;
986 /* init the remainder */
987 cur_rem = NOT_A_CARD;
989 count_rem = -1;
992 /* find the column number in which 'card' can be found */
993 int find_card_col( int card )
995 int i;
996 int c;
998 if( card == NOT_A_CARD ) return NOT_A_COL;
1000 for( i=0; i<COL_NUM; i++ )
1002 c = cols[i];
1003 while( c != NOT_A_CARD )
1005 if( c == card ) return i;
1006 c = deck[c].next;
1010 for( i=0; i<SUITS; i++ )
1012 c = stacks[i];
1013 while( c != NOT_A_CARD )
1015 if( c == card ) return STACKS_COL + i;
1016 c = deck[c].next;
1020 return REM_COL;
1023 /* find the card preceding 'card' */
1024 /* if it doesn't exist, return NOT_A_CARD */
1025 int find_prev_card( int card ){
1026 int i;
1028 for( i=0; i < NUM_CARDS; i++ )
1030 if( deck[i].next == card ) return i;
1033 return NOT_A_CARD;
1036 /* find the last card of a given column */
1037 int find_last_card( int col )
1039 int c;
1041 if( col < COL_NUM )
1043 c = cols[col];
1045 else if( col < REM_COL )
1047 c = stacks[col - STACKS_COL];
1049 else
1051 c = cur_rem;
1054 if(c == NOT_A_CARD)
1055 return c;
1056 else
1058 while(deck[c].next != NOT_A_CARD)
1059 c = deck[c].next;
1060 return c;
1064 enum move { MOVE_OK, MOVE_NOT_OK };
1066 enum move move_card( int dest_col, int src_card )
1068 /* the column on which to take src_card */
1069 int src_col;
1071 /* the last card of dest_col */
1072 int dest_card;
1074 /* the card under src_card */
1075 int src_card_prev;
1077 /* you can't move no card (at least, it doesn't have any consequence) */
1078 if( src_card == NOT_A_CARD ) return MOVE_NOT_OK;
1079 /* you can't put a card back on the remains' stack */
1080 if( dest_col == REM_COL ) return MOVE_NOT_OK;
1081 /* you can't move an unknown card */
1082 if( !deck[src_card].known ) return MOVE_NOT_OK;
1084 src_col = find_card_col( src_card );
1085 dest_card = find_last_card( dest_col );
1086 src_card_prev = find_prev_card( src_card );
1088 /* you can't move more than one card at a time from the final stack */
1089 /* to the rest of the game */
1090 if( src_col >= COL_NUM && src_col < REM_COL
1091 && deck[src_card].next != NOT_A_CARD )
1093 return MOVE_NOT_OK;
1096 /* if we (that means dest) are on one of the 7 columns ... */
1097 if( dest_col < COL_NUM )
1099 /* ... check is we are on an empty color and that the src is a king */
1100 if( dest_card == NOT_A_CARD
1101 && deck[src_card].num == CARDS_PER_SUIT - 1 )
1103 /* this is a winning combination */
1104 cols[dest_col] = src_card;
1106 /* ... or check if the cards follow one another and have
1107 * different colorsuit */
1108 else if(( deck[dest_card].suit + deck[src_card].suit)%2==1
1109 && deck[dest_card].num == deck[src_card].num + 1 )
1111 /* this is a winning combination */
1112 deck[dest_card].next = src_card;
1114 /* ... or, humpf, well that's not good news */
1115 else
1117 /* this is not a winning combination */
1118 return MOVE_NOT_OK;
1121 /* if we are on one of the 4 final stacks ... */
1122 else if( dest_col < REM_COL )
1124 /* ... check if we are on an empty stack... */
1125 if( dest_card == NOT_A_CARD )
1127 /* ... and the src is an ace and this is the correct final stack */
1128 if( deck[src_card].num == 0
1129 && deck[src_card].suit == dest_col - STACKS_COL )
1131 /* this is a winning combination */
1132 stacks[dest_col - STACKS_COL] = src_card;
1134 else
1136 /* this is not a winning combination */
1137 return MOVE_NOT_OK;
1140 else /* non-empty stack */
1142 /* ...check if the cards follow one another, have the same suit and
1143 * {that src has no .next element or is from the remains' stack} */
1144 if( deck[dest_card].suit == deck[src_card].suit
1145 && deck[dest_card].num + 1 == deck[src_card].num
1146 && (deck[src_card].next == NOT_A_CARD || src_col == REM_COL) )
1148 /* this is a winning combination */
1149 deck[dest_card].next = src_card;
1151 /* ... or, well that's not good news */
1152 else
1154 /* this is not a winning combination */
1155 return MOVE_NOT_OK;
1159 /* if we are on the remains' stack */
1160 else
1162 /* you can't move a card back to the remains' stack */
1163 return MOVE_NOT_OK;
1166 /* if the src card is from the remains' stack, we don't want to take
1167 * the following cards */
1168 if( src_col == REM_COL )
1170 /* if src card is the first card from the stack */
1171 if( src_card_prev == NOT_A_CARD )
1173 rem = deck[src_card].next;
1175 /* if src card is not the first card from the stack */
1176 else
1178 deck[src_card_prev].next = deck[src_card].next;
1180 deck[src_card].next = NOT_A_CARD;
1181 cur_rem = src_card_prev;
1182 count_rem--;
1184 /* if the src card is from somewhere else, just take everything */
1185 else
1187 if( src_card_prev == NOT_A_CARD )
1189 if( src_col < COL_NUM )
1191 cols[src_col] = NOT_A_CARD;
1193 else
1195 stacks[src_col - STACKS_COL] = NOT_A_CARD;
1198 else
1200 deck[src_card_prev].next = NOT_A_CARD;
1201 deck[src_card_prev].known = true;
1204 sel_card = NOT_A_CARD;
1205 /* tada ! */
1206 return MOVE_OK;
1209 enum { SOLITAIRE_WIN, SOLITAIRE_SAVE_AND_QUIT, SOLITAIRE_QUIT, SOLITAIRE_USB };
1212 * Bouncing cards at the end of the game
1215 #define BC_ACCEL ((1<<16)*LCD_HEIGHT/128)
1216 #define BC_MYSPEED (6*BC_ACCEL)
1217 #define BC_MXSPEED (6*LCD_HEIGHT/128)
1219 int bouncing_cards( void )
1221 int i, j, x, vx, y, fp_y, fp_vy, button;
1223 /* flush the button queue */
1224 while( ( button = rb->button_get( false ) ) != BUTTON_NONE )
1226 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1227 return SOLITAIRE_USB;
1230 /* fun stuff :) */
1231 for( i = CARDS_PER_SUIT-1; i>=0; i-- )
1233 for( j = 0; j < SUITS; j++ )
1235 x = LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*j+j+1;
1236 fp_y = MARGIN<<16;
1238 #if LCD_WIDTH > 200
1239 vx = rb->rand() % (4*BC_MXSPEED/3-2) - BC_MXSPEED;
1240 if( vx >= -1 )
1241 vx += 3;
1242 #else
1243 vx = rb->rand() % (4*BC_MXSPEED/3) - BC_MXSPEED;
1244 if( vx >= 0 )
1245 vx++;
1246 #endif
1248 fp_vy = -rb->rand() % BC_MYSPEED;
1250 while( x < LCD_WIDTH && x + CARD_WIDTH > 0 )
1252 fp_vy += BC_ACCEL;
1253 x += vx;
1254 fp_y += fp_vy;
1255 if( fp_y >= (LCD_HEIGHT-CARD_HEIGHT) << 16 )
1257 fp_vy = -fp_vy*4/5;
1258 fp_y = (LCD_HEIGHT-CARD_HEIGHT) << 16;
1260 y = fp_y >> 16;
1261 draw_card( &deck[j*CARDS_PER_SUIT+i], x, y,
1262 false, false );
1263 rb->lcd_update_rect( x<0?0:x, y<0?0:y,
1264 CARD_WIDTH, CARD_HEIGHT );
1266 button = rb->button_get_w_tmo( 2 );
1267 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1268 return SOLITAIRE_USB;
1269 if( button == SOL_QUIT || button == SOL_MOVE )
1270 return SOLITAIRE_WIN;
1274 return SOLITAIRE_WIN;
1278 * Game save/load routines
1280 void get_save_filename( char *buf )
1282 char *s;
1283 rb->strcpy( buf, rb->plugin_get_current_filename() );
1284 s = rb->strrchr( buf, '/' ) + 1;
1285 *s = '\0';
1286 rb->strcat( s, "sol.save" );
1289 int open_save_file( int flags )
1291 char buf[MAX_PATH];
1292 get_save_filename( buf );
1293 return rb->open( buf, flags );
1296 void delete_save_file( void )
1298 char buf[MAX_PATH];
1299 get_save_filename( buf );
1300 rb->remove( buf );
1303 #ifdef write
1304 # undef write
1305 #endif
1306 int save_write( int fd, const void *buf, size_t count, int *checksum )
1308 size_t i;
1309 if( rb->write( fd, buf, count ) < (ssize_t)count )
1310 return 1;
1311 for( i = 0; i < count; i++ )
1312 *checksum += (int)(((const char *)buf)[i]);
1313 return 0;
1316 #ifdef read
1317 # undef read
1318 #endif
1319 int save_read( int fd, void *buf, size_t count, int *checksum )
1321 size_t i;
1322 if( rb->read( fd, buf, count ) < (ssize_t)count )
1323 return 1;
1324 for( i = 0; i < count; i++ )
1325 *checksum -= (int)(((const char *)buf)[i]);
1326 return 0;
1329 int save_game( void )
1331 int fd = open_save_file( O_CREAT|O_WRONLY|O_TRUNC );
1332 int checksum = 42;
1333 if( fd < 0 )
1334 return -1;
1335 if( save_write( fd, &cur_card, sizeof( int ), &checksum )
1336 || save_write( fd, &cur_col, sizeof( int ), &checksum )
1337 || save_write( fd, &sel_card, sizeof( int ), &checksum )
1338 || save_write( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1339 || save_write( fd, &rem, sizeof( int ), &checksum )
1340 || save_write( fd, &cur_rem, sizeof( int ), &checksum )
1341 || save_write( fd, &count_rem, sizeof( int ), &checksum )
1342 || save_write( fd, &cards_per_draw, sizeof( int ), &checksum )
1343 || save_write( fd, cols, COL_NUM * sizeof( int ), &checksum )
1344 || save_write( fd, stacks, SUITS * sizeof( int ), &checksum )
1345 || ( rb->write( fd, &checksum, sizeof( int ) ) < (ssize_t)(sizeof( int ) ) ) )
1347 rb->close( fd );
1348 rb->splash( 2*HZ, "Error while saving game. Aborting." );
1349 return -2;
1351 rb->close( fd );
1352 return 0;
1355 int load_game( void )
1357 int checksum, retval;
1359 int fd = open_save_file( O_RDONLY );
1360 if( fd < 0 )
1361 return -1;
1363 retval = 0; /* Assume good case */
1364 if( ( rb->lseek( fd, -sizeof( int ), SEEK_END ) == -((ssize_t)sizeof( int ))-1 )
1365 || ( rb->read( fd, &checksum, sizeof( int ) ) < ((ssize_t)sizeof( int )) )
1366 || ( rb->lseek( fd, 0, SEEK_SET ) == -1 )
1367 || save_read( fd, &cur_card, sizeof( int ), &checksum )
1368 || save_read( fd, &cur_col, sizeof( int ), &checksum )
1369 || save_read( fd, &sel_card, sizeof( int ), &checksum )
1370 || save_read( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1371 || save_read( fd, &rem, sizeof( int ), &checksum )
1372 || save_read( fd, &cur_rem, sizeof( int ), &checksum )
1373 || save_read( fd, &count_rem, sizeof( int ), &checksum )
1374 || save_read( fd, &cards_per_draw, sizeof( int ), &checksum )
1375 || save_read( fd, cols, COL_NUM * sizeof( int ), &checksum )
1376 || save_read( fd, stacks, SUITS * sizeof( int ), &checksum ) )
1378 rb->splash( 2*HZ, "Error while loading saved game. Aborting." );
1379 retval = -2;
1381 else if( checksum != 42 )
1383 rb->splash( 2*HZ, "Save file was corrupted. Aborting." );
1384 retval = -3;
1387 rb->close( fd );
1388 delete_save_file();
1389 return retval;
1393 * The main game loop
1395 * If skipmenu is defined to SOLITAIRE_QUIT, the menu will be skipped and
1396 * game will resume.
1399 int solitaire( int skipmenu )
1402 int i,j;
1403 int button, lastbutton = 0;
1404 int c,h,prevcard;
1405 int biggest_col_length;
1407 rb->srand( *rb->current_tick );
1408 if( skipmenu != SOLITAIRE_QUIT )
1410 switch( solitaire_menu(false) )
1412 case MENU_QUIT:
1413 return SOLITAIRE_QUIT;
1415 case MENU_USB:
1416 return SOLITAIRE_USB;
1418 solitaire_init();
1421 while( true )
1423 rb->lcd_clear_display();
1425 /* get the biggest column length so that display can be "optimized" */
1426 biggest_col_length = 0;
1428 for(i=0;i<COL_NUM;i++)
1430 j = 0;
1431 c = cols[i];
1433 if( c != NOT_A_CARD )
1435 while( true )
1437 /* don't include the last card in the column length. */
1438 if( deck[c].next == NOT_A_CARD )
1440 break; /* no successor: get outta here. */
1442 else
1444 if( deck[c].known )
1445 j += 2;
1446 else
1447 j++;
1449 c = deck[c].next;
1451 /* make column distinguishable from an empty column,
1452 * and avoid division by zero while displaying */
1453 if( j == 0 )
1454 j = 1;
1456 if( j > biggest_col_length )
1457 biggest_col_length = j;
1460 /* check if there are cards remaining in the game. */
1461 /* if there aren't any, that means you won :) */
1462 if( biggest_col_length == 0 && rem == NOT_A_CARD )
1464 rb->lcd_update();
1465 rb->splash( HZ, "You Won :)" );
1466 return bouncing_cards();
1469 /* draw the columns */
1470 for( i = 0; i < COL_NUM; i++ )
1472 c = cols[i];
1473 j = CARD_START;
1474 while( true )
1476 if( c == NOT_A_CARD )
1478 /* draw the cursor on empty columns */
1479 if( cur_col == i )
1481 draw_cursor( MARGIN + i * (CARD_WIDTH
1482 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1483 j );
1485 break;
1488 draw_card( &deck[c], MARGIN + i * (CARD_WIDTH
1489 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1490 j, c == sel_card, c == cur_card );
1492 h = c;
1493 c = deck[c].next;
1494 if( c == NOT_A_CARD ) break;
1496 /* This is where we change the spacing between cards so that
1497 * they don't overflow out of the LCD */
1498 if( h == cur_card )
1499 j += SYMBOL_HEIGHT;
1500 else if( deck[h].known )
1501 j += min( SYMBOL_HEIGHT,
1502 2 * (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1503 / biggest_col_length );
1504 else
1505 j += min( SYMBOL_HEIGHT / 2,
1506 (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1507 / biggest_col_length );
1511 /* draw the stacks */
1512 for( i=0; i<SUITS; i++ )
1514 c = find_last_card( STACKS_COL + i );
1516 if( c != NOT_A_CARD )
1518 draw_card( &deck[c],
1519 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1520 MARGIN,
1521 c == sel_card, cur_col == STACKS_COL + i );
1523 else
1525 draw_empty_stack( i,
1526 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1527 MARGIN, cur_col == STACKS_COL + i );
1531 /* draw the remains */
1532 if( rem != NOT_A_CARD &&
1533 ( cur_rem == NOT_A_CARD || deck[cur_rem].next != NOT_A_CARD ) )
1535 /* gruik ! (we want to display a card back) */
1536 deck[rem].known = false;
1537 draw_card( &deck[rem], MARGIN, MARGIN, false, false );
1538 deck[rem].known = true;
1541 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1543 if( count_rem < 0 )
1545 prevcard = rem;
1546 count_rem = 0;
1547 while( prevcard != cur_rem && count_rem < cards_per_draw-1 )
1549 prevcard = deck[prevcard].next;
1550 count_rem++;
1553 prevcard = cur_rem;
1554 j = CARD_WIDTH+2*MARGIN+1;
1555 for( i = 0; i < count_rem; i++ )
1556 prevcard = find_prev_card(prevcard);
1557 for( i = 0; i <= count_rem; i++ )
1559 draw_card( &deck[prevcard], j,
1560 MARGIN, sel_card == prevcard,
1561 cur_card == prevcard );
1562 prevcard = deck[prevcard].next;
1563 j += CARD_WIDTH/2;
1566 if( ( cur_rem == NOT_A_CARD || rem == NOT_A_CARD )
1567 && cur_col == REM_COL )
1569 draw_cursor( CARD_WIDTH+2*MARGIN+1, MARGIN );
1572 rb->lcd_update();
1574 /* what to do when a key is pressed ... */
1575 button = rb->button_get( true );
1576 #if (CONFIG_KEYPAD == SANSA_E200_PAD) || (CONFIG_KEYPAD == SANSA_FUZE_PAD)
1577 if (button&(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD))
1578 button = button & (~BUTTON_REPEAT);
1579 #endif
1580 switch( button )
1582 /* move cursor to the last card of the previous column
1583 * or to the previous final stack
1584 * or to the remains stack */
1585 case SOL_RIGHT:
1586 #ifdef SOL_RIGHT_PRE
1587 if( lastbutton != SOL_RIGHT_PRE )
1588 break;
1589 #endif
1590 if( cur_col >= COL_NUM )
1592 cur_col = 0;
1594 else if( cur_col == COL_NUM - 1 )
1596 cur_col = REM_COL;
1598 else
1600 cur_col = (cur_col+1)%(REM_COL+1);
1602 if(cur_col == REM_COL)
1604 cur_card = cur_rem;
1605 break;
1607 cur_card = find_last_card( cur_col );
1608 break;
1610 /* move cursor to the last card of the next column
1611 * or to the next final stack
1612 * or to the remains stack */
1613 case SOL_LEFT:
1614 #ifdef SOL_LEFT_PRE
1615 if( lastbutton != SOL_LEFT_PRE )
1616 break;
1617 #endif
1618 if( cur_col == 0 )
1620 cur_col = REM_COL;
1622 else if( cur_col >= COL_NUM )
1624 cur_col = COL_NUM - 1;
1626 else
1628 cur_col = (cur_col + REM_COL)%(REM_COL+1);
1630 if( cur_col == REM_COL )
1632 cur_card = cur_rem;
1633 break;
1635 cur_card = find_last_card( cur_col );
1636 break;
1638 /* move cursor to card that's bellow */
1639 case SOL_DOWN:
1640 #ifdef SOL_DOWN_PRE
1641 if( lastbutton != SOL_DOWN_PRE )
1642 break;
1643 #else
1644 case SOL_DOWN|BUTTON_REPEAT:
1645 #endif
1646 if( cur_col >= COL_NUM )
1648 cur_col = (cur_col - COL_NUM + 1)%(SUITS + 1) + COL_NUM;
1649 if( cur_col == REM_COL )
1651 cur_card = cur_rem;
1653 else
1655 cur_card = find_last_card( cur_col );
1657 break;
1659 if( cur_card == NOT_A_CARD ) break;
1660 if( deck[cur_card].next != NOT_A_CARD )
1662 cur_card = deck[cur_card].next;
1664 else
1666 cur_card = cols[cur_col];
1667 while( !deck[ cur_card].known
1668 && deck[cur_card].next != NOT_A_CARD )
1670 cur_card = deck[cur_card].next;
1673 break;
1675 /* move cursor to card that's above */
1676 case SOL_UP:
1677 #ifdef SOL_UP_PRE
1678 if( lastbutton != SOL_UP_PRE )
1679 break;
1680 #else
1681 case SOL_UP|BUTTON_REPEAT:
1682 #endif
1683 if( cur_col >= COL_NUM )
1685 cur_col = (cur_col - COL_NUM + SUITS)%(SUITS + 1) + COL_NUM;
1686 if( cur_col == REM_COL )
1688 cur_card = cur_rem;
1690 else
1692 cur_card = find_last_card( cur_col );
1694 break;
1696 if( cur_card == NOT_A_CARD ) break;
1697 do {
1698 cur_card = find_prev_card( cur_card );
1699 if( cur_card == NOT_A_CARD )
1701 cur_card = find_last_card( cur_col );
1703 } while( deck[cur_card].next != NOT_A_CARD
1704 && !deck[cur_card].known );
1705 break;
1707 /* Try to put card under cursor on one of the stacks */
1708 case SOL_CUR2STACK:
1709 #ifdef SOL_CUR2STACK_PRE
1710 if( lastbutton != SOL_CUR2STACK_PRE )
1711 break;
1712 #endif
1713 move_card( deck[cur_card].suit + STACKS_COL, cur_card );
1714 break;
1716 /* Move cards arround, Uncover cards, ... */
1717 case SOL_MOVE:
1718 #ifdef SOL_MOVE_PRE
1719 if( lastbutton != SOL_MOVE_PRE )
1720 break;
1721 #endif
1723 if( sel_card == NOT_A_CARD )
1725 if( cur_card != NOT_A_CARD )
1727 if( deck[cur_card].next == NOT_A_CARD
1728 && !deck[cur_card].known )
1730 /* reveal a hidden card */
1731 deck[cur_card].known = true;
1733 else if( cur_col == REM_COL && cur_rem == NOT_A_CARD )
1735 break;
1737 else
1739 /* select a card */
1740 sel_card = cur_card;
1744 else if( sel_card == cur_card )
1746 /* unselect card or try putting card on
1747 * one of the 4 stacks */
1748 if( move_card( deck[sel_card].suit + COL_NUM, sel_card )
1749 == MOVE_OK && cur_col == REM_COL )
1751 cur_card = cur_rem;
1753 sel_card = NOT_A_CARD;
1755 else
1757 /* try moving cards */
1758 /* The flexible move must not be used from the remains stack. */
1759 if (find_card_col(sel_card) == REM_COL)
1761 if (move_card( cur_col, sel_card ) == MOVE_NOT_OK)
1762 sel_card = NOT_A_CARD;
1764 else
1766 do {
1767 if (move_card( cur_col, sel_card) == MOVE_OK)
1768 break;
1769 sel_card = find_prev_card(sel_card);
1770 } while (sel_card != NOT_A_CARD);
1773 break;
1775 /* If the card on the top of the remains can be put where
1776 * the cursor is, go ahead */
1777 case SOL_REM2CUR:
1778 #ifdef SOL_REM2CUR_PRE
1779 if( lastbutton != SOL_REM2CUR_PRE )
1780 break;
1781 #endif
1782 move_card( cur_col, cur_rem );
1783 break;
1785 /* If the card on top of the remains can be put on one
1786 * of the stacks, do so */
1787 case SOL_REM2STACK:
1788 #ifdef SOL_REM2STACK_PRE
1789 if( lastbutton != SOL_REM2STACK_PRE )
1790 break;
1791 #endif
1792 move_card( deck[cur_rem].suit + COL_NUM, cur_rem );
1793 break;
1795 #ifdef SOL_REM
1796 case SOL_REM:
1797 if( sel_card != NOT_A_CARD )
1799 /* unselect selected card */
1800 sel_card = NOT_A_CARD;
1801 break;
1803 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1805 sel_card = cur_rem;
1806 break;
1808 break;
1809 #endif
1811 /* unselect selected card or ...
1812 * draw new cards from the remains of the deck */
1813 case SOL_DRAW:
1814 #ifdef SOL_DRAW_PRE
1815 if( lastbutton != SOL_DRAW_PRE )
1816 break;
1817 #endif
1818 if( sel_card != NOT_A_CARD )
1820 /* unselect selected card */
1821 sel_card = NOT_A_CARD;
1822 break;
1824 if( rem != NOT_A_CARD )
1826 int cur_rem_old = cur_rem;
1827 count_rem = -1;
1828 /* draw new cards form the remains of the deck */
1829 if( cur_rem == NOT_A_CARD )
1831 /*if the cursor card is null*/
1832 cur_rem = rem;
1833 i = cards_per_draw - 1;
1834 count_rem++;
1836 else
1838 i = cards_per_draw;
1841 while( i > 0 && deck[cur_rem].next != NOT_A_CARD )
1843 cur_rem = deck[cur_rem].next;
1844 i--;
1845 count_rem++;
1847 /* test if any cards are really left on
1848 * the remains' stack */
1849 if( i == cards_per_draw )
1851 cur_rem = NOT_A_CARD;
1852 count_rem = -1;
1854 /* if cursor was on remains' stack when new cards were
1855 * drawn, put cursor on top of remains' stack */
1856 if( cur_col == REM_COL && cur_card == cur_rem_old )
1858 cur_card = cur_rem;
1859 sel_card = NOT_A_CARD;
1862 break;
1864 /* Show the menu */
1865 #ifdef SOL_RC_QUIT
1866 case SOL_RC_QUIT:
1867 #endif
1868 case SOL_QUIT:
1869 switch( solitaire_menu(true) )
1871 case MENU_SAVE_AND_QUIT:
1872 return SOLITAIRE_SAVE_AND_QUIT;
1874 case MENU_QUIT:
1875 return SOLITAIRE_QUIT;
1877 case MENU_USB:
1878 return SOLITAIRE_USB;
1880 break;
1882 case SYS_POWEROFF:
1883 return SOLITAIRE_SAVE_AND_QUIT;
1885 default:
1886 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1887 return SOLITAIRE_USB;
1888 break;
1891 if( button != BUTTON_NONE )
1892 lastbutton = button;
1894 /* fix incoherences concerning cur_col and cur_card */
1895 c = find_card_col( cur_card );
1896 if( c != NOT_A_COL && c != cur_col )
1897 cur_card = find_last_card( cur_col );
1899 if( cur_card == NOT_A_CARD
1900 && find_last_card( cur_col ) != NOT_A_CARD )
1901 cur_card = find_last_card( cur_col );
1903 rb->yield();
1908 * Plugin entry point
1911 enum plugin_status plugin_start(const void* parameter )
1913 int result;
1915 /* plugin init */
1916 (void)parameter;
1918 rb->splash( HZ, "Welcome to Solitaire!" );
1920 configfile_load(CONFIG_FILENAME, config,
1921 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1922 rb->memcpy(&sol, &sol_disk, sizeof(sol)); /* copy to running config */
1924 if( load_game() == 0 )
1926 rb->splash( HZ, "Resuming saved game." );
1927 result = SOLITAIRE_QUIT;
1929 else
1930 result = SOLITAIRE_WIN;
1932 init_help();
1934 /* play the game :)
1935 * Keep playing if a game was won (that means display the menu after
1936 * winning instead of quiting) */
1937 while( ( result = solitaire( result ) ) == SOLITAIRE_WIN );
1939 if( result != SOLITAIRE_QUIT )
1940 /* result == SOLITAIRE_USB || result == SOLITAIRE_SAVE_AND_QUIT */
1941 save_game();
1943 if (rb->memcmp(&sol, &sol_disk, sizeof(sol))) /* save settings if changed */
1945 rb->memcpy(&sol_disk, &sol, sizeof(sol));
1946 configfile_save(CONFIG_FILENAME, config,
1947 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1950 /* Exit the plugin */
1951 return ( result == SOLITAIRE_USB ) ? PLUGIN_USB_CONNECTED : PLUGIN_OK;
1954 #endif