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