src/video/wayland/SDL_waylanddyn.c
author Gabriel Jacobo <gabomdq@gmail.com>
Thu, 09 Jan 2014 13:56:21 -0300
changeset 8104 2e4f1bd21196
child 8149 681eb46b8ac4
permissions -rw-r--r--
Dynamic loading support for Wayland
gabomdq@8104
     1
/*
gabomdq@8104
     2
  Simple DirectMedia Layer
gabomdq@8104
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
gabomdq@8104
     4
gabomdq@8104
     5
  This software is provided 'as-is', without any express or implied
gabomdq@8104
     6
  warranty.  In no event will the authors be held liable for any damages
gabomdq@8104
     7
  arising from the use of this software.
gabomdq@8104
     8
gabomdq@8104
     9
  Permission is granted to anyone to use this software for any purpose,
gabomdq@8104
    10
  including commercial applications, and to alter it and redistribute it
gabomdq@8104
    11
  freely, subject to the following restrictions:
gabomdq@8104
    12
gabomdq@8104
    13
  1. The origin of this software must not be misrepresented; you must not
gabomdq@8104
    14
     claim that you wrote the original software. If you use this software
gabomdq@8104
    15
     in a product, an acknowledgment in the product documentation would be
gabomdq@8104
    16
     appreciated but is not required.
gabomdq@8104
    17
  2. Altered source versions must be plainly marked as such, and must not be
gabomdq@8104
    18
     misrepresented as being the original software.
gabomdq@8104
    19
  3. This notice may not be removed or altered from any source distribution.
gabomdq@8104
    20
*/
gabomdq@8104
    21
#include "../../SDL_internal.h"
gabomdq@8104
    22
gabomdq@8104
    23
#if SDL_VIDEO_DRIVER_WAYLAND
gabomdq@8104
    24
gabomdq@8104
    25
#define DEBUG_DYNAMIC_WAYLAND 0
gabomdq@8104
    26
gabomdq@8104
    27
#include "SDL_waylanddyn.h"
gabomdq@8104
    28
gabomdq@8104
    29
#if DEBUG_DYNAMIC_WAYLAND
gabomdq@8104
    30
#include "SDL_log.h"
gabomdq@8104
    31
#endif
gabomdq@8104
    32
gabomdq@8104
    33
#ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
gabomdq@8104
    34
gabomdq@8104
    35
#include "SDL_name.h"
gabomdq@8104
    36
#include "SDL_loadso.h"
gabomdq@8104
    37
gabomdq@8104
    38
typedef struct
gabomdq@8104
    39
{
gabomdq@8104
    40
    void *lib;
gabomdq@8104
    41
    const char *libname;
gabomdq@8104
    42
} waylanddynlib;
gabomdq@8104
    43
gabomdq@8104
    44
#ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
gabomdq@8104
    45
#define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC NULL
gabomdq@8104
    46
#endif
gabomdq@8104
    47
#ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL
gabomdq@8104
    48
#define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL NULL
gabomdq@8104
    49
#endif
gabomdq@8104
    50
#ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR
gabomdq@8104
    51
#define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR NULL
gabomdq@8104
    52
#endif
gabomdq@8104
    53
#ifndef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON
gabomdq@8104
    54
#define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON NULL
gabomdq@8104
    55
#endif
gabomdq@8104
    56
gabomdq@8104
    57
static waylanddynlib waylandlibs[] = {
gabomdq@8104
    58
    {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC},
gabomdq@8104
    59
    {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL},
gabomdq@8104
    60
    {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR},
gabomdq@8104
    61
    {NULL, SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON}
gabomdq@8104
    62
};
gabomdq@8104
    63
gabomdq@8104
    64
static void *
gabomdq@8104
    65
WAYLAND_GetSym(const char *fnname, int *pHasModule)
gabomdq@8104
    66
{
gabomdq@8104
    67
    int i;
gabomdq@8104
    68
    void *fn = NULL;
gabomdq@8104
    69
    for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
gabomdq@8104
    70
        if (waylandlibs[i].lib != NULL) {
gabomdq@8104
    71
            fn = SDL_LoadFunction(waylandlibs[i].lib, fnname);
gabomdq@8104
    72
            if (fn != NULL)
gabomdq@8104
    73
                break;
gabomdq@8104
    74
        }
gabomdq@8104
    75
    }
