This commit is contained in:
souldbminersmwc
2025-12-23 19:10:41 -05:00
parent 152f92a9a9
commit b5022ea839
43 changed files with 5143 additions and 3939 deletions

View File

@@ -76,6 +76,7 @@ typedef enum
SysClkModule_CPU = 0,
SysClkModule_GPU,
SysClkModule_MEM,
HorizonOCModule_Governor,
SysClkModule_EnumMax,
} SysClkModule;
@@ -129,8 +130,10 @@ static inline const char* sysclkFormatModule(SysClkModule module, bool pretty)
return pretty ? "GPU" : "gpu";
case SysClkModule_MEM:
return pretty ? "Memory" : "mem";
case HorizonOCModule_Governor:
return pretty ? "Governor" : "gov";
default:
return NULL;
return "null";
}
}

View File

@@ -50,9 +50,6 @@ typedef enum {
HocClkConfigValue_ThermalThrottle,
HocClkConfigValue_ThermalThrottleThreshold,
HocClkConfigValue_HandheldGovernor,
HocClkConfigValue_DockedGovernor,
HocClkConfigValue_HandheldTDP,
HocClkConfigValue_HandheldTDPLimit,
@@ -212,11 +209,6 @@ static inline const char* sysclkFormatConfigValue(SysClkConfigValue val, bool pr
case HocClkConfigValue_ThermalThrottleThreshold:
return pretty ? "Thermal Throttle Threshold" : "thermal_throttle_threshold";
case HocClkConfigValue_HandheldGovernor:
return pretty ? "Handheld Governor" : "governor";
case HocClkConfigValue_DockedGovernor:
return pretty ? "Docked Governor" : "governor_docked";
case HocClkConfigValue_HandheldTDP:
return pretty ? "Handheld TDP" : "handheld_tdp";
@@ -422,8 +414,6 @@ static inline uint64_t sysclkDefaultConfigValue(SysClkConfigValue val)
return 1862ULL;
case HocClkConfigValue_ThermalThrottle:
case HocClkConfigValue_DockedGovernor:
case HocClkConfigValue_HandheldGovernor:
case HocClkConfigValue_HandheldTDP:
case HocClkConfigValue_EnforceBoardLimit:
case HocClkConfigValue_KipEditing:
@@ -464,8 +454,6 @@ static inline uint64_t sysclkValidConfigValue(SysClkConfigValue val, uint64_t in
case HocClkConfigValue_UncappedClocks:
case HocClkConfigValue_OverwriteBoostMode:
case HocClkConfigValue_ThermalThrottle:
case HocClkConfigValue_DockedGovernor:
case HocClkConfigValue_HandheldGovernor:
case HocClkConfigValue_HandheldTDP:
case HocClkConfigValue_EnforceBoardLimit:
case HocClkConfigValue_KipEditing:

View File

@@ -78,7 +78,7 @@ Together, these flags (-ffunction-sections, -fdata-sections, -Wl,--gc-sections,
- [Status Monitor Overlay](https://github.com/ppkantorski/Status-Monitor-Overlay)
- [Edizon Overlay](https://github.com/ppkantorski/EdiZon-Overlay)
- [Edizon Overlay](https://github.com/proferabg/EdiZon-Overlay)
- [Sysmodules](https://github.com/ppkantorski/ovl-sysmodules)
@@ -208,4 +208,4 @@ Contributions are welcome! If you have any ideas, suggestions, or bug reports, p
This project is licensed and distributed under [GPLv2](LICENSE) with a [custom library](libultra) utilizing [CC-BY-4.0](SUB_LICENSE).
Copyright (c) 2024 ppkantorski
Copyright (c) 2023-2025 ppkantorski

View File

@@ -1520,140 +1520,85 @@ static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, in
// Force inline for maximum performance
static inline int stbtt_FindGlyphIndex_impl(stbtt_uint8 *data, stbtt_uint32 index_map, int unicode_codepoint)
{
// Read format once - single memory access
stbtt_uint16 format = FAST_USHORT(data + index_map);
stbtt_uint32 uc = (stbtt_uint32)unicode_codepoint;
// Variables used across multiple cases or frequently in loops
stbtt_uint32 low, high, mid;
stbtt_uint8 *group;
stbtt_uint32 start_char, end_char;
stbtt_uint32 uc;
// Switch for jump table optimization
switch (format) {
case 4: { // Windows fonts - most common
// Early exit for out-of-range Unicode
if ((unsigned)unicode_codepoint > 0xffff) return 0;
case 4: {
if (uc > 0xffff) return 0;
// Cache all header values in one go - burst read
stbtt_uint8 *header = data + index_map;
stbtt_uint16 segcount = FAST_USHORT(header + 6) >> 1;
stbtt_uint16 searchRange = FAST_USHORT(header + 8) >> 1;
stbtt_uint16 entrySelector = FAST_USHORT(header + 10);
stbtt_uint16 rangeShift = FAST_USHORT(header + 12) >> 1;
// Optimized binary search with fewer memory accesses
stbtt_uint32 endCount = index_map + 14;
stbtt_uint32 search = endCount;
// Use the precomputed rangeShift for initial jump
if (unicode_codepoint >= FAST_USHORT(data + search + (rangeShift << 1)))
if (uc >= (stbtt_uint16)FAST_USHORT(data + search + (rangeShift << 1)))
search += rangeShift << 1;
search -= 2;
stbtt_uint16 entrySelector = FAST_USHORT(header + 10);
stbtt_uint16 searchRange = FAST_USHORT(header + 8) >> 1;
// Unrolled binary search - most critical path
while (entrySelector) {
searchRange >>= 1;
stbtt_uint32 test_pos = search + (searchRange << 1);
if (unicode_codepoint > FAST_USHORT(data + test_pos))
if (uc > (stbtt_uint16)FAST_USHORT(data + test_pos))
search = test_pos;
--entrySelector;
}
search += 2;
stbtt_uint16 item = (stbtt_uint16)((search - endCount) >> 1);
stbtt_uint32 base = index_map + 14;
// Calculate all offsets upfront - better instruction scheduling
stbtt_uint32 base1 = index_map + 14;
stbtt_uint32 startCode_offset = base1 + (segcount << 1) + 2 + (item << 1);
stbtt_uint32 endCode_offset = endCount + (item << 1);
stbtt_uint32 idRangeOffset_offset = base1 + (segcount * 6) + 2 + (item << 1);
stbtt_uint16 start = FAST_USHORT(data + base + (segcount << 1) + 2 + (item << 1));
stbtt_uint16 end = FAST_USHORT(data + endCount + (item << 1));
stbtt_uint16 start = FAST_USHORT(data + startCode_offset);
stbtt_uint16 end = FAST_USHORT(data + endCode_offset);
// Single comparison using unsigned arithmetic trick
if ((unsigned)(unicode_codepoint - start) > (unsigned)(end - start))
if ((stbtt_uint32)(uc - start) > (stbtt_uint32)(end - start))
return 0;
stbtt_uint16 offset = FAST_USHORT(data + idRangeOffset_offset);
if (offset == 0) {
stbtt_uint32 idDelta_offset = base1 + (segcount << 2) + 2 + (item << 1);
return (stbtt_uint16)(unicode_codepoint + FAST_SHORT(data + idDelta_offset));
}
stbtt_uint16 offset = FAST_USHORT(data + base + (segcount * 6) + 2 + (item << 1));
if (offset == 0)
return (stbtt_uint16)(uc + FAST_SHORT(data + base + (segcount << 2) + 2 + (item << 1)));
return FAST_USHORT(data + offset + ((unicode_codepoint - start) << 1) + idRangeOffset_offset);
return FAST_USHORT(data + offset + ((uc - start) << 1) + base + (segcount * 6) + 2 + (item << 1));
}
case 12: { // 32-bit format
stbtt_uint32 ngroups = FAST_ULONG(data + index_map + 12);
uc = (stbtt_uint32)unicode_codepoint;
// Optimized binary search with minimal memory access
low = 0; high = ngroups;
case 12:
case 13: {
stbtt_uint32 low = 0, high = FAST_ULONG(data + index_map + 12);
stbtt_uint8 *groups_base = data + index_map + 16;
while (low < high) {
mid = (low + high) >> 1;
group = groups_base + (mid * 12);
stbtt_uint32 mid = (low + high) >> 1;
stbtt_uint8 *group = groups_base + (mid * 12);
stbtt_uint32 start_char = FAST_ULONG(group);
start_char = FAST_ULONG(group);
if (uc < start_char) {
high = mid;
} else if (uc <= FAST_ULONG(group + 4)) {
stbtt_uint32 start_glyph = FAST_ULONG(group + 8);
return (format == 12) ? (start_glyph + uc - start_char) : start_glyph;
} else {
end_char = FAST_ULONG(group + 4);
if (uc <= end_char) {
stbtt_uint32 start_glyph = FAST_ULONG(group + 8);
return start_glyph + uc - start_char;
}
low = mid + 1;
}
}
return 0;
}
case 13: { // 32-bit format, many-to-one mapping
stbtt_uint32 ngroups = FAST_ULONG(data + index_map + 12);
uc = (stbtt_uint32)unicode_codepoint;
low = 0; high = ngroups;
stbtt_uint8 *groups_base = data + index_map + 16;
while (low < high) {
mid = (low + high) >> 1;
group = groups_base + (mid * 12);
start_char = FAST_ULONG(group);
if (uc < start_char) {
high = mid;
} else {
end_char = FAST_ULONG(group + 4);
if (uc <= end_char) {
return FAST_ULONG(group + 8); // Same glyph for all chars in range
}
low = mid + 1;
}
}
return 0;
}
case 0:
return (uc < (stbtt_uint32)(FAST_USHORT(data + index_map + 2) - 6)) ?
data[index_map + 6 + uc] : 0;
case 0: { // Apple byte encoding - simple and fast
stbtt_int32 bytes = FAST_USHORT(data + index_map + 2);
return ((unsigned)unicode_codepoint < (unsigned)(bytes - 6)) ?
data[index_map + 6 + unicode_codepoint] : 0;
}
case 6: { // Trimmed table mapping
stbtt_uint32 first = FAST_USHORT(data + index_map + 6);
stbtt_uint32 count = FAST_USHORT(data + index_map + 8);
uc = (stbtt_uint32)unicode_codepoint;
stbtt_uint32 offset = uc - first;
return (offset < count) ? FAST_USHORT(data + index_map + 10 + (offset << 1)) : 0;
case 6: {
stbtt_uint32 offset = uc - FAST_USHORT(data + index_map + 6);
return (offset < (stbtt_uint32)FAST_USHORT(data + index_map + 8)) ?
FAST_USHORT(data + index_map + 10 + (offset << 1)) : 0;
}
default:
return 0; // Unsupported format
return 0;
}
}

View File

@@ -1,8 +1,8 @@
/********************************************************************************
* File: audio_player.hpp
* File: audio.hpp
* Author: ppkantorski
* Description:
* This header defines the AudioPlayer class and related structures used for
* This header defines the Audio class and related structures used for
* handling sound playback within the Ultrahand Overlay. It provides interfaces
* for loading, caching, and playing WAV audio through libnxs audout service,
* along with basic sound type management and synchronization support.
@@ -25,9 +25,10 @@
#include <atomic>
#include <cstring>
#include <mutex>
#include "tsl_utils.hpp"
namespace ult {
class AudioPlayer {
class Audio {
public:
enum class SoundType : uint8_t { // <- uint8_t saves space
Navigate,
@@ -64,7 +65,7 @@ namespace ult {
static void setMasterVolume(float volume);
static void setEnabled(bool enabled);
static bool isEnabled();
static bool isDocked();
//static bool isDocked();
static bool reloadIfDockedChanged();
static void reloadAllSounds();
static void unloadAllSounds(const std::initializer_list<SoundType>& excludeSounds = {});

View File

@@ -12,7 +12,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#pragma once

View File

@@ -13,7 +13,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#pragma once

View File

@@ -14,7 +14,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#pragma once

View File

@@ -13,7 +13,7 @@
* altered or removed.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
@@ -39,6 +39,11 @@ namespace ult {
extern const std::string ROOT_PATH;
extern const std::string SETTINGS_PATH;
extern const std::string NX_OVLLOADER_PATH;
extern const std::string OVL_HEAP_CONFIG_PATH;
extern const std::string OVL_EXIT_FLAG_PATH;
extern const std::string ULTRAHAND_CONFIG_INI_PATH;
extern const std::string TESLA_CONFIG_INI_PATH;
extern const std::string LANG_PATH;
@@ -63,7 +68,8 @@ namespace ult {
extern const std::string PACKAGE_FILENAME;
extern const std::string DOWNLOADS_PATH;
extern const std::string EXPANSION_PATH;
//extern const std::string EXPANSION_PATH;
extern const std::string FUSE_DATA_INI_PATH;
extern const std::string PACKAGE_PATH;
extern const std::string OVERLAY_PATH;
@@ -77,12 +83,13 @@ namespace ult {
extern const std::string ULTRAHAND_REPO_URL;
extern const std::string INCLUDED_THEME_FOLDER_URL;
extern const std::string LATEST_RELEASE_INFO_URL;
extern const std::string NX_OVLLOADER_ZIP_URL;
extern const std::string NX_OVLLOADER_PLUS_ZIP_URL;
extern const std::string LATEST_UPDATER_INI_URL;
//extern const std::string NX_OVLLOADER_ZIP_URL;
//extern const std::string NX_OVLLOADER_PLUS_ZIP_URL;
extern const std::string OLD_NX_OVLLOADER_ZIP_URL;
extern const std::string OLD_NX_OVLLOADER_PLUS_ZIP_URL;
//extern const std::string OLD_NX_OVLLOADER_PLUS_ZIP_URL;
extern const std::string UPDATER_PAYLOAD_URL;
extern const std::string SOUND_EFFECTS_URL;
//extern const std::string SOUND_EFFECTS_URL;
extern const std::string LAUNCH_ARGS_STR;
extern const std::string USE_LAUNCH_ARGS_STR;
@@ -137,6 +144,7 @@ namespace ult {
extern const std::string FALSE_STR;
extern const std::string GLOBAL_STR;
extern const std::string DEFAULT_STR;
extern const std::string HOLD_STR;
extern const std::string SLOT_STR;
extern const std::string OPTION_STR;
extern const std::string FORWARDER_STR;
@@ -161,6 +169,7 @@ namespace ult {
extern const std::string INPROGRESS_SYMBOL;
extern const std::string STAR_SYMBOL;
extern const std::string DIVIDER_SYMBOL;
extern const std::string NOTIFY_HEADER;
extern const std::vector<std::string> THROBBER_SYMBOLS;

View File

@@ -26,12 +26,12 @@
namespace ult {
//extern bool rumbleInitialized;
extern std::atomic<bool> rumbleActive;
extern std::atomic<bool> clickActive;
extern std::atomic<bool> doubleClickActive;
//void initRumble();
void deinitRumble();
void checkAndReinitRumble();
void initHaptics();
void deinitHaptics();
void checkAndReinitHaptics();
void rumbleClick();
void rumbleDoubleClick();

View File

@@ -13,7 +13,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#pragma once
@@ -206,6 +206,9 @@ namespace ult {
std::string extractVersionFromBinary(const std::string &filePath);
std::string decodeBase64ToString(const std::string& b64);
}
#endif

View File

@@ -13,7 +13,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#pragma once
@@ -290,7 +290,7 @@ namespace ult {
//}
void syncIniValue(std::map<std::string, std::map<std::string, std::string>>& packageConfigData,
bool syncIniValue(std::map<std::string, std::map<std::string, std::string>>& packageConfigData,
const std::string& packageConfigIniPath,
const std::string& optionName,
const std::string& key,
@@ -339,6 +339,36 @@ namespace ult {
* @param data The complete INI data structure to save.
*/
void saveIniFileData(const std::string& filePath, const std::map<std::string, std::map<std::string, std::string>>& data);
/**
* @brief Adds a key-value pair to all sections that contain a specified pattern key.
*
* If patternKey is empty, the key-value pair will be added to ALL sections.
* If patternKey is specified, only sections containing that key will be modified.
*
* @param filePath The path to the INI file.
* @param patternKey The key to search for (empty = all sections).
* @param newKey The new key to add.
* @param newValue The value for the new key.
*/
void addKeyToMatchingSections(const std::string& filePath, const std::string& patternKey,
const std::string& newKey, const std::string& newValue);
/**
* @brief Removes a key from all sections that contain a specified pattern key.
*
* If patternKey is empty, the key will be removed from ALL sections.
* If patternKey is specified, only sections containing that key will have keyToRemove deleted.
*
* @param filePath The path to the INI file.
* @param patternKey The key to search for (empty = all sections).
* @param keyToRemove The key to remove from matching sections.
*/
void removeKeyFromMatchingSections(const std::string& filePath, const std::string& patternKey,
const std::string& keyToRemove);
}
#endif

View File

@@ -12,8 +12,9 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#pragma once
#ifndef JSON_FUNCS_HPP
#define JSON_FUNCS_HPP

View File

@@ -14,7 +14,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
@@ -70,7 +70,7 @@ namespace ult {
// Function to read file into a vector of strings
std::vector<std::string> readListFromFile(const std::string& filePath, size_t maxLines=0);
std::vector<std::string> readListFromFile(const std::string& filePath, size_t maxLines=0, bool preserveNewlines = false);
// Function to get an entry from the list based on the index
@@ -89,7 +89,7 @@ namespace ult {
// Function to read file into a set of strings
std::unordered_set<std::string> readSetFromFile(const std::string& filePath);
std::unordered_set<std::string> readSetFromFile(const std::string& filePath, const std::string& packagePath = "");
// Function to write a set to a file

View File

@@ -13,7 +13,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2024-2025 ppkantorski
********************************************************************************/
#pragma once

View File

@@ -14,7 +14,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#pragma once
@@ -36,6 +36,7 @@
#include "get_funcs.hpp"
#include <queue>
#include <mutex>
#include <unordered_set>
namespace ult {
@@ -143,7 +144,7 @@ namespace ult {
*
* @param pathPattern The pattern used to match and delete files or directories.
*/
void deleteFileOrDirectoryByPattern(const std::string& pathPattern, const std::string& logSource = "");
void deleteFileOrDirectoryByPattern(const std::string& pathPattern, const std::string& logSourc = "", const std::unordered_set<std::string>* filterSet = nullptr);
void moveDirectory(const std::string& sourcePath, const std::string& destinationPath,
@@ -176,12 +177,16 @@ namespace ult {
*
* This function identifies files or directories that match the `sourcePathPattern` and moves them to the `destinationPath`.
* It processes each matching entry in the source directory pattern and moves them to the specified destination.
* Files/directories in the filterSet will be skipped.
*
* @param sourcePathPattern The pattern used to match files or directories to be moved.
* @param destinationPath The destination directory where matching files or directories will be moved.
* @param logSource Optional log source identifier.
* @param logDestination Optional log destination identifier.
* @param filterSet Optional set of paths to exclude from moving (nullptr to move all).
*/
void moveFilesOrDirectoriesByPattern(const std::string& sourcePathPattern, const std::string& destinationPath,
const std::string& logSource = "", const std::string& logDestination = "");
const std::string& logSource = "", const std::string& logDestination = "", const std::unordered_set<std::string>* filterSet = nullptr);
@@ -227,12 +232,16 @@ namespace ult {
*
* This function identifies files or directories that match the `sourcePathPattern` and copies them to the `toDirectory`.
* It processes each matching entry in the source directory pattern and copies them to the specified destination.
* Files/directories in the filterSet will be skipped.
*
* @param sourcePathPattern The pattern used to match files or directories to be copied.
* @param toDirectory The destination directory where matching files or directories will be copied.
* @param logSource Optional log source identifier.
* @param logDestination Optional log destination identifier.
* @param filterSet Optional set of paths to exclude from copying (nullptr to copy all).
*/
void copyFileOrDirectoryByPattern(const std::string& sourcePathPattern, const std::string& toDirectory,
const std::string& logSource = "", const std::string& logDestination = "");
const std::string& logSource = "", const std::string& logDestination = "", const std::unordered_set<std::string>* filterSet = nullptr);

View File

@@ -14,7 +14,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#pragma once
@@ -40,7 +40,7 @@ namespace ult {
extern int stoi(const std::string& str, std::size_t* pos = nullptr, int base = 10);
extern float stof(const std::string& str);
extern bool canConvertToInt(const std::string& str);
//extern bool canConvertToInt(const std::string& str);
/**
* @brief A lightweight string stream class that mimics basic functionality of std::istringstream.
@@ -144,7 +144,7 @@ namespace ult {
* @param input The input string to process.
* @return The string with multiple slashes replaced.
*/
std::string replaceMultipleSlashes(const std::string& input);
//std::string replaceMultipleSlashes(const std::string& input);
void resolveDirectoryTraversal(std::string& path);

View File

@@ -18,7 +18,7 @@
* altered or removed.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
@@ -54,39 +54,39 @@
#include <map>
#include <barrier>
#ifndef APPROXIMATE_cos
// Approximation for cos(x) using Taylor series around 0
#define APPROXIMATE_cos(x) (1 - (x) * (x) / 2 + (x) * (x) * (x) * (x) / 24) // valid for small x
#endif
#ifndef APPROXIMATE_ifloor
#define APPROXIMATE_ifloor(x) ((int)((x) >= 0 ? (x) : (x) - 1)) // truncate toward negative infinity
#define APPROXIMATE_iceil(x) ((int)((x) == (int)(x) ? (x) : ((x) > 0 ? (int)(x) + 1 : (int)(x)))) // truncate toward positive infinity
#endif
#ifndef APPROXIMATE_sqrt
// Fast approximation for sqrt using Newton's method
#define APPROXIMATE_sqrt(x) ((x) <= 0 ? 0 : (x) / 2.0 * (3.0 - ((x) * (x) * 0.5))) // Approximation for x close to 1
#define APPROXIMATE_pow(x, y) ((y) == 0 ? 1 : ((y) == 1 ? (x) : APPROXIMATE_sqrt(x))) // limited to approximate sqrt if y=0.5
#endif
#ifndef APPROXIMATE_fmod
#define APPROXIMATE_fmod(x, y) ((x) - ((int)((x) / (y)) * (y))) // equivalent to x - floor(x/y) * y
#endif
#ifndef APPROXIMATE_cos
// Approximation for cos(x) using Taylor series around 0
#define APPROXIMATE_cos(x) (1 - (x) * (x) / 2 + (x) * (x) * (x) * (x) / 24) // valid for small x
#endif
#ifndef APPROXIMATE_acos
#define APPROXIMATE_acos(x) (1.5708 - (x) - (x)*(x)*(x) / 6) // limited approximation for acos in range [-1, 1]
#endif
#ifndef APPROXIMATE_fabs
#define APPROXIMATE_fabs(x) ((x) < 0 ? -(x) : (x))
#endif
//#ifndef APPROXIMATE_cos
//// Approximation for cos(x) using Taylor series around 0
//#define APPROXIMATE_cos(x) (1 - (x) * (x) / 2 + (x) * (x) * (x) * (x) / 24) // valid for small x
//#endif
//
//
//#ifndef APPROXIMATE_ifloor
//#define APPROXIMATE_ifloor(x) ((int)((x) >= 0 ? (x) : (x) - 1)) // truncate toward negative infinity
//#define APPROXIMATE_iceil(x) ((int)((x) == (int)(x) ? (x) : ((x) > 0 ? (int)(x) + 1 : (int)(x)))) // truncate toward positive infinity
//#endif
//
//#ifndef APPROXIMATE_sqrt
//// Fast approximation for sqrt using Newton's method
//#define APPROXIMATE_sqrt(x) ((x) <= 0 ? 0 : (x) / 2.0 * (3.0 - ((x) * (x) * 0.5))) // Approximation for x close to 1
//#define APPROXIMATE_pow(x, y) ((y) == 0 ? 1 : ((y) == 1 ? (x) : APPROXIMATE_sqrt(x))) // limited to approximate sqrt if y=0.5
//#endif
//
//#ifndef APPROXIMATE_fmod
//#define APPROXIMATE_fmod(x, y) ((x) - ((int)((x) / (y)) * (y))) // equivalent to x - floor(x/y) * y
//#endif
//
//#ifndef APPROXIMATE_cos
//// Approximation for cos(x) using Taylor series around 0
//#define APPROXIMATE_cos(x) (1 - (x) * (x) / 2 + (x) * (x) * (x) * (x) / 24) // valid for small x
//#endif
//
//#ifndef APPROXIMATE_acos
//#define APPROXIMATE_acos(x) (1.5708 - (x) - (x)*(x)*(x) / 6) // limited approximation for acos in range [-1, 1]
//#endif
//
//#ifndef APPROXIMATE_fabs
//#define APPROXIMATE_fabs(x) ((x) < 0 ? -(x) : (x))
//#endif
struct OverlayCombo {
std::string path; // full overlay path
@@ -99,6 +99,35 @@ struct SwapDepth {
};
namespace ult {
// math funcs
inline double cos(double x) {
static constexpr double PI = 3.14159265358979323846;
static constexpr double TWO_PI = 6.28318530717958647692;
static constexpr double HALF_PI = 1.57079632679489661923;
// Fast normalization using multiply instead of divide when possible
x = x - TWO_PI * static_cast<int>(x * 0.159154943091895); // 1/(2π)
if (x < 0) x += TWO_PI;
// Use symmetry to reduce range
int sign = 1;
if (x > PI) {
x -= PI;
sign = -1;
}
if (x > HALF_PI) {
x = PI - x;
sign = -sign;
}
// Horner's method for faster polynomial evaluation (fewer operations)
// 5-term minimax polynomial for [0, π/2] - accurate to ~10^-8
const double x2 = x * x;
return sign * (1.0 + x2 * (-0.5 + x2 * (0.04166666666666666 + x2 * (-0.001388888888888889 + x2 * (0.0000248015873015873 - x2 * 0.0000002755731922398589)))));
}
extern bool correctFrameSize; // for detecting the correct Overlay display size
extern u16 DefaultFramebufferWidth; ///< Width of the framebuffer
@@ -182,6 +211,9 @@ namespace ult {
extern std::atomic<float> selectWidth;
extern std::atomic<float> nextPageWidth;
extern std::atomic<bool> inMainMenu;
extern std::atomic<bool> inHiddenMode;
extern std::atomic<bool> inSettingsMenu;
extern std::atomic<bool> inSubSettingsMenu;
extern std::atomic<bool> inOverlaysPage;
extern std::atomic<bool> inPackagesPage;
@@ -200,7 +232,7 @@ namespace ult {
//bool progressAnimation = false;
extern bool disableTransparency;
//bool useCustomWallpaper = false;
extern bool useMemoryExpansion;
//extern bool useMemoryExpansion;
extern bool useOpaqueScreenshots;
extern std::atomic<bool> onTrackBar;
@@ -344,6 +376,7 @@ namespace ult {
extern std::string HIDE_OVERLAY;
extern std::string HIDE_PACKAGE;
extern std::string LAUNCH_ARGUMENTS;
extern std::string FORCE_AMS110_SUPPORT;
extern std::string QUICK_LAUNCH;
extern std::string BOOT_COMMANDS;
extern std::string EXIT_COMMANDS;
@@ -371,6 +404,8 @@ namespace ult {
extern std::string USER_GUIDE;
extern std::string SHOW_HIDDEN;
extern std::string SHOW_DELETE;
extern std::string SHOW_UNSUPPORTED;
extern std::string PAGE_SWAP;
extern std::string RIGHT_SIDE_MODE;
extern std::string OVERLAY_VERSIONS;
@@ -379,7 +414,7 @@ namespace ult {
//extern std::string VERSION_LABELS;
extern std::string KEY_COMBO;
extern std::string MODE;
extern std::string MODES;
extern std::string LAUNCH_MODES;
extern std::string LANGUAGE;
extern std::string OVERLAY_INFO;
extern std::string SOFTWARE_UPDATE;
@@ -394,11 +429,19 @@ namespace ult {
extern std::string VENDOR;
extern std::string MODEL;
extern std::string STORAGE;
extern std::string NOTICE;
extern std::string UTILIZES;
//extern std::string NOTICE;
//extern std::string UTILIZES;
extern std::string MEMORY_EXPANSION;
extern std::string REBOOT_REQUIRED;
extern std::string OVERLAY_MEMORY;
extern std::string NOT_ENOUGH_MEMORY;
extern std::string WALLPAPER_SUPPORT_DISABLED;
extern std::string SOUND_SUPPORT_DISABLED;
extern std::string WALLPAPER_SUPPORT_ENABLED;
extern std::string SOUND_SUPPORT_ENABLED;
extern std::string EXIT_OVERLAY_SYSTEM;
//extern std::string MEMORY_EXPANSION;
//extern std::string REBOOT_REQUIRED;
extern std::string LOCAL_IP;
extern std::string WALLPAPER;
extern std::string THEME;
@@ -443,9 +486,16 @@ namespace ult {
extern std::string ULTRAHAND_HAS_STARTED;
extern std::string NEW_UPDATE_IS_AVAILABLE;
extern std::string REBOOT_IS_REQUIRED;
extern std::string HOLD_A_TO_DELETE;
//extern std::string REBOOT_IS_REQUIRED;
//extern std::string HOLD_A_TO_DELETE;
extern std::string DELETE_PACKAGE;
extern std::string DELETE_OVERLAY;
extern std::string SELECTION_IS_EMPTY;
extern std::string FORCED_SUPPORT_WARNING;
extern std::string TASK_IS_COMPLETE;
extern std::string TASK_HAS_FAILED;
//extern std::string PACKAGE_VERSIONS;
//extern std::string PROGRESS_ANIMATION;
@@ -456,6 +506,8 @@ namespace ult {
extern std::string BOOT_ENTRY;
#endif
extern std::string INCOMPATIBLE_WARNING;
extern std::string SYSTEM_RAM;
extern std::string FREE;
extern std::string DEFAULT_CHAR_WIDTH;
@@ -567,7 +619,7 @@ namespace ult {
float calculateAmplitude(float x, float peakDurationFactor = 0.25f);
//float calculateAmplitude(float x, float peakDurationFactor = 0.25f);
extern std::atomic<bool> refreshWallpaperNow;
@@ -685,9 +737,44 @@ namespace ult {
extern bool cleanVersionLabels, hideOverlayVersions, hidePackageVersions, useLibultrahandTitles, useLibultrahandVersions, usePackageTitles, usePackageVersions;
// nx-ovlloader settings
enum class OverlayHeapSize : u64 {
Size_4MB = 0x400000,
Size_6MB = 0x600000,
Size_8MB = 0x800000
};
// Static cache
static struct {
bool initialized = false;
OverlayHeapSize cachedSize = OverlayHeapSize::Size_6MB;
u64 customSizeMB = 0; // NEW: store custom size in MB
} heapSizeCache;
// Helper function to convert MB to bytes
extern u64 mbToBytes(u32 mb);
// Helper function to convert bytes to MB
extern u32 bytesToMB(u64 bytes);
// Implementation
OverlayHeapSize getCurrentHeapSize();
extern OverlayHeapSize currentHeapSize;
bool setOverlayHeapSize(OverlayHeapSize heapSize);
// Implementation
bool requestOverlayExit();
extern const std::string loaderInfo;
extern const std::string loaderTitle;
extern const bool expandedMemory;
extern std::string loaderTitle;
extern bool expandedMemory;
extern bool furtherExpandedMemory;
extern bool limitedMemory;
extern std::string versionLabel;

View File

@@ -19,7 +19,7 @@
* altered or removed.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2024-2025 ppkantorski
********************************************************************************/
#pragma once
@@ -43,7 +43,7 @@
#include "download_funcs.hpp"
#include "mod_funcs.hpp"
#include "tsl_utils.hpp"
#include "audio_player.hpp"
#include "audio.hpp"
#include "haptics.hpp"
#endif // ULTRA_HPP

View File

@@ -1,5 +1,5 @@
/********************************************************************************
* File: audio_player.cpp
* File: audio.cpp
* Author: ppkantorski
* Description:
* Memory-optimized version with reduced allocation overhead and chunked I/O.
@@ -19,17 +19,17 @@
* Copyright (c) 2025 ppkantorski
********************************************************************************/
#include "audio_player.hpp"
#include "audio.hpp"
namespace ult {
bool AudioPlayer::m_initialized = false;
std::atomic<bool> AudioPlayer::m_enabled{true};
float AudioPlayer::m_masterVolume = 0.6f;
bool AudioPlayer::m_lastDockedState = false;
std::vector<AudioPlayer::CachedSound> AudioPlayer::m_cachedSounds;
std::mutex AudioPlayer::m_audioMutex;
bool Audio::m_initialized = false;
std::atomic<bool> Audio::m_enabled{true};
float Audio::m_masterVolume = 0.6f;
bool Audio::m_lastDockedState = false;
std::vector<Audio::CachedSound> Audio::m_cachedSounds;
std::mutex Audio::m_audioMutex;
bool AudioPlayer::initialize() {
bool Audio::initialize() {
std::lock_guard<std::mutex> lock(m_audioMutex);
if (m_initialized) return true;
@@ -41,13 +41,13 @@ namespace ult {
m_initialized = true;
m_cachedSounds.resize(static_cast<uint32_t>(SoundType::Count));
m_lastDockedState = isDocked();
m_lastDockedState = ult::consoleIsDocked();
reloadAllSounds();
return true;
}
void AudioPlayer::exit() {
void Audio::exit() {
std::lock_guard<std::mutex> lock(m_audioMutex);
// Free all cached sound buffers
@@ -67,24 +67,24 @@ namespace ult {
}
}
void AudioPlayer::reloadAllSounds() {
void Audio::reloadAllSounds() {
for (uint32_t i = 0; i < static_cast<uint32_t>(SoundType::Count); ++i) {
loadSoundFromWav(static_cast<SoundType>(i), m_soundPaths[i]);
}
}
void AudioPlayer::unloadAllSounds(const std::initializer_list<SoundType>& excludeSounds) {
void Audio::unloadAllSounds(const std::initializer_list<SoundType>& excludeSounds) {
std::lock_guard<std::mutex> lock(m_audioMutex);
if (!m_initialized) return;
for (uint32_t i = 0; i < m_cachedSounds.size(); ++i) {
SoundType current = static_cast<SoundType>(i);
// Skip if this sound is in the exclude list
if (std::find(excludeSounds.begin(), excludeSounds.end(), current) != excludeSounds.end()) {
continue;
}
auto& cached = m_cachedSounds[i];
if (cached.buffer) {
free(cached.buffer);
@@ -95,10 +95,10 @@ namespace ult {
}
}
bool AudioPlayer::reloadIfDockedChanged() {
bool Audio::reloadIfDockedChanged() {
if (!m_initialized) return false;
const bool currentDocked = isDocked();
const bool currentDocked = ult::consoleIsDocked();
if (currentDocked == m_lastDockedState) return false;
std::lock_guard<std::mutex> lock(m_audioMutex);
@@ -108,28 +108,28 @@ namespace ult {
return true;
}
bool AudioPlayer::loadSoundFromWav(SoundType type, const char* path) {
bool Audio::loadSoundFromWav(SoundType type, const char* path) {
const uint32_t idx = static_cast<uint32_t>(type);
if (!m_initialized || idx >= static_cast<uint32_t>(SoundType::Count)) return false;
// Free existing buffer
free(m_cachedSounds[idx].buffer);
m_cachedSounds[idx] = { nullptr, 0, 0 };
FILE* f = fopen(path, "rb");
if (!f) return false;
// Parse WAV header
char hdr[12];
if (fread(hdr, 1, 12, f) != 12 || memcmp(hdr, "RIFF", 4) || memcmp(hdr + 8, "WAVE", 4)) {
fclose(f);
return false;
}
u16 fmt = 0, ch = 0, bits = 0;
u32 rate = 0, dSize = 0;
long dPos = 0;
// Find fmt and data chunks
while (fread(hdr, 1, 8, f) == 8) {
const u32 sz = *(u32*)(hdr + 4);
@@ -148,13 +148,13 @@ namespace ult {
fseek(f, sz, SEEK_CUR);
}
}
// Validate format
if (!dSize || fmt != 1 || ch == 0 || ch > 2 || (bits != 8 && bits != 16)) {
fclose(f);
return false;
}
// Calculate buffer sizes
// Note: audout REQUIRES stereo (2 channels), so we must duplicate mono
const bool mono = (ch == 1);
@@ -165,14 +165,14 @@ namespace ult {
// Use smaller alignment to reduce waste (256 bytes instead of 4KB)
const uint32_t align = 0x100;
const uint32_t bufSize = (outSize + align - 1) & ~(align - 1);
// Allocate output buffer
void* buf = aligned_alloc(align, bufSize);
if (!buf) {
fclose(f);
return false;
}
fseek(f, dPos, SEEK_SET);
s16* out = (s16*)buf;
@@ -182,7 +182,7 @@ namespace ult {
effectiveVolume *= 0.5f;
}
const float scale = std::clamp(effectiveVolume, 0.0f, 1.0f);
// Process audio in chunks to minimize memory usage
// This eliminates the need for temporary vectors
constexpr uint32_t CHUNK_SIZE = 512;
@@ -241,25 +241,25 @@ namespace ult {
remaining -= toRead;
}
}
fclose(f);
// Zero-fill any padding
if (outSize < bufSize) {
memset((u8*)buf + outSize, 0, bufSize - outSize);
}
m_cachedSounds[idx] = { buf, bufSize, outSize };
return true;
}
void AudioPlayer::playSound(SoundType type) {
void Audio::playSound(SoundType type) {
// Lock-free check - SAFE with atomic
if (!m_enabled.load(std::memory_order_relaxed)) return;
const uint32_t idx = static_cast<uint32_t>(type);
if (idx >= static_cast<uint32_t>(SoundType::Count)) return;
std::lock_guard<std::mutex> lock(m_audioMutex);
// Check again under lock
@@ -267,12 +267,12 @@ namespace ult {
auto& cached = m_cachedSounds[idx];
if (!cached.buffer) return;
// Release any finished buffers
AudioOutBuffer* releasedBuffers = nullptr;
u32 releasedCount = 0;
audoutGetReleasedAudioOutBuffer(&releasedBuffers, &releasedCount);
// Static buffer is safe with mutex protection
static AudioOutBuffer audioBuffer = {};
audioBuffer = {};
@@ -281,32 +281,32 @@ namespace ult {
audioBuffer.data_size = cached.dataSize;
audioBuffer.data_offset = 0;
audioBuffer.next = nullptr;
AudioOutBuffer* rel = nullptr;
audoutPlayBuffer(&audioBuffer, &rel);
}
void AudioPlayer::setMasterVolume(float v) {
void Audio::setMasterVolume(float v) {
std::lock_guard<std::mutex> lock(m_audioMutex);
m_masterVolume = std::clamp(v, 0.0f, 1.0f);
}
void AudioPlayer::setEnabled(bool e) {
void Audio::setEnabled(bool e) {
m_enabled.store(e, std::memory_order_relaxed);
}
bool AudioPlayer::isEnabled() {
bool Audio::isEnabled() {
return m_enabled.load(std::memory_order_relaxed);
}
bool AudioPlayer::isDocked() {
Result rc = apmInitialize();
if (R_FAILED(rc)) return false;
ApmPerformanceMode perfMode = ApmPerformanceMode_Invalid;
rc = apmGetPerformanceMode(&perfMode);
apmExit();
return R_SUCCEEDED(rc) && (perfMode == ApmPerformanceMode_Boost);
}
//bool Audio::isDocked() {
// Result rc = apmInitialize();
// if (R_FAILED(rc)) return false;
//
// ApmPerformanceMode perfMode = ApmPerformanceMode_Invalid;
// rc = apmGetPerformanceMode(&perfMode);
// apmExit();
//
// return R_SUCCEEDED(rc) && (perfMode == ApmPerformanceMode_Boost);
//}
}

View File

@@ -4,6 +4,15 @@
* Description:
* This source file contains the implementation of debugging functions for the
* Ultrahand Overlay project.
*
* For the latest updates and contributions, visit the project's GitHub repository.
* (GitHub Repository: https://github.com/ppkantorski/Ultrahand-Overlay)
*
* Note: Please be aware that this notice cannot be altered or removed. It is a part
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#include "debug_funcs.hpp"

View File

@@ -14,7 +14,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#include "download_funcs.hpp"
@@ -23,8 +23,8 @@
namespace ult {
// Base loader definitions
size_t DOWNLOAD_READ_BUFFER = 8*1024;//64 * 1024;//4096*10;
size_t DOWNLOAD_WRITE_BUFFER = 8*1024;//64 * 1024;
size_t DOWNLOAD_READ_BUFFER = 32*1024;//64 * 1024;//4096*10;
size_t DOWNLOAD_WRITE_BUFFER = 16*1024;//64 * 1024;
size_t UNZIP_READ_BUFFER = 32*1024;//131072*2;//4096*4;
size_t UNZIP_WRITE_BUFFER = 16*1024;//131072*2;//4096*4;
@@ -131,7 +131,7 @@ int progressCallback(void *ptr, curl_off_t totalToDownload, curl_off_t nowDownlo
// }
//}
std::unique_ptr<char[]> globalWriteBuffer;
//std::unique_ptr<char[]> writeBuffer;
/**
* @brief Downloads a file from a URL to a specified destination.
@@ -192,12 +192,12 @@ bool downloadFile(const std::string& url, const std::string& toDestination, bool
}
// ADD THIS: Set up write buffer for better performance
//std::unique_ptr<char[]> globalWriteBuffer;
std::unique_ptr<char[]> writeBuffer;
if (DOWNLOAD_WRITE_BUFFER > 0) {
//if (!globalWriteBuffer)
globalWriteBuffer = std::make_unique<char[]>(DOWNLOAD_WRITE_BUFFER);
//if (!writeBuffer)
writeBuffer = std::make_unique<char[]>(DOWNLOAD_WRITE_BUFFER);
// _IOFBF = full buffering, _IOLBF = line buffering, _IONBF = no buffering
setvbuf(file.get(), globalWriteBuffer.get(), _IOFBF, DOWNLOAD_WRITE_BUFFER);
setvbuf(file.get(), writeBuffer.get(), _IOFBF, DOWNLOAD_WRITE_BUFFER);
}
//setvbuf(file.get(), NULL, _IOFBF, DOWNLOAD_WRITE_BUFFER);
@@ -224,7 +224,7 @@ bool downloadFile(const std::string& url, const std::string& toDestination, bool
file.close();
#else
file.reset();
globalWriteBuffer.reset();
writeBuffer.reset();
#endif
return false;
}
@@ -277,12 +277,15 @@ bool downloadFile(const std::string& url, const std::string& toDestination, bool
//const bool wasAborted = (result == CURLE_ABORTED_BY_CALLBACK ||
// abortDownload.load(std::memory_order_acquire));
// Check HTTP response code BEFORE closing file/curl
long http_code = 0;
curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &http_code);
#if USING_FSTREAM_DIRECTIVE
file.close();
#else
file.reset();
globalWriteBuffer.reset();
writeBuffer.reset();
#endif
curl.reset();
@@ -309,6 +312,20 @@ bool downloadFile(const std::string& url, const std::string& toDestination, bool
//cleanupCurl();
//socketExit();
// Check for HTTP errors (404, 500, etc.)
if (result == CURLE_OK && (http_code < 200 || http_code >= 300)) {
#if USING_LOGGING_DIRECTIVE
if (!disableLogging)
logMessage("HTTP error " + std::to_string(http_code) + " downloading: " + url);
#endif
deleteFileOrDirectory(tempFilePath);
if (!noPercentagePolling) {
downloadPercentage.store(-1, std::memory_order_release);
}
return false;
}
if (result != CURLE_OK) {
#if USING_LOGGING_DIRECTIVE
@@ -382,6 +399,7 @@ bool downloadFile(const std::string& url, const std::string& toDestination, bool
}
moveFile(tempFilePath, destination);
return true;
}
@@ -622,7 +640,7 @@ bool unzipFile(const std::string& zipFilePath, const std::string& toDestination)
const size_t bufferSize = UNZIP_WRITE_BUFFER;
//std::unique_ptr<char[]> buffer = std::make_unique<char[]>(bufferSize);
globalWriteBuffer = std::make_unique<char[]>(bufferSize);
std::unique_ptr<char[]> writeBuffer = std::make_unique<char[]>(bufferSize);
char filenameBuffer[512]; // Stack allocated for filename reading
@@ -758,14 +776,14 @@ bool unzipFile(const std::string& zipFilePath, const std::string& toDestination)
fileBytesProcessed = 0;
while ((bytesRead = unzReadCurrentFile(zipFile, globalWriteBuffer.get(), bufferSize)) > 0) {
while ((bytesRead = unzReadCurrentFile(zipFile, writeBuffer.get(), bufferSize)) > 0) {
if (abortUnzip.load(std::memory_order_relaxed)) {
extractSuccess = false;
break; // RAII will handle cleanup
}
// Write data to file
if (outputFile.write(globalWriteBuffer.get(), bytesRead) != static_cast<size_t>(bytesRead)) {
if (outputFile.write(writeBuffer.get(), bytesRead) != static_cast<size_t>(bytesRead)) {
extractSuccess = false;
break;
}
@@ -845,7 +863,7 @@ bool unzipFile(const std::string& zipFilePath, const std::string& toDestination)
result = unzGoToNextFile(zipFile);
}
globalWriteBuffer.reset();
writeBuffer.reset();
// Check final abort state
if (abortUnzip.load(std::memory_order_relaxed)) {

View File

@@ -15,7 +15,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#include "get_funcs.hpp"

View File

@@ -13,7 +13,7 @@
* altered or removed.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#include "global_vars.hpp"
@@ -26,6 +26,10 @@ namespace ult {
const std::string TESLA_CONFIG_PATH = ROOT_PATH + "config/tesla/";
const std::string SWITCH_PATH = ROOT_PATH + "switch/";
const std::string NX_OVLLOADER_PATH = ROOT_PATH + "config/nx-ovlloader/";
const std::string OVL_HEAP_CONFIG_PATH = NX_OVLLOADER_PATH + "heap_size.bin";
const std::string OVL_EXIT_FLAG_PATH = NX_OVLLOADER_PATH + "exit_flag.bin";
// Filenames
CONSTEXPR_STRING std::string CONFIG_FILENAME = "config.ini";
const std::string BOOT_PACKAGE_FILENAME = "boot_package.ini";
@@ -59,7 +63,8 @@ namespace ult {
std::string THEME_CONFIG_INI_PATH = BASE_CONFIG_PATH + THEME_FILENAME;
std::string WALLPAPER_PATH = BASE_CONFIG_PATH + WALLPAPER_FILENAME;
const std::string DOWNLOADS_PATH = BASE_CONFIG_PATH + "downloads/";
const std::string EXPANSION_PATH = BASE_CONFIG_PATH + "expansion/";
//const std::string EXPANSION_PATH = BASE_CONFIG_PATH + "expansion/";
const std::string FUSE_DATA_INI_PATH = BASE_CONFIG_PATH + FUSE_FILENAME;
const std::string PACKAGE_PATH = SWITCH_PATH + ".packages/";
const std::string OVERLAY_PATH = SWITCH_PATH + ".overlays/";
@@ -79,12 +84,13 @@ namespace ult {
const std::string ULTRAHAND_REPO_URL = GITHUB_BASE_URL + "Ultrahand-Overlay/";
const std::string INCLUDED_THEME_FOLDER_URL = GITHUB_RAW_BASE_URL + "Ultrahand-Overlay/main/themes/";
const std::string LATEST_RELEASE_INFO_URL = GITHUB_RAW_BASE_URL + "Ultrahand-Overlay/main/RELEASE.ini";
const std::string NX_OVLLOADER_ZIP_URL = GITHUB_BASE_URL + "nx-ovlloader/releases/latest/download/nx-ovlloader.zip";
const std::string NX_OVLLOADER_PLUS_ZIP_URL = GITHUB_BASE_URL + "nx-ovlloader/releases/latest/download/nx-ovlloader+.zip";
const std::string LATEST_UPDATER_INI_URL = ULTRAHAND_REPO_URL + "releases/latest/download/update.ini";
//const std::string NX_OVLLOADER_ZIP_URL = GITHUB_BASE_URL + "nx-ovlloader/releases/latest/download/nx-ovlloader.zip";
//const std::string NX_OVLLOADER_PLUS_ZIP_URL = GITHUB_BASE_URL + "nx-ovlloader/releases/latest/download/nx-ovlloader+.zip";
const std::string OLD_NX_OVLLOADER_ZIP_URL = GITHUB_BASE_URL + "nx-ovlloader/releases/download/v1.0.8/nx-ovlloader.zip";
const std::string OLD_NX_OVLLOADER_PLUS_ZIP_URL = GITHUB_BASE_URL + "nx-ovlloader/releases/download/v1.0.8/nx-ovlloader+.zip";
//const std::string OLD_NX_OVLLOADER_PLUS_ZIP_URL = GITHUB_BASE_URL + "nx-ovlloader/releases/download/v1.0.8/nx-ovlloader+.zip";
const std::string UPDATER_PAYLOAD_URL = GITHUB_RAW_BASE_URL + "Ultrahand-Overlay/main/payloads/ultrahand_updater.bin";
const std::string SOUND_EFFECTS_URL = GITHUB_RAW_BASE_URL + "Ultrahand-Overlay/main/sounds/sounds.zip";
//const std::string SOUND_EFFECTS_URL = GITHUB_RAW_BASE_URL + "Ultrahand-Overlay/main/sounds/sounds.zip";
// Launch options
const std::string LAUNCH_ARGS_STR = "launch_args";
@@ -140,6 +146,7 @@ namespace ult {
CONSTEXPR_STRING std::string FALSE_STR = "false";
CONSTEXPR_STRING std::string GLOBAL_STR = "global";
CONSTEXPR_STRING std::string DEFAULT_STR = "default";
CONSTEXPR_STRING std::string HOLD_STR = "hold";
CONSTEXPR_STRING std::string SLOT_STR = "slot";
CONSTEXPR_STRING std::string OPTION_STR = "option";
CONSTEXPR_STRING std::string FORWARDER_STR = "forwarder";
@@ -164,6 +171,7 @@ namespace ult {
CONSTEXPR_STRING std::string INPROGRESS_SYMBOL = "\u25CF";
CONSTEXPR_STRING std::string STAR_SYMBOL = "\u2605";
CONSTEXPR_STRING std::string DIVIDER_SYMBOL = "";
CONSTEXPR_STRING std::string NOTIFY_HEADER = "";
const std::vector<std::string> THROBBER_SYMBOLS = {"", "", "", "", "", "", "", ""};

View File

@@ -30,10 +30,13 @@ namespace ult {
static HidVibrationDeviceHandle vibPlayer1Right;
static u64 rumbleStartTick = 0;
static u64 doubleClickTick = 0;
static u8 doubleClickPulse = 0;
static u8 doubleClickPulse = 0;
static u32 cachedHandheldStyle = 0;
static u32 cachedPlayer1Style = 0;
// ===== Shared flags (accessible globally) =====
std::atomic<bool> rumbleActive{false};
std::atomic<bool> clickActive{false};
std::atomic<bool> doubleClickActive{false};
// ===== Constants =====
@@ -41,144 +44,182 @@ namespace ult {
static constexpr u64 DOUBLE_CLICK_PULSE_DURATION_NS = 30'000'000ULL;
static constexpr u64 DOUBLE_CLICK_GAP_NS = 100'000'000ULL;
static constexpr HidVibrationValue clickDocked = {
//static constexpr HidVibrationValue clickDocked = {
// .amp_low = 0.20f,
// .freq_low = 100.0f,
// .amp_high = 0.80f,
// .freq_high = 300.0f
//};
//
//static constexpr HidVibrationValue clickHandheld = {
// .amp_low = 0.20f,
// .freq_low = 100.0f,
// .amp_high = 0.80f,
// .freq_high = 300.0f
//};
static constexpr HidVibrationValue hapticsPreset = {
.amp_low = 0.20f,
.freq_low = 100.0f,
.amp_high = 0.80f,
.freq_high = 300.0f
};
static constexpr HidVibrationValue clickHandheld = {
.amp_low = 0.25f,
.freq_low = 100.0f,
.amp_high = 1.0f,
.freq_high = 300.0f
};
static constexpr HidVibrationValue vibrationStop{0};
// ===== Internal helpers =====
static void initController(HidNpadIdType npad, HidVibrationDeviceHandle* handles, int count) {
const u32 styleMask = hidGetNpadStyleSet(npad);
if (styleMask)
hidInitializeVibrationDevices(handles, count, npad, static_cast<HidNpadStyleTag>(styleMask));
}
static void sendVibration(const HidVibrationValue* value) {
if (hidGetNpadStyleSet(HidNpadIdType_Handheld))
static inline void sendVibration(const HidVibrationValue* value) {
if (cachedHandheldStyle)
hidSendVibrationValue(vibHandheld, value);
if (hidGetNpadStyleSet(HidNpadIdType_No1)) {
if (cachedPlayer1Style) {
hidSendVibrationValue(vibPlayer1Left, value);
hidSendVibrationValue(vibPlayer1Right, value);
}
}
// ===== Public API =====
void initRumble() {
//if (rumbleInitialized) return;
// Try to initialize whatever is available
// Don't check if controllers exist - let initController handle it
initController(HidNpadIdType_Handheld, &vibHandheld, 1);
HidVibrationDeviceHandle handles[2];
initController(HidNpadIdType_No1, handles, 2);
vibPlayer1Left = handles[0];
vibPlayer1Right = handles[1];
// Only mark as initialized if at least one controller was found
hidGetNpadStyleSet(HidNpadIdType_Handheld);
hidGetNpadStyleSet(HidNpadIdType_No1);
//rumbleInitialized = (handheldStyle || player1Style);
// If neither exist, stay uninitialized so we retry later
static inline void sendVibration2x(const HidVibrationValue* value) {
sendVibration(value);
sendVibration(value);
}
//void deinitRumble() {
// ===== Public API =====
void initHaptics() {
const u32 handheldStyle = hidGetNpadStyleSet(HidNpadIdType_Handheld);
const u32 player1Style = hidGetNpadStyleSet(HidNpadIdType_No1);
// Clear previous handles to avoid using stale handles if controllers were removed
vibHandheld = (HidVibrationDeviceHandle)0;
vibPlayer1Left = (HidVibrationDeviceHandle)0;
vibPlayer1Right = (HidVibrationDeviceHandle)0;
// Handheld
if (handheldStyle) {
hidInitializeVibrationDevices(&vibHandheld, 1,
HidNpadIdType_Handheld,
(HidNpadStyleTag)handheldStyle);
}
// Player 1 (left + right Joy-Con or Pro Controller)
if (player1Style) {
HidVibrationDeviceHandle tmp[2] = { (HidVibrationDeviceHandle)0, (HidVibrationDeviceHandle)0 };
hidInitializeVibrationDevices(tmp, 2,
HidNpadIdType_No1,
(HidNpadStyleTag)player1Style);
vibPlayer1Left = tmp[0];
vibPlayer1Right = tmp[1];
}
// Ensure cache is valid immediately after initHaptics()
cachedHandheldStyle = handheldStyle;
cachedPlayer1Style = player1Style;
}
//void deinitHaptics() {
// rumbleInitialized = false;
//}
void checkAndReinitRumble() {
void checkAndReinitHaptics() {
static u32 lastHandheldStyle = 0;
static u32 lastPlayer1Style = 0;
static u32 lastPlayer1Style = 0;
const u32 currentHandheldStyle = hidGetNpadStyleSet(HidNpadIdType_Handheld);
const u32 currentPlayer1Style = hidGetNpadStyleSet(HidNpadIdType_No1);
const u32 currentPlayer1Style = hidGetNpadStyleSet(HidNpadIdType_No1);
// If not initialized but controllers exist, try to init
// This handles the boot race condition where HID reports controllers
// but vibration subsystem isn't ready yet
//if (!rumbleInitialized && (currentHandheldStyle || currentPlayer1Style)) {
// initRumble();
//}
// Reinitialize only if something changed (appearance/disappearance or style change)
//const bool changed =
// (currentHandheldStyle != lastHandheldStyle) || (currentPlayer1Style != lastPlayer1Style);
// Reinit if controller configuration changed
if (currentHandheldStyle != lastHandheldStyle || currentPlayer1Style != lastPlayer1Style) {
//rumbleInitialized = false;
initRumble();
if ((currentHandheldStyle != lastHandheldStyle) || (currentPlayer1Style != lastPlayer1Style)) {
initHaptics();
}
// Update last style tracking regardless
// Update last-known styles for change detection
lastHandheldStyle = currentHandheldStyle;
lastPlayer1Style = currentPlayer1Style;
lastPlayer1Style = currentPlayer1Style;
// Update cached styles used by sendVibration()/rumble paths
cachedHandheldStyle = currentHandheldStyle;
cachedPlayer1Style = currentPlayer1Style;
}
void rumbleClick() {
//if (!rumbleInitialized) {
// initRumble();
// if (!rumbleInitialized) return;
// Use cached style bit instead of querying hid each call
//const HidVibrationValue* pattern = cachedHandheldStyle ? &clickHandheld : &clickDocked;
sendVibration(&vibrationStop);
//if (cachedHandheldStyle) {
// sendVibration(&clickHandheld);
// sendVibration(&clickHandheld);
//} else {
// sendVibration(&clickDocked);
// sendVibration(&clickDocked);
//}
sendVibration(hidGetNpadStyleSet(HidNpadIdType_Handheld) ? &clickHandheld : &clickDocked);
rumbleActive.store(true, std::memory_order_release);
sendVibration2x(&hapticsPreset);
clickActive.store(true, std::memory_order_release);
rumbleStartTick = armGetSystemTick();
}
void rumbleDoubleClick() {
//if (!rumbleInitialized) {
// initRumble();
// if (!rumbleInitialized) return;
//onst HidVibrationValue* pattern = cachedHandheldStyle ? &clickHandheld : &clickDocked;
sendVibration(&vibrationStop);
//if (cachedHandheldStyle) {
// sendVibration(&clickHandheld);
// sendVibration(&clickHandheld);
//} else {
// sendVibration(&clickDocked);
// sendVibration(&clickDocked);
//}
sendVibration(hidGetNpadStyleSet(HidNpadIdType_Handheld) ? &clickHandheld : &clickDocked);
sendVibration2x(&hapticsPreset);
doubleClickActive.store(true, std::memory_order_release);
doubleClickPulse = 1;
doubleClickTick = armGetSystemTick();
doubleClickTick = armGetSystemTick(); // Set ONCE
}
void processRumbleStop(u64 nowNs) {
if (rumbleActive.load(std::memory_order_acquire) &&
if (clickActive.load(std::memory_order_acquire) &&
nowNs - armTicksToNs(rumbleStartTick) >= RUMBLE_DURATION_NS) {
sendVibration(&vibrationStop);
rumbleActive.store(false, std::memory_order_release);
clickActive.store(false, std::memory_order_release);
}
}
void processRumbleDoubleClick(u64 nowNs) {
if (!doubleClickActive.load(std::memory_order_acquire)) return;
const u64 elapsed = nowNs - armTicksToNs(doubleClickTick);
const u64 elapsed = nowNs - armTicksToNs(doubleClickTick); // Always from original start
switch (doubleClickPulse) {
case 1:
if (elapsed >= DOUBLE_CLICK_PULSE_DURATION_NS) {
sendVibration(&vibrationStop);
doubleClickPulse = 2;
doubleClickTick = armGetSystemTick();
// Don't reset tick!
}
break;
case 2:
if (elapsed >= DOUBLE_CLICK_GAP_NS) {
sendVibration(hidGetNpadStyleSet(HidNpadIdType_Handheld) ? &clickHandheld : &clickDocked);
if (elapsed >= DOUBLE_CLICK_PULSE_DURATION_NS + DOUBLE_CLICK_GAP_NS) {
// Use cached style here too
//if (cachedHandheldStyle) {
// sendVibration(&clickHandheld);
// sendVibration(&clickHandheld);
//} else {
// sendVibration(&clickDocked);
// sendVibration(&clickDocked);
//}
sendVibration2x(&hapticsPreset);
doubleClickPulse = 3;
doubleClickTick = armGetSystemTick();
// Don't reset tick!
}
break;
case 3:
if (elapsed >= DOUBLE_CLICK_PULSE_DURATION_NS) {
if (elapsed >= (DOUBLE_CLICK_PULSE_DURATION_NS * 2) + DOUBLE_CLICK_GAP_NS) {
sendVibration(&vibrationStop);
doubleClickActive.store(false, std::memory_order_release);
doubleClickPulse = 0;
@@ -187,16 +228,34 @@ namespace ult {
}
}
void rumbleDoubleClickStandalone() {
sendVibration(hidGetNpadStyleSet(HidNpadIdType_Handheld) ? &clickHandheld : &clickDocked);
// Standalone uses sleeps, but still use cached style for decision
//const HidVibrationValue* pattern = cachedHandheldStyle ? &clickHandheld : &clickDocked;
sendVibration(&vibrationStop);
//if (cachedHandheldStyle) {
// sendVibration(&clickHandheld);
// sendVibration(&clickHandheld);
//} else {
// sendVibration(&clickDocked);
// sendVibration(&clickDocked);
//}
sendVibration2x(&hapticsPreset);
svcSleepThread(DOUBLE_CLICK_PULSE_DURATION_NS);
sendVibration(&vibrationStop);
svcSleepThread(DOUBLE_CLICK_GAP_NS);
sendVibration(hidGetNpadStyleSet(HidNpadIdType_Handheld) ? &clickHandheld : &clickDocked);
//if (cachedHandheldStyle) {
// sendVibration(&clickHandheld);
// sendVibration(&clickHandheld);
//} else {
// sendVibration(&clickDocked);
// sendVibration(&clickDocked);
//}
sendVibration2x(&hapticsPreset);
svcSleepThread(DOUBLE_CLICK_PULSE_DURATION_NS);
sendVibration(&vibrationStop);
}
}

View File

@@ -15,7 +15,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#include "hex_funcs.hpp"
@@ -229,15 +229,16 @@ namespace ult {
const size_t fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
std::vector<unsigned char> binaryData;
if (hexData.length() % 2 != 0) {
fclose(file);
return offsets;
}
const size_t hexLen = hexData.length();
binaryData.resize(hexLen / 2);
const size_t patternLen = hexLen / 2;
// Use heap allocation for the buffer to avoid stack overflow with large buffer sizes
std::unique_ptr<unsigned char[]> binaryData(new unsigned char[patternLen]);
const unsigned char* hexPtr = reinterpret_cast<const unsigned char*>(hexData.c_str());
// Unrolled hex conversion loop
@@ -252,17 +253,17 @@ namespace ult {
}
// Optimized search variables
const unsigned char* patternPtr = binaryData.data();
const size_t patternLen = binaryData.size();
const unsigned char* patternPtr = binaryData.get();
const unsigned char firstByte = patternPtr[0];
std::vector<unsigned char> buffer(HEX_BUFFER_SIZE);
// Use heap allocation for the buffer to avoid stack overflow with large buffer sizes
std::unique_ptr<unsigned char[]> buffer(new unsigned char[HEX_BUFFER_SIZE]);
size_t bytesRead = 0;
size_t offset = 0;
while ((bytesRead = fread(buffer.data(), 1, HEX_BUFFER_SIZE, file)) > 0) {
const unsigned char* bufPtr = buffer.data();
while ((bytesRead = fread(buffer.get(), 1, HEX_BUFFER_SIZE, file)) > 0) {
const unsigned char* bufPtr = buffer.get();
// Optimized search with first-byte filtering and loop unrolling
i = 0;
@@ -322,15 +323,16 @@ namespace ult {
const size_t fileSize = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<unsigned char> binaryData;
if (hexData.length() % 2 != 0) {
file.close();
return offsets;
}
const size_t hexLen = hexData.length();
binaryData.resize(hexLen / 2);
const size_t patternLen = hexLen / 2;
// Use heap allocation for the buffer to avoid stack overflow with large buffer sizes
std::unique_ptr<unsigned char[]> binaryData(new unsigned char[patternLen]);
const unsigned char* hexPtr = reinterpret_cast<const unsigned char*>(hexData.c_str());
size_t i = 0;
@@ -342,17 +344,17 @@ namespace ult {
binaryData[i/2] = (hexTable[hexPtr[i]] << 4) | hexTable[hexPtr[i + 1]];
}
const unsigned char* patternPtr = binaryData.data();
const size_t patternLen = binaryData.size();
const unsigned char* patternPtr = binaryData.get();
const unsigned char firstByte = patternPtr[0];
std::vector<unsigned char> buffer(HEX_BUFFER_SIZE);
// Use heap allocation for the buffer to avoid stack overflow with large buffer sizes
std::unique_ptr<unsigned char[]> buffer(new unsigned char[HEX_BUFFER_SIZE]);
size_t bytesRead = 0;
size_t offset = 0;
while (file.read(reinterpret_cast<char*>(buffer.data()), HEX_BUFFER_SIZE) || file.gcount() > 0) {
while (file.read(reinterpret_cast<char*>(buffer.get()), HEX_BUFFER_SIZE) || file.gcount() > 0) {
bytesRead = file.gcount();
const unsigned char* bufPtr = buffer.data();
const unsigned char* bufPtr = buffer.get();
// Same optimized search as FILE* version
i = 0;
@@ -418,7 +420,6 @@ namespace ult {
void hexEditByOffset(const std::string& filePath, const std::string& offsetStr, const std::string& hexData) {
// Lock file writes to prevent concurrent modifications to the same file
std::lock_guard<std::mutex> fileWriteLock(fileWriteMutex);
const std::streampos offset = std::stoll(offsetStr);
#if !USING_FSTREAM_DIRECTIVE
@@ -435,7 +436,6 @@ namespace ult {
// Retrieve the file size
fseek(file, 0, SEEK_END);
const std::streampos fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
if (offset >= fileSize) {
#if USING_LOGGING_DIRECTIVE
@@ -446,18 +446,37 @@ namespace ult {
return;
}
// Convert the hex string to binary data
std::vector<unsigned char> binaryData(hexData.length() / 2);
std::string byteString;
for (size_t i = 0, j = 0; i < hexData.length(); i += 2, ++j) {
byteString = hexData.substr(i, 2);
binaryData[j] = static_cast<unsigned char>(ult::stoi(byteString, nullptr, 16));
// Validate hex data length
const size_t hexLen = hexData.length();
if (hexLen % 2 != 0) {
#if USING_LOGGING_DIRECTIVE
if (!disableLogging)
logMessage("Invalid hex data length.");
#endif
fclose(file);
return;
}
// Convert the hex string to binary data using optimized lookup table
const size_t dataLen = hexLen / 2;
std::unique_ptr<unsigned char[]> binaryData(new unsigned char[dataLen]);
const unsigned char* hexPtr = reinterpret_cast<const unsigned char*>(hexData.c_str());
// Unrolled hex conversion loop (same as findHexDataOffsets)
size_t i = 0;
for (; i + 4 <= hexLen; i += 4) {
binaryData[i/2] = (hexTable[hexPtr[i]] << 4) | hexTable[hexPtr[i + 1]];
binaryData[i/2 + 1] = (hexTable[hexPtr[i + 2]] << 4) | hexTable[hexPtr[i + 3]];
}
// Handle remaining bytes
for (; i < hexLen; i += 2) {
binaryData[i/2] = (hexTable[hexPtr[i]] << 4) | hexTable[hexPtr[i + 1]];
}
// Move to the specified offset and write the binary data directly to the file
fseek(file, offset, SEEK_SET);
const size_t bytesWritten = fwrite(binaryData.data(), sizeof(unsigned char), binaryData.size(), file);
if (bytesWritten != binaryData.size()) {
const size_t bytesWritten = fwrite(binaryData.get(), sizeof(unsigned char), dataLen, file);
if (bytesWritten != dataLen) {
#if USING_LOGGING_DIRECTIVE
if (!disableLogging)
logMessage("Failed to write data to the file.");
@@ -481,7 +500,6 @@ namespace ult {
// Retrieve the file size
file.seekg(0, std::ios::end);
const std::streampos fileSize = file.tellg();
file.seekg(0, std::ios::beg);
if (offset >= fileSize) {
#if USING_LOGGING_DIRECTIVE
@@ -491,17 +509,34 @@ namespace ult {
return;
}
// Convert the hex string to binary data
std::vector<unsigned char> binaryData(hexData.length() / 2);
std::string byteString;
for (size_t i = 0, j = 0; i < hexData.length(); i += 2, ++j) {
byteString = hexData.substr(i, 2);
binaryData[j] = static_cast<unsigned char>(ult::stoi(byteString, nullptr, 16));
// Validate hex data length
const size_t hexLen = hexData.length();
if (hexLen % 2 != 0) {
#if USING_LOGGING_DIRECTIVE
if (!disableLogging)
logMessage("Invalid hex data length.");
#endif
return;
}
// Convert the hex string to binary data using optimized lookup table
const size_t dataLen = hexLen / 2;
std::unique_ptr<unsigned char[]> binaryData(new unsigned char[dataLen]);
const unsigned char* hexPtr = reinterpret_cast<const unsigned char*>(hexData.c_str());
// Unrolled hex conversion loop
size_t i = 0;
for (; i + 4 <= hexLen; i += 4) {
binaryData[i/2] = (hexTable[hexPtr[i]] << 4) | hexTable[hexPtr[i + 1]];
binaryData[i/2 + 1] = (hexTable[hexPtr[i + 2]] << 4) | hexTable[hexPtr[i + 3]];
}
for (; i < hexLen; i += 2) {
binaryData[i/2] = (hexTable[hexPtr[i]] << 4) | hexTable[hexPtr[i + 1]];
}
// Move to the specified offset and write the binary data directly to the file
file.seekp(offset);
file.write(reinterpret_cast<const char*>(binaryData.data()), binaryData.size());
file.write(reinterpret_cast<const char*>(binaryData.get()), dataLen);
if (!file) {
#if USING_LOGGING_DIRECTIVE
if (!disableLogging)
@@ -676,8 +711,10 @@ namespace ult {
}
const std::streampos totalOffset = hexSum + std::stoll(offsetStr);
std::vector<char> hexBuffer(length);
std::vector<char> hexStream(length * 2);
// Pre-allocate final string size to avoid reallocation
std::string result;
result.reserve(length * 2);
#if !USING_FSTREAM_DIRECTIVE
FILE* file = fopen(filePath.c_str(), "rb");
@@ -698,23 +735,41 @@ namespace ult {
return "";
}
const size_t bytesRead = fread(hexBuffer.data(), sizeof(char), length, file);
if (bytesRead == length) {
static constexpr char hexDigits[] = "0123456789ABCDEF";
for (size_t i = 0; i < length; ++i) {
hexStream[i * 2] = hexDigits[(hexBuffer[i] >> 4) & 0xF];
hexStream[i * 2 + 1] = hexDigits[hexBuffer[i] & 0xF];
}
} else {
// Use heap allocation for the buffer to avoid stack overflow with large buffer sizes
std::unique_ptr<unsigned char[]> buffer(new unsigned char[length]);
const size_t bytesRead = fread(buffer.get(), 1, length, file);
fclose(file);
if (bytesRead != length) {
#if USING_LOGGING_DIRECTIVE
if (!disableLogging)
logMessage("Error reading data from file or end of file reached.");
#endif
fclose(file);
return "";
}
fclose(file);
// Optimized hex conversion - directly build uppercase string
static constexpr char hexDigits[] = "0123456789ABCDEF";
result.resize(length * 2);
// Unrolled loop for better performance
size_t i = 0;
for (; i + 4 <= length; i += 4) {
result[i * 2] = hexDigits[(buffer[i] >> 4) & 0xF];
result[i * 2 + 1] = hexDigits[buffer[i] & 0xF];
result[i * 2 + 2] = hexDigits[(buffer[i + 1] >> 4) & 0xF];
result[i * 2 + 3] = hexDigits[buffer[i + 1] & 0xF];
result[i * 2 + 4] = hexDigits[(buffer[i + 2] >> 4) & 0xF];
result[i * 2 + 5] = hexDigits[buffer[i + 2] & 0xF];
result[i * 2 + 6] = hexDigits[(buffer[i + 3] >> 4) & 0xF];
result[i * 2 + 7] = hexDigits[buffer[i + 3] & 0xF];
}
// Handle remaining bytes
for (; i < length; ++i) {
result[i * 2] = hexDigits[(buffer[i] >> 4) & 0xF];
result[i * 2 + 1] = hexDigits[buffer[i] & 0xF];
}
#else
std::ifstream file(filePath, std::ios::binary);
if (!file) {
@@ -734,14 +789,12 @@ namespace ult {
return "";
}
file.read(hexBuffer.data(), length);
if (file.gcount() == static_cast<std::streamsize>(length)) {
static constexpr char hexDigits[] = "0123456789ABCDEF";
for (size_t i = 0; i < length; ++i) {
hexStream[i * 2] = hexDigits[(hexBuffer[i] >> 4) & 0xF];
hexStream[i * 2 + 1] = hexDigits[hexBuffer[i] & 0xF];
}
} else {
// Use heap allocation for the buffer to avoid stack overflow with large buffer sizes
std::unique_ptr<unsigned char[]> buffer(new unsigned char[length]);
file.read(reinterpret_cast<char*>(buffer.get()), length);
file.close();
if (file.gcount() != static_cast<std::streamsize>(length)) {
#if USING_LOGGING_DIRECTIVE
if (!disableLogging)
logMessage("Error reading data from file or end of file reached.");
@@ -749,12 +802,29 @@ namespace ult {
return "";
}
file.close();
// Optimized hex conversion - directly build uppercase string
static constexpr char hexDigits[] = "0123456789ABCDEF";
result.resize(length * 2);
// Unrolled loop for better performance
size_t i = 0;
for (; i + 4 <= length; i += 4) {
result[i * 2] = hexDigits[(buffer[i] >> 4) & 0xF];
result[i * 2 + 1] = hexDigits[buffer[i] & 0xF];
result[i * 2 + 2] = hexDigits[(buffer[i + 1] >> 4) & 0xF];
result[i * 2 + 3] = hexDigits[buffer[i + 1] & 0xF];
result[i * 2 + 4] = hexDigits[(buffer[i + 2] >> 4) & 0xF];
result[i * 2 + 5] = hexDigits[buffer[i + 2] & 0xF];
result[i * 2 + 6] = hexDigits[(buffer[i + 3] >> 4) & 0xF];
result[i * 2 + 7] = hexDigits[buffer[i + 3] & 0xF];
}
// Handle remaining bytes
for (; i < length; ++i) {
result[i * 2] = hexDigits[(buffer[i] >> 4) & 0xF];
result[i * 2 + 1] = hexDigits[buffer[i] & 0xF];
}
#endif
std::string result(hexStream.begin(), hexStream.end());
result = stringToUppercase(result);
return result;
}
@@ -878,4 +948,71 @@ namespace ult {
return ""; // Return empty string if no match is found
}
// 1. Table optimization: Mark as constexpr for compile-time evaluation
static constexpr uint8_t b64_table[256] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,62, 0xFF,0xFF,0xFF,63,
52,53,54,55,56,57,58,59,60,61,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
// 2. Optimized decode: Pre-calculate output size, reduce bounds checking
static size_t base64_decode(const char* src, uint8_t* out) {
size_t outLen = 0;
const char* p = src;
// Process 4 chars at a time (unrolled loop for better instruction pipelining)
while (*p) {
uint8_t a = b64_table[static_cast<uint8_t>(*p++)];
if (a == 0xFF) break;
uint8_t b = b64_table[static_cast<uint8_t>(*p++)];
if (b == 0xFF) break;
out[outLen++] = (a << 2) | (b >> 4);
uint8_t cChar = *p++;
if (cChar == '=' || cChar == '\0') break;
uint8_t c = b64_table[cChar];
if (c == 0xFF) break;
out[outLen++] = (b << 4) | (c >> 2);
uint8_t dChar = *p++;
if (dChar == '=' || dChar == '\0') break;
uint8_t d = b64_table[dChar];
if (d == 0xFF) break;
out[outLen++] = (c << 6) | d;
}
return outLen;
}
// 3. Optimized wrapper: Pre-calculate exact output size, avoid vector overhead
std::string decodeBase64ToString(const std::string& b64) {
// Base64 decodes to ~3/4 original size
const size_t maxOutSize = (b64.size() * 3) / 4 + 3;
std::string result(maxOutSize, '\0');
const size_t len = base64_decode(b64.c_str(), reinterpret_cast<uint8_t*>(result.data()));
result.resize(len);
return result;
}
}

View File

@@ -12,7 +12,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#include "json_funcs.hpp"

View File

@@ -14,7 +14,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#include <list_funcs.hpp>
@@ -84,9 +84,9 @@ namespace ult {
}
}
// Function to read file into a vector of strings with optional cap
std::vector<std::string> readListFromFile(const std::string& filePath, size_t maxLines) {
// Function to read file into a vector of strings with optional cap and newline preservation
std::vector<std::string> readListFromFile(const std::string& filePath, size_t maxLines, bool preserveNewlines) {
std::lock_guard<std::mutex> lock(file_access_mutex);
std::vector<std::string> lines;
@@ -109,18 +109,22 @@ namespace ult {
break;
}
// More efficient newline removal
len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0';
--len;
// Also remove carriage return if present
if (len > 0 && buffer[len - 1] == '\r') {
if (preserveNewlines) {
// Keep the line as-is, including newlines
lines.emplace_back(buffer);
} else {
// Remove newlines
len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0';
--len;
// Also remove carriage return if present
if (len > 0 && buffer[len - 1] == '\r') {
buffer[len - 1] = '\0';
}
}
lines.emplace_back(buffer);
}
lines.emplace_back(buffer);
}
fclose(file);
@@ -140,12 +144,17 @@ namespace ult {
break;
}
// Remove carriage return if present (getline removes \n but not \r)
if (!line.empty() && line.back() == '\r') {
line.pop_back();
if (preserveNewlines) {
// Add back the newline that getline removed
line += '\n';
lines.emplace_back(std::move(line));
} else {
// Remove carriage return if present (getline removes \n but not \r)
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
lines.emplace_back(std::move(line));
}
lines.emplace_back(std::move(line));
}
file.close();
@@ -282,7 +291,7 @@ namespace ult {
// Function to read file into a set of strings
std::unordered_set<std::string> readSetFromFile(const std::string& filePath) {
std::unordered_set<std::string> readSetFromFile(const std::string& filePath, const std::string& packagePath) {
std::lock_guard<std::mutex> lock(file_access_mutex);
std::unordered_set<std::string> lines;
@@ -304,7 +313,12 @@ namespace ult {
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0';
}
lines.insert(buffer);
std::string line = buffer;
if (!packagePath.empty()) {
preprocessPath(line, packagePath);
}
lines.insert(std::move(line));
}
fclose(file);
@@ -319,6 +333,9 @@ namespace ult {
std::string line;
while (std::getline(file, line)) {
if (!packagePath.empty()) {
preprocessPath(line, packagePath);
}
lines.insert(std::move(line));
}

View File

@@ -14,7 +14,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2024-2025 ppkantorski
********************************************************************************/
#include <mod_funcs.hpp>

View File

@@ -15,7 +15,7 @@
* of the project's documentation and must remain intact.
*
* Licensed under both GPLv2 and CC-BY-4.0
* Copyright (c) 2024 ppkantorski
* Copyright (c) 2023-2025 ppkantorski
********************************************************************************/
#include "string_funcs.hpp"
@@ -226,12 +226,14 @@ namespace ult {
* @return The string with quotes removed.
*/
void removeQuotes(std::string& str) {
if (str.size() >= 2) {
const size_t len = str.size();
if (len >= 2) {
const char front = str[0];
const char back = str[str.size() - 1];
const char back = str[len - 1];
if ((front == '\'' && back == '\'') || (front == '"' && back == '"')) {
str.erase(0, 1);
str.pop_back();
std::memmove(&str[0], &str[1], len - 2);
str.resize(len - 2);
}
}
}
@@ -245,25 +247,25 @@ namespace ult {
* @param input The input string to process.
* @return The string with multiple slashes replaced.
*/
std::string replaceMultipleSlashes(const std::string& input) {
std::string output;
output.reserve(input.size()); // Reserve space for the output string
bool previousSlash = false;
for (char c : input) {
if (c == '/') {
if (!previousSlash) {
output.push_back(c);
}
previousSlash = true;
} else {
output.push_back(c);
previousSlash = false;
}
}
return output;
}
//std::string replaceMultipleSlashes(const std::string& input) {
// std::string output;
// output.reserve(input.size()); // Reserve space for the output string
//
// bool previousSlash = false;
// for (char c : input) {
// if (c == '/') {
// if (!previousSlash) {
// output.push_back(c);
// }
// previousSlash = true;
// } else {
// output.push_back(c);
// previousSlash = false;
// }
// }
//
// return output;
//}
@@ -366,7 +368,7 @@ namespace ult {
* @param filename The input filename from which to drop the extension, passed by reference and modified in-place.
*/
void dropExtension(std::string& filename) {
const size_t lastDotPos = filename.find_last_of('.'); // Single char instead of string
const size_t lastDotPos = filename.rfind('.');
if (lastDotPos != std::string::npos) {
filename.resize(lastDotPos);
}
@@ -510,9 +512,12 @@ namespace ult {
* @param input The input string from which to remove the tag, passed by reference and modified in-place.
*/
void removeTag(std::string &input) {
const size_t pos = input.find('?');
if (pos != std::string::npos) {
input.resize(pos); // Modify the string in-place to remove everything after the '?'
const char* pos = static_cast<const char*>(
std::memchr(input.data(), '?', input.size())
);
if (pos) {
input.resize(pos - input.data());
}
}

View File

@@ -97,12 +97,33 @@ void AppProfileGui::addModuleListItem(SysClkProfile profile, SysClkModule module
this->listElement->addItem(listItem);
}
void AppProfileGui::addModuleListItemToggle(SysClkProfile profile, SysClkModule module)
{
const char* moduleName = sysclkFormatModule(module, true);
std::uint32_t currentValue = this->profileList->mhzMap[profile][module];
tsl::elm::ToggleListItem* toggle = new tsl::elm::ToggleListItem(moduleName, currentValue != 0);
toggle->setStateChangedListener([this, profile, module](bool state) {
this->profileList->mhzMap[profile][module] = state ? 1 : 0;
Result rc = sysclkIpcSetProfiles(this->applicationId, this->profileList);
if(R_FAILED(rc))
{
FatalGui::openWithResultCode("sysclkIpcSetProfiles", rc);
}
});
this->listElement->addItem(toggle);
}
void AppProfileGui::addProfileUI(SysClkProfile profile)
{
this->listElement->addItem(new tsl::elm::CategoryHeader(sysclkFormatProfile(profile, true) + std::string(" ") + ult::DIVIDER_SYMBOL + "  Reset"));
this->addModuleListItem(profile, SysClkModule_CPU);
this->addModuleListItem(profile, SysClkModule_GPU);
this->addModuleListItem(profile, SysClkModule_MEM);
this->addModuleListItemToggle(profile, HorizonOCModule_Governor);
}
void AppProfileGui::listUI()

View File

@@ -40,6 +40,7 @@ class AppProfileGui : public BaseMenuGui
void openFreqChoiceGui(tsl::elm::ListItem* listItem, SysClkProfile profile, SysClkModule module);
void addModuleListItem(SysClkProfile profile, SysClkModule module);
void addModuleListItemToggle(SysClkProfile profile, SysClkModule module);
void addProfileUI(SysClkProfile profile);
public:

View File

@@ -27,7 +27,6 @@
#include "base_gui.h"
#include "../elements/base_frame.h"
#include "logo_rgba_bin.h"
#include <tesla.hpp>
#include <math.h>

View File

@@ -96,16 +96,40 @@ void GlobalOverrideGui::addModuleListItem(SysClkModule module)
}
return false;
});
this->listElement->addItem(listItem);
this->listItems[module] = listItem;
}
void GlobalOverrideGui::addModuleToggleItem(SysClkModule module)
{
const char* moduleName = sysclkFormatModule(module, true);
bool isOn = this->listHz[module];
// Create a ToggleListItem
tsl::elm::ToggleListItem* toggle = new tsl::elm::ToggleListItem(moduleName, isOn);
toggle->setStateChangedListener([this, module, toggle](bool state) {
Result rc = sysclkIpcSetOverride(module, state ? 1 : 0);
if(R_FAILED(rc))
{
FatalGui::openWithResultCode("sysclkIpcSetProfiles", rc);
}
});
// Add to list and track
this->listElement->addItem(toggle);
this->listItems[module] = toggle;
}
void GlobalOverrideGui::listUI()
{
this->listElement->addItem(new tsl::elm::CategoryHeader("Temporary Overrides " + ult::DIVIDER_SYMBOL + "  Reset"));
this->addModuleListItem(SysClkModule_CPU);
this->addModuleListItem(SysClkModule_GPU);
this->addModuleListItem(SysClkModule_MEM);
this->addModuleToggleItem(HorizonOCModule_Governor);
}
void GlobalOverrideGui::refresh()
@@ -115,6 +139,8 @@ void GlobalOverrideGui::refresh()
{
for(std::uint16_t m = 0; m < SysClkModule_EnumMax; m++)
{
if(m > SysClkModule_MEM)
continue;
if(this->listItems[m] != nullptr && this->listHz[m] != this->context->overrideFreqs[m])
{
this->listItems[m]->setValue(formatListFreqHz(this->context->overrideFreqs[m]));

View File

@@ -39,7 +39,7 @@ class GlobalOverrideGui : public BaseMenuGui
void openFreqChoiceGui(SysClkModule module);
void addModuleListItem(SysClkModule module);
void addModuleToggleItem(SysClkModule module);
public:
GlobalOverrideGui();
~GlobalOverrideGui() {}

View File

@@ -399,8 +399,6 @@ void MiscGui::listUI()
chargerCurrents,
false
);
addConfigToggle(HocClkConfigValue_HandheldGovernor, nullptr);
}

BIN
Source/sys-clk/sys-clk.zip Normal file

Binary file not shown.

View File

@@ -37,7 +37,9 @@
#include "notification.h"
#define HOSPPC_HAS_BOOST (hosversionAtLeast(7,0,0))
bool isGovernorEnabled = false; // to avoid thread messes
bool lastGovernorState = false;
bool hasChanged = true;
ClockManager *ClockManager::instance = NULL;
Thread governorTHREAD;
@@ -276,7 +278,7 @@ void ClockManager::GovernorThread(void* arg)
std::scoped_lock lock{mgr->contextMutex};
if (!mgr->config->GetConfigValue(HocClkConfigValue_HandheldGovernor))
if (!isGovernorEnabled)
{
svcSleepThread(50'000'000);
continue;
@@ -388,7 +390,6 @@ void ClockManager::Tick()
}
}
if(((tmp451TempSoc() / 1000) > (int)this->config->GetConfigValue(HocClkConfigValue_ThermalThrottleThreshold)) && this->config->GetConfigValue(HocClkConfigValue_ThermalThrottle)) {
ResetToStockClocks();
return;
@@ -402,47 +403,66 @@ void ClockManager::Tick()
std::uint32_t maxHz = 0;
std::uint32_t nearestHz = 0;
if(apmExtIsBoostMode(mode) && !this->config->GetConfigValue(HocClkConfigValue_OverwriteBoostMode)) {
ResetToStockClocks();
return;
}
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++)
if(apmExtIsBoostMode(mode) && !this->config->GetConfigValue(HocClkConfigValue_OverwriteBoostMode)) {
ResetToStockClocks();
return;
}
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++)
{
targetHz = this->context->overrideFreqs[module];
if (!targetHz)
{
if(this->config->GetConfigValue(HocClkConfigValue_HandheldGovernor)) {
noGPU = true;
} else {
noGPU = false;
}
if(noGPU && module == SysClkModule_GPU)
continue;
targetHz = this->context->overrideFreqs[module];
if (!targetHz)
{
targetHz = this->config->GetAutoClockHz(this->context->applicationId, (SysClkModule)module, this->context->profile);
if(!targetHz)
targetHz = this->config->GetAutoClockHz(GLOBAL_PROFILE_ID, (SysClkModule)module, this->context->profile);
}
targetHz = this->config->GetAutoClockHz(this->context->applicationId, (SysClkModule)module, this->context->profile);
if(!targetHz)
targetHz = this->config->GetAutoClockHz(GLOBAL_PROFILE_ID, (SysClkModule)module, this->context->profile);
}
if (targetHz)
{
if(module == HorizonOCModule_Governor) {
bool newGovernorState = targetHz;
if(newGovernorState != lastGovernorState) {
FileUtils::LogLine("[mgr] Governor state changed: %s", newGovernorState ? "enabled" : "disabled");
lastGovernorState = newGovernorState;
maxHz = this->GetMaxAllowedHz((SysClkModule)module, this->context->profile);
nearestHz = this->GetNearestHz((SysClkModule)module, targetHz, maxHz);
if (nearestHz != this->context->freqs[module] && this->context->enabled) {
FileUtils::LogLine(
"[mgr] %s clock set : %u.%u MHz (target = %u.%u MHz)",
Board::GetModuleName((SysClkModule)module, true),
nearestHz / 1000000, nearestHz / 100000 - nearestHz / 1000000 * 10,
targetHz / 1000000, targetHz / 100000 - targetHz / 1000000 * 10);
// Force a "context refresh" like on app/profile change
hasChanged = true;
this->context->enabled = this->GetConfig()->Enabled();
Board::ResetToStock(); // optional: reset clocks before re-applying
}
isGovernorEnabled = newGovernorState;
}
Board::SetHz((SysClkModule)module, nearestHz);
this->context->freqs[module] = nearestHz;
}
}
// Skip GPU if governor handles it
if(module > SysClkModule_MEM) {
continue;
}
if(isGovernorEnabled) {
noGPU = true;
} else {
noGPU = false;
}
if(noGPU && module == SysClkModule_GPU)
continue;
if (targetHz)
{
maxHz = this->GetMaxAllowedHz((SysClkModule)module, this->context->profile);
nearestHz = this->GetNearestHz((SysClkModule)module, targetHz, maxHz);
if (nearestHz != this->context->freqs[module] && this->context->enabled) {
FileUtils::LogLine(
"[mgr] %s clock set : %u.%u MHz (target = %u.%u MHz)",
Board::GetModuleName((SysClkModule)module, true),
nearestHz / 1000000, nearestHz / 100000 - nearestHz / 1000000 * 10,
targetHz / 1000000, targetHz / 100000 - targetHz / 1000000 * 10
);
Board::SetHz((SysClkModule)module, nearestHz);
this->context->freqs[module] = nearestHz;
}
}
}
}
}
void ClockManager::ResetToStockClocks() {