Clear screen when leave usb screen so that usblogo doesn't remain on the screen.
[kugel-rb.git] / apps / screens.c
blob036e2a8f06de4875b4c0b0a1062e9382dd5dda82
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 #ifdef HAVE_REMOTE_LCD
29 #include "lcd-remote.h"
30 #endif
31 #include "lang.h"
32 #include "icons.h"
33 #include "font.h"
34 #include "audio.h"
35 #include "mp3_playback.h"
36 #include "usb.h"
37 #if defined(HAVE_USBSTACK)
38 #include "usb_core.h"
39 #ifdef USB_ENABLE_HID
40 #include "usbstack/usb_hid.h"
41 #endif
42 #endif
43 #include "settings.h"
44 #include "status.h"
45 #include "playlist.h"
46 #include "sprintf.h"
47 #include "kernel.h"
48 #include "power.h"
49 #include "system.h"
50 #include "powermgmt.h"
51 #include "talk.h"
52 #include "misc.h"
53 #include "metadata.h"
54 #include "screens.h"
55 #include "debug.h"
56 #include "led.h"
57 #include "sound.h"
58 #include "splash.h"
59 #include "statusbar.h"
60 #include "screen_access.h"
61 #include "pcmbuf.h"
62 #include "list.h"
63 #include "yesno.h"
64 #include "backdrop.h"
65 #include "viewport.h"
67 #ifdef HAVE_LCD_BITMAP
68 #include "bitmaps/usblogo.h"
69 #endif
71 #ifdef HAVE_REMOTE_LCD
72 #include "bitmaps/remote_usblogo.h"
73 #endif
75 #if (CONFIG_STORAGE & STORAGE_MMC)
76 #include "ata_mmc.h"
77 #endif
78 #if CONFIG_CODEC == SWCODEC
79 #include "dsp.h"
80 #endif
82 /* only used in set_time screen */
83 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
84 static int clamp_value_wrap(int value, int max, int min)
86 if (value > max)
87 return min;
88 if (value < min)
89 return max;
90 return value;
92 #endif
94 #ifndef SIMULATOR
95 static int handle_usb_events(void)
97 int next_update=0;
98 #ifdef HAVE_TOUCHSCREEN
99 enum touchscreen_mode old_mode = touchscreen_get_mode();
101 /* TODO: Paint buttons on screens OR switch to point mode and use
102 * touchscreen as a touchpad to move the host's mouse cursor */
103 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
104 #endif
106 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
107 while(1)
109 int button;
110 #if defined(HAVE_USBSTACK) && defined(USB_ENABLE_HID)
111 bool hid_enabled = usb_core_driver_enabled(USB_DRIVER_HID);
113 if (hid_enabled)
115 int id = HID_CONSUMER_USAGE_UNASSIGNED;
116 button = get_action(CONTEXT_USB_HID, HZ/4);
118 switch (button)
120 case ACTION_USB_HID_PLAY:
121 id = HID_CONSUMER_USAGE_PLAY_PAUSE;
122 break;
123 case ACTION_USB_HID_STOP:
124 id = HID_CONSUMER_USAGE_STOP;
125 break;
126 case ACTION_USB_HID_SKIPPREV:
127 id = HID_CONSUMER_USAGE_SCAN_PREVIOUS_TRACK;
128 break;
129 case ACTION_USB_HID_SKIPNEXT:
130 id = HID_CONSUMER_USAGE_SCAN_NEXT_TRACK;
131 break;
132 case ACTION_USB_HID_VOLDOWN:
133 id = HID_CONSUMER_USAGE_VOLUME_DECREMENT;
134 break;
135 case ACTION_USB_HID_VOLUP:
136 id = HID_CONSUMER_USAGE_VOLUME_INCREMENT;
137 break;
138 case ACTION_USB_HID_MUTE:
139 id = HID_CONSUMER_USAGE_MUTE;
140 break;
143 if (id != HID_CONSUMER_USAGE_UNASSIGNED)
144 usb_hid_send(HID_USAGE_PAGE_CONSUMER, id);
146 else
147 #endif
148 button = button_get_w_tmo(HZ/4);
150 switch(button)
152 case SYS_USB_DISCONNECTED:
153 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
154 goto Exit;
155 case SYS_TIMEOUT:
156 break;
159 if(TIME_AFTER(current_tick,next_update))
161 if(usb_inserted()) {
162 #if (CONFIG_STORAGE & STORAGE_MMC) /* USB-MMC bridge can report activity */
163 led(mmc_usb_active(HZ));
164 #endif /* STORAGE_MMC */
165 gui_syncstatusbar_draw(&statusbars, false);
167 next_update=current_tick+HZ/2;
170 Exit:
171 #ifdef HAVE_TOUCHSCREEN
172 touchscreen_set_mode(old_mode);
173 #endif
174 return 0;
176 #endif
178 void usb_screen(void)
180 #ifdef USB_NONE
181 /* nothing here! */
182 #else
183 int i;
184 int statusbar = global_settings.statusbar; /* force the statusbar */
185 if(!global_settings.statusbar)
186 global_settings.statusbar = STATUSBAR_TOP;
188 FOR_NB_SCREENS(i)
190 screens[i].backdrop_show(BACKDROP_MAIN);
191 screens[i].backlight_on();
192 screens[i].clear_display();
193 #if NB_SCREENS > 1
194 if (i == SCREEN_REMOTE)
196 screens[i].bitmap(remote_usblogo,
197 (LCD_REMOTE_WIDTH-BMPWIDTH_remote_usblogo),
198 (LCD_REMOTE_HEIGHT-BMPHEIGHT_remote_usblogo)/2,
199 BMPWIDTH_remote_usblogo, BMPHEIGHT_remote_usblogo);
201 else
203 #endif
204 #ifdef HAVE_LCD_BITMAP
205 screens[i].transparent_bitmap(usblogo,
206 (LCD_WIDTH-BMPWIDTH_usblogo),
207 (LCD_HEIGHT-BMPHEIGHT_usblogo)/2,
208 BMPWIDTH_usblogo, BMPHEIGHT_usblogo);
209 #else
210 screens[i].double_height(false);
211 screens[i].puts_scroll(0, 0, "[USB Mode]");
212 status_set_param(false);
213 status_set_audio(false);
214 status_set_usb(true);
215 #endif /* HAVE_LCD_BITMAP */
216 #if NB_SCREENS > 1
218 #endif
219 screens[i].update();
222 #ifdef SIMULATOR
223 while (button_get(true) & BUTTON_REL);
224 #else
225 usb_acknowledge(SYS_USB_CONNECTED_ACK);
226 while (handle_usb_events());
227 #endif /* SIMULATOR */
228 #ifdef HAVE_LCD_CHARCELLS
229 status_set_usb(false);
230 #endif /* HAVE_LCD_CHARCELLS */
231 FOR_NB_SCREENS(i)
233 screens[i].backlight_on();
234 screens[i].clear_display();
235 screens[i].update();
237 global_settings.statusbar = statusbar;
238 #endif /* USB_NONE */
241 #if (CONFIG_STORAGE & STORAGE_MMC)
242 int mmc_remove_request(void)
244 struct queue_event ev;
245 int i;
246 FOR_NB_SCREENS(i)
247 screens[i].clear_display();
248 splash(0, ID2P(LANG_REMOVE_MMC));
250 while (1)
252 queue_wait_w_tmo(&button_queue, &ev, HZ/2);
253 switch (ev.id)
255 case SYS_HOTSWAP_EXTRACTED:
256 return SYS_HOTSWAP_EXTRACTED;
258 case SYS_USB_DISCONNECTED:
259 return SYS_USB_DISCONNECTED;
263 #endif
265 /* the charging screen is only used for archos targets */
266 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING) && defined(CPU_SH)
268 #ifdef HAVE_LCD_BITMAP
269 static void charging_display_info(bool animate)
271 unsigned char charging_logo[36];
272 const int pox_x = (LCD_WIDTH - sizeof(charging_logo)) / 2;
273 const int pox_y = 32;
274 static unsigned phase = 3;
275 unsigned i;
276 char buf[32];
277 (void)buf;
279 #ifdef NEED_ATA_POWER_BATT_MEASURE
280 if (ide_powered()) /* FM and V2 can only measure when ATA power is on */
281 #endif
283 int battv = battery_voltage();
284 snprintf(buf, 32, " Batt: %d.%02dV %d%% ", battv / 1000,
285 (battv % 1000) / 10, battery_level());
286 lcd_puts(0, 7, buf);
289 #ifdef ARCHOS_RECORER
290 snprintf(buf, 32, "Charge mode:");
291 lcd_puts(0, 2, buf);
293 if (charge_state == CHARGING)
294 snprintf(buf, 32, str(LANG_BATTERY_CHARGE));
295 else if (charge_state == TOPOFF)
296 snprintf(buf, 32, str(LANG_BATTERY_TOPOFF_CHARGE));
297 else if (charge_state == TRICKLE)
298 snprintf(buf, 32, str(LANG_BATTERY_TRICKLE_CHARGE));
299 else
300 snprintf(buf, 32, "not charging");
302 lcd_puts(0, 3, buf);
303 if (!charger_enabled())
304 animate = false;
305 #endif /* ARCHOS_RECORER */
307 /* middle part */
308 memset(charging_logo+3, 0x00, 32);
309 charging_logo[0] = 0x3C;
310 charging_logo[1] = 0x24;
311 charging_logo[2] = charging_logo[35] = 0xFF;
313 if (!animate)
314 { /* draw the outline */
315 /* middle part */
316 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
317 sizeof(charging_logo), 8);
318 lcd_set_drawmode(DRMODE_FG);
319 /* upper line */
320 charging_logo[0] = charging_logo[1] = 0x00;
321 memset(charging_logo+2, 0x80, 34);
322 lcd_mono_bitmap(charging_logo, pox_x, pox_y, sizeof(charging_logo), 8);
323 /* lower line */
324 memset(charging_logo+2, 0x01, 34);
325 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 16,
326 sizeof(charging_logo), 8);
327 lcd_set_drawmode(DRMODE_SOLID);
329 else
330 { /* animate the middle part */
331 for (i = 3; i<MIN(sizeof(charging_logo)-1, phase); i++)
333 if ((i-phase) % 8 == 0)
334 { /* draw a "bubble" here */
335 unsigned bitpos;
336 bitpos = (phase + i/8) % 15; /* "bounce" effect */
337 if (bitpos > 7)
338 bitpos = 14 - bitpos;
339 charging_logo[i] = BIT_N(bitpos);
342 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
343 sizeof(charging_logo), 8);
344 phase++;
346 lcd_update();
348 #else /* not HAVE_LCD_BITMAP */
350 static unsigned long logo_chars[4];
351 static const unsigned char logo_pattern[] = {
352 0x07, 0x04, 0x1c, 0x14, 0x1c, 0x04, 0x07, 0, /* char 1 */
353 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 2 */
354 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 3 */
355 0x1f, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1f, 0, /* char 4 */
358 static void logo_lock_patterns(bool on)
360 int i;
362 if (on)
364 for (i = 0; i < 4; i++)
365 logo_chars[i] = lcd_get_locked_pattern();
367 else
369 for (i = 0; i < 4; i++)
370 lcd_unlock_pattern(logo_chars[i]);
374 static void charging_display_info(bool animate)
376 int battv;
377 unsigned i, ypos;
378 static unsigned phase = 3;
379 char buf[32];
381 battv = battery_voltage();
382 snprintf(buf, sizeof(buf), " %d.%02dV", battv / 1000, (battv % 1000) / 10);
383 lcd_puts(4, 1, buf);
385 memcpy(buf, logo_pattern, 32); /* copy logo patterns */
387 if (!animate) /* build the screen */
389 lcd_double_height(false);
390 lcd_puts(0, 0, "[Charging]");
391 for (i = 0; i < 4; i++)
392 lcd_putc(i, 1, logo_chars[i]);
394 else /* animate the logo */
396 for (i = 3; i < MIN(19, phase); i++)
398 if ((i - phase) % 5 == 0)
399 { /* draw a "bubble" here */
400 ypos = (phase + i/5) % 9; /* "bounce" effect */
401 if (ypos > 4)
402 ypos = 8 - ypos;
403 buf[5 - ypos + 8 * (i/5)] |= 0x10u >> (i%5);
406 phase++;
409 for (i = 0; i < 4; i++)
410 lcd_define_pattern(logo_chars[i], buf + 8 * i);
412 lcd_update();
414 #endif /* (not) HAVE_LCD_BITMAP */
416 /* blocks while charging, returns on event:
417 1 if charger cable was removed
418 2 if Off/Stop key was pressed
419 3 if On key was pressed
420 4 if USB was connected */
422 int charging_screen(void)
424 unsigned int button;
425 int rc = 0;
427 ide_power_enable(false); /* power down the disk, else would be spinning */
429 lcd_clear_display();
430 backlight_set_timeout(global_settings.backlight_timeout);
431 #ifdef HAVE_REMOTE_LCD
432 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
433 #endif
434 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
436 #ifdef HAVE_LCD_CHARCELLS
437 logo_lock_patterns(true);
438 #endif
439 charging_display_info(false);
443 gui_syncstatusbar_draw(&statusbars, false);
444 charging_display_info(true);
445 button = get_action(CONTEXT_STD,HZ/3);
446 if (button == ACTION_STD_OK)
447 rc = 2;
448 else if (usb_detect() == USB_INSERTED)
449 rc = 3;
450 else if (!charger_inserted())
451 rc = 1;
452 } while (!rc);
454 #ifdef HAVE_LCD_CHARCELLS
455 logo_lock_patterns(false);
456 #endif
457 return rc;
459 #endif /* CONFIG_CHARGING && !HAVE_POWEROFF_WHILE_CHARGING && defined(CPU_SH) */
461 #if CONFIG_CHARGING
462 void charging_splash(void)
464 splash(2*HZ, str(LANG_BATTERY_CHARGE));
465 button_clear_queue();
467 #endif
470 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
472 /* little helper function for voice output */
473 static void say_time(int cursorpos, const struct tm *tm)
475 int value = 0;
476 int unit = 0;
478 if (!global_settings.talk_menu)
479 return;
481 switch(cursorpos)
483 case 0:
484 value = tm->tm_hour;
485 unit = UNIT_HOUR;
486 break;
487 case 1:
488 value = tm->tm_min;
489 unit = UNIT_MIN;
490 break;
491 case 2:
492 value = tm->tm_sec;
493 unit = UNIT_SEC;
494 break;
495 case 3:
496 value = tm->tm_year + 1900;
497 break;
498 case 5:
499 value = tm->tm_mday;
500 break;
503 if (cursorpos == 4) /* month */
504 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, false);
505 else
506 talk_value(value, unit, false);
510 #define INDEX_X 0
511 #define INDEX_Y 1
513 #define SEPARATOR ":"
514 bool set_time_screen(const char* title, struct tm *tm)
516 bool done = false;
517 int button;
518 unsigned int i, j, s;
519 int cursorpos = 0;
520 unsigned int realyear;
521 unsigned int width;
522 unsigned int min, max;
523 unsigned int statusbar_height = 0;
524 unsigned int separator_width, weekday_width;
525 unsigned int prev_line_height;
526 static unsigned char daysinmonth[] =
527 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
528 unsigned char buffer[20];
529 struct viewport vp[NB_SCREENS];
530 int nb_lines;
532 /* 6 possible cursor possitions, 2 values stored for each: x, y */
533 unsigned int cursor[6][2];
535 int *valptr = NULL;
536 unsigned char *ptr[6];
538 if(global_settings.statusbar)
539 statusbar_height = STATUSBAR_HEIGHT;
541 /* speak selection when screen is entered */
542 say_time(cursorpos, tm);
544 while (!done) {
545 /* for easy acess in the drawing loop */
546 ptr[0] = buffer; /* hours */
547 ptr[1] = buffer + 3; /* minutes */
548 ptr[2] = buffer + 6; /* seconds */
549 ptr[3] = buffer + 9; /* year */
550 ptr[4] = str(LANG_MONTH_JANUARY + tm->tm_mon); /* monthname */
551 ptr[5] = buffer + 14; /* day of month */
553 /* calculate the number of days in febuary */
554 realyear = tm->tm_year + 1900;
555 if((realyear % 4 == 0 && !(realyear % 100 == 0)) || realyear % 400 == 0)
556 daysinmonth[1] = 29;
557 else
558 daysinmonth[1] = 28;
560 /* fix day if month or year changed */
561 if (tm->tm_mday > daysinmonth[tm->tm_mon])
562 tm->tm_mday = daysinmonth[tm->tm_mon];
564 /* calculate day of week */
565 set_day_of_week(tm);
567 /* put all the numbers we want from the tm struct into
568 an easily printable buffer */
569 snprintf(buffer, sizeof(buffer),
570 "%02d " "%02d " "%02d " "%04d " "%02d",
571 tm->tm_hour, tm->tm_min, tm->tm_sec,
572 tm->tm_year+1900, tm->tm_mday);
574 /* convert spaces in the buffer to '\0' to make it possible to work
575 directly on the buffer */
576 for(i=0; i < sizeof(buffer); i++)
578 if(buffer[i] == ' ')
579 buffer[i] = '\0';
582 FOR_NB_SCREENS(s)
584 viewport_set_defaults(&vp[s], s);
585 screens[s].set_viewport(&vp[s]);
586 nb_lines = viewport_get_nb_lines(&vp[s]);
588 /* minimum lines needed is 2 + title line */
589 if (nb_lines < 4)
591 vp[s].font = FONT_SYSFIXED;
592 nb_lines = viewport_get_nb_lines(&vp[s]);
595 /* recalculate the positions and offsets */
596 if (nb_lines >= 3)
597 screens[s].getstringsize(title, NULL, &prev_line_height);
598 else
599 prev_line_height = 0;
601 screens[s].getstringsize(SEPARATOR, &separator_width, NULL);
603 /* weekday */
604 screens[s].getstringsize(str(LANG_WEEKDAY_SUNDAY + tm->tm_wday),
605 &weekday_width, NULL);
606 screens[s].getstringsize(" ", &separator_width, NULL);
608 for(i=0, j=0; i < 6; i++)
610 if(i==3) /* second row */
612 j = weekday_width + separator_width;
613 prev_line_height *= 2;
615 screens[s].getstringsize(ptr[i], &width, NULL);
616 cursor[i][INDEX_Y] = prev_line_height + statusbar_height;
617 cursor[i][INDEX_X] = j;
618 j += width + separator_width;
621 /* draw the screen */
622 screens[s].set_viewport(&vp[s]);
623 screens[s].clear_viewport();
624 /* display the screen title */
625 screens[s].puts_scroll(0, 0, title);
627 /* these are not selectable, so we draw them outside the loop */
628 /* name of the week day */
629 screens[s].putsxy(0, cursor[3][INDEX_Y],
630 str(LANG_WEEKDAY_SUNDAY + tm->tm_wday));
632 /* draw the selected item with drawmode set to
633 DRMODE_SOLID|DRMODE_INVERSEVID, all other selectable
634 items with drawmode DRMODE_SOLID */
635 for(i=0; i<6; i++)
637 if (cursorpos == (int)i)
638 vp[s].drawmode = (DRMODE_SOLID|DRMODE_INVERSEVID);
640 screens[s].putsxy(cursor[i][INDEX_X],
641 cursor[i][INDEX_Y], ptr[i]);
643 vp[s].drawmode = DRMODE_SOLID;
645 screens[s].putsxy(cursor[i/4 +1][INDEX_X] - separator_width,
646 cursor[0][INDEX_Y], SEPARATOR);
649 /* print help text */
650 if (nb_lines > 4)
651 screens[s].puts(0, 4, str(LANG_TIME_SET_BUTTON));
652 if (nb_lines > 5)
653 screens[s].puts(0, 5, str(LANG_TIME_REVERT));
654 screens[s].update_viewport();
655 screens[s].set_viewport(NULL);
658 /* set the most common numbers */
659 min = 0;
660 max = 59;
661 /* calculate the minimum and maximum for the number under cursor */
662 switch(cursorpos) {
663 case 0: /* hour */
664 max = 23;
665 valptr = &tm->tm_hour;
666 break;
667 case 1: /* minute */
668 valptr = &tm->tm_min;
669 break;
670 case 2: /* second */
671 valptr = &tm->tm_sec;
672 break;
673 case 3: /* year */
674 min = 1;
675 max = 200;
676 valptr = &tm->tm_year;
677 break;
678 case 4: /* month */
679 max = 11;
680 valptr = &tm->tm_mon;
681 break;
682 case 5: /* day */
683 min = 1;
684 max = daysinmonth[tm->tm_mon];
685 valptr = &tm->tm_mday;
686 break;
689 #ifdef HAVE_TOUCHSCREEN
690 enum touchscreen_mode old_mode = touchscreen_get_mode();
692 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
693 #endif
694 button = get_action(CONTEXT_SETTINGS_TIME, TIMEOUT_BLOCK);
695 #ifdef HAVE_TOUCHSCREEN
696 touchscreen_set_mode(old_mode);
697 #endif
698 switch ( button ) {
699 case ACTION_STD_PREV:
700 cursorpos = clamp_value_wrap(--cursorpos, 5, 0);
701 say_time(cursorpos, tm);
702 break;
703 case ACTION_STD_NEXT:
704 cursorpos = clamp_value_wrap(++cursorpos, 5, 0);
705 say_time(cursorpos, tm);
706 break;
707 case ACTION_SETTINGS_INC:
708 case ACTION_SETTINGS_INCREPEAT:
709 *valptr = clamp_value_wrap(++(*valptr), max, min);
710 say_time(cursorpos, tm);
711 break;
712 case ACTION_SETTINGS_DEC:
713 case ACTION_SETTINGS_DECREPEAT:
714 *valptr = clamp_value_wrap(--(*valptr), max, min);
715 say_time(cursorpos, tm);
716 break;
718 case ACTION_STD_OK:
719 done = true;
720 break;
722 case ACTION_STD_CANCEL:
723 done = true;
724 tm->tm_year = -1;
725 break;
727 default:
728 if (default_event_handler(button) == SYS_USB_CONNECTED)
729 return true;
730 break;
733 return false;
735 #endif /* defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0) */
737 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
738 bool shutdown_screen(void)
740 int button;
741 bool done = false;
742 long time_entered = current_tick;
744 lcd_stop_scroll();
746 splash(0, str(LANG_CONFIRM_SHUTDOWN));
748 while(!done && TIME_BEFORE(current_tick,time_entered+HZ*2))
750 button = get_action(CONTEXT_STD,HZ);
751 switch(button)
753 case ACTION_STD_CANCEL:
754 sys_poweroff();
755 break;
757 /* do nothing here, because ACTION_NONE might be caused
758 * by timeout or button release. In case of timeout the loop
759 * is terminated by TIME_BEFORE */
760 case ACTION_NONE:
761 break;
763 default:
764 if(default_event_handler(button) == SYS_USB_CONNECTED)
765 return true;
766 done = true;
767 break;
770 return false;
772 #endif
774 static const int id3_headers[]=
776 LANG_ID3_TITLE,
777 LANG_ID3_ARTIST,
778 LANG_ID3_ALBUM,
779 LANG_ID3_ALBUMARTIST,
780 LANG_ID3_GROUPING,
781 LANG_ID3_DISCNUM,
782 LANG_ID3_TRACKNUM,
783 LANG_ID3_COMMENT,
784 LANG_ID3_GENRE,
785 LANG_ID3_YEAR,
786 LANG_ID3_LENGTH,
787 LANG_ID3_PLAYLIST,
788 LANG_ID3_BITRATE,
789 LANG_ID3_FREQUENCY,
790 #if CONFIG_CODEC == SWCODEC
791 LANG_ID3_TRACK_GAIN,
792 LANG_ID3_ALBUM_GAIN,
793 #endif
794 LANG_ID3_PATH,
797 struct id3view_info {
798 struct mp3entry* id3;
799 int count;
800 int info_id[ARRAYLEN(id3_headers)];
803 static const char* id3_get_info(int selected_item, void* data,
804 char *buffer, size_t buffer_len)
806 struct id3view_info *info = (struct id3view_info*)data;
807 struct mp3entry* id3 =info->id3;
808 int info_no=selected_item/2;
809 if(!(selected_item%2))
810 {/* header */
811 return(str(id3_headers[info->info_id[info_no]]));
813 else
814 {/* data */
816 char * val=NULL;
817 switch(info->info_id[info_no])
819 case 0:/*LANG_ID3_TITLE*/
820 val=id3->title;
821 break;
822 case 1:/*LANG_ID3_ARTIST*/
823 val=id3->artist;
824 break;
825 case 2:/*LANG_ID3_ALBUM*/
826 val=id3->album;
827 break;
828 case 3:/*LANG_ID3_ALBUMARTIST*/
829 val=id3->albumartist;
830 break;
831 case 4:/*LANG_ID3_GROUPING*/
832 val=id3->grouping;
833 break;
834 case 5:/*LANG_ID3_DISCNUM*/
835 if (id3->disc_string)
836 val = id3->disc_string;
837 else if (id3->discnum)
839 snprintf(buffer, buffer_len, "%d", id3->discnum);
840 val = buffer;
842 break;
843 case 6:/*LANG_ID3_TRACKNUM*/
844 if (id3->track_string)
845 val = id3->track_string;
846 else if (id3->tracknum)
848 snprintf(buffer, buffer_len, "%d", id3->tracknum);
849 val = buffer;
851 break;
852 case 7:/*LANG_ID3_COMMENT*/
853 val=id3->comment;
854 break;
855 case 8:/*LANG_ID3_GENRE*/
856 val = id3->genre_string;
857 break;
858 case 9:/*LANG_ID3_YEAR*/
859 if (id3->year_string)
860 val = id3->year_string;
861 else if (id3->year)
863 snprintf(buffer, buffer_len, "%d", id3->year);
864 val = buffer;
866 break;
867 case 10:/*LANG_ID3_LENGTH*/
868 format_time(buffer, buffer_len, id3->length);
869 val=buffer;
870 break;
871 case 11:/*LANG_ID3_PLAYLIST*/
872 snprintf(buffer, buffer_len, "%d/%d",
873 playlist_get_display_index(), playlist_amount());
874 val=buffer;
875 break;
876 case 12:/*LANG_ID3_BITRATE*/
877 snprintf(buffer, buffer_len, "%d kbps%s", id3->bitrate,
878 id3->vbr ? str(LANG_ID3_VBR) : (const unsigned char*) "");
879 val=buffer;
880 break;
881 case 13:/*LANG_ID3_FREQUENCY*/
882 snprintf(buffer, buffer_len, "%ld Hz", id3->frequency);
883 val=buffer;
884 break;
885 #if CONFIG_CODEC == SWCODEC
886 case 14:/*LANG_ID3_TRACK_GAIN*/
887 val=id3->track_gain_string;
888 break;
889 case 15:/*LANG_ID3_ALBUM_GAIN*/
890 val=id3->album_gain_string;
891 break;
892 case 16:/*LANG_ID3_PATH*/
893 #else
894 case 14:/*LANG_ID3_PATH*/
895 #endif
896 val=id3->path;
897 break;
899 return val && *val ? val : NULL;
903 bool browse_id3(void)
905 struct gui_synclist id3_lists;
906 struct mp3entry* id3 = audio_current_track();
907 int key;
908 unsigned int i;
909 struct id3view_info info;
910 info.count = 0;
911 info.id3 = id3;
912 for (i = 0; i < ARRAYLEN(id3_headers); i++)
914 char temp[8];
915 info.info_id[i] = i;
916 if (id3_get_info((i*2)+1, &info, temp, 8) != NULL)
917 info.info_id[info.count++] = i;
920 gui_synclist_init(&id3_lists, &id3_get_info, &info, true, 2, NULL);
921 gui_synclist_set_nb_items(&id3_lists, info.count*2);
922 gui_synclist_draw(&id3_lists);
923 while (true) {
924 key = get_action(CONTEXT_LIST,HZ/2);
925 if(key!=ACTION_NONE && key!=ACTION_UNKNOWN
926 && !gui_synclist_do_button(&id3_lists, &key,LIST_WRAP_UNLESS_HELD))
928 return(default_event_handler(key) == SYS_USB_CONNECTED);
933 static const char* runtime_get_data(int selected_item, void* data,
934 char* buffer, size_t buffer_len)
936 (void)data;
937 int t;
938 switch (selected_item)
940 case 0: return str(LANG_RUNNING_TIME);
941 case 1: t = global_status.runtime; break;
942 case 2: return str(LANG_TOP_TIME);
943 case 3: t = global_status.topruntime; break;
944 default:
945 return "";
948 snprintf(buffer, buffer_len, "%dh %dm %ds",
949 t / 3600, (t % 3600) / 60, t % 60);
950 return buffer;
953 static int runtime_speak_data(int selected_item, void* data)
955 (void) data;
956 talk_ids(false,
957 (selected_item < 2) ? LANG_RUNNING_TIME : LANG_TOP_TIME,
958 TALK_ID((selected_item < 2) ? global_status.runtime
959 : global_status.topruntime, UNIT_TIME));
960 return 0;
964 bool view_runtime(void)
966 static const char *lines[]={ID2P(LANG_CLEAR_TIME)};
967 static const struct text_message message={lines, 1};
969 struct gui_synclist lists;
970 int action;
971 gui_synclist_init(&lists, runtime_get_data, NULL, false, 2, NULL);
972 #if !defined(HAVE_LCD_CHARCELLS)
973 gui_synclist_set_title(&lists, str(LANG_RUNNING_TIME), NOICON);
974 #else
975 gui_synclist_set_title(&lists, NULL, NOICON);
976 #endif
977 if(global_settings.talk_menu)
978 gui_synclist_set_voice_callback(&lists, runtime_speak_data);
979 gui_synclist_set_icon_callback(&lists, NULL);
980 gui_synclist_set_nb_items(&lists, 4);
981 gui_synclist_speak_item(&lists);
982 while(1)
984 #if CONFIG_CHARGING
985 if (charger_inserted())
987 global_status.runtime = 0;
989 else
990 #endif
992 global_status.runtime += ((current_tick - lasttime) / HZ);
994 lasttime = current_tick;
995 gui_synclist_draw(&lists);
996 list_do_action(CONTEXT_STD, HZ,
997 &lists, &action, LIST_WRAP_UNLESS_HELD);
998 if(action == ACTION_STD_CANCEL)
999 break;
1000 if(action == ACTION_STD_OK) {
1001 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
1003 if (!(gui_synclist_get_sel_pos(&lists)/2))
1004 global_status.runtime = 0;
1005 else
1006 global_status.topruntime = 0;
1007 gui_synclist_speak_item(&lists);
1010 if(default_event_handler(action) == SYS_USB_CONNECTED)
1011 return true;
1013 return false;
1016 #ifdef HAVE_TOUCHSCREEN
1017 static int get_sample(struct touchscreen_calibration *cal, int x, int y, int i,
1018 struct screen* screen)
1020 int action;
1021 short ts_x, ts_y;
1023 /* Draw a cross */
1024 screen->drawline(x - 10, y, x - 2, y);
1025 screen->drawline(x + 2, y, x + 10, y);
1026 screen->drawline(x, y - 10, x, y - 2);
1027 screen->drawline(x, y + 2, x, y + 10);
1028 screen->update();
1030 /* Wait for a touchscreen press */
1031 while(true)
1033 action = get_action(CONTEXT_STD, TIMEOUT_BLOCK);
1034 if(action == ACTION_TOUCHSCREEN)
1036 if(action_get_touchscreen_press(&ts_x, &ts_y) == BUTTON_REL)
1037 break;
1039 else if(action == ACTION_STD_CANCEL)
1040 return -1;
1043 cal->x[i][0] = ts_x;
1044 cal->y[i][0] = ts_y;
1045 cal->x[i][1] = x;
1046 cal->y[i][1] = y;
1048 return 0;
1052 int calibrate(void)
1054 short points[3][2] = {
1055 {LCD_WIDTH/10, LCD_HEIGHT/10},
1056 {7*LCD_WIDTH/8, LCD_HEIGHT/2},
1057 {LCD_WIDTH/2, 7*LCD_HEIGHT/8}
1059 struct screen* screen = &screens[SCREEN_MAIN];
1060 enum touchscreen_mode old_mode = touchscreen_get_mode();
1061 struct touchscreen_calibration cal;
1062 int i, ret = 0;
1063 int statusbar = global_settings.statusbar; /* hide the statusbar */
1064 global_settings.statusbar = STATUSBAR_OFF;
1066 touchscreen_disable_mapping(); /* set raw mode */
1067 touchscreen_set_mode(TOUCHSCREEN_POINT);
1068 for(i=0; i<3; i++)
1070 screen->clear_display();
1072 if(get_sample(&cal, points[i][0], points[i][1], i, screen))
1074 ret = -1;
1075 break;
1079 if(ret == 0)
1080 touchscreen_calibrate(&cal);
1081 else
1082 touchscreen_reset_mapping();
1083 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1084 touchscreen_set_mode(old_mode);
1085 global_settings.statusbar = statusbar;
1087 return ret;
1090 int reset_mapping(void)
1092 touchscreen_reset_mapping();
1093 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1094 return 0;
1096 #endif