mirror of
https://github.com/DarkMatterCore/nxdumptool.git
synced 2024-11-29 21:52:22 +00:00
168 lines
6.3 KiB
C
168 lines
6.3 KiB
C
/*
|
|
* utils.h
|
|
*
|
|
* Copyright (c) 2020-2021, DarkMatterCore <pabloacurielz@gmail.com>.
|
|
*
|
|
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
|
|
*
|
|
* nxdumptool is free software; you can redistribute it and/or modify it
|
|
* under the terms and conditions of the GNU General Public License,
|
|
* version 2, as published by the Free Software Foundation.
|
|
*
|
|
* nxdumptool is distributed in the hope it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
* more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#ifndef __UTILS_H__
|
|
#define __UTILS_H__
|
|
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stddef.h>
|
|
#include <stdarg.h>
|
|
#include <malloc.h>
|
|
#include <errno.h>
|
|
#include <ctype.h>
|
|
#include <math.h>
|
|
#include <time.h>
|
|
#include <sys/stat.h>
|
|
#include <stdatomic.h>
|
|
#include <assert.h>
|
|
#include <switch.h>
|
|
|
|
#include "common.h"
|
|
#include "ums.h"
|
|
|
|
#define APP_BASE_PATH "sdmc:/switch/" APP_TITLE "/"
|
|
|
|
#define MEMBER_SIZE(type, member) sizeof(((type*)NULL)->member)
|
|
|
|
#define MAX_ELEMENTS(x) ((sizeof((x))) / (sizeof((x)[0])))
|
|
|
|
#define LOGFILE(fmt, ...) utilsWriteMessageToLogFile(__func__, fmt, ##__VA_ARGS__)
|
|
#define LOGBUF(dst, dst_size, fmt, ...) utilsWriteMessageToLogBuffer(dst, dst_size, __func__, fmt, ##__VA_ARGS__)
|
|
|
|
#define BIT_LONG(n) (1UL << (n))
|
|
|
|
#define ALIGN_UP(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
|
|
#define ALIGN_DOWN(x, y) ((x) > (y) ? (((x) - ((y) - 1)) & ~((y) - 1)) : 0)
|
|
#define IS_ALIGNED(x, y) (((x) & ((y) - 1)) == 0)
|
|
|
|
#define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0)
|
|
|
|
#define BIS_SYSTEM_PARTITION_MOUNT_NAME "sys:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Used to determine which CFW is the application running under.
|
|
typedef enum {
|
|
UtilsCustomFirmwareType_Unknown = 0,
|
|
UtilsCustomFirmwareType_Atmosphere = 1,
|
|
UtilsCustomFirmwareType_SXOS = 2,
|
|
UtilsCustomFirmwareType_ReiNX = 3
|
|
} UtilsCustomFirmwareType;
|
|
|
|
/// Resource (de)initialization.
|
|
/// Called at program startup.
|
|
bool utilsInitializeResources(void);
|
|
void utilsCloseResources(void);
|
|
|
|
/// Thread management functions.
|
|
bool utilsCreateThread(Thread *out_thread, ThreadFunc func, void *arg, int cpu_id);
|
|
void utilsJoinThread(Thread *thread);
|
|
|
|
/// Returns true if the application is running under a development unit.
|
|
bool utilsIsDevelopmentUnit(void);
|
|
|
|
/// Functions to retrieve down/held buttons from all input controllers.
|
|
/// utilsScanPads() must be called before utilsGetButtonsDown() / utilsGetButtonsHeld().
|
|
void utilsScanPads(void);
|
|
u64 utilsGetButtonsDown(void);
|
|
u64 utilsGetButtonsHeld(void);
|
|
|
|
/// Waits until any button matching the provided input flag is pressed.
|
|
/// If 'flag' is set to zero, any button press will count.
|
|
void utilsWaitForButtonPress(u64 flag);
|
|
|
|
/// Formats a string and appends it to the provided buffer.
|
|
/// If the buffer isn't big enough to hold both its current contents and the new formatted string, it will be resized.
|
|
bool utilsAppendFormattedStringToBuffer(char **dst, size_t *dst_size, const char *fmt, ...);
|
|
|
|
/// Logfile management functions.
|
|
void utilsWriteMessageToLogFile(const char *func_name, const char *fmt, ...);
|
|
void utilsWriteMessageToLogBuffer(char **dst, size_t *dst_size, const char *func_name, const char *fmt, ...);
|
|
void utilsWriteLogBufferToLogFile(const char *src);
|
|
void utilsLogFileMutexControl(bool lock);
|
|
|
|
/// Replaces illegal FAT characters in the provided string with underscores.
|
|
/// If 'ascii_only' is set to true, all characters outside the (0x20,0x7E] range will also be replaced with underscores.
|
|
void utilsReplaceIllegalCharacters(char *str, bool ascii_only);
|
|
|
|
/// Trims whitespace characters from the provided string.
|
|
void utilsTrimString(char *str);
|
|
|
|
/// Generates a lowercase hex string representation of the binary data stored in 'src' and stores it in 'dst'.
|
|
void utilsGenerateHexStringFromData(char *dst, size_t dst_size, const void *src, size_t src_size);
|
|
|
|
/// Formats the provided 'size' value to a human readable size string and stores it in 'dst'.
|
|
void utilsGenerateFormattedSizeString(u64 size, char *dst, size_t dst_size);
|
|
|
|
/// Saves the total size and free space available from the filesystem pointed to by the input path (e.g. "sdmc:/") to 'out_total' and 'out_free', respectively.
|
|
/// Either 'out_total' or 'out_free' can be set to NULL, but at least one of them must be set to a valid pointer.
|
|
/// Returns false if there's an error.
|
|
bool utilsGetFileSystemStatsByPath(const char *path, u64 *out_total, u64 *out_free);
|
|
|
|
/// Commits SD card filesystem changes.
|
|
/// Must be used after closing a file handle from the SD card.
|
|
bool utilsCommitSdCardFileSystemChanges(void);
|
|
|
|
/// Returns true if a file exists.
|
|
bool utilsCheckIfFileExists(const char *path);
|
|
|
|
/// Deletes a ConcatenationFile located at the input path.
|
|
void utilsRemoveConcatenationFile(const char *path);
|
|
|
|
/// Creates a ConcatenationFile at the input path.
|
|
bool utilsCreateConcatenationFile(const char *path);
|
|
|
|
/// Creates a full directory tree using the provided path.
|
|
/// If 'create_last_element' is true, the last element from the provided path will be created as well.
|
|
void utilsCreateDirectoryTree(const char *path, bool create_last_element);
|
|
|
|
/// Returns a pointer to a dynamically allocated string that holds the full path formed by the provided arguments.
|
|
char *utilsGeneratePath(const char *prefix, const char *filename, const char *extension);
|
|
|
|
/// Returns true if the application is running under Applet Mode.
|
|
bool utilsAppletModeCheck(void);
|
|
|
|
/// (Un)blocks HOME button presses.
|
|
void utilsChangeHomeButtonBlockStatus(bool block);
|
|
|
|
/// Returns a UtilsCustomFirmwareType value.
|
|
u8 utilsGetCustomFirmwareType(void);
|
|
|
|
/// Returns a pointer to the FsStorage object for the eMMC BIS System partition.
|
|
FsStorage *utilsGetEmmcBisSystemPartitionStorage(void);
|
|
|
|
/// Enables/disables CPU/MEM overclocking.
|
|
void utilsOverclockSystem(bool overclock);
|
|
|
|
/// Simple wrapper to sleep the current thread for a specific number of full seconds.
|
|
NX_INLINE void utilsSleep(u64 seconds)
|
|
{
|
|
if (seconds) svcSleepThread(seconds * (u64)1000000000);
|
|
}
|
|
|
|
#endif /* __UTILS_H__ */
|