src/SDL_hints.c
changeset 7432 58908b180ebd
parent 7191 75360622e65f
child 7716 be4cda94bce2
     1.1 --- a/src/SDL_hints.c	Sat Jul 13 11:06:34 2013 +0800
     1.2 +++ b/src/SDL_hints.c	Sat Jul 13 03:13:41 2013 -0700
     1.3 @@ -21,43 +21,35 @@
     1.4  #include "SDL_config.h"
     1.5  
     1.6  #include "SDL_hints.h"
     1.7 -#include "SDL_hints_c.h"
     1.8 +#include "SDL_error.h"
     1.9  
    1.10  
    1.11  /* Assuming there aren't many hints set and they aren't being queried in
    1.12 -   critical performance paths, we'll just use a linked list here.
    1.13 +   critical performance paths, we'll just use linked lists here.
    1.14   */
    1.15 +typedef struct SDL_HintWatch {
    1.16 +    SDL_HintCallback callback;
    1.17 +    void *userdata;
    1.18 +    struct SDL_HintWatch *next;
    1.19 +} SDL_HintWatch;
    1.20 +
    1.21  typedef struct SDL_Hint {
    1.22      char *name;
    1.23      char *value;
    1.24      SDL_HintPriority priority;
    1.25 -    SDL_HintChangedCb callback;
    1.26 +    SDL_HintWatch *callbacks;
    1.27      struct SDL_Hint *next;
    1.28  } SDL_Hint;
    1.29  
    1.30  static SDL_Hint *SDL_hints;
    1.31  
    1.32  SDL_bool
    1.33 -SDL_RegisterHintChangedCb(const char *name, SDL_HintChangedCb hintCb)
    1.34 -{
    1.35 -    SDL_Hint *hint;
    1.36 -
    1.37 -    for (hint = SDL_hints; hint; hint = hint->next) {
    1.38 -        if (SDL_strcmp(name, hint->name) == 0) {
    1.39 -            hint->callback = hintCb;
    1.40 -            return SDL_TRUE;
    1.41 -        }
    1.42 -    }
    1.43 -
    1.44 -    return SDL_FALSE;
    1.45 -}
    1.46 -
    1.47 -SDL_bool
    1.48  SDL_SetHintWithPriority(const char *name, const char *value,
    1.49                          SDL_HintPriority priority)
    1.50  {
    1.51      const char *env;
    1.52      SDL_Hint *hint;
    1.53 +    SDL_HintWatch *entry;
    1.54  
    1.55      if (!name || !value) {
    1.56          return SDL_FALSE;
    1.57 @@ -73,12 +65,21 @@
    1.58              if (priority < hint->priority) {
    1.59                  return SDL_FALSE;
    1.60              }
    1.61 -            if (SDL_strcmp(hint->value, value) != 0) {
    1.62 -                if (hint->callback != NULL) {
    1.63 -                    (*hint->callback)(name, hint->value, value);
    1.64 +            if (!hint->value || !value || SDL_strcmp(hint->value, value) != 0) {
    1.65 +                for (entry = hint->callbacks; entry; ) {
    1.66 +                    /* Save the next entry in case this one is deleted */
    1.67 +                    SDL_HintWatch *next = entry->next;
    1.68 +                    entry->callback(entry->userdata, name, hint->value, value);
    1.69 +                    entry = next;
    1.70                  }
    1.71 -                SDL_free(hint->value);
    1.72 -                hint->value = SDL_strdup(value);
    1.73 +                if (hint->value) {
    1.74 +                    SDL_free(hint->value);
    1.75 +                }
    1.76 +                if (value) {
    1.77 +                    hint->value = SDL_strdup(value);
    1.78 +                } else {
    1.79 +                    hint->value = NULL;
    1.80 +                }
    1.81              }
    1.82              hint->priority = priority;
    1.83              return SDL_TRUE;
    1.84 @@ -91,9 +92,9 @@
    1.85          return SDL_FALSE;
    1.86      }
    1.87      hint->name = SDL_strdup(name);
    1.88 -    hint->value = SDL_strdup(value);
    1.89 +    hint->value = value ? SDL_strdup(value) : NULL;
    1.90      hint->priority = priority;
    1.91 -    hint->callback = NULL;
    1.92 +    hint->callbacks = NULL;
    1.93      hint->next = SDL_hints;
    1.94      SDL_hints = hint;
    1.95      return SDL_TRUE;
    1.96 @@ -123,16 +124,100 @@
    1.97      return env;
    1.98  }
    1.99  
   1.100 +void
   1.101 +SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
   1.102 +{
   1.103 +    SDL_Hint *hint;
   1.104 +    SDL_HintWatch *entry;
   1.105 +    const char *value;
   1.106 +
   1.107 +    if (!name || !*name) {
   1.108 +        SDL_InvalidParamError("name");
   1.109 +        return;
   1.110 +    }
   1.111 +    if (!callback) {
   1.112 +        SDL_InvalidParamError("callback");
   1.113 +        return;
   1.114 +    }
   1.115 +
   1.116 +    SDL_DelHintCallback(name, callback, userdata);
   1.117 +
   1.118 +    entry = (SDL_HintWatch *)SDL_malloc(sizeof(*entry));
   1.119 +    entry->callback = callback;
   1.120 +    entry->userdata = userdata;
   1.121 +
   1.122 +    for (hint = SDL_hints; hint; hint = hint->next) {
   1.123 +        if (SDL_strcmp(name, hint->name) == 0) {
   1.124 +            break;
   1.125 +        }
   1.126 +    }
   1.127 +    if (!hint) {
   1.128 +        /* Need to add a hint entry for this watcher */
   1.129 +        hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
   1.130 +        if (!hint) {
   1.131 +            return;
   1.132 +        }
   1.133 +        hint->name = SDL_strdup(name);
   1.134 +        hint->value = NULL;
   1.135 +        hint->priority = SDL_HINT_DEFAULT;
   1.136 +        hint->callbacks = NULL;
   1.137 +        hint->next = SDL_hints;
   1.138 +        SDL_hints = hint;
   1.139 +    }
   1.140 +
   1.141 +    /* Add it to the callbacks for this hint */
   1.142 +    entry->next = hint->callbacks;
   1.143 +    hint->callbacks = entry;
   1.144 +
   1.145 +    /* Now call it with the current value */
   1.146 +    value = SDL_GetHint(name);
   1.147 +    callback(userdata, name, value, value);
   1.148 +}
   1.149 +
   1.150 +void
   1.151 +SDL_DelHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
   1.152 +{
   1.153 +    SDL_Hint *hint;
   1.154 +    SDL_HintWatch *entry, *prev;
   1.155 +
   1.156 +    for (hint = SDL_hints; hint; hint = hint->next) {
   1.157 +        if (SDL_strcmp(name, hint->name) == 0) {
   1.158 +            prev = NULL;
   1.159 +            for (entry = hint->callbacks; entry; entry = entry->next) {
   1.160 +                if (callback == entry->callback && userdata == entry->userdata) {
   1.161 +                    if (prev) {
   1.162 +                        prev->next = entry->next;
   1.163 +                    } else {
   1.164 +                        hint->callbacks = entry->next;
   1.165 +                    }
   1.166 +                    SDL_free(entry);
   1.167 +                    break;
   1.168 +                }
   1.169 +                prev = entry;
   1.170 +            }
   1.171 +            return;
   1.172 +        }
   1.173 +    }
   1.174 +}
   1.175 +
   1.176  void SDL_ClearHints(void)
   1.177  {
   1.178      SDL_Hint *hint;
   1.179 +    SDL_HintWatch *entry;
   1.180  
   1.181      while (SDL_hints) {
   1.182          hint = SDL_hints;
   1.183          SDL_hints = hint->next;
   1.184  
   1.185          SDL_free(hint->name);
   1.186 -        SDL_free(hint->value);
   1.187 +        if (hint->value) {
   1.188 +            SDL_free(hint->value);
   1.189 +        }
   1.190 +        for (entry = hint->callbacks; entry; ) {
   1.191 +            SDL_HintWatch *freeable = entry;
   1.192 +            entry = entry->next;
   1.193 +            SDL_free(freeable);
   1.194 +        }
   1.195          SDL_free(hint);
   1.196      }
   1.197  }