blob: f37bd5f081bbcba399b727e7b7335cd943c198ab [file] [log] [blame]
Daniel Stenberg5befad02002-11-22 13:00:10 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Daniel Stenberg
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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 Stenberg5befad02002-11-22 13:00:10 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Björn Stenberg6c33c512004-09-19 21:58:37 +000021#include <stdlib.h>
Linus Nielsen Feltzing26440c92004-07-05 11:15:50 +000022#include <ctype.h>
Dave Chapman78d29f52008-03-26 23:35:34 +000023#include "config.h"
24#include "lcd.h"
25#include "file.h"
26#ifdef __PCTOOL__
27#include <stdarg.h>
28#else
Dave Chapman7418c352008-03-25 21:09:58 +000029#include "sprintf.h"
Linus Nielsen Feltzing5b5003d2004-07-24 21:26:41 +000030#include "lang.h"
Linus Nielsen Feltzinga97de952003-04-14 15:58:51 +000031#include "string.h"
Linus Nielsen Feltzing6dca87e2005-02-11 14:38:44 +000032#include "dir.h"
Linus Nielsen Feltzing60895bc2005-09-02 05:39:09 +000033#include "lcd-remote.h"
Linus Nielsen Feltzing75b41a82003-05-04 02:04:31 +000034#include "errno.h"
35#include "system.h"
Linus Nielsen Feltzinga8dab4c2004-06-30 13:31:14 +000036#include "timefuncs.h"
Linus Nielsen Feltzing5b5003d2004-07-24 21:26:41 +000037#include "screens.h"
Jens Arnold2116bba2005-01-31 00:39:20 +000038#include "talk.h"
Linus Nielsen Feltzing5b5003d2004-07-24 21:26:41 +000039#include "mpeg.h"
Linus Nielsen Feltzing3d217812005-04-06 11:12:22 +000040#include "audio.h"
Linus Nielsen Feltzing5b5003d2004-07-24 21:26:41 +000041#include "mp3_playback.h"
42#include "settings.h"
43#include "ata.h"
Jonathan Gordon0d941422006-11-09 12:36:50 +000044#include "ata_idle_notify.h"
Linus Nielsen Feltzing5b5003d2004-07-24 21:26:41 +000045#include "kernel.h"
46#include "power.h"
Christi Scarborough247fe6d2005-02-06 09:57:57 +000047#include "powermgmt.h"
Linus Nielsen Feltzingb03ff1e2004-10-13 09:08:32 +000048#include "backlight.h"
Linus Nielsen Feltzing60895bc2005-09-02 05:39:09 +000049#include "version.h"
50#include "font.h"
Kevin Ferraree991bee2005-11-16 15:12:15 +000051#include "splash.h"
Miika Pekkarinen3b313462006-04-16 17:32:54 +000052#include "tagcache.h"
Linus Nielsen Feltzingda153da2006-10-19 09:42:58 +000053#include "scrobbler.h"
Nils Wallméniusf46657e2007-05-30 17:57:32 +000054#include "sound.h"
Nils Wallméniusdabcb812008-05-01 10:13:12 +000055#include "playlist.h"
56#include "yesno.h"
Dave Chapman7418c352008-03-25 21:09:58 +000057
Jens Arnold7f6b6402004-10-12 00:02:51 +000058#ifdef HAVE_MMC
59#include "ata_mmc.h"
60#endif
Linus Nielsen Feltzing74353a72005-09-14 09:07:05 +000061#include "tree.h"
Miika Pekkarinen954b7322006-08-05 20:19:10 +000062#include "eeprom_settings.h"
Dave Chapman7418c352008-03-25 21:09:58 +000063#if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
Peter D'Hoye583f1862007-08-25 15:53:54 +000064#include "recording.h"
65#endif
Dave Chapman7418c352008-03-25 21:09:58 +000066#if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
Linus Nielsen Feltzing60895bc2005-09-02 05:39:09 +000067#include "bmp.h"
68#include "icons.h"
69#endif /* End HAVE_LCD_BITMAP */
Miika Pekkarinen6938f562006-10-18 12:54:23 +000070#include "gui/gwps-common.h"
Jens Arnold178c5652007-04-09 13:39:37 +000071#include "bookmark.h"
Linus Nielsen Feltzing60895bc2005-09-02 05:39:09 +000072
Michael Sevakis0f5cb942006-11-06 18:07:30 +000073#include "misc.h"
Nils Wallménius5b769362007-08-06 13:08:36 +000074#include "playback.h"
Michael Sevakis0f5cb942006-11-06 18:07:30 +000075
Peter D'Hoyed2b30552007-03-30 21:54:48 +000076#ifdef BOOTFILE
Jens Arnoldffb3dfd2007-08-01 20:21:34 +000077#if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
Peter D'Hoyed2b30552007-03-30 21:54:48 +000078#include "rolo.h"
79#include "yesno.h"
80#endif
Peter D'Hoyedc33e1e2007-03-30 23:36:10 +000081#endif
Peter D'Hoyed2b30552007-03-30 21:54:48 +000082
Jens Arnold2116bba2005-01-31 00:39:20 +000083/* 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.*/
88char *output_dyn_value(char *buf, int buf_size, int value,
89 const unsigned char **units, bool bin_scale)
Daniel Stenberg5befad02002-11-22 13:00:10 +000090{
Jens Arnold2116bba2005-01-31 00:39:20 +000091 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 Stenberg5befad02002-11-22 13:00:10 +0000101 }
Jens Arnold2116bba2005-01-31 00:39:20 +0000102 if (bin_scale)
103 fraction = fraction * 1000 / 1024;
104
Jens Arnoldc8e1d572005-01-31 02:25:36 +0000105 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 Arnold2116bba2005-01-31 00:39:20 +0000112 if (buf)
113 {
Jens Arnoldc8e1d572005-01-31 02:25:36 +0000114 if (strlen(tbuf))
115 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
116 tbuf, P2STR(units[unit_no]));
Jens Arnold2116bba2005-01-31 00:39:20 +0000117 else
Jens Arnoldc8e1d572005-01-31 02:25:36 +0000118 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
Daniel Stenberg5befad02002-11-22 13:00:10 +0000119 }
Jens Arnold2116bba2005-01-31 00:39:20 +0000120 else
121 {
Nils Wallménius2521bf52008-04-20 11:02:42 +0000122 talk_fractional(tbuf, value, P2ID(units[unit_no]));
Daniel Stenberg5befad02002-11-22 13:00:10 +0000123 }
Jens Arnold2116bba2005-01-31 00:39:20 +0000124 return buf;
Daniel Stenberg5befad02002-11-22 13:00:10 +0000125}
126
Jens Arnold58e94122005-09-02 01:15:35 +0000127/* Create a filename with a number part in a way that the number is 1
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000128 * 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 Arnold58e94122005-09-02 01:15:35 +0000135char *create_numbered_filename(char *buffer, const char *path,
136 const char *prefix, const char *suffix,
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000137 int numberlen IF_CNFN_NUM_(, int *num))
Jens Arnold58e94122005-09-02 01:15:35 +0000138{
139 DIR *dir;
140 struct dirent *entry;
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000141 int max_num;
Jens Arnold58e94122005-09-02 01:15:35 +0000142 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 Sevakis0f5cb942006-11-06 18:07:30 +0000151#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 Arnold58e94122005-09-02 01:15:35 +0000163 dir = opendir(pathlen ? buffer : "/");
164 if (!dir)
165 return NULL;
166
167 while ((entry = readdir(dir)))
168 {
169 int curr_num;
170
Daniel Stenbergf981ea92005-12-05 22:44:42 +0000171 if (strncasecmp((char *)entry->d_name, prefix, prefixlen)
172 || strcasecmp((char *)entry->d_name + prefixlen + numberlen, suffix))
Jens Arnold58e94122005-09-02 01:15:35 +0000173 continue;
174
Daniel Stenbergf981ea92005-12-05 22:44:42 +0000175 curr_num = atoi((char *)entry->d_name + prefixlen);
Jens Arnold58e94122005-09-02 01:15:35 +0000176 if (curr_num > max_num)
177 max_num = curr_num;
178 }
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000179
Jens Arnold58e94122005-09-02 01:15:35 +0000180 closedir(dir);
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000181 }
182
183 max_num++;
Jens Arnold58e94122005-09-02 01:15:35 +0000184
185 snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen);
186 snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix,
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000187 max_num, suffix);
188
189#ifdef IF_CNFN_NUM
190 if (num)
191 *num = max_num;
192#endif
Jens Arnold58e94122005-09-02 01:15:35 +0000193
194 return buffer;
195}
196
Magnus Holmgren14ba91e2007-02-17 13:36:44 +0000197/* 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 */
203void 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 Arnold2c7b1272007-03-16 23:47:03 +0000218#if CONFIG_RTC
Jens Arnold58e94122005-09-02 01:15:35 +0000219/* Create a filename with a date+time part.
220 It is allowed that buffer and path point to the same memory location,
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000221 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 Arnold58e94122005-09-02 01:15:35 +0000224char *create_datetime_filename(char *buffer, const char *path,
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000225 const char *prefix, const char *suffix,
226 bool unique_time)
Jens Arnold58e94122005-09-02 01:15:35 +0000227{
228 struct tm *tm = get_time();
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000229 static struct tm last_tm;
Jens Arnold58e94122005-09-02 01:15:35 +0000230 int pathlen;
231
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000232 while (unique_time && !memcmp(get_time(), &last_tm, sizeof (struct tm)))
233 sleep(HZ/10);
234
235 last_tm = *tm;
236
Jens Arnold58e94122005-09-02 01:15:35 +0000237 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 Chapman8c800cf2005-12-04 15:23:47 +0000248#endif /* CONFIG_RTC */
Jens Arnold58e94122005-09-02 01:15:35 +0000249
Nils Wallméniusdabcb812008-05-01 10:13:12 +0000250/* Ask the user if they really want to erase the current dynamic playlist
251 * returns true if the playlist should be replaced */
252bool 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 Feltzing75b41a82003-05-04 02:04:31 +0000268/* Read (up to) a line of text from fd into buffer and return number of bytes
Jens Arnold2116bba2005-01-31 00:39:20 +0000269 * read (which may be larger than the number of bytes stored in buffer). If
Jens Arnold58e94122005-09-02 01:15:35 +0000270 * 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 Feltzing75b41a82003-05-04 02:04:31 +0000272 * stored in buffer.
273 */
274int 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 Pekkarinena1ac7432006-10-21 20:37:33 +0000304/* Performance optimized version of the previous function. */
305int 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 Feltzinga8dab4c2004-06-30 13:31:14 +0000351#ifdef HAVE_LCD_BITMAP
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000352
Jens Arnoldb0a4b3e2005-11-17 00:13:07 +0000353#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 Arnoldf894a4c2005-07-06 22:58:02 +0000358#if LCD_DEPTH <= 8
359#define BMP_NUMCOLORS (1 << LCD_DEPTH)
360#else
361#define BMP_NUMCOLORS 0
362#endif
Jens Arnoldb0a4b3e2005-11-17 00:13:07 +0000363#endif
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000364
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 Arnoldb0a4b3e2005-11-17 00:13:07 +0000374#elif LCD_DEPTH <= 16
375#define BMP_BPP 16
376#define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000377#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 Arnoldabe77a12004-08-01 21:50:57 +0000389static const unsigned char bmpheader[] =
Linus Nielsen Feltzinga97de952003-04-14 15:58:51 +0000390{
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000391 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 Arnoldb0a4b3e2005-11-17 00:13:07 +0000401 LE32_CONST(BMP_COMPRESSION),/* Compression mode */
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000402 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 Kukla3e3e1d52008-03-02 21:28:26 +0000409#ifdef MROBE_100
410 2, 2, 94, 0x00, /* Colour #0 */
411 3, 6, 241, 0x00 /* Colour #1 */
Robert Kuklaf3ef9aa2008-03-02 21:40:50 +0000412#else
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000413 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
414 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
Robert Kukla3e3e1d52008-03-02 21:28:26 +0000415#endif
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000416#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 Arnoldb0a4b3e2005-11-17 00:13:07 +0000421#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 Feltzing6dca87e2005-02-11 14:38:44 +0000425#endif
Linus Nielsen Feltzinga97de952003-04-14 15:58:51 +0000426};
427
Jens Arnold05042af2005-10-01 10:20:55 +0000428static void (*screen_dump_hook)(int fh) = NULL;
429
Linus Nielsen Feltzinga97de952003-04-14 15:58:51 +0000430void screen_dump(void)
431{
Jens Arnold0f046562004-09-03 22:24:29 +0000432 int fh;
Linus Nielsen Feltzinga97de952003-04-14 15:58:51 +0000433 char filename[MAX_PATH];
Dave Chapmanb27ecbd2005-11-13 16:13:00 +0000434 int bx, by;
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000435#if LCD_DEPTH == 1
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000436 static unsigned char line_block[8][BMP_LINESIZE];
437#elif LCD_DEPTH == 2
Jens Arnoldda5cef62006-02-19 12:31:04 +0000438#if LCD_PIXELFORMAT == HORIZONTAL_PACKING
439 static unsigned char line_block[BMP_LINESIZE];
Jens Arnoldfc2a7572008-03-14 10:13:58 +0000440#elif LCD_PIXELFORMAT == VERTICAL_PACKING
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000441 static unsigned char line_block[4][BMP_LINESIZE];
Jens Arnoldfc2a7572008-03-14 10:13:58 +0000442#elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
443 static unsigned char line_block[8][BMP_LINESIZE];
Jens Arnoldda5cef62006-02-19 12:31:04 +0000444#endif
Dave Chapmanb27ecbd2005-11-13 16:13:00 +0000445#elif LCD_DEPTH == 16
Jens Arnoldb0a4b3e2005-11-17 00:13:07 +0000446 static unsigned short line_block[BMP_LINESIZE/2];
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000447#endif
448
Jens Arnold2c7b1272007-03-16 23:47:03 +0000449#if CONFIG_RTC
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000450 create_datetime_filename(filename, "", "dump ", ".bmp", false);
Linus Nielsen Feltzing6dca87e2005-02-11 14:38:44 +0000451#else
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000452 create_numbered_filename(filename, "", "dump_", ".bmp", 4
453 IF_CNFN_NUM_(, NULL));
Linus Nielsen Feltzing6dca87e2005-02-11 14:38:44 +0000454#endif
Jens Arnold0f046562004-09-03 22:24:29 +0000455
Jens Arnold67eb1542007-02-01 23:08:15 +0000456 fh = creat(filename);
Jens Arnold0f046562004-09-03 22:24:29 +0000457 if (fh < 0)
458 return;
459
Jens Arnold05042af2005-10-01 10:20:55 +0000460 if (screen_dump_hook)
461 {
462 screen_dump_hook(fh);
463 }
464 else
465 {
466 write(fh, bmpheader, sizeof(bmpheader));
Jens Arnold0f046562004-09-03 22:24:29 +0000467
Jens Arnold05042af2005-10-01 10:20:55 +0000468 /* BMP image goes bottom up */
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000469#if LCD_DEPTH == 1
Jens Arnold51223e52007-02-20 19:31:34 +0000470 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
Linus Nielsen Feltzinga97de952003-04-14 15:58:51 +0000471 {
Jens Arnold42054072005-11-26 23:50:24 +0000472 unsigned char *src = &lcd_framebuffer[by][0];
473 unsigned char *dst = &line_block[0][0];
Jens Arnold05042af2005-10-01 10:20:55 +0000474
Jens Arnold42054072005-11-26 23:50:24 +0000475 memset(line_block, 0, sizeof(line_block));
476 for (bx = LCD_WIDTH/8; bx > 0; bx--)
Jens Arnold0f046562004-09-03 22:24:29 +0000477 {
Jens Arnold42054072005-11-26 23:50:24 +0000478 unsigned dst_mask = 0x80;
479 int ix;
480
481 for (ix = 8; ix > 0; ix--)
Jens Arnold0f046562004-09-03 22:24:29 +0000482 {
Jens Arnold42054072005-11-26 23:50:24 +0000483 unsigned char *dst_blk = dst;
484 unsigned src_byte = *src++;
485 int iy;
486
487 for (iy = 8; iy > 0; iy--)
Jens Arnold05042af2005-10-01 10:20:55 +0000488 {
Jens Arnold42054072005-11-26 23:50:24 +0000489 if (src_byte & 0x80)
490 *dst_blk |= dst_mask;
491 src_byte <<= 1;
492 dst_blk += BMP_LINESIZE;
Jens Arnold05042af2005-10-01 10:20:55 +0000493 }
Jens Arnold42054072005-11-26 23:50:24 +0000494 dst_mask >>= 1;
Jens Arnold0f046562004-09-03 22:24:29 +0000495 }
Jens Arnold42054072005-11-26 23:50:24 +0000496 dst++;
Jens Arnold0f046562004-09-03 22:24:29 +0000497 }
Jens Arnold05042af2005-10-01 10:20:55 +0000498
Jens Arnold42054072005-11-26 23:50:24 +0000499 write(fh, line_block, sizeof(line_block));
Linus Nielsen Feltzinga97de952003-04-14 15:58:51 +0000500 }
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000501#elif LCD_DEPTH == 2
Jens Arnoldda5cef62006-02-19 12:31:04 +0000502#if LCD_PIXELFORMAT == HORIZONTAL_PACKING
Jens Arnold51223e52007-02-20 19:31:34 +0000503 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
Jens Arnoldda5cef62006-02-19 12:31:04 +0000504 {
505 unsigned char *src = &lcd_framebuffer[by][0];
506 unsigned char *dst = line_block;
507
508 memset(line_block, 0, sizeof(line_block));
Jens Arnold51223e52007-02-20 19:31:34 +0000509 for (bx = LCD_FBWIDTH; bx > 0; bx--)
Jens Arnoldda5cef62006-02-19 12:31:04 +0000510 {
511 unsigned src_byte = *src++;
Jens Arnold51223e52007-02-20 19:31:34 +0000512
Jens Arnold542d9b52006-05-03 21:26:38 +0000513 *dst++ = ((src_byte >> 2) & 0x30) | ((src_byte >> 4) & 0x03);
514 *dst++ = ((src_byte << 2) & 0x30) | (src_byte & 0x03);
Jens Arnoldda5cef62006-02-19 12:31:04 +0000515 }
516
517 write(fh, line_block, sizeof(line_block));
518 }
Jens Arnoldfc2a7572008-03-14 10:13:58 +0000519#elif LCD_PIXELFORMAT == VERTICAL_PACKING
Jens Arnold51223e52007-02-20 19:31:34 +0000520 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000521 {
Jens Arnold42054072005-11-26 23:50:24 +0000522 unsigned char *src = &lcd_framebuffer[by][0];
523 unsigned char *dst = &line_block[3][0];
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000524
Jens Arnold42054072005-11-26 23:50:24 +0000525 memset(line_block, 0, sizeof(line_block));
526 for (bx = LCD_WIDTH/2; bx > 0; bx--)
Jens Arnold05042af2005-10-01 10:20:55 +0000527 {
Jens Arnold42054072005-11-26 23:50:24 +0000528 unsigned char *dst_blk = dst++;
Jens Arnoldfc2a7572008-03-14 10:13:58 +0000529 unsigned src_byte0 = *src++ << 4;
Jens Arnold42054072005-11-26 23:50:24 +0000530 unsigned src_byte1 = *src++;
531 int iy;
532
533 for (iy = 4; iy > 0; iy--)
Jens Arnold05042af2005-10-01 10:20:55 +0000534 {
Jens Arnoldfc2a7572008-03-14 10:13:58 +0000535 *dst_blk = (src_byte0 & 0x30) | (src_byte1 & 0x03);
Jens Arnold42054072005-11-26 23:50:24 +0000536 src_byte0 >>= 2;
537 src_byte1 >>= 2;
538 dst_blk -= BMP_LINESIZE;
Jens Arnold05042af2005-10-01 10:20:55 +0000539 }
540 }
541
Jens Arnold42054072005-11-26 23:50:24 +0000542 write(fh, line_block, sizeof(line_block));
Jens Arnold05042af2005-10-01 10:20:55 +0000543 }
Jens Arnoldfc2a7572008-03-14 10:13:58 +0000544#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 Arnoldda5cef62006-02-19 12:31:04 +0000570#endif
Jens Arnoldb0a4b3e2005-11-17 00:13:07 +0000571#elif LCD_DEPTH == 16
Dave Chapmanb27ecbd2005-11-13 16:13:00 +0000572 for (by = LCD_HEIGHT - 1; by >= 0; by--)
573 {
Jens Arnold42054072005-11-26 23:50:24 +0000574 unsigned short *src = &lcd_framebuffer[by][0];
575 unsigned short *dst = line_block;
Dave Chapmanb27ecbd2005-11-13 16:13:00 +0000576
Jens Arnold42054072005-11-26 23:50:24 +0000577 memset(line_block, 0, sizeof(line_block));
578 for (bx = LCD_WIDTH; bx > 0; bx--)
Dave Chapmanb27ecbd2005-11-13 16:13:00 +0000579 {
Dave Chapman664a4182005-11-19 02:14:16 +0000580#if (LCD_PIXELFORMAT == RGB565SWAPPED)
Jens Arnoldb0a4b3e2005-11-17 00:13:07 +0000581 /* iPod LCD data is big endian although the CPU is not */
Jens Arnold42054072005-11-26 23:50:24 +0000582 *dst++ = htobe16(*src++);
Jens Arnoldb0a4b3e2005-11-17 00:13:07 +0000583#else
584 *dst++ = htole16(*src++);
585#endif
Dave Chapmanb27ecbd2005-11-13 16:13:00 +0000586 }
587
588 write(fh, line_block, sizeof(line_block));
589 }
Jens Arnold05042af2005-10-01 10:20:55 +0000590#endif /* LCD_DEPTH */
Jens Arnoldf894a4c2005-07-06 22:58:02 +0000591 }
Jens Arnold05042af2005-10-01 10:20:55 +0000592
Jens Arnold0f046562004-09-03 22:24:29 +0000593 close(fh);
Linus Nielsen Feltzinga97de952003-04-14 15:58:51 +0000594}
Jens Arnold05042af2005-10-01 10:20:55 +0000595
596void screen_dump_set_hook(void (*hook)(int fh))
597{
598 screen_dump_hook = hook;
599}
600
601#endif /* HAVE_LCD_BITMAP */
Linus Nielsen Feltzing26440c92004-07-05 11:15:50 +0000602
Jens Arnold0f046562004-09-03 22:24:29 +0000603/* parse a line from a configuration file. the line format is:
Linus Nielsen Feltzing26440c92004-07-05 11:15:50 +0000604
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
613bool 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 Feltzing5b5003d2004-07-24 21:26:41 +0000635
Linus Nielsen Feltzing74353a72005-09-14 09:07:05 +0000636static void system_flush(void)
637{
638 tree_flush();
Jonathan Gordon65811462006-12-12 10:45:34 +0000639 call_ata_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
Linus Nielsen Feltzing74353a72005-09-14 09:07:05 +0000640}
641
642static void system_restore(void)
643{
644 tree_restore();
645}
646
Linus Nielsen Feltzing670abf92005-08-18 14:23:18 +0000647static bool clean_shutdown(void (*callback)(void *), void *parameter)
Linus Nielsen Feltzing5b5003d2004-07-24 21:26:41 +0000648{
Björn Stenberg6c33c512004-09-19 21:58:37 +0000649#ifdef SIMULATOR
Linus Nielsen Feltzing49483bc2005-08-18 14:46:35 +0000650 (void)callback;
651 (void)parameter;
Magnus Holmgrena50c2b12007-08-22 16:33:34 +0000652 bookmark_autobookmark();
Jonathan Gordon4049d442006-11-26 09:53:42 +0000653 call_ata_idle_notifys(true);
Björn Stenberg6c33c512004-09-19 21:58:37 +0000654 exit(0);
655#else
Nils Wallménius5b769362007-08-06 13:08:36 +0000656 long msg_id = -1;
Jens Arnold5074ec02006-04-24 06:34:45 +0000657 int i;
658
Peter D'Hoye12d2d0f2007-06-24 18:46:04 +0000659 scrobbler_poweroff();
660
Jonathan Gordon9a6f4192007-02-18 05:32:06 +0000661#if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
Linus Nielsen Feltzing5b5003d2004-07-24 21:26:41 +0000662 if(!charger_inserted())
Linus Nielsen Feltzing6da68012005-02-10 21:53:41 +0000663#endif
Linus Nielsen Feltzing5b5003d2004-07-24 21:26:41 +0000664 {
Jens Arnoldda5910e2007-06-29 19:01:24 +0000665 bool batt_safe = battery_level_safe();
Michael Sevakis0d768a32006-12-07 19:34:26 +0000666 int audio_stat = audio_status();
667
Jens Arnold5074ec02006-04-24 06:34:45 +0000668 FOR_NB_SCREENS(i)
669 screens[i].clear_display();
Jens Arnold671f5d42007-08-12 11:18:52 +0000670
Jens Arnoldda5910e2007-06-29 19:01:24 +0000671 if (batt_safe)
672 {
Jonathan Gordon710ccb72006-10-25 10:17:57 +0000673#ifdef HAVE_TAGCACHE
Barry Wardell8d2711b2006-11-11 01:18:57 +0000674 if (!tagcache_prepare_shutdown())
675 {
676 cancel_shutdown();
Nils Wallménius5b769362007-08-06 13:08:36 +0000677 gui_syncsplash(HZ, ID2P(LANG_TAGCACHE_BUSY));
Barry Wardell8d2711b2006-11-11 01:18:57 +0000678 return false;
679 }
Jonathan Gordon710ccb72006-10-25 10:17:57 +0000680#endif
Jens Arnoldda5910e2007-06-29 19:01:24 +0000681 if (battery_level() > 10)
682 gui_syncsplash(0, str(LANG_SHUTTINGDOWN));
683 else
Nils Wallménius5b769362007-08-06 13:08:36 +0000684 {
685 msg_id = LANG_WARNING_BATTERY_LOW;
Jens Arnoldda5910e2007-06-29 19:01:24 +0000686 gui_syncsplash(0, "%s %s",
687 str(LANG_WARNING_BATTERY_LOW),
688 str(LANG_SHUTTINGDOWN));
Nils Wallménius5b769362007-08-06 13:08:36 +0000689 }
Jens Arnoldda5910e2007-06-29 19:01:24 +0000690 }
691 else
692 {
Nils Wallménius5b769362007-08-06 13:08:36 +0000693 msg_id = LANG_WARNING_BATTERY_EMPTY;
Jens Arnoldda5910e2007-06-29 19:01:24 +0000694 gui_syncsplash(0, "%s %s",
695 str(LANG_WARNING_BATTERY_EMPTY),
696 str(LANG_SHUTTINGDOWN));
Barry Wardell8d2711b2006-11-11 01:18:57 +0000697 }
Miika Pekkarinen6ee82e72006-09-23 10:29:14 +0000698
Jens Arnoldda5910e2007-06-29 19:01:24 +0000699 if (global_settings.fade_on_stop
Michael Sevakis0d768a32006-12-07 19:34:26 +0000700 && (audio_stat & AUDIO_STATUS_PLAY))
Miika Pekkarinen53d662b2006-10-18 12:59:13 +0000701 {
Jonathan Gordon4982b872008-06-23 02:45:55 +0000702 fade(false, false);
Miika Pekkarinen53d662b2006-10-18 12:59:13 +0000703 }
Michael Sevakis0d768a32006-12-07 19:34:26 +0000704
Jens Arnoldda5910e2007-06-29 19:01:24 +0000705 if (batt_safe) /* do not save on critical battery */
Michael Sevakis0d768a32006-12-07 19:34:26 +0000706 {
Jens Arnoldda5910e2007-06-29 19:01:24 +0000707#if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
708 if (audio_stat & AUDIO_STATUS_RECORD)
Michael Sevakisf2c19872007-07-25 02:15:04 +0000709 {
Peter D'Hoye583f1862007-08-25 15:53:54 +0000710 rec_command(RECORDING_CMD_STOP);
Michael Sevakisf2c19872007-07-25 02:15:04 +0000711 /* wait for stop to complete */
712 while (audio_status() & AUDIO_STATUS_RECORD)
713 sleep(1);
714 }
Michael Sevakis0d768a32006-12-07 19:34:26 +0000715#endif
Magnus Holmgrena50c2b12007-08-22 16:33:34 +0000716 bookmark_autobookmark();
717
Jens Arnoldda5910e2007-06-29 19:01:24 +0000718 /* audio_stop_recording == audio_stop for HWCODEC */
719 audio_stop();
Michael Sevakis0d768a32006-12-07 19:34:26 +0000720
Jens Arnoldda5910e2007-06-29 19:01:24 +0000721 if (callback != NULL)
722 callback(parameter);
Linus Nielsen Feltzing74353a72005-09-14 09:07:05 +0000723
Michael Sevakisf2c19872007-07-25 02:15:04 +0000724#if CONFIG_CODEC != SWCODEC
Jens Arnoldda5910e2007-06-29 19:01:24 +0000725 /* wait for audio_stop or audio_stop_recording to complete */
726 while (audio_status())
727 sleep(1);
Michael Sevakisf2c19872007-07-25 02:15:04 +0000728#endif
Jens Arnoldda5910e2007-06-29 19:01:24 +0000729
730#if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
731 audio_close_recording();
732#endif
Nils Wallménius5b769362007-08-06 13:08:36 +0000733
Steve Bavin32a95752007-10-19 15:31:42 +0000734 if(global_settings.talk_menu)
Nils Wallménius5b769362007-08-06 13:08:36 +0000735 {
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 Wardell8d2711b2006-11-11 01:18:57 +0000748 system_flush();
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000749#ifdef HAVE_EEPROM_SETTINGS
Jens Arnoldda5910e2007-06-29 19:01:24 +0000750 if (firmware_settings.initialized)
751 {
752 firmware_settings.disk_clean = true;
753 firmware_settings.bl_version = 0;
754 eeprom_settings_store();
755 }
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000756#endif
Jens Arnoldda5910e2007-06-29 19:01:24 +0000757 }
758#ifdef HAVE_DIRCACHE
759 else
760 dircache_disable();
761#endif
762
Christi Scarborough247fe6d2005-02-06 09:57:57 +0000763 shutdown_hw();
Linus Nielsen Feltzing5b5003d2004-07-24 21:26:41 +0000764 }
765#endif
766 return false;
767}
Linus Nielsen Feltzingade5d7b2004-07-26 16:06:59 +0000768
Jens Arnold178c5652007-04-09 13:39:37 +0000769bool 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 Gordon4982b872008-06-23 02:45:55 +0000779 fade(false, false);
Jens Arnold178c5652007-04-09 13:39:37 +0000780 bookmark_autobookmark();
781 audio_stop();
Magnus Holmgren1d9f6932007-08-05 15:21:47 +0000782 ret = true; /* bookmarking can make a refresh necessary */
Jens Arnold178c5652007-04-09 13:39:37 +0000783 }
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 Gordon9a6f4192007-02-18 05:32:06 +0000816#if CONFIG_CHARGING
Linus Nielsen Feltzing3d217812005-04-06 11:12:22 +0000817static bool waiting_to_resume_play = false;
818static long play_resume_tick;
819
820static 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 Stenberg8cece5a2007-01-08 21:25:46 +0000844 if (global_settings.fade_on_stop)
Jonathan Gordon4982b872008-06-23 02:45:55 +0000845 fade(false, false);
Björn Stenberg8cece5a2007-01-08 21:25:46 +0000846 else
847 audio_pause();
Linus Nielsen Feltzing3d217812005-04-06 11:12:22 +0000848 }
Linus Nielsen Feltzing16301882007-10-08 09:09:40 +0000849 waiting_to_resume_play = false;
Linus Nielsen Feltzing3d217812005-04-06 11:12:22 +0000850 }
851 }
852}
853
854static 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 Feltzing16301882007-10-08 09:09:40 +0000862 queue_broadcast(SYS_CAR_ADAPTER_RESUME, 0);
Linus Nielsen Feltzing3d217812005-04-06 11:12:22 +0000863 }
864 waiting_to_resume_play = false;
865 }
866 }
867}
868
869void car_adapter_mode_init(void)
870{
871 tick_add_task(car_adapter_tick);
872}
873#endif
874
Linus Nielsen Feltzing537c7542006-09-26 10:03:56 +0000875#ifdef HAVE_HEADPHONE_DETECTION
Jens Arnold314f3b52006-12-12 22:22:21 +0000876static void unplug_change(bool inserted)
Linus Nielsen Feltzing537c7542006-09-26 10:03:56 +0000877{
Brandon Low7b1de202008-03-18 07:01:23 +0000878 static bool headphone_caused_pause = false;
879
Linus Nielsen Feltzing537c7542006-09-26 10:03:56 +0000880 if (global_settings.unplug_mode)
881 {
Brandon Low7b1de202008-03-18 07:01:23 +0000882 int audio_stat = audio_status();
Linus Nielsen Feltzing537c7542006-09-26 10:03:56 +0000883 if (inserted)
884 {
Brandon Low7b1de202008-03-18 07:01:23 +0000885 if ((audio_stat & AUDIO_STATUS_PLAY) &&
886 headphone_caused_pause &&
887 global_settings.unplug_mode > 1 )
Linus Nielsen Feltzing537c7542006-09-26 10:03:56 +0000888 audio_resume();
889 backlight_on();
Brandon Low7b1de202008-03-18 07:01:23 +0000890 headphone_caused_pause = false;
Linus Nielsen Feltzing537c7542006-09-26 10:03:56 +0000891 } else {
Brandon Low7b1de202008-03-18 07:01:23 +0000892 if ((audio_stat & AUDIO_STATUS_PLAY) &&
893 !(audio_stat & AUDIO_STATUS_PAUSE))
Linus Nielsen Feltzing537c7542006-09-26 10:03:56 +0000894 {
Brandon Low7b1de202008-03-18 07:01:23 +0000895 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 Feltzing537c7542006-09-26 10:03:56 +0000907 }
908 }
909 }
910}
911#endif
912
Jean-Philippe Bernardyf64939e2005-02-07 22:56:37 +0000913long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
Linus Nielsen Feltzingade5d7b2004-07-26 16:06:59 +0000914{
Linus Nielsen Feltzingf7490932004-07-26 23:31:40 +0000915 switch(event)
Linus Nielsen Feltzingade5d7b2004-07-26 16:06:59 +0000916 {
Stéphane Doyonda932992007-11-03 05:00:49 +0000917 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 Feltzingade5d7b2004-07-26 16:06:59 +0000927 case SYS_USB_CONNECTED:
Jens Arnold0dc52d52004-10-12 22:45:01 +0000928 if (callback != NULL)
929 callback(parameter);
Jens Arnold7f6b6402004-10-12 00:02:51 +0000930#ifdef HAVE_MMC
Michael Sevakis1167e3c2007-06-30 02:08:27 +0000931 if (!mmc_touched() ||
932 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
Jens Arnold7f6b6402004-10-12 00:02:51 +0000933#endif
Linus Nielsen Feltzing74353a72005-09-14 09:07:05 +0000934 {
Linus Nielsen Feltzingda153da2006-10-19 09:42:58 +0000935 scrobbler_flush_cache();
Linus Nielsen Feltzing74353a72005-09-14 09:07:05 +0000936 system_flush();
Peter D'Hoyed2b30552007-03-30 21:54:48 +0000937#ifdef BOOTFILE
Jens Arnoldffb3dfd2007-08-01 20:21:34 +0000938#if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
Peter D'Hoyed2b30552007-03-30 21:54:48 +0000939 check_bootfile(false); /* gets initial size */
940#endif
Peter D'Hoyedc33e1e2007-03-30 23:36:10 +0000941#endif
Jens Arnold7f6b6402004-10-12 00:02:51 +0000942 usb_screen();
Peter D'Hoyed2b30552007-03-30 21:54:48 +0000943#ifdef BOOTFILE
Jens Arnoldffb3dfd2007-08-01 20:21:34 +0000944#if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
Peter D'Hoyed2b30552007-03-30 21:54:48 +0000945 check_bootfile(true);
946#endif
Peter D'Hoyedc33e1e2007-03-30 23:36:10 +0000947#endif
Linus Nielsen Feltzing74353a72005-09-14 09:07:05 +0000948 system_restore();
949 }
Linus Nielsen Feltzingade5d7b2004-07-26 16:06:59 +0000950 return SYS_USB_CONNECTED;
Linus Nielsen Feltzing0b3ea1b2004-10-12 11:00:19 +0000951 case SYS_POWEROFF:
Linus Nielsen Feltzing670abf92005-08-18 14:23:18 +0000952 if (!clean_shutdown(callback, parameter))
Linus Nielsen Feltzing0b3ea1b2004-10-12 11:00:19 +0000953 return SYS_POWEROFF;
954 break;
Jonathan Gordon9a6f4192007-02-18 05:32:06 +0000955#if CONFIG_CHARGING
Linus Nielsen Feltzing3d217812005-04-06 11:12:22 +0000956 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 Feltzing16301882007-10-08 09:09:40 +0000963
964 case SYS_CAR_ADAPTER_RESUME:
965 audio_resume();
966 return SYS_CAR_ADAPTER_RESUME;
Linus Nielsen Feltzing3d217812005-04-06 11:12:22 +0000967#endif
Linus Nielsen Feltzing537c7542006-09-26 10:03:56 +0000968#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 Feltzingade5d7b2004-07-26 16:06:59 +0000977 }
978 return 0;
979}
Jens Arnold0dc52d52004-10-12 22:45:01 +0000980
Jean-Philippe Bernardyf64939e2005-02-07 22:56:37 +0000981long default_event_handler(long event)
Jens Arnold0dc52d52004-10-12 22:45:01 +0000982{
983 return default_event_handler_ex(event, NULL, NULL);
984}
Linus Nielsen Feltzing60895bc2005-09-02 05:39:09 +0000985
986int show_logo( void )
987{
988#ifdef HAVE_LCD_BITMAP
989 char version[32];
990 int font_h, font_w;
991
Jens Arnoldf9b90e92007-04-06 22:55:00 +0000992 snprintf(version, sizeof(version), "Ver. %s", appsversion);
993
Linus Nielsen Feltzing60895bc2005-09-02 05:39:09 +0000994 lcd_clear_display();
Dave Chapman95538f62006-02-21 01:23:30 +0000995 lcd_bitmap(rockboxlogo, 0, 10, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
Jens Arnoldf9b90e92007-04-06 22:55:00 +0000996 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 Gordonc9397742007-04-12 12:14:54 +00001000 lcd_setfont(FONT_UI);
Jens Arnoldf9b90e92007-04-06 22:55:00 +00001001
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 Feltzing60895bc2005-09-02 05:39:09 +00001011
1012#ifdef HAVE_REMOTE_LCD
1013 lcd_remote_clear_display();
Jens Arnolda0483462006-07-26 23:47:29 +00001014 lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo,
Dave Chapman95538f62006-02-21 01:23:30 +00001015 BMPHEIGHT_remote_rockboxlogo);
Linus Nielsen Feltzing60895bc2005-09-02 05:39:09 +00001016 lcd_remote_setfont(FONT_SYSFIXED);
Daniel Stenbergf981ea92005-12-05 22:44:42 +00001017 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
Linus Nielsen Feltzing60895bc2005-09-02 05:39:09 +00001018 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
Jonathan Gordonc9397742007-04-12 12:14:54 +00001019 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
Marcoen Hirschberg88c05bd2007-04-12 13:36:49 +00001020 lcd_remote_setfont(FONT_UI);
Linus Nielsen Feltzing60895bc2005-09-02 05:39:09 +00001021 lcd_remote_update();
1022#endif
1023
Linus Nielsen Feltzing60895bc2005-09-02 05:39:09 +00001024 return 0;
1025}
Magnus Holmgrenea799242006-11-15 20:26:33 +00001026
1027#if CONFIG_CODEC == SWCODEC
1028int 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'Hoyed2b30552007-03-30 21:54:48 +00001042
1043#ifdef BOOTFILE
Jens Arnoldffb3dfd2007-08-01 20:21:34 +00001044#if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
Peter D'Hoyed2b30552007-03-30 21:54:48 +00001045/*
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*/
1050void check_bootfile(bool do_rolo)
1051{
Peter D'Hoye0db06ce2007-04-05 21:38:27 +00001052 static unsigned short wrtdate = 0;
1053 static unsigned short wrttime = 0;
Peter D'Hoyed2b30552007-03-30 21:54:48 +00001054 DIR* dir = NULL;
1055 struct dirent* entry = NULL;
1056
Peter D'Hoyebf92f9c2007-04-05 20:24:07 +00001057 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
1058 dir = opendir(BOOTDIR);
Peter D'Hoyed2b30552007-03-30 21:54:48 +00001059
1060 if(!dir) return; /* do we want an error splash? */
1061
Peter D'Hoyebf92f9c2007-04-05 20:24:07 +00001062 /* loop all files in BOOTDIR */
Peter D'Hoyed2b30552007-03-30 21:54:48 +00001063 while(0 != (entry = readdir(dir)))
1064 {
1065 if(!strcasecmp(entry->d_name, BOOTFILE))
1066 {
1067 /* found the bootfile */
Peter D'Hoye33975fb2007-04-05 21:24:21 +00001068 if(wrtdate && do_rolo)
Peter D'Hoyed2b30552007-03-30 21:54:48 +00001069 {
Peter D'Hoye33975fb2007-04-05 21:24:21 +00001070 if((entry->wrtdate != wrtdate) ||
1071 (entry->wrttime != wrttime))
Peter D'Hoyed2b30552007-03-30 21:54:48 +00001072 {
Nils Wallménius33c44462008-04-26 09:30:24 +00001073 static const char *lines[] = { ID2P(LANG_BOOT_CHANGED),
1074 ID2P(LANG_REBOOT_NOW) };
1075 static const struct text_message message={ lines, 2 };
Peter D'Hoyed2b30552007-03-30 21:54:48 +00001076 button_clear_queue(); /* Empty the keyboard buffer */
1077 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
Peter D'Hoyebf92f9c2007-04-05 20:24:07 +00001078 rolo_load(BOOTDIR "/" BOOTFILE);
Peter D'Hoyed2b30552007-03-30 21:54:48 +00001079 }
1080 }
Peter D'Hoye33975fb2007-04-05 21:24:21 +00001081 wrtdate = entry->wrtdate;
1082 wrttime = entry->wrttime;
Peter D'Hoyed2b30552007-03-30 21:54:48 +00001083 }
1084 }
1085 closedir(dir);
1086}
1087#endif
Peter D'Hoyedc33e1e2007-03-30 23:36:10 +00001088#endif
Nils Wallméniusf46657e2007-05-30 17:57:32 +00001089
1090/* check range, set volume and save settings */
1091void 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énius4acae4d2007-11-18 14:12:01 +00001103char* 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 Kuklad87b0372007-11-21 21:28:27 +00001119/* Test file existence, using dircache of possible */
1120bool 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
1139bool dir_exists(const char *path)
1140{
1141 DIR* d = opendir(path);
Robert Kuklaed64a662007-11-21 22:42:52 +00001142 if (!d)
1143 return false;
1144 closedir(d);
1145 return true;
Robert Kuklad87b0372007-11-21 21:28:27 +00001146}
Nicolas Pennequin536b5a02008-01-18 10:02:03 +00001147
1148/*
1149 * removes the extension of filename (if it doesn't start with a .)
1150 * puts the result in buffer
1151 */
Magnus Holmgren22350812008-02-06 19:51:19 +00001152char *strip_extension(char* buffer, int buffer_size, const char *filename)
Nicolas Pennequin536b5a02008-01-18 10:02:03 +00001153{
Nicolas Pennequin536b5a02008-01-18 10:02:03 +00001154 char *dot = strrchr(filename, '.');
Magnus Holmgren22350812008-02-06 19:51:19 +00001155 int len;
1156
1157 if (buffer_size <= 0)
1158 {
1159 return NULL;
1160 }
1161
1162 buffer_size--; /* Make room for end nil */
1163
Nicolas Pennequin536b5a02008-01-18 10:02:03 +00001164 if (dot != 0 && filename[0] != '.')
1165 {
Magnus Holmgren22350812008-02-06 19:51:19 +00001166 len = dot - filename;
1167 len = MIN(len, buffer_size);
1168 strncpy(buffer, filename, len);
Nicolas Pennequin536b5a02008-01-18 10:02:03 +00001169 }
1170 else
Magnus Holmgren22350812008-02-06 19:51:19 +00001171 {
1172 len = buffer_size;
1173 strncpy(buffer, filename, buffer_size);
1174 }
1175
1176 buffer[len] = 0;
1177
Nicolas Pennequin536b5a02008-01-18 10:02:03 +00001178 return buffer;
1179}
Dave Chapman7418c352008-03-25 21:09:58 +00001180#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
1187static int hex2dec(int c)
1188{
1189 return (((c) >= '0' && ((c) <= '9')) ? (c) - '0' :
1190 (toupper(c)) - 'A' + 10);
1191}
1192
1193int 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 Chapmane92d2c52008-03-21 13:41:35 +00001213
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 Gordonae49b382008-06-07 09:01:02 +00001222 set_vals - if not NULL 1 is set in the bitplace if the item was read OK
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001223 0 if not read.
1224 first item is LSB, (max 32 items! )
Jonathan Gordoncbbbaac2008-06-07 08:30:06 +00001225 Stops parseing if an item is invalid unless the item == '-'
Dave Chapmane92d2c52008-03-21 13:41:35 +00001226 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 Gordon930447c2008-06-07 09:12:23 +00001237const char* parse_list(const char *fmt, uint32_t *set_vals,
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001238 const char sep, const char* str, ...)
Dave Chapmane92d2c52008-03-21 13:41:35 +00001239{
1240 va_list ap;
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001241 const char* p = str, *f = fmt;
Dave Chapmane92d2c52008-03-21 13:41:35 +00001242 const char** s;
1243 int* d;
Jonathan Gordonae49b382008-06-07 09:01:02 +00001244 bool set;
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001245 int i=0;
Dave Chapmane92d2c52008-03-21 13:41:35 +00001246
1247 va_start(ap, str);
Jonathan Gordonae49b382008-06-07 09:01:02 +00001248 if (set_vals)
1249 *set_vals = 0;
Dave Chapmane92d2c52008-03-21 13:41:35 +00001250 while (*fmt)
1251 {
1252 /* Check for separator, if we're not at the start */
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001253 if (f != fmt)
Dave Chapmane92d2c52008-03-21 13:41:35 +00001254 {
1255 if (*p != sep)
1256 goto err;
1257 p++;
1258 }
Jonathan Gordonae49b382008-06-07 09:01:02 +00001259 set = false;
Dave Chapmane92d2c52008-03-21 13:41:35 +00001260 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 Gordon61f5dd62008-06-07 09:35:57 +00001268 set = (s[0][0]!='-') && (s[0][1]!=sep) ;
Dave Chapmane92d2c52008-03-21 13:41:35 +00001269 break;
1270
1271 case 'd': /* int */
1272 d = va_arg(ap, int*);
1273 if (!isdigit(*p))
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001274 {
Jonathan Gordonae49b382008-06-07 09:01:02 +00001275 if (!set_vals || *p != '-')
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001276 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 Gordonae49b382008-06-07 09:01:02 +00001285 set = true;
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001286 }
Dave Chapmane92d2c52008-03-21 13:41:35 +00001287
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 Gordonf002b7d2008-06-05 08:20:39 +00001295 {
Jonathan Gordonae49b382008-06-07 09:01:02 +00001296 if (!set_vals || *p != '-')
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001297 goto err;
1298 while (*p && *p != sep)
1299 p++;
1300 }
1301 else
1302 {
1303 p += 6;
Jonathan Gordonae49b382008-06-07 09:01:02 +00001304 set = true;
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001305 }
Dave Chapmane92d2c52008-03-21 13:41:35 +00001306
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 Gordonf002b7d2008-06-05 08:20:39 +00001315 {
Dave Chapmane92d2c52008-03-21 13:41:35 +00001316 *d = *p++ - '0';
Jonathan Gordonae49b382008-06-07 09:01:02 +00001317 set = true;
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001318 }
Jonathan Gordonae49b382008-06-07 09:01:02 +00001319 else if (!set_vals || *p != '-')
Dave Chapmane92d2c52008-03-21 13:41:35 +00001320 goto err;
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001321 else
1322 {
1323 while (*p && *p != sep)
1324 p++;
1325 }
Dave Chapmane92d2c52008-03-21 13:41:35 +00001326
1327 break;
1328#endif
1329
1330 default: /* Unknown format type */
1331 goto err;
1332 break;
1333 }
Jonathan Gordonae49b382008-06-07 09:01:02 +00001334 if (set_vals && set)
1335 *set_vals |= (1<<i);
Jonathan Gordonf002b7d2008-06-05 08:20:39 +00001336 i++;
Dave Chapmane92d2c52008-03-21 13:41:35 +00001337 }
1338
1339 va_end(ap);
1340 return p;
1341
1342err:
1343 va_end(ap);
1344 return 0;
1345}
1346#endif