1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*
* boblight
* Copyright (C) Bob 2009
*
* boblight is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* boblight is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//if you define BOBLIGHT_DLOPEN, all boblight functions are defined as pointers
//you can then call boblight_loadlibrary to load libboblight with dlopen and the function pointers with dlsym
//if you pass NULL to boblight_loadlibrary's first argument, the default filename for libboblight is used
//if boblight_loadlibrary returns NULL, dlopen and dlsym went ok, if not it returns a char* from dlerror
//if you want to use the boblight functions from multiple files, you can define BOBLIGHT_DLOPEN in one file,
//and define BOBLIGHT_DLOPEN_EXTERN in the other file, the functionpointers are then defined as extern
#ifndef LIBBOBLIGHT
#define LIBBOBLIGHT
#if !defined(BOBLIGHT_DLOPEN) && !defined(BOBLIGHT_DLOPEN_EXTERN)
#ifdef __cplusplus
extern "C" {
#endif
//generate normal prototypes
#define BOBLIGHT_FUNCTION(returnvalue, name, arguments) returnvalue name arguments
#include "boblight-functions.h"
#undef BOBLIGHT_FUNCTION
#ifdef __cplusplus
}
#endif
#elif defined(BOBLIGHT_DLOPEN)
#include <dlfcn.h>
#include <stddef.h>
//generate function pointers
#define BOBLIGHT_FUNCTION(returnvalue, name, arguments) returnvalue (* name ) arguments = NULL
#include "boblight-functions.h"
#undef BOBLIGHT_FUNCTION
#ifdef __cplusplus
#define BOBLIGHT_CAST(value) reinterpret_cast<value>
#else
#define BOBLIGHT_CAST(value) (value)
#endif
//gets a functionpointer from dlsym, and returns char* from dlerror if it didn't work
#define BOBLIGHT_FUNCTION(returnvalue, name, arguments) \
name = BOBLIGHT_CAST(returnvalue (*) arguments)(dlsym(p_boblight, #name)); \
{ char* error = dlerror(); if (error) return error; }
void* p_boblight = NULL; //where we put the lib
//load function pointers
char* boblight_loadlibrary(const char* filename)
{
if (filename == NULL)
filename = "libboblight.so";
if (p_boblight != NULL)
{
dlclose(p_boblight);
p_boblight = NULL;
}
p_boblight = dlopen(filename, RTLD_NOW);
if (p_boblight == NULL)
return dlerror();
//generate dlsym lines
#include "boblight-functions.h"
return NULL;
}
#undef BOBLIGHT_FUNCTION
#undef BOBLIGHT_CAST
//you can define BOBLIGHT_DLOPEN_EXTERN when you load the library in another file
#elif defined(BOBLIGHT_DLOPEN_EXTERN)
extern char* boblight_loadlibrary(const char* filename);
extern void* p_boblight;
#define BOBLIGHT_FUNCTION(returnvalue, name, arguments) extern returnvalue (* name ) arguments
#include "boblight-functions.h"
#undef BOBLIGHT_FUNCTION
#endif //BOBLIGHT_DLOPEN_EXTERN
#endif //LIBBOBLIGHT
|