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