src/video/x11/SDL_x11dyn.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 30 Oct 2016 21:01:33 +0100
changeset 10563 e3d84016cb79
parent 10079 6eaf52c8d398
child 10737 3406a0f8b041
permissions -rw-r--r--
Fixed outdated info in README.
slouken@1950
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@1950
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1950
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1950
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1950
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1950
    22
slouken@5481
    23
#if SDL_VIDEO_DRIVER_X11
slouken@5481
    24
slouken@1950
    25
#define DEBUG_DYNAMIC_X11 0
slouken@1950
    26
slouken@1950
    27
#include "SDL_x11dyn.h"
slouken@1950
    28
slouken@1950
    29
#if DEBUG_DYNAMIC_X11
slouken@1950
    30
#include <stdio.h>
slouken@1950
    31
#endif
slouken@1950
    32
slouken@1950
    33
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
    34
slouken@1950
    35
#include "SDL_name.h"
slouken@1950
    36
#include "SDL_loadso.h"
slouken@1950
    37
slouken@1950
    38
typedef struct
slouken@1950
    39
{
slouken@1950
    40
    void *lib;
slouken@1950
    41
    const char *libname;
slouken@1950
    42
} x11dynlib;
slouken@1950
    43
slouken@1950
    44
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
    45
#define SDL_VIDEO_DRIVER_X11_DYNAMIC NULL
slouken@1950
    46
#endif
slouken@1950
    47
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
slouken@1950
    48
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT NULL
slouken@1950
    49
#endif
slouken@5471
    50
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR
slouken@5471
    51
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR NULL
slouken@5471
    52
#endif
slouken@5408
    53
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA
slouken@5408
    54
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA NULL
slouken@5408
    55
#endif
slouken@6311
    56
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2
slouken@6311
    57
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 NULL
slouken@1950
    58
#endif
slouken@1950
    59
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
slouken@1950
    60
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR NULL
slouken@1950
    61
#endif
bob@3036
    62
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS
bob@3036
    63
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS NULL
bob@3036
    64
#endif
slouken@5408
    65
#ifndef SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE
slouken@5408
    66
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE NULL
slouken@5408
    67
#endif
slouken@1950
    68
slouken@1950
    69
static x11dynlib x11libs[] = {
slouken@1950
    70
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC},
slouken@1950
    71
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT},
slouken@5471
    72
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR},
slouken@5408
    73
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA},
slouken@6311
    74
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2},
slouken@1950
    75
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR},
slouken@3025
    76
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS},
slouken@5408
    77
    {NULL, SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE}
slouken@1950
    78
};
slouken@1950
    79
icculus@6376
    80
static void *
icculus@6376
    81
X11_GetSym(const char *fnname, int *pHasModule)
slouken@1950
    82
{
slouken@1950
    83
    int i;
icculus@6376
    84
    void *fn = NULL;
slouken@1950
    85
    for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
    86
        if (x11libs[i].lib != NULL) {
icculus@6376
    87
            fn = SDL_LoadFunction(x11libs[i].lib, fnname);
icculus@6376
    88
            if (fn != NULL)
slouken@1950
    89
                break;
slouken@1950
    90
        }
slouken@1950
    91
    }
slouken@1950
    92
slouken@1950
    93
#if DEBUG_DYNAMIC_X11
icculus@6376
    94
    if (fn != NULL)
icculus@6376
    95
        printf("X11: Found '%s' in %s (%p)\n", fnname, x11libs[i].libname, fn);
slouken@1950
    96
    else
slouken@1950
    97
        printf("X11: Symbol '%s' NOT FOUND!\n", fnname);
slouken@1950
    98
#endif
slouken@1950
    99
icculus@6376
   100
    if (fn == NULL)
icculus@6376
   101
        *pHasModule = 0;  /* kill this module. */
icculus@6376
   102
icculus@6376
   103
    return fn;
slouken@1950
   104
}
slouken@1950
   105
icculus@7827
   106
#endif /* SDL_VIDEO_DRIVER_X11_DYNAMIC */
slouken@1950
   107
slouken@1950
   108
/* Define all the function pointers and wrappers... */
icculus@7827
   109
#define SDL_X11_SYM(rc,fn,params,args,ret) SDL_DYNX11FN_##fn X11_##fn = NULL;
slouken@1950
   110
#include "SDL_x11sym.h"
slouken@1950
   111
slouken@1950
   112
/* Annoying varargs entry point... */
slouken@1950
   113
#ifdef X_HAVE_UTF8_STRING
icculus@7827
   114
SDL_DYNX11FN_XCreateIC X11_XCreateIC = NULL;
icculus@7827
   115
SDL_DYNX11FN_XGetICValues X11_XGetICValues = NULL;
slouken@1950
   116
#endif
slouken@1950
   117
slouken@1950
   118
/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
icculus@6026
   119
#define SDL_X11_MODULE(modname) int SDL_X11_HAVE_##modname = 0;
slouken@1950
   120
#include "SDL_x11sym.h"
slouken@1950
   121
slouken@1950
   122
static int x11_load_refcount = 0;
slouken@1950
   123
