4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
13 #include <sys/types.h>
18 #include "plugin_lib.h"
19 #include "linux/fbdev.h"
20 #include "common/fonts.h"
21 #include "common/input.h"
25 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
28 int pl_frame_interval;
30 static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp;
31 static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec;
32 static float vsps_cur;
34 static int get_cpu_ticks(void)
36 static unsigned long last_utime;
38 unsigned long utime, ret;
42 fd = open("/proc/self/stat", O_RDONLY);
43 lseek(fd, 0, SEEK_SET);
45 read(fd, buf, sizeof(buf));
46 buf[sizeof(buf) - 1] = 0;
48 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
49 ret = utime - last_utime;
54 static void print_fps(void)
56 if (pl_fbdev_bpp == 16)
57 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
60 static void print_cpu_usage(void)
62 if (pl_fbdev_bpp == 16)
63 pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec);
66 void *pl_fbdev_set_mode(int w, int h, int bpp)
70 if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
77 vout_fbdev_clear(layer_fb);
78 ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
80 fprintf(stderr, "failed to set mode\n");
84 menu_notify_mode_change(w, h, bpp);
89 void *pl_fbdev_flip(void)
93 if (pl_fbdev_buf != NULL) {
94 if (g_opts & OPT_SHOWFPS)
96 if (g_opts & OPT_SHOWCPU)
101 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
105 int pl_fbdev_open(void)
107 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
108 omap_enable_layer(1);
112 void pl_fbdev_close(void)
114 omap_enable_layer(0);
117 static void update_input(void)
119 int actions[IN_BINDTYPE_COUNT] = { 0, };
122 if (actions[IN_BINDTYPE_EMU] & PEV_MENU)
124 keystate = actions[IN_BINDTYPE_PLAYER12];
127 extern int x11_update_keys(void);
128 keystate |= x11_update_keys();
132 #define MAX_LAG_FRAMES 3
134 #define tvdiff(tv, tv_old) \
135 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
136 // assumes us < 1000000
137 #define tvadd(tv, us) { \
139 if (tv.tv_usec >= 1000000) { \
140 tv.tv_usec -= 1000000; \
145 /* called on every vsync */
146 void pl_frame_limit(void)
148 static struct timeval tv_old, tv_expect;
154 /* doing input here because the pad is polled
155 * thousands of times per frame for some reason */
159 gettimeofday(&now, 0);
161 if (now.tv_sec != tv_old.tv_sec) {
162 diff = tvdiff(now, tv_old);
164 if (0 < diff && diff < 2000000)
165 vsps_cur = 1000000.0f * vsync_cnt / diff;
166 flips_per_sec = flip_cnt;
167 vsync_cnt = flip_cnt = 0;
169 if (g_opts & OPT_SHOWCPU)
170 tick_per_sec = get_cpu_ticks();
173 static int ya_vsync_count;
174 if (++ya_vsync_count == PCNT_FRAMES) {
175 pcnt_print(vsps_cur);
180 if (!(g_opts & OPT_NO_FRAMELIM)) {
181 tvadd(tv_expect, pl_frame_interval);
182 diff = tvdiff(tv_expect, now);
183 if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) {
184 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval);
187 else if (diff > pl_frame_interval) {
188 // yay for working usleep on pandora!
189 //printf("usleep %d\n", diff - pl_frame_interval / 2);
190 usleep(diff - pl_frame_interval / 2);
194 pcnt_start(PCNT_ALL);
197 static void pl_text_out16_(int x, int y, const char *text)
199 int i, l, len = strlen(text), w = pl_fbdev_w;
200 unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
201 unsigned short val = 0xffff;
203 for (i = 0; i < len; i++, screen += 8)
205 for (l = 0; l < 8; l++)
207 unsigned char fd = fontdata8x8[text[i] * 8 + l];
208 unsigned short *s = screen + l * w;
209 if (fd&0x80) s[0] = val;
210 if (fd&0x40) s[1] = val;
211 if (fd&0x20) s[2] = val;
212 if (fd&0x10) s[3] = val;
213 if (fd&0x08) s[4] = val;
214 if (fd&0x04) s[5] = val;
215 if (fd&0x02) s[6] = val;
216 if (fd&0x01) s[7] = val;
221 void pl_text_out16(int x, int y, const char *texto, ...)
226 va_start(args, texto);
227 vsnprintf(buffer, sizeof(buffer), texto, args);
230 pl_text_out16_(x, y, buffer);
233 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
241 extern int UseFrameSkip; // hmh
243 const struct rearmed_cbs pl_rearmed_cbs = {