Revert the recent change to bufread()
[Rockbox.git] / apps / screens.c
blob703ec7badb630b3591547d2bf77573de4b3ff2b9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdbool.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include "backlight.h"
24 #include "action.h"
25 #include "lcd.h"
26 #ifdef HAVE_REMOTE_LCD
27 #include "lcd-remote.h"
28 #endif
29 #include "lang.h"
30 #include "icons.h"
31 #include "font.h"
32 #include "audio.h"
33 #include "mp3_playback.h"
34 #include "usb.h"
35 #include "settings.h"
36 #include "status.h"
37 #include "playlist.h"
38 #include "sprintf.h"
39 #include "kernel.h"
40 #include "power.h"
41 #include "system.h"
42 #include "powermgmt.h"
43 #include "adc.h"
44 #include "action.h"
45 #include "talk.h"
46 #include "misc.h"
47 #include "id3.h"
48 #include "screens.h"
49 #include "debug.h"
50 #include "led.h"
51 #include "sound.h"
52 #include "splash.h"
53 #include "statusbar.h"
54 #include "screen_access.h"
55 #include "quickscreen.h"
56 #include "pcmbuf.h"
57 #include "list.h"
58 #include "yesno.h"
60 #ifdef HAVE_LCD_BITMAP
61 #include <bitmaps/usblogo.h>
62 #endif
64 #ifdef HAVE_REMOTE_LCD
65 #include <bitmaps/remote_usblogo.h>
66 #endif
68 #ifdef HAVE_MMC
69 #include "ata_mmc.h"
70 #endif
71 #if CONFIG_CODEC == SWCODEC
72 #include "dsp.h"
73 #endif
75 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
76 #include "backdrop.h"
77 #endif
79 #ifdef HAVE_LCD_BITMAP
80 #define SCROLLBAR_WIDTH 6
81 #endif
83 void usb_screen(void)
85 #ifdef USB_NONE
86 /* nothing here! */
87 #else
88 int i;
89 bool statusbar = global_settings.statusbar; /* force the statusbar */
90 global_settings.statusbar = true;
91 #if LCD_DEPTH > 1
92 show_main_backdrop();
93 #endif
94 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
95 show_remote_main_backdrop();
96 #endif
97 FOR_NB_SCREENS(i)
99 screens[i].backlight_on();
100 screens[i].clear_display();
101 #if NB_SCREENS > 1
102 if (i == SCREEN_REMOTE)
104 screens[i].bitmap(remote_usblogo,
105 (LCD_REMOTE_WIDTH-BMPWIDTH_remote_usblogo),
106 (LCD_REMOTE_HEIGHT-BMPHEIGHT_remote_usblogo)/2,
107 BMPWIDTH_remote_usblogo, BMPHEIGHT_remote_usblogo);
109 else
111 #endif
112 #ifdef HAVE_LCD_BITMAP
113 screens[i].transparent_bitmap(usblogo,
114 (LCD_WIDTH-BMPWIDTH_usblogo),
115 (LCD_HEIGHT-BMPHEIGHT_usblogo)/2,
116 BMPWIDTH_usblogo, BMPHEIGHT_usblogo);
117 #else
118 screens[i].double_height(false);
119 screens[i].puts_scroll(0, 0, "[USB Mode]");
120 status_set_param(false);
121 status_set_audio(false);
122 status_set_usb(true);
123 #endif /* HAVE_LCD_BITMAP */
124 #if NB_SCREENS > 1
126 #endif
127 screens[i].update();
130 gui_syncstatusbar_draw(&statusbars, true);
131 #ifdef SIMULATOR
132 while (button_get(true) & BUTTON_REL);
133 #else
134 usb_acknowledge(SYS_USB_CONNECTED_ACK);
135 while(usb_wait_for_disconnect_w_tmo(&button_queue, HZ)) {
136 if(usb_inserted()) {
137 #ifdef HAVE_MMC /* USB-MMC bridge can report activity */
138 led(mmc_usb_active(HZ));
139 #endif /* HAVE_MMC */
140 gui_syncstatusbar_draw(&statusbars, false);
143 #endif /* SIMULATOR */
144 #ifdef HAVE_LCD_CHARCELLS
145 status_set_usb(false);
146 #endif /* HAVE_LCD_CHARCELLS */
147 FOR_NB_SCREENS(i)
148 screens[i].backlight_on();
149 global_settings.statusbar = statusbar;
150 #endif /* USB_NONE */
153 #ifdef HAVE_MMC
154 int mmc_remove_request(void)
156 struct queue_event ev;
157 int i;
158 FOR_NB_SCREENS(i)
159 screens[i].clear_display();
160 gui_syncsplash(1, str(LANG_REMOVE_MMC));
161 if (global_settings.talk_menu)
162 talk_id(LANG_REMOVE_MMC, false);
164 while (1)
166 queue_wait_w_tmo(&button_queue, &ev, HZ/2);
167 switch (ev.id)
169 case SYS_HOTSWAP_EXTRACTED:
170 return SYS_HOTSWAP_EXTRACTED;
172 case SYS_USB_DISCONNECTED:
173 return SYS_USB_DISCONNECTED;
177 #endif
179 /* the charging screen is only used for archos targets */
180 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING) && defined(CPU_SH)
182 #ifdef HAVE_LCD_BITMAP
183 static void charging_display_info(bool animate)
185 unsigned char charging_logo[36];
186 const int pox_x = (LCD_WIDTH - sizeof(charging_logo)) / 2;
187 const int pox_y = 32;
188 static unsigned phase = 3;
189 unsigned i;
190 char buf[32];
191 (void)buf;
193 #ifdef NEED_ATA_POWER_BATT_MEASURE
194 if (ide_powered()) /* FM and V2 can only measure when ATA power is on */
195 #endif
197 int battv = battery_voltage();
198 snprintf(buf, 32, " Batt: %d.%02dV %d%% ", battv / 1000,
199 (battv % 1000) / 10, battery_level());
200 lcd_puts(0, 7, buf);
203 #if CONFIG_CHARGING == CHARGING_CONTROL
205 snprintf(buf, 32, "Charge mode:");
206 lcd_puts(0, 2, buf);
208 if (charge_state == CHARGING)
209 snprintf(buf, 32, str(LANG_BATTERY_CHARGE));
210 else if (charge_state == TOPOFF)
211 snprintf(buf, 32, str(LANG_BATTERY_TOPOFF_CHARGE));
212 else if (charge_state == TRICKLE)
213 snprintf(buf, 32, str(LANG_BATTERY_TRICKLE_CHARGE));
214 else
215 snprintf(buf, 32, "not charging");
217 lcd_puts(0, 3, buf);
218 if (!charger_enabled)
219 animate = false;
220 #endif /* CONFIG_CHARGING == CHARGING_CONTROL */
223 /* middle part */
224 memset(charging_logo+3, 0x00, 32);
225 charging_logo[0] = 0x3C;
226 charging_logo[1] = 0x24;
227 charging_logo[2] = charging_logo[35] = 0xFF;
229 if (!animate)
230 { /* draw the outline */
231 /* middle part */
232 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8, sizeof(charging_logo), 8);
233 lcd_set_drawmode(DRMODE_FG);
234 /* upper line */
235 charging_logo[0] = charging_logo[1] = 0x00;
236 memset(charging_logo+2, 0x80, 34);
237 lcd_mono_bitmap(charging_logo, pox_x, pox_y, sizeof(charging_logo), 8);
238 /* lower line */
239 memset(charging_logo+2, 0x01, 34);
240 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 16, sizeof(charging_logo), 8);
241 lcd_set_drawmode(DRMODE_SOLID);
243 else
244 { /* animate the middle part */
245 for (i = 3; i<MIN(sizeof(charging_logo)-1, phase); i++)
247 if ((i-phase) % 8 == 0)
248 { /* draw a "bubble" here */
249 unsigned bitpos;
250 bitpos = (phase + i/8) % 15; /* "bounce" effect */
251 if (bitpos > 7)
252 bitpos = 14 - bitpos;
253 charging_logo[i] = 0x01 << bitpos;
256 lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8, sizeof(charging_logo), 8);
257 phase++;
259 lcd_update();
261 #else /* not HAVE_LCD_BITMAP */
263 static unsigned long logo_chars[4];
264 static const unsigned char logo_pattern[] = {
265 0x07, 0x04, 0x1c, 0x14, 0x1c, 0x04, 0x07, 0, /* char 1 */
266 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 2 */
267 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0, /* char 3 */
268 0x1f, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1f, 0, /* char 4 */
271 static void logo_lock_patterns(bool on)
273 int i;
275 if (on)
277 for (i = 0; i < 4; i++)
278 logo_chars[i] = lcd_get_locked_pattern();
280 else
282 for (i = 0; i < 4; i++)
283 lcd_unlock_pattern(logo_chars[i]);
287 static void charging_display_info(bool animate)
289 int battv;
290 unsigned i, ypos;
291 static unsigned phase = 3;
292 char buf[32];
294 battv = battery_voltage();
295 snprintf(buf, sizeof(buf), " %d.%02dV", battv / 1000, (battv % 1000) / 10);
296 lcd_puts(4, 1, buf);
298 memcpy(buf, logo_pattern, 32); /* copy logo patterns */
300 if (!animate) /* build the screen */
302 lcd_double_height(false);
303 lcd_puts(0, 0, "[Charging]");
304 for (i = 0; i < 4; i++)
305 lcd_putc(i, 1, logo_chars[i]);
307 else /* animate the logo */
309 for (i = 3; i < MIN(19, phase); i++)
311 if ((i - phase) % 5 == 0)
312 { /* draw a "bubble" here */
313 ypos = (phase + i/5) % 9; /* "bounce" effect */
314 if (ypos > 4)
315 ypos = 8 - ypos;
316 buf[5 - ypos + 8 * (i/5)] |= 0x10 >> (i%5);
319 phase++;
322 for (i = 0; i < 4; i++)
323 lcd_define_pattern(logo_chars[i], buf + 8 * i);
325 lcd_update();
327 #endif /* (not) HAVE_LCD_BITMAP */
329 /* blocks while charging, returns on event:
330 1 if charger cable was removed
331 2 if Off/Stop key was pressed
332 3 if On key was pressed
333 4 if USB was connected */
335 int charging_screen(void)
337 unsigned int button;
338 int rc = 0;
340 ide_power_enable(false); /* power down the disk, else would be spinning */
342 lcd_clear_display();
343 backlight_set_timeout(global_settings.backlight_timeout);
344 #ifdef HAVE_REMOTE_LCD
345 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
346 #endif
347 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
348 gui_syncstatusbar_draw(&statusbars, true);
350 #ifdef HAVE_LCD_CHARCELLS
351 logo_lock_patterns(true);
352 #endif
353 charging_display_info(false);
357 gui_syncstatusbar_draw(&statusbars, false);
358 charging_display_info(true);
359 button = get_action(CONTEXT_STD,HZ/3);
360 if (button == ACTION_STD_OK)
361 rc = 2;
362 else if (usb_detect())
363 rc = 3;
364 else if (!charger_inserted())
365 rc = 1;
366 } while (!rc);
368 #ifdef HAVE_LCD_CHARCELLS
369 logo_lock_patterns(false);
370 #endif
371 return rc;
373 #endif /* CONFIG_CHARGING && !HAVE_POWEROFF_WHILE_CHARGING && defined(CPU_SH) */
375 #ifdef HAVE_PITCHSCREEN
377 #define PITCH_MAX 2000
378 #define PITCH_MIN 500
379 #define PITCH_SMALL_DELTA 1
380 #define PITCH_BIG_DELTA 10
381 #define PITCH_NUDGE_DELTA 20
383 #define PITCH_MODE_ABSOLUTE 1
384 #define PITCH_MODE_SEMITONE -PITCH_MODE_ABSOLUTE
386 static int pitch_mode = PITCH_MODE_ABSOLUTE; /* 1 - absolute, -1 - semitone */
388 /* returns:
389 0 if no key was pressed
390 1 if USB was connected */
392 static void pitch_screen_draw(struct screen *display, int pitch, int pitch_mode)
394 unsigned char* ptr;
395 unsigned char buf[32];
396 int w, h;
398 display->clear_display();
400 if (display->nb_lines < 4) /* very small screen, just show the pitch value */
402 w = snprintf((char *)buf, sizeof(buf), "%s: %d.%d%%",str(LANG_PITCH),
403 pitch / 10, pitch % 10 );
404 display->putsxy((display->width-(w*display->char_width))/2,
405 display->nb_lines/2,buf);
407 else /* bigger screen, show everything... */
410 /* UP: Pitch Up */
411 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
412 ptr = str(LANG_PITCH_UP);
413 } else {
414 ptr = str(LANG_PITCH_UP_SEMITONE);
416 display->getstringsize(ptr,&w,&h);
417 display->putsxy((display->width-w)/2, 0, ptr);
418 display->mono_bitmap(bitmap_icons_7x8[Icon_UpArrow],
419 display->width/2 - 3, h, 7, 8);
421 /* DOWN: Pitch Down */
422 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
423 ptr = str(LANG_PITCH_DOWN);
424 } else {
425 ptr = str(LANG_PITCH_DOWN_SEMITONE);
427 display->getstringsize(ptr,&w,&h);
428 display->putsxy((display->width-w)/2, display->height - h, ptr);
429 display->mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
430 display->width/2 - 3, display->height - h*2, 7, 8);
432 /* RIGHT: +2% */
433 ptr = "+2%";
434 display->getstringsize(ptr,&w,&h);
435 display->putsxy(display->width-w, (display->height-h)/2, ptr);
436 display->mono_bitmap(bitmap_icons_7x8[Icon_FastForward],
437 display->width-w-8, (display->height-h)/2, 7, 8);
439 /* LEFT: -2% */
440 ptr = "-2%";
441 display->getstringsize(ptr,&w,&h);
442 display->putsxy(0, (display->height-h)/2, ptr);
443 display->mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
444 w+1, (display->height-h)/2, 7, 8);
446 /* "Pitch" */
447 snprintf((char *)buf, sizeof(buf), str(LANG_PITCH));
448 display->getstringsize(buf,&w,&h);
449 display->putsxy((display->width-w)/2, (display->height/2)-h, buf);
450 /* "XX.X%" */
451 snprintf((char *)buf, sizeof(buf), "%d.%d%%",
452 pitch / 10, pitch % 10 );
453 display->getstringsize(buf,&w,&h);
454 display->putsxy((display->width-w)/2, display->height/2, buf);
457 display->update();
460 static int pitch_increase(int pitch, int delta,
461 bool allow_cutoff, bool redraw_screens) {
462 int new_pitch;
463 int i;
465 if (delta < 0) {
466 if (pitch + delta >= PITCH_MIN) {
467 new_pitch = pitch + delta;
468 } else {
469 if (!allow_cutoff) {
470 return pitch;
472 new_pitch = PITCH_MIN;
474 } else if (delta > 0) {
475 if (pitch + delta <= PITCH_MAX) {
476 new_pitch = pitch + delta;
477 } else {
478 if (!allow_cutoff) {
479 return pitch;
481 new_pitch = PITCH_MAX;
483 } else {
484 /* delta == 0 -> no real change */
485 return pitch;
487 sound_set_pitch(new_pitch);
489 if (redraw_screens) {
490 FOR_NB_SCREENS(i)
491 pitch_screen_draw(&screens[i], pitch, pitch_mode);
494 return new_pitch;
497 /* Factor for changing the pitch one half tone up.
498 The exact value is 2^(1/12) = 1.05946309436
499 But we use only integer arithmetics, so take
500 rounded factor multiplied by 10^5=100,000. This is
501 enough to get the same promille values as if we
502 had used floating point (checked with a spread
503 sheet).
505 #define PITCH_SEMITONE_FACTOR 105946L
507 /* Some helpful constants. K is the scaling factor for SEMITONE.
508 N is for more accurate rounding
509 KN is K * N
511 #define PITCH_K_FCT 100000UL
512 #define PITCH_N_FCT 10
513 #define PITCH_KN_FCT 1000000UL
515 static int pitch_increase_semitone(int pitch, bool up) {
516 uint32_t tmp;
517 uint32_t round_fct; /* How much to scale down at the end */
518 tmp = pitch;
519 if (up) {
520 tmp = tmp * PITCH_SEMITONE_FACTOR;
521 round_fct = PITCH_K_FCT;
522 } else {
523 tmp = (tmp * PITCH_KN_FCT) / PITCH_SEMITONE_FACTOR;
524 round_fct = PITCH_N_FCT;
526 /* Scaling down with rounding */
527 tmp = (tmp + round_fct / 2) / round_fct;
528 return pitch_increase(pitch, tmp - pitch, false, false);
531 bool pitch_screen(void)
533 int button;
534 int pitch = sound_get_pitch();
535 int new_pitch;
536 bool nudged = false;
537 bool exit = false;
538 int i;
540 #if CONFIG_CODEC == SWCODEC
541 pcmbuf_set_low_latency(true);
542 #endif
544 while (!exit)
546 FOR_NB_SCREENS(i)
547 pitch_screen_draw(&screens[i], pitch, pitch_mode);
549 button = get_action(CONTEXT_PITCHSCREEN,TIMEOUT_BLOCK);
550 switch (button) {
551 case ACTION_PS_INC_SMALL:
552 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
553 pitch = pitch_increase(pitch, PITCH_SMALL_DELTA, true, false);
554 } else {
555 pitch = pitch_increase_semitone(pitch, true);
557 break;
559 case ACTION_PS_INC_BIG:
560 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
561 pitch = pitch_increase(pitch, PITCH_BIG_DELTA, true, false);
563 break;
565 case ACTION_PS_DEC_SMALL:
566 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
567 pitch = pitch_increase(pitch, -PITCH_SMALL_DELTA, true, false);
568 } else {
569 pitch = pitch_increase_semitone(pitch, false);
571 break;
573 case ACTION_PS_DEC_BIG:
574 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
575 pitch = pitch_increase(pitch, -PITCH_BIG_DELTA, true, false);
577 break;
579 case ACTION_PS_NUDGE_RIGHT:
580 new_pitch = pitch_increase(pitch, PITCH_NUDGE_DELTA, false, true);
581 nudged = (new_pitch != pitch);
582 pitch = new_pitch;
583 break;
584 case ACTION_PS_NUDGE_RIGHTOFF:
585 if (nudged) {
586 pitch = pitch_increase(pitch, -PITCH_NUDGE_DELTA, false, false);
588 nudged = false;
589 break;
591 case ACTION_PS_NUDGE_LEFT:
592 new_pitch = pitch_increase(pitch, -PITCH_NUDGE_DELTA, false, true);
593 nudged = (new_pitch != pitch);
594 pitch = new_pitch;
595 break;
597 case ACTION_PS_NUDGE_LEFTOFF:
598 if (nudged) {
599 pitch = pitch_increase(pitch, PITCH_NUDGE_DELTA, false, false);
601 nudged = false;
602 break;
604 case ACTION_PS_RESET:
605 pitch = 1000;
606 sound_set_pitch( pitch );
607 break;
609 case ACTION_PS_TOGGLE_MODE:
610 pitch_mode = -pitch_mode;
611 break;
613 case ACTION_PS_EXIT:
614 exit = true;
615 break;
617 default:
618 if(default_event_handler(button) == SYS_USB_CONNECTED)
619 return 1;
620 break;
623 #if CONFIG_CODEC == SWCODEC
624 pcmbuf_set_low_latency(false);
625 #endif
626 lcd_setfont(FONT_UI);
627 return 0;
629 #endif /* HAVE_PITCHSCREEN */
631 #ifdef HAVE_QUICKSCREEN
633 #define bool_to_int(b)\
634 b?1:0
635 #define int_to_bool(i)\
636 i==0?false:true
638 static void quick_screen_quick_apply(struct gui_quickscreen *qs)
640 global_settings.playlist_shuffle=int_to_bool(qs->left_option->option);
641 global_settings.dirfilter=qs->bottom_option->option;
642 global_settings.repeat_mode=qs->right_option->option;
645 bool quick_screen_quick(int button_enter)
647 bool res, oldshuffle;
648 struct option_select left_option;
649 struct option_select bottom_option;
650 struct option_select right_option;
651 int oldrepeat, old_x_margin, old_y_margin;
653 static const struct opt_items left_items[] = {
654 [0]={ STR(LANG_SYSFONT_OFF) },
655 [1]={ STR(LANG_SYSFONT_ON) }
657 static const struct opt_items bottom_items[] = {
658 [SHOW_ALL]={ STR(LANG_SYSFONT_ALL) },
659 [SHOW_SUPPORTED]={ STR(LANG_SYSFONT_FILTER_SUPPORTED) },
660 [SHOW_MUSIC]={ STR(LANG_SYSFONT_FILTER_MUSIC) },
661 [SHOW_PLAYLIST]={ STR(LANG_SYSFONT_FILTER_PLAYLIST) },
663 static const struct opt_items right_items[] = {
664 [REPEAT_OFF]={ STR(LANG_SYSFONT_OFF) },
665 [REPEAT_ALL]={ STR(LANG_SYSFONT_ALL) },
666 [REPEAT_ONE]={ STR(LANG_SYSFONT_REPEAT_ONE) },
667 [REPEAT_SHUFFLE]={ STR(LANG_SYSFONT_SHUFFLE) },
668 #ifdef AB_REPEAT_ENABLE
669 [REPEAT_AB]={ STR(LANG_SYSFONT_REPEAT_AB) }
670 #endif
672 struct gui_quickscreen qs;
674 old_x_margin = lcd_getxmargin();
675 old_y_margin = lcd_getymargin();
676 lcd_setmargins(0, 0);
678 option_select_init_items(&left_option,
679 (char *)str(LANG_SYSFONT_SHUFFLE),
680 bool_to_int(global_settings.playlist_shuffle),
681 left_items,
683 option_select_init_items(&bottom_option,
684 (char *)str(LANG_SYSFONT_FILTER),
685 global_settings.dirfilter,
686 bottom_items,
687 sizeof(bottom_items)/sizeof(struct opt_items));
688 option_select_init_items(&right_option,
689 (char *)str(LANG_SYSFONT_REPEAT),
690 global_settings.repeat_mode,
691 right_items,
692 sizeof(right_items)/sizeof(struct opt_items));
694 gui_quickscreen_init(&qs, &left_option, &bottom_option, &right_option,
695 &quick_screen_quick_apply);
696 oldrepeat=global_settings.repeat_mode;
697 oldshuffle=global_settings.playlist_shuffle;
698 res=gui_syncquickscreen_run(&qs, button_enter);
699 if(!res)
701 if ( oldrepeat != global_settings.repeat_mode &&
702 (audio_status() & AUDIO_STATUS_PLAY) )
703 audio_flush_and_reload_tracks();
704 if(oldshuffle != global_settings.playlist_shuffle
705 && audio_status() & AUDIO_STATUS_PLAY)
707 #if CONFIG_CODEC == SWCODEC
708 dsp_set_replaygain();
709 #endif
710 if (global_settings.playlist_shuffle)
711 playlist_randomise(NULL, current_tick, true);
712 else
713 playlist_sort(NULL, true);
715 settings_save();
717 lcd_setmargins(old_x_margin, old_y_margin);
718 return(res);
721 #ifdef BUTTON_F3
722 static void quick_screen_f3_apply(struct gui_quickscreen *qs)
724 global_settings.scrollbar=int_to_bool(qs->left_option->option);
726 global_settings.flip_display=int_to_bool(qs->bottom_option->option);
727 button_set_flip(global_settings.flip_display);
728 lcd_set_flip(global_settings.flip_display);
730 global_settings.statusbar=int_to_bool(qs->right_option->option);
731 gui_syncstatusbar_draw(&statusbars, true);
734 bool quick_screen_f3(int button_enter)
736 bool res;
737 struct option_select left_option;
738 struct option_select bottom_option;
739 struct option_select right_option;
740 int old_x_margin, old_y_margin;
742 static const struct opt_items onoff_items[] = {
743 [0]={ STR(LANG_SYSFONT_OFF) },
744 [1]={ STR(LANG_SYSFONT_ON) }
746 static const struct opt_items yesno_items[] = {
747 [0]={ STR(LANG_SYSFONT_SET_BOOL_NO) },
748 [1]={ STR(LANG_SYSFONT_SET_BOOL_YES) }
751 struct gui_quickscreen qs;
753 old_x_margin = lcd_getxmargin();
754 old_y_margin = lcd_getymargin();
755 lcd_setmargins(0, 0);
757 option_select_init_items(&left_option,
758 str(LANG_SYSFONT_SCROLL_BAR),
759 bool_to_int(global_settings.scrollbar),
760 onoff_items,
762 option_select_init_items(&bottom_option,
763 str(LANG_SYSFONT_FLIP_DISPLAY),
764 bool_to_int(global_settings.flip_display),
765 yesno_items,
767 option_select_init_items(&right_option,
768 str(LANG_SYSFONT_STATUS_BAR),
769 bool_to_int(global_settings.statusbar),
770 onoff_items,
772 gui_quickscreen_init(&qs, &left_option, &bottom_option, &right_option,
773 &quick_screen_f3_apply);
774 res=gui_syncquickscreen_run(&qs, button_enter);
775 if(!res)
776 settings_save();
777 lcd_setmargins(old_x_margin, old_y_margin);
778 return(res);
780 #endif /* BUTTON_F3 */
781 #endif /* CONFIG_KEYPAD in (RECORDER_PAD |IRIVER_H100_PAD | IRIVER_H300_PAD) */
783 #if CONFIG_CHARGING
784 void charging_splash(void)
786 gui_syncsplash(2*HZ, (unsigned char *)str(LANG_BATTERY_CHARGE));
787 button_clear_queue();
789 #endif
792 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
794 /* little helper function for voice output */
795 static void say_time(int cursorpos, const struct tm *tm)
797 int value = 0;
798 int unit = 0;
800 if (!global_settings.talk_menu)
801 return;
803 switch(cursorpos)
805 case 0:
806 value = tm->tm_hour;
807 unit = UNIT_HOUR;
808 break;
809 case 1:
810 value = tm->tm_min;
811 unit = UNIT_MIN;
812 break;
813 case 2:
814 value = tm->tm_sec;
815 unit = UNIT_SEC;
816 break;
817 case 3:
818 value = tm->tm_year + 1900;
819 break;
820 case 5:
821 value = tm->tm_mday;
822 break;
825 if (cursorpos == 4) /* month */
826 talk_id(LANG_MONTH_JANUARY + tm->tm_mon, false);
827 else
828 talk_value(value, unit, false);
832 #define INDEX_X 0
833 #define INDEX_Y 1
834 #define INDEX_WIDTH 2
836 #define SEPARATOR ":"
837 bool set_time_screen(const char* title, struct tm *tm)
839 bool done = false;
840 int button;
841 unsigned int i, s;
842 unsigned int cursorpos = 0;
843 unsigned int lastcursorpos = 1;
844 unsigned int julianday;
845 unsigned int realyear;
846 unsigned int width;
847 unsigned int min = 0, steps = 0;
848 unsigned int statusbar_height = 0;
849 unsigned int separator_width, weekday_width;
850 unsigned int line_height, prev_line_height;
851 unsigned char daysinmonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
852 unsigned char buffer[20];
854 /* 6 possible cursor possitions, 3 values stored for each: x, y, width */
855 unsigned char cursor[6][3];
856 memset(cursor, 0, sizeof(cursor));
858 int *valptr = NULL;
859 unsigned char *ptr[6];
861 if(global_settings.statusbar)
862 statusbar_height = STATUSBAR_HEIGHT;
864 while ( !done ) {
865 /* for easy acess in the drawing loop */
866 ptr[0] = buffer; /* hours */
867 ptr[1] = buffer + 3; /* minutes */
868 ptr[2] = buffer + 6; /* seconds */
869 ptr[3] = buffer + 9; /* year */
870 ptr[4] = str(LANG_MONTH_JANUARY + tm->tm_mon); /* monthname */
871 ptr[5] = buffer + 14; /* day of month */
873 /* calculate the number of days in febuary */
874 realyear = tm->tm_year + 1900;
875 if((realyear % 4 == 0 && !(realyear % 100 == 0)) || realyear % 400 == 0)
876 daysinmonth[1] = 29;
877 else
878 daysinmonth[1] = 28;
880 /* fix day if month or year changed */
881 if (tm->tm_mday > daysinmonth[tm->tm_mon])
882 tm->tm_mday = daysinmonth[tm->tm_mon];
884 /* calculate day of week */
885 julianday = 0;
886 for(i = 0; (int)i < tm->tm_mon; i++) {
887 julianday += daysinmonth[i];
889 julianday += tm->tm_mday;
890 tm->tm_wday = (realyear + julianday + (realyear - 1) / 4 -
891 (realyear - 1) / 100 + (realyear - 1) / 400 + 7 - 1) % 7;
893 /* put all the numbers we want from the tm struct into
894 an easily printable buffer */
895 snprintf(buffer, sizeof(buffer),
896 "%02d " "%02d " "%02d " "%04d " "%02d",
897 tm->tm_hour, tm->tm_min, tm->tm_sec,
898 tm->tm_year+1900, tm->tm_mday);
900 /* convert spaces in the buffer to '\0' to make it possible to work
901 directly on the buffer */
902 for(i=0; i < sizeof(buffer); i++)
904 if(buffer[i] == ' ')
905 buffer[i] = '\0';
908 FOR_NB_SCREENS(s)
910 /* minimum lines needed is 2 + title line */
911 gui_textarea_update_nblines(&screens[s]);
912 if (screens[s].nb_lines < 4)
914 screens[s].setfont(FONT_SYSFIXED);
915 gui_textarea_update_nblines(&screens[s]);
918 /* recalculate the positions and offsets */
919 if (screens[s].nb_lines >= 3)
920 screens[s].getstringsize(title, NULL, &prev_line_height);
921 else
922 prev_line_height = 0;
923 screens[s].getstringsize(buffer, NULL, &line_height);
924 screens[s].getstringsize(SEPARATOR, &separator_width, NULL);
927 /* get width for each string except the last one and put them
928 in the cursor array */
929 for(i=0; i < 5; i++)
931 screens[s].getstringsize(ptr[i], &width, NULL);
932 cursor[i][INDEX_WIDTH] = width;
935 /* hour */
936 /* cursor[0][INDEX_X] is already 0 because of the memset */
937 cursor[0][INDEX_Y] = prev_line_height + statusbar_height;
939 /* minute */
940 cursor[1][INDEX_X] = cursor[0][INDEX_WIDTH] + separator_width;
941 cursor[1][INDEX_Y] = prev_line_height + statusbar_height;
943 /* second */
944 cursor[2][INDEX_X] = cursor[0][INDEX_WIDTH] + separator_width +
945 cursor[1][INDEX_WIDTH] + separator_width;
946 cursor[2][INDEX_Y] = prev_line_height + statusbar_height;
948 /* weekday */
949 screens[s].getstringsize(str(LANG_WEEKDAY_SUNDAY + tm->tm_wday), &weekday_width, NULL);
950 screens[s].getstringsize(" ", &separator_width, NULL);
952 /* year */
953 cursor[3][INDEX_X] = weekday_width + separator_width;
954 cursor[3][INDEX_Y] = cursor[0][INDEX_Y] + prev_line_height;
956 /* month */
957 cursor[4][INDEX_X] = weekday_width + 2 * separator_width +
958 cursor[3][INDEX_WIDTH];
959 cursor[4][INDEX_Y] = cursor[0][INDEX_Y] + prev_line_height;
961 /* day */
962 cursor[5][INDEX_X] = weekday_width + 3 * separator_width +
963 cursor[3][INDEX_WIDTH] +
964 cursor[4][INDEX_WIDTH];
965 cursor[5][INDEX_Y] = cursor[0][INDEX_Y] + prev_line_height;
967 /* draw the screen */
968 screens[s].set_drawmode(DRMODE_SOLID);
969 gui_textarea_clear(&screens[s]);
970 /* display the screen title */
971 screens[s].puts_scroll(0, 0, title);
973 /* these are not selectable, so we draw them outside the loop */
974 screens[s].putsxy(0, cursor[3][INDEX_Y], str(LANG_WEEKDAY_SUNDAY + tm->tm_wday)); /* name of the week day */
975 screens[s].putsxy(cursor[1][INDEX_X] - separator_width,
976 cursor[0][INDEX_Y], SEPARATOR);
977 screens[s].putsxy(cursor[2][INDEX_X] - separator_width,
978 cursor[0][INDEX_Y], SEPARATOR);
980 /* draw the selected item with drawmode set to
981 DRMODE_SOLID|DRMODE_INVERSEVID, all other selectable
982 items with drawmode DRMODE_SOLID */
983 for(i=0; i<6; i++)
985 if (cursorpos == i)
986 screens[s].set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
987 else
988 screens[s].set_drawmode(DRMODE_SOLID);
990 screens[s].putsxy(cursor[i][INDEX_X],
991 cursor[i][INDEX_Y], ptr[i]);
994 /* print help text */
995 if (screens[s].nb_lines > 4)
996 screens[s].puts(0, 4, str(LANG_TIME_SET_BUTTON));
997 if (screens[s].nb_lines > 5)
998 screens[s].puts(0, 5, str(LANG_TIME_REVERT));
999 screens[s].update();
1001 gui_syncstatusbar_draw(&statusbars, true);
1003 /* calculate the minimum and maximum for the number under cursor */
1004 if(cursorpos!=lastcursorpos) {
1005 lastcursorpos=cursorpos;
1006 switch(cursorpos) {
1007 case 0: /* hour */
1008 min = 0;
1009 steps = 24;
1010 valptr = &tm->tm_hour;
1011 break;
1012 case 1: /* minute */
1013 min = 0;
1014 steps = 60;
1015 valptr = &tm->tm_min;
1016 break;
1017 case 2: /* second */
1018 min = 0;
1019 steps = 60;
1020 valptr = &tm->tm_sec;
1021 break;
1022 case 3: /* year */
1023 min = 1;
1024 steps = 200;
1025 valptr = &tm->tm_year;
1026 break;
1027 case 4: /* month */
1028 min = 0;
1029 steps = 12;
1030 valptr = &tm->tm_mon;
1031 break;
1032 case 5: /* day */
1033 min = 1;
1034 steps = daysinmonth[tm->tm_mon];
1035 valptr = &tm->tm_mday;
1036 break;
1038 say_time(cursorpos, tm);
1041 button = get_action(CONTEXT_SETTINGS_TIME, TIMEOUT_BLOCK);
1042 switch ( button ) {
1043 case ACTION_STD_PREV:
1044 cursorpos = (cursorpos + 6 - 1) % 6;
1045 break;
1046 case ACTION_STD_NEXT:
1047 cursorpos = (cursorpos + 6 + 1) % 6;
1048 break;
1049 case ACTION_SETTINGS_INC:
1050 case ACTION_SETTINGS_INCREPEAT:
1051 *valptr = (*valptr + steps - min + 1) %
1052 steps + min;
1053 if(*valptr == 0)
1054 *valptr = min;
1055 say_time(cursorpos, tm);
1056 break;
1057 case ACTION_SETTINGS_DEC:
1058 case ACTION_SETTINGS_DECREPEAT:
1059 *valptr = (*valptr + steps - min - 1) %
1060 steps + min;
1061 if(*valptr == 0)
1062 *valptr = min;
1063 say_time(cursorpos, tm);
1064 break;
1066 case ACTION_STD_OK:
1067 done = true;
1068 break;
1070 case ACTION_STD_CANCEL:
1071 done = true;
1072 tm->tm_year = -1;
1073 break;
1075 default:
1076 if (default_event_handler(button) == SYS_USB_CONNECTED)
1077 return true;
1078 break;
1081 FOR_NB_SCREENS(i)
1083 screens[i].setfont(FONT_UI);
1084 gui_textarea_update_nblines(&screens[i]);
1086 return false;
1088 #endif /* defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0) */
1090 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
1091 bool shutdown_screen(void)
1093 int button;
1094 bool done = false;
1095 long time_entered = current_tick;
1097 lcd_stop_scroll();
1099 gui_syncsplash(0, str(LANG_CONFIRM_SHUTDOWN));
1101 while(!done && TIME_BEFORE(current_tick,time_entered+HZ*2))
1103 button = get_action(CONTEXT_STD,HZ);
1104 switch(button)
1106 case ACTION_STD_CANCEL:
1107 sys_poweroff();
1108 break;
1110 /* do nothing here, because ACTION_NONE might be caused
1111 * by timeout or button release. In case of timeout the loop
1112 * is terminated by TIME_BEFORE */
1113 case ACTION_NONE:
1114 break;
1116 default:
1117 if(default_event_handler(button) == SYS_USB_CONNECTED)
1118 return true;
1119 done = true;
1120 break;
1123 return false;
1125 #endif
1127 static const int id3_headers[]=
1129 LANG_ID3_TITLE,
1130 LANG_ID3_ARTIST,
1131 LANG_ID3_ALBUM,
1132 LANG_ID3_ALBUMARTIST,
1133 LANG_ID3_GROUPING,
1134 LANG_ID3_DISCNUM,
1135 LANG_ID3_TRACKNUM,
1136 LANG_ID3_COMMENT,
1137 LANG_ID3_GENRE,
1138 LANG_ID3_YEAR,
1139 LANG_ID3_LENGTH,
1140 LANG_ID3_PLAYLIST,
1141 LANG_ID3_BITRATE,
1142 LANG_ID3_FREQUENCY,
1143 #if CONFIG_CODEC == SWCODEC
1144 LANG_ID3_TRACK_GAIN,
1145 LANG_ID3_ALBUM_GAIN,
1146 #endif
1147 LANG_ID3_PATH,
1150 static char * id3_get_info(int selected_item, void* data, char *buffer)
1152 struct mp3entry* id3 =(struct mp3entry*)data;
1153 int info_no=selected_item/2;
1154 if(!(selected_item%2))
1155 {/* header */
1156 return( str(id3_headers[info_no]));
1158 else
1159 {/* data */
1161 char * info=NULL;
1162 switch(info_no)
1164 case 0:/*LANG_ID3_TITLE*/
1165 info=id3->title;
1166 break;
1167 case 1:/*LANG_ID3_ARTIST*/
1168 info=id3->artist;
1169 break;
1170 case 2:/*LANG_ID3_ALBUM*/
1171 info=id3->album;
1172 break;
1173 case 3:/*LANG_ID3_ALBUMARTIST*/
1174 info=id3->albumartist;
1175 break;
1176 case 4:/*LANG_ID3_GROUPING*/
1177 info=id3->grouping;
1178 break;
1179 case 5:/*LANG_ID3_DISCNUM*/
1180 if (id3->disc_string)
1181 info = id3->disc_string;
1182 else if (id3->discnum)
1184 snprintf(buffer, MAX_PATH, "%d", id3->discnum);
1185 info = buffer;
1187 break;
1188 case 6:/*LANG_ID3_TRACKNUM*/
1189 if (id3->track_string)
1190 info = id3->track_string;
1191 else if (id3->tracknum)
1193 snprintf(buffer, MAX_PATH, "%d", id3->tracknum);
1194 info = buffer;
1196 break;
1197 case 7:/*LANG_ID3_COMMENT*/
1198 info=id3->comment;
1199 break;
1200 case 8:/*LANG_ID3_GENRE*/
1201 info = id3->genre_string;
1202 break;
1203 case 9:/*LANG_ID3_YEAR*/
1204 if (id3->year_string)
1205 info = id3->year_string;
1206 else if (id3->year)
1208 snprintf(buffer, MAX_PATH, "%d", id3->year);
1209 info = buffer;
1211 break;
1212 case 10:/*LANG_ID3_LENGTH*/
1213 format_time(buffer, MAX_PATH, id3->length);
1214 info=buffer;
1215 break;
1216 case 11:/*LANG_ID3_PLAYLIST*/
1217 snprintf(buffer, MAX_PATH, "%d/%d", playlist_get_display_index(),
1218 playlist_amount());
1219 info=buffer;
1220 break;
1221 case 12:/*LANG_ID3_BITRATE*/
1222 snprintf(buffer, MAX_PATH, "%d kbps%s", id3->bitrate,
1223 id3->vbr ? str(LANG_ID3_VBR) : (const unsigned char*) "");
1224 info=buffer;
1225 break;
1226 case 13:/*LANG_ID3_FREQUENCY*/
1227 snprintf(buffer, MAX_PATH, "%ld Hz", id3->frequency);
1228 info=buffer;
1229 break;
1230 #if CONFIG_CODEC == SWCODEC
1231 case 14:/*LANG_ID3_TRACK_GAIN*/
1232 info=id3->track_gain_string;
1233 break;
1234 case 15:/*LANG_ID3_ALBUM_GAIN*/
1235 info=id3->album_gain_string;
1236 break;
1237 case 16:/*LANG_ID3_PATH*/
1238 #else
1239 case 14:/*LANG_ID3_PATH*/
1240 #endif
1241 info=id3->path;
1242 break;
1244 return info && *info ? info : (char*) str(LANG_ID3_NO_INFO);
1248 bool browse_id3(void)
1250 struct gui_synclist id3_lists;
1251 struct mp3entry* id3 = audio_current_track();
1252 int key;
1254 gui_synclist_init(&id3_lists, &id3_get_info, id3, true, 2);
1255 gui_synclist_set_nb_items(&id3_lists,
1256 sizeof(id3_headers)/sizeof(id3_headers[0])*2);
1257 gui_synclist_draw(&id3_lists);
1258 gui_syncstatusbar_draw(&statusbars, true);
1259 while (true) {
1260 gui_syncstatusbar_draw(&statusbars, false);
1261 key = get_action(CONTEXT_LIST,HZ/2);
1262 if(key!=ACTION_NONE && key!=ACTION_UNKNOWN
1263 && !gui_synclist_do_button(&id3_lists, &key,LIST_WRAP_UNLESS_HELD))
1265 return(default_event_handler(key) == SYS_USB_CONNECTED);
1270 static char* runtime_get_data(int selected_item, void* data, char* buffer)
1272 (void) data;
1273 unsigned char *headers[] = {str(LANG_RUNNING_TIME), str(LANG_TOP_TIME) };
1274 int t;
1275 if(!(selected_item%2))
1276 return headers[selected_item/2];
1278 if(selected_item/2) t = global_status.topruntime;
1279 else t = global_status.runtime;
1281 snprintf(buffer, 16, "%dh %dm %ds",
1282 t / 3600, (t % 3600) / 60, t % 60);
1283 return buffer;
1288 bool view_runtime(void)
1290 unsigned char *lines[]={str(LANG_CLEAR_TIME)};
1291 struct text_message message={(char **)lines, 1};
1293 struct gui_synclist lists;
1294 int action;
1295 gui_synclist_init(&lists, runtime_get_data, NULL, false, 2);
1296 #if !defined(HAVE_LCD_CHARCELLS)
1297 gui_synclist_set_title(&lists, str(LANG_RUNNING_TIME), NOICON);
1298 #else
1299 gui_synclist_set_title(&lists, NULL, NOICON);
1300 #endif
1301 gui_synclist_set_icon_callback(&lists, NULL);
1302 gui_synclist_set_nb_items(&lists, 4);
1303 while(1)
1305 #if CONFIG_CHARGING
1306 if (charger_inserted()
1307 #ifdef HAVE_USB_POWER
1308 || usb_powered()
1309 #endif
1312 global_status.runtime = 0;
1314 else
1315 #endif
1317 global_status.runtime += ((current_tick - lasttime) / HZ);
1319 lasttime = current_tick;
1320 gui_synclist_draw(&lists);
1321 gui_syncstatusbar_draw(&statusbars, true);
1322 action = get_action(CONTEXT_STD, HZ);
1323 gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD);
1324 if(action == ACTION_STD_CANCEL)
1325 break;
1326 if(action == ACTION_STD_OK) {
1327 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
1329 if (!(gui_synclist_get_sel_pos(&lists)/2))
1330 global_status.runtime = 0;
1331 else
1332 global_status.topruntime = 0;
1335 if(default_event_handler(action) == SYS_USB_CONNECTED)
1336 return true;
1338 return false;