checkwps: Do not error out on rwps for non-remote targets.
[maemo-rb.git] / apps / plugins / chessclock.c
blob0e18ccfcd66138200acec24662de4d7073741765
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Kjell Ericson
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 ****************************************************************************/
21 #include "plugin.h"
22 #include "lib/playback_control.h"
26 /* variable button definitions */
27 #if CONFIG_KEYPAD == RECORDER_PAD
28 #define CHC_QUIT BUTTON_OFF
29 #define CHC_STARTSTOP BUTTON_PLAY
30 #define CHC_RESET BUTTON_LEFT
31 #define CHC_MENU BUTTON_F1
32 #define CHC_SETTINGS_INC BUTTON_UP
33 #define CHC_SETTINGS_DEC BUTTON_DOWN
34 #define CHC_SETTINGS_OK BUTTON_PLAY
35 #define CHC_SETTINGS_OK2 BUTTON_LEFT
36 #define CHC_SETTINGS_CANCEL BUTTON_OFF
38 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
39 #define CHC_QUIT BUTTON_OFF
40 #define CHC_STARTSTOP BUTTON_SELECT
41 #define CHC_RESET BUTTON_LEFT
42 #define CHC_MENU BUTTON_F1
43 #define CHC_SETTINGS_INC BUTTON_UP
44 #define CHC_SETTINGS_DEC BUTTON_DOWN
45 #define CHC_SETTINGS_OK BUTTON_SELECT
46 #define CHC_SETTINGS_OK2 BUTTON_LEFT
47 #define CHC_SETTINGS_CANCEL BUTTON_OFF
49 #elif CONFIG_KEYPAD == ONDIO_PAD
50 #define CHC_QUIT BUTTON_OFF
51 #define CHC_STARTSTOP BUTTON_RIGHT
52 #define CHC_RESET BUTTON_LEFT
53 #define CHC_MENU BUTTON_MENU
54 #define CHC_SETTINGS_INC BUTTON_UP
55 #define CHC_SETTINGS_DEC BUTTON_DOWN
56 #define CHC_SETTINGS_OK BUTTON_RIGHT
57 #define CHC_SETTINGS_OK2 BUTTON_LEFT
58 #define CHC_SETTINGS_CANCEL BUTTON_MENU
60 #elif CONFIG_KEYPAD == PLAYER_PAD
61 #define CHC_QUIT BUTTON_ON
62 #define CHC_STARTSTOP BUTTON_PLAY
63 #define CHC_RESET BUTTON_STOP
64 #define CHC_MENU BUTTON_MENU
65 #define CHC_SETTINGS_INC BUTTON_RIGHT
66 #define CHC_SETTINGS_DEC BUTTON_LEFT
67 #define CHC_SETTINGS_OK BUTTON_PLAY
68 #define CHC_SETTINGS_CANCEL BUTTON_STOP
69 #define CHC_SETTINGS_CANCEL2 BUTTON_MENU
71 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
72 (CONFIG_KEYPAD == IRIVER_H300_PAD)
73 #define CHC_QUIT BUTTON_SELECT
74 #define CHC_STARTSTOP BUTTON_ON
75 #define CHC_RESET BUTTON_OFF
76 #define CHC_MENU BUTTON_REC
77 #define CHC_SETTINGS_INC BUTTON_RIGHT
78 #define CHC_SETTINGS_DEC BUTTON_LEFT
79 #define CHC_SETTINGS_OK BUTTON_ON
80 #define CHC_SETTINGS_CANCEL BUTTON_OFF
81 #define CHC_SETTINGS_CANCEL2 BUTTON_REC
83 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
84 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
85 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
86 #define CHC_QUIT BUTTON_PLAY
87 #define CHC_STARTSTOP BUTTON_SELECT
88 #define CHC_RESET BUTTON_LEFT
89 #define CHC_MENU BUTTON_MENU
90 #define CHC_SETTINGS_INC BUTTON_SCROLL_FWD
91 #define CHC_SETTINGS_DEC BUTTON_SCROLL_BACK
92 #define CHC_SETTINGS_OK BUTTON_SELECT
93 #define CHC_SETTINGS_CANCEL BUTTON_MENU
95 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
96 #define CHC_QUIT BUTTON_PLAY
97 #define CHC_STARTSTOP BUTTON_MODE
98 #define CHC_RESET BUTTON_EQ
99 #define CHC_MENU BUTTON_SELECT
100 #define CHC_SETTINGS_INC BUTTON_RIGHT
101 #define CHC_SETTINGS_DEC BUTTON_LEFT
102 #define CHC_SETTINGS_OK BUTTON_SELECT
103 #define CHC_SETTINGS_CANCEL BUTTON_PLAY
105 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
106 #define CHC_QUIT BUTTON_REC
107 #define CHC_STARTSTOP BUTTON_PLAY
108 #define CHC_RESET BUTTON_POWER
109 #define CHC_MENU BUTTON_SELECT
110 #define CHC_SETTINGS_INC BUTTON_RIGHT
111 #define CHC_SETTINGS_DEC BUTTON_LEFT
112 #define CHC_SETTINGS_OK BUTTON_SELECT
113 #define CHC_SETTINGS_CANCEL BUTTON_REC
115 #elif CONFIG_KEYPAD == GIGABEAT_PAD
116 #define CHC_QUIT BUTTON_POWER
117 #define CHC_STARTSTOP BUTTON_SELECT
118 #define CHC_RESET BUTTON_A
119 #define CHC_MENU BUTTON_MENU
120 #define CHC_SETTINGS_INC BUTTON_UP
121 #define CHC_SETTINGS_DEC BUTTON_DOWN
122 #define CHC_SETTINGS_OK BUTTON_SELECT
123 #define CHC_SETTINGS_CANCEL BUTTON_POWER
125 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
126 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
127 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
128 (CONFIG_KEYPAD == SANSA_M200_PAD)
129 #define CHC_QUIT BUTTON_POWER
130 #define CHC_STARTSTOP BUTTON_SELECT
131 #define CHC_RESET BUTTON_DOWN
132 #define CHC_MENU BUTTON_UP
133 #define CHC_SETTINGS_INC BUTTON_RIGHT
134 #define CHC_SETTINGS_DEC BUTTON_LEFT
135 #define CHC_SETTINGS_OK BUTTON_SELECT
136 #define CHC_SETTINGS_CANCEL BUTTON_POWER
138 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
139 #define CHC_QUIT BUTTON_POWER
140 #define CHC_STARTSTOP BUTTON_SELECT
141 #define CHC_RESET BUTTON_DOWN
142 #define CHC_MENU BUTTON_UP
143 #define CHC_SETTINGS_INC BUTTON_RIGHT
144 #define CHC_SETTINGS_DEC BUTTON_LEFT
145 #define CHC_SETTINGS_OK BUTTON_SELECT
146 #define CHC_SETTINGS_CANCEL (BUTTON_HOME|BUTTON_REPEAT)
148 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
149 #define CHC_QUIT BUTTON_POWER
150 #define CHC_STARTSTOP BUTTON_PLAY
151 #define CHC_RESET BUTTON_FF
152 #define CHC_MENU BUTTON_REW
153 #define CHC_SETTINGS_INC BUTTON_RIGHT
154 #define CHC_SETTINGS_DEC BUTTON_LEFT
155 #define CHC_SETTINGS_OK BUTTON_PLAY
156 #define CHC_SETTINGS_CANCEL BUTTON_POWER
158 #elif CONFIG_KEYPAD == MROBE500_PAD
159 #define CHC_QUIT BUTTON_POWER
160 #define CHC_STARTSTOP BUTTON_RC_PLAY
161 #define CHC_RESET BUTTON_RC_HEART
162 #define CHC_MENU BUTTON_RC_MODE
163 #define CHC_SETTINGS_INC BUTTON_RC_VOL_UP
164 #define CHC_SETTINGS_DEC BUTTON_RC_VOL_DOWN
165 #define CHC_SETTINGS_OK BUTTON_RC_PLAY
166 #define CHC_SETTINGS_CANCEL BUTTON_POWER
168 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
169 #define CHC_QUIT BUTTON_BACK
170 #define CHC_STARTSTOP BUTTON_PLAY
171 #define CHC_RESET BUTTON_PREV
172 #define CHC_MENU BUTTON_MENU
173 #define CHC_SETTINGS_INC BUTTON_UP
174 #define CHC_SETTINGS_DEC BUTTON_DOWN
175 #define CHC_SETTINGS_OK BUTTON_SELECT
176 #define CHC_SETTINGS_CANCEL BUTTON_BACK
178 #elif CONFIG_KEYPAD == MROBE100_PAD
179 #define CHC_QUIT BUTTON_POWER
180 #define CHC_STARTSTOP BUTTON_SELECT
181 #define CHC_RESET BUTTON_DISPLAY
182 #define CHC_MENU BUTTON_MENU
183 #define CHC_SETTINGS_INC BUTTON_UP
184 #define CHC_SETTINGS_DEC BUTTON_DOWN
185 #define CHC_SETTINGS_OK BUTTON_SELECT
186 #define CHC_SETTINGS_CANCEL BUTTON_POWER
188 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
189 #define CHC_QUIT BUTTON_RC_REC
190 #define CHC_STARTSTOP BUTTON_RC_PLAY
191 #define CHC_RESET BUTTON_RC_REW
192 #define CHC_MENU BUTTON_RC_MENU
193 #define CHC_SETTINGS_INC BUTTON_RC_VOL_UP
194 #define CHC_SETTINGS_DEC BUTTON_RC_VOL_DOWN
195 #define CHC_SETTINGS_OK BUTTON_RC_PLAY
196 #define CHC_SETTINGS_CANCEL BUTTON_RC_REC
198 #elif CONFIG_KEYPAD == COWON_D2_PAD
199 #define CHC_QUIT BUTTON_POWER
200 #define CHC_RESET (BUTTON_CENTER|BUTTON_MENU)
201 #define CHC_MENU BUTTON_MENU
202 #define CHC_SETTINGS_INC BUTTON_PLUS
203 #define CHC_SETTINGS_DEC BUTTON_MINUS
204 #define CHC_SETTINGS_CANCEL BUTTON_POWER
206 #elif CONFIG_KEYPAD == IAUDIO67_PAD
207 #define CHC_QUIT BUTTON_POWER
208 #define CHC_STARTSTOP BUTTON_PLAY
209 #define CHC_RESET BUTTON_LEFT
210 #define CHC_MENU BUTTON_MENU
211 #define CHC_SETTINGS_INC BUTTON_VOLUP
212 #define CHC_SETTINGS_DEC BUTTON_VOLDOWN
213 #define CHC_SETTINGS_OK BUTTON_PLAY
214 #define CHC_SETTINGS_OK2 BUTTON_LEFT
215 #define CHC_SETTINGS_CANCEL BUTTON_POWER
217 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
218 #define CHC_QUIT BUTTON_BACK
219 #define CHC_STARTSTOP BUTTON_PLAY
220 #define CHC_RESET BUTTON_CUSTOM
221 #define CHC_MENU BUTTON_MENU
222 #define CHC_SETTINGS_INC BUTTON_UP
223 #define CHC_SETTINGS_DEC BUTTON_DOWN
224 #define CHC_SETTINGS_OK BUTTON_SELECT
225 #define CHC_SETTINGS_CANCEL BUTTON_BACK
227 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
228 #define CHC_QUIT BUTTON_POWER
229 #define CHC_STARTSTOP BUTTON_SELECT
230 #define CHC_RESET BUTTON_VIEW
231 #define CHC_MENU BUTTON_MENU
232 #define CHC_SETTINGS_INC BUTTON_RIGHT
233 #define CHC_SETTINGS_DEC BUTTON_LEFT
234 #define CHC_SETTINGS_OK BUTTON_SELECT
235 #define CHC_SETTINGS_CANCEL BUTTON_POWER
237 #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
238 #define CHC_QUIT BUTTON_POWER
239 #define CHC_STARTSTOP BUTTON_PLAY
240 #define CHC_RESET BUTTON_RIGHT
241 #define CHC_MENU BUTTON_MENU
242 #define CHC_SETTINGS_INC BUTTON_NEXT
243 #define CHC_SETTINGS_DEC BUTTON_PREV
244 #define CHC_SETTINGS_OK BUTTON_PLAY
245 #define CHC_SETTINGS_CANCEL BUTTON_POWER
247 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
248 #define CHC_QUIT BUTTON_POWER
249 #define CHC_STARTSTOP BUTTON_PLAY
250 #define CHC_RESET BUTTON_RIGHT
251 #define CHC_MENU BUTTON_MENU
252 #define CHC_SETTINGS_INC BUTTON_NEXT
253 #define CHC_SETTINGS_DEC BUTTON_PREV
254 #define CHC_SETTINGS_OK BUTTON_PLAY
255 #define CHC_SETTINGS_CANCEL BUTTON_POWER
257 #elif CONFIG_KEYPAD == ONDAVX747_PAD
258 #define CHC_QUIT BUTTON_POWER
259 #define CHC_RESET (BUTTON_VOL_UP|BUTTON_VOL_DOWN)
260 #define CHC_MENU BUTTON_MENU
261 #define CHC_SETTINGS_INC BUTTON_VOL_UP
262 #define CHC_SETTINGS_DEC BUTTON_VOL_DOWN
263 #define CHC_SETTINGS_CANCEL BUTTON_POWER
265 #elif CONFIG_KEYPAD == ONDAVX777_PAD
266 #define CHC_QUIT BUTTON_POWER
267 #define CHC_SETTINGS_CANCEL BUTTON_POWER
269 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
270 #define CHC_QUIT BUTTON_REC
271 #define CHC_STARTSTOP BUTTON_PLAY
272 #define CHC_RESET BUTTON_FFWD
273 #define CHC_MENU BUTTON_LEFT
274 #define CHC_SETTINGS_INC BUTTON_UP
275 #define CHC_SETTINGS_DEC BUTTON_DOWN
276 #define CHC_SETTINGS_OK BUTTON_PLAY
277 #define CHC_SETTINGS_CANCEL BUTTON_REW
279 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
280 #define CHC_QUIT BUTTON_REC
281 #define CHC_STARTSTOP BUTTON_PLAY
282 #define CHC_RESET BUTTON_PREV
283 #define CHC_MENU BUTTON_MENU
284 #define CHC_SETTINGS_INC BUTTON_UP
285 #define CHC_SETTINGS_DEC BUTTON_DOWN
286 #define CHC_SETTINGS_OK BUTTON_OK
287 #define CHC_SETTINGS_CANCEL BUTTON_CANCEL
289 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
290 #define CHC_QUIT (BUTTON_REC|BUTTON_PLAY)
291 #define CHC_STARTSTOP BUTTON_PLAY
292 #define CHC_RESET BUTTON_REW
293 #define CHC_MENU (BUTTON_FUNC|BUTTON_REPEAT)
294 #define CHC_SETTINGS_INC BUTTON_VOL_UP
295 #define CHC_SETTINGS_DEC BUTTON_VOL_DOWN
296 #define CHC_SETTINGS_OK BUTTON_FUNC
297 #define CHC_SETTINGS_CANCEL BUTTON_REC
299 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
300 #define CHC_QUIT (BUTTON_MENU|BUTTON_REPEAT)
301 #define CHC_STARTSTOP BUTTON_PLAY
302 #define CHC_RESET BUTTON_REW
303 #define CHC_MENU BUTTON_MENU
304 #define CHC_SETTINGS_INC BUTTON_UP
305 #define CHC_SETTINGS_DEC BUTTON_DOWN
306 #define CHC_SETTINGS_OK BUTTON_ENTER
307 #define CHC_SETTINGS_CANCEL BUTTON_REC
309 #elif CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD
310 #define CHC_QUIT BUTTON_POWER
311 #define CHC_STARTSTOP BUTTON_PLAYPAUSE
312 #define CHC_RESET BUTTON_BACK
313 #define CHC_MENU BUTTON_SELECT
314 #define CHC_SETTINGS_INC BUTTON_UP
315 #define CHC_SETTINGS_DEC BUTTON_DOWN
316 #define CHC_SETTINGS_OK BUTTON_SELECT
317 #define CHC_SETTINGS_CANCEL BUTTON_BACK
319 #elif CONFIG_KEYPAD == SANSA_CONNECT_PAD
320 #define CHC_QUIT BUTTON_POWER
321 #define CHC_STARTSTOP BUTTON_NEXT
322 #define CHC_RESET BUTTON_PREV
323 #define CHC_MENU BUTTON_SELECT
324 #define CHC_SETTINGS_INC BUTTON_VOL_UP
325 #define CHC_SETTINGS_DEC BUTTON_VOL_DOWN
326 #define CHC_SETTINGS_OK BUTTON_SELECT
327 #define CHC_SETTINGS_CANCEL BUTTON_LEFT
329 #elif CONFIG_KEYPAD == SAMSUNG_YPR0_PAD
330 #define CHC_QUIT BUTTON_BACK
331 #define CHC_STARTSTOP BUTTON_SELECT
332 #define CHC_RESET BUTTON_USER
333 #define CHC_MENU BUTTON_MENU
334 #define CHC_SETTINGS_INC BUTTON_UP
335 #define CHC_SETTINGS_DEC BUTTON_DOWN
336 #define CHC_SETTINGS_OK BUTTON_SELECT
337 #define CHC_SETTINGS_CANCEL BUTTON_BACK
339 #elif CONFIG_KEYPAD == HM60X_PAD
340 #define CHC_QUIT BUTTON_POWER
341 #define CHC_STARTSTOP BUTTON_SELECT
342 #define CHC_RESET BUTTON_LEFT
343 #define CHC_MENU BUTTON_RIGHT
344 #define CHC_SETTINGS_INC BUTTON_UP
345 #define CHC_SETTINGS_DEC BUTTON_DOWN
346 #define CHC_SETTINGS_OK BUTTON_SELECT
347 #define CHC_SETTINGS_CANCEL BUTTON_POWER
349 #elif CONFIG_KEYPAD == HM801_PAD
350 #define CHC_QUIT BUTTON_POWER
351 #define CHC_STARTSTOP BUTTON_PLAY
352 #define CHC_RESET BUTTON_LEFT
353 #define CHC_MENU BUTTON_RIGHT
354 #define CHC_SETTINGS_INC BUTTON_UP
355 #define CHC_SETTINGS_DEC BUTTON_DOWN
356 #define CHC_SETTINGS_OK BUTTON_SELECT
357 #define CHC_SETTINGS_CANCEL BUTTON_PREV
359 #else
360 #error No keymap defined!
361 #endif
363 #ifdef HAVE_TOUCHSCREEN
364 #ifndef CHC_SETTINGS_OK
365 #define CHC_SETTINGS_OK BUTTON_CENTER
366 #endif
367 #ifndef CHC_STARTSTOP
368 #define CHC_STARTSTOP BUTTON_CENTER
369 #endif
370 #ifndef CHC_SETTINGS_INC
371 #define CHC_SETTINGS_INC BUTTON_TOPMIDDLE
372 #endif
373 #ifndef CHC_SETTINGS_DEC
374 #define CHC_SETTINGS_DEC BUTTON_BOTTOMMIDDLE
375 #endif
376 #ifndef CHC_RESET
377 #define CHC_RESET BUTTON_TOPLEFT
378 #endif
379 #ifndef CHC_MENU
380 #define CHC_MENU BUTTON_TOPRIGHT
381 #endif
382 #endif
385 /* leave first line blank on bitmap display, for pause icon */
386 #ifdef HAVE_LCD_BITMAP
387 #define FIRST_LINE 1
388 #else
389 #define FIRST_LINE 0
390 #endif
392 #define MAX_PLAYERS 10
394 enum {
395 CHCL_OK,
396 CHCL_CANCEL,
397 CHCL_USB,
398 CHCL_NEXT,
399 CHCL_PREV,
402 static struct {
403 int nr_timers;
404 int total_time;
405 int round_time;
406 } settings;
408 static struct {
409 int total_time;
410 int used_time;
411 bool hidden;
412 } timer_holder[MAX_PLAYERS];
414 static int run_timer(int nr);
415 static int chessclock_set_int(char* string,
416 int* variable,
417 int step,
418 int min,
419 int max,
420 int flags);
421 #define FLAGS_SET_INT_SECONDS 1
423 static char * show_time(int secs);
425 static bool chesspause;
427 #define MAX_TIME 7200
429 /* this is the plugin entry point */
430 enum plugin_status plugin_start(const void* parameter)
432 int i;
433 bool done;
434 int nr;
436 (void)parameter;
438 settings.nr_timers = 1;
439 settings.total_time = 10;
440 settings.round_time = 10;
442 /* now go ahead and have fun! */
443 rb->splash(HZ, "Chess Clock");
445 rb->lcd_clear_display();
446 i=0;
447 while (1) {
448 int res;
449 switch (i) {
450 case 0:
451 res=chessclock_set_int("Number of players",
452 &settings.nr_timers, 1, 1,
453 MAX_PLAYERS, 0);
454 break;
455 case 1:
456 res=chessclock_set_int("Total time",
457 &settings.total_time, 10, 10, MAX_TIME,
458 FLAGS_SET_INT_SECONDS);
459 settings.round_time=settings.total_time;
460 break;
461 case 2:
462 res=chessclock_set_int("Max round time", &settings.round_time,
463 10, 10, settings.round_time,
464 FLAGS_SET_INT_SECONDS);
465 break;
466 default:
467 res=-2; /* done */
468 break;
470 if (res==CHCL_USB) {
471 return PLUGIN_USB_CONNECTED;
472 } else if (res==CHCL_CANCEL) {
473 i--;
474 if (i<0) {
475 return PLUGIN_OK;
477 } else if (res==CHCL_OK) {
478 i++;
479 } else if (res==-2) { /* done */
480 break;
483 for (i=0; i<settings.nr_timers; i++) {
484 timer_holder[i].total_time=settings.total_time;
485 timer_holder[i].used_time=0;
486 timer_holder[i].hidden=false;
489 chesspause=true; /* We start paused */
491 nr=0;
492 do {
493 int ret=0;
494 done=true;
495 i = nr;
496 do {
497 if (!timer_holder[i].hidden)
499 nr = i;
500 done = false;
501 break;
503 if (++i == settings.nr_timers)
504 i = 0;
505 } while (i != nr);
507 if (done) {
508 return PLUGIN_OK;
511 ret = run_timer(nr);
512 switch (ret) {
513 case CHCL_CANCEL: /* exit */
514 done=true;
515 break;
516 case CHCL_USB:
517 return PLUGIN_USB_CONNECTED;
518 case CHCL_NEXT:
519 nr++;
520 if (nr>=settings.nr_timers)
521 nr=0;
522 break;
523 case CHCL_PREV:
524 do {
525 nr--;
526 if (nr<0)
527 nr=settings.nr_timers-1;
528 } while (timer_holder[nr].hidden);
529 break;
531 } while (!done);
532 return PLUGIN_OK;
535 #ifdef HAVE_LCD_BITMAP
536 static void show_pause_mode(bool enabled)
538 static const char pause_icon[] = {0x00,0x7f,0x7f,0x00,0x7f,0x7f,0x00};
540 if (enabled)
541 rb->lcd_mono_bitmap((unsigned char *)pause_icon, 52, 0, 7, 8);
542 else
544 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
545 rb->lcd_fillrect(52, 0, 7, 8);
546 rb->lcd_set_drawmode(DRMODE_SOLID);
549 #else
550 #define show_pause_mode(enabled) rb->lcd_icon(ICON_PAUSE, enabled)
551 #endif
553 static int run_timer(int nr)
555 char buf[40];
556 char player_info[13];
557 long last_tick;
558 bool done=false;
559 int retval=CHCL_OK;
560 long max_ticks=timer_holder[nr].total_time*HZ-timer_holder[nr].used_time;
561 long ticks=0;
562 bool round_time=false;
564 show_pause_mode(chesspause);
566 if (settings.round_time*HZ<max_ticks) {
567 max_ticks=settings.round_time*HZ;
568 round_time=true;
570 rb->snprintf(player_info, sizeof(player_info), "Player %d", nr+1);
571 rb->lcd_puts(0, FIRST_LINE, (unsigned char *)player_info);
572 last_tick=*rb->current_tick;
574 while (!done) {
575 int button;
576 long now;
577 if (ticks>=max_ticks) {
578 if (round_time)
579 rb->lcd_puts(0, FIRST_LINE+1, (unsigned char *)"ROUND UP!");
580 else
581 rb->lcd_puts(0, FIRST_LINE+1, (unsigned char *)"TIME OUT!");
582 rb->backlight_on();
583 ticks = max_ticks;
584 } else {
585 now=*rb->current_tick;
586 if (!chesspause) {
587 ticks+=now-last_tick;
588 if ((max_ticks-ticks)/HZ == 10) {
589 /* Backlight on if 10 seconds remain */
590 rb->backlight_on();
593 last_tick=now;
594 if (round_time) {
595 rb->snprintf(buf, sizeof(buf), "%s/",
596 show_time((max_ticks-ticks+HZ-1)/HZ));
597 /* Append total time */
598 rb->strcpy(&buf[rb->strlen(buf)],
599 show_time((timer_holder[nr].total_time*HZ-
600 timer_holder[nr].used_time-
601 ticks+HZ-1)/HZ));
602 rb->lcd_puts(0, FIRST_LINE+1, (unsigned char *)buf);
603 } else {
604 rb->lcd_puts(0, FIRST_LINE+1,
605 (unsigned char *)show_time((max_ticks-ticks+HZ-1)/HZ));
608 rb->lcd_update();
610 button = rb->button_get(false);
611 switch (button) {
612 /* OFF/ON key to exit */
613 case CHC_QUIT:
614 return CHCL_CANCEL; /* Indicate exit */
616 /* PLAY = Stop/Start toggle */
617 case CHC_STARTSTOP:
618 chesspause=!chesspause;
619 show_pause_mode(chesspause);
620 break;
622 /* LEFT = Reset timer */
623 case CHC_RESET:
624 ticks=0;
625 break;
627 /* MENU */
628 case CHC_MENU:
630 MENUITEM_STRINGLIST(menu, "Menu", NULL,
631 "Delete player", "Restart round",
632 "Set round time", "Set total time",
633 "Playback Control");
635 int val, res;
636 switch(rb->do_menu(&menu, NULL, NULL, false))
638 case 0:
639 /* delete player */
640 timer_holder[nr].hidden=true;
641 retval = CHCL_NEXT;
642 done=true;
643 break;
644 case 1:
645 /* restart */
646 ticks=0;
647 last_tick=*rb->current_tick;
648 break;
649 case 2:
650 /* set round time */
651 val=(max_ticks-ticks)/HZ;
652 res=chessclock_set_int("Round time",
653 &val, 10, 0, MAX_TIME,
654 FLAGS_SET_INT_SECONDS);
655 if (res==CHCL_USB) {
656 retval = CHCL_USB;
657 done=true;
658 } else if (res==CHCL_OK) {
659 ticks=max_ticks-val*HZ;
660 last_tick=*rb->current_tick;
662 break;
663 case 3:
664 /* set total time */
665 val=timer_holder[nr].total_time;
666 res=chessclock_set_int("Total time",
667 &val,
668 10, 0, MAX_TIME,
669 FLAGS_SET_INT_SECONDS);
670 if (res==CHCL_USB) {
671 retval = CHCL_USB;
672 done=true;
673 } else if (res==CHCL_OK) {
674 timer_holder[nr].total_time=val;
676 break;
677 case 4:
678 playback_control(NULL);
679 break;
680 case MENU_ATTACHED_USB:
681 retval = CHCL_USB;
682 done=true;
683 break;
685 rb->lcd_clear_display();
686 show_pause_mode(chesspause);
687 rb->lcd_puts(0, FIRST_LINE, (unsigned char *)player_info);
689 break;
691 /* UP (RIGHT/+) = Scroll Lap timer up */
692 case CHC_SETTINGS_INC:
693 retval = CHCL_NEXT;
694 done = true;
695 break;
697 /* DOWN (LEFT/-) = Scroll Lap timer down */
698 case CHC_SETTINGS_DEC:
699 retval = CHCL_PREV;
700 done = true;
701 break;
703 default:
704 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
705 retval = CHCL_USB;
706 done = true;
708 break;
710 rb->sleep(HZ/4); /* Sleep 1/4 of a second */
713 timer_holder[nr].used_time+=ticks;
715 return retval;
718 static int chessclock_set_int(char* string,
719 int* variable,
720 int step,
721 int min,
722 int max,
723 int flags)
725 bool done = false;
726 int button;
728 rb->lcd_clear_display();
729 rb->lcd_puts_scroll(0, FIRST_LINE, (unsigned char *)string);
731 while (!done) {
732 char str[32];
733 if (flags & FLAGS_SET_INT_SECONDS)
734 rb->snprintf(str, sizeof str,"%s (m:s)", show_time(*variable));
735 else
736 rb->snprintf(str, sizeof str,"%d", *variable);
737 rb->lcd_puts(0, FIRST_LINE+1, (unsigned char *)str);
738 rb->lcd_update();
740 button = rb->button_get(true);
741 switch(button) {
742 case CHC_SETTINGS_INC:
743 case CHC_SETTINGS_INC | BUTTON_REPEAT:
744 *variable += step;
745 break;
747 case CHC_SETTINGS_DEC:
748 case CHC_SETTINGS_DEC | BUTTON_REPEAT:
749 *variable -= step;
750 break;
752 case CHC_SETTINGS_OK:
753 #ifdef CHC_SETTINGS_OK2
754 case CHC_SETTINGS_OK2:
755 #endif
756 done = true;
757 break;
759 case CHC_SETTINGS_CANCEL:
760 #ifdef CHC_SETTINGS_CANCEL2
761 case CHC_SETTINGS_CANCEL2:
762 #endif
763 return CHCL_CANCEL;
764 break;
766 default:
767 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
768 return CHCL_USB;
769 break;
772 if(*variable > max )
773 *variable = max;
775 if(*variable < min )
776 *variable = min;
779 rb->lcd_stop_scroll();
781 return CHCL_OK;
784 static char * show_time(int seconds)
786 static char buf[]="00:00";
787 rb->snprintf(buf, sizeof(buf), "%02d:%02d", seconds/60, seconds%60);
788 return buf;