"remote_control: don't use goto.
[kugel-rb.git] / apps / plugins / clock / clock_settings.c
blob8464ab0ed166989a8bde51c1077731599d9bbb00
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Copyright Kévin Ferrare based on Zakk Roberts's work
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 "clock.h"
23 #include "clock_bitmaps.h"
24 #include "clock_draw.h"
25 #include "clock_settings.h"
26 #include "lib/picture.h"
28 static int max_skin[]={
29 [ANALOG]=2,
30 [BINARY]=3,
31 [DIGITAL]=2,
34 enum message{
35 MESSAGE_LOADING,
36 MESSAGE_LOADED,
37 MESSAGE_ERRLOAD,
38 MESSAGE_SAVING,
39 MESSAGE_SAVED,
40 MESSAGE_ERRSAVE
43 enum settings_file_status{
44 LOADED, ERRLOAD,
45 SAVED, ERRSAVE
48 struct clock_settings clock_settings;
50 /* The settings as they exist on the hard disk, so that
51 * we can know at saving time if changes have been made */
52 struct clock_settings hdd_clock_settings;
54 bool settings_needs_saving(struct clock_settings* settings){
55 return(rb->memcmp(settings, &hdd_clock_settings, sizeof(*settings)));
58 void clock_settings_reset(struct clock_settings* settings){
59 settings->mode = ANALOG;
60 int i;
61 for(i=0;i<NB_CLOCK_MODES;i++){
62 settings->skin[i]=0;
64 settings->general.hour_format = H12;
65 settings->general.date_format = EUROPEAN;
66 settings->general.show_counter = true;
67 settings->general.save_settings = true;
68 settings->general.idle_poweroff=true;
69 settings->general.backlight = ROCKBOX_SETTING;
71 settings->analog.show_date = false;
72 settings->analog.show_seconds = true;
73 settings->analog.show_border = true;
75 settings->digital.show_seconds = true;
76 settings->digital.blinkcolon = false;
77 apply_backlight_setting(settings->general.backlight);
80 void apply_backlight_setting(int backlight_setting)
82 if(backlight_setting == ALWAS_OFF)
83 rb->backlight_set_timeout(-1);
84 else if(backlight_setting == ROCKBOX_SETTING)
85 rb->backlight_set_timeout(rb->global_settings->backlight_timeout);
86 else if(backlight_setting == ALWAYS_ON)
87 rb->backlight_set_timeout(0);
90 void clock_settings_skin_next(struct clock_settings* settings){
91 settings->skin[settings->mode]++;
92 if(settings->skin[settings->mode]>=max_skin[settings->mode])
93 settings->skin[settings->mode]=0;
96 void clock_settings_skin_previous(struct clock_settings* settings){
97 settings->skin[settings->mode]--;
98 if(settings->skin[settings->mode]<0)
99 settings->skin[settings->mode]=max_skin[settings->mode]-1;
102 enum settings_file_status clock_settings_load(struct clock_settings* settings,
103 char* filename){
104 int fd = rb->open(filename, O_RDONLY);
105 if(fd >= 0){ /* does file exist? */
106 /* basic consistency check */
107 if(rb->filesize(fd) == sizeof(*settings)){
108 rb->read(fd, settings, sizeof(*settings));
109 rb->close(fd);
110 apply_backlight_setting(settings->general.backlight);
111 rb->memcpy(&hdd_clock_settings, settings, sizeof(*settings));
112 return(LOADED);
115 /* Initializes the settings with default values at least */
116 clock_settings_reset(settings);
117 return(ERRLOAD);
120 enum settings_file_status clock_settings_save(struct clock_settings* settings,
121 char* filename){
122 int fd = rb->creat(filename);
123 if(fd >= 0){ /* does file exist? */
124 rb->write (fd, settings, sizeof(*settings));
125 rb->close(fd);
126 return(SAVED);
128 return(ERRSAVE);
131 void draw_logo(struct screen* display){
132 #ifdef HAVE_LCD_COLOR
133 if(display->is_color){
134 display->set_foreground(LCD_BLACK);
135 display->set_background(LCD_RGBPACK(180,200,230));
137 #endif
139 const struct picture* logo = &(logos[display->screen_type]);
140 display->clear_display();
141 picture_draw(display, logo, 0, 0);
144 void draw_message(struct screen* display, int msg, int y){
145 const struct picture* message = &(messages[display->screen_type]);
146 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
147 display->fillrect(0, display->getheight()-message->slide_height,
148 display->getwidth(), message->slide_height);
149 display->set_drawmode(DRMODE_SOLID);
150 vertical_picture_draw_sprite(display, message, msg,
151 0, display->getheight() -
152 (message->slide_height*y));
155 void load_settings(void){
156 int i;
157 struct screen* display;
158 FOR_NB_SCREENS(i){
159 display=rb->screens[i];
160 display->clear_display();
161 draw_logo(display);
162 draw_message(display, MESSAGE_LOADING, 1);
163 display->update();
166 enum settings_file_status load_status=
167 clock_settings_load(&clock_settings, settings_filename);
169 FOR_NB_SCREENS(i){
170 display=rb->screens[i];
171 if(load_status==LOADED)
172 draw_message(display, MESSAGE_LOADED, 1);
173 else
174 draw_message(display, MESSAGE_ERRLOAD, 1);
175 display->update();
177 rb->storage_sleep();
178 rb->sleep(HZ);
181 void save_settings(void){
182 int i;
183 struct screen* display;
184 if(!settings_needs_saving(&clock_settings))
185 return;
187 FOR_NB_SCREENS(i){
188 display=rb->screens[i];
189 display->clear_display();
190 draw_logo(display);
192 draw_message(display, MESSAGE_SAVING, 1);
194 display->update();
196 enum settings_file_status load_status=
197 clock_settings_save(&clock_settings, settings_filename);
199 FOR_NB_SCREENS(i){
200 display=rb->screens[i];
202 if(load_status==SAVED)
203 draw_message(display, MESSAGE_SAVED, 1);
204 else
205 draw_message(display, MESSAGE_ERRSAVE, 1);
206 display->update();
208 rb->sleep(HZ);
211 void save_settings_wo_gui(void){
212 clock_settings_save(&clock_settings, settings_filename);