Skip to content

Commit b5a62fc

Browse files
authored
Add lightgun support to cocoa input driver (#17318)
1 parent a590c51 commit b5a62fc

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

input/drivers/cocoa_input.m

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,69 @@ static void cocoa_input_poll(void *data)
422422
}
423423
}
424424

425+
static int16_t cocoa_lightgun_aiming_state(
426+
cocoa_input_data_t *apple, unsigned idx, unsigned id)
427+
{
428+
struct video_viewport vp = {0};
429+
int16_t res_x = 0;
430+
int16_t res_y = 0;
431+
int16_t res_screen_x = 0;
432+
int16_t res_screen_y = 0;
433+
434+
int16_t x = apple->window_pos_x;
435+
int16_t y = apple->window_pos_y;
436+
437+
#ifndef IOS
438+
x *= cocoa_screen_get_backing_scale_factor();
439+
y *= cocoa_screen_get_backing_scale_factor();
440+
#endif
441+
442+
if (video_driver_translate_coord_viewport_wrap(
443+
&vp, x, y,
444+
&res_x, &res_y, &res_screen_x, &res_screen_y))
445+
{
446+
switch (id)
447+
{
448+
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X:
449+
return res_x;
450+
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y:
451+
return res_y;
452+
case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
453+
return input_driver_pointer_is_offscreen(res_x, res_y);
454+
default:
455+
break;
456+
}
457+
}
458+
459+
return 0;
460+
}
461+
462+
static bool cocoa_mouse_button_pressed(
463+
cocoa_input_data_t *apple, unsigned port, unsigned key)
464+
{
465+
switch (key)
466+
{
467+
case RETRO_DEVICE_ID_MOUSE_LEFT:
468+
return apple->mouse_buttons & 1;
469+
case RETRO_DEVICE_ID_MOUSE_RIGHT:
470+
return apple->mouse_buttons & 2;
471+
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
472+
case RETRO_DEVICE_ID_MOUSE_BUTTON_4:
473+
case RETRO_DEVICE_ID_MOUSE_BUTTON_5:
474+
return false;
475+
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
476+
return apple->mouse_wu;
477+
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
478+
return apple->mouse_wd;
479+
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP:
480+
return apple->mouse_wl;
481+
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN:
482+
return apple->mouse_wr;
483+
}
484+
485+
return false;
486+
}
487+
425488
static int16_t cocoa_input_state(
426489
void *data,
427490
const input_device_driver_t *joypad,
@@ -609,6 +672,64 @@ static int16_t cocoa_input_state(
609672
}
610673
}
611674
break;
675+
case RETRO_DEVICE_LIGHTGUN:
676+
switch (id)
677+
{
678+
/*aiming*/
679+
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X:
680+
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y:
681+
case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
682+
return cocoa_lightgun_aiming_state(apple, idx, id);
683+
/*buttons*/
684+
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
685+
case RETRO_DEVICE_ID_LIGHTGUN_RELOAD:
686+
case RETRO_DEVICE_ID_LIGHTGUN_AUX_A:
687+
case RETRO_DEVICE_ID_LIGHTGUN_AUX_B:
688+
case RETRO_DEVICE_ID_LIGHTGUN_AUX_C:
689+
case RETRO_DEVICE_ID_LIGHTGUN_START:
690+
case RETRO_DEVICE_ID_LIGHTGUN_SELECT:
691+
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_UP:
692+
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_DOWN:
693+
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_LEFT:
694+
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT:
695+
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
696+
{
697+
unsigned new_id = input_driver_lightgun_id_convert(id);
698+
const uint64_t bind_joykey = input_config_binds[port][new_id].joykey;
699+
const uint64_t bind_joyaxis = input_config_binds[port][new_id].joyaxis;
700+
const uint64_t autobind_joykey = input_autoconf_binds[port][new_id].joykey;
701+
const uint64_t autobind_joyaxis= input_autoconf_binds[port][new_id].joyaxis;
702+
uint16_t joyport = joypad_info->joy_idx;
703+
float axis_threshold = joypad_info->axis_threshold;
704+
const uint64_t joykey = (bind_joykey != NO_BTN) ? bind_joykey : autobind_joykey;
705+
const uint32_t joyaxis = (bind_joyaxis != AXIS_NONE) ? bind_joyaxis : autobind_joyaxis;
706+
707+
if (binds[port][new_id].valid)
708+
{
709+
if ((uint16_t)joykey != NO_BTN && joypad->button(joyport, (uint16_t)joykey))
710+
return 1;
711+
if (joyaxis != AXIS_NONE &&
712+
((float)abs(joypad->axis(joyport, joyaxis))
713+
/ 0x8000) > axis_threshold)
714+
return 1;
715+
else if ((binds[port][new_id].key && binds[port][new_id].key < RETROK_LAST)
716+
&& !keyboard_mapping_blocked
717+
&& apple_key_state[rarch_keysym_lut[(enum retro_key)binds[port][new_id].key]])
718+
return 1;
719+
else
720+
{
721+
settings_t *settings = config_get_ptr();
722+
if (settings->uints.input_mouse_index[port] == 0)
723+
{
724+
if (cocoa_mouse_button_pressed(apple, port, binds[port][new_id].mbutton))
725+
return 1;
726+
}
727+
}
728+
}
729+
}
730+
break;
731+
}
732+
break;
612733
}
613734

614735
return 0;
@@ -634,6 +755,7 @@ static uint64_t cocoa_input_get_capabilities(void *data)
634755
(1 << RETRO_DEVICE_JOYPAD)
635756
| (1 << RETRO_DEVICE_MOUSE)
636757
| (1 << RETRO_DEVICE_KEYBOARD)
758+
| (1 << RETRO_DEVICE_LIGHTGUN)
637759
| (1 << RETRO_DEVICE_POINTER)
638760
| (1 << RETRO_DEVICE_ANALOG);
639761
}

0 commit comments

Comments
 (0)