From 8f9f10f4d37288fdb005fb9db74378ee6e1547ff Mon Sep 17 00:00:00 2001 From: OBattler Date: Sat, 12 Aug 2023 00:17:01 +0200 Subject: [PATCH] The atomic mouse coordinates now use atomic functions and mouse movement is no longer scaled by DPI if mouse coordinates are not raw. --- src/device/mouse.c | 77 +++++++++++++++++++++++------------- src/include/86box/mouse.h | 3 +- src/include/86box/video.h | 4 +- src/qt/qt_renderercommon.cpp | 4 +- src/qt/qt_rendererstack.cpp | 13 +++++- 5 files changed, 68 insertions(+), 33 deletions(-) diff --git a/src/device/mouse.c b/src/device/mouse.c index 99bb596d1..3921b6eb6 100644 --- a/src/device/mouse.c +++ b/src/device/mouse.c @@ -111,6 +111,7 @@ static int mouse_old_b; static const device_t *mouse_curr; static void *mouse_priv; static int mouse_nbut; +static int mouse_raw; static int (*mouse_dev_poll)(void *priv); static void (*mouse_poll_ex)(void) = NULL; @@ -167,7 +168,7 @@ mouse_clear_buttons(void) void mouse_subtract_x(int *delta_x, int *o_x, int min, int max, int abs) { - double real_x = mouse_x; + double real_x = atomic_load(&mouse_x); double smax_x; double rsmin_x; double smin_x; @@ -203,7 +204,7 @@ mouse_subtract_x(int *delta_x, int *o_x, int min, int max, int abs) if (abs) real_x -= rsmin_x; - mouse_x = real_x; + atomic_store(&mouse_x, real_x); } /* It appears all host platforms give us y in the Microsoft format @@ -212,7 +213,7 @@ mouse_subtract_x(int *delta_x, int *o_x, int min, int max, int abs) void mouse_subtract_y(int *delta_y, int *o_y, int min, int max, int invert, int abs) { - double real_y = mouse_y; + double real_y = atomic_load(&mouse_y); double smax_y; double rsmin_y; double smin_y; @@ -254,7 +255,7 @@ mouse_subtract_y(int *delta_y, int *o_y, int min, int max, int invert, int abs) if (invert) real_y = -real_y; - mouse_y = real_y; + atomic_store(&mouse_y, real_y); } /* It appears all host platforms give us y in the Microsoft format @@ -272,7 +273,8 @@ int mouse_moved(void) { /* Convert them to integer so we treat < 1.0 and > -1.0 as 0. */ - int ret = (((int) floor(mouse_x) != 0) || ((int) floor(mouse_y) != 0)); + int ret = (((int) floor(atomic_load(&mouse_x)) != 0) || + ((int) floor(atomic_load(&mouse_y)) != 0)); return ret; } @@ -280,14 +282,16 @@ mouse_moved(void) int mouse_state_changed(void) { + int b; int b_mask = (1 << mouse_nbut) - 1; int wheel = (mouse_nbut >= 4); int ret; - mouse_delta_b = (mouse_buttons ^ mouse_old_b); - mouse_old_b = mouse_buttons; + b = atomic_load(&mouse_buttons); + mouse_delta_b = (b ^ mouse_old_b); + mouse_old_b = b; - ret = mouse_moved() || ((mouse_z != 0) && wheel) || (mouse_delta_b & b_mask); + ret = mouse_moved() || ((atomic_load(&mouse_z) != 0) && wheel) || (mouse_delta_b & b_mask); return ret; } @@ -314,48 +318,62 @@ mouse_timer_poll(UNUSED(void *priv)) #endif } -void -mouse_scale(int x, int y) +static void +atomic_double_add(_Atomic double *var, double val) { - double ratio_x = (double) monitors[0].mon_unscaled_size_x / (double) monitors[0].mon_res_x; - double ratio_y = (double) monitors[0].mon_efscrnsz_y / (double) monitors[0].mon_res_y; + double temp = atomic_load(var); - mouse_x += (((double) x) * mouse_sensitivity * ratio_x); - mouse_y += (((double) y) * mouse_sensitivity * ratio_y); + temp += val; + + atomic_store(var, temp); } void mouse_scale_x(int x) { - double ratio_x = (double) monitors[0].mon_unscaled_size_x / (double) monitors[0].mon_res_x; + double ratio_x = ((double) monitors[0].mon_unscaled_size_x) / monitors[0].mon_res_x; - mouse_x += (((double) x) * mouse_sensitivity * ratio_x); + if (mouse_raw) + ratio_x /= plat_get_dpi(); + + atomic_double_add(&mouse_x, (((double) x) * mouse_sensitivity * ratio_x)); } void mouse_scale_y(int y) { - double ratio_y = (double) monitors[0].mon_efscrnsz_y / (double) monitors[0].mon_res_y; + double ratio_y = ((double) monitors[0].mon_efscrnsz_y) / monitors[0].mon_res_y; - mouse_y += (((double) y) * mouse_sensitivity * ratio_y); + if (mouse_raw) + ratio_y /= plat_get_dpi(); + + atomic_double_add(&mouse_y, (((double) y) * mouse_sensitivity * ratio_y)); +} + +void +mouse_scale(int x, int y) +{ + mouse_scale_x(x); + mouse_scale_y(y); } void mouse_set_z(int z) { - mouse_z += z; + atomic_fetch_add(&mouse_z, z); } void mouse_clear_z(void) { - mouse_z = 0; + atomic_store(&mouse_z, 0); } void mouse_subtract_z(int *delta_z, int min, int max, int invert) { - int real_z = invert ? -mouse_z : mouse_z; + int z = atomic_load(&mouse_z); + int real_z = invert ? -z : z; if (mouse_z > max) { *delta_z = max; @@ -365,23 +383,22 @@ mouse_subtract_z(int *delta_z, int min, int max, int invert) real_z += ABS(min); } else { *delta_z = mouse_z; - mouse_clear_z(); real_z = 0; } - mouse_z = invert ? -real_z : real_z; + atomic_store(&mouse_z, invert ? -real_z : real_z); } void mouse_set_buttons_ex(int b) { - mouse_buttons = b; + atomic_store(&mouse_buttons, b); } int mouse_get_buttons_ex(void) { - return mouse_buttons; + return atomic_load(&mouse_buttons); } void @@ -406,8 +423,8 @@ mouse_set_buttons(int buttons) void mouse_get_abs_coords(double *x_abs, double *y_abs) { - *x_abs = mouse_x_abs; - *y_abs = mouse_y_abs; + *x_abs = atomic_load(&mouse_x_abs); + *y_abs = atomic_load(&mouse_y_abs); } void @@ -496,6 +513,12 @@ mouse_get_ndev(void) return ((sizeof(mouse_devices) / sizeof(mouse_t)) - 1); } +void +mouse_set_raw(int raw) +{ + mouse_raw = raw; +} + void mouse_reset(void) { diff --git a/src/include/86box/mouse.h b/src/include/86box/mouse.h index 2c10f1e57..5133ddfbc 100644 --- a/src/include/86box/mouse.h +++ b/src/include/86box/mouse.h @@ -85,9 +85,9 @@ extern void mouse_subtract_coords(int *delta_x, int *delta_y, int *o_ extern int mouse_moved(void); extern int mouse_state_changed(void); extern int mouse_mbut_changed(void); -extern void mouse_scale(int x, int y); extern void mouse_scale_x(int x); extern void mouse_scale_y(int y); +extern void mouse_scale(int x, int y); extern void mouse_set_z(int z); extern void mouse_clear_z(void); extern void mouse_subtract_z(int *delta_z, int min, int max, int invert); @@ -108,6 +108,7 @@ extern const device_t *mouse_get_device(int mouse); #endif extern int mouse_get_buttons(void); extern int mouse_get_ndev(void); +extern void mouse_set_raw(int raw); extern void mouse_reset(void); extern void mouse_close(void); extern void mouse_init(void); diff --git a/src/include/86box/video.h b/src/include/86box/video.h index 78395a2f6..d2865aa41 100644 --- a/src/include/86box/video.h +++ b/src/include/86box/video.h @@ -103,8 +103,8 @@ typedef struct monitor_t { int mon_efscrnsz_y; int mon_unscaled_size_x; int mon_unscaled_size_y; - int mon_res_x; - int mon_res_y; + double mon_res_x; + double mon_res_y; int mon_bpp; bitmap_t *target_buffer; int mon_video_timing_read_b; diff --git a/src/qt/qt_renderercommon.cpp b/src/qt/qt_renderercommon.cpp index 0f16257d7..6685eede5 100644 --- a/src/qt/qt_renderercommon.cpp +++ b/src/qt/qt_renderercommon.cpp @@ -115,8 +115,8 @@ RendererCommon::onResize(int width, int height) } } - monitors[r_monitor_index].mon_res_x = (int) ((double) destination.width() * plat_get_dpi()); - monitors[r_monitor_index].mon_res_y = (int) ((double) destination.height() * plat_get_dpi()); + monitors[r_monitor_index].mon_res_x = (double) destination.width(); + monitors[r_monitor_index].mon_res_y = (double) destination.height(); } bool diff --git a/src/qt/qt_rendererstack.cpp b/src/qt/qt_rendererstack.cpp index 54f81d80c..55ab814c2 100644 --- a/src/qt/qt_rendererstack.cpp +++ b/src/qt/qt_rendererstack.cpp @@ -69,6 +69,12 @@ RendererStack::RendererStack(QWidget *parent, int monitor_index) : QStackedWidget(parent) , ui(new Ui::RendererStack) { +#ifdef Q_OS_WINDOWS + int raw = 1; +#else + int raw = 0; +#endif + ui->setupUi(this); m_monitor_index = monitor_index; @@ -95,8 +101,10 @@ RendererStack::RendererStack(QWidget *parent, int monitor_index) } # endif # ifdef EVDEV_INPUT - if (!stricmp(mousedata.mouse_type, "evdev")) + if (!stricmp(mousedata.mouse_type, "evdev")) { evdev_init(); + raw = 0; + } # endif if (!stricmp(mousedata.mouse_type, "xinput2")) { extern void xinput2_init(); @@ -105,6 +113,9 @@ RendererStack::RendererStack(QWidget *parent, int monitor_index) this->mouse_exit_func = xinput2_exit; } #endif + + if (monitor_index == 0) + mouse_set_raw(raw); } RendererStack::~RendererStack()