fix the charging icon so it will actually display correctly
[kugel-rb.git] / apps / screens.c
blob5f84830c0029cccba13436ccce7618fc9aa7155a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
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 <stdbool.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include "backlight.h"
26 #include "action.h"
27 #include "lcd.h"
28 #include "lang.h"
29 #include "icons.h"
30 #include "font.h"
31 #include "audio.h"
32 #include "mp3_playback.h"
33 #include "usb.h"
34 #include "settings.h"
35 #include "status.h"
36 #include "playlist.h"
37 #include "sprintf.h"
38 #include "kernel.h"
39 #include "power.h"
40 #include "system.h"
41 #include "powermgmt.h"
42 #include "talk.h"
43 #include "misc.h"
44 #include "metadata.h"
45 #include "screens.h"
46 #include "debug.h"
47 #include "led.h"
48 #include "sound.h"
49 #include "splash.h"
50 #include "statusbar.h"
51 #include "screen_access.h"
52 #include "pcmbuf.h"
53 #include "list.h"
54 #include "yesno.h"
55 #include "backdrop.h"
56 #include "viewport.h"
57 #include "language.h"
59 #if CONFIG_CODEC == SWCODEC
60 #include "dsp.h"
61 #endif
63 #if (CONFIG_STORAGE & STORAGE_MMC)
64 int mmc_remove_request(void)
66 struct queue_event ev;
67 int i;
68 FOR_NB_SCREENS(i)
69 screens[i].clear_display();
70 splash(0, ID2P(LANG_REMOVE_MMC));
72 while (1)
74 queue_wait_w_tmo(&button_queue, &ev, HZ/2);
75 switch (ev.id)
77 case SYS_HOTSWAP_EXTRACTED:
78 return SYS_HOTSWAP_EXTRACTED;
80 case SYS_USB_DISCONNECTED:
81 return SYS_USB_DISCONNECTED;
85 #endif
87 /* the charging screen is only used for archos targets */
88 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING) && defined(CPU_SH)
90 #ifdef HAVE_LCD_BITMAP
91 static void charging_display_info(bool animate)
93 unsigned char charging_logo[36];
94 const int pox_x = (LCD_WIDTH - sizeof(charging_logo)) / 2;
95 const int pox_y = 32;
96 static unsigned phase = 3;
97 unsigned i;
99 #ifdef NEED_ATA_POWER_BATT_MEASURE
100 if (ide_powered()) /* FM and V2 can only measure when ATA power is on */
101 #endif
103 int battv = battery_voltage();
104 lcd_putsf(0, 7, " Batt: %d.%02dV %d%% ", battv / 1000,
105 (battv % 1000) / 10, battery_level());
108 #ifdef ARCHOS_RECORDER
109 lcd_puts(0, 2, "Charge mode:");
111 const char *s;
112 if (charge_state == CHARGING)
113 s = str(LANG_BATTERY_CHARGE);
114 else if (charge_state == TOPOFF)
115 s = str(LANG_BATTERY_TOPOFF_CHARGE);
116 else if (charge_state == TRICKLE)
117 s = str(LANG_BATTERY_TRICKLE_CHARGE);
118 else
119 s = "not charging";
121 lcd_puts(0, 3, s);
122 if (!charger_enabled())
123 animate = false;
124 #endif /* ARCHOS_RECORDER */
126 /* middle part */
127 memset(charging_logo+3, 0x00, 32);
128 charging_logo[0] = 0x3C;
129 charging_logo[1] = 0x24;
130 charging_logo[2] = charging_logo[35] = 0xFF;
132 if (!animate)
133 { /* draw the outline */
134 /* middle part */
135 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
136 sizeof(charging_logo), 8);
137 lcd_set_drawmode(DRMODE_FG);
138 /* upper line */
139 charging_logo[0] = charging_logo[1] = 0x00;
140 memset(charging_logo+2, 0x80, 34);
141 lcd_mono_bitmap(charging_logo, pox_x, pox_y, sizeof(charging_logo), 8);
142 /* lower line */
143 memset(charging_logo+2, 0x01, 34);
144 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 16,
145 sizeof(charging_logo), 8);
146 lcd_set_drawmode(DRMODE_SOLID);
148 else
149 { /* animate the middle part */
150 for (i = 3; i<MIN(sizeof(charging_logo)-1, phase); i++)
152 if ((i-phase) % 8 == 0)
153 { /* draw a "bubble" here */
154 unsigned bitpos;
155 bitpos = (phase + i/8) % 15; /* "bounce" effect */
156 if (bitpos > 7)
157 bitpos = 14 - bitpos;
158 charging_logo[i] = BIT_N(bitpos);
161 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
162 sizeof(charging_logo), 8);
163 phase++;
165 lcd_update();
167 #else /* not HAVE_LCD_BITMAP */
169 static unsigned long logo_chars[4];
170 static const unsigned char logo_pattern[] = {
171 0x07, 0x04, 0x1c, 0x14, 0x1c, 0x04, 0x07, 0, /* char 1 */
172 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 2 */
173 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 3 */
174 0x1f, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1f, 0, /* char 4 */
177 static void logo_lock_patterns(bool on)
179 int i;
181 if (on)
183 for (i = 0; i < 4; i++)
184 logo_chars[i] = lcd_get_locked_pattern();
186 else
188 for (i = 0; i < 4; i++)
189 lcd_unlock_pattern(logo_chars[i]);
193 static void charging_display_info(bool animate)
195 int battv;
196 unsigned i, ypos;
197 static unsigned phase = 3;
198 char buf[32];
200 battv = battery_voltage();
201 lcd_putsf(4, 1, " %d.%02dV", battv / 1000, (battv % 1000) / 10);
203 memcpy(buf, logo_pattern, 32); /* copy logo patterns */
205 if (!animate) /* build the screen */
207 lcd_double_height(false);
208 lcd_puts(0, 0, "[Charging]");
209 for (i = 0; i < 4; i++)
210 lcd_putc(i, 1, logo_chars[i]);
212 else /* animate the logo */
214 for (i = 3; i < MIN(19, phase); i++)
216 if ((i - phase) % 5 == 0)
217 { /* draw a "bubble" here */
218 ypos = (phase + i/5) % 9; /* "bounce" effect */
219 if (ypos > 4)
220 ypos = 8 - ypos;
221 buf[5 - ypos + 8 * (i/5)] |= 0x10u >> (i%5);
224 phase++;
227 for (i = 0; i < 4; i++)
228 lcd_define_pattern(logo_chars[i], buf + 8 * i);
230 lcd_update();
232 #endif /* (not) HAVE_LCD_BITMAP */
234 /* blocks while charging, returns on event:
235 1 if charger cable was removed
236 2 if Off/Stop key was pressed
237 3 if On key was pressed
238 4 if USB was connected */
240 int charging_screen(void)
242 unsigned int button;
243 int rc = 0;
245 ide_power_enable(false); /* power down the disk, else would be spinning */
247 lcd_clear_display();
248 backlight_set_timeout(global_settings.backlight_timeout);
249 #ifdef HAVE_REMOTE_LCD
250 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
251 #endif
252 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
254 #ifdef HAVE_LCD_CHARCELLS
255 logo_lock_patterns(true);
256 #endif
257 charging_display_info(false);
261 gui_syncstatusbar_draw(&statusbars, false);
262 charging_display_info(true);
263 button = get_action(CONTEXT_STD,HZ/3);
264 if (button == ACTION_STD_OK)
265 rc = 2;
266 else if (usb_detect() == USB_INSERTED)
267 rc = 3;
268 else if (!charger_inserted())
269 rc = 1;
270 } while (!rc);
272 #ifdef HAVE_LCD_CHARCELLS
273 logo_lock_patterns(false);
274 #endif
275 return rc;
277 #endif /* CONFIG_CHARGING && !HAVE_POWEROFF_WHILE_CHARGING && defined(CPU_SH) */
279 #if CONFIG_CHARGING
280 void charging_splash(void)
282 splash(2*HZ, str(LANG_BATTERY_CHARGE));
283 button_clear_queue();
285 #endif
288 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
290 /* little helper function for voice output */
291 static void say_time(int cursorpos, const struct tm *tm)
293 int value = 0;
294 int unit = 0;
296 if (!global_settings.talk_menu)
297 return;
299 switch(cursorpos)
301 case 0:
302 value = tm->tm_hour;
303 unit = UNIT_HOUR;
304 break;
305 case 1:
306 value = tm->tm_min;
307 unit = UNIT_MIN;
308 break;
309 case 2:
310 value = tm->tm_sec;
311 unit = UNIT_SEC;
312 break;
313 case 3:
314 value = tm->tm_year + 1900;
315 break;
316 case 5:
317 value = tm->tm_mday;
318 break;
321 if (cursorpos == 4) /* month */
322 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, false);
323 else
324 talk_value(value, unit, false);
328 #define INDEX_X 0
329 #define INDEX_Y 1
331 #define SEPARATOR ":"
333 #define IDX_HOURS 0
334 #define IDX_MINUTES 1
335 #define IDX_SECONDS 2
336 #define IDX_YEAR 3
337 #define IDX_MONTH 4
338 #define IDX_DAY 5
340 #define OFF_HOURS 0
341 #define OFF_MINUTES 3
342 #define OFF_SECONDS 6
343 #define OFF_YEAR 9
344 #define OFF_DAY 14
346 bool set_time_screen(const char* title, struct tm *tm)
348 bool done = false;
349 int cursorpos = 0;
350 unsigned int statusbar_height = 0;
351 unsigned char offsets_ptr[] =
352 { OFF_HOURS, OFF_MINUTES, OFF_SECONDS, OFF_YEAR, 0, OFF_DAY };
354 if (lang_is_rtl())
356 offsets_ptr[IDX_HOURS] = OFF_SECONDS;
357 offsets_ptr[IDX_SECONDS] = OFF_HOURS;
358 offsets_ptr[IDX_YEAR] = OFF_DAY;
359 offsets_ptr[IDX_DAY] = OFF_YEAR;
362 if(global_settings.statusbar)
363 statusbar_height = STATUSBAR_HEIGHT;
365 /* speak selection when screen is entered */
366 say_time(cursorpos, tm);
368 while (!done) {
369 int button;
370 unsigned int i, s, realyear, min, max;
371 unsigned char *ptr[6];
372 unsigned char buffer[20];
373 int *valptr = NULL;
374 static unsigned char daysinmonth[] =
375 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
377 /* for easy acess in the drawing loop */
378 for (i = 0; i < 6; i++)
379 ptr[i] = buffer + offsets_ptr[i];
380 ptr[IDX_MONTH] = str(LANG_MONTH_JANUARY + tm->tm_mon); /* month name */
382 /* calculate the number of days in febuary */
383 realyear = tm->tm_year + 1900;
384 if((realyear % 4 == 0 && !(realyear % 100 == 0)) || realyear % 400 == 0)
385 daysinmonth[1] = 29;
386 else
387 daysinmonth[1] = 28;
389 /* fix day if month or year changed */
390 if (tm->tm_mday > daysinmonth[tm->tm_mon])
391 tm->tm_mday = daysinmonth[tm->tm_mon];
393 /* calculate day of week */
394 set_day_of_week(tm);
396 /* put all the numbers we want from the tm struct into
397 an easily printable buffer */
398 snprintf(buffer, sizeof(buffer),
399 "%02d " "%02d " "%02d " "%04d " "%02d",
400 tm->tm_hour, tm->tm_min, tm->tm_sec,
401 tm->tm_year+1900, tm->tm_mday);
403 /* convert spaces in the buffer to '\0' to make it possible to work
404 directly on the buffer */
405 for(i=0; i < sizeof(buffer); i++)
407 if(buffer[i] == ' ')
408 buffer[i] = '\0';
411 FOR_NB_SCREENS(s)
413 int pos, nb_lines;
414 unsigned int separator_width, weekday_width;
415 unsigned int j, width, prev_line_height;
416 struct viewport viewports[NB_SCREENS];
417 /* 6 possible cursor possitions, 2 values stored for each: x, y */
418 unsigned int cursor[6][2];
419 struct viewport *vp = &viewports[s];
420 struct screen *screen = &screens[s];
421 static unsigned char rtl_idx[] =
422 { IDX_SECONDS, IDX_MINUTES, IDX_HOURS, IDX_DAY, IDX_MONTH, IDX_YEAR };
424 viewport_set_defaults(vp, s);
425 screen->set_viewport(vp);
426 nb_lines = viewport_get_nb_lines(vp);
428 /* minimum lines needed is 2 + title line */
429 if (nb_lines < 4)
431 vp->font = FONT_SYSFIXED;
432 nb_lines = viewport_get_nb_lines(vp);
435 /* recalculate the positions and offsets */
436 if (nb_lines >= 3)
437 screen->getstringsize(title, NULL, &prev_line_height);
438 else
439 prev_line_height = 0;
441 screen->getstringsize(SEPARATOR, &separator_width, NULL);
443 /* weekday */
444 screen->getstringsize(str(LANG_WEEKDAY_SUNDAY + tm->tm_wday),
445 &weekday_width, NULL);
446 screen->getstringsize(" ", &separator_width, NULL);
448 for(i=0, j=0; i < 6; i++)
450 if(i==3) /* second row */
452 j = weekday_width + separator_width;
453 prev_line_height *= 2;
455 screen->getstringsize(ptr[i], &width, NULL);
456 cursor[i][INDEX_Y] = prev_line_height + statusbar_height;
457 cursor[i][INDEX_X] = j;
458 j += width + separator_width;
461 /* draw the screen */
462 screen->set_viewport(vp);
463 screen->clear_viewport();
464 /* display the screen title */
465 screen->puts_scroll(0, 0, title);
467 /* these are not selectable, so we draw them outside the loop */
468 /* name of the week day */
469 screen->putsxy(0, cursor[3][INDEX_Y],
470 str(LANG_WEEKDAY_SUNDAY + tm->tm_wday));
472 pos = lang_is_rtl() ? rtl_idx[cursorpos] : cursorpos;
473 /* draw the selected item with drawmode set to
474 DRMODE_SOLID|DRMODE_INVERSEVID, all other selectable
475 items with drawmode DRMODE_SOLID */
476 for(i=0; i<6; i++)
478 if (pos == (int)i)
479 vp->drawmode = (DRMODE_SOLID|DRMODE_INVERSEVID);
481 screen->putsxy(cursor[i][INDEX_X],
482 cursor[i][INDEX_Y], ptr[i]);
484 vp->drawmode = DRMODE_SOLID;
486 screen->putsxy(cursor[i/4 +1][INDEX_X] - separator_width,
487 cursor[0][INDEX_Y], SEPARATOR);
490 /* print help text */
491 if (nb_lines > 4)
492 screen->puts(0, 4, str(LANG_TIME_SET_BUTTON));
493 if (nb_lines > 5)
494 screen->puts(0, 5, str(LANG_TIME_REVERT));
495 screen->update_viewport();
496 screen->set_viewport(NULL);
499 /* set the most common numbers */
500 min = 0;
501 max = 59;
502 /* calculate the minimum and maximum for the number under cursor */
503 switch(cursorpos) {
504 case 0: /* hour */
505 max = 23;
506 valptr = &tm->tm_hour;
507 break;
508 case 1: /* minute */
509 valptr = &tm->tm_min;
510 break;
511 case 2: /* second */
512 valptr = &tm->tm_sec;
513 break;
514 case 3: /* year */
515 min = 1;
516 max = 200;
517 valptr = &tm->tm_year;
518 break;
519 case 4: /* month */
520 max = 11;
521 valptr = &tm->tm_mon;
522 break;
523 case 5: /* day */
524 min = 1;
525 max = daysinmonth[tm->tm_mon];
526 valptr = &tm->tm_mday;
527 break;
530 #ifdef HAVE_TOUCHSCREEN
531 enum touchscreen_mode old_mode = touchscreen_get_mode();
533 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
534 #endif
535 button = get_action(CONTEXT_SETTINGS_TIME, TIMEOUT_BLOCK);
536 #ifdef HAVE_TOUCHSCREEN
537 touchscreen_set_mode(old_mode);
538 #endif
539 switch ( button ) {
540 case ACTION_STD_PREV:
541 cursorpos = clamp_value_wrap(--cursorpos, 5, 0);
542 say_time(cursorpos, tm);
543 break;
544 case ACTION_STD_NEXT:
545 cursorpos = clamp_value_wrap(++cursorpos, 5, 0);
546 say_time(cursorpos, tm);
547 break;
548 case ACTION_SETTINGS_INC:
549 case ACTION_SETTINGS_INCREPEAT:
550 *valptr = clamp_value_wrap(++(*valptr), max, min);
551 say_time(cursorpos, tm);
552 break;
553 case ACTION_SETTINGS_DEC:
554 case ACTION_SETTINGS_DECREPEAT:
555 *valptr = clamp_value_wrap(--(*valptr), max, min);
556 say_time(cursorpos, tm);
557 break;
559 case ACTION_STD_OK:
560 done = true;
561 break;
563 case ACTION_STD_CANCEL:
564 done = true;
565 tm->tm_year = -1;
566 break;
568 default:
569 if (default_event_handler(button) == SYS_USB_CONNECTED)
570 return true;
571 break;
574 return false;
576 #endif /* defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0) */
578 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
579 bool shutdown_screen(void)
581 int button;
582 bool done = false;
583 long time_entered = current_tick;
585 lcd_stop_scroll();
587 splash(0, str(LANG_CONFIRM_SHUTDOWN));
589 while(!done && TIME_BEFORE(current_tick,time_entered+HZ*2))
591 button = get_action(CONTEXT_STD,HZ);
592 switch(button)
594 case ACTION_STD_CANCEL:
595 sys_poweroff();
596 break;
598 /* do nothing here, because ACTION_NONE might be caused
599 * by timeout or button release. In case of timeout the loop
600 * is terminated by TIME_BEFORE */
601 case ACTION_NONE:
602 break;
604 default:
605 if(default_event_handler(button) == SYS_USB_CONNECTED)
606 return true;
607 done = true;
608 break;
611 return false;
613 #endif
615 static const int id3_headers[]=
617 LANG_ID3_TITLE,
618 LANG_ID3_ARTIST,
619 LANG_ID3_ALBUM,
620 LANG_ID3_ALBUMARTIST,
621 LANG_ID3_GROUPING,
622 LANG_ID3_DISCNUM,
623 LANG_ID3_TRACKNUM,
624 LANG_ID3_COMMENT,
625 LANG_ID3_GENRE,
626 LANG_ID3_YEAR,
627 LANG_ID3_LENGTH,
628 LANG_ID3_PLAYLIST,
629 LANG_ID3_BITRATE,
630 LANG_ID3_FREQUENCY,
631 #if CONFIG_CODEC == SWCODEC
632 LANG_ID3_TRACK_GAIN,
633 LANG_ID3_ALBUM_GAIN,
634 #endif
635 LANG_ID3_PATH,
638 struct id3view_info {
639 struct mp3entry* id3;
640 int count;
641 int info_id[ARRAYLEN(id3_headers)];
644 static const char* id3_get_info(int selected_item, void* data,
645 char *buffer, size_t buffer_len)
647 struct id3view_info *info = (struct id3view_info*)data;
648 struct mp3entry* id3 =info->id3;
649 int info_no=selected_item/2;
650 if(!(selected_item%2))
651 {/* header */
652 return(str(id3_headers[info->info_id[info_no]]));
654 else
655 {/* data */
657 char * val=NULL;
658 switch(info->info_id[info_no])
660 case 0:/*LANG_ID3_TITLE*/
661 val=id3->title;
662 break;
663 case 1:/*LANG_ID3_ARTIST*/
664 val=id3->artist;
665 break;
666 case 2:/*LANG_ID3_ALBUM*/
667 val=id3->album;
668 break;
669 case 3:/*LANG_ID3_ALBUMARTIST*/
670 val=id3->albumartist;
671 break;
672 case 4:/*LANG_ID3_GROUPING*/
673 val=id3->grouping;
674 break;
675 case 5:/*LANG_ID3_DISCNUM*/
676 if (id3->disc_string)
677 val = id3->disc_string;
678 else if (id3->discnum)
680 snprintf(buffer, buffer_len, "%d", id3->discnum);
681 val = buffer;
683 break;
684 case 6:/*LANG_ID3_TRACKNUM*/
685 if (id3->track_string)
686 val = id3->track_string;
687 else if (id3->tracknum)
689 snprintf(buffer, buffer_len, "%d", id3->tracknum);
690 val = buffer;
692 break;
693 case 7:/*LANG_ID3_COMMENT*/
694 val=id3->comment;
695 break;
696 case 8:/*LANG_ID3_GENRE*/
697 val = id3->genre_string;
698 break;
699 case 9:/*LANG_ID3_YEAR*/
700 if (id3->year_string)
701 val = id3->year_string;
702 else if (id3->year)
704 snprintf(buffer, buffer_len, "%d", id3->year);
705 val = buffer;
707 break;
708 case 10:/*LANG_ID3_LENGTH*/
709 format_time(buffer, buffer_len, id3->length);
710 val=buffer;
711 break;
712 case 11:/*LANG_ID3_PLAYLIST*/
713 snprintf(buffer, buffer_len, "%d/%d",
714 playlist_get_display_index(), playlist_amount());
715 val=buffer;
716 break;
717 case 12:/*LANG_ID3_BITRATE*/
718 snprintf(buffer, buffer_len, "%d kbps%s", id3->bitrate,
719 id3->vbr ? str(LANG_ID3_VBR) : (const unsigned char*) "");
720 val=buffer;
721 break;
722 case 13:/*LANG_ID3_FREQUENCY*/
723 snprintf(buffer, buffer_len, "%ld Hz", id3->frequency);
724 val=buffer;
725 break;
726 #if CONFIG_CODEC == SWCODEC
727 case 14:/*LANG_ID3_TRACK_GAIN*/
728 val=id3->track_gain_string;
729 break;
730 case 15:/*LANG_ID3_ALBUM_GAIN*/
731 val=id3->album_gain_string;
732 break;
733 case 16:/*LANG_ID3_PATH*/
734 #else
735 case 14:/*LANG_ID3_PATH*/
736 #endif
737 val=id3->path;
738 break;
740 return val && *val ? val : NULL;
744 bool browse_id3(void)
746 struct gui_synclist id3_lists;
747 struct mp3entry* id3 = audio_current_track();
748 int key;
749 unsigned int i;
750 struct id3view_info info;
751 info.count = 0;
752 info.id3 = id3;
753 for (i = 0; i < ARRAYLEN(id3_headers); i++)
755 char temp[8];
756 info.info_id[i] = i;
757 if (id3_get_info((i*2)+1, &info, temp, 8) != NULL)
758 info.info_id[info.count++] = i;
761 gui_synclist_init(&id3_lists, &id3_get_info, &info, true, 2, NULL);
762 gui_synclist_set_nb_items(&id3_lists, info.count*2);
763 gui_synclist_draw(&id3_lists);
764 while (true) {
765 key = get_action(CONTEXT_LIST,HZ/2);
766 if(key!=ACTION_NONE && key!=ACTION_UNKNOWN
767 && !gui_synclist_do_button(&id3_lists, &key,LIST_WRAP_UNLESS_HELD))
769 return(default_event_handler(key) == SYS_USB_CONNECTED);
774 static const char* runtime_get_data(int selected_item, void* data,
775 char* buffer, size_t buffer_len)
777 (void)data;
778 int t;
779 switch (selected_item)
781 case 0: return str(LANG_RUNNING_TIME);
782 case 1: t = global_status.runtime; break;
783 case 2: return str(LANG_TOP_TIME);
784 case 3: t = global_status.topruntime; break;
785 default:
786 return "";
789 snprintf(buffer, buffer_len, "%dh %dm %ds",
790 t / 3600, (t % 3600) / 60, t % 60);
791 return buffer;
794 static int runtime_speak_data(int selected_item, void* data)
796 (void) data;
797 talk_ids(false,
798 (selected_item < 2) ? LANG_RUNNING_TIME : LANG_TOP_TIME,
799 TALK_ID((selected_item < 2) ? global_status.runtime
800 : global_status.topruntime, UNIT_TIME));
801 return 0;
805 bool view_runtime(void)
807 static const char *lines[]={ID2P(LANG_CLEAR_TIME)};
808 static const struct text_message message={lines, 1};
810 struct gui_synclist lists;
811 int action;
812 gui_synclist_init(&lists, runtime_get_data, NULL, false, 2, NULL);
813 #if !defined(HAVE_LCD_CHARCELLS)
814 gui_synclist_set_title(&lists, str(LANG_RUNNING_TIME), NOICON);
815 #else
816 gui_synclist_set_title(&lists, NULL, NOICON);
817 #endif
818 if(global_settings.talk_menu)
819 gui_synclist_set_voice_callback(&lists, runtime_speak_data);
820 gui_synclist_set_icon_callback(&lists, NULL);
821 gui_synclist_set_nb_items(&lists, 4);
822 gui_synclist_speak_item(&lists);
823 while(1)
825 #if CONFIG_CHARGING
826 if (charger_inserted())
828 global_status.runtime = 0;
830 else
831 #endif
833 global_status.runtime += ((current_tick - lasttime) / HZ);
835 lasttime = current_tick;
836 gui_synclist_draw(&lists);
837 list_do_action(CONTEXT_STD, HZ,
838 &lists, &action, LIST_WRAP_UNLESS_HELD);
839 if(action == ACTION_STD_CANCEL)
840 break;
841 if(action == ACTION_STD_OK) {
842 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
844 if (!(gui_synclist_get_sel_pos(&lists)/2))
845 global_status.runtime = 0;
846 else
847 global_status.topruntime = 0;
848 gui_synclist_speak_item(&lists);
851 if(default_event_handler(action) == SYS_USB_CONNECTED)
852 return true;
854 return false;
857 #ifdef HAVE_TOUCHSCREEN
858 static int get_sample(struct touchscreen_calibration *cal, int x, int y, int i,
859 struct screen* screen)
861 int action;
862 short ts_x, ts_y;
864 /* Draw a cross */
865 screen->drawline(x - 10, y, x - 2, y);
866 screen->drawline(x + 2, y, x + 10, y);
867 screen->drawline(x, y - 10, x, y - 2);
868 screen->drawline(x, y + 2, x, y + 10);
869 screen->update();
871 /* Wait for a touchscreen press */
872 while(true)
874 action = get_action(CONTEXT_STD, TIMEOUT_BLOCK);
875 if(action == ACTION_TOUCHSCREEN)
877 if(action_get_touchscreen_press(&ts_x, &ts_y) == BUTTON_REL)
878 break;
880 else if(action == ACTION_STD_CANCEL)
881 return -1;
884 cal->x[i][0] = ts_x;
885 cal->y[i][0] = ts_y;
886 cal->x[i][1] = x;
887 cal->y[i][1] = y;
889 return 0;
893 int calibrate(void)
895 short points[3][2] = {
896 {LCD_WIDTH/10, LCD_HEIGHT/10},
897 {7*LCD_WIDTH/8, LCD_HEIGHT/2},
898 {LCD_WIDTH/2, 7*LCD_HEIGHT/8}
900 struct screen* screen = &screens[SCREEN_MAIN];
901 enum touchscreen_mode old_mode = touchscreen_get_mode();
902 struct touchscreen_calibration cal;
903 int i, ret = 0;
904 int statusbar = global_settings.statusbar; /* hide the statusbar */
905 global_settings.statusbar = STATUSBAR_OFF;
907 touchscreen_disable_mapping(); /* set raw mode */
908 touchscreen_set_mode(TOUCHSCREEN_POINT);
909 for(i=0; i<3; i++)
911 screen->clear_display();
913 if(get_sample(&cal, points[i][0], points[i][1], i, screen))
915 ret = -1;
916 break;
920 if(ret == 0)
921 touchscreen_calibrate(&cal);
922 else
923 touchscreen_reset_mapping();
924 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
925 touchscreen_set_mode(old_mode);
926 global_settings.statusbar = statusbar;
928 return ret;
931 int reset_mapping(void)
933 touchscreen_reset_mapping();
934 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
935 return 0;
937 #endif