Skip to content

Commit d712720

Browse files
committed
Add mouse-to-analog-stick input mode
with configurable sensitivity and button mappings.
1 parent bc43bce commit d712720

3 files changed

Lines changed: 252 additions & 0 deletions

File tree

custom/mupen64plus-core/plugin/emulate_game_controller_via_libretro.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ extern int l_cbutton;
4747
extern int d_cbutton;
4848
extern int u_cbutton;
4949
extern bool alternate_mapping;
50+
extern bool mouse_mode;
51+
extern int mouse_sensitivity_x;
52+
extern int mouse_sensitivity_y;
53+
extern int mouse_left_btn;
54+
extern int mouse_right_btn;
55+
extern int mouse_middle_btn;
5056
static bool libretro_supports_bitmasks = false;
5157

5258
extern m64p_rom_header ROM_HEADER;
@@ -278,6 +284,24 @@ EXPORT void CALL inputControllerCommand(int Control, unsigned char *Command)
278284
#define CSTICK_DOWN 0x400
279285

280286

287+
static void apply_mouse_button(BUTTONS* Keys, int btn_mapping)
288+
{
289+
switch (btn_mapping)
290+
{
291+
case 1: Keys->Z_TRIG = 1; break;
292+
case 2: Keys->A_BUTTON = 1; break;
293+
case 3: Keys->B_BUTTON = 1; break;
294+
case 4: Keys->L_TRIG = 1; break;
295+
case 5: Keys->R_TRIG = 1; break;
296+
case 6: Keys->START_BUTTON = 1; break;
297+
case 7: Keys->U_CBUTTON = 1; break;
298+
case 8: Keys->D_CBUTTON = 1; break;
299+
case 9: Keys->L_CBUTTON = 1; break;
300+
case 10: Keys->R_CBUTTON = 1; break;
301+
default: break;
302+
}
303+
}
304+
281305
static void inputGetKeys_reuse(int16_t analogX, int16_t analogY, int Control, BUTTONS* Keys)
282306
{
283307
double radius, angle;
@@ -306,6 +330,26 @@ static void inputGetKeys_reuse(int16_t analogX, int16_t analogY, int Control, BU
306330
Keys->X_AXIS = 0;
307331
Keys->Y_AXIS = 0;
308332
}
333+
334+
if (mouse_mode && Control == 0 && Keys->X_AXIS == 0 && Keys->Y_AXIS == 0) // Only player 1
335+
{
336+
int32_t stickX = (int32_t)(input_cb(Control, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X) * mouse_sensitivity_x / 50.0f);
337+
int32_t stickY = (int32_t)(input_cb(Control, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y) * mouse_sensitivity_y / 50.0f);
338+
339+
Keys->X_AXIS = (stickX > 80.0f) ? 80 : (stickX < -80.0f) ? -80 : (int32_t)stickX;
340+
Keys->Y_AXIS = (stickY > 80.0f) ? 80 : (stickY < -80.0f) ? -80 : (int32_t)stickY;
341+
342+
if (input_cb(Control, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT))
343+
apply_mouse_button(Keys, mouse_left_btn);
344+
if (input_cb(Control, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT))
345+
apply_mouse_button(Keys, mouse_right_btn);
346+
if (input_cb(Control, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_MIDDLE))
347+
apply_mouse_button(Keys, mouse_middle_btn);
348+
if (input_cb(Control, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_WHEELUP))
349+
apply_mouse_button(Keys, mouse_wheel_up_btn);
350+
if (input_cb(Control, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_WHEELDOWN))
351+
apply_mouse_button(Keys, mouse_wheel_down_btn);
352+
}
309353
}
310354

311355
void inputGetKeys_default( int Control, BUTTONS *Keys )

libretro/libretro.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ int l_cbutton;
124124
int d_cbutton;
125125
int u_cbutton;
126126
bool alternate_mapping;
127+
bool mouse_mode;
128+
int mouse_sensitivity_x;
129+
int mouse_sensitivity_y;
130+
int mouse_left_btn;
131+
int mouse_right_btn;
132+
int mouse_middle_btn;
127133

128134
static uint8_t* game_data = NULL;
129135
static uint32_t game_size = 0;
@@ -843,6 +849,60 @@ void update_controllers()
843849
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
844850
astick_sensitivity = atoi(var.value);
845851

