Skip to content

Commit

Permalink
SWITCH: first commit, missing audio, input... (libsdl-org#1)
Browse files Browse the repository at this point in the history
* SWITCH: first commit, missing audio, input...
  • Loading branch information
Cpasjuste authored and WinterMute committed Jun 18, 2021
1 parent 9a27289 commit 005829b
Show file tree
Hide file tree
Showing 17 changed files with 977 additions and 2 deletions.
78 changes: 78 additions & 0 deletions CMakeLists.switch
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
cmake_minimum_required(VERSION 3.0)
#set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_SYSTEM_NAME "Generic")

set(DEVKITPRO $ENV{DEVKITPRO})
set(CMAKE_SYSTEM_PROCESSOR "armv8-a")
set(CMAKE_C_COMPILER "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-gcc")
set(CMAKE_CXX_COMPILER "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-g++")
set(CMAKE_ASM_COMPILER "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-as")
set(CMAKE_AR "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-gcc-ar" CACHE STRING "")
set(CMAKE_RANLIB "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-gcc-ranlib" CACHE STRING "")
set(CMAKE_C_FLAGS "-O2 -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIC -ftls-model=local-exec -I${DEVKITPRO}/libnx/include -I${DEVKITPRO}/portlibs/switch/include" CACHE STRING "C flags")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -fpermissive -fno-rtti -fno-exceptions -std=gnu++11" CACHE STRING "C++ flags")
set(CMAKE_FIND_ROOT_PATH ${DEVKITPRO} ${DEVKITPRO}/devkitA64 ${DEVKITPRO}/libnx ${DEVKITPRO}/portlibs/switch)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "Shared libs not available")

project(SDL2)

set(SRC_DIRS
src
src/atomic
src/audio
#src/audio/switch
src/audio/dummy
src/cpuinfo
src/events
src/file
src/filesystem/dummy
src/haptic
src/haptic/dummy
src/joystick
#src/joystick/switch
src/joystick/dummy
src/libm
src/power
src/render
src/render/software
src/stdlib
src/thread
src/thread/switch
src/timer
src/timer/switch
src/video
src/video/yuv2rgb
src/video/switch
)

set(SRC_FILES )
foreach (DIR ${SRC_DIRS})
file(GLOB FILES ${DIR}/*.c*)
list(APPEND SRC_FILES ${FILES})
endforeach (DIR)

# SDL2 library
add_library(${PROJECT_NAME} STATIC ${SRC_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC ${SRC_DIRS} include)
target_compile_options(${PROJECT_NAME} PUBLIC -O3 -DSWITCH)

# SDL2 test
add_executable(${PROJECT_NAME}.elf test/testswitch.c)
target_include_directories(${PROJECT_NAME}.elf PRIVATE include)
#target_include_directories(${PROJECT_NAME}.elf PRIVATE ${DEVKITPRO}/portlibs/switch/include)
target_compile_options(${PROJECT_NAME}.elf PRIVATE -O3 -DSWITCH)
target_link_libraries(${PROJECT_NAME}.elf
${PROJECT_NAME}
#${DEVKITPRO}/portlibs/switch/lib/libSDL2.a
${DEVKITPRO}/libnx/lib/libnx.a
m
)
set_target_properties(${PROJECT_NAME}.elf PROPERTIES LINK_FLAGS "-specs=${DEVKITPRO}/libnx/switch.specs")
add_custom_target(${PROJECT_NAME}.nro
DEPENDS ${PROJECT_NAME}.elf
COMMAND elf2nro ${PROJECT_NAME}.elf ${PROJECT_NAME}.nro)
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -4233,6 +4233,7 @@ AS_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
aarch64-none-elf*)
ARCH=switch
EXTRA_CFLAGS="$EXTRA_CFLAGS -isystem${DEVKITPRO}/libnx/include -I${DEVKITPRO}/portlibs/switch/include -D__SWITCH__=1"
EXTRA_CFLAGS="$EXTRA_CFLAGS -g -O2 -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIC -ftls-model=local-exec"
CheckDeclarationAfterStatement
CheckDiskAudio
CheckDummyAudio
Expand Down
140 changes: 140 additions & 0 deletions src/thread/switch/SDL_syscond.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2015 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <switch.h>
#include "../../SDL_internal.h"

#if SDL_THREAD_SWITCH

/* An implementation of condition variables using semaphores and mutexes */
/*
This implementation borrows heavily from the BeOS condition variable
implementation, written by Christopher Tate and Owen Smith. Thanks!
*/

#include "SDL_thread.h"

struct SDL_cond
{
Mutex mutex;
CondVar var;
};

/* Create a condition variable */
SDL_cond *
SDL_CreateCond(void)
{
SDL_cond *cond;

cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
if (cond) {
mutexInit(&cond->mutex);
condvarInit(&cond->var, &cond->mutex);
}
else {
SDL_OutOfMemory();
}
return (cond);
}

/* Destroy a condition variable */
void
SDL_DestroyCond(SDL_cond *cond)
{
if (cond) {
SDL_free(cond);
}
}

/* Restart one of the threads that are waiting on the condition variable */
int
SDL_CondSignal(SDL_cond *cond)
{
if (!cond) {
return SDL_SetError("Passed a NULL condition variable");
}

condvarWakeOne(&cond->var);

return 0;
}

/* Restart all threads that are waiting on the condition variable */
int
SDL_CondBroadcast(SDL_cond *cond)
{
if (!cond) {
return SDL_SetError("Passed a NULL condition variable");
}

condvarWakeAll(&cond->var);

return 0;
}

/* Wait on the condition variable for at most 'ms' milliseconds.
The mutex must be locked before entering this function!
The mutex is unlocked during the wait, and locked again after the wait.
Typical use:
Thread A:
SDL_LockMutex(lock);
while ( ! condition ) {
SDL_CondWait(cond, lock);
}
SDL_UnlockMutex(lock);
Thread B:
SDL_LockMutex(lock);
...
condition = true;
...
SDL_CondSignal(cond);
SDL_UnlockMutex(lock);
*/
int
SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
{
if (!cond) {
return SDL_SetError("Passed a NULL condition variable");
}

/* Unlock the mutex, as is required by condition variable semantics */
SDL_UnlockMutex(mutex);

condvarWaitTimeout(&cond->var, ms * 1000000);

/* Lock the mutex, as is required by condition variable semantics */
SDL_LockMutex(mutex);

return 0;
}

/* Wait on the condition variable forever */
int
SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
}

