Added SDL_GetSandbox()

This adds support for detecting whether you're running in a sandbox on macOS
This commit is contained in:
Sam Lantinga 2024-10-16 11:02:49 -07:00
parent d7b1ba1bfc
commit d6981da5a4
12 changed files with 69 additions and 95 deletions

View file

@ -1726,7 +1726,6 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU)
sdl_sources( sdl_sources(
"${SDL3_SOURCE_DIR}/src/core/linux/SDL_evdev_capabilities.c" "${SDL3_SOURCE_DIR}/src/core/linux/SDL_evdev_capabilities.c"
"${SDL3_SOURCE_DIR}/src/core/linux/SDL_threadprio.c" "${SDL3_SOURCE_DIR}/src/core/linux/SDL_threadprio.c"
"${SDL3_SOURCE_DIR}/src/core/linux/SDL_sandbox.c"
) )
# src/core/unix/*.c is included in a generic if(UNIX) section, elsewhere. # src/core/unix/*.c is included in a generic if(UNIX) section, elsewhere.

View file

@ -565,6 +565,30 @@ extern SDL_DECLSPEC bool SDLCALL SDL_IsTablet(void);
*/ */
extern SDL_DECLSPEC bool SDLCALL SDL_IsTV(void); extern SDL_DECLSPEC bool SDLCALL SDL_IsTV(void);
/**
* Application sandbox environment.
*
* \since This enum is available since SDL 3.1.6.
*/
typedef enum SDL_Sandbox
{
SDL_SANDBOX_NONE = 0,
SDL_SANDBOX_UNKNOWN,
SDL_SANDBOX_FLATPAK,
SDL_SANDBOX_SNAP,
SDL_SANDBOX_MACOS
} SDL_Sandbox;
/**
* Get the application sandbox environment, if any.
*
* \returns the application sandbox environment or SDL_SANDBOX_NONE if the application is not running in a sandbox environment.
*
* \since This function is available since SDL 3.1.6.
*/
extern SDL_DECLSPEC SDL_Sandbox SDLCALL SDL_GetSandbox(void);
/* Functions used by iOS app delegates to notify SDL about state changes. */ /* Functions used by iOS app delegates to notify SDL about state changes. */
/** /**

View file

@ -753,6 +753,44 @@ bool SDL_IsTV(void)
#endif #endif
} }
static SDL_Sandbox SDL_DetectSandbox(void)
{
#if defined(SDL_PLATFORM_LINUX)
if (access("/.flatpak-info", F_OK) == 0) {
return SDL_SANDBOX_FLATPAK;
}
/* For Snap, we check multiple variables because they might be set for
* unrelated reasons. This is the same thing WebKitGTK does. */
if (SDL_getenv("SNAP") && SDL_getenv("SNAP_NAME") && SDL_getenv("SNAP_REVISION")) {
return SDL_SANDBOX_SNAP;
}
if (access("/run/host/container-manager", F_OK) == 0) {
return SDL_SANDBOX_UNKNOWN;
}
#elif defined(SDL_PLATFORM_MACOS)
if (SDL_getenv("APP_SANDBOX_CONTAINER_ID")) {
return SDL_SANDBOX_MACOS;
}
#endif
return SDL_SANDBOX_NONE;
}
SDL_Sandbox SDL_GetSandbox(void)
{
static SDL_Sandbox sandbox;
static bool sandbox_initialized;
if (!sandbox_initialized) {
sandbox = SDL_DetectSandbox();
sandbox_initialized = true;
}
return sandbox;
}
#ifdef SDL_PLATFORM_WIN32 #ifdef SDL_PLATFORM_WIN32
#if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB) #if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB)

View file

