Ugly workaround for segfaults when build with x11 and lua
[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  */
21
22 #include "conky.h"
23 #include "logging.h"
24 #include "build.h"
25
26 #ifdef HAVE_SYS_INOTIFY_H
27 #include <sys/inotify.h>
28
29 void llua_append_notify(const char *name);
30 void llua_rm_notifies(void);
31 static int llua_block_notify = 0;
32 #endif /* HAVE_SYS_INOTIFY_H */
33
34 static char *draw_pre = 0;
35 static char *draw_post = 0;
36
37 lua_State *lua_L = NULL;
38
39 static int llua_conky_parse(lua_State *L)
40 {
41         int n = lua_gettop(L);    /* number of arguments */
42         char *str;
43         char *buf = calloc(1, max_user_text);
44         if (n != 1) {
45                 lua_pushstring(L, "incorrect arguments, conky_parse(string) takes exactly 1 argument");
46                 lua_error(L);
47         }
48         if (!lua_isstring(L, 1)) {
49                 lua_pushstring(L, "incorrect argument (expecting a string)");
50                 lua_error(L);
51         }
52         str = strdup(lua_tostring(L, 1));
53         evaluate(str, buf);
54         lua_pushstring(L, buf);
55         free(str);
56         free(buf);
57         return 1;                 /* number of results */
58 }
59
60 void llua_init(void)
61 {
62         const char *libs = PACKAGE_LIBDIR"/lib?.so;";
63         char *old_path, *new_path;
64         if (lua_L) return;
65         lua_L = lua_open();
66
67         /* add our library path to the lua package.cpath global var */
68         luaL_openlibs(lua_L);
69         lua_getglobal(lua_L, "package");
70         lua_getfield(lua_L, -1, "cpath");
71         old_path = strdup(lua_tostring(lua_L, -1));
72         new_path = malloc(strlen(old_path) + strlen(libs) + 1);
73         strcpy(new_path, libs);
74         strcat(new_path, old_path);
75         lua_pushstring(lua_L, new_path);
76         lua_setfield(lua_L, -3, "cpath");
77         lua_pop(lua_L, 2);
78         free(old_path);
79         free(new_path);
80
81         lua_pushstring(lua_L, PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH);
82         lua_setglobal(lua_L, "conky_build_info");
83
84         lua_pushstring(lua_L, VERSION);
85         lua_setglobal(lua_L, "conky_version");
86
87         lua_pushstring(lua_L, current_config);
88         lua_setglobal(lua_L, "conky_config");
89
90         lua_pushcfunction(lua_L, &llua_conky_parse);
91         lua_setglobal(lua_L, "conky_parse");
92 }
93
94 void llua_load(const char *script)
95 {
96         int error;
97         char path[DEFAULT_TEXT_BUFFER_SIZE];
98
99         llua_init();
100
101         to_real_path(path, script);
102         error = luaL_dofile(lua_L, path);
103         if (error) {
104                 ERR("llua_load: %s", lua_tostring(lua_L, -1));
105                 lua_pop(lua_L, 1);
106 #ifdef HAVE_SYS_INOTIFY_H
107         } else if (!llua_block_notify && inotify_fd != -1) {
108                 llua_append_notify(path);
109 #endif /* HAVE_SYS_INOTIFY_H */
110         }
111 }
112
113 /*
114         llua_do_call does a flexible call to any Lua function
115         string: <function> [par1] [par2...]
116         retc: the number of return values expected
117 */
118 char *llua_do_call(const char *string, int retc)
119 {
120         static char func[64];
121         int argc = 0;
122
123         char *tmp = strdup(string);
124         char *ptr = strtok(tmp, " ");
125
126         /* proceed only if the function name is present */
127         if (!ptr) {
128                 free(tmp);
129                 return NULL;
130         }
131
132         /* call only conky_ prefixed functions */
133         snprintf(func, 64, "conky_%s", ptr);
134
135         /* push the function name to stack */
136         lua_getglobal(lua_L, func);
137
138         /* parse all function parameters from args and push them to the stack */
139         ptr = strtok(NULL, " ");
140         while (ptr) {
141                 lua_pushstring(lua_L, ptr);
142                 ptr = strtok(NULL, " ");
143                 argc++;
144         }
145
146         free(tmp);
147
148         if(lua_pcall(lua_L, argc, retc, 0) != 0) {
149                 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
150                 lua_pop(lua_L, -1);
151                 return NULL;
152         }
153
154         return func;
155 }
156
157 /*
158  * same as llua_do_call() except passes everything after func as one arg.
159  */
160 char *llua_do_read_call(const char *function, const char *arg, int retc)
161 {
162         static char func[64];
163         snprintf(func, 64, "conky_%s", function);
164         
165         /* push the function name to stack */
166         lua_getglobal(lua_L, func);
167
168         /* push function parameter to the stack */
169         lua_pushstring(lua_L, arg);
170
171         if (lua_pcall(lua_L, 1, retc, 0) != 0) {
172                 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
173                 lua_pop(lua_L, -1);
174                 return NULL;
175         }
176
177         return func;
178 }
179
180 char *llua_getstring(const char *args)
181 {
182         char *func;
183         char *ret = NULL;
184
185         if(!lua_L) return NULL;
186
187         func = llua_do_call(args, 1);
188         if (func) {
189                 if (!lua_isstring(lua_L, -1)) {
190                         ERR("llua_getstring: function %s didn't return a string, result discarded", func);
191                 } else {
192                         ret = strdup(lua_tostring(lua_L, -1));
193                         lua_pop(lua_L, 1);
194                 }
195         }
196
197         return ret;
198 }
199
200 char *llua_getstring_read(const char *function, const char *arg)
201 {
202         char *func;
203         char *ret = NULL;
204
205         if(!lua_L) return NULL;
206
207         func = llua_do_read_call(function, arg, 1);
208         if (func) {
209                 if(!lua_isstring(lua_L, -1)) {
210                         ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
211                 } else {
212                         ret = strdup(lua_tostring(lua_L, -1));
213                         lua_pop(lua_L, 1);
214                 }
215         }
216
217         return ret;
218 }
219
220 int llua_getnumber(const char *args, double *ret)
221 {
222         char *func;
223
224         if(!lua_L) return 0;
225
226         func = llua_do_call(args, 1);
227         if(func) {
228                 if(!lua_isnumber(lua_L, -1)) {
229                         ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
230                 } else {
231                         *ret = lua_tonumber(lua_L, -1);
232                         lua_pop(lua_L, 1);
233                         return 1;
234                 }
235         }
236         return 0;
237 }
238
239 void llua_close(void)
240 {
241 #ifdef HAVE_SYS_INOTIFY_H
242         llua_rm_notifies();
243 #endif /* HAVE_SYS_INOTIFY_H */
244         if (draw_pre) {
245                 free(draw_pre);
246                 draw_pre = 0;
247         }
248         if (draw_post) {
249                 free(draw_post);
250                 draw_post = 0;
251         }
252         if(!lua_L) return;
253         lua_close(lua_L);
254         lua_L = NULL;
255 }
256
257 #ifdef HAVE_SYS_INOTIFY_H
258 struct _lua_notify_s {
259         int wd;
260         char name[DEFAULT_TEXT_BUFFER_SIZE];
261         struct _lua_notify_s *next;
262 };
263 static struct _lua_notify_s *lua_notifies = 0;
264
265 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
266 {
267         struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
268         memset(ret, 0, sizeof(struct _lua_notify_s));
269         strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
270         return ret;
271 }
272
273 void llua_append_notify(const char *name)
274 {
275         /* do it */
276         struct _lua_notify_s *new_tail = 0;
277         if (!lua_notifies) {
278                 /* empty, fresh new digs */
279                 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
280         } else {
281                 struct _lua_notify_s *tail = lua_notifies;
282                 while (tail->next) {
283                         tail = tail->next;
284                 }
285                 // should be @ the end now
286                 new_tail = llua_notify_list_do_alloc(name);
287                 tail->next = new_tail;
288         }
289         new_tail->wd = inotify_add_watch(inotify_fd,
290                         new_tail->name,
291                         IN_MODIFY);
292 }
293
294 void llua_rm_notifies(void)
295 {
296         /* git 'er done */
297         struct _lua_notify_s *head = lua_notifies;
298         struct _lua_notify_s *next = 0;
299         if (!lua_notifies) return;
300         inotify_rm_watch(inotify_fd, head->wd);
301         if (head->next) next = head->next;
302         free(head);
303         while (next) {
304                 head = next;
305                 next = head->next;
306                 inotify_rm_watch(inotify_fd, head->wd);
307                 free(head);
308         }
309         lua_notifies = 0;
310 }
311
312 void llua_inotify_query(int wd, int mask)
313 {
314         struct _lua_notify_s *head = lua_notifies;
315         if (mask & IN_MODIFY || mask & IN_IGNORED) {
316                 /* for whatever reason, i keep getting IN_IGNORED when the file is
317                  * modified */
318                 while (head) {
319                         if (head->wd == wd) {
320                                 llua_block_notify = 1;
321                                 llua_load(head->name);
322                                 llua_block_notify = 0;
323                                 ERR("Lua script '%s' reloaded", head->name);
324                                 if (mask & IN_IGNORED) {
325                                         /* for some reason we get IN_IGNORED here
326                                          * sometimes, so we need to re-add the watch */
327                                         head->wd = inotify_add_watch(inotify_fd,
328                                                         head->name,
329                                                         IN_MODIFY);
330                                 }
331                                 return;
332                         }
333                         head = head->next;
334                 }
335         }
336 }
337 #endif /* HAVE_SYS_INOTIFY_H */
338
339 #ifdef X11
340 void llua_draw_pre_hook(void)
341 {
342         if (!lua_L || !draw_pre) return;
343         llua_do_call(draw_pre, 0);
344 }
345
346 void llua_draw_post_hook(void)
347 {
348         if (!lua_L || !draw_post) return;
349         llua_do_call(draw_post, 0);
350 }
351
352 void llua_set_draw_pre_hook(const char *args)
353 {
354         draw_pre = strdup(args);
355 }
356
357 void llua_set_draw_post_hook(const char *args)
358 {
359         draw_post = strdup(args);
360 }
361
362 void llua_set_long(const char *key, long value)
363 {
364         lua_pushnumber(lua_L, value);
365         lua_setfield(lua_L, -2, key);
366 }
367
368 /* this function mostly copied from tolua++ source so that we could play nice
369  * with tolua++ libs.  tolua++ is provided 'as is'
370  */
371 void llua_set_userdata(const char *key, const char *type, void *value)
372 {
373         if (value == NULL) {
374                 lua_pushnil(lua_L);
375         } else {
376                 luaL_getmetatable(lua_L, type);
377                 lua_pushstring(lua_L,"tolua_ubox");
378                 lua_rawget(lua_L,-2);        /* stack: mt ubox */
379                 if (lua_isnil(lua_L, -1)) {
380                         lua_pop(lua_L, 1);
381                         lua_pushstring(lua_L, "tolua_ubox");
382                         lua_rawget(lua_L, LUA_REGISTRYINDEX);
383                 }
384                 lua_pushlightuserdata(lua_L,value);
385                 lua_rawget(lua_L,-2);                       /* stack: mt ubox ubox[u] */
386                 if (lua_isnil(lua_L,-1)) {
387                         lua_pop(lua_L,1);                          /* stack: mt ubox */
388                         lua_pushlightuserdata(lua_L,value);
389                         *(void**)lua_newuserdata(lua_L,sizeof(void *)) = value;   /* stack: mt ubox u newud */
390                         lua_pushvalue(lua_L,-1);                   /* stack: mt ubox u newud newud */
391                         lua_insert(lua_L,-4);                      /* stack: mt newud ubox u newud */
392                         lua_rawset(lua_L,-3);                      /* stack: mt newud ubox */
393                         lua_pop(lua_L,1);                          /* stack: mt newud */
394                         /*luaL_getmetatable(lua_L,type);*/
395                         lua_pushvalue(lua_L, -2);                       /* stack: mt newud mt */
396                         lua_setmetatable(lua_L,-2);                     /* stack: mt newud */
397
398                 } else {
399                         /* check the need of updating the metatable to a more specialized class */
400                         lua_insert(lua_L,-2);                       /* stack: mt ubox[u] ubox */
401                         lua_pop(lua_L,1);                           /* stack: mt ubox[u] */
402                         lua_pushstring(lua_L,"tolua_super");
403                         lua_rawget(lua_L,LUA_REGISTRYINDEX);        /* stack: mt ubox[u] super */
404                         lua_getmetatable(lua_L,-2);                 /* stack: mt ubox[u] super mt */
405                         lua_rawget(lua_L,-2);                       /* stack: mt ubox[u] super super[mt] */
406                         if (lua_istable(lua_L,-1)) {
407                                 lua_pushstring(lua_L,type);                 /* stack: mt ubox[u] super super[mt] type */
408                                 lua_rawget(lua_L,-2);                       /* stack: mt ubox[u] super super[mt] flag */
409                                 if (lua_toboolean(lua_L,-1) == 1) {
410                                         /* if true */
411                                         lua_pop(lua_L,3);       /* mt ubox[u]*/
412                                         lua_remove(lua_L, -2);
413                                         return;
414                                 }
415                         }
416                         /* type represents a more specilized type */
417                         /*luaL_getmetatable(lua_L,type);             // stack: mt ubox[u] super super[mt] flag mt */
418                         lua_pushvalue(lua_L, -5);                                       /* stack: mt ubox[u] super super[mt] flag mt */
419                         lua_setmetatable(lua_L,-5);                /* stack: mt ubox[u] super super[mt] flag */
420                         lua_pop(lua_L,3);                          /* stack: mt ubox[u] */
421                 }
422                 lua_remove(lua_L, -2);  /* stack: ubox[u]*/
423         }
424         lua_setfield(lua_L, -2, key);
425 }
426
427 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
428 {
429         lua_newtable(lua_L);
430         
431 /* TODO fix this: (segfaults)
432         llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
433         llua_set_userdata("visual", "Visual", window.visual);
434         llua_set_userdata("display", "Display", display);
435 */
436
437         llua_set_long("width", window.width);
438         llua_set_long("height", window.height);
439         llua_set_long("border_inner_margin", window.border_inner_margin);
440         llua_set_long("border_outer_margin", window.border_outer_margin);
441         llua_set_long("border_width", window.border_width);
442
443         llua_set_long("text_start_x", text_start_x);
444         llua_set_long("text_start_y", text_start_y);
445         llua_set_long("text_width", text_width);
446         llua_set_long("text_height", text_height);
447
448         lua_setglobal(lua_L, "conky_window");
449 }
450
451 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
452 {
453         llua_init();    //needed because sometimes lua isn't initialised resulting in segfaults
454         lua_getglobal(lua_L, "conky_window");
455         if (lua_isnil(lua_L, -1)) {
456                 /* window table isn't populated yet */
457                 return;
458         }
459
460         llua_set_long("width", window.width);
461         llua_set_long("height", window.height);
462
463         llua_set_long("text_start_x", text_start_x);
464         llua_set_long("text_start_y", text_start_y);
465         llua_set_long("text_width", text_width);
466         llua_set_long("text_height", text_height);
467
468         lua_setglobal(lua_L, "conky_window");
469 }
470 #endif /* X11 */
471