Skip to content

Commit

Permalink
Implemented Home button override
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Nov 3, 2024
1 parent 4cf9c53 commit 5793982
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 25 deletions.
31 changes: 27 additions & 4 deletions library/include/borealis/platforms/switch/switch_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
namespace brls
{

enum class ButtonOverrideMode {
NONE,
CUSTOM_EVENT,
GUIDE_BUTTON
};

// InputManager that uses the hid sysmodule to get inputs
class SwitchInputManager : public InputManager
{
Expand Down Expand Up @@ -57,16 +63,33 @@ class SwitchInputManager : public InputManager

void clearVibration(int controller);

bool isReplaceScreenshotWithGuideButton() { return replaceScreenshotWithGuideButton; }
void setReplaceScreenshotWithGuideButton(bool value) { replaceScreenshotWithGuideButton = value; }
/*
* It will not report about screenshot button state, if screenshotButtonOverrideMode equals NONE
*/
bool isScreenshotButtonPressed() { return m_isScreenshotPressed; }

/*
* It will not report about home button state, if homeButtonOverrideMode equals NONE
*/
bool isHomeButtonPressed() { return m_isHomePressed; }

ButtonOverrideMode screenshotButtonOverrideMode() { return m_screenshotButtonMode; }
void setScreenshotButtonOverrideMode(ButtonOverrideMode value) { m_screenshotButtonMode = value; }

ButtonOverrideMode homeButtonOverrideMode() { return m_homeButtonMode; }
void setHomeButtonOverrideMode(ButtonOverrideMode value) { m_homeButtonMode = value; }

private:
void screenshot_button_thread_fn(std::stop_token token);
bool replaceScreenshotWithGuideButton = false;
std::jthread screenshot_button_thread;

ButtonOverrideMode m_screenshotButtonMode = ButtonOverrideMode::NONE;
ButtonOverrideMode m_homeButtonMode = ButtonOverrideMode::NONE;

private:
bool isScreenshotPressed = false;
bool m_isScreenshotPressed = false;
bool m_isHomePressed = false;

bool cursorInited = false;
int cursorWidth, cursorHeight;
int cursorTexture = 0;
Expand Down
61 changes: 42 additions & 19 deletions library/lib/platforms/switch/switch_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <borealis/core/thread.hpp>
#include <borealis/core/application.hpp>
#include <borealis/platforms/switch/switch_input.hpp>
#include <borealis/core/application.hpp>

// Avoid namespace collision
#define NXEvent ::Event
Expand Down Expand Up @@ -236,8 +237,13 @@ void SwitchInputManager::updateControllerStateInner(ControllerState* state, PadS
state->buttons[i] = keysDown & switchKey;
}

if (replaceScreenshotWithGuideButton)
state->buttons[BUTTON_GUIDE] = isScreenshotPressed;
state->buttons[BUTTON_GUIDE] = false;

if (m_screenshotButtonMode == ButtonOverrideMode::GUIDE_BUTTON)
state->buttons[BUTTON_GUIDE] |= m_isScreenshotPressed;

if (m_homeButtonMode == ButtonOverrideMode::GUIDE_BUTTON)
state->buttons[BUTTON_GUIDE] |= m_isHomePressed;

HidAnalogStickState analog_stick_l = padGetStickPos(pad, 0);
HidAnalogStickState analog_stick_r = padGetStickPos(pad, 1);
Expand Down Expand Up @@ -648,50 +654,67 @@ void SwitchInputManager::screenshot_button_thread_fn(std::stop_token token) {
return;
}

NXEvent home_evt;
DEFER([&home_evt] { eventClose(&home_evt); });
if (auto rc = hidsysAcquireHomeButtonEventHandle(&home_evt, false); R_FAILED(rc)) {
Logger::error("Failed to acquire the home button event: {}\n", rc);
return;
}

NXEvent activity_evt;
DEFER([&activity_evt] { eventClose(&activity_evt); });
if (auto rc = inssGetWritableEvent(0, &activity_evt); R_FAILED(rc)) {
Logger::error("Failed to acquire the activity event: {}\n", rc);
return;
}

UTimer activity_timer;
DEFER([&activity_timer] { utimerStop(&activity_timer); });

utimerCreate(&activity_timer, std::chrono::nanoseconds(500ms).count(), TimerType_Repeating);
utimerStart(&activity_timer);

eventClear(&screenshot_evt);
eventClear(&home_evt);

u64 screenshot_down_start_tick = 0;
u64 home_down_start_tick = 0;

u64 down_start_tick = 0;
while (!token.stop_requested()) {
s32 idx;
auto rc = waitMulti(&idx, std::chrono::nanoseconds(50ms).count(),
waiterForEvent(&screenshot_evt), waiterForUTimer(&activity_timer));

if (!this->replaceScreenshotWithGuideButton)
continue;
waiterForEvent(&screenshot_evt), waiterForEvent(&home_evt));

if (rc == KERNELRESULT(TimedOut))
continue;

switch (idx) {
case 0: // Screenshot button
if (!this->replaceScreenshotWithGuideButton)
if (this->m_screenshotButtonMode == ButtonOverrideMode::NONE)
break;

eventClear(&screenshot_evt);

if (!down_start_tick) {
down_start_tick = armGetSystemTick();
isScreenshotPressed = true;
if (!screenshot_down_start_tick) {
screenshot_down_start_tick = armGetSystemTick();
m_isScreenshotPressed = true;
Logger::info("Screenshot button clicked");
} else {
down_start_tick = 0;
isScreenshotPressed = false;
screenshot_down_start_tick = 0;
m_isScreenshotPressed = false;
Logger::info("Screenshot button released");
}
break;
case 1: // Home button
if (this->m_homeButtonMode == ButtonOverrideMode::NONE)
break;

eventClear(&home_evt);

if (!home_down_start_tick) {
home_down_start_tick = armGetSystemTick();
m_isHomePressed = true;
Logger::info("Home button clicked");
} else {
home_down_start_tick = 0;
m_isHomePressed = false;
Logger::info("Home button released");
}
break;
default:
break;
}
Expand Down
4 changes: 2 additions & 2 deletions library/lib/platforms/switch/switch_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ void userAppInit()
socketInitialize(&cfg);
}

#ifdef DEBUG
// #ifdef DEBUG
nxlink_sock = nxlinkStdio();
#endif
// #endif

romfsInit();
hidsysInitialize();
Expand Down

0 comments on commit 5793982

Please sign in to comment.