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 "pl_gun_ts.h"
29 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
30 #include "../libpcsxcore/psemu_plugin_defs.h"
33 int pl_frame_interval;
34 int in_type1, in_type2;
35 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
36 int in_keystate, in_state_gun;
38 static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp;
39 static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec;
40 static float vsps_cur;
41 static int vsync_usec_time;
44 static __attribute__((noinline)) int get_cpu_ticks(void)
46 static unsigned long last_utime;
48 unsigned long utime, ret;
52 fd = open("/proc/self/stat", O_RDONLY);
53 lseek(fd, 0, SEEK_SET);
55 read(fd, buf, sizeof(buf));
56 buf[sizeof(buf) - 1] = 0;
58 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
59 ret = utime - last_utime;
64 static void print_hud(void)
66 if (pl_fbdev_bpp == 16)
67 pl_text_out16(2, pl_fbdev_h - 10, "%s", hud_msg);
70 static void print_fps(void)
72 if (pl_fbdev_bpp == 16)
73 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
76 static void print_cpu_usage(void)
78 if (pl_fbdev_bpp == 16)
79 pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec);
82 // draw 192x8 status of 24 sound channels
83 static __attribute__((noinline)) void draw_active_chans(void)
85 extern void spu_get_debug_info(int *chans_out, int *fmod_chans_out, int *noise_chans_out); // hack
86 int live_chans, fmod_chans, noise_chans;
88 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
89 unsigned short *dest = (unsigned short *)pl_fbdev_buf +
90 pl_fbdev_w * (pl_fbdev_h - 10) + pl_fbdev_w / 2 - 192/2;
94 if (pl_fbdev_bpp != 16)
97 spu_get_debug_info(&live_chans, &fmod_chans, &noise_chans);
99 for (c = 0; c < 24; c++) {
101 p = !(live_chans & (1<<c)) ? 0 :
102 (fmod_chans & (1<<c)) ? 0xf000 :
103 (noise_chans & (1<<c)) ? 0x001f :
105 for (y = 0; y < 8; y++, d += pl_fbdev_w)
106 for (x = 0; x < 8; x++)
111 void *pl_fbdev_set_mode(int w, int h, int bpp)
115 if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
122 vout_fbdev_clear(layer_fb);
123 ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
125 fprintf(stderr, "failed to set mode\n");
129 menu_notify_mode_change(w, h, bpp);
134 void *pl_fbdev_flip(void)
138 if (pl_fbdev_buf != NULL) {
139 if (g_opts & OPT_SHOWSPU)
144 else if (g_opts & OPT_SHOWFPS)
147 if (g_opts & OPT_SHOWCPU)
152 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
156 int pl_fbdev_open(void)
160 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
161 omap_enable_layer(1);
163 // try to align redraws to vsync
164 vout_fbdev_wait_vsync(layer_fb);
165 gettimeofday(&now, 0);
166 vsync_usec_time = now.tv_usec;
167 while (vsync_usec_time >= pl_frame_interval)
168 vsync_usec_time -= pl_frame_interval;
173 void pl_fbdev_close(void)
175 omap_enable_layer(0);
178 void *pl_prepare_screenshot(int *w, int *h, int *bpp)
187 static void update_input(void)
189 int actions[IN_BINDTYPE_COUNT] = { 0, };
190 unsigned int emu_act;
193 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
195 emu_act = actions[IN_BINDTYPE_EMU];
196 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
198 emu_act &= ~SACTION_GUN_MASK;
201 for (; !(emu_act & 1); emu_act >>= 1, which++)
205 emu_set_action(emu_act);
207 in_keystate = actions[IN_BINDTYPE_PLAYER12];
210 extern int x11_update_keys(unsigned int *action);
211 in_keystate |= x11_update_keys(&emu_act);
212 emu_set_action(emu_act);
216 void pl_update_gun(int *xn, int *xres, int *y, int *in)
219 pl_gun_ts_update(ts, xn, y, in);
222 *y = *y * pl_fbdev_h >> 10;
225 #define MAX_LAG_FRAMES 3
227 #define tvdiff(tv, tv_old) \
228 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
229 // assumes us < 1000000
230 #define tvadd(tv, us) { \
232 if (tv.tv_usec >= 1000000) { \
233 tv.tv_usec -= 1000000; \
238 /* called on every vsync */
239 void pl_frame_limit(void)
241 static struct timeval tv_old, tv_expect;
242 static int vsync_cnt_prev;
248 /* doing input here because the pad is polled
249 * thousands of times per frame for some reason */
253 gettimeofday(&now, 0);
255 if (now.tv_sec != tv_old.tv_sec) {
256 diff = tvdiff(now, tv_old);
258 if (0 < diff && diff < 2000000)
259 vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
260 vsync_cnt_prev = vsync_cnt;
261 flips_per_sec = flip_cnt;
264 if (g_opts & OPT_SHOWCPU)
265 tick_per_sec = get_cpu_ticks();
267 if (hud_new_msg > 0) {
269 if (hud_new_msg == 0)
274 static int ya_vsync_count;
275 if (++ya_vsync_count == PCNT_FRAMES) {
276 pcnt_print(vsps_cur);
281 tvadd(tv_expect, pl_frame_interval);
282 diff = tvdiff(tv_expect, now);
283 if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) {
284 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval);
287 // try to align with vsync
288 usadj = vsync_usec_time;
289 while (usadj < tv_expect.tv_usec - pl_frame_interval)
290 usadj += pl_frame_interval;
291 tv_expect.tv_usec = usadj;
294 if (!(g_opts & OPT_NO_FRAMELIM) && diff > pl_frame_interval) {
295 // yay for working usleep on pandora!
296 //printf("usleep %d\n", diff - pl_frame_interval / 2);
297 usleep(diff - pl_frame_interval / 2);
300 if (pl_rearmed_cbs.frameskip) {
301 if (diff < -pl_frame_interval)
302 pl_rearmed_cbs.fskip_advice = 1;
304 pl_rearmed_cbs.fskip_advice = 0;
307 pcnt_start(PCNT_ALL);
310 static void pl_text_out16_(int x, int y, const char *text)
312 int i, l, len = strlen(text), w = pl_fbdev_w;
313 unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
314 unsigned short val = 0xffff;
316 for (i = 0; i < len; i++, screen += 8)
318 for (l = 0; l < 8; l++)
320 unsigned char fd = fontdata8x8[text[i] * 8 + l];
321 unsigned short *s = screen + l * w;
322 if (fd&0x80) s[0] = val;
323 if (fd&0x40) s[1] = val;
324 if (fd&0x20) s[2] = val;
325 if (fd&0x10) s[3] = val;
326 if (fd&0x08) s[4] = val;
327 if (fd&0x04) s[5] = val;
328 if (fd&0x02) s[6] = val;
329 if (fd&0x01) s[7] = val;
334 void pl_text_out16(int x, int y, const char *texto, ...)
339 va_start(args, texto);
340 vsnprintf(buffer, sizeof(buffer), texto, args);
343 pl_text_out16_(x, y, buffer);
346 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
354 struct rearmed_cbs pl_rearmed_cbs = {
363 static void *watchdog_thread(void *unused)
365 int vsync_cnt_old = 0;
370 // don't interfere with debug
382 if (vsync_cnt != vsync_cnt_old) {
383 vsync_cnt_old = vsync_cnt;
392 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
394 fprintf(stderr, "watchdog: lockup detected, aborting\n");
395 // we can't do any cleanup here really, the main thread is
396 // likely touching resources and would crash anyway
402 void pl_start_watchdog(void)
408 pthread_attr_init(&attr);
409 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
411 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
413 fprintf(stderr, "could not start watchdog: %d\n", ret);
418 ts = pl_gun_ts_init();