+ if (blocking)
+ sceCtrlReadBufferPositive(&pad, 1);
+ else sceCtrlPeekBufferPositive(&pad, 1);
+ buttons = pad.Buttons;
+
+ // analog..
+ buttons &= ~(BTN_NUB_UP|BTN_NUB_DOWN|BTN_NUB_LEFT|BTN_NUB_RIGHT);
+ if (pad.Lx < 128 - ANALOG_DEADZONE) buttons |= BTN_NUB_LEFT;
+ if (pad.Lx > 128 + ANALOG_DEADZONE) buttons |= BTN_NUB_RIGHT;
+ if (pad.Ly < 128 - ANALOG_DEADZONE) buttons |= BTN_NUB_UP;
+ if (pad.Ly > 128 + ANALOG_DEADZONE) buttons |= BTN_NUB_DOWN;
+
+ return buttons;
+}
+
+int psp_get_cpu_clock(void)
+{
+ return scePowerGetCpuClockFrequencyInt();
+}
+
+int psp_set_cpu_clock(int clock)
+{
+ int ret = scePowerSetClockFrequency(clock, clock, clock/2);
+ if (ret != 0) lprintf("failed to set clock: %i\n", ret);
+
+ return ret;
+}
+
+char *psp_get_status_line(void)
+{
+ static char buff[64];
+ int ret, bat_percent, bat_time;
+ pspTime time;
+
+ ret = sceRtcGetCurrentClockLocalTime(&time);
+ bat_percent = scePowerGetBatteryLifePercent();
+ bat_time = scePowerGetBatteryLifeTime();
+ if (ret < 0 || bat_percent < 0 || bat_time < 0) return NULL;
+
+ snprintf(buff, sizeof(buff), "%02i:%02i bat: %3i%%", time.hour, time.minutes, bat_percent);
+ if (!scePowerIsPowerOnline())
+ snprintf(buff+strlen(buff), sizeof(buff)-strlen(buff), " (%i:%02i)", bat_time/60, bat_time%60);
+ return buff;
+}
+
+void psp_wait_suspend(void)
+{
+ // probably should do something smarter here?
+ sceDisplayWaitVblankStart();
+}
+
+void psp_resume_suspend(void)
+{
+ // for some reason file IO doesn't seem to work
+ // after resume for some period of time, at least on 1.5
+ SceUID fd;
+ int i;
+ for (i = 0; i < 30; i++) {
+ fd = sceIoOpen("dummy.txt", PSP_O_WRONLY|PSP_O_APPEND, 0777);
+ if (fd != 0x80010013) break; // device not available
+ sceKernelDelayThread(32 * 1024);
+ }
+ if (fd >= 0) sceIoClose(fd);
+ sceDisplayWaitVblankStart();
+ psp_unhandled_suspend = 0;
+ if (i < 30)
+ lprintf("io resumed after %i tries\n", i);
+ else lprintf("io resume failed\n");
+}
+
+/* alt logging */
+#define LOG_FILE "log.txt"
+
+#ifndef LPRINTF_STDIO
+typedef struct _log_entry
+{
+ char buff[256];
+ struct _log_entry *next;
+} log_entry;
+
+static log_entry *le_root = NULL;
+#endif
+
+void lprintf(const char *fmt, ...)
+{
+ va_list vl;
+
+#ifdef LPRINTF_STDIO
+ va_start(vl, fmt);
+ vprintf(fmt, vl);
+ va_end(vl);
+#else
+ static SceUID logfd = -1;
+ static int msg_count = 0;
+ char buff[256];
+ log_entry *le, *le1;
+
+ if (logfd == -2) return; // disabled
+
+ va_start(vl, fmt);
+ vsnprintf(buff, sizeof(buff), fmt, vl);
+ va_end(vl);