add vibration support for Caanoo
[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"
24#include "omap.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 };
35int in_keystate, in_state_gun;
b944a30e 36int in_enable_vibration;
55b0eeea 37int pl_flip_cnt;
faf2b2aa 38void *tsdev;
76f7048e 39void *pl_vout_buf;
40static int pl_vout_w, pl_vout_h, pl_vout_bpp;
55b0eeea 41static int vsync_cnt, flips_per_sec, tick_per_sec;
bce6b056 42static float vsps_cur;
76f7048e 43static int frame_interval, frame_interval1024, vsync_usec_time;
72228559 44
4c08b9e7 45
46static __attribute__((noinline)) int get_cpu_ticks(void)
72228559 47{
48 static unsigned long last_utime;
49 static int fd;
50 unsigned long utime, ret;
51 char buf[128];
52
53 if (fd == 0)
54 fd = open("/proc/self/stat", O_RDONLY);
55 lseek(fd, 0, SEEK_SET);
56 buf[0] = 0;
57 read(fd, buf, sizeof(buf));
58 buf[sizeof(buf) - 1] = 0;
59
60 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
61 ret = utime - last_utime;
62 last_utime = utime;
63 return ret;
64}
65
41f55c9f 66static void print_msg(int h, int border)
8f892648 67{
76f7048e 68 if (pl_vout_bpp == 16)
41f55c9f 69 pl_text_out16(border + 2, h - 10, "%s", hud_msg);
8f892648 70}
71
41f55c9f 72static void print_fps(int h, int border)
72228559 73{
76f7048e 74 if (pl_vout_bpp == 16)
41f55c9f 75 pl_text_out16(border + 2, h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
72228559 76}
77
41f55c9f 78static void print_cpu_usage(int w, int h, int border)
72228559 79{
76f7048e 80 if (pl_vout_bpp == 16)
41f55c9f 81 pl_text_out16(w - border - 28, h - 10, "%3d", tick_per_sec);
72228559 82}
b60f2812 83
90f1d26c 84// draw 192x8 status of 24 sound channels
55b0eeea 85static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h)
90f1d26c 86{
174c454a 87 extern void spu_get_debug_info(int *chans_out, int *run_chans,
88 int *fmod_chans_out, int *noise_chans_out); // hack
89 int live_chans, run_chans, fmod_chans, noise_chans;
90f1d26c 90
91 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
76f7048e 92 unsigned short *dest = (unsigned short *)pl_vout_buf +
55b0eeea 93 vout_w * (vout_h - 10) + vout_w / 2 - 192/2;
90f1d26c 94 unsigned short *d, p;
95 int c, x, y;
96
76f7048e 97 if (pl_vout_bpp != 16)
90f1d26c 98 return;
99
174c454a 100 spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
90f1d26c 101
102 for (c = 0; c < 24; c++) {
103 d = dest + c * 8;
174c454a 104 p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
90f1d26c 105 (fmod_chans & (1<<c)) ? 0xf000 :
106 (noise_chans & (1<<c)) ? 0x001f :
107 colors[c & 1];
55b0eeea 108 for (y = 0; y < 8; y++, d += vout_w)
90f1d26c 109 for (x = 0; x < 8; x++)
110 d[x] = p;
111 }
112}
113
41f55c9f 114void pl_print_hud(int w, int h, int xborder)
55b0eeea 115{
116 pl_vout_w = w; // used by pollux
117 pl_vout_h = h;
118
119 if (g_opts & OPT_SHOWSPU)
120 draw_active_chans(w, h);
121
122 if (hud_msg[0] != 0)
41f55c9f 123 print_msg(h, xborder);
55b0eeea 124 else if (g_opts & OPT_SHOWFPS)
41f55c9f 125 print_fps(h, xborder);
55b0eeea 126
127 if (g_opts & OPT_SHOWCPU)
41f55c9f 128 print_cpu_usage(w, h, xborder);
55b0eeea 129}
130
76f7048e 131static void *pl_vout_set_mode(int w, int h, int bpp)
69af03a2 132{
a185be70 133 // special h handling, Wipeout likes to change it by 1-6
321ca84d 134 static int vsync_cnt_ms_prev;
135 if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60)
136 h = (h + 7) & ~7;
137 vsync_cnt_ms_prev = vsync_cnt;
a185be70 138
76f7048e 139 if (w == pl_vout_w && h == pl_vout_h && bpp == pl_vout_bpp)
140 return pl_vout_buf;
b60f2812 141
76f7048e 142 pl_vout_w = w;
143 pl_vout_h = h;
144 pl_vout_bpp = bpp;
d352cde2 145
76f7048e 146#if defined(VOUT_FBDEV)
d352cde2 147 vout_fbdev_clear(layer_fb);
76f7048e 148 pl_vout_buf = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
149#elif defined(MAEMO)
150 extern void *hildon_set_mode(int w, int h);
151 pl_vout_buf = hildon_set_mode(w, h);
152#endif
d352cde2 153
76f7048e 154 if (pl_vout_buf == NULL)
155 fprintf(stderr, "failed to set mode\n");
a185be70 156
157 // menu decides on layer size, we commit it
bd6267e6 158 menu_notify_mode_change(w, h, bpp);
a185be70 159 omap_enable_layer(1);
3c70c47b 160
76f7048e 161 return pl_vout_buf;
69af03a2 162}
163
76f7048e 164static void *pl_vout_flip(void)
69af03a2 165{
55b0eeea 166 pl_flip_cnt++;
8f892648 167
55b0eeea 168 if (pl_vout_buf != NULL)
41f55c9f 169 pl_print_hud(pl_vout_w, pl_vout_h, 0);
72228559 170
69af03a2 171 // let's flip now
76f7048e 172#if defined(VOUT_FBDEV)
173 pl_vout_buf = vout_fbdev_flip(layer_fb);
174#elif defined(MAEMO)
175 extern void *hildon_flip(void);
176 pl_vout_buf = hildon_flip();
177#endif
178 return pl_vout_buf;
b60f2812 179}
180
76f7048e 181static int pl_vout_open(void)
6d1a1ac2 182{
cefe86b7 183 struct timeval now;
184
6d1a1ac2 185 omap_enable_layer(1);
76f7048e 186#if defined(VOUT_FBDEV)
321ca84d 187 // force mode update
188 int h = pl_vout_h;
189 pl_vout_h--;
190 pl_vout_buf = pl_vout_set_mode(pl_vout_w, h, pl_vout_bpp);
cefe86b7 191
192 // try to align redraws to vsync
193 vout_fbdev_wait_vsync(layer_fb);
76f7048e 194#elif defined(MAEMO)
195 extern void *hildon_flip(void);
196 pl_vout_buf = hildon_flip();
197#endif
198
cefe86b7 199 gettimeofday(&now, 0);
200 vsync_usec_time = now.tv_usec;
76f7048e 201 while (vsync_usec_time >= frame_interval)
202 vsync_usec_time -= frame_interval;
cefe86b7 203
6d1a1ac2 204 return 0;
205}
206
76f7048e 207static void pl_vout_close(void)
b60f2812 208{
6d1a1ac2 209 omap_enable_layer(0);
b60f2812 210}
211
29a8c4f3 212void *pl_prepare_screenshot(int *w, int *h, int *bpp)
213{
47821672 214#if defined(VOUT_FBDEV)
76f7048e 215 *w = pl_vout_w;
216 *h = pl_vout_h;
217 *bpp = pl_vout_bpp;
29a8c4f3 218
76f7048e 219 return pl_vout_buf;
47821672 220#else
221 return plat_prepare_screenshot(w, h, bpp);
222#endif
29a8c4f3 223}
224
15d46930 225static void update_input(void)
226{
76f7048e 227#ifndef MAEMO
15d46930 228 int actions[IN_BINDTYPE_COUNT] = { 0, };
8f892648 229 unsigned int emu_act;
15d46930 230
231 in_update(actions);
4c08b9e7 232 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
799b0b87 233 in_update_analogs();
8f892648 234 emu_act = actions[IN_BINDTYPE_EMU];
4c08b9e7 235 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
236
237 emu_act &= ~SACTION_GUN_MASK;
8f892648 238 if (emu_act) {
239 int which = 0;
240 for (; !(emu_act & 1); emu_act >>= 1, which++)
241 ;
242 emu_act = which;
243 }
244 emu_set_action(emu_act);
245
799b0b87 246 in_keystate = actions[IN_BINDTYPE_PLAYER12];
76f7048e 247#endif
447783f8 248#ifdef X11
4218954f 249 extern int x11_update_keys(unsigned int *action);
250 in_keystate |= x11_update_keys(&emu_act);
251 emu_set_action(emu_act);
447783f8 252#endif
15d46930 253}
254
4c08b9e7 255void pl_update_gun(int *xn, int *xres, int *y, int *in)
256{
faf2b2aa 257 if (tsdev)
258 pl_gun_ts_update(tsdev, xn, y, in);
4c08b9e7 259
76f7048e 260 *xres = pl_vout_w;
261 *y = *y * pl_vout_h >> 10;
4c08b9e7 262}
263
bce6b056 264#define MAX_LAG_FRAMES 3
265
266#define tvdiff(tv, tv_old) \
267 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
bce6b056 268
72228559 269/* called on every vsync */
270void pl_frame_limit(void)
271{
bce6b056 272 static struct timeval tv_old, tv_expect;
2f546f9a 273 static int vsync_cnt_prev, drc_active_vsyncs;
bce6b056 274 struct timeval now;
cefe86b7 275 int diff, usadj;
bce6b056 276
277 vsync_cnt++;
72228559 278
15d46930 279 /* doing input here because the pad is polled
280 * thousands of times per frame for some reason */
281 update_input();
282
72228559 283 pcnt_end(PCNT_ALL);
bce6b056 284 gettimeofday(&now, 0);
72228559 285
bce6b056 286 if (now.tv_sec != tv_old.tv_sec) {
287 diff = tvdiff(now, tv_old);
288 vsps_cur = 0.0f;
289 if (0 < diff && diff < 2000000)
7c0f51de 290 vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
291 vsync_cnt_prev = vsync_cnt;
55b0eeea 292 flips_per_sec = pl_flip_cnt;
293 pl_flip_cnt = 0;
bce6b056 294 tv_old = now;
bd6267e6 295 if (g_opts & OPT_SHOWCPU)
296 tick_per_sec = get_cpu_ticks();
8f892648 297
298 if (hud_new_msg > 0) {
299 hud_new_msg--;
300 if (hud_new_msg == 0)
301 hud_msg[0] = 0;
302 }
72228559 303 }
304#ifdef PCNT
305 static int ya_vsync_count;
306 if (++ya_vsync_count == PCNT_FRAMES) {
bce6b056 307 pcnt_print(vsps_cur);
72228559 308 ya_vsync_count = 0;
309 }
310#endif
311
76f7048e 312 // tv_expect uses usec*1024 units instead of usecs for better accuracy
313 tv_expect.tv_usec += frame_interval1024;
314 if (tv_expect.tv_usec >= (1000000 << 10)) {
315 tv_expect.tv_usec -= (1000000 << 10);
316 tv_expect.tv_sec++;
317 }
318 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
319
320 if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
321 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
c89cd762 322 tv_expect = now;
323 diff = 0;
cefe86b7 324 // try to align with vsync
325 usadj = vsync_usec_time;
76f7048e 326 while (usadj < tv_expect.tv_usec - frame_interval)
327 usadj += frame_interval;
328 tv_expect.tv_usec = usadj << 10;
c89cd762 329 }
330
76f7048e 331 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
c89cd762 332 // yay for working usleep on pandora!
76f7048e 333 //printf("usleep %d\n", diff - frame_interval / 2);
47f37583 334 usleep(diff - frame_interval);
c89cd762 335 }
336
e64dc4c5 337 if (pl_rearmed_cbs.frameskip) {
76f7048e 338 if (diff < -frame_interval)
e64dc4c5 339 pl_rearmed_cbs.fskip_advice = 1;
340 else if (diff >= 0)
341 pl_rearmed_cbs.fskip_advice = 0;
2f546f9a 342
343 // recompilation is not that fast and may cause frame skip on
344 // loading screens and such, resulting in flicker or glitches
345 if (new_dynarec_did_compile) {
346 if (drc_active_vsyncs < 32)
347 pl_rearmed_cbs.fskip_advice = 0;
348 drc_active_vsyncs++;
349 }
350 else
351 drc_active_vsyncs = 0;
352 new_dynarec_did_compile = 0;
bce6b056 353 }
72228559 354
355 pcnt_start(PCNT_ALL);
356}
357
76f7048e 358void pl_timing_prepare(int is_pal)
359{
360 pl_rearmed_cbs.fskip_advice = 0;
361
362 frame_interval = is_pal ? 20000 : 16667;
363 frame_interval1024 = is_pal ? 20000*1024 : 17066667;
364
365 // used by P.E.Op.S. frameskip code
366 pl_rearmed_cbs.gpu_peops.fFrameRateHz = is_pal ? 50.0f : 59.94f;
367 pl_rearmed_cbs.gpu_peops.dwFrameRateTicks =
368 (100000*100 / (unsigned long)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100));
369}
370
69af03a2 371static void pl_text_out16_(int x, int y, const char *text)
b60f2812 372{
76f7048e 373 int i, l, len = strlen(text), w = pl_vout_w;
374 unsigned short *screen = (unsigned short *)pl_vout_buf + x + y * w;
69af03a2 375 unsigned short val = 0xffff;
376
377 for (i = 0; i < len; i++, screen += 8)
378 {
379 for (l = 0; l < 8; l++)
380 {
381 unsigned char fd = fontdata8x8[text[i] * 8 + l];
382 unsigned short *s = screen + l * w;
383 if (fd&0x80) s[0] = val;
384 if (fd&0x40) s[1] = val;
385 if (fd&0x20) s[2] = val;
386 if (fd&0x10) s[3] = val;
387 if (fd&0x08) s[4] = val;
388 if (fd&0x04) s[5] = val;
389 if (fd&0x02) s[6] = val;
390 if (fd&0x01) s[7] = val;
391 }
392 }
b60f2812 393}
394
69af03a2 395void pl_text_out16(int x, int y, const char *texto, ...)
b60f2812 396{
69af03a2 397 va_list args;
398 char buffer[256];
399
400 va_start(args, texto);
401 vsnprintf(buffer, sizeof(buffer), texto, args);
402 va_end(args);
403
404 pl_text_out16_(x, y, buffer);
b60f2812 405}
406
201c21e2 407static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
408{
409 *x = g_layer_x;
410 *y = g_layer_y;
411 *w = g_layer_w;
412 *h = g_layer_h;
413}
414
e64dc4c5 415struct rearmed_cbs pl_rearmed_cbs = {
201c21e2 416 pl_get_layer_pos,
76f7048e 417 pl_vout_open,
418 pl_vout_set_mode,
419 pl_vout_flip,
420 pl_vout_close,
201c21e2 421};
422
7c0f51de 423/* watchdog */
424static void *watchdog_thread(void *unused)
425{
426 int vsync_cnt_old = 0;
427 int seen_dead = 0;
428 int sleep_time = 5;
429
d3f3bf09 430#if !defined(NDEBUG) || defined(DRC_DBG)
799b0b87 431 // don't interfere with debug
432 return NULL;
433#endif
7c0f51de 434 while (1)
435 {
436 sleep(sleep_time);
437
438 if (stop) {
439 seen_dead = 0;
440 sleep_time = 5;
441 continue;
442 }
443 if (vsync_cnt != vsync_cnt_old) {
444 vsync_cnt_old = vsync_cnt;
445 seen_dead = 0;
446 sleep_time = 2;
447 continue;
448 }
449
450 seen_dead++;
451 sleep_time = 1;
452 if (seen_dead > 1)
453 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
454 if (seen_dead > 4) {
455 fprintf(stderr, "watchdog: lockup detected, aborting\n");
456 // we can't do any cleanup here really, the main thread is
457 // likely touching resources and would crash anyway
458 abort();
459 }
460 }
461}
462
463void pl_start_watchdog(void)
464{
465 pthread_attr_t attr;
466 pthread_t tid;
467 int ret;
468
469 pthread_attr_init(&attr);
470 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
471
472 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
473 if (ret != 0)
474 fprintf(stderr, "could not start watchdog: %d\n", ret);
475}
476
4c08b9e7 477void pl_init(void)
478{
24de2dd4 479 extern unsigned int hSyncCount; // from psxcounters
3ece2f0c 480 extern unsigned int frame_counter;
24de2dd4 481
321ca84d 482 pl_vout_w = pl_vout_h = 256;
483 pl_vout_bpp = 16;
484
faf2b2aa 485 tsdev = pl_gun_ts_init();
24de2dd4 486
487 pl_rearmed_cbs.gpu_hcnt = &hSyncCount;
3ece2f0c 488 pl_rearmed_cbs.gpu_frame_count = &frame_counter;
4c08b9e7 489}