Don't die on opendir() failure. Index .mp2 files too.
[kugel-rb.git] / apps / misc.c
blobb4a21a1dac55dbef202609057aa2f5520784705e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Daniel Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdlib.h>
20 #include <ctype.h>
21 #include "lang.h"
22 #include "string.h"
23 #include "config.h"
24 #include "file.h"
25 #include "lcd.h"
26 #include "sprintf.h"
27 #include "errno.h"
28 #include "system.h"
29 #include "timefuncs.h"
30 #include "screens.h"
31 #include "mpeg.h"
32 #include "mp3_playback.h"
33 #include "settings.h"
34 #include "ata.h"
35 #include "kernel.h"
36 #include "power.h"
37 #include "backlight.h"
38 #ifdef HAVE_MMC
39 #include "ata_mmc.h"
40 #endif
42 #define ONE_KILOBYTE 1024
43 #define ONE_MEGABYTE (1024*1024)
45 /* The point of this function would be to return a string of the input data,
46 but never longer than 5 columns. Add suffix k and M when suitable...
47 Make sure to have space for 6 bytes in the buffer. 5 letters plus the
48 terminating zero byte. */
49 char *num2max5(unsigned int bytes, char *max5)
51 if(bytes < 100000) {
52 snprintf(max5, 6, "%5d", bytes);
53 return max5;
55 if(bytes < (9999*ONE_KILOBYTE)) {
56 snprintf(max5, 6, "%4dk", bytes/ONE_KILOBYTE);
57 return max5;
59 if(bytes < (100*ONE_MEGABYTE)) {
60 /* 'XX.XM' is good as long as we're less than 100 megs */
61 snprintf(max5, 6, "%2d.%0dM",
62 bytes/ONE_MEGABYTE,
63 (bytes%ONE_MEGABYTE)/(ONE_MEGABYTE/10) );
64 return max5;
66 snprintf(max5, 6, "%4dM", bytes/ONE_MEGABYTE);
67 return max5;
70 /* Read (up to) a line of text from fd into buffer and return number of bytes
71 * read (which may be larger than the number of bytes stored in buffer). If
72 * an error occurs, -1 is returned (and buffer contains whatever could be
73 * read). A line is terminated by a LF char. Neither LF nor CR chars are
74 * stored in buffer.
76 int read_line(int fd, char* buffer, int buffer_size)
78 int count = 0;
79 int num_read = 0;
81 errno = 0;
83 while (count < buffer_size)
85 unsigned char c;
87 if (1 != read(fd, &c, 1))
88 break;
90 num_read++;
92 if ( c == '\n' )
93 break;
95 if ( c == '\r' )
96 continue;
98 buffer[count++] = c;
101 buffer[MIN(count, buffer_size - 1)] = 0;
103 return errno ? -1 : num_read;
106 #ifdef TEST_MAX5
107 int main(int argc, char **argv)
109 char buffer[32];
110 if(argc>1) {
111 printf("%d => %s\n",
112 atoi(argv[1]),
113 num2max5(atoi(argv[1]), buffer));
115 return 0;
118 #endif
120 #ifdef HAVE_LCD_BITMAP
121 extern unsigned char lcd_framebuffer[LCD_HEIGHT/8][LCD_WIDTH];
122 static const unsigned char bmpheader[] =
124 0x42, 0x4d, 0x3e, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00,
125 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x40, 0x00,
126 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
127 0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0x00, 0x00,
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0xee, 0x90, 0x00, 0x00, 0x00,
129 0x00, 0x00
132 void screen_dump(void)
134 int fh;
135 int bx, by, ix, iy;
136 int src_byte, src_mask, dst_mask;
137 char filename[MAX_PATH];
138 static unsigned char line_block[8][16];
139 struct tm *tm = get_time();
141 snprintf(filename, MAX_PATH, "/dump %04d-%02d-%02d %02d-%02d-%02d.bmp",
142 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
143 tm->tm_hour, tm->tm_min, tm->tm_sec);
145 fh = creat(filename, O_WRONLY);
146 if (fh < 0)
147 return;
149 write(fh, bmpheader, sizeof(bmpheader));
151 /* BMP image goes bottom up */
152 for (by = LCD_HEIGHT/8 - 1; by >= 0; by--)
154 memset(&line_block[0][0], 0, 8*16);
156 for (bx = 0; bx < LCD_WIDTH/8; bx++)
158 dst_mask = 0x01;
159 for (ix = 7; ix >= 0; ix--)
161 src_byte = lcd_framebuffer[by][8*bx+ix];
162 src_mask = 0x01;
163 for (iy = 7; iy >= 0; iy--)
165 if (src_byte & src_mask)
166 line_block[iy][bx] |= dst_mask;
167 src_mask <<= 1;
169 dst_mask <<= 1;
173 write(fh, &line_block[0][0], 8*16);
175 close(fh);
177 #endif
179 /* parse a line from a configuration file. the line format is:
181 name: value
183 Any whitespace before setting name or value (after ':') is ignored.
184 A # as first non-whitespace character discards the whole line.
185 Function sets pointers to null-terminated setting name and value.
186 Returns false if no valid config entry was found.
189 bool settings_parseline(char* line, char** name, char** value)
191 char* ptr;
193 while ( isspace(*line) )
194 line++;
196 if ( *line == '#' )
197 return false;
199 ptr = strchr(line, ':');
200 if ( !ptr )
201 return false;
203 *name = line;
204 *ptr = 0;
205 ptr++;
206 while (isspace(*ptr))
207 ptr++;
208 *value = ptr;
209 return true;
212 bool clean_shutdown(void)
214 #ifdef SIMULATOR
215 exit(0);
216 #else
217 if(!charger_inserted())
219 lcd_clear_display();
220 splash(0, true, str(LANG_SHUTTINGDOWN));
221 mpeg_stop();
222 ata_flush();
223 ata_spindown(1);
224 while(ata_disk_is_active())
225 sleep(HZ/10);
227 mp3_shutdown();
228 #if CONFIG_KEYPAD == ONDIO_PAD
229 backlight_off();
230 sleep(1);
231 lcd_set_contrast(0);
232 #endif
233 power_off();
235 #endif
236 return false;
239 int default_event_handler_ex(int event, void (*callback)(void *), void *parameter)
241 switch(event)
243 case SYS_USB_CONNECTED:
244 if (callback != NULL)
245 callback(parameter);
246 #ifdef HAVE_MMC
247 if (!mmc_detect() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
248 #endif
249 usb_screen();
250 return SYS_USB_CONNECTED;
251 case SYS_POWEROFF:
252 if (callback != NULL)
253 callback(parameter);
254 if (!clean_shutdown())
255 return SYS_POWEROFF;
256 break;
258 return 0;
261 int default_event_handler(int event)
263 return default_event_handler_ex(event, NULL, NULL);