slouken@1950
   124
void
slouken@1950
   125
SDL_X11_UnloadSymbols(void)
slouken@1950
   126
{
slouken@1950
   127
    /* Don't actually unload if more than one module is using the libs... */
slouken@1950
   128
    if (x11_load_refcount > 0) {
slouken@1950
   129
        if (--x11_load_refcount == 0) {
slouken@1950
   130
            int i;
slouken@1950
   131
slouken@1950
   132
            /* set all the function pointers to NULL. */
icculus@6026
   133
#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 0;
icculus@7827
   134
#define SDL_X11_SYM(rc,fn,params,args,ret) X11_##fn = NULL;
slouken@1950
   135
#include "SDL_x11sym.h"
slouken@1950
   136
slouken@1950
   137
#ifdef X_HAVE_UTF8_STRING
icculus@7827
   138
            X11_XCreateIC = NULL;
icculus@7827
   139
            X11_XGetICValues = NULL;
slouken@1950
   140
#endif
slouken@1950
   141
icculus@7827
   142
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   143
            for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   144
                if (x11libs[i].lib != NULL) {
slouken@1950
   145
                    SDL_UnloadObject(x11libs[i].lib);
slouken@1950
   146
                    x11libs[i].lib = NULL;
slouken@1950
   147
                }
slouken@1950
   148
            }
icculus@7827
   149
#endif
slouken@1950
   150
        }
slouken@1950
   151
    }
slouken@1950
   152
}
slouken@1950
   153
slouken@1950
   154
/* returns non-zero if all needed symbols were loaded. */
slouken@1950
   155
int
slouken@1950
   156
SDL_X11_LoadSymbols(void)
slouken@1950
   157
{
slouken@1950
   158
    int rc = 1;                 /* always succeed if not using Dynamic X11 stuff. */
slouken@1950
   159
slouken@1950
   160
    /* deal with multiple modules (dga, x11, etc) needing these symbols... */
slouken@1950
   161
    if (x11_load_refcount++ == 0) {
icculus@7827
   162
#ifdef SDL_VIDEO_DRIVER_X11_DYNAMIC
slouken@1950
   163
        int i;
slouken@1950
   164
        int *thismod = NULL;
slouken@1950
   165
        for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
slouken@1950
   166
            if (x11libs[i].libname != NULL) {
slouken@1950
   167
                x11libs[i].lib = SDL_LoadObject(x11libs[i].libname);
slouken@1950
   168
            }
slouken@1950
   169
        }
icculus@6030
   170
icculus@6030
   171
#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1; /* default yes */
icculus@6030
   172
#include "SDL_x11sym.h"
icculus@6030
   173
slouken@1950
   174
#define SDL_X11_MODULE(modname) thismod = &SDL_X11_HAVE_##modname;
icculus@7827
   175
#define SDL_X11_SYM(a,fn,x,y,z) X11_##fn = (SDL_DYNX11FN_##fn) X11_GetSym(#fn,thismod);
slouken@1950
   176
#include "SDL_x11sym.h"
slouken@1950
   177
slouken@1950
   178
#ifdef X_HAVE_UTF8_STRING
icculus@7827
   179
        X11_XCreateIC = (SDL_DYNX11FN_XCreateIC)
icculus@6376
   180
                        X11_GetSym("XCreateIC", &SDL_X11_HAVE_UTF8);
icculus@7827
   181
        X11_XGetICValues = (SDL_DYNX11FN_XGetICValues)
icculus@6376
   182
                        X11_GetSym("XGetICValues", &SDL_X11_HAVE_UTF8);
slouken@1950
   183
#endif
slouken@1950
   184
slouken@2120
   185
        if (SDL_X11_HAVE_BASEXLIB) {
slouken@2120
   186
            /* all required symbols loaded. */
icculus@2118
   187
            SDL_ClearError();
icculus@2118
   188
        } else {
slouken@2120
   189
            /* in case something got loaded... */
slouken@2120
   190
            SDL_X11_UnloadSymbols();
slouken@1950
   191
            rc = 0;
slouken@1950
   192
        }
icculus@7827
   193
icculus@7827
   194
#else  /* no dynamic X11 */
icculus@7827
   195
slouken@6898
   196
#define SDL_X11_MODULE(modname) SDL_X11_HAVE_##modname = 1; /* default yes */
icculus@9718
   197
#define SDL_X11_SYM(a,fn,x,y,z) X11_##fn = (SDL_DYNX11FN_##fn) fn;
slouken@6898
   198
#include "SDL_x11sym.h"
slouken@6898
   199
slouken@1950
   200
#ifdef X_HAVE_UTF8_STRING
icculus@7827
   201
        X11_XCreateIC = XCreateIC;
icculus@7827
   202
        X11_XGetICValues = XGetICValues;
slouken@1950
   203
#endif
slouken@1950
   204
#endif
icculus@7827
   205
    }
slouken@1950
   206
slouken@1950
   207
    return rc;
slouken@1950
   208
}
slouken@1950
   209
slouken@5481
   210
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   211
slouken@1950
   212
/* vi: set ts=4 sw=4 expandtab: */