#include "SDL_internal.h"
#include "../SDL_dialog.h"
#include "./SDL_portaldialog.h"
#include "./SDL_zenitydialog.h"
static void (*detected_function)(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props) = NULL;
void SDLCALL hint_callback(void *userdata, const char *name, const char *oldValue, const char *newValue);
static void set_callback(void)
{
static bool is_set = false;
if (is_set == false) {
is_set = true;
SDL_AddHintCallback(SDL_HINT_FILE_DIALOG_DRIVER, hint_callback, NULL);
}
}
static int detect_available_methods(const char *value)
{
const char *driver = value ? value : SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER);
set_callback();
if (driver == NULL || SDL_strcmp(driver, "portal") == 0) {
if (SDL_Portal_detect()) {
detected_function = SDL_Portal_ShowFileDialogWithProperties;
return 1;
}
}
if (driver == NULL || SDL_strcmp(driver, "zenity") == 0) {
if (SDL_Zenity_detect()) {
detected_function = SDL_Zenity_ShowFileDialogWithProperties;
return 2;
}
}
SDL_SetError("File dialog driver unsupported (supported values for SDL_HINT_FILE_DIALOG_DRIVER are 'zenity' and 'portal')");
return 0;
}
void SDLCALL hint_callback(void *userdata, const char *name, const char *oldValue, const char *newValue)
{
detect_available_methods(newValue);
}
void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props)
{
if (!detected_function && !detect_available_methods(NULL)) {
callback(userdata, NULL, -1);
return;
}
detected_function(type, callback, userdata, props);
}