Re-add the lseek to the beginning of the file which was accidentally removed.
[kugel-rb.git] / apps / plugins / solitaire.c
blobfeafd42f765f2c9cdb05960912df4f9ce82449b2
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 "lib/display_text.h"
26 #include "button.h"
27 #include "lcd.h"
29 #define min(a,b) (a<b?a:b)
31 /**
32 * Key definitions
35 #if CONFIG_KEYPAD == RECORDER_PAD
36 # define SOL_QUIT BUTTON_OFF
37 # define SOL_UP BUTTON_UP
38 # define SOL_DOWN BUTTON_DOWN
39 # define SOL_LEFT BUTTON_LEFT
40 # define SOL_RIGHT BUTTON_RIGHT
41 # define SOL_MOVE BUTTON_ON
42 # define SOL_DRAW BUTTON_F1
43 # define SOL_REM2CUR BUTTON_PLAY
44 # define SOL_CUR2STACK BUTTON_F2
45 # define SOL_REM2STACK BUTTON_F3
46 # define HK_MOVE "ON"
47 # define HK_DRAW "F1"
48 # define HK_REM2CUR "PLAY"
49 # define HK_CUR2STACK "F2"
50 # define HK_REM2STACK "F3"
52 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
53 # define SOL_QUIT BUTTON_OFF
54 # define SOL_UP BUTTON_UP
55 # define SOL_DOWN BUTTON_DOWN
56 # define SOL_LEFT BUTTON_LEFT
57 # define SOL_RIGHT BUTTON_RIGHT
58 # define SOL_MOVE BUTTON_ON
59 # define SOL_DRAW BUTTON_F1
60 # define SOL_REM2CUR BUTTON_SELECT
61 # define SOL_CUR2STACK BUTTON_F2
62 # define SOL_REM2STACK BUTTON_F3
63 # define HK_MOVE "ON"
64 # define HK_DRAW "F1"
65 # define HK_REM2CUR "SELECT"
66 # define HK_CUR2STACK "F2"
67 # define HK_REM2STACK "F3"
69 #elif CONFIG_KEYPAD == ONDIO_PAD
70 # define SOL_QUIT BUTTON_OFF
71 # define SOL_UP_PRE BUTTON_UP
72 # define SOL_UP (BUTTON_UP | BUTTON_REL)
73 # define SOL_DOWN_PRE BUTTON_DOWN
74 # define SOL_DOWN (BUTTON_DOWN | BUTTON_REL)
75 # define SOL_LEFT_PRE BUTTON_LEFT
76 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
77 # define SOL_RIGHT_PRE BUTTON_RIGHT
78 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
79 # define SOL_MOVE_PRE BUTTON_MENU
80 # define SOL_MOVE (BUTTON_MENU | BUTTON_REL)
81 # define SOL_DRAW_PRE BUTTON_MENU
82 # define SOL_DRAW (BUTTON_MENU | BUTTON_REPEAT)
83 # define SOL_REM2CUR_PRE BUTTON_DOWN
84 # define SOL_REM2CUR (BUTTON_DOWN | BUTTON_REPEAT)
85 # define SOL_CUR2STACK_PRE BUTTON_UP
86 # define SOL_CUR2STACK (BUTTON_UP | BUTTON_REPEAT)
87 # define SOL_REM2STACK_PRE BUTTON_RIGHT
88 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
89 # define HK_MOVE "MODE"
90 # define HK_DRAW "MODE.."
91 # define HK_REM2CUR "DOWN.."
92 # define HK_CUR2STACK "UP.."
93 # define HK_REM2STACK "RIGHT.."
95 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
96 (CONFIG_KEYPAD == IRIVER_H300_PAD)
97 # define SOL_QUIT BUTTON_OFF
98 # define SOL_UP BUTTON_UP
99 # define SOL_DOWN BUTTON_DOWN
100 # define SOL_LEFT BUTTON_LEFT
101 # define SOL_RIGHT BUTTON_RIGHT
102 # define SOL_MOVE_PRE BUTTON_SELECT
103 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
104 # define SOL_DRAW BUTTON_MODE
105 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_ON)
106 # define SOL_CUR2STACK_PRE BUTTON_SELECT
107 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
108 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_ON)
109 # define SOL_REM BUTTON_REC
110 # define SOL_RC_QUIT BUTTON_RC_STOP
111 # define HK_MOVE "NAVI"
112 # define HK_DRAW "A-B"
113 # define HK_REM2CUR "PLAY+LEFT"
114 # define HK_CUR2STACK "NAVI.."
115 # define HK_REM2STACK "PLAY+RIGHT"
117 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
118 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
119 # define SOL_QUIT (BUTTON_SELECT | BUTTON_MENU)
120 # define SOL_UP BUTTON_SCROLL_BACK
121 # define SOL_DOWN BUTTON_SCROLL_FWD
122 # define SOL_LEFT_PRE BUTTON_LEFT
123 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
124 # define SOL_RIGHT_PRE BUTTON_RIGHT
125 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
126 # define SOL_MOVE_PRE BUTTON_SELECT
127 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
128 # define SOL_DRAW_PRE BUTTON_MENU
129 # define SOL_DRAW (BUTTON_MENU | BUTTON_REL)
130 # define SOL_REM2CUR BUTTON_PLAY
131 # define SOL_CUR2STACK_PRE BUTTON_MENU
132 # define SOL_CUR2STACK (BUTTON_MENU | BUTTON_REPEAT)
133 # define SOL_REM2STACK_PRE BUTTON_RIGHT
134 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
135 # define HK_UD "SCROLL U/D"
136 # define HK_MOVE "SELECT"
137 # define HK_DRAW "MENU"
138 # define HK_REM2CUR "PLAY"
139 # define HK_CUR2STACK "MENU.."
140 # define HK_REM2STACK "RIGHT.."
142 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
143 # define SOL_QUIT BUTTON_POWER
144 # define SOL_UP BUTTON_UP
145 # define SOL_DOWN BUTTON_DOWN
146 # define SOL_LEFT BUTTON_LEFT
147 # define SOL_RIGHT BUTTON_RIGHT
148 # define SOL_MOVE_PRE BUTTON_SELECT
149 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
150 # define SOL_DRAW_PRE BUTTON_PLAY
151 # define SOL_DRAW (BUTTON_PLAY | BUTTON_REL)
152 # define SOL_REM2CUR_PRE BUTTON_PLAY
153 # define SOL_REM2CUR (BUTTON_PLAY | BUTTON_REPEAT)
154 # define SOL_CUR2STACK_PRE BUTTON_SELECT
155 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
156 # define SOL_REM2STACK BUTTON_REC
157 # define HK_MOVE "SELECT"
158 # define HK_DRAW "PLAY"
159 # define HK_REM2CUR "PLAY.."
160 # define HK_CUR2STACK "SELECT.."
161 # define HK_REM2STACK "REC"
163 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
164 # define SOL_QUIT BUTTON_POWER
165 # define SOL_UP BUTTON_UP
166 # define SOL_DOWN BUTTON_DOWN
167 # define SOL_LEFT BUTTON_LEFT
168 # define SOL_RIGHT BUTTON_RIGHT
169 # define SOL_MOVE_PRE BUTTON_SELECT
170 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
171 # define SOL_DRAW BUTTON_MENU
172 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_A)
173 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
174 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_A)
175 # define HK_MOVE "SELECT"
176 # define HK_DRAW "MENU"
177 # define HK_REM2CUR "A+LEFT"
178 # define HK_CUR2STACK "SELECT.."
179 # define HK_REM2STACK "A+RIGHT"
181 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
182 # define SOL_QUIT BUTTON_POWER
183 # define SOL_UP BUTTON_UP
184 # define SOL_DOWN BUTTON_DOWN
185 # define SOL_LEFT BUTTON_SCROLL_BACK
186 # define SOL_RIGHT BUTTON_SCROLL_FWD
187 # define SOL_MOVE BUTTON_SELECT
188 # define SOL_DRAW BUTTON_REC
189 # define SOL_REM2CUR BUTTON_LEFT
190 # define SOL_CUR2STACK_PRE BUTTON_REC
191 # define SOL_CUR2STACK (BUTTON_REC | BUTTON_RIGHT)
192 # define SOL_REM2STACK BUTTON_RIGHT
193 # define HK_MOVE "SELECT"
194 # define HK_DRAW "REC"
195 # define HK_REM2CUR "LEFT"
196 # define HK_CUR2STACK "DOUBLE SELECT"
197 # define HK_REM2STACK "RIGHT"
199 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
200 # define SOL_QUIT (BUTTON_HOME|BUTTON_REPEAT)
201 # define SOL_UP BUTTON_UP
202 # define SOL_DOWN BUTTON_DOWN
203 # define SOL_LEFT BUTTON_SCROLL_BACK
204 # define SOL_RIGHT BUTTON_SCROLL_FWD
205 # define SOL_MOVE (BUTTON_SELECT|BUTTON_REL)
206 # define SOL_DRAW (BUTTON_HOME|BUTTON_REL)
207 # define SOL_REM2CUR BUTTON_LEFT
208 # define SOL_CUR2STACK_PRE (BUTTON_RIGHT | BUTTON_REPEAT)
209 # define SOL_CUR2STACK BUTTON_RIGHT
210 # define SOL_REM2STACK (BUTTON_LEFT|BUTTON_REPEAT)
211 # define SOL_REM2STACK_PRE BUTTON_LEFT
212 # define HK_MOVE "SHORT SELECT"
213 # define HK_DRAW "SHORT HOME"
214 # define HK_REM2CUR "LEFT"
215 # define HK_CUR2STACK "DOUBLE SELECT"
216 # define HK_REM2STACK "LEFT"
218 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
219 # define SOL_QUIT BUTTON_POWER
220 # define SOL_UP BUTTON_UP
221 # define SOL_DOWN BUTTON_DOWN
222 # define SOL_LEFT BUTTON_LEFT
223 # define SOL_RIGHT BUTTON_RIGHT
224 # define SOL_MOVE_PRE BUTTON_SELECT
225 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
226 # define SOL_DRAW BUTTON_VOL_DOWN
227 # define SOL_REM2CUR BUTTON_REC
228 # define SOL_CUR2STACK_PRE BUTTON_SELECT
229 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
230 # define SOL_REM2STACK BUTTON_VOL_UP
231 # define HK_MOVE "SELECT"
232 # define HK_DRAW "REC"
233 # define HK_REM2CUR "LEFT"
234 # define HK_CUR2STACK "DOUBLE SELECT"
235 # define HK_REM2STACK "RIGHT"
237 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
238 # define SOL_QUIT BUTTON_POWER
239 # define SOL_UP BUTTON_UP
240 # define SOL_DOWN BUTTON_DOWN
241 # define SOL_LEFT BUTTON_LEFT
242 # define SOL_RIGHT BUTTON_RIGHT
243 # define SOL_MOVE_PRE BUTTON_SELECT
244 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
245 # define SOL_DRAW BUTTON_HOME
246 # define SOL_REM2CUR BUTTON_VOL_DOWN
247 # define SOL_CUR2STACK_PRE BUTTON_SELECT
248 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
249 # define SOL_REM2STACK BUTTON_VOL_UP
250 # define HK_MOVE "SELECT"
251 # define HK_DRAW "HOME"
252 # define HK_REM2CUR "LEFT"
253 # define HK_CUR2STACK "DOUBLE SELECT"
254 # define HK_REM2STACK "RIGHT"
256 #elif CONFIG_KEYPAD == SANSA_M200_PAD
257 # define SOL_QUIT BUTTON_POWER
258 # define SOL_UP BUTTON_UP
259 # define SOL_DOWN BUTTON_DOWN
260 # define SOL_LEFT BUTTON_LEFT
261 # define SOL_RIGHT BUTTON_RIGHT
262 # define SOL_MOVE_PRE BUTTON_SELECT
263 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
264 # define SOL_DRAW (BUTTON_SELECT | BUTTON_UP)
265 # define SOL_REM2CUR BUTTON_VOL_DOWN
266 # define SOL_CUR2STACK_PRE BUTTON_SELECT
267 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
268 # define SOL_REM2STACK BUTTON_VOL_UP
269 # define HK_MOVE "SELECT"
270 # define HK_DRAW "SELECT + UP"
271 # define HK_REM2CUR "LEFT"
272 # define HK_CUR2STACK "DOUBLE SELECT"
273 # define HK_REM2STACK "RIGHT"
275 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
276 # define SOL_QUIT BUTTON_POWER
277 # define SOL_UP BUTTON_SCROLL_UP
278 # define SOL_DOWN BUTTON_SCROLL_DOWN
279 # define SOL_LEFT_PRE BUTTON_LEFT
280 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
281 # define SOL_RIGHT_PRE BUTTON_RIGHT
282 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
283 # define SOL_MOVE BUTTON_PLAY
284 # define SOL_DRAW_PRE BUTTON_LEFT
285 # define SOL_DRAW (BUTTON_LEFT | BUTTON_REPEAT)
286 # define SOL_REM2CUR BUTTON_FF
287 # define SOL_CUR2STACK BUTTON_REW
288 # define SOL_REM2STACK_PRE BUTTON_RIGHT
289 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
290 # define HK_MOVE "PLAY"
291 # define HK_DRAW "LEFT.."
292 # define HK_REM2CUR "FF"
293 # define HK_CUR2STACK "REW"
294 # define HK_REM2STACK "RIGHT.."
296 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
297 # define SOL_QUIT BUTTON_BACK
298 # define SOL_UP BUTTON_UP
299 # define SOL_DOWN BUTTON_DOWN
300 # define SOL_LEFT BUTTON_LEFT
301 # define SOL_RIGHT BUTTON_RIGHT
302 # define SOL_MOVE_PRE BUTTON_SELECT
303 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
304 # define SOL_DRAW BUTTON_MENU
305 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_SELECT)
306 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
307 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_SELECT)
308 # define HK_MOVE "SELECT"
309 # define HK_DRAW "MENU"
310 # define HK_REM2CUR "SELECT+LEFT"
311 # define HK_CUR2STACK "SELECT.."
312 # define HK_REM2STACK "SELECT+RIGHT"
314 #elif (CONFIG_KEYPAD == MROBE100_PAD)
315 # define SOL_QUIT BUTTON_POWER
316 # define SOL_UP BUTTON_UP
317 # define SOL_DOWN BUTTON_DOWN
318 # define SOL_LEFT BUTTON_LEFT
319 # define SOL_RIGHT BUTTON_RIGHT
320 # define SOL_MOVE_PRE BUTTON_SELECT
321 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
322 # define SOL_DRAW BUTTON_MENU
323 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_DISPLAY)
324 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
325 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_DISPLAY)
326 # define HK_MOVE "SELECT"
327 # define HK_DRAW "MENU"
328 # define HK_REM2CUR "DISPLAY+LEFT"
329 # define HK_CUR2STACK "SELECT.."
330 # define HK_REM2STACK "DISPLAY+RIGHT"
332 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
333 # define SOL_QUIT BUTTON_RC_REC
334 # define SOL_UP BUTTON_RC_VOL_UP
335 # define SOL_DOWN BUTTON_RC_VOL_DOWN
336 # define SOL_LEFT BUTTON_RC_REW
337 # define SOL_RIGHT BUTTON_RC_FF
338 # define SOL_MOVE BUTTON_RC_PLAY
339 # define SOL_DRAW_PRE BUTTON_RC_MENU
340 # define SOL_DRAW (BUTTON_RC_MENU | BUTTON_REL)
341 # define SOL_REM2CUR_PRE BUTTON_RC_MENU
342 # define SOL_REM2CUR (BUTTON_RC_MENU | BUTTON_REPEAT)
343 # define SOL_CUR2STACK_PRE BUTTON_RC_MODE
344 # define SOL_CUR2STACK (BUTTON_RC_MODE | BUTTON_REL)
345 # define SOL_REM2STACK_PRE BUTTON_RC_MODE
346 # define SOL_REM2STACK (BUTTON_RC_MODE | BUTTON_REPEAT)
347 # define HK_LR "REW/FF"
348 # define HK_UD "VOL UP/DOWN"
349 # define HK_MOVE "PLAY"
350 # define HK_DRAW "MENU"
351 # define HK_REM2CUR "MENU.."
352 # define HK_CUR2STACK "MODE"
353 # define HK_REM2STACK "MODE.."
355 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
356 # define SOL_QUIT BUTTON_POWER
358 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
359 # define SOL_QUIT BUTTON_BACK
360 # define SOL_UP BUTTON_UP
361 # define SOL_DOWN BUTTON_DOWN
362 # define SOL_LEFT BUTTON_LEFT
363 # define SOL_RIGHT BUTTON_RIGHT
364 # define SOL_MOVE_PRE BUTTON_SELECT
365 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
366 # define SOL_DRAW BUTTON_MENU
367 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_SELECT)
368 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
369 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_SELECT)
370 # define HK_MOVE "MIDDLE"
371 # define HK_DRAW "MENU"
372 # define HK_REM2CUR "PLAY+LEFT"
373 # define HK_CUR2STACK "PLAY.."
374 # define HK_REM2STACK "PLAY+RIGHT"
376 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
377 # define SOL_QUIT BUTTON_POWER
378 # define SOL_UP BUTTON_UP
379 # define SOL_DOWN BUTTON_DOWN
380 # define SOL_LEFT BUTTON_LEFT
381 # define SOL_RIGHT BUTTON_RIGHT
382 # define SOL_MOVE_PRE BUTTON_SELECT
383 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
384 # define SOL_DRAW BUTTON_MENU
385 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_VIEW)
386 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
387 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_VIEW)
388 # define HK_MOVE "SELECT"
389 # define HK_DRAW "MENU"
390 # define HK_REM2CUR "VIEW+LEFT"
391 # define HK_CUR2STACK "SELECT.."
392 # define HK_REM2STACK "VIEW+RIGHT"
394 #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
395 # define SOL_QUIT BUTTON_POWER
396 # define SOL_UP BUTTON_UP
397 # define SOL_DOWN BUTTON_DOWN
398 # define SOL_LEFT BUTTON_PREV
399 # define SOL_RIGHT BUTTON_NEXT
400 # define SOL_MOVE_PRE BUTTON_PLAY
401 # define SOL_MOVE (BUTTON_PLAY | BUTTON_REL)
402 # define SOL_DRAW BUTTON_MENU
403 # define SOL_REM2CUR BUTTON_LEFT
404 # define SOL_CUR2STACK (BUTTON_PLAY | BUTTON_REPEAT)
405 # define SOL_REM2STACK BUTTON_RIGHT
406 # define HK_MOVE "PLAY"
407 # define HK_DRAW "MENU"
408 # define HK_REM2CUR "LEFT"
409 # define HK_CUR2STACK "PLAY.."
410 # define HK_REM2STACK "RIGHT"
412 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
413 # define SOL_QUIT BUTTON_POWER
414 # define SOL_UP BUTTON_UP
415 # define SOL_DOWN BUTTON_DOWN
416 # define SOL_LEFT BUTTON_PREV
417 # define SOL_RIGHT BUTTON_NEXT
418 # define SOL_MOVE_PRE BUTTON_PLAY
419 # define SOL_MOVE (BUTTON_PLAY | BUTTON_REL)
420 # define SOL_DRAW BUTTON_MENU
421 # define SOL_REM2CUR BUTTON_LEFT
422 # define SOL_CUR2STACK (BUTTON_PLAY | BUTTON_REPEAT)
423 # define SOL_REM2STACK BUTTON_RIGHT
424 # define HK_MOVE "PLAY"
425 # define HK_DRAW "MENU"
426 # define HK_REM2CUR "LEFT"
427 # define HK_CUR2STACK "PLAY..."
428 # define HK_REM2STACK "RIGHT"
430 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
431 (CONFIG_KEYPAD == ONDAVX777_PAD) || \
432 CONFIG_KEYPAD == MROBE500_PAD
433 # define SOL_QUIT BUTTON_POWER
435 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
436 # define SOL_QUIT BUTTON_REC
437 # define SOL_UP BUTTON_UP
438 # define SOL_DOWN BUTTON_DOWN
439 # define SOL_LEFT_PRE BUTTON_LEFT
440 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REW)
441 # define SOL_RIGHT_PRE BUTTON_RIGHT
442 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REW)
443 # define SOL_MOVE BUTTON_PLAY
444 # define SOL_DRAW_PRE (BUTTON_PLAY | BUTTON_RIGHT)
445 # define SOL_DRAW (BUTTON_PLAY | BUTTON_LEFT)
446 # define SOL_REM2CUR BUTTON_FFWD
447 # define SOL_CUR2STACK BUTTON_REW
448 # define SOL_REM2STACK_PRE (BUTTON_FFWD | BUTTON_RIGHT)
449 # define SOL_REM2STACK (BUTTON_FFWD | BUTTON_LEFT)
450 # define HK_MOVE "PLAY"
451 # define HK_DRAW "REW.."
452 # define HK_REM2CUR "FFWD"
453 # define HK_CUR2STACK "REW"
454 # define HK_REM2STACK "FFWD.."
456 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
457 # define SOL_QUIT BUTTON_REC
458 # define SOL_UP BUTTON_UP
459 # define SOL_DOWN BUTTON_DOWN
460 # define SOL_LEFT BUTTON_PREV
461 # define SOL_RIGHT BUTTON_NEXT
462 # define SOL_MOVE_PRE BUTTON_OK
463 # define SOL_MOVE (BUTTON_OK | BUTTON_REL)
464 # define SOL_DRAW BUTTON_MENU
465 # define SOL_REM2CUR BUTTON_CANCEL
466 # define SOL_CUR2STACK BUTTON_PLAY
467 # define SOL_REM2STACK (BUTTON_PLAY | BUTTON_REPEAT)
468 # define HK_MOVE "OK"
469 # define HK_DRAW "MENU"
470 # define HK_REM2CUR "CANCEL"
471 # define HK_CUR2STACK "PLAY"
472 # define HK_REM2STACK "PLAY..."
474 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
475 # define SOL_QUIT (BUTTON_REC | BUTTON_PLAY)
476 # define SOL_UP BUTTON_REW
477 # define SOL_DOWN BUTTON_FF
478 # define SOL_LEFT BUTTON_VOL_DOWN
479 # define SOL_RIGHT BUTTON_VOL_UP
480 # define SOL_MOVE_PRE BUTTON_FUNC
481 # define SOL_MOVE (BUTTON_FUNC | BUTTON_REL)
482 # define SOL_DRAW BUTTON_REC
483 # define SOL_REM2CUR (BUTTON_REC | BUTTON_REPEAT)
484 # define SOL_CUR2STACK BUTTON_PLAY
485 # define SOL_REM2STACK (BUTTON_PLAY | BUTTON_REPEAT)
486 # define HK_MOVE "FUNC"
487 # define HK_DRAW "REC"
488 # define HK_REM2CUR "REC.."
489 # define HK_CUR2STACK "PLAY"
490 # define HK_REM2STACK "PLAY...."
492 #else
493 #error No keymap defined!
494 #endif
496 #ifdef HAVE_TOUCHSCREEN
497 //#ifndef SOL_QUIT
498 //# define SOL_QUIT BUTTON_TOPLEFT
499 //endif
500 #ifndef SOL_UP
501 # define SOL_UP BUTTON_TOPMIDDLE
502 #endif
503 #ifndef SOL_DOWN
504 # define SOL_DOWN BUTTON_BOTTOMMIDDLE
505 #endif
506 #ifndef SOL_LEFT
507 # define SOL_LEFT BUTTON_MIDLEFT
508 #endif
509 #ifndef SOL_RIGHT
510 # define SOL_RIGHT BUTTON_MIDRIGHT
511 #endif
512 #ifndef SOL_MOVE
513 # define SOL_MOVE BUTTON_CENTER
514 # define HK_MOVE "CENTRE"
515 #endif
516 #ifndef SOL_DRAW
517 # define SOL_DRAW BUTTON_TOPLEFT
518 # define HK_DRAW "TOPLEFT"
519 #endif
520 #ifndef SOL_REM2CUR
521 # define SOL_REM2CUR BUTTON_TOPRIGHT
522 # define HK_REM2CUR "TOPRIGHT"
523 #endif
524 #ifndef SOL_CUR2STACK
525 # define SOL_CUR2STACK BUTTON_BOTTOMLEFT
526 # define HK_CUR2STACK "BOTTOMLEFT"
527 #endif
528 #ifndef SOL_REM2STACK
529 # define SOL_REM2STACK BUTTON_BOTTOMRIGHT
530 # define HK_REM2STACK "BOTTOMRIGHT"
531 #endif
533 #endif
535 #ifndef HK_LR
536 # define HK_LR "LEFT/RIGHT"
537 #endif
538 #ifndef HK_UD
539 # define HK_UD "UP/DOWN"
540 #endif
543 * Misc constants, graphics and other defines
546 #include "pluginbitmaps/card_back.h"
547 #include "pluginbitmaps/card_deck.h"
548 #include "pluginbitmaps/solitaire_suitsi.h"
550 #define CARD_GFX_WIDTH BMPWIDTH_card_back
551 #define CARD_GFX_HEIGHT BMPHEIGHT_card_back
552 #define CARD_WIDTH (BMPWIDTH_card_back+2)
553 #define CARD_HEIGHT (BMPHEIGHT_card_back+2)
555 #if LCD_WIDTH >= 640
556 # define MARGIN 4
557 # define LARGE_CARD
558 # define SYMBOL_HEIGHT 24
560 #elif LCD_WIDTH >= 320
561 # define MARGIN 4
562 # define LARGE_CARD
563 # define SYMBOL_HEIGHT 12
564 #elif LCD_WIDTH >= 220
565 # define MARGIN 3
566 # define LARGE_CARD
567 # define SYMBOL_HEIGHT 12
568 #elif LCD_WIDTH >= 160
569 # define MARGIN 2
570 # define SYMBOL_HEIGHT 11
571 #elif LCD_WIDTH >= 128
572 # define MARGIN 1
573 # define SYMBOL_HEIGHT 10
574 #else
575 # define MARGIN 0
576 # define SYMBOL_HEIGHT 8
577 #endif
579 #define CARD_START (CARD_HEIGHT+2*MARGIN+1)
581 /* background color */
582 #ifdef HAVE_LCD_COLOR
583 # define BACKGROUND_COLOR LCD_RGBPACK(0,157,0)
584 # define FRAME_COLOR LCD_RGBPACK(23,119,218)
585 #elif LCD_DEPTH > 1
586 # define BACKGROUND_COLOR LCD_WHITE
587 # define FRAME_COLOR LCD_BLACK
588 #endif
591 #define CONFIG_FILENAME "sol.cfg"
593 #define NOT_A_CARD -1
595 /* number of cards per suit */
596 #define CARDS_PER_SUIT 13
598 /* number of suits */
599 #define SUITS 4
601 #define NUM_CARDS ( CARDS_PER_SUIT * SUITS )
603 /* number of columns */
604 #define COL_NUM 7
606 /* pseudo column numbers to be used for cursor coordinates */
607 /* columns COL_NUM to COL_NUM + SUITS - 1 correspond to the final stacks */
608 #define STACKS_COL COL_NUM
609 /* column COL_NUM + SUITS corresponds to the remains' stack */
610 #define REM_COL (STACKS_COL + SUITS)
612 #define NOT_A_COL -1
614 typedef struct
616 signed char suit;
617 signed char num;
618 bool known : 1;
619 bool used : 1; /* this is what is used when dealing cards */
620 signed char next;
621 } card_t;
625 * LCD card drawing routines
628 static void draw_cursor( int x, int y )
630 rb->lcd_set_drawmode( DRMODE_COMPLEMENT );
631 rb->lcd_fillrect( x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
632 #ifdef LARGE_CARD
633 rb->lcd_drawpixel( x+1, y+1 );
634 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
635 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
636 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
637 #endif
638 rb->lcd_set_drawmode( DRMODE_SOLID );
641 /* Draw a card's border, select it if it's selected and draw the cursor
642 * if the cursor is currently over the card */
643 static void draw_card_ext( int x, int y, bool selected, bool cursor )
645 #if LCD_DEPTH > 1
646 int oldfg = rb->lcd_get_foreground();
648 rb->lcd_set_foreground( LCD_BLACK );
649 #endif
650 #ifdef LARGE_CARD
651 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y );
652 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y+CARD_HEIGHT-1 );
653 rb->lcd_vline( x, y+2, y+CARD_HEIGHT-3 );
654 rb->lcd_vline( x+CARD_WIDTH-1, y+2, y+CARD_HEIGHT-3 );
655 rb->lcd_drawpixel( x+1, y+1 );
656 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
657 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
658 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
659 #else
660 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y );
661 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y+CARD_HEIGHT-1 );
662 rb->lcd_vline( x, y+1, y+CARD_HEIGHT-2 );
663 rb->lcd_vline( x+CARD_WIDTH-1, y+1, y+CARD_HEIGHT-2 );
664 #endif
666 if( selected )
668 #if LCD_DEPTH > 1
669 rb->lcd_set_foreground( FRAME_COLOR );
670 #endif
671 rb->lcd_drawrect( x+1, y+1, CARD_WIDTH-2, CARD_HEIGHT-2 );
672 #ifdef LARGE_CARD
673 rb->lcd_drawrect( x+2, y+2, CARD_WIDTH-4, CARD_HEIGHT-4 );
674 #endif
676 #if LCD_DEPTH > 1
677 rb->lcd_set_foreground( oldfg );
678 #endif
680 if( cursor )
682 draw_cursor( x, y );
686 /* Draw a card's inner graphics */
687 static void draw_card( card_t *card, int x, int y,
688 bool selected, bool cursor )
690 if( card->known )
692 rb->lcd_bitmap_part( card_deck, CARD_GFX_WIDTH * card->num,
693 CARD_GFX_HEIGHT * card->suit,
694 STRIDE(SCREEN_MAIN,
695 BMPWIDTH_card_deck, BMPHEIGHT_card_deck),
696 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
698 else
700 rb->lcd_bitmap( card_back, x+1, y+1,
701 CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
703 draw_card_ext( x, y, selected, cursor );
706 /* Draw an empty stack */
707 static void draw_empty_stack( int s, int x, int y, bool cursor )
709 rb->lcd_bitmap_part( solitaire_suitsi, 0,
710 CARD_GFX_HEIGHT * s,
711 STRIDE( SCREEN_MAIN,
712 BMPWIDTH_solitaire_suitsi, BMPHEIGHT_solitaire_suitsi),
713 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
715 draw_card_ext( x, y, false, cursor );
718 /* Help */
719 static bool solitaire_help( void )
721 static char* help_text[] = {
722 "Solitaire", "", "Controls", "",
723 HK_LR ":", "Move", "the", "cursor", "to", "the",
724 "previous/", "next", "column.", "",
725 HK_UD ":", "Move", "the", "cursor", "up/", "down", "in", "the",
726 "column.", "",
727 HK_MOVE ":", "Select", "cards,", "move", "cards...", "",
728 HK_DRAW ":", "Deselect", "a", "card", "if", "it", "was", "selected.",
729 "Else", "draw", "new", "card(s)", "from", "the", "remains",
730 "stack.", "", "",
731 "Shortcuts", "",
732 HK_REM2CUR ":", "Put", "the", "card", "on", "top", "of", "the",
733 "remains", "stack", "on", "top", "of", "the", "cursor.", "",
734 HK_CUR2STACK ":", "Put", "the", "card", "under", "the", "cursor",
735 "on", "one", "of", "the", "4", "final", "stacks.", "",
736 HK_REM2STACK ":", "Put", "the", "card", "on", "top", "of", "the",
737 "remains", "stack", "on", "one", "of", "the", "4", "final",
738 "stacks."
740 static struct style_text formation[]={
741 { 0, TEXT_CENTER|TEXT_UNDERLINE },
742 { 2, C_RED },
743 { 48, C_RED },
744 LAST_STYLE_ITEM
746 if (display_text(ARRAYLEN(help_text), help_text, formation, NULL, true))
747 return true;
748 return false;
752 * Custom menu / options
755 #define CFGFILE_VERSION 0
757 struct sol_config {
758 int draw_type;
761 struct sol_config sol_disk = {0};
762 struct sol_config sol;
764 static struct configdata config[] = {
765 { TYPE_INT, 0, 1, { .int_p = &sol_disk.draw_type }, "draw_type", NULL }
768 static const struct opt_items drawcards[2] = {
769 { "Draw Three Cards", -1 },
770 { "Draw One Card", -1 },
773 void solitaire_init(void);
775 /* menu return codes */
776 enum { MENU_RESUME, MENU_SAVE_AND_QUIT, MENU_QUIT, MENU_USB };
778 static bool _ingame;
779 int solitaire_menu_cb(int action, const struct menu_item_ex *this_item)
781 int i = (intptr_t)this_item;
782 if( action == ACTION_REQUEST_MENUITEM )
784 if((!_ingame && (i==0 || i==5)) || ( _ingame && i==2 ))
785 return ACTION_EXIT_MENUITEM;
787 return action;
790 int solitaire_menu(bool in_game)
792 int selected = 0;
793 int result = -1;
795 MENUITEM_STRINGLIST(menu, "Solitaire Menu", solitaire_menu_cb,
796 "Resume Game", "Start New Game",
797 "Draw Cards Option",
798 "Help", "Playback Control",
799 "Save and Quit", "Quit");
800 _ingame = in_game;
802 while (result < 0)
804 switch (rb->do_menu(&menu, &selected, NULL, false))
806 default:
807 result = MENU_RESUME;
808 break;
810 case MENU_ATTACHED_USB:
811 result = MENU_USB;
812 break;
814 case 0:
815 result = MENU_RESUME;
816 break;
818 case 1:
819 solitaire_init();
820 result = MENU_RESUME;
821 break;
823 case 2:
824 if (rb->set_option("Draw Cards Option", &sol.draw_type,
825 INT, drawcards, 2, NULL))
826 result = MENU_USB;
827 break;
829 case 3:
830 if (solitaire_help())
831 result = MENU_USB;
832 break;
834 case 4:
835 playback_control(NULL);
836 break;
838 case 5:
839 result = MENU_SAVE_AND_QUIT;
840 break;
842 case 6:
843 result = MENU_QUIT;
844 break;
847 return result;
851 * Global variables
854 /* player's cursor */
855 int cur_card;
856 /* player's cursor column num */
857 int cur_col;
859 /* selected card */
860 int sel_card;
862 /* the deck */
863 card_t deck[ NUM_CARDS ];
865 /* the remaining cards */
866 /* first card of the remains' stack */
867 int rem;
868 /* upper visible card from the remains' stack */
869 int cur_rem;
870 /* number of cards drawn from the remains stack - 1 */
871 int count_rem;
872 /* number of cards per draw of the remains' stack */
873 int cards_per_draw;
875 /* the 7 game columns */
876 int cols[COL_NUM];
877 /* the 4 final stacks */
878 int stacks[SUITS];
881 * Card handling routines
884 int next_random_card( card_t *deck )
886 int i,r;
888 r = rb->rand()%(NUM_CARDS)+1;
889 i = 0;
891 while( r>0 )
893 i = (i + 1)%(NUM_CARDS);
894 if( !deck[i].used ) r--;
897 deck[i].used = true;
899 return i;
903 /* initialize the game */
904 void solitaire_init( void )
907 int c;
908 int i, j;
910 /* number of cards that are drawn on the remains' stack (by pressing F2) */
911 if( sol.draw_type == 0 )
913 cards_per_draw = 3;
915 else
917 cards_per_draw = 1;
920 /* init deck */
921 for( i=0; i<SUITS; i++ )
923 for( j=0; j<CARDS_PER_SUIT; j++ )
925 #define card deck[i*CARDS_PER_SUIT+j]
926 card.suit = i;
927 card.num = j;
928 card.known = true;
929 card.used = false;
930 card.next = NOT_A_CARD;
931 #undef card
935 /* deal the cards ... */
936 /* ... in the columns */
937 for( i=0; i<COL_NUM; i++ )
939 c = NOT_A_CARD;
940 for( j=0; j<=i; j++ )
942 if( c == NOT_A_CARD )
944 cols[i] = next_random_card( deck );
945 c = cols[i];
947 else
949 deck[c].next = next_random_card( deck );
950 c = deck[c].next;
952 if( j < i )
953 deck[c].known = false;
957 /* ... shuffle what's left of the deck */
958 rem = next_random_card(deck);
959 c = rem;
961 for( i=1; i < NUM_CARDS - COL_NUM * (COL_NUM + 1)/2; i++ )
963 deck[c].next = next_random_card( deck );
964 c = deck[c].next;
967 /* we now finished dealing the cards. The game can start ! (at last) */
969 /* init the stack */
970 for( i = 0; i<SUITS; i++ )
972 stacks[i] = NOT_A_CARD;
975 /* the cursor starts on upper left card */
976 cur_card = cols[0];
977 cur_col = 0;
979 /* no card is selected */
980 sel_card = NOT_A_CARD;
982 /* init the remainder */
983 cur_rem = NOT_A_CARD;
985 count_rem = -1;
988 /* find the column number in which 'card' can be found */
989 int find_card_col( int card )
991 int i;
992 int c;
994 if( card == NOT_A_CARD ) return NOT_A_COL;
996 for( i=0; i<COL_NUM; i++ )
998 c = cols[i];
999 while( c != NOT_A_CARD )
1001 if( c == card ) return i;
1002 c = deck[c].next;
1006 for( i=0; i<SUITS; i++ )
1008 c = stacks[i];
1009 while( c != NOT_A_CARD )
1011 if( c == card ) return STACKS_COL + i;
1012 c = deck[c].next;
1016 return REM_COL;
1019 /* find the card preceding 'card' */
1020 /* if it doesn't exist, return NOT_A_CARD */
1021 int find_prev_card( int card ){
1022 int i;
1024 for( i=0; i < NUM_CARDS; i++ )
1026 if( deck[i].next == card ) return i;
1029 return NOT_A_CARD;
1032 /* find the last card of a given column */
1033 int find_last_card( int col )
1035 int c;
1037 if( col < COL_NUM )
1039 c = cols[col];
1041 else if( col < REM_COL )
1043 c = stacks[col - STACKS_COL];
1045 else
1047 c = cur_rem;
1050 if(c == NOT_A_CARD)
1051 return c;
1052 else
1054 while(deck[c].next != NOT_A_CARD)
1055 c = deck[c].next;
1056 return c;
1060 enum move { MOVE_OK, MOVE_NOT_OK };
1062 enum move move_card( int dest_col, int src_card )
1064 /* the column on which to take src_card */
1065 int src_col;
1067 /* the last card of dest_col */
1068 int dest_card;
1070 /* the card under src_card */
1071 int src_card_prev;
1073 /* you can't move no card (at least, it doesn't have any consequence) */
1074 if( src_card == NOT_A_CARD ) return MOVE_NOT_OK;
1075 /* you can't put a card back on the remains' stack */
1076 if( dest_col == REM_COL ) return MOVE_NOT_OK;
1077 /* you can't move an unknown card */
1078 if( !deck[src_card].known ) return MOVE_NOT_OK;
1080 src_col = find_card_col( src_card );
1081 dest_card = find_last_card( dest_col );
1082 src_card_prev = find_prev_card( src_card );
1084 /* you can't move more than one card at a time from the final stack */
1085 /* to the rest of the game */
1086 if( src_col >= COL_NUM && src_col < REM_COL
1087 && deck[src_card].next != NOT_A_CARD )
1089 return MOVE_NOT_OK;
1092 /* if we (that means dest) are on one of the 7 columns ... */
1093 if( dest_col < COL_NUM )
1095 /* ... check is we are on an empty color and that the src is a king */
1096 if( dest_card == NOT_A_CARD
1097 && deck[src_card].num == CARDS_PER_SUIT - 1 )
1099 /* this is a winning combination */
1100 cols[dest_col] = src_card;
1102 /* ... or check if the cards follow one another and have
1103 * different colorsuit */
1104 else if(( deck[dest_card].suit + deck[src_card].suit)%2==1
1105 && deck[dest_card].num == deck[src_card].num + 1 )
1107 /* this is a winning combination */
1108 deck[dest_card].next = src_card;
1110 /* ... or, humpf, well that's not good news */
1111 else
1113 /* this is not a winning combination */
1114 return MOVE_NOT_OK;
1117 /* if we are on one of the 4 final stacks ... */
1118 else if( dest_col < REM_COL )
1120 /* ... check if we are on an empty stack... */
1121 if( dest_card == NOT_A_CARD )
1123 /* ... and the src is an ace and this is the correct final stack */
1124 if( deck[src_card].num == 0
1125 && deck[src_card].suit == dest_col - STACKS_COL )
1127 /* this is a winning combination */
1128 stacks[dest_col - STACKS_COL] = src_card;
1130 else
1132 /* this is not a winning combination */
1133 return MOVE_NOT_OK;
1136 else /* non-empty stack */
1138 /* ...check if the cards follow one another, have the same suit and
1139 * {that src has no .next element or is from the remains' stack} */
1140 if( deck[dest_card].suit == deck[src_card].suit
1141 && deck[dest_card].num + 1 == deck[src_card].num
1142 && (deck[src_card].next == NOT_A_CARD || src_col == REM_COL) )
1144 /* this is a winning combination */
1145 deck[dest_card].next = src_card;
1147 /* ... or, well that's not good news */
1148 else
1150 /* this is not a winning combination */
1151 return MOVE_NOT_OK;
1155 /* if we are on the remains' stack */
1156 else
1158 /* you can't move a card back to the remains' stack */
1159 return MOVE_NOT_OK;
1162 /* if the src card is from the remains' stack, we don't want to take
1163 * the following cards */
1164 if( src_col == REM_COL )
1166 /* if src card is the first card from the stack */
1167 if( src_card_prev == NOT_A_CARD )
1169 rem = deck[src_card].next;
1171 /* if src card is not the first card from the stack */
1172 else
1174 deck[src_card_prev].next = deck[src_card].next;
1176 deck[src_card].next = NOT_A_CARD;
1177 cur_rem = src_card_prev;
1178 count_rem--;
1180 /* if the src card is from somewhere else, just take everything */
1181 else
1183 if( src_card_prev == NOT_A_CARD )
1185 if( src_col < COL_NUM )
1187 cols[src_col] = NOT_A_CARD;
1189 else
1191 stacks[src_col - STACKS_COL] = NOT_A_CARD;
1194 else
1196 deck[src_card_prev].next = NOT_A_CARD;
1197 deck[src_card_prev].known = true;
1200 sel_card = NOT_A_CARD;
1201 /* tada ! */
1202 return MOVE_OK;
1205 enum { SOLITAIRE_WIN, SOLITAIRE_SAVE_AND_QUIT, SOLITAIRE_QUIT, SOLITAIRE_USB };
1208 * Bouncing cards at the end of the game
1211 #define BC_ACCEL ((1<<16)*LCD_HEIGHT/128)
1212 #define BC_MYSPEED (6*BC_ACCEL)
1213 #define BC_MXSPEED (6*LCD_HEIGHT/128)
1215 int bouncing_cards( void )
1217 int i, j, x, vx, y, fp_y, fp_vy, button;
1219 /* flush the button queue */
1220 while( ( button = rb->button_get( false ) ) != BUTTON_NONE )
1222 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1223 return SOLITAIRE_USB;
1226 /* fun stuff :) */
1227 for( i = CARDS_PER_SUIT-1; i>=0; i-- )
1229 for( j = 0; j < SUITS; j++ )
1231 x = LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*j+j+1;
1232 fp_y = MARGIN<<16;
1234 #if LCD_WIDTH > 200
1235 vx = rb->rand() % (4*BC_MXSPEED/3-2) - BC_MXSPEED;
1236 if( vx >= -1 )
1237 vx += 3;
1238 #else
1239 vx = rb->rand() % (4*BC_MXSPEED/3) - BC_MXSPEED;
1240 if( vx >= 0 )
1241 vx++;
1242 #endif
1244 fp_vy = -rb->rand() % BC_MYSPEED;
1246 while( x < LCD_WIDTH && x + CARD_WIDTH > 0 )
1248 fp_vy += BC_ACCEL;
1249 x += vx;
1250 fp_y += fp_vy;
1251 if( fp_y >= (LCD_HEIGHT-CARD_HEIGHT) << 16 )
1253 fp_vy = -fp_vy*4/5;
1254 fp_y = (LCD_HEIGHT-CARD_HEIGHT) << 16;
1256 y = fp_y >> 16;
1257 draw_card( &deck[j*CARDS_PER_SUIT+i], x, y,
1258 false, false );
1259 rb->lcd_update_rect( x<0?0:x, y<0?0:y,
1260 CARD_WIDTH, CARD_HEIGHT );
1262 button = rb->button_get_w_tmo( 2 );
1263 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1264 return SOLITAIRE_USB;
1265 if( button == SOL_QUIT || button == SOL_MOVE )
1266 return SOLITAIRE_WIN;
1270 return SOLITAIRE_WIN;
1274 * Game save/load routines
1276 void get_save_filename( char *buf )
1278 char *s;
1279 rb->strcpy( buf, rb->plugin_get_current_filename() );
1280 s = rb->strrchr( buf, '/' ) + 1;
1281 *s = '\0';
1282 rb->strcat( s, "sol.save" );
1285 int open_save_file( int flags )
1287 char buf[MAX_PATH];
1288 get_save_filename( buf );
1289 return rb->open( buf, flags, 0666);
1292 void delete_save_file( void )
1294 char buf[MAX_PATH];
1295 get_save_filename( buf );
1296 rb->remove( buf );
1299 #ifdef write
1300 # undef write
1301 #endif
1302 int save_write( int fd, const void *buf, size_t count, int *checksum )
1304 size_t i;
1305 if( rb->write( fd, buf, count ) < (ssize_t)count )
1306 return 1;
1307 for( i = 0; i < count; i++ )
1308 *checksum += (int)(((const char *)buf)[i]);
1309 return 0;
1312 #ifdef read
1313 # undef read
1314 #endif
1315 int save_read( int fd, void *buf, size_t count, int *checksum )
1317 size_t i;
1318 if( rb->read( fd, buf, count ) < (ssize_t)count )
1319 return 1;
1320 for( i = 0; i < count; i++ )
1321 *checksum -= (int)(((const char *)buf)[i]);
1322 return 0;
1325 int save_game( void )
1327 int fd = open_save_file( O_CREAT|O_WRONLY|O_TRUNC );
1328 int checksum = 42;
1329 if( fd < 0 )
1330 return -1;
1331 if( save_write( fd, &cur_card, sizeof( int ), &checksum )
1332 || save_write( fd, &cur_col, sizeof( int ), &checksum )
1333 || save_write( fd, &sel_card, sizeof( int ), &checksum )
1334 || save_write( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1335 || save_write( fd, &rem, sizeof( int ), &checksum )
1336 || save_write( fd, &cur_rem, sizeof( int ), &checksum )
1337 || save_write( fd, &count_rem, sizeof( int ), &checksum )
1338 || save_write( fd, &cards_per_draw, sizeof( int ), &checksum )
1339 || save_write( fd, cols, COL_NUM * sizeof( int ), &checksum )
1340 || save_write( fd, stacks, SUITS * sizeof( int ), &checksum )
1341 || ( rb->write( fd, &checksum, sizeof( int ) ) < (ssize_t)(sizeof( int ) ) ) )
1343 rb->close( fd );
1344 rb->splash( 2*HZ, "Error while saving game. Aborting." );
1345 return -2;
1347 rb->close( fd );
1348 return 0;
1351 int load_game( void )
1353 int checksum, retval;
1355 int fd = open_save_file( O_RDONLY );
1356 if( fd < 0 )
1357 return -1;
1359 retval = 0; /* Assume good case */
1360 if( ( rb->lseek( fd, -sizeof( int ), SEEK_END ) == -((ssize_t)sizeof( int ))-1 )
1361 || ( rb->read( fd, &checksum, sizeof( int ) ) < ((ssize_t)sizeof( int )) )
1362 || ( rb->lseek( fd, 0, SEEK_SET ) == -1 )
1363 || save_read( fd, &cur_card, sizeof( int ), &checksum )
1364 || save_read( fd, &cur_col, sizeof( int ), &checksum )
1365 || save_read( fd, &sel_card, sizeof( int ), &checksum )
1366 || save_read( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1367 || save_read( fd, &rem, sizeof( int ), &checksum )
1368 || save_read( fd, &cur_rem, sizeof( int ), &checksum )
1369 || save_read( fd, &count_rem, sizeof( int ), &checksum )
1370 || save_read( fd, &cards_per_draw, sizeof( int ), &checksum )
1371 || save_read( fd, cols, COL_NUM * sizeof( int ), &checksum )
1372 || save_read( fd, stacks, SUITS * sizeof( int ), &checksum ) )
1374 rb->splash( 2*HZ, "Error while loading saved game. Aborting." );
1375 retval = -2;
1377 else if( checksum != 42 )
1379 rb->splash( 2*HZ, "Save file was corrupted. Aborting." );
1380 retval = -3;
1383 rb->close( fd );
1384 delete_save_file();
1385 return retval;
1389 * The main game loop
1391 * If skipmenu is defined to SOLITAIRE_QUIT, the menu will be skipped and
1392 * game will resume.
1395 int solitaire( int skipmenu )
1398 int i,j;
1399 int button, lastbutton = 0;
1400 int c,h,prevcard;
1401 int biggest_col_length;
1403 rb->srand( *rb->current_tick );
1404 if( skipmenu != SOLITAIRE_QUIT )
1406 switch( solitaire_menu(false) )
1408 case MENU_QUIT:
1409 return SOLITAIRE_QUIT;
1411 case MENU_USB:
1412 return SOLITAIRE_USB;
1414 solitaire_init();
1417 while( true )
1419 rb->lcd_clear_display();
1421 /* get the biggest column length so that display can be "optimized" */
1422 biggest_col_length = 0;
1424 for(i=0;i<COL_NUM;i++)
1426 j = 0;
1427 c = cols[i];
1429 if( c != NOT_A_CARD )
1431 while( true )
1433 /* don't include the last card in the column length. */
1434 if( deck[c].next == NOT_A_CARD )
1436 break; /* no successor: get outta here. */
1438 else
1440 if( deck[c].known )
1441 j += 2;
1442 else
1443 j++;
1445 c = deck[c].next;
1447 /* make column distinguishable from an empty column,
1448 * and avoid division by zero while displaying */
1449 if( j == 0 )
1450 j = 1;
1452 if( j > biggest_col_length )
1453 biggest_col_length = j;
1456 /* check if there are cards remaining in the game. */
1457 /* if there aren't any, that means you won :) */
1458 if( biggest_col_length == 0 && rem == NOT_A_CARD )
1460 rb->lcd_update();
1461 rb->splash( HZ, "You Won :)" );
1462 return bouncing_cards();
1465 /* draw the columns */
1466 for( i = 0; i < COL_NUM; i++ )
1468 c = cols[i];
1469 j = CARD_START;
1470 while( true )
1472 if( c == NOT_A_CARD )
1474 /* draw the cursor on empty columns */
1475 if( cur_col == i )
1477 draw_cursor( MARGIN + i * (CARD_WIDTH
1478 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1479 j );
1481 break;
1484 draw_card( &deck[c], MARGIN + i * (CARD_WIDTH
1485 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1486 j, c == sel_card, c == cur_card );
1488 h = c;
1489 c = deck[c].next;
1490 if( c == NOT_A_CARD ) break;
1492 /* This is where we change the spacing between cards so that
1493 * they don't overflow out of the LCD */
1494 if( h == cur_card )
1495 j += SYMBOL_HEIGHT;
1496 else if( deck[h].known )
1497 j += min( SYMBOL_HEIGHT,
1498 2 * (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1499 / biggest_col_length );
1500 else
1501 j += min( SYMBOL_HEIGHT / 2,
1502 (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1503 / biggest_col_length );
1507 /* draw the stacks */
1508 for( i=0; i<SUITS; i++ )
1510 c = find_last_card( STACKS_COL + i );
1512 if( c != NOT_A_CARD )
1514 draw_card( &deck[c],
1515 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1516 MARGIN,
1517 c == sel_card, cur_col == STACKS_COL + i );
1519 else
1521 draw_empty_stack( i,
1522 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1523 MARGIN, cur_col == STACKS_COL + i );
1527 /* draw the remains */
1528 if( rem != NOT_A_CARD &&
1529 ( cur_rem == NOT_A_CARD || deck[cur_rem].next != NOT_A_CARD ) )
1531 /* gruik ! (we want to display a card back) */
1532 deck[rem].known = false;
1533 draw_card( &deck[rem], MARGIN, MARGIN, false, false );
1534 deck[rem].known = true;
1537 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1539 if( count_rem < 0 )
1541 prevcard = rem;
1542 count_rem = 0;
1543 while( prevcard != cur_rem && count_rem < cards_per_draw-1 )
1545 prevcard = deck[prevcard].next;
1546 count_rem++;
1549 prevcard = cur_rem;
1550 j = CARD_WIDTH+2*MARGIN+1;
1551 for( i = 0; i < count_rem; i++ )
1552 prevcard = find_prev_card(prevcard);
1553 for( i = 0; i <= count_rem; i++ )
1555 draw_card( &deck[prevcard], j,
1556 MARGIN, sel_card == prevcard,
1557 cur_card == prevcard );
1558 prevcard = deck[prevcard].next;
1559 j += CARD_WIDTH/2;
1562 if( ( cur_rem == NOT_A_CARD || rem == NOT_A_CARD )
1563 && cur_col == REM_COL )
1565 draw_cursor( CARD_WIDTH+2*MARGIN+1, MARGIN );
1568 rb->lcd_update();
1570 /* what to do when a key is pressed ... */
1571 button = rb->button_get( true );
1572 #if (CONFIG_KEYPAD == SANSA_E200_PAD) || (CONFIG_KEYPAD == SANSA_FUZE_PAD)
1573 if (button&(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD))
1574 button = button & (~BUTTON_REPEAT);
1575 #endif
1576 switch( button )
1578 /* move cursor to the last card of the previous column
1579 * or to the previous final stack
1580 * or to the remains stack */
1581 case SOL_RIGHT:
1582 #ifdef SOL_RIGHT_PRE
1583 if( lastbutton != SOL_RIGHT_PRE )
1584 break;
1585 #endif
1586 if( cur_col >= COL_NUM )
1588 cur_col = 0;
1590 else if( cur_col == COL_NUM - 1 )
1592 cur_col = REM_COL;
1594 else
1596 cur_col = (cur_col+1)%(REM_COL+1);
1598 if(cur_col == REM_COL)
1600 cur_card = cur_rem;
1601 break;
1603 cur_card = find_last_card( cur_col );
1604 break;
1606 /* move cursor to the last card of the next column
1607 * or to the next final stack
1608 * or to the remains stack */
1609 case SOL_LEFT:
1610 #ifdef SOL_LEFT_PRE
1611 if( lastbutton != SOL_LEFT_PRE )
1612 break;
1613 #endif
1614 if( cur_col == 0 )
1616 cur_col = REM_COL;
1618 else if( cur_col >= COL_NUM )
1620 cur_col = COL_NUM - 1;
1622 else
1624 cur_col = (cur_col + REM_COL)%(REM_COL+1);
1626 if( cur_col == REM_COL )
1628 cur_card = cur_rem;
1629 break;
1631 cur_card = find_last_card( cur_col );
1632 break;
1634 /* move cursor to card that's bellow */
1635 case SOL_DOWN:
1636 #ifdef SOL_DOWN_PRE
1637 if( lastbutton != SOL_DOWN_PRE )
1638 break;
1639 #else
1640 case SOL_DOWN|BUTTON_REPEAT:
1641 #endif
1642 if( cur_col >= COL_NUM )
1644 cur_col = (cur_col - COL_NUM + 1)%(SUITS + 1) + COL_NUM;
1645 if( cur_col == REM_COL )
1647 cur_card = cur_rem;
1649 else
1651 cur_card = find_last_card( cur_col );
1653 break;
1655 if( cur_card == NOT_A_CARD ) break;
1656 if( deck[cur_card].next != NOT_A_CARD )
1658 cur_card = deck[cur_card].next;
1660 else
1662 cur_card = cols[cur_col];
1663 while( !deck[ cur_card].known
1664 && deck[cur_card].next != NOT_A_CARD )
1666 cur_card = deck[cur_card].next;
1669 break;
1671 /* move cursor to card that's above */
1672 case SOL_UP:
1673 #ifdef SOL_UP_PRE
1674 if( lastbutton != SOL_UP_PRE )
1675 break;
1676 #else
1677 case SOL_UP|BUTTON_REPEAT:
1678 #endif
1679 if( cur_col >= COL_NUM )
1681 cur_col = (cur_col - COL_NUM + SUITS)%(SUITS + 1) + COL_NUM;
1682 if( cur_col == REM_COL )
1684 cur_card = cur_rem;
1686 else
1688 cur_card = find_last_card( cur_col );
1690 break;
1692 if( cur_card == NOT_A_CARD ) break;
1693 do {
1694 cur_card = find_prev_card( cur_card );
1695 if( cur_card == NOT_A_CARD )
1697 cur_card = find_last_card( cur_col );
1699 } while( deck[cur_card].next != NOT_A_CARD
1700 && !deck[cur_card].known );
1701 break;
1703 /* Try to put card under cursor on one of the stacks */
1704 case SOL_CUR2STACK:
1705 #ifdef SOL_CUR2STACK_PRE
1706 if( lastbutton != SOL_CUR2STACK_PRE )
1707 break;
1708 #endif
1709 move_card( deck[cur_card].suit + STACKS_COL, cur_card );
1710 break;
1712 /* Move cards arround, Uncover cards, ... */
1713 case SOL_MOVE:
1714 #ifdef SOL_MOVE_PRE
1715 if( lastbutton != SOL_MOVE_PRE )
1716 break;
1717 #endif
1719 if( sel_card == NOT_A_CARD )
1721 if( cur_card != NOT_A_CARD )
1723 if( deck[cur_card].next == NOT_A_CARD
1724 && !deck[cur_card].known )
1726 /* reveal a hidden card */
1727 deck[cur_card].known = true;
1729 else if( cur_col == REM_COL && cur_rem == NOT_A_CARD )
1731 break;
1733 else
1735 /* select a card */
1736 sel_card = cur_card;
1740 else if( sel_card == cur_card )
1742 /* unselect card or try putting card on
1743 * one of the 4 stacks */
1744 if( move_card( deck[sel_card].suit + COL_NUM, sel_card )
1745 == MOVE_OK && cur_col == REM_COL )
1747 cur_card = cur_rem;
1749 sel_card = NOT_A_CARD;
1751 else
1753 /* try moving cards */
1754 /* The flexible move must not be used from the remains stack. */
1755 if (find_card_col(sel_card) == REM_COL)
1757 if (move_card( cur_col, sel_card ) == MOVE_NOT_OK)
1758 sel_card = NOT_A_CARD;
1760 else
1762 do {
1763 if (move_card( cur_col, sel_card) == MOVE_OK)
1764 break;
1765 sel_card = find_prev_card(sel_card);
1766 } while (sel_card != NOT_A_CARD);
1769 break;
1771 /* If the card on the top of the remains can be put where
1772 * the cursor is, go ahead */
1773 case SOL_REM2CUR:
1774 #ifdef SOL_REM2CUR_PRE
1775 if( lastbutton != SOL_REM2CUR_PRE )
1776 break;
1777 #endif
1778 move_card( cur_col, cur_rem );
1779 break;
1781 /* If the card on top of the remains can be put on one
1782 * of the stacks, do so */
1783 case SOL_REM2STACK:
1784 #ifdef SOL_REM2STACK_PRE
1785 if( lastbutton != SOL_REM2STACK_PRE )
1786 break;
1787 #endif
1788 move_card( deck[cur_rem].suit + COL_NUM, cur_rem );
1789 break;
1791 #ifdef SOL_REM
1792 case SOL_REM:
1793 if( sel_card != NOT_A_CARD )
1795 /* unselect selected card */
1796 sel_card = NOT_A_CARD;
1797 break;
1799 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1801 sel_card = cur_rem;
1802 break;
1804 break;
1805 #endif
1807 /* unselect selected card or ...
1808 * draw new cards from the remains of the deck */
1809 case SOL_DRAW:
1810 #ifdef SOL_DRAW_PRE
1811 if( lastbutton != SOL_DRAW_PRE )
1812 break;
1813 #endif
1814 if( sel_card != NOT_A_CARD )
1816 /* unselect selected card */
1817 sel_card = NOT_A_CARD;
1818 break;
1820 if( rem != NOT_A_CARD )
1822 int cur_rem_old = cur_rem;
1823 count_rem = -1;
1824 /* draw new cards form the remains of the deck */
1825 if( cur_rem == NOT_A_CARD )
1827 /*if the cursor card is null*/
1828 cur_rem = rem;
1829 i = cards_per_draw - 1;
1830 count_rem++;
1832 else
1834 i = cards_per_draw;
1837 while( i > 0 && deck[cur_rem].next != NOT_A_CARD )
1839 cur_rem = deck[cur_rem].next;
1840 i--;
1841 count_rem++;
1843 /* test if any cards are really left on
1844 * the remains' stack */
1845 if( i == cards_per_draw )
1847 cur_rem = NOT_A_CARD;
1848 count_rem = -1;
1850 /* if cursor was on remains' stack when new cards were
1851 * drawn, put cursor on top of remains' stack */
1852 if( cur_col == REM_COL && cur_card == cur_rem_old )
1854 cur_card = cur_rem;
1855 sel_card = NOT_A_CARD;
1858 break;
1860 /* Show the menu */
1861 #ifdef SOL_RC_QUIT
1862 case SOL_RC_QUIT:
1863 #endif
1864 case SOL_QUIT:
1865 switch( solitaire_menu(true) )
1867 case MENU_SAVE_AND_QUIT:
1868 return SOLITAIRE_SAVE_AND_QUIT;
1870 case MENU_QUIT:
1871 return SOLITAIRE_QUIT;
1873 case MENU_USB:
1874 return SOLITAIRE_USB;
1876 break;
1878 case SYS_POWEROFF:
1879 return SOLITAIRE_SAVE_AND_QUIT;
1881 default:
1882 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1883 return SOLITAIRE_USB;
1884 break;
1887 if( button != BUTTON_NONE )
1888 lastbutton = button;
1890 /* fix incoherences concerning cur_col and cur_card */
1891 c = find_card_col( cur_card );
1892 if( c != NOT_A_COL && c != cur_col )
1893 cur_card = find_last_card( cur_col );
1895 if( cur_card == NOT_A_CARD
1896 && find_last_card( cur_col ) != NOT_A_CARD )
1897 cur_card = find_last_card( cur_col );
1899 rb->yield();
1904 * Plugin entry point
1907 enum plugin_status plugin_start(const void* parameter )
1909 int result;
1911 /* plugin init */
1912 (void)parameter;
1914 configfile_load(CONFIG_FILENAME, config,
1915 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1916 rb->memcpy(&sol, &sol_disk, sizeof(sol)); /* copy to running config */
1918 if( load_game() == 0 )
1920 rb->splash( HZ, "Resuming saved game." );
1921 result = SOLITAIRE_QUIT;
1923 else
1924 result = SOLITAIRE_WIN;
1926 /* play the game :)
1927 * Keep playing if a game was won (that means display the menu after
1928 * winning instead of quiting) */
1929 while( ( result = solitaire( result ) ) == SOLITAIRE_WIN );
1931 if( result != SOLITAIRE_QUIT )
1932 /* result == SOLITAIRE_USB || result == SOLITAIRE_SAVE_AND_QUIT */
1933 save_game();
1935 if (rb->memcmp(&sol, &sol_disk, sizeof(sol))) /* save settings if changed */
1937 rb->memcpy(&sol_disk, &sol, sizeof(sol));
1938 configfile_save(CONFIG_FILENAME, config,
1939 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1942 /* Exit the plugin */
1943 return ( result == SOLITAIRE_USB ) ? PLUGIN_USB_CONNECTED : PLUGIN_OK;