src/SDL_hints.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6389 43a190ad60a7
child 7191 75360622e65f
permissions -rw-r--r--
Happy New Year!
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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 *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     for (hint = SDL_hints; hint; hint = hint->next) {
    72         if (SDL_strcmp(name, hint->name) == 0) {
    73             if (priority < hint->priority) {
    74                 return SDL_FALSE;
    75             }
    76             if (SDL_strcmp(hint->value, value) != 0) {
    77                 if (hint->callback != NULL) {
    78                     (*hint->callback)(name, hint->value, value);
    79                 }
    80                 SDL_free(hint->value);
    81                 hint->value = SDL_strdup(value);
    82             }
    83             hint->priority = priority;
    84             return SDL_TRUE;
    85         }
    86     }
    87 
    88     /* Couldn't find the hint, add a new one */
    89     hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
    90     if (!hint) {
    91         return SDL_FALSE;
    92     }
    93     hint->name = SDL_strdup(name);
    94     hint->value = SDL_strdup(value);
    95     hint->priority = priority;
    96     hint->callback = NULL;
    97     hint->next = SDL_hints;
    98     SDL_hints = hint;
    99     return SDL_TRUE;
   100 }
   101 
   102 SDL_bool
   103 SDL_SetHint(const char *name, const char *value)
   104 {
   105     return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL);
   106 }
   107 
   108 const char *
   109 SDL_GetHint(const char *name)
   110 {
   111     const char *env;
   112     SDL_Hint *hint;
   113 
   114     env = SDL_getenv(name);
   115     for (hint = SDL_hints; hint; hint = hint->next) {
   116         if (SDL_strcmp(name, hint->name) == 0) {
   117             if (!env || hint->priority == SDL_HINT_OVERRIDE) {
   118                 return hint->value;
   119             }
   120             break;
   121         }
   122     }
   123     return env;
   124 }
   125 
   126 void SDL_ClearHints(void)
   127 {
   128     SDL_Hint *hint;
   129 
   130     while (SDL_hints) {
   131         hint = SDL_hints;
   132         SDL_hints = hint->next;
   133 
   134         SDL_free(hint->name);
   135         SDL_free(hint->value);
   136         SDL_free(hint);
   137     }
   138 }
   139 
   140 /* vi: set ts=4 sw=4 expandtab: */