21ac7d252786aa3c917692077f866bfe56002c82
[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(!lua_L) return;
307         lua_close(lua_L);
308         lua_L = NULL;
309 }
310
311 #ifdef HAVE_SYS_INOTIFY_H
312 struct _lua_notify_s {
313         int wd;
314         char name[DEFAULT_TEXT_BUFFER_SIZE];
315         struct _lua_notify_s *next;
316 };
317 static struct _lua_notify_s *lua_notifies = 0;
318
319 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
320 {
321         struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
322         memset(ret, 0, sizeof(struct _lua_notify_s));
323         strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
324         return ret;
325 }
326
327 void llua_append_notify(const char *name)
328 {
329         /* do it */
330         struct _lua_notify_s *new_tail = 0;
331         if (!lua_notifies) {
332                 /* empty, fresh new digs */
333                 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
334         } else {
335                 struct _lua_notify_s *tail = lua_notifies;
336                 while (tail->next) {
337                         tail = tail->next;
338                 }
339                 // should be @ the end now
340                 new_tail = llua_notify_list_do_alloc(name);
341                 tail->next = new_tail;
342         }
343         new_tail->wd = inotify_add_watch(inotify_fd,
344                         new_tail->name,
345                         IN_MODIFY);
346 }
347
348 void llua_rm_notifies(void)
349 {
350         /* git 'er done */
351         struct _lua_notify_s *head = lua_notifies;
352         struct _lua_notify_s *next = 0;
353         if (!lua_notifies) return;
354         inotify_rm_watch(inotify_fd, head->wd);
355         if (head->next) next = head->next;
356         free(head);
357         while (next) {
358                 head = next;
359                 next = head->next;
360                 inotify_rm_watch(inotify_fd, head->wd);
361                 free(head);
362         }
363         lua_notifies = 0;
364 }
365
366 void llua_inotify_query(int wd, int mask)
367 {
368         struct _lua_notify_s *head = lua_notifies;
369         if (mask & IN_MODIFY || mask & IN_IGNORED) {
370                 /* for whatever reason, i keep getting IN_IGNORED when the file is
371                  * modified */
372                 while (head) {
373                         if (head->wd == wd) {
374                                 llua_block_notify = 1;
375                                 llua_load(head->name);
376                                 llua_block_notify = 0;
377                                 NORM_ERR("Lua script '%s' reloaded", head->name);
378                                 if (mask & IN_IGNORED) {
379                                         /* for some reason we get IN_IGNORED here
380                                          * sometimes, so we need to re-add the watch */
381                                         head->wd = inotify_add_watch(inotify_fd,
382                                                         head->name,
383                                                         IN_MODIFY);
384                                 }
385                                 return;
386                         }
387                         head = head->next;
388                 }
389         }
390 }
391 #endif /* HAVE_SYS_INOTIFY_H */
392
393 void llua_set_number(const char *key, double value)
394 {
395         lua_pushnumber(lua_L, value);
396         lua_setfield(lua_L, -2, key);
397 }
398
399 void llua_set_startup_hook(const char *args)
400 {
401         startup_hook = strdup(args);
402 }
403
404 void llua_set_shutdown_hook(const char *args)
405 {
406         shutdown_hook = strdup(args);
407 }
408
409 void llua_startup_hook(void)
410 {
411         if (!lua_L || !startup_hook) return;
412         llua_do_call(startup_hook, 0);
413 }
414
415 void llua_shutdown_hook(void)
416 {
417         if (!lua_L || !shutdown_hook) return;
418         llua_do_call(shutdown_hook, 0);
419 }
420
421 #ifdef X11
422 void llua_draw_pre_hook(void)
423 {
424         if (!lua_L || !draw_pre_hook) return;
425         llua_do_call(draw_pre_hook, 0);
426 }
427
428 void llua_draw_post_hook(void)
429 {
430         if (!lua_L || !draw_post_hook) return;
431         llua_do_call(draw_post_hook, 0);
432 }
433
434 void llua_set_draw_pre_hook(const char *args)
435 {
436         draw_pre_hook = strdup(args);
437 }
438
439 void llua_set_draw_post_hook(const char *args)
440 {
441         draw_post_hook = strdup(args);
442 }
443
444 #ifdef LUA_EXTRAS
445 void llua_set_userdata(const char *key, const char *type, void *value)
446 {
447         tolua_pushusertype(lua_L, value, type);
448         lua_setfield(lua_L, -2, key);
449 }
450 #endif /* LUA_EXTRAS */
451
452 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
453 {
454         if (!lua_L) return;
455         lua_newtable(lua_L);
456
457         if (output_methods & TO_X) {
458 #ifdef LUA_EXTRAS
459                 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
460                 llua_set_userdata("visual", "Visual", window.visual);
461                 llua_set_userdata("display", "Display", display);
462 #endif /* LUA_EXTRAS */
463
464
465                 llua_set_number("width", window.width);
466                 llua_set_number("height", window.height);
467                 llua_set_number("border_inner_margin", window.border_inner_margin);
468                 llua_set_number("border_outer_margin", window.border_outer_margin);
469                 llua_set_number("border_width", window.border_width);
470
471                 llua_set_number("text_start_x", text_start_x);
472                 llua_set_number("text_start_y", text_start_y);
473                 llua_set_number("text_width", text_width);
474                 llua_set_number("text_height", text_height);
475
476                 lua_setglobal(lua_L, "conky_window");
477         }
478 }
479
480 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
481 {
482         if (!lua_L) return;
483
484         lua_getglobal(lua_L, "conky_window");
485         if (lua_isnil(lua_L, -1)) {
486                 /* window table isn't populated yet */
487                 lua_pop(lua_L, 1);
488                 return;
489         }
490
491         llua_set_number("width", window.width);
492         llua_set_number("height", window.height);
493
494         llua_set_number("text_start_x", text_start_x);
495         llua_set_number("text_start_y", text_start_y);
496         llua_set_number("text_width", text_width);
497         llua_set_number("text_height", text_height);
498
499         lua_setglobal(lua_L, "conky_window");
500 }
501 #endif /* X11 */
502
503 void llua_setup_info(struct information *i, double u_interval)
504 {
505         if (!lua_L) return;
506         lua_newtable(lua_L);
507
508         llua_set_number("update_interval", u_interval);
509         llua_set_number("uptime", i->uptime);
510
511         lua_setglobal(lua_L, "conky_info");
512 }
513
514 void llua_update_info(struct information *i, double u_interval)
515 {
516         if (!lua_L) return;
517
518         lua_getglobal(lua_L, "conky_info");
519         if (lua_isnil(lua_L, -1)) {
520                 /* window table isn't populated yet */
521                 lua_pop(lua_L, 1);
522                 return;
523         }
524
525         llua_set_number("update_interval", u_interval);
526         llua_set_number("uptime", i->uptime);
527
528         lua_setglobal(lua_L, "conky_info");
529 }
530
531 void print_lua(struct text_object *obj, char *p, int p_max_size)
532 {
533         char *str = llua_getstring(obj->data.s);
534         if (str) {
535                 snprintf(p, p_max_size, "%s", str);
536                 free(str);
537         }
538 }
539
540 void print_lua_parse(struct text_object *obj, char *p, int p_max_size)
541 {
542         char *str = llua_getstring(obj->data.s);
543         if (str) {
544                 evaluate(str, p, p_max_size);
545                 free(str);
546         }
547 }
548
549 void print_lua_bar(struct text_object *obj, char *p, int p_max_size)
550 {
551         double per;
552         if (llua_getnumber(obj->data.s, &per)) {
553                 new_bar(obj, p, p_max_size, (per/100.0 * 255));
554         }
555 }
556
557 #ifdef X11
558 void print_lua_graph(struct text_object *obj, char *p, int p_max_size)
559 {
560         double per;
561
562         if (!p_max_size)
563                 return;
564
565         if (llua_getnumber(obj->data.s, &per)) {
566                 new_graph(obj, p, p_max_size, per);
567         }
568 }
569 #endif /* X11 */
570
571 void print_lua_gauge(struct text_object *obj, char *p, int p_max_size)
572 {
573         double per;
574
575         if (!p_max_size)
576                 return;
577
578         if (llua_getnumber(obj->data.s, &per)) {
579                 new_gauge(obj, p, p_max_size, (per/100.0 * 255));
580         }
581 }