1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "lib/playback_control.h"
24 #include "lib/configfile.h"
25 #include "lib/display_text.h"
29 #ifdef HAVE_LCD_BITMAP
33 #define min(a,b) (a<b?a:b)
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
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
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..."
461 #error No keymap defined!
464 #ifdef HAVE_TOUCHSCREEN
466 //# define SOL_QUIT BUTTON_TOPLEFT
469 # define SOL_UP BUTTON_TOPMIDDLE
472 # define SOL_DOWN BUTTON_BOTTOMMIDDLE
475 # define SOL_LEFT BUTTON_MIDLEFT
478 # define SOL_RIGHT BUTTON_MIDRIGHT
481 # define SOL_MOVE BUTTON_CENTER
482 # define HK_MOVE "CENTRE"
485 # define SOL_DRAW BUTTON_TOPLEFT
486 # define HK_DRAW "TOPLEFT"
489 # define SOL_REM2CUR BUTTON_TOPRIGHT
490 # define HK_REM2CUR "TOPRIGHT"
492 #ifndef SOL_CUR2STACK
493 # define SOL_CUR2STACK BUTTON_BOTTOMLEFT
494 # define HK_CUR2STACK "BOTTOMLEFT"
496 #ifndef SOL_REM2STACK
497 # define SOL_REM2STACK BUTTON_BOTTOMRIGHT
498 # define HK_REM2STACK "BOTTOMRIGHT"
504 # define HK_LR "LEFT/RIGHT"
507 # define HK_UD "UP/DOWN"
511 * Misc constants, graphics and other defines
514 #include "pluginbitmaps/card_back.h"
515 #include "pluginbitmaps/card_deck.h"
516 #include "pluginbitmaps/solitaire_suitsi.h"
518 #define CARD_GFX_WIDTH BMPWIDTH_card_back
519 #define CARD_GFX_HEIGHT BMPHEIGHT_card_back
520 #define CARD_WIDTH (BMPWIDTH_card_back+2)
521 #define CARD_HEIGHT (BMPHEIGHT_card_back+2)
526 # define SYMBOL_HEIGHT 24
528 #elif LCD_WIDTH >= 320
531 # define SYMBOL_HEIGHT 12
532 #elif LCD_WIDTH >= 220
535 # define SYMBOL_HEIGHT 12
536 #elif LCD_WIDTH >= 160
538 # define SYMBOL_HEIGHT 11
539 #elif LCD_WIDTH >= 128
541 # define SYMBOL_HEIGHT 10
544 # define SYMBOL_HEIGHT 8
547 #define CARD_START (CARD_HEIGHT+2*MARGIN+1)
549 /* background color */
550 #ifdef HAVE_LCD_COLOR
551 # define BACKGROUND_COLOR LCD_RGBPACK(0,157,0)
552 # define FRAME_COLOR LCD_RGBPACK(23,119,218)
554 # define BACKGROUND_COLOR LCD_WHITE
555 # define FRAME_COLOR LCD_BLACK
559 #define CONFIG_FILENAME "sol.cfg"
561 #define NOT_A_CARD -1
563 /* number of cards per suit */
564 #define CARDS_PER_SUIT 13
566 /* number of suits */
569 #define NUM_CARDS ( CARDS_PER_SUIT * SUITS )
571 /* number of columns */
574 /* pseudo column numbers to be used for cursor coordinates */
575 /* columns COL_NUM to COL_NUM + SUITS - 1 correspond to the final stacks */
576 #define STACKS_COL COL_NUM
577 /* column COL_NUM + SUITS corresponds to the remains' stack */
578 #define REM_COL (STACKS_COL + SUITS)
587 bool used
: 1; /* this is what is used when dealing cards */
593 * LCD card drawing routines
596 static void draw_cursor( int x
, int y
)
598 rb
->lcd_set_drawmode( DRMODE_COMPLEMENT
);
599 rb
->lcd_fillrect( x
+1, y
+1, CARD_GFX_WIDTH
, CARD_GFX_HEIGHT
);
601 rb
->lcd_drawpixel( x
+1, y
+1 );
602 rb
->lcd_drawpixel( x
+1, y
+CARD_HEIGHT
-2 );
603 rb
->lcd_drawpixel( x
+CARD_WIDTH
-2, y
+1 );
604 rb
->lcd_drawpixel( x
+CARD_WIDTH
-2, y
+CARD_HEIGHT
-2 );
606 rb
->lcd_set_drawmode( DRMODE_SOLID
);
609 /* Draw a card's border, select it if it's selected and draw the cursor
610 * if the cursor is currently over the card */
611 static void draw_card_ext( int x
, int y
, bool selected
, bool cursor
)
614 int oldfg
= rb
->lcd_get_foreground();
616 rb
->lcd_set_foreground( LCD_BLACK
);
619 rb
->lcd_hline( x
+2, x
+CARD_WIDTH
-3, y
);
620 rb
->lcd_hline( x
+2, x
+CARD_WIDTH
-3, y
+CARD_HEIGHT
-1 );
621 rb
->lcd_vline( x
, y
+2, y
+CARD_HEIGHT
-3 );
622 rb
->lcd_vline( x
+CARD_WIDTH
-1, y
+2, y
+CARD_HEIGHT
-3 );
623 rb
->lcd_drawpixel( x
+1, y
+1 );
624 rb
->lcd_drawpixel( x
+1, y
+CARD_HEIGHT
-2 );
625 rb
->lcd_drawpixel( x
+CARD_WIDTH
-2, y
+1 );
626 rb
->lcd_drawpixel( x
+CARD_WIDTH
-2, y
+CARD_HEIGHT
-2 );
628 rb
->lcd_hline( x
+1, x
+CARD_WIDTH
-2, y
);
629 rb
->lcd_hline( x
+1, x
+CARD_WIDTH
-2, y
+CARD_HEIGHT
-1 );
630 rb
->lcd_vline( x
, y
+1, y
+CARD_HEIGHT
-2 );
631 rb
->lcd_vline( x
+CARD_WIDTH
-1, y
+1, y
+CARD_HEIGHT
-2 );
637 rb
->lcd_set_foreground( FRAME_COLOR
);
639 rb
->lcd_drawrect( x
+1, y
+1, CARD_WIDTH
-2, CARD_HEIGHT
-2 );
641 rb
->lcd_drawrect( x
+2, y
+2, CARD_WIDTH
-4, CARD_HEIGHT
-4 );
645 rb
->lcd_set_foreground( oldfg
);
654 /* Draw a card's inner graphics */
655 static void draw_card( card_t
*card
, int x
, int y
,
656 bool selected
, bool cursor
)
660 rb
->lcd_bitmap_part( card_deck
, CARD_GFX_WIDTH
* card
->num
,
661 CARD_GFX_HEIGHT
* card
->suit
,
663 BMPWIDTH_card_deck
, BMPHEIGHT_card_deck
),
664 x
+1, y
+1, CARD_GFX_WIDTH
, CARD_GFX_HEIGHT
);
668 rb
->lcd_bitmap( card_back
, x
+1, y
+1,
669 CARD_GFX_WIDTH
, CARD_GFX_HEIGHT
);
671 draw_card_ext( x
, y
, selected
, cursor
);
674 /* Draw an empty stack */
675 static void draw_empty_stack( int s
, int x
, int y
, bool cursor
)
677 rb
->lcd_bitmap_part( solitaire_suitsi
, 0,
680 BMPWIDTH_solitaire_suitsi
, BMPHEIGHT_solitaire_suitsi
),
681 x
+1, y
+1, CARD_GFX_WIDTH
, CARD_GFX_HEIGHT
);
683 draw_card_ext( x
, y
, false, cursor
);
687 static bool solitaire_help( void )
689 static char* help_text
[] = {
690 "Solitaire", "", "Controls", "",
691 HK_LR
":", "Move", "the", "cursor", "to", "the",
692 "previous/", "next", "column.", "",
693 HK_UD
":", "Move", "the", "cursor", "up/", "down", "in", "the",
695 HK_MOVE
":", "Select", "cards,", "move", "cards...", "",
696 HK_DRAW
":", "Deselect", "a", "card", "if", "it", "was", "selected.",
697 "Else", "draw", "new", "card(s)", "from", "the", "remains",
700 HK_REM2CUR
":", "Put", "the", "card", "on", "top", "of", "the",
701 "remains", "stack", "on", "top", "of", "the", "cursor.", "",
702 HK_CUR2STACK
":", "Put", "the", "card", "under", "the", "cursor",
703 "on", "one", "of", "the", "4", "final", "stacks.", "",
704 HK_REM2STACK
":", "Put", "the", "card", "on", "top", "of", "the",
705 "remains", "stack", "on", "one", "of", "the", "4", "final",
708 static struct style_text formation
[]={
709 { 0, TEXT_CENTER
|TEXT_UNDERLINE
},
714 if (display_text(ARRAYLEN(help_text
), help_text
, formation
, NULL
, true))
720 * Custom menu / options
723 #define CFGFILE_VERSION 0
729 struct sol_config sol_disk
= {0};
730 struct sol_config sol
;
732 static struct configdata config
[] = {
733 { TYPE_INT
, 0, 1, { .int_p
= &sol_disk
.draw_type
}, "draw_type", NULL
}
736 static const struct opt_items drawcards
[2] = {
737 { "Draw Three Cards", -1 },
738 { "Draw One Card", -1 },
741 void solitaire_init(void);
743 /* menu return codes */
744 enum { MENU_RESUME
, MENU_SAVE_AND_QUIT
, MENU_QUIT
, MENU_USB
};
747 int solitaire_menu_cb(int action
, const struct menu_item_ex
*this_item
)
749 int i
= (intptr_t)this_item
;
750 if( action
== ACTION_REQUEST_MENUITEM
)
752 if((!_ingame
&& (i
==0 || i
==5)) || ( _ingame
&& i
==2 ))
753 return ACTION_EXIT_MENUITEM
;
758 int solitaire_menu(bool in_game
)
763 MENUITEM_STRINGLIST(menu
, "Solitaire Menu", solitaire_menu_cb
,
764 "Resume Game", "Start New Game",
766 "Help", "Playback Control",
767 "Save and Quit", "Quit");
772 switch (rb
->do_menu(&menu
, &selected
, NULL
, false))
775 result
= MENU_RESUME
;
778 case MENU_ATTACHED_USB
:
783 result
= MENU_RESUME
;
788 result
= MENU_RESUME
;
792 if (rb
->set_option("Draw Cards Option", &sol
.draw_type
,
793 INT
, drawcards
, 2, NULL
))
798 if (solitaire_help())
803 playback_control(NULL
);
807 result
= MENU_SAVE_AND_QUIT
;
822 /* player's cursor */
824 /* player's cursor column num */
831 card_t deck
[ NUM_CARDS
];
833 /* the remaining cards */
834 /* first card of the remains' stack */
836 /* upper visible card from the remains' stack */
838 /* number of cards drawn from the remains stack - 1 */
840 /* number of cards per draw of the remains' stack */
843 /* the 7 game columns */
845 /* the 4 final stacks */
849 * Card handling routines
852 int next_random_card( card_t
*deck
)
856 r
= rb
->rand()%(NUM_CARDS
)+1;
861 i
= (i
+ 1)%(NUM_CARDS
);
862 if( !deck
[i
].used
) r
--;
871 /* initialize the game */
872 void solitaire_init( void )
878 /* number of cards that are drawn on the remains' stack (by pressing F2) */
879 if( sol
.draw_type
== 0 )
889 for( i
=0; i
<SUITS
; i
++ )
891 for( j
=0; j
<CARDS_PER_SUIT
; j
++ )
893 #define card deck[i*CARDS_PER_SUIT+j]
898 card
.next
= NOT_A_CARD
;
903 /* deal the cards ... */
904 /* ... in the columns */
905 for( i
=0; i
<COL_NUM
; i
++ )
908 for( j
=0; j
<=i
; j
++ )
910 if( c
== NOT_A_CARD
)
912 cols
[i
] = next_random_card( deck
);
917 deck
[c
].next
= next_random_card( deck
);
921 deck
[c
].known
= false;
925 /* ... shuffle what's left of the deck */
926 rem
= next_random_card(deck
);
929 for( i
=1; i
< NUM_CARDS
- COL_NUM
* (COL_NUM
+ 1)/2; i
++ )
931 deck
[c
].next
= next_random_card( deck
);
935 /* we now finished dealing the cards. The game can start ! (at last) */
938 for( i
= 0; i
<SUITS
; i
++ )
940 stacks
[i
] = NOT_A_CARD
;
943 /* the cursor starts on upper left card */
947 /* no card is selected */
948 sel_card
= NOT_A_CARD
;
950 /* init the remainder */
951 cur_rem
= NOT_A_CARD
;
956 /* find the column number in which 'card' can be found */
957 int find_card_col( int card
)
962 if( card
== NOT_A_CARD
) return NOT_A_COL
;
964 for( i
=0; i
<COL_NUM
; i
++ )
967 while( c
!= NOT_A_CARD
)
969 if( c
== card
) return i
;
974 for( i
=0; i
<SUITS
; i
++ )
977 while( c
!= NOT_A_CARD
)
979 if( c
== card
) return STACKS_COL
+ i
;
987 /* find the card preceding 'card' */
988 /* if it doesn't exist, return NOT_A_CARD */
989 int find_prev_card( int card
){
992 for( i
=0; i
< NUM_CARDS
; i
++ )
994 if( deck
[i
].next
== card
) return i
;
1000 /* find the last card of a given column */
1001 int find_last_card( int col
)
1009 else if( col
< REM_COL
)
1011 c
= stacks
[col
- STACKS_COL
];
1022 while(deck
[c
].next
!= NOT_A_CARD
)
1028 enum move
{ MOVE_OK
, MOVE_NOT_OK
};
1030 enum move
move_card( int dest_col
, int src_card
)
1032 /* the column on which to take src_card */
1035 /* the last card of dest_col */
1038 /* the card under src_card */
1041 /* you can't move no card (at least, it doesn't have any consequence) */
1042 if( src_card
== NOT_A_CARD
) return MOVE_NOT_OK
;
1043 /* you can't put a card back on the remains' stack */
1044 if( dest_col
== REM_COL
) return MOVE_NOT_OK
;
1045 /* you can't move an unknown card */
1046 if( !deck
[src_card
].known
) return MOVE_NOT_OK
;
1048 src_col
= find_card_col( src_card
);
1049 dest_card
= find_last_card( dest_col
);
1050 src_card_prev
= find_prev_card( src_card
);
1052 /* you can't move more than one card at a time from the final stack */
1053 /* to the rest of the game */
1054 if( src_col
>= COL_NUM
&& src_col
< REM_COL
1055 && deck
[src_card
].next
!= NOT_A_CARD
)
1060 /* if we (that means dest) are on one of the 7 columns ... */
1061 if( dest_col
< COL_NUM
)
1063 /* ... check is we are on an empty color and that the src is a king */
1064 if( dest_card
== NOT_A_CARD
1065 && deck
[src_card
].num
== CARDS_PER_SUIT
- 1 )
1067 /* this is a winning combination */
1068 cols
[dest_col
] = src_card
;
1070 /* ... or check if the cards follow one another and have
1071 * different colorsuit */
1072 else if(( deck
[dest_card
].suit
+ deck
[src_card
].suit
)%2==1
1073 && deck
[dest_card
].num
== deck
[src_card
].num
+ 1 )
1075 /* this is a winning combination */
1076 deck
[dest_card
].next
= src_card
;
1078 /* ... or, humpf, well that's not good news */
1081 /* this is not a winning combination */
1085 /* if we are on one of the 4 final stacks ... */
1086 else if( dest_col
< REM_COL
)
1088 /* ... check if we are on an empty stack... */
1089 if( dest_card
== NOT_A_CARD
)
1091 /* ... and the src is an ace and this is the correct final stack */
1092 if( deck
[src_card
].num
== 0
1093 && deck
[src_card
].suit
== dest_col
- STACKS_COL
)
1095 /* this is a winning combination */
1096 stacks
[dest_col
- STACKS_COL
] = src_card
;
1100 /* this is not a winning combination */
1104 else /* non-empty stack */
1106 /* ...check if the cards follow one another, have the same suit and
1107 * {that src has no .next element or is from the remains' stack} */
1108 if( deck
[dest_card
].suit
== deck
[src_card
].suit
1109 && deck
[dest_card
].num
+ 1 == deck
[src_card
].num
1110 && (deck
[src_card
].next
== NOT_A_CARD
|| src_col
== REM_COL
) )
1112 /* this is a winning combination */
1113 deck
[dest_card
].next
= src_card
;
1115 /* ... or, well that's not good news */
1118 /* this is not a winning combination */
1123 /* if we are on the remains' stack */
1126 /* you can't move a card back to the remains' stack */
1130 /* if the src card is from the remains' stack, we don't want to take
1131 * the following cards */
1132 if( src_col
== REM_COL
)
1134 /* if src card is the first card from the stack */
1135 if( src_card_prev
== NOT_A_CARD
)
1137 rem
= deck
[src_card
].next
;
1139 /* if src card is not the first card from the stack */
1142 deck
[src_card_prev
].next
= deck
[src_card
].next
;
1144 deck
[src_card
].next
= NOT_A_CARD
;
1145 cur_rem
= src_card_prev
;
1148 /* if the src card is from somewhere else, just take everything */
1151 if( src_card_prev
== NOT_A_CARD
)
1153 if( src_col
< COL_NUM
)
1155 cols
[src_col
] = NOT_A_CARD
;
1159 stacks
[src_col
- STACKS_COL
] = NOT_A_CARD
;
1164 deck
[src_card_prev
].next
= NOT_A_CARD
;
1165 deck
[src_card_prev
].known
= true;
1168 sel_card
= NOT_A_CARD
;
1173 enum { SOLITAIRE_WIN
, SOLITAIRE_SAVE_AND_QUIT
, SOLITAIRE_QUIT
, SOLITAIRE_USB
};
1176 * Bouncing cards at the end of the game
1179 #define BC_ACCEL ((1<<16)*LCD_HEIGHT/128)
1180 #define BC_MYSPEED (6*BC_ACCEL)
1181 #define BC_MXSPEED (6*LCD_HEIGHT/128)
1183 int bouncing_cards( void )
1185 int i
, j
, x
, vx
, y
, fp_y
, fp_vy
, button
;
1187 /* flush the button queue */
1188 while( ( button
= rb
->button_get( false ) ) != BUTTON_NONE
)
1190 if( rb
->default_event_handler( button
) == SYS_USB_CONNECTED
)
1191 return SOLITAIRE_USB
;
1195 for( i
= CARDS_PER_SUIT
-1; i
>=0; i
-- )
1197 for( j
= 0; j
< SUITS
; j
++ )
1199 x
= LCD_WIDTH
-(CARD_WIDTH
*4+4+MARGIN
)+CARD_WIDTH
*j
+j
+1;
1203 vx
= rb
->rand() % (4*BC_MXSPEED
/3-2) - BC_MXSPEED
;
1207 vx
= rb
->rand() % (4*BC_MXSPEED
/3) - BC_MXSPEED
;
1212 fp_vy
= -rb
->rand() % BC_MYSPEED
;
1214 while( x
< LCD_WIDTH
&& x
+ CARD_WIDTH
> 0 )
1219 if( fp_y
>= (LCD_HEIGHT
-CARD_HEIGHT
) << 16 )
1222 fp_y
= (LCD_HEIGHT
-CARD_HEIGHT
) << 16;
1225 draw_card( &deck
[j
*CARDS_PER_SUIT
+i
], x
, y
,
1227 rb
->lcd_update_rect( x
<0?0:x
, y
<0?0:y
,
1228 CARD_WIDTH
, CARD_HEIGHT
);
1230 button
= rb
->button_get_w_tmo( 2 );
1231 if( rb
->default_event_handler( button
) == SYS_USB_CONNECTED
)
1232 return SOLITAIRE_USB
;
1233 if( button
== SOL_QUIT
|| button
== SOL_MOVE
)
1234 return SOLITAIRE_WIN
;
1238 return SOLITAIRE_WIN
;
1242 * Game save/load routines
1244 void get_save_filename( char *buf
)
1247 rb
->strcpy( buf
, rb
->plugin_get_current_filename() );
1248 s
= rb
->strrchr( buf
, '/' ) + 1;
1250 rb
->strcat( s
, "sol.save" );
1253 int open_save_file( int flags
)
1256 get_save_filename( buf
);
1257 return rb
->open( buf
, flags
);
1260 void delete_save_file( void )
1263 get_save_filename( buf
);
1270 int save_write( int fd
, const void *buf
, size_t count
, int *checksum
)
1273 if( rb
->write( fd
, buf
, count
) < (ssize_t
)count
)
1275 for( i
= 0; i
< count
; i
++ )
1276 *checksum
+= (int)(((const char *)buf
)[i
]);
1283 int save_read( int fd
, void *buf
, size_t count
, int *checksum
)
1286 if( rb
->read( fd
, buf
, count
) < (ssize_t
)count
)
1288 for( i
= 0; i
< count
; i
++ )
1289 *checksum
-= (int)(((const char *)buf
)[i
]);
1293 int save_game( void )
1295 int fd
= open_save_file( O_CREAT
|O_WRONLY
|O_TRUNC
);
1299 if( save_write( fd
, &cur_card
, sizeof( int ), &checksum
)
1300 || save_write( fd
, &cur_col
, sizeof( int ), &checksum
)
1301 || save_write( fd
, &sel_card
, sizeof( int ), &checksum
)
1302 || save_write( fd
, deck
, NUM_CARDS
* sizeof( card_t
), &checksum
)
1303 || save_write( fd
, &rem
, sizeof( int ), &checksum
)
1304 || save_write( fd
, &cur_rem
, sizeof( int ), &checksum
)
1305 || save_write( fd
, &count_rem
, sizeof( int ), &checksum
)
1306 || save_write( fd
, &cards_per_draw
, sizeof( int ), &checksum
)
1307 || save_write( fd
, cols
, COL_NUM
* sizeof( int ), &checksum
)
1308 || save_write( fd
, stacks
, SUITS
* sizeof( int ), &checksum
)
1309 || ( rb
->write( fd
, &checksum
, sizeof( int ) ) < (ssize_t
)(sizeof( int ) ) ) )
1312 rb
->splash( 2*HZ
, "Error while saving game. Aborting." );
1319 int load_game( void )
1321 int checksum
, retval
;
1323 int fd
= open_save_file( O_RDONLY
);
1327 retval
= 0; /* Assume good case */
1328 if( ( rb
->lseek( fd
, -sizeof( int ), SEEK_END
) == -((ssize_t
)sizeof( int ))-1 )
1329 || ( rb
->read( fd
, &checksum
, sizeof( int ) ) < ((ssize_t
)sizeof( int )) )
1330 || ( rb
->lseek( fd
, 0, SEEK_SET
) == -1 )
1331 || save_read( fd
, &cur_card
, sizeof( int ), &checksum
)
1332 || save_read( fd
, &cur_col
, sizeof( int ), &checksum
)
1333 || save_read( fd
, &sel_card
, sizeof( int ), &checksum
)
1334 || save_read( fd
, deck
, NUM_CARDS
* sizeof( card_t
), &checksum
)
1335 || save_read( fd
, &rem
, sizeof( int ), &checksum
)
1336 || save_read( fd
, &cur_rem
, sizeof( int ), &checksum
)
1337 || save_read( fd
, &count_rem
, sizeof( int ), &checksum
)
1338 || save_read( fd
, &cards_per_draw
, sizeof( int ), &checksum
)
1339 || save_read( fd
, cols
, COL_NUM
* sizeof( int ), &checksum
)
1340 || save_read( fd
, stacks
, SUITS
* sizeof( int ), &checksum
) )
1342 rb
->splash( 2*HZ
, "Error while loading saved game. Aborting." );
1345 else if( checksum
!= 42 )
1347 rb
->splash( 2*HZ
, "Save file was corrupted. Aborting." );
1357 * The main game loop
1359 * If skipmenu is defined to SOLITAIRE_QUIT, the menu will be skipped and
1363 int solitaire( int skipmenu
)
1367 int button
, lastbutton
= 0;
1369 int biggest_col_length
;
1371 rb
->srand( *rb
->current_tick
);
1372 if( skipmenu
!= SOLITAIRE_QUIT
)
1374 switch( solitaire_menu(false) )
1377 return SOLITAIRE_QUIT
;
1380 return SOLITAIRE_USB
;
1387 rb
->lcd_clear_display();
1389 /* get the biggest column length so that display can be "optimized" */
1390 biggest_col_length
= 0;
1392 for(i
=0;i
<COL_NUM
;i
++)
1397 if( c
!= NOT_A_CARD
)
1401 /* don't include the last card in the column length. */
1402 if( deck
[c
].next
== NOT_A_CARD
)
1404 break; /* no successor: get outta here. */
1415 /* make column distinguishable from an empty column,
1416 * and avoid division by zero while displaying */
1420 if( j
> biggest_col_length
)
1421 biggest_col_length
= j
;
1424 /* check if there are cards remaining in the game. */
1425 /* if there aren't any, that means you won :) */
1426 if( biggest_col_length
== 0 && rem
== NOT_A_CARD
)
1429 rb
->splash( HZ
, "You Won :)" );
1430 return bouncing_cards();
1433 /* draw the columns */
1434 for( i
= 0; i
< COL_NUM
; i
++ )
1440 if( c
== NOT_A_CARD
)
1442 /* draw the cursor on empty columns */
1445 draw_cursor( MARGIN
+ i
* (CARD_WIDTH
1446 +(LCD_WIDTH
-COL_NUM
*CARD_WIDTH
-2*MARGIN
)/(COL_NUM
-1)),
1452 draw_card( &deck
[c
], MARGIN
+ i
* (CARD_WIDTH
1453 +(LCD_WIDTH
-COL_NUM
*CARD_WIDTH
-2*MARGIN
)/(COL_NUM
-1)),
1454 j
, c
== sel_card
, c
== cur_card
);
1458 if( c
== NOT_A_CARD
) break;
1460 /* This is where we change the spacing between cards so that
1461 * they don't overflow out of the LCD */
1464 else if( deck
[h
].known
)
1465 j
+= min( SYMBOL_HEIGHT
,
1466 2 * (LCD_HEIGHT
- CARD_START
- CARD_HEIGHT
- MARGIN
)
1467 / biggest_col_length
);
1469 j
+= min( SYMBOL_HEIGHT
/ 2,
1470 (LCD_HEIGHT
- CARD_START
- CARD_HEIGHT
- MARGIN
)
1471 / biggest_col_length
);
1475 /* draw the stacks */
1476 for( i
=0; i
<SUITS
; i
++ )
1478 c
= find_last_card( STACKS_COL
+ i
);
1480 if( c
!= NOT_A_CARD
)
1482 draw_card( &deck
[c
],
1483 LCD_WIDTH
-(CARD_WIDTH
*4+4+MARGIN
)+CARD_WIDTH
*i
+i
+1,
1485 c
== sel_card
, cur_col
== STACKS_COL
+ i
);
1489 draw_empty_stack( i
,
1490 LCD_WIDTH
-(CARD_WIDTH
*4+4+MARGIN
)+CARD_WIDTH
*i
+i
+1,
1491 MARGIN
, cur_col
== STACKS_COL
+ i
);
1495 /* draw the remains */
1496 if( rem
!= NOT_A_CARD
&&
1497 ( cur_rem
== NOT_A_CARD
|| deck
[cur_rem
].next
!= NOT_A_CARD
) )
1499 /* gruik ! (we want to display a card back) */
1500 deck
[rem
].known
= false;
1501 draw_card( &deck
[rem
], MARGIN
, MARGIN
, false, false );
1502 deck
[rem
].known
= true;
1505 if( rem
!= NOT_A_CARD
&& cur_rem
!= NOT_A_CARD
)
1511 while( prevcard
!= cur_rem
&& count_rem
< cards_per_draw
-1 )
1513 prevcard
= deck
[prevcard
].next
;
1518 j
= CARD_WIDTH
+2*MARGIN
+1;
1519 for( i
= 0; i
< count_rem
; i
++ )
1520 prevcard
= find_prev_card(prevcard
);
1521 for( i
= 0; i
<= count_rem
; i
++ )
1523 draw_card( &deck
[prevcard
], j
,
1524 MARGIN
, sel_card
== prevcard
,
1525 cur_card
== prevcard
);
1526 prevcard
= deck
[prevcard
].next
;
1530 if( ( cur_rem
== NOT_A_CARD
|| rem
== NOT_A_CARD
)
1531 && cur_col
== REM_COL
)
1533 draw_cursor( CARD_WIDTH
+2*MARGIN
+1, MARGIN
);
1538 /* what to do when a key is pressed ... */
1539 button
= rb
->button_get( true );
1540 #if (CONFIG_KEYPAD == SANSA_E200_PAD) || (CONFIG_KEYPAD == SANSA_FUZE_PAD)
1541 if (button
&(BUTTON_SCROLL_BACK
|BUTTON_SCROLL_FWD
))
1542 button
= button
& (~BUTTON_REPEAT
);
1546 /* move cursor to the last card of the previous column
1547 * or to the previous final stack
1548 * or to the remains stack */
1550 #ifdef SOL_RIGHT_PRE
1551 if( lastbutton
!= SOL_RIGHT_PRE
)
1554 if( cur_col
>= COL_NUM
)
1558 else if( cur_col
== COL_NUM
- 1 )
1564 cur_col
= (cur_col
+1)%(REM_COL
+1);
1566 if(cur_col
== REM_COL
)
1571 cur_card
= find_last_card( cur_col
);
1574 /* move cursor to the last card of the next column
1575 * or to the next final stack
1576 * or to the remains stack */
1579 if( lastbutton
!= SOL_LEFT_PRE
)
1586 else if( cur_col
>= COL_NUM
)
1588 cur_col
= COL_NUM
- 1;
1592 cur_col
= (cur_col
+ REM_COL
)%(REM_COL
+1);
1594 if( cur_col
== REM_COL
)
1599 cur_card
= find_last_card( cur_col
);
1602 /* move cursor to card that's bellow */
1605 if( lastbutton
!= SOL_DOWN_PRE
)
1608 case SOL_DOWN
|BUTTON_REPEAT
:
1610 if( cur_col
>= COL_NUM
)
1612 cur_col
= (cur_col
- COL_NUM
+ 1)%(SUITS
+ 1) + COL_NUM
;
1613 if( cur_col
== REM_COL
)
1619 cur_card
= find_last_card( cur_col
);
1623 if( cur_card
== NOT_A_CARD
) break;
1624 if( deck
[cur_card
].next
!= NOT_A_CARD
)
1626 cur_card
= deck
[cur_card
].next
;
1630 cur_card
= cols
[cur_col
];
1631 while( !deck
[ cur_card
].known
1632 && deck
[cur_card
].next
!= NOT_A_CARD
)
1634 cur_card
= deck
[cur_card
].next
;
1639 /* move cursor to card that's above */
1642 if( lastbutton
!= SOL_UP_PRE
)
1645 case SOL_UP
|BUTTON_REPEAT
:
1647 if( cur_col
>= COL_NUM
)
1649 cur_col
= (cur_col
- COL_NUM
+ SUITS
)%(SUITS
+ 1) + COL_NUM
;
1650 if( cur_col
== REM_COL
)
1656 cur_card
= find_last_card( cur_col
);
1660 if( cur_card
== NOT_A_CARD
) break;
1662 cur_card
= find_prev_card( cur_card
);
1663 if( cur_card
== NOT_A_CARD
)
1665 cur_card
= find_last_card( cur_col
);
1667 } while( deck
[cur_card
].next
!= NOT_A_CARD
1668 && !deck
[cur_card
].known
);
1671 /* Try to put card under cursor on one of the stacks */
1673 #ifdef SOL_CUR2STACK_PRE
1674 if( lastbutton
!= SOL_CUR2STACK_PRE
)
1677 move_card( deck
[cur_card
].suit
+ STACKS_COL
, cur_card
);
1680 /* Move cards arround, Uncover cards, ... */
1683 if( lastbutton
!= SOL_MOVE_PRE
)
1687 if( sel_card
== NOT_A_CARD
)
1689 if( cur_card
!= NOT_A_CARD
)
1691 if( deck
[cur_card
].next
== NOT_A_CARD
1692 && !deck
[cur_card
].known
)
1694 /* reveal a hidden card */
1695 deck
[cur_card
].known
= true;
1697 else if( cur_col
== REM_COL
&& cur_rem
== NOT_A_CARD
)
1704 sel_card
= cur_card
;
1708 else if( sel_card
== cur_card
)
1710 /* unselect card or try putting card on
1711 * one of the 4 stacks */
1712 if( move_card( deck
[sel_card
].suit
+ COL_NUM
, sel_card
)
1713 == MOVE_OK
&& cur_col
== REM_COL
)
1717 sel_card
= NOT_A_CARD
;
1721 /* try moving cards */
1722 /* The flexible move must not be used from the remains stack. */
1723 if (find_card_col(sel_card
) == REM_COL
)
1725 if (move_card( cur_col
, sel_card
) == MOVE_NOT_OK
)
1726 sel_card
= NOT_A_CARD
;
1731 if (move_card( cur_col
, sel_card
) == MOVE_OK
)
1733 sel_card
= find_prev_card(sel_card
);
1734 } while (sel_card
!= NOT_A_CARD
);
1739 /* If the card on the top of the remains can be put where
1740 * the cursor is, go ahead */
1742 #ifdef SOL_REM2CUR_PRE
1743 if( lastbutton
!= SOL_REM2CUR_PRE
)
1746 move_card( cur_col
, cur_rem
);
1749 /* If the card on top of the remains can be put on one
1750 * of the stacks, do so */
1752 #ifdef SOL_REM2STACK_PRE
1753 if( lastbutton
!= SOL_REM2STACK_PRE
)
1756 move_card( deck
[cur_rem
].suit
+ COL_NUM
, cur_rem
);
1761 if( sel_card
!= NOT_A_CARD
)
1763 /* unselect selected card */
1764 sel_card
= NOT_A_CARD
;
1767 if( rem
!= NOT_A_CARD
&& cur_rem
!= NOT_A_CARD
)
1775 /* unselect selected card or ...
1776 * draw new cards from the remains of the deck */
1779 if( lastbutton
!= SOL_DRAW_PRE
)
1782 if( sel_card
!= NOT_A_CARD
)
1784 /* unselect selected card */
1785 sel_card
= NOT_A_CARD
;
1788 if( rem
!= NOT_A_CARD
)
1790 int cur_rem_old
= cur_rem
;
1792 /* draw new cards form the remains of the deck */
1793 if( cur_rem
== NOT_A_CARD
)
1795 /*if the cursor card is null*/
1797 i
= cards_per_draw
- 1;
1805 while( i
> 0 && deck
[cur_rem
].next
!= NOT_A_CARD
)
1807 cur_rem
= deck
[cur_rem
].next
;
1811 /* test if any cards are really left on
1812 * the remains' stack */
1813 if( i
== cards_per_draw
)
1815 cur_rem
= NOT_A_CARD
;
1818 /* if cursor was on remains' stack when new cards were
1819 * drawn, put cursor on top of remains' stack */
1820 if( cur_col
== REM_COL
&& cur_card
== cur_rem_old
)
1823 sel_card
= NOT_A_CARD
;
1833 switch( solitaire_menu(true) )
1835 case MENU_SAVE_AND_QUIT
:
1836 return SOLITAIRE_SAVE_AND_QUIT
;
1839 return SOLITAIRE_QUIT
;
1842 return SOLITAIRE_USB
;
1847 return SOLITAIRE_SAVE_AND_QUIT
;
1850 if( rb
->default_event_handler( button
) == SYS_USB_CONNECTED
)
1851 return SOLITAIRE_USB
;
1855 if( button
!= BUTTON_NONE
)
1856 lastbutton
= button
;
1858 /* fix incoherences concerning cur_col and cur_card */
1859 c
= find_card_col( cur_card
);
1860 if( c
!= NOT_A_COL
&& c
!= cur_col
)
1861 cur_card
= find_last_card( cur_col
);
1863 if( cur_card
== NOT_A_CARD
1864 && find_last_card( cur_col
) != NOT_A_CARD
)
1865 cur_card
= find_last_card( cur_col
);
1872 * Plugin entry point
1875 enum plugin_status
plugin_start(const void* parameter
)
1882 configfile_load(CONFIG_FILENAME
, config
,
1883 sizeof(config
) / sizeof(config
[0]), CFGFILE_VERSION
);
1884 rb
->memcpy(&sol
, &sol_disk
, sizeof(sol
)); /* copy to running config */
1886 if( load_game() == 0 )
1888 rb
->splash( HZ
, "Resuming saved game." );
1889 result
= SOLITAIRE_QUIT
;
1892 result
= SOLITAIRE_WIN
;
1895 * Keep playing if a game was won (that means display the menu after
1896 * winning instead of quiting) */
1897 while( ( result
= solitaire( result
) ) == SOLITAIRE_WIN
);
1899 if( result
!= SOLITAIRE_QUIT
)
1900 /* result == SOLITAIRE_USB || result == SOLITAIRE_SAVE_AND_QUIT */
1903 if (rb
->memcmp(&sol
, &sol_disk
, sizeof(sol
))) /* save settings if changed */
1905 rb
->memcpy(&sol_disk
, &sol
, sizeof(sol
));
1906 configfile_save(CONFIG_FILENAME
, config
,
1907 sizeof(config
) / sizeof(config
[0]), CFGFILE_VERSION
);
1910 /* Exit the plugin */
1911 return ( result
== SOLITAIRE_USB
) ? PLUGIN_USB_CONNECTED
: PLUGIN_OK
;