+ emu_action_old = emu_action;
+
+ switch (emu_action) {
+ case SACTION_LOAD_STATE:
+ ret = emu_load_state(state_slot);
+ snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
+ break;
+ case SACTION_SAVE_STATE:
+ ret = emu_save_state(state_slot);
+ snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
+ break;
+#ifndef NO_FRONTEND
+ case SACTION_ENTER_MENU:
+ toggle_fast_forward(1);
+ menu_loop();
+ return;
+ case SACTION_NEXT_SSLOT:
+ state_slot++;
+ if (state_slot > 9)
+ state_slot = 0;
+ goto do_state_slot;
+ case SACTION_PREV_SSLOT:
+ state_slot--;
+ if (state_slot < 0)
+ state_slot = 9;
+do_state_slot:
+ snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
+ emu_check_state(state_slot) == 0 ? "USED" : "FREE");
+ hud_new_msg = 3;
+ printf("* %s\n", hud_msg);
+ break;
+ case SACTION_TOGGLE_FSKIP:
+ pl_rearmed_cbs.fskip_advice = 0;
+ pl_rearmed_cbs.frameskip++;
+ if (pl_rearmed_cbs.frameskip > 1)
+ pl_rearmed_cbs.frameskip = -1;
+ snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
+ pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
+ pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
+ plugin_call_rearmed_cbs();
+ break;
+ case SACTION_SWITCH_DISPMODE:
+ pl_switch_dispmode();
+ plugin_call_rearmed_cbs();
+ if (GPU_open != NULL && GPU_close != NULL) {
+ GPU_close();
+ GPU_open(&gpuDisp, "PCSX", NULL);
+ }
+ break;
+ case SACTION_FAST_FORWARD:
+ toggle_fast_forward(0);
+ plugin_call_rearmed_cbs();
+ break;
+ case SACTION_SCREENSHOT:
+ {
+ char buf[MAXPATHLEN];
+ void *scrbuf;
+ int w, h, bpp;
+ time_t t = time(NULL);
+ struct tm *tb = localtime(&t);
+ int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
+ tb->tm_min * 100 + tb->tm_sec;
+
+ scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
+ get_gameid_filename(buf, sizeof(buf),
+ "screenshots/%.32s-%.9s.%d.png", ti);
+ ret = -1;
+ if (scrbuf != 0 && bpp == 16)
+ ret = writepng(buf, scrbuf, w, h);
+ if (ret == 0)
+ snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
+ break;
+ }
+ case SACTION_VOLUME_UP:
+ case SACTION_VOLUME_DOWN:
+ plat_target_step_volume(emu_action == SACTION_VOLUME_UP);
+ return;
+ case SACTION_MINIMIZE:
+ if (GPU_close != NULL)
+ GPU_close();
+
+ plat_minimize();
+
+ if (GPU_open != NULL) {
+ ret = GPU_open(&gpuDisp, "PCSX", NULL);
+ if (ret)
+ fprintf(stderr, "GPU_open returned %d\n", ret);
+ }
+ return;
+#endif
+ default:
+ return;
+ }
+
+ hud_new_msg = 3;
+}
+
+static int cdidcmp(const char *id1, const char *id2)
+{
+ while (*id1 != 0 && *id2 != 0) {
+ if (*id1 == '_') { id1++; continue; }
+ if (*id2 == '_') { id2++; continue; }
+ if (*id1 != *id2)
+ break;
+ id1++;
+ id2++;
+ }
+
+ return *id1 - *id2;
+}
+
+static void parse_cwcheat(void)
+{
+ char line[256], buf[64], name[64], *p;
+ int newcheat = 1;
+ u32 a, v;
+ FILE *f;
+
+ f = fopen("cheatpops.db", "r");
+ if (f == NULL)
+ return;
+
+ /* find the game */
+ while (fgets(line, sizeof(line), f)) {
+ if (sscanf(line, "_S %63s", buf) != 1)
+ continue;
+ if (cdidcmp(buf, CdromId) == 0)
+ break;
+ }
+
+ if (feof(f))
+ goto out;
+
+ printf("cwcheat section found for %s\n", CdromId);
+ while (fgets(line, sizeof(line), f))
+ {
+ p = line + strlen(line);
+ for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
+ *p = 0;
+ if (*p == 0 || *p == '#' || *p == ';')
+ continue;
+
+ if (strncmp(line, "_S", 2) == 0)
+ break;
+ if (strncmp(line, "_G", 2) == 0) {
+ printf(" cwcheat game name: '%s'\n", line + 3);
+ continue;
+ }
+ if (strncmp(line, "_C0", 3) == 0) {
+ if (!newcheat && Cheats[NumCheats - 1].n == 0) {
+ printf("cheat '%s' failed to parse\n", name);
+ free(Cheats[NumCheats - 1].Descr);
+ NumCheats--;
+ }
+ snprintf(name, sizeof(name), "%s", line + 4);
+ newcheat = 1;
+ continue;
+ }
+ if (sscanf(line, "_L %x %x", &a, &v) != 2) {
+ printf("line failed to parse: '%s'\n", line);
+ continue;
+ }
+
+ if (newcheat) {
+ if (NumCheats >= NumCheatsAllocated) {
+ NumCheatsAllocated += 16;
+ Cheats = realloc(Cheats, sizeof(Cheat) *
+ NumCheatsAllocated);
+ if (Cheats == NULL)
+ break;
+ }
+ Cheats[NumCheats].Descr = strdup(name);
+ Cheats[NumCheats].Enabled = 0;
+ Cheats[NumCheats].WasEnabled = 0;
+ Cheats[NumCheats].First = NumCodes;
+ Cheats[NumCheats].n = 0;
+ NumCheats++;
+ newcheat = 0;
+ }
+
+ if (NumCodes >= NumCodesAllocated) {
+ NumCodesAllocated += 16;
+ CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
+ NumCodesAllocated);
+ if (CheatCodes == NULL)
+ break;
+ }
+ CheatCodes[NumCodes].Addr = a;
+ CheatCodes[NumCodes].Val = v;
+ NumCodes++;
+ Cheats[NumCheats - 1].n++;
+ }
+
+out:
+ fclose(f);
+}
+
+void emu_on_new_cd(void)
+{
+ ClearAllCheats();
+ parse_cwcheat();
+
+ if (Config.HLE) {
+ printf("note: running with HLE BIOS, expect compatibility problems\n");
+ printf("----------------------------------------------------------\n");
+ }
+
+ snprintf(hud_msg, sizeof(hud_msg), BOOT_MSG);
+ hud_new_msg = 3;
+}
+
+int emu_core_preinit(void)
+{