#endif /* SDL_THREAD_SWITCH */

/* vi: set ts=4 sw=4 expandtab: */
122 changes: 122 additions & 0 deletions src/thread/switch/SDL_sysmutex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2015 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"

#if SDL_THREAD_SWITCH

/* An implementation of mutexes using semaphores */

#include "SDL_thread.h"
#include "SDL_systhread_c.h"

struct SDL_mutex
{
int recursive;
Uint32 owner;
Mutex mutex;
};

/* Create a mutex */
SDL_mutex *
SDL_CreateMutex(void)
{
SDL_mutex *mutex;

mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
if (mutex) {
mutexInit(&mutex->mutex);
mutex->recursive = 0;
mutex->owner = 0;
}
else {
SDL_OutOfMemory();
}
return mutex;
}

/* Free the mutex */
void
SDL_DestroyMutex(SDL_mutex *mutex)
{
if (mutex) {
SDL_free(mutex);
}
}

/* Lock the semaphore */
int
SDL_mutexP(SDL_mutex *mutex)
{
Uint32 this_thread;

if (mutex == NULL) {
SDL_SetError("Passed a NULL mutex");
return -1;
}

this_thread = (Uint32) SDL_ThreadID();
if (mutex->owner == this_thread) {
++mutex->recursive;
}
else {
/* The order of operations is important.
We set the locking thread id after we obtain the lock
so unlocks from other threads will fail.
*/
mutexLock(&mutex->mutex);
mutex->owner = this_thread;
mutex->recursive = 0;
}

return 0;
}

/* Unlock the mutex */
int
SDL_mutexV(SDL_mutex *mutex)
{
if (mutex == NULL) {
SDL_SetError("Passed a NULL mutex");
return -1;
}

/* If we don't own the mutex, we can't unlock it */
if (SDL_ThreadID() != mutex->owner) {
SDL_SetError("mutex not owned by this thread");
return -1;
}

if (mutex->recursive) {
--mutex->recursive;
}
else {
/* The order of operations is important.
First reset the owner so another thread doesn't lock
the mutex and set the ownership before we reset it,
then release the lock semaphore.
*/
mutex->owner = 0;
mutexUnlock(&mutex->mutex);
}
return 0;
}

#endif /* SDL_THREAD_SWITCH */
22 changes: 22 additions & 0 deletions src/thread/switch/SDL_sysmutex_c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2015 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#include "../../SDL_internal.h"
Loading

0 comments on commit 005829b

Please sign in to comment.