+ emu_act &= ~SACTION_GUN_MASK;
+ if (emu_act) {
+ int which = 0;
+ for (; !(emu_act & 1); emu_act >>= 1, which++)
+ ;
+ emu_act = which;
+ }
+ emu_set_action(emu_act);
+
+ in_keystate = actions[IN_BINDTYPE_PLAYER12];
+#endif
+#ifdef X11
+ extern int x11_update_keys(unsigned int *action);
+ in_keystate |= x11_update_keys(&emu_act);
+ emu_set_action(emu_act);
+#endif
+}
+
+void pl_update_gun(int *xn, int *xres, int *y, int *in)
+{
+ if (tsdev)
+ pl_gun_ts_update(tsdev, xn, y, in);
+
+ *xres = pl_vout_w;
+ *y = *y * pl_vout_h >> 10;
+}
+
+#define MAX_LAG_FRAMES 3
+
+#define tvdiff(tv, tv_old) \
+ ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
+
+/* called on every vsync */
+void pl_frame_limit(void)
+{
+ static struct timeval tv_old, tv_expect;
+ static int vsync_cnt_prev, drc_active_vsyncs;
+ struct timeval now;
+ int diff, usadj;
+
+ vsync_cnt++;
+
+ /* doing input here because the pad is polled
+ * thousands of times per frame for some reason */
+ update_input();
+
+ pcnt_end(PCNT_ALL);
+ gettimeofday(&now, 0);
+
+ if (now.tv_sec != tv_old.tv_sec) {
+ diff = tvdiff(now, tv_old);
+ vsps_cur = 0.0f;
+ if (0 < diff && diff < 2000000)
+ vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
+ vsync_cnt_prev = vsync_cnt;
+ flips_per_sec = pl_flip_cnt;
+ pl_flip_cnt = 0;
+ tv_old = now;
+ if (g_opts & OPT_SHOWCPU)
+ tick_per_sec = get_cpu_ticks();
+
+ if (hud_new_msg > 0) {
+ hud_new_msg--;
+ if (hud_new_msg == 0)
+ hud_msg[0] = 0;
+ }
+ }
+#ifdef PCNT
+ static int ya_vsync_count;
+ if (++ya_vsync_count == PCNT_FRAMES) {
+ pcnt_print(vsps_cur);
+ ya_vsync_count = 0;
+ }
+#endif
+
+ // tv_expect uses usec*1024 units instead of usecs for better accuracy
+ tv_expect.tv_usec += frame_interval1024;
+ if (tv_expect.tv_usec >= (1000000 << 10)) {
+ tv_expect.tv_usec -= (1000000 << 10);
+ tv_expect.tv_sec++;
+ }
+ diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
+
+ if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
+ //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
+ tv_expect = now;
+ diff = 0;
+ // try to align with vsync
+ usadj = vsync_usec_time;
+ while (usadj < tv_expect.tv_usec - frame_interval)
+ usadj += frame_interval;
+ tv_expect.tv_usec = usadj << 10;
+ }
+
+ if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
+ // yay for working usleep on pandora!
+ //printf("usleep %d\n", diff - frame_interval / 2);
+ usleep(diff - frame_interval);
+ }
+
+ if (pl_rearmed_cbs.frameskip) {
+ if (diff < -frame_interval)
+ pl_rearmed_cbs.fskip_advice = 1;
+ else if (diff >= 0)
+ pl_rearmed_cbs.fskip_advice = 0;
+
+ // recompilation is not that fast and may cause frame skip on
+ // loading screens and such, resulting in flicker or glitches
+ if (new_dynarec_did_compile) {
+ if (drc_active_vsyncs < 32)
+ pl_rearmed_cbs.fskip_advice = 0;
+ drc_active_vsyncs++;
+ }
+ else
+ drc_active_vsyncs = 0;
+ new_dynarec_did_compile = 0;
+ }
+
+ pcnt_start(PCNT_ALL);