src/SDL_hints.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:28:07 -0500
changeset 6138 4c64952a58fb
parent 5555 3ecad5e5dbe4
child 6389 43a190ad60a7
permissions -rwxr-xr-x
Happy New Year!
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 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 #include "SDL_hints_c.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     SDL_HintChangedCb callback;
    35     struct SDL_Hint *next;
    36 } SDL_Hint;
    37 
    38 static SDL_Hint *SDL_hints;
    39 
    40 SDL_bool
    41 SDL_RegisterHintChangedCb(const char *name, SDL_HintChangedCb hintCb)
    42 {
    43     SDL_Hint *hint;
    44 	
    45     for (hint = SDL_hints; hint; hint = hint->next) {
    46         if (SDL_strcmp(name, hint->name) == 0) {
    47             hint->callback = hintCb;
    48             return SDL_TRUE;
    49         }
    50     }
    51 	
    52     return SDL_FALSE;
    53 }
    54 
    55 SDL_bool
    56 SDL_SetHintWithPriority(const char *name, const char *value,
    57                         SDL_HintPriority priority)
    58 {
    59     const char *env;
    60     SDL_Hint *prev, *hint;
    61 
    62     if (!name || !value) {
    63         return SDL_FALSE;
    64     }
    65 
    66     env = SDL_getenv(name);
    67     if (env && priority < SDL_HINT_OVERRIDE) {
    68         return SDL_FALSE;
    69     }
    70 
    71     prev = NULL;
    72     for (hint = SDL_hints; hint; prev = hint, hint = hint->next) {
    73         if (SDL_strcmp(name, hint->name) == 0) {
    74             if (priority < hint->priority) {
    75                 return SDL_FALSE;
    76             }
    77             if (SDL_strcmp(hint->value, value) != 0) {
    78                 if (hint->callback != NULL) {
    79                     (*hint->callback)(name, hint->value, value);
    80                 }
    81                 SDL_free(hint->value);
    82                 hint->value = SDL_strdup(value);
    83             }
    84             hint->priority = priority;
    85             return SDL_TRUE;
    86         }
    87     }
    88 
    89     /* Couldn't find the hint, add a new one */
    90     hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
    91     if (!hint) {
    92         return SDL_FALSE;
    93     }
    94     hint->name = SDL_strdup(name);
    95     hint->value = SDL_strdup(value);
    96     hint->priority = priority;
    97     hint->callback = NULL;
    98     hint->next = SDL_hints;
    99     SDL_hints = hint;
   100     return SDL_TRUE;
   101 }
   102 
   103 SDL_bool
   104 SDL_SetHint(const char *name, const char *value)
   105 {
   106     return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL);
   107 }
   108 
   109 const char *
   110 SDL_GetHint(const char *name)
   111 {
   112     const char *env;
   113     SDL_Hint *hint;
   114 
   115     env = SDL_getenv(name);
   116     for (hint = SDL_hints; hint; hint = hint->next) {
   117         if (SDL_strcmp(name, hint->name) == 0) {
   118             if (!env || hint->priority == SDL_HINT_OVERRIDE) {
   119                 return hint->value;
   120             }
   121             break;
   122         }
   123     }
   124     return env;
   125 }
   126 
   127 void SDL_ClearHints(void)
   128 {
   129     SDL_Hint *hint;
   130 
   131     while (SDL_hints) {
   132         hint = SDL_hints;
   133         SDL_hints = hint->next;
   134 
   135         SDL_free(hint->name);
   136         SDL_free(hint->value);
   137         SDL_free(hint);
   138     }
   139 }
   140 
   141 /* vi: set ts=4 sw=4 expandtab: */