+static int emu_sound_ioctl(int fd, int request, void *argp)
+{
+ int *arg = argp;
+
+#if 0
+ dbg("snd ioctl(%d, %08x, %p)", fd, request, argp);
+ if (arg != NULL)
+ dbg_c(" [%d]", *arg);
+ dbg_c("\n");
+#endif
+
+ /* People set strange frag settings on GP2X, which even manage
+ * to break audio on pandora (causes writes to fail).
+ * Catch this and set to something that works. */
+ switch(request) {
+ case SNDCTL_DSP_SETFRAGMENT: {
+ int ret, bsize, frag, frag_cnt;
+ if (arg == NULL)
+ break;
+
+ frag = *arg & 0xffff;
+ frag_cnt = *arg >> 16;
+ bsize = frag_cnt << frag;
+ if (frag < 10 || bsize < 4096*4 || bsize > 4096*4*2) {
+ /*
+ * ~4ms. gpSP wants small buffers or else it stutters
+ * because of it's audio thread sync stuff
+ * XXX: hardcoding, as low samplerates will result in small fragment size,
+ * which itself causes ALSA stall and hangs the program.
+ * Also some apps change samplerate without reopening /dev/dsp,
+ * which causes ALSA to reject SNDCTL_DSP_SETFRAGMENT.
+ */
+ bsize = 44100 / 250 * 4;
+
+ for (frag = 0; bsize; bsize >>= 1, frag++)
+ ;
+
+ frag_cnt = 16;
+ }
+
+ frag |= frag_cnt << 16;
+ ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
+ if (ret != 0) {
+ err("snd ioctl SETFRAGMENT %08x: ", frag);
+ perror(NULL);
+ }
+ // indicate success even if we fail (because of ALSA mostly),
+ // things like MikMod will bail out otherwise.
+ return 0;
+ }
+ case SNDCTL_DSP_SYNC:
+ // Franxis tends to use sync/write loops, bad idea under ALSA
+ return 0;
+ default:
+ break;
+ }
+
+ return ioctl(fd, request, argp);
+}
+
+int emu_do_ioctl(int fd, int request, void *argp)
+{
+ if (fd == emu_interesting_fds[IFD_SOUND].fd)
+ return emu_sound_ioctl(fd, request, argp);
+
+ switch (fd) {
+ /* *********************** */
+ case FAKEDEV_FB0:
+ case FAKEDEV_FB1:
+ if (argp == NULL)
+ goto fail;
+
+ switch (request) {
+ case FBIOGET_FSCREENINFO: {
+ struct fb_fix_screeninfo *fix = argp;
+
+ memset(fix, 0, sizeof(*fix));
+ strcpy(fix->id, "mmsp2_RGB0");
+ fix->type = FB_TYPE_PACKED_PIXELS;
+ fix->accel = FB_ACCEL_NONE;
+ fix->visual = FB_VISUAL_TRUECOLOR;
+ fix->line_length = 320*2;
+ fix->smem_start = (fd == FAKEDEV_FB0) ? 0x03101000 : 0x03381000;
+ fix->smem_len = 320*240*2;
+ return 0;
+ }
+ case FBIOGET_VSCREENINFO: {
+ struct fb_var_screeninfo *var = argp;
+ static const struct fb_bitfield fbb_red = { offset: 11, length: 5, };
+ static const struct fb_bitfield fbb_green = { offset: 5, length: 6, };
+ static const struct fb_bitfield fbb_blue = { offset: 0, length: 5, };
+
+ memset(var, 0, sizeof(*var));
+ var->activate = FB_ACTIVATE_NOW;
+ var->xres =
+ var->xres_virtual = 320;
+ var->yres =
+ var->yres_virtual = 240;
+ var->width =
+ var->height = -1;
+ var->vmode = FB_VMODE_NONINTERLACED;
+ var->bits_per_pixel = 16;
+ var->red = fbb_red;
+ var->green = fbb_green;
+ var->blue = fbb_blue;
+ return 0;
+ }
+ case FBIOPUT_VSCREENINFO: {
+ struct fb_var_screeninfo *var = argp;
+ dbg(" put vscreen: %dx%d@%d\n", var->xres, var->yres, var->bits_per_pixel);
+ if (var->xres != 320 || var->yres != 240 || var->bits_per_pixel != 16)
+ return -1;
+ return 0;
+ }
+ }
+
+ /* *********************** */
+ case FAKEDEV_TTY0:
+ // fake tty0 to make GPH SDL happy
+ if (request == 0x4b46) // KDGKBENT
+ return -1;
+ return 0;
+ }
+
+fail:
+ err("bad/ni ioctl(%d, %08x, %p)\n", fd, request, argp);
+ errno = EINVAL;
+ return -1;
+}
+
+struct dev_fd_t emu_interesting_fds[] = {
+ [IFD_SOUND] = { "/dev/dsp", -1, emu_sound_open },
+ { NULL, 0, NULL },
+};
+
+static const struct {
+ const char *from;
+ const char *to;
+} path_map[] = {
+ { "/mnt/tmp", "./tmp" },
+};
+
+static const char *wrap_path(const char *path)
+{
+ char *buff, *p;
+ size_t size;
+ int i, len;
+
+ // do only path mapping for now
+ for (i = 0; i < ARRAY_SIZE(path_map); i++) {
+ p = strstr(path, path_map[i].from);
+ if (p != NULL) {
+ size = strlen(path) + strlen(path_map[i].to) + 1;
+ buff = malloc(size);
+ if (buff == NULL)
+ break;
+ len = p - path;
+ strncpy(buff, path, len);
+ snprintf(buff + len, size - len, "%s%s", path_map[i].to,
+ path + len + strlen(path_map[i].from));
+ dbg("mapped path \"%s\" -> \"%s\"\n", path, buff);
+ return buff;
+ }
+ }
+
+ return path;
+}
+
+static void wrap_path_free(const char *w_path, const char *old_path)
+{
+ if (w_path != old_path)
+ free((void *)w_path);
+}
+
+void *emu_do_fopen(const char *path, const char *mode)
+{
+ const char *w_path;
+ FILE *ret;
+
+ w_path = wrap_path(path);
+ ret = fopen(w_path, mode);
+ wrap_path_free(w_path, path);
+
+ return ret;
+}
+
+// FIXME: threads..
+int emu_do_system(const char *command)
+{
+ static char tmp_path[512];
+ int need_ginge = 0;
+ const char *p2;
+ char *p;
+ int ret;
+
+ if (command == NULL)
+ return -1;
+
+ for (p2 = command; *p2 && isspace(*p2); p2++)
+ ;
+
+ if (*p2 == '.') // relative path?
+ need_ginge = 1;
+ else if (*p2 == '/' && strncmp(p2, "/bin", 4) && strncmp(p2, "/lib", 4)
+ && strncmp(p2, "/sbin", 4) && strncmp(p2, "/usr", 4))
+ // absolute path, but not a system command
+ need_ginge = 1;
+
+ p2 = wrap_path(command);
+ if (need_ginge) {
+ make_local_path(tmp_path, sizeof(tmp_path), "ginge_prep");
+ p = tmp_path + strlen(tmp_path);
+
+ snprintf(p, sizeof(tmp_path) - (p - tmp_path), " --nomenu %s", p2);