From f4968931914b7e3d2e923150fb8980121da1b74e Mon Sep 17 00:00:00 2001 From: Caroline Bell Date: Sat, 13 Jan 2024 09:57:55 -0600 Subject: [PATCH 1/2] [Switch] Implement LibNX psm.h for power_switch.cpp It handles everything but power_seconds_left. --- platform/switch/power_switch.cpp | 39 +++++++++++++++++++++++++------- platform/switch/power_switch.h | 15 +++++++----- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/platform/switch/power_switch.cpp b/platform/switch/power_switch.cpp index 51b40a9ac300..949baac9a6e6 100644 --- a/platform/switch/power_switch.cpp +++ b/platform/switch/power_switch.cpp @@ -29,24 +29,47 @@ /**************************************************************************/ #include "power_switch.h" + #include "switch_wrapper.h" PowerSwitch::PowerSwitch() { - nsecs_left = 0; - percent_left = 0; - power_state = OS::PowerState::POWERSTATE_ON_BATTERY; + if (R_SUCCEEDED(psmInitialize())) { + initialized = true; + } } -PowerSwitch::~PowerSwitch() {} - OS::PowerState PowerSwitch::get_power_state() { - return power_state; + if (!initialized) { + return OS::POWERSTATE_UNKNOWN; + } + + bool enough_power; + psmIsEnoughPowerSupplied(&enough_power); + + if (!enough_power) { + return OS::PowerState::POWERSTATE_ON_BATTERY; + } + + int percentage = PowerSwitch::get_power_percent_left(); + + if (percentage == 100) { + return OS::PowerState::POWERSTATE_CHARGED; + } + + return OS::PowerState::POWERSTATE_CHARGING; } int PowerSwitch::get_power_seconds_left() { - return nsecs_left / 1000000; + WARN_PRINT("power_seconds_left is not implemented on this platform, defaulting to -1"); + return -1; } int PowerSwitch::get_power_percent_left() { - return percent_left; + if (!initialized) { + return -1; + } + + u32 voltage_percentage; + psmGetBatteryChargePercentage(&voltage_percentage); + return (int)voltage_percentage; } diff --git a/platform/switch/power_switch.h b/platform/switch/power_switch.h index be83c6fc40e9..48767a72bc10 100644 --- a/platform/switch/power_switch.h +++ b/platform/switch/power_switch.h @@ -29,21 +29,24 @@ /**************************************************************************/ #pragma once + +#ifndef POWER_SWITCH_H +#define POWER_SWITCH_H + #include "core/os/os.h" class PowerSwitch { private: - int nsecs_left; - int percent_left; - OS::PowerState power_state; + bool initialized = false; bool GetPowerInfo_Switch(); public: - PowerSwitch(); - virtual ~PowerSwitch(); - OS::PowerState get_power_state(); int get_power_seconds_left(); int get_power_percent_left(); + + PowerSwitch(); }; + +#endif // POWER_SWITCH_H From 70a8e791b246ef67caab1d89cb774701595eff94 Mon Sep 17 00:00:00 2001 From: Caroline Bell Date: Tue, 23 Jan 2024 20:48:46 -0600 Subject: [PATCH 2/2] [Switch] Delete power_manager singleton It served little to no purpose. --- platform/switch/SCsub | 1 - platform/switch/os_switch.cpp | 38 +++++++++++++--- platform/switch/os_switch.h | 4 +- platform/switch/power_switch.cpp | 75 -------------------------------- platform/switch/power_switch.h | 52 ---------------------- 5 files changed, 34 insertions(+), 136 deletions(-) delete mode 100644 platform/switch/power_switch.cpp delete mode 100644 platform/switch/power_switch.h diff --git a/platform/switch/SCsub b/platform/switch/SCsub index 66e23ab11e87..e190389ec04d 100644 --- a/platform/switch/SCsub +++ b/platform/switch/SCsub @@ -7,7 +7,6 @@ Import("env") files = [ "godot_switch.cpp", "os_switch.cpp", - "power_switch.cpp", "joypad_switch.cpp", "context_gl_switch_egl.cpp", ] diff --git a/platform/switch/os_switch.cpp b/platform/switch/os_switch.cpp index 639d23f4d4ae..bc371992fc52 100644 --- a/platform/switch/os_switch.cpp +++ b/platform/switch/os_switch.cpp @@ -170,7 +170,9 @@ Error OS_Switch::initialize(const VideoMode &p_desired, int p_video_driver, int } joypad = memnew(JoypadSwitch(input)); - power_manager = memnew(PowerSwitch); + if (R_SUCCEEDED(psmInitialize())) { + OS_Switch::psm_initialized = true; + } AudioDriverManager::initialize(p_audio_driver); @@ -195,8 +197,8 @@ void OS_Switch::finalize() { memdelete(joypad); visual_server->finish(); memdelete(visual_server); - memdelete(power_manager); memdelete(gl_context); + psmExit(); } void OS_Switch::finalize_core() { @@ -505,15 +507,39 @@ void OS_Switch::hide_virtual_keyboard() { } OS::PowerState OS_Switch::get_power_state() { - return power_manager->get_power_state(); + if (!OS_Switch::psm_initialized) { + return OS::POWERSTATE_UNKNOWN; + } + + bool enough_power; + psmIsEnoughPowerSupplied(&enough_power); + + if (!enough_power) { + return OS::PowerState::POWERSTATE_ON_BATTERY; + } + + int percentage = OS_Switch::get_power_percent_left(); + + if (percentage == 100) { + return OS::PowerState::POWERSTATE_CHARGED; + } + + return OS::PowerState::POWERSTATE_CHARGING; } int OS_Switch::get_power_seconds_left() { - return power_manager->get_power_seconds_left(); + WARN_PRINT("power_seconds_left is not implemented on this platform, defaulting to -1"); + return -1; } int OS_Switch::get_power_percent_left() { - return power_manager->get_power_percent_left(); + if (!OS_Switch::psm_initialized) { + return -1; + } + + u32 voltage_percentage; + psmGetBatteryChargePercentage(&voltage_percentage); + return (int)voltage_percentage; } String OS_Switch::get_executable_path() const { @@ -554,8 +580,8 @@ OS_Switch::OS_Switch() { main_loop = nullptr; visual_server = nullptr; input = nullptr; - power_manager = nullptr; gl_context = nullptr; + AudioDriverManager::add_driver(&driver_audren); swkbdInlineCreate(&inline_keyboard); diff --git a/platform/switch/os_switch.h b/platform/switch/os_switch.h index 48cee058c7c2..b0d27361c177 100644 --- a/platform/switch/os_switch.h +++ b/platform/switch/os_switch.h @@ -34,7 +34,6 @@ #include "drivers/audren/audio_driver_audren.h" #include "joypad_switch.h" #include "main/input_default.h" -#include "power_switch.h" #include "servers/visual/visual_server_raster.h" class OS_Switch : public OS { @@ -43,7 +42,6 @@ class OS_Switch : public OS { VideoMode current_videomode; VisualServer *visual_server; InputDefault *input; - PowerSwitch *power_manager; ContextGLSwitchEGL *gl_context; JoypadSwitch *joypad; AudioDriverAudren driver_audren; @@ -51,6 +49,8 @@ class OS_Switch : public OS { SwkbdInline inline_keyboard; + bool psm_initialized = false; + protected: virtual void initialize_core(); virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); diff --git a/platform/switch/power_switch.cpp b/platform/switch/power_switch.cpp deleted file mode 100644 index 949baac9a6e6..000000000000 --- a/platform/switch/power_switch.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/**************************************************************************/ -/* power_switch.cpp */ -/**************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/**************************************************************************/ -/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ -/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/**************************************************************************/ - -#include "power_switch.h" - -#include "switch_wrapper.h" - -PowerSwitch::PowerSwitch() { - if (R_SUCCEEDED(psmInitialize())) { - initialized = true; - } -} - -OS::PowerState PowerSwitch::get_power_state() { - if (!initialized) { - return OS::POWERSTATE_UNKNOWN; - } - - bool enough_power; - psmIsEnoughPowerSupplied(&enough_power); - - if (!enough_power) { - return OS::PowerState::POWERSTATE_ON_BATTERY; - } - - int percentage = PowerSwitch::get_power_percent_left(); - - if (percentage == 100) { - return OS::PowerState::POWERSTATE_CHARGED; - } - - return OS::PowerState::POWERSTATE_CHARGING; -} - -int PowerSwitch::get_power_seconds_left() { - WARN_PRINT("power_seconds_left is not implemented on this platform, defaulting to -1"); - return -1; -} - -int PowerSwitch::get_power_percent_left() { - if (!initialized) { - return -1; - } - - u32 voltage_percentage; - psmGetBatteryChargePercentage(&voltage_percentage); - return (int)voltage_percentage; -} diff --git a/platform/switch/power_switch.h b/platform/switch/power_switch.h deleted file mode 100644 index 48767a72bc10..000000000000 --- a/platform/switch/power_switch.h +++ /dev/null @@ -1,52 +0,0 @@ -/**************************************************************************/ -/* power_switch.h */ -/**************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/**************************************************************************/ -/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ -/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/**************************************************************************/ - -#pragma once - -#ifndef POWER_SWITCH_H -#define POWER_SWITCH_H - -#include "core/os/os.h" - -class PowerSwitch { -private: - bool initialized = false; - - bool GetPowerInfo_Switch(); - -public: - OS::PowerState get_power_state(); - int get_power_seconds_left(); - int get_power_percent_left(); - - PowerSwitch(); -}; - -#endif // POWER_SWITCH_H