Added SDL properties API
Fixes https://github.com/libsdl-org/SDL/issues/7799
This commit is contained in:
@@ -38,6 +38,7 @@
|
||||
|
||||
#include "SDL_assert_c.h"
|
||||
#include "SDL_log_c.h"
|
||||
#include "SDL_properties_c.h"
|
||||
#include "audio/SDL_audio_c.h"
|
||||
#include "video/SDL_video_c.h"
|
||||
#include "events/SDL_events_c.h"
|
||||
@@ -167,6 +168,7 @@ int SDL_InitSubSystem(Uint32 flags)
|
||||
}
|
||||
|
||||
SDL_InitLog();
|
||||
SDL_InitProperties();
|
||||
|
||||
/* Clear the error message */
|
||||
SDL_ClearError();
|
||||
@@ -499,6 +501,7 @@ void SDL_Quit(void)
|
||||
SDL_DBus_Quit();
|
||||
#endif
|
||||
|
||||
SDL_QuitProperties();
|
||||
SDL_QuitLog();
|
||||
|
||||
/* Now that every subsystem has been quit, we reset the subsystem refcount
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
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_hashtable.h"
|
||||
|
||||
@@ -209,6 +208,21 @@ SDL_bool SDL_IterateHashTableKeys(const SDL_HashTable *table, const void **_key,
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
SDL_bool SDL_HashTableEmpty(SDL_HashTable *table)
|
||||
{
|
||||
if (table != NULL) {
|
||||
Uint32 i;
|
||||
|
||||
for (i = 0; i < table->table_len; i++) {
|
||||
SDL_HashItem *item = table->table[i];
|
||||
if (item != NULL) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
void SDL_DestroyHashTable(SDL_HashTable *table)
|
||||
{
|
||||
if (table != NULL) {
|
||||
@@ -240,10 +254,10 @@ static SDL_INLINE Uint32 hash_string_djbxor(const char *str, size_t len)
|
||||
return hash;
|
||||
}
|
||||
|
||||
Uint32 SDL_HashString(const void *sym, void *data)
|
||||
Uint32 SDL_HashString(const void *key, void *data)
|
||||
{
|
||||
const char *str = (const char*) sym;
|
||||
return hash_string_djbxor(str, SDL_strlen((const char *) str));
|
||||
const char *str = (const char *)key;
|
||||
return hash_string_djbxor(str, SDL_strlen(str));
|
||||
}
|
||||
|
||||
SDL_bool SDL_KeyMatchString(const void *a, const void *b, void *data)
|
||||
@@ -253,7 +267,21 @@ SDL_bool SDL_KeyMatchString(const void *a, const void *b, void *data)
|
||||
} else if (!a || !b) {
|
||||
return SDL_FALSE; /* one pointer is NULL (and first test shows they aren't the same pointer), must not match. */
|
||||
}
|
||||
return (SDL_strcmp((const char *) a, (const char *) b) == 0) ? SDL_TRUE : SDL_FALSE; /* Check against actual string contents. */
|
||||
return (SDL_strcmp((const char *)a, (const char *)b) == 0) ? SDL_TRUE : SDL_FALSE; /* Check against actual string contents. */
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
/* We assume we can fit the ID in the key directly */
|
||||
SDL_COMPILE_TIME_ASSERT(SDL_HashID_KeySize, sizeof(Uint32) <= sizeof(const void *));
|
||||
|
||||
Uint32 SDL_HashID(const void *key, void *unused)
|
||||
{
|
||||
return (Uint32)(uintptr_t)key;
|
||||
}
|
||||
|
||||
SDL_bool SDL_KeyMatchID(const void *a, const void *b, void *unused)
|
||||
{
|
||||
if (a == b) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -40,14 +40,15 @@ void SDL_DestroyHashTable(SDL_HashTable *table);
|
||||
SDL_bool SDL_InsertIntoHashTable(SDL_HashTable *table, const void *key, const void *value);
|
||||
SDL_bool SDL_RemoveFromHashTable(SDL_HashTable *table, const void *key);
|
||||
SDL_bool SDL_FindInHashTable(const SDL_HashTable *table, const void *key, const void **_value);
|
||||
SDL_bool SDL_HashTableEmpty(SDL_HashTable *table);
|
||||
|
||||
SDL_bool SDL_IterateHashTable(const SDL_HashTable *table, const void *key, const void **_value, void **iter);
|
||||
SDL_bool SDL_IterateHashTableKeys(const SDL_HashTable *table, const void **_key, void **iter);
|
||||
|
||||
Uint32 SDL_HashString(const void *sym, void *unused);
|
||||
Uint32 SDL_HashString(const void *key, void *unused);
|
||||
SDL_bool SDL_KeyMatchString(const void *a, const void *b, void *unused);
|
||||
|
||||
Uint32 SDL_HashID(const void *key, void *unused);
|
||||
SDL_bool SDL_KeyMatchID(const void *a, const void *b, void *unused);
|
||||
|
||||
#endif /* SDL_hashtable_h_ */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
|
||||
278
src/SDL_properties.c
Normal file
278
src/SDL_properties.c
Normal file
@@ -0,0 +1,278 @@
|
||||
/*
|
||||
Simple DiretMedia Layer
|
||||
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
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_hashtable.h"
|
||||
#include "SDL_properties_c.h"
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void *value;
|
||||
void (SDLCALL *cleanup)(void *userdata, void *value);
|
||||
void *userdata;
|
||||
} SDL_Property;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
SDL_HashTable *props;
|
||||
SDL_Mutex *lock;
|
||||
} SDL_Properties;
|
||||
|
||||
static SDL_HashTable *SDL_properties;
|
||||
static SDL_RWLock *SDL_properties_lock;
|
||||
static SDL_PropertiesID SDL_last_properties_id;
|
||||
|
||||
|
||||
static void SDL_FreeProperty(const void *key, const void *value, void *data)
|
||||
{
|
||||
SDL_Property *property = (SDL_Property *)value;
|
||||
if (property->cleanup) {
|
||||
property->cleanup(property->userdata, property->value);
|
||||
}
|
||||
SDL_free((void *)key);
|
||||
SDL_free((void *)value);
|
||||
}
|
||||
|
||||
static void SDL_FreeProperties(const void *key, const void *value, void *data)
|
||||
{
|
||||
SDL_Properties *properties = (SDL_Properties *)value;
|
||||
if (properties) {
|
||||
if (properties->props) {
|
||||
SDL_DestroyHashTable(properties->props);
|
||||
properties->props = NULL;
|
||||
}
|
||||
if (properties->lock) {
|
||||
SDL_DestroyMutex(properties->lock);
|
||||
properties->lock = NULL;
|
||||
}
|
||||
SDL_free(properties);
|
||||
}
|
||||
}
|
||||
|
||||
int SDL_InitProperties(void)
|
||||
{
|
||||
if (!SDL_properties_lock) {
|
||||
SDL_properties_lock = SDL_CreateRWLock();
|
||||
if (!SDL_properties_lock) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (!SDL_properties) {
|
||||
SDL_properties = SDL_CreateHashTable(NULL, 16, SDL_HashID, SDL_KeyMatchID, SDL_FreeProperties, SDL_FALSE);
|
||||
if (!SDL_properties) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SDL_QuitProperties(void)
|
||||
{
|
||||
if (SDL_properties) {
|
||||
SDL_DestroyHashTable(SDL_properties);
|
||||
SDL_properties = NULL;
|
||||
}
|
||||
if (SDL_properties_lock) {
|
||||
SDL_DestroyRWLock(SDL_properties_lock);
|
||||
SDL_properties_lock = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_PropertiesID SDL_CreateProperties(void)
|
||||
{
|
||||
SDL_PropertiesID props;
|
||||
SDL_Properties *properties = NULL;
|
||||
SDL_bool inserted = SDL_FALSE;
|
||||
|
||||
if (!SDL_properties && SDL_InitProperties() < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
properties = SDL_malloc(sizeof(*properties));
|
||||
if (!properties) {
|
||||
goto error;
|
||||
}
|
||||
properties->props = SDL_CreateHashTable(NULL, 4, SDL_HashString, SDL_KeyMatchString, SDL_FreeProperty, SDL_FALSE);
|
||||
if (!properties->props) {
|
||||
goto error;
|
||||
}
|
||||
properties->lock = SDL_CreateMutex();
|
||||
if (!properties->lock) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (SDL_InitProperties() < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (SDL_LockRWLockForWriting(SDL_properties_lock) == 0) {
|
||||
props = ++SDL_last_properties_id;
|
||||
if (SDL_InsertIntoHashTable(SDL_properties, (const void *)(uintptr_t)props, properties)) {
|
||||
inserted = SDL_TRUE;
|
||||
}
|
||||
SDL_UnlockRWLock(SDL_properties_lock);
|
||||
}
|
||||
|
||||
if (inserted) {
|
||||
/* All done! */
|
||||
return props;
|
||||
}
|
||||
|
||||
error:
|
||||
SDL_FreeProperties(NULL, properties, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SDL_LockProperties(SDL_PropertiesID props)
|
||||
{
|
||||
SDL_Properties *properties = NULL;
|
||||
|
||||
if (!props) {
|
||||
return SDL_InvalidParamError("props");
|
||||
}
|
||||
if (SDL_LockRWLockForReading(SDL_properties_lock) == 0) {
|
||||
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
||||
SDL_UnlockRWLock(SDL_properties_lock);
|
||||
}
|
||||
if (!properties) {
|
||||
SDL_InvalidParamError("props");
|
||||
return -1;
|
||||
}
|
||||
return SDL_LockMutex(properties->lock);
|
||||
}
|
||||
|
||||
void SDL_UnlockProperties(SDL_PropertiesID props)
|
||||
{
|
||||
SDL_Properties *properties = NULL;
|
||||
|
||||
if (!props) {
|
||||
return;
|
||||
}
|
||||
if (SDL_LockRWLockForReading(SDL_properties_lock) == 0) {
|
||||
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
||||
SDL_UnlockRWLock(SDL_properties_lock);
|
||||
}
|
||||
if (!properties) {
|
||||
return;
|
||||
}
|
||||
SDL_UnlockMutex(properties->lock);
|
||||
}
|
||||
|
||||
int SDL_SetProperty(SDL_PropertiesID props, const char *name, void *value, void (SDLCALL *cleanup)(void *userdata, void *value), void *userdata)
|
||||
{
|
||||
SDL_Properties *properties = NULL;
|
||||
SDL_Property *property = NULL;
|
||||
int result = 0;
|
||||
|
||||
if (!props) {
|
||||
return SDL_InvalidParamError("props");
|
||||
}
|
||||
if (!name || !*name) {
|
||||
return SDL_InvalidParamError("name");
|
||||
}
|
||||
|
||||
if (SDL_LockRWLockForReading(SDL_properties_lock) == 0) {
|
||||
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
||||
SDL_UnlockRWLock(SDL_properties_lock);
|
||||
}
|
||||
if (!properties) {
|
||||
return SDL_InvalidParamError("props");
|
||||
}
|
||||
|
||||
if (value) {
|
||||
property = (SDL_Property *)SDL_malloc(sizeof(*property));
|
||||
if (!property) {
|
||||
return SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
property->value = value;
|
||||
property->cleanup = cleanup;
|
||||
property->userdata = userdata;
|
||||
}
|
||||
|
||||
SDL_LockMutex(properties->lock);
|
||||
{
|
||||
SDL_RemoveFromHashTable(properties->props, name);
|
||||
if (property) {
|
||||
char *key = SDL_strdup(name);
|
||||
if (!SDL_InsertIntoHashTable(properties->props, key, property)) {
|
||||
SDL_FreeProperty(key, property, NULL);
|
||||
result = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_UnlockMutex(properties->lock);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void *SDL_GetProperty(SDL_PropertiesID props, const char *name)
|
||||
{
|
||||
SDL_Properties *properties = NULL;
|
||||
void *value = NULL;
|
||||
|
||||
if (!props) {
|
||||
SDL_InvalidParamError("props");
|
||||
return NULL;
|
||||
}
|
||||
if (!name || !*name) {
|
||||
SDL_InvalidParamError("name");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (SDL_LockRWLockForReading(SDL_properties_lock) == 0) {
|
||||
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
||||
SDL_UnlockRWLock(SDL_properties_lock);
|
||||
}
|
||||
if (!properties) {
|
||||
SDL_InvalidParamError("props");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Note that taking the lock here only guarantees that we won't read the
|
||||
* hashtable while it's being modified. The value itself can easily be
|
||||
* freed from another thread after it is returned here.
|
||||
*/
|
||||
SDL_LockMutex(properties->lock);
|
||||
{
|
||||
SDL_Property *property = NULL;
|
||||
if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
|
||||
value = property->value;
|
||||
} else {
|
||||
SDL_SetError("Couldn't find property named %s", name);
|
||||
}
|
||||
}
|
||||
SDL_UnlockMutex(properties->lock);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void SDL_DestroyProperties(SDL_PropertiesID props)
|
||||
{
|
||||
if (!props) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (SDL_LockRWLockForWriting(SDL_properties_lock) == 0) {
|
||||
SDL_RemoveFromHashTable(SDL_properties, (const void *)(uintptr_t)props);
|
||||
SDL_UnlockRWLock(SDL_properties_lock);
|
||||
}
|
||||
}
|
||||
23
src/SDL_properties_c.h
Normal file
23
src/SDL_properties_c.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
Simple DiretMedia Layer
|
||||
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
extern int SDL_InitProperties(void);
|
||||
extern void SDL_QuitProperties(void);
|
||||
@@ -908,6 +908,12 @@ SDL3_0.0.0 {
|
||||
SDL_SetAudioPostmixCallback;
|
||||
SDL_GetAudioStreamQueued;
|
||||
SDL_GetTextureDXGIResource;
|
||||
SDL_CreateProperties;
|
||||
SDL_LockProperties;
|
||||
SDL_UnlockProperties;
|
||||
SDL_SetProperty;
|
||||
SDL_GetProperty;
|
||||
SDL_DestroyProperties;
|
||||
# extra symbols go here (don't modify this line)
|
||||
local: *;
|
||||
};
|
||||
|
||||
@@ -933,3 +933,9 @@
|
||||
#define SDL_SetAudioPostmixCallback SDL_SetAudioPostmixCallback_REAL
|
||||
#define SDL_GetAudioStreamQueued SDL_GetAudioStreamQueued_REAL
|
||||
#define SDL_GetTextureDXGIResource SDL_GetTextureDXGIResource_REAL
|
||||
#define SDL_CreateProperties SDL_CreateProperties_REAL
|
||||
#define SDL_LockProperties SDL_LockProperties_REAL
|
||||
#define SDL_UnlockProperties SDL_UnlockProperties_REAL
|
||||
#define SDL_SetProperty SDL_SetProperty_REAL
|
||||
#define SDL_GetProperty SDL_GetProperty_REAL
|
||||
#define SDL_DestroyProperties SDL_DestroyProperties_REAL
|
||||
|
||||
@@ -979,3 +979,9 @@ SDL_DYNAPI_PROC(int,SDL_SetAudioStreamFrequencyRatio,(SDL_AudioStream *a, float
|
||||
SDL_DYNAPI_PROC(int,SDL_SetAudioPostmixCallback,(SDL_AudioDeviceID a, SDL_AudioPostmixCallback b, void *c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_GetAudioStreamQueued,(SDL_AudioStream *a),(a),return)
|
||||
SDL_DYNAPI_PROC(IDXGIResource*,SDL_GetTextureDXGIResource,(SDL_Texture *a),(a),return)
|
||||
SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_CreateProperties,(void),(),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_LockProperties,(SDL_PropertiesID a),(a),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_UnlockProperties,(SDL_PropertiesID a),(a),)
|
||||
SDL_DYNAPI_PROC(int,SDL_SetProperty,(SDL_PropertiesID a, const char *b, void *c, void (SDLCALL *d)(void *userdata, void *value), void *e),(a,b,c,d,e),return)
|
||||
SDL_DYNAPI_PROC(void*,SDL_GetProperty,(SDL_PropertiesID a, const char *b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_DestroyProperties,(SDL_PropertiesID a),(a),)
|
||||
|
||||
Reference in New Issue
Block a user