mirror of
https://github.com/switchbrew/libnx.git
synced 2025-07-04 02:22:15 +02:00
Merge d59b41e28a
into 5e9869cc41
This commit is contained in:
commit
cc89d88195
@ -310,7 +310,8 @@ typedef enum
|
||||
CONTROLLER_PLAYER_8 = 7,
|
||||
CONTROLLER_HANDHELD = 8,
|
||||
CONTROLLER_UNKNOWN = 9,
|
||||
CONTROLLER_P1_AUTO = 10, /// Not an actual HID-sysmodule ID. Only for hidKeys*(). Automatically uses CONTROLLER_PLAYER_1 when connected, otherwise uses CONTROLLER_HANDHELD.
|
||||
CONTROLLER_P1_AUTO = 10, /// Not an actual HID-sysmodule ID. Only for hidKeys*(). Switches the commanding controller between handheld and player 1 based on activity.
|
||||
CONTROLLER_ANY = 11, /// Not an actual HID-sysmodule ID. Only for hidKeys*(). Switches the commanding controller based on activity.
|
||||
} HidControllerID;
|
||||
|
||||
typedef struct touchPosition
|
||||
|
@ -23,11 +23,15 @@ static u64 g_mouseOld, g_mouseHeld, g_mouseDown, g_mouseUp;
|
||||
static u64 g_keyboardModOld, g_keyboardModHeld, g_keyboardModDown, g_keyboardModUp;
|
||||
static u32 g_keyboardOld[8], g_keyboardHeld[8], g_keyboardDown[8], g_keyboardUp[8];
|
||||
static u64 g_controllerOld[10], g_controllerHeld[10], g_controllerDown[10], g_controllerUp[10];
|
||||
static u64 g_controllerHeldStartTime[10], g_controllerStickHeldStartTime[10];
|
||||
|
||||
static HidControllerLayoutType g_controllerLayout[10];
|
||||
static u64 g_touchTimestamp, g_mouseTimestamp, g_keyboardTimestamp, g_controllerTimestamps[10];
|
||||
|
||||
static HidControllerID g_controllerP1AutoID;
|
||||
static HidControllerID g_controllerP1StickAutoID;
|
||||
static HidControllerID g_controllerAnyID;
|
||||
static HidControllerID g_controllerStickAnyID;
|
||||
|
||||
static RwLock g_hidLock;
|
||||
|
||||
@ -106,6 +110,9 @@ void hidReset(void)
|
||||
for (int i = 0; i < 10; i++)
|
||||
g_controllerOld[i] = g_controllerHeld[i] = g_controllerDown[i] = g_controllerUp[i] = 0;
|
||||
|
||||
memset(g_controllerHeldStartTime, 0, sizeof(g_controllerHeldStartTime));
|
||||
memset(g_controllerStickHeldStartTime, 0, sizeof(g_controllerStickHeldStartTime));
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
g_controllerLayout[i] = LAYOUT_DEFAULT;
|
||||
|
||||
@ -114,6 +121,9 @@ void hidReset(void)
|
||||
g_controllerTimestamps[i] = 0;
|
||||
|
||||
g_controllerP1AutoID = CONTROLLER_HANDHELD;
|
||||
g_controllerP1StickAutoID = CONTROLLER_HANDHELD;
|
||||
g_controllerAnyID = CONTROLLER_HANDHELD;
|
||||
g_controllerStickAnyID = CONTROLLER_HANDHELD;
|
||||
|
||||
rwlockWriteUnlock(&g_hidLock);
|
||||
}
|
||||
@ -215,17 +225,73 @@ void hidScanInput(void) {
|
||||
|
||||
g_controllerDown[i] = (~g_controllerOld[i]) & g_controllerHeld[i];
|
||||
g_controllerUp[i] = g_controllerOld[i] & (~g_controllerHeld[i]);
|
||||
|
||||
if (g_controllerDown[i]) {
|
||||
g_controllerHeldStartTime[i] = g_controllerTimestamps[i];
|
||||
} else if (g_controllerUp[i]) {
|
||||
g_controllerHeldStartTime[i] = 0;
|
||||
}
|
||||
|
||||
bool sticksActive = (g_controllerEntries[i].joysticks[JOYSTICK_LEFT].dx
|
||||
|| g_controllerEntries[i].joysticks[JOYSTICK_RIGHT].dx
|
||||
|| g_controllerEntries[i].joysticks[JOYSTICK_LEFT].dy
|
||||
|| g_controllerEntries[i].joysticks[JOYSTICK_RIGHT].dy);
|
||||
if (sticksActive && !g_controllerStickHeldStartTime[i]) {
|
||||
g_controllerStickHeldStartTime[i] = g_controllerTimestamps[i];
|
||||
} else if (!sticksActive && g_controllerStickHeldStartTime[i]){
|
||||
g_controllerStickHeldStartTime[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// For P1_AUTO and ANY, newer inputs > older inputs
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if (g_controllerHeldStartTime[i] > g_controllerHeldStartTime[g_controllerAnyID])
|
||||
g_controllerAnyID = i;
|
||||
}
|
||||
|
||||
if (g_controllerHeldStartTime[CONTROLLER_HANDHELD] > g_controllerHeldStartTime[CONTROLLER_PLAYER_1])
|
||||
g_controllerP1AutoID = CONTROLLER_HANDHELD;
|
||||
if (g_controllerEntries[CONTROLLER_PLAYER_1].connectionState & CONTROLLER_STATE_CONNECTED)
|
||||
else
|
||||
g_controllerP1AutoID = CONTROLLER_PLAYER_1;
|
||||
|
||||
// and older joystick values > newer joystick values
|
||||
bool noCommandingJoystick = true;
|
||||
bool noCommandingJoystickAuto = true;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if (!g_controllerStickHeldStartTime[i]) continue;
|
||||
noCommandingJoystick = false;
|
||||
|
||||
if (g_controllerStickHeldStartTime[i] < g_controllerStickHeldStartTime[g_controllerStickAnyID]
|
||||
|| !g_controllerStickHeldStartTime[g_controllerStickAnyID])
|
||||
g_controllerStickAnyID = i;
|
||||
}
|
||||
|
||||
int p1AutoIDs[2] = {CONTROLLER_HANDHELD, CONTROLLER_PLAYER_1};
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
int id = p1AutoIDs[i];
|
||||
if (!g_controllerStickHeldStartTime[id]) continue;
|
||||
noCommandingJoystickAuto = false;
|
||||
|
||||
if (g_controllerStickHeldStartTime[id] < g_controllerStickHeldStartTime[g_controllerP1StickAutoID]
|
||||
|| !g_controllerStickHeldStartTime[g_controllerP1StickAutoID])
|
||||
g_controllerP1StickAutoID = id;
|
||||
}
|
||||
|
||||
// Fall back to the commanding controller if no joysticks are held
|
||||
// Note: The commanding stick controller is what actually gets shown
|
||||
// in the home menu, album, etc in the lower left corner.
|
||||
if (noCommandingJoystick) g_controllerStickAnyID = g_controllerAnyID;
|
||||
if (noCommandingJoystickAuto) g_controllerP1StickAutoID = g_controllerP1AutoID;
|
||||
|
||||
rwlockWriteUnlock(&g_hidLock);
|
||||
}
|
||||
|
||||
u64 hidKeysHeld(HidControllerID id) {
|
||||
if (id==CONTROLLER_P1_AUTO) return hidKeysHeld(g_controllerP1AutoID);
|
||||
if (id==CONTROLLER_ANY) return hidKeysHeld(g_controllerAnyID);
|
||||
if (id < 0 || id > 9) return 0;
|
||||
|
||||
rwlockReadLock(&g_hidLock);
|
||||
@ -237,6 +303,7 @@ u64 hidKeysHeld(HidControllerID id) {
|
||||
|
||||
u64 hidKeysDown(HidControllerID id) {
|
||||
if (id==CONTROLLER_P1_AUTO) return hidKeysDown(g_controllerP1AutoID);
|
||||
if (id==CONTROLLER_ANY) return hidKeysDown(g_controllerAnyID);
|
||||
if (id < 0 || id > 9) return 0;
|
||||
|
||||
rwlockReadLock(&g_hidLock);
|
||||
@ -248,6 +315,7 @@ u64 hidKeysDown(HidControllerID id) {
|
||||
|
||||
u64 hidKeysUp(HidControllerID id) {
|
||||
if (id==CONTROLLER_P1_AUTO) return hidKeysUp(g_controllerP1AutoID);
|
||||
if (id==CONTROLLER_ANY) return hidKeysUp(g_controllerAnyID);
|
||||
if (id < 0 || id > 9) return 0;
|
||||
|
||||
rwlockReadLock(&g_hidLock);
|
||||
@ -355,7 +423,8 @@ void hidTouchRead(touchPosition *pos, u32 point_id) {
|
||||
}
|
||||
|
||||
void hidJoystickRead(JoystickPosition *pos, HidControllerID id, HidControllerJoystick stick) {
|
||||
if (id == CONTROLLER_P1_AUTO) return hidJoystickRead(pos, g_controllerP1AutoID, stick);
|
||||
if (id == CONTROLLER_P1_AUTO) return hidJoystickRead(pos, g_controllerP1StickAutoID, stick);
|
||||
if (id == CONTROLLER_ANY) return hidJoystickRead(pos, g_controllerStickAnyID, stick);
|
||||
|
||||
if (pos) {
|
||||
if (id < 0 || id > 9 || stick >= JOYSTICK_NUM_STICKS) {
|
||||
|
Loading…
Reference in New Issue
Block a user