852+
var.key = CORE_NAME "-mouse-mode";
853+
var.value = NULL;
854+
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
855+
mouse_mode = !strcmp(var.value, "True");
856+
857+
var.key = CORE_NAME "-mouse-sensitivity-x";
858+
var.value = NULL;
859+
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
860+
{
861+
int val = atoi(var.value);
862+
mouse_sensitivity_x = (val < -500) ? -500 : (val > 500) ? 500 : val;
863+
}
864+
865+
var.key = CORE_NAME "-mouse-sensitivity-y";
866+
var.value = NULL;
867+
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
868+
{
869+
int val = atoi(var.value);
870+
mouse_sensitivity_y = (val < -500) ? -500 : (val > 500) ? 500 : val;
871+
}
872+
873+
#define PARSE_MOUSE_BTN(val) \
874+
(!strcmp(val, "None") ? 0 : !strcmp(val, "Z") ? 1 : !strcmp(val, "A") ? 2 : \
875+
!strcmp(val, "B") ? 3 : !strcmp(val, "L") ? 4 : !strcmp(val, "R") ? 5 : \
876+
!strcmp(val, "Start") ? 6 : !strcmp(val, "C-Up") ? 7 : !strcmp(val, "C-Down") ? 8 : \
877+
!strcmp(val, "C-Left") ? 9 : !strcmp(val, "C-Right") ? 10 : 0)
878+
879+
var.key = CORE_NAME "-mouse-left";
880+
var.value = NULL;
881+
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
882+
mouse_left_btn = PARSE_MOUSE_BTN(var.value);
883+
884+
var.key = CORE_NAME "-mouse-right";
885+
var.value = NULL;
886+
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
887+
mouse_right_btn = PARSE_MOUSE_BTN(var.value);
888+
889+
var.key = CORE_NAME "-mouse-middle";
890+
var.value = NULL;
891+
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
892+
mouse_middle_btn = PARSE_MOUSE_BTN(var.value);
893+
894+
var.key = CORE_NAME "-mouse-wheel-up";
895+
var.value = NULL;
896+
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
897+
mouse_wheel_up_btn = PARSE_MOUSE_BTN(var.value);
898+
899+
var.key = CORE_NAME "-mouse-wheel-down";
900+
var.value = NULL;
901+
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
902+
mouse_wheel_down_btn = PARSE_MOUSE_BTN(var.value);
903+
904+
#undef PARSE_MOUSE_BTN
905+
846906
var.key = CORE_NAME "-r-cbutton";
847907
var.value = NULL;
848908
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)

