Add headers to dummy *-target.h files
[kugel-rb.git] / apps / plugins / solitaire.c
blob1c7aefb1c396ca2c8fbe9dfa2d12768ae1554102
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 #ifdef HAVE_LCD_BITMAP
31 PLUGIN_HEADER
33 #define min(a,b) (a<b?a:b)
35 /**
36 * Key definitions
39 #if CONFIG_KEYPAD == RECORDER_PAD
40 # define SOL_QUIT BUTTON_OFF
41 # define SOL_UP BUTTON_UP
42 # define SOL_DOWN BUTTON_DOWN
43 # define SOL_LEFT BUTTON_LEFT
44 # define SOL_RIGHT BUTTON_RIGHT
45 # define SOL_MOVE BUTTON_ON
46 # define SOL_DRAW BUTTON_F1
47 # define SOL_REM2CUR BUTTON_PLAY
48 # define SOL_CUR2STACK BUTTON_F2
49 # define SOL_REM2STACK BUTTON_F3
50 # define HK_MOVE "ON"
51 # define HK_DRAW "F1"
52 # define HK_REM2CUR "PLAY"
53 # define HK_CUR2STACK "F2"
54 # define HK_REM2STACK "F3"
56 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
57 # define SOL_QUIT BUTTON_OFF
58 # define SOL_UP BUTTON_UP
59 # define SOL_DOWN BUTTON_DOWN
60 # define SOL_LEFT BUTTON_LEFT
61 # define SOL_RIGHT BUTTON_RIGHT
62 # define SOL_MOVE BUTTON_ON
63 # define SOL_DRAW BUTTON_F1
64 # define SOL_REM2CUR BUTTON_SELECT
65 # define SOL_CUR2STACK BUTTON_F2
66 # define SOL_REM2STACK BUTTON_F3
67 # define HK_MOVE "ON"
68 # define HK_DRAW "F1"
69 # define HK_REM2CUR "SELECT"
70 # define HK_CUR2STACK "F2"
71 # define HK_REM2STACK "F3"
73 #elif CONFIG_KEYPAD == ONDIO_PAD
74 # define SOL_QUIT BUTTON_OFF
75 # define SOL_UP_PRE BUTTON_UP
76 # define SOL_UP (BUTTON_UP | BUTTON_REL)
77 # define SOL_DOWN_PRE BUTTON_DOWN
78 # define SOL_DOWN (BUTTON_DOWN | BUTTON_REL)
79 # define SOL_LEFT_PRE BUTTON_LEFT
80 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
81 # define SOL_RIGHT_PRE BUTTON_RIGHT
82 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
83 # define SOL_MOVE_PRE BUTTON_MENU
84 # define SOL_MOVE (BUTTON_MENU | BUTTON_REL)
85 # define SOL_DRAW_PRE BUTTON_MENU
86 # define SOL_DRAW (BUTTON_MENU | BUTTON_REPEAT)
87 # define SOL_REM2CUR_PRE BUTTON_DOWN
88 # define SOL_REM2CUR (BUTTON_DOWN | BUTTON_REPEAT)
89 # define SOL_CUR2STACK_PRE BUTTON_UP
90 # define SOL_CUR2STACK (BUTTON_UP | BUTTON_REPEAT)
91 # define SOL_REM2STACK_PRE BUTTON_RIGHT
92 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
93 # define HK_MOVE "MODE"
94 # define HK_DRAW "MODE.."
95 # define HK_REM2CUR "DOWN.."
96 # define HK_CUR2STACK "UP.."
97 # define HK_REM2STACK "RIGHT.."
99 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
100 (CONFIG_KEYPAD == IRIVER_H300_PAD)
101 # define SOL_QUIT BUTTON_OFF
102 # define SOL_UP BUTTON_UP
103 # define SOL_DOWN BUTTON_DOWN
104 # define SOL_LEFT BUTTON_LEFT
105 # define SOL_RIGHT BUTTON_RIGHT
106 # define SOL_MOVE_PRE BUTTON_SELECT
107 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
108 # define SOL_DRAW BUTTON_MODE
109 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_ON)
110 # define SOL_CUR2STACK_PRE BUTTON_SELECT
111 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
112 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_ON)
113 # define SOL_REM BUTTON_REC
114 # define SOL_RC_QUIT BUTTON_RC_STOP
115 # define HK_MOVE "NAVI"
116 # define HK_DRAW "A-B"
117 # define HK_REM2CUR "PLAY+LEFT"
118 # define HK_CUR2STACK "NAVI.."
119 # define HK_REM2STACK "PLAY+RIGHT"
121 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
122 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
123 # define SOL_QUIT (BUTTON_SELECT | BUTTON_MENU)
124 # define SOL_UP BUTTON_SCROLL_BACK
125 # define SOL_DOWN BUTTON_SCROLL_FWD
126 # define SOL_LEFT_PRE BUTTON_LEFT
127 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
128 # define SOL_RIGHT_PRE BUTTON_RIGHT
129 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
130 # define SOL_MOVE_PRE BUTTON_SELECT
131 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
132 # define SOL_DRAW_PRE BUTTON_MENU
133 # define SOL_DRAW (BUTTON_MENU | BUTTON_REL)
134 # define SOL_REM2CUR BUTTON_PLAY
135 # define SOL_CUR2STACK_PRE BUTTON_MENU
136 # define SOL_CUR2STACK (BUTTON_MENU | BUTTON_REPEAT)
137 # define SOL_REM2STACK_PRE BUTTON_RIGHT
138 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
139 # define HK_UD "SCROLL U/D"
140 # define HK_MOVE "SELECT"
141 # define HK_DRAW "MENU"
142 # define HK_REM2CUR "PLAY"
143 # define HK_CUR2STACK "MENU.."
144 # define HK_REM2STACK "RIGHT.."
146 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
147 # define SOL_QUIT BUTTON_POWER
148 # define SOL_UP BUTTON_UP
149 # define SOL_DOWN BUTTON_DOWN
150 # define SOL_LEFT BUTTON_LEFT
151 # define SOL_RIGHT BUTTON_RIGHT
152 # define SOL_MOVE_PRE BUTTON_SELECT
153 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
154 # define SOL_DRAW_PRE BUTTON_PLAY
155 # define SOL_DRAW (BUTTON_PLAY | BUTTON_REL)
156 # define SOL_REM2CUR_PRE BUTTON_PLAY
157 # define SOL_REM2CUR (BUTTON_PLAY | BUTTON_REPEAT)
158 # define SOL_CUR2STACK_PRE BUTTON_SELECT
159 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
160 # define SOL_REM2STACK BUTTON_REC
161 # define HK_MOVE "SELECT"
162 # define HK_DRAW "PLAY"
163 # define HK_REM2CUR "PLAY.."
164 # define HK_CUR2STACK "SELECT.."
165 # define HK_REM2STACK "REC"
167 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
168 # define SOL_QUIT BUTTON_POWER
169 # define SOL_UP BUTTON_UP
170 # define SOL_DOWN BUTTON_DOWN
171 # define SOL_LEFT BUTTON_LEFT
172 # define SOL_RIGHT BUTTON_RIGHT
173 # define SOL_MOVE_PRE BUTTON_SELECT
174 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
175 # define SOL_DRAW BUTTON_MENU
176 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_A)
177 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
178 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_A)
179 # define HK_MOVE "SELECT"
180 # define HK_DRAW "MENU"
181 # define HK_REM2CUR "A+LEFT"
182 # define HK_CUR2STACK "SELECT.."
183 # define HK_REM2STACK "A+RIGHT"
185 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
186 # define SOL_QUIT BUTTON_POWER
187 # define SOL_UP BUTTON_UP
188 # define SOL_DOWN BUTTON_DOWN
189 # define SOL_LEFT BUTTON_SCROLL_BACK
190 # define SOL_RIGHT BUTTON_SCROLL_FWD
191 # define SOL_MOVE BUTTON_SELECT
192 # define SOL_DRAW BUTTON_REC
193 # define SOL_REM2CUR BUTTON_LEFT
194 # define SOL_CUR2STACK_PRE BUTTON_REC
195 # define SOL_CUR2STACK (BUTTON_REC | BUTTON_RIGHT)
196 # define SOL_REM2STACK BUTTON_RIGHT
197 # define HK_MOVE "SELECT"
198 # define HK_DRAW "REC"
199 # define HK_REM2CUR "LEFT"
200 # define HK_CUR2STACK "DOUBLE SELECT"
201 # define HK_REM2STACK "RIGHT"
203 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
204 # define SOL_QUIT (BUTTON_HOME|BUTTON_REPEAT)
205 # define SOL_UP BUTTON_UP
206 # define SOL_DOWN BUTTON_DOWN
207 # define SOL_LEFT BUTTON_SCROLL_BACK
208 # define SOL_RIGHT BUTTON_SCROLL_FWD
209 # define SOL_MOVE (BUTTON_SELECT|BUTTON_REL)
210 # define SOL_DRAW (BUTTON_HOME|BUTTON_REL)
211 # define SOL_REM2CUR BUTTON_LEFT
212 # define SOL_CUR2STACK_PRE (BUTTON_RIGHT | BUTTON_REPEAT)
213 # define SOL_CUR2STACK BUTTON_RIGHT
214 # define SOL_REM2STACK (BUTTON_LEFT|BUTTON_REPEAT)
215 # define SOL_REM2STACK_PRE BUTTON_LEFT
216 # define HK_MOVE "SHORT SELECT"
217 # define HK_DRAW "SHORT HOME"
218 # define HK_REM2CUR "LEFT"
219 # define HK_CUR2STACK "DOUBLE SELECT"
220 # define HK_REM2STACK "LEFT"
222 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
223 # define SOL_QUIT BUTTON_POWER
224 # define SOL_UP BUTTON_UP
225 # define SOL_DOWN BUTTON_DOWN
226 # define SOL_LEFT BUTTON_LEFT
227 # define SOL_RIGHT BUTTON_RIGHT
228 # define SOL_MOVE_PRE BUTTON_SELECT
229 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
230 # define SOL_DRAW BUTTON_VOL_DOWN
231 # define SOL_REM2CUR BUTTON_REC
232 # define SOL_CUR2STACK_PRE BUTTON_SELECT
233 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
234 # define SOL_REM2STACK BUTTON_VOL_UP
235 # define HK_MOVE "SELECT"
236 # define HK_DRAW "REC"
237 # define HK_REM2CUR "LEFT"
238 # define HK_CUR2STACK "DOUBLE SELECT"
239 # define HK_REM2STACK "RIGHT"
241 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
242 # define SOL_QUIT BUTTON_POWER
243 # define SOL_UP BUTTON_UP
244 # define SOL_DOWN BUTTON_DOWN
245 # define SOL_LEFT BUTTON_LEFT
246 # define SOL_RIGHT BUTTON_RIGHT
247 # define SOL_MOVE_PRE BUTTON_SELECT
248 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
249 # define SOL_DRAW BUTTON_HOME
250 # define SOL_REM2CUR BUTTON_VOL_DOWN
251 # define SOL_CUR2STACK_PRE BUTTON_SELECT
252 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
253 # define SOL_REM2STACK BUTTON_VOL_UP
254 # define HK_MOVE "SELECT"
255 # define HK_DRAW "HOME"
256 # define HK_REM2CUR "LEFT"
257 # define HK_CUR2STACK "DOUBLE SELECT"
258 # define HK_REM2STACK "RIGHT"
260 #elif CONFIG_KEYPAD == SANSA_M200_PAD
261 # define SOL_QUIT BUTTON_POWER
262 # define SOL_UP BUTTON_UP
263 # define SOL_DOWN BUTTON_DOWN
264 # define SOL_LEFT BUTTON_LEFT
265 # define SOL_RIGHT BUTTON_RIGHT
266 # define SOL_MOVE_PRE BUTTON_SELECT
267 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
268 # define SOL_DRAW (BUTTON_SELECT | BUTTON_UP)
269 # define SOL_REM2CUR BUTTON_VOL_DOWN
270 # define SOL_CUR2STACK_PRE BUTTON_SELECT
271 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
272 # define SOL_REM2STACK BUTTON_VOL_UP
273 # define HK_MOVE "SELECT"
274 # define HK_DRAW "SELECT + UP"
275 # define HK_REM2CUR "LEFT"
276 # define HK_CUR2STACK "DOUBLE SELECT"
277 # define HK_REM2STACK "RIGHT"
279 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
280 # define SOL_QUIT BUTTON_POWER
281 # define SOL_UP BUTTON_SCROLL_UP
282 # define SOL_DOWN BUTTON_SCROLL_DOWN
283 # define SOL_LEFT_PRE BUTTON_LEFT
284 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
285 # define SOL_RIGHT_PRE BUTTON_RIGHT
286 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
287 # define SOL_MOVE BUTTON_PLAY
288 # define SOL_DRAW_PRE BUTTON_LEFT
289 # define SOL_DRAW (BUTTON_LEFT | BUTTON_REPEAT)
290 # define SOL_REM2CUR BUTTON_FF
291 # define SOL_CUR2STACK BUTTON_REW
292 # define SOL_REM2STACK_PRE BUTTON_RIGHT
293 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
294 # define HK_MOVE "PLAY"
295 # define HK_DRAW "LEFT.."
296 # define HK_REM2CUR "FF"
297 # define HK_CUR2STACK "REW"
298 # define HK_REM2STACK "RIGHT.."
300 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
301 # define SOL_QUIT BUTTON_BACK
302 # define SOL_UP BUTTON_UP
303 # define SOL_DOWN BUTTON_DOWN
304 # define SOL_LEFT BUTTON_LEFT
305 # define SOL_RIGHT BUTTON_RIGHT
306 # define SOL_MOVE_PRE BUTTON_SELECT
307 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
308 # define SOL_DRAW BUTTON_MENU
309 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_SELECT)
310 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
311 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_SELECT)
312 # define HK_MOVE "SELECT"
313 # define HK_DRAW "MENU"
314 # define HK_REM2CUR "SELECT+LEFT"
315 # define HK_CUR2STACK "SELECT.."
316 # define HK_REM2STACK "SELECT+RIGHT"
318 #elif (CONFIG_KEYPAD == MROBE100_PAD)
319 # define SOL_QUIT BUTTON_POWER
320 # define SOL_UP BUTTON_UP
321 # define SOL_DOWN BUTTON_DOWN
322 # define SOL_LEFT BUTTON_LEFT
323 # define SOL_RIGHT BUTTON_RIGHT
324 # define SOL_MOVE_PRE BUTTON_SELECT
325 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
326 # define SOL_DRAW BUTTON_MENU
327 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_DISPLAY)
328 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
329 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_DISPLAY)
330 # define HK_MOVE "SELECT"
331 # define HK_DRAW "MENU"
332 # define HK_REM2CUR "DISPLAY+LEFT"
333 # define HK_CUR2STACK "SELECT.."
334 # define HK_REM2STACK "DISPLAY+RIGHT"
336 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
337 # define SOL_QUIT BUTTON_RC_REC
338 # define SOL_UP BUTTON_RC_VOL_UP
339 # define SOL_DOWN BUTTON_RC_VOL_DOWN
340 # define SOL_LEFT BUTTON_RC_REW
341 # define SOL_RIGHT BUTTON_RC_FF
342 # define SOL_MOVE BUTTON_RC_PLAY
343 # define SOL_DRAW_PRE BUTTON_RC_MENU
344 # define SOL_DRAW (BUTTON_RC_MENU | BUTTON_REL)
345 # define SOL_REM2CUR_PRE BUTTON_RC_MENU
346 # define SOL_REM2CUR (BUTTON_RC_MENU | BUTTON_REPEAT)
347 # define SOL_CUR2STACK_PRE BUTTON_RC_MODE
348 # define SOL_CUR2STACK (BUTTON_RC_MODE | BUTTON_REL)
349 # define SOL_REM2STACK_PRE BUTTON_RC_MODE
350 # define SOL_REM2STACK (BUTTON_RC_MODE | BUTTON_REPEAT)
351 # define HK_LR "REW/FF"
352 # define HK_UD "VOL UP/DOWN"
353 # define HK_MOVE "PLAY"
354 # define HK_DRAW "MENU"
355 # define HK_REM2CUR "MENU.."
356 # define HK_CUR2STACK "MODE"
357 # define HK_REM2STACK "MODE.."
359 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
360 # define SOL_QUIT BUTTON_POWER
362 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
363 # define SOL_QUIT BUTTON_BACK
364 # define SOL_UP BUTTON_UP
365 # define SOL_DOWN BUTTON_DOWN
366 # define SOL_LEFT BUTTON_LEFT
367 # define SOL_RIGHT BUTTON_RIGHT
368 # define SOL_MOVE_PRE BUTTON_SELECT
369 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
370 # define SOL_DRAW BUTTON_MENU
371 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_SELECT)
372 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
373 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_SELECT)
374 # define HK_MOVE "MIDDLE"
375 # define HK_DRAW "MENU"
376 # define HK_REM2CUR "PLAY+LEFT"
377 # define HK_CUR2STACK "PLAY.."
378 # define HK_REM2STACK "PLAY+RIGHT"
380 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
381 # define SOL_QUIT BUTTON_POWER
382 # define SOL_UP BUTTON_UP
383 # define SOL_DOWN BUTTON_DOWN
384 # define SOL_LEFT BUTTON_LEFT
385 # define SOL_RIGHT BUTTON_RIGHT
386 # define SOL_MOVE_PRE BUTTON_SELECT
387 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
388 # define SOL_DRAW BUTTON_MENU
389 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_VIEW)
390 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
391 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_VIEW)
392 # define HK_MOVE "SELECT"
393 # define HK_DRAW "MENU"
394 # define HK_REM2CUR "VIEW+LEFT"
395 # define HK_CUR2STACK "SELECT.."
396 # define HK_REM2STACK "VIEW+RIGHT"
398 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
399 # define SOL_QUIT BUTTON_POWER
400 # define SOL_UP BUTTON_UP
401 # define SOL_DOWN BUTTON_DOWN
402 # define SOL_LEFT BUTTON_PREV
403 # define SOL_RIGHT BUTTON_NEXT
404 # define SOL_MOVE_PRE BUTTON_PLAY
405 # define SOL_MOVE (BUTTON_PLAY | BUTTON_REL)
406 # define SOL_DRAW BUTTON_MENU
407 # define SOL_REM2CUR BUTTON_LEFT
408 # define SOL_CUR2STACK (BUTTON_PLAY | BUTTON_REPEAT)
409 # define SOL_REM2STACK BUTTON_RIGHT
410 # define HK_MOVE "PLAY"
411 # define HK_DRAW "MENU"
412 # define HK_REM2CUR "LEFT"
413 # define HK_CUR2STACK "PLAY..."
414 # define HK_REM2STACK "RIGHT"
416 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
417 (CONFIG_KEYPAD == ONDAVX777_PAD) || \
418 CONFIG_KEYPAD == MROBE500_PAD
419 # define SOL_QUIT BUTTON_POWER
421 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
422 # define SOL_QUIT BUTTON_REC
423 # define SOL_UP BUTTON_UP
424 # define SOL_DOWN BUTTON_DOWN
425 # define SOL_LEFT_PRE BUTTON_LEFT
426 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REW)
427 # define SOL_RIGHT_PRE BUTTON_RIGHT
428 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REW)
429 # define SOL_MOVE BUTTON_PLAY
430 # define SOL_DRAW_PRE (BUTTON_PLAY | BUTTON_RIGHT)
431 # define SOL_DRAW (BUTTON_PLAY | BUTTON_LEFT)
432 # define SOL_REM2CUR BUTTON_FFWD
433 # define SOL_CUR2STACK BUTTON_REW
434 # define SOL_REM2STACK_PRE (BUTTON_FFWD | BUTTON_RIGHT)
435 # define SOL_REM2STACK (BUTTON_FFWD | BUTTON_LEFT)
436 # define HK_MOVE "PLAY"
437 # define HK_DRAW "REW.."
438 # define HK_REM2CUR "FFWD"
439 # define HK_CUR2STACK "REW"
440 # define HK_REM2STACK "FFWD.."
442 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
443 # define SOL_QUIT BUTTON_REC
444 # define SOL_UP BUTTON_UP
445 # define SOL_DOWN BUTTON_DOWN
446 # define SOL_LEFT BUTTON_PREV
447 # define SOL_RIGHT BUTTON_NEXT
448 # define SOL_MOVE_PRE BUTTON_OK
449 # define SOL_MOVE (BUTTON_OK | BUTTON_REL)
450 # define SOL_DRAW BUTTON_MENU
451 # define SOL_REM2CUR BUTTON_CANCEL
452 # define SOL_CUR2STACK BUTTON_PLAY
453 # define SOL_REM2STACK (BUTTON_PLAY | BUTTON_REPEAT)
454 # define HK_MOVE "OK"
455 # define HK_DRAW "MENU"
456 # define HK_REM2CUR "CANCEL"
457 # define HK_CUR2STACK "PLAY"
458 # define HK_REM2STACK "PLAY..."
460 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
461 # define SOL_QUIT (BUTTON_REC | BUTTON_PLAY)
462 # define SOL_UP BUTTON_PREV
463 # define SOL_DOWN BUTTON_NEXT
464 # define SOL_LEFT BUTTON_VOL_DOWN
465 # define SOL_RIGHT BUTTON_VOL_UP
466 # define SOL_MOVE_PRE BUTTON_SELECT
467 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
468 # define SOL_DRAW BUTTON_REC
469 # define SOL_REM2CUR (BUTTON_REC | BUTTON_REPEAT)
470 # define SOL_CUR2STACK BUTTON_PLAY
471 # define SOL_REM2STACK (BUTTON_PLAY | BUTTON_REPEAT)
472 # define HK_MOVE "SELECT"
473 # define HK_DRAW "REC"
474 # define HK_REM2CUR "REC.."
475 # define HK_CUR2STACK "PLAY"
476 # define HK_REM2STACK "PLAY...."
478 #else
479 #error No keymap defined!
480 #endif
482 #ifdef HAVE_TOUCHSCREEN
483 //#ifndef SOL_QUIT
484 //# define SOL_QUIT BUTTON_TOPLEFT
485 //endif
486 #ifndef SOL_UP
487 # define SOL_UP BUTTON_TOPMIDDLE
488 #endif
489 #ifndef SOL_DOWN
490 # define SOL_DOWN BUTTON_BOTTOMMIDDLE
491 #endif
492 #ifndef SOL_LEFT
493 # define SOL_LEFT BUTTON_MIDLEFT
494 #endif
495 #ifndef SOL_RIGHT
496 # define SOL_RIGHT BUTTON_MIDRIGHT
497 #endif
498 #ifndef SOL_MOVE
499 # define SOL_MOVE BUTTON_CENTER
500 # define HK_MOVE "CENTRE"
501 #endif
502 #ifndef SOL_DRAW
503 # define SOL_DRAW BUTTON_TOPLEFT
504 # define HK_DRAW "TOPLEFT"
505 #endif
506 #ifndef SOL_REM2CUR
507 # define SOL_REM2CUR BUTTON_TOPRIGHT
508 # define HK_REM2CUR "TOPRIGHT"
509 #endif
510 #ifndef SOL_CUR2STACK
511 # define SOL_CUR2STACK BUTTON_BOTTOMLEFT
512 # define HK_CUR2STACK "BOTTOMLEFT"
513 #endif
514 #ifndef SOL_REM2STACK
515 # define SOL_REM2STACK BUTTON_BOTTOMRIGHT
516 # define HK_REM2STACK "BOTTOMRIGHT"
517 #endif
519 #endif
521 #ifndef HK_LR
522 # define HK_LR "LEFT/RIGHT"
523 #endif
524 #ifndef HK_UD
525 # define HK_UD "UP/DOWN"
526 #endif
529 * Misc constants, graphics and other defines
532 #include "pluginbitmaps/card_back.h"
533 #include "pluginbitmaps/card_deck.h"
534 #include "pluginbitmaps/solitaire_suitsi.h"
536 #define CARD_GFX_WIDTH BMPWIDTH_card_back
537 #define CARD_GFX_HEIGHT BMPHEIGHT_card_back
538 #define CARD_WIDTH (BMPWIDTH_card_back+2)
539 #define CARD_HEIGHT (BMPHEIGHT_card_back+2)
541 #if LCD_WIDTH >= 640
542 # define MARGIN 4
543 # define LARGE_CARD
544 # define SYMBOL_HEIGHT 24
546 #elif LCD_WIDTH >= 320
547 # define MARGIN 4
548 # define LARGE_CARD
549 # define SYMBOL_HEIGHT 12
550 #elif LCD_WIDTH >= 220
551 # define MARGIN 3
552 # define LARGE_CARD
553 # define SYMBOL_HEIGHT 12
554 #elif LCD_WIDTH >= 160
555 # define MARGIN 2
556 # define SYMBOL_HEIGHT 11
557 #elif LCD_WIDTH >= 128
558 # define MARGIN 1
559 # define SYMBOL_HEIGHT 10
560 #else
561 # define MARGIN 0
562 # define SYMBOL_HEIGHT 8
563 #endif
565 #define CARD_START (CARD_HEIGHT+2*MARGIN+1)
567 /* background color */
568 #ifdef HAVE_LCD_COLOR
569 # define BACKGROUND_COLOR LCD_RGBPACK(0,157,0)
570 # define FRAME_COLOR LCD_RGBPACK(23,119,218)
571 #elif LCD_DEPTH > 1
572 # define BACKGROUND_COLOR LCD_WHITE
573 # define FRAME_COLOR LCD_BLACK
574 #endif
577 #define CONFIG_FILENAME "sol.cfg"
579 #define NOT_A_CARD -1
581 /* number of cards per suit */
582 #define CARDS_PER_SUIT 13
584 /* number of suits */
585 #define SUITS 4
587 #define NUM_CARDS ( CARDS_PER_SUIT * SUITS )
589 /* number of columns */
590 #define COL_NUM 7
592 /* pseudo column numbers to be used for cursor coordinates */
593 /* columns COL_NUM to COL_NUM + SUITS - 1 correspond to the final stacks */
594 #define STACKS_COL COL_NUM
595 /* column COL_NUM + SUITS corresponds to the remains' stack */
596 #define REM_COL (STACKS_COL + SUITS)
598 #define NOT_A_COL -1
600 typedef struct
602 signed char suit;
603 signed char num;
604 bool known : 1;
605 bool used : 1; /* this is what is used when dealing cards */
606 signed char next;
607 } card_t;
611 * LCD card drawing routines
614 static void draw_cursor( int x, int y )
616 rb->lcd_set_drawmode( DRMODE_COMPLEMENT );
617 rb->lcd_fillrect( x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
618 #ifdef LARGE_CARD
619 rb->lcd_drawpixel( x+1, y+1 );
620 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
621 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
622 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
623 #endif
624 rb->lcd_set_drawmode( DRMODE_SOLID );
627 /* Draw a card's border, select it if it's selected and draw the cursor
628 * if the cursor is currently over the card */
629 static void draw_card_ext( int x, int y, bool selected, bool cursor )
631 #if LCD_DEPTH > 1
632 int oldfg = rb->lcd_get_foreground();
634 rb->lcd_set_foreground( LCD_BLACK );
635 #endif
636 #ifdef LARGE_CARD
637 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y );
638 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y+CARD_HEIGHT-1 );
639 rb->lcd_vline( x, y+2, y+CARD_HEIGHT-3 );
640 rb->lcd_vline( x+CARD_WIDTH-1, y+2, y+CARD_HEIGHT-3 );
641 rb->lcd_drawpixel( x+1, y+1 );
642 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
643 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
644 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
645 #else
646 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y );
647 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y+CARD_HEIGHT-1 );
648 rb->lcd_vline( x, y+1, y+CARD_HEIGHT-2 );
649 rb->lcd_vline( x+CARD_WIDTH-1, y+1, y+CARD_HEIGHT-2 );
650 #endif
652 if( selected )
654 #if LCD_DEPTH > 1
655 rb->lcd_set_foreground( FRAME_COLOR );
656 #endif
657 rb->lcd_drawrect( x+1, y+1, CARD_WIDTH-2, CARD_HEIGHT-2 );
658 #ifdef LARGE_CARD
659 rb->lcd_drawrect( x+2, y+2, CARD_WIDTH-4, CARD_HEIGHT-4 );
660 #endif
662 #if LCD_DEPTH > 1
663 rb->lcd_set_foreground( oldfg );
664 #endif
666 if( cursor )
668 draw_cursor( x, y );
672 /* Draw a card's inner graphics */
673 static void draw_card( card_t *card, int x, int y,
674 bool selected, bool cursor )
676 if( card->known )
678 rb->lcd_bitmap_part( card_deck, CARD_GFX_WIDTH * card->num,
679 CARD_GFX_HEIGHT * card->suit,
680 STRIDE(SCREEN_MAIN,
681 BMPWIDTH_card_deck, BMPHEIGHT_card_deck),
682 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
684 else
686 rb->lcd_bitmap( card_back, x+1, y+1,
687 CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
689 draw_card_ext( x, y, selected, cursor );
692 /* Draw an empty stack */
693 static void draw_empty_stack( int s, int x, int y, bool cursor )
695 rb->lcd_bitmap_part( solitaire_suitsi, 0,
696 CARD_GFX_HEIGHT * s,
697 STRIDE( SCREEN_MAIN,
698 BMPWIDTH_solitaire_suitsi, BMPHEIGHT_solitaire_suitsi),
699 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
701 draw_card_ext( x, y, false, cursor );
704 /* Help */
705 static bool solitaire_help( void )
707 static char* help_text[] = {
708 "Solitaire", "", "Controls", "",
709 HK_LR ":", "Move", "the", "cursor", "to", "the",
710 "previous/", "next", "column.", "",
711 HK_UD ":", "Move", "the", "cursor", "up/", "down", "in", "the",
712 "column.", "",
713 HK_MOVE ":", "Select", "cards,", "move", "cards...", "",
714 HK_DRAW ":", "Deselect", "a", "card", "if", "it", "was", "selected.",
715 "Else", "draw", "new", "card(s)", "from", "the", "remains",
716 "stack.", "", "",
717 "Shortcuts", "",
718 HK_REM2CUR ":", "Put", "the", "card", "on", "top", "of", "the",
719 "remains", "stack", "on", "top", "of", "the", "cursor.", "",
720 HK_CUR2STACK ":", "Put", "the", "card", "under", "the", "cursor",
721 "on", "one", "of", "the", "4", "final", "stacks.", "",
722 HK_REM2STACK ":", "Put", "the", "card", "on", "top", "of", "the",
723 "remains", "stack", "on", "one", "of", "the", "4", "final",
724 "stacks."
726 static struct style_text formation[]={
727 { 0, TEXT_CENTER|TEXT_UNDERLINE },
728 { 2, C_RED },
729 { 48, C_RED },
730 LAST_STYLE_ITEM
732 if (display_text(ARRAYLEN(help_text), help_text, formation, NULL, true))
733 return true;
734 return false;
738 * Custom menu / options
741 #define CFGFILE_VERSION 0
743 struct sol_config {
744 int draw_type;
747 struct sol_config sol_disk = {0};
748 struct sol_config sol;
750 static struct configdata config[] = {
751 { TYPE_INT, 0, 1, { .int_p = &sol_disk.draw_type }, "draw_type", NULL }
754 static const struct opt_items drawcards[2] = {
755 { "Draw Three Cards", -1 },
756 { "Draw One Card", -1 },
759 void solitaire_init(void);
761 /* menu return codes */
762 enum { MENU_RESUME, MENU_SAVE_AND_QUIT, MENU_QUIT, MENU_USB };
764 static bool _ingame;
765 int solitaire_menu_cb(int action, const struct menu_item_ex *this_item)
767 int i = (intptr_t)this_item;
768 if( action == ACTION_REQUEST_MENUITEM )
770 if((!_ingame && (i==0 || i==5)) || ( _ingame && i==2 ))
771 return ACTION_EXIT_MENUITEM;
773 return action;
776 int solitaire_menu(bool in_game)
778 int selected = 0;
779 int result = -1;
781 MENUITEM_STRINGLIST(menu, "Solitaire Menu", solitaire_menu_cb,
782 "Resume Game", "Start New Game",
783 "Draw Cards Option",
784 "Help", "Playback Control",
785 "Save and Quit", "Quit");
786 _ingame = in_game;
788 while (result < 0)
790 switch (rb->do_menu(&menu, &selected, NULL, false))
792 default:
793 result = MENU_RESUME;
794 break;
796 case MENU_ATTACHED_USB:
797 result = MENU_USB;
798 break;
800 case 0:
801 result = MENU_RESUME;
802 break;
804 case 1:
805 solitaire_init();
806 result = MENU_RESUME;
807 break;
809 case 2:
810 if (rb->set_option("Draw Cards Option", &sol.draw_type,
811 INT, drawcards, 2, NULL))
812 result = MENU_USB;
813 break;
815 case 3:
816 if (solitaire_help())
817 result = MENU_USB;
818 break;
820 case 4:
821 playback_control(NULL);
822 break;
824 case 5:
825 result = MENU_SAVE_AND_QUIT;
826 break;
828 case 6:
829 result = MENU_QUIT;
830 break;
833 return result;
837 * Global variables
840 /* player's cursor */
841 int cur_card;
842 /* player's cursor column num */
843 int cur_col;
845 /* selected card */
846 int sel_card;
848 /* the deck */
849 card_t deck[ NUM_CARDS ];
851 /* the remaining cards */
852 /* first card of the remains' stack */
853 int rem;
854 /* upper visible card from the remains' stack */
855 int cur_rem;
856 /* number of cards drawn from the remains stack - 1 */
857 int count_rem;
858 /* number of cards per draw of the remains' stack */
859 int cards_per_draw;
861 /* the 7 game columns */
862 int cols[COL_NUM];
863 /* the 4 final stacks */
864 int stacks[SUITS];
867 * Card handling routines
870 int next_random_card( card_t *deck )
872 int i,r;
874 r = rb->rand()%(NUM_CARDS)+1;
875 i = 0;
877 while( r>0 )
879 i = (i + 1)%(NUM_CARDS);
880 if( !deck[i].used ) r--;
883 deck[i].used = true;
885 return i;
889 /* initialize the game */
890 void solitaire_init( void )
893 int c;
894 int i, j;
896 /* number of cards that are drawn on the remains' stack (by pressing F2) */
897 if( sol.draw_type == 0 )
899 cards_per_draw = 3;
901 else
903 cards_per_draw = 1;
906 /* init deck */
907 for( i=0; i<SUITS; i++ )
909 for( j=0; j<CARDS_PER_SUIT; j++ )
911 #define card deck[i*CARDS_PER_SUIT+j]
912 card.suit = i;
913 card.num = j;
914 card.known = true;
915 card.used = false;
916 card.next = NOT_A_CARD;
917 #undef card
921 /* deal the cards ... */
922 /* ... in the columns */
923 for( i=0; i<COL_NUM; i++ )
925 c = NOT_A_CARD;
926 for( j=0; j<=i; j++ )
928 if( c == NOT_A_CARD )
930 cols[i] = next_random_card( deck );
931 c = cols[i];
933 else
935 deck[c].next = next_random_card( deck );
936 c = deck[c].next;
938 if( j < i )
939 deck[c].known = false;
943 /* ... shuffle what's left of the deck */
944 rem = next_random_card(deck);
945 c = rem;
947 for( i=1; i < NUM_CARDS - COL_NUM * (COL_NUM + 1)/2; i++ )
949 deck[c].next = next_random_card( deck );
950 c = deck[c].next;
953 /* we now finished dealing the cards. The game can start ! (at last) */
955 /* init the stack */
956 for( i = 0; i<SUITS; i++ )
958 stacks[i] = NOT_A_CARD;
961 /* the cursor starts on upper left card */
962 cur_card = cols[0];
963 cur_col = 0;
965 /* no card is selected */
966 sel_card = NOT_A_CARD;
968 /* init the remainder */
969 cur_rem = NOT_A_CARD;
971 count_rem = -1;
974 /* find the column number in which 'card' can be found */
975 int find_card_col( int card )
977 int i;
978 int c;
980 if( card == NOT_A_CARD ) return NOT_A_COL;
982 for( i=0; i<COL_NUM; i++ )
984 c = cols[i];
985 while( c != NOT_A_CARD )
987 if( c == card ) return i;
988 c = deck[c].next;
992 for( i=0; i<SUITS; i++ )
994 c = stacks[i];
995 while( c != NOT_A_CARD )
997 if( c == card ) return STACKS_COL + i;
998 c = deck[c].next;
1002 return REM_COL;
1005 /* find the card preceding 'card' */
1006 /* if it doesn't exist, return NOT_A_CARD */
1007 int find_prev_card( int card ){
1008 int i;
1010 for( i=0; i < NUM_CARDS; i++ )
1012 if( deck[i].next == card ) return i;
1015 return NOT_A_CARD;
1018 /* find the last card of a given column */
1019 int find_last_card( int col )
1021 int c;
1023 if( col < COL_NUM )
1025 c = cols[col];
1027 else if( col < REM_COL )
1029 c = stacks[col - STACKS_COL];
1031 else
1033 c = cur_rem;
1036 if(c == NOT_A_CARD)
1037 return c;
1038 else
1040 while(deck[c].next != NOT_A_CARD)
1041 c = deck[c].next;
1042 return c;
1046 enum move { MOVE_OK, MOVE_NOT_OK };
1048 enum move move_card( int dest_col, int src_card )
1050 /* the column on which to take src_card */
1051 int src_col;
1053 /* the last card of dest_col */
1054 int dest_card;
1056 /* the card under src_card */
1057 int src_card_prev;
1059 /* you can't move no card (at least, it doesn't have any consequence) */
1060 if( src_card == NOT_A_CARD ) return MOVE_NOT_OK;
1061 /* you can't put a card back on the remains' stack */
1062 if( dest_col == REM_COL ) return MOVE_NOT_OK;
1063 /* you can't move an unknown card */
1064 if( !deck[src_card].known ) return MOVE_NOT_OK;
1066 src_col = find_card_col( src_card );
1067 dest_card = find_last_card( dest_col );
1068 src_card_prev = find_prev_card( src_card );
1070 /* you can't move more than one card at a time from the final stack */
1071 /* to the rest of the game */
1072 if( src_col >= COL_NUM && src_col < REM_COL
1073 && deck[src_card].next != NOT_A_CARD )
1075 return MOVE_NOT_OK;
1078 /* if we (that means dest) are on one of the 7 columns ... */
1079 if( dest_col < COL_NUM )
1081 /* ... check is we are on an empty color and that the src is a king */
1082 if( dest_card == NOT_A_CARD
1083 && deck[src_card].num == CARDS_PER_SUIT - 1 )
1085 /* this is a winning combination */
1086 cols[dest_col] = src_card;
1088 /* ... or check if the cards follow one another and have
1089 * different colorsuit */
1090 else if(( deck[dest_card].suit + deck[src_card].suit)%2==1
1091 && deck[dest_card].num == deck[src_card].num + 1 )
1093 /* this is a winning combination */
1094 deck[dest_card].next = src_card;
1096 /* ... or, humpf, well that's not good news */
1097 else
1099 /* this is not a winning combination */
1100 return MOVE_NOT_OK;
1103 /* if we are on one of the 4 final stacks ... */
1104 else if( dest_col < REM_COL )
1106 /* ... check if we are on an empty stack... */
1107 if( dest_card == NOT_A_CARD )
1109 /* ... and the src is an ace and this is the correct final stack */
1110 if( deck[src_card].num == 0
1111 && deck[src_card].suit == dest_col - STACKS_COL )
1113 /* this is a winning combination */
1114 stacks[dest_col - STACKS_COL] = src_card;
1116 else
1118 /* this is not a winning combination */
1119 return MOVE_NOT_OK;
1122 else /* non-empty stack */
1124 /* ...check if the cards follow one another, have the same suit and
1125 * {that src has no .next element or is from the remains' stack} */
1126 if( deck[dest_card].suit == deck[src_card].suit
1127 && deck[dest_card].num + 1 == deck[src_card].num
1128 && (deck[src_card].next == NOT_A_CARD || src_col == REM_COL) )
1130 /* this is a winning combination */
1131 deck[dest_card].next = src_card;
1133 /* ... or, well that's not good news */
1134 else
1136 /* this is not a winning combination */
1137 return MOVE_NOT_OK;
1141 /* if we are on the remains' stack */
1142 else
1144 /* you can't move a card back to the remains' stack */
1145 return MOVE_NOT_OK;
1148 /* if the src card is from the remains' stack, we don't want to take
1149 * the following cards */
1150 if( src_col == REM_COL )
1152 /* if src card is the first card from the stack */
1153 if( src_card_prev == NOT_A_CARD )
1155 rem = deck[src_card].next;
1157 /* if src card is not the first card from the stack */
1158 else
1160 deck[src_card_prev].next = deck[src_card].next;
1162 deck[src_card].next = NOT_A_CARD;
1163 cur_rem = src_card_prev;
1164 count_rem--;
1166 /* if the src card is from somewhere else, just take everything */
1167 else
1169 if( src_card_prev == NOT_A_CARD )
1171 if( src_col < COL_NUM )
1173 cols[src_col] = NOT_A_CARD;
1175 else
1177 stacks[src_col - STACKS_COL] = NOT_A_CARD;
1180 else
1182 deck[src_card_prev].next = NOT_A_CARD;
1183 deck[src_card_prev].known = true;
1186 sel_card = NOT_A_CARD;
1187 /* tada ! */
1188 return MOVE_OK;
1191 enum { SOLITAIRE_WIN, SOLITAIRE_SAVE_AND_QUIT, SOLITAIRE_QUIT, SOLITAIRE_USB };
1194 * Bouncing cards at the end of the game
1197 #define BC_ACCEL ((1<<16)*LCD_HEIGHT/128)
1198 #define BC_MYSPEED (6*BC_ACCEL)
1199 #define BC_MXSPEED (6*LCD_HEIGHT/128)
1201 int bouncing_cards( void )
1203 int i, j, x, vx, y, fp_y, fp_vy, button;
1205 /* flush the button queue */
1206 while( ( button = rb->button_get( false ) ) != BUTTON_NONE )
1208 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1209 return SOLITAIRE_USB;
1212 /* fun stuff :) */
1213 for( i = CARDS_PER_SUIT-1; i>=0; i-- )
1215 for( j = 0; j < SUITS; j++ )
1217 x = LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*j+j+1;
1218 fp_y = MARGIN<<16;
1220 #if LCD_WIDTH > 200
1221 vx = rb->rand() % (4*BC_MXSPEED/3-2) - BC_MXSPEED;
1222 if( vx >= -1 )
1223 vx += 3;
1224 #else
1225 vx = rb->rand() % (4*BC_MXSPEED/3) - BC_MXSPEED;
1226 if( vx >= 0 )
1227 vx++;
1228 #endif
1230 fp_vy = -rb->rand() % BC_MYSPEED;
1232 while( x < LCD_WIDTH && x + CARD_WIDTH > 0 )
1234 fp_vy += BC_ACCEL;
1235 x += vx;
1236 fp_y += fp_vy;
1237 if( fp_y >= (LCD_HEIGHT-CARD_HEIGHT) << 16 )
1239 fp_vy = -fp_vy*4/5;
1240 fp_y = (LCD_HEIGHT-CARD_HEIGHT) << 16;
1242 y = fp_y >> 16;
1243 draw_card( &deck[j*CARDS_PER_SUIT+i], x, y,
1244 false, false );
1245 rb->lcd_update_rect( x<0?0:x, y<0?0:y,
1246 CARD_WIDTH, CARD_HEIGHT );
1248 button = rb->button_get_w_tmo( 2 );
1249 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1250 return SOLITAIRE_USB;
1251 if( button == SOL_QUIT || button == SOL_MOVE )
1252 return SOLITAIRE_WIN;
1256 return SOLITAIRE_WIN;
1260 * Game save/load routines
1262 void get_save_filename( char *buf )
1264 char *s;
1265 rb->strcpy( buf, rb->plugin_get_current_filename() );
1266 s = rb->strrchr( buf, '/' ) + 1;
1267 *s = '\0';
1268 rb->strcat( s, "sol.save" );
1271 int open_save_file( int flags )
1273 char buf[MAX_PATH];
1274 get_save_filename( buf );
1275 return rb->open( buf, flags, 0666);
1278 void delete_save_file( void )
1280 char buf[MAX_PATH];
1281 get_save_filename( buf );
1282 rb->remove( buf );
1285 #ifdef write
1286 # undef write
1287 #endif
1288 int save_write( int fd, const void *buf, size_t count, int *checksum )
1290 size_t i;
1291 if( rb->write( fd, buf, count ) < (ssize_t)count )
1292 return 1;
1293 for( i = 0; i < count; i++ )
1294 *checksum += (int)(((const char *)buf)[i]);
1295 return 0;
1298 #ifdef read
1299 # undef read
1300 #endif
1301 int save_read( int fd, void *buf, size_t count, int *checksum )
1303 size_t i;
1304 if( rb->read( fd, buf, count ) < (ssize_t)count )
1305 return 1;
1306 for( i = 0; i < count; i++ )
1307 *checksum -= (int)(((const char *)buf)[i]);
1308 return 0;
1311 int save_game( void )
1313 int fd = open_save_file( O_CREAT|O_WRONLY|O_TRUNC );
1314 int checksum = 42;
1315 if( fd < 0 )
1316 return -1;
1317 if( save_write( fd, &cur_card, sizeof( int ), &checksum )
1318 || save_write( fd, &cur_col, sizeof( int ), &checksum )
1319 || save_write( fd, &sel_card, sizeof( int ), &checksum )
1320 || save_write( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1321 || save_write( fd, &rem, sizeof( int ), &checksum )
1322 || save_write( fd, &cur_rem, sizeof( int ), &checksum )
1323 || save_write( fd, &count_rem, sizeof( int ), &checksum )
1324 || save_write( fd, &cards_per_draw, sizeof( int ), &checksum )
1325 || save_write( fd, cols, COL_NUM * sizeof( int ), &checksum )
1326 || save_write( fd, stacks, SUITS * sizeof( int ), &checksum )
1327 || ( rb->write( fd, &checksum, sizeof( int ) ) < (ssize_t)(sizeof( int ) ) ) )
1329 rb->close( fd );
1330 rb->splash( 2*HZ, "Error while saving game. Aborting." );
1331 return -2;
1333 rb->close( fd );
1334 return 0;
1337 int load_game( void )
1339 int checksum, retval;
1341 int fd = open_save_file( O_RDONLY );
1342 if( fd < 0 )
1343 return -1;
1345 retval = 0; /* Assume good case */
1346 if( ( rb->lseek( fd, -sizeof( int ), SEEK_END ) == -((ssize_t)sizeof( int ))-1 )
1347 || ( rb->read( fd, &checksum, sizeof( int ) ) < ((ssize_t)sizeof( int )) )
1348 || ( rb->lseek( fd, 0, SEEK_SET ) == -1 )
1349 || save_read( fd, &cur_card, sizeof( int ), &checksum )
1350 || save_read( fd, &cur_col, sizeof( int ), &checksum )
1351 || save_read( fd, &sel_card, sizeof( int ), &checksum )
1352 || save_read( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1353 || save_read( fd, &rem, sizeof( int ), &checksum )
1354 || save_read( fd, &cur_rem, sizeof( int ), &checksum )
1355 || save_read( fd, &count_rem, sizeof( int ), &checksum )
1356 || save_read( fd, &cards_per_draw, sizeof( int ), &checksum )
1357 || save_read( fd, cols, COL_NUM * sizeof( int ), &checksum )
1358 || save_read( fd, stacks, SUITS * sizeof( int ), &checksum ) )
1360 rb->splash( 2*HZ, "Error while loading saved game. Aborting." );
1361 retval = -2;
1363 else if( checksum != 42 )
1365 rb->splash( 2*HZ, "Save file was corrupted. Aborting." );
1366 retval = -3;
1369 rb->close( fd );
1370 delete_save_file();
1371 return retval;
1375 * The main game loop
1377 * If skipmenu is defined to SOLITAIRE_QUIT, the menu will be skipped and
1378 * game will resume.
1381 int solitaire( int skipmenu )
1384 int i,j;
1385 int button, lastbutton = 0;
1386 int c,h,prevcard;
1387 int biggest_col_length;
1389 rb->srand( *rb->current_tick );
1390 if( skipmenu != SOLITAIRE_QUIT )
1392 switch( solitaire_menu(false) )
1394 case MENU_QUIT:
1395 return SOLITAIRE_QUIT;
1397 case MENU_USB:
1398 return SOLITAIRE_USB;
1400 solitaire_init();
1403 while( true )
1405 rb->lcd_clear_display();
1407 /* get the biggest column length so that display can be "optimized" */
1408 biggest_col_length = 0;
1410 for(i=0;i<COL_NUM;i++)
1412 j = 0;
1413 c = cols[i];
1415 if( c != NOT_A_CARD )
1417 while( true )
1419 /* don't include the last card in the column length. */
1420 if( deck[c].next == NOT_A_CARD )
1422 break; /* no successor: get outta here. */
1424 else
1426 if( deck[c].known )
1427 j += 2;
1428 else
1429 j++;
1431 c = deck[c].next;
1433 /* make column distinguishable from an empty column,
1434 * and avoid division by zero while displaying */
1435 if( j == 0 )
1436 j = 1;
1438 if( j > biggest_col_length )
1439 biggest_col_length = j;
1442 /* check if there are cards remaining in the game. */
1443 /* if there aren't any, that means you won :) */
1444 if( biggest_col_length == 0 && rem == NOT_A_CARD )
1446 rb->lcd_update();
1447 rb->splash( HZ, "You Won :)" );
1448 return bouncing_cards();
1451 /* draw the columns */
1452 for( i = 0; i < COL_NUM; i++ )
1454 c = cols[i];
1455 j = CARD_START;
1456 while( true )
1458 if( c == NOT_A_CARD )
1460 /* draw the cursor on empty columns */
1461 if( cur_col == i )
1463 draw_cursor( MARGIN + i * (CARD_WIDTH
1464 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1465 j );
1467 break;
1470 draw_card( &deck[c], MARGIN + i * (CARD_WIDTH
1471 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1472 j, c == sel_card, c == cur_card );
1474 h = c;
1475 c = deck[c].next;
1476 if( c == NOT_A_CARD ) break;
1478 /* This is where we change the spacing between cards so that
1479 * they don't overflow out of the LCD */
1480 if( h == cur_card )
1481 j += SYMBOL_HEIGHT;
1482 else if( deck[h].known )
1483 j += min( SYMBOL_HEIGHT,
1484 2 * (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1485 / biggest_col_length );
1486 else
1487 j += min( SYMBOL_HEIGHT / 2,
1488 (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1489 / biggest_col_length );
1493 /* draw the stacks */
1494 for( i=0; i<SUITS; i++ )
1496 c = find_last_card( STACKS_COL + i );
1498 if( c != NOT_A_CARD )
1500 draw_card( &deck[c],
1501 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1502 MARGIN,
1503 c == sel_card, cur_col == STACKS_COL + i );
1505 else
1507 draw_empty_stack( i,
1508 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1509 MARGIN, cur_col == STACKS_COL + i );
1513 /* draw the remains */
1514 if( rem != NOT_A_CARD &&
1515 ( cur_rem == NOT_A_CARD || deck[cur_rem].next != NOT_A_CARD ) )
1517 /* gruik ! (we want to display a card back) */
1518 deck[rem].known = false;
1519 draw_card( &deck[rem], MARGIN, MARGIN, false, false );
1520 deck[rem].known = true;
1523 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1525 if( count_rem < 0 )
1527 prevcard = rem;
1528 count_rem = 0;
1529 while( prevcard != cur_rem && count_rem < cards_per_draw-1 )
1531 prevcard = deck[prevcard].next;
1532 count_rem++;
1535 prevcard = cur_rem;
1536 j = CARD_WIDTH+2*MARGIN+1;
1537 for( i = 0; i < count_rem; i++ )
1538 prevcard = find_prev_card(prevcard);
1539 for( i = 0; i <= count_rem; i++ )
1541 draw_card( &deck[prevcard], j,
1542 MARGIN, sel_card == prevcard,
1543 cur_card == prevcard );
1544 prevcard = deck[prevcard].next;
1545 j += CARD_WIDTH/2;
1548 if( ( cur_rem == NOT_A_CARD || rem == NOT_A_CARD )
1549 && cur_col == REM_COL )
1551 draw_cursor( CARD_WIDTH+2*MARGIN+1, MARGIN );
1554 rb->lcd_update();
1556 /* what to do when a key is pressed ... */
1557 button = rb->button_get( true );
1558 #if (CONFIG_KEYPAD == SANSA_E200_PAD) || (CONFIG_KEYPAD == SANSA_FUZE_PAD)
1559 if (button&(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD))
1560 button = button & (~BUTTON_REPEAT);
1561 #endif
1562 switch( button )
1564 /* move cursor to the last card of the previous column
1565 * or to the previous final stack
1566 * or to the remains stack */
1567 case SOL_RIGHT:
1568 #ifdef SOL_RIGHT_PRE
1569 if( lastbutton != SOL_RIGHT_PRE )
1570 break;
1571 #endif
1572 if( cur_col >= COL_NUM )
1574 cur_col = 0;
1576 else if( cur_col == COL_NUM - 1 )
1578 cur_col = REM_COL;
1580 else
1582 cur_col = (cur_col+1)%(REM_COL+1);
1584 if(cur_col == REM_COL)
1586 cur_card = cur_rem;
1587 break;
1589 cur_card = find_last_card( cur_col );
1590 break;
1592 /* move cursor to the last card of the next column
1593 * or to the next final stack
1594 * or to the remains stack */
1595 case SOL_LEFT:
1596 #ifdef SOL_LEFT_PRE
1597 if( lastbutton != SOL_LEFT_PRE )
1598 break;
1599 #endif
1600 if( cur_col == 0 )
1602 cur_col = REM_COL;
1604 else if( cur_col >= COL_NUM )
1606 cur_col = COL_NUM - 1;
1608 else
1610 cur_col = (cur_col + REM_COL)%(REM_COL+1);
1612 if( cur_col == REM_COL )
1614 cur_card = cur_rem;
1615 break;
1617 cur_card = find_last_card( cur_col );
1618 break;
1620 /* move cursor to card that's bellow */
1621 case SOL_DOWN:
1622 #ifdef SOL_DOWN_PRE
1623 if( lastbutton != SOL_DOWN_PRE )
1624 break;
1625 #else
1626 case SOL_DOWN|BUTTON_REPEAT:
1627 #endif
1628 if( cur_col >= COL_NUM )
1630 cur_col = (cur_col - COL_NUM + 1)%(SUITS + 1) + COL_NUM;
1631 if( cur_col == REM_COL )
1633 cur_card = cur_rem;
1635 else
1637 cur_card = find_last_card( cur_col );
1639 break;
1641 if( cur_card == NOT_A_CARD ) break;
1642 if( deck[cur_card].next != NOT_A_CARD )
1644 cur_card = deck[cur_card].next;
1646 else
1648 cur_card = cols[cur_col];
1649 while( !deck[ cur_card].known
1650 && deck[cur_card].next != NOT_A_CARD )
1652 cur_card = deck[cur_card].next;
1655 break;
1657 /* move cursor to card that's above */
1658 case SOL_UP:
1659 #ifdef SOL_UP_PRE
1660 if( lastbutton != SOL_UP_PRE )
1661 break;
1662 #else
1663 case SOL_UP|BUTTON_REPEAT:
1664 #endif
1665 if( cur_col >= COL_NUM )
1667 cur_col = (cur_col - COL_NUM + SUITS)%(SUITS + 1) + COL_NUM;
1668 if( cur_col == REM_COL )
1670 cur_card = cur_rem;
1672 else
1674 cur_card = find_last_card( cur_col );
1676 break;
1678 if( cur_card == NOT_A_CARD ) break;
1679 do {
1680 cur_card = find_prev_card( cur_card );
1681 if( cur_card == NOT_A_CARD )
1683 cur_card = find_last_card( cur_col );
1685 } while( deck[cur_card].next != NOT_A_CARD
1686 && !deck[cur_card].known );
1687 break;
1689 /* Try to put card under cursor on one of the stacks */
1690 case SOL_CUR2STACK:
1691 #ifdef SOL_CUR2STACK_PRE
1692 if( lastbutton != SOL_CUR2STACK_PRE )
1693 break;
1694 #endif
1695 move_card( deck[cur_card].suit + STACKS_COL, cur_card );
1696 break;
1698 /* Move cards arround, Uncover cards, ... */
1699 case SOL_MOVE:
1700 #ifdef SOL_MOVE_PRE
1701 if( lastbutton != SOL_MOVE_PRE )
1702 break;
1703 #endif
1705 if( sel_card == NOT_A_CARD )
1707 if( cur_card != NOT_A_CARD )
1709 if( deck[cur_card].next == NOT_A_CARD
1710 && !deck[cur_card].known )
1712 /* reveal a hidden card */
1713 deck[cur_card].known = true;
1715 else if( cur_col == REM_COL && cur_rem == NOT_A_CARD )
1717 break;
1719 else
1721 /* select a card */
1722 sel_card = cur_card;
1726 else if( sel_card == cur_card )
1728 /* unselect card or try putting card on
1729 * one of the 4 stacks */
1730 if( move_card( deck[sel_card].suit + COL_NUM, sel_card )
1731 == MOVE_OK && cur_col == REM_COL )
1733 cur_card = cur_rem;
1735 sel_card = NOT_A_CARD;
1737 else
1739 /* try moving cards */
1740 /* The flexible move must not be used from the remains stack. */
1741 if (find_card_col(sel_card) == REM_COL)
1743 if (move_card( cur_col, sel_card ) == MOVE_NOT_OK)
1744 sel_card = NOT_A_CARD;
1746 else
1748 do {
1749 if (move_card( cur_col, sel_card) == MOVE_OK)
1750 break;
1751 sel_card = find_prev_card(sel_card);
1752 } while (sel_card != NOT_A_CARD);
1755 break;
1757 /* If the card on the top of the remains can be put where
1758 * the cursor is, go ahead */
1759 case SOL_REM2CUR:
1760 #ifdef SOL_REM2CUR_PRE
1761 if( lastbutton != SOL_REM2CUR_PRE )
1762 break;
1763 #endif
1764 move_card( cur_col, cur_rem );
1765 break;
1767 /* If the card on top of the remains can be put on one
1768 * of the stacks, do so */
1769 case SOL_REM2STACK:
1770 #ifdef SOL_REM2STACK_PRE
1771 if( lastbutton != SOL_REM2STACK_PRE )
1772 break;
1773 #endif
1774 move_card( deck[cur_rem].suit + COL_NUM, cur_rem );
1775 break;
1777 #ifdef SOL_REM
1778 case SOL_REM:
1779 if( sel_card != NOT_A_CARD )
1781 /* unselect selected card */
1782 sel_card = NOT_A_CARD;
1783 break;
1785 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1787 sel_card = cur_rem;
1788 break;
1790 break;
1791 #endif
1793 /* unselect selected card or ...
1794 * draw new cards from the remains of the deck */
1795 case SOL_DRAW:
1796 #ifdef SOL_DRAW_PRE
1797 if( lastbutton != SOL_DRAW_PRE )
1798 break;
1799 #endif
1800 if( sel_card != NOT_A_CARD )
1802 /* unselect selected card */
1803 sel_card = NOT_A_CARD;
1804 break;
1806 if( rem != NOT_A_CARD )
1808 int cur_rem_old = cur_rem;
1809 count_rem = -1;
1810 /* draw new cards form the remains of the deck */
1811 if( cur_rem == NOT_A_CARD )
1813 /*if the cursor card is null*/
1814 cur_rem = rem;
1815 i = cards_per_draw - 1;
1816 count_rem++;
1818 else
1820 i = cards_per_draw;
1823 while( i > 0 && deck[cur_rem].next != NOT_A_CARD )
1825 cur_rem = deck[cur_rem].next;
1826 i--;
1827 count_rem++;
1829 /* test if any cards are really left on
1830 * the remains' stack */
1831 if( i == cards_per_draw )
1833 cur_rem = NOT_A_CARD;
1834 count_rem = -1;
1836 /* if cursor was on remains' stack when new cards were
1837 * drawn, put cursor on top of remains' stack */
1838 if( cur_col == REM_COL && cur_card == cur_rem_old )
1840 cur_card = cur_rem;
1841 sel_card = NOT_A_CARD;
1844 break;
1846 /* Show the menu */
1847 #ifdef SOL_RC_QUIT
1848 case SOL_RC_QUIT:
1849 #endif
1850 case SOL_QUIT:
1851 switch( solitaire_menu(true) )
1853 case MENU_SAVE_AND_QUIT:
1854 return SOLITAIRE_SAVE_AND_QUIT;
1856 case MENU_QUIT:
1857 return SOLITAIRE_QUIT;
1859 case MENU_USB:
1860 return SOLITAIRE_USB;
1862 break;
1864 case SYS_POWEROFF:
1865 return SOLITAIRE_SAVE_AND_QUIT;
1867 default:
1868 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1869 return SOLITAIRE_USB;
1870 break;
1873 if( button != BUTTON_NONE )
1874 lastbutton = button;
1876 /* fix incoherences concerning cur_col and cur_card */
1877 c = find_card_col( cur_card );
1878 if( c != NOT_A_COL && c != cur_col )
1879 cur_card = find_last_card( cur_col );
1881 if( cur_card == NOT_A_CARD
1882 && find_last_card( cur_col ) != NOT_A_CARD )
1883 cur_card = find_last_card( cur_col );
1885 rb->yield();
1890 * Plugin entry point
1893 enum plugin_status plugin_start(const void* parameter )
1895 int result;
1897 /* plugin init */
1898 (void)parameter;
1900 configfile_load(CONFIG_FILENAME, config,
1901 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1902 rb->memcpy(&sol, &sol_disk, sizeof(sol)); /* copy to running config */
1904 if( load_game() == 0 )
1906 rb->splash( HZ, "Resuming saved game." );
1907 result = SOLITAIRE_QUIT;
1909 else
1910 result = SOLITAIRE_WIN;
1912 /* play the game :)
1913 * Keep playing if a game was won (that means display the menu after
1914 * winning instead of quiting) */
1915 while( ( result = solitaire( result ) ) == SOLITAIRE_WIN );
1917 if( result != SOLITAIRE_QUIT )
1918 /* result == SOLITAIRE_USB || result == SOLITAIRE_SAVE_AND_QUIT */
1919 save_game();
1921 if (rb->memcmp(&sol, &sol_disk, sizeof(sol))) /* save settings if changed */
1923 rb->memcpy(&sol_disk, &sol, sizeof(sol));
1924 configfile_save(CONFIG_FILENAME, config,
1925 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1928 /* Exit the plugin */
1929 return ( result == SOLITAIRE_USB ) ? PLUGIN_USB_CONNECTED : PLUGIN_OK;
1932 #endif