Skip to content

Commit e5fde4d

Browse files
authored
Pointer sanitization - winraw, dinput, sdl (partial) (#17283)
Adapt the sanitized pointer handling, discussed at #17196 : winraw and dinput driver specific changes: make sure pointer position is always within [-0x7fff,0x7fff] by using the confined wrapper remove extra "inside" checks, general simplification enable pointer offscreen reporting use common functions for edge detection and lightgun button ID conversion sdl driver specific changes: pointer handling aligned with the other input drivers, as above added TODO for lightgun part - no suitable test env at the moment where SDL input can be used
1 parent b124fe6 commit e5fde4d

File tree

3 files changed

+32
-168
lines changed

3 files changed

+32
-168
lines changed

input/drivers/dinput.c

Lines changed: 18 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ static bool dinput_mouse_button_pressed(
432432
static int16_t dinput_lightgun_aiming_state(
433433
struct dinput_input *di, unsigned idx, unsigned id)
434434
{
435-
struct video_viewport vp;
436-
const int edge_detect = 32700;
435+
struct video_viewport vp = {0};
437436
int16_t res_x = 0;
438437
int16_t res_y = 0;
439438
int16_t res_screen_x = 0;
@@ -446,13 +445,6 @@ static int16_t dinput_lightgun_aiming_state(
446445
struct dinput_pointer_status
447446
*check_pos = di->pointer_head.next;
448447

449-
vp.x = 0;
450-
vp.y = 0;
451-
vp.width = 0;
452-
vp.height = 0;
453-
vp.full_width = 0;
454-
vp.full_height = 0;
455-
456448
while (check_pos && num < idx)
457449
{
458450
num++;
@@ -475,24 +467,14 @@ static int16_t dinput_lightgun_aiming_state(
475467
&vp, x, y,
476468
&res_x, &res_y, &res_screen_x, &res_screen_y))
477469
{
478-
bool inside =
479-
(res_x >= -edge_detect)
480-
&& (res_y >= -edge_detect)
481-
&& (res_x <= edge_detect)
482-
&& (res_y <= edge_detect);
483-
484470
switch (id)
485471
{
486472
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X:
487-
if (inside)
488-
return res_x;
489-
break;
473+
return res_x;
490474
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y:
491-
if (inside)
492-
return res_y;
493-
break;
475+
return res_y;
494476
case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
495-
return !inside;
477+
return input_driver_pointer_is_offscreen(res_x, res_y);
496478
default:
497479
break;
498480
}
@@ -501,41 +483,6 @@ static int16_t dinput_lightgun_aiming_state(
501483
return 0;
502484
}
503485

504-
static unsigned dinput_retro_id_to_rarch(unsigned id)
505-
{
506-
switch (id)
507-
{
508-
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT:
509-
return RARCH_LIGHTGUN_DPAD_RIGHT;
510-
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_LEFT:
511-
return RARCH_LIGHTGUN_DPAD_LEFT;
512-
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_UP:
513-
return RARCH_LIGHTGUN_DPAD_UP;
514-
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_DOWN:
515-
return RARCH_LIGHTGUN_DPAD_DOWN;
516-
case RETRO_DEVICE_ID_LIGHTGUN_SELECT:
517-
return RARCH_LIGHTGUN_SELECT;
518-
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
519-
return RARCH_LIGHTGUN_START;
520-
case RETRO_DEVICE_ID_LIGHTGUN_RELOAD:
521-
return RARCH_LIGHTGUN_RELOAD;
522-
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
523-
return RARCH_LIGHTGUN_TRIGGER;
524-
case RETRO_DEVICE_ID_LIGHTGUN_AUX_A:
525-
return RARCH_LIGHTGUN_AUX_A;
526-
case RETRO_DEVICE_ID_LIGHTGUN_AUX_B:
527-
return RARCH_LIGHTGUN_AUX_B;
528-
case RETRO_DEVICE_ID_LIGHTGUN_AUX_C:
529-
return RARCH_LIGHTGUN_AUX_C;
530-
case RETRO_DEVICE_ID_LIGHTGUN_START:
531-
return RARCH_LIGHTGUN_START;
532-
default:
533-
break;
534-
}
535-
536-
return 0;
537-
}
538-
539486
static int16_t dinput_input_state(
540487
void *data,
541488
const input_device_driver_t *joypad,
@@ -717,8 +664,7 @@ static int16_t dinput_input_state(
717664
case RETRO_DEVICE_POINTER:
718665
case RARCH_DEVICE_POINTER_SCREEN:
719666
{
720-
struct video_viewport vp;
721-
bool inside = false;
667+
struct video_viewport vp = {0};
722668
int x = 0;
723669
int y = 0;
724670
int16_t res_x = 0;
@@ -729,13 +675,6 @@ static int16_t dinput_input_state(
729675
struct dinput_pointer_status *
730676
check_pos = di->pointer_head.next;
731677

732-
vp.x = 0;
733-
vp.y = 0;
734-
vp.width = 0;
735-
vp.height = 0;
736-
vp.full_width = 0;
737-
vp.full_height = 0;
738-
739678
while (check_pos && num < idx)
740679
{
741680
num++;
@@ -753,7 +692,7 @@ static int16_t dinput_input_state(
753692
y = check_pos->pointer_y;
754693
}
755694

756-
if (video_driver_translate_coord_viewport_wrap(&vp, x, y,
695+
if (video_driver_translate_coord_viewport_confined_wrap(&vp, x, y,
757696
&res_x, &res_y, &res_screen_x, &res_screen_y))
758697
{
759698
if (device == RARCH_DEVICE_POINTER_SCREEN)
@@ -762,19 +701,18 @@ static int16_t dinput_input_state(
762701
res_y = res_screen_y;
763702
}
764703

765-
if ((inside = (res_x >= -0x7fff) && (res_y >= -0x7fff)))
704+
switch (id)
766705
{
767-
switch (id)
768-
{
769-
case RETRO_DEVICE_ID_POINTER_X:
770-
return res_x;
771-
case RETRO_DEVICE_ID_POINTER_Y:
772-
return res_y;
773-
case RETRO_DEVICE_ID_POINTER_PRESSED:
774-
return check_pos ? 1 : (di->flags & DINP_FLAG_MOUSE_L_BTN) > 0;
775-
default:
776-
break;
777-
}
706+
case RETRO_DEVICE_ID_POINTER_X:
707+
return res_x;
708+
case RETRO_DEVICE_ID_POINTER_Y:
709+
return res_y;
710+
case RETRO_DEVICE_ID_POINTER_PRESSED:
711+
return check_pos ? 1 : (di->flags & DINP_FLAG_MOUSE_L_BTN) > 0;
712+
case RETRO_DEVICE_ID_POINTER_IS_OFFSCREEN:
713+
return input_driver_pointer_is_offscreen(res_x, res_y);
714+
default:
715+
break;
778716
}
779717
}
780718
}
@@ -802,7 +740,7 @@ static int16_t dinput_input_state(
802740
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT:
803741
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
804742
{
805-
unsigned new_id = dinput_retro_id_to_rarch(id);
743+
unsigned new_id = input_driver_lightgun_id_convert(id);
806744
const uint64_t bind_joykey = input_config_binds[port][new_id].joykey;
807745
const uint64_t bind_joyaxis = input_config_binds[port][new_id].joyaxis;
808746
const uint64_t autobind_joykey = input_autoconf_binds[port][new_id].joykey;

input/drivers/sdl_input.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -256,24 +256,15 @@ static int16_t sdl_input_state(
256256
case RARCH_DEVICE_POINTER_SCREEN:
257257
if (idx == 0)
258258
{
259-
struct video_viewport vp;
259+
struct video_viewport vp = {0};
260260
bool screen = device ==
261261
RARCH_DEVICE_POINTER_SCREEN;
262-
const int edge_detect = 32700;
263-
bool inside = false;
264262
int16_t res_x = 0;
265263
int16_t res_y = 0;
266264
int16_t res_screen_x = 0;
267265
int16_t res_screen_y = 0;
268266

269-
vp.x = 0;
270-
vp.y = 0;
271-
vp.width = 0;
272-
vp.height = 0;
273-
vp.full_width = 0;
274-
vp.full_height = 0;
275-
276-
if (video_driver_translate_coord_viewport_wrap(
267+
if (video_driver_translate_coord_viewport_confined_wrap(
277268
&vp, sdl->mouse_abs_x, sdl->mouse_abs_y,
278269
&res_x, &res_y, &res_screen_x, &res_screen_y))
279270
{
@@ -283,11 +274,6 @@ static int16_t sdl_input_state(
283274
res_y = res_screen_y;
284275
}
285276

286-
inside = (res_x >= -edge_detect)
287-
&& (res_y >= -edge_detect)
288-
&& (res_x <= edge_detect)
289-
&& (res_y <= edge_detect);
290-
291277
switch (id)
292278
{
293279
case RETRO_DEVICE_ID_POINTER_X:
@@ -296,14 +282,15 @@ static int16_t sdl_input_state(
296282
return res_y;
297283
case RETRO_DEVICE_ID_POINTER_PRESSED:
298284
return sdl->mouse_l;
299-
case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
300-
return !inside;
285+
case RETRO_DEVICE_ID_POINTER_IS_OFFSCREEN:
286+
return input_driver_pointer_is_offscreen(res_x, res_y);
301287
}
302288
}
303289
}
304290
break;
305291
case RETRO_DEVICE_KEYBOARD:
306292
return (id && id < RETROK_LAST) && sdl_key_pressed(id);
293+
/* TODO: update to match other input drivers (aiming state, button binds) */
307294
case RETRO_DEVICE_LIGHTGUN:
308295
switch (id)
309296
{

input/drivers/winraw_input.c

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -230,41 +230,24 @@ static int16_t winraw_lightgun_aiming_state(winraw_input_t *wr,
230230
winraw_mouse_t *mouse,
231231
unsigned port, unsigned id)
232232
{
233-
struct video_viewport vp;
233+
struct video_viewport vp = {0};
234234
int16_t res_x = 0;
235235
int16_t res_y = 0;
236236
int16_t res_screen_x = 0;
237237
int16_t res_screen_y = 0;
238238

239-
vp.x = 0;
240-
vp.y = 0;
241-
vp.width = 0;
242-
vp.height = 0;
243-
vp.full_width = 0;
244-
vp.full_height = 0;
245-
246239
if ((video_driver_translate_coord_viewport_wrap(
247240
&vp, mouse->x, mouse->y,
248241
&res_x, &res_y, &res_screen_x, &res_screen_y)))
249242
{
250-
const int edge_detect = 32700;
251-
bool inside = (res_x >= -edge_detect)
252-
&& (res_y >= -edge_detect)
253-
&& (res_x <= edge_detect)
254-
&& (res_y <= edge_detect);
255-
256243
switch (id)
257244
{
258245
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X:
259-
if (inside)
260-
return res_x;
261-
break;
246+
return res_x;
262247
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y:
263-
if (inside)
264-
return res_y;
265-
break;
248+
return res_y;
266249
case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
267-
return !inside;
250+
return input_driver_pointer_is_offscreen(res_x, res_y);
268251
default:
269252
break;
270253
}
@@ -675,41 +658,6 @@ static void winraw_poll(void *data)
675658
memset(wr->kb_keys, 0, SC_LAST);
676659
}
677660

678-
static unsigned winraw_retro_id_to_rarch(unsigned id)
679-
{
680-
switch (id)
681-
{
682-
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT:
683-
return RARCH_LIGHTGUN_DPAD_RIGHT;
684-
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_LEFT:
685-
return RARCH_LIGHTGUN_DPAD_LEFT;
686-
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_UP:
687-
return RARCH_LIGHTGUN_DPAD_UP;
688-
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_DOWN:
689-
return RARCH_LIGHTGUN_DPAD_DOWN;
690-
case RETRO_DEVICE_ID_LIGHTGUN_SELECT:
691-
return RARCH_LIGHTGUN_SELECT;
692-
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
693-
return RARCH_LIGHTGUN_START;
694-
case RETRO_DEVICE_ID_LIGHTGUN_RELOAD:
695-
return RARCH_LIGHTGUN_RELOAD;
696-
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
697-
return RARCH_LIGHTGUN_TRIGGER;
698-
case RETRO_DEVICE_ID_LIGHTGUN_AUX_A:
699-
return RARCH_LIGHTGUN_AUX_A;
700-
case RETRO_DEVICE_ID_LIGHTGUN_AUX_B:
701-
return RARCH_LIGHTGUN_AUX_B;
702-
case RETRO_DEVICE_ID_LIGHTGUN_AUX_C:
703-
return RARCH_LIGHTGUN_AUX_C;
704-
case RETRO_DEVICE_ID_LIGHTGUN_START:
705-
return RARCH_LIGHTGUN_START;
706-
default:
707-
break;
708-
}
709-
710-
return 0;
711-
}
712-
713661
static int16_t winraw_input_state(
714662
void *data,
715663
const input_device_driver_t *joypad,
@@ -876,9 +824,8 @@ static int16_t winraw_input_state(
876824
case RETRO_DEVICE_POINTER:
877825
case RARCH_DEVICE_POINTER_SCREEN:
878826
{
879-
struct video_viewport vp;
827+
struct video_viewport vp = {0};
880828
bool pointer_down = false;
881-
bool inside = false;
882829
int x = 0;
883830
int y = 0;
884831
int16_t res_x = 0;
@@ -889,13 +836,6 @@ static int16_t winraw_input_state(
889836
struct winraw_pointer_status *
890837
check_pos = wr->pointer_head.next;
891838

892-
vp.x = 0;
893-
vp.y = 0;
894-
vp.width = 0;
895-
vp.height = 0;
896-
vp.full_width = 0;
897-
vp.full_height = 0;
898-
899839
while (check_pos && num < idx)
900840
{
901841
num++;
@@ -918,7 +858,7 @@ static int16_t winraw_input_state(
918858
pointer_down = true;
919859
}
920860

921-
if (!(video_driver_translate_coord_viewport_wrap(&vp, x, y,
861+
if (!(video_driver_translate_coord_viewport_confined_wrap(&vp, x, y,
922862
&res_x, &res_y, &res_screen_x, &res_screen_y)))
923863
return 0;
924864

@@ -928,9 +868,6 @@ static int16_t winraw_input_state(
928868
res_y = res_screen_y;
929869
}
930870

931-
if (!(inside = (res_x >= -0x7fff) && (res_y >= -0x7fff)))
932-
return 0;
933-
934871
switch (id)
935872
{
936873
case RETRO_DEVICE_ID_POINTER_X:
@@ -939,6 +876,8 @@ static int16_t winraw_input_state(
939876
return res_y;
940877
case RETRO_DEVICE_ID_POINTER_PRESSED:
941878
return pointer_down;
879+
case RETRO_DEVICE_ID_POINTER_IS_OFFSCREEN:
880+
return input_driver_pointer_is_offscreen(res_x, res_y);
942881
default:
943882
break;
944883
}
@@ -966,7 +905,7 @@ static int16_t winraw_input_state(
966905
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT:
967906
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE: /* deprecated */
968907
{
969-
unsigned new_id = winraw_retro_id_to_rarch(id);
908+
unsigned new_id = input_driver_lightgun_id_convert(id);
970909
const uint64_t bind_joykey = input_config_binds[port][new_id].joykey;
971910
const uint64_t bind_joyaxis = input_config_binds[port][new_id].joyaxis;
972911
const uint64_t autobind_joykey = input_autoconf_binds[port][new_id].joykey;

0 commit comments

Comments
 (0)