various enhancement tweaks
[pcsx_rearmed.git] / frontend / plugin_lib.c
CommitLineData
b60f2812 1/*
8f892648 2 * (C) notaz, 2010-2011
b60f2812 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>
799b0b87 12#include <stdint.h>
72228559 13#include <sys/time.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <fcntl.h>
17#include <unistd.h>
7c0f51de 18#include <pthread.h>
b60f2812 19
72228559 20#include "plugin_lib.h"
b60f2812 21#include "linux/fbdev.h"
69af03a2 22#include "common/fonts.h"
23#include "common/input.h"
9ee0fd5b 24#include "common/plat.h"
3c70c47b 25#include "menu.h"
8f892648 26#include "main.h"
47821672 27#include "plat.h"
72228559 28#include "pcnt.h"
4c08b9e7 29#include "pl_gun_ts.h"
69af03a2 30#include "../libpcsxcore/new_dynarec/new_dynarec.h"
799b0b87 31#include "../libpcsxcore/psemu_plugin_defs.h"
b60f2812 32
4c08b9e7 33int in_type1, in_type2;
34int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
1b0c5139 35int in_adev[2] = { -1, -1 }, in_adev_axis[2][2] = {{ 0, 1 }, { 0, 1 }};
4c08b9e7 36int in_keystate, in_state_gun;
b944a30e 37int in_enable_vibration;
faf2b2aa 38void *tsdev;
76f7048e 39void *pl_vout_buf;
6469a8c4 40int g_layer_x, g_layer_y, g_layer_w, g_layer_h;
ddc0a02a 41static int pl_vout_w, pl_vout_h, pl_vout_bpp; /* output display/layer */
42static int psx_w, psx_h, psx_bpp;
a92f6af1 43static int vsync_cnt;
ab423939 44static int is_pal, frame_interval, frame_interval1024;
45static int vsync_usec_time;
72228559 46
4c08b9e7 47
48static __attribute__((noinline)) int get_cpu_ticks(void)
72228559 49{
50 static unsigned long last_utime;
51 static int fd;
52 unsigned long utime, ret;
53 char buf[128];
54
55 if (fd == 0)
56 fd = open("/proc/self/stat", O_RDONLY);
57 lseek(fd, 0, SEEK_SET);
58 buf[0] = 0;
59 read(fd, buf, sizeof(buf));
60 buf[sizeof(buf) - 1] = 0;
61
62 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
63 ret = utime - last_utime;
64 last_utime = utime;
65 return ret;
66}
67
41f55c9f 68static void print_msg(int h, int border)
8f892648 69{
76f7048e 70 if (pl_vout_bpp == 16)
41f55c9f 71 pl_text_out16(border + 2, h - 10, "%s", hud_msg);
8f892648 72}
73
41f55c9f 74static void print_fps(int h, int border)
72228559 75{
76f7048e 76 if (pl_vout_bpp == 16)
a92f6af1 77 pl_text_out16(border + 2, h - 10, "%2d %4.1f",
78 pl_rearmed_cbs.flips_per_sec, pl_rearmed_cbs.vsps_cur);
72228559 79}
80
41f55c9f 81static void print_cpu_usage(int w, int h, int border)
72228559 82{
76f7048e 83 if (pl_vout_bpp == 16)
a92f6af1 84 pl_text_out16(w - border - 28, h - 10, "%3d", pl_rearmed_cbs.cpu_usage);
72228559 85}
b60f2812 86
90f1d26c 87// draw 192x8 status of 24 sound channels
55b0eeea 88static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h)
90f1d26c 89{
174c454a 90 extern void spu_get_debug_info(int *chans_out, int *run_chans,
91 int *fmod_chans_out, int *noise_chans_out); // hack
92 int live_chans, run_chans, fmod_chans, noise_chans;
90f1d26c 93
94 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
76f7048e 95 unsigned short *dest = (unsigned short *)pl_vout_buf +
55b0eeea 96 vout_w * (vout_h - 10) + vout_w / 2 - 192/2;
90f1d26c 97 unsigned short *d, p;
98 int c, x, y;
99
76f7048e 100 if (pl_vout_bpp != 16)
90f1d26c 101 return;
102
174c454a 103 spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
90f1d26c 104
105 for (c = 0; c < 24; c++) {
106 d = dest + c * 8;
174c454a 107 p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
90f1d26c 108 (fmod_chans & (1<<c)) ? 0xf000 :
109 (noise_chans & (1<<c)) ? 0x001f :
110 colors[c & 1];
55b0eeea 111 for (y = 0; y < 8; y++, d += vout_w)
90f1d26c 112 for (x = 0; x < 8; x++)
113 d[x] = p;
114 }
115}
116
6469a8c4 117void pl_print_hud(int xborder)
55b0eeea 118{
6469a8c4 119 int w = pl_vout_w, h = pl_vout_h;
55b0eeea 120
aafcb4dd 121 if (h < 16)
122 return;
123
55b0eeea 124 if (g_opts & OPT_SHOWSPU)
125 draw_active_chans(w, h);
126
127 if (hud_msg[0] != 0)
41f55c9f 128 print_msg(h, xborder);
55b0eeea 129 else if (g_opts & OPT_SHOWFPS)
41f55c9f 130 print_fps(h, xborder);
55b0eeea 131
132 if (g_opts & OPT_SHOWCPU)
41f55c9f 133 print_cpu_usage(w, h, xborder);
55b0eeea 134}
135
9e5ac38f 136/* update scaler target size according to user settings */
137static void update_layer_size(int w, int h)
138{
139 float mult;
140 int imult;
141
142 switch (g_scaler) {
143 case SCALE_1_1:
144 g_layer_w = w; g_layer_h = h;
145 break;
146
147 case SCALE_4_3v2:
148 if (h > g_menuscreen_h || (240 < h && h <= 360))
149 goto fractional_4_3;
150
151 // 4:3 that prefers integer scaling
152 imult = g_menuscreen_h / h;
153 g_layer_w = w * imult;
154 g_layer_h = h * imult;
155 mult = (float)g_layer_w / (float)g_layer_h;
156 if (mult < 1.25f || mult > 1.666f)
157 g_layer_w = 4.0f/3.0f * (float)g_layer_h;
158 printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
159 break;
160
161 fractional_4_3:
162 case SCALE_4_3:
163 mult = 240.0f / (float)h * 4.0f / 3.0f;
164 if (h > 256)
165 mult *= 2.0f;
166 g_layer_w = mult * (float)g_menuscreen_h;
167 g_layer_h = g_menuscreen_h;
168 printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
169 break;
170
171 case SCALE_FULLSCREEN:
172 g_layer_w = g_menuscreen_w;
173 g_layer_h = g_menuscreen_h;
174 break;
175
176 default:
177 break;
178 }
179
180 g_layer_x = g_menuscreen_w / 2 - g_layer_w / 2;
181 g_layer_y = g_menuscreen_h / 2 - g_layer_h / 2;
182 if (g_layer_x < 0) g_layer_x = 0;
183 if (g_layer_y < 0) g_layer_y = 0;
184 if (g_layer_w > g_menuscreen_w) g_layer_w = g_menuscreen_w;
185 if (g_layer_h > g_menuscreen_h) g_layer_h = g_menuscreen_h;
186}
187
76f7048e 188static void *pl_vout_set_mode(int w, int h, int bpp)
69af03a2 189{
a185be70 190 // special h handling, Wipeout likes to change it by 1-6
321ca84d 191 static int vsync_cnt_ms_prev;
192 if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60)
193 h = (h + 7) & ~7;
194 vsync_cnt_ms_prev = vsync_cnt;
a185be70 195
ddc0a02a 196 if (w == psx_w && h == psx_h && bpp == psx_bpp)
76f7048e 197 return pl_vout_buf;
b60f2812 198
ddc0a02a 199 pl_vout_w = psx_w = w;
200 pl_vout_h = psx_h = h;
201 pl_vout_bpp = psx_bpp = bpp;
d352cde2 202
9e5ac38f 203 update_layer_size(pl_vout_w, pl_vout_h);
204
6469a8c4 205 pl_vout_buf = plat_gvideo_set_mode(&pl_vout_w, &pl_vout_h, &pl_vout_bpp);
ddc0a02a 206 if (pl_vout_buf == NULL && pl_rearmed_cbs.pl_vout_raw_flip == NULL)
207 fprintf(stderr, "failed to set mode %dx%d@%d\n",
208 psx_w, psx_h, psx_bpp);
a185be70 209
ddc0a02a 210 menu_notify_mode_change(pl_vout_w, pl_vout_h, pl_vout_bpp);
3c70c47b 211
76f7048e 212 return pl_vout_buf;
69af03a2 213}
214
6469a8c4 215// only used if raw flip is not defined
76f7048e 216static void *pl_vout_flip(void)
69af03a2 217{
a92f6af1 218 pl_rearmed_cbs.flip_cnt++;
8f892648 219
55b0eeea 220 if (pl_vout_buf != NULL)
6469a8c4 221 pl_print_hud(0);
72228559 222
69af03a2 223 // let's flip now
6469a8c4 224 pl_vout_buf = plat_gvideo_flip();
76f7048e 225 return pl_vout_buf;
b60f2812 226}
227
76f7048e 228static int pl_vout_open(void)
6d1a1ac2 229{
cefe86b7 230 struct timeval now;
6469a8c4 231 int h;
cefe86b7 232
321ca84d 233 // force mode update
ddc0a02a 234 h = psx_h;
235 psx_h--;
236 pl_vout_buf = pl_vout_set_mode(psx_w, h, psx_bpp);
cefe86b7 237
ab423939 238 plat_gvideo_open(is_pal);
76f7048e 239
cefe86b7 240 gettimeofday(&now, 0);
241 vsync_usec_time = now.tv_usec;
76f7048e 242 while (vsync_usec_time >= frame_interval)
243 vsync_usec_time -= frame_interval;
cefe86b7 244
6d1a1ac2 245 return 0;
246}
247
76f7048e 248static void pl_vout_close(void)
b60f2812 249{
6469a8c4 250 plat_gvideo_close();
b60f2812 251}
252
29a8c4f3 253void *pl_prepare_screenshot(int *w, int *h, int *bpp)
254{
6469a8c4 255 void *ret = plat_prepare_screenshot(w, h, bpp);
256 if (ret != NULL)
257 return ret;
258
76f7048e 259 *w = pl_vout_w;
260 *h = pl_vout_h;
261 *bpp = pl_vout_bpp;
29a8c4f3 262
76f7048e 263 return pl_vout_buf;
29a8c4f3 264}
265
1b0c5139 266#ifndef MAEMO
267static void update_analogs(void)
268{
269 int *nubp[2] = { in_a1, in_a2 };
270 int i, a, v, ret;
271
272 for (i = 0; i < 2; i++)
273 {
274 if (in_adev[i] < 0)
275 continue;
276
277 for (a = 0; a < 2; a++) {
278 nubp[i][a] = 127;
279
280 ret = in_update_analog(in_adev[i], in_adev_axis[i][a], &v);
281 if (ret == 0) {
282 v = v / (IN_ABS_RANGE / 128) + 127;
283 nubp[i][a] = v < 0 ? 0 : v;
284 }
285 }
286 }
287 //printf("%4d %4d %4d %4d\n", in_a1[0], in_a1[1], in_a2[0], in_a2[1]);
288}
289
15d46930 290static void update_input(void)
291{
292 int actions[IN_BINDTYPE_COUNT] = { 0, };
8f892648 293 unsigned int emu_act;
15d46930 294
295 in_update(actions);
4c08b9e7 296 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
1b0c5139 297 update_analogs();
8f892648 298 emu_act = actions[IN_BINDTYPE_EMU];
4c08b9e7 299 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
300
301 emu_act &= ~SACTION_GUN_MASK;
8f892648 302 if (emu_act) {
303 int which = 0;
304 for (; !(emu_act & 1); emu_act >>= 1, which++)
305 ;
306 emu_act = which;
307 }
308 emu_set_action(emu_act);
309
799b0b87 310 in_keystate = actions[IN_BINDTYPE_PLAYER12];
447783f8 311#ifdef X11
4218954f 312 extern int x11_update_keys(unsigned int *action);
313 in_keystate |= x11_update_keys(&emu_act);
314 emu_set_action(emu_act);
447783f8 315#endif
15d46930 316}
1b0c5139 317#else /* MAEMO */
318static void update_input(void)
319{
320}
321#endif
15d46930 322
4c08b9e7 323void pl_update_gun(int *xn, int *xres, int *y, int *in)
324{
faf2b2aa 325 if (tsdev)
326 pl_gun_ts_update(tsdev, xn, y, in);
4c08b9e7 327
76f7048e 328 *xres = pl_vout_w;
329 *y = *y * pl_vout_h >> 10;
4c08b9e7 330}
331
bce6b056 332#define MAX_LAG_FRAMES 3
333
334#define tvdiff(tv, tv_old) \
335 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
bce6b056 336
72228559 337/* called on every vsync */
338void pl_frame_limit(void)
339{
bce6b056 340 static struct timeval tv_old, tv_expect;
2f546f9a 341 static int vsync_cnt_prev, drc_active_vsyncs;
bce6b056 342 struct timeval now;
cefe86b7 343 int diff, usadj;
bce6b056 344
345 vsync_cnt++;
72228559 346
15d46930 347 /* doing input here because the pad is polled
348 * thousands of times per frame for some reason */
349 update_input();
350
72228559 351 pcnt_end(PCNT_ALL);
bce6b056 352 gettimeofday(&now, 0);
72228559 353
bce6b056 354 if (now.tv_sec != tv_old.tv_sec) {
355 diff = tvdiff(now, tv_old);
a92f6af1 356 pl_rearmed_cbs.vsps_cur = 0.0f;
bce6b056 357 if (0 < diff && diff < 2000000)
a92f6af1 358 pl_rearmed_cbs.vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
7c0f51de 359 vsync_cnt_prev = vsync_cnt;
a92f6af1 360
361 if (g_opts & OPT_SHOWFPS)
362 pl_rearmed_cbs.flips_per_sec = pl_rearmed_cbs.flip_cnt;
363 pl_rearmed_cbs.flip_cnt = 0;
bd6267e6 364 if (g_opts & OPT_SHOWCPU)
a92f6af1 365 pl_rearmed_cbs.cpu_usage = get_cpu_ticks();
8f892648 366
367 if (hud_new_msg > 0) {
368 hud_new_msg--;
369 if (hud_new_msg == 0)
370 hud_msg[0] = 0;
371 }
a92f6af1 372 tv_old = now;
72228559 373 }
374#ifdef PCNT
375 static int ya_vsync_count;
376 if (++ya_vsync_count == PCNT_FRAMES) {
a92f6af1 377 pcnt_print(pl_rearmed_cbs.vsps_cur);
72228559 378 ya_vsync_count = 0;
379 }
380#endif
381
76f7048e 382 // tv_expect uses usec*1024 units instead of usecs for better accuracy
383 tv_expect.tv_usec += frame_interval1024;
384 if (tv_expect.tv_usec >= (1000000 << 10)) {
385 tv_expect.tv_usec -= (1000000 << 10);
386 tv_expect.tv_sec++;
387 }
388 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
389
390 if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
391 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
c89cd762 392 tv_expect = now;
393 diff = 0;
cefe86b7 394 // try to align with vsync
395 usadj = vsync_usec_time;
76f7048e 396 while (usadj < tv_expect.tv_usec - frame_interval)
397 usadj += frame_interval;
398 tv_expect.tv_usec = usadj << 10;
c89cd762 399 }
400
76f7048e 401 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
c89cd762 402 // yay for working usleep on pandora!
76f7048e 403 //printf("usleep %d\n", diff - frame_interval / 2);
47f37583 404 usleep(diff - frame_interval);
c89cd762 405 }
406
e64dc4c5 407 if (pl_rearmed_cbs.frameskip) {
76f7048e 408 if (diff < -frame_interval)
e64dc4c5 409 pl_rearmed_cbs.fskip_advice = 1;
410 else if (diff >= 0)
411 pl_rearmed_cbs.fskip_advice = 0;
2f546f9a 412
413 // recompilation is not that fast and may cause frame skip on
414 // loading screens and such, resulting in flicker or glitches
415 if (new_dynarec_did_compile) {
416 if (drc_active_vsyncs < 32)
417 pl_rearmed_cbs.fskip_advice = 0;
418 drc_active_vsyncs++;
419 }
420 else
421 drc_active_vsyncs = 0;
422 new_dynarec_did_compile = 0;
bce6b056 423 }
72228559 424
425 pcnt_start(PCNT_ALL);
426}
427
ab423939 428void pl_timing_prepare(int is_pal_)
76f7048e 429{
430 pl_rearmed_cbs.fskip_advice = 0;
a92f6af1 431 pl_rearmed_cbs.flips_per_sec = 0;
432 pl_rearmed_cbs.cpu_usage = 0;
76f7048e 433
ab423939 434 is_pal = is_pal_;
76f7048e 435 frame_interval = is_pal ? 20000 : 16667;
436 frame_interval1024 = is_pal ? 20000*1024 : 17066667;
437
438 // used by P.E.Op.S. frameskip code
439 pl_rearmed_cbs.gpu_peops.fFrameRateHz = is_pal ? 50.0f : 59.94f;
440 pl_rearmed_cbs.gpu_peops.dwFrameRateTicks =
441 (100000*100 / (unsigned long)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100));
442}
443
69af03a2 444static void pl_text_out16_(int x, int y, const char *text)
b60f2812 445{
76f7048e 446 int i, l, len = strlen(text), w = pl_vout_w;
447 unsigned short *screen = (unsigned short *)pl_vout_buf + x + y * w;
69af03a2 448 unsigned short val = 0xffff;
449
450 for (i = 0; i < len; i++, screen += 8)
451 {
452 for (l = 0; l < 8; l++)
453 {
454 unsigned char fd = fontdata8x8[text[i] * 8 + l];
455 unsigned short *s = screen + l * w;
456 if (fd&0x80) s[0] = val;
457 if (fd&0x40) s[1] = val;
458 if (fd&0x20) s[2] = val;
459 if (fd&0x10) s[3] = val;
460 if (fd&0x08) s[4] = val;
461 if (fd&0x04) s[5] = val;
462 if (fd&0x02) s[6] = val;
463 if (fd&0x01) s[7] = val;
464 }
465 }
b60f2812 466}
467
69af03a2 468void pl_text_out16(int x, int y, const char *texto, ...)
b60f2812 469{
69af03a2 470 va_list args;
471 char buffer[256];
472
473 va_start(args, texto);
474 vsnprintf(buffer, sizeof(buffer), texto, args);
475 va_end(args);
476
477 pl_text_out16_(x, y, buffer);
b60f2812 478}
479
201c21e2 480static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
481{
482 *x = g_layer_x;
483 *y = g_layer_y;
484 *w = g_layer_w;
485 *h = g_layer_h;
486}
487
9ee0fd5b 488static void *pl_mmap(unsigned int size)
489{
a2ad8cc5 490 return plat_mmap(0, size, 0, 0);
9ee0fd5b 491}
492
493static void pl_munmap(void *ptr, unsigned int size)
494{
495 plat_munmap(ptr, size);
496}
497
e64dc4c5 498struct rearmed_cbs pl_rearmed_cbs = {
201c21e2 499 pl_get_layer_pos,
76f7048e 500 pl_vout_open,
501 pl_vout_set_mode,
502 pl_vout_flip,
503 pl_vout_close,
9ee0fd5b 504 pl_mmap,
505 pl_munmap,
201c21e2 506};
507
7c0f51de 508/* watchdog */
509static void *watchdog_thread(void *unused)
510{
511 int vsync_cnt_old = 0;
512 int seen_dead = 0;
513 int sleep_time = 5;
514
d3f3bf09 515#if !defined(NDEBUG) || defined(DRC_DBG)
799b0b87 516 // don't interfere with debug
517 return NULL;
518#endif
7c0f51de 519 while (1)
520 {
521 sleep(sleep_time);
522
523 if (stop) {
524 seen_dead = 0;
525 sleep_time = 5;
526 continue;
527 }
528 if (vsync_cnt != vsync_cnt_old) {
529 vsync_cnt_old = vsync_cnt;
530 seen_dead = 0;
531 sleep_time = 2;
532 continue;
533 }
534
535 seen_dead++;
536 sleep_time = 1;
537 if (seen_dead > 1)
538 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
539 if (seen_dead > 4) {
540 fprintf(stderr, "watchdog: lockup detected, aborting\n");
541 // we can't do any cleanup here really, the main thread is
542 // likely touching resources and would crash anyway
543 abort();
544 }
545 }
546}
547
548void pl_start_watchdog(void)
549{
550 pthread_attr_t attr;
551 pthread_t tid;
552 int ret;
553
554 pthread_attr_init(&attr);
555 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
556
557 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
558 if (ret != 0)
559 fprintf(stderr, "could not start watchdog: %d\n", ret);
560}
561
4c08b9e7 562void pl_init(void)
563{
24de2dd4 564 extern unsigned int hSyncCount; // from psxcounters
3ece2f0c 565 extern unsigned int frame_counter;
24de2dd4 566
ddc0a02a 567 psx_w = psx_h = pl_vout_w = pl_vout_h = 256;
568 psx_bpp = pl_vout_bpp = 16;
321ca84d 569
faf2b2aa 570 tsdev = pl_gun_ts_init();
24de2dd4 571
572 pl_rearmed_cbs.gpu_hcnt = &hSyncCount;
3ece2f0c 573 pl_rearmed_cbs.gpu_frame_count = &frame_counter;
4c08b9e7 574}