@ -20,7 +20,6 @@
*/ */
#include "SDL_internal.h" #include "SDL_internal.h"
#include "SDL_dbus.h" #include "SDL_dbus.h"
#include "SDL_sandbox.h"
#include "../../stdlib/SDL_vacopy.h" #include "../../stdlib/SDL_vacopy.h"
#ifdef SDL_USE_LIBDBUS #ifdef SDL_USE_LIBDBUS
@ -443,7 +442,7 @@ bool SDL_DBus_ScreensaverInhibit(bool inhibit)
return false; return false;
} }
if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) { if (SDL_GetSandbox() != SDL_SANDBOX_NONE) {
const char *bus_name = "org.freedesktop.portal.Desktop"; const char *bus_name = "org.freedesktop.portal.Desktop";
const char *path = "/org/freedesktop/portal/desktop"; const char *path = "/org/freedesktop/portal/desktop";
const char *interface = "org.freedesktop.portal.Inhibit"; const char *interface = "org.freedesktop.portal.Inhibit";

View file

@ -1,47 +0,0 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 2022 Collabora Ltd.
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"
#include "SDL_sandbox.h"
#include <unistd.h>
SDL_Sandbox SDL_DetectSandbox(void)
{
if (access("/.flatpak-info", F_OK) == 0) {
return SDL_SANDBOX_FLATPAK;
}
/* For Snap, we check multiple variables because they might be set for
* unrelated reasons. This is the same thing WebKitGTK does. */
if (SDL_getenv("SNAP") != NULL &&
SDL_getenv("SNAP_NAME") != NULL &&
SDL_getenv("SNAP_REVISION") != NULL) {
return SDL_SANDBOX_SNAP;
}
if (access("/run/host/container-manager", F_OK) == 0) {
return SDL_SANDBOX_UNKNOWN_CONTAINER;
}
return SDL_SANDBOX_NONE;
}

View file

@ -1,37 +0,0 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 2022 Collabora Ltd.
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.
*/
#ifndef SDL_SANDBOX_H
#define SDL_SANDBOX_H
typedef enum
{
SDL_SANDBOX_NONE = 0,
SDL_SANDBOX_UNKNOWN_CONTAINER,
SDL_SANDBOX_FLATPAK,
SDL_SANDBOX_SNAP,
} SDL_Sandbox;
// Return the sandbox type currently in use, if any
SDL_Sandbox SDL_DetectSandbox(void);
#endif // SDL_SANDBOX_H

View file

@ -1182,6 +1182,7 @@ SDL3_0.0.0 {
SDL_SetErrorV; SDL_SetErrorV;
SDL_GetDefaultLogOutputFunction; SDL_GetDefaultLogOutputFunction;
SDL_RenderDebugText; SDL_RenderDebugText;
SDL_GetSandbox;
# extra symbols go here (don't modify this line) # extra symbols go here (don't modify this line)
local: *; local: *;
}; };

View file

@ -1207,3 +1207,4 @@
#define SDL_SetErrorV SDL_SetErrorV_REAL #define SDL_SetErrorV SDL_SetErrorV_REAL
#define SDL_GetDefaultLogOutputFunction SDL_GetDefaultLogOutputFunction_REAL #define SDL_GetDefaultLogOutputFunction SDL_GetDefaultLogOutputFunction_REAL
#define SDL_RenderDebugText SDL_RenderDebugText_REAL #define SDL_RenderDebugText SDL_RenderDebugText_REAL
#define SDL_GetSandbox SDL_GetSandbox_REAL

View file

@ -1213,3 +1213,4 @@ SDL_DYNAPI_PROC(Uint32,SDL_CalculateGPUTextureFormatSize,(SDL_GPUTextureFormat a
SDL_DYNAPI_PROC(bool,SDL_SetErrorV,(SDL_PRINTF_FORMAT_STRING const char *a,va_list b),(a,b),return) SDL_DYNAPI_PROC(bool,SDL_SetErrorV,(SDL_PRINTF_FORMAT_STRING const char *a,va_list b),(a,b),return)
SDL_DYNAPI_PROC(SDL_LogOutputFunction,SDL_GetDefaultLogOutputFunction,(void),(),return) SDL_DYNAPI_PROC(SDL_LogOutputFunction,SDL_GetDefaultLogOutputFunction,(void),(),return)
SDL_DYNAPI_PROC(bool,SDL_RenderDebugText,(SDL_Renderer *a,float b,float c,const char *d),(a,b,c,d),return) SDL_DYNAPI_PROC(bool,SDL_RenderDebugText,(SDL_Renderer *a,float b,float c,const char *d),(a,b,c,d),return)
SDL_DYNAPI_PROC(SDL_Sandbox,SDL_GetSandbox,(void),(),return)

View file

@ -60,7 +60,6 @@
#include "../core/linux/SDL_udev.h" #include "../core/linux/SDL_udev.h"
#ifdef SDL_USE_LIBUDEV #ifdef SDL_USE_LIBUDEV
#include <poll.h> #include <poll.h>
#include "../core/linux/SDL_sandbox.h"
#endif #endif
#ifdef HAVE_INOTIFY #ifdef HAVE_INOTIFY
@ -1144,7 +1143,7 @@ int SDL_hid_init(void)
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
"udev disabled by SDL_HINT_HIDAPI_UDEV"); "udev disabled by SDL_HINT_HIDAPI_UDEV");
linux_enumeration_method = ENUMERATION_FALLBACK; linux_enumeration_method = ENUMERATION_FALLBACK;
} else if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) { } else if (SDL_GetSandbox() != SDL_SANDBOX_NONE) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
"Container detected, disabling HIDAPI udev integration"); "Container detected, disabling HIDAPI udev integration");
linux_enumeration_method = ENUMERATION_FALLBACK; linux_enumeration_method = ENUMERATION_FALLBACK;

