src/SDL_hints.c
changeset 5189 6f6a9340fb93
child 5200 01bced9a4cc1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/SDL_hints.c	Sat Feb 05 10:02:39 2011 -0800
     1.3 @@ -0,0 +1,115 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#include "SDL_hints.h"
    1.28 +
    1.29 +
    1.30 +/* Assuming there aren't many hints set and they aren't being queried in
    1.31 +   critical performance paths, we'll just use a linked list here.
    1.32 + */
    1.33 +typedef struct SDL_Hint {
    1.34 +    char *name;
    1.35 +    char *value;
    1.36 +    SDL_HintPriority priority;
    1.37 +    struct SDL_Hint *next;
    1.38 +} SDL_Hint;
    1.39 +
    1.40 +static SDL_Hint *SDL_hints;
    1.41 +
    1.42 +
    1.43 +SDL_bool
    1.44 +SDL_SetHint(const char *name, const char *value, SDL_HintPriority priority)
    1.45 +{
    1.46 +    const char *env;
    1.47 +    SDL_Hint *prev, *hint;
    1.48 +
    1.49 +    if (!name || !value) {
    1.50 +        return SDL_FALSE;
    1.51 +    }
    1.52 +
    1.53 +    env = SDL_getenv(name);
    1.54 +    if (env && priority < SDL_HINT_OVERRIDE) {
    1.55 +        return SDL_FALSE;
    1.56 +    }
    1.57 +
    1.58 +    prev = NULL;
    1.59 +    for (hint = SDL_hints; hint; prev = hint, hint = hint->next) {
    1.60 +        if (SDL_strcmp(name, hint->name) == 0) {
    1.61 +            if (priority < hint->priority) {
    1.62 +                return SDL_FALSE;
    1.63 +            }
    1.64 +            if (SDL_strcmp(hint->value, value) != 0) {
    1.65 +                SDL_free(hint->value);
    1.66 +                hint->value = SDL_strdup(value);
    1.67 +            }
    1.68 +            hint->priority = priority;
    1.69 +            return SDL_TRUE;
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +    /* Couldn't find the hint, add a new one */
    1.74 +    hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
    1.75 +    if (!hint) {
    1.76 +        return SDL_FALSE;
    1.77 +    }
    1.78 +    hint->name = SDL_strdup(name);
    1.79 +    hint->value = SDL_strdup(value);
    1.80 +    hint->priority = priority;
    1.81 +    hint->next = SDL_hints;
    1.82 +    SDL_hints = hint;
    1.83 +    return SDL_TRUE;
    1.84 +}
    1.85 +
    1.86 +const char *
    1.87 +SDL_GetHint(const char *name)
    1.88 +{
    1.89 +    const char *env;
    1.90 +    SDL_Hint *hint;
    1.91 +
    1.92 +    env = SDL_getenv(name);
    1.93 +    for (hint = SDL_hints; hint; hint = hint->next) {
    1.94 +        if (SDL_strcmp(name, hint->name) == 0) {
    1.95 +            if (!env || hint->priority == SDL_HINT_OVERRIDE) {
    1.96 +                return hint->value;
    1.97 +            }
    1.98 +            break;
    1.99 +        }
   1.100 +    }
   1.101 +    return env;
   1.102 +}
   1.103 +
   1.104 +void SDL_ClearHints()
   1.105 +{
   1.106 +    SDL_Hint *hint;
   1.107 +
   1.108 +    while (SDL_hints) {
   1.109 +        hint = SDL_hints;
   1.110 +        SDL_hints = hint->next;
   1.111 +
   1.112 +        SDL_free(hint->name);
   1.113 +        SDL_free(hint->value);
   1.114 +        SDL_free(hint);
   1.115 +    }
   1.116 +}
   1.117 +
   1.118 +/* vi: set ts=4 sw=4 expandtab: */