/rusxmms/librcc

To get this branch, use:
bzr branch http://suren.me/webbzr/rusxmms/librcc

« back to all changes in this revision

Viewing changes to src/plugin.c

  • Committer: Suren A. Chilingaryan
  • Date: 2005-07-13 16:49:47 UTC
  • Revision ID: Arch-1:ds@dside.dyndns.org--darksoft-2004%librcc--main--0.1--patch-12
Engine Plugins

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <stdio.h>
 
2
#include <string.h>
2
3
 
 
4
#include "internal.h"
3
5
#include "plugin.h"
4
6
 
5
7
#ifdef RCC_PLUGINS
13
15
rcc_library_handle rccLibraryOpen(char *filename)
14
16
{
15
17
#ifdef RCC_PLUGINS
16
 
    return (rcc_library_handle)dlopen(filename, RTLD_NOW);
17
 
#else
 
18
    if (filename) return (rcc_library_handle)dlopen(filename, RTLD_NOW);
 
19
#endif /* RCC_PLUGINS */
 
20
 
18
21
    return NULL;
19
 
#endif /* RCC_PLUGINS */
20
22
}
21
23
 
22
24
void rccLibraryClose(rcc_library_handle handle)
23
25
{
24
26
#ifdef RCC_PLUGINS
25
 
    puts("HHHHHHHHHHHHHHHHHHHH");
26
 
    dlclose(handle);
 
27
    if (handle) dlclose(handle);
27
28
#endif /* RCC_PLUGINS */
28
29
}
29
30
 
30
31
void* rccLibraryFind(rcc_library_handle handle, const char *symbol)
31
32
{
32
33
#ifdef RCC_PLUGINS
33
 
    return dlsym(handle, symbol);
34
 
#else
35
 
    return NULL;
 
34
    if ((handle)||(symbol)) return dlsym(handle, symbol);
36
35
#endif /* RCC_PLUGINS */
 
36
 
 
37
    return NULL;
 
38
}
 
39
 
 
40
 
 
41
static rcc_plugin_handle_s rcc_engine_handles[RCC_MAX_PLUGINS];
 
42
 
 
43
int rccPluginInit() {
 
44
    unsigned int i;
 
45
    
 
46
    for (i=0;i<RCC_MAX_PLUGINS;i++)
 
47
        rcc_engine_handles[i].sn = NULL;
 
48
    
 
49
    return 0;
 
50
}
 
51
 
 
52
void rccPluginFree() {
 
53
    unsigned int i;
 
54
    
 
55
    for (i=0;i<RCC_MAX_PLUGINS;i++)
 
56
        if (rcc_engine_handles[i].sn) {
 
57
            rccLibraryClose(rcc_engine_handles[i].handle);
 
58
            free(rcc_engine_handles[i].sn);
 
59
            rcc_engine_handles[i].sn = NULL;
 
60
        }
 
61
}
 
62
 
 
63
 
 
64
rcc_plugin_handle rccPluginGetHandleByName(rcc_plugin_type type, const char *name) {
 
65
    unsigned int i;
 
66
 
 
67
    rcc_plugin_handle handles;
 
68
    
 
69
    if (!name) return NULL;
 
70
    
 
71
    switch(type) {
 
72
        case RCC_PLUGIN_TYPE_ENGINE:
 
73
            handles = rcc_engine_handles;
 
74
        break;
 
75
        default:
 
76
            return NULL;
 
77
    }
 
78
    
 
79
    for (i=0;i<RCC_MAX_PLUGINS;i++)
 
80
        if ((handles[i].sn)&&(!strcasecmp(handles[i].sn, name))) return handles+i;
 
81
    
 
82
    return NULL;
 
83
}
 
84
 
 
85
rcc_plugin_handle rccPluginGetFreeHandle(rcc_plugin_type type) {
 
86
    unsigned int i;
 
87
 
 
88
    rcc_plugin_handle handles;
 
89
    
 
90
    switch(type) {
 
91
        case RCC_PLUGIN_TYPE_ENGINE:
 
92
            handles = rcc_engine_handles;
 
93
        break;
 
94
        default:
 
95
            return NULL;
 
96
    }
 
97
    
 
98
    for (i=0;i<RCC_MAX_PLUGINS;i++)
 
99
        if (!handles[i].sn) return handles+i;
 
100
    
 
101
    return NULL;
 
102
}
 
103
 
 
104
rcc_plugin_handle rccPluginLoad(rcc_plugin_type type, const char *name) {
 
105
    FILE *f;
 
106
    void *res;
 
107
    void *infofunc;
 
108
    char *pluginfn;
 
109
 
 
110
    rcc_plugin_handle plugin;
 
111
    
 
112
    if (!name) return NULL;
 
113
    
 
114
    plugin = rccPluginGetHandleByName(type, name);
 
115
    if (plugin) return plugin;
 
116
    
 
117
    plugin = rccPluginGetFreeHandle(type);
 
118
    if (!plugin) return plugin;
 
119
    
 
120
    switch (type) {
 
121
        case RCC_PLUGIN_TYPE_ENGINE:
 
122
            pluginfn = (char*)malloc((32 + strlen(rcc_home_dir) + strlen(name))*sizeof(char));
 
123
            if (!pluginfn) return NULL;
 
124
        
 
125
            sprintf(pluginfn, "%s/.rcc/engines/lib%s.so", rcc_home_dir, name);
 
126
            res = dlopen(pluginfn, RTLD_NOW);
 
127
            if (!res) {
 
128
                sprintf(pluginfn, LIBRCC_DATA_DIR "/engines/lib%s.so", name);
 
129
                res = dlopen(pluginfn, RTLD_NOW);
 
130
            }
 
131
            free(pluginfn);
 
132
            
 
133
            if (res) {
 
134
                infofunc = rccLibraryFind(res, "rccGetInfo");
 
135
                if (infofunc) {
 
136
                    plugin->sn = strdup(name);
 
137
                    if (plugin->sn) {
 
138
                        plugin->handle = (rcc_library_handle)res;
 
139
                        plugin->info_function = infofunc;
 
140
                        return plugin;
 
141
                    } else rccLibraryClose(res);
 
142
                } else rccLibraryClose(res);
 
143
            }
 
144
        break;
 
145
    }
 
146
    
 
147
    return NULL;
 
148
}
 
149
 
 
150
 
 
151
rcc_engine *rccPluginEngineGetInfo(const char *name, const char *language) {
 
152
    int err;
 
153
    rcc_plugin_handle handle;
 
154
    rcc_plugin_engine_info_function infofunc;
 
155
    
 
156
    handle = rccPluginLoad(RCC_PLUGIN_TYPE_ENGINE, name);
 
157
    if (!handle) return NULL;
 
158
    
 
159
    infofunc = (rcc_plugin_engine_info_function)(handle->info_function);
 
160
    if (!infofunc) return NULL;
 
161
    
 
162
    return infofunc(language);
37
163
}