gabomdq@8104
    76
gabomdq@8104
    77
#if DEBUG_DYNAMIC_WAYLAND
gabomdq@8104
    78
    if (fn != NULL)
gabomdq@8104
    79
        SDL_Log("WAYLAND: Found '%s' in %s (%p)\n", fnname, waylandlibs[i].libname, fn);
gabomdq@8104
    80
    else
gabomdq@8104
    81
        SDL_Log("WAYLAND: Symbol '%s' NOT FOUND!\n", fnname);
gabomdq@8104
    82
#endif
gabomdq@8104
    83
gabomdq@8104
    84
    if (fn == NULL)
gabomdq@8104
    85
        *pHasModule = 0;  /* kill this module. */
gabomdq@8104
    86
gabomdq@8104
    87
    return fn;
gabomdq@8104
    88
}
gabomdq@8104
    89
gabomdq@8104
    90
#endif /* SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC */
gabomdq@8104
    91
gabomdq@8104
    92
/* Define all the function pointers and wrappers... */
gabomdq@8104
    93
#define SDL_WAYLAND_MODULE(modname) int SDL_WAYLAND_HAVE_##modname = 0;
gabomdq@8104
    94
#define SDL_WAYLAND_SYM(rc,fn,params) SDL_DYNWAYLANDFN_##fn WAYLAND_##fn = NULL;
gabomdq@8104
    95
#define SDL_WAYLAND_INTERFACE(iface) const struct wl_interface *WAYLAND_##iface = NULL;
gabomdq@8104
    96
#include "SDL_waylandsym.h"
gabomdq@8104
    97
#undef SDL_WAYLAND_MODULE
gabomdq@8104
    98
#undef SDL_WAYLAND_SYM
gabomdq@8104
    99
#undef SDL_WAYLAND_INTERFACE
gabomdq@8104
   100
gabomdq@8104
   101
static int wayland_load_refcount = 0;
gabomdq@8104
   102
gabomdq@8104
   103
void
gabomdq@8104
   104
SDL_WAYLAND_UnloadSymbols(void)
gabomdq@8104
   105
{
gabomdq@8104
   106
    /* Don't actually unload if more than one module is using the libs... */
gabomdq@8104
   107
    if (wayland_load_refcount > 0) {
gabomdq@8104
   108
        if (--wayland_load_refcount == 0) {
gabomdq@8104
   109
#ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC            
gabomdq@8104
   110
            int i;
gabomdq@8104
   111
#endif
gabomdq@8104
   112
            
gabomdq@8104
   113
            /* set all the function pointers to NULL. */
gabomdq@8104
   114
#define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 0;
gabomdq@8104
   115
#define SDL_WAYLAND_SYM(rc,fn,params) WAYLAND_##fn = NULL;
gabomdq@8104
   116
#define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = NULL;
gabomdq@8104
   117
#include "SDL_waylandsym.h"
gabomdq@8104
   118
#undef SDL_WAYLAND_MODULE
gabomdq@8104
   119
#undef SDL_WAYLAND_SYM
gabomdq@8104
   120
#undef SDL_WAYLAND_INTERFACE
gabomdq@8104
   121
gabomdq@8104
   122
gabomdq@8104
   123
#ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
gabomdq@8104
   124
            for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
gabomdq@8104
   125
                if (waylandlibs[i].lib != NULL) {
gabomdq@8104
   126
                    SDL_UnloadObject(waylandlibs[i].lib);
gabomdq@8104
   127
                    waylandlibs[i].lib = NULL;
gabomdq@8104
   128
                }
gabomdq@8104
   129
            }
gabomdq@8104
   130
#endif
gabomdq@8104
   131
        }
gabomdq@8104
   132
    }
gabomdq@8104
   133
}
gabomdq@8104
   134
gabomdq@8104
   135
/* returns non-zero if all needed symbols were loaded. */
gabomdq@8104
   136
int
gabomdq@8104
   137
