More clean-ups and mouse fixes.

This commit is contained in:
OBattler
2023-08-11 22:29:53 +02:00
parent 750f0ef90e
commit 2391c11260
11 changed files with 395 additions and 388 deletions

View File

@@ -41,12 +41,6 @@ typedef struct mouse_t {
} mouse_t;
int mouse_type = 0;
atomic_int mouse_x;
atomic_int mouse_y;
atomic_int mouse_z;
atomic_int mouse_buttons;
atomic_int old_mouse_x;
atomic_int old_mouse_y;
int mouse_mode;
int mouse_timed = 1;
int mouse_tablet_in_proximity = 0;
@@ -56,10 +50,6 @@ double mouse_x_abs;
double mouse_y_abs;
double mouse_sensitivity = 1.0;
_Atomic double mouse_x_error = 0.0;
_Atomic double mouse_y_error = 0.0;
_Atomic double mouse_x_raw = 0.0;
_Atomic double mouse_y_raw = 0.0;
pc_timer_t mouse_timer; /* mouse event timer */
@@ -97,7 +87,7 @@ static mouse_t mouse_devices[] = {
{ &mouse_internal_device },
{ &mouse_logibus_device },
{ &mouse_msinport_device },
#if 0
#ifdef USE_GENIBUS
{ &mouse_genibus_device },
#endif
{ &mouse_mssystems_device },
@@ -110,10 +100,18 @@ static mouse_t mouse_devices[] = {
// clang-format on
};
static _Atomic double mouse_x;
static _Atomic double mouse_y;
static atomic_int mouse_z;
static atomic_int mouse_buttons;
static int mouse_delta_b;
static int mouse_old_b;
static const device_t *mouse_curr;
static void *mouse_priv;
static int mouse_nbut;
static int (*mouse_dev_poll)(int x, int y, int z, int b, void *priv);
static int (*mouse_dev_poll)(void *priv);
static void (*mouse_poll_ex)(void) = NULL;
static double sample_rate = 200.0;
@@ -136,70 +134,127 @@ mouse_log(const char *fmt, ...)
# define mouse_log(fmt, ...)
#endif
void
mouse_clear_x(void)
{
mouse_x = 0.0;
}
void
mouse_clear_y(void)
{
mouse_y = 0.0;
}
void
mouse_clear_coords(void)
{
mouse_x = mouse_y = mouse_z = 0;
old_mouse_x = old_mouse_y = 0;
mouse_x_error = mouse_y_error = 0.0;
mouse_x_raw = mouse_y_raw = 0.0;
mouse_clear_x();
mouse_clear_y();
mouse_z = 0;
}
/* Initialize the mouse module. */
void
mouse_init(void)
static void
mouse_clear_buttons(void)
{
/* Initialize local data. */
mouse_clear_coords();
mouse_buttons = 0x00;
mouse_buttons = 0x00;
mouse_old_b = 0x00;
mouse_type = MOUSE_TYPE_NONE;
mouse_curr = NULL;
mouse_priv = NULL;
mouse_nbut = 0;
mouse_dev_poll = NULL;
mouse_delta_b = 0x00;
}
void
mouse_close(void)
mouse_subtract_x(int *delta_x, int *o_x, int min, int max, int abs)
{
if (mouse_curr == NULL)
return;
double real_x = mouse_x;
double smax_x;
double rsmin_x;
double smin_x;
mouse_curr = NULL;
mouse_priv = NULL;
mouse_nbut = 0;
mouse_dev_poll = NULL;
rsmin_x = (double) min;
if (abs) {
smax_x = (double) max + ABS(rsmin_x);
max += ABS(min);
real_x += rsmin_x;
smin_x = 0;
} else {
smax_x = (double) max;
smin_x = rsmin_x;
}
timer_stop(&mouse_timer);
/* Default the X and Y overflows to 1. */
if (o_x != NULL)
*o_x = 1;
if (real_x > smax_x) {
*delta_x = abs ? (int) real_x : max;
real_x -= smax_x;
} else if (real_x < smin_x) {
*delta_x = abs ? (int) real_x : min;
real_x += ABS(smin_x);
} else {
*delta_x = (int) real_x;
real_x = 0.0;
if (o_x != NULL)
*o_x = 0;
}
if (abs)
real_x -= rsmin_x;
mouse_x = real_x;
}
static int
mouse_scale_coord_x(int x, int mul)
/* It appears all host platforms give us y in the Microsoft format
(positive to the south), so for all non-Microsoft report formsts,
we have to invert that. */
void
mouse_subtract_y(int *delta_y, int *o_y, int min, int max, int invert, int abs)
{
double temp_x = (double) x;
double ratio = (double) monitors[0].mon_unscaled_size_x / (double) monitors[0].mon_res_x;
double real_y = mouse_y;
double smax_y;
double rsmin_y;
double smin_y;
if (mul)
temp_x *= ratio;
else
temp_x /= ratio;
if (invert)
real_y = -real_y;
return (int) temp_x;
}
rsmin_y = (double) min;
if (abs) {
smax_y = (double) max + ABS(rsmin_y);
max += ABS(min);
real_y += rsmin_y;
smin_y = 0;
} else {
smax_y = (double) max;
smin_y = rsmin_y;
}
static int
mouse_scale_coord_y(int y, int mul)
{
double temp_y = (double) y;
double ratio = (double) monitors[0].mon_efscrnsz_y / (double) monitors[0].mon_res_y;
/* Default the X and Y overflows to 1. */
if (o_y != NULL)
*o_y = 1;
if (mul)
temp_y *= ratio;
else
temp_y /= ratio;
if (real_y > smax_y) {
*delta_y = abs ? (int) real_y : max;
real_y -= smax_y;
} else if (real_y < smin_y) {
*delta_y = abs ? (int) real_y : min;
real_y += ABS(smin_y);
} else {
*delta_y = (int) real_y;
real_y = 0.0;
if (o_y != NULL)
*o_y = 0;
}
return (int) temp_y;
if (abs)
real_y -= rsmin_y;
if (invert)
real_y = -real_y;
mouse_y = real_y;
}
/* It appears all host platforms give us y in the Microsoft format
@@ -209,95 +264,38 @@ void
mouse_subtract_coords(int *delta_x, int *delta_y, int *o_x, int *o_y,
int min, int max, int invert, int abs)
{
int real_x = mouse_x;
int real_y = mouse_y;
int smax_x;
int smax_y;
int rsmin_x;
int rsmin_y;
int smin_x;
int smin_y;
mouse_subtract_x(delta_x, o_x, min, max, abs);
mouse_subtract_y(delta_y, o_y, min, max, invert, abs);
}
if (invert)
real_y = -real_y;
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));
rsmin_x = mouse_scale_coord_x(min, 0);
rsmin_y = mouse_scale_coord_y(min, 0);
if (abs) {
smax_x = mouse_scale_coord_x(max, 0) + ABS(rsmin_x);
smax_y = mouse_scale_coord_y(max, 0) + ABS(rsmin_y);
max += ABS(min);
real_x += rsmin_x;
real_y += rsmin_y;
smin_x = 0;
smin_y = 0;
} else {
smax_x = mouse_scale_coord_x(max, 0);
smax_y = mouse_scale_coord_y(max, 0);
smin_x = rsmin_x;
smin_y = rsmin_y;
}
return ret;
}
/* Default the X and Y overflows to 1. */
if (o_x != NULL)
*o_x = 1;
if (o_y != NULL)
*o_y = 1;
int
mouse_state_changed(void)
{
int b_mask = (1 << mouse_nbut) - 1;
int wheel = (mouse_nbut >= 4);
int ret;
if (real_x > smax_x) {
if (abs)
*delta_x = mouse_scale_coord_x(real_x, 1);
else
*delta_x = max;
real_x -= smax_x;
} else if (real_x < smin_x) {
if (abs)
*delta_x = mouse_scale_coord_x(real_x, 1);
else
*delta_x = min;
real_x += ABS(smin_x);
} else {
if (abs)
*delta_x = mouse_scale_coord_x(real_x, 1);
else
*delta_x = mouse_scale_coord_x(real_x, 1);
real_x = 0;
if (o_x != NULL)
*o_x = 0;
}
mouse_delta_b = (mouse_buttons ^ mouse_old_b);
mouse_old_b = mouse_buttons;
if (real_y > smax_y) {
if (abs)
*delta_y = mouse_scale_coord_y(real_y, 1);
else
*delta_y = max;
real_y -= smax_y;
} else if (real_y < smin_y) {
if (abs)
*delta_y = mouse_scale_coord_y(real_y, 1);
else
*delta_y = min;
real_y += ABS(smin_y);
} else {
if (abs)
*delta_y = mouse_scale_coord_y(real_y, 1);
else
*delta_y = mouse_scale_coord_y(real_y, 1);
real_y = 0;
if (o_y != NULL)
*o_y = 0;
}
ret = mouse_moved() || ((mouse_z != 0) && wheel) || (mouse_delta_b & b_mask);
if (abs) {
real_x -= rsmin_x;
real_y -= rsmin_y;
}
return ret;
}
if (invert)
real_y = -real_y;
mouse_x = real_x;
mouse_y = real_y;
int
mouse_mbut_changed(void)
{
return !!(mouse_delta_b & 0x04);
}
static void
@@ -319,41 +317,27 @@ mouse_timer_poll(UNUSED(void *priv))
void
mouse_scale(int x, int y)
{
double scaled_x = (((double) x) * mouse_sensitivity);
double scaled_y = (((double) y) * mouse_sensitivity);
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;
scaled_x += mouse_x_error;
scaled_y += mouse_y_error;
mouse_x += (int) scaled_x;
mouse_y += (int) scaled_y;
mouse_x_error = scaled_x - floor(scaled_x);
mouse_y_error = scaled_y - floor(scaled_y);
mouse_x += (((double) x) * mouse_sensitivity * ratio_x);
mouse_y += (((double) y) * mouse_sensitivity * ratio_y);
}
void
mouse_scale_x(int x)
{
double scaled_x = (((double) x) * mouse_sensitivity);
double ratio_x = (double) monitors[0].mon_unscaled_size_x / (double) monitors[0].mon_res_x;
scaled_x += mouse_x_error;
mouse_x += (int) scaled_x;
mouse_x_error = scaled_x - floor(scaled_x);
mouse_x += (((double) x) * mouse_sensitivity * ratio_x);
}
void
mouse_scale_y(int y)
{
double scaled_y = (((double) y) * mouse_sensitivity);
double ratio_y = (double) monitors[0].mon_efscrnsz_y / (double) monitors[0].mon_res_y;
scaled_y += mouse_y_error;
mouse_y += (int) scaled_y;
mouse_y_error = scaled_y - floor(scaled_y);
mouse_y += (((double) y) * mouse_sensitivity * ratio_y);
}
void
@@ -362,6 +346,32 @@ mouse_set_z(int z)
mouse_z += z;
}
void
mouse_clear_z(void)
{
mouse_z = 0;
}
void
mouse_subtract_z(int *delta_z, int min, int max, int invert)
{
int real_z = invert ? -mouse_z : mouse_z;
if (mouse_z > max) {
*delta_z = max;
real_z -= max;
} else if (mouse_z < min) {
*delta_z = min;
real_z += ABS(min);
} else {
*delta_z = mouse_z;
mouse_clear_z();
real_z = 0;
}
mouse_z = invert ? -real_z : real_z;
}
void
mouse_set_buttons_ex(int b)
{
@@ -386,39 +396,6 @@ mouse_set_sample_rate(double new_rate)
timer_on_auto(&mouse_timer, 1000000.0 / sample_rate);
}
void
mouse_reset(void)
{
if (mouse_curr != NULL)
return; /* Mouse already initialized. */
mouse_log("MOUSE: reset(type=%d, '%s')\n",
mouse_type, mouse_devices[mouse_type].device->name);
/* Clear local data. */
mouse_clear_coords();
mouse_buttons = 0x00;
mouse_mode = 0;
mouse_timed = 1;
mouse_x_error = mouse_y_error = 0.0;
/* If no mouse configured, we're done. */
if (mouse_type == 0)
return;
timer_add(&mouse_timer, mouse_timer_poll, NULL, 0);
/* Poll at 100 Hz, the default of a PS/2 mouse. */
sample_rate = 100.0;
timer_on_auto(&mouse_timer, 1000000.0 / sample_rate);
mouse_curr = mouse_devices[mouse_type].device;
if (mouse_curr != NULL)
mouse_priv = device_add(mouse_curr);
}
/* Callback from the hardware driver. */
void
mouse_set_buttons(int buttons)
@@ -427,9 +404,10 @@ mouse_set_buttons(int buttons)
}
void
mouse_set_poll_ex(void (*poll_ex)(void))
mouse_get_abs_coords(double *x_abs, double *y_abs)
{
mouse_poll_ex = poll_ex;
*x_abs = mouse_x_abs;
*y_abs = mouse_y_abs;
}
void
@@ -442,14 +420,20 @@ mouse_process(void)
mouse_poll_ex();
else if ((mouse_mode == 0) && ((mouse_dev_poll != NULL) || (mouse_curr->poll != NULL))) {
if (mouse_curr->poll != NULL)
mouse_curr->poll(mouse_x, mouse_y, mouse_z, mouse_buttons, mouse_x_abs, mouse_y_abs, mouse_priv);
mouse_curr->poll(mouse_priv);
else
mouse_dev_poll(mouse_x, mouse_y, mouse_z, mouse_buttons, mouse_priv);
mouse_dev_poll(mouse_priv);
}
}
void
mouse_set_poll(int (*func)(int, int, int, int, void *), void *arg)
mouse_set_poll_ex(void (*poll_ex)(void))
{
mouse_poll_ex = poll_ex;
}
void
mouse_set_poll(int (*func)(void *), void *arg)
{
if (mouse_type != MOUSE_TYPE_INTERNAL)
return;
@@ -511,3 +495,63 @@ mouse_get_ndev(void)
{
return ((sizeof(mouse_devices) / sizeof(mouse_t)) - 1);
}
void
mouse_reset(void)
{
if (mouse_curr != NULL)
return; /* Mouse already initialized. */
mouse_log("MOUSE: reset(type=%d, '%s')\n",
mouse_type, mouse_devices[mouse_type].device->name);
/* Clear local data. */
mouse_clear_coords();
mouse_clear_buttons();
mouse_mode = 0;
mouse_timed = 1;
/* If no mouse configured, we're done. */
if (mouse_type == 0)
return;
timer_add(&mouse_timer, mouse_timer_poll, NULL, 0);
/* Poll at 100 Hz, the default of a PS/2 mouse. */
sample_rate = 100.0;
timer_on_auto(&mouse_timer, 1000000.0 / sample_rate);
mouse_curr = mouse_devices[mouse_type].device;
if (mouse_curr != NULL)
mouse_priv = device_add(mouse_curr);
}
void
mouse_close(void)
{
if (mouse_curr == NULL)
return;
mouse_curr = NULL;
mouse_priv = NULL;
mouse_nbut = 0;
mouse_dev_poll = NULL;
timer_stop(&mouse_timer);
}
/* Initialize the mouse module. */
void
mouse_init(void)
{
/* Initialize local data. */
mouse_clear_coords();
mouse_clear_buttons();
mouse_type = MOUSE_TYPE_NONE;
mouse_curr = NULL;
mouse_priv = NULL;
mouse_nbut = 0;
mouse_dev_poll = NULL;
}