source: titan/titan/plugin.h @ 15272

Last change on this file since 15272 was 12889, checked in by nit, 12 years ago

[titan] make plugins linkable

File size: 6.6 KB
Line 
1#ifndef PLUGIN_H
2#define PLUGIN_H
3
4int delplugin(char *pluginname)
5{
6        debug(1000, "in");
7        struct skin* plugin = getscreen("plugin");
8        struct skin* listbox = getscreennode(plugin, "listbox");
9        struct skin* child = getscreennode(plugin, pluginname);
10        void (*deinitplugin)(void);
11
12        if(plugin == NULL || child == NULL || listbox == NULL)
13        {
14                debug(1000, "out -> NULL detect");
15                return 1;
16        }
17
18        deinitplugin = dlsym(child->pluginhandle, "deinit");
19        if(deinitplugin != NULL)
20                deinitplugin();
21
22        dlclose(child->pluginhandle);
23        delscreennode(plugin, pluginname);
24
25        listbox->aktpage = 0;
26        listbox->aktline = 0;
27        listbox->select = NULL;
28
29        debug(1000, "out");
30        return 0;
31}
32
33struct skin* getplugin(char* pluginname)
34{
35        debug(1000, "in");
36        struct skin* plugin = getscreen("plugin");
37        struct skin* pluginnode = getscreennode(plugin, pluginname);
38
39        if(pluginnode != status.skinerr)
40                return pluginnode;
41        else
42                return NULL;
43}
44
45int addplugin(char *pluginname, char* plugindesc, char* pluginpic, char* pluginhandle, int hidden)
46{
47        debug(1000, "in");
48        struct skin* plugin = getscreen("plugin");
49        struct skin* listbox = getscreennode(plugin, "listbox");
50        struct skin* child = NULL;
51        char* tmpstr = NULL;
52        char* tmppng = NULL;
53
54        child = checkscreennode(plugin, pluginname);
55        if(child != status.skinerr)
56        {
57                err("plugin %s exists, don't load it", pluginname);
58                return 1;
59        }
60        child = NULL;
61
62        child = addlistbox(plugin, listbox, child, 0);
63        if(child != NULL)
64        {
65                char* defaultdir = NULL;
66                defaultdir = ostrcat(getconfig("skinpath", NULL), "/skin/", 0, 0);
67
68                tmppng = ostrcat("", pluginname, 0, 0);
69                stringreplacechar(tmppng, ' ', '_');
70                stringreplacechar(tmppng, ':', '_');
71                string_tolower(tmppng);
72
73                defaultdir = ostrcat(defaultdir, tmppng, 1, 0);
74                defaultdir = ostrcat(defaultdir, ".png", 1, 0);
75                if(file_exist(defaultdir))
76                        changepic(child, defaultdir);
77                else if(pluginpic != NULL)
78                        changepic(child, pluginpic);
79                else
80                {
81                        tmpstr = ostrcat(getconfig("skinpath", NULL), "/skin/plugin.png", 0, 0);
82                        changepic(child, tmpstr);
83                        free(tmpstr); tmpstr = NULL;
84                }
85                free(tmppng); tmppng = NULL;
86                free(defaultdir), defaultdir = NULL;
87                child->valign = MIDDLE;
88//              child->height = listbox->fontsize + 30;
89                child->hspace = 5;
90                child->height = 50;
91                if(hidden) child->hidden = YES;
92                if(pluginname != NULL)
93                {
94                        changename(child, pluginname);
95                        changetext(child, _(pluginname));
96                }
97//              child->textposx = listbox->textposx;
98                child->textposx = 120;
99                child->pluginhandle = pluginhandle;
100                child->del = PLUGINDELMARK;
101        }
102
103        debug(1000, "out");
104        return 0;
105}
106
107int readplugin(char *dir)
108{
109        debug(1000, "in");
110        struct dirent **filelist;
111        void *pluginhandle = NULL;
112        int count = 0, ret = 0;
113        char *pluginpath = NULL;
114        char *pluginname = NULL;
115        char *plugindesc = NULL;
116        char *pluginpic = NULL;
117        void **pluginmenu = NULL, **pluginmenu1 = NULL;
118        int *pluginflag = NULL;
119        int *pluginaktiv = NULL;
120        void (*initplugin)(void);
121
122        count = scandir(dir, &filelist, 0, 0);
123        if(count < 0)
124        {
125                perr("scandir");
126                return 1;
127        }
128
129        while(count--)
130        {
131                if(filelist[count]->d_type != DT_DIR && fnmatch("*.so", filelist[count]->d_name, FNM_CASEFOLD) == 0)
132                {
133                        pluginpath = createpath(dir, filelist[count]->d_name);
134                        pluginhandle = dlopen(pluginpath, RTLD_LOCAL | RTLD_LAZY);
135                        if(pluginhandle == NULL)
136                        {
137                                perr("load plugin %s", pluginpath);
138                                free(pluginpath); pluginpath = NULL;
139                                free(filelist[count]);
140                                continue;
141                        }
142                        dlerror();
143
144                        pluginname = dlsym(pluginhandle, "pluginname");
145                        if(pluginname == NULL)
146                        {
147                                err("not a plugin -> pluginname not found: %s", pluginpath);
148                                free(pluginpath); pluginpath = NULL;
149                                free(filelist[count]);
150                                continue;
151                        }
152                        free(pluginpath); pluginpath = NULL;
153
154                        pluginflag = dlsym(pluginhandle, "pluginflag");
155                        pluginaktiv = dlsym(pluginhandle, "pluginaktiv");
156                        plugindesc = dlsym(pluginhandle, "plugindesc");
157                        pluginpic = dlsym(pluginhandle, "pluginpic");
158                        pluginflag = dlsym(pluginhandle, "pluginflag");
159
160                        if(pluginaktiv != NULL && *pluginaktiv == 0)
161                        {
162                                if(pluginflag == NULL || *pluginflag == 0)
163                                        ret = addplugin(pluginname, plugindesc, pluginpic, pluginhandle, 0);
164                                else
165                                        ret = addplugin(pluginname, plugindesc, pluginpic, pluginhandle, 1);
166                                if(ret == 0)
167                                {
168                                        initplugin = dlsym(pluginhandle, "init");
169                                        if(initplugin != NULL)
170                                                initplugin();
171
172                                        pluginmenu = dlsym(pluginhandle, "pluginmenu");
173                                        if(pluginmenu != NULL)
174                                                ((struct skin*)*pluginmenu)->pluginhandle = pluginhandle;
175                                        pluginmenu1 = dlsym(pluginhandle, "pluginmenu1");
176                                        if(pluginmenu1 != NULL)
177                                                ((struct skin*)*pluginmenu1)->pluginhandle = pluginhandle;
178                                }
179                                else
180                                        dlclose(pluginhandle);
181                        }
182                        else
183                                dlclose(pluginhandle);
184                }
185                free(filelist[count]);
186        }
187
188        free(filelist);
189        debug(1000, "out");
190        return 0;
191}
192
193int loadplugin()
194{
195        debug(1000, "in");
196        struct dirent **filelist;
197        int count = 0;
198        char *pluginpath = NULL;
199        char *tmpstr = NULL;
200        char *tmppath = NULL;
201
202        tmppath = getconfig("pluginpath", NULL);
203
204        if(tmppath == NULL)
205                return 1;
206
207        count = scandir(tmppath, &filelist, 0, 0);
208        if(count < 0)
209        {
210                perr("scandir");
211                return 1;
212        }
213
214        while(count--)
215        {
216                //check if link is a dir
217                if(filelist[count]->d_type == DT_LNK || filelist[count]->d_type == DT_UNKNOWN)
218                {
219                        tmpstr = createpath(tmppath, filelist[count]->d_name);
220                        if(isdir(tmpstr) == 1)
221                                filelist[count]->d_type = DT_DIR;
222
223                        free(tmpstr); tmpstr = NULL;
224                }
225
226                if(filelist[count]->d_type == DT_DIR && ostrcmp(filelist[count]->d_name, ".") != 0 && ostrcmp(filelist[count]->d_name, "..") != 0)
227                {
228                        pluginpath = createpath(tmppath, filelist[count]->d_name);
229                        readplugin(pluginpath);
230                        free(pluginpath); pluginpath = NULL;
231
232                }
233                free(filelist[count]);
234        }
235        free(filelist);
236
237        return 0;
238        debug(1000, "out");
239}
240
241void screenplugin()
242{
243        int rcret = 0;
244        void (*startplugin)(void);
245        struct skin* plugin = getscreen("plugin");
246        struct skin* listbox = getscreennode(plugin, "listbox");
247
248        drawscreen(plugin, 0);
249        addscreenrc(plugin, listbox);
250
251        while(1)
252        {
253                rcret = waitrc(plugin, 0, 0);
254
255                if(rcret == getrcconfigint("rcexit", NULL)) break;
256                if(rcret == getrcconfigint("rcok", NULL))
257                {
258                        if(listbox->select != NULL)
259                        {
260                                startplugin = dlsym(listbox->select->pluginhandle, "start");
261                                if(startplugin != NULL)
262                                {
263                                        clearscreen(plugin);
264                                        startplugin();
265                                        drawscreen(plugin, 0);
266                                }
267                        }
268                        continue;
269                }
270                if(rcret == getrcconfigint("rcred", NULL))
271                {
272                        if(listbox->select != NULL)
273                        {
274                                delplugin(listbox->select->name);
275                                drawscreen(plugin, 0);
276                        }
277                        continue;
278                }
279                if(rcret == getrcconfigint("rcblue", NULL))
280                {
281                        loadplugin();
282                        drawscreen(plugin, 0);
283                        continue;
284                }
285        }
286
287        delownerrc(plugin);
288        clearscreen(plugin);
289}
290
291#endif
Note: See TracBrowser for help on using the repository browser.