SDL_WAYLAND_LoadSymbols(void)
gabomdq@8104
   138
{
gabomdq@8104
   139
    int rc = 1;                 /* always succeed if not using Dynamic WAYLAND stuff. */
gabomdq@8104
   140
gabomdq@8104
   141
    /* deal with multiple modules (dga, wayland, etc) needing these symbols... */
gabomdq@8104
   142
    if (wayland_load_refcount++ == 0) {
gabomdq@8104
   143
#ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC
gabomdq@8104
   144
        int i;
gabomdq@8104
   145
        int *thismod = NULL;
gabomdq@8104
   146
        for (i = 0; i < SDL_TABLESIZE(waylandlibs); i++) {
gabomdq@8104
   147
            if (waylandlibs[i].libname != NULL) {
gabomdq@8104
   148
                waylandlibs[i].lib = SDL_LoadObject(waylandlibs[i].libname);
gabomdq@8104
   149
            }
gabomdq@8104
   150
        }
gabomdq@8104
   151
gabomdq@8104
   152
#define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 1; /* default yes */
gabomdq@8104
   153
#define SDL_WAYLAND_SYM(rc,fn,params)
gabomdq@8104
   154
#define SDL_WAYLAND_INTERFACE(iface)
gabomdq@8104
   155
#include "SDL_waylandsym.h"
gabomdq@8104
   156
#undef SDL_WAYLAND_MODULE
gabomdq@8104
   157
#undef SDL_WAYLAND_SYM
gabomdq@8104
   158
#undef SDL_WAYLAND_INTERFACE
gabomdq@8104
   159
gabomdq@8104
   160
#define SDL_WAYLAND_MODULE(modname) thismod = &SDL_WAYLAND_HAVE_##modname;
gabomdq@8104
   161
#define SDL_WAYLAND_SYM(rc,fn,params) WAYLAND_##fn = (SDL_DYNWAYLANDFN_##fn) WAYLAND_GetSym(#fn,thismod);
gabomdq@8104
   162
#define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = (struct wl_interface *) WAYLAND_GetSym(#iface,thismod);
gabomdq@8104
   163
#include "SDL_waylandsym.h"
gabomdq@8104
   164
#undef SDL_WAYLAND_MODULE
gabomdq@8104
   165
#undef SDL_WAYLAND_SYM
gabomdq@8104
   166
#undef SDL_WAYLAND_INTERFACE
gabomdq@8104
   167
gabomdq@8104
   168
        if (SDL_WAYLAND_HAVE_WAYLAND_CLIENT) {
gabomdq@8104
   169
            /* all required symbols loaded. */
gabomdq@8104
   170
            SDL_ClearError();
gabomdq@8104
   171
        } else {
gabomdq@8104
   172
            /* in case something got loaded... */
gabomdq@8104
   173
            SDL_WAYLAND_UnloadSymbols();
gabomdq@8104
   174
            rc = 0;
gabomdq@8104
   175
        }
gabomdq@8104
   176
gabomdq@8104
   177
#else  /* no dynamic WAYLAND */
gabomdq@8104
   178
gabomdq@8104
   179
#define SDL_WAYLAND_MODULE(modname) SDL_WAYLAND_HAVE_##modname = 1; /* default yes */
gabomdq@8104
   180
#define SDL_WAYLAND_SYM(rc,fn,params) WAYLAND_##fn = fn;
gabomdq@8104
   181
#define SDL_WAYLAND_INTERFACE(iface) WAYLAND_##iface = &iface;
gabomdq@8104
   182
#include "SDL_waylandsym.h"
gabomdq@8104
   183
#undef SDL_WAYLAND_MODULE
gabomdq@8104
   184
#undef SDL_WAYLAND_SYM
gabomdq@8104
   185
#undef SDL_WAYLAND_INTERFACE
gabomdq@8104
   186
gabomdq@8104
   187
#endif
gabomdq@8104
   188
    }
gabomdq@8104
   189
gabomdq@8104
   190
    return rc;
gabomdq@8104
   191
}
gabomdq@8104
   192
gabomdq@8104
   193
#endif /* SDL_VIDEO_DRIVER_WAYLAND */
gabomdq@8104
   194
gabomdq@8104
   195
/* vi: set ts=4 sw=4 expandtab: */