Kevin Ferrare | 93b2f9f | 2007-08-04 03:01:46 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id: jackpot.c 14034 2007-07-28 05:42:55Z kevin $ |
| 9 | * |
Nicolas Pennequin | 357ffb3 | 2008-05-05 10:32:46 +0000 | [diff] [blame] | 10 | * Copyright (C) 2007 Copyright Kévin Ferrare based on Zakk Roberts's work |
Kevin Ferrare | 93b2f9f | 2007-08-04 03:01:46 +0000 | [diff] [blame] | 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Kevin Ferrare | 93b2f9f | 2007-08-04 03:01:46 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #include "clock.h" |
| 23 | #include "clock_bitmaps.h" |
| 24 | #include "clock_draw.h" |
| 25 | #include "clock_settings.h" |
| 26 | #include "picture.h" |
| 27 | |
| 28 | static int max_skin[]={ |
| 29 | [ANALOG]=2, |
| 30 | [BINARY]=3, |
| 31 | [DIGITAL]=2, |
| 32 | }; |
| 33 | |
| 34 | enum message{ |
| 35 | MESSAGE_LOADING, |
| 36 | MESSAGE_LOADED, |
| 37 | MESSAGE_ERRLOAD, |
| 38 | MESSAGE_SAVING, |
| 39 | MESSAGE_SAVED, |
| 40 | MESSAGE_ERRSAVE |
| 41 | }; |
| 42 | |
| 43 | enum settings_file_status{ |
| 44 | LOADED, ERRLOAD, |
| 45 | SAVED, ERRSAVE |
| 46 | }; |
| 47 | |
| 48 | struct clock_settings clock_settings; |
| 49 | |
Kevin Ferrare | 721c897 | 2007-08-04 08:32:49 +0000 | [diff] [blame] | 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; |
| 53 | |
| 54 | bool settings_needs_saving(struct clock_settings* settings){ |
| 55 | return(rb->memcmp(settings, &hdd_clock_settings, sizeof(*settings))); |
| 56 | } |
| 57 | |
Kevin Ferrare | 93b2f9f | 2007-08-04 03:01:46 +0000 | [diff] [blame] | 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; |
| 63 | } |
| 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; |
| 70 | |
| 71 | settings->analog.show_date = false; |
| 72 | settings->analog.show_seconds = true; |
| 73 | settings->analog.show_border = true; |
| 74 | |
| 75 | settings->digital.show_seconds = true; |
| 76 | settings->digital.blinkcolon = false; |
| 77 | apply_backlight_setting(settings->general.backlight); |
| 78 | } |
| 79 | |
| 80 | void apply_backlight_setting(int backlight_setting) |
| 81 | { |
| 82 | if(backlight_setting == ALWAS_OFF) |
Jens Arnold | bf2a334 | 2007-11-29 23:55:58 +0000 | [diff] [blame] | 83 | rb->backlight_set_timeout(-1); |
Kevin Ferrare | 93b2f9f | 2007-08-04 03:01:46 +0000 | [diff] [blame] | 84 | else if(backlight_setting == ROCKBOX_SETTING) |
| 85 | rb->backlight_set_timeout(rb->global_settings->backlight_timeout); |
| 86 | else if(backlight_setting == ALWAYS_ON) |
Jens Arnold | bf2a334 | 2007-11-29 23:55:58 +0000 | [diff] [blame] | 87 | rb->backlight_set_timeout(0); |
Kevin Ferrare | 93b2f9f | 2007-08-04 03:01:46 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 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; |
| 94 | } |
| 95 | |
| 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; |
| 100 | } |
| 101 | |
| 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); |
Kevin Ferrare | 721c897 | 2007-08-04 08:32:49 +0000 | [diff] [blame] | 111 | rb->memcpy(&hdd_clock_settings, settings, sizeof(*settings)); |
Kevin Ferrare | 93b2f9f | 2007-08-04 03:01:46 +0000 | [diff] [blame] | 112 | return(LOADED); |
| 113 | } |
| 114 | } |
| 115 | /* Initializes the settings with default values at least */ |
| 116 | clock_settings_reset(settings); |
| 117 | return(ERRLOAD); |
| 118 | } |
| 119 | |
| 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); |
| 127 | } |
| 128 | return(ERRSAVE); |
| 129 | } |
| 130 | |
| 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)); |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | const struct picture* logo = &(logos[display->screen_type]); |
| 140 | display->clear_display(); |
| 141 | picture_draw(display, logo, 0, 0); |
| 142 | } |
| 143 | |
| 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->height-message->height, |
| 148 | display->width, message->height); |
| 149 | display->set_drawmode(DRMODE_SOLID); |
| 150 | vertical_picture_draw_sprite(display, message, msg, |
| 151 | 0, display->height-(message->height*y)); |
| 152 | } |
| 153 | |
| 154 | void load_settings(void){ |
| 155 | int i; |
| 156 | struct screen* display; |
| 157 | FOR_NB_SCREENS(i){ |
| 158 | display=rb->screens[i]; |
| 159 | display->clear_display(); |
| 160 | draw_logo(display); |
| 161 | draw_message(display, MESSAGE_LOADING, 1); |
| 162 | display->update(); |
| 163 | } |
| 164 | |
| 165 | enum settings_file_status load_status= |
| 166 | clock_settings_load(&clock_settings, settings_filename); |
| 167 | |
| 168 | FOR_NB_SCREENS(i){ |
| 169 | display=rb->screens[i]; |
| 170 | if(load_status==LOADED) |
| 171 | draw_message(display, MESSAGE_LOADED, 1); |
| 172 | else |
| 173 | draw_message(display, MESSAGE_ERRLOAD, 1); |
| 174 | display->update(); |
| 175 | } |
Kevin Ferrare | 93b2f9f | 2007-08-04 03:01:46 +0000 | [diff] [blame] | 176 | rb->ata_sleep(); |
Kevin Ferrare | 93b2f9f | 2007-08-04 03:01:46 +0000 | [diff] [blame] | 177 | rb->sleep(HZ); |
| 178 | } |
| 179 | |
| 180 | void save_settings(void){ |
| 181 | int i; |
| 182 | struct screen* display; |
Kevin Ferrare | 721c897 | 2007-08-04 08:32:49 +0000 | [diff] [blame] | 183 | if(!settings_needs_saving(&clock_settings)) |
| 184 | return; |
| 185 | |
Kevin Ferrare | 93b2f9f | 2007-08-04 03:01:46 +0000 | [diff] [blame] | 186 | FOR_NB_SCREENS(i){ |
| 187 | display=rb->screens[i]; |
| 188 | display->clear_display(); |
| 189 | draw_logo(display); |
| 190 | |
| 191 | draw_message(display, MESSAGE_SAVING, 1); |
| 192 | |
| 193 | display->update(); |
| 194 | } |
| 195 | enum settings_file_status load_status= |
| 196 | clock_settings_save(&clock_settings, settings_filename); |
| 197 | |
| 198 | FOR_NB_SCREENS(i){ |
| 199 | display=rb->screens[i]; |
| 200 | |
| 201 | if(load_status==SAVED) |
| 202 | draw_message(display, MESSAGE_SAVED, 1); |
| 203 | else |
| 204 | draw_message(display, MESSAGE_ERRSAVE, 1); |
| 205 | display->update(); |
| 206 | } |
| 207 | rb->sleep(HZ); |
| 208 | } |
| 209 | |
| 210 | void save_settings_wo_gui(void){ |
| 211 | clock_settings_save(&clock_settings, settings_filename); |
| 212 | } |