4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
14 #include <sys/types.h>
20 #include "plugin_lib.h"
21 #include "linux/fbdev.h"
22 #include "common/fonts.h"
23 #include "common/input.h"
28 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
29 #include "../libpcsxcore/psemu_plugin_defs.h"
32 int pl_frame_interval;
33 int in_type, in_keystate, in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
34 static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp;
35 static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec;
36 static float vsps_cur;
37 static int plugin_skip_advice;
39 extern int UseFrameSkip;
40 extern float fps_skip;
42 static int get_cpu_ticks(void)
44 static unsigned long last_utime;
46 unsigned long utime, ret;
50 fd = open("/proc/self/stat", O_RDONLY);
51 lseek(fd, 0, SEEK_SET);
53 read(fd, buf, sizeof(buf));
54 buf[sizeof(buf) - 1] = 0;
56 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
57 ret = utime - last_utime;
62 static void print_hud(void)
64 if (pl_fbdev_bpp == 16)
65 pl_text_out16(2, pl_fbdev_h - 10, "%s", hud_msg);
68 static void print_fps(void)
70 if (pl_fbdev_bpp == 16)
71 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
74 static void print_cpu_usage(void)
76 if (pl_fbdev_bpp == 16)
77 pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec);
80 void *pl_fbdev_set_mode(int w, int h, int bpp)
84 if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
91 vout_fbdev_clear(layer_fb);
92 ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
94 fprintf(stderr, "failed to set mode\n");
98 menu_notify_mode_change(w, h, bpp);
103 void *pl_fbdev_flip(void)
107 if (pl_fbdev_buf != NULL) {
110 else if (g_opts & OPT_SHOWFPS)
113 if (g_opts & OPT_SHOWCPU)
118 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
122 int pl_fbdev_open(void)
124 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
125 omap_enable_layer(1);
129 void pl_fbdev_close(void)
131 omap_enable_layer(0);
134 void *pl_prepare_screenshot(int *w, int *h, int *bpp)
143 static void update_input(void)
145 int actions[IN_BINDTYPE_COUNT] = { 0, };
146 unsigned int emu_act;
149 if (in_type == PSE_PAD_TYPE_ANALOGPAD)
151 emu_act = actions[IN_BINDTYPE_EMU];
154 for (; !(emu_act & 1); emu_act >>= 1, which++)
158 emu_set_action(emu_act);
160 in_keystate = actions[IN_BINDTYPE_PLAYER12];
163 extern int x11_update_keys(void);
164 in_keystate |= x11_update_keys();
168 #define MAX_LAG_FRAMES 3
170 #define tvdiff(tv, tv_old) \
171 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
172 // assumes us < 1000000
173 #define tvadd(tv, us) { \
175 if (tv.tv_usec >= 1000000) { \
176 tv.tv_usec -= 1000000; \
181 /* called on every vsync */
182 void pl_frame_limit(void)
184 static struct timeval tv_old, tv_expect;
185 static int vsync_cnt_prev;
191 /* doing input here because the pad is polled
192 * thousands of times per frame for some reason */
196 gettimeofday(&now, 0);
198 if (now.tv_sec != tv_old.tv_sec) {
199 diff = tvdiff(now, tv_old);
201 if (0 < diff && diff < 2000000)
202 vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
203 vsync_cnt_prev = vsync_cnt;
204 flips_per_sec = flip_cnt;
207 if (g_opts & OPT_SHOWCPU)
208 tick_per_sec = get_cpu_ticks();
210 if (hud_new_msg > 0) {
212 if (hud_new_msg == 0)
217 static int ya_vsync_count;
218 if (++ya_vsync_count == PCNT_FRAMES) {
219 pcnt_print(vsps_cur);
224 tvadd(tv_expect, pl_frame_interval);
225 diff = tvdiff(tv_expect, now);
226 if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) {
227 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval);
232 if (!(g_opts & OPT_NO_FRAMELIM) && diff > pl_frame_interval) {
233 // yay for working usleep on pandora!
234 //printf("usleep %d\n", diff - pl_frame_interval / 2);
235 usleep(diff - pl_frame_interval / 2);
239 if (diff < -pl_frame_interval) {
240 // P.E.Op.S. makes skip decision based on this
242 plugin_skip_advice = 1;
244 else if (diff >= 0) {
246 plugin_skip_advice = 0;
250 pcnt_start(PCNT_ALL);
253 static void pl_text_out16_(int x, int y, const char *text)
255 int i, l, len = strlen(text), w = pl_fbdev_w;
256 unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
257 unsigned short val = 0xffff;
259 for (i = 0; i < len; i++, screen += 8)
261 for (l = 0; l < 8; l++)
263 unsigned char fd = fontdata8x8[text[i] * 8 + l];
264 unsigned short *s = screen + l * w;
265 if (fd&0x80) s[0] = val;
266 if (fd&0x40) s[1] = val;
267 if (fd&0x20) s[2] = val;
268 if (fd&0x10) s[3] = val;
269 if (fd&0x08) s[4] = val;
270 if (fd&0x04) s[5] = val;
271 if (fd&0x02) s[6] = val;
272 if (fd&0x01) s[7] = val;
277 void pl_text_out16(int x, int y, const char *texto, ...)
282 va_start(args, texto);
283 vsnprintf(buffer, sizeof(buffer), texto, args);
286 pl_text_out16_(x, y, buffer);
289 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
297 const struct rearmed_cbs pl_rearmed_cbs = {
307 static void *watchdog_thread(void *unused)
309 int vsync_cnt_old = 0;
314 // don't interfere with debug
326 if (vsync_cnt != vsync_cnt_old) {
327 vsync_cnt_old = vsync_cnt;
336 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
338 fprintf(stderr, "watchdog: lockup detected, aborting\n");
339 // we can't do any cleanup here really, the main thread is
340 // likely touching resources and would crash anyway
346 void pl_start_watchdog(void)
352 pthread_attr_init(&attr);
353 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
355 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
357 fprintf(stderr, "could not start watchdog: %d\n", ret);