Introduce a small api for loading code (codecs,plugins) from disk/memory.
[maemo-rb.git] / uisimulator / common / io.c
blobc8d6a8a67df3654acfb42d0250adf6121ae69e25
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 (0 == (CONFIG_PLATFORM & PLATFORM_ANDROID) && !defined(WIN32))
32 #if HAVE_STATVFS
33 #include <sys/statvfs.h>
34 #endif
36 #ifdef WIN32
37 #include <windows.h>
38 #endif
40 #ifndef _MSC_VER
41 #include <dirent.h>
42 #include <unistd.h>
43 #else
44 #include "dir-win32.h"
45 #endif
47 #include <fcntl.h>
48 #if (CONFIG_PLATFORM & PLATFORM_SDL)
49 #include <SDL.h>
50 #include <SDL_thread.h>
51 #include "thread-sdl.h"
52 #else
53 #define sim_thread_unlock() NULL
54 #define sim_thread_lock(a)
55 #endif
56 #include "thread.h"
57 #include "kernel.h"
58 #include "debug.h"
59 #include "ata.h" /* for IF_MV2 et al. */
60 #include "rbpaths.h"
61 #include "load_code.h"
63 /* keep this in sync with file.h! */
64 #undef MAX_PATH /* this avoids problems when building simulator */
65 #define MAX_PATH 260
66 #define MAX_OPEN_FILES 11
68 /* Windows (and potentially other OSes) distinguish binary and text files.
69 * Define a dummy for the others. */
70 #ifndef O_BINARY
71 #define O_BINARY 0
72 #endif
74 /* Unicode compatibility for win32 */
75 #if defined __MINGW32__
76 /* Rockbox unicode functions */
77 extern const unsigned char* utf8decode(const unsigned char *utf8,
78 unsigned short *ucs);
79 extern unsigned char* utf8encode(unsigned long ucs, unsigned char *utf8);
81 /* Static buffers for the conversion results. This isn't thread safe,
82 * but it's sufficient for rockbox. */
83 static unsigned char convbuf1[3*MAX_PATH];
84 static unsigned char convbuf2[3*MAX_PATH];
86 static wchar_t* utf8_to_ucs2(const unsigned char *utf8, void *buffer)
88 wchar_t *ucs = buffer;
90 while (*utf8)
91 utf8 = utf8decode(utf8, ucs++);
93 *ucs = 0;
94 return buffer;
96 static unsigned char *ucs2_to_utf8(const wchar_t *ucs, unsigned char *buffer)
98 unsigned char *utf8 = buffer;
100 while (*ucs)
101 utf8 = utf8encode(*ucs++, utf8);
103 *utf8 = 0;
104 return buffer;
107 #define UTF8_TO_OS(a) utf8_to_ucs2(a,convbuf1)
108 #define OS_TO_UTF8(a) ucs2_to_utf8(a,convbuf1)
109 #define DIR_T _WDIR
110 #define DIRENT_T struct _wdirent
111 #define STAT_T struct _stat
112 extern int _wmkdir(const wchar_t*);
113 extern int _wrmdir(const wchar_t*);
114 #define MKDIR(a,b) (_wmkdir)(UTF8_TO_OS(a))
115 #define RMDIR(a) (_wrmdir)(UTF8_TO_OS(a))
116 #define OPENDIR(a) (_wopendir)(UTF8_TO_OS(a))
117 #define READDIR(a) (_wreaddir)(a)
118 #define CLOSEDIR(a) (_wclosedir)(a)
119 #define STAT(a,b) (_wstat)(UTF8_TO_OS(a),b)
120 /* empty variable parameter list doesn't work for variadic macros,
121 * so pretend the second parameter is variable too */
122 #define OPEN(a,...) (_wopen)(UTF8_TO_OS(a), __VA_ARGS__)
123 #define CLOSE(a) (close)(a)
124 #define REMOVE(a) (_wremove)(UTF8_TO_OS(a))
125 #define RENAME(a,b) (_wrename)(UTF8_TO_OS(a),utf8_to_ucs2(b,convbuf2))
127 #else /* !__MINGW32__ */
129 #define UTF8_TO_OS(a) (a)
130 #define OS_TO_UTF8(a) (a)
131 #define DIR_T DIR
132 #define DIRENT_T struct dirent
133 #define STAT_T struct stat
134 #define MKDIR(a,b) (mkdir)(a,b)
135 #define RMDIR(a) (rmdir)(a)
136 #define OPENDIR(a) (opendir)(a)
137 #define READDIR(a) (readdir)(a)
138 #define CLOSEDIR(a) (closedir)(a)
139 #define STAT(a,b) (stat)(a,b)
140 /* empty variable parameter list doesn't work for variadic macros,
141 * so pretend the second parameter is variable too */
142 #define OPEN(a, ...) (open)(a, __VA_ARGS__)
143 #define CLOSE(x) (close)(x)
144 #define REMOVE(a) (remove)(a)
145 #define RENAME(a,b) (rename)(a,b)
147 #endif /* !__MINGW32__ */
150 #ifdef HAVE_DIRCACHE
151 void dircache_remove(const char *name);
152 void dircache_rename(const char *oldname, const char *newname);
153 #endif
156 #define SIMULATOR_DEFAULT_ROOT "simdisk"
157 extern const char *sim_root_dir;
159 static int num_openfiles = 0;
161 struct sim_dirent {
162 unsigned char d_name[MAX_PATH];
163 int attribute;
164 long size;
165 long startcluster;
166 unsigned short wrtdate; /* Last write date */
167 unsigned short wrttime; /* Last write time */
170 struct dirstruct {
171 void *dir; /* actually a DIR* dir */
172 char *name;
173 } SIM_DIR;
175 struct mydir {
176 DIR_T *dir;
177 char *name;
180 typedef struct mydir MYDIR;
182 #if 1 /* maybe this needs disabling for MSVC... */
183 static unsigned int rockbox2sim(int opt)
185 int newopt = O_BINARY;
187 if(opt & 1)
188 newopt |= O_WRONLY;
189 if(opt & 2)
190 newopt |= O_RDWR;
191 if(opt & 4)
192 newopt |= O_CREAT;
193 if(opt & 8)
194 newopt |= O_APPEND;
195 if(opt & 0x10)
196 newopt |= O_TRUNC;
198 return newopt;
200 #endif
202 /** Simulator I/O engine routines **/
203 #define IO_YIELD_THRESHOLD 512
205 enum io_dir
207 IO_READ,
208 IO_WRITE,
211 struct sim_io
213 struct mutex sim_mutex; /* Rockbox mutex */
214 int cmd; /* The command to perform */
215 int ready; /* I/O ready flag - 1= ready */
216 int fd; /* The file to read/write */
217 void *buf; /* The buffer to read/write */
218 size_t count; /* Number of bytes to read/write */
219 size_t accum; /* Acculated bytes transferred */
222 static struct sim_io io;
224 int ata_init(void)
226 /* Initialize the rockbox kernel objects on a rockbox thread */
227 mutex_init(&io.sim_mutex);
228 io.accum = 0;
229 return 1;
232 int ata_spinup_time(void)
234 return HZ;
237 static ssize_t io_trigger_and_wait(enum io_dir cmd)
239 void *mythread = NULL;
240 ssize_t result;
242 if (io.count > IO_YIELD_THRESHOLD ||
243 (io.accum += io.count) >= IO_YIELD_THRESHOLD)
245 /* Allow other rockbox threads to run */
246 io.accum = 0;
247 mythread = sim_thread_unlock();
250 switch (cmd)
252 case IO_READ:
253 result = read(io.fd, io.buf, io.count);
254 break;
255 case IO_WRITE:
256 result = write(io.fd, io.buf, io.count);
257 break;
258 /* shut up gcc */
259 default:
260 result = -1;
263 /* Regain our status as current */
264 if (mythread != NULL)
266 sim_thread_lock(mythread);
269 return result;
272 #if !defined(__PCTOOL__) && !defined(APPLICATION)
273 static const char *get_sim_pathname(const char *name)
275 static char buffer[MAX_PATH]; /* sufficiently big */
277 if(name[0] == '/')
279 snprintf(buffer, sizeof(buffer), "%s%s",
280 sim_root_dir != NULL ? sim_root_dir : SIMULATOR_DEFAULT_ROOT, name);
281 return buffer;
283 fprintf(stderr, "WARNING, bad file name lacks slash: %s\n", name);
284 return name;
286 #else
287 #define get_sim_pathname(name) name
288 #endif
290 MYDIR *sim_opendir(const char *name)
292 DIR_T *dir;
294 dir = (DIR_T *) OPENDIR(get_sim_pathname(name));
296 if (dir)
298 MYDIR *my = (MYDIR *)malloc(sizeof(MYDIR));
299 my->dir = dir;
300 my->name = (char *)malloc(strlen(name)+1);
301 strcpy(my->name, name);
303 return my;
305 /* failed open, return NULL */
306 return (MYDIR *)0;
309 struct sim_dirent *sim_readdir(MYDIR *dir)
311 char buffer[MAX_PATH]; /* sufficiently big */
312 static struct sim_dirent secret;
313 STAT_T s;
314 DIRENT_T *x11 = READDIR(dir->dir);
315 struct tm* tm;
317 if(!x11)
318 return (struct sim_dirent *)0;
320 strcpy((char *)secret.d_name, OS_TO_UTF8(x11->d_name));
322 /* build file name */
323 snprintf(buffer, sizeof(buffer), "%s/%s",
324 get_sim_pathname(dir->name), secret.d_name);
325 STAT(buffer, &s); /* get info */
327 #define ATTR_DIRECTORY 0x10
329 secret.attribute = S_ISDIR(s.st_mode)?ATTR_DIRECTORY:0;
330 secret.size = s.st_size;
332 tm = localtime(&(s.st_mtime));
333 secret.wrtdate = ((tm->tm_year - 80) << 9) |
334 ((tm->tm_mon + 1) << 5) |
335 tm->tm_mday;
336 secret.wrttime = (tm->tm_hour << 11) |
337 (tm->tm_min << 5) |
338 (tm->tm_sec >> 1);
339 return &secret;
342 void sim_closedir(MYDIR *dir)
344 free(dir->name);
345 CLOSEDIR(dir->dir);
347 free(dir);
350 int sim_open(const char *name, int o, ...)
352 int opts = rockbox2sim(o);
353 int ret;
354 if (num_openfiles >= MAX_OPEN_FILES)
355 return -2;
357 if (opts & O_CREAT)
359 va_list ap;
360 va_start(ap, o);
361 mode_t mode = va_arg(ap, unsigned int);
362 ret = OPEN(get_sim_pathname(name), opts, mode);
363 va_end(ap);
365 else
366 ret = OPEN(get_sim_pathname(name), opts);
368 if (ret >= 0)
369 num_openfiles++;
370 return ret;
373 int sim_close(int fd)
375 int ret;
376 ret = CLOSE(fd);
377 if (ret == 0)
378 num_openfiles--;
379 return ret;
382 int sim_creat(const char *name, mode_t mode)
384 return OPEN(get_sim_pathname(name), O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, mode);
387 ssize_t sim_read(int fd, void *buf, size_t count)
389 ssize_t result;
391 mutex_lock(&io.sim_mutex);
393 /* Setup parameters */
394 io.fd = fd;
395 io.buf = buf;
396 io.count = count;
398 result = io_trigger_and_wait(IO_READ);
400 mutex_unlock(&io.sim_mutex);
402 return result;
405 ssize_t sim_write(int fd, const void *buf, size_t count)
407 ssize_t result;
409 mutex_lock(&io.sim_mutex);
411 io.fd = fd;
412 io.buf = (void*)buf;
413 io.count = count;
415 result = io_trigger_and_wait(IO_WRITE);
417 mutex_unlock(&io.sim_mutex);
419 return result;
422 int sim_mkdir(const char *name)
424 return MKDIR(get_sim_pathname(name), 0777);
427 int sim_rmdir(const char *name)
429 return RMDIR(get_sim_pathname(name));
432 int sim_remove(const char *name)
434 #ifdef HAVE_DIRCACHE
435 dircache_remove(name);
436 #endif
437 return REMOVE(get_sim_pathname(name));
440 int sim_rename(const char *oldname, const char *newname)
442 char sim_old[MAX_PATH];
443 char sim_new[MAX_PATH];
444 #ifdef HAVE_DIRCACHE
445 dircache_rename(oldname, newname);
446 #endif
447 // This is needed as get_sim_pathname() has a static buffer
448 strncpy(sim_old, get_sim_pathname(oldname), MAX_PATH);
449 strncpy(sim_new, get_sim_pathname(newname), MAX_PATH);
450 return RENAME(sim_old, sim_new);
453 /* rockbox off_t may be different from system off_t */
454 long sim_lseek(int fildes, long offset, int whence)
456 return lseek(fildes, offset, whence);
459 long sim_filesize(int fd)
461 #ifdef WIN32
462 return _filelength(fd);
463 #else
464 struct stat buf;
466 if (!fstat(fd, &buf))
467 return buf.st_size;
468 else
469 return -1;
470 #endif
473 void fat_size(IF_MV2(int volume,) unsigned long* size, unsigned long* free)
475 #ifdef HAVE_MULTIVOLUME
476 if (volume != 0) {
477 /* debugf("io.c: fat_size(volume=%d); simulator only supports volume 0\n",volume); */
479 if (size) *size = 0;
480 if (free) *free = 0;
481 return;
483 #endif
485 #ifdef WIN32
486 long secperclus, bytespersec, free_clusters, num_clusters;
488 if (GetDiskFreeSpace(NULL, &secperclus, &bytespersec, &free_clusters,
489 &num_clusters)) {
490 if (size)
491 *size = num_clusters * secperclus / 2 * (bytespersec / 512);
492 if (free)
493 *free = free_clusters * secperclus / 2 * (bytespersec / 512);
495 #elif HAVE_STATVFS
496 struct statvfs vfs;
498 if (!statvfs(".", &vfs)) {
499 DEBUGF("statvfs: frsize=%d blocks=%ld free=%ld\n",
500 (int)vfs.f_frsize, (long)vfs.f_blocks, (long)vfs.f_bfree);
501 if (size)
502 *size = vfs.f_blocks / 2 * (vfs.f_frsize / 512);
503 if (free)
504 *free = vfs.f_bfree / 2 * (vfs.f_frsize / 512);
505 } else
506 #endif
508 if (size)
509 *size = 0;
510 if (free)
511 *free = 0;
515 int sim_fsync(int fd)
517 #ifdef WIN32
518 return _commit(fd);
519 #else
520 return fsync(fd);
521 #endif
524 #ifdef WIN32
525 /* sim-win32 */
526 #define dlopen(_x_, _y_) LoadLibraryW(UTF8_TO_OS(_x_))
527 #define dlsym(_x_, _y_) (void *)GetProcAddress(_x_, _y_)
528 #define dlclose(_x_) FreeLibrary(_x_)
529 #else
530 /* sim-x11 */
531 #include <dlfcn.h>
532 #endif
535 void *lc_open(const char *filename, char *buf, size_t buf_size)
537 const char *sim_path = get_sim_pathname(filename);
538 void *handle = _lc_open((const char*)UTF8_TO_OS(sim_path), buf, buf_size);
540 if (handle == NULL)
542 DEBUGF("failed to load %s\n", filename);
543 DEBUGF("lc_open(%s): %s\n", filename, lc_last_error());
545 return handle;
548 void *lc_get_header(void *handle)
550 return _lc_get_header(handle);
553 void lc_close(void *handle)
555 _lc_close(handle);
558 #ifdef WIN32
559 static unsigned old_cp;
561 void debug_exit(void)
563 /* Reset console output codepage */
564 SetConsoleOutputCP(old_cp);
567 void debug_init(void)
569 old_cp = GetConsoleOutputCP();
570 /* Set console output codepage to UTF8. Only works
571 * correctly when the console uses a truetype font. */
572 SetConsoleOutputCP(65001);
573 atexit(debug_exit);
575 #else
576 void debug_init(void)
578 /* nothing to be done */
580 #endif
582 void debugf(const char *fmt, ...)
584 va_list ap;
585 va_start( ap, fmt );
586 vfprintf( stderr, fmt, ap );
587 va_end( ap );
590 void ldebugf(const char* file, int line, const char *fmt, ...)
592 va_list ap;
593 va_start( ap, fmt );
594 fprintf( stderr, "%s:%d ", file, line );
595 vfprintf( stderr, fmt, ap );
596 va_end( ap );
599 /* rockbox off_t may be different from system off_t */
600 int sim_ftruncate(int fd, long length)
602 #ifdef WIN32
603 return _chsize(fd, length);
604 #else
605 return ftruncate(fd, length);
606 #endif