Fix yellow.
[kugel-rb.git] / apps / plugins / solitaire.c
bloba87311035feb1580c6669638c4b6cad4beac01c2
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 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
493 # define SOL_QUIT BUTTON_REC
494 # define SOL_UP BUTTON_UP
495 # define SOL_DOWN BUTTON_DOWN
496 # define SOL_LEFT BUTTON_MENU
497 # define SOL_RIGHT BUTTON_ENTER
498 # define SOL_MOVE_PRE BUTTON_REW
499 # define SOL_MOVE BUTTON_FF
500 # define SOL_DRAW (BUTTON_ENTER | BUTTON_PLAY)
501 # define SOL_REM2CUR (BUTTON_MENU | BUTTON_REPEAT)
502 # define SOL_CUR2STACK BUTTON_PLAY
503 # define SOL_REM2STACK (BUTTON_PLAY | BUTTON_REPEAT)
504 # define HK_MOVE "FF"
505 # define HK_DRAW "ENTER + PLAY"
506 # define HK_REM2CUR "EMNU.."
507 # define HK_CUR2STACK "PLAY"
508 # define HK_REM2STACK "PLAY...."
510 #else
511 #error No keymap defined!
512 #endif
514 #ifdef HAVE_TOUCHSCREEN
515 //#ifndef SOL_QUIT
516 //# define SOL_QUIT BUTTON_TOPLEFT
517 //endif
518 #ifndef SOL_UP
519 # define SOL_UP BUTTON_TOPMIDDLE
520 #endif
521 #ifndef SOL_DOWN
522 # define SOL_DOWN BUTTON_BOTTOMMIDDLE
523 #endif
524 #ifndef SOL_LEFT
525 # define SOL_LEFT BUTTON_MIDLEFT
526 #endif
527 #ifndef SOL_RIGHT
528 # define SOL_RIGHT BUTTON_MIDRIGHT
529 #endif
530 #ifndef SOL_MOVE
531 # define SOL_MOVE BUTTON_CENTER
532 # define HK_MOVE "CENTRE"
533 #endif
534 #ifndef SOL_DRAW
535 # define SOL_DRAW BUTTON_TOPLEFT
536 # define HK_DRAW "TOPLEFT"
537 #endif
538 #ifndef SOL_REM2CUR
539 # define SOL_REM2CUR BUTTON_TOPRIGHT
540 # define HK_REM2CUR "TOPRIGHT"
541 #endif
542 #ifndef SOL_CUR2STACK
543 # define SOL_CUR2STACK BUTTON_BOTTOMLEFT
544 # define HK_CUR2STACK "BOTTOMLEFT"
545 #endif
546 #ifndef SOL_REM2STACK
547 # define SOL_REM2STACK BUTTON_BOTTOMRIGHT
548 # define HK_REM2STACK "BOTTOMRIGHT"
549 #endif
551 #endif
553 #ifndef HK_LR
554 # define HK_LR "LEFT/RIGHT"
555 #endif
556 #ifndef HK_UD
557 # define HK_UD "UP/DOWN"
558 #endif
561 * Misc constants, graphics and other defines
564 #include "pluginbitmaps/card_back.h"
565 #include "pluginbitmaps/card_deck.h"
566 #include "pluginbitmaps/solitaire_suitsi.h"
568 #define CARD_GFX_WIDTH BMPWIDTH_card_back
569 #define CARD_GFX_HEIGHT BMPHEIGHT_card_back
570 #define CARD_WIDTH (BMPWIDTH_card_back+2)
571 #define CARD_HEIGHT (BMPHEIGHT_card_back+2)
573 #if LCD_WIDTH >= 640
574 # define MARGIN 4
575 # define LARGE_CARD
576 # define SYMBOL_HEIGHT 24
578 #elif LCD_WIDTH >= 320
579 # define MARGIN 4
580 # define LARGE_CARD
581 # define SYMBOL_HEIGHT 12
582 #elif LCD_WIDTH >= 220
583 # define MARGIN 3
584 # define LARGE_CARD
585 # define SYMBOL_HEIGHT 12
586 #elif LCD_WIDTH >= 160
587 # define MARGIN 2
588 # define SYMBOL_HEIGHT 11
589 #elif LCD_WIDTH >= 128
590 # define MARGIN 1
591 # define SYMBOL_HEIGHT 10
592 #else
593 # define MARGIN 0
594 # define SYMBOL_HEIGHT 8
595 #endif
597 #define CARD_START (CARD_HEIGHT+2*MARGIN+1)
599 /* background color */
600 #ifdef HAVE_LCD_COLOR
601 # define BACKGROUND_COLOR LCD_RGBPACK(0,157,0)
602 # define FRAME_COLOR LCD_RGBPACK(23,119,218)
603 #elif LCD_DEPTH > 1
604 # define BACKGROUND_COLOR LCD_WHITE
605 # define FRAME_COLOR LCD_BLACK
606 #endif
609 #define CONFIG_FILENAME "sol.cfg"
611 #define NOT_A_CARD -1
613 /* number of cards per suit */
614 #define CARDS_PER_SUIT 13
616 /* number of suits */
617 #define SUITS 4
619 #define NUM_CARDS ( CARDS_PER_SUIT * SUITS )
621 /* number of columns */
622 #define COL_NUM 7
624 /* pseudo column numbers to be used for cursor coordinates */
625 /* columns COL_NUM to COL_NUM + SUITS - 1 correspond to the final stacks */
626 #define STACKS_COL COL_NUM
627 /* column COL_NUM + SUITS corresponds to the remains' stack */
628 #define REM_COL (STACKS_COL + SUITS)
630 #define NOT_A_COL -1
632 typedef struct
634 signed char suit;
635 signed char num;
636 bool known : 1;
637 bool used : 1; /* this is what is used when dealing cards */
638 signed char next;
639 } card_t;
643 * LCD card drawing routines
646 static void draw_cursor( int x, int y )
648 rb->lcd_set_drawmode( DRMODE_COMPLEMENT );
649 rb->lcd_fillrect( x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
650 #ifdef LARGE_CARD
651 rb->lcd_drawpixel( x+1, y+1 );
652 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
653 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
654 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
655 #endif
656 rb->lcd_set_drawmode( DRMODE_SOLID );
659 /* Draw a card's border, select it if it's selected and draw the cursor
660 * if the cursor is currently over the card */
661 static void draw_card_ext( int x, int y, bool selected, bool cursor )
663 #if LCD_DEPTH > 1
664 int oldfg = rb->lcd_get_foreground();
666 rb->lcd_set_foreground( LCD_BLACK );
667 #endif
668 #ifdef LARGE_CARD
669 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y );
670 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y+CARD_HEIGHT-1 );
671 rb->lcd_vline( x, y+2, y+CARD_HEIGHT-3 );
672 rb->lcd_vline( x+CARD_WIDTH-1, y+2, y+CARD_HEIGHT-3 );
673 rb->lcd_drawpixel( x+1, y+1 );
674 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
675 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
676 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
677 #else
678 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y );
679 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y+CARD_HEIGHT-1 );
680 rb->lcd_vline( x, y+1, y+CARD_HEIGHT-2 );
681 rb->lcd_vline( x+CARD_WIDTH-1, y+1, y+CARD_HEIGHT-2 );
682 #endif
684 if( selected )
686 #if LCD_DEPTH > 1
687 rb->lcd_set_foreground( FRAME_COLOR );
688 #endif
689 rb->lcd_drawrect( x+1, y+1, CARD_WIDTH-2, CARD_HEIGHT-2 );
690 #ifdef LARGE_CARD
691 rb->lcd_drawrect( x+2, y+2, CARD_WIDTH-4, CARD_HEIGHT-4 );
692 #endif
694 #if LCD_DEPTH > 1
695 rb->lcd_set_foreground( oldfg );
696 #endif
698 if( cursor )
700 draw_cursor( x, y );
704 /* Draw a card's inner graphics */
705 static void draw_card( card_t *card, int x, int y,
706 bool selected, bool cursor )
708 if( card->known )
710 rb->lcd_bitmap_part( card_deck, CARD_GFX_WIDTH * card->num,
711 CARD_GFX_HEIGHT * card->suit,
712 STRIDE(SCREEN_MAIN,
713 BMPWIDTH_card_deck, BMPHEIGHT_card_deck),
714 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
716 else
718 rb->lcd_bitmap( card_back, x+1, y+1,
719 CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
721 draw_card_ext( x, y, selected, cursor );
724 /* Draw an empty stack */
725 static void draw_empty_stack( int s, int x, int y, bool cursor )
727 rb->lcd_bitmap_part( solitaire_suitsi, 0,
728 CARD_GFX_HEIGHT * s,
729 STRIDE( SCREEN_MAIN,
730 BMPWIDTH_solitaire_suitsi, BMPHEIGHT_solitaire_suitsi),
731 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
733 draw_card_ext( x, y, false, cursor );
736 /* Help */
737 static bool solitaire_help( void )
739 static char* help_text[] = {
740 "Solitaire", "", "Controls", "",
741 HK_LR ":", "Move", "the", "cursor", "to", "the",
742 "previous/", "next", "column.", "",
743 HK_UD ":", "Move", "the", "cursor", "up/", "down", "in", "the",
744 "column.", "",
745 HK_MOVE ":", "Select", "cards,", "move", "cards...", "",
746 HK_DRAW ":", "Deselect", "a", "card", "if", "it", "was", "selected.",
747 "Else", "draw", "new", "card(s)", "from", "the", "remains",
748 "stack.", "", "",
749 "Shortcuts", "",
750 HK_REM2CUR ":", "Put", "the", "card", "on", "top", "of", "the",
751 "remains", "stack", "on", "top", "of", "the", "cursor.", "",
752 HK_CUR2STACK ":", "Put", "the", "card", "under", "the", "cursor",
753 "on", "one", "of", "the", "4", "final", "stacks.", "",
754 HK_REM2STACK ":", "Put", "the", "card", "on", "top", "of", "the",
755 "remains", "stack", "on", "one", "of", "the", "4", "final",
756 "stacks."
758 static struct style_text formation[]={
759 { 0, TEXT_CENTER|TEXT_UNDERLINE },
760 { 2, C_RED },
761 { 48, C_RED },
762 LAST_STYLE_ITEM
764 if (display_text(ARRAYLEN(help_text), help_text, formation, NULL, true))
765 return true;
766 return false;
770 * Custom menu / options
773 #define CFGFILE_VERSION 0
775 struct sol_config {
776 int draw_type;
779 struct sol_config sol_disk = {0};
780 struct sol_config sol;
782 static struct configdata config[] = {
783 { TYPE_INT, 0, 1, { .int_p = &sol_disk.draw_type }, "draw_type", NULL }
786 static const struct opt_items drawcards[2] = {
787 { "Draw Three Cards", -1 },
788 { "Draw One Card", -1 },
791 void solitaire_init(void);
793 /* menu return codes */
794 enum { MENU_RESUME, MENU_SAVE_AND_QUIT, MENU_QUIT, MENU_USB };
796 static bool _ingame;
797 int solitaire_menu_cb(int action, const struct menu_item_ex *this_item)
799 int i = (intptr_t)this_item;
800 if( action == ACTION_REQUEST_MENUITEM )
802 if((!_ingame && (i==0 || i==5)) || ( _ingame && i==2 ))
803 return ACTION_EXIT_MENUITEM;
805 return action;
808 int solitaire_menu(bool in_game)
810 int selected = 0;
811 int result = -1;
813 MENUITEM_STRINGLIST(menu, "Solitaire Menu", solitaire_menu_cb,
814 "Resume Game", "Start New Game",
815 "Draw Cards Option",
816 "Help", "Playback Control",
817 "Save and Quit", "Quit");
818 _ingame = in_game;
820 while (result < 0)
822 switch (rb->do_menu(&menu, &selected, NULL, false))
824 default:
825 result = MENU_RESUME;
826 break;
828 case MENU_ATTACHED_USB:
829 result = MENU_USB;
830 break;
832 case 0:
833 result = MENU_RESUME;
834 break;
836 case 1:
837 solitaire_init();
838 result = MENU_RESUME;
839 break;
841 case 2:
842 if (rb->set_option("Draw Cards Option", &sol.draw_type,
843 INT, drawcards, 2, NULL))
844 result = MENU_USB;
845 break;
847 case 3:
848 if (solitaire_help())
849 result = MENU_USB;
850 break;
852 case 4:
853 playback_control(NULL);
854 break;
856 case 5:
857 result = MENU_SAVE_AND_QUIT;
858 break;
860 case 6:
861 result = MENU_QUIT;
862 break;
865 return result;
869 * Global variables
872 /* player's cursor */
873 int cur_card;
874 /* player's cursor column num */
875 int cur_col;
877 /* selected card */
878 int sel_card;
880 /* the deck */
881 card_t deck[ NUM_CARDS ];
883 /* the remaining cards */
884 /* first card of the remains' stack */
885 int rem;
886 /* upper visible card from the remains' stack */
887 int cur_rem;
888 /* number of cards drawn from the remains stack - 1 */
889 int count_rem;
890 /* number of cards per draw of the remains' stack */
891 int cards_per_draw;
893 /* the 7 game columns */
894 int cols[COL_NUM];
895 /* the 4 final stacks */
896 int stacks[SUITS];
899 * Card handling routines
902 int next_random_card( card_t *deck )
904 int i,r;
906 r = rb->rand()%(NUM_CARDS)+1;
907 i = 0;
909 while( r>0 )
911 i = (i + 1)%(NUM_CARDS);
912 if( !deck[i].used ) r--;
915 deck[i].used = true;
917 return i;
921 /* initialize the game */
922 void solitaire_init( void )
925 int c;
926 int i, j;
928 /* number of cards that are drawn on the remains' stack (by pressing F2) */
929 if( sol.draw_type == 0 )
931 cards_per_draw = 3;
933 else
935 cards_per_draw = 1;
938 /* init deck */
939 for( i=0; i<SUITS; i++ )
941 for( j=0; j<CARDS_PER_SUIT; j++ )
943 #define card deck[i*CARDS_PER_SUIT+j]
944 card.suit = i;
945 card.num = j;
946 card.known = true;
947 card.used = false;
948 card.next = NOT_A_CARD;
949 #undef card
953 /* deal the cards ... */
954 /* ... in the columns */
955 for( i=0; i<COL_NUM; i++ )
957 c = NOT_A_CARD;
958 for( j=0; j<=i; j++ )
960 if( c == NOT_A_CARD )
962 cols[i] = next_random_card( deck );
963 c = cols[i];
965 else
967 deck[c].next = next_random_card( deck );
968 c = deck[c].next;
970 if( j < i )
971 deck[c].known = false;
975 /* ... shuffle what's left of the deck */
976 rem = next_random_card(deck);
977 c = rem;
979 for( i=1; i < NUM_CARDS - COL_NUM * (COL_NUM + 1)/2; i++ )
981 deck[c].next = next_random_card( deck );
982 c = deck[c].next;
985 /* we now finished dealing the cards. The game can start ! (at last) */
987 /* init the stack */
988 for( i = 0; i<SUITS; i++ )
990 stacks[i] = NOT_A_CARD;
993 /* the cursor starts on upper left card */
994 cur_card = cols[0];
995 cur_col = 0;
997 /* no card is selected */
998 sel_card = NOT_A_CARD;
1000 /* init the remainder */
1001 cur_rem = NOT_A_CARD;
1003 count_rem = -1;
1006 /* find the column number in which 'card' can be found */
1007 int find_card_col( int card )
1009 int i;
1010 int c;
1012 if( card == NOT_A_CARD ) return NOT_A_COL;
1014 for( i=0; i<COL_NUM; i++ )
1016 c = cols[i];
1017 while( c != NOT_A_CARD )
1019 if( c == card ) return i;
1020 c = deck[c].next;
1024 for( i=0; i<SUITS; i++ )
1026 c = stacks[i];
1027 while( c != NOT_A_CARD )
1029 if( c == card ) return STACKS_COL + i;
1030 c = deck[c].next;
1034 return REM_COL;
1037 /* find the card preceding 'card' */
1038 /* if it doesn't exist, return NOT_A_CARD */
1039 int find_prev_card( int card ){
1040 int i;
1042 for( i=0; i < NUM_CARDS; i++ )
1044 if( deck[i].next == card ) return i;
1047 return NOT_A_CARD;
1050 /* find the last card of a given column */
1051 int find_last_card( int col )
1053 int c;
1055 if( col < COL_NUM )
1057 c = cols[col];
1059 else if( col < REM_COL )
1061 c = stacks[col - STACKS_COL];
1063 else
1065 c = cur_rem;
1068 if(c == NOT_A_CARD)
1069 return c;
1070 else
1072 while(deck[c].next != NOT_A_CARD)
1073 c = deck[c].next;
1074 return c;
1078 enum move { MOVE_OK, MOVE_NOT_OK };
1080 enum move move_card( int dest_col, int src_card )
1082 /* the column on which to take src_card */
1083 int src_col;
1085 /* the last card of dest_col */
1086 int dest_card;
1088 /* the card under src_card */
1089 int src_card_prev;
1091 /* you can't move no card (at least, it doesn't have any consequence) */
1092 if( src_card == NOT_A_CARD ) return MOVE_NOT_OK;
1093 /* you can't put a card back on the remains' stack */
1094 if( dest_col == REM_COL ) return MOVE_NOT_OK;
1095 /* you can't move an unknown card */
1096 if( !deck[src_card].known ) return MOVE_NOT_OK;
1098 src_col = find_card_col( src_card );
1099 dest_card = find_last_card( dest_col );
1100 src_card_prev = find_prev_card( src_card );
1102 /* you can't move more than one card at a time from the final stack */
1103 /* to the rest of the game */
1104 if( src_col >= COL_NUM && src_col < REM_COL
1105 && deck[src_card].next != NOT_A_CARD )
1107 return MOVE_NOT_OK;
1110 /* if we (that means dest) are on one of the 7 columns ... */
1111 if( dest_col < COL_NUM )
1113 /* ... check is we are on an empty color and that the src is a king */
1114 if( dest_card == NOT_A_CARD
1115 && deck[src_card].num == CARDS_PER_SUIT - 1 )
1117 /* this is a winning combination */
1118 cols[dest_col] = src_card;
1120 /* ... or check if the cards follow one another and have
1121 * different colorsuit */
1122 else if(( deck[dest_card].suit + deck[src_card].suit)%2==1
1123 && deck[dest_card].num == deck[src_card].num + 1 )
1125 /* this is a winning combination */
1126 deck[dest_card].next = src_card;
1128 /* ... or, humpf, well that's not good news */
1129 else
1131 /* this is not a winning combination */
1132 return MOVE_NOT_OK;
1135 /* if we are on one of the 4 final stacks ... */
1136 else if( dest_col < REM_COL )
1138 /* ... check if we are on an empty stack... */
1139 if( dest_card == NOT_A_CARD )
1141 /* ... and the src is an ace and this is the correct final stack */
1142 if( deck[src_card].num == 0
1143 && deck[src_card].suit == dest_col - STACKS_COL )
1145 /* this is a winning combination */
1146 stacks[dest_col - STACKS_COL] = src_card;
1148 else
1150 /* this is not a winning combination */
1151 return MOVE_NOT_OK;
1154 else /* non-empty stack */
1156 /* ...check if the cards follow one another, have the same suit and
1157 * {that src has no .next element or is from the remains' stack} */
1158 if( deck[dest_card].suit == deck[src_card].suit
1159 && deck[dest_card].num + 1 == deck[src_card].num
1160 && (deck[src_card].next == NOT_A_CARD || src_col == REM_COL) )
1162 /* this is a winning combination */
1163 deck[dest_card].next = src_card;
1165 /* ... or, well that's not good news */
1166 else
1168 /* this is not a winning combination */
1169 return MOVE_NOT_OK;
1173 /* if we are on the remains' stack */
1174 else
1176 /* you can't move a card back to the remains' stack */
1177 return MOVE_NOT_OK;
1180 /* if the src card is from the remains' stack, we don't want to take
1181 * the following cards */
1182 if( src_col == REM_COL )
1184 /* if src card is the first card from the stack */
1185 if( src_card_prev == NOT_A_CARD )
1187 rem = deck[src_card].next;
1189 /* if src card is not the first card from the stack */
1190 else
1192 deck[src_card_prev].next = deck[src_card].next;
1194 deck[src_card].next = NOT_A_CARD;
1195 cur_rem = src_card_prev;
1196 count_rem--;
1198 /* if the src card is from somewhere else, just take everything */
1199 else
1201 if( src_card_prev == NOT_A_CARD )
1203 if( src_col < COL_NUM )
1205 cols[src_col] = NOT_A_CARD;
1207 else
1209 stacks[src_col - STACKS_COL] = NOT_A_CARD;
1212 else
1214 deck[src_card_prev].next = NOT_A_CARD;
1215 deck[src_card_prev].known = true;
1218 sel_card = NOT_A_CARD;
1219 /* tada ! */
1220 return MOVE_OK;
1223 enum { SOLITAIRE_WIN, SOLITAIRE_SAVE_AND_QUIT, SOLITAIRE_QUIT, SOLITAIRE_USB };
1226 * Bouncing cards at the end of the game
1229 #define BC_ACCEL ((1<<16)*LCD_HEIGHT/128)
1230 #define BC_MYSPEED (6*BC_ACCEL)
1231 #define BC_MXSPEED (6*LCD_HEIGHT/128)
1233 int bouncing_cards( void )
1235 int i, j, x, vx, y, fp_y, fp_vy, button;
1237 /* flush the button queue */
1238 while( ( button = rb->button_get( false ) ) != BUTTON_NONE )
1240 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1241 return SOLITAIRE_USB;
1244 /* fun stuff :) */
1245 for( i = CARDS_PER_SUIT-1; i>=0; i-- )
1247 for( j = 0; j < SUITS; j++ )
1249 x = LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*j+j+1;
1250 fp_y = MARGIN<<16;
1252 #if LCD_WIDTH > 200
1253 vx = rb->rand() % (4*BC_MXSPEED/3-2) - BC_MXSPEED;
1254 if( vx >= -1 )
1255 vx += 3;
1256 #else
1257 vx = rb->rand() % (4*BC_MXSPEED/3) - BC_MXSPEED;
1258 if( vx >= 0 )
1259 vx++;
1260 #endif
1262 fp_vy = -rb->rand() % BC_MYSPEED;
1264 while( x < LCD_WIDTH && x + CARD_WIDTH > 0 )
1266 fp_vy += BC_ACCEL;
1267 x += vx;
1268 fp_y += fp_vy;
1269 if( fp_y >= (LCD_HEIGHT-CARD_HEIGHT) << 16 )
1271 fp_vy = -fp_vy*4/5;
1272 fp_y = (LCD_HEIGHT-CARD_HEIGHT) << 16;
1274 y = fp_y >> 16;
1275 draw_card( &deck[j*CARDS_PER_SUIT+i], x, y,
1276 false, false );
1277 rb->lcd_update_rect( x<0?0:x, y<0?0:y,
1278 CARD_WIDTH, CARD_HEIGHT );
1280 button = rb->button_get_w_tmo( 2 );
1281 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1282 return SOLITAIRE_USB;
1283 if( button == SOL_QUIT || button == SOL_MOVE )
1284 return SOLITAIRE_WIN;
1288 return SOLITAIRE_WIN;
1292 * Game save/load routines
1294 void get_save_filename( char *buf )
1296 #ifdef APPLICATION
1297 rb->snprintf(buf, sizeof(buf), PLUGIN_DATA_DIR "/sol.save");
1298 #else
1299 char *s;
1300 rb->strcpy( buf, rb->plugin_get_current_filename() );
1301 s = rb->strrchr( buf, '/' ) + 1;
1302 *s = '\0';
1303 rb->strcat( s, "sol.save" );
1304 #endif
1307 int open_save_file( int flags )
1309 char buf[MAX_PATH];
1310 get_save_filename( buf );
1311 return rb->open( buf, flags, 0666);
1314 void delete_save_file( void )
1316 char buf[MAX_PATH];
1317 get_save_filename( buf );
1318 rb->remove( buf );
1321 #ifdef write
1322 # undef write
1323 #endif
1324 int save_write( int fd, const void *buf, size_t count, int *checksum )
1326 size_t i;
1327 if( rb->write( fd, buf, count ) < (ssize_t)count )
1328 return 1;
1329 for( i = 0; i < count; i++ )
1330 *checksum += (int)(((const char *)buf)[i]);
1331 return 0;
1334 #ifdef read
1335 # undef read
1336 #endif
1337 int save_read( int fd, void *buf, size_t count, int *checksum )
1339 size_t i;
1340 if( rb->read( fd, buf, count ) < (ssize_t)count )
1341 return 1;
1342 for( i = 0; i < count; i++ )
1343 *checksum -= (int)(((const char *)buf)[i]);
1344 return 0;
1347 int save_game( void )
1349 int fd = open_save_file( O_CREAT|O_WRONLY|O_TRUNC );
1350 int checksum = 42;
1351 if( fd < 0 )
1352 return -1;
1353 if( save_write( fd, &cur_card, sizeof( int ), &checksum )
1354 || save_write( fd, &cur_col, sizeof( int ), &checksum )
1355 || save_write( fd, &sel_card, sizeof( int ), &checksum )
1356 || save_write( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1357 || save_write( fd, &rem, sizeof( int ), &checksum )
1358 || save_write( fd, &cur_rem, sizeof( int ), &checksum )
1359 || save_write( fd, &count_rem, sizeof( int ), &checksum )
1360 || save_write( fd, &cards_per_draw, sizeof( int ), &checksum )
1361 || save_write( fd, cols, COL_NUM * sizeof( int ), &checksum )
1362 || save_write( fd, stacks, SUITS * sizeof( int ), &checksum )
1363 || ( rb->write( fd, &checksum, sizeof( int ) ) < (ssize_t)(sizeof( int ) ) ) )
1365 rb->close( fd );
1366 rb->splash( 2*HZ, "Error while saving game. Aborting." );
1367 return -2;
1369 rb->close( fd );
1370 return 0;
1373 int load_game( void )
1375 int checksum, retval;
1377 int fd = open_save_file( O_RDONLY );
1378 if( fd < 0 )
1379 return -1;
1381 retval = 0; /* Assume good case */
1382 if( ( rb->lseek( fd, -sizeof( int ), SEEK_END ) == -((ssize_t)sizeof( int ))-1 )
1383 || ( rb->read( fd, &checksum, sizeof( int ) ) < ((ssize_t)sizeof( int )) )
1384 || ( rb->lseek( fd, 0, SEEK_SET ) == -1 )
1385 || save_read( fd, &cur_card, sizeof( int ), &checksum )
1386 || save_read( fd, &cur_col, sizeof( int ), &checksum )
1387 || save_read( fd, &sel_card, sizeof( int ), &checksum )
1388 || save_read( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1389 || save_read( fd, &rem, sizeof( int ), &checksum )
1390 || save_read( fd, &cur_rem, sizeof( int ), &checksum )
1391 || save_read( fd, &count_rem, sizeof( int ), &checksum )
1392 || save_read( fd, &cards_per_draw, sizeof( int ), &checksum )
1393 || save_read( fd, cols, COL_NUM * sizeof( int ), &checksum )
1394 || save_read( fd, stacks, SUITS * sizeof( int ), &checksum ) )
1396 rb->splash( 2*HZ, "Error while loading saved game. Aborting." );
1397 retval = -2;
1399 else if( checksum != 42 )
1401 rb->splash( 2*HZ, "Save file was corrupted. Aborting." );
1402 retval = -3;
1405 rb->close( fd );
1406 delete_save_file();
1407 return retval;
1411 * The main game loop
1413 * If skipmenu is defined to SOLITAIRE_QUIT, the menu will be skipped and
1414 * game will resume.
1417 int solitaire( int skipmenu )
1420 int i,j;
1421 int button, lastbutton = 0;
1422 int c,h,prevcard;
1423 int biggest_col_length;
1425 rb->srand( *rb->current_tick );
1426 if( skipmenu != SOLITAIRE_QUIT )
1428 switch( solitaire_menu(false) )
1430 case MENU_QUIT:
1431 return SOLITAIRE_QUIT;
1433 case MENU_USB:
1434 return SOLITAIRE_USB;
1436 solitaire_init();
1439 while( true )
1441 rb->lcd_clear_display();
1443 /* get the biggest column length so that display can be "optimized" */
1444 biggest_col_length = 0;
1446 for(i=0;i<COL_NUM;i++)
1448 j = 0;
1449 c = cols[i];
1451 if( c != NOT_A_CARD )
1453 while( true )
1455 /* don't include the last card in the column length. */
1456 if( deck[c].next == NOT_A_CARD )
1458 break; /* no successor: get outta here. */
1460 else
1462 if( deck[c].known )
1463 j += 2;
1464 else
1465 j++;
1467 c = deck[c].next;
1469 /* make column distinguishable from an empty column,
1470 * and avoid division by zero while displaying */
1471 if( j == 0 )
1472 j = 1;
1474 if( j > biggest_col_length )
1475 biggest_col_length = j;
1478 /* check if there are cards remaining in the game. */
1479 /* if there aren't any, that means you won :) */
1480 if( biggest_col_length == 0 && rem == NOT_A_CARD )
1482 rb->lcd_update();
1483 rb->splash( HZ, "You Won :)" );
1484 return bouncing_cards();
1487 /* draw the columns */
1488 for( i = 0; i < COL_NUM; i++ )
1490 c = cols[i];
1491 j = CARD_START;
1492 while( true )
1494 if( c == NOT_A_CARD )
1496 /* draw the cursor on empty columns */
1497 if( cur_col == i )
1499 draw_cursor( MARGIN + i * (CARD_WIDTH
1500 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1501 j );
1503 break;
1506 draw_card( &deck[c], MARGIN + i * (CARD_WIDTH
1507 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1508 j, c == sel_card, c == cur_card );
1510 h = c;
1511 c = deck[c].next;
1512 if( c == NOT_A_CARD ) break;
1514 /* This is where we change the spacing between cards so that
1515 * they don't overflow out of the LCD */
1516 if( h == cur_card )
1517 j += SYMBOL_HEIGHT;
1518 else if( deck[h].known )
1519 j += min( SYMBOL_HEIGHT,
1520 2 * (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1521 / biggest_col_length );
1522 else
1523 j += min( SYMBOL_HEIGHT / 2,
1524 (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1525 / biggest_col_length );
1529 /* draw the stacks */
1530 for( i=0; i<SUITS; i++ )
1532 c = find_last_card( STACKS_COL + i );
1534 if( c != NOT_A_CARD )
1536 draw_card( &deck[c],
1537 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1538 MARGIN,
1539 c == sel_card, cur_col == STACKS_COL + i );
1541 else
1543 draw_empty_stack( i,
1544 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1545 MARGIN, cur_col == STACKS_COL + i );
1549 /* draw the remains */
1550 if( rem != NOT_A_CARD &&
1551 ( cur_rem == NOT_A_CARD || deck[cur_rem].next != NOT_A_CARD ) )
1553 /* gruik ! (we want to display a card back) */
1554 deck[rem].known = false;
1555 draw_card( &deck[rem], MARGIN, MARGIN, false, false );
1556 deck[rem].known = true;
1559 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1561 if( count_rem < 0 )
1563 prevcard = rem;
1564 count_rem = 0;
1565 while( prevcard != cur_rem && count_rem < cards_per_draw-1 )
1567 prevcard = deck[prevcard].next;
1568 count_rem++;
1571 prevcard = cur_rem;
1572 j = CARD_WIDTH+2*MARGIN+1;
1573 for( i = 0; i < count_rem; i++ )
1574 prevcard = find_prev_card(prevcard);
1575 for( i = 0; i <= count_rem; i++ )
1577 draw_card( &deck[prevcard], j,
1578 MARGIN, sel_card == prevcard,
1579 cur_card == prevcard );
1580 prevcard = deck[prevcard].next;
1581 j += CARD_WIDTH/2;
1584 if( ( cur_rem == NOT_A_CARD || rem == NOT_A_CARD )
1585 && cur_col == REM_COL )
1587 draw_cursor( CARD_WIDTH+2*MARGIN+1, MARGIN );
1590 rb->lcd_update();
1592 /* what to do when a key is pressed ... */
1593 button = rb->button_get( true );
1594 #if (CONFIG_KEYPAD == SANSA_E200_PAD) || (CONFIG_KEYPAD == SANSA_FUZE_PAD)
1595 if (button&(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD))
1596 button = button & (~BUTTON_REPEAT);
1597 #endif
1598 switch( button )
1600 /* move cursor to the last card of the previous column
1601 * or to the previous final stack
1602 * or to the remains stack */
1603 case SOL_RIGHT:
1604 #ifdef SOL_RIGHT_PRE
1605 if( lastbutton != SOL_RIGHT_PRE )
1606 break;
1607 #endif
1608 if( cur_col >= COL_NUM )
1610 cur_col = 0;
1612 else if( cur_col == COL_NUM - 1 )
1614 cur_col = REM_COL;
1616 else
1618 cur_col = (cur_col+1)%(REM_COL+1);
1620 if(cur_col == REM_COL)
1622 cur_card = cur_rem;
1623 break;
1625 cur_card = find_last_card( cur_col );
1626 break;
1628 /* move cursor to the last card of the next column
1629 * or to the next final stack
1630 * or to the remains stack */
1631 case SOL_LEFT:
1632 #ifdef SOL_LEFT_PRE
1633 if( lastbutton != SOL_LEFT_PRE )
1634 break;
1635 #endif
1636 if( cur_col == 0 )
1638 cur_col = REM_COL;
1640 else if( cur_col >= COL_NUM )
1642 cur_col = COL_NUM - 1;
1644 else
1646 cur_col = (cur_col + REM_COL)%(REM_COL+1);
1648 if( cur_col == REM_COL )
1650 cur_card = cur_rem;
1651 break;
1653 cur_card = find_last_card( cur_col );
1654 break;
1656 /* move cursor to card that's bellow */
1657 case SOL_DOWN:
1658 #ifdef SOL_DOWN_PRE
1659 if( lastbutton != SOL_DOWN_PRE )
1660 break;
1661 #else
1662 case SOL_DOWN|BUTTON_REPEAT:
1663 #endif
1664 if( cur_col >= COL_NUM )
1666 cur_col = (cur_col - COL_NUM + 1)%(SUITS + 1) + COL_NUM;
1667 if( cur_col == REM_COL )
1669 cur_card = cur_rem;
1671 else
1673 cur_card = find_last_card( cur_col );
1675 break;
1677 if( cur_card == NOT_A_CARD ) break;
1678 if( deck[cur_card].next != NOT_A_CARD )
1680 cur_card = deck[cur_card].next;
1682 else
1684 cur_card = cols[cur_col];
1685 while( !deck[ cur_card].known
1686 && deck[cur_card].next != NOT_A_CARD )
1688 cur_card = deck[cur_card].next;
1691 break;
1693 /* move cursor to card that's above */
1694 case SOL_UP:
1695 #ifdef SOL_UP_PRE
1696 if( lastbutton != SOL_UP_PRE )
1697 break;
1698 #else
1699 case SOL_UP|BUTTON_REPEAT:
1700 #endif
1701 if( cur_col >= COL_NUM )
1703 cur_col = (cur_col - COL_NUM + SUITS)%(SUITS + 1) + COL_NUM;
1704 if( cur_col == REM_COL )
1706 cur_card = cur_rem;
1708 else
1710 cur_card = find_last_card( cur_col );
1712 break;
1714 if( cur_card == NOT_A_CARD ) break;
1715 do {
1716 cur_card = find_prev_card( cur_card );
1717 if( cur_card == NOT_A_CARD )
1719 cur_card = find_last_card( cur_col );
1721 } while( deck[cur_card].next != NOT_A_CARD
1722 && !deck[cur_card].known );
1723 break;
1725 /* Try to put card under cursor on one of the stacks */
1726 case SOL_CUR2STACK:
1727 #ifdef SOL_CUR2STACK_PRE
1728 if( lastbutton != SOL_CUR2STACK_PRE )
1729 break;
1730 #endif
1731 move_card( deck[cur_card].suit + STACKS_COL, cur_card );
1732 break;
1734 /* Move cards arround, Uncover cards, ... */
1735 case SOL_MOVE:
1736 #ifdef SOL_MOVE_PRE
1737 if( lastbutton != SOL_MOVE_PRE )
1738 break;
1739 #endif
1741 if( sel_card == NOT_A_CARD )
1743 if( cur_card != NOT_A_CARD )
1745 if( deck[cur_card].next == NOT_A_CARD
1746 && !deck[cur_card].known )
1748 /* reveal a hidden card */
1749 deck[cur_card].known = true;
1751 else if( cur_col == REM_COL && cur_rem == NOT_A_CARD )
1753 break;
1755 else
1757 /* select a card */
1758 sel_card = cur_card;
1762 else if( sel_card == cur_card )
1764 /* unselect card or try putting card on
1765 * one of the 4 stacks */
1766 if( move_card( deck[sel_card].suit + COL_NUM, sel_card )
1767 == MOVE_OK && cur_col == REM_COL )
1769 cur_card = cur_rem;
1771 sel_card = NOT_A_CARD;
1773 else
1775 /* try moving cards */
1776 /* The flexible move must not be used from the remains stack. */
1777 if (find_card_col(sel_card) == REM_COL)
1779 if (move_card( cur_col, sel_card ) == MOVE_NOT_OK)
1780 sel_card = NOT_A_CARD;
1782 else
1784 do {
1785 if (move_card( cur_col, sel_card) == MOVE_OK)
1786 break;
1787 sel_card = find_prev_card(sel_card);
1788 } while (sel_card != NOT_A_CARD);
1791 break;
1793 /* If the card on the top of the remains can be put where
1794 * the cursor is, go ahead */
1795 case SOL_REM2CUR:
1796 #ifdef SOL_REM2CUR_PRE
1797 if( lastbutton != SOL_REM2CUR_PRE )
1798 break;
1799 #endif
1800 move_card( cur_col, cur_rem );
1801 break;
1803 /* If the card on top of the remains can be put on one
1804 * of the stacks, do so */
1805 case SOL_REM2STACK:
1806 #ifdef SOL_REM2STACK_PRE
1807 if( lastbutton != SOL_REM2STACK_PRE )
1808 break;
1809 #endif
1810 move_card( deck[cur_rem].suit + COL_NUM, cur_rem );
1811 break;
1813 #ifdef SOL_REM
1814 case SOL_REM:
1815 if( sel_card != NOT_A_CARD )
1817 /* unselect selected card */
1818 sel_card = NOT_A_CARD;
1819 break;
1821 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1823 sel_card = cur_rem;
1824 break;
1826 break;
1827 #endif
1829 /* unselect selected card or ...
1830 * draw new cards from the remains of the deck */
1831 case SOL_DRAW:
1832 #ifdef SOL_DRAW_PRE
1833 if( lastbutton != SOL_DRAW_PRE )
1834 break;
1835 #endif
1836 if( sel_card != NOT_A_CARD )
1838 /* unselect selected card */
1839 sel_card = NOT_A_CARD;
1840 break;
1842 if( rem != NOT_A_CARD )
1844 int cur_rem_old = cur_rem;
1845 count_rem = -1;
1846 /* draw new cards form the remains of the deck */
1847 if( cur_rem == NOT_A_CARD )
1849 /*if the cursor card is null*/
1850 cur_rem = rem;
1851 i = cards_per_draw - 1;
1852 count_rem++;
1854 else
1856 i = cards_per_draw;
1859 while( i > 0 && deck[cur_rem].next != NOT_A_CARD )
1861 cur_rem = deck[cur_rem].next;
1862 i--;
1863 count_rem++;
1865 /* test if any cards are really left on
1866 * the remains' stack */
1867 if( i == cards_per_draw )
1869 cur_rem = NOT_A_CARD;
1870 count_rem = -1;
1872 /* if cursor was on remains' stack when new cards were
1873 * drawn, put cursor on top of remains' stack */
1874 if( cur_col == REM_COL && cur_card == cur_rem_old )
1876 cur_card = cur_rem;
1877 sel_card = NOT_A_CARD;
1880 break;
1882 /* Show the menu */
1883 #ifdef SOL_RC_QUIT
1884 case SOL_RC_QUIT:
1885 #endif
1886 case SOL_QUIT:
1887 switch( solitaire_menu(true) )
1889 case MENU_SAVE_AND_QUIT:
1890 return SOLITAIRE_SAVE_AND_QUIT;
1892 case MENU_QUIT:
1893 return SOLITAIRE_QUIT;
1895 case MENU_USB:
1896 return SOLITAIRE_USB;
1898 break;
1900 case SYS_POWEROFF:
1901 return SOLITAIRE_SAVE_AND_QUIT;
1903 default:
1904 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1905 return SOLITAIRE_USB;
1906 break;
1909 if( button != BUTTON_NONE )
1910 lastbutton = button;
1912 /* fix incoherences concerning cur_col and cur_card */
1913 c = find_card_col( cur_card );
1914 if( c != NOT_A_COL && c != cur_col )
1915 cur_card = find_last_card( cur_col );
1917 if( cur_card == NOT_A_CARD
1918 && find_last_card( cur_col ) != NOT_A_CARD )
1919 cur_card = find_last_card( cur_col );
1921 rb->yield();
1926 * Plugin entry point
1929 enum plugin_status plugin_start(const void* parameter )
1931 int result;
1933 /* plugin init */
1934 (void)parameter;
1936 configfile_load(CONFIG_FILENAME, config,
1937 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1938 rb->memcpy(&sol, &sol_disk, sizeof(sol)); /* copy to running config */
1940 if( load_game() == 0 )
1942 rb->splash( HZ, "Resuming saved game." );
1943 result = SOLITAIRE_QUIT;
1945 else
1946 result = SOLITAIRE_WIN;
1948 /* play the game :)
1949 * Keep playing if a game was won (that means display the menu after
1950 * winning instead of quiting) */
1951 while( ( result = solitaire( result ) ) == SOLITAIRE_WIN );
1953 if( result != SOLITAIRE_QUIT )
1954 /* result == SOLITAIRE_USB || result == SOLITAIRE_SAVE_AND_QUIT */
1955 save_game();
1957 if (rb->memcmp(&sol, &sol_disk, sizeof(sol))) /* save settings if changed */
1959 rb->memcpy(&sol_disk, &sol, sizeof(sol));
1960 configfile_save(CONFIG_FILENAME, config,
1961 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1964 /* Exit the plugin */
1965 return ( result == SOLITAIRE_USB ) ? PLUGIN_USB_CONNECTED : PLUGIN_OK;