1 | #ifndef PLUGIN_H |
---|
2 | #define PLUGIN_H |
---|
3 | |
---|
4 | int 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 | |
---|
33 | struct 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 | |
---|
45 | int 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 | |
---|
107 | int 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 | int *pluginversion = NULL; |
---|
121 | void (*initplugin)(void); |
---|
122 | |
---|
123 | count = scandir(dir, &filelist, 0, 0); |
---|
124 | if(count < 0) |
---|
125 | { |
---|
126 | perr("scandir"); |
---|
127 | return 1; |
---|
128 | } |
---|
129 | |
---|
130 | while(count--) |
---|
131 | { |
---|
132 | if(filelist[count]->d_type != DT_DIR && fnmatch("*.so", filelist[count]->d_name, FNM_CASEFOLD) == 0) |
---|
133 | { |
---|
134 | pluginpath = createpath(dir, filelist[count]->d_name); |
---|
135 | pluginhandle = dlopen(pluginpath, RTLD_LOCAL | RTLD_LAZY); |
---|
136 | if(pluginhandle == NULL) |
---|
137 | { |
---|
138 | perr("load plugin %s", pluginpath); |
---|
139 | free(pluginpath); pluginpath = NULL; |
---|
140 | free(filelist[count]); |
---|
141 | continue; |
---|
142 | } |
---|
143 | dlerror(); |
---|
144 | |
---|
145 | //check plugin version |
---|
146 | pluginversion = dlsym(pluginhandle, "pluginversion"); |
---|
147 | if(pluginversion == NULL || *pluginversion != PLUGINVERSION) |
---|
148 | { |
---|
149 | if(pluginversion == NULL) |
---|
150 | { |
---|
151 | err("pluginversion not ok titan=%d plugin=NULL (%s)", PLUGINVERSION, pluginpath); |
---|
152 | } |
---|
153 | else |
---|
154 | { |
---|
155 | err("pluginversion not ok titan=%d plugin=%d (%s)", PLUGINVERSION, *pluginversion, pluginpath); |
---|
156 | } |
---|
157 | dlclose(pluginhandle); |
---|
158 | free(pluginpath); pluginpath = NULL; |
---|
159 | free(filelist[count]); |
---|
160 | continue; |
---|
161 | } |
---|
162 | |
---|
163 | pluginname = dlsym(pluginhandle, "pluginname"); |
---|
164 | if(pluginname == NULL) |
---|
165 | { |
---|
166 | err("not a plugin -> pluginname not found: %s", pluginpath); |
---|
167 | dlclose(pluginhandle); |
---|
168 | free(pluginpath); pluginpath = NULL; |
---|
169 | free(filelist[count]); |
---|
170 | continue; |
---|
171 | } |
---|
172 | free(pluginpath); pluginpath = NULL; |
---|
173 | |
---|
174 | pluginflag = dlsym(pluginhandle, "pluginflag"); |
---|
175 | pluginaktiv = dlsym(pluginhandle, "pluginaktiv"); |
---|
176 | plugindesc = dlsym(pluginhandle, "plugindesc"); |
---|
177 | pluginpic = dlsym(pluginhandle, "pluginpic"); |
---|
178 | pluginflag = dlsym(pluginhandle, "pluginflag"); |
---|
179 | |
---|
180 | if(pluginaktiv != NULL && *pluginaktiv == 0) |
---|
181 | { |
---|
182 | if(pluginflag == NULL || *pluginflag == 0) |
---|
183 | ret = addplugin(pluginname, plugindesc, pluginpic, pluginhandle, 0); |
---|
184 | else |
---|
185 | ret = addplugin(pluginname, plugindesc, pluginpic, pluginhandle, 1); |
---|
186 | if(ret == 0) |
---|
187 | { |
---|
188 | initplugin = dlsym(pluginhandle, "init"); |
---|
189 | if(initplugin != NULL) |
---|
190 | initplugin(); |
---|
191 | |
---|
192 | pluginmenu = dlsym(pluginhandle, "pluginmenu"); |
---|
193 | if(pluginmenu != NULL && *pluginmenu != NULL) |
---|
194 | ((struct skin*)*pluginmenu)->pluginhandle = pluginhandle; |
---|
195 | pluginmenu1 = dlsym(pluginhandle, "pluginmenu1"); |
---|
196 | if(pluginmenu1 != NULL && *pluginmenu1 != NULL) |
---|
197 | ((struct skin*)*pluginmenu1)->pluginhandle = pluginhandle; |
---|
198 | } |
---|
199 | else |
---|
200 | dlclose(pluginhandle); |
---|
201 | } |
---|
202 | else |
---|
203 | dlclose(pluginhandle); |
---|
204 | } |
---|
205 | free(filelist[count]); |
---|
206 | } |
---|
207 | |
---|
208 | free(filelist); |
---|
209 | debug(1000, "out"); |
---|
210 | return 0; |
---|
211 | } |
---|
212 | |
---|
213 | int loadplugin() |
---|
214 | { |
---|
215 | debug(1000, "in"); |
---|
216 | struct dirent **filelist; |
---|
217 | int count = 0; |
---|
218 | char *pluginpath = NULL; |
---|
219 | char *tmpstr = NULL; |
---|
220 | char *tmppath = NULL; |
---|
221 | |
---|
222 | tmppath = getconfig("pluginpath", NULL); |
---|
223 | |
---|
224 | if(tmppath == NULL) |
---|
225 | return 1; |
---|
226 | |
---|
227 | count = scandir(tmppath, &filelist, 0, 0); |
---|
228 | if(count < 0) |
---|
229 | { |
---|
230 | perr("scandir"); |
---|
231 | return 1; |
---|
232 | } |
---|
233 | |
---|
234 | while(count--) |
---|
235 | { |
---|
236 | //check if link is a dir |
---|
237 | if(filelist[count]->d_type == DT_LNK || filelist[count]->d_type == DT_UNKNOWN) |
---|
238 | { |
---|
239 | tmpstr = createpath(tmppath, filelist[count]->d_name); |
---|
240 | if(isdir(tmpstr) == 1) |
---|
241 | filelist[count]->d_type = DT_DIR; |
---|
242 | |
---|
243 | free(tmpstr); tmpstr = NULL; |
---|
244 | } |
---|
245 | |
---|
246 | if(filelist[count]->d_type == DT_DIR && ostrcmp(filelist[count]->d_name, ".") != 0 && ostrcmp(filelist[count]->d_name, "..") != 0) |
---|
247 | { |
---|
248 | pluginpath = createpath(tmppath, filelist[count]->d_name); |
---|
249 | readplugin(pluginpath); |
---|
250 | free(pluginpath); pluginpath = NULL; |
---|
251 | |
---|
252 | } |
---|
253 | free(filelist[count]); |
---|
254 | } |
---|
255 | free(filelist); |
---|
256 | |
---|
257 | return 0; |
---|
258 | debug(1000, "out"); |
---|
259 | } |
---|
260 | |
---|
261 | void screenplugin() |
---|
262 | { |
---|
263 | int rcret = 0; |
---|
264 | void (*startplugin)(void); |
---|
265 | struct skin* plugin = getscreen("plugin"); |
---|
266 | struct skin* listbox = getscreennode(plugin, "listbox"); |
---|
267 | |
---|
268 | drawscreen(plugin, 0, 0); |
---|
269 | addscreenrc(plugin, listbox); |
---|
270 | |
---|
271 | while(1) |
---|
272 | { |
---|
273 | rcret = waitrc(plugin, 0, 0); |
---|
274 | |
---|
275 | if(rcret == getrcconfigint("rcexit", NULL)) break; |
---|
276 | if(rcret == getrcconfigint("rcok", NULL)) |
---|
277 | { |
---|
278 | if(listbox->select != NULL) |
---|
279 | { |
---|
280 | startplugin = dlsym(listbox->select->pluginhandle, "start"); |
---|
281 | if(startplugin != NULL) |
---|
282 | { |
---|
283 | clearscreen(plugin); |
---|
284 | resettvpic(); |
---|
285 | startplugin(); |
---|
286 | drawscreen(plugin, 0, 0); |
---|
287 | } |
---|
288 | } |
---|
289 | continue; |
---|
290 | } |
---|
291 | if(rcret == getrcconfigint("rcred", NULL)) |
---|
292 | { |
---|
293 | if(listbox->select != NULL) |
---|
294 | { |
---|
295 | delplugin(listbox->select->name); |
---|
296 | drawscreen(plugin, 0, 0); |
---|
297 | } |
---|
298 | continue; |
---|
299 | } |
---|
300 | if(rcret == getrcconfigint("rcblue", NULL)) |
---|
301 | { |
---|
302 | loadplugin(); |
---|
303 | drawscreen(plugin, 0, 0); |
---|
304 | continue; |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | delownerrc(plugin); |
---|
309 | clearscreen(plugin); |
---|
310 | } |
---|
311 | |
---|
312 | #endif |
---|