Calibrate iPod Classic battery gauge a bit better
[kugel-rb.git] / uisimulator / common / io.c
blob0d680e0e8a7d55b4737dae34768ea1c69e139885
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Daniel Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <sys/stat.h>
27 #include <time.h>
28 #include "config.h"
30 #define HAVE_STATVFS (!defined(WIN32))
31 #define HAVE_LSTAT (!defined(WIN32))
33 #if HAVE_STATVFS
34 #include <sys/statvfs.h>
35 #endif
37 #ifdef WIN32
38 #include <windows.h>
39 #endif
41 #ifndef _MSC_VER
42 #include <dirent.h>
43 #include <unistd.h>
44 #else
45 #include "dir-win32.h"
46 #endif
48 #include <fcntl.h>
49 #ifdef HAVE_SDL_THREADS
50 #include "thread-sdl.h"
51 #else
52 #define sim_thread_unlock() NULL
53 #define sim_thread_lock(a)
54 #endif
55 #include "thread.h"
56 #include "kernel.h"
57 #include "debug.h"
58 #include "ata.h" /* for IF_MV2 et al. */
59 #include "rbpaths.h"
60 #include "load_code.h"
62 /* keep this in sync with file.h! */
63 #undef MAX_PATH /* this avoids problems when building simulator */
64 #define MAX_PATH 260
65 #define MAX_OPEN_FILES 11
67 /* Windows (and potentially other OSes) distinguish binary and text files.
68 * Define a dummy for the others. */
69 #ifndef O_BINARY
70 #define O_BINARY 0
71 #endif
73 /* Unicode compatibility for win32 */
74 #if defined __MINGW32__
75 /* Rockbox unicode functions */
76 extern const unsigned char* utf8decode(const unsigned char *utf8,
77 unsigned short *ucs);
78 extern unsigned char* utf8encode(unsigned long ucs, unsigned char *utf8);
80 /* Static buffers for the conversion results. This isn't thread safe,
81 * but it's sufficient for rockbox. */
82 static unsigned char convbuf1[3*MAX_PATH];
83 static unsigned char convbuf2[3*MAX_PATH];
85 static wchar_t* utf8_to_ucs2(const unsigned char *utf8, void *buffer)
87 wchar_t *ucs = buffer;
89 while (*utf8)
90 utf8 = utf8decode(utf8, ucs++);
92 *ucs = 0;
93 return buffer;
95 static unsigned char *ucs2_to_utf8(const wchar_t *ucs, unsigned char *buffer)
97 unsigned char *utf8 = buffer;
99 while (*ucs)
100 utf8 = utf8encode(*ucs++, utf8);
102 *utf8 = 0;
103 return buffer;
106 #define UTF8_TO_OS(a) utf8_to_ucs2(a,convbuf1)
107 #define OS_TO_UTF8(a) ucs2_to_utf8(a,convbuf1)
108 #define DIR_T _WDIR
109 #define DIRENT_T struct _wdirent
110 #define STAT_T struct _stat
111 extern int _wmkdir(const wchar_t*);
112 extern int _wrmdir(const wchar_t*);
113 #define MKDIR(a,b) (_wmkdir)(UTF8_TO_OS(a))
114 #define RMDIR(a) (_wrmdir)(UTF8_TO_OS(a))
115 #define OPENDIR(a) (_wopendir)(UTF8_TO_OS(a))
116 #define READDIR(a) (_wreaddir)(a)
117 #define CLOSEDIR(a) (_wclosedir)(a)
118 #define STAT(a,b) (_wstat)(UTF8_TO_OS(a),b)
119 /* empty variable parameter list doesn't work for variadic macros,
120 * so pretend the second parameter is variable too */
121 #define OPEN(a,...) (_wopen)(UTF8_TO_OS(a), __VA_ARGS__)
122 #define CLOSE(a) (close)(a)
123 #define REMOVE(a) (_wremove)(UTF8_TO_OS(a))
124 #define RENAME(a,b) (_wrename)(UTF8_TO_OS(a),utf8_to_ucs2(b,convbuf2))
126 #else /* !__MINGW32__ */
128 #define UTF8_TO_OS(a) (a)
129 #define OS_TO_UTF8(a) (a)
130 #define DIR_T DIR
131 #define DIRENT_T struct dirent
132 #define STAT_T struct stat
133 #define MKDIR(a,b) (mkdir)(a,b)
134 #define RMDIR(a) (rmdir)(a)
135 #define OPENDIR(a) (opendir)(a)
136 #define READDIR(a) (readdir)(a)
137 #define CLOSEDIR(a) (closedir)(a)
138 #define STAT(a,b) (stat)(a,b)
139 /* empty variable parameter list doesn't work for variadic macros,
140 * so pretend the second parameter is variable too */
141 #define OPEN(a, ...) (open)(a, __VA_ARGS__)
142 #define CLOSE(x) (close)(x)
143 #define REMOVE(a) (remove)(a)
144 #define RENAME(a,b) (rename)(a,b)
146 #endif /* !__MINGW32__ */
149 #ifdef HAVE_DIRCACHE
150 void dircache_remove(const char *name);
151 void dircache_rename(const char *oldname, const char *newname);
152 #endif
155 #define SIMULATOR_DEFAULT_ROOT "simdisk"
156 extern const char *sim_root_dir;
158 static int num_openfiles = 0;
160 /* from dir.h */
161 struct dirinfo {
162 int attribute;
163 long size;
164 unsigned short wrtdate;
165 unsigned short wrttime;
168 struct sim_dirent {
169 unsigned char d_name[MAX_PATH];
170 struct dirinfo info;
171 long startcluster;
174 struct dirstruct {
175 void *dir; /* actually a DIR* dir */
176 char *name;
177 } SIM_DIR;
179 struct mydir {
180 DIR_T *dir;
181 char *name;
184 typedef struct mydir MYDIR;
186 static unsigned int rockbox2sim(int opt)
188 #if 0
189 /* this shouldn't be needed since we use the host's versions */
190 int newopt = O_BINARY;
192 if(opt & 1)
193 newopt |= O_WRONLY;
194 if(opt & 2)
195 newopt |= O_RDWR;
196 if(opt & 4)
197 newopt |= O_CREAT;
198 if(opt & 8)
199 newopt |= O_APPEND;
200 if(opt & 0x10)
201 newopt |= O_TRUNC;
203 return newopt;
204 #else
205 return opt|O_BINARY;
206 #endif
209 /** Simulator I/O engine routines **/
210 #define IO_YIELD_THRESHOLD 512
212 enum io_dir
214 IO_READ,
215 IO_WRITE,
218 struct sim_io
220 struct mutex sim_mutex; /* Rockbox mutex */
221 int cmd; /* The command to perform */
222 int ready; /* I/O ready flag - 1= ready */
223 int fd; /* The file to read/write */
224 void *buf; /* The buffer to read/write */
225 size_t count; /* Number of bytes to read/write */
226 size_t accum; /* Acculated bytes transferred */
229 static struct sim_io io;
231 int ata_init(void)
233 /* Initialize the rockbox kernel objects on a rockbox thread */
234 mutex_init(&io.sim_mutex);
235 io.accum = 0;
236 return 1;
239 int ata_spinup_time(void)
241 return HZ;
244 static ssize_t io_trigger_and_wait(enum io_dir cmd)
246 void *mythread = NULL;
247 ssize_t result;
249 if (io.count > IO_YIELD_THRESHOLD ||
250 (io.accum += io.count) >= IO_YIELD_THRESHOLD)
252 /* Allow other rockbox threads to run */
253 io.accum = 0;
254 mythread = sim_thread_unlock();
257 switch (cmd)
259 case IO_READ:
260 result = read(io.fd, io.buf, io.count);
261 break;
262 case IO_WRITE:
263 result = write(io.fd, io.buf, io.count);
264 break;
265 /* shut up gcc */
266 default:
267 result = -1;
270 /* Regain our status as current */
271 if (mythread != NULL)
273 sim_thread_lock(mythread);
276 return result;
279 #if !defined(__PCTOOL__) && !defined(APPLICATION)
280 static const char *get_sim_pathname(const char *name)
282 static char buffer[MAX_PATH]; /* sufficiently big */
284 if(name[0] == '/')
286 snprintf(buffer, sizeof(buffer), "%s%s",
287 sim_root_dir != NULL ? sim_root_dir : SIMULATOR_DEFAULT_ROOT, name);
288 return buffer;
290 fprintf(stderr, "WARNING, bad file name lacks slash: %s\n", name);
291 return name;
293 #else
294 #define get_sim_pathname(name) name
295 #endif
297 MYDIR *sim_opendir(const char *name)
299 DIR_T *dir;
300 dir = (DIR_T *) OPENDIR(get_sim_pathname(name));
302 if (dir)
304 MYDIR *my = (MYDIR *)malloc(sizeof(MYDIR));
305 my->dir = dir;
306 my->name = (char *)malloc(strlen(name)+1);
307 strcpy(my->name, name);
309 return my;
311 /* failed open, return NULL */
312 return (MYDIR *)0;
315 #if defined(WIN32)
316 static inline struct tm* localtime_r (const time_t *clock, struct tm *result) {
317 if (!clock || !result) return NULL;
318 memcpy(result,localtime(clock),sizeof(*result));
319 return result;
321 #endif
323 struct sim_dirent *sim_readdir(MYDIR *dir)
325 char buffer[MAX_PATH]; /* sufficiently big */
326 static struct sim_dirent secret;
327 STAT_T s;
328 DIRENT_T *x11 = READDIR(dir->dir);
329 struct tm tm;
331 if(!x11)
332 return (struct sim_dirent *)0;
334 strcpy((char *)secret.d_name, OS_TO_UTF8(x11->d_name));
336 /* build file name */
337 snprintf(buffer, sizeof(buffer), "%s/%s",
338 get_sim_pathname(dir->name), secret.d_name);
339 if (STAT(buffer, &s)) /* get info */
340 return NULL;
342 #define ATTR_DIRECTORY 0x10
344 secret.info.attribute = 0;
346 if (S_ISDIR(s.st_mode))
347 secret.info.attribute = ATTR_DIRECTORY;
349 secret.info.size = s.st_size;
351 if (localtime_r(&(s.st_mtime), &tm) == NULL)
352 return NULL;
353 secret.info.wrtdate = ((tm.tm_year - 80) << 9) |
354 ((tm.tm_mon + 1) << 5) |
355 tm.tm_mday;
356 secret.info.wrttime = (tm.tm_hour << 11) |
357 (tm.tm_min << 5) |
358 (tm.tm_sec >> 1);
360 #if HAVE_LSTAT
361 #define ATTR_LINK 0x80
362 if (!lstat(buffer, &s) && S_ISLNK(s.st_mode))
364 secret.info.attribute |= ATTR_LINK;
366 #endif
368 return &secret;
371 void sim_closedir(MYDIR *dir)
373 free(dir->name);
374 CLOSEDIR(dir->dir);
376 free(dir);
379 int sim_open(const char *name, int o, ...)
381 int opts = rockbox2sim(o);
382 int ret;
383 if (num_openfiles >= MAX_OPEN_FILES)
384 return -2;
386 if (opts & O_CREAT)
388 va_list ap;
389 va_start(ap, o);
390 mode_t mode = va_arg(ap, unsigned int);
391 ret = OPEN(get_sim_pathname(name), opts, mode);
392 va_end(ap);
394 else
395 ret = OPEN(get_sim_pathname(name), opts);
397 if (ret >= 0)
398 num_openfiles++;
399 return ret;
402 int sim_close(int fd)
404 int ret;
405 ret = CLOSE(fd);
406 if (ret == 0)
407 num_openfiles--;
408 return ret;
411 int sim_creat(const char *name, mode_t mode)
413 return OPEN(get_sim_pathname(name), O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, mode);
416 ssize_t sim_read(int fd, void *buf, size_t count)
418 ssize_t result;
420 mutex_lock(&io.sim_mutex);
422 /* Setup parameters */
423 io.fd = fd;
424 io.buf = buf;
425 io.count = count;
427 result = io_trigger_and_wait(IO_READ);
429 mutex_unlock(&io.sim_mutex);
431 return result;
434 ssize_t sim_write(int fd, const void *buf, size_t count)
436 ssize_t result;
438 mutex_lock(&io.sim_mutex);
440 io.fd = fd;
441 io.buf = (void*)buf;
442 io.count = count;
444 result = io_trigger_and_wait(IO_WRITE);
446 mutex_unlock(&io.sim_mutex);
448 return result;
451 int sim_mkdir(const char *name)
453 return MKDIR(get_sim_pathname(name), 0777);
456 int sim_rmdir(const char *name)
458 return RMDIR(get_sim_pathname(name));
461 int sim_remove(const char *name)
463 #ifdef HAVE_DIRCACHE
464 dircache_remove(name);
465 #endif
466 return REMOVE(get_sim_pathname(name));
469 int sim_rename(const char *oldname, const char *newname)
471 char sim_old[MAX_PATH];
472 char sim_new[MAX_PATH];
473 #ifdef HAVE_DIRCACHE
474 dircache_rename(oldname, newname);
475 #endif
476 // This is needed as get_sim_pathname() has a static buffer
477 strncpy(sim_old, get_sim_pathname(oldname), MAX_PATH);
478 strncpy(sim_new, get_sim_pathname(newname), MAX_PATH);
479 return RENAME(sim_old, sim_new);
482 /* rockbox off_t may be different from system off_t */
483 long sim_lseek(int fildes, long offset, int whence)
485 return lseek(fildes, offset, whence);
488 long sim_filesize(int fd)
490 #ifdef WIN32
491 return _filelength(fd);
492 #else
493 struct stat buf;
495 if (!fstat(fd, &buf))
496 return buf.st_size;
497 else
498 return -1;
499 #endif
502 void fat_size(IF_MV2(int volume,) unsigned long* size, unsigned long* free)
504 #ifdef HAVE_MULTIVOLUME
505 if (volume != 0) {
506 /* debugf("io.c: fat_size(volume=%d); simulator only supports volume 0\n",volume); */
508 if (size) *size = 0;
509 if (free) *free = 0;
510 return;
512 #endif
514 #ifdef WIN32
515 long secperclus, bytespersec, free_clusters, num_clusters;
517 if (GetDiskFreeSpace(NULL, &secperclus, &bytespersec, &free_clusters,
518 &num_clusters)) {
519 if (size)
520 *size = num_clusters * secperclus / 2 * (bytespersec / 512);
521 if (free)
522 *free = free_clusters * secperclus / 2 * (bytespersec / 512);
524 #elif HAVE_STATVFS
525 struct statvfs vfs;
527 if (!statvfs(".", &vfs)) {
528 DEBUGF("statvfs: frsize=%d blocks=%ld free=%ld\n",
529 (int)vfs.f_frsize, (long)vfs.f_blocks, (long)vfs.f_bfree);
530 if (size)
531 *size = vfs.f_blocks / 2 * (vfs.f_frsize / 512);
532 if (free)
533 *free = vfs.f_bfree / 2 * (vfs.f_frsize / 512);
534 } else
535 #endif
537 if (size)
538 *size = 0;
539 if (free)
540 *free = 0;
544 int sim_fsync(int fd)
546 #ifdef WIN32
547 return _commit(fd);
548 #else
549 return fsync(fd);
550 #endif
553 #ifndef __PCTOOL__
555 void *lc_open(const char *filename, unsigned char *buf, size_t buf_size)
557 const char *sim_path = get_sim_pathname(filename);
558 void *handle = _lc_open(UTF8_TO_OS(sim_path), buf, buf_size);
560 if (handle == NULL)
562 DEBUGF("failed to load %s\n", filename);
563 DEBUGF("lc_open(%s): %s\n", filename, lc_last_error());
565 return handle;
568 void *lc_get_header(void *handle)
570 return _lc_get_header(handle);
573 void lc_close(void *handle)
575 _lc_close(handle);
578 #endif /* __PCTOOL__ */
579 #ifdef WIN32
580 static unsigned old_cp;
582 void debug_exit(void)
584 /* Reset console output codepage */
585 SetConsoleOutputCP(old_cp);
588 void debug_init(void)
590 old_cp = GetConsoleOutputCP();
591 /* Set console output codepage to UTF8. Only works
592 * correctly when the console uses a truetype font. */
593 SetConsoleOutputCP(65001);
594 atexit(debug_exit);
596 #else
597 void debug_init(void)
599 /* nothing to be done */
601 #endif
603 void debugf(const char *fmt, ...)
605 va_list ap;
606 va_start( ap, fmt );
607 vfprintf( stderr, fmt, ap );
608 va_end( ap );
611 void ldebugf(const char* file, int line, const char *fmt, ...)
613 va_list ap;
614 va_start( ap, fmt );
615 fprintf( stderr, "%s:%d ", file, line );
616 vfprintf( stderr, fmt, ap );
617 va_end( ap );
620 /* rockbox off_t may be different from system off_t */
621 int sim_ftruncate(int fd, long length)
623 #ifdef WIN32
624 return _chsize(fd, length);
625 #else
626 return ftruncate(fd, length);
627 #endif