src/SDL_hints.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 12 Feb 2011 12:04:23 -0800
changeset 5273 151f40ed9e55
parent 5262 b530ef003506
child 5535 96594ac5fd1a
permissions -rw-r--r--
This should match the header change.
slouken@5189
     1
/*
slouken@5189
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@5189
     4
slouken@5189
     5
    This library is free software; you can redistribute it and/or
slouken@5189
     6
    modify it under the terms of the GNU Lesser General Public
slouken@5189
     7
    License as published by the Free Software Foundation; either
slouken@5189
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@5189
     9
slouken@5189
    10
    This library is distributed in the hope that it will be useful,
slouken@5189
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@5189
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@5189
    13
    Lesser General Public License for more details.
slouken@5189
    14
slouken@5189
    15
    You should have received a copy of the GNU Lesser General Public
slouken@5189
    16
    License along with this library; if not, write to the Free Software
slouken@5189
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@5189
    18
slouken@5189
    19
    Sam Lantinga
slouken@5189
    20
    slouken@libsdl.org
slouken@5189
    21
*/
slouken@5189
    22
#include "SDL_config.h"
slouken@5189
    23
slouken@5189
    24
#include "SDL_hints.h"
slouken@5189
    25
slouken@5189
    26
slouken@5189
    27
/* Assuming there aren't many hints set and they aren't being queried in
slouken@5189
    28
   critical performance paths, we'll just use a linked list here.
slouken@5189
    29
 */
slouken@5189
    30
typedef struct SDL_Hint {
slouken@5189
    31
    char *name;
slouken@5189
    32
    char *value;
slouken@5189
    33
    SDL_HintPriority priority;
slouken@5189
    34
    struct SDL_Hint *next;
slouken@5189
    35
} SDL_Hint;
slouken@5189
    36
slouken@5189
    37
static SDL_Hint *SDL_hints;
slouken@5189
    38
slouken@5189
    39
slouken@5189
    40
SDL_bool
slouken@5200
    41
SDL_SetHintWithPriority(const char *name, const char *value,
slouken@5200
    42
                        SDL_HintPriority priority)
slouken@5189
    43
{
slouken@5189
    44
    const char *env;
slouken@5189
    45
    SDL_Hint *prev, *hint;
slouken@5189
    46
slouken@5189
    47
    if (!name || !value) {
slouken@5189
    48
        return SDL_FALSE;
slouken@5189
    49
    }
slouken@5189
    50
slouken@5189
    51
    env = SDL_getenv(name);
slouken@5189
    52
    if (env && priority < SDL_HINT_OVERRIDE) {
slouken@5189
    53
        return SDL_FALSE;
slouken@5189
    54
    }
slouken@5189
    55
slouken@5189
    56
    prev = NULL;
slouken@5189
    57
    for (hint = SDL_hints; hint; prev = hint, hint = hint->next) {
slouken@5189
    58
        if (SDL_strcmp(name, hint->name) == 0) {
slouken@5189
    59
            if (priority < hint->priority) {
slouken@5189
    60
                return SDL_FALSE;
slouken@5189
    61
            }
slouken@5189
    62
            if (SDL_strcmp(hint->value, value) != 0) {
slouken@5189
    63
                SDL_free(hint->value);
slouken@5189
    64
                hint->value = SDL_strdup(value);
slouken@5189
    65
            }
slouken@5189
    66
            hint->priority = priority;
slouken@5189
    67
            return SDL_TRUE;
slouken@5189
    68
        }
slouken@5189
    69
    }
slouken@5189
    70
slouken@5189
    71
    /* Couldn't find the hint, add a new one */
slouken@5189
    72
    hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
slouken@5189
    73
    if (!hint) {
slouken@5189
    74
        return SDL_FALSE;
slouken@5189
    75
    }
slouken@5189
    76
    hint->name = SDL_strdup(name);
slouken@5189
    77
    hint->value = SDL_strdup(value);
slouken@5189
    78
    hint->priority = priority;
slouken@5189
    79
    hint->next = SDL_hints;
slouken@5189
    80
    SDL_hints = hint;
slouken@5189
    81
    return SDL_TRUE;
slouken@5189
    82
}
slouken@5189
    83
slouken@5200
    84
SDL_bool
slouken@5200
    85
SDL_SetHint(const char *name, const char *value)
slouken@5200
    86
{
slouken@5200
    87
    return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL);
slouken@5200
    88
}
slouken@5200
    89
slouken@5189
    90
const char *
slouken@5189
    91
SDL_GetHint(const char *name)
slouken@5189
    92
{
slouken@5189
    93
    const char *env;
slouken@5189
    94
    SDL_Hint *hint;
slouken@5189
    95
slouken@5189
    96
    env = SDL_getenv(name);
slouken@5189
    97
    for (hint = SDL_hints; hint; hint = hint->next) {
slouken@5189
    98
        if (SDL_strcmp(name, hint->name) == 0) {
slouken@5189
    99
            if (!env || hint->priority == SDL_HINT_OVERRIDE) {
slouken@5189
   100
                return hint->value;
slouken@5189
   101
            }
slouken@5189
   102
            break;
slouken@5189
   103
        }
slouken@5189
   104
    }
slouken@5189
   105
    return env;
slouken@5189
   106
}
slouken@5189
   107
slouken@5273
   108
void SDL_ClearHints(void)
slouken@5189
   109
{
slouken@5189
   110
    SDL_Hint *hint;
slouken@5189
   111
slouken@5189
   112
    while (SDL_hints) {
slouken@5189
   113
        hint = SDL_hints;
slouken@5189
   114
        SDL_hints = hint->next;
slouken@5189
   115
slouken@5189
   116
        SDL_free(hint->name);
slouken@5189
   117
        SDL_free(hint->value);
slouken@5189
   118
        SDL_free(hint);
slouken@5189
   119
    }
slouken@5189
   120
}
slouken@5189
   121
slouken@5189
   122
/* vi: set ts=4 sw=4 expandtab: */