Daniel Stenberg | 5befad0 | 2002-11-22 13:00:10 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 by Daniel Stenberg |
| 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. |
Daniel Stenberg | 5befad0 | 2002-11-22 13:00:10 +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 | ****************************************************************************/ |
Björn Stenberg | 6c33c51 | 2004-09-19 21:58:37 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
Linus Nielsen Feltzing | 26440c9 | 2004-07-05 11:15:50 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
Dave Chapman | 78d29f5 | 2008-03-26 23:35:34 +0000 | [diff] [blame] | 23 | #include "config.h" |
| 24 | #include "lcd.h" |
| 25 | #include "file.h" |
| 26 | #ifdef __PCTOOL__ |
| 27 | #include <stdarg.h> |
| 28 | #else |
Dave Chapman | 7418c35 | 2008-03-25 21:09:58 +0000 | [diff] [blame] | 29 | #include "sprintf.h" |
Linus Nielsen Feltzing | 5b5003d | 2004-07-24 21:26:41 +0000 | [diff] [blame] | 30 | #include "lang.h" |
Linus Nielsen Feltzing | a97de95 | 2003-04-14 15:58:51 +0000 | [diff] [blame] | 31 | #include "string.h" |
Linus Nielsen Feltzing | 6dca87e | 2005-02-11 14:38:44 +0000 | [diff] [blame] | 32 | #include "dir.h" |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 33 | #include "lcd-remote.h" |
Linus Nielsen Feltzing | 75b41a8 | 2003-05-04 02:04:31 +0000 | [diff] [blame] | 34 | #include "errno.h" |
| 35 | #include "system.h" |
Linus Nielsen Feltzing | a8dab4c | 2004-06-30 13:31:14 +0000 | [diff] [blame] | 36 | #include "timefuncs.h" |
Linus Nielsen Feltzing | 5b5003d | 2004-07-24 21:26:41 +0000 | [diff] [blame] | 37 | #include "screens.h" |
Jens Arnold | 2116bba | 2005-01-31 00:39:20 +0000 | [diff] [blame] | 38 | #include "talk.h" |
Linus Nielsen Feltzing | 5b5003d | 2004-07-24 21:26:41 +0000 | [diff] [blame] | 39 | #include "mpeg.h" |
Linus Nielsen Feltzing | 3d21781 | 2005-04-06 11:12:22 +0000 | [diff] [blame] | 40 | #include "audio.h" |
Linus Nielsen Feltzing | 5b5003d | 2004-07-24 21:26:41 +0000 | [diff] [blame] | 41 | #include "mp3_playback.h" |
| 42 | #include "settings.h" |
| 43 | #include "ata.h" |
Jonathan Gordon | 0d94142 | 2006-11-09 12:36:50 +0000 | [diff] [blame] | 44 | #include "ata_idle_notify.h" |
Linus Nielsen Feltzing | 5b5003d | 2004-07-24 21:26:41 +0000 | [diff] [blame] | 45 | #include "kernel.h" |
| 46 | #include "power.h" |
Christi Scarborough | 247fe6d | 2005-02-06 09:57:57 +0000 | [diff] [blame] | 47 | #include "powermgmt.h" |
Linus Nielsen Feltzing | b03ff1e | 2004-10-13 09:08:32 +0000 | [diff] [blame] | 48 | #include "backlight.h" |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 49 | #include "version.h" |
| 50 | #include "font.h" |
Kevin Ferrare | e991bee | 2005-11-16 15:12:15 +0000 | [diff] [blame] | 51 | #include "splash.h" |
Miika Pekkarinen | 3b31346 | 2006-04-16 17:32:54 +0000 | [diff] [blame] | 52 | #include "tagcache.h" |
Linus Nielsen Feltzing | da153da | 2006-10-19 09:42:58 +0000 | [diff] [blame] | 53 | #include "scrobbler.h" |
Nils Wallménius | f46657e | 2007-05-30 17:57:32 +0000 | [diff] [blame] | 54 | #include "sound.h" |
Nils Wallménius | dabcb81 | 2008-05-01 10:13:12 +0000 | [diff] [blame] | 55 | #include "playlist.h" |
| 56 | #include "yesno.h" |
Dave Chapman | 7418c35 | 2008-03-25 21:09:58 +0000 | [diff] [blame] | 57 | |
Jens Arnold | 7f6b640 | 2004-10-12 00:02:51 +0000 | [diff] [blame] | 58 | #ifdef HAVE_MMC |
| 59 | #include "ata_mmc.h" |
| 60 | #endif |
Linus Nielsen Feltzing | 74353a7 | 2005-09-14 09:07:05 +0000 | [diff] [blame] | 61 | #include "tree.h" |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 62 | #include "eeprom_settings.h" |
Dave Chapman | 7418c35 | 2008-03-25 21:09:58 +0000 | [diff] [blame] | 63 | #if defined(HAVE_RECORDING) && !defined(__PCTOOL__) |
Peter D'Hoye | 583f186 | 2007-08-25 15:53:54 +0000 | [diff] [blame] | 64 | #include "recording.h" |
| 65 | #endif |
Dave Chapman | 7418c35 | 2008-03-25 21:09:58 +0000 | [diff] [blame] | 66 | #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__) |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 67 | #include "bmp.h" |
| 68 | #include "icons.h" |
| 69 | #endif /* End HAVE_LCD_BITMAP */ |
Miika Pekkarinen | 6938f56 | 2006-10-18 12:54:23 +0000 | [diff] [blame] | 70 | #include "gui/gwps-common.h" |
Jens Arnold | 178c565 | 2007-04-09 13:39:37 +0000 | [diff] [blame] | 71 | #include "bookmark.h" |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 72 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 73 | #include "misc.h" |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 74 | #include "playback.h" |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 75 | |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 76 | #ifdef BOOTFILE |
Jens Arnold | ffb3dfd | 2007-08-01 20:21:34 +0000 | [diff] [blame] | 77 | #if !defined(USB_NONE) && !defined(USB_IPODSTYLE) |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 78 | #include "rolo.h" |
| 79 | #include "yesno.h" |
| 80 | #endif |
Peter D'Hoye | dc33e1e | 2007-03-30 23:36:10 +0000 | [diff] [blame] | 81 | #endif |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 82 | |
Jens Arnold | 2116bba | 2005-01-31 00:39:20 +0000 | [diff] [blame] | 83 | /* Format a large-range value for output, using the appropriate unit so that |
| 84 | * the displayed value is in the range 1 <= display < 1000 (1024 for "binary" |
| 85 | * units) if possible, and 3 significant digits are shown. If a buffer is |
| 86 | * given, the result is snprintf()'d into that buffer, otherwise the result is |
| 87 | * voiced.*/ |
| 88 | char *output_dyn_value(char *buf, int buf_size, int value, |
| 89 | const unsigned char **units, bool bin_scale) |
Daniel Stenberg | 5befad0 | 2002-11-22 13:00:10 +0000 | [diff] [blame] | 90 | { |
Jens Arnold | 2116bba | 2005-01-31 00:39:20 +0000 | [diff] [blame] | 91 | int scale = bin_scale ? 1024 : 1000; |
| 92 | int fraction = 0; |
| 93 | int unit_no = 0; |
| 94 | char tbuf[5]; |
| 95 | |
| 96 | while (value >= scale) |
| 97 | { |
| 98 | fraction = value % scale; |
| 99 | value /= scale; |
| 100 | unit_no++; |
Daniel Stenberg | 5befad0 | 2002-11-22 13:00:10 +0000 | [diff] [blame] | 101 | } |
Jens Arnold | 2116bba | 2005-01-31 00:39:20 +0000 | [diff] [blame] | 102 | if (bin_scale) |
| 103 | fraction = fraction * 1000 / 1024; |
| 104 | |
Jens Arnold | c8e1d57 | 2005-01-31 02:25:36 +0000 | [diff] [blame] | 105 | if (value >= 100 || !unit_no) |
| 106 | tbuf[0] = '\0'; |
| 107 | else if (value >= 10) |
| 108 | snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100); |
| 109 | else |
| 110 | snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10); |
| 111 | |
Jens Arnold | 2116bba | 2005-01-31 00:39:20 +0000 | [diff] [blame] | 112 | if (buf) |
| 113 | { |
Jens Arnold | c8e1d57 | 2005-01-31 02:25:36 +0000 | [diff] [blame] | 114 | if (strlen(tbuf)) |
| 115 | snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT), |
| 116 | tbuf, P2STR(units[unit_no])); |
Jens Arnold | 2116bba | 2005-01-31 00:39:20 +0000 | [diff] [blame] | 117 | else |
Jens Arnold | c8e1d57 | 2005-01-31 02:25:36 +0000 | [diff] [blame] | 118 | snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no])); |
Daniel Stenberg | 5befad0 | 2002-11-22 13:00:10 +0000 | [diff] [blame] | 119 | } |
Jens Arnold | 2116bba | 2005-01-31 00:39:20 +0000 | [diff] [blame] | 120 | else |
| 121 | { |
Nils Wallménius | 2521bf5 | 2008-04-20 11:02:42 +0000 | [diff] [blame] | 122 | talk_fractional(tbuf, value, P2ID(units[unit_no])); |
Daniel Stenberg | 5befad0 | 2002-11-22 13:00:10 +0000 | [diff] [blame] | 123 | } |
Jens Arnold | 2116bba | 2005-01-31 00:39:20 +0000 | [diff] [blame] | 124 | return buf; |
Daniel Stenberg | 5befad0 | 2002-11-22 13:00:10 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 127 | /* Create a filename with a number part in a way that the number is 1 |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 128 | * higher than the highest numbered file matching the same pattern. |
| 129 | * It is allowed that buffer and path point to the same memory location, |
| 130 | * saving a strcpy(). Path must always be given without trailing slash. |
| 131 | * "num" can point to an int specifying the number to use or NULL or a value |
| 132 | * less than zero to number automatically. The final number used will also |
| 133 | * be returned in *num. If *num is >= 0 then *num will be incremented by |
| 134 | * one. */ |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 135 | char *create_numbered_filename(char *buffer, const char *path, |
| 136 | const char *prefix, const char *suffix, |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 137 | int numberlen IF_CNFN_NUM_(, int *num)) |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 138 | { |
| 139 | DIR *dir; |
| 140 | struct dirent *entry; |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 141 | int max_num; |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 142 | int pathlen; |
| 143 | int prefixlen = strlen(prefix); |
| 144 | char fmtstring[12]; |
| 145 | |
| 146 | if (buffer != path) |
| 147 | strncpy(buffer, path, MAX_PATH); |
| 148 | |
| 149 | pathlen = strlen(buffer); |
| 150 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 151 | #ifdef IF_CNFN_NUM |
| 152 | if (num && *num >= 0) |
| 153 | { |
| 154 | /* number specified */ |
| 155 | max_num = *num; |
| 156 | } |
| 157 | else |
| 158 | #endif |
| 159 | { |
| 160 | /* automatic numbering */ |
| 161 | max_num = 0; |
| 162 | |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 163 | dir = opendir(pathlen ? buffer : "/"); |
| 164 | if (!dir) |
| 165 | return NULL; |
| 166 | |
| 167 | while ((entry = readdir(dir))) |
| 168 | { |
| 169 | int curr_num; |
| 170 | |
Daniel Stenberg | f981ea9 | 2005-12-05 22:44:42 +0000 | [diff] [blame] | 171 | if (strncasecmp((char *)entry->d_name, prefix, prefixlen) |
| 172 | || strcasecmp((char *)entry->d_name + prefixlen + numberlen, suffix)) |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 173 | continue; |
| 174 | |
Daniel Stenberg | f981ea9 | 2005-12-05 22:44:42 +0000 | [diff] [blame] | 175 | curr_num = atoi((char *)entry->d_name + prefixlen); |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 176 | if (curr_num > max_num) |
| 177 | max_num = curr_num; |
| 178 | } |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 179 | |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 180 | closedir(dir); |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | max_num++; |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 184 | |
| 185 | snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen); |
| 186 | snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix, |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 187 | max_num, suffix); |
| 188 | |
| 189 | #ifdef IF_CNFN_NUM |
| 190 | if (num) |
| 191 | *num = max_num; |
| 192 | #endif |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 193 | |
| 194 | return buffer; |
| 195 | } |
| 196 | |
Magnus Holmgren | 14ba91e | 2007-02-17 13:36:44 +0000 | [diff] [blame] | 197 | /* Format time into buf. |
| 198 | * |
| 199 | * buf - buffer to format to. |
| 200 | * buf_size - size of buffer. |
| 201 | * t - time to format, in milliseconds. |
| 202 | */ |
| 203 | void format_time(char* buf, int buf_size, long t) |
| 204 | { |
| 205 | if ( t < 3600000 ) |
| 206 | { |
| 207 | snprintf(buf, buf_size, "%d:%02d", |
| 208 | (int) (t / 60000), (int) (t % 60000 / 1000)); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | snprintf(buf, buf_size, "%d:%02d:%02d", |
| 213 | (int) (t / 3600000), (int) (t % 3600000 / 60000), |
| 214 | (int) (t % 60000 / 1000)); |
| 215 | } |
| 216 | } |
| 217 | |
Jens Arnold | 2c7b127 | 2007-03-16 23:47:03 +0000 | [diff] [blame] | 218 | #if CONFIG_RTC |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 219 | /* Create a filename with a date+time part. |
| 220 | It is allowed that buffer and path point to the same memory location, |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 221 | saving a strcpy(). Path must always be given without trailing slash. |
| 222 | unique_time as true makes the function wait until the current time has |
| 223 | changed. */ |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 224 | char *create_datetime_filename(char *buffer, const char *path, |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 225 | const char *prefix, const char *suffix, |
| 226 | bool unique_time) |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 227 | { |
| 228 | struct tm *tm = get_time(); |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 229 | static struct tm last_tm; |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 230 | int pathlen; |
| 231 | |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 232 | while (unique_time && !memcmp(get_time(), &last_tm, sizeof (struct tm))) |
| 233 | sleep(HZ/10); |
| 234 | |
| 235 | last_tm = *tm; |
| 236 | |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 237 | if (buffer != path) |
| 238 | strncpy(buffer, path, MAX_PATH); |
| 239 | |
| 240 | pathlen = strlen(buffer); |
| 241 | snprintf(buffer + pathlen, MAX_PATH - pathlen, |
| 242 | "/%s%02d%02d%02d-%02d%02d%02d%s", prefix, |
| 243 | tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday, |
| 244 | tm->tm_hour, tm->tm_min, tm->tm_sec, suffix); |
| 245 | |
| 246 | return buffer; |
| 247 | } |
Dave Chapman | 8c800cf | 2005-12-04 15:23:47 +0000 | [diff] [blame] | 248 | #endif /* CONFIG_RTC */ |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 249 | |
Nils Wallménius | dabcb81 | 2008-05-01 10:13:12 +0000 | [diff] [blame] | 250 | /* Ask the user if they really want to erase the current dynamic playlist |
| 251 | * returns true if the playlist should be replaced */ |
| 252 | bool warn_on_pl_erase(void) |
| 253 | { |
| 254 | if (global_settings.warnon_erase_dynplaylist && |
| 255 | !global_settings.party_mode && |
| 256 | playlist_modified(NULL)) |
| 257 | { |
| 258 | static const char *lines[] = |
| 259 | {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)}; |
| 260 | static const struct text_message message={lines, 1}; |
| 261 | |
| 262 | return (gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES); |
| 263 | } |
| 264 | else |
| 265 | return true; |
| 266 | } |
| 267 | |
Linus Nielsen Feltzing | 75b41a8 | 2003-05-04 02:04:31 +0000 | [diff] [blame] | 268 | /* Read (up to) a line of text from fd into buffer and return number of bytes |
Jens Arnold | 2116bba | 2005-01-31 00:39:20 +0000 | [diff] [blame] | 269 | * read (which may be larger than the number of bytes stored in buffer). If |
Jens Arnold | 58e9412 | 2005-09-02 01:15:35 +0000 | [diff] [blame] | 270 | * an error occurs, -1 is returned (and buffer contains whatever could be |
| 271 | * read). A line is terminated by a LF char. Neither LF nor CR chars are |
Linus Nielsen Feltzing | 75b41a8 | 2003-05-04 02:04:31 +0000 | [diff] [blame] | 272 | * stored in buffer. |
| 273 | */ |
| 274 | int read_line(int fd, char* buffer, int buffer_size) |
| 275 | { |
| 276 | int count = 0; |
| 277 | int num_read = 0; |
| 278 | |
| 279 | errno = 0; |
| 280 | |
| 281 | while (count < buffer_size) |
| 282 | { |
| 283 | unsigned char c; |
| 284 | |
| 285 | if (1 != read(fd, &c, 1)) |
| 286 | break; |
| 287 | |
| 288 | num_read++; |
| 289 | |
| 290 | if ( c == '\n' ) |
| 291 | break; |
| 292 | |
| 293 | if ( c == '\r' ) |
| 294 | continue; |
| 295 | |
| 296 | buffer[count++] = c; |
| 297 | } |
| 298 | |
| 299 | buffer[MIN(count, buffer_size - 1)] = 0; |
| 300 | |
| 301 | return errno ? -1 : num_read; |
| 302 | } |
| 303 | |
Miika Pekkarinen | a1ac743 | 2006-10-21 20:37:33 +0000 | [diff] [blame] | 304 | /* Performance optimized version of the previous function. */ |
| 305 | int fast_readline(int fd, char *buf, int buf_size, void *parameters, |
| 306 | int (*callback)(int n, const char *buf, void *parameters)) |
| 307 | { |
| 308 | char *p, *next; |
| 309 | int rc, pos = 0; |
| 310 | int count = 0; |
| 311 | |
| 312 | while ( 1 ) |
| 313 | { |
| 314 | next = NULL; |
| 315 | |
| 316 | rc = read(fd, &buf[pos], buf_size - pos - 1); |
| 317 | if (rc >= 0) |
| 318 | buf[pos+rc] = '\0'; |
| 319 | |
| 320 | if ( (p = strchr(buf, '\r')) != NULL) |
| 321 | { |
| 322 | *p = '\0'; |
| 323 | next = ++p; |
| 324 | } |
| 325 | else |
| 326 | p = buf; |
| 327 | |
| 328 | if ( (p = strchr(p, '\n')) != NULL) |
| 329 | { |
| 330 | *p = '\0'; |
| 331 | next = ++p; |
| 332 | } |
| 333 | |
| 334 | rc = callback(count, buf, parameters); |
| 335 | if (rc < 0) |
| 336 | return rc; |
| 337 | |
| 338 | count++; |
| 339 | if (next) |
| 340 | { |
| 341 | pos = buf_size - ((long)next - (long)buf) - 1; |
| 342 | memmove(buf, next, pos); |
| 343 | } |
| 344 | else |
| 345 | break ; |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
Linus Nielsen Feltzing | a8dab4c | 2004-06-30 13:31:14 +0000 | [diff] [blame] | 351 | #ifdef HAVE_LCD_BITMAP |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 352 | |
Jens Arnold | b0a4b3e | 2005-11-17 00:13:07 +0000 | [diff] [blame] | 353 | #if LCD_DEPTH == 16 |
| 354 | #define BMP_COMPRESSION 3 /* BI_BITFIELDS */ |
| 355 | #define BMP_NUMCOLORS 3 |
| 356 | #else |
| 357 | #define BMP_COMPRESSION 0 /* BI_RGB */ |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 358 | #if LCD_DEPTH <= 8 |
| 359 | #define BMP_NUMCOLORS (1 << LCD_DEPTH) |
| 360 | #else |
| 361 | #define BMP_NUMCOLORS 0 |
| 362 | #endif |
Jens Arnold | b0a4b3e | 2005-11-17 00:13:07 +0000 | [diff] [blame] | 363 | #endif |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 364 | |
| 365 | #if LCD_DEPTH == 1 |
| 366 | #define BMP_BPP 1 |
| 367 | #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3) |
| 368 | #elif LCD_DEPTH <= 4 |
| 369 | #define BMP_BPP 4 |
| 370 | #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3) |
| 371 | #elif LCD_DEPTH <= 8 |
| 372 | #define BMP_BPP 8 |
| 373 | #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3) |
Jens Arnold | b0a4b3e | 2005-11-17 00:13:07 +0000 | [diff] [blame] | 374 | #elif LCD_DEPTH <= 16 |
| 375 | #define BMP_BPP 16 |
| 376 | #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3) |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 377 | #else |
| 378 | #define BMP_BPP 24 |
| 379 | #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3) |
| 380 | #endif |
| 381 | |
| 382 | #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS) |
| 383 | #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT) |
| 384 | #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE) |
| 385 | |
| 386 | #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff |
| 387 | #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff |
| 388 | |
Jens Arnold | abe77a1 | 2004-08-01 21:50:57 +0000 | [diff] [blame] | 389 | static const unsigned char bmpheader[] = |
Linus Nielsen Feltzing | a97de95 | 2003-04-14 15:58:51 +0000 | [diff] [blame] | 390 | { |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 391 | 0x42, 0x4d, /* 'BM' */ |
| 392 | LE32_CONST(BMP_TOTALSIZE), /* Total file size */ |
| 393 | 0x00, 0x00, 0x00, 0x00, /* Reserved */ |
| 394 | LE32_CONST(BMP_HEADERSIZE), /* Offset to start of pixel data */ |
| 395 | |
| 396 | 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */ |
| 397 | LE32_CONST(LCD_WIDTH), /* Width in pixels */ |
| 398 | LE32_CONST(LCD_HEIGHT), /* Height in pixels */ |
| 399 | 0x01, 0x00, /* Number of planes (always 1) */ |
| 400 | LE16_CONST(BMP_BPP), /* Bits per pixel 1/4/8/16/24 */ |
Jens Arnold | b0a4b3e | 2005-11-17 00:13:07 +0000 | [diff] [blame] | 401 | LE32_CONST(BMP_COMPRESSION),/* Compression mode */ |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 402 | LE32_CONST(BMP_DATASIZE), /* Size of bitmap data */ |
| 403 | 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */ |
| 404 | 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */ |
| 405 | LE32_CONST(BMP_NUMCOLORS), /* Number of used colours */ |
| 406 | LE32_CONST(BMP_NUMCOLORS), /* Number of important colours */ |
| 407 | |
| 408 | #if LCD_DEPTH == 1 |
Robert Kukla | 3e3e1d5 | 2008-03-02 21:28:26 +0000 | [diff] [blame] | 409 | #ifdef MROBE_100 |
| 410 | 2, 2, 94, 0x00, /* Colour #0 */ |
| 411 | 3, 6, 241, 0x00 /* Colour #1 */ |
Robert Kukla | f3ef9aa | 2008-03-02 21:40:50 +0000 | [diff] [blame] | 412 | #else |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 413 | 0x90, 0xee, 0x90, 0x00, /* Colour #0 */ |
| 414 | 0x00, 0x00, 0x00, 0x00 /* Colour #1 */ |
Robert Kukla | 3e3e1d5 | 2008-03-02 21:28:26 +0000 | [diff] [blame] | 415 | #endif |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 416 | #elif LCD_DEPTH == 2 |
| 417 | 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */ |
| 418 | 0x99, 0x90, 0x73, 0x00, /* Colour #1 */ |
| 419 | 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */ |
| 420 | 0x00, 0x00, 0x00, 0x00 /* Colour #3 */ |
Jens Arnold | b0a4b3e | 2005-11-17 00:13:07 +0000 | [diff] [blame] | 421 | #elif LCD_DEPTH == 16 |
| 422 | 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */ |
| 423 | 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */ |
| 424 | 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */ |
Linus Nielsen Feltzing | 6dca87e | 2005-02-11 14:38:44 +0000 | [diff] [blame] | 425 | #endif |
Linus Nielsen Feltzing | a97de95 | 2003-04-14 15:58:51 +0000 | [diff] [blame] | 426 | }; |
| 427 | |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 428 | static void (*screen_dump_hook)(int fh) = NULL; |
| 429 | |
Linus Nielsen Feltzing | a97de95 | 2003-04-14 15:58:51 +0000 | [diff] [blame] | 430 | void screen_dump(void) |
| 431 | { |
Jens Arnold | 0f04656 | 2004-09-03 22:24:29 +0000 | [diff] [blame] | 432 | int fh; |
Linus Nielsen Feltzing | a97de95 | 2003-04-14 15:58:51 +0000 | [diff] [blame] | 433 | char filename[MAX_PATH]; |
Dave Chapman | b27ecbd | 2005-11-13 16:13:00 +0000 | [diff] [blame] | 434 | int bx, by; |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 435 | #if LCD_DEPTH == 1 |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 436 | static unsigned char line_block[8][BMP_LINESIZE]; |
| 437 | #elif LCD_DEPTH == 2 |
Jens Arnold | da5cef6 | 2006-02-19 12:31:04 +0000 | [diff] [blame] | 438 | #if LCD_PIXELFORMAT == HORIZONTAL_PACKING |
| 439 | static unsigned char line_block[BMP_LINESIZE]; |
Jens Arnold | fc2a757 | 2008-03-14 10:13:58 +0000 | [diff] [blame] | 440 | #elif LCD_PIXELFORMAT == VERTICAL_PACKING |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 441 | static unsigned char line_block[4][BMP_LINESIZE]; |
Jens Arnold | fc2a757 | 2008-03-14 10:13:58 +0000 | [diff] [blame] | 442 | #elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED |
| 443 | static unsigned char line_block[8][BMP_LINESIZE]; |
Jens Arnold | da5cef6 | 2006-02-19 12:31:04 +0000 | [diff] [blame] | 444 | #endif |
Dave Chapman | b27ecbd | 2005-11-13 16:13:00 +0000 | [diff] [blame] | 445 | #elif LCD_DEPTH == 16 |
Jens Arnold | b0a4b3e | 2005-11-17 00:13:07 +0000 | [diff] [blame] | 446 | static unsigned short line_block[BMP_LINESIZE/2]; |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 447 | #endif |
| 448 | |
Jens Arnold | 2c7b127 | 2007-03-16 23:47:03 +0000 | [diff] [blame] | 449 | #if CONFIG_RTC |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 450 | create_datetime_filename(filename, "", "dump ", ".bmp", false); |
Linus Nielsen Feltzing | 6dca87e | 2005-02-11 14:38:44 +0000 | [diff] [blame] | 451 | #else |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 452 | create_numbered_filename(filename, "", "dump_", ".bmp", 4 |
| 453 | IF_CNFN_NUM_(, NULL)); |
Linus Nielsen Feltzing | 6dca87e | 2005-02-11 14:38:44 +0000 | [diff] [blame] | 454 | #endif |
Jens Arnold | 0f04656 | 2004-09-03 22:24:29 +0000 | [diff] [blame] | 455 | |
Jens Arnold | 67eb154 | 2007-02-01 23:08:15 +0000 | [diff] [blame] | 456 | fh = creat(filename); |
Jens Arnold | 0f04656 | 2004-09-03 22:24:29 +0000 | [diff] [blame] | 457 | if (fh < 0) |
| 458 | return; |
| 459 | |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 460 | if (screen_dump_hook) |
| 461 | { |
| 462 | screen_dump_hook(fh); |
| 463 | } |
| 464 | else |
| 465 | { |
| 466 | write(fh, bmpheader, sizeof(bmpheader)); |
Jens Arnold | 0f04656 | 2004-09-03 22:24:29 +0000 | [diff] [blame] | 467 | |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 468 | /* BMP image goes bottom up */ |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 469 | #if LCD_DEPTH == 1 |
Jens Arnold | 51223e5 | 2007-02-20 19:31:34 +0000 | [diff] [blame] | 470 | for (by = LCD_FBHEIGHT - 1; by >= 0; by--) |
Linus Nielsen Feltzing | a97de95 | 2003-04-14 15:58:51 +0000 | [diff] [blame] | 471 | { |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 472 | unsigned char *src = &lcd_framebuffer[by][0]; |
| 473 | unsigned char *dst = &line_block[0][0]; |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 474 | |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 475 | memset(line_block, 0, sizeof(line_block)); |
| 476 | for (bx = LCD_WIDTH/8; bx > 0; bx--) |
Jens Arnold | 0f04656 | 2004-09-03 22:24:29 +0000 | [diff] [blame] | 477 | { |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 478 | unsigned dst_mask = 0x80; |
| 479 | int ix; |
| 480 | |
| 481 | for (ix = 8; ix > 0; ix--) |
Jens Arnold | 0f04656 | 2004-09-03 22:24:29 +0000 | [diff] [blame] | 482 | { |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 483 | unsigned char *dst_blk = dst; |
| 484 | unsigned src_byte = *src++; |
| 485 | int iy; |
| 486 | |
| 487 | for (iy = 8; iy > 0; iy--) |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 488 | { |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 489 | if (src_byte & 0x80) |
| 490 | *dst_blk |= dst_mask; |
| 491 | src_byte <<= 1; |
| 492 | dst_blk += BMP_LINESIZE; |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 493 | } |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 494 | dst_mask >>= 1; |
Jens Arnold | 0f04656 | 2004-09-03 22:24:29 +0000 | [diff] [blame] | 495 | } |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 496 | dst++; |
Jens Arnold | 0f04656 | 2004-09-03 22:24:29 +0000 | [diff] [blame] | 497 | } |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 498 | |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 499 | write(fh, line_block, sizeof(line_block)); |
Linus Nielsen Feltzing | a97de95 | 2003-04-14 15:58:51 +0000 | [diff] [blame] | 500 | } |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 501 | #elif LCD_DEPTH == 2 |
Jens Arnold | da5cef6 | 2006-02-19 12:31:04 +0000 | [diff] [blame] | 502 | #if LCD_PIXELFORMAT == HORIZONTAL_PACKING |
Jens Arnold | 51223e5 | 2007-02-20 19:31:34 +0000 | [diff] [blame] | 503 | for (by = LCD_FBHEIGHT - 1; by >= 0; by--) |
Jens Arnold | da5cef6 | 2006-02-19 12:31:04 +0000 | [diff] [blame] | 504 | { |
| 505 | unsigned char *src = &lcd_framebuffer[by][0]; |
| 506 | unsigned char *dst = line_block; |
| 507 | |
| 508 | memset(line_block, 0, sizeof(line_block)); |
Jens Arnold | 51223e5 | 2007-02-20 19:31:34 +0000 | [diff] [blame] | 509 | for (bx = LCD_FBWIDTH; bx > 0; bx--) |
Jens Arnold | da5cef6 | 2006-02-19 12:31:04 +0000 | [diff] [blame] | 510 | { |
| 511 | unsigned src_byte = *src++; |
Jens Arnold | 51223e5 | 2007-02-20 19:31:34 +0000 | [diff] [blame] | 512 | |
Jens Arnold | 542d9b5 | 2006-05-03 21:26:38 +0000 | [diff] [blame] | 513 | *dst++ = ((src_byte >> 2) & 0x30) | ((src_byte >> 4) & 0x03); |
| 514 | *dst++ = ((src_byte << 2) & 0x30) | (src_byte & 0x03); |
Jens Arnold | da5cef6 | 2006-02-19 12:31:04 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | write(fh, line_block, sizeof(line_block)); |
| 518 | } |
Jens Arnold | fc2a757 | 2008-03-14 10:13:58 +0000 | [diff] [blame] | 519 | #elif LCD_PIXELFORMAT == VERTICAL_PACKING |
Jens Arnold | 51223e5 | 2007-02-20 19:31:34 +0000 | [diff] [blame] | 520 | for (by = LCD_FBHEIGHT - 1; by >= 0; by--) |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 521 | { |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 522 | unsigned char *src = &lcd_framebuffer[by][0]; |
| 523 | unsigned char *dst = &line_block[3][0]; |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 524 | |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 525 | memset(line_block, 0, sizeof(line_block)); |
| 526 | for (bx = LCD_WIDTH/2; bx > 0; bx--) |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 527 | { |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 528 | unsigned char *dst_blk = dst++; |
Jens Arnold | fc2a757 | 2008-03-14 10:13:58 +0000 | [diff] [blame] | 529 | unsigned src_byte0 = *src++ << 4; |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 530 | unsigned src_byte1 = *src++; |
| 531 | int iy; |
| 532 | |
| 533 | for (iy = 4; iy > 0; iy--) |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 534 | { |
Jens Arnold | fc2a757 | 2008-03-14 10:13:58 +0000 | [diff] [blame] | 535 | *dst_blk = (src_byte0 & 0x30) | (src_byte1 & 0x03); |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 536 | src_byte0 >>= 2; |
| 537 | src_byte1 >>= 2; |
| 538 | dst_blk -= BMP_LINESIZE; |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 542 | write(fh, line_block, sizeof(line_block)); |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 543 | } |
Jens Arnold | fc2a757 | 2008-03-14 10:13:58 +0000 | [diff] [blame] | 544 | #elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED |
| 545 | for (by = LCD_FBHEIGHT - 1; by >= 0; by--) |
| 546 | { |
| 547 | const fb_data *src = &lcd_framebuffer[by][0]; |
| 548 | unsigned char *dst = &line_block[7][0]; |
| 549 | |
| 550 | memset(line_block, 0, sizeof(line_block)); |
| 551 | for (bx = LCD_WIDTH/2; bx > 0; bx--) |
| 552 | { |
| 553 | unsigned char *dst_blk = dst++; |
| 554 | unsigned src_data0 = *src++ << 4; |
| 555 | unsigned src_data1 = *src++; |
| 556 | int iy; |
| 557 | |
| 558 | for (iy = 8; iy > 0; iy--) |
| 559 | { |
| 560 | *dst_blk = (src_data0 & 0x10) | (src_data1 & 0x01) |
| 561 | | ((src_data0 & 0x1000) | (src_data1 & 0x0100)) >> 7; |
| 562 | src_data0 >>= 1; |
| 563 | src_data1 >>= 1; |
| 564 | dst_blk -= BMP_LINESIZE; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | write(fh, line_block, sizeof(line_block)); |
| 569 | } |
Jens Arnold | da5cef6 | 2006-02-19 12:31:04 +0000 | [diff] [blame] | 570 | #endif |
Jens Arnold | b0a4b3e | 2005-11-17 00:13:07 +0000 | [diff] [blame] | 571 | #elif LCD_DEPTH == 16 |
Dave Chapman | b27ecbd | 2005-11-13 16:13:00 +0000 | [diff] [blame] | 572 | for (by = LCD_HEIGHT - 1; by >= 0; by--) |
| 573 | { |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 574 | unsigned short *src = &lcd_framebuffer[by][0]; |
| 575 | unsigned short *dst = line_block; |
Dave Chapman | b27ecbd | 2005-11-13 16:13:00 +0000 | [diff] [blame] | 576 | |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 577 | memset(line_block, 0, sizeof(line_block)); |
| 578 | for (bx = LCD_WIDTH; bx > 0; bx--) |
Dave Chapman | b27ecbd | 2005-11-13 16:13:00 +0000 | [diff] [blame] | 579 | { |
Dave Chapman | 664a418 | 2005-11-19 02:14:16 +0000 | [diff] [blame] | 580 | #if (LCD_PIXELFORMAT == RGB565SWAPPED) |
Jens Arnold | b0a4b3e | 2005-11-17 00:13:07 +0000 | [diff] [blame] | 581 | /* iPod LCD data is big endian although the CPU is not */ |
Jens Arnold | 4205407 | 2005-11-26 23:50:24 +0000 | [diff] [blame] | 582 | *dst++ = htobe16(*src++); |
Jens Arnold | b0a4b3e | 2005-11-17 00:13:07 +0000 | [diff] [blame] | 583 | #else |
| 584 | *dst++ = htole16(*src++); |
| 585 | #endif |
Dave Chapman | b27ecbd | 2005-11-13 16:13:00 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | write(fh, line_block, sizeof(line_block)); |
| 589 | } |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 590 | #endif /* LCD_DEPTH */ |
Jens Arnold | f894a4c | 2005-07-06 22:58:02 +0000 | [diff] [blame] | 591 | } |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 592 | |
Jens Arnold | 0f04656 | 2004-09-03 22:24:29 +0000 | [diff] [blame] | 593 | close(fh); |
Linus Nielsen Feltzing | a97de95 | 2003-04-14 15:58:51 +0000 | [diff] [blame] | 594 | } |
Jens Arnold | 05042af | 2005-10-01 10:20:55 +0000 | [diff] [blame] | 595 | |
| 596 | void screen_dump_set_hook(void (*hook)(int fh)) |
| 597 | { |
| 598 | screen_dump_hook = hook; |
| 599 | } |
| 600 | |
| 601 | #endif /* HAVE_LCD_BITMAP */ |
Linus Nielsen Feltzing | 26440c9 | 2004-07-05 11:15:50 +0000 | [diff] [blame] | 602 | |
Jens Arnold | 0f04656 | 2004-09-03 22:24:29 +0000 | [diff] [blame] | 603 | /* parse a line from a configuration file. the line format is: |
Linus Nielsen Feltzing | 26440c9 | 2004-07-05 11:15:50 +0000 | [diff] [blame] | 604 | |
| 605 | name: value |
| 606 | |
| 607 | Any whitespace before setting name or value (after ':') is ignored. |
| 608 | A # as first non-whitespace character discards the whole line. |
| 609 | Function sets pointers to null-terminated setting name and value. |
| 610 | Returns false if no valid config entry was found. |
| 611 | */ |
| 612 | |
| 613 | bool settings_parseline(char* line, char** name, char** value) |
| 614 | { |
| 615 | char* ptr; |
| 616 | |
| 617 | while ( isspace(*line) ) |
| 618 | line++; |
| 619 | |
| 620 | if ( *line == '#' ) |
| 621 | return false; |
| 622 | |
| 623 | ptr = strchr(line, ':'); |
| 624 | if ( !ptr ) |
| 625 | return false; |
| 626 | |
| 627 | *name = line; |
| 628 | *ptr = 0; |
| 629 | ptr++; |
| 630 | while (isspace(*ptr)) |
| 631 | ptr++; |
| 632 | *value = ptr; |
| 633 | return true; |
| 634 | } |
Linus Nielsen Feltzing | 5b5003d | 2004-07-24 21:26:41 +0000 | [diff] [blame] | 635 | |
Linus Nielsen Feltzing | 74353a7 | 2005-09-14 09:07:05 +0000 | [diff] [blame] | 636 | static void system_flush(void) |
| 637 | { |
| 638 | tree_flush(); |
Jonathan Gordon | 6581146 | 2006-12-12 10:45:34 +0000 | [diff] [blame] | 639 | call_ata_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */ |
Linus Nielsen Feltzing | 74353a7 | 2005-09-14 09:07:05 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | static void system_restore(void) |
| 643 | { |
| 644 | tree_restore(); |
| 645 | } |
| 646 | |
Linus Nielsen Feltzing | 670abf9 | 2005-08-18 14:23:18 +0000 | [diff] [blame] | 647 | static bool clean_shutdown(void (*callback)(void *), void *parameter) |
Linus Nielsen Feltzing | 5b5003d | 2004-07-24 21:26:41 +0000 | [diff] [blame] | 648 | { |
Björn Stenberg | 6c33c51 | 2004-09-19 21:58:37 +0000 | [diff] [blame] | 649 | #ifdef SIMULATOR |
Linus Nielsen Feltzing | 49483bc | 2005-08-18 14:46:35 +0000 | [diff] [blame] | 650 | (void)callback; |
| 651 | (void)parameter; |
Magnus Holmgren | a50c2b1 | 2007-08-22 16:33:34 +0000 | [diff] [blame] | 652 | bookmark_autobookmark(); |
Jonathan Gordon | 4049d44 | 2006-11-26 09:53:42 +0000 | [diff] [blame] | 653 | call_ata_idle_notifys(true); |
Björn Stenberg | 6c33c51 | 2004-09-19 21:58:37 +0000 | [diff] [blame] | 654 | exit(0); |
| 655 | #else |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 656 | long msg_id = -1; |
Jens Arnold | 5074ec0 | 2006-04-24 06:34:45 +0000 | [diff] [blame] | 657 | int i; |
| 658 | |
Peter D'Hoye | 12d2d0f | 2007-06-24 18:46:04 +0000 | [diff] [blame] | 659 | scrobbler_poweroff(); |
| 660 | |
Jonathan Gordon | 9a6f419 | 2007-02-18 05:32:06 +0000 | [diff] [blame] | 661 | #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING) |
Linus Nielsen Feltzing | 5b5003d | 2004-07-24 21:26:41 +0000 | [diff] [blame] | 662 | if(!charger_inserted()) |
Linus Nielsen Feltzing | 6da6801 | 2005-02-10 21:53:41 +0000 | [diff] [blame] | 663 | #endif |
Linus Nielsen Feltzing | 5b5003d | 2004-07-24 21:26:41 +0000 | [diff] [blame] | 664 | { |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 665 | bool batt_safe = battery_level_safe(); |
Michael Sevakis | 0d768a3 | 2006-12-07 19:34:26 +0000 | [diff] [blame] | 666 | int audio_stat = audio_status(); |
| 667 | |
Jens Arnold | 5074ec0 | 2006-04-24 06:34:45 +0000 | [diff] [blame] | 668 | FOR_NB_SCREENS(i) |
| 669 | screens[i].clear_display(); |
Jens Arnold | 671f5d4 | 2007-08-12 11:18:52 +0000 | [diff] [blame] | 670 | |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 671 | if (batt_safe) |
| 672 | { |
Jonathan Gordon | 710ccb7 | 2006-10-25 10:17:57 +0000 | [diff] [blame] | 673 | #ifdef HAVE_TAGCACHE |
Barry Wardell | 8d2711b | 2006-11-11 01:18:57 +0000 | [diff] [blame] | 674 | if (!tagcache_prepare_shutdown()) |
| 675 | { |
| 676 | cancel_shutdown(); |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 677 | gui_syncsplash(HZ, ID2P(LANG_TAGCACHE_BUSY)); |
Barry Wardell | 8d2711b | 2006-11-11 01:18:57 +0000 | [diff] [blame] | 678 | return false; |
| 679 | } |
Jonathan Gordon | 710ccb7 | 2006-10-25 10:17:57 +0000 | [diff] [blame] | 680 | #endif |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 681 | if (battery_level() > 10) |
| 682 | gui_syncsplash(0, str(LANG_SHUTTINGDOWN)); |
| 683 | else |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 684 | { |
| 685 | msg_id = LANG_WARNING_BATTERY_LOW; |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 686 | gui_syncsplash(0, "%s %s", |
| 687 | str(LANG_WARNING_BATTERY_LOW), |
| 688 | str(LANG_SHUTTINGDOWN)); |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 689 | } |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 690 | } |
| 691 | else |
| 692 | { |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 693 | msg_id = LANG_WARNING_BATTERY_EMPTY; |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 694 | gui_syncsplash(0, "%s %s", |
| 695 | str(LANG_WARNING_BATTERY_EMPTY), |
| 696 | str(LANG_SHUTTINGDOWN)); |
Barry Wardell | 8d2711b | 2006-11-11 01:18:57 +0000 | [diff] [blame] | 697 | } |
Miika Pekkarinen | 6ee82e7 | 2006-09-23 10:29:14 +0000 | [diff] [blame] | 698 | |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 699 | if (global_settings.fade_on_stop |
Michael Sevakis | 0d768a3 | 2006-12-07 19:34:26 +0000 | [diff] [blame] | 700 | && (audio_stat & AUDIO_STATUS_PLAY)) |
Miika Pekkarinen | 53d662b | 2006-10-18 12:59:13 +0000 | [diff] [blame] | 701 | { |
Jonathan Gordon | 4982b87 | 2008-06-23 02:45:55 +0000 | [diff] [blame] | 702 | fade(false, false); |
Miika Pekkarinen | 53d662b | 2006-10-18 12:59:13 +0000 | [diff] [blame] | 703 | } |
Michael Sevakis | 0d768a3 | 2006-12-07 19:34:26 +0000 | [diff] [blame] | 704 | |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 705 | if (batt_safe) /* do not save on critical battery */ |
Michael Sevakis | 0d768a3 | 2006-12-07 19:34:26 +0000 | [diff] [blame] | 706 | { |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 707 | #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC |
| 708 | if (audio_stat & AUDIO_STATUS_RECORD) |
Michael Sevakis | f2c1987 | 2007-07-25 02:15:04 +0000 | [diff] [blame] | 709 | { |
Peter D'Hoye | 583f186 | 2007-08-25 15:53:54 +0000 | [diff] [blame] | 710 | rec_command(RECORDING_CMD_STOP); |
Michael Sevakis | f2c1987 | 2007-07-25 02:15:04 +0000 | [diff] [blame] | 711 | /* wait for stop to complete */ |
| 712 | while (audio_status() & AUDIO_STATUS_RECORD) |
| 713 | sleep(1); |
| 714 | } |
Michael Sevakis | 0d768a3 | 2006-12-07 19:34:26 +0000 | [diff] [blame] | 715 | #endif |
Magnus Holmgren | a50c2b1 | 2007-08-22 16:33:34 +0000 | [diff] [blame] | 716 | bookmark_autobookmark(); |
| 717 | |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 718 | /* audio_stop_recording == audio_stop for HWCODEC */ |
| 719 | audio_stop(); |
Michael Sevakis | 0d768a3 | 2006-12-07 19:34:26 +0000 | [diff] [blame] | 720 | |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 721 | if (callback != NULL) |
| 722 | callback(parameter); |
Linus Nielsen Feltzing | 74353a7 | 2005-09-14 09:07:05 +0000 | [diff] [blame] | 723 | |
Michael Sevakis | f2c1987 | 2007-07-25 02:15:04 +0000 | [diff] [blame] | 724 | #if CONFIG_CODEC != SWCODEC |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 725 | /* wait for audio_stop or audio_stop_recording to complete */ |
| 726 | while (audio_status()) |
| 727 | sleep(1); |
Michael Sevakis | f2c1987 | 2007-07-25 02:15:04 +0000 | [diff] [blame] | 728 | #endif |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 729 | |
| 730 | #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC |
| 731 | audio_close_recording(); |
| 732 | #endif |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 733 | |
Steve Bavin | 32a9575 | 2007-10-19 15:31:42 +0000 | [diff] [blame] | 734 | if(global_settings.talk_menu) |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 735 | { |
| 736 | bool enqueue = false; |
| 737 | if(msg_id != -1) |
| 738 | { |
| 739 | talk_id(msg_id, enqueue); |
| 740 | enqueue = true; |
| 741 | } |
| 742 | talk_id(LANG_SHUTTINGDOWN, enqueue); |
| 743 | #if CONFIG_CODEC == SWCODEC |
| 744 | voice_wait(); |
| 745 | #endif |
| 746 | } |
| 747 | |
Barry Wardell | 8d2711b | 2006-11-11 01:18:57 +0000 | [diff] [blame] | 748 | system_flush(); |
Peter D'Hoye | c4a59a2 | 2006-08-15 22:54:06 +0000 | [diff] [blame] | 749 | #ifdef HAVE_EEPROM_SETTINGS |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 750 | if (firmware_settings.initialized) |
| 751 | { |
| 752 | firmware_settings.disk_clean = true; |
| 753 | firmware_settings.bl_version = 0; |
| 754 | eeprom_settings_store(); |
| 755 | } |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 756 | #endif |
Jens Arnold | da5910e | 2007-06-29 19:01:24 +0000 | [diff] [blame] | 757 | } |
| 758 | #ifdef HAVE_DIRCACHE |
| 759 | else |
| 760 | dircache_disable(); |
| 761 | #endif |
| 762 | |
Christi Scarborough | 247fe6d | 2005-02-06 09:57:57 +0000 | [diff] [blame] | 763 | shutdown_hw(); |
Linus Nielsen Feltzing | 5b5003d | 2004-07-24 21:26:41 +0000 | [diff] [blame] | 764 | } |
| 765 | #endif |
| 766 | return false; |
| 767 | } |
Linus Nielsen Feltzing | ade5d7b | 2004-07-26 16:06:59 +0000 | [diff] [blame] | 768 | |
Jens Arnold | 178c565 | 2007-04-09 13:39:37 +0000 | [diff] [blame] | 769 | bool list_stop_handler(void) |
| 770 | { |
| 771 | bool ret = false; |
| 772 | |
| 773 | /* Stop the music if it is playing */ |
| 774 | if(audio_status()) |
| 775 | { |
| 776 | if (!global_settings.party_mode) |
| 777 | { |
| 778 | if (global_settings.fade_on_stop) |
Jonathan Gordon | 4982b87 | 2008-06-23 02:45:55 +0000 | [diff] [blame] | 779 | fade(false, false); |
Jens Arnold | 178c565 | 2007-04-09 13:39:37 +0000 | [diff] [blame] | 780 | bookmark_autobookmark(); |
| 781 | audio_stop(); |
Magnus Holmgren | 1d9f693 | 2007-08-05 15:21:47 +0000 | [diff] [blame] | 782 | ret = true; /* bookmarking can make a refresh necessary */ |
Jens Arnold | 178c565 | 2007-04-09 13:39:37 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | #if CONFIG_CHARGING |
| 786 | #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF) |
| 787 | else |
| 788 | { |
| 789 | if (charger_inserted()) |
| 790 | charging_splash(); |
| 791 | else |
| 792 | shutdown_screen(); /* won't return if shutdown actually happens */ |
| 793 | |
| 794 | ret = true; /* screen is dirty, caller needs to refresh */ |
| 795 | } |
| 796 | #endif |
| 797 | #ifndef HAVE_POWEROFF_WHILE_CHARGING |
| 798 | { |
| 799 | static long last_off = 0; |
| 800 | |
| 801 | if (TIME_BEFORE(current_tick, last_off + HZ/2)) |
| 802 | { |
| 803 | if (charger_inserted()) |
| 804 | { |
| 805 | charging_splash(); |
| 806 | ret = true; /* screen is dirty, caller needs to refresh */ |
| 807 | } |
| 808 | } |
| 809 | last_off = current_tick; |
| 810 | } |
| 811 | #endif |
| 812 | #endif /* CONFIG_CHARGING */ |
| 813 | return ret; |
| 814 | } |
| 815 | |
Jonathan Gordon | 9a6f419 | 2007-02-18 05:32:06 +0000 | [diff] [blame] | 816 | #if CONFIG_CHARGING |
Linus Nielsen Feltzing | 3d21781 | 2005-04-06 11:12:22 +0000 | [diff] [blame] | 817 | static bool waiting_to_resume_play = false; |
| 818 | static long play_resume_tick; |
| 819 | |
| 820 | static void car_adapter_mode_processing(bool inserted) |
| 821 | { |
| 822 | if (global_settings.car_adapter_mode) |
| 823 | { |
| 824 | if(inserted) |
| 825 | { |
| 826 | /* |
| 827 | * Just got plugged in, delay & resume if we were playing |
| 828 | */ |
| 829 | if (audio_status() & AUDIO_STATUS_PAUSE) |
| 830 | { |
| 831 | /* delay resume a bit while the engine is cranking */ |
| 832 | play_resume_tick = current_tick + HZ*5; |
| 833 | waiting_to_resume_play = true; |
| 834 | } |
| 835 | } |
| 836 | else |
| 837 | { |
| 838 | /* |
| 839 | * Just got unplugged, pause if playing |
| 840 | */ |
| 841 | if ((audio_status() & AUDIO_STATUS_PLAY) && |
| 842 | !(audio_status() & AUDIO_STATUS_PAUSE)) |
| 843 | { |
Björn Stenberg | 8cece5a | 2007-01-08 21:25:46 +0000 | [diff] [blame] | 844 | if (global_settings.fade_on_stop) |
Jonathan Gordon | 4982b87 | 2008-06-23 02:45:55 +0000 | [diff] [blame] | 845 | fade(false, false); |
Björn Stenberg | 8cece5a | 2007-01-08 21:25:46 +0000 | [diff] [blame] | 846 | else |
| 847 | audio_pause(); |
Linus Nielsen Feltzing | 3d21781 | 2005-04-06 11:12:22 +0000 | [diff] [blame] | 848 | } |
Linus Nielsen Feltzing | 1630188 | 2007-10-08 09:09:40 +0000 | [diff] [blame] | 849 | waiting_to_resume_play = false; |
Linus Nielsen Feltzing | 3d21781 | 2005-04-06 11:12:22 +0000 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | static void car_adapter_tick(void) |
| 855 | { |
| 856 | if (waiting_to_resume_play) |
| 857 | { |
| 858 | if (TIME_AFTER(current_tick, play_resume_tick)) |
| 859 | { |
| 860 | if (audio_status() & AUDIO_STATUS_PAUSE) |
| 861 | { |
Linus Nielsen Feltzing | 1630188 | 2007-10-08 09:09:40 +0000 | [diff] [blame] | 862 | queue_broadcast(SYS_CAR_ADAPTER_RESUME, 0); |
Linus Nielsen Feltzing | 3d21781 | 2005-04-06 11:12:22 +0000 | [diff] [blame] | 863 | } |
| 864 | waiting_to_resume_play = false; |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | void car_adapter_mode_init(void) |
| 870 | { |
| 871 | tick_add_task(car_adapter_tick); |
| 872 | } |
| 873 | #endif |
| 874 | |
Linus Nielsen Feltzing | 537c754 | 2006-09-26 10:03:56 +0000 | [diff] [blame] | 875 | #ifdef HAVE_HEADPHONE_DETECTION |
Jens Arnold | 314f3b5 | 2006-12-12 22:22:21 +0000 | [diff] [blame] | 876 | static void unplug_change(bool inserted) |
Linus Nielsen Feltzing | 537c754 | 2006-09-26 10:03:56 +0000 | [diff] [blame] | 877 | { |
Brandon Low | 7b1de20 | 2008-03-18 07:01:23 +0000 | [diff] [blame] | 878 | static bool headphone_caused_pause = false; |
| 879 | |
Linus Nielsen Feltzing | 537c754 | 2006-09-26 10:03:56 +0000 | [diff] [blame] | 880 | if (global_settings.unplug_mode) |
| 881 | { |
Brandon Low | 7b1de20 | 2008-03-18 07:01:23 +0000 | [diff] [blame] | 882 | int audio_stat = audio_status(); |
Linus Nielsen Feltzing | 537c754 | 2006-09-26 10:03:56 +0000 | [diff] [blame] | 883 | if (inserted) |
| 884 | { |
Brandon Low | 7b1de20 | 2008-03-18 07:01:23 +0000 | [diff] [blame] | 885 | if ((audio_stat & AUDIO_STATUS_PLAY) && |
| 886 | headphone_caused_pause && |
| 887 | global_settings.unplug_mode > 1 ) |
Linus Nielsen Feltzing | 537c754 | 2006-09-26 10:03:56 +0000 | [diff] [blame] | 888 | audio_resume(); |
| 889 | backlight_on(); |
Brandon Low | 7b1de20 | 2008-03-18 07:01:23 +0000 | [diff] [blame] | 890 | headphone_caused_pause = false; |
Linus Nielsen Feltzing | 537c754 | 2006-09-26 10:03:56 +0000 | [diff] [blame] | 891 | } else { |
Brandon Low | 7b1de20 | 2008-03-18 07:01:23 +0000 | [diff] [blame] | 892 | if ((audio_stat & AUDIO_STATUS_PLAY) && |
| 893 | !(audio_stat & AUDIO_STATUS_PAUSE)) |
Linus Nielsen Feltzing | 537c754 | 2006-09-26 10:03:56 +0000 | [diff] [blame] | 894 | { |
Brandon Low | 7b1de20 | 2008-03-18 07:01:23 +0000 | [diff] [blame] | 895 | headphone_caused_pause = true; |
| 896 | audio_pause(); |
| 897 | |
| 898 | if (global_settings.unplug_rw) |
| 899 | { |
| 900 | if (audio_current_track()->elapsed > |
| 901 | (unsigned long)(global_settings.unplug_rw*1000)) |
| 902 | audio_ff_rewind(audio_current_track()->elapsed - |
| 903 | (global_settings.unplug_rw*1000)); |
| 904 | else |
| 905 | audio_ff_rewind(0); |
| 906 | } |
Linus Nielsen Feltzing | 537c754 | 2006-09-26 10:03:56 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | #endif |
| 912 | |
Jean-Philippe Bernardy | f64939e | 2005-02-07 22:56:37 +0000 | [diff] [blame] | 913 | long default_event_handler_ex(long event, void (*callback)(void *), void *parameter) |
Linus Nielsen Feltzing | ade5d7b | 2004-07-26 16:06:59 +0000 | [diff] [blame] | 914 | { |
Linus Nielsen Feltzing | f749093 | 2004-07-26 23:31:40 +0000 | [diff] [blame] | 915 | switch(event) |
Linus Nielsen Feltzing | ade5d7b | 2004-07-26 16:06:59 +0000 | [diff] [blame] | 916 | { |
Stéphane Doyon | da93299 | 2007-11-03 05:00:49 +0000 | [diff] [blame] | 917 | case SYS_BATTERY_UPDATE: |
| 918 | if(global_settings.talk_battery_level) |
| 919 | { |
| 920 | talk_ids(true, VOICE_PAUSE, VOICE_PAUSE, |
| 921 | LANG_BATTERY_TIME, |
| 922 | TALK_ID(battery_level(), UNIT_PERCENT), |
| 923 | VOICE_PAUSE); |
| 924 | talk_force_enqueue_next(); |
| 925 | } |
| 926 | break; |
Linus Nielsen Feltzing | ade5d7b | 2004-07-26 16:06:59 +0000 | [diff] [blame] | 927 | case SYS_USB_CONNECTED: |
Jens Arnold | 0dc52d5 | 2004-10-12 22:45:01 +0000 | [diff] [blame] | 928 | if (callback != NULL) |
| 929 | callback(parameter); |
Jens Arnold | 7f6b640 | 2004-10-12 00:02:51 +0000 | [diff] [blame] | 930 | #ifdef HAVE_MMC |
Michael Sevakis | 1167e3c | 2007-06-30 02:08:27 +0000 | [diff] [blame] | 931 | if (!mmc_touched() || |
| 932 | (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED)) |
Jens Arnold | 7f6b640 | 2004-10-12 00:02:51 +0000 | [diff] [blame] | 933 | #endif |
Linus Nielsen Feltzing | 74353a7 | 2005-09-14 09:07:05 +0000 | [diff] [blame] | 934 | { |
Linus Nielsen Feltzing | da153da | 2006-10-19 09:42:58 +0000 | [diff] [blame] | 935 | scrobbler_flush_cache(); |
Linus Nielsen Feltzing | 74353a7 | 2005-09-14 09:07:05 +0000 | [diff] [blame] | 936 | system_flush(); |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 937 | #ifdef BOOTFILE |
Jens Arnold | ffb3dfd | 2007-08-01 20:21:34 +0000 | [diff] [blame] | 938 | #if !defined(USB_NONE) && !defined(USB_IPODSTYLE) |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 939 | check_bootfile(false); /* gets initial size */ |
| 940 | #endif |
Peter D'Hoye | dc33e1e | 2007-03-30 23:36:10 +0000 | [diff] [blame] | 941 | #endif |
Jens Arnold | 7f6b640 | 2004-10-12 00:02:51 +0000 | [diff] [blame] | 942 | usb_screen(); |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 943 | #ifdef BOOTFILE |
Jens Arnold | ffb3dfd | 2007-08-01 20:21:34 +0000 | [diff] [blame] | 944 | #if !defined(USB_NONE) && !defined(USB_IPODSTYLE) |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 945 | check_bootfile(true); |
| 946 | #endif |
Peter D'Hoye | dc33e1e | 2007-03-30 23:36:10 +0000 | [diff] [blame] | 947 | #endif |
Linus Nielsen Feltzing | 74353a7 | 2005-09-14 09:07:05 +0000 | [diff] [blame] | 948 | system_restore(); |
| 949 | } |
Linus Nielsen Feltzing | ade5d7b | 2004-07-26 16:06:59 +0000 | [diff] [blame] | 950 | return SYS_USB_CONNECTED; |
Linus Nielsen Feltzing | 0b3ea1b | 2004-10-12 11:00:19 +0000 | [diff] [blame] | 951 | case SYS_POWEROFF: |
Linus Nielsen Feltzing | 670abf9 | 2005-08-18 14:23:18 +0000 | [diff] [blame] | 952 | if (!clean_shutdown(callback, parameter)) |
Linus Nielsen Feltzing | 0b3ea1b | 2004-10-12 11:00:19 +0000 | [diff] [blame] | 953 | return SYS_POWEROFF; |
| 954 | break; |
Jonathan Gordon | 9a6f419 | 2007-02-18 05:32:06 +0000 | [diff] [blame] | 955 | #if CONFIG_CHARGING |
Linus Nielsen Feltzing | 3d21781 | 2005-04-06 11:12:22 +0000 | [diff] [blame] | 956 | case SYS_CHARGER_CONNECTED: |
| 957 | car_adapter_mode_processing(true); |
| 958 | return SYS_CHARGER_CONNECTED; |
| 959 | |
| 960 | case SYS_CHARGER_DISCONNECTED: |
| 961 | car_adapter_mode_processing(false); |
| 962 | return SYS_CHARGER_DISCONNECTED; |
Linus Nielsen Feltzing | 1630188 | 2007-10-08 09:09:40 +0000 | [diff] [blame] | 963 | |
| 964 | case SYS_CAR_ADAPTER_RESUME: |
| 965 | audio_resume(); |
| 966 | return SYS_CAR_ADAPTER_RESUME; |
Linus Nielsen Feltzing | 3d21781 | 2005-04-06 11:12:22 +0000 | [diff] [blame] | 967 | #endif |
Linus Nielsen Feltzing | 537c754 | 2006-09-26 10:03:56 +0000 | [diff] [blame] | 968 | #ifdef HAVE_HEADPHONE_DETECTION |
| 969 | case SYS_PHONE_PLUGGED: |
| 970 | unplug_change(true); |
| 971 | return SYS_PHONE_PLUGGED; |
| 972 | |
| 973 | case SYS_PHONE_UNPLUGGED: |
| 974 | unplug_change(false); |
| 975 | return SYS_PHONE_UNPLUGGED; |
| 976 | #endif |
Linus Nielsen Feltzing | ade5d7b | 2004-07-26 16:06:59 +0000 | [diff] [blame] | 977 | } |
| 978 | return 0; |
| 979 | } |
Jens Arnold | 0dc52d5 | 2004-10-12 22:45:01 +0000 | [diff] [blame] | 980 | |
Jean-Philippe Bernardy | f64939e | 2005-02-07 22:56:37 +0000 | [diff] [blame] | 981 | long default_event_handler(long event) |
Jens Arnold | 0dc52d5 | 2004-10-12 22:45:01 +0000 | [diff] [blame] | 982 | { |
| 983 | return default_event_handler_ex(event, NULL, NULL); |
| 984 | } |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 985 | |
| 986 | int show_logo( void ) |
| 987 | { |
| 988 | #ifdef HAVE_LCD_BITMAP |
| 989 | char version[32]; |
| 990 | int font_h, font_w; |
| 991 | |
Jens Arnold | f9b90e9 | 2007-04-06 22:55:00 +0000 | [diff] [blame] | 992 | snprintf(version, sizeof(version), "Ver. %s", appsversion); |
| 993 | |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 994 | lcd_clear_display(); |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 995 | lcd_bitmap(rockboxlogo, 0, 10, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo); |
Jens Arnold | f9b90e9 | 2007-04-06 22:55:00 +0000 | [diff] [blame] | 996 | lcd_setfont(FONT_SYSFIXED); |
| 997 | lcd_getstringsize((unsigned char *)"A", &font_w, &font_h); |
| 998 | lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2), |
| 999 | LCD_HEIGHT-font_h, (unsigned char *)version); |
Jonathan Gordon | c939774 | 2007-04-12 12:14:54 +0000 | [diff] [blame] | 1000 | lcd_setfont(FONT_UI); |
Jens Arnold | f9b90e9 | 2007-04-06 22:55:00 +0000 | [diff] [blame] | 1001 | |
| 1002 | #else |
| 1003 | char *rockbox = " ROCKbox!"; |
| 1004 | |
| 1005 | lcd_clear_display(); |
| 1006 | lcd_double_height(true); |
| 1007 | lcd_puts(0, 0, rockbox); |
| 1008 | lcd_puts_scroll(0, 1, appsversion); |
| 1009 | #endif |
| 1010 | lcd_update(); |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 1011 | |
| 1012 | #ifdef HAVE_REMOTE_LCD |
| 1013 | lcd_remote_clear_display(); |
Jens Arnold | a048346 | 2006-07-26 23:47:29 +0000 | [diff] [blame] | 1014 | lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo, |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 1015 | BMPHEIGHT_remote_rockboxlogo); |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 1016 | lcd_remote_setfont(FONT_SYSFIXED); |
Daniel Stenberg | f981ea9 | 2005-12-05 22:44:42 +0000 | [diff] [blame] | 1017 | lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h); |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 1018 | lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2), |
Jonathan Gordon | c939774 | 2007-04-12 12:14:54 +0000 | [diff] [blame] | 1019 | LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version); |
Marcoen Hirschberg | 88c05bd | 2007-04-12 13:36:49 +0000 | [diff] [blame] | 1020 | lcd_remote_setfont(FONT_UI); |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 1021 | lcd_remote_update(); |
| 1022 | #endif |
| 1023 | |
Linus Nielsen Feltzing | 60895bc | 2005-09-02 05:39:09 +0000 | [diff] [blame] | 1024 | return 0; |
| 1025 | } |
Magnus Holmgren | ea79924 | 2006-11-15 20:26:33 +0000 | [diff] [blame] | 1026 | |
| 1027 | #if CONFIG_CODEC == SWCODEC |
| 1028 | int get_replaygain_mode(bool have_track_gain, bool have_album_gain) |
| 1029 | { |
| 1030 | int type; |
| 1031 | |
| 1032 | bool track = ((global_settings.replaygain_type == REPLAYGAIN_TRACK) |
| 1033 | || ((global_settings.replaygain_type == REPLAYGAIN_SHUFFLE) |
| 1034 | && global_settings.playlist_shuffle)); |
| 1035 | |
| 1036 | type = (!track && have_album_gain) ? REPLAYGAIN_ALBUM |
| 1037 | : have_track_gain ? REPLAYGAIN_TRACK : -1; |
| 1038 | |
| 1039 | return type; |
| 1040 | } |
| 1041 | #endif |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 1042 | |
| 1043 | #ifdef BOOTFILE |
Jens Arnold | ffb3dfd | 2007-08-01 20:21:34 +0000 | [diff] [blame] | 1044 | #if !defined(USB_NONE) && !defined(USB_IPODSTYLE) |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 1045 | /* |
| 1046 | memorize/compare details about the BOOTFILE |
| 1047 | we don't use dircache because it may not be up to date after |
| 1048 | USB disconnect (scanning in the background) |
| 1049 | */ |
| 1050 | void check_bootfile(bool do_rolo) |
| 1051 | { |
Peter D'Hoye | 0db06ce | 2007-04-05 21:38:27 +0000 | [diff] [blame] | 1052 | static unsigned short wrtdate = 0; |
| 1053 | static unsigned short wrttime = 0; |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 1054 | DIR* dir = NULL; |
| 1055 | struct dirent* entry = NULL; |
| 1056 | |
Peter D'Hoye | bf92f9c | 2007-04-05 20:24:07 +0000 | [diff] [blame] | 1057 | /* 1. open BOOTDIR and find the BOOTFILE dir entry */ |
| 1058 | dir = opendir(BOOTDIR); |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 1059 | |
| 1060 | if(!dir) return; /* do we want an error splash? */ |
| 1061 | |
Peter D'Hoye | bf92f9c | 2007-04-05 20:24:07 +0000 | [diff] [blame] | 1062 | /* loop all files in BOOTDIR */ |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 1063 | while(0 != (entry = readdir(dir))) |
| 1064 | { |
| 1065 | if(!strcasecmp(entry->d_name, BOOTFILE)) |
| 1066 | { |
| 1067 | /* found the bootfile */ |
Peter D'Hoye | 33975fb | 2007-04-05 21:24:21 +0000 | [diff] [blame] | 1068 | if(wrtdate && do_rolo) |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 1069 | { |
Peter D'Hoye | 33975fb | 2007-04-05 21:24:21 +0000 | [diff] [blame] | 1070 | if((entry->wrtdate != wrtdate) || |
| 1071 | (entry->wrttime != wrttime)) |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 1072 | { |
Nils Wallménius | 33c4446 | 2008-04-26 09:30:24 +0000 | [diff] [blame] | 1073 | static const char *lines[] = { ID2P(LANG_BOOT_CHANGED), |
| 1074 | ID2P(LANG_REBOOT_NOW) }; |
| 1075 | static const struct text_message message={ lines, 2 }; |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 1076 | button_clear_queue(); /* Empty the keyboard buffer */ |
| 1077 | if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES) |
Peter D'Hoye | bf92f9c | 2007-04-05 20:24:07 +0000 | [diff] [blame] | 1078 | rolo_load(BOOTDIR "/" BOOTFILE); |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 1079 | } |
| 1080 | } |
Peter D'Hoye | 33975fb | 2007-04-05 21:24:21 +0000 | [diff] [blame] | 1081 | wrtdate = entry->wrtdate; |
| 1082 | wrttime = entry->wrttime; |
Peter D'Hoye | d2b3055 | 2007-03-30 21:54:48 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | closedir(dir); |
| 1086 | } |
| 1087 | #endif |
Peter D'Hoye | dc33e1e | 2007-03-30 23:36:10 +0000 | [diff] [blame] | 1088 | #endif |
Nils Wallménius | f46657e | 2007-05-30 17:57:32 +0000 | [diff] [blame] | 1089 | |
| 1090 | /* check range, set volume and save settings */ |
| 1091 | void setvol(void) |
| 1092 | { |
| 1093 | const int min_vol = sound_min(SOUND_VOLUME); |
| 1094 | const int max_vol = sound_max(SOUND_VOLUME); |
| 1095 | if (global_settings.volume < min_vol) |
| 1096 | global_settings.volume = min_vol; |
| 1097 | if (global_settings.volume > max_vol) |
| 1098 | global_settings.volume = max_vol; |
| 1099 | sound_set_volume(global_settings.volume); |
| 1100 | settings_save(); |
| 1101 | } |
| 1102 | |
Nils Wallménius | 4acae4d | 2007-11-18 14:12:01 +0000 | [diff] [blame] | 1103 | char* strrsplt(char* str, int c) |
| 1104 | { |
| 1105 | char* s = strrchr(str, c); |
| 1106 | |
| 1107 | if (s != NULL) |
| 1108 | { |
| 1109 | *s++ = '\0'; |
| 1110 | } |
| 1111 | else |
| 1112 | { |
| 1113 | s = str; |
| 1114 | } |
| 1115 | |
| 1116 | return s; |
| 1117 | } |
| 1118 | |
Robert Kukla | d87b037 | 2007-11-21 21:28:27 +0000 | [diff] [blame] | 1119 | /* Test file existence, using dircache of possible */ |
| 1120 | bool file_exists(const char *file) |
| 1121 | { |
| 1122 | int fd; |
| 1123 | |
| 1124 | if (!file || strlen(file) <= 0) |
| 1125 | return false; |
| 1126 | |
| 1127 | #ifdef HAVE_DIRCACHE |
| 1128 | if (dircache_is_enabled()) |
| 1129 | return (dircache_get_entry_ptr(file) != NULL); |
| 1130 | #endif |
| 1131 | |
| 1132 | fd = open(file, O_RDONLY); |
| 1133 | if (fd < 0) |
| 1134 | return false; |
| 1135 | close(fd); |
| 1136 | return true; |
| 1137 | } |
| 1138 | |
| 1139 | bool dir_exists(const char *path) |
| 1140 | { |
| 1141 | DIR* d = opendir(path); |
Robert Kukla | ed64a66 | 2007-11-21 22:42:52 +0000 | [diff] [blame] | 1142 | if (!d) |
| 1143 | return false; |
| 1144 | closedir(d); |
| 1145 | return true; |
Robert Kukla | d87b037 | 2007-11-21 21:28:27 +0000 | [diff] [blame] | 1146 | } |
Nicolas Pennequin | 536b5a0 | 2008-01-18 10:02:03 +0000 | [diff] [blame] | 1147 | |
| 1148 | /* |
| 1149 | * removes the extension of filename (if it doesn't start with a .) |
| 1150 | * puts the result in buffer |
| 1151 | */ |
Magnus Holmgren | 2235081 | 2008-02-06 19:51:19 +0000 | [diff] [blame] | 1152 | char *strip_extension(char* buffer, int buffer_size, const char *filename) |
Nicolas Pennequin | 536b5a0 | 2008-01-18 10:02:03 +0000 | [diff] [blame] | 1153 | { |
Nicolas Pennequin | 536b5a0 | 2008-01-18 10:02:03 +0000 | [diff] [blame] | 1154 | char *dot = strrchr(filename, '.'); |
Magnus Holmgren | 2235081 | 2008-02-06 19:51:19 +0000 | [diff] [blame] | 1155 | int len; |
| 1156 | |
| 1157 | if (buffer_size <= 0) |
| 1158 | { |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | |
| 1162 | buffer_size--; /* Make room for end nil */ |
| 1163 | |
Nicolas Pennequin | 536b5a0 | 2008-01-18 10:02:03 +0000 | [diff] [blame] | 1164 | if (dot != 0 && filename[0] != '.') |
| 1165 | { |
Magnus Holmgren | 2235081 | 2008-02-06 19:51:19 +0000 | [diff] [blame] | 1166 | len = dot - filename; |
| 1167 | len = MIN(len, buffer_size); |
| 1168 | strncpy(buffer, filename, len); |
Nicolas Pennequin | 536b5a0 | 2008-01-18 10:02:03 +0000 | [diff] [blame] | 1169 | } |
| 1170 | else |
Magnus Holmgren | 2235081 | 2008-02-06 19:51:19 +0000 | [diff] [blame] | 1171 | { |
| 1172 | len = buffer_size; |
| 1173 | strncpy(buffer, filename, buffer_size); |
| 1174 | } |
| 1175 | |
| 1176 | buffer[len] = 0; |
| 1177 | |
Nicolas Pennequin | 536b5a0 | 2008-01-18 10:02:03 +0000 | [diff] [blame] | 1178 | return buffer; |
| 1179 | } |
Dave Chapman | 7418c35 | 2008-03-25 21:09:58 +0000 | [diff] [blame] | 1180 | #endif /* !defined(__PCTOOL__) */ |
| 1181 | |
| 1182 | #ifdef HAVE_LCD_COLOR |
| 1183 | /* |
| 1184 | * Helper function to convert a string of 6 hex digits to a native colour |
| 1185 | */ |
| 1186 | |
| 1187 | static int hex2dec(int c) |
| 1188 | { |
| 1189 | return (((c) >= '0' && ((c) <= '9')) ? (c) - '0' : |
| 1190 | (toupper(c)) - 'A' + 10); |
| 1191 | } |
| 1192 | |
| 1193 | int hex_to_rgb(const char* hex, int* color) |
| 1194 | { |
| 1195 | int red, green, blue; |
| 1196 | int i = 0; |
| 1197 | |
| 1198 | while ((i < 6) && (isxdigit(hex[i]))) |
| 1199 | i++; |
| 1200 | |
| 1201 | if (i < 6) |
| 1202 | return -1; |
| 1203 | |
| 1204 | red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]); |
| 1205 | green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]); |
| 1206 | blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]); |
| 1207 | |
| 1208 | *color = LCD_RGBPACK(red,green,blue); |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | #endif /* HAVE_LCD_COLOR */ |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1213 | |
| 1214 | #ifdef HAVE_LCD_BITMAP |
| 1215 | /* A simplified scanf - used (at time of writing) by wps parsing functions. |
| 1216 | |
| 1217 | fmt - char array specifying the format of each list option. Valid values |
| 1218 | are: d - int |
| 1219 | s - string (sets pointer to string, without copying) |
| 1220 | c - hex colour (RGB888 - e.g. ff00ff) |
| 1221 | g - greyscale "colour" (0-3) |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1222 | set_vals - if not NULL 1 is set in the bitplace if the item was read OK |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1223 | 0 if not read. |
| 1224 | first item is LSB, (max 32 items! ) |
Jonathan Gordon | cbbbaac | 2008-06-07 08:30:06 +0000 | [diff] [blame] | 1225 | Stops parseing if an item is invalid unless the item == '-' |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1226 | sep - list separator (e.g. ',' or '|') |
| 1227 | str - string to parse, must be terminated by 0 or sep |
| 1228 | ... - pointers to store the parsed values |
| 1229 | |
| 1230 | return value - pointer to char after parsed data, 0 if there was an error. |
| 1231 | |
| 1232 | */ |
| 1233 | |
| 1234 | /* '0'-'3' are ASCII 0x30 to 0x33 */ |
| 1235 | #define is0123(x) (((x) & 0xfc) == 0x30) |
| 1236 | |
Jonathan Gordon | 930447c | 2008-06-07 09:12:23 +0000 | [diff] [blame] | 1237 | const char* parse_list(const char *fmt, uint32_t *set_vals, |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1238 | const char sep, const char* str, ...) |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1239 | { |
| 1240 | va_list ap; |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1241 | const char* p = str, *f = fmt; |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1242 | const char** s; |
| 1243 | int* d; |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1244 | bool set; |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1245 | int i=0; |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1246 | |
| 1247 | va_start(ap, str); |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1248 | if (set_vals) |
| 1249 | *set_vals = 0; |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1250 | while (*fmt) |
| 1251 | { |
| 1252 | /* Check for separator, if we're not at the start */ |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1253 | if (f != fmt) |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1254 | { |
| 1255 | if (*p != sep) |
| 1256 | goto err; |
| 1257 | p++; |
| 1258 | } |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1259 | set = false; |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1260 | switch (*fmt++) |
| 1261 | { |
| 1262 | case 's': /* string - return a pointer to it (not a copy) */ |
| 1263 | s = va_arg(ap, const char **); |
| 1264 | |
| 1265 | *s = p; |
| 1266 | while (*p && *p != sep) |
| 1267 | p++; |
Jonathan Gordon | 61f5dd6 | 2008-06-07 09:35:57 +0000 | [diff] [blame] | 1268 | set = (s[0][0]!='-') && (s[0][1]!=sep) ; |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1269 | break; |
| 1270 | |
| 1271 | case 'd': /* int */ |
| 1272 | d = va_arg(ap, int*); |
| 1273 | if (!isdigit(*p)) |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1274 | { |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1275 | if (!set_vals || *p != '-') |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1276 | goto err; |
| 1277 | while (*p && *p != sep) |
| 1278 | p++; |
| 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | *d = *p++ - '0'; |
| 1283 | while (isdigit(*p)) |
| 1284 | *d = (*d * 10) + (*p++ - '0'); |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1285 | set = true; |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1286 | } |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1287 | |
| 1288 | break; |
| 1289 | |
| 1290 | #ifdef HAVE_LCD_COLOR |
| 1291 | case 'c': /* colour (rrggbb - e.g. f3c1a8) */ |
| 1292 | d = va_arg(ap, int*); |
| 1293 | |
| 1294 | if (hex_to_rgb(p, d) < 0) |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1295 | { |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1296 | if (!set_vals || *p != '-') |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1297 | goto err; |
| 1298 | while (*p && *p != sep) |
| 1299 | p++; |
| 1300 | } |
| 1301 | else |
| 1302 | { |
| 1303 | p += 6; |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1304 | set = true; |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1305 | } |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1306 | |
| 1307 | break; |
| 1308 | #endif |
| 1309 | |
| 1310 | #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2) |
| 1311 | case 'g': /* greyscale colour (0-3) */ |
| 1312 | d = va_arg(ap, int*); |
| 1313 | |
| 1314 | if (is0123(*p)) |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1315 | { |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1316 | *d = *p++ - '0'; |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1317 | set = true; |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1318 | } |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1319 | else if (!set_vals || *p != '-') |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1320 | goto err; |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1321 | else |
| 1322 | { |
| 1323 | while (*p && *p != sep) |
| 1324 | p++; |
| 1325 | } |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1326 | |
| 1327 | break; |
| 1328 | #endif |
| 1329 | |
| 1330 | default: /* Unknown format type */ |
| 1331 | goto err; |
| 1332 | break; |
| 1333 | } |
Jonathan Gordon | ae49b38 | 2008-06-07 09:01:02 +0000 | [diff] [blame] | 1334 | if (set_vals && set) |
| 1335 | *set_vals |= (1<<i); |
Jonathan Gordon | f002b7d | 2008-06-05 08:20:39 +0000 | [diff] [blame] | 1336 | i++; |
Dave Chapman | e92d2c5 | 2008-03-21 13:41:35 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | va_end(ap); |
| 1340 | return p; |
| 1341 | |
| 1342 | err: |
| 1343 | va_end(ap); |
| 1344 | return 0; |
| 1345 | } |
| 1346 | #endif |