mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-01 22:56:02 +02:00
Added SDL_SetAppMetadata() (#10404)
Removed duplicate hints SDL_HINT_APP_NAME, SDL_HINT_APP_ID, and SDL_HINT_AUDIO_DEVICE_APP_NAME. Wired up a few things to use the metadata; more to come! Fixes https://github.com/libsdl-org/SDL/issues/4703
This commit is contained in:
57
src/SDL.c
57
src/SDL.c
@@ -107,6 +107,63 @@ SDL_NORETURN void SDL_ExitProcess(int exitcode)
|
||||
#endif
|
||||
}
|
||||
|
||||
/* App metadata */
|
||||
|
||||
int SDL_SetAppMetadata(const char *appname, const char *appversion, const char *appidentifier)
|
||||
{
|
||||
SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING, appname);
|
||||
SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_VERSION_STRING, appversion);
|
||||
SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, appidentifier);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SDL_bool SDL_ValidMetadataProperty(const char *name)
|
||||
{
|
||||
if (!name || !*name) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
if (SDL_strcmp(name, SDL_PROP_APP_METADATA_NAME_STRING) == 0 ||
|
||||
SDL_strcmp(name, SDL_PROP_APP_METADATA_VERSION_STRING) == 0 ||
|
||||
SDL_strcmp(name, SDL_PROP_APP_METADATA_IDENTIFIER_STRING) == 0 ||
|
||||
SDL_strcmp(name, SDL_PROP_APP_METADATA_CREATOR_STRING) == 0 ||
|
||||
SDL_strcmp(name, SDL_PROP_APP_METADATA_COPYRIGHT_STRING) == 0 ||
|
||||
SDL_strcmp(name, SDL_PROP_APP_METADATA_URL_STRING) == 0 ||
|
||||
SDL_strcmp(name, SDL_PROP_APP_METADATA_TYPE_STRING) == 0) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
int SDL_SetAppMetadataProperty(const char *name, const char *value)
|
||||
{
|
||||
if (!SDL_ValidMetadataProperty(name)) {
|
||||
return SDL_InvalidParamError("name");
|
||||
}
|
||||
|
||||
return SDL_SetStringProperty(SDL_GetGlobalProperties(), name, value);
|
||||
}
|
||||
|
||||
const char *SDL_GetAppMetadataProperty(const char *name)
|
||||
{
|
||||
if (!SDL_ValidMetadataProperty(name)) {
|
||||
SDL_InvalidParamError("name");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const SDL_PropertiesID props = SDL_GetGlobalProperties();
|
||||
const char *value = SDL_GetStringProperty(props, name, NULL);
|
||||
if (!value || !*value) {
|
||||
if (SDL_strcmp(name, SDL_PROP_APP_METADATA_NAME_STRING) == 0) {
|
||||
value = "SDL Application";
|
||||
} else if (SDL_strcmp(name, SDL_PROP_APP_METADATA_TYPE_STRING) == 0) {
|
||||
value = "application";
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/* The initialized subsystems */
|
||||
#ifdef SDL_MAIN_NEEDED
|
||||
static SDL_bool SDL_MainIsReady = SDL_FALSE;
|
||||
|
||||
@@ -261,18 +261,10 @@ static void JACK_CloseDevice(SDL_AudioDevice *device)
|
||||
}
|
||||
}
|
||||
|
||||
// !!! FIXME: unify this (PulseAudio has a getAppName, Pipewire has a thing, etc
|
||||
// !!! FIXME: unify this (PulseAudio has a getAppName, Pipewire has a thing, etc)
|
||||
static const char *GetJackAppName(void)
|
||||
{
|
||||
const char *retval = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_APP_NAME);
|
||||
if (retval && *retval) {
|
||||
return retval;
|
||||
}
|
||||
retval = SDL_GetHint(SDL_HINT_APP_NAME);
|
||||
if (retval && *retval) {
|
||||
return retval;
|
||||
}
|
||||
return "SDL Application";
|
||||
return SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING);
|
||||
}
|
||||
|
||||
static int JACK_OpenDevice(SDL_AudioDevice *device)
|
||||
|
||||
@@ -1049,13 +1049,7 @@ static int PIPEWIRE_OpenDevice(SDL_AudioDevice *device)
|
||||
const int min_period = PW_MIN_SAMPLES * SPA_MAX(device->spec.freq / PW_BASE_CLOCK_RATE, 1);
|
||||
|
||||
// Get the hints for the application name, icon name, stream name and role
|
||||
app_name = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_APP_NAME);
|
||||
if (!app_name || *app_name == '\0') {
|
||||
app_name = SDL_GetHint(SDL_HINT_APP_NAME);
|
||||
if (!app_name || *app_name == '\0') {
|
||||
app_name = "SDL Application";
|
||||
}
|
||||
}
|
||||
app_name = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING);
|
||||
|
||||
icon_name = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_APP_ICON_NAME);
|
||||
if (!icon_name || *icon_name == '\0') {
|
||||
@@ -1063,7 +1057,7 @@ static int PIPEWIRE_OpenDevice(SDL_AudioDevice *device)
|
||||
}
|
||||
|
||||
// App ID. Default to NULL if not available.
|
||||
app_id = SDL_GetHint(SDL_HINT_APP_ID);
|
||||
app_id = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING);
|
||||
|
||||
stream_name = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_STREAM_NAME);
|
||||
if (!stream_name || *stream_name == '\0') {
|
||||
|
||||
@@ -264,34 +264,9 @@ static int load_pulseaudio_syms(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SDL_INLINE int squashVersion(const int major, const int minor, const int patch)
|
||||
{
|
||||
return ((major & 0xFF) << 16) | ((minor & 0xFF) << 8) | (patch & 0xFF);
|
||||
}
|
||||
|
||||
// Workaround for older pulse: pa_context_new() must have non-NULL appname
|
||||
static const char *getAppName(void)
|
||||
{
|
||||
const char *retval = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_APP_NAME);
|
||||
if (retval && *retval) {
|
||||
return retval;
|
||||
}
|
||||
retval = SDL_GetHint(SDL_HINT_APP_NAME);
|
||||
if (retval && *retval) {
|
||||
return retval;
|
||||
} else {
|
||||
const char *verstr = PULSEAUDIO_pa_get_library_version();
|
||||
retval = "SDL Application"; // the "oh well" default.
|
||||
if (verstr) {
|
||||
int maj, min, patch;
|
||||
if (SDL_sscanf(verstr, "%d.%d.%d", &maj, &min, &patch) == 3) {
|
||||
if (squashVersion(maj, min, patch) >= squashVersion(0, 9, 15)) {
|
||||
retval = NULL; // 0.9.15+ handles NULL correctly.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
return SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING);
|
||||
}
|
||||
|
||||
static void OperationStateChangeCallback(pa_operation *o, void *userdata)
|
||||
|
||||
@@ -495,11 +495,8 @@ SDL_bool SDL_DBus_ScreensaverInhibit(SDL_bool inhibit)
|
||||
const char *interface = "org.freedesktop.ScreenSaver";
|
||||
|
||||
if (inhibit) {
|
||||
const char *app = SDL_GetHint(SDL_HINT_APP_NAME);
|
||||
const char *app = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING);
|
||||
const char *reason = SDL_GetHint(SDL_HINT_SCREENSAVER_INHIBIT_ACTIVITY_NAME);
|
||||
if (!app || !app[0]) {
|
||||
app = "My SDL application";
|
||||
}
|
||||
if (!reason || !reason[0]) {
|
||||
reason = default_inhibit_reason;
|
||||
}
|
||||
|
||||
@@ -59,8 +59,7 @@ const char *SDL_GetExeName(void)
|
||||
|
||||
const char *SDL_GetAppID(void)
|
||||
{
|
||||
/* Always check the hint, as it may have changed */
|
||||
const char *id_str = SDL_GetHint(SDL_HINT_APP_ID);
|
||||
const char *id_str = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING);
|
||||
|
||||
if (!id_str) {
|
||||
/* If the hint isn't set, try to use the application's executable name */
|
||||
|
||||
@@ -158,6 +158,7 @@ SDL3_0.0.0 {
|
||||
SDL_GetAndroidInternalStoragePath;
|
||||
SDL_GetAndroidJNIEnv;
|
||||
SDL_GetAndroidSDKVersion;
|
||||
SDL_GetAppMetadataProperty;
|
||||
SDL_GetAssertionHandler;
|
||||
SDL_GetAssertionReport;
|
||||
SDL_GetAudioDeviceChannelMap;
|
||||
@@ -694,6 +695,8 @@ SDL3_0.0.0 {
|
||||
SDL_SendGamepadEffect;
|
||||
SDL_SendJoystickEffect;
|
||||
SDL_SendJoystickVirtualSensorData;
|
||||
SDL_SetAppMetadata;
|
||||
SDL_SetAppMetadataProperty;
|
||||
SDL_SetAssertionHandler;
|
||||
SDL_SetAudioDeviceGain;
|
||||
SDL_SetAudioPostmixCallback;
|
||||
|
||||
@@ -183,6 +183,7 @@
|
||||
#define SDL_GetAndroidInternalStoragePath SDL_GetAndroidInternalStoragePath_REAL
|
||||
#define SDL_GetAndroidJNIEnv SDL_GetAndroidJNIEnv_REAL
|
||||
#define SDL_GetAndroidSDKVersion SDL_GetAndroidSDKVersion_REAL
|
||||
#define SDL_GetAppMetadataProperty SDL_GetAppMetadataProperty_REAL
|
||||
#define SDL_GetAssertionHandler SDL_GetAssertionHandler_REAL
|
||||
#define SDL_GetAssertionReport SDL_GetAssertionReport_REAL
|
||||
#define SDL_GetAudioDeviceChannelMap SDL_GetAudioDeviceChannelMap_REAL
|
||||
@@ -719,6 +720,8 @@
|
||||
#define SDL_SendGamepadEffect SDL_SendGamepadEffect_REAL
|
||||
#define SDL_SendJoystickEffect SDL_SendJoystickEffect_REAL
|
||||
#define SDL_SendJoystickVirtualSensorData SDL_SendJoystickVirtualSensorData_REAL
|
||||
#define SDL_SetAppMetadata SDL_SetAppMetadata_REAL
|
||||
#define SDL_SetAppMetadataProperty SDL_SetAppMetadataProperty_REAL
|
||||
#define SDL_SetAssertionHandler SDL_SetAssertionHandler_REAL
|
||||
#define SDL_SetAudioDeviceGain SDL_SetAudioDeviceGain_REAL
|
||||
#define SDL_SetAudioPostmixCallback SDL_SetAudioPostmixCallback_REAL
|
||||
|
||||
@@ -203,6 +203,7 @@ SDL_DYNAPI_PROC(Uint32,SDL_GetAndroidExternalStorageState,(void),(),return)
|
||||
SDL_DYNAPI_PROC(const char*,SDL_GetAndroidInternalStoragePath,(void),(),return)
|
||||
SDL_DYNAPI_PROC(void*,SDL_GetAndroidJNIEnv,(void),(),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_GetAndroidSDKVersion,(void),(),return)
|
||||
SDL_DYNAPI_PROC(const char*,SDL_GetAppMetadataProperty,(const char *a),(a),return)
|
||||
SDL_DYNAPI_PROC(SDL_AssertionHandler,SDL_GetAssertionHandler,(void **a),(a),return)
|
||||
SDL_DYNAPI_PROC(const SDL_AssertData*,SDL_GetAssertionReport,(void),(),return)
|
||||
SDL_DYNAPI_PROC(int*,SDL_GetAudioDeviceChannelMap,(SDL_AudioDeviceID a, int *b),(a,b),return)
|
||||
@@ -730,6 +731,8 @@ SDL_DYNAPI_PROC(int,SDL_SendAndroidMessage,(Uint32 a, int b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_SendGamepadEffect,(SDL_Gamepad *a, const void *b, int c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_SendJoystickEffect,(SDL_Joystick *a, const void *b, int c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_SendJoystickVirtualSensorData,(SDL_Joystick *a, SDL_SensorType b, Uint64 c, const float *d, int e),(a,b,c,d,e),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_SetAppMetadata,(const char *a, const char *b, const char *c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_SetAppMetadataProperty,(const char *a, const char *b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_SetAssertionHandler,(SDL_AssertionHandler a, void *b),(a,b),)
|
||||
SDL_DYNAPI_PROC(int,SDL_SetAudioDeviceGain,(SDL_AudioDeviceID a, float b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_SetAudioPostmixCallback,(SDL_AudioDeviceID a, SDL_AudioPostmixCallback b, void *c),(a,b,c),return)
|
||||
|
||||
@@ -362,12 +362,19 @@ static SDL3AppDelegate *appDelegate = nil;
|
||||
|
||||
static NSString *GetApplicationName(void)
|
||||
{
|
||||
NSString *appName;
|
||||
NSString *appName = nil;
|
||||
|
||||
const char *metaname = SDL_GetStringProperty(SDL_GetGlobalProperties(), SDL_PROP_APP_METADATA_NAME_STRING, NULL);
|
||||
if (metaname && *metaname) {
|
||||
appName = [NSString stringWithUTF8String:metaname];
|
||||
}
|
||||
|
||||
/* Determine the application name */
|
||||
appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
|
||||
if (!appName) {
|
||||
appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
|
||||
appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
|
||||
if (!appName) {
|
||||
appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
|
||||
}
|
||||
}
|
||||
|
||||
if (![appName length]) {
|
||||
@@ -420,6 +427,10 @@ static void CreateApplicationMenus(void)
|
||||
|
||||
/* Add menu items */
|
||||
title = [@"About " stringByAppendingString:appName];
|
||||
|
||||
// !!! FIXME: Menu items can't take parameters, just a basic selector, so this should instead call a selector
|
||||
// !!! FIXME: that itself calls -[NSApplication orderFrontStandardAboutPanelWithOptions:optionsDictionary],
|
||||
// !!! FIXME: filling in that NSDictionary with SDL_GetAppMetadataProperty()
|
||||
[appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
|
||||
|
||||
[appleMenu addItem:[NSMenuItem separatorItem]];
|
||||
|
||||
Reference in New Issue
Block a user