Revise r23225 a bit, removing the debug_printf function and implementing more generic...
[kugel-rb.git] / apps / screens.c
blobcebe5df023a071bdb9e9d3517b0044ae0d3bf59f
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"
67 #include "language.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
98 #ifdef USB_ENABLE_HID
99 int usb_keypad_mode;
100 #endif
102 static int handle_usb_events(void)
104 #if (CONFIG_STORAGE & STORAGE_MMC)
105 int next_update=0;
106 #endif /* STORAGE_MMC */
108 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
109 while(1)
111 int button;
112 #ifdef USB_ENABLE_HID
113 if (usb_core_driver_enabled(USB_DRIVER_HID))
115 button = get_hid_usb_action();
117 /* On mode change, we need to refresh the screen */
118 if (button == ACTION_USB_HID_MODE_SWITCH_NEXT ||
119 button == ACTION_USB_HID_MODE_SWITCH_PREV)
121 break;
124 else
125 #endif
127 button = button_get_w_tmo(HZ/2);
128 /* hid emits the event in get_action */
129 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
132 switch(button)
134 case SYS_USB_DISCONNECTED:
135 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
136 return 1;
137 case SYS_TIMEOUT:
138 break;
141 #if (CONFIG_STORAGE & STORAGE_MMC) /* USB-MMC bridge can report activity */
142 if(TIME_AFTER(current_tick,next_update))
144 if(usb_inserted()) {
145 led(mmc_usb_active(HZ));
147 next_update=current_tick+HZ/2;
149 #endif /* STORAGE_MMC */
152 return 0;
154 #endif
156 #define MODE_NAME_LEN 32
157 void usb_screen(void)
159 #ifdef USB_NONE
160 /* nothing here! */
161 #else
162 int i;
163 int usb_bars = VP_SB_ALLSCREENS; /* force statusbars */
164 int old_bars = viewportmanager_get_statusbar();
165 #if defined HAVE_TOUCHSCREEN
166 enum touchscreen_mode old_mode = touchscreen_get_mode();
168 /* TODO: Paint buttons on screens OR switch to point mode and use
169 * touchscreen as a touchpad to move the host's mouse cursor */
170 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
171 #endif
173 #ifndef SIMULATOR
174 usb_acknowledge(SYS_USB_CONNECTED_ACK);
175 #endif
177 #ifdef USB_ENABLE_HID
178 usb_keypad_mode = global_settings.usb_keypad_mode;
179 #endif
181 while (1)
183 FOR_NB_SCREENS(i)
185 screens[i].backdrop_show(BACKDROP_MAIN);
186 screens[i].backlight_on();
187 screens[i].clear_display();
188 #ifdef HAVE_REMOTE_LCD
189 if (i == SCREEN_REMOTE)
191 screens[i].bitmap(remote_usblogo,
192 (LCD_REMOTE_WIDTH-BMPWIDTH_remote_usblogo),
193 (LCD_REMOTE_HEIGHT-BMPHEIGHT_remote_usblogo)/2,
194 BMPWIDTH_remote_usblogo, BMPHEIGHT_remote_usblogo);
196 else
197 #endif
199 #ifdef HAVE_LCD_BITMAP
200 screens[i].transparent_bitmap(usblogo,
201 (LCD_WIDTH-BMPWIDTH_usblogo),
202 (LCD_HEIGHT-BMPHEIGHT_usblogo)/2,
203 BMPWIDTH_usblogo, BMPHEIGHT_usblogo);
204 #ifdef USB_ENABLE_HID
205 int w, h;
207 screens[i].getstringsize(str(keypad_mode_name_get()), &w, &h);
208 screens[i].putsxy((LCD_WIDTH - w) / 2, BMPHEIGHT_usblogo +
209 (LCD_HEIGHT - BMPHEIGHT_usblogo + h) / 2,
210 str(keypad_mode_name_get()));
211 #endif /* USB_ENABLE_HID */
212 #else /* HAVE_LCD_BITMAP */
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 */
220 screens[i].update();
222 /* force statusbar by ignoring the setting */
223 usb_bars |= VP_SB_IGNORE_SETTING(i);
226 viewportmanager_set_statusbar(usb_bars);
228 #ifdef SIMULATOR
229 if (button_get_w_tmo(HZ/2))
230 break;
231 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
232 #else
233 if (handle_usb_events())
234 break;
235 #endif /* SIMULATOR */
238 #ifdef USB_ENABLE_HID
239 if (global_settings.usb_keypad_mode != usb_keypad_mode)
241 global_settings.usb_keypad_mode = usb_keypad_mode;
242 settings_save();
244 #endif
246 #ifdef HAVE_TOUCHSCREEN
247 touchscreen_set_mode(old_mode);
248 #endif
250 #ifdef HAVE_LCD_CHARCELLS
251 status_set_usb(false);
252 #endif /* HAVE_LCD_CHARCELLS */
253 FOR_NB_SCREENS(i)
255 screens[i].backlight_on();
257 viewportmanager_set_statusbar(old_bars);
258 send_event(GUI_EVENT_REFRESH, NULL);
260 #endif /* USB_NONE */
263 #if (CONFIG_STORAGE & STORAGE_MMC)
264 int mmc_remove_request(void)
266 struct queue_event ev;
267 int i;
268 FOR_NB_SCREENS(i)
269 screens[i].clear_display();
270 splash(0, ID2P(LANG_REMOVE_MMC));
272 while (1)
274 queue_wait_w_tmo(&button_queue, &ev, HZ/2);
275 switch (ev.id)
277 case SYS_HOTSWAP_EXTRACTED:
278 return SYS_HOTSWAP_EXTRACTED;
280 case SYS_USB_DISCONNECTED:
281 return SYS_USB_DISCONNECTED;
285 #endif
287 /* the charging screen is only used for archos targets */
288 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING) && defined(CPU_SH)
290 #ifdef HAVE_LCD_BITMAP
291 static void charging_display_info(bool animate)
293 unsigned char charging_logo[36];
294 const int pox_x = (LCD_WIDTH - sizeof(charging_logo)) / 2;
295 const int pox_y = 32;
296 static unsigned phase = 3;
297 unsigned i;
299 #ifdef NEED_ATA_POWER_BATT_MEASURE
300 if (ide_powered()) /* FM and V2 can only measure when ATA power is on */
301 #endif
303 int battv = battery_voltage();
304 lcd_putsf(0, 7, " Batt: %d.%02dV %d%% ", battv / 1000,
305 (battv % 1000) / 10, battery_level());
308 #ifdef ARCHOS_RECORDER
309 lcd_puts(0, 2, "Charge mode:");
311 const char *s;
312 if (charge_state == CHARGING)
313 s = str(LANG_BATTERY_CHARGE);
314 else if (charge_state == TOPOFF)
315 s = str(LANG_BATTERY_TOPOFF_CHARGE);
316 else if (charge_state == TRICKLE)
317 s = str(LANG_BATTERY_TRICKLE_CHARGE);
318 else
319 s = "not charging";
321 lcd_puts(0, 3, s);
322 if (!charger_enabled())
323 animate = false;
324 #endif /* ARCHOS_RECORDER */
326 /* middle part */
327 memset(charging_logo+3, 0x00, 32);
328 charging_logo[0] = 0x3C;
329 charging_logo[1] = 0x24;
330 charging_logo[2] = charging_logo[35] = 0xFF;
332 if (!animate)
333 { /* draw the outline */
334 /* middle part */
335 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
336 sizeof(charging_logo), 8);
337 lcd_set_drawmode(DRMODE_FG);
338 /* upper line */
339 charging_logo[0] = charging_logo[1] = 0x00;
340 memset(charging_logo+2, 0x80, 34);
341 lcd_mono_bitmap(charging_logo, pox_x, pox_y, sizeof(charging_logo), 8);
342 /* lower line */
343 memset(charging_logo+2, 0x01, 34);
344 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 16,
345 sizeof(charging_logo), 8);
346 lcd_set_drawmode(DRMODE_SOLID);
348 else
349 { /* animate the middle part */
350 for (i = 3; i<MIN(sizeof(charging_logo)-1, phase); i++)
352 if ((i-phase) % 8 == 0)
353 { /* draw a "bubble" here */
354 unsigned bitpos;
355 bitpos = (phase + i/8) % 15; /* "bounce" effect */
356 if (bitpos > 7)
357 bitpos = 14 - bitpos;
358 charging_logo[i] = BIT_N(bitpos);
361 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
362 sizeof(charging_logo), 8);
363 phase++;
365 lcd_update();
367 #else /* not HAVE_LCD_BITMAP */
369 static unsigned long logo_chars[4];
370 static const unsigned char logo_pattern[] = {
371 0x07, 0x04, 0x1c, 0x14, 0x1c, 0x04, 0x07, 0, /* char 1 */
372 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 2 */
373 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 3 */
374 0x1f, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1f, 0, /* char 4 */
377 static void logo_lock_patterns(bool on)
379 int i;
381 if (on)
383 for (i = 0; i < 4; i++)
384 logo_chars[i] = lcd_get_locked_pattern();
386 else
388 for (i = 0; i < 4; i++)
389 lcd_unlock_pattern(logo_chars[i]);
393 static void charging_display_info(bool animate)
395 int battv;
396 unsigned i, ypos;
397 static unsigned phase = 3;
398 char buf[32];
400 battv = battery_voltage();
401 lcd_putsf(4, 1, " %d.%02dV", battv / 1000, (battv % 1000) / 10);
403 memcpy(buf, logo_pattern, 32); /* copy logo patterns */
405 if (!animate) /* build the screen */
407 lcd_double_height(false);
408 lcd_puts(0, 0, "[Charging]");
409 for (i = 0; i < 4; i++)
410 lcd_putc(i, 1, logo_chars[i]);
412 else /* animate the logo */
414 for (i = 3; i < MIN(19, phase); i++)
416 if ((i - phase) % 5 == 0)
417 { /* draw a "bubble" here */
418 ypos = (phase + i/5) % 9; /* "bounce" effect */
419 if (ypos > 4)
420 ypos = 8 - ypos;
421 buf[5 - ypos + 8 * (i/5)] |= 0x10u >> (i%5);
424 phase++;
427 for (i = 0; i < 4; i++)
428 lcd_define_pattern(logo_chars[i], buf + 8 * i);
430 lcd_update();
432 #endif /* (not) HAVE_LCD_BITMAP */
434 /* blocks while charging, returns on event:
435 1 if charger cable was removed
436 2 if Off/Stop key was pressed
437 3 if On key was pressed
438 4 if USB was connected */
440 int charging_screen(void)
442 unsigned int button;
443 int rc = 0;
445 ide_power_enable(false); /* power down the disk, else would be spinning */
447 lcd_clear_display();
448 backlight_set_timeout(global_settings.backlight_timeout);
449 #ifdef HAVE_REMOTE_LCD
450 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
451 #endif
452 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
454 #ifdef HAVE_LCD_CHARCELLS
455 logo_lock_patterns(true);
456 #endif
457 charging_display_info(false);
461 gui_syncstatusbar_draw(&statusbars, false);
462 charging_display_info(true);
463 button = get_action(CONTEXT_STD,HZ/3);
464 if (button == ACTION_STD_OK)
465 rc = 2;
466 else if (usb_detect() == USB_INSERTED)
467 rc = 3;
468 else if (!charger_inserted())
469 rc = 1;
470 } while (!rc);
472 #ifdef HAVE_LCD_CHARCELLS
473 logo_lock_patterns(false);
474 #endif
475 return rc;
477 #endif /* CONFIG_CHARGING && !HAVE_POWEROFF_WHILE_CHARGING && defined(CPU_SH) */
479 #if CONFIG_CHARGING
480 void charging_splash(void)
482 splash(2*HZ, str(LANG_BATTERY_CHARGE));
483 button_clear_queue();
485 #endif
488 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
490 /* little helper function for voice output */
491 static void say_time(int cursorpos, const struct tm *tm)
493 int value = 0;
494 int unit = 0;
496 if (!global_settings.talk_menu)
497 return;
499 switch(cursorpos)
501 case 0:
502 value = tm->tm_hour;
503 unit = UNIT_HOUR;
504 break;
505 case 1:
506 value = tm->tm_min;
507 unit = UNIT_MIN;
508 break;
509 case 2:
510 value = tm->tm_sec;
511 unit = UNIT_SEC;
512 break;
513 case 3:
514 value = tm->tm_year + 1900;
515 break;
516 case 5:
517 value = tm->tm_mday;
518 break;
521 if (cursorpos == 4) /* month */
522 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, false);
523 else
524 talk_value(value, unit, false);
528 #define INDEX_X 0
529 #define INDEX_Y 1
531 #define SEPARATOR ":"
533 #define IDX_HOURS 0
534 #define IDX_MINUTES 1
535 #define IDX_SECONDS 2
536 #define IDX_YEAR 3
537 #define IDX_MONTH 4
538 #define IDX_DAY 5
540 #define OFF_HOURS 0
541 #define OFF_MINUTES 3
542 #define OFF_SECONDS 6
543 #define OFF_YEAR 9
544 #define OFF_DAY 14
546 bool set_time_screen(const char* title, struct tm *tm)
548 bool done = false;
549 int cursorpos = 0;
550 unsigned int statusbar_height = 0;
551 unsigned char offsets_ptr[] =
552 { OFF_HOURS, OFF_MINUTES, OFF_SECONDS, OFF_YEAR, 0, OFF_DAY };
554 if (lang_is_rtl())
556 offsets_ptr[IDX_HOURS] = OFF_SECONDS;
557 offsets_ptr[IDX_SECONDS] = OFF_HOURS;
558 offsets_ptr[IDX_YEAR] = OFF_DAY;
559 offsets_ptr[IDX_DAY] = OFF_YEAR;
562 if(global_settings.statusbar)
563 statusbar_height = STATUSBAR_HEIGHT;
565 /* speak selection when screen is entered */
566 say_time(cursorpos, tm);
568 while (!done) {
569 int button;
570 unsigned int i, s, realyear, min, max;
571 unsigned char *ptr[6];
572 unsigned char buffer[20];
573 int *valptr = NULL;
574 static unsigned char daysinmonth[] =
575 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
577 /* for easy acess in the drawing loop */
578 for (i = 0; i < 6; i++)
579 ptr[i] = buffer + offsets_ptr[i];
580 ptr[IDX_MONTH] = str(LANG_MONTH_JANUARY + tm->tm_mon); /* month name */
582 /* calculate the number of days in febuary */
583 realyear = tm->tm_year + 1900;
584 if((realyear % 4 == 0 && !(realyear % 100 == 0)) || realyear % 400 == 0)
585 daysinmonth[1] = 29;
586 else
587 daysinmonth[1] = 28;
589 /* fix day if month or year changed */
590 if (tm->tm_mday > daysinmonth[tm->tm_mon])
591 tm->tm_mday = daysinmonth[tm->tm_mon];
593 /* calculate day of week */
594 set_day_of_week(tm);
596 /* put all the numbers we want from the tm struct into
597 an easily printable buffer */
598 snprintf(buffer, sizeof(buffer),
599 "%02d " "%02d " "%02d " "%04d " "%02d",
600 tm->tm_hour, tm->tm_min, tm->tm_sec,
601 tm->tm_year+1900, tm->tm_mday);
603 /* convert spaces in the buffer to '\0' to make it possible to work
604 directly on the buffer */
605 for(i=0; i < sizeof(buffer); i++)
607 if(buffer[i] == ' ')
608 buffer[i] = '\0';
611 FOR_NB_SCREENS(s)
613 int pos, nb_lines;
614 unsigned int separator_width, weekday_width;
615 unsigned int j, width, prev_line_height;
616 struct viewport viewports[NB_SCREENS];
617 /* 6 possible cursor possitions, 2 values stored for each: x, y */
618 unsigned int cursor[6][2];
619 struct viewport *vp = &viewports[s];
620 struct screen *screen = &screens[s];
621 static unsigned char rtl_idx[] =
622 { IDX_SECONDS, IDX_MINUTES, IDX_HOURS, IDX_DAY, IDX_MONTH, IDX_YEAR };
624 viewport_set_defaults(vp, s);
625 screen->set_viewport(vp);
626 nb_lines = viewport_get_nb_lines(vp);
628 /* minimum lines needed is 2 + title line */
629 if (nb_lines < 4)
631 vp->font = FONT_SYSFIXED;
632 nb_lines = viewport_get_nb_lines(vp);
635 /* recalculate the positions and offsets */
636 if (nb_lines >= 3)
637 screen->getstringsize(title, NULL, &prev_line_height);
638 else
639 prev_line_height = 0;
641 screen->getstringsize(SEPARATOR, &separator_width, NULL);
643 /* weekday */
644 screen->getstringsize(str(LANG_WEEKDAY_SUNDAY + tm->tm_wday),
645 &weekday_width, NULL);
646 screen->getstringsize(" ", &separator_width, NULL);
648 for(i=0, j=0; i < 6; i++)
650 if(i==3) /* second row */
652 j = weekday_width + separator_width;
653 prev_line_height *= 2;
655 screen->getstringsize(ptr[i], &width, NULL);
656 cursor[i][INDEX_Y] = prev_line_height + statusbar_height;
657 cursor[i][INDEX_X] = j;
658 j += width + separator_width;
661 /* draw the screen */
662 screen->set_viewport(vp);
663 screen->clear_viewport();
664 /* display the screen title */
665 screen->puts_scroll(0, 0, title);
667 /* these are not selectable, so we draw them outside the loop */
668 /* name of the week day */
669 screen->putsxy(0, cursor[3][INDEX_Y],
670 str(LANG_WEEKDAY_SUNDAY + tm->tm_wday));
672 pos = lang_is_rtl() ? rtl_idx[cursorpos] : cursorpos;
673 /* draw the selected item with drawmode set to
674 DRMODE_SOLID|DRMODE_INVERSEVID, all other selectable
675 items with drawmode DRMODE_SOLID */
676 for(i=0; i<6; i++)
678 if (pos == (int)i)
679 vp->drawmode = (DRMODE_SOLID|DRMODE_INVERSEVID);
681 screen->putsxy(cursor[i][INDEX_X],
682 cursor[i][INDEX_Y], ptr[i]);
684 vp->drawmode = DRMODE_SOLID;
686 screen->putsxy(cursor[i/4 +1][INDEX_X] - separator_width,
687 cursor[0][INDEX_Y], SEPARATOR);
690 /* print help text */
691 if (nb_lines > 4)
692 screen->puts(0, 4, str(LANG_TIME_SET_BUTTON));
693 if (nb_lines > 5)
694 screen->puts(0, 5, str(LANG_TIME_REVERT));
695 screen->update_viewport();
696 screen->set_viewport(NULL);
699 /* set the most common numbers */
700 min = 0;
701 max = 59;
702 /* calculate the minimum and maximum for the number under cursor */
703 switch(cursorpos) {
704 case 0: /* hour */
705 max = 23;
706 valptr = &tm->tm_hour;
707 break;
708 case 1: /* minute */
709 valptr = &tm->tm_min;
710 break;
711 case 2: /* second */
712 valptr = &tm->tm_sec;
713 break;
714 case 3: /* year */
715 min = 1;
716 max = 200;
717 valptr = &tm->tm_year;
718 break;
719 case 4: /* month */
720 max = 11;
721 valptr = &tm->tm_mon;
722 break;
723 case 5: /* day */
724 min = 1;
725 max = daysinmonth[tm->tm_mon];
726 valptr = &tm->tm_mday;
727 break;
730 #ifdef HAVE_TOUCHSCREEN
731 enum touchscreen_mode old_mode = touchscreen_get_mode();
733 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
734 #endif
735 button = get_action(CONTEXT_SETTINGS_TIME, TIMEOUT_BLOCK);
736 #ifdef HAVE_TOUCHSCREEN
737 touchscreen_set_mode(old_mode);
738 #endif
739 switch ( button ) {
740 case ACTION_STD_PREV:
741 cursorpos = clamp_value_wrap(--cursorpos, 5, 0);
742 say_time(cursorpos, tm);
743 break;
744 case ACTION_STD_NEXT:
745 cursorpos = clamp_value_wrap(++cursorpos, 5, 0);
746 say_time(cursorpos, tm);
747 break;
748 case ACTION_SETTINGS_INC:
749 case ACTION_SETTINGS_INCREPEAT:
750 *valptr = clamp_value_wrap(++(*valptr), max, min);
751 say_time(cursorpos, tm);
752 break;
753 case ACTION_SETTINGS_DEC:
754 case ACTION_SETTINGS_DECREPEAT:
755 *valptr = clamp_value_wrap(--(*valptr), max, min);
756 say_time(cursorpos, tm);
757 break;
759 case ACTION_STD_OK:
760 done = true;
761 break;
763 case ACTION_STD_CANCEL:
764 done = true;
765 tm->tm_year = -1;
766 break;
768 default:
769 if (default_event_handler(button) == SYS_USB_CONNECTED)
770 return true;
771 break;
774 return false;
776 #endif /* defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0) */
778 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
779 bool shutdown_screen(void)
781 int button;
782 bool done = false;
783 long time_entered = current_tick;
785 lcd_stop_scroll();
787 splash(0, str(LANG_CONFIRM_SHUTDOWN));
789 while(!done && TIME_BEFORE(current_tick,time_entered+HZ*2))
791 button = get_action(CONTEXT_STD,HZ);
792 switch(button)
794 case ACTION_STD_CANCEL:
795 sys_poweroff();
796 break;
798 /* do nothing here, because ACTION_NONE might be caused
799 * by timeout or button release. In case of timeout the loop
800 * is terminated by TIME_BEFORE */
801 case ACTION_NONE:
802 break;
804 default:
805 if(default_event_handler(button) == SYS_USB_CONNECTED)
806 return true;
807 done = true;
808 break;
811 return false;
813 #endif
815 static const int id3_headers[]=
817 LANG_ID3_TITLE,
818 LANG_ID3_ARTIST,
819 LANG_ID3_ALBUM,
820 LANG_ID3_ALBUMARTIST,
821 LANG_ID3_GROUPING,
822 LANG_ID3_DISCNUM,
823 LANG_ID3_TRACKNUM,
824 LANG_ID3_COMMENT,
825 LANG_ID3_GENRE,
826 LANG_ID3_YEAR,
827 LANG_ID3_LENGTH,
828 LANG_ID3_PLAYLIST,
829 LANG_ID3_BITRATE,
830 LANG_ID3_FREQUENCY,
831 #if CONFIG_CODEC == SWCODEC
832 LANG_ID3_TRACK_GAIN,
833 LANG_ID3_ALBUM_GAIN,
834 #endif
835 LANG_ID3_PATH,
838 struct id3view_info {
839 struct mp3entry* id3;
840 int count;
841 int info_id[ARRAYLEN(id3_headers)];
844 static const char* id3_get_info(int selected_item, void* data,
845 char *buffer, size_t buffer_len)
847 struct id3view_info *info = (struct id3view_info*)data;
848 struct mp3entry* id3 =info->id3;
849 int info_no=selected_item/2;
850 if(!(selected_item%2))
851 {/* header */
852 return(str(id3_headers[info->info_id[info_no]]));
854 else
855 {/* data */
857 char * val=NULL;
858 switch(info->info_id[info_no])
860 case 0:/*LANG_ID3_TITLE*/
861 val=id3->title;
862 break;
863 case 1:/*LANG_ID3_ARTIST*/
864 val=id3->artist;
865 break;
866 case 2:/*LANG_ID3_ALBUM*/
867 val=id3->album;
868 break;
869 case 3:/*LANG_ID3_ALBUMARTIST*/
870 val=id3->albumartist;
871 break;
872 case 4:/*LANG_ID3_GROUPING*/
873 val=id3->grouping;
874 break;
875 case 5:/*LANG_ID3_DISCNUM*/
876 if (id3->disc_string)
877 val = id3->disc_string;
878 else if (id3->discnum)
880 snprintf(buffer, buffer_len, "%d", id3->discnum);
881 val = buffer;
883 break;
884 case 6:/*LANG_ID3_TRACKNUM*/
885 if (id3->track_string)
886 val = id3->track_string;
887 else if (id3->tracknum)
889 snprintf(buffer, buffer_len, "%d", id3->tracknum);
890 val = buffer;
892 break;
893 case 7:/*LANG_ID3_COMMENT*/
894 val=id3->comment;
895 break;
896 case 8:/*LANG_ID3_GENRE*/
897 val = id3->genre_string;
898 break;
899 case 9:/*LANG_ID3_YEAR*/
900 if (id3->year_string)
901 val = id3->year_string;
902 else if (id3->year)
904 snprintf(buffer, buffer_len, "%d", id3->year);
905 val = buffer;
907 break;
908 case 10:/*LANG_ID3_LENGTH*/
909 format_time(buffer, buffer_len, id3->length);
910 val=buffer;
911 break;
912 case 11:/*LANG_ID3_PLAYLIST*/
913 snprintf(buffer, buffer_len, "%d/%d",
914 playlist_get_display_index(), playlist_amount());
915 val=buffer;
916 break;
917 case 12:/*LANG_ID3_BITRATE*/
918 snprintf(buffer, buffer_len, "%d kbps%s", id3->bitrate,
919 id3->vbr ? str(LANG_ID3_VBR) : (const unsigned char*) "");
920 val=buffer;
921 break;
922 case 13:/*LANG_ID3_FREQUENCY*/
923 snprintf(buffer, buffer_len, "%ld Hz", id3->frequency);
924 val=buffer;
925 break;
926 #if CONFIG_CODEC == SWCODEC
927 case 14:/*LANG_ID3_TRACK_GAIN*/
928 val=id3->track_gain_string;
929 break;
930 case 15:/*LANG_ID3_ALBUM_GAIN*/
931 val=id3->album_gain_string;
932 break;
933 case 16:/*LANG_ID3_PATH*/
934 #else
935 case 14:/*LANG_ID3_PATH*/
936 #endif
937 val=id3->path;
938 break;
940 return val && *val ? val : NULL;
944 bool browse_id3(void)
946 struct gui_synclist id3_lists;
947 struct mp3entry* id3 = audio_current_track();
948 int key;
949 unsigned int i;
950 struct id3view_info info;
951 info.count = 0;
952 info.id3 = id3;
953 for (i = 0; i < ARRAYLEN(id3_headers); i++)
955 char temp[8];
956 info.info_id[i] = i;
957 if (id3_get_info((i*2)+1, &info, temp, 8) != NULL)
958 info.info_id[info.count++] = i;
961 gui_synclist_init(&id3_lists, &id3_get_info, &info, true, 2, NULL);
962 gui_synclist_set_nb_items(&id3_lists, info.count*2);
963 gui_synclist_draw(&id3_lists);
964 while (true) {
965 key = get_action(CONTEXT_LIST,HZ/2);
966 if(key!=ACTION_NONE && key!=ACTION_UNKNOWN
967 && !gui_synclist_do_button(&id3_lists, &key,LIST_WRAP_UNLESS_HELD))
969 return(default_event_handler(key) == SYS_USB_CONNECTED);
974 static const char* runtime_get_data(int selected_item, void* data,
975 char* buffer, size_t buffer_len)
977 (void)data;
978 int t;
979 switch (selected_item)
981 case 0: return str(LANG_RUNNING_TIME);
982 case 1: t = global_status.runtime; break;
983 case 2: return str(LANG_TOP_TIME);
984 case 3: t = global_status.topruntime; break;
985 default:
986 return "";
989 snprintf(buffer, buffer_len, "%dh %dm %ds",
990 t / 3600, (t % 3600) / 60, t % 60);
991 return buffer;
994 static int runtime_speak_data(int selected_item, void* data)
996 (void) data;
997 talk_ids(false,
998 (selected_item < 2) ? LANG_RUNNING_TIME : LANG_TOP_TIME,
999 TALK_ID((selected_item < 2) ? global_status.runtime
1000 : global_status.topruntime, UNIT_TIME));
1001 return 0;
1005 bool view_runtime(void)
1007 static const char *lines[]={ID2P(LANG_CLEAR_TIME)};
1008 static const struct text_message message={lines, 1};
1010 struct gui_synclist lists;
1011 int action;
1012 gui_synclist_init(&lists, runtime_get_data, NULL, false, 2, NULL);
1013 #if !defined(HAVE_LCD_CHARCELLS)
1014 gui_synclist_set_title(&lists, str(LANG_RUNNING_TIME), NOICON);
1015 #else
1016 gui_synclist_set_title(&lists, NULL, NOICON);
1017 #endif
1018 if(global_settings.talk_menu)
1019 gui_synclist_set_voice_callback(&lists, runtime_speak_data);
1020 gui_synclist_set_icon_callback(&lists, NULL);
1021 gui_synclist_set_nb_items(&lists, 4);
1022 gui_synclist_speak_item(&lists);
1023 while(1)
1025 #if CONFIG_CHARGING
1026 if (charger_inserted())
1028 global_status.runtime = 0;
1030 else
1031 #endif
1033 global_status.runtime += ((current_tick - lasttime) / HZ);
1035 lasttime = current_tick;
1036 gui_synclist_draw(&lists);
1037 list_do_action(CONTEXT_STD, HZ,
1038 &lists, &action, LIST_WRAP_UNLESS_HELD);
1039 if(action == ACTION_STD_CANCEL)
1040 break;
1041 if(action == ACTION_STD_OK) {
1042 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
1044 if (!(gui_synclist_get_sel_pos(&lists)/2))
1045 global_status.runtime = 0;
1046 else
1047 global_status.topruntime = 0;
1048 gui_synclist_speak_item(&lists);
1051 if(default_event_handler(action) == SYS_USB_CONNECTED)
1052 return true;
1054 return false;
1057 #ifdef HAVE_TOUCHSCREEN
1058 static int get_sample(struct touchscreen_calibration *cal, int x, int y, int i,
1059 struct screen* screen)
1061 int action;
1062 short ts_x, ts_y;
1064 /* Draw a cross */
1065 screen->drawline(x - 10, y, x - 2, y);
1066 screen->drawline(x + 2, y, x + 10, y);
1067 screen->drawline(x, y - 10, x, y - 2);
1068 screen->drawline(x, y + 2, x, y + 10);
1069 screen->update();
1071 /* Wait for a touchscreen press */
1072 while(true)
1074 action = get_action(CONTEXT_STD, TIMEOUT_BLOCK);
1075 if(action == ACTION_TOUCHSCREEN)
1077 if(action_get_touchscreen_press(&ts_x, &ts_y) == BUTTON_REL)
1078 break;
1080 else if(action == ACTION_STD_CANCEL)
1081 return -1;
1084 cal->x[i][0] = ts_x;
1085 cal->y[i][0] = ts_y;
1086 cal->x[i][1] = x;
1087 cal->y[i][1] = y;
1089 return 0;
1093 int calibrate(void)
1095 short points[3][2] = {
1096 {LCD_WIDTH/10, LCD_HEIGHT/10},
1097 {7*LCD_WIDTH/8, LCD_HEIGHT/2},
1098 {LCD_WIDTH/2, 7*LCD_HEIGHT/8}
1100 struct screen* screen = &screens[SCREEN_MAIN];
1101 enum touchscreen_mode old_mode = touchscreen_get_mode();
1102 struct touchscreen_calibration cal;
1103 int i, ret = 0;
1104 int statusbar = global_settings.statusbar; /* hide the statusbar */
1105 global_settings.statusbar = STATUSBAR_OFF;
1107 touchscreen_disable_mapping(); /* set raw mode */
1108 touchscreen_set_mode(TOUCHSCREEN_POINT);
1109 for(i=0; i<3; i++)
1111 screen->clear_display();
1113 if(get_sample(&cal, points[i][0], points[i][1], i, screen))
1115 ret = -1;
1116 break;
1120 if(ret == 0)
1121 touchscreen_calibrate(&cal);
1122 else
1123 touchscreen_reset_mapping();
1124 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1125 touchscreen_set_mode(old_mode);
1126 global_settings.statusbar = statusbar;
1128 return ret;
1131 int reset_mapping(void)
1133 touchscreen_reset_mapping();
1134 memcpy(&global_settings.ts_calibration_data, &calibration_parameters, sizeof(struct touchscreen_parameter));
1135 return 0;
1137 #endif