From b98b739920cd5a9fe813d72288747e00115c2ef8 Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 3 Mar 2022 23:50:19 +0100 Subject: [PATCH] first commit --- .clang-format | 67 +++++++++++++ .github/workflows/pr.yml | 25 +++++ .github/workflows/push_image.yml | 33 +++++++ .gitignore | 9 ++ Dockerfile | 9 ++ Dockerfile.buildlocal | 3 + Makefile | 160 +++++++++++++++++++++++++++++++ README.md | 29 ++++++ include/sdutils/sdutils.h | 79 +++++++++++++++ source/utils.cpp | 69 +++++++++++++ 10 files changed, 483 insertions(+) create mode 100644 .clang-format create mode 100644 .github/workflows/pr.yml create mode 100644 .github/workflows/push_image.yml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 Dockerfile.buildlocal create mode 100644 Makefile create mode 100644 README.md create mode 100644 include/sdutils/sdutils.h create mode 100644 source/utils.cpp diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..56cc685 --- /dev/null +++ b/.clang-format @@ -0,0 +1,67 @@ +# Generated from CLion C/C++ Code Style settings +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: Consecutive +AlignConsecutiveMacros: AcrossEmptyLinesAndComments +AlignOperands: Align +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Always +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterReturnType: None +AlwaysBreakTemplateDeclarations: Yes +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: true +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +ColumnLimit: 0 +CompactNamespaces: false +ContinuationIndentWidth: 8 +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 4 +KeepEmptyLinesAtTheStartOfBlocks: true +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: All +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PointerAlignment: Right +ReflowComments: false +SpaceAfterCStyleCast: true +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Never diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 0000000..54f5094 --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,25 @@ +name: CI-PR + +on: [pull_request] + +jobs: + clang-format: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - name: clang-format + run: | + docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source ./include + build-lib: + runs-on: ubuntu-18.04 + needs: clang-format + steps: + - uses: actions/checkout@v2 + - name: build lib + run: | + docker build . -f Dockerfile.buildlocal -t builder + docker run --rm -v ${PWD}:/project builder make + - uses: actions/upload-artifact@master + with: + name: lib + path: "lib/*.a" \ No newline at end of file diff --git a/.github/workflows/push_image.yml b/.github/workflows/push_image.yml new file mode 100644 index 0000000..df6eb37 --- /dev/null +++ b/.github/workflows/push_image.yml @@ -0,0 +1,33 @@ +name: Publish Docker Image +on: + push: + branches: + - master +jobs: + clang-format: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - name: clang-format + run: | + docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source ./include + build: + runs-on: ubuntu-latest + needs: clang-format + steps: + - uses: actions/checkout@master + - name: Get release version + id: get_release_tag + run: | + echo RELEASE_VERSION=$(echo $(date '+%Y%m%d')) >> $GITHUB_ENV + echo REPOSITORY_NAME=$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $2}' | sed -e "s/:refs//" | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV + echo REPOSITORY_OWNER=$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $1}' | sed 's/[^a-zA-Z0-9]//g' | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV + - name: Publish to Registry + uses: elgohr/Publish-Docker-Github-Action@master + with: + name: ${{ env.REPOSITORY_OWNER }}/${{ env.REPOSITORY_NAME }} + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + snapshot: true + cache: true + tags: "latest, ${{ env.RELEASE_VERSION }}" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f6ace4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/*.a +/build +*.bz2 +release/ +lib/ +CMakeLists.txt +.idea/ +cmake-build-debug/ +share/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..82d9b22 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM wiiuenv/devkitppc:20220303 + +WORKDIR tmp_build +COPY . . +RUN make clean && make && mkdir -p /artifacts/wums && cp -r lib /artifacts/wums && cp -r include /artifacts/wums && cp -r share /artifacts/wums +WORKDIR /artifacts + +FROM scratch +COPY --from=0 /artifacts /artifacts \ No newline at end of file diff --git a/Dockerfile.buildlocal b/Dockerfile.buildlocal new file mode 100644 index 0000000..1b9fe75 --- /dev/null +++ b/Dockerfile.buildlocal @@ -0,0 +1,3 @@ +FROM wiiuenv/devkitppc:20220303 + +WORKDIR project \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d338b63 --- /dev/null +++ b/Makefile @@ -0,0 +1,160 @@ +#------------------------------------------------------------------------------- +.SUFFIXES: +#------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITPRO)),) +$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") +endif + +TOPDIR ?= $(CURDIR) + +include $(DEVKITPRO)/wut/share/wut_rules + +export VER_MAJOR := 1 +export VER_MINOR := 0 +export VER_PATCH := 0 + +VERSION := $(VER_MAJOR).$(VER_MINOR).$(VER_PATCH) + +#------------------------------------------------------------------------------- +# TARGET is the name of the output +# BUILD is the directory where object files & intermediate files will be placed +# SOURCES is a list of directories containing source code +# DATA is a list of directories containing data files +# INCLUDES is a list of directories containing header files +#------------------------------------------------------------------------------- +TARGET := $(notdir $(CURDIR)) +BUILD := build +SOURCES := source +DATA := data +INCLUDES := source \ + include \ + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- +CFLAGS := -Wall -Werror -save-temps \ + -ffunction-sections -fdata-sections \ + $(MACHDEP) \ + $(BUILD_CFLAGS) + +CFLAGS += $(INCLUDE) -D__WIIU__ + +CXXFLAGS := $(CFLAGS) -std=gnu++17 + +ASFLAGS := $(MACHDEP) + +LDFLAGS = $(ARCH) -Wl,--gc-sections + + +LIBS := + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := $(PORTLIBS) $(WUT_ROOT) + +#--------------------------------------------------------------------------------- +# no real need to edit anything past this point unless you need to add additional +# rules for different file extensions +#--------------------------------------------------------------------------------- +ifneq ($(BUILD),$(notdir $(CURDIR))) +#--------------------------------------------------------------------------------- + +export TOPDIR := $(CURDIR) + +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) + +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) +SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) +DEFFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.def))) +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) + +#--------------------------------------------------------------------------------- +# use CXX for linking C++ projects, CC for standard C +#--------------------------------------------------------------------------------- +ifeq ($(strip $(CPPFILES)),) +#--------------------------------------------------------------------------------- + export LD := $(CC) +#--------------------------------------------------------------------------------- +else +#--------------------------------------------------------------------------------- + export LD := $(CXX) +#--------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------- + +export OFILES_BIN := $(addsuffix .o,$(BINFILES)) +export OFILES_SRC := $(DEFFILES:.def=.o) $(SFILES:.s=.o) $(CFILES:.c=.o) $(CPPFILES:.cpp=.o) +export OFILES := $(OFILES_BIN) $(OFILES_SRC) +export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) + +export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ + -I. + +.PHONY: all dist-bin dist-src dist install clean + +#--------------------------------------------------------------------------------- +all: lib/libsdutils.a + +dist-bin: all + @tar --exclude=*~ -cjf libsdutils-$(VERSION).tar.bz2 include lib share + +dist-src: + @tar --exclude=*~ -cjf libsdutils-src-$(VERSION).tar.bz2 include source Makefile + +dist: dist-src dist-bin + +install: dist-bin + mkdir -p $(DESTDIR)$(DEVKITPRO)/wums + bzip2 -cd libsdutils-$(VERSION).tar.bz2 | tar -xf - -C $(DESTDIR)$(DEVKITPRO)/wums + +lib: + @[ -d $@ ] || mkdir -p $@ + +share: + @[ -d $@ ] || mkdir -p $@ + +release: + @[ -d $@ ] || mkdir -p $@ + +lib/libsdutils.a :$(SOURCES) $(INCLUDES) | lib release + @$(MAKE) BUILD=release OUTPUT=$(CURDIR)/$@ \ + BUILD_CFLAGS="-DNDEBUG=1 -O2 -s" \ + DEPSDIR=$(CURDIR)/release \ + --no-print-directory -C release \ + -f $(CURDIR)/Makefile + +#--------------------------------------------------------------------------------- +clean: + @echo clean ... + @rm -rf release lib + +#--------------------------------------------------------------------------------- +else + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +$(OUTPUT) : $(OFILES) + +$(OFILES_SRC) : $(HFILES) + +#--------------------------------------------------------------------------------- +%_bin.h %.bin.o : %.bin +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- diff --git a/README.md b/README.md new file mode 100644 index 0000000..f8762a4 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +[![Publish Docker Image](https://github.com/wiiu-env/libsdutils/actions/workflows/push_image.yml/badge.svg)](https://github.com/wiiu-env/libsdutils/actions/workflows/push_image.yml) + +# libsdutils +Requires the [SDHotSwapModule](https://github.com/wiiu-env/SDHotSwapModule) to be running via [WUMSLoader](https://github.com/wiiu-env/WUMSLoader). +Requires [wut](https://github.com/devkitPro/wut) for building. +Install via `make install`. + +## Usage +Make sure to define +``` +WUMS_ROOT := $(DEVKITPRO)/wums +``` +and add `-lkernel` to `LIBS` and `$(WUMS_ROOT)` to `LIBDIRS`. + +After that you can simply include `` to get access to the sdutils functions. + +## Use this lib in Dockerfiles. +A prebuilt version of this lib can found on dockerhub. To use it for your projects, add this to your Dockerfile. +``` +[...] +COPY --from=wiiuenv/libsdutils:[tag] /artifacts $DEVKITPRO +[...] +``` +Replace [tag] with a tag you want to use, a list of tags can be found [here](https://hub.docker.com/r/wiiuenv/libsdutils/tags). +It's highly recommended to pin the version to the **latest date** instead of using `latest`. + +## Format the code via docker + +`docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source ./include -i` \ No newline at end of file diff --git a/include/sdutils/sdutils.h b/include/sdutils/sdutils.h new file mode 100644 index 0000000..e39fb2b --- /dev/null +++ b/include/sdutils/sdutils.h @@ -0,0 +1,79 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +enum SDUtilsStatus { + SDUTILS_RESULT_SUCCESS = 0, + SDUTILS_RESULT_MODULE_NOT_FOUND = -1, + SDUTILS_RESULT_MODULE_MISSING_EXPORT = -2, + SDUTILS_RESULT_MAX_CALLBACKS = -3, + SDUTILS_RESULT_NOT_FOUND = -4, + SDUTILS_RESULT_INVALID_ARGUMENT = -5, + SDUTILS_RESULT_FAILED = -10, + SDUTILS_RESULT_LIB_UNINITIALIZED = -20, +}; + +enum SDUtilsAttachStatus { + SDUTILS_ATTACH_MOUNTED = 1, + SDUTILS_ATTACH_UNMOUNTED = 0, +}; + +typedef void (*SDAttachHandlerFn)(SDUtilsAttachStatus status); + +/** + * Initializes the SDUtils library. This must be call before any other function can be called + * @return SDUTILS_RESULT_SUCCESS on success, the functions of this lib can be used + * SDUTILS_RESULT_MODULE_NOT_FOUND when the SDHotSwapModule is not loaded + * SDUTILS_RESULT_MODULE_MISSING_EXPORT when the SDHotSwapModule does not export the expected functions. + */ +SDUtilsStatus SDUtils_Init(); + +/** + * Deinitializes the SDUtils library, must be called before exiting the application + * @return SDUTILS_RESULT_SUCCESS on success + */ +SDUtilsStatus SDUtils_DeInit(); + +/** + * Registers a callback which will be called whenever a sd card will be inserted or ejected. + * This is only true for future events, if the sd card is already inserted before registering + * a callback, the callback is only called on the next ejecting but not for the initial + * insertion that happened in the past. + * + * Any previously registered callbacks will be removed when the currently running application + * is closing. + * + * @param fn callback that will be called + * @return SDUTILS_RESULT_SUCCESS on success + * SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the + * maximum amount of callback has been reached + * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initalized properly + */ +SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn); + +/** + * Removed a previously registered callback + * + * @param fn + * @return SDUTILS_RESULT_SUCCESS on success + * SDUTILS_RESULT_NOT_FOUND when the given callback was not registered. + * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initalized properly + */ +SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn); + +/** + * Checks if a FAT32 formatted SD Card is inserted, mounted and available via `fs:/vol/external01` + * + * @param outStatus stores the result of the function + * @return SDUTILS_RESULT_SUCCESS on success + * SDUTILS_RESULT_INVALID_ARGUMENT when an invalid ptr has been given + */ +SDUtilsStatus SDUtils_IsSdCardMounted(bool *outStatus); + +#ifdef __cplusplus +} // extern "C" +#endif \ No newline at end of file diff --git a/source/utils.cpp b/source/utils.cpp new file mode 100644 index 0000000..7a7fe95 --- /dev/null +++ b/source/utils.cpp @@ -0,0 +1,69 @@ +#include "sdutils/sdutils.h" +#include +#include +#include + +static OSDynLoad_Module sModuleHandle = nullptr; + +static bool (*sSDUtilsAddAttachHandler)(SDAttachHandlerFn) = nullptr; +static bool (*sSDUtilsRemoveAttachHandler)(SDAttachHandlerFn) = nullptr; + +SDUtilsStatus SDUtils_Init() { + if (OSDynLoad_Acquire("homebrew_sdhotswap", &sModuleHandle) != OS_DYNLOAD_OK) { + OSReport("SDUtils_Init: OSDynLoad_Acquire failed.\n"); + return SDUTILS_RESULT_MODULE_NOT_FOUND; + } + + if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddAttachHandler", (void **) &sSDUtilsAddAttachHandler) != OS_DYNLOAD_OK) { + OSReport("SDUtils_Init: SDUtilsAddAttachHandler failed.\n"); + return SDUTILS_RESULT_MODULE_MISSING_EXPORT; + } + + if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsRemoveAttachHandler", (void **) &sSDUtilsRemoveAttachHandler) != OS_DYNLOAD_OK) { + OSReport("SDUtils_Init: SDUtilsRemoveAttachHandler failed.\n"); + return SDUTILS_RESULT_MODULE_MISSING_EXPORT; + } + + return SDUTILS_RESULT_SUCCESS; +} + +SDUtilsStatus SDUtils_DeInit() { + // We don't need to release the OSDynLoad handle for modules. + return SDUTILS_RESULT_SUCCESS; +} + + +SDUtilsStatus SDUtils_IsSdCardMounted(bool *status) { + if (status == nullptr) { + return SDUTILS_RESULT_INVALID_ARGUMENT; + } + auto dir = opendir("fs:/vol/external01/"); + if (dir != nullptr) { + closedir(dir); + *status = true; + } else { + *status = false; + } + return SDUTILS_RESULT_SUCCESS; +} + +bool AddAttachHandler(SDAttachHandlerFn); + +SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn) { + if (sSDUtilsAddAttachHandler == nullptr) { + return SDUTILS_RESULT_LIB_UNINITIALIZED; + } + auto res = reinterpret_cast(sSDUtilsAddAttachHandler)(fn); + return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS; +} + + +bool RemoveAttachHandler(SDAttachHandlerFn); + +SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn) { + if (sSDUtilsRemoveAttachHandler == nullptr) { + return SDUTILS_RESULT_LIB_UNINITIALIZED; + } + auto res = reinterpret_cast(sSDUtilsRemoveAttachHandler)(fn); + return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND; +} \ No newline at end of file