View file

@ -31,9 +31,6 @@
#include "../windows/SDL_rawinputjoystick_c.h" #include "../windows/SDL_rawinputjoystick_c.h"
#endif #endif
#ifdef SDL_USE_LIBUDEV
#include "../../core/linux/SDL_sandbox.h"
#endif
struct joystick_hwdata struct joystick_hwdata
{ {
@ -586,7 +583,7 @@ static bool HIDAPI_JoystickInit(void)
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
"udev disabled by SDL_HINT_HIDAPI_UDEV"); "udev disabled by SDL_HINT_HIDAPI_UDEV");
linux_enumeration_method = ENUMERATION_FALLBACK; linux_enumeration_method = ENUMERATION_FALLBACK;
} else if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) { } else if (SDL_GetSandbox() != SDL_SANDBOX_NONE) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
"Container detected, disabling HIDAPI udev integration"); "Container detected, disabling HIDAPI udev integration");
linux_enumeration_method = ENUMERATION_FALLBACK; linux_enumeration_method = ENUMERATION_FALLBACK;

View file

@ -124,7 +124,6 @@
#include "../../core/linux/SDL_evdev_capabilities.h" #include "../../core/linux/SDL_evdev_capabilities.h"
#include "../../core/linux/SDL_udev.h" #include "../../core/linux/SDL_udev.h"
#include "../../core/linux/SDL_sandbox.h"
#if 0 #if 0
#define DEBUG_INPUT_EVENTS 1 #define DEBUG_INPUT_EVENTS 1
@ -1069,7 +1068,7 @@ static bool LINUX_JoystickInit(void)
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
"udev disabled by SDL_JOYSTICK_DISABLE_UDEV"); "udev disabled by SDL_JOYSTICK_DISABLE_UDEV");
enumeration_method = ENUMERATION_FALLBACK; enumeration_method = ENUMERATION_FALLBACK;
} else if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) { } else if (SDL_GetSandbox() != SDL_SANDBOX_NONE) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
"Container detected, disabling udev integration"); "Container detected, disabling udev integration");
enumeration_method = ENUMERATION_FALLBACK; enumeration_method = ENUMERATION_FALLBACK;