Fix the pitch detector plugin for iaudioM3
[kugel-rb.git] / apps / screens.c
blob92ab2b7e4bf6ad04268e2dfdff38421bb5737943
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 #if (CONFIG_STORAGE & STORAGE_MMC)
100 int next_update=0;
101 #endif /* STORAGE_MMC */
103 #ifdef HAVE_TOUCHSCREEN
104 enum touchscreen_mode old_mode = touchscreen_get_mode();
106 /* TODO: Paint buttons on screens OR switch to point mode and use
107 * touchscreen as a touchpad to move the host's mouse cursor */
108 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
109 #endif
111 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
112 while(1)
114 int button;
115 #if defined(HAVE_USBSTACK) && defined(USB_ENABLE_HID)
116 bool hid_enabled = usb_core_driver_enabled(USB_DRIVER_HID);
118 if (hid_enabled)
120 int id = HID_CONSUMER_USAGE_UNASSIGNED;
121 button = get_action(CONTEXT_USB_HID, HZ/2);
123 switch (button)
125 case ACTION_USB_HID_PLAY:
126 id = HID_CONSUMER_USAGE_PLAY_PAUSE;
127 break;
128 case ACTION_USB_HID_STOP:
129 id = HID_CONSUMER_USAGE_STOP;
130 break;
131 case ACTION_USB_HID_SKIPPREV:
132 id = HID_CONSUMER_USAGE_SCAN_PREVIOUS_TRACK;
133 break;
134 case ACTION_USB_HID_SKIPNEXT:
135 id = HID_CONSUMER_USAGE_SCAN_NEXT_TRACK;
136 break;
137 case ACTION_USB_HID_VOLDOWN:
138 id = HID_CONSUMER_USAGE_VOLUME_DECREMENT;
139 break;
140 case ACTION_USB_HID_VOLUP:
141 id = HID_CONSUMER_USAGE_VOLUME_INCREMENT;
142 break;
143 case ACTION_USB_HID_MUTE:
144 id = HID_CONSUMER_USAGE_MUTE;
145 break;
148 if (id != HID_CONSUMER_USAGE_UNASSIGNED)
149 usb_hid_send(HID_USAGE_PAGE_CONSUMER, id);
151 else
152 #endif
154 button = button_get_w_tmo(HZ/2);
155 /* hid emits the event in get_action */
156 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
159 switch(button)
161 case SYS_USB_DISCONNECTED:
162 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
163 goto Exit;
164 case SYS_TIMEOUT:
165 break;
168 #if (CONFIG_STORAGE & STORAGE_MMC) /* USB-MMC bridge can report activity */
169 if(TIME_AFTER(current_tick,next_update))
171 if(usb_inserted()) {
172 led(mmc_usb_active(HZ));
174 next_update=current_tick+HZ/2;
176 #endif /* STORAGE_MMC */
178 Exit:
179 #ifdef HAVE_TOUCHSCREEN
180 touchscreen_set_mode(old_mode);
181 #endif
182 return 0;
184 #endif
186 void usb_screen(void)
188 #ifdef USB_NONE
189 /* nothing here! */
190 #else
191 int i;
192 int usb_bars = VP_SB_ALLSCREENS; /* force statusbars */
193 int old_bars = viewportmanager_get_statusbar();
195 FOR_NB_SCREENS(i)
197 screens[i].backdrop_show(BACKDROP_MAIN);
198 screens[i].backlight_on();
199 screens[i].clear_display();
200 #ifdef HAVE_REMOTE_LCD
201 if (i == SCREEN_REMOTE)
203 screens[i].bitmap(remote_usblogo,
204 (LCD_REMOTE_WIDTH-BMPWIDTH_remote_usblogo),
205 (LCD_REMOTE_HEIGHT-BMPHEIGHT_remote_usblogo)/2,
206 BMPWIDTH_remote_usblogo, BMPHEIGHT_remote_usblogo);
208 else
209 #endif
211 #ifdef HAVE_LCD_BITMAP
212 screens[i].transparent_bitmap(usblogo,
213 (LCD_WIDTH-BMPWIDTH_usblogo),
214 (LCD_HEIGHT-BMPHEIGHT_usblogo)/2,
215 BMPWIDTH_usblogo, BMPHEIGHT_usblogo);
216 #else
217 screens[i].double_height(false);
218 screens[i].puts_scroll(0, 0, "[USB Mode]");
219 status_set_param(false);
220 status_set_audio(false);
221 status_set_usb(true);
222 #endif /* HAVE_LCD_BITMAP */
224 screens[i].update();
226 /* force statusbar by ignoring the setting */
227 usb_bars |= VP_SB_IGNORE_SETTING(i);
230 viewportmanager_set_statusbar(usb_bars);
232 #ifdef SIMULATOR
233 while (1)
235 if (button_get_w_tmo(HZ/2))
236 break;
237 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
239 #else
240 usb_acknowledge(SYS_USB_CONNECTED_ACK);
241 while (handle_usb_events());
242 #endif /* SIMULATOR */
243 #ifdef HAVE_LCD_CHARCELLS
244 status_set_usb(false);
245 #endif /* HAVE_LCD_CHARCELLS */
246 FOR_NB_SCREENS(i)
248 screens[i].backlight_on();
250 viewportmanager_set_statusbar(old_bars);
251 send_event(GUI_EVENT_REFRESH, NULL);
253 #endif /* USB_NONE */
256 #if (CONFIG_STORAGE & STORAGE_MMC)
257 int mmc_remove_request(void)
259 struct queue_event ev;
260 int i;
261 FOR_NB_SCREENS(i)
262 screens[i].clear_display();
263 splash(0, ID2P(LANG_REMOVE_MMC));
265 while (1)
267 queue_wait_w_tmo(&button_queue, &ev, HZ/2);
268 switch (ev.id)
270 case SYS_HOTSWAP_EXTRACTED:
271 return SYS_HOTSWAP_EXTRACTED;
273 case SYS_USB_DISCONNECTED:
274 return SYS_USB_DISCONNECTED;
278 #endif
280 /* the charging screen is only used for archos targets */
281 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING) && defined(CPU_SH)
283 #ifdef HAVE_LCD_BITMAP
284 static void charging_display_info(bool animate)
286 unsigned char charging_logo[36];
287 const int pox_x = (LCD_WIDTH - sizeof(charging_logo)) / 2;
288 const int pox_y = 32;
289 static unsigned phase = 3;
290 unsigned i;
291 char buf[32];
292 (void)buf;
294 #ifdef NEED_ATA_POWER_BATT_MEASURE
295 if (ide_powered()) /* FM and V2 can only measure when ATA power is on */
296 #endif
298 int battv = battery_voltage();
299 snprintf(buf, 32, " Batt: %d.%02dV %d%% ", battv / 1000,
300 (battv % 1000) / 10, battery_level());
301 lcd_puts(0, 7, buf);
304 #ifdef ARCHOS_RECORER
305 snprintf(buf, 32, "Charge mode:");
306 lcd_puts(0, 2, buf);
308 if (charge_state == CHARGING)
309 snprintf(buf, 32, str(LANG_BATTERY_CHARGE));
310 else if (charge_state == TOPOFF)
311 snprintf(buf, 32, str(LANG_BATTERY_TOPOFF_CHARGE));
312 else if (charge_state == TRICKLE)
313 snprintf(buf, 32, str(LANG_BATTERY_TRICKLE_CHARGE));
314 else
315 snprintf(buf, 32, "not charging");
317 lcd_puts(0, 3, buf);
318 if (!charger_enabled())
319 animate = false;
320 #endif /* ARCHOS_RECORER */
322 /* middle part */
323 memset(charging_logo+3, 0x00, 32);
324 charging_logo[0] = 0x3C;
325 charging_logo[1] = 0x24;
326 charging_logo[2] = charging_logo[35] = 0xFF;
328 if (!animate)
329 { /* draw the outline */
330 /* middle part */
331 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
332 sizeof(charging_logo), 8);
333 lcd_set_drawmode(DRMODE_FG);
334 /* upper line */
335 charging_logo[0] = charging_logo[1] = 0x00;
336 memset(charging_logo+2, 0x80, 34);
337 lcd_mono_bitmap(charging_logo, pox_x, pox_y, sizeof(charging_logo), 8);
338 /* lower line */
339 memset(charging_logo+2, 0x01, 34);
340 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 16,
341 sizeof(charging_logo), 8);
342 lcd_set_drawmode(DRMODE_SOLID);
344 else
345 { /* animate the middle part */
346 for (i = 3; i<MIN(sizeof(charging_logo)-1, phase); i++)
348 if ((i-phase) % 8 == 0)
349 { /* draw a "bubble" here */
350 unsigned bitpos;
351 bitpos = (phase + i/8) % 15; /* "bounce" effect */
352 if (bitpos > 7)
353 bitpos = 14 - bitpos;
354 charging_logo[i] = BIT_N(bitpos);
357 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
358 sizeof(charging_logo), 8);
359 phase++;
361 lcd_update();
363 #else /* not HAVE_LCD_BITMAP */
365 static unsigned long logo_chars[4];
366 static const unsigned char logo_pattern[] = {
367 0x07, 0x04, 0x1c, 0x14, 0x1c, 0x04, 0x07, 0, /* char 1 */
368 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 2 */
369 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 3 */
370 0x1f, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1f, 0, /* char 4 */
373 static void logo_lock_patterns(bool on)
375 int i;
377 if (on)
379 for (i = 0; i < 4; i++)
380 logo_chars[i] = lcd_get_locked_pattern();
382 else
384 for (i = 0; i < 4; i++)
385 lcd_unlock_pattern(logo_chars[i]);
389 static void charging_display_info(bool animate)
391 int battv;
392 unsigned i, ypos;
393 static unsigned phase = 3;
394 char buf[32];
396 battv = battery_voltage();
397 snprintf(buf, sizeof(buf), " %d.%02dV", battv / 1000, (battv % 1000) / 10);
398 lcd_puts(4, 1, buf);
400 memcpy(buf, logo_pattern, 32); /* copy logo patterns */
402 if (!animate) /* build the screen */
404 lcd_double_height(false);
405 lcd_puts(0, 0, "[Charging]");
406 for (i = 0; i < 4; i++)
407 lcd_putc(i, 1, logo_chars[i]);
409 else /* animate the logo */
411 for (i = 3; i < MIN(19, phase); i++)
413 if ((i - phase) % 5 == 0)
414 { /* draw a "bubble" here */
415 ypos = (phase + i/5) % 9; /* "bounce" effect */
416 if (ypos > 4)
417 ypos = 8 - ypos;
418 buf[5 - ypos + 8 * (i/5)] |= 0x10u >> (i%5);
421 phase++;
424 for (i = 0; i < 4; i++)
425 lcd_define_pattern(logo_chars[i], buf + 8 * i);
427 lcd_update();
429 #endif /* (not) HAVE_LCD_BITMAP */
431 /* blocks while charging, returns on event:
432 1 if charger cable was removed
433 2 if Off/Stop key was pressed
434 3 if On key was pressed
435 4 if USB was connected */
437 int charging_screen(void)
439 unsigned int button;
440 int rc = 0;
442 ide_power_enable(false); /* power down the disk, else would be spinning */
444 lcd_clear_display();
445 backlight_set_timeout(global_settings.backlight_timeout);
446 #ifdef HAVE_REMOTE_LCD
447 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
448 #endif
449 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
451 #ifdef HAVE_LCD_CHARCELLS
452 logo_lock_patterns(true);
453 #endif
454 charging_display_info(false);
458 gui_syncstatusbar_draw(&statusbars, false);
459 charging_display_info(true);
460 button = get_action(CONTEXT_STD,HZ/3);
461 if (button == ACTION_STD_OK)
462 rc = 2;
463 else if (usb_detect() == USB_INSERTED)
464 rc = 3;
465 else if (!charger_inserted())
466 rc = 1;
467 } while (!rc);
469 #ifdef HAVE_LCD_CHARCELLS
470 logo_lock_patterns(false);
471 #endif
472 return rc;
474 #endif /* CONFIG_CHARGING && !HAVE_POWEROFF_WHILE_CHARGING && defined(CPU_SH) */
476 #if CONFIG_CHARGING
477 void charging_splash(void)
479 splash(2*HZ, str(LANG_BATTERY_CHARGE));
480 button_clear_queue();
482 #endif
485 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
487 /* little helper function for voice output */
488 static void say_time(int cursorpos, const struct tm *tm)
490 int value = 0;
491 int unit = 0;
493 if (!global_settings.talk_menu)
494 return;
496 switch(cursorpos)
498 case 0:
499 value = tm->tm_hour;
500 unit = UNIT_HOUR;
501 break;
502 case 1:
503 value = tm->tm_min;
504 unit = UNIT_MIN;
505 break;
506 case 2:
507 value = tm->tm_sec;
508 unit = UNIT_SEC;
509 break;
510 case 3:
511 value = tm->tm_year + 1900;
512 break;
513 case 5:
514 value = tm->tm_mday;
515 break;
518 if (cursorpos == 4) /* month */
519 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, false);
520 else
521 talk_value(value, unit, false);
525 #define INDEX_X 0
526 #define INDEX_Y 1
528 #define SEPARATOR ":"
529 bool set_time_screen(const char* title, struct tm *tm)
531 bool done = false;
532 int button;
533 unsigned int i, j, s;
534 int cursorpos = 0;
535 unsigned int realyear;
536 unsigned int width;
537 unsigned int min, max;
538 unsigned int statusbar_height = 0;
539 unsigned int separator_width, weekday_width;
540 unsigned int prev_line_height;
541 static unsigned char daysinmonth[] =
542 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
543 unsigned char buffer[20];
544 struct viewport vp[NB_SCREENS];
545 int nb_lines;
547 /* 6 possible cursor possitions, 2 values stored for each: x, y */
548 unsigned int cursor[6][2];
550 int *valptr = NULL;
551 unsigned char *ptr[6];
553 if(global_settings.statusbar)
554 statusbar_height = STATUSBAR_HEIGHT;
556 /* speak selection when screen is entered */
557 say_time(cursorpos, tm);
559 while (!done) {
560 /* for easy acess in the drawing loop */
561 ptr[0] = buffer; /* hours */
562 ptr[1] = buffer + 3; /* minutes */
563 ptr[2] = buffer + 6; /* seconds */
564 ptr[3] = buffer + 9; /* year */
565 ptr[4] = str(LANG_MONTH_JANUARY + tm->tm_mon); /* monthname */
566 ptr[5] = buffer + 14; /* day of month */
568 /* calculate the number of days in febuary */
569 realyear = tm->tm_year + 1900;
570 if((realyear % 4 == 0 && !(realyear % 100 == 0)) || realyear % 400 == 0)
571 daysinmonth[1] = 29;
572 else
573 daysinmonth[1] = 28;
575 /* fix day if month or year changed */
576 if (tm->tm_mday > daysinmonth[tm->tm_mon])
577 tm->tm_mday = daysinmonth[tm->tm_mon];
579 /* calculate day of week */
580 set_day_of_week(tm);
582 /* put all the numbers we want from the tm struct into
583 an easily printable buffer */
584 snprintf(buffer, sizeof(buffer),
585 "%02d " "%02d " "%02d " "%04d " "%02d",
586 tm->tm_hour, tm->tm_min, tm->tm_sec,
587 tm->tm_year+1900, tm->tm_mday);
589 /* convert spaces in the buffer to '\0' to make it possible to work
590 directly on the buffer */
591 for(i=0; i < sizeof(buffer); i++)
593 if(buffer[i] == ' ')
594 buffer[i] = '\0';
597 FOR_NB_SCREENS(s)
599 viewport_set_defaults(&vp[s], s);
600 screens[s].set_viewport(&vp[s]);
601 nb_lines = viewport_get_nb_lines(&vp[s]);
603 /* minimum lines needed is 2 + title line */
604 if (nb_lines < 4)
606 vp[s].font = FONT_SYSFIXED;
607 nb_lines = viewport_get_nb_lines(&vp[s]);
610 /* recalculate the positions and offsets */
611 if (nb_lines >= 3)
612 screens[s].getstringsize(title, NULL, &prev_line_height);
613 else
614 prev_line_height = 0;
616 screens[s].getstringsize(SEPARATOR, &separator_width, NULL);
618 /* weekday */
619 screens[s].getstringsize(str(LANG_WEEKDAY_SUNDAY + tm->tm_wday),
620 &weekday_width, NULL);
621 screens[s].getstringsize(" ", &separator_width, NULL);
623 for(i=0, j=0; i < 6; i++)
625 if(i==3) /* second row */
627 j = weekday_width + separator_width;
628 prev_line_height *= 2;
630 screens[s].getstringsize(ptr[i], &width, NULL);
631 cursor[i][INDEX_Y] = prev_line_height + statusbar_height;
632 cursor[i][INDEX_X] = j;
633 j += width + separator_width;
636 /* draw the screen */
637 screens[s].set_viewport(&vp[s]);
638 screens[s].clear_viewport();
639 /* display the screen title */
640 screens[s].puts_scroll(0, 0, title);
642 /* these are not selectable, so we draw them outside the loop */
643 /* name of the week day */
644 screens[s].putsxy(0, cursor[3][INDEX_Y],
645 str(LANG_WEEKDAY_SUNDAY + tm->tm_wday));
647 /* draw the selected item with drawmode set to
648 DRMODE_SOLID|DRMODE_INVERSEVID, all other selectable
649 items with drawmode DRMODE_SOLID */
650 for(i=0; i<6; i++)
652 if (cursorpos == (int)i)
653 vp[s].drawmode = (DRMODE_SOLID|DRMODE_INVERSEVID);
655 screens[s].putsxy(cursor[i][INDEX_X],
656 cursor[i][INDEX_Y], ptr[i]);
658 vp[s].drawmode = DRMODE_SOLID;
660 screens[s].putsxy(cursor[i/4 +1][INDEX_X] - separator_width,
661 cursor[0][INDEX_Y], SEPARATOR);
664 /* print help text */
665 if (nb_lines > 4)
666 screens[s].puts(0, 4, str(LANG_TIME_SET_BUTTON));
667 if (nb_lines > 5)
668 screens[s].puts(0, 5, str(LANG_TIME_REVERT));
669 screens[s].update_viewport();
670 screens[s].set_viewport(NULL);
673 /* set the most common numbers */
674 min = 0;
675 max = 59;
676 /* calculate the minimum and maximum for the number under cursor */
677 switch(cursorpos) {
678 case 0: /* hour */
679 max = 23;
680 valptr = &tm->tm_hour;
681 break;
682 case 1: /* minute */
683 valptr = &tm->tm_min;
684 break;
685 case 2: /* second */
686 valptr = &tm->tm_sec;
687 break;
688 case 3: /* year */
689 min = 1;
690 max = 200;
691 valptr = &tm->tm_year;
692 break;
693 case 4: /* month */
694 max = 11;
695 valptr = &tm->tm_mon;
696 break;
697 case 5: /* day */
698 min = 1;
699 max = daysinmonth[tm->tm_mon];
700 valptr = &tm->tm_mday;
701 break;
704 #ifdef HAVE_TOUCHSCREEN
705 enum touchscreen_mode old_mode = touchscreen_get_mode();
707 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
708 #endif
709 button = get_action(CONTEXT_SETTINGS_TIME, TIMEOUT_BLOCK);
710 #ifdef HAVE_TOUCHSCREEN
711 touchscreen_set_mode(old_mode);
712 #endif
713 switch ( button ) {
714 case ACTION_STD_PREV:
715 cursorpos = clamp_value_wrap(--cursorpos, 5, 0);
716 say_time(cursorpos, tm);
717 break;
718 case ACTION_STD_NEXT:
719 cursorpos = clamp_value_wrap(++cursorpos, 5, 0);
720 say_time(cursorpos, tm);
721 break;
722 case ACTION_SETTINGS_INC:
723 case ACTION_SETTINGS_INCREPEAT:
724 *valptr = clamp_value_wrap(++(*valptr), max, min);
725 say_time(cursorpos, tm);
726 break;
727 case ACTION_SETTINGS_DEC:
728 case ACTION_SETTINGS_DECREPEAT:
729 *valptr = clamp_value_wrap(--(*valptr), max, min);
730 say_time(cursorpos, tm);
731 break;
733 case ACTION_STD_OK:
734 done = true;
735 break;
737 case ACTION_STD_CANCEL:
738 done = true;
739 tm->tm_year = -1;
740 break;
742 default:
743 if (default_event_handler(button) == SYS_USB_CONNECTED)
744 return true;
745 break;
748 return false;
750 #endif /* defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0) */
752 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
753 bool shutdown_screen(void)
755 int button;
756 bool done = false;
757 long time_entered = current_tick;
759 lcd_stop_scroll();
761 splash(0, str(LANG_CONFIRM_SHUTDOWN));
763 while(!done && TIME_BEFORE(current_tick,time_entered+HZ*2))
765 button = get_action(CONTEXT_STD,HZ);
766 switch(button)
768 case ACTION_STD_CANCEL:
769 sys_poweroff();
770 break;
772 /* do nothing here, because ACTION_NONE might be caused
773 * by timeout or button release. In case of timeout the loop
774 * is terminated by TIME_BEFORE */
775 case ACTION_NONE:
776 break;
778 default:
779 if(default_event_handler(button) == SYS_USB_CONNECTED)
780 return true;
781 done = true;
782 break;
785 return false;
787 #endif
789 static const int id3_headers[]=
791 LANG_ID3_TITLE,
792 LANG_ID3_ARTIST,
793 LANG_ID3_ALBUM,
794 LANG_ID3_ALBUMARTIST,
795 LANG_ID3_GROUPING,
796 LANG_ID3_DISCNUM,
797 LANG_ID3_TRACKNUM,
798 LANG_ID3_COMMENT,
799 LANG_ID3_GENRE,
800 LANG_ID3_YEAR,
801 LANG_ID3_LENGTH,
802 LANG_ID3_PLAYLIST,
803 LANG_ID3_BITRATE,
804 LANG_ID3_FREQUENCY,
805 #if CONFIG_CODEC == SWCODEC
806 LANG_ID3_TRACK_GAIN,
807 LANG_ID3_ALBUM_GAIN,
808 #endif
809 LANG_ID3_PATH,
812 struct id3view_info {
813 struct mp3entry* id3;
814 int count;
815 int info_id[ARRAYLEN(id3_headers)];
818 static const char* id3_get_info(int selected_item, void* data,
819 char *buffer, size_t buffer_len)
821 struct id3view_info *info = (struct id3view_info*)data;
822 struct mp3entry* id3 =info->id3;
823 int info_no=selected_item/2;
824 if(!(selected_item%2))
825 {/* header */
826 return(str(id3_headers[info->info_id[info_no]]));
828 else
829 {/* data */
831 char * val=NULL;
832 switch(info->info_id[info_no])
834 case 0:/*LANG_ID3_TITLE*/
835 val=id3->title;
836 break;
837 case 1:/*LANG_ID3_ARTIST*/
838 val=id3->artist;
839 break;
840 case 2:/*LANG_ID3_ALBUM*/
841 val=id3->album;
842 break;
843 case 3:/*LANG_ID3_ALBUMARTIST*/
844 val=id3->albumartist;
845 break;
846 case 4:/*LANG_ID3_GROUPING*/
847 val=id3->grouping;
848 break;
849 case 5:/*LANG_ID3_DISCNUM*/
850 if (id3->disc_string)
851 val = id3->disc_string;
852 else if (id3->discnum)
854 snprintf(buffer, buffer_len, "%d", id3->discnum);
855 val = buffer;
857 break;
858 case 6:/*LANG_ID3_TRACKNUM*/
859 if (id3->track_string)
860 val = id3->track_string;
861 else if (id3->tracknum)
863 snprintf(buffer, buffer_len, "%d", id3->tracknum);
864 val = buffer;
866 break;
867 case 7:/*LANG_ID3_COMMENT*/
868 val=id3->comment;
869 break;
870 case 8:/*LANG_ID3_GENRE*/
871 val = id3->genre_string;
872 break;
873 case 9:/*LANG_ID3_YEAR*/
874 if (id3->year_string)
875 val = id3->year_string;
876 else if (id3->year)
878 snprintf(buffer, buffer_len, "%d", id3->year);
879 val = buffer;
881 break;
882 case 10:/*LANG_ID3_LENGTH*/
883 format_time(buffer, buffer_len, id3->length);
884 val=buffer;
885 break;
886 case 11:/*LANG_ID3_PLAYLIST*/
887 snprintf(buffer, buffer_len, "%d/%d",
888 playlist_get_display_index(), playlist_amount());
889 val=buffer;
890 break;
891 case 12:/*LANG_ID3_BITRATE*/
892 snprintf(buffer, buffer_len, "%d kbps%s", id3->bitrate,
893 id3->vbr ? str(LANG_ID3_VBR) : (const unsigned char*) "");
894 val=buffer;
895 break;
896 case 13:/*LANG_ID3_FREQUENCY*/
897 snprintf(buffer, buffer_len, "%ld Hz", id3->frequency);
898 val=buffer;
899 break;
900 #if CONFIG_CODEC == SWCODEC
901 case 14:/*LANG_ID3_TRACK_GAIN*/
902 val=id3->track_gain_string;
903 break;
904 case 15:/*LANG_ID3_ALBUM_GAIN*/
905 val=id3->album_gain_string;
906 break;
907 case 16:/*LANG_ID3_PATH*/
908 #else
909 case 14:/*LANG_ID3_PATH*/
910 #endif
911 val=id3->path;
912 break;
914 return val && *val ? val : NULL;
918 bool browse_id3(void)
920 struct gui_synclist id3_lists;
921 struct mp3entry* id3 = audio_current_track();
922 int key;
923 unsigned int i;
924 struct id3view_info info;
925 info.count = 0;
926 info.id3 = id3;
927 for (i = 0; i < ARRAYLEN(id3_headers); i++)
929 char temp[8];
930 info.info_id[i] = i;
931 if (id3_get_info((i*2)+1, &info, temp, 8) != NULL)
932 info.info_id[info.count++] = i;
935 gui_synclist_init(&id3_lists, &id3_get_info, &info, true, 2, NULL);
936 gui_synclist_set_nb_items(&id3_lists, info.count*2);
937 gui_synclist_draw(&id3_lists);
938 while (true) {
939 key = get_action(CONTEXT_LIST,HZ/2);
940 if(key!=ACTION_NONE && key!=ACTION_UNKNOWN
941 && !gui_synclist_do_button(&id3_lists, &key,LIST_WRAP_UNLESS_HELD))
943 return(default_event_handler(key) == SYS_USB_CONNECTED);
948 static const char* runtime_get_data(int selected_item, void* data,
949 char* buffer, size_t buffer_len)
951 (void)data;
952 int t;
953 switch (selected_item)
955 case 0: return str(LANG_RUNNING_TIME);
956 case 1: t = global_status.runtime; break;
957 case 2: return str(LANG_TOP_TIME);
958 case 3: t = global_status.topruntime; break;
959 default:
960 return "";
963 snprintf(buffer, buffer_len, "%dh %dm %ds",
964 t / 3600, (t % 3600) / 60, t % 60);
965 return buffer;
968 static int runtime_speak_data(int selected_item, void* data)
970 (void) data;
971 talk_ids(false,
972 (selected_item < 2) ? LANG_RUNNING_TIME : LANG_TOP_TIME,
973 TALK_ID((selected_item < 2) ? global_status.runtime
974 : global_status.topruntime, UNIT_TIME));
975 return 0;
979 bool view_runtime(void)
981 static const char *lines[]={ID2P(LANG_CLEAR_TIME)};
982 static const struct text_message message={lines, 1};
984 struct gui_synclist lists;
985 int action;
986 gui_synclist_init(&lists, runtime_get_data, NULL, false, 2, NULL);
987 #if !defined(HAVE_LCD_CHARCELLS)
988 gui_synclist_set_title(&lists, str(LANG_RUNNING_TIME), NOICON);
989 #else
990 gui_synclist_set_title(&lists, NULL, NOICON);
991 #endif
992 if(global_settings.talk_menu)
993 gui_synclist_set_voice_callback(&lists, runtime_speak_data);
994 gui_synclist_set_icon_callback(&lists, NULL);
995 gui_synclist_set_nb_items(&lists, 4);
996 gui_synclist_speak_item(&lists);
997 while(1)
999 #if CONFIG_CHARGING
1000 if (charger_inserted())
1002 global_status.runtime = 0;
1004 else
1005 #endif
1007 global_status.runtime += ((current_tick - lasttime) / HZ);
1009 lasttime = current_tick;
1010 gui_synclist_draw(&lists);
1011 list_do_action(CONTEXT_STD, HZ,
1012 &lists, &action, LIST_WRAP_UNLESS_HELD);
1013 if(action == ACTION_STD_CANCEL)
1014 break;
1015 if(action == ACTION_STD_OK) {
1016 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
1018 if (!(gui_synclist_get_sel_pos(&lists)/2))
1019 global_status.runtime = 0;
1020 else
1021 global_status.topruntime = 0;
1022 gui_synclist_speak_item(&lists);
1025 if(default_event_handler(action) == SYS_USB_CONNECTED)
1026 return true;
1028 return false;
1031 #ifdef HAVE_TOUCHSCREEN
1032 static int get_sample(struct touchscreen_calibration *cal, int x, int y, int i,
1033 struct screen* screen)
1035 int action;
1036 short ts_x, ts_y;
1038 /* Draw a cross */
1039 screen->drawline(x - 10, y, x - 2, y);
1040 screen->drawline(x + 2, y, x + 10, y);
1041 screen->drawline(x, y - 10, x, y - 2);
1042 screen->drawline(x, y + 2, x, y + 10);
1043 screen->update();
1045 /* Wait for a touchscreen press */
1046 while(true)
1048 action = get_action(CONTEXT_STD, TIMEOUT_BLOCK);
1049 if(action == ACTION_TOUCHSCREEN)
1051 if(action_get_touchscreen_press(&ts_x, &ts_y) == BUTTON_REL)
1052 break;
1054 else if(action == ACTION_STD_CANCEL)
1055 return -1;
1058 cal->x[i][0] = ts_x;
1059 cal->y[i][0] = ts_y;
1060 cal->x[i][1] = x;
1061 cal->y[i][1] = y;
1063 return 0;
1067 int calibrate(void)
1069 short points[3][2] = {
1070 {LCD_WIDTH/10, LCD_HEIGHT/10},
1071 {7*LCD_WIDTH/8, LCD_HEIGHT/2},
1072 {LCD_WIDTH/2, 7*LCD_HEIGHT/8}
1074 struct screen* screen = &screens[SCREEN_MAIN];
1075 enum touchscreen_mode old_mode = touchscreen_get_mode();
1076 struct touchscreen_calibration cal;
1077 int i, ret = 0;
1078 int statusbar = global_settings.statusbar; /* hide the statusbar */
1079 global_settings.statusbar = STATUSBAR_OFF;
1081 touchscreen_disable_mapping(); /* set raw mode */
1082 touchscreen_set_mode(TOUCHSCREEN_POINT);
1083 for(i=0; i<3; i++)
1085 screen->clear_display();
1087 if(get_sample(&cal, points[i][0], points[i][1], i, screen))
1089 ret = -1;
1090 break;
1094 if(ret == 0)
1095 touchscreen_calibrate(&cal);
1096 else
1097 touchscreen_reset_mapping();
1098 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1099 touchscreen_set_mode(old_mode);
1100 global_settings.statusbar = statusbar;
1102 return ret;
1105 int reset_mapping(void)
1107 touchscreen_reset_mapping();
1108 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1109 return 0;
1111 #endif