bugfix
[pcsx_rearmed.git] / frontend / plugin_lib.c
CommitLineData
b60f2812 1/*
2 * (C) notaz, 2010
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>
69af03a2 10#include <string.h>
11#include <stdarg.h>
72228559 12#include <sys/time.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <fcntl.h>
16#include <unistd.h>
b60f2812 17
72228559 18#include "plugin_lib.h"
b60f2812 19#include "linux/fbdev.h"
69af03a2 20#include "common/fonts.h"
21#include "common/input.h"
22#include "omap.h"
3c70c47b 23#include "menu.h"
72228559 24#include "pcnt.h"
69af03a2 25#include "../libpcsxcore/new_dynarec/new_dynarec.h"
b60f2812 26
b60f2812 27void *pl_fbdev_buf;
69af03a2 28int keystate;
72228559 29static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp;
30static int flip_cnt, flips_per_sec, tick_per_sec;
31extern float fps_cur; // XXX
32
33static int get_cpu_ticks(void)
34{
35 static unsigned long last_utime;
36 static int fd;
37 unsigned long utime, ret;
38 char buf[128];
39
40 if (fd == 0)
41 fd = open("/proc/self/stat", O_RDONLY);
42 lseek(fd, 0, SEEK_SET);
43 buf[0] = 0;
44 read(fd, buf, sizeof(buf));
45 buf[sizeof(buf) - 1] = 0;
46
47 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
48 ret = utime - last_utime;
49 last_utime = utime;
50 return ret;
51}
52
53static void print_fps(void)
54{
55 if (pl_fbdev_bpp == 16)
56 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, fps_cur);
57}
58
59static void print_cpu_usage(void)
60{
61 if (pl_fbdev_bpp == 16)
62 pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec);
63}
b60f2812 64
297b3d63 65void *pl_fbdev_set_mode(int w, int h, int bpp)
69af03a2 66{
d352cde2 67 void *ret;
b60f2812 68
72228559 69 if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
7947ab55 70 return pl_fbdev_buf;
72228559 71
69af03a2 72 pl_fbdev_w = w;
72228559 73 pl_fbdev_h = h;
74 pl_fbdev_bpp = bpp;
d352cde2 75
76 vout_fbdev_clear(layer_fb);
69af03a2 77 ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
d352cde2 78 if (ret == NULL)
69af03a2 79 fprintf(stderr, "failed to set mode\n");
d352cde2 80 else
81 pl_fbdev_buf = ret;
82
bd6267e6 83 menu_notify_mode_change(w, h, bpp);
3c70c47b 84
297b3d63 85 return pl_fbdev_buf;
69af03a2 86}
87
ffd0d743 88void *pl_fbdev_flip(void)
69af03a2 89{
72228559 90 flip_cnt++;
297b3d63 91
92 if (pl_fbdev_buf != NULL) {
93 if (g_opts & OPT_SHOWFPS)
94 print_fps();
95 if (g_opts & OPT_SHOWCPU)
96 print_cpu_usage();
97 }
72228559 98
69af03a2 99 // let's flip now
100 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
ffd0d743 101 return pl_fbdev_buf;
b60f2812 102}
103
6d1a1ac2 104int pl_fbdev_open(void)
105{
106 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
107 omap_enable_layer(1);
108 return 0;
109}
110
111void pl_fbdev_close(void)
b60f2812 112{
6d1a1ac2 113 omap_enable_layer(0);
b60f2812 114}
115
15d46930 116static void update_input(void)
117{
118 int actions[IN_BINDTYPE_COUNT] = { 0, };
119
120 in_update(actions);
121 if (actions[IN_BINDTYPE_EMU] & PEV_MENU)
122 stop = 1;
123 keystate = actions[IN_BINDTYPE_PLAYER12];
447783f8 124
125#ifdef X11
126 extern void x11_update_keys(void);
127 x11_update_keys();
128#endif
15d46930 129}
130
72228559 131/* called on every vsync */
132void pl_frame_limit(void)
133{
134 extern void CheckFrameRate(void);
135 static int oldsec;
136 struct timeval tv;
137
15d46930 138 /* doing input here because the pad is polled
139 * thousands of times per frame for some reason */
140 update_input();
141
72228559 142 pcnt_end(PCNT_ALL);
143 gettimeofday(&tv, 0);
144
145 if (tv.tv_sec != oldsec) {
146 flips_per_sec = flip_cnt;
147 flip_cnt = 0;
72228559 148 oldsec = tv.tv_sec;
bd6267e6 149 if (g_opts & OPT_SHOWCPU)
150 tick_per_sec = get_cpu_ticks();
72228559 151 }
152#ifdef PCNT
153 static int ya_vsync_count;
154 if (++ya_vsync_count == PCNT_FRAMES) {
155 pcnt_print(fps_cur);
156 ya_vsync_count = 0;
157 }
158#endif
159
160 CheckFrameRate();
161
162 pcnt_start(PCNT_ALL);
163}
164
69af03a2 165static void pl_text_out16_(int x, int y, const char *text)
b60f2812 166{
69af03a2 167 int i, l, len = strlen(text), w = pl_fbdev_w;
168 unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
169 unsigned short val = 0xffff;
170
171 for (i = 0; i < len; i++, screen += 8)
172 {
173 for (l = 0; l < 8; l++)
174 {
175 unsigned char fd = fontdata8x8[text[i] * 8 + l];
176 unsigned short *s = screen + l * w;
177 if (fd&0x80) s[0] = val;
178 if (fd&0x40) s[1] = val;
179 if (fd&0x20) s[2] = val;
180 if (fd&0x10) s[3] = val;
181 if (fd&0x08) s[4] = val;
182 if (fd&0x04) s[5] = val;
183 if (fd&0x02) s[6] = val;
184 if (fd&0x01) s[7] = val;
185 }
186 }
b60f2812 187}
188
69af03a2 189void pl_text_out16(int x, int y, const char *texto, ...)
b60f2812 190{
69af03a2 191 va_list args;
192 char buffer[256];
193
194 va_start(args, texto);
195 vsnprintf(buffer, sizeof(buffer), texto, args);
196 va_end(args);
197
198 pl_text_out16_(x, y, buffer);
b60f2812 199}
200
201c21e2 201static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
202{
203 *x = g_layer_x;
204 *y = g_layer_y;
205 *w = g_layer_w;
206 *h = g_layer_h;
207}
208
78d78c3b 209extern int UseFrameSkip; // hmh
210
201c21e2 211const struct rearmed_cbs pl_rearmed_cbs = {
212 pl_get_layer_pos,
ffd0d743 213 pl_fbdev_open,
214 pl_fbdev_set_mode,
215 pl_fbdev_flip,
216 pl_fbdev_close,
78d78c3b 217 &UseFrameSkip,
201c21e2 218};
219