442608cd652309b1798358c85c47748125e1672c
[monky] / src / llua.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Copyright (c) 2009 Toni Spets
4  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
5  *      (see AUTHORS)
6  * All rights reserved.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * vim: ts=4 sw=4 noet ai cindent syntax=c
21  *
22  */
23
24 #include "conky.h"
25 #include "logging.h"
26 #include "build.h"
27
28 #ifdef LUA_EXTRAS
29 #include <tolua++.h>
30 #endif /* LUA_EXTRAS */
31
32 #ifdef HAVE_SYS_INOTIFY_H
33 #include <sys/inotify.h>
34
35 void llua_append_notify(const char *name);
36 void llua_rm_notifies(void);
37 static int llua_block_notify = 0;
38 #endif /* HAVE_SYS_INOTIFY_H */
39
40 static char *draw_pre = 0;
41 static char *draw_post = 0;
42
43 lua_State *lua_L = NULL;
44
45 static int llua_conky_parse(lua_State *L)
46 {
47         int n = lua_gettop(L);    /* number of arguments */
48         char *str;
49         char *buf = calloc(1, max_user_text);
50         if (n != 1) {
51                 lua_pushstring(L, "incorrect arguments, conky_parse(string) takes exactly 1 argument");
52                 lua_error(L);
53         }
54         if (!lua_isstring(L, 1)) {
55                 lua_pushstring(L, "incorrect argument (expecting a string)");
56                 lua_error(L);
57         }
58         str = strdup(lua_tostring(L, 1));
59         evaluate(str, buf);
60         lua_pushstring(L, buf);
61         free(str);
62         free(buf);
63         return 1;                 /* number of results */
64 }
65
66 void llua_init(void)
67 {
68         const char *libs = PACKAGE_LIBDIR"/lib?.so;";
69         char *old_path, *new_path;
70         if (lua_L) return;
71         lua_L = lua_open();
72
73         /* add our library path to the lua package.cpath global var */
74         luaL_openlibs(lua_L);
75         lua_getglobal(lua_L, "package");
76         lua_getfield(lua_L, -1, "cpath");
77         old_path = strdup(lua_tostring(lua_L, -1));
78         new_path = malloc(strlen(old_path) + strlen(libs) + 1);
79         strcpy(new_path, libs);
80         strcat(new_path, old_path);
81         lua_pushstring(lua_L, new_path);
82         lua_setfield(lua_L, -3, "cpath");
83         lua_pop(lua_L, 2);
84         free(old_path);
85         free(new_path);
86
87         lua_pushstring(lua_L, PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH);
88         lua_setglobal(lua_L, "conky_build_info");
89
90         lua_pushstring(lua_L, VERSION);
91         lua_setglobal(lua_L, "conky_version");
92
93         lua_pushstring(lua_L, BUILD_DATE);
94         lua_setglobal(lua_L, "conky_build_date");
95
96         lua_pushstring(lua_L, BUILD_ARCH);
97         lua_setglobal(lua_L, "conky_build_arch");
98
99         lua_pushstring(lua_L, current_config);
100         lua_setglobal(lua_L, "conky_config");
101
102         lua_pushcfunction(lua_L, &llua_conky_parse);
103         lua_setglobal(lua_L, "conky_parse");
104
105 #if defined(X11) && defined(LUA_EXTRAS)
106         /* register tolua++ user types */
107         tolua_open(lua_L);
108         tolua_usertype(lua_L, "Drawable");
109         tolua_usertype(lua_L, "Visual");
110         tolua_usertype(lua_L, "Display");
111 #endif /* X11 */
112 }
113
114 void llua_load(const char *script)
115 {
116         int error;
117         char path[DEFAULT_TEXT_BUFFER_SIZE];
118
119         llua_init();
120
121         to_real_path(path, script);
122         error = luaL_dofile(lua_L, path);
123         if (error) {
124                 ERR("llua_load: %s", lua_tostring(lua_L, -1));
125                 lua_pop(lua_L, 1);
126 #ifdef HAVE_SYS_INOTIFY_H
127         } else if (!llua_block_notify && inotify_fd != -1) {
128                 llua_append_notify(path);
129 #endif /* HAVE_SYS_INOTIFY_H */
130         }
131 }
132
133 /*
134    llua_do_call does a flexible call to any Lua function
135 string: <function> [par1] [par2...]
136 retc: the number of return values expected
137  */
138 char *llua_do_call(const char *string, int retc)
139 {
140         static char func[64];
141         int argc = 0;
142
143         char *tmp = strdup(string);
144         char *ptr = strtok(tmp, " ");
145
146         /* proceed only if the function name is present */
147         if (!ptr) {
148                 free(tmp);
149                 return NULL;
150         }
151
152         /* call only conky_ prefixed functions */
153         if(strncmp(ptr, LUAPREFIX, strlen(LUAPREFIX)) == 0) {
154                 snprintf(func, 64, "%s", ptr);
155         }else{
156                 snprintf(func, 64, "%s%s", LUAPREFIX, ptr);
157         }
158
159         /* push the function name to stack */
160         lua_getglobal(lua_L, func);
161
162         /* parse all function parameters from args and push them to the stack */
163         ptr = strtok(NULL, " ");
164         while (ptr) {
165                 lua_pushstring(lua_L, ptr);
166                 ptr = strtok(NULL, " ");
167                 argc++;
168         }
169
170         free(tmp);
171
172         if(lua_pcall(lua_L, argc, retc, 0) != 0) {
173                 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
174                 lua_pop(lua_L, -1);
175                 return NULL;
176         }
177
178         return func;
179 }
180
181 /*
182  * same as llua_do_call() except passes everything after func as one arg.
183  */
184 char *llua_do_read_call(const char *function, const char *arg, int retc)
185 {
186         static char func[64];
187         snprintf(func, 64, "conky_%s", function);
188
189         /* push the function name to stack */
190         lua_getglobal(lua_L, func);
191
192         /* push function parameter to the stack */
193         lua_pushstring(lua_L, arg);
194
195         if (lua_pcall(lua_L, 1, retc, 0) != 0) {
196                 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
197                 lua_pop(lua_L, -1);
198                 return NULL;
199         }
200
201         return func;
202 }
203
204 char *llua_getstring(const char *args)
205 {
206         char *func;
207         char *ret = NULL;
208
209         if(!lua_L) return NULL;
210
211         func = llua_do_call(args, 1);
212         if (func) {
213                 if (!lua_isstring(lua_L, -1)) {
214                         ERR("llua_getstring: function %s didn't return a string, result discarded", func);
215                 } else {
216                         ret = strdup(lua_tostring(lua_L, -1));
217                         lua_pop(lua_L, 1);
218                 }
219         }
220
221         return ret;
222 }
223
224 char *llua_getstring_read(const char *function, const char *arg)
225 {
226         char *func;
227         char *ret = NULL;
228
229         if(!lua_L) return NULL;
230
231         func = llua_do_read_call(function, arg, 1);
232         if (func) {
233                 if(!lua_isstring(lua_L, -1)) {
234                         ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
235                 } else {
236                         ret = strdup(lua_tostring(lua_L, -1));
237                         lua_pop(lua_L, 1);
238                 }
239         }
240
241         return ret;
242 }
243
244 int llua_getnumber(const char *args, double *ret)
245 {
246         char *func;
247
248         if(!lua_L) return 0;
249
250         func = llua_do_call(args, 1);
251         if(func) {
252                 if(!lua_isnumber(lua_L, -1)) {
253                         ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
254                 } else {
255                         *ret = lua_tonumber(lua_L, -1);
256                         lua_pop(lua_L, 1);
257                         return 1;
258                 }
259         }
260         return 0;
261 }
262
263 void llua_close(void)
264 {
265 #ifdef HAVE_SYS_INOTIFY_H
266         llua_rm_notifies();
267 #endif /* HAVE_SYS_INOTIFY_H */
268         if (draw_pre) {
269                 free(draw_pre);
270                 draw_pre = 0;
271         }
272         if (draw_post) {
273                 free(draw_post);
274                 draw_post = 0;
275         }
276         if(!lua_L) return;
277         lua_close(lua_L);
278         lua_L = NULL;
279 }
280
281 #ifdef HAVE_SYS_INOTIFY_H
282 struct _lua_notify_s {
283         int wd;
284         char name[DEFAULT_TEXT_BUFFER_SIZE];
285         struct _lua_notify_s *next;
286 };
287 static struct _lua_notify_s *lua_notifies = 0;
288
289 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
290 {
291         struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
292         memset(ret, 0, sizeof(struct _lua_notify_s));
293         strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
294         return ret;
295 }
296
297 void llua_append_notify(const char *name)
298 {
299         /* do it */
300         struct _lua_notify_s *new_tail = 0;
301         if (!lua_notifies) {
302                 /* empty, fresh new digs */
303                 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
304         } else {
305                 struct _lua_notify_s *tail = lua_notifies;
306                 while (tail->next) {
307                         tail = tail->next;
308                 }
309                 // should be @ the end now
310                 new_tail = llua_notify_list_do_alloc(name);
311                 tail->next = new_tail;
312         }
313         new_tail->wd = inotify_add_watch(inotify_fd,
314                         new_tail->name,
315                         IN_MODIFY);
316 }
317
318 void llua_rm_notifies(void)
319 {
320         /* git 'er done */
321         struct _lua_notify_s *head = lua_notifies;
322         struct _lua_notify_s *next = 0;
323         if (!lua_notifies) return;
324         inotify_rm_watch(inotify_fd, head->wd);
325         if (head->next) next = head->next;
326         free(head);
327         while (next) {
328                 head = next;
329                 next = head->next;
330                 inotify_rm_watch(inotify_fd, head->wd);
331                 free(head);
332         }
333         lua_notifies = 0;
334 }
335
336 void llua_inotify_query(int wd, int mask)
337 {
338         struct _lua_notify_s *head = lua_notifies;
339         if (mask & IN_MODIFY || mask & IN_IGNORED) {
340                 /* for whatever reason, i keep getting IN_IGNORED when the file is
341                  * modified */
342                 while (head) {
343                         if (head->wd == wd) {
344                                 llua_block_notify = 1;
345                                 llua_load(head->name);
346                                 llua_block_notify = 0;
347                                 ERR("Lua script '%s' reloaded", head->name);
348                                 if (mask & IN_IGNORED) {
349                                         /* for some reason we get IN_IGNORED here
350                                          * sometimes, so we need to re-add the watch */
351                                         head->wd = inotify_add_watch(inotify_fd,
352                                                         head->name,
353                                                         IN_MODIFY);
354                                 }
355                                 return;
356                         }
357                         head = head->next;
358                 }
359         }
360 }
361 #endif /* HAVE_SYS_INOTIFY_H */
362
363 #ifdef X11
364 void llua_draw_pre_hook(void)
365 {
366         if (!lua_L || !draw_pre) return;
367         llua_do_call(draw_pre, 0);
368 }
369
370 void llua_draw_post_hook(void)
371 {
372         if (!lua_L || !draw_post) return;
373         llua_do_call(draw_post, 0);
374 }
375
376 void llua_set_draw_pre_hook(const char *args)
377 {
378         draw_pre = strdup(args);
379 }
380
381 void llua_set_draw_post_hook(const char *args)
382 {
383         draw_post = strdup(args);
384 }
385
386 void llua_set_long(const char *key, long value)
387 {
388         lua_pushnumber(lua_L, value);
389         lua_setfield(lua_L, -2, key);
390 }
391
392 #ifdef LUA_EXTRAS
393 void llua_set_userdata(const char *key, const char *type, void *value)
394 {
395         tolua_pushusertype(lua_L, value, type);
396         lua_setfield(lua_L, -2, key);
397 }
398 #endif /* LUA_EXTRAS */
399
400 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
401 {
402         if (!lua_L) return;
403         lua_newtable(lua_L);
404
405         if (output_methods & TO_X) {
406 #ifdef LUA_EXTRAS
407                 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
408                 llua_set_userdata("visual", "Visual", window.visual);
409                 llua_set_userdata("display", "Display", display);
410 #endif /* LUA_EXTRAS */
411
412
413                 llua_set_long("width", window.width);
414                 llua_set_long("height", window.height);
415                 llua_set_long("border_inner_margin", window.border_inner_margin);
416                 llua_set_long("border_outer_margin", window.border_outer_margin);
417                 llua_set_long("border_width", window.border_width);
418
419                 llua_set_long("text_start_x", text_start_x);
420                 llua_set_long("text_start_y", text_start_y);
421                 llua_set_long("text_width", text_width);
422                 llua_set_long("text_height", text_height);
423
424                 lua_setglobal(lua_L, "conky_window");
425         }
426 }
427
428 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
429 {
430         if (!lua_L) return;
431
432         lua_getglobal(lua_L, "conky_window");
433         if (lua_isnil(lua_L, -1)) {
434                 /* window table isn't populated yet */
435                 lua_pop(lua_L, 1);
436                 return;
437         }
438
439         llua_set_long("width", window.width);
440         llua_set_long("height", window.height);
441
442         llua_set_long("text_start_x", text_start_x);
443         llua_set_long("text_start_y", text_start_y);
444         llua_set_long("text_width", text_width);
445         llua_set_long("text_height", text_height);
446
447         lua_setglobal(lua_L, "conky_window");
448 }
449 #endif /* X11 */
450