Skip to content

Commit

Permalink
Implemented Screenshot button override as Guide button
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Nov 2, 2024
1 parent cacf8f4 commit 9668dc8
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
2 changes: 1 addition & 1 deletion library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ message(STATUS "borealis driver ${BOREALIS_DRIVER}")
# borealis Library
add_library(borealis STATIC ${BOREALIS_SRC})
set_target_properties(borealis PROPERTIES
CXX_STANDARD 17
CXX_STANDARD 20
UNITY_BUILD ${BRLS_UNITY_BUILD}
)
set_source_files_properties(${BOREALIS_PATH}/lib/platforms/desktop/desktop_darwin.mm PROPERTIES
Expand Down
26 changes: 26 additions & 0 deletions library/include/borealis/core/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,30 @@ bool startsWith(const std::string& data, const std::string& prefix);

std::string loadFileContents(const std::string& path);

#define _BR_CAT(x, y) x ## y
#define BR_CAT(x, y) _BR_CAT(x, y)
#define BR_ANONYMOUS BR_CAT(var, __COUNTER__)
#define DEFER(f) auto BR_ANONYMOUS = ::brls::_Defer(f)

template <typename F>
struct _Defer {
[[nodiscard]] _Defer(F &&f): f(std::move(f)) { }

_Defer(const _Defer &) = delete;
_Defer &operator =(const _Defer &) = delete;

~_Defer() {
if (this->want_run)
this->f();
}

void cancel() {
this->want_run = false;
}

private:
bool want_run = true;
F f;
};

} // namespace brls
12 changes: 11 additions & 1 deletion library/include/borealis/platforms/switch/switch_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <switch.h>

#include <stop_token>
#include <thread>
#include <borealis/core/input.hpp>

#define TOUCHES_MAX 10
Expand Down Expand Up @@ -53,10 +55,18 @@ class SwitchInputManager : public InputManager

void drawCursor(NVGcontext* vg) override;


void clearVibration(int controller);

bool getReplaceScreenshotWithGuideButton() { return replaceScreenshotWithGuideButton; }
void setReplaceScreenshotWithGuideButton(bool value) { replaceScreenshotWithGuideButton = value; }

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

private:
bool isScreenshotPressed = false;
bool cursorInited = false;
int cursorWidth, cursorHeight;
int cursorTexture = 0;
Expand Down
74 changes: 74 additions & 0 deletions library/lib/platforms/switch/switch_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@
limitations under the License.
*/

#include <chrono>
#include <borealis/core/thread.hpp>
#include <borealis/core/application.hpp>
#include <borealis/platforms/switch/switch_input.hpp>

// Avoid namespace collision
#define NXEvent ::Event

using namespace std::chrono_literals;

namespace brls
{

Expand Down Expand Up @@ -98,6 +105,8 @@ static const size_t SWITCH_AXIS_MAPPING[_AXES_MAX] = {

SwitchInputManager::SwitchInputManager()
{
this->screenshot_button_thread = std::jthread(&SwitchInputManager::screenshot_button_thread_fn, this);

padConfigureInput(GAMEPADS_MAX, HidNpadStyleSet_NpadStandard);
hidSetNpadJoyHoldType(HidNpadJoyHoldType_Horizontal);
hidSetNpadHandheldActivationMode(HidNpadHandheldActivationMode_Single);
Expand Down Expand Up @@ -131,6 +140,7 @@ SwitchInputManager::SwitchInputManager()

SwitchInputManager::~SwitchInputManager()
{
this->screenshot_button_thread.request_stop();
NVGcontext* vg = Application::getNVGContext();

if (this->cursorTexture != 0)
Expand Down Expand Up @@ -226,6 +236,9 @@ void SwitchInputManager::updateControllerStateInner(ControllerState* state, PadS
state->buttons[i] = keysDown & switchKey;
}

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

HidAnalogStickState analog_stick_l = padGetStickPos(pad, 0);
HidAnalogStickState analog_stick_r = padGetStickPos(pad, 1);

Expand Down Expand Up @@ -624,4 +637,65 @@ BrlsKeyboardScancode SwitchInputManager::switchKeyToGlfwKey(int key)
}
}

void SwitchInputManager::screenshot_button_thread_fn(std::stop_token token) {
// This needs a high priority because we are racing am to clear the event
svcSetThreadPriority(CUR_THREAD_HANDLE, 0x20);

NXEvent screenshot_evt;
DEFER([&screenshot_evt] { eventClose(&screenshot_evt); });
if (auto rc = hidsysAcquireCaptureButtonEventHandle(&screenshot_evt, false); R_FAILED(rc)) {
Logger::error("Failed to acquire the screenshot 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);

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;

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

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

eventClear(&screenshot_evt);

if (!down_start_tick) {
down_start_tick = armGetSystemTick();
isScreenshotPressed = true;
Logger::info("Screenshot button clicked");
} else {
down_start_tick = 0;
isScreenshotPressed = false;
Logger::info("Screenshot button released");
}
break;
default:
break;
}
}
}

} // namespace brls
4 changes: 4 additions & 0 deletions library/lib/platforms/switch/switch_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ void userAppInit()
#endif

romfsInit();
hidsysInitialize();
inssInitialize();
plInitialize(PlServiceType_User);
setsysInitialize();
setInitialize();
Expand All @@ -73,6 +75,8 @@ void userAppExit()
// system font
plExit();

inssExit();
hidsysExit();
romfsExit();

if (nxlink_sock != -1)
Expand Down

0 comments on commit 9668dc8

Please sign in to comment.