src/SDL_hints.c
author Ryan C. Gordon
Tue, 07 Apr 2020 14:03:13 -0400
changeset 13704 25edf3df6e51
parent 13422 fd6a12de91c7
permissions -rw-r--r--
emscripten: support KaiOS's Left Soft Key and Right Soft Key (thanks, pelya!).

Fixes Bugzilla #5027.
slouken@5189
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@13422
     3
  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
slouken@5189
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5189
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5189
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5189
    20
*/
icculus@8093
    21
#include "./SDL_internal.h"
slouken@5189
    22
slouken@5189
    23
#include "SDL_hints.h"
slouken@7432
    24
#include "SDL_error.h"
sylvain@13246
    25
#include "SDL_hints_c.h"
slouken@5189
    26
slouken@5189
    27
slouken@5189
    28
/* Assuming there aren't many hints set and they aren't being queried in
slouken@7432
    29
   critical performance paths, we'll just use linked lists here.
slouken@5189
    30
 */
slouken@7432
    31
typedef struct SDL_HintWatch {
slouken@7432
    32
    SDL_HintCallback callback;
slouken@7432
    33
    void *userdata;
slouken@7432
    34
    struct SDL_HintWatch *next;
slouken@7432
    35
} SDL_HintWatch;
slouken@7432
    36
slouken@5189
    37
typedef struct SDL_Hint {
slouken@5189
    38
    char *name;
slouken@5189
    39
    char *value;
slouken@5189
    40
    SDL_HintPriority priority;
slouken@7432
    41
    SDL_HintWatch *callbacks;
slouken@5189
    42
    struct SDL_Hint *next;
slouken@5189
    43
} SDL_Hint;
slouken@5189
    44
slouken@5189
    45
static SDL_Hint *SDL_hints;
slouken@5189
    46
tim@5555
    47
SDL_bool
slouken@5200
    48
SDL_SetHintWithPriority(const char *name, const char *value,
slouken@5200
    49
                        SDL_HintPriority priority)
slouken@5189
    50
{
slouken@5189
    51
    const char *env;
icculus@6389
    52
    SDL_Hint *hint;
slouken@7432
    53
    SDL_HintWatch *entry;
slouken@5189
    54
slouken@5189
    55
    if (!name || !value) {
slouken@5189
    56
        return SDL_FALSE;
slouken@5189
    57
    }
slouken@5189
    58
slouken@5189
    59
    env = SDL_getenv(name);
slouken@5189
    60
    if (env && priority < SDL_HINT_OVERRIDE) {
slouken@5189
    61
        return SDL_FALSE;
slouken@5189
    62
    }
slouken@5189
    63
icculus@6389
    64
    for (hint = SDL_hints; hint; hint = hint->next) {
slouken@5189
    65
        if (SDL_strcmp(name, hint->name) == 0) {
slouken@5189
    66
            if (priority < hint->priority) {
slouken@5189
    67
                return SDL_FALSE;
slouken@5189
    68
            }
slouken@7432
    69
            if (!hint->value || !value || SDL_strcmp(hint->value, value) != 0) {
slouken@7432
    70
                for (entry = hint->callbacks; entry; ) {
slouken@7432
    71
                    /* Save the next entry in case this one is deleted */
slouken@7432
    72
                    SDL_HintWatch *next = entry->next;
slouken@7432
    73
                    entry->callback(entry->userdata, name, hint->value, value);
slouken@7432
    74
                    entry = next;
tim@5555
    75
                }
slouken@7719
    76
                SDL_free(hint->value);
slouken@7716
    77
                hint->value = value ? SDL_strdup(value) : NULL;
slouken@5189
    78
            }
slouken@5189
    79
            hint->priority = priority;
slouken@5189
    80
            return SDL_TRUE;
slouken@5189
    81
        }
slouken@5189
    82
    }
slouken@5189
    83
slouken@5189
    84
    /* Couldn't find the hint, add a new one */
slouken@5189
    85
    hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
slouken@5189
    86
    if (!hint) {
slouken@5189
    87
        return SDL_FALSE;
slouken@5189
    88
    }
slouken@5189
    89
    hint->name = SDL_strdup(name);
slouken@7432
    90
    hint->value = value ? SDL_strdup(value) : NULL;
slouken@5189
    91
    hint->priority = priority;
slouken@7432
    92
    hint->callbacks = NULL;
slouken@5189
    93
    hint->next = SDL_hints;
slouken@5189
    94
    SDL_hints = hint;
slouken@5189
    95
    return SDL_TRUE;
slouken@5189
    96
}
slouken@5189
    97
