src/SDL_hints.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 05 Feb 2011 10:02:39 -0800
changeset 5189 6f6a9340fb93
child 5200 01bced9a4cc1
permissions -rw-r--r--
Added a hint system to allow configuration hints to be specified by the application.
slouken@5189
     1
/*
slouken@5189
     2
    SDL - Simple DirectMedia Layer
slouken@5189
     3
    Copyright (C) 1997-2010 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@5189
    41
SDL_SetHint(const char *name, const char *value, 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@5189
    83
const char *
slouken@5189
    84
SDL_GetHint(const char *name)
slouken@5189
    85
{
slouken@5189
    86
    const char *env;
slouken@5189
    87
    SDL_Hint *hint;
slouken@5189
    88
slouken@5189
    89
    env = SDL_getenv(name);
slouken@5189
    90
    for (hint = SDL_hints; hint; hint = hint->next) {
slouken@5189
    91
        if (SDL_strcmp(name, hint->name) == 0) {
slouken@5189
    92
            if (!env || hint->priority == SDL_HINT_OVERRIDE) {
slouken@5189
    93
                return hint->value;
slouken@5189
    94
            }
slouken@5189
    95
            break;
slouken@5189
    96
        }
slouken@5189
    97
    }
slouken@5189
    98
    return env;
slouken@5189
    99
}
slouken@5189
   100
slouken@5189
   101
void SDL_ClearHints()
slouken@5189
   102
{
slouken@5189
   103
    SDL_Hint *hint;
slouken@5189
   104
slouken@5189
   105
    while (SDL_hints) {
slouken@5189
   106
        hint = SDL_hints;
slouken@5189
   107
        SDL_hints = hint->next;
slouken@5189
   108
slouken@5189
   109
        SDL_free(hint->name);
slouken@5189
   110
        SDL_free(hint->value);
slouken@5189
   111
        SDL_free(hint);
slouken@5189
   112
    }
slouken@5189
   113
}
slouken@5189
   114
slouken@5189
   115
/* vi: set ts=4 sw=4 expandtab: */