652bfa0eedb7a962d75e88e93dbfbe86187d9a41
[monky] / src / llua.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Copyright (c) 2009 Toni Spets
7  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
8  *      (see AUTHORS)
9  * All rights reserved.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "conky.h"
26 #include "logging.h"
27 #include "build.h"
28
29 #ifdef LUA_EXTRAS
30 #include <tolua++.h>
31 #endif /* LUA_EXTRAS */
32
33 #ifdef HAVE_SYS_INOTIFY_H
34 #include <sys/inotify.h>
35
36 void llua_append_notify(const char *name);
37 void llua_rm_notifies(void);
38 static int llua_block_notify = 0;
39 #endif /* HAVE_SYS_INOTIFY_H */
40
41 static char *draw_pre_hook = 0;
42 static char *draw_post_hook = 0;
43 static char *startup_hook = 0;
44 static char *shutdown_hook = 0;
45
46 lua_State *lua_L = NULL;
47
48 static int llua_conky_parse(lua_State *L)
49 {
50         int n = lua_gettop(L);    /* number of arguments */
51         char *str;
52         char *buf = calloc(1, max_user_text);
53         if (n != 1) {
54                 lua_pushstring(L, "incorrect arguments, conky_parse(string) takes exactly 1 argument");
55                 lua_error(L);
56         }
57         if (!lua_isstring(L, 1)) {
58                 lua_pushstring(L, "incorrect argument (expecting a string)");
59                 lua_error(L);
60         }
61         str = strdup(lua_tostring(L, 1));
62         evaluate(str, buf, max_user_text);
63         lua_pushstring(L, buf);
64         free(str);
65         free(buf);
66         return 1;                 /* number of results */
67 }
68
69 static int llua_conky_set_update_interval(lua_State *L)
70 {
71         int n = lua_gettop(L);    /* number of arguments */
72         double value;
73         if (n != 1) {
74                 lua_pushstring(L, "incorrect arguments, conky_set_update_interval(number) takes exactly 1 argument");
75                 lua_error(L);
76         }
77         if (!lua_isnumber(L, 1)) {
78                 lua_pushstring(L, "incorrect argument (expecting a number)");
79                 lua_error(L);
80         }
81         value = lua_tonumber(L, 1);
82         set_update_interval(value);
83         return 0;                 /* number of results */
84 }
85
86 void llua_init(void)
87 {
88         const char *libs = PACKAGE_LIBDIR"/lib?.so;";
89         char *old_path, *new_path;
90         if (lua_L) return;
91         lua_L = lua_open();
92
93         /* add our library path to the lua package.cpath global var */
94         luaL_openlibs(lua_L);
95         lua_getglobal(lua_L, "package");
96         lua_getfield(lua_L, -1, "cpath");
97         old_path = strdup(lua_tostring(lua_L, -1));
98         new_path = malloc(strlen(old_path) + strlen(libs) + 1);
99         strcpy(new_path, libs);
100         strcat(new_path, old_path);
101         lua_pushstring(lua_L, new_path);
102         lua_setfield(lua_L, -3, "cpath");
103         lua_pop(lua_L, 2);
104         free(old_path);
105         free(new_path);
106
107         lua_pushstring(lua_L, PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH);
108         lua_setglobal(lua_L, "conky_build_info");
109
110         lua_pushstring(lua_L, VERSION);
111         lua_setglobal(lua_L, "conky_version");
112
113         lua_pushstring(lua_L, BUILD_DATE);
114         lua_setglobal(lua_L, "conky_build_date");
115
116         lua_pushstring(lua_L, BUILD_ARCH);
117         lua_setglobal(lua_L, "conky_build_arch");
118
119         lua_pushstring(lua_L, current_config);
120         lua_setglobal(lua_L, "conky_config");
121
122         lua_pushcfunction(lua_L, &llua_conky_parse);
123         lua_setglobal(lua_L, "conky_parse");
124
125         lua_pushcfunction(lua_L, &llua_conky_set_update_interval);
126         lua_setglobal(lua_L, "conky_set_update_interval");
127
128 #if defined(X11) && defined(LUA_EXTRAS)
129         /* register tolua++ user types */
130         tolua_open(lua_L);
131         tolua_usertype(lua_L, "Drawable");
132         tolua_usertype(lua_L, "Visual");
133         tolua_usertype(lua_L, "Display");
134 #endif /* X11 */
135 }
136
137 void llua_load(const char *script)
138 {
139         int error;
140         char path[DEFAULT_TEXT_BUFFER_SIZE];
141
142         llua_init();
143
144         to_real_path(path, script);
145         error = luaL_dofile(lua_L, path);
146         if (error) {
147                 NORM_ERR("llua_load: %s", lua_tostring(lua_L, -1));
148                 lua_pop(lua_L, 1);
149 #ifdef HAVE_SYS_INOTIFY_H
150         } else if (!llua_block_notify && inotify_fd != -1) {
151                 llua_append_notify(path);
152 #endif /* HAVE_SYS_INOTIFY_H */
153         }
154 }
155
156 /*
157    llua_do_call does a flexible call to any Lua function
158 string: <function> [par1] [par2...]
159 retc: the number of return values expected
160  */
161 static char *llua_do_call(const char *string, int retc)
162 {
163         static char func[64];
164         int argc = 0;
165
166         char *tmp = strdup(string);
167         char *ptr = strtok(tmp, " ");
168
169         /* proceed only if the function name is present */
170         if (!ptr) {
171                 free(tmp);
172                 return NULL;
173         }
174
175         /* call only conky_ prefixed functions */
176         if(strncmp(ptr, LUAPREFIX, strlen(LUAPREFIX)) == 0) {
177                 snprintf(func, 64, "%s", ptr);
178         }else{
179                 snprintf(func, 64, "%s%s", LUAPREFIX, ptr);
180         }
181
182         /* push the function name to stack */
183         lua_getglobal(lua_L, func);
184
185         /* parse all function parameters from args and push them to the stack */
186         ptr = strtok(NULL, " ");
187         while (ptr) {
188                 lua_pushstring(lua_L, ptr);
189                 ptr = strtok(NULL, " ");
190                 argc++;
191         }
192
193         free(tmp);
194
195         if(lua_pcall(lua_L, argc, retc, 0) != 0) {
196                 NORM_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 #if 0
205 /*
206  * same as llua_do_call() except passes everything after func as one arg.
207  */
208 static char *llua_do_read_call(const char *function, const char *arg, int retc)
209 {
210         static char func[64];
211         snprintf(func, 64, "conky_%s", function);
212
213         /* push the function name to stack */
214         lua_getglobal(lua_L, func);
215
216         /* push function parameter to the stack */
217         lua_pushstring(lua_L, arg);
218
219         if (lua_pcall(lua_L, 1, retc, 0) != 0) {
220                 NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
221                 lua_pop(lua_L, -1);
222                 return NULL;
223         }
224
225         return func;
226 }
227 #endif
228
229 /* call a function with args, and return a string from it (must be free'd) */
230 static char *llua_getstring(const char *args)
231 {
232         char *func;
233         char *ret = NULL;
234
235         if(!lua_L) return NULL;
236
237         func = llua_do_call(args, 1);
238         if (func) {
239                 if (!lua_isstring(lua_L, -1)) {
240                         NORM_ERR("llua_getstring: function %s didn't return a string, result discarded", func);
241                 } else {
242                         ret = strdup(lua_tostring(lua_L, -1));
243                         lua_pop(lua_L, 1);
244                 }
245         }
246
247         return ret;
248 }
249
250 #if 0
251 /* call a function with args, and return a string from it (must be free'd) */
252 static char *llua_getstring_read(const char *function, const char *arg)
253 {
254         char *func;
255         char *ret = NULL;
256
257         if(!lua_L) return NULL;
258
259         func = llua_do_read_call(function, arg, 1);
260         if (func) {
261                 if(!lua_isstring(lua_L, -1)) {
262                         NORM_ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
263                 } else {
264                         ret = strdup(lua_tostring(lua_L, -1));
265                         lua_pop(lua_L, 1);
266                 }
267         }
268
269         return ret;
270 }
271 #endif
272
273 /* call a function with args, and put the result in ret */
274 static int llua_getnumber(const char *args, double *ret)
275 {
276         char *func;
277
278         if(!lua_L) return 0;
279
280         func = llua_do_call(args, 1);
281         if(func) {
282                 if(!lua_isnumber(lua_L, -1)) {
283                         NORM_ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
284                 } else {
285                         *ret = lua_tonumber(lua_L, -1);
286                         lua_pop(lua_L, 1);
287                         return 1;
288                 }
289         }
290         return 0;
291 }
292
293 void llua_close(void)
294 {
295 #ifdef HAVE_SYS_INOTIFY_H
296         llua_rm_notifies();
297 #endif /* HAVE_SYS_INOTIFY_H */
298         if (draw_pre_hook) {
299                 free(draw_pre_hook);
300                 draw_pre_hook = 0;
301         }
302         if (draw_post_hook) {
303                 free(draw_post_hook);
304                 draw_post_hook = 0;
305         }
306         if (startup_hook) {
307                 free(startup_hook);
308                 startup_hook = 0;
309         }
310         if (shutdown_hook) {
311                 free(shutdown_hook);
312                 shutdown_hook = 0;
313         }
314         if(!lua_L) return;
315         lua_close(lua_L);
316         lua_L = NULL;
317 }
318
319 #ifdef HAVE_SYS_INOTIFY_H
320 struct _lua_notify_s {
321         int wd;
322         char name[DEFAULT_TEXT_BUFFER_SIZE];
323         struct _lua_notify_s *next;
324 };
325 static struct _lua_notify_s *lua_notifies = 0;
326
327 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
328 {
329         struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
330         memset(ret, 0, sizeof(struct _lua_notify_s));
331         strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
332         return ret;
333 }
334
335 void llua_append_notify(const char *name)
336 {
337         /* do it */
338         struct _lua_notify_s *new_tail = 0;
339         if (!lua_notifies) {
340                 /* empty, fresh new digs */
341                 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
342         } else {
343                 struct _lua_notify_s *tail = lua_notifies;
344                 while (tail->next) {
345                         tail = tail->next;
346                 }
347                 // should be @ the end now
348                 new_tail = llua_notify_list_do_alloc(name);
349                 tail->next = new_tail;
350         }
351         new_tail->wd = inotify_add_watch(inotify_fd,
352                         new_tail->name,
353                         IN_MODIFY);
354 }
355
356 void llua_rm_notifies(void)
357 {
358         /* git 'er done */
359         struct _lua_notify_s *head = lua_notifies;
360         struct _lua_notify_s *next = 0;
361         if (!lua_notifies) return;
362         inotify_rm_watch(inotify_fd, head->wd);
363         if (head->next) next = head->next;
364         free(head);
365         while (next) {
366                 head = next;
367                 next = head->next;
368                 inotify_rm_watch(inotify_fd, head->wd);
369                 free(head);
370         }
371         lua_notifies = 0;
372 }
373
374 void llua_inotify_query(int wd, int mask)
375 {
376         struct _lua_notify_s *head = lua_notifies;
377         if (mask & IN_MODIFY || mask & IN_IGNORED) {
378                 /* for whatever reason, i keep getting IN_IGNORED when the file is
379                  * modified */
380                 while (head) {
381                         if (head->wd == wd) {
382                                 llua_block_notify = 1;
383                                 llua_load(head->name);
384                                 llua_block_notify = 0;
385                                 NORM_ERR("Lua script '%s' reloaded", head->name);
386                                 if (mask & IN_IGNORED) {
387                                         /* for some reason we get IN_IGNORED here
388                                          * sometimes, so we need to re-add the watch */
389                                         head->wd = inotify_add_watch(inotify_fd,
390                                                         head->name,
391                                                         IN_MODIFY);
392                                 }
393                                 return;
394                         }
395                         head = head->next;
396                 }
397         }
398 }
399 #endif /* HAVE_SYS_INOTIFY_H */
400
401 void llua_set_number(const char *key, double value)
402 {
403         lua_pushnumber(lua_L, value);
404         lua_setfield(lua_L, -2, key);
405 }
406
407 void llua_set_startup_hook(const char *args)
408 {
409         if (startup_hook) free(startup_hook);
410         startup_hook = strdup(args);
411 }
412
413 void llua_set_shutdown_hook(const char *args)
414 {
415         if (shutdown_hook) free(shutdown_hook);
416         shutdown_hook = strdup(args);
417 }
418
419 void llua_startup_hook(void)
420 {
421         if (!lua_L || !startup_hook) return;
422         llua_do_call(startup_hook, 0);
423 }
424
425 void llua_shutdown_hook(void)
426 {
427         if (!lua_L || !shutdown_hook) return;
428         llua_do_call(shutdown_hook, 0);
429 }
430
431 #ifdef X11
432 void llua_draw_pre_hook(void)
433 {
434         if (!lua_L || !draw_pre_hook) return;
435         llua_do_call(draw_pre_hook, 0);
436 }
437
438 void llua_draw_post_hook(void)
439 {
440         if (!lua_L || !draw_post_hook) return;
441         llua_do_call(draw_post_hook, 0);
442 }
443
444 void llua_set_draw_pre_hook(const char *args)
445 {
446         draw_pre_hook = strdup(args);
447 }
448
449 void llua_set_draw_post_hook(const char *args)
450 {
451         draw_post_hook = strdup(args);
452 }
453
454 #ifdef LUA_EXTRAS
455 void llua_set_userdata(const char *key, const char *type, void *value)
456 {
457         tolua_pushusertype(lua_L, value, type);
458         lua_setfield(lua_L, -2, key);
459 }
460 #endif /* LUA_EXTRAS */
461
462 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
463 {
464         if (!lua_L) return;
465         lua_newtable(lua_L);
466
467         if (output_methods & TO_X) {
468 #ifdef LUA_EXTRAS
469                 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
470                 llua_set_userdata("visual", "Visual", window.visual);
471                 llua_set_userdata("display", "Display", display);
472 #endif /* LUA_EXTRAS */
473
474
475                 llua_set_number("width", window.width);
476                 llua_set_number("height", window.height);
477                 llua_set_number("border_inner_margin", window.border_inner_margin);
478                 llua_set_number("border_outer_margin", window.border_outer_margin);
479                 llua_set_number("border_width", window.border_width);
480
481                 llua_set_number("text_start_x", text_start_x);
482                 llua_set_number("text_start_y", text_start_y);
483                 llua_set_number("text_width", text_width);
484                 llua_set_number("text_height", text_height);
485
486                 lua_setglobal(lua_L, "conky_window");
487         }
488 }
489
490 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
491 {
492         if (!lua_L) return;
493
494         lua_getglobal(lua_L, "conky_window");
495         if (lua_isnil(lua_L, -1)) {
496                 /* window table isn't populated yet */
497                 lua_pop(lua_L, 1);
498                 return;
499         }
500
501         llua_set_number("width", window.width);
502         llua_set_number("height", window.height);
503
504         llua_set_number("text_start_x", text_start_x);
505         llua_set_number("text_start_y", text_start_y);
506         llua_set_number("text_width", text_width);
507         llua_set_number("text_height", text_height);
508
509         lua_setglobal(lua_L, "conky_window");
510 }
511 #endif /* X11 */
512
513 void llua_setup_info(struct information *i, double u_interval)
514 {
515         if (!lua_L) return;
516         lua_newtable(lua_L);
517
518         llua_set_number("update_interval", u_interval);
519         llua_set_number("uptime", i->uptime);
520
521         lua_setglobal(lua_L, "conky_info");
522 }
523
524 void llua_update_info(struct information *i, double u_interval)
525 {
526         if (!lua_L) return;
527
528         lua_getglobal(lua_L, "conky_info");
529         if (lua_isnil(lua_L, -1)) {
530                 /* window table isn't populated yet */
531                 lua_pop(lua_L, 1);
532                 return;
533         }
534
535         llua_set_number("update_interval", u_interval);
536         llua_set_number("uptime", i->uptime);
537
538         lua_setglobal(lua_L, "conky_info");
539 }
540
541 void print_lua(struct text_object *obj, char *p, int p_max_size)
542 {
543         char *str = llua_getstring(obj->data.s);
544         if (str) {
545                 snprintf(p, p_max_size, "%s", str);
546                 free(str);
547         }
548 }
549
550 void print_lua_parse(struct text_object *obj, char *p, int p_max_size)
551 {
552         char *str = llua_getstring(obj->data.s);
553         if (str) {
554                 evaluate(str, p, p_max_size);
555                 free(str);
556         }
557 }
558
559 void print_lua_bar(struct text_object *obj, char *p, int p_max_size)
560 {
561         double per;
562         if (llua_getnumber(obj->data.s, &per)) {
563                 new_bar(obj, p, p_max_size, (per/100.0 * 255));
564         }
565 }
566
567 #ifdef X11
568 void print_lua_graph(struct text_object *obj, char *p, int p_max_size)
569 {
570         double per;
571
572         if (!p_max_size)
573                 return;
574
575         if (llua_getnumber(obj->data.s, &per)) {
576                 new_graph(obj, p, p_max_size, per);
577         }
578 }
579 #endif /* X11 */
580
581 void print_lua_gauge(struct text_object *obj, char *p, int p_max_size)
582 {
583         double per;
584
585         if (!p_max_size)
586                 return;
587
588         if (llua_getnumber(obj->data.s, &per)) {
589                 new_gauge(obj, p, p_max_size, (per/100.0 * 255));
590         }
591 }