Skip to content

Commit

Permalink
streamline udev calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
zoltanvb committed Dec 24, 2024
1 parent 0dbeacb commit 3f27c26
Showing 1 changed file with 45 additions and 80 deletions.
125 changes: 45 additions & 80 deletions input/drivers/udev_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,96 +781,60 @@ static int16_t udev_mouse_get_y(const udev_input_mouse_t *mouse)
return y + (y < 0 ? -0.5 : 0.5);
}

static int16_t udev_mouse_get_pointer_x(const udev_input_mouse_t *mouse, bool screen, bool confined)
static bool udev_mouse_get_pointer(const udev_input_mouse_t *mouse,
bool screen, bool confined, int16_t *ret_x, int16_t *ret_y)
{
video_viewport_t vp;
double src_min;
double src_width;
int32_t x;
struct video_viewport vp = {0};
int16_t scaled_x;
int16_t scaled_y;
int16_t res_x = 0;
int16_t res_y = 0;
int16_t res_screen_x = 0;
int16_t res_screen_y = 0;

if (!video_driver_get_viewport_info(&vp))
return 0;
return false;

/* mouse coords are absolute? */
if (mouse->abs)
{
/* mouse coordinates are relative to the screen; convert them
/* mouse coordinates are relative to the full screen; convert them
* to be relative to the viewport */
double scaled_x;
src_min = mouse->x_min;
src_width = mouse->x_max - mouse->x_min + 1;
scaled_x = vp.full_width * (mouse->x_abs - src_min) / src_width;
x = -32767.0 + 65535.0 / vp.width * (scaled_x - vp.x);
scaled_x = mouse->x_abs - mouse->x_min;
scaled_y = mouse->y_abs - mouse->y_min;
}
else /* mouse coords are viewport relative */
{
if (screen)
{
src_min = 0.0;
src_width = vp.full_width;
}
else
{
src_min = vp.x;
src_width = vp.width;
}
x = -32767.0 + 65535.0 / src_width * (mouse->x_abs - src_min);
scaled_x = mouse->x_abs;
scaled_y = mouse->y_abs;
}

x += (x < 0 ? -0.5 : 0.5);

if (x < -0x7fff)
return confined ? -0x7fff : -0x8000;
else if (x > 0x7fff)
return confined ? 0x7fff : -0x8000;

return x;
}

static int16_t udev_mouse_get_pointer_y(const udev_input_mouse_t *mouse, bool screen, bool confined)
{
video_viewport_t vp;
double src_min;
double src_height;
int32_t y;

if (!video_driver_get_viewport_info(&vp))
return 0;

/* Mouse coords are absolute? */
if (mouse->abs)
if (confined && video_driver_translate_coord_viewport_confined_wrap(
&vp, scaled_x, scaled_y,
&res_x, &res_y, &res_screen_x, &res_screen_y))
{
double scaled_y;
/* mouse coordinates are relative to the screen; convert them
* to be relative to the viewport */
src_min = mouse->y_min;
src_height = mouse->y_max - mouse->y_min + 1;
scaled_y = vp.full_height * (mouse->y_abs - src_min) / src_height;
y = -32767.0 + 65535.0 / vp.height * (scaled_y - vp.y);
}
else /* mouse coords are viewport relative */
else if (!confined && video_driver_translate_coord_viewport_wrap(
&vp, scaled_x, scaled_y,
&res_x, &res_y, &res_screen_x, &res_screen_y))
{
if (screen)
{
src_min = 0.0;
src_height = vp.full_height;
}
else
{
src_min = vp.y;
src_height = vp.height;
}
y = -32767.0 + 65535.0 / src_height * (mouse->y_abs - src_min);
}

y += (y < 0 ? -0.5 : 0.5);

if (y < -0x7fff)
return confined ? -0x7fff : -0x8000;
else if (y > 0x7fff)
return confined ? 0x7fff : -0x8000;

return y;
else
{
return false;
}

if (screen)
{
*ret_x = res_screen_x;
*ret_y = res_screen_y;
}
else
{
*ret_x = res_x;
*ret_y = res_y;
}
return true;
}

static void udev_handle_mouse(void *data,
Expand Down Expand Up @@ -3617,12 +3581,11 @@ static int16_t udev_lightgun_aiming_state(
{

udev_input_mouse_t *mouse = udev_get_mouse(udev, port);
int16_t res_x;
int16_t res_y;

if (mouse)
if (mouse && udev_mouse_get_pointer(mouse, false, false, &res_x, &res_y))
{
int16_t res_x = udev_mouse_get_pointer_x(mouse, false, false);
int16_t res_y = udev_mouse_get_pointer_y(mouse, false, false);

switch ( id )
{
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X:
Expand Down Expand Up @@ -3725,15 +3688,17 @@ static int16_t udev_pointer_state(udev_input_t *udev,
unsigned port, unsigned id, bool screen)
{
udev_input_mouse_t *mouse = udev_get_mouse(udev, port);
int16_t res_x;
int16_t res_y;

if (mouse)
if (mouse && udev_mouse_get_pointer(mouse, screen, true, &res_x, &res_y))
{
switch (id)
{
case RETRO_DEVICE_ID_POINTER_X:
return udev_mouse_get_pointer_x(mouse, screen, true);
return res_x;
case RETRO_DEVICE_ID_POINTER_Y:
return udev_mouse_get_pointer_y(mouse, screen, true);
return res_y;
case RETRO_DEVICE_ID_POINTER_PRESSED:
if (mouse->abs == 1)
return mouse->pp;
Expand Down

0 comments on commit 3f27c26

Please sign in to comment.