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 01302ba commit 3f80695
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 30 deletions.
3 changes: 1 addition & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
],
"defines": [
"__SWITCH__",
"PLATFORM_SWITCH",
"BOREALIS_USE_DEKO3D"
"PLATFORM_SWITCH"
],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
Expand Down
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
"valarray": "cpp",
"variant": "cpp",
"*.inc": "cpp",
"*.cpp2": "cpp"
"*.cpp2": "cpp",
"complex": "cpp",
"text_encoding": "cpp",
"cfenv": "cpp",
"typeindex": "cpp"
}
}
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ endif ()

# building target
program_target(${PROJECT_NAME} "${MAIN_SRC}")
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20)


# building release file
Expand Down Expand Up @@ -360,6 +360,9 @@ else ()
endif ()

if (USE_GL_RENDERER)
if (PLATFORM_MACOS)
set(SUPPORT_HDR ON)
endif ()
add_definitions(-DUSE_GL_RENDERER)
elseif (USE_METAL_RENDERER)
set(SUPPORT_HDR ON)
Expand Down
6 changes: 6 additions & 0 deletions app/include/ingame_overlay_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ class OptionsTab : public brls::Box {
private:
StreamingView* streamView;

std::string getTextFromButtons(std::vector<brls::ControllerButton> buttons);
NVGcolor getColorFromButtons(std::vector<brls::ControllerButton> buttons);
void setupButtonsSelectorCell(brls::DetailCell* cell, std::vector<brls::ControllerButton> buttons);

BRLS_BIND(brls::DetailCell, inputOverlayButton, "input_overlay");
BRLS_BIND(brls::SelectorCell, keyboardType, "keyboard_type");
BRLS_BIND(brls::SelectorCell, keyboardFingers, "keyboard_fingers");
BRLS_BIND(brls::BooleanCell, touchscreenMouseMode, "touchscreen_mouse_mode");
BRLS_BIND(brls::DetailCell, guideKeyButtons, "guide_key_buttons");
BRLS_BIND(brls::BooleanCell, guideByScreenshot, "guide_by_screenshot");
BRLS_BIND(brls::Header, volumeHeader, "volume_header");
BRLS_BIND(brls::Slider, volumeSlider, "volume_slider");
BRLS_BIND(brls::Header, rumbleForceHeader, "rumble_slider_header");
Expand Down
1 change: 1 addition & 0 deletions app/include/settings_tab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SettingsTab : public brls::Box {
BRLS_BIND(brls::Slider, rumbleForceSlider, "rumble_slider");
BRLS_BIND(brls::BooleanCell, swapStickToDpad, "swap_stick_to_dpad");
BRLS_BIND(brls::DetailCell, guideKeyButtons, "guide_key_buttons");
BRLS_BIND(brls::BooleanCell, guideByScreenshot, "guide_by_screenshot");
BRLS_BIND(brls::SelectorCell, overlayTime, "overlay_time");
BRLS_BIND(brls::DetailCell, overlayButtons, "overlay_buttons");
BRLS_BIND(brls::SelectorCell, mouseInputTime, "mouse_input_time");
Expand Down
2 changes: 1 addition & 1 deletion app/src/about_tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ AboutTab::AboutTab() {
std::string themePart =
variant == brls::ThemeVariant::DARK ? "_dark" : "_light";

std::string subtitle = fmt::format("about/version"_i18n, APP_VERSION);
std::string subtitle = fmt::format(fmt::runtime("about/version"_i18n), APP_VERSION);
versionLabel->setSubtitle(subtitle);

std::string githubLink = "https://github.com/XITRIX/Moonlight-Switch";
Expand Down
58 changes: 58 additions & 0 deletions app/src/ingame_overlay_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
// Created by Даниил Виноградов on 29.05.2021.
//

#ifdef PLATFORM_SWITCH
#include <borealis/platforms/switch/switch_input.hpp>
#endif

#include "ingame_overlay_view.hpp"
#include "streaming_input_overlay.hpp"
#include "button_selecting_dialog.hpp"
#include <libretro-common/retro_timers.h>

#include <iomanip>
Expand Down Expand Up @@ -79,6 +84,32 @@ LogoutTab::LogoutTab(StreamingView* streamView) : streamView(streamView) {
OptionsTab::OptionsTab(StreamingView* streamView) : streamView(streamView) {
this->inflateFromXMLRes("xml/views/ingame_overlay/options_tab.xml");

guideKeyButtons->setText("settings/guide_key_buttons"_i18n);
setupButtonsSelectorCell(guideKeyButtons,
Settings::instance().guide_key_options().buttons);
guideKeyButtons->registerClickAction([this](View* view) {
ButtonSelectingDialog* dialog = ButtonSelectingDialog::create(
"settings/guide_key_setup_message"_i18n, [this](auto buttons) {
auto options = Settings::instance().guide_key_options();
options.buttons = buttons;
Settings::instance().set_guide_key_options(options);
setupButtonsSelectorCell(guideKeyButtons, buttons);
});

dialog->open();
return true;
});

#ifndef PLATFORM_SWITCH
guideByScreenshot->removeFromSuperView();
#else
guideByScreenshot->init("settings/guide_by_screenshot"_i18n, Settings::instance().replace_screenshot_with_guide_button(),
[](bool value) {
Settings::instance().set_replace_screenshot_with_guide_button(value);
((SwitchInputManager*) brls::Application::getPlatform()->getInputManager())->setReplaceScreenshotWithGuideButton(Settings::instance().replace_screenshot_with_guide_button());
});
#endif

volumeHeader->setSubtitle(
std::to_string(Settings::instance().get_volume()) + "%");
float amplification =
Expand Down Expand Up @@ -181,3 +212,30 @@ OptionsTab::OptionsTab(StreamingView* streamView) : streamView(streamView) {
}

OptionsTab::~OptionsTab() { Settings::instance().save(); }

std::string
OptionsTab::getTextFromButtons(std::vector<ControllerButton> buttons) {
std::string buttonsText = "";
if (buttons.size() > 0) {
for (int i = 0; i < buttons.size(); i++) {
buttonsText += brls::Hint::getKeyIcon(buttons[i], true);
if (i < buttons.size() - 1)
buttonsText += " + ";
}
} else {
buttonsText = "hints/off"_i18n;
}
return buttonsText;
}

NVGcolor
OptionsTab::getColorFromButtons(std::vector<brls::ControllerButton> buttons) {
Theme theme = Application::getTheme();
return buttons.empty() ? theme["brls/text_disabled"]
: theme["brls/list/listItem_value_color"];
}

void OptionsTab::setupButtonsSelectorCell(brls::DetailCell* cell, std::vector<ControllerButton> buttons) {
cell->setDetailText(getTextFromButtons(buttons));
cell->setDetailTextColor(getColorFromButtons(buttons));
}
20 changes: 16 additions & 4 deletions app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Switch include only necessary for demo videos recording
#ifdef __SWITCH__
#include <switch.h>
#include <borealis/platforms/switch/switch_input.hpp>
#endif

#include <cstdlib>
Expand Down Expand Up @@ -43,11 +44,18 @@ int main(int argc, char* argv[]) {
appletInitializeGamePlayRecording();
appletSetWirelessPriorityMode(AppletWirelessPriorityMode_OptimizedForWlan);

extern u32 __nx_applet_type;
auto saved_applet_type = std::exchange(__nx_applet_type, AppletType_LibraryApplet);
// Keep the main thread above others so that the program stays responsive
// when doing software decoding
svcSetThreadPriority(CUR_THREAD_HANDLE, 0x20);

nvInitialize();
__nx_applet_type = saved_applet_type;
// auto at = appletGetAppletType();
// g_application_mode = at == AppletType_Application || at == AppletType_SystemApplication;

// // To get access to /dev/nvhost-nvjpg, we need nvdrv:{a,s,t}
// // However, nvdrv:{a,s} have limited address space for gpu mappings
// extern u32 __nx_nv_service_type, __nx_nv_transfermem_size;
// __nx_nv_service_type = NvServiceType_Factory;
// __nx_nv_transfermem_size = (g_application_mode ? 16 : 3) * 0x100000;
#endif

// Set log level
Expand All @@ -69,6 +77,10 @@ int main(int argc, char* argv[]) {
Settings::instance().set_working_dir(home);
brls::Logger::info("Working dir, {}", home);

#ifdef PLATFORM_SWITCH
((SwitchInputManager*) brls::Application::getPlatform()->getInputManager())->setReplaceScreenshotWithGuideButton(Settings::instance().replace_screenshot_with_guide_button());
#endif

// Have the application register an action on every activity that will quit
// when you press BUTTON_START
brls::Application::setGlobalQuit(false);
Expand Down
14 changes: 14 additions & 0 deletions app/src/settings_tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// Created by XITRIX on 26.05.2021.
//

#ifdef PLATFORM_SWITCH
#include <borealis/platforms/switch/switch_input.hpp>
#endif

#include "settings_tab.hpp"
#include "Settings.hpp"
#include "button_selecting_dialog.hpp"
Expand Down Expand Up @@ -307,6 +311,16 @@ SettingsTab::SettingsTab() {
return true;
});

#ifndef PLATFORM_SWITCH
guideByScreenshot->removeFromSuperView();
#else
guideByScreenshot->init("settings/guide_by_screenshot"_i18n, Settings::instance().replace_screenshot_with_guide_button(),
[](bool value) {
Settings::instance().set_replace_screenshot_with_guide_button(value);
((SwitchInputManager*) brls::Application::getPlatform()->getInputManager())->setReplaceScreenshotWithGuideButton(Settings::instance().replace_screenshot_with_guide_button());
});
#endif

overlayTime->init(
"settings/overlay_time"_i18n,
{"settings/overlay_zero_time"_i18n, "1", "2", "3", "4", "5"},
Expand Down
4 changes: 3 additions & 1 deletion app/src/streaming/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ GamepadState MoonlightInputManager::getControllerState(int controllerNum,
if (guideCombo ||
lastGamepadStates[controllerNum].buttonFlags & SPECIAL_FLAG)
gamepadState.buttonFlags = 0;
guideCombo ? (gamepadState.buttonFlags |= SPECIAL_FLAG)

bool guidePressed = guideCombo || controller.buttons[brls::BUTTON_GUIDE];
guidePressed ? (gamepadState.buttonFlags |= SPECIAL_FLAG)
: (gamepadState.buttonFlags &= ~SPECIAL_FLAG);

return gamepadState;
Expand Down
2 changes: 1 addition & 1 deletion app/src/streaming/MoonlightSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void MoonlightSession::connection_log_message(const char* format, ...) {
vsnprintf(buffer, size, format, arglist);
va_end(arglist);

brls::Logger::info(std::string(buffer));
brls::Logger::info(fmt::runtime(std::string(buffer)));
}

void MoonlightSession::connection_rumble(unsigned short controller,
Expand Down
32 changes: 15 additions & 17 deletions app/src/streaming/ffmpeg/FFmpegVideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,32 +169,22 @@ int FFmpegVideoDecoder::setup(int video_format, int width, int height,

// Need to align Switch frame to 256, need to de reviewed
#if defined(PLATFORM_SWITCH) && !defined(BOREALIS_USE_DEKO3D)
// int err = av_frame_get_buffer(m_frames[i], 256);
int err = av_frame_get_buffer(m_frames[i], 0);
int err = av_frame_get_buffer(m_frames[i], 256);
if (err < 0) {
char errs[64];
brls::Logger::error("FFmpeg: Couldn't allocate frame buffer: {}", av_make_error_string(errs, 64, err));
return -1;
}

// for (int j = 0; j < 2; j++) {
// uintptr_t ptr = (uintptr_t)m_frames[i]->data[j];
// uintptr_t dst = (((ptr)+(256)-1)&~((256)-1));
// uintptr_t gap = dst - ptr;
// m_frames[i]->data[j] += gap;
// }
for (int j = 0; j < 2; j++) {
uintptr_t ptr = (uintptr_t)m_frames[i]->data[j];
uintptr_t dst = (((ptr)+(256)-1)&~((256)-1));
uintptr_t gap = dst - ptr;
m_frames[i]->data[j] += gap;
}
#endif
}

#if defined(PLATFORM_SWITCH) && !defined(BOREALIS_USE_DEKO3D)
for (int j = 0; j < 2; j++) {
uintptr_t ptr = (uintptr_t)tmp_frame->data[j];
uintptr_t dst = (((ptr)+(256)-1)&~((256)-1));
uintptr_t gap = dst - ptr;
tmp_frame->data[j] += gap;
}
#endif

m_ffmpeg_buffer =
(char*)malloc(DECODER_BUFFER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
if (m_ffmpeg_buffer == NULL) {
Expand Down Expand Up @@ -385,6 +375,14 @@ AVFrame* FFmpegVideoDecoder::get_frame(bool native_frame) {
// Android already produce software Frame
resultFrame = decodeFrame;
#else

for (int i = 0; i < 2; ++i) {
if (((uintptr_t)resultFrame->data[i] & 0xff) || (resultFrame->linesize[i] & 0xff)) {
brls::Logger::error("Frame address/pitch not aligned to 256, falling back to cpu transfer");
break;
}
}

// Copy hardware frame into software frame
if ((err = av_hwframe_transfer_data(resultFrame, decodeFrame, 0)) < 0) {
char a[AV_ERROR_MAX_STRING_SIZE] = { 0 };
Expand Down
5 changes: 5 additions & 0 deletions app/src/utils/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ void Settings::load() {
}
}

if (json_t* replace_screenshot_with_guide_button = json_object_get(settings, "replace_screenshot_with_guide_button")) {
m_replace_screenshot_with_guide_button = json_typeof(replace_screenshot_with_guide_button) == JSON_TRUE;
}

if (json_t* buttons = json_object_get(settings, "guide_key_buttons")) {
m_guide_key_options.buttons.clear();
size_t size = json_array_size(buttons);
Expand Down Expand Up @@ -485,6 +489,7 @@ void Settings::save() {
json_object_set_new(settings, "current_mapping_layout", json_integer(m_current_mapping_layout));
json_object_set_new(settings, "keyboard_type", json_integer(m_keyboard_type));
json_object_set_new(settings, "keyboard_fingers", json_integer(m_keyboard_fingers));
json_object_set_new(settings, "replace_screenshot_with_guide_button", m_replace_screenshot_with_guide_button ? json_true() : json_false());

if (json_t* overlayButtons = json_array()) {
for (auto button: m_overlay_options.buttons) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/utils/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ class Settings : public Singleton<Settings> {
void set_swap_mouse_scroll(bool swap_mouse_scroll) { m_swap_mouse_scroll = swap_mouse_scroll; }
[[nodiscard]] bool swap_mouse_scroll() const { return m_swap_mouse_scroll; }

void set_replace_screenshot_with_guide_button(bool value) { m_replace_screenshot_with_guide_button = value; }
[[nodiscard]] bool replace_screenshot_with_guide_button() const { return m_replace_screenshot_with_guide_button; }

void set_guide_key_options(KeyComboOptions options) { m_guide_key_options = std::move(options); }
[[nodiscard]] KeyComboOptions guide_key_options() const { return m_guide_key_options; }

Expand Down Expand Up @@ -194,6 +197,7 @@ class Settings : public Singleton<Settings> {
int m_mouse_speed_multiplier = 34;
int m_current_mapping_layout = 0;
std::vector<KeyMappingLayout> m_mapping_laouts;
bool m_replace_screenshot_with_guide_button = false;
KeyComboOptions m_guide_key_options{
.holdTime = 0,
.buttons = {},
Expand Down
1 change: 1 addition & 0 deletions resources/i18n/en-US/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"debugging_view": "Show debugging view",
"decoder_threads": "Decoder Threads",
"fps": "FPS",
"guide_by_screenshot": "Use Screenshot button",
"guide_key": "Guide key (clicks immediately)",
"guide_key_buttons": "Buttons combination",
"guide_key_setup_message": "Press keys you'd like to use to press Guide button:\n\n",
Expand Down
3 changes: 2 additions & 1 deletion resources/i18n/ru/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
"keyboard_fullsized": "Полноразмерная",
"touchscreen_mouse_mode": "Touchscreen режим",
"keyboard_fingers": "Нажатий для открытия клавиатуры",
"request_hdr": "Запрашивать HDR Видео"
"request_hdr": "Запрашивать HDR Видео",
"guide_by_screenshot": "Использовать кнопку скриншота"
},
"app_list": {
"reload_app_list": "Перезагрузить",
Expand Down
3 changes: 3 additions & 0 deletions resources/xml/tabs/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
<brls:DetailCell
id="guide_key_buttons"/>

<brls:BooleanCell
id="guide_by_screenshot"/>

<brls:Header
title="@i18n/settings/overlay"
paddingTop="60"/>
Expand Down
10 changes: 10 additions & 0 deletions resources/xml/views/ingame_overlay/options_tab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@

<brls:BooleanCell
id="touchscreen_mouse_mode"/>

<brls:Header
title="@i18n/settings/guide_key"
paddingTop="60"/>

<brls:DetailCell
id="guide_key_buttons"/>

<brls:BooleanCell
id="guide_by_screenshot"/>

<brls:Header
id="volume_header"
Expand Down

0 comments on commit 3f80695

Please sign in to comment.