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.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #include "SDL_hints.h"
    24 
    25 
    26 /* Assuming there aren't many hints set and they aren't being queried in
    27    critical performance paths, we'll just use a linked list here.
    28  */
    29 typedef struct SDL_Hint {
    30     char *name;
    31     char *value;
    32     SDL_HintPriority priority;
    33     struct SDL_Hint *next;
    34 } SDL_Hint;
    35 
    36 static SDL_Hint *SDL_hints;
    37 
    38 
    39 SDL_bool
    40 SDL_SetHintWithPriority(const char *name, const char *value,
    41                         SDL_HintPriority priority)
    42 {
    43     const char *env;
    44     SDL_Hint *prev, *hint;
    45 
    46     if (!name || !value) {
    47         return SDL_FALSE;
    48     }
    49 
    50     env = SDL_getenv(name);
    51     if (env && priority < SDL_HINT_OVERRIDE) {
    52         return SDL_FALSE;
    53     }
    54 
    55     prev = NULL;
    56     for (hint = SDL_hints; hint; prev = hint, hint = hint->next) {
    57         if (SDL_strcmp(name, hint->name) == 0) {
    58             if (priority < hint->priority) {
    59                 return SDL_FALSE;
    60             }
    61             if (SDL_strcmp(hint->value, value) != 0) {
    62                 SDL_free(hint->value);
    63                 hint->value = SDL_strdup(value);
    64             }
    65             hint->priority = priority;
    66             return SDL_TRUE;
    67         }
    68     }
    69 
    70     /* Couldn't find the hint, add a new one */
    71     hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
    72     if (!hint) {
    73         return SDL_FALSE;
    74     }
    75     hint->name = SDL_strdup(name);
    76     hint->value = SDL_strdup(value);
    77     hint->priority = priority;
    78     hint->next = SDL_hints;
    79     SDL_hints = hint;
    80     return SDL_TRUE;
    81 }
    82 
    83 SDL_bool
    84 SDL_SetHint(const char *name, const char *value)
    85 {
    86     return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL);
    87 }
    88 
    89 const char *
    90 SDL_GetHint(const char *name)
    91 {
    92     const char *env;
    93     SDL_Hint *hint;
    94 
    95     env = SDL_getenv(name);
    96     for (hint = SDL_hints; hint; hint = hint->next) {
    97         if (SDL_strcmp(name, hint->name) == 0) {
    98             if (!env || hint->priority == SDL_HINT_OVERRIDE) {
    99                 return hint->value;
   100             }
   101             break;
   102         }
   103     }
   104     return env;
   105 }
   106 
   107 void SDL_ClearHints(void)
   108 {
   109     SDL_Hint *hint;
   110 
   111     while (SDL_hints) {
   112         hint = SDL_hints;
   113         SDL_hints = hint->next;
   114 
   115         SDL_free(hint->name);
   116         SDL_free(hint->value);
   117         SDL_free(hint);
   118     }
   119 }
   120 
   121 /* vi: set ts=4 sw=4 expandtab: */