src/SDL_hints.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5273 151f40ed9e55
child 5555 3ecad5e5dbe4
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@5189
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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
*/
slouken@5189
    21
#include "SDL_config.h"
slouken@5189
    22
slouken@5189
    23
#include "SDL_hints.h"
slouken@5189
    24
slouken@5189
    25
slouken@5189
    26
/* Assuming there aren't many hints set and they aren't being queried in
slouken@5189
    27
   critical performance paths, we'll just use a linked list here.
slouken@5189
    28
 */
slouken@5189
    29
typedef struct SDL_Hint {
slouken@5189
    30
    char *name;
slouken@5189
    31
    char *value;
slouken@5189
    32
    SDL_HintPriority priority;
slouken@5189
    33
    struct SDL_Hint *next;
slouken@5189
    34
} SDL_Hint;
slouken@5189
    35
slouken@5189
    36
static SDL_Hint *SDL_hints;
slouken@5189
    37
slouken@5189
    38
slouken@5189
    39
SDL_bool
slouken@5200
    40
SDL_SetHintWithPriority(const char *name, const char *value,
slouken@5200
    41
                        SDL_HintPriority priority)
slouken@5189
    42
{
slouken@5189
    43
    const char *env;
slouken@5189
    44
    SDL_Hint *prev, *hint;
slouken@5189
    45
slouken@5189
    46
    if (!name || !value) {
slouken@5189
    47
        return SDL_FALSE;
slouken@5189
    48
    }
slouken@5189
    49
slouken@5189
    50
    env = SDL_getenv(name);
slouken@5189
    51
    if (env && priority < SDL_HINT_OVERRIDE) {
slouken@5189
    52
        return SDL_FALSE;
slouken@5189
    53
    }
slouken@5189
    54
slouken@5189
    55
    prev = NULL;
slouken@5189
    56
    for (hint = SDL_hints; hint; prev = hint, hint = hint->next) {
slouken@5189
    57
        if (SDL_strcmp(name, hint->name) == 0) {
slouken@5189
    58
            if (priority < hint->priority) {
slouken@5189
    59
                return SDL_FALSE;
slouken@5189
    60
            }
slouken@5189
    61
            if (SDL_strcmp(hint->value, value) != 0) {
slouken@5189
    62
                SDL_free(hint->value);
slouken@5189
    63
                hint->value = SDL_strdup(value);
slouken@5189
    64
            }
slouken@5189
    65
            hint->priority = priority;
slouken@5189
    66
            return SDL_TRUE;
slouken@5189
    67
        }
slouken@5189
    68
    }
slouken@5189
    69
slouken@5189
    70
    /* Couldn't find the hint, add a new one */
slouken@5189
    71
    hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
slouken@5189
    72
    if (!hint) {
slouken@5189
    73
        return SDL_FALSE;
slouken@5189
    74
    }
slouken@5189
    75
    hint->name = SDL_strdup(name);
slouken@5189
    76
    hint->value = SDL_strdup(value);
slouken@5189
    77
    hint->priority = priority;
slouken@5189
    78
    hint->next = SDL_hints;
slouken@5189
    79
    SDL_hints = hint;
slouken@5189
    80
    return SDL_TRUE;
slouken@5189
    81
}
slouken@5189
    82
slouken@5200
    83
SDL_bool
slouken@5200
    84
SDL_SetHint(const char *name, const char *value)
slouken@5200
    85
{
slouken@5200
    86
    return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL);
slouken@5200
    87
}
slouken@5200
    88
slouken@5189
    89
const char *
slouken@5189
    90
SDL_GetHint(const char *name)
slouken@5189
    91
{
slouken@5189
    92
    const char *env;
slouken@5189
    93
    SDL_Hint *hint;
slouken@5189
    94
slouken@5189
    95
    env = SDL_getenv(name);
slouken@5189
    96
    for (hint = SDL_hints; hint; hint = hint->next) {
slouken@5189
    97
        if (SDL_strcmp(name, hint->name) == 0) {
slouken@5189
    98
            if (!env || hint->priority == SDL_HINT_OVERRIDE) {
slouken@5189
    99
                return hint->value;
slouken@5189
   100
            }
slouken@5189
   101
            break;
slouken@5189
   102
        }
slouken@5189
   103
    }
slouken@5189
   104
    return env;
slouken@5189
   105
}
slouken@5189
   106
slouken@5273
   107
void SDL_ClearHints(void)
slouken@5189
   108
{
slouken@5189
   109
    SDL_Hint *hint;
slouken@5189
   110
slouken@5189
   111
    while (SDL_hints) {
slouken@5189
   112
        hint = SDL_hints;
slouken@5189
   113
        SDL_hints = hint->next;
slouken@5189
   114
slouken@5189
   115
        SDL_free(hint->name);
slouken@5189
   116
        SDL_free(hint->value);
slouken@5189
   117
        SDL_free(hint);
slouken@5189
   118
    }
slouken@5189
   119
}
slouken@5189
   120
slouken@5189
   121
/* vi: set ts=4 sw=4 expandtab: */