relicense common/menu.* to suit this project
[pcsx_rearmed.git] / frontend / plugin_lib.c
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>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <sys/time.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17
18 #include "plugin_lib.h"
19 #include "linux/fbdev.h"
20 #include "common/fonts.h"
21 #include "common/input.h"
22 #include "omap.h"
23 #include "menu.h"
24 #include "pcnt.h"
25 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
26
27 void *pl_fbdev_buf;
28 int keystate;
29 static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp;
30 static int flip_cnt, flips_per_sec, tick_per_sec;
31 extern float fps_cur; // XXX
32
33 static 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
53 static 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
59 static 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 }
64
65 void *pl_fbdev_set_mode(int w, int h, int bpp)
66 {
67         void *ret;
68
69         if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
70                 return 0;
71
72         pl_fbdev_w = w;
73         pl_fbdev_h = h;
74         pl_fbdev_bpp = bpp;
75
76         vout_fbdev_clear(layer_fb);
77         ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
78         if (ret == NULL)
79                 fprintf(stderr, "failed to set mode\n");
80         else
81                 pl_fbdev_buf = ret;
82
83         menu_notify_mode_change(w, h, bpp);
84
85         return pl_fbdev_buf;
86 }
87
88 void *pl_fbdev_flip(void)
89 {
90         flip_cnt++;
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         }
98
99         // let's flip now
100         pl_fbdev_buf = vout_fbdev_flip(layer_fb);
101         return pl_fbdev_buf;
102 }
103
104 int pl_fbdev_open(void)
105 {
106         pl_fbdev_buf = vout_fbdev_flip(layer_fb);
107         omap_enable_layer(1);
108         return 0;
109 }
110
111 void pl_fbdev_close(void)
112 {
113         omap_enable_layer(0);
114 }
115
116 static 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];
124 }
125
126 /* called on every vsync */
127 void pl_frame_limit(void)
128 {
129         extern void CheckFrameRate(void);
130         static int oldsec;
131         struct timeval tv;
132
133         /* doing input here because the pad is polled
134          * thousands of times per frame for some reason */
135         update_input();
136
137         pcnt_end(PCNT_ALL);
138         gettimeofday(&tv, 0);
139
140         if (tv.tv_sec != oldsec) {
141                 flips_per_sec = flip_cnt;
142                 flip_cnt = 0;
143                 oldsec = tv.tv_sec;
144                 if (g_opts & OPT_SHOWCPU)
145                         tick_per_sec = get_cpu_ticks();
146         }
147 #ifdef PCNT
148         static int ya_vsync_count;
149         if (++ya_vsync_count == PCNT_FRAMES) {
150                 pcnt_print(fps_cur);
151                 ya_vsync_count = 0;
152         }
153 #endif
154
155         CheckFrameRate();
156
157         pcnt_start(PCNT_ALL);
158 }
159
160 static void pl_text_out16_(int x, int y, const char *text)
161 {
162         int i, l, len = strlen(text), w = pl_fbdev_w;
163         unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
164         unsigned short val = 0xffff;
165
166         for (i = 0; i < len; i++, screen += 8)
167         {
168                 for (l = 0; l < 8; l++)
169                 {
170                         unsigned char fd = fontdata8x8[text[i] * 8 + l];
171                         unsigned short *s = screen + l * w;
172                         if (fd&0x80) s[0] = val;
173                         if (fd&0x40) s[1] = val;
174                         if (fd&0x20) s[2] = val;
175                         if (fd&0x10) s[3] = val;
176                         if (fd&0x08) s[4] = val;
177                         if (fd&0x04) s[5] = val;
178                         if (fd&0x02) s[6] = val;
179                         if (fd&0x01) s[7] = val;
180                 }
181         }
182 }
183
184 void pl_text_out16(int x, int y, const char *texto, ...)
185 {
186         va_list args;
187         char    buffer[256];
188
189         va_start(args, texto);
190         vsnprintf(buffer, sizeof(buffer), texto, args);
191         va_end(args);
192
193         pl_text_out16_(x, y, buffer);
194 }
195
196 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
197 {
198         *x = g_layer_x;
199         *y = g_layer_y;
200         *w = g_layer_w;
201         *h = g_layer_h;
202 }
203
204 extern int UseFrameSkip; // hmh
205
206 const struct rearmed_cbs pl_rearmed_cbs = {
207         pl_get_layer_pos,
208         pl_fbdev_open,
209         pl_fbdev_set_mode,
210         pl_fbdev_flip,
211         pl_fbdev_close,
212         &UseFrameSkip,
213 };
214