Remove USB mode displaying in non lcd-bitmap targets, as there are no such target...
[kugel-rb.git] / apps / screens.c
blob478338de553cb2a443d8103cf2fbec0ee39f44e7
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 "usb_keymaps.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"
68 #ifdef HAVE_LCD_BITMAP
69 #include "bitmaps/usblogo.h"
70 #endif
72 #ifdef HAVE_REMOTE_LCD
73 #include "bitmaps/remote_usblogo.h"
74 #endif
76 #if (CONFIG_STORAGE & STORAGE_MMC)
77 #include "ata_mmc.h"
78 #endif
79 #if CONFIG_CODEC == SWCODEC
80 #include "dsp.h"
81 #endif
83 /* only used in set_time screen */
84 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
85 static int clamp_value_wrap(int value, int max, int min)
87 if (value > max)
88 return min;
89 if (value < min)
90 return max;
91 return value;
93 #endif
95 #ifndef SIMULATOR
97 #ifdef USB_ENABLE_HID
98 int usb_keypad_mode;
99 #endif
101 static int handle_usb_events(void)
103 #if (CONFIG_STORAGE & STORAGE_MMC)
104 int next_update=0;
105 #endif /* STORAGE_MMC */
107 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
108 while(1)
110 int button;
111 #ifdef USB_ENABLE_HID
112 if (usb_core_driver_enabled(USB_DRIVER_HID))
114 button = get_hid_usb_action();
116 /* On mode change, we need to refresh the screen */
117 if (button == ACTION_USB_HID_MODE ||
118 button == ACTION_USB_HID_MODE_LONG)
120 break;
123 else
124 #endif
126 button = button_get_w_tmo(HZ/2);
127 /* hid emits the event in get_action */
128 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
131 switch(button)
133 case SYS_USB_DISCONNECTED:
134 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
135 return 1;
136 case SYS_TIMEOUT:
137 break;
140 #if (CONFIG_STORAGE & STORAGE_MMC) /* USB-MMC bridge can report activity */
141 if(TIME_AFTER(current_tick,next_update))
143 if(usb_inserted()) {
144 led(mmc_usb_active(HZ));
146 next_update=current_tick+HZ/2;
148 #endif /* STORAGE_MMC */
151 return 0;
153 #endif
155 #define MODE_NAME_LEN 32
156 void usb_screen(void)
158 #ifdef USB_NONE
159 /* nothing here! */
160 #else
161 int i;
162 int usb_bars = VP_SB_ALLSCREENS; /* force statusbars */
163 int old_bars = viewportmanager_get_statusbar();
164 #if defined HAVE_TOUCHSCREEN
165 enum touchscreen_mode old_mode = touchscreen_get_mode();
167 /* TODO: Paint buttons on screens OR switch to point mode and use
168 * touchscreen as a touchpad to move the host's mouse cursor */
169 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
170 #endif
172 #ifndef SIMULATOR
173 usb_acknowledge(SYS_USB_CONNECTED_ACK);
174 #endif
176 #ifdef USB_ENABLE_HID
177 usb_keypad_mode = global_settings.usb_keypad_mode;
178 #endif
180 while (1)
182 FOR_NB_SCREENS(i)
184 screens[i].backdrop_show(BACKDROP_MAIN);
185 screens[i].backlight_on();
186 screens[i].clear_display();
187 #ifdef HAVE_REMOTE_LCD
188 if (i == SCREEN_REMOTE)
190 screens[i].bitmap(remote_usblogo,
191 (LCD_REMOTE_WIDTH-BMPWIDTH_remote_usblogo),
192 (LCD_REMOTE_HEIGHT-BMPHEIGHT_remote_usblogo)/2,
193 BMPWIDTH_remote_usblogo, BMPHEIGHT_remote_usblogo);
195 else
196 #endif
198 #ifdef HAVE_LCD_BITMAP
199 screens[i].transparent_bitmap(usblogo,
200 (LCD_WIDTH-BMPWIDTH_usblogo),
201 (LCD_HEIGHT-BMPHEIGHT_usblogo)/2,
202 BMPWIDTH_usblogo, BMPHEIGHT_usblogo);
203 #ifdef USB_ENABLE_HID
204 int w, h;
206 screens[i].getstringsize(str(keypad_mode_name_get()), &w, &h);
207 screens[i].putsxy((LCD_WIDTH - w) / 2, BMPHEIGHT_usblogo +
208 (LCD_HEIGHT - BMPHEIGHT_usblogo + h) / 2,
209 str(keypad_mode_name_get()));
210 #endif /* USB_ENABLE_HID */
211 #else /* HAVE_LCD_BITMAP */
212 screens[i].double_height(false);
213 screens[i].puts_scroll(0, 0, "[USB Mode]");
214 status_set_param(false);
215 status_set_audio(false);
216 status_set_usb(true);
217 #endif /* HAVE_LCD_BITMAP */
219 screens[i].update();
221 /* force statusbar by ignoring the setting */
222 usb_bars |= VP_SB_IGNORE_SETTING(i);
225 viewportmanager_set_statusbar(usb_bars);
227 #ifdef SIMULATOR
228 if (button_get_w_tmo(HZ/2))
229 break;
230 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
231 #else
232 if (handle_usb_events())
233 break;
234 #endif /* SIMULATOR */
237 #ifdef USB_ENABLE_HID
238 if (global_settings.usb_keypad_mode != usb_keypad_mode)
240 global_settings.usb_keypad_mode = usb_keypad_mode;
241 settings_save();
243 #endif
245 #ifdef HAVE_TOUCHSCREEN
246 touchscreen_set_mode(old_mode);
247 #endif
249 #ifdef HAVE_LCD_CHARCELLS
250 status_set_usb(false);
251 #endif /* HAVE_LCD_CHARCELLS */
252 FOR_NB_SCREENS(i)
254 screens[i].backlight_on();
256 viewportmanager_set_statusbar(old_bars);
257 send_event(GUI_EVENT_REFRESH, NULL);
259 #endif /* USB_NONE */
262 #if (CONFIG_STORAGE & STORAGE_MMC)
263 int mmc_remove_request(void)
265 struct queue_event ev;
266 int i;
267 FOR_NB_SCREENS(i)
268 screens[i].clear_display();
269 splash(0, ID2P(LANG_REMOVE_MMC));
271 while (1)
273 queue_wait_w_tmo(&button_queue, &ev, HZ/2);
274 switch (ev.id)
276 case SYS_HOTSWAP_EXTRACTED:
277 return SYS_HOTSWAP_EXTRACTED;
279 case SYS_USB_DISCONNECTED:
280 return SYS_USB_DISCONNECTED;
284 #endif
286 /* the charging screen is only used for archos targets */
287 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING) && defined(CPU_SH)
289 #ifdef HAVE_LCD_BITMAP
290 static void charging_display_info(bool animate)
292 unsigned char charging_logo[36];
293 const int pox_x = (LCD_WIDTH - sizeof(charging_logo)) / 2;
294 const int pox_y = 32;
295 static unsigned phase = 3;
296 unsigned i;
297 char buf[32];
298 (void)buf;
300 #ifdef NEED_ATA_POWER_BATT_MEASURE
301 if (ide_powered()) /* FM and V2 can only measure when ATA power is on */
302 #endif
304 int battv = battery_voltage();
305 snprintf(buf, 32, " Batt: %d.%02dV %d%% ", battv / 1000,
306 (battv % 1000) / 10, battery_level());
307 lcd_puts(0, 7, buf);
310 #ifdef ARCHOS_RECORER
311 snprintf(buf, 32, "Charge mode:");
312 lcd_puts(0, 2, buf);
314 if (charge_state == CHARGING)
315 snprintf(buf, 32, str(LANG_BATTERY_CHARGE));
316 else if (charge_state == TOPOFF)
317 snprintf(buf, 32, str(LANG_BATTERY_TOPOFF_CHARGE));
318 else if (charge_state == TRICKLE)
319 snprintf(buf, 32, str(LANG_BATTERY_TRICKLE_CHARGE));
320 else
321 snprintf(buf, 32, "not charging");
323 lcd_puts(0, 3, buf);
324 if (!charger_enabled())
325 animate = false;
326 #endif /* ARCHOS_RECORER */
328 /* middle part */
329 memset(charging_logo+3, 0x00, 32);
330 charging_logo[0] = 0x3C;
331 charging_logo[1] = 0x24;
332 charging_logo[2] = charging_logo[35] = 0xFF;
334 if (!animate)
335 { /* draw the outline */
336 /* middle part */
337 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
338 sizeof(charging_logo), 8);
339 lcd_set_drawmode(DRMODE_FG);
340 /* upper line */
341 charging_logo[0] = charging_logo[1] = 0x00;
342 memset(charging_logo+2, 0x80, 34);
343 lcd_mono_bitmap(charging_logo, pox_x, pox_y, sizeof(charging_logo), 8);
344 /* lower line */
345 memset(charging_logo+2, 0x01, 34);
346 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 16,
347 sizeof(charging_logo), 8);
348 lcd_set_drawmode(DRMODE_SOLID);
350 else
351 { /* animate the middle part */
352 for (i = 3; i<MIN(sizeof(charging_logo)-1, phase); i++)
354 if ((i-phase) % 8 == 0)
355 { /* draw a "bubble" here */
356 unsigned bitpos;
357 bitpos = (phase + i/8) % 15; /* "bounce" effect */
358 if (bitpos > 7)
359 bitpos = 14 - bitpos;
360 charging_logo[i] = BIT_N(bitpos);
363 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
364 sizeof(charging_logo), 8);
365 phase++;
367 lcd_update();
369 #else /* not HAVE_LCD_BITMAP */
371 static unsigned long logo_chars[4];
372 static const unsigned char logo_pattern[] = {
373 0x07, 0x04, 0x1c, 0x14, 0x1c, 0x04, 0x07, 0, /* char 1 */
374 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 2 */
375 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 3 */
376 0x1f, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1f, 0, /* char 4 */
379 static void logo_lock_patterns(bool on)
381 int i;
383 if (on)
385 for (i = 0; i < 4; i++)
386 logo_chars[i] = lcd_get_locked_pattern();
388 else
390 for (i = 0; i < 4; i++)
391 lcd_unlock_pattern(logo_chars[i]);
395 static void charging_display_info(bool animate)
397 int battv;
398 unsigned i, ypos;
399 static unsigned phase = 3;
400 char buf[32];
402 battv = battery_voltage();
403 snprintf(buf, sizeof(buf), " %d.%02dV", battv / 1000, (battv % 1000) / 10);
404 lcd_puts(4, 1, buf);
406 memcpy(buf, logo_pattern, 32); /* copy logo patterns */
408 if (!animate) /* build the screen */
410 lcd_double_height(false);
411 lcd_puts(0, 0, "[Charging]");
412 for (i = 0; i < 4; i++)
413 lcd_putc(i, 1, logo_chars[i]);
415 else /* animate the logo */
417 for (i = 3; i < MIN(19, phase); i++)
419 if ((i - phase) % 5 == 0)
420 { /* draw a "bubble" here */
421 ypos = (phase + i/5) % 9; /* "bounce" effect */
422 if (ypos > 4)
423 ypos = 8 - ypos;
424 buf[5 - ypos + 8 * (i/5)] |= 0x10u >> (i%5);
427 phase++;
430 for (i = 0; i < 4; i++)
431 lcd_define_pattern(logo_chars[i], buf + 8 * i);
433 lcd_update();
435 #endif /* (not) HAVE_LCD_BITMAP */
437 /* blocks while charging, returns on event:
438 1 if charger cable was removed
439 2 if Off/Stop key was pressed
440 3 if On key was pressed
441 4 if USB was connected */
443 int charging_screen(void)
445 unsigned int button;
446 int rc = 0;
448 ide_power_enable(false); /* power down the disk, else would be spinning */
450 lcd_clear_display();
451 backlight_set_timeout(global_settings.backlight_timeout);
452 #ifdef HAVE_REMOTE_LCD
453 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
454 #endif
455 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
457 #ifdef HAVE_LCD_CHARCELLS
458 logo_lock_patterns(true);
459 #endif
460 charging_display_info(false);
464 gui_syncstatusbar_draw(&statusbars, false);
465 charging_display_info(true);
466 button = get_action(CONTEXT_STD,HZ/3);
467 if (button == ACTION_STD_OK)
468 rc = 2;
469 else if (usb_detect() == USB_INSERTED)
470 rc = 3;
471 else if (!charger_inserted())
472 rc = 1;
473 } while (!rc);
475 #ifdef HAVE_LCD_CHARCELLS
476 logo_lock_patterns(false);
477 #endif
478 return rc;
480 #endif /* CONFIG_CHARGING && !HAVE_POWEROFF_WHILE_CHARGING && defined(CPU_SH) */
482 #if CONFIG_CHARGING
483 void charging_splash(void)
485 splash(2*HZ, str(LANG_BATTERY_CHARGE));
486 button_clear_queue();
488 #endif
491 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
493 /* little helper function for voice output */
494 static void say_time(int cursorpos, const struct tm *tm)
496 int value = 0;
497 int unit = 0;
499 if (!global_settings.talk_menu)
500 return;
502 switch(cursorpos)
504 case 0:
505 value = tm->tm_hour;
506 unit = UNIT_HOUR;
507 break;
508 case 1:
509 value = tm->tm_min;
510 unit = UNIT_MIN;
511 break;
512 case 2:
513 value = tm->tm_sec;
514 unit = UNIT_SEC;
515 break;
516 case 3:
517 value = tm->tm_year + 1900;
518 break;
519 case 5:
520 value = tm->tm_mday;
521 break;
524 if (cursorpos == 4) /* month */
525 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, false);
526 else
527 talk_value(value, unit, false);
531 #define INDEX_X 0
532 #define INDEX_Y 1
534 #define SEPARATOR ":"
535 bool set_time_screen(const char* title, struct tm *tm)
537 bool done = false;
538 int button;
539 unsigned int i, j, s;
540 int cursorpos = 0;
541 unsigned int realyear;
542 unsigned int width;
543 unsigned int min, max;
544 unsigned int statusbar_height = 0;
545 unsigned int separator_width, weekday_width;
546 unsigned int prev_line_height;
547 static unsigned char daysinmonth[] =
548 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
549 unsigned char buffer[20];
550 struct viewport vp[NB_SCREENS];
551 int nb_lines;
553 /* 6 possible cursor possitions, 2 values stored for each: x, y */
554 unsigned int cursor[6][2];
556 int *valptr = NULL;
557 unsigned char *ptr[6];
559 if(global_settings.statusbar)
560 statusbar_height = STATUSBAR_HEIGHT;
562 /* speak selection when screen is entered */
563 say_time(cursorpos, tm);
565 while (!done) {
566 /* for easy acess in the drawing loop */
567 ptr[0] = buffer; /* hours */
568 ptr[1] = buffer + 3; /* minutes */
569 ptr[2] = buffer + 6; /* seconds */
570 ptr[3] = buffer + 9; /* year */
571 ptr[4] = str(LANG_MONTH_JANUARY + tm->tm_mon); /* monthname */
572 ptr[5] = buffer + 14; /* day of month */
574 /* calculate the number of days in febuary */
575 realyear = tm->tm_year + 1900;
576 if((realyear % 4 == 0 && !(realyear % 100 == 0)) || realyear % 400 == 0)
577 daysinmonth[1] = 29;
578 else
579 daysinmonth[1] = 28;
581 /* fix day if month or year changed */
582 if (tm->tm_mday > daysinmonth[tm->tm_mon])
583 tm->tm_mday = daysinmonth[tm->tm_mon];
585 /* calculate day of week */
586 set_day_of_week(tm);
588 /* put all the numbers we want from the tm struct into
589 an easily printable buffer */
590 snprintf(buffer, sizeof(buffer),
591 "%02d " "%02d " "%02d " "%04d " "%02d",
592 tm->tm_hour, tm->tm_min, tm->tm_sec,
593 tm->tm_year+1900, tm->tm_mday);
595 /* convert spaces in the buffer to '\0' to make it possible to work
596 directly on the buffer */
597 for(i=0; i < sizeof(buffer); i++)
599 if(buffer[i] == ' ')
600 buffer[i] = '\0';
603 FOR_NB_SCREENS(s)
605 viewport_set_defaults(&vp[s], s);
606 screens[s].set_viewport(&vp[s]);
607 nb_lines = viewport_get_nb_lines(&vp[s]);
609 /* minimum lines needed is 2 + title line */
610 if (nb_lines < 4)
612 vp[s].font = FONT_SYSFIXED;
613 nb_lines = viewport_get_nb_lines(&vp[s]);
616 /* recalculate the positions and offsets */
617 if (nb_lines >= 3)
618 screens[s].getstringsize(title, NULL, &prev_line_height);
619 else
620 prev_line_height = 0;
622 screens[s].getstringsize(SEPARATOR, &separator_width, NULL);
624 /* weekday */
625 screens[s].getstringsize(str(LANG_WEEKDAY_SUNDAY + tm->tm_wday),
626 &weekday_width, NULL);
627 screens[s].getstringsize(" ", &separator_width, NULL);
629 for(i=0, j=0; i < 6; i++)
631 if(i==3) /* second row */
633 j = weekday_width + separator_width;
634 prev_line_height *= 2;
636 screens[s].getstringsize(ptr[i], &width, NULL);
637 cursor[i][INDEX_Y] = prev_line_height + statusbar_height;
638 cursor[i][INDEX_X] = j;
639 j += width + separator_width;
642 /* draw the screen */
643 screens[s].set_viewport(&vp[s]);
644 screens[s].clear_viewport();
645 /* display the screen title */
646 screens[s].puts_scroll(0, 0, title);
648 /* these are not selectable, so we draw them outside the loop */
649 /* name of the week day */
650 screens[s].putsxy(0, cursor[3][INDEX_Y],
651 str(LANG_WEEKDAY_SUNDAY + tm->tm_wday));
653 /* draw the selected item with drawmode set to
654 DRMODE_SOLID|DRMODE_INVERSEVID, all other selectable
655 items with drawmode DRMODE_SOLID */
656 for(i=0; i<6; i++)
658 if (cursorpos == (int)i)
659 vp[s].drawmode = (DRMODE_SOLID|DRMODE_INVERSEVID);
661 screens[s].putsxy(cursor[i][INDEX_X],
662 cursor[i][INDEX_Y], ptr[i]);
664 vp[s].drawmode = DRMODE_SOLID;
666 screens[s].putsxy(cursor[i/4 +1][INDEX_X] - separator_width,
667 cursor[0][INDEX_Y], SEPARATOR);
670 /* print help text */
671 if (nb_lines > 4)
672 screens[s].puts(0, 4, str(LANG_TIME_SET_BUTTON));
673 if (nb_lines > 5)
674 screens[s].puts(0, 5, str(LANG_TIME_REVERT));
675 screens[s].update_viewport();
676 screens[s].set_viewport(NULL);
679 /* set the most common numbers */
680 min = 0;
681 max = 59;
682 /* calculate the minimum and maximum for the number under cursor */
683 switch(cursorpos) {
684 case 0: /* hour */
685 max = 23;
686 valptr = &tm->tm_hour;
687 break;
688 case 1: /* minute */
689 valptr = &tm->tm_min;
690 break;
691 case 2: /* second */
692 valptr = &tm->tm_sec;
693 break;
694 case 3: /* year */
695 min = 1;
696 max = 200;
697 valptr = &tm->tm_year;
698 break;
699 case 4: /* month */
700 max = 11;
701 valptr = &tm->tm_mon;
702 break;
703 case 5: /* day */
704 min = 1;
705 max = daysinmonth[tm->tm_mon];
706 valptr = &tm->tm_mday;
707 break;
710 #ifdef HAVE_TOUCHSCREEN
711 enum touchscreen_mode old_mode = touchscreen_get_mode();
713 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
714 #endif
715 button = get_action(CONTEXT_SETTINGS_TIME, TIMEOUT_BLOCK);
716 #ifdef HAVE_TOUCHSCREEN
717 touchscreen_set_mode(old_mode);
718 #endif
719 switch ( button ) {
720 case ACTION_STD_PREV:
721 cursorpos = clamp_value_wrap(--cursorpos, 5, 0);
722 say_time(cursorpos, tm);
723 break;
724 case ACTION_STD_NEXT:
725 cursorpos = clamp_value_wrap(++cursorpos, 5, 0);
726 say_time(cursorpos, tm);
727 break;
728 case ACTION_SETTINGS_INC:
729 case ACTION_SETTINGS_INCREPEAT:
730 *valptr = clamp_value_wrap(++(*valptr), max, min);
731 say_time(cursorpos, tm);
732 break;
733 case ACTION_SETTINGS_DEC:
734 case ACTION_SETTINGS_DECREPEAT:
735 *valptr = clamp_value_wrap(--(*valptr), max, min);
736 say_time(cursorpos, tm);
737 break;
739 case ACTION_STD_OK:
740 done = true;
741 break;
743 case ACTION_STD_CANCEL:
744 done = true;
745 tm->tm_year = -1;
746 break;
748 default:
749 if (default_event_handler(button) == SYS_USB_CONNECTED)
750 return true;
751 break;
754 return false;
756 #endif /* defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0) */
758 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
759 bool shutdown_screen(void)
761 int button;
762 bool done = false;
763 long time_entered = current_tick;
765 lcd_stop_scroll();
767 splash(0, str(LANG_CONFIRM_SHUTDOWN));
769 while(!done && TIME_BEFORE(current_tick,time_entered+HZ*2))
771 button = get_action(CONTEXT_STD,HZ);
772 switch(button)
774 case ACTION_STD_CANCEL:
775 sys_poweroff();
776 break;
778 /* do nothing here, because ACTION_NONE might be caused
779 * by timeout or button release. In case of timeout the loop
780 * is terminated by TIME_BEFORE */
781 case ACTION_NONE:
782 break;
784 default:
785 if(default_event_handler(button) == SYS_USB_CONNECTED)
786 return true;
787 done = true;
788 break;
791 return false;
793 #endif
795 static const int id3_headers[]=
797 LANG_ID3_TITLE,
798 LANG_ID3_ARTIST,
799 LANG_ID3_ALBUM,
800 LANG_ID3_ALBUMARTIST,
801 LANG_ID3_GROUPING,
802 LANG_ID3_DISCNUM,
803 LANG_ID3_TRACKNUM,
804 LANG_ID3_COMMENT,
805 LANG_ID3_GENRE,
806 LANG_ID3_YEAR,
807 LANG_ID3_LENGTH,
808 LANG_ID3_PLAYLIST,
809 LANG_ID3_BITRATE,
810 LANG_ID3_FREQUENCY,
811 #if CONFIG_CODEC == SWCODEC
812 LANG_ID3_TRACK_GAIN,
813 LANG_ID3_ALBUM_GAIN,
814 #endif
815 LANG_ID3_PATH,
818 struct id3view_info {
819 struct mp3entry* id3;
820 int count;
821 int info_id[ARRAYLEN(id3_headers)];
824 static const char* id3_get_info(int selected_item, void* data,
825 char *buffer, size_t buffer_len)
827 struct id3view_info *info = (struct id3view_info*)data;
828 struct mp3entry* id3 =info->id3;
829 int info_no=selected_item/2;
830 if(!(selected_item%2))
831 {/* header */
832 return(str(id3_headers[info->info_id[info_no]]));
834 else
835 {/* data */
837 char * val=NULL;
838 switch(info->info_id[info_no])
840 case 0:/*LANG_ID3_TITLE*/
841 val=id3->title;
842 break;
843 case 1:/*LANG_ID3_ARTIST*/
844 val=id3->artist;
845 break;
846 case 2:/*LANG_ID3_ALBUM*/
847 val=id3->album;
848 break;
849 case 3:/*LANG_ID3_ALBUMARTIST*/
850 val=id3->albumartist;
851 break;
852 case 4:/*LANG_ID3_GROUPING*/
853 val=id3->grouping;
854 break;
855 case 5:/*LANG_ID3_DISCNUM*/
856 if (id3->disc_string)
857 val = id3->disc_string;
858 else if (id3->discnum)
860 snprintf(buffer, buffer_len, "%d", id3->discnum);
861 val = buffer;
863 break;
864 case 6:/*LANG_ID3_TRACKNUM*/
865 if (id3->track_string)
866 val = id3->track_string;
867 else if (id3->tracknum)
869 snprintf(buffer, buffer_len, "%d", id3->tracknum);
870 val = buffer;
872 break;
873 case 7:/*LANG_ID3_COMMENT*/
874 val=id3->comment;
875 break;
876 case 8:/*LANG_ID3_GENRE*/
877 val = id3->genre_string;
878 break;
879 case 9:/*LANG_ID3_YEAR*/
880 if (id3->year_string)
881 val = id3->year_string;
882 else if (id3->year)
884 snprintf(buffer, buffer_len, "%d", id3->year);
885 val = buffer;
887 break;
888 case 10:/*LANG_ID3_LENGTH*/
889 format_time(buffer, buffer_len, id3->length);
890 val=buffer;
891 break;
892 case 11:/*LANG_ID3_PLAYLIST*/
893 snprintf(buffer, buffer_len, "%d/%d",
894 playlist_get_display_index(), playlist_amount());
895 val=buffer;
896 break;
897 case 12:/*LANG_ID3_BITRATE*/
898 snprintf(buffer, buffer_len, "%d kbps%s", id3->bitrate,
899 id3->vbr ? str(LANG_ID3_VBR) : (const unsigned char*) "");
900 val=buffer;
901 break;
902 case 13:/*LANG_ID3_FREQUENCY*/
903 snprintf(buffer, buffer_len, "%ld Hz", id3->frequency);
904 val=buffer;
905 break;
906 #if CONFIG_CODEC == SWCODEC
907 case 14:/*LANG_ID3_TRACK_GAIN*/
908 val=id3->track_gain_string;
909 break;
910 case 15:/*LANG_ID3_ALBUM_GAIN*/
911 val=id3->album_gain_string;
912 break;
913 case 16:/*LANG_ID3_PATH*/
914 #else
915 case 14:/*LANG_ID3_PATH*/
916 #endif
917 val=id3->path;
918 break;
920 return val && *val ? val : NULL;
924 bool browse_id3(void)
926 struct gui_synclist id3_lists;
927 struct mp3entry* id3 = audio_current_track();
928 int key;
929 unsigned int i;
930 struct id3view_info info;
931 info.count = 0;
932 info.id3 = id3;
933 for (i = 0; i < ARRAYLEN(id3_headers); i++)
935 char temp[8];
936 info.info_id[i] = i;
937 if (id3_get_info((i*2)+1, &info, temp, 8) != NULL)
938 info.info_id[info.count++] = i;
941 gui_synclist_init(&id3_lists, &id3_get_info, &info, true, 2, NULL);
942 gui_synclist_set_nb_items(&id3_lists, info.count*2);
943 gui_synclist_draw(&id3_lists);
944 while (true) {
945 key = get_action(CONTEXT_LIST,HZ/2);
946 if(key!=ACTION_NONE && key!=ACTION_UNKNOWN
947 && !gui_synclist_do_button(&id3_lists, &key,LIST_WRAP_UNLESS_HELD))
949 return(default_event_handler(key) == SYS_USB_CONNECTED);
954 static const char* runtime_get_data(int selected_item, void* data,
955 char* buffer, size_t buffer_len)
957 (void)data;
958 int t;
959 switch (selected_item)
961 case 0: return str(LANG_RUNNING_TIME);
962 case 1: t = global_status.runtime; break;
963 case 2: return str(LANG_TOP_TIME);
964 case 3: t = global_status.topruntime; break;
965 default:
966 return "";
969 snprintf(buffer, buffer_len, "%dh %dm %ds",
970 t / 3600, (t % 3600) / 60, t % 60);
971 return buffer;
974 static int runtime_speak_data(int selected_item, void* data)
976 (void) data;
977 talk_ids(false,
978 (selected_item < 2) ? LANG_RUNNING_TIME : LANG_TOP_TIME,
979 TALK_ID((selected_item < 2) ? global_status.runtime
980 : global_status.topruntime, UNIT_TIME));
981 return 0;
985 bool view_runtime(void)
987 static const char *lines[]={ID2P(LANG_CLEAR_TIME)};
988 static const struct text_message message={lines, 1};
990 struct gui_synclist lists;
991 int action;
992 gui_synclist_init(&lists, runtime_get_data, NULL, false, 2, NULL);
993 #if !defined(HAVE_LCD_CHARCELLS)
994 gui_synclist_set_title(&lists, str(LANG_RUNNING_TIME), NOICON);
995 #else
996 gui_synclist_set_title(&lists, NULL, NOICON);
997 #endif
998 if(global_settings.talk_menu)
999 gui_synclist_set_voice_callback(&lists, runtime_speak_data);
1000 gui_synclist_set_icon_callback(&lists, NULL);
1001 gui_synclist_set_nb_items(&lists, 4);
1002 gui_synclist_speak_item(&lists);
1003 while(1)
1005 #if CONFIG_CHARGING
1006 if (charger_inserted())
1008 global_status.runtime = 0;
1010 else
1011 #endif
1013 global_status.runtime += ((current_tick - lasttime) / HZ);
1015 lasttime = current_tick;
1016 gui_synclist_draw(&lists);
1017 list_do_action(CONTEXT_STD, HZ,
1018 &lists, &action, LIST_WRAP_UNLESS_HELD);
1019 if(action == ACTION_STD_CANCEL)
1020 break;
1021 if(action == ACTION_STD_OK) {
1022 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
1024 if (!(gui_synclist_get_sel_pos(&lists)/2))
1025 global_status.runtime = 0;
1026 else
1027 global_status.topruntime = 0;
1028 gui_synclist_speak_item(&lists);
1031 if(default_event_handler(action) == SYS_USB_CONNECTED)
1032 return true;
1034 return false;
1037 #ifdef HAVE_TOUCHSCREEN
1038 static int get_sample(struct touchscreen_calibration *cal, int x, int y, int i,
1039 struct screen* screen)
1041 int action;
1042 short ts_x, ts_y;
1044 /* Draw a cross */
1045 screen->drawline(x - 10, y, x - 2, y);
1046 screen->drawline(x + 2, y, x + 10, y);
1047 screen->drawline(x, y - 10, x, y - 2);
1048 screen->drawline(x, y + 2, x, y + 10);
1049 screen->update();
1051 /* Wait for a touchscreen press */
1052 while(true)
1054 action = get_action(CONTEXT_STD, TIMEOUT_BLOCK);
1055 if(action == ACTION_TOUCHSCREEN)
1057 if(action_get_touchscreen_press(&ts_x, &ts_y) == BUTTON_REL)
1058 break;
1060 else if(action == ACTION_STD_CANCEL)
1061 return -1;
1064 cal->x[i][0] = ts_x;
1065 cal->y[i][0] = ts_y;
1066 cal->x[i][1] = x;
1067 cal->y[i][1] = y;
1069 return 0;
1073 int calibrate(void)
1075 short points[3][2] = {
1076 {LCD_WIDTH/10, LCD_HEIGHT/10},
1077 {7*LCD_WIDTH/8, LCD_HEIGHT/2},
1078 {LCD_WIDTH/2, 7*LCD_HEIGHT/8}
1080 struct screen* screen = &screens[SCREEN_MAIN];
1081 enum touchscreen_mode old_mode = touchscreen_get_mode();
1082 struct touchscreen_calibration cal;
1083 int i, ret = 0;
1084 int statusbar = global_settings.statusbar; /* hide the statusbar */
1085 global_settings.statusbar = STATUSBAR_OFF;
1087 touchscreen_disable_mapping(); /* set raw mode */
1088 touchscreen_set_mode(TOUCHSCREEN_POINT);
1089 for(i=0; i<3; i++)
1091 screen->clear_display();
1093 if(get_sample(&cal, points[i][0], points[i][1], i, screen))
1095 ret = -1;
1096 break;
1100 if(ret == 0)
1101 touchscreen_calibrate(&cal);
1102 else
1103 touchscreen_reset_mapping();
1104 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1105 touchscreen_set_mode(old_mode);
1106 global_settings.statusbar = statusbar;
1108 return ret;
1111 int reset_mapping(void)
1113 touchscreen_reset_mapping();
1114 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1115 return 0;
1117 #endif