slouken@5200
    98
SDL_bool
slouken@5200
    99
SDL_SetHint(const char *name, const char *value)
slouken@5200
   100
{
slouken@5200
   101
    return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL);
slouken@5200
   102
}
slouken@5200
   103
slouken@5189
   104
const char *
slouken@5189
   105
SDL_GetHint(const char *name)
slouken@5189
   106
{
slouken@5189
   107
    const char *env;
slouken@5189
   108
    SDL_Hint *hint;
slouken@5189
   109
slouken@5189
   110
    env = SDL_getenv(name);
slouken@5189
   111
    for (hint = SDL_hints; hint; hint = hint->next) {
slouken@5189
   112
        if (SDL_strcmp(name, hint->name) == 0) {
slouken@5189
   113
            if (!env || hint->priority == SDL_HINT_OVERRIDE) {
slouken@5189
   114
                return hint->value;
slouken@5189
   115
            }
slouken@5189
   116
            break;
slouken@5189
   117
        }
slouken@5189
   118
    }
slouken@5189
   119
    return env;
slouken@5189
   120
}
slouken@5189
   121
slouken@10499
   122
SDL_bool
slouken@13244
   123
SDL_GetStringBoolean(const char *value, SDL_bool default_value)
slouken@13244
   124
{
slouken@13244
   125
    if (!value || !*value) {
slouken@13244
   126
        return default_value;
slouken@13244
   127
    }
slouken@13244
   128
    if (*value == '0' || SDL_strcasecmp(value, "false") == 0) {
slouken@13244
   129
        return SDL_FALSE;
slouken@13244
   130
    }
slouken@13244
   131
    return SDL_TRUE;
slouken@13244
   132
}
slouken@13244
   133
slouken@13244
   134
SDL_bool
slouken@10499
   135
SDL_GetHintBoolean(const char *name, SDL_bool default_value)
slouken@10499
   136
{
slouken@10499
   137
    const char *hint = SDL_GetHint(name);
slouken@13244
   138
    return SDL_GetStringBoolean(hint, default_value);
slouken@10499
   139
}
slouken@10499
   140
slouken@7432
   141
void
slouken@7432
   142
SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
slouken@7432
   143
{
slouken@7432
   144
    SDL_Hint *hint;
slouken@7432
   145
    SDL_HintWatch *entry;
slouken@7432
   146
    const char *value;
slouken@7432
   147
slouken@7432
   148
    if (!name || !*name) {
slouken@7432
   149
        SDL_InvalidParamError("name");
slouken@7432
   150
        return;
slouken@7432
   151
    }
slouken@7432
   152
    if (!callback) {
slouken@7432
   153
        SDL_InvalidParamError("callback");
slouken@7432
   154
        return;
slouken@7432
   155
    }
slouken@7432
   156
slouken@7432
   157
    SDL_DelHintCallback(name, callback, userdata);
slouken@7432
   158
slouken@7432
   159
    entry = (SDL_HintWatch *)SDL_malloc(sizeof(*entry));
philipp@9202
   160
    if (!entry) {
philipp@9202
   161
        SDL_OutOfMemory();
philipp@9202
   162
        return;
philipp@9202
   163
    }
slouken@7432
   164
    entry->callback = callback;
slouken@7432
   165
    entry->userdata = userdata;
slouken@7432
   166
slouken@7432
   167
    for (hint = SDL_hints; hint; hint = hint->next) {
slouken@7432
   168
        if (SDL_strcmp(name, hint->name) == 0) {
slouken@7432
   169
            break;
slouken@7432
   170
        }
slouken@7432
   171
    }
slouken@7432
   172
    if (!hint) {
slouken@7432
   173
        /* Need to add a hint entry for this watcher */
slouken@7432
   174
        hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
slouken@7432
   175
        if (!hint) {
philipp@9202
   176
            SDL_OutOfMemory();
philipp@9204
   177
            SDL_free(entry);
slouken@7432
   178
            return;
slouken@7432
   179
        }
slouken@7432
   180
        hint->name = SDL_strdup(name);
slouken@7432
   181
        hint->value = NULL;
slouken@7432
   182
        hint->priority = SDL_HINT_DEFAULT;
slouken@7432
   183
        hint->callbacks = NULL;
slouken@7432
   184
        hint->next = SDL_hints;
slouken@7432
   185
        SDL_hints = hint;
slouken@7432
   186
    }
slouken@7432
   187
slouken@7432
   188
    /* Add it to the callbacks for this hint */
slouken@7432
   189
    entry->next = hint->callbacks;
slouken@7432
   190
    hint->callbacks = entry;
slouken@7432
   191
slouken@7432
   192
    /* Now call it with the current value */
slouken@7432
   193
    value = SDL_GetHint(name);
slouken@7432
   194
    callback(userdata, name, value, value);
slouken@7432
   195
}
slouken@7432
   196
slouken@7432
   197
void
slouken@7432
   198
SDL_DelHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
slouken@7432
   199
{
slouken@7432
   200
    SDL_Hint *hint;
slouken@7432
   201
    SDL_HintWatch *entry, *prev;
slouken@7432
   202
slouken@7432
   203
    for (hint = SDL_hints; hint; hint = hint->next) {
slouken@7432
   204
        if (SDL_strcmp(name, hint->name) == 0) {
slouken@7432
   205
            prev = NULL;
slouken@7432
   206
            for (entry = hint->callbacks; entry; entry = entry->next) {
slouken@7432
   207
                if (callback == entry->callback && userdata == entry->userdata) {
slouken@7432
   208
                    if (prev) {
slouken@7432
   209
                        prev->next = entry->next;
slouken@7432
   210
                    } else {
slouken@7432
   211
                        hint->callbacks = entry->next;
slouken@7432
   212
                    }
slouken@7432
   213
                    SDL_free(entry);
slouken@7432
   214
                    break;
slouken@7432
   215
                }
slouken@7432
   216
                prev = entry;
slouken@7432
   217
            }
slouken@7432
   218
            return;
slouken@7432
   219
        }
slouken@7432
   220
    }
slouken@7432
   221
}
slouken@7432
   222
slouken@5273
   223
void SDL_ClearHints(void)
slouken@5189
   224
{
slouken@5189
   225
    SDL_Hint *hint;
slouken@7432
   226
    SDL_HintWatch *entry;
slouken@5189
   227
slouken@5189
   228
    while (SDL_hints) {
slouken@5189
   229
        hint = SDL_hints;
slouken@5189
   230
        SDL_hints = hint->next;
slouken@5189
   231
slouken@5189
   232
        SDL_free(hint->name);
slouken@7719
   233
        SDL_free(hint->value);
slouken@7432
   234
        for (entry = hint->callbacks; entry; ) {
slouken@7432
   235
            SDL_HintWatch *freeable = entry;
slouken@7432
   236
            entry = entry->next;
slouken@7432
   237
            SDL_free(freeable);
slouken@7432
   238
        }
slouken@5189
   239
        SDL_free(hint);
slouken@5189
   240
    }
slouken@5189
   241
}
slouken@5189
   242
slouken@5189
   243
/* vi: set ts=4 sw=4 expandtab: */