Move a line up into the existing FOR_NB_SCREENS loop and add a comment.
[kugel-rb.git] / apps / screens.c
blob537ec9e21fafce79871b26bd70dee45548bf6570
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"
66 #include "appevents.h"
67 #include "statusbar.h"
69 #ifdef HAVE_LCD_BITMAP
70 #include "bitmaps/usblogo.h"
71 #endif
73 #ifdef HAVE_REMOTE_LCD
74 #include "bitmaps/remote_usblogo.h"
75 #endif
77 #if (CONFIG_STORAGE & STORAGE_MMC)
78 #include "ata_mmc.h"
79 #endif
80 #if CONFIG_CODEC == SWCODEC
81 #include "dsp.h"
82 #endif
84 /* only used in set_time screen */
85 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
86 static int clamp_value_wrap(int value, int max, int min)
88 if (value > max)
89 return min;
90 if (value < min)
91 return max;
92 return value;
94 #endif
96 #ifndef SIMULATOR
97 static int handle_usb_events(void)
99 int next_update=0;
100 #ifdef HAVE_TOUCHSCREEN
101 enum touchscreen_mode old_mode = touchscreen_get_mode();
103 /* TODO: Paint buttons on screens OR switch to point mode and use
104 * touchscreen as a touchpad to move the host's mouse cursor */
105 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
106 #endif
108 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
109 while(1)
111 int button;
112 #if defined(HAVE_USBSTACK) && defined(USB_ENABLE_HID)
113 bool hid_enabled = usb_core_driver_enabled(USB_DRIVER_HID);
115 if (hid_enabled)
117 int id = HID_CONSUMER_USAGE_UNASSIGNED;
118 button = get_action(CONTEXT_USB_HID, HZ/4);
120 switch (button)
122 case ACTION_USB_HID_PLAY:
123 id = HID_CONSUMER_USAGE_PLAY_PAUSE;
124 break;
125 case ACTION_USB_HID_STOP:
126 id = HID_CONSUMER_USAGE_STOP;
127 break;
128 case ACTION_USB_HID_SKIPPREV:
129 id = HID_CONSUMER_USAGE_SCAN_PREVIOUS_TRACK;
130 break;
131 case ACTION_USB_HID_SKIPNEXT:
132 id = HID_CONSUMER_USAGE_SCAN_NEXT_TRACK;
133 break;
134 case ACTION_USB_HID_VOLDOWN:
135 id = HID_CONSUMER_USAGE_VOLUME_DECREMENT;
136 break;
137 case ACTION_USB_HID_VOLUP:
138 id = HID_CONSUMER_USAGE_VOLUME_INCREMENT;
139 break;
140 case ACTION_USB_HID_MUTE:
141 id = HID_CONSUMER_USAGE_MUTE;
142 break;
145 if (id != HID_CONSUMER_USAGE_UNASSIGNED)
146 usb_hid_send(HID_USAGE_PAGE_CONSUMER, id);
148 else
149 #endif
150 button = button_get_w_tmo(HZ/4);
152 switch(button)
154 case SYS_USB_DISCONNECTED:
155 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
156 goto Exit;
157 case SYS_TIMEOUT:
158 break;
161 if(TIME_AFTER(current_tick,next_update))
163 if(usb_inserted()) {
164 #if (CONFIG_STORAGE & STORAGE_MMC) /* USB-MMC bridge can report activity */
165 led(mmc_usb_active(HZ));
166 #endif /* STORAGE_MMC */
167 gui_syncstatusbar_draw(&statusbars, false);
169 next_update=current_tick+HZ/2;
172 Exit:
173 #ifdef HAVE_TOUCHSCREEN
174 touchscreen_set_mode(old_mode);
175 #endif
176 return 0;
178 #endif
180 void usb_screen(void)
182 #ifdef USB_NONE
183 /* nothing here! */
184 #else
185 int i;
186 int usb_bars = VP_SB_ALLSCREENS; /* force statusbars */
187 int old_bars = viewportmanager_get_statusbar();
189 FOR_NB_SCREENS(i)
191 screens[i].backdrop_show(BACKDROP_MAIN);
192 screens[i].backlight_on();
193 screens[i].clear_display();
194 #ifdef HAVE_REMOTE_LCD
195 if (i == SCREEN_REMOTE)
197 screens[i].bitmap(remote_usblogo,
198 (LCD_REMOTE_WIDTH-BMPWIDTH_remote_usblogo),
199 (LCD_REMOTE_HEIGHT-BMPHEIGHT_remote_usblogo)/2,
200 BMPWIDTH_remote_usblogo, BMPHEIGHT_remote_usblogo);
202 else
203 #endif
205 #ifdef HAVE_LCD_BITMAP
206 screens[i].transparent_bitmap(usblogo,
207 (LCD_WIDTH-BMPWIDTH_usblogo),
208 (LCD_HEIGHT-BMPHEIGHT_usblogo)/2,
209 BMPWIDTH_usblogo, BMPHEIGHT_usblogo);
210 #else
211 screens[i].double_height(false);
212 screens[i].puts_scroll(0, 0, "[USB Mode]");
213 status_set_param(false);
214 status_set_audio(false);
215 status_set_usb(true);
216 #endif /* HAVE_LCD_BITMAP */
218 screens[i].update();
220 /* force statusbar by ignoring the setting */
221 usb_bars |= VP_SB_IGNORE_SETTING(i);
224 viewportmanager_set_statusbar(usb_bars);
226 #ifdef SIMULATOR
227 while (button_get(true) & BUTTON_REL);
228 #else
229 usb_acknowledge(SYS_USB_CONNECTED_ACK);
230 while (handle_usb_events());
231 #endif /* SIMULATOR */
232 #ifdef HAVE_LCD_CHARCELLS
233 status_set_usb(false);
234 #endif /* HAVE_LCD_CHARCELLS */
235 FOR_NB_SCREENS(i)
237 screens[i].backlight_on();
239 viewportmanager_set_statusbar(old_bars);
240 send_event(GUI_EVENT_REFRESH, NULL);
242 #endif /* USB_NONE */
245 #if (CONFIG_STORAGE & STORAGE_MMC)
246 int mmc_remove_request(void)
248 struct queue_event ev;
249 int i;
250 FOR_NB_SCREENS(i)
251 screens[i].clear_display();
252 splash(0, ID2P(LANG_REMOVE_MMC));
254 while (1)
256 queue_wait_w_tmo(&button_queue, &ev, HZ/2);
257 switch (ev.id)
259 case SYS_HOTSWAP_EXTRACTED:
260 return SYS_HOTSWAP_EXTRACTED;
262 case SYS_USB_DISCONNECTED:
263 return SYS_USB_DISCONNECTED;
267 #endif
269 /* the charging screen is only used for archos targets */
270 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING) && defined(CPU_SH)
272 #ifdef HAVE_LCD_BITMAP
273 static void charging_display_info(bool animate)
275 unsigned char charging_logo[36];
276 const int pox_x = (LCD_WIDTH - sizeof(charging_logo)) / 2;
277 const int pox_y = 32;
278 static unsigned phase = 3;
279 unsigned i;
280 char buf[32];
281 (void)buf;
283 #ifdef NEED_ATA_POWER_BATT_MEASURE
284 if (ide_powered()) /* FM and V2 can only measure when ATA power is on */
285 #endif
287 int battv = battery_voltage();
288 snprintf(buf, 32, " Batt: %d.%02dV %d%% ", battv / 1000,
289 (battv % 1000) / 10, battery_level());
290 lcd_puts(0, 7, buf);
293 #ifdef ARCHOS_RECORER
294 snprintf(buf, 32, "Charge mode:");
295 lcd_puts(0, 2, buf);
297 if (charge_state == CHARGING)
298 snprintf(buf, 32, str(LANG_BATTERY_CHARGE));
299 else if (charge_state == TOPOFF)
300 snprintf(buf, 32, str(LANG_BATTERY_TOPOFF_CHARGE));
301 else if (charge_state == TRICKLE)
302 snprintf(buf, 32, str(LANG_BATTERY_TRICKLE_CHARGE));
303 else
304 snprintf(buf, 32, "not charging");
306 lcd_puts(0, 3, buf);
307 if (!charger_enabled())
308 animate = false;
309 #endif /* ARCHOS_RECORER */
311 /* middle part */
312 memset(charging_logo+3, 0x00, 32);
313 charging_logo[0] = 0x3C;
314 charging_logo[1] = 0x24;
315 charging_logo[2] = charging_logo[35] = 0xFF;
317 if (!animate)
318 { /* draw the outline */
319 /* middle part */
320 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
321 sizeof(charging_logo), 8);
322 lcd_set_drawmode(DRMODE_FG);
323 /* upper line */
324 charging_logo[0] = charging_logo[1] = 0x00;
325 memset(charging_logo+2, 0x80, 34);
326 lcd_mono_bitmap(charging_logo, pox_x, pox_y, sizeof(charging_logo), 8);
327 /* lower line */
328 memset(charging_logo+2, 0x01, 34);
329 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 16,
330 sizeof(charging_logo), 8);
331 lcd_set_drawmode(DRMODE_SOLID);
333 else
334 { /* animate the middle part */
335 for (i = 3; i<MIN(sizeof(charging_logo)-1, phase); i++)
337 if ((i-phase) % 8 == 0)
338 { /* draw a "bubble" here */
339 unsigned bitpos;
340 bitpos = (phase + i/8) % 15; /* "bounce" effect */
341 if (bitpos > 7)
342 bitpos = 14 - bitpos;
343 charging_logo[i] = BIT_N(bitpos);
346 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
347 sizeof(charging_logo), 8);
348 phase++;
350 lcd_update();
352 #else /* not HAVE_LCD_BITMAP */
354 static unsigned long logo_chars[4];
355 static const unsigned char logo_pattern[] = {
356 0x07, 0x04, 0x1c, 0x14, 0x1c, 0x04, 0x07, 0, /* char 1 */
357 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 2 */
358 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 3 */
359 0x1f, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1f, 0, /* char 4 */
362 static void logo_lock_patterns(bool on)
364 int i;
366 if (on)
368 for (i = 0; i < 4; i++)
369 logo_chars[i] = lcd_get_locked_pattern();
371 else
373 for (i = 0; i < 4; i++)
374 lcd_unlock_pattern(logo_chars[i]);
378 static void charging_display_info(bool animate)
380 int battv;
381 unsigned i, ypos;
382 static unsigned phase = 3;
383 char buf[32];
385 battv = battery_voltage();
386 snprintf(buf, sizeof(buf), " %d.%02dV", battv / 1000, (battv % 1000) / 10);
387 lcd_puts(4, 1, buf);
389 memcpy(buf, logo_pattern, 32); /* copy logo patterns */
391 if (!animate) /* build the screen */
393 lcd_double_height(false);
394 lcd_puts(0, 0, "[Charging]");
395 for (i = 0; i < 4; i++)
396 lcd_putc(i, 1, logo_chars[i]);
398 else /* animate the logo */
400 for (i = 3; i < MIN(19, phase); i++)
402 if ((i - phase) % 5 == 0)
403 { /* draw a "bubble" here */
404 ypos = (phase + i/5) % 9; /* "bounce" effect */
405 if (ypos > 4)
406 ypos = 8 - ypos;
407 buf[5 - ypos + 8 * (i/5)] |= 0x10u >> (i%5);
410 phase++;
413 for (i = 0; i < 4; i++)
414 lcd_define_pattern(logo_chars[i], buf + 8 * i);
416 lcd_update();
418 #endif /* (not) HAVE_LCD_BITMAP */
420 /* blocks while charging, returns on event:
421 1 if charger cable was removed
422 2 if Off/Stop key was pressed
423 3 if On key was pressed
424 4 if USB was connected */
426 int charging_screen(void)
428 unsigned int button;
429 int rc = 0;
431 ide_power_enable(false); /* power down the disk, else would be spinning */
433 lcd_clear_display();
434 backlight_set_timeout(global_settings.backlight_timeout);
435 #ifdef HAVE_REMOTE_LCD
436 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
437 #endif
438 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
440 #ifdef HAVE_LCD_CHARCELLS
441 logo_lock_patterns(true);
442 #endif
443 charging_display_info(false);
447 gui_syncstatusbar_draw(&statusbars, false);
448 charging_display_info(true);
449 button = get_action(CONTEXT_STD,HZ/3);
450 if (button == ACTION_STD_OK)
451 rc = 2;
452 else if (usb_detect() == USB_INSERTED)
453 rc = 3;
454 else if (!charger_inserted())
455 rc = 1;
456 } while (!rc);
458 #ifdef HAVE_LCD_CHARCELLS
459 logo_lock_patterns(false);
460 #endif
461 return rc;
463 #endif /* CONFIG_CHARGING && !HAVE_POWEROFF_WHILE_CHARGING && defined(CPU_SH) */
465 #if CONFIG_CHARGING
466 void charging_splash(void)
468 splash(2*HZ, str(LANG_BATTERY_CHARGE));
469 button_clear_queue();
471 #endif
474 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
476 /* little helper function for voice output */
477 static void say_time(int cursorpos, const struct tm *tm)
479 int value = 0;
480 int unit = 0;
482 if (!global_settings.talk_menu)
483 return;
485 switch(cursorpos)
487 case 0:
488 value = tm->tm_hour;
489 unit = UNIT_HOUR;
490 break;
491 case 1:
492 value = tm->tm_min;
493 unit = UNIT_MIN;
494 break;
495 case 2:
496 value = tm->tm_sec;
497 unit = UNIT_SEC;
498 break;
499 case 3:
500 value = tm->tm_year + 1900;
501 break;
502 case 5:
503 value = tm->tm_mday;
504 break;
507 if (cursorpos == 4) /* month */
508 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, false);
509 else
510 talk_value(value, unit, false);
514 #define INDEX_X 0
515 #define INDEX_Y 1
517 #define SEPARATOR ":"
518 bool set_time_screen(const char* title, struct tm *tm)
520 bool done = false;
521 int button;
522 unsigned int i, j, s;
523 int cursorpos = 0;
524 unsigned int realyear;
525 unsigned int width;
526 unsigned int min, max;
527 unsigned int statusbar_height = 0;
528 unsigned int separator_width, weekday_width;
529 unsigned int prev_line_height;
530 static unsigned char daysinmonth[] =
531 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
532 unsigned char buffer[20];
533 struct viewport vp[NB_SCREENS];
534 int nb_lines;
536 /* 6 possible cursor possitions, 2 values stored for each: x, y */
537 unsigned int cursor[6][2];
539 int *valptr = NULL;
540 unsigned char *ptr[6];
542 if(global_settings.statusbar)
543 statusbar_height = STATUSBAR_HEIGHT;
545 /* speak selection when screen is entered */
546 say_time(cursorpos, tm);
548 while (!done) {
549 /* for easy acess in the drawing loop */
550 ptr[0] = buffer; /* hours */
551 ptr[1] = buffer + 3; /* minutes */
552 ptr[2] = buffer + 6; /* seconds */
553 ptr[3] = buffer + 9; /* year */
554 ptr[4] = str(LANG_MONTH_JANUARY + tm->tm_mon); /* monthname */
555 ptr[5] = buffer + 14; /* day of month */
557 /* calculate the number of days in febuary */
558 realyear = tm->tm_year + 1900;
559 if((realyear % 4 == 0 && !(realyear % 100 == 0)) || realyear % 400 == 0)
560 daysinmonth[1] = 29;
561 else
562 daysinmonth[1] = 28;
564 /* fix day if month or year changed */
565 if (tm->tm_mday > daysinmonth[tm->tm_mon])
566 tm->tm_mday = daysinmonth[tm->tm_mon];
568 /* calculate day of week */
569 set_day_of_week(tm);
571 /* put all the numbers we want from the tm struct into
572 an easily printable buffer */
573 snprintf(buffer, sizeof(buffer),
574 "%02d " "%02d " "%02d " "%04d " "%02d",
575 tm->tm_hour, tm->tm_min, tm->tm_sec,
576 tm->tm_year+1900, tm->tm_mday);
578 /* convert spaces in the buffer to '\0' to make it possible to work
579 directly on the buffer */
580 for(i=0; i < sizeof(buffer); i++)
582 if(buffer[i] == ' ')
583 buffer[i] = '\0';
586 FOR_NB_SCREENS(s)
588 viewport_set_defaults(&vp[s], s);
589 screens[s].set_viewport(&vp[s]);
590 nb_lines = viewport_get_nb_lines(&vp[s]);
592 /* minimum lines needed is 2 + title line */
593 if (nb_lines < 4)
595 vp[s].font = FONT_SYSFIXED;
596 nb_lines = viewport_get_nb_lines(&vp[s]);
599 /* recalculate the positions and offsets */
600 if (nb_lines >= 3)
601 screens[s].getstringsize(title, NULL, &prev_line_height);
602 else
603 prev_line_height = 0;
605 screens[s].getstringsize(SEPARATOR, &separator_width, NULL);
607 /* weekday */
608 screens[s].getstringsize(str(LANG_WEEKDAY_SUNDAY + tm->tm_wday),
609 &weekday_width, NULL);
610 screens[s].getstringsize(" ", &separator_width, NULL);
612 for(i=0, j=0; i < 6; i++)
614 if(i==3) /* second row */
616 j = weekday_width + separator_width;
617 prev_line_height *= 2;
619 screens[s].getstringsize(ptr[i], &width, NULL);
620 cursor[i][INDEX_Y] = prev_line_height + statusbar_height;
621 cursor[i][INDEX_X] = j;
622 j += width + separator_width;
625 /* draw the screen */
626 screens[s].set_viewport(&vp[s]);
627 screens[s].clear_viewport();
628 /* display the screen title */
629 screens[s].puts_scroll(0, 0, title);
631 /* these are not selectable, so we draw them outside the loop */
632 /* name of the week day */
633 screens[s].putsxy(0, cursor[3][INDEX_Y],
634 str(LANG_WEEKDAY_SUNDAY + tm->tm_wday));
636 /* draw the selected item with drawmode set to
637 DRMODE_SOLID|DRMODE_INVERSEVID, all other selectable
638 items with drawmode DRMODE_SOLID */
639 for(i=0; i<6; i++)
641 if (cursorpos == (int)i)
642 vp[s].drawmode = (DRMODE_SOLID|DRMODE_INVERSEVID);
644 screens[s].putsxy(cursor[i][INDEX_X],
645 cursor[i][INDEX_Y], ptr[i]);
647 vp[s].drawmode = DRMODE_SOLID;
649 screens[s].putsxy(cursor[i/4 +1][INDEX_X] - separator_width,
650 cursor[0][INDEX_Y], SEPARATOR);
653 /* print help text */
654 if (nb_lines > 4)
655 screens[s].puts(0, 4, str(LANG_TIME_SET_BUTTON));
656 if (nb_lines > 5)
657 screens[s].puts(0, 5, str(LANG_TIME_REVERT));
658 screens[s].update_viewport();
659 screens[s].set_viewport(NULL);
662 /* set the most common numbers */
663 min = 0;
664 max = 59;
665 /* calculate the minimum and maximum for the number under cursor */
666 switch(cursorpos) {
667 case 0: /* hour */
668 max = 23;
669 valptr = &tm->tm_hour;
670 break;
671 case 1: /* minute */
672 valptr = &tm->tm_min;
673 break;
674 case 2: /* second */
675 valptr = &tm->tm_sec;
676 break;
677 case 3: /* year */
678 min = 1;
679 max = 200;
680 valptr = &tm->tm_year;
681 break;
682 case 4: /* month */
683 max = 11;
684 valptr = &tm->tm_mon;
685 break;
686 case 5: /* day */
687 min = 1;
688 max = daysinmonth[tm->tm_mon];
689 valptr = &tm->tm_mday;
690 break;
693 #ifdef HAVE_TOUCHSCREEN
694 enum touchscreen_mode old_mode = touchscreen_get_mode();
696 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
697 #endif
698 button = get_action(CONTEXT_SETTINGS_TIME, TIMEOUT_BLOCK);
699 #ifdef HAVE_TOUCHSCREEN
700 touchscreen_set_mode(old_mode);
701 #endif
702 switch ( button ) {
703 case ACTION_STD_PREV:
704 cursorpos = clamp_value_wrap(--cursorpos, 5, 0);
705 say_time(cursorpos, tm);
706 break;
707 case ACTION_STD_NEXT:
708 cursorpos = clamp_value_wrap(++cursorpos, 5, 0);
709 say_time(cursorpos, tm);
710 break;
711 case ACTION_SETTINGS_INC:
712 case ACTION_SETTINGS_INCREPEAT:
713 *valptr = clamp_value_wrap(++(*valptr), max, min);
714 say_time(cursorpos, tm);
715 break;
716 case ACTION_SETTINGS_DEC:
717 case ACTION_SETTINGS_DECREPEAT:
718 *valptr = clamp_value_wrap(--(*valptr), max, min);
719 say_time(cursorpos, tm);
720 break;
722 case ACTION_STD_OK:
723 done = true;
724 break;
726 case ACTION_STD_CANCEL:
727 done = true;
728 tm->tm_year = -1;
729 break;
731 default:
732 if (default_event_handler(button) == SYS_USB_CONNECTED)
733 return true;
734 break;
737 return false;
739 #endif /* defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0) */
741 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
742 bool shutdown_screen(void)
744 int button;
745 bool done = false;
746 long time_entered = current_tick;
748 lcd_stop_scroll();
750 splash(0, str(LANG_CONFIRM_SHUTDOWN));
752 while(!done && TIME_BEFORE(current_tick,time_entered+HZ*2))
754 button = get_action(CONTEXT_STD,HZ);
755 switch(button)
757 case ACTION_STD_CANCEL:
758 sys_poweroff();
759 break;
761 /* do nothing here, because ACTION_NONE might be caused
762 * by timeout or button release. In case of timeout the loop
763 * is terminated by TIME_BEFORE */
764 case ACTION_NONE:
765 break;
767 default:
768 if(default_event_handler(button) == SYS_USB_CONNECTED)
769 return true;
770 done = true;
771 break;
774 return false;
776 #endif
778 static const int id3_headers[]=
780 LANG_ID3_TITLE,
781 LANG_ID3_ARTIST,
782 LANG_ID3_ALBUM,
783 LANG_ID3_ALBUMARTIST,
784 LANG_ID3_GROUPING,
785 LANG_ID3_DISCNUM,
786 LANG_ID3_TRACKNUM,
787 LANG_ID3_COMMENT,
788 LANG_ID3_GENRE,
789 LANG_ID3_YEAR,
790 LANG_ID3_LENGTH,
791 LANG_ID3_PLAYLIST,
792 LANG_ID3_BITRATE,
793 LANG_ID3_FREQUENCY,
794 #if CONFIG_CODEC == SWCODEC
795 LANG_ID3_TRACK_GAIN,
796 LANG_ID3_ALBUM_GAIN,
797 #endif
798 LANG_ID3_PATH,
801 struct id3view_info {
802 struct mp3entry* id3;
803 int count;
804 int info_id[ARRAYLEN(id3_headers)];
807 static const char* id3_get_info(int selected_item, void* data,
808 char *buffer, size_t buffer_len)
810 struct id3view_info *info = (struct id3view_info*)data;
811 struct mp3entry* id3 =info->id3;
812 int info_no=selected_item/2;
813 if(!(selected_item%2))
814 {/* header */
815 return(str(id3_headers[info->info_id[info_no]]));
817 else
818 {/* data */
820 char * val=NULL;
821 switch(info->info_id[info_no])
823 case 0:/*LANG_ID3_TITLE*/
824 val=id3->title;
825 break;
826 case 1:/*LANG_ID3_ARTIST*/
827 val=id3->artist;
828 break;
829 case 2:/*LANG_ID3_ALBUM*/
830 val=id3->album;
831 break;
832 case 3:/*LANG_ID3_ALBUMARTIST*/
833 val=id3->albumartist;
834 break;
835 case 4:/*LANG_ID3_GROUPING*/
836 val=id3->grouping;
837 break;
838 case 5:/*LANG_ID3_DISCNUM*/
839 if (id3->disc_string)
840 val = id3->disc_string;
841 else if (id3->discnum)
843 snprintf(buffer, buffer_len, "%d", id3->discnum);
844 val = buffer;
846 break;
847 case 6:/*LANG_ID3_TRACKNUM*/
848 if (id3->track_string)
849 val = id3->track_string;
850 else if (id3->tracknum)
852 snprintf(buffer, buffer_len, "%d", id3->tracknum);
853 val = buffer;
855 break;
856 case 7:/*LANG_ID3_COMMENT*/
857 val=id3->comment;
858 break;
859 case 8:/*LANG_ID3_GENRE*/
860 val = id3->genre_string;
861 break;
862 case 9:/*LANG_ID3_YEAR*/
863 if (id3->year_string)
864 val = id3->year_string;
865 else if (id3->year)
867 snprintf(buffer, buffer_len, "%d", id3->year);
868 val = buffer;
870 break;
871 case 10:/*LANG_ID3_LENGTH*/
872 format_time(buffer, buffer_len, id3->length);
873 val=buffer;
874 break;
875 case 11:/*LANG_ID3_PLAYLIST*/
876 snprintf(buffer, buffer_len, "%d/%d",
877 playlist_get_display_index(), playlist_amount());
878 val=buffer;
879 break;
880 case 12:/*LANG_ID3_BITRATE*/
881 snprintf(buffer, buffer_len, "%d kbps%s", id3->bitrate,
882 id3->vbr ? str(LANG_ID3_VBR) : (const unsigned char*) "");
883 val=buffer;
884 break;
885 case 13:/*LANG_ID3_FREQUENCY*/
886 snprintf(buffer, buffer_len, "%ld Hz", id3->frequency);
887 val=buffer;
888 break;
889 #if CONFIG_CODEC == SWCODEC
890 case 14:/*LANG_ID3_TRACK_GAIN*/
891 val=id3->track_gain_string;
892 break;
893 case 15:/*LANG_ID3_ALBUM_GAIN*/
894 val=id3->album_gain_string;
895 break;
896 case 16:/*LANG_ID3_PATH*/
897 #else
898 case 14:/*LANG_ID3_PATH*/
899 #endif
900 val=id3->path;
901 break;
903 return val && *val ? val : NULL;
907 bool browse_id3(void)
909 struct gui_synclist id3_lists;
910 struct mp3entry* id3 = audio_current_track();
911 int key;
912 unsigned int i;
913 struct id3view_info info;
914 info.count = 0;
915 info.id3 = id3;
916 for (i = 0; i < ARRAYLEN(id3_headers); i++)
918 char temp[8];
919 info.info_id[i] = i;
920 if (id3_get_info((i*2)+1, &info, temp, 8) != NULL)
921 info.info_id[info.count++] = i;
924 gui_synclist_init(&id3_lists, &id3_get_info, &info, true, 2, NULL);
925 gui_synclist_set_nb_items(&id3_lists, info.count*2);
926 gui_synclist_draw(&id3_lists);
927 while (true) {
928 key = get_action(CONTEXT_LIST,HZ/2);
929 if(key!=ACTION_NONE && key!=ACTION_UNKNOWN
930 && !gui_synclist_do_button(&id3_lists, &key,LIST_WRAP_UNLESS_HELD))
932 return(default_event_handler(key) == SYS_USB_CONNECTED);
937 static const char* runtime_get_data(int selected_item, void* data,
938 char* buffer, size_t buffer_len)
940 (void)data;
941 int t;
942 switch (selected_item)
944 case 0: return str(LANG_RUNNING_TIME);
945 case 1: t = global_status.runtime; break;
946 case 2: return str(LANG_TOP_TIME);
947 case 3: t = global_status.topruntime; break;
948 default:
949 return "";
952 snprintf(buffer, buffer_len, "%dh %dm %ds",
953 t / 3600, (t % 3600) / 60, t % 60);
954 return buffer;
957 static int runtime_speak_data(int selected_item, void* data)
959 (void) data;
960 talk_ids(false,
961 (selected_item < 2) ? LANG_RUNNING_TIME : LANG_TOP_TIME,
962 TALK_ID((selected_item < 2) ? global_status.runtime
963 : global_status.topruntime, UNIT_TIME));
964 return 0;
968 bool view_runtime(void)
970 static const char *lines[]={ID2P(LANG_CLEAR_TIME)};
971 static const struct text_message message={lines, 1};
973 struct gui_synclist lists;
974 int action;
975 gui_synclist_init(&lists, runtime_get_data, NULL, false, 2, NULL);
976 #if !defined(HAVE_LCD_CHARCELLS)
977 gui_synclist_set_title(&lists, str(LANG_RUNNING_TIME), NOICON);
978 #else
979 gui_synclist_set_title(&lists, NULL, NOICON);
980 #endif
981 if(global_settings.talk_menu)
982 gui_synclist_set_voice_callback(&lists, runtime_speak_data);
983 gui_synclist_set_icon_callback(&lists, NULL);
984 gui_synclist_set_nb_items(&lists, 4);
985 gui_synclist_speak_item(&lists);
986 while(1)
988 #if CONFIG_CHARGING
989 if (charger_inserted())
991 global_status.runtime = 0;
993 else
994 #endif
996 global_status.runtime += ((current_tick - lasttime) / HZ);
998 lasttime = current_tick;
999 gui_synclist_draw(&lists);
1000 list_do_action(CONTEXT_STD, HZ,
1001 &lists, &action, LIST_WRAP_UNLESS_HELD);
1002 if(action == ACTION_STD_CANCEL)
1003 break;
1004 if(action == ACTION_STD_OK) {
1005 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
1007 if (!(gui_synclist_get_sel_pos(&lists)/2))
1008 global_status.runtime = 0;
1009 else
1010 global_status.topruntime = 0;
1011 gui_synclist_speak_item(&lists);
1014 if(default_event_handler(action) == SYS_USB_CONNECTED)
1015 return true;
1017 return false;
1020 #ifdef HAVE_TOUCHSCREEN
1021 static int get_sample(struct touchscreen_calibration *cal, int x, int y, int i,
1022 struct screen* screen)
1024 int action;
1025 short ts_x, ts_y;
1027 /* Draw a cross */
1028 screen->drawline(x - 10, y, x - 2, y);
1029 screen->drawline(x + 2, y, x + 10, y);
1030 screen->drawline(x, y - 10, x, y - 2);
1031 screen->drawline(x, y + 2, x, y + 10);
1032 screen->update();
1034 /* Wait for a touchscreen press */
1035 while(true)
1037 action = get_action(CONTEXT_STD, TIMEOUT_BLOCK);
1038 if(action == ACTION_TOUCHSCREEN)
1040 if(action_get_touchscreen_press(&ts_x, &ts_y) == BUTTON_REL)
1041 break;
1043 else if(action == ACTION_STD_CANCEL)
1044 return -1;
1047 cal->x[i][0] = ts_x;
1048 cal->y[i][0] = ts_y;
1049 cal->x[i][1] = x;
1050 cal->y[i][1] = y;
1052 return 0;
1056 int calibrate(void)
1058 short points[3][2] = {
1059 {LCD_WIDTH/10, LCD_HEIGHT/10},
1060 {7*LCD_WIDTH/8, LCD_HEIGHT/2},
1061 {LCD_WIDTH/2, 7*LCD_HEIGHT/8}
1063 struct screen* screen = &screens[SCREEN_MAIN];
1064 enum touchscreen_mode old_mode = touchscreen_get_mode();
1065 struct touchscreen_calibration cal;
1066 int i, ret = 0;
1067 int statusbar = global_settings.statusbar; /* hide the statusbar */
1068 global_settings.statusbar = STATUSBAR_OFF;
1070 touchscreen_disable_mapping(); /* set raw mode */
1071 touchscreen_set_mode(TOUCHSCREEN_POINT);
1072 for(i=0; i<3; i++)
1074 screen->clear_display();
1076 if(get_sample(&cal, points[i][0], points[i][1], i, screen))
1078 ret = -1;
1079 break;
1083 if(ret == 0)
1084 touchscreen_calibrate(&cal);
1085 else
1086 touchscreen_reset_mapping();
1087 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1088 touchscreen_set_mode(old_mode);
1089 global_settings.statusbar = statusbar;
1091 return ret;
1094 int reset_mapping(void)
1096 touchscreen_reset_mapping();
1097 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1098 return 0;
1100 #endif