comparison src/SDL_hints.c @ 5192:6f6a9340fb93

Added a hint system to allow configuration hints to be specified by the application.
author Sam Lantinga <slouken@libsdl.org>
date Sat, 05 Feb 2011 10:02:39 -0800
parents
children 01bced9a4cc1
comparison
equal deleted inserted replaced
5191:59e0688d766f 5192:6f6a9340fb93
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2010 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_SetHint(const char *name, const char *value, 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 const char *
84 SDL_GetHint(const char *name)
85 {
86 const char *env;
87 SDL_Hint *hint;
88
89 env = SDL_getenv(name);
90 for (hint = SDL_hints; hint; hint = hint->next) {
91 if (SDL_strcmp(name, hint->name) == 0) {
92 if (!env || hint->priority == SDL_HINT_OVERRIDE) {
93 return hint->value;
94 }
95 break;
96 }
97 }
98 return env;
99 }
100
101 void SDL_ClearHints()
102 {
103 SDL_Hint *hint;
104
105 while (SDL_hints) {
106 hint = SDL_hints;
107 SDL_hints = hint->next;
108
109 SDL_free(hint->name);
110 SDL_free(hint->value);
111 SDL_free(hint);
112 }
113 }
114
115 /* vi: set ts=4 sw=4 expandtab: */