SDL  2.0
SDL_hints.c File Reference
#include "./SDL_internal.h"
#include "SDL_hints.h"
#include "SDL_error.h"
+ Include dependency graph for SDL_hints.c:

Go to the source code of this file.

Data Structures

struct  SDL_HintWatch
 
struct  SDL_Hint
 

Functions

SDL_bool SDL_SetHintWithPriority (const char *name, const char *value, SDL_HintPriority priority)
 Set a hint with a specific priority. More...
 
SDL_bool SDL_SetHint (const char *name, const char *value)
 Set a hint with normal priority. More...
 
const char * SDL_GetHint (const char *name)
 Get a hint. More...
 
SDL_bool SDL_GetHintBoolean (const char *name, SDL_bool default_value)
 Get a hint. More...
 
void SDL_AddHintCallback (const char *name, SDL_HintCallback callback, void *userdata)
 Add a function to watch a particular hint. More...
 
void SDL_DelHintCallback (const char *name, SDL_HintCallback callback, void *userdata)
 Remove a function watching a particular hint. More...
 
void SDL_ClearHints (void)
 Clear all hints. More...
 

Variables

static SDL_HintSDL_hints
 

Function Documentation

◆ SDL_AddHintCallback()

void SDL_AddHintCallback ( const char *  name,
SDL_HintCallback  callback,
void userdata 
)

Add a function to watch a particular hint.

Parameters
nameThe hint to watch
callbackThe function to call when the hint value changes
userdataA pointer to pass to the callback function

Definition at line 135 of file SDL_hints.c.

136 {
137  SDL_Hint *hint;
138  SDL_HintWatch *entry;
139  const char *value;
140 
141  if (!name || !*name) {
142  SDL_InvalidParamError("name");
143  return;
144  }
145  if (!callback) {
146  SDL_InvalidParamError("callback");
147  return;
148  }
149 
150  SDL_DelHintCallback(name, callback, userdata);
151 
152  entry = (SDL_HintWatch *)SDL_malloc(sizeof(*entry));
153  if (!entry) {
154  SDL_OutOfMemory();
155  return;
156  }
157  entry->callback = callback;
158  entry->userdata = userdata;
159 
160  for (hint = SDL_hints; hint; hint = hint->next) {
161  if (SDL_strcmp(name, hint->name) == 0) {
162  break;
163  }
164  }
165  if (!hint) {
166  /* Need to add a hint entry for this watcher */
167  hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
168  if (!hint) {
169  SDL_OutOfMemory();
170  SDL_free(entry);
171  return;
172  }
173  hint->name = SDL_strdup(name);
174  hint->value = NULL;
175  hint->priority = SDL_HINT_DEFAULT;
176  hint->callbacks = NULL;
177  hint->next = SDL_hints;
178  SDL_hints = hint;
179  }
180 
181  /* Add it to the callbacks for this hint */
182  entry->next = hint->callbacks;
183  hint->callbacks = entry;
184 
185  /* Now call it with the current value */
187  callback(userdata, name, value, value);
188 }

References SDL_HintWatch::callback, callback(), SDL_Hint::callbacks, SDL_Hint::name, SDL_HintWatch::next, SDL_Hint::next, NULL, SDL_Hint::priority, SDL_DelHintCallback(), SDL_free, SDL_GetHint(), SDL_HINT_DEFAULT, SDL_hints, SDL_InvalidParamError, SDL_malloc, SDL_OutOfMemory, SDL_strcmp, SDL_strdup, SDL_HintWatch::userdata, and SDL_Hint::value.

◆ SDL_ClearHints()

void SDL_ClearHints ( void  )

Clear all hints.

This function is called during SDL_Quit() to free stored hints.

Definition at line 216 of file SDL_hints.c.

217 {
218  SDL_Hint *hint;
219  SDL_HintWatch *entry;
220 
221  while (SDL_hints) {
222  hint = SDL_hints;
223  SDL_hints = hint->next;
224 
225  SDL_free(hint->name);
226  SDL_free(hint->value);
227  for (entry = hint->callbacks; entry; ) {
228  SDL_HintWatch *freeable = entry;
229  entry = entry->next;
230  SDL_free(freeable);
231  }
232  SDL_free(hint);
233  }
234 }

