add redump.org SBI support
[pcsx_rearmed.git] / frontend / plugin_lib.c
1 /*
2  * (C) notaz, 2010-2011
3  *
4  * This work is licensed under the terms of the GNU GPLv2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <stdint.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <pthread.h>
19
20 #include "plugin_lib.h"
21 #include "linux/fbdev.h"
22 #include "common/fonts.h"
23 #include "common/input.h"
24 #include "omap.h"
25 #include "menu.h"
26 #include "main.h"
27 #include "pcnt.h"
28 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
29 #include "../libpcsxcore/psemu_plugin_defs.h"
30
31 void *pl_fbdev_buf;
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;
38 // P.E.Op.S.
39 extern int UseFrameSkip;
40 extern float fps_skip;
41
42 static int get_cpu_ticks(void)
43 {
44         static unsigned long last_utime;
45         static int fd;
46         unsigned long utime, ret;
47         char buf[128];
48
49         if (fd == 0)
50                 fd = open("/proc/self/stat", O_RDONLY);
51         lseek(fd, 0, SEEK_SET);
52         buf[0] = 0;
53         read(fd, buf, sizeof(buf));
54         buf[sizeof(buf) - 1] = 0;
55
56         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
57         ret = utime - last_utime;
58         last_utime = utime;
59         return ret;
60 }
61
62 static void print_hud(void)
63 {
64         if (pl_fbdev_bpp == 16)
65                 pl_text_out16(2, pl_fbdev_h - 10, "%s", hud_msg);
66 }
67
68 static void print_fps(void)
69 {
70         if (pl_fbdev_bpp == 16)
71                 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
72 }
73
74 static void print_cpu_usage(void)
75 {
76         if (pl_fbdev_bpp == 16)
77                 pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec);
78 }
79
80 void *pl_fbdev_set_mode(int w, int h, int bpp)
81 {
82         void *ret;
83
84         if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
85                 return pl_fbdev_buf;
86
87         pl_fbdev_w = w;
88         pl_fbdev_h = h;
89         pl_fbdev_bpp = bpp;
90
91         vout_fbdev_clear(layer_fb);
92         ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
93         if (ret == NULL)
94                 fprintf(stderr, "failed to set mode\n");
95         else
96                 pl_fbdev_buf = ret;
97
98         menu_notify_mode_change(w, h, bpp);
99
100         return pl_fbdev_buf;
101 }
102
103 void *pl_fbdev_flip(void)
104 {
105         flip_cnt++;
106
107         if (pl_fbdev_buf != NULL) {
108                 if (hud_msg[0] != 0)
109                         print_hud();
110                 else if (g_opts & OPT_SHOWFPS)
111                         print_fps();
112
113                 if (g_opts & OPT_SHOWCPU)
114                         print_cpu_usage();
115         }
116
117         // let's flip now
118         pl_fbdev_buf = vout_fbdev_flip(layer_fb);
119         return pl_fbdev_buf;
120 }
121
122 int pl_fbdev_open(void)
123 {
124         pl_fbdev_buf = vout_fbdev_flip(layer_fb);
125         omap_enable_layer(1);
126         return 0;
127 }
128
129 void pl_fbdev_close(void)
130 {
131         omap_enable_layer(0);
132 }
133
134 static void update_input(void)
135 {
136         int actions[IN_BINDTYPE_COUNT] = { 0, };
137         unsigned int emu_act;
138
139         in_update(actions);
140         if (in_type == PSE_PAD_TYPE_ANALOGPAD)
141                 in_update_analogs();
142         emu_act = actions[IN_BINDTYPE_EMU];
143         if (emu_act) {
144                 int which = 0;
145                 for (; !(emu_act & 1); emu_act >>= 1, which++)
146                         ;
147                 emu_act = which;
148         }
149         emu_set_action(emu_act);
150
151         in_keystate = actions[IN_BINDTYPE_PLAYER12];
152
153 #ifdef X11
154         extern int x11_update_keys(void);
155         in_keystate |= x11_update_keys();
156 #endif
157 }
158
159 #define MAX_LAG_FRAMES 3
160
161 #define tvdiff(tv, tv_old) \
162         ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
163 // assumes us < 1000000
164 #define tvadd(tv, us) { \
165         tv.tv_usec += us; \
166         if (tv.tv_usec >= 1000000) { \
167                 tv.tv_usec -= 1000000; \
168                 tv.tv_sec++; \
169         } \
170 }
171
172 /* called on every vsync */
173 void pl_frame_limit(void)
174 {
175         static struct timeval tv_old, tv_expect;
176         static int vsync_cnt_prev;
177         struct timeval now;
178         int diff;
179
180         vsync_cnt++;
181
182         /* doing input here because the pad is polled
183          * thousands of times per frame for some reason */
184         update_input();
185
186         pcnt_end(PCNT_ALL);
187         gettimeofday(&now, 0);
188
189         if (now.tv_sec != tv_old.tv_sec) {
190                 diff = tvdiff(now, tv_old);
191                 vsps_cur = 0.0f;
192                 if (0 < diff && diff < 2000000)
193                         vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
194                 vsync_cnt_prev = vsync_cnt;
195                 flips_per_sec = flip_cnt;
196                 flip_cnt = 0;
197                 tv_old = now;
198                 if (g_opts & OPT_SHOWCPU)
199                         tick_per_sec = get_cpu_ticks();
200
201                 if (hud_new_msg > 0) {
202                         hud_new_msg--;
203                         if (hud_new_msg == 0)
204                                 hud_msg[0] = 0;
205                 }
206         }
207 #ifdef PCNT
208         static int ya_vsync_count;
209         if (++ya_vsync_count == PCNT_FRAMES) {
210                 pcnt_print(vsps_cur);
211                 ya_vsync_count = 0;
212         }
213 #endif
214
215         tvadd(tv_expect, pl_frame_interval);
216         diff = tvdiff(tv_expect, now);
217         if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) {
218                 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval);
219                 tv_expect = now;
220                 diff = 0;
221         }
222
223         if (!(g_opts & OPT_NO_FRAMELIM) && diff > pl_frame_interval) {
224                 // yay for working usleep on pandora!
225                 //printf("usleep %d\n", diff - pl_frame_interval / 2);
226                 usleep(diff - pl_frame_interval / 2);
227         }
228
229         if (UseFrameSkip) {
230                 if (diff < -pl_frame_interval) {
231                         // P.E.Op.S. makes skip decision based on this
232                         fps_skip = 1.0f;
233                         plugin_skip_advice = 1;
234                 }
235                 else if (diff >= 0) {
236                         fps_skip = 100.0f;
237                         plugin_skip_advice = 0;
238                 }
239         }
240
241         pcnt_start(PCNT_ALL);
242 }
243
244 static void pl_text_out16_(int x, int y, const char *text)
245 {
246         int i, l, len = strlen(text), w = pl_fbdev_w;
247         unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
248         unsigned short val = 0xffff;
249
250         for (i = 0; i < len; i++, screen += 8)
251         {
252                 for (l = 0; l < 8; l++)
253                 {
254                         unsigned char fd = fontdata8x8[text[i] * 8 + l];
255                         unsigned short *s = screen + l * w;
256                         if (fd&0x80) s[0] = val;
257                         if (fd&0x40) s[1] = val;
258                         if (fd&0x20) s[2] = val;
259                         if (fd&0x10) s[3] = val;
260                         if (fd&0x08) s[4] = val;
261                         if (fd&0x04) s[5] = val;
262                         if (fd&0x02) s[6] = val;
263                         if (fd&0x01) s[7] = val;
264                 }
265         }
266 }
267
268 void pl_text_out16(int x, int y, const char *texto, ...)
269 {
270         va_list args;
271         char    buffer[256];
272
273         va_start(args, texto);
274         vsnprintf(buffer, sizeof(buffer), texto, args);
275         va_end(args);
276
277         pl_text_out16_(x, y, buffer);
278 }
279
280 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
281 {
282         *x = g_layer_x;
283         *y = g_layer_y;
284         *w = g_layer_w;
285         *h = g_layer_h;
286 }
287
288 const struct rearmed_cbs pl_rearmed_cbs = {
289         pl_get_layer_pos,
290         pl_fbdev_open,
291         pl_fbdev_set_mode,
292         pl_fbdev_flip,
293         pl_fbdev_close,
294         &plugin_skip_advice,
295 };
296
297 /* watchdog */
298 static void *watchdog_thread(void *unused)
299 {
300         int vsync_cnt_old = 0;
301         int seen_dead = 0;
302         int sleep_time = 5;
303
304 #ifndef NDEBUG
305         // don't interfere with debug
306         return NULL;
307 #endif
308         while (1)
309         {
310                 sleep(sleep_time);
311
312                 if (stop) {
313                         seen_dead = 0;
314                         sleep_time = 5;
315                         continue;
316                 }
317                 if (vsync_cnt != vsync_cnt_old) {
318                         vsync_cnt_old = vsync_cnt;
319                         seen_dead = 0;
320                         sleep_time = 2;
321                         continue;
322                 }
323
324                 seen_dead++;
325                 sleep_time = 1;
326                 if (seen_dead > 1)
327                         fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
328                 if (seen_dead > 4) {
329                         fprintf(stderr, "watchdog: lockup detected, aborting\n");
330                         // we can't do any cleanup here really, the main thread is
331                         // likely touching resources and would crash anyway
332                         abort();
333                 }
334         }
335 }
336
337 void pl_start_watchdog(void)
338 {
339         pthread_attr_t attr;
340         pthread_t tid;
341         int ret;
342         
343         pthread_attr_init(&attr);
344         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
345
346         ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
347         if (ret != 0)
348                 fprintf(stderr, "could not start watchdog: %d\n", ret);
349 }
350