libretro/libretro_core_options.h

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,154 @@ struct retro_core_option_v2_definition option_defs_us[] = {
15821582
},
15831583
"100"
15841584
},
1585+
{
1586+
CORE_NAME "-mouse-mode",
1587+
"Mouse to Analog Stick",
1588+
NULL,
1589+
"Use mouse input to control the N64 analog stick. Useful for FPS games.",
1590+
NULL,
1591+
"input",
1592+
{
1593+
{"False", "Disabled"},
1594+
{"True", "Enabled"},
1595+
{ NULL, NULL },
1596+
},
1597+
"False"
1598+
},
1599+
{
1600+
CORE_NAME "-mouse-sensitivity-x",
1601+
"Mouse Sensitivity X (percent)",
1602+
NULL,
1603+
"Horizontal mouse sensitivity. Negative values invert axis. Set to 0 to disable.",
1604+
NULL,
1605+
"input",
1606+
{
1607+
{"-500", NULL}, {"-400", NULL}, {"-300", NULL}, {"-250", NULL}, {"-200", NULL},
1608+
{"-175", NULL}, {"-150", NULL}, {"-125", NULL}, {"-100", NULL}, {"-75", NULL}, {"-50", NULL},
1609+
{"0", NULL},
1610+
{"50", NULL}, {"75", NULL}, {"100", NULL}, {"125", NULL}, {"150", NULL},
1611+
{"175", NULL}, {"200", NULL}, {"250", NULL}, {"300", NULL}, {"400", NULL}, {"500", NULL},
1612+
{ NULL, NULL },
1613+
},
1614+
"100"
1615+
},
1616+
{
1617+
CORE_NAME "-mouse-sensitivity-y",
1618+
"Mouse Sensitivity Y (percent)",
1619+
NULL,
1620+
"Vertical mouse sensitivity. Negative values invert axis. Set to 0 for Doom-like horizontal-only control.",
1621+
NULL,
1622+
"input",
1623+
{
1624+
{"-500", NULL}, {"-400", NULL}, {"-300", NULL}, {"-250", NULL}, {"-200", NULL},
1625+
{"-175", NULL}, {"-150", NULL}, {"-125", NULL}, {"-100", NULL}, {"-75", NULL}, {"-50", NULL},
1626+
{"0", NULL},
1627+
{"50", NULL}, {"75", NULL}, {"100", NULL}, {"125", NULL}, {"150", NULL},
1628+
{"175", NULL}, {"200", NULL}, {"250", NULL}, {"300", NULL}, {"400", NULL}, {"500", NULL},
1629+
{ NULL, NULL },
1630+
},
1631+
"-100"
1632+
},
1633+
{
1634+
CORE_NAME "-mouse-left",
1635+
"Mouse Left Click",
1636+
NULL,
1637+
"Map mouse left click to N64 button.",
1638+
NULL,
1639+
"input",
1640+
{
1641+
{"Z", "Z Trigger"},
1642+
{"A", "A Button"},
1643+
{"B", "B Button"},
1644+
{"L", "L Trigger"},
1645+
{"R", "R Trigger"},
1646+
{"Start", "Start"},
1647+
{"C-Up", NULL}, {"C-Down", NULL}, {"C-Left", NULL}, {"C-Right", NULL},
1648+
{"None", "Disabled"},
1649+
{ NULL, NULL },
1650+
},
1651+
"Z"
1652+
},
1653+
{
1654+
CORE_NAME "-mouse-right",
1655+
"Mouse Right Click",
1656+
NULL,
1657+
"Map mouse right click to N64 button.",
1658+
NULL,
1659+
"input",
1660+
{
1661+
{"A", "A Button"},
1662+
{"B", "B Button"},
1663+
{"Z", "Z Trigger"},
1664+
{"L", "L Trigger"},
1665+
{"R", "R Trigger"},
1666+
{"Start", "Start"},
1667+
{"C-Up", NULL}, {"C-Down", NULL}, {"C-Left", NULL}, {"C-Right", NULL},
1668+
{"None", "Disabled"},
1669+
{ NULL, NULL },
1670+
},
1671+
"A"
1672+
},
1673+
{
1674+
CORE_NAME "-mouse-middle",
1675+
"Mouse Middle Click",
1676+
NULL,
1677+
"Map mouse middle click to N64 button.",
1678+
NULL,
1679+
"input",
1680+
{
1681+
{"None", "Disabled"},
1682+
{"A", "A Button"},
1683+
{"B", "B Button"},
1684+
{"Z", "Z Trigger"},
1685+
{"L", "L Trigger"},
1686+
{"R", "R Trigger"},
1687+
{"Start", "Start"},
1688+
{"C-Up", NULL}, {"C-Down", NULL}, {"C-Left", NULL}, {"C-Right", NULL},
1689+
{ NULL, NULL },
1690+
},
1691+
"None"
1692+
},
1693+
{
1694+
CORE_NAME "-mouse-wheel-up",
1695+
"Mouse Wheel Up",
1696+
NULL,
1697+
"Map mouse wheel up to N64 button.",
1698+
NULL,
1699+
"input",
1700+
{
1701+
{"L", "L Trigger"},
1702+
{"R", "R Trigger"},
1703+
{"A", "A Button"},
1704+
{"B", "B Button"},
1705+
{"Z", "Z Trigger"},
1706+
{"Start", "Start"},
1707+
{"C-Up", NULL}, {"C-Down", NULL}, {"C-Left", NULL}, {"C-Right", NULL},
1708+
{"None", "Disabled"},
1709+
{ NULL, NULL },
1710+
},
1711+
"L"
1712+
},
1713+
{
1714+
CORE_NAME "-mouse-wheel-down",
1715+
"Mouse Wheel Down",
1716+
NULL,
1717+
"Map mouse wheel down to N64 button.",
1718+
NULL,
1719+
"input",
1720+
{
1721+
{"R", "R Trigger"},
1722+
{"L", "L Trigger"},
1723+
{"A", "A Button"},
1724+
{"B", "B Button"},
1725+
{"Z", "Z Trigger"},
1726+
{"Start", "Start"},
1727+
{"C-Up", NULL}, {"C-Down", NULL}, {"C-Left", NULL}, {"C-Right", NULL},
1728+
{"None", "Disabled"},
1729+
{ NULL, NULL },
1730+
},
1731+
"R"
1732+
},
15851733
{
15861734
CORE_NAME "-r-cbutton",
15871735
"Right C Button",

0 commit comments

Comments
 (0)