+ extern void bgr555_to_rgb565(void *dst, void *src, int bytes);
+ static const int psx_widths[8] = { 256, 368, 320, 384, 512, 512, 640, 640 };
+ int x, y, w, h;
+ char fname[MAXPATHLEN];
+ GPUFreeze_t *gpu;
+ u16 *s, *d;
+ gzFile f;
+ int ret;
+ u32 tmp;
+
+ ret = get_state_filename(fname, sizeof(fname), slot);
+ if (ret != 0)
+ return;
+
+ f = gzopen(fname, "rb");
+ if (f == NULL)
+ return;
+
+ if (gzseek(f, 0x29933d, SEEK_SET) != 0x29933d) {
+ fprintf(stderr, "gzseek failed\n");
+ gzclose(f);
+ return;
+ }
+
+ gpu = malloc(sizeof(*gpu));
+ if (gpu == NULL) {
+ gzclose(f);
+ return;
+ }
+
+ ret = gzread(f, gpu, sizeof(*gpu));
+ gzclose(f);
+ if (ret != sizeof(*gpu)) {
+ fprintf(stderr, "gzread failed\n");
+ goto out;
+ }
+
+ memcpy(g_menubg_ptr, g_menubg_src_ptr, g_menuscreen_w * g_menuscreen_h * 2);
+
+ if ((gpu->ulStatus & 0x800000) || (gpu->ulStatus & 0x200000))
+ goto out; // disabled || 24bpp (NYET)
+
+ x = gpu->ulControl[5] & 0x3ff;
+ y = (gpu->ulControl[5] >> 10) & 0x1ff;
+ s = (u16 *)gpu->psxVRam + y * 1024 + (x & ~3);
+ w = psx_widths[(gpu->ulStatus >> 16) & 7];
+ tmp = gpu->ulControl[7];
+ h = ((tmp >> 10) & 0x3ff) - (tmp & 0x3ff);
+ if (gpu->ulStatus & 0x80000) // doubleheight
+ h *= 2;
+
+ x = max(0, g_menuscreen_w - w) & ~3;
+ y = max(0, g_menuscreen_h / 2 - h / 2);
+ w = min(g_menuscreen_w, w);
+ h = min(g_menuscreen_h, h);
+ d = (u16 *)g_menubg_ptr + g_menuscreen_w * y + x;
+
+ for (; h > 0; h--, d += g_menuscreen_w, s += 1024)
+ bgr555_to_rgb565(d, s, w * 2);
+
+out:
+ free(gpu);
+}
+
+// ---------- pandora specific -----------
+
+static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
+static char **pnd_filter_list;
+
+static int get_cpu_clock(void)
+{
+ FILE *f;
+ int ret = 0;
+ f = fopen("/proc/pandora/cpu_mhz_max", "r");
+ if (f) {
+ fscanf(f, "%d", &ret);
+ fclose(f);
+ }
+ return ret;
+}
+
+static void apply_cpu_clock(void)
+{
+ char buf[128];
+
+ if (cpu_clock != 0 && cpu_clock != get_cpu_clock()) {
+ snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
+ pnd_script_base, cpu_clock);
+ system(buf);
+ }
+}
+
+static void apply_filter(int which)
+{
+ static int old = -1;
+ char buf[128];
+ int i;
+
+ if (pnd_filter_list == NULL || which == old)
+ return;
+
+ for (i = 0; i < which; i++)
+ if (pnd_filter_list[i] == NULL)
+ return;
+
+ if (pnd_filter_list[i] == NULL)
+ return;
+
+ snprintf(buf, sizeof(buf), "%s/op_videofir.sh %s", pnd_script_base, pnd_filter_list[i]);
+ system(buf);
+ old = which;
+}
+
+static menu_entry e_menu_gfx_options[];
+
+static void pnd_menu_init(void)
+{
+ struct dirent *ent;
+ int i, count = 0;
+ char **mfilters;
+ char buff[64];
+ DIR *dir;
+
+ cpu_clock_st = cpu_clock = get_cpu_clock();
+
+ dir = opendir("/etc/pandora/conf/dss_fir");
+ if (dir == NULL) {
+ perror("filter opendir");
+ return;
+ }
+
+ while (1) {
+ errno = 0;
+ ent = readdir(dir);
+ if (ent == NULL) {
+ if (errno != 0)
+ perror("readdir");
+ break;
+ }
+
+ if (ent->d_type != DT_REG && ent->d_type != DT_LNK)
+ continue;
+
+ count++;
+ }
+
+ if (count == 0)
+ return;
+
+ mfilters = calloc(count + 1, sizeof(mfilters[0]));
+ if (mfilters == NULL)
+ return;
+
+ rewinddir(dir);
+ for (i = 0; (ent = readdir(dir)); ) {
+ size_t len;
+
+ if (ent->d_type != DT_REG && ent->d_type != DT_LNK)
+ continue;
+
+ len = strlen(ent->d_name);
+
+ // skip pre-HF5 extra files
+ if (len >= 3 && strcmp(ent->d_name + len - 3, "_v3") == 0)
+ continue;
+ if (len >= 3 && strcmp(ent->d_name + len - 3, "_v5") == 0)
+ continue;
+
+ // have to cut "_up_h" for pre-HF5
+ if (len > 5 && strcmp(ent->d_name + len - 5, "_up_h") == 0)
+ len -= 5;
+
+ if (len > sizeof(buff) - 1)
+ continue;
+
+ strncpy(buff, ent->d_name, len);
+ buff[len] = 0;
+ mfilters[i] = strdup(buff);
+ if (mfilters[i] != NULL)
+ i++;
+ }
+ closedir(dir);
+
+ i = me_id2offset(e_menu_gfx_options, MA_OPT_FILTERING);
+ e_menu_gfx_options[i].data = (void *)mfilters;
+ pnd_filter_list = mfilters;
+}
+
+void menu_finish(void)
+{
+ cpu_clock = cpu_clock_st;
+ apply_cpu_clock();