Move vi modelines closer to the beginning, so they're more likely to be actually...
[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-2009 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);
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 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 /*
205  * same as llua_do_call() except passes everything after func as one arg.
206  */
207 char *llua_do_read_call(const char *function, const char *arg, int retc)
208 {
209         static char func[64];
210         snprintf(func, 64, "conky_%s", function);
211
212         /* push the function name to stack */
213         lua_getglobal(lua_L, func);
214
215         /* push function parameter to the stack */
216         lua_pushstring(lua_L, arg);
217
218         if (lua_pcall(lua_L, 1, retc, 0) != 0) {
219                 NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
220                 lua_pop(lua_L, -1);
221                 return NULL;
222         }
223
224         return func;
225 }
226
227 char *llua_getstring(const char *args)
228 {
229         char *func;
230         char *ret = NULL;
231
232         if(!lua_L) return NULL;
233
234         func = llua_do_call(args, 1);
235         if (func) {
236                 if (!lua_isstring(lua_L, -1)) {
237                         NORM_ERR("llua_getstring: function %s didn't return a string, result discarded", func);
238                 } else {
239                         ret = strdup(lua_tostring(lua_L, -1));
240                         lua_pop(lua_L, 1);
241                 }
242         }
243
244         return ret;
245 }
246
247 char *llua_getstring_read(const char *function, const char *arg)
248 {
249         char *func;
250         char *ret = NULL;
251
252         if(!lua_L) return NULL;
253
254         func = llua_do_read_call(function, arg, 1);
255         if (func) {
256                 if(!lua_isstring(lua_L, -1)) {
257                         NORM_ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
258                 } else {
259                         ret = strdup(lua_tostring(lua_L, -1));
260                         lua_pop(lua_L, 1);
261                 }
262         }
263
264         return ret;
265 }
266
267 int llua_getnumber(const char *args, double *ret)
268 {
269         char *func;
270
271         if(!lua_L) return 0;
272
273         func = llua_do_call(args, 1);
274         if(func) {
275                 if(!lua_isnumber(lua_L, -1)) {
276                         NORM_ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
277                 } else {
278                         *ret = lua_tonumber(lua_L, -1);
279                         lua_pop(lua_L, 1);
280                         return 1;
281                 }
282         }
283         return 0;
284 }
285
286 void llua_close(void)
287 {
288 #ifdef HAVE_SYS_INOTIFY_H
289         llua_rm_notifies();
290 #endif /* HAVE_SYS_INOTIFY_H */
291         if (draw_pre_hook) {
292                 free(draw_pre_hook);
293                 draw_pre_hook = 0;
294         }
295         if (draw_post_hook) {
296                 free(draw_post_hook);
297                 draw_post_hook = 0;
298         }
299         if(!lua_L) return;
300         lua_close(lua_L);
301         lua_L = NULL;
302 }
303
304 #ifdef HAVE_SYS_INOTIFY_H
305 struct _lua_notify_s {
306         int wd;
307         char name[DEFAULT_TEXT_BUFFER_SIZE];
308         struct _lua_notify_s *next;
309 };
310 static struct _lua_notify_s *lua_notifies = 0;
311
312 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
313 {
314         struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
315         memset(ret, 0, sizeof(struct _lua_notify_s));
316         strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
317         return ret;
318 }
319
320 void llua_append_notify(const char *name)
321 {
322         /* do it */
323         struct _lua_notify_s *new_tail = 0;
324         if (!lua_notifies) {
325                 /* empty, fresh new digs */
326                 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
327         } else {
328                 struct _lua_notify_s *tail = lua_notifies;
329                 while (tail->next) {
330                         tail = tail->next;
331                 }
332                 // should be @ the end now
333                 new_tail = llua_notify_list_do_alloc(name);
334                 tail->next = new_tail;
335         }
336         new_tail->wd = inotify_add_watch(inotify_fd,
337                         new_tail->name,
338                         IN_MODIFY);
339 }
340
341 void llua_rm_notifies(void)
342 {
343         /* git 'er done */
344         struct _lua_notify_s *head = lua_notifies;
345         struct _lua_notify_s *next = 0;
346         if (!lua_notifies) return;
347         inotify_rm_watch(inotify_fd, head->wd);
348         if (head->next) next = head->next;
349         free(head);
350         while (next) {
351                 head = next;
352                 next = head->next;
353                 inotify_rm_watch(inotify_fd, head->wd);
354                 free(head);
355         }
356         lua_notifies = 0;
357 }
358
359 void llua_inotify_query(int wd, int mask)
360 {
361         struct _lua_notify_s *head = lua_notifies;
362         if (mask & IN_MODIFY || mask & IN_IGNORED) {
363                 /* for whatever reason, i keep getting IN_IGNORED when the file is
364                  * modified */
365                 while (head) {
366                         if (head->wd == wd) {
367                                 llua_block_notify = 1;
368                                 llua_load(head->name);
369                                 llua_block_notify = 0;
370                                 NORM_ERR("Lua script '%s' reloaded", head->name);
371                                 if (mask & IN_IGNORED) {
372                                         /* for some reason we get IN_IGNORED here
373                                          * sometimes, so we need to re-add the watch */
374                                         head->wd = inotify_add_watch(inotify_fd,
375                                                         head->name,
376                                                         IN_MODIFY);
377                                 }
378                                 return;
379                         }
380                         head = head->next;
381                 }
382         }
383 }
384 #endif /* HAVE_SYS_INOTIFY_H */
385
386 void llua_set_number(const char *key, double value)
387 {
388         lua_pushnumber(lua_L, value);
389         lua_setfield(lua_L, -2, key);
390 }
391
392 void llua_set_startup_hook(const char *args)
393 {
394         startup_hook = strdup(args);
395 }
396
397 void llua_set_shutdown_hook(const char *args)
398 {
399         shutdown_hook = strdup(args);
400 }
401
402 void llua_startup_hook(void)
403 {
404         if (!lua_L || !startup_hook) return;
405         llua_do_call(startup_hook, 0);
406 }
407
408 void llua_shutdown_hook(void)
409 {
410         if (!lua_L || !shutdown_hook) return;
411         llua_do_call(shutdown_hook, 0);
412 }
413
414 #ifdef X11
415 void llua_draw_pre_hook(void)
416 {
417         if (!lua_L || !draw_pre_hook) return;
418         llua_do_call(draw_pre_hook, 0);
419 }
420
421 void llua_draw_post_hook(void)
422 {
423         if (!lua_L || !draw_post_hook) return;
424         llua_do_call(draw_post_hook, 0);
425 }
426
427 void llua_set_draw_pre_hook(const char *args)
428 {
429         draw_pre_hook = strdup(args);
430 }
431
432 void llua_set_draw_post_hook(const char *args)
433 {
434         draw_post_hook = strdup(args);
435 }
436
437 #ifdef LUA_EXTRAS
438 void llua_set_userdata(const char *key, const char *type, void *value)
439 {
440         tolua_pushusertype(lua_L, value, type);
441         lua_setfield(lua_L, -2, key);
442 }
443 #endif /* LUA_EXTRAS */
444
445 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
446 {
447         if (!lua_L) return;
448         lua_newtable(lua_L);
449
450         if (output_methods & TO_X) {
451 #ifdef LUA_EXTRAS
452                 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
453                 llua_set_userdata("visual", "Visual", window.visual);
454                 llua_set_userdata("display", "Display", display);
455 #endif /* LUA_EXTRAS */
456
457
458                 llua_set_number("width", window.width);
459                 llua_set_number("height", window.height);
460                 llua_set_number("border_inner_margin", window.border_inner_margin);
461                 llua_set_number("border_outer_margin", window.border_outer_margin);
462                 llua_set_number("border_width", window.border_width);
463
464                 llua_set_number("text_start_x", text_start_x);
465                 llua_set_number("text_start_y", text_start_y);
466                 llua_set_number("text_width", text_width);
467                 llua_set_number("text_height", text_height);
468
469                 lua_setglobal(lua_L, "conky_window");
470         }
471 }
472
473 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
474 {
475         if (!lua_L) return;
476
477         lua_getglobal(lua_L, "conky_window");
478         if (lua_isnil(lua_L, -1)) {
479                 /* window table isn't populated yet */
480                 lua_pop(lua_L, 1);
481                 return;
482         }
483
484         llua_set_number("width", window.width);
485         llua_set_number("height", window.height);
486
487         llua_set_number("text_start_x", text_start_x);
488         llua_set_number("text_start_y", text_start_y);
489         llua_set_number("text_width", text_width);
490         llua_set_number("text_height", text_height);
491
492         lua_setglobal(lua_L, "conky_window");
493 }
494 #endif /* X11 */
495
496 void llua_setup_info(struct information *i, double u_interval)
497 {
498         if (!lua_L) return;
499         lua_newtable(lua_L);
500
501         llua_set_number("update_interval", u_interval);
502         llua_set_number("uptime", i->uptime);
503
504         lua_setglobal(lua_L, "conky_info");
505 }
506
507 void llua_update_info(struct information *i, double u_interval)
508 {
509         if (!lua_L) return;
510
511         lua_getglobal(lua_L, "conky_info");
512         if (lua_isnil(lua_L, -1)) {
513                 /* window table isn't populated yet */
514                 lua_pop(lua_L, 1);
515                 return;
516         }
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