Don't recalculate the array length during the loop, use the ARRAYLEN macro and correc...
[kugel-rb.git] / apps / screens.c
blob9a986716f344c915de5eb72a30a6b012e138a23a
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 bool statusbar = global_settings.statusbar; /* force the statusbar */
185 global_settings.statusbar = true;
186 #if LCD_DEPTH > 1
187 show_main_backdrop();
188 #endif
189 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
190 show_remote_main_backdrop();
191 #endif
192 FOR_NB_SCREENS(i)
194 screens[i].backlight_on();
195 screens[i].clear_display();
196 #if NB_SCREENS > 1
197 if (i == SCREEN_REMOTE)
199 screens[i].bitmap(remote_usblogo,
200 (LCD_REMOTE_WIDTH-BMPWIDTH_remote_usblogo),
201 (LCD_REMOTE_HEIGHT-BMPHEIGHT_remote_usblogo)/2,
202 BMPWIDTH_remote_usblogo, BMPHEIGHT_remote_usblogo);
204 else
206 #endif
207 #ifdef HAVE_LCD_BITMAP
208 screens[i].transparent_bitmap(usblogo,
209 (LCD_WIDTH-BMPWIDTH_usblogo),
210 (LCD_HEIGHT-BMPHEIGHT_usblogo)/2,
211 BMPWIDTH_usblogo, BMPHEIGHT_usblogo);
212 #else
213 screens[i].double_height(false);
214 screens[i].puts_scroll(0, 0, "[USB Mode]");
215 status_set_param(false);
216 status_set_audio(false);
217 status_set_usb(true);
218 #endif /* HAVE_LCD_BITMAP */
219 #if NB_SCREENS > 1
221 #endif
222 screens[i].update();
225 #ifdef SIMULATOR
226 while (button_get(true) & BUTTON_REL);
227 #else
228 usb_acknowledge(SYS_USB_CONNECTED_ACK);
229 while (handle_usb_events());
230 #endif /* SIMULATOR */
231 #ifdef HAVE_LCD_CHARCELLS
232 status_set_usb(false);
233 #endif /* HAVE_LCD_CHARCELLS */
234 FOR_NB_SCREENS(i)
235 screens[i].backlight_on();
236 global_settings.statusbar = statusbar;
237 #endif /* USB_NONE */
240 #if (CONFIG_STORAGE & STORAGE_MMC)
241 int mmc_remove_request(void)
243 struct queue_event ev;
244 int i;
245 FOR_NB_SCREENS(i)
246 screens[i].clear_display();
247 splash(0, ID2P(LANG_REMOVE_MMC));
249 while (1)
251 queue_wait_w_tmo(&button_queue, &ev, HZ/2);
252 switch (ev.id)
254 case SYS_HOTSWAP_EXTRACTED:
255 return SYS_HOTSWAP_EXTRACTED;
257 case SYS_USB_DISCONNECTED:
258 return SYS_USB_DISCONNECTED;
262 #endif
264 /* the charging screen is only used for archos targets */
265 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING) && defined(CPU_SH)
267 #ifdef HAVE_LCD_BITMAP
268 static void charging_display_info(bool animate)
270 unsigned char charging_logo[36];
271 const int pox_x = (LCD_WIDTH - sizeof(charging_logo)) / 2;
272 const int pox_y = 32;
273 static unsigned phase = 3;
274 unsigned i;
275 char buf[32];
276 (void)buf;
278 #ifdef NEED_ATA_POWER_BATT_MEASURE
279 if (ide_powered()) /* FM and V2 can only measure when ATA power is on */
280 #endif
282 int battv = battery_voltage();
283 snprintf(buf, 32, " Batt: %d.%02dV %d%% ", battv / 1000,
284 (battv % 1000) / 10, battery_level());
285 lcd_puts(0, 7, buf);
288 #ifdef ARCHOS_RECORER
289 snprintf(buf, 32, "Charge mode:");
290 lcd_puts(0, 2, buf);
292 if (charge_state == CHARGING)
293 snprintf(buf, 32, str(LANG_BATTERY_CHARGE));
294 else if (charge_state == TOPOFF)
295 snprintf(buf, 32, str(LANG_BATTERY_TOPOFF_CHARGE));
296 else if (charge_state == TRICKLE)
297 snprintf(buf, 32, str(LANG_BATTERY_TRICKLE_CHARGE));
298 else
299 snprintf(buf, 32, "not charging");
301 lcd_puts(0, 3, buf);
302 if (!charger_enabled())
303 animate = false;
304 #endif /* ARCHOS_RECORER */
306 /* middle part */
307 memset(charging_logo+3, 0x00, 32);
308 charging_logo[0] = 0x3C;
309 charging_logo[1] = 0x24;
310 charging_logo[2] = charging_logo[35] = 0xFF;
312 if (!animate)
313 { /* draw the outline */
314 /* middle part */
315 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
316 sizeof(charging_logo), 8);
317 lcd_set_drawmode(DRMODE_FG);
318 /* upper line */
319 charging_logo[0] = charging_logo[1] = 0x00;
320 memset(charging_logo+2, 0x80, 34);
321 lcd_mono_bitmap(charging_logo, pox_x, pox_y, sizeof(charging_logo), 8);
322 /* lower line */
323 memset(charging_logo+2, 0x01, 34);
324 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 16,
325 sizeof(charging_logo), 8);
326 lcd_set_drawmode(DRMODE_SOLID);
328 else
329 { /* animate the middle part */
330 for (i = 3; i<MIN(sizeof(charging_logo)-1, phase); i++)
332 if ((i-phase) % 8 == 0)
333 { /* draw a "bubble" here */
334 unsigned bitpos;
335 bitpos = (phase + i/8) % 15; /* "bounce" effect */
336 if (bitpos > 7)
337 bitpos = 14 - bitpos;
338 charging_logo[i] = BIT_N(bitpos);
341 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
342 sizeof(charging_logo), 8);
343 phase++;
345 lcd_update();
347 #else /* not HAVE_LCD_BITMAP */
349 static unsigned long logo_chars[4];
350 static const unsigned char logo_pattern[] = {
351 0x07, 0x04, 0x1c, 0x14, 0x1c, 0x04, 0x07, 0, /* char 1 */
352 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 2 */
353 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 3 */
354 0x1f, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1f, 0, /* char 4 */
357 static void logo_lock_patterns(bool on)
359 int i;
361 if (on)
363 for (i = 0; i < 4; i++)
364 logo_chars[i] = lcd_get_locked_pattern();
366 else
368 for (i = 0; i < 4; i++)
369 lcd_unlock_pattern(logo_chars[i]);
373 static void charging_display_info(bool animate)
375 int battv;
376 unsigned i, ypos;
377 static unsigned phase = 3;
378 char buf[32];
380 battv = battery_voltage();
381 snprintf(buf, sizeof(buf), " %d.%02dV", battv / 1000, (battv % 1000) / 10);
382 lcd_puts(4, 1, buf);
384 memcpy(buf, logo_pattern, 32); /* copy logo patterns */
386 if (!animate) /* build the screen */
388 lcd_double_height(false);
389 lcd_puts(0, 0, "[Charging]");
390 for (i = 0; i < 4; i++)
391 lcd_putc(i, 1, logo_chars[i]);
393 else /* animate the logo */
395 for (i = 3; i < MIN(19, phase); i++)
397 if ((i - phase) % 5 == 0)
398 { /* draw a "bubble" here */
399 ypos = (phase + i/5) % 9; /* "bounce" effect */
400 if (ypos > 4)
401 ypos = 8 - ypos;
402 buf[5 - ypos + 8 * (i/5)] |= 0x10u >> (i%5);
405 phase++;
408 for (i = 0; i < 4; i++)
409 lcd_define_pattern(logo_chars[i], buf + 8 * i);
411 lcd_update();
413 #endif /* (not) HAVE_LCD_BITMAP */
415 /* blocks while charging, returns on event:
416 1 if charger cable was removed
417 2 if Off/Stop key was pressed
418 3 if On key was pressed
419 4 if USB was connected */
421 int charging_screen(void)
423 unsigned int button;
424 int rc = 0;
426 ide_power_enable(false); /* power down the disk, else would be spinning */
428 lcd_clear_display();
429 backlight_set_timeout(global_settings.backlight_timeout);
430 #ifdef HAVE_REMOTE_LCD
431 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
432 #endif
433 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
435 #ifdef HAVE_LCD_CHARCELLS
436 logo_lock_patterns(true);
437 #endif
438 charging_display_info(false);
442 gui_syncstatusbar_draw(&statusbars, false);
443 charging_display_info(true);
444 button = get_action(CONTEXT_STD,HZ/3);
445 if (button == ACTION_STD_OK)
446 rc = 2;
447 else if (usb_detect() == USB_INSERTED)
448 rc = 3;
449 else if (!charger_inserted())
450 rc = 1;
451 } while (!rc);
453 #ifdef HAVE_LCD_CHARCELLS
454 logo_lock_patterns(false);
455 #endif
456 return rc;
458 #endif /* CONFIG_CHARGING && !HAVE_POWEROFF_WHILE_CHARGING && defined(CPU_SH) */
460 #if CONFIG_CHARGING
461 void charging_splash(void)
463 splash(2*HZ, str(LANG_BATTERY_CHARGE));
464 button_clear_queue();
466 #endif
469 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
471 /* little helper function for voice output */
472 static void say_time(int cursorpos, const struct tm *tm)
474 int value = 0;
475 int unit = 0;
477 if (!global_settings.talk_menu)
478 return;
480 switch(cursorpos)
482 case 0:
483 value = tm->tm_hour;
484 unit = UNIT_HOUR;
485 break;
486 case 1:
487 value = tm->tm_min;
488 unit = UNIT_MIN;
489 break;
490 case 2:
491 value = tm->tm_sec;
492 unit = UNIT_SEC;
493 break;
494 case 3:
495 value = tm->tm_year + 1900;
496 break;
497 case 5:
498 value = tm->tm_mday;
499 break;
502 if (cursorpos == 4) /* month */
503 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, false);
504 else
505 talk_value(value, unit, false);
509 #define INDEX_X 0
510 #define INDEX_Y 1
512 #define SEPARATOR ":"
513 bool set_time_screen(const char* title, struct tm *tm)
515 bool done = false;
516 int button;
517 unsigned int i, j, s;
518 int cursorpos = 0;
519 unsigned int julianday;
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 int daysinmonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
527 unsigned char buffer[20];
528 struct viewport vp[NB_SCREENS];
529 int nb_lines;
531 /* 6 possible cursor possitions, 2 values stored for each: x, y */
532 unsigned int cursor[6][2];
534 int *valptr = NULL;
535 unsigned char *ptr[6];
537 if(global_settings.statusbar)
538 statusbar_height = STATUSBAR_HEIGHT;
540 /* speak selection when screen is entered */
541 say_time(cursorpos, tm);
543 while (!done) {
544 /* for easy acess in the drawing loop */
545 ptr[0] = buffer; /* hours */
546 ptr[1] = buffer + 3; /* minutes */
547 ptr[2] = buffer + 6; /* seconds */
548 ptr[3] = buffer + 9; /* year */
549 ptr[4] = str(LANG_MONTH_JANUARY + tm->tm_mon); /* monthname */
550 ptr[5] = buffer + 14; /* day of month */
552 /* calculate the number of days in febuary */
553 realyear = tm->tm_year + 1900;
554 if((realyear % 4 == 0 && !(realyear % 100 == 0)) || realyear % 400 == 0)
555 daysinmonth[1] = 29;
556 else
557 daysinmonth[1] = 28;
559 /* fix day if month or year changed */
560 if (tm->tm_mday > daysinmonth[tm->tm_mon])
561 tm->tm_mday = daysinmonth[tm->tm_mon];
563 /* calculate day of week */
564 julianday = tm->tm_mday;
565 for(i = 0; (int)i < tm->tm_mon; i++) {
566 julianday += daysinmonth[i];
569 tm->tm_wday = (realyear + julianday + (realyear - 1) / 4 -
570 (realyear - 1) / 100 + (realyear - 1) / 400 + 7 - 1) % 7;
572 /* put all the numbers we want from the tm struct into
573 an easily printable buffer */
574 snprintf(buffer, sizeof(buffer),
575 "%02d " "%02d " "%02d " "%04d " "%02d",
576 tm->tm_hour, tm->tm_min, tm->tm_sec,
577 tm->tm_year+1900, tm->tm_mday);
579 /* convert spaces in the buffer to '\0' to make it possible to work
580 directly on the buffer */
581 for(i=0; i < sizeof(buffer); i++)
583 if(buffer[i] == ' ')
584 buffer[i] = '\0';
587 FOR_NB_SCREENS(s)
589 viewport_set_defaults(&vp[s], s);
590 screens[s].set_viewport(&vp[s]);
591 nb_lines = viewport_get_nb_lines(&vp[s]);
593 /* minimum lines needed is 2 + title line */
594 if (nb_lines < 4)
596 vp[s].font = FONT_SYSFIXED;
597 nb_lines = viewport_get_nb_lines(&vp[s]);
600 /* recalculate the positions and offsets */
601 if (nb_lines >= 3)
602 screens[s].getstringsize(title, NULL, &prev_line_height);
603 else
604 prev_line_height = 0;
606 screens[s].getstringsize(SEPARATOR, &separator_width, NULL);
608 /* weekday */
609 screens[s].getstringsize(str(LANG_WEEKDAY_SUNDAY + tm->tm_wday),
610 &weekday_width, NULL);
611 screens[s].getstringsize(" ", &separator_width, NULL);
613 for(i=0, j=0; i < 6; i++)
615 if(i==3) /* second row */
617 j = weekday_width + separator_width;
618 prev_line_height *= 2;
620 screens[s].getstringsize(ptr[i], &width, NULL);
621 cursor[i][INDEX_Y] = prev_line_height + statusbar_height;
622 cursor[i][INDEX_X] = j;
623 j += width + separator_width;
626 /* draw the screen */
627 screens[s].set_viewport(&vp[s]);
628 screens[s].clear_viewport();
629 /* display the screen title */
630 screens[s].puts_scroll(0, 0, title);
632 /* these are not selectable, so we draw them outside the loop */
633 /* name of the week day */
634 screens[s].putsxy(0, cursor[3][INDEX_Y],
635 str(LANG_WEEKDAY_SUNDAY + tm->tm_wday));
637 /* draw the selected item with drawmode set to
638 DRMODE_SOLID|DRMODE_INVERSEVID, all other selectable
639 items with drawmode DRMODE_SOLID */
640 for(i=0; i<6; i++)
642 if (cursorpos == (int)i)
643 vp[s].drawmode = (DRMODE_SOLID|DRMODE_INVERSEVID);
645 screens[s].putsxy(cursor[i][INDEX_X],
646 cursor[i][INDEX_Y], ptr[i]);
648 vp[s].drawmode = DRMODE_SOLID;
650 screens[s].putsxy(cursor[i/4 +1][INDEX_X] - separator_width,
651 cursor[0][INDEX_Y], SEPARATOR);
654 /* print help text */
655 if (nb_lines > 4)
656 screens[s].puts(0, 4, str(LANG_TIME_SET_BUTTON));
657 if (nb_lines > 5)
658 screens[s].puts(0, 5, str(LANG_TIME_REVERT));
659 screens[s].update_viewport();
660 screens[s].set_viewport(NULL);
663 /* set the most common numbers */
664 min = 0;
665 max = 59;
666 /* calculate the minimum and maximum for the number under cursor */
667 switch(cursorpos) {
668 case 0: /* hour */
669 max = 23;
670 valptr = &tm->tm_hour;
671 break;
672 case 1: /* minute */
673 valptr = &tm->tm_min;
674 break;
675 case 2: /* second */
676 valptr = &tm->tm_sec;
677 break;
678 case 3: /* year */
679 min = 1;
680 max = 200;
681 valptr = &tm->tm_year;
682 break;
683 case 4: /* month */
684 max = 11;
685 valptr = &tm->tm_mon;
686 break;
687 case 5: /* day */
688 min = 1;
689 max = daysinmonth[tm->tm_mon];
690 valptr = &tm->tm_mday;
691 break;
694 #ifdef HAVE_TOUCHSCREEN
695 enum touchscreen_mode old_mode = touchscreen_get_mode();
697 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
698 #endif
699 button = get_action(CONTEXT_SETTINGS_TIME, TIMEOUT_BLOCK);
700 #ifdef HAVE_TOUCHSCREEN
701 touchscreen_set_mode(old_mode);
702 #endif
703 switch ( button ) {
704 case ACTION_STD_PREV:
705 cursorpos = clamp_value_wrap(--cursorpos, 5, 0);
706 say_time(cursorpos, tm);
707 break;
708 case ACTION_STD_NEXT:
709 cursorpos = clamp_value_wrap(++cursorpos, 5, 0);
710 say_time(cursorpos, tm);
711 break;
712 case ACTION_SETTINGS_INC:
713 case ACTION_SETTINGS_INCREPEAT:
714 *valptr = clamp_value_wrap(++(*valptr), max, min);
715 say_time(cursorpos, tm);
716 break;
717 case ACTION_SETTINGS_DEC:
718 case ACTION_SETTINGS_DECREPEAT:
719 *valptr = clamp_value_wrap(--(*valptr), max, min);
720 say_time(cursorpos, tm);
721 break;
723 case ACTION_STD_OK:
724 done = true;
725 break;
727 case ACTION_STD_CANCEL:
728 done = true;
729 tm->tm_year = -1;
730 break;
732 default:
733 if (default_event_handler(button) == SYS_USB_CONNECTED)
734 return true;
735 break;
738 return false;
740 #endif /* defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0) */
742 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
743 bool shutdown_screen(void)
745 int button;
746 bool done = false;
747 long time_entered = current_tick;
749 lcd_stop_scroll();
751 splash(0, str(LANG_CONFIRM_SHUTDOWN));
753 while(!done && TIME_BEFORE(current_tick,time_entered+HZ*2))
755 button = get_action(CONTEXT_STD,HZ);
756 switch(button)
758 case ACTION_STD_CANCEL:
759 sys_poweroff();
760 break;
762 /* do nothing here, because ACTION_NONE might be caused
763 * by timeout or button release. In case of timeout the loop
764 * is terminated by TIME_BEFORE */
765 case ACTION_NONE:
766 break;
768 default:
769 if(default_event_handler(button) == SYS_USB_CONNECTED)
770 return true;
771 done = true;
772 break;
775 return false;
777 #endif
779 static const int id3_headers[]=
781 LANG_ID3_TITLE,
782 LANG_ID3_ARTIST,
783 LANG_ID3_ALBUM,
784 LANG_ID3_ALBUMARTIST,
785 LANG_ID3_GROUPING,
786 LANG_ID3_DISCNUM,
787 LANG_ID3_TRACKNUM,
788 LANG_ID3_COMMENT,
789 LANG_ID3_GENRE,
790 LANG_ID3_YEAR,
791 LANG_ID3_LENGTH,
792 LANG_ID3_PLAYLIST,
793 LANG_ID3_BITRATE,
794 LANG_ID3_FREQUENCY,
795 #if CONFIG_CODEC == SWCODEC
796 LANG_ID3_TRACK_GAIN,
797 LANG_ID3_ALBUM_GAIN,
798 #endif
799 LANG_ID3_PATH,
801 struct id3view_info {
802 struct mp3entry* id3;
803 int count;
804 int info_id[sizeof(id3_headers)/sizeof(id3_headers[0])];
806 static char * id3_get_info(int selected_item, void* data,
807 char *buffer, size_t buffer_len)
809 struct id3view_info *info = (struct id3view_info*)data;
810 struct mp3entry* id3 =info->id3;
811 int info_no=selected_item/2;
812 if(!(selected_item%2))
813 {/* header */
814 return( str(id3_headers[info->info_id[info_no]]));
816 else
817 {/* data */
819 char * val=NULL;
820 switch(info->info_id[info_no])
822 case 0:/*LANG_ID3_TITLE*/
823 val=id3->title;
824 break;
825 case 1:/*LANG_ID3_ARTIST*/
826 val=id3->artist;
827 break;
828 case 2:/*LANG_ID3_ALBUM*/
829 val=id3->album;
830 break;
831 case 3:/*LANG_ID3_ALBUMARTIST*/
832 val=id3->albumartist;
833 break;
834 case 4:/*LANG_ID3_GROUPING*/
835 val=id3->grouping;
836 break;
837 case 5:/*LANG_ID3_DISCNUM*/
838 if (id3->disc_string)
839 val = id3->disc_string;
840 else if (id3->discnum)
842 snprintf(buffer, buffer_len, "%d", id3->discnum);
843 val = buffer;
845 break;
846 case 6:/*LANG_ID3_TRACKNUM*/
847 if (id3->track_string)
848 val = id3->track_string;
849 else if (id3->tracknum)
851 snprintf(buffer, buffer_len, "%d", id3->tracknum);
852 val = buffer;
854 break;
855 case 7:/*LANG_ID3_COMMENT*/
856 val=id3->comment;
857 break;
858 case 8:/*LANG_ID3_GENRE*/
859 val = id3->genre_string;
860 break;
861 case 9:/*LANG_ID3_YEAR*/
862 if (id3->year_string)
863 val = id3->year_string;
864 else if (id3->year)
866 snprintf(buffer, buffer_len, "%d", id3->year);
867 val = buffer;
869 break;
870 case 10:/*LANG_ID3_LENGTH*/
871 format_time(buffer, buffer_len, id3->length);
872 val=buffer;
873 break;
874 case 11:/*LANG_ID3_PLAYLIST*/
875 snprintf(buffer, buffer_len, "%d/%d",
876 playlist_get_display_index(), playlist_amount());
877 val=buffer;
878 break;
879 case 12:/*LANG_ID3_BITRATE*/
880 snprintf(buffer, buffer_len, "%d kbps%s", id3->bitrate,
881 id3->vbr ? str(LANG_ID3_VBR) : (const unsigned char*) "");
882 val=buffer;
883 break;
884 case 13:/*LANG_ID3_FREQUENCY*/
885 snprintf(buffer, buffer_len, "%ld Hz", id3->frequency);
886 val=buffer;
887 break;
888 #if CONFIG_CODEC == SWCODEC
889 case 14:/*LANG_ID3_TRACK_GAIN*/
890 val=id3->track_gain_string;
891 break;
892 case 15:/*LANG_ID3_ALBUM_GAIN*/
893 val=id3->album_gain_string;
894 break;
895 case 16:/*LANG_ID3_PATH*/
896 #else
897 case 14:/*LANG_ID3_PATH*/
898 #endif
899 val=id3->path;
900 break;
902 return val && *val ? val : NULL;
906 bool browse_id3(void)
908 struct gui_synclist id3_lists;
909 struct mp3entry* id3 = audio_current_track();
910 int key;
911 unsigned int i;
912 struct id3view_info info;
913 info.count = 0;
914 info.id3 = id3;
915 for (i=0; i<sizeof(id3_headers)/sizeof(id3_headers[0]); i++)
917 char temp[8];
918 info.info_id[i] = i;
919 if (id3_get_info((i*2)+1, &info, temp, 8) != NULL)
920 info.info_id[info.count++] = i;
923 gui_synclist_init(&id3_lists, &id3_get_info, &info, true, 2, NULL);
924 gui_synclist_set_nb_items(&id3_lists, info.count*2);
925 gui_synclist_draw(&id3_lists);
926 while (true) {
927 key = get_action(CONTEXT_LIST,HZ/2);
928 if(key!=ACTION_NONE && key!=ACTION_UNKNOWN
929 && !gui_synclist_do_button(&id3_lists, &key,LIST_WRAP_UNLESS_HELD))
931 return(default_event_handler(key) == SYS_USB_CONNECTED);
936 static char* runtime_get_data(int selected_item, void* data,
937 char* buffer, size_t buffer_len)
939 (void)data;
940 int t;
941 switch (selected_item)
943 case 0: return str(LANG_RUNNING_TIME);
944 case 1: t = global_status.runtime; break;
945 case 2: return str(LANG_TOP_TIME);
946 case 3: t = global_status.topruntime; break;
947 default:
948 return "";
951 snprintf(buffer, buffer_len, "%dh %dm %ds",
952 t / 3600, (t % 3600) / 60, t % 60);
953 return buffer;
956 static int runtime_speak_data(int selected_item, void* data)
958 (void) data;
959 talk_ids(false,
960 (selected_item < 2) ? LANG_RUNNING_TIME : LANG_TOP_TIME,
961 TALK_ID((selected_item < 2) ? global_status.runtime
962 : global_status.topruntime, UNIT_TIME));
963 return 0;
967 bool view_runtime(void)
969 static const char *lines[]={ID2P(LANG_CLEAR_TIME)};
970 static const struct text_message message={lines, 1};
972 struct gui_synclist lists;
973 int action;
974 gui_synclist_init(&lists, runtime_get_data, NULL, false, 2, NULL);
975 #if !defined(HAVE_LCD_CHARCELLS)
976 gui_synclist_set_title(&lists, str(LANG_RUNNING_TIME), NOICON);
977 #else
978 gui_synclist_set_title(&lists, NULL, NOICON);
979 #endif
980 if(global_settings.talk_menu)
981 gui_synclist_set_voice_callback(&lists, runtime_speak_data);
982 gui_synclist_set_icon_callback(&lists, NULL);
983 gui_synclist_set_nb_items(&lists, 4);
984 gui_synclist_speak_item(&lists);
985 while(1)
987 #if CONFIG_CHARGING
988 if (charger_inserted())
990 global_status.runtime = 0;
992 else
993 #endif
995 global_status.runtime += ((current_tick - lasttime) / HZ);
997 lasttime = current_tick;
998 gui_synclist_draw(&lists);
999 list_do_action(CONTEXT_STD, HZ,
1000 &lists, &action, LIST_WRAP_UNLESS_HELD);
1001 if(action == ACTION_STD_CANCEL)
1002 break;
1003 if(action == ACTION_STD_OK) {
1004 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
1006 if (!(gui_synclist_get_sel_pos(&lists)/2))
1007 global_status.runtime = 0;
1008 else
1009 global_status.topruntime = 0;
1010 gui_synclist_speak_item(&lists);
1013 if(default_event_handler(action) == SYS_USB_CONNECTED)
1014 return true;
1016 return false;
1019 #ifdef HAVE_TOUCHSCREEN
1020 static int get_sample(struct touchscreen_calibration *cal, int x, int y, int i,
1021 struct screen* screen)
1023 int action;
1024 short ts_x, ts_y;
1026 /* Draw a cross */
1027 screen->drawline(x - 10, y, x - 2, y);
1028 screen->drawline(x + 2, y, x + 10, y);
1029 screen->drawline(x, y - 10, x, y - 2);
1030 screen->drawline(x, y + 2, x, y + 10);
1031 screen->update();
1033 /* Wait for a touchscreen press */
1034 while(true)
1036 action = get_action(CONTEXT_STD, TIMEOUT_BLOCK);
1037 if(action == ACTION_TOUCHSCREEN)
1039 if(action_get_touchscreen_press(&ts_x, &ts_y) == BUTTON_REL)
1040 break;
1042 else if(action == ACTION_STD_CANCEL)
1043 return -1;
1046 cal->x[i][0] = ts_x;
1047 cal->y[i][0] = ts_y;
1048 cal->x[i][1] = x;
1049 cal->y[i][1] = y;
1051 return 0;
1055 int calibrate(void)
1057 short points[3][2] = {
1058 {LCD_WIDTH/10, LCD_HEIGHT/10},
1059 {7*LCD_WIDTH/8, LCD_HEIGHT/2},
1060 {LCD_WIDTH/2, 7*LCD_HEIGHT/8}
1062 struct screen* screen = &screens[SCREEN_MAIN];
1063 enum touchscreen_mode old_mode = touchscreen_get_mode();
1064 struct touchscreen_calibration cal;
1065 int i, ret = 0;
1066 bool statusbar = global_settings.statusbar; /* hide the statusbar */
1068 global_settings.statusbar = false;
1069 touchscreen_disable_mapping(); /* set raw mode */
1070 touchscreen_set_mode(TOUCHSCREEN_POINT);
1071 for(i=0; i<3; i++)
1073 screen->clear_display();
1075 if(get_sample(&cal, points[i][0], points[i][1], i, screen))
1077 ret = -1;
1078 break;
1082 if(ret == 0)
1083 touchscreen_calibrate(&cal);
1084 else
1085 touchscreen_reset_mapping();
1086 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1087 touchscreen_set_mode(old_mode);
1088 global_settings.statusbar = statusbar;
1090 return ret;
1093 int reset_mapping(void)
1095 touchscreen_reset_mapping();
1096 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1097 return 0;
1099 #endif