Extended SDL_SetWindowData() to allow arbitrary named values.
authorSam Lantinga <slouken@libsdl.org>
Thu, 03 Feb 2011 11:16:57 -0800
changeset 51652b1989f59674
parent 5164 97423d858a1a
child 5166 4d39eeaad00b
Extended SDL_SetWindowData() to allow arbitrary named values.
include/SDL_video.h
src/video/SDL_sysvideo.h
src/video/SDL_video.c
     1.1 --- a/include/SDL_video.h	Thu Feb 03 10:03:55 2011 -0800
     1.2 +++ b/include/SDL_video.h	Thu Feb 03 11:16:57 2011 -0800
     1.3 @@ -483,24 +483,39 @@
     1.4                                                 SDL_Surface * icon);
     1.5  
     1.6  /**
     1.7 - *  \brief Associate an arbitrary pointer with a window.
     1.8 + *  \brief Associate an arbitrary named pointer with a window.
     1.9   *  
    1.10 + *  \param window   The window to associate with the pointer.
    1.11 + *  \param name     The name of the pointer.
    1.12 + *  \param userdata The associated pointer.
    1.13 + *
    1.14 + *  \return The previous value associated with 'name'
    1.15 + *
    1.16 + *  \note The name is case-sensitive.
    1.17 + *
    1.18   *  \sa SDL_GetWindowData()
    1.19   */
    1.20 -extern DECLSPEC void SDLCALL SDL_SetWindowData(SDL_Window * window,
    1.21 -                                               void *userdata);
    1.22 +extern DECLSPEC void* SDLCALL SDL_SetWindowData(SDL_Window * window,
    1.23 +                                                const char *name,
    1.24 +                                                void *userdata);
    1.25  
    1.26  /**
    1.27   *  \brief Retrieve the data pointer associated with a window.
    1.28   *  
    1.29 + *  \param window   The window to query.
    1.30 + *  \param name     The name of the pointer.
    1.31 + *
    1.32 + *  \return The value associated with 'name'
    1.33 + *  
    1.34   *  \sa SDL_SetWindowData()
    1.35   */
    1.36 -extern DECLSPEC void *SDLCALL SDL_GetWindowData(SDL_Window * window);
    1.37 +extern DECLSPEC void *SDLCALL SDL_GetWindowData(SDL_Window * window,
    1.38 +                                                const char *name);
    1.39  
    1.40  /**
    1.41   *  \brief Set the position of a window.
    1.42   *  
    1.43 - *  \param window The window to reposition.
    1.44 + *  \param window   The window to reposition.
    1.45   *  \param x        The x coordinate of the window, ::SDL_WINDOWPOS_CENTERED, or
    1.46                      ::SDL_WINDOWPOS_UNDEFINED.
    1.47   *  \param y        The y coordinate of the window, ::SDL_WINDOWPOS_CENTERED, or
     2.1 --- a/src/video/SDL_sysvideo.h	Thu Feb 03 10:03:55 2011 -0800
     2.2 +++ b/src/video/SDL_sysvideo.h	Thu Feb 03 11:16:57 2011 -0800
     2.3 @@ -59,6 +59,13 @@
     2.4      int (*ResizeWindowShape)(SDL_Window *window);
     2.5  };
     2.6  
     2.7 +typedef struct SDL_WindowUserData
     2.8 +{
     2.9 +    char *name;
    2.10 +    void *data;
    2.11 +    struct SDL_WindowUserData *next;
    2.12 +} SDL_WindowUserData;
    2.13 +
    2.14  /* Define the SDL window structure, corresponding to toplevel windows */
    2.15  struct SDL_Window
    2.16  {
    2.17 @@ -75,7 +82,8 @@
    2.18      
    2.19      SDL_WindowShaper *shaper;
    2.20  
    2.21 -    void *userdata;
    2.22 +    SDL_WindowUserData *data;
    2.23 +
    2.24      void *driverdata;
    2.25  
    2.26      SDL_Window *prev;
     3.1 --- a/src/video/SDL_video.c	Thu Feb 03 10:03:55 2011 -0800
     3.2 +++ b/src/video/SDL_video.c	Thu Feb 03 11:16:57 2011 -0800
     3.3 @@ -982,20 +982,60 @@
     3.4      }
     3.5  }
     3.6  
     3.7 -void
     3.8 -SDL_SetWindowData(SDL_Window * window, void *userdata)
     3.9 +void*
    3.10 +SDL_SetWindowData(SDL_Window * window, const char *name, void *userdata)
    3.11  {
    3.12 -    CHECK_WINDOW_MAGIC(window, );
    3.13 +    SDL_WindowUserData *prev, *data;
    3.14  
    3.15 -    window->userdata = userdata;
    3.16 +    CHECK_WINDOW_MAGIC(window, NULL);
    3.17 +
    3.18 +    /* See if the named data already exists */
    3.19 +    prev = NULL;
    3.20 +    for (data = window->data; data; prev = data, data = data->next) {
    3.21 +        if (SDL_strcmp(data->name, name) == 0) {
    3.22 +            void *last_value = data->data;
    3.23 +
    3.24 +            if (userdata) {
    3.25 +                /* Set the new value */
    3.26 +                data->data = userdata;
    3.27 +            } else {
    3.28 +                /* Delete this value */
    3.29 +                if (prev) {
    3.30 +                    prev->next = data->next;
    3.31 +                } else {
    3.32 +                    window->data = data->next;
    3.33 +                }
    3.34 +                SDL_free(data->name);
    3.35 +                SDL_free(data);
    3.36 +            }
    3.37 +            return last_value;
    3.38 +        }
    3.39 +    }
    3.40 +
    3.41 +    /* Add new data to the window */
    3.42 +    if (userdata) {
    3.43 +        data = (SDL_WindowUserData *)SDL_malloc(sizeof(*data));
    3.44 +        data->name = SDL_strdup(name);
    3.45 +        data->data = userdata;
    3.46 +        data->next = window->data;
    3.47 +        window->data = data;
    3.48 +    }
    3.49 +    return NULL;
    3.50  }
    3.51  
    3.52  void *
    3.53 -SDL_GetWindowData(SDL_Window * window)
    3.54 +SDL_GetWindowData(SDL_Window * window, const char *name)
    3.55  {
    3.56 +    SDL_WindowUserData *data;
    3.57 +
    3.58      CHECK_WINDOW_MAGIC(window, NULL);
    3.59  
    3.60 -    return window->userdata;
    3.61 +    for (data = window->data; data; data = data->next) {
    3.62 +        if (SDL_strcmp(data->name, name) == 0) {
    3.63 +            return data->data;
    3.64 +        }
    3.65 +    }
    3.66 +    return NULL;
    3.67  }
    3.68  
    3.69  void
    3.70 @@ -1293,10 +1333,6 @@
    3.71  
    3.72      CHECK_WINDOW_MAGIC(window, );
    3.73  
    3.74 -    if (window->title) {
    3.75 -        SDL_free(window->title);
    3.76 -    }
    3.77 -
    3.78      /* Restore video mode, etc. */
    3.79      SDL_UpdateFullscreenMode(window, SDL_FALSE);
    3.80  
    3.81 @@ -1310,6 +1346,18 @@
    3.82      /* Now invalidate magic */
    3.83      window->magic = NULL;
    3.84  
    3.85 +    /* Free memory associated with the window */
    3.86 +    if (window->title) {
    3.87 +        SDL_free(window->title);
    3.88 +    }
    3.89 +    while (window->data) {
    3.90 +        SDL_WindowUserData *data = window->data;
    3.91 +
    3.92 +        window->data = data->next;
    3.93 +        SDL_free(data->name);
    3.94 +        SDL_free(data);
    3.95 +    }
    3.96 +
    3.97      /* Unlink the window from the list */
    3.98      display = window->display;
    3.99      if (window->next) {