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