References SDL_Hint::callbacks, SDL_Hint::name, SDL_HintWatch::next, SDL_Hint::next, SDL_free, SDL_hints, and SDL_Hint::value.

◆ SDL_DelHintCallback()

void SDL_DelHintCallback ( const char *  name,
SDL_HintCallback  callback,
void userdata 
)

Remove a function watching a particular hint.

Parameters
nameThe hint being watched
callbackThe function being called when the hint value changes
userdataA pointer being passed to the callback function

Definition at line 191 of file SDL_hints.c.

192 {
193  SDL_Hint *hint;
194  SDL_HintWatch *entry, *prev;
195 
196  for (hint = SDL_hints; hint; hint = hint->next) {
197  if (SDL_strcmp(name, hint->name) == 0) {
198  prev = NULL;
199  for (entry = hint->callbacks; entry; entry = entry->next) {
200  if (callback == entry->callback && userdata == entry->userdata) {
201  if (prev) {
202  prev->next = entry->next;
203  } else {
204  hint->callbacks = entry->next;
205  }
206  SDL_free(entry);
207  break;
208  }
209  prev = entry;
210  }
211  return;
212  }
213  }
214 }

References SDL_HintWatch::callback, callback(), SDL_Hint::callbacks, SDL_Hint::name, SDL_HintWatch::next, SDL_Hint::next, NULL, SDL_free, SDL_hints, SDL_strcmp, and SDL_HintWatch::userdata.

Referenced by SDL_AddHintCallback().

◆ SDL_GetHint()

const char* SDL_GetHint ( const char *  name)

Get a hint.

Returns
The string value of a hint variable.

Definition at line 104 of file SDL_hints.c.

105 {
106  const char *env;
107  SDL_Hint *hint;
108 
109  env = SDL_getenv(name);
110  for (hint = SDL_hints; hint; hint = hint->next) {
111  if (SDL_strcmp(name, hint->name) == 0) {
112  if (!env || hint->priority == SDL_HINT_OVERRIDE) {
113  return hint->value;
114  }
115  break;
116  }
117  }
118  return env;
119 }

References SDL_Hint::name, SDL_Hint::next, SDL_Hint::priority, SDL_getenv, SDL_HINT_OVERRIDE, SDL_hints, SDL_strcmp, and SDL_Hint::value.

Referenced by SDL_AddHintCallback(), and SDL_GetHintBoolean().

◆ SDL_GetHintBoolean()

SDL_bool SDL_GetHintBoolean ( const char *  name,
SDL_bool  default_value 
)

Get a hint.

Returns
The boolean value of a hint variable.

Definition at line 122 of file SDL_hints.c.

123 {
124  const char *hint = SDL_GetHint(name);
125  if (!hint || !*hint) {
126  return default_value;
127  }
128  if (*hint == '0' || SDL_strcasecmp(hint, "false") == 0) {
129  return SDL_FALSE;
130  }
131  return SDL_TRUE;
132 }

References SDL_FALSE, SDL_GetHint(), SDL_strcasecmp, and SDL_TRUE.

◆ SDL_SetHint()

SDL_bool SDL_SetHint ( const char *  name,
const char *  value 
)

Set a hint with normal priority.

Returns
SDL_TRUE if the hint was set, SDL_FALSE otherwise

Definition at line 98 of file SDL_hints.c.

99 {
101 }

References SDL_HINT_NORMAL, and SDL_SetHintWithPriority().

◆ SDL_SetHintWithPriority()

SDL_bool SDL_SetHintWithPriority ( const char *  name,
const char *  value,
SDL_HintPriority  priority 
)

Set a hint with a specific priority.

The priority controls the behavior when setting a hint that already has a value. Hints will replace existing hints of their priority and lower. Environment variables are considered to have override priority.

Returns
SDL_TRUE if the hint was set, SDL_FALSE otherwise

Definition at line 47 of file SDL_hints.c.

49 {
50  const char *env;
51  SDL_Hint *hint;
52  SDL_HintWatch *entry;
53 
54  if (!name || !value) {
55  return SDL_FALSE;
56  }
57 
58  env = SDL_getenv(name);
59  if (env && priority < SDL_HINT_OVERRIDE) {
60  return SDL_FALSE;
61  }
62 
63  for (hint = SDL_hints; hint; hint = hint->next) {
64  if (SDL_strcmp(name, hint->name) == 0) {
65  if (priority < hint->priority) {
66  return SDL_FALSE;
67  }
68  if (!hint->value || !value || SDL_strcmp(hint->value, value) != 0) {
69  for (entry = hint->callbacks; entry; ) {
70  /* Save the next entry in case this one is deleted */
71  SDL_HintWatch *next = entry->next;
72  entry->callback(entry->userdata, name, hint->value, value);
73  entry = next;
74  }
75  SDL_free(hint->value);
76  hint->value = value ? SDL_strdup(value) : NULL;
77  }
78  hint->priority = priority;
79  return SDL_TRUE;
80  }
81  }
82 
83  /* Couldn't find the hint, add a new one */
84  hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
85  if (!hint) {
86  return SDL_FALSE;
87  }
88  hint->name = SDL_strdup(name);
89  hint->value = value ? SDL_strdup(value) : NULL;
90  hint->priority = priority;
91  hint->callbacks = NULL;
92  hint->next = SDL_hints;
93  SDL_hints = hint;
94  return SDL_TRUE;
95 }

References SDL_HintWatch::callback, SDL_Hint::callbacks, SDL_Hint::name, SDL_HintWatch::next, SDL_Hint::next, NULL, SDL_Hint::priority, SDL_FALSE, SDL_free, SDL_getenv, SDL_HINT_OVERRIDE, SDL_hints, SDL_malloc, SDL_strcmp, SDL_strdup, SDL_TRUE, SDL_HintWatch::userdata, and SDL_Hint::value.

Referenced by SDL_SetHint().

Variable Documentation

◆ SDL_hints

SDL_Hint* SDL_hints
static
SDL_DelHintCallback
void SDL_DelHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
Remove a function watching a particular hint.
Definition: SDL_hints.c:191
SDL_hints
static SDL_Hint * SDL_hints
Definition: SDL_hints.c:44
SDL_SetHintWithPriority
SDL_bool SDL_SetHintWithPriority(const char *name, const char *value, SDL_HintPriority priority)
Set a hint with a specific priority.
Definition: SDL_hints.c:47
SDL_GetHint
const char * SDL_GetHint(const char *name)
Get a hint.
Definition: SDL_hints.c:104
NULL
#define NULL
Definition: begin_code.h:167
SDL_Hint
Definition: SDL_hints.c:36
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
callback
static Uint32 callback(Uint32 interval, void *param)
Definition: testtimer.c:34
SDL_HINT_DEFAULT
Definition: SDL_hints.h:1182
SDL_Hint::callbacks
SDL_HintWatch * callbacks
Definition: SDL_hints.c:40
SDL_strcasecmp
#define SDL_strcasecmp
Definition: SDL_dynapi_overrides.h:419
SDL_Hint::next
struct SDL_Hint * next
Definition: SDL_hints.c:41
SDL_HintWatch::userdata
void * userdata
Definition: SDL_hints.c:32
SDL_Hint::value
char * value
Definition: SDL_hints.c:38
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_FALSE
Definition: SDL_stdinc.h:163
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_HintWatch::next
struct SDL_HintWatch * next
Definition: SDL_hints.c:33
SDL_HintWatch
Definition: SDL_hints.c:30
SDL_Hint::name
char * name
Definition: SDL_hints.c:37
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_HintWatch::callback
SDL_HintCallback callback
Definition: SDL_hints.c:31
SDL_getenv
#define SDL_getenv
Definition: SDL_dynapi_overrides.h:378
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_strdup
#define SDL_strdup
Definition: SDL_dynapi_overrides.h:397
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_strcmp
#define SDL_strcmp
Definition: SDL_dynapi_overrides.h:417
SDL_HINT_NORMAL
Definition: SDL_hints.h:1183
SDL_HINT_OVERRIDE
Definition: SDL_hints.h:1184
SDL_Hint::priority
SDL_HintPriority priority
Definition: SDL_hints.c:39