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
4 * Conky, a system monitor, based on torsmo
6 * Copyright (c) 2009 Toni Spets
7 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
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.
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/>.
31 #endif /* LUA_EXTRAS */
33 #ifdef HAVE_SYS_INOTIFY_H
34 #include <sys/inotify.h>
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 */
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;
46 lua_State *lua_L = NULL;
48 static int llua_conky_parse(lua_State *L)
50 int n = lua_gettop(L); /* number of arguments */
52 char *buf = calloc(1, max_user_text);
54 lua_pushstring(L, "incorrect arguments, conky_parse(string) takes exactly 1 argument");
57 if (!lua_isstring(L, 1)) {
58 lua_pushstring(L, "incorrect argument (expecting a string)");
61 str = strdup(lua_tostring(L, 1));
62 evaluate(str, buf, max_user_text);
63 lua_pushstring(L, buf);
66 return 1; /* number of results */
69 static int llua_conky_set_update_interval(lua_State *L)
71 int n = lua_gettop(L); /* number of arguments */
74 lua_pushstring(L, "incorrect arguments, conky_set_update_interval(number) takes exactly 1 argument");
77 if (!lua_isnumber(L, 1)) {
78 lua_pushstring(L, "incorrect argument (expecting a number)");
81 value = lua_tonumber(L, 1);
82 set_update_interval(value);
83 return 0; /* number of results */
88 const char *libs = PACKAGE_LIBDIR"/lib?.so;";
89 char *old_path, *new_path;
93 /* add our library path to the lua package.cpath global var */
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");
107 lua_pushstring(lua_L, PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH);
108 lua_setglobal(lua_L, "conky_build_info");
110 lua_pushstring(lua_L, VERSION);
111 lua_setglobal(lua_L, "conky_version");
113 lua_pushstring(lua_L, BUILD_DATE);
114 lua_setglobal(lua_L, "conky_build_date");
116 lua_pushstring(lua_L, BUILD_ARCH);
117 lua_setglobal(lua_L, "conky_build_arch");
119 lua_pushstring(lua_L, current_config);
120 lua_setglobal(lua_L, "conky_config");
122 lua_pushcfunction(lua_L, &llua_conky_parse);
123 lua_setglobal(lua_L, "conky_parse");
125 lua_pushcfunction(lua_L, &llua_conky_set_update_interval);
126 lua_setglobal(lua_L, "conky_set_update_interval");
128 #if defined(X11) && defined(LUA_EXTRAS)
129 /* register tolua++ user types */
131 tolua_usertype(lua_L, "Drawable");
132 tolua_usertype(lua_L, "Visual");
133 tolua_usertype(lua_L, "Display");
137 void llua_load(const char *script)
140 char path[DEFAULT_TEXT_BUFFER_SIZE];
144 to_real_path(path, script);
145 error = luaL_dofile(lua_L, path);
147 NORM_ERR("llua_load: %s", lua_tostring(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 */
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
161 static char *llua_do_call(const char *string, int retc)
163 static char func[64];
166 char *tmp = strdup(string);
167 char *ptr = strtok(tmp, " ");
169 /* proceed only if the function name is present */
175 /* call only conky_ prefixed functions */
176 if(strncmp(ptr, LUAPREFIX, strlen(LUAPREFIX)) == 0) {
177 snprintf(func, 64, "%s", ptr);
179 snprintf(func, 64, "%s%s", LUAPREFIX, ptr);
182 /* push the function name to stack */
183 lua_getglobal(lua_L, func);
185 /* parse all function parameters from args and push them to the stack */
186 ptr = strtok(NULL, " ");
188 lua_pushstring(lua_L, ptr);
189 ptr = strtok(NULL, " ");
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));
206 * same as llua_do_call() except passes everything after func as one arg.
208 static char *llua_do_read_call(const char *function, const char *arg, int retc)
210 static char func[64];
211 snprintf(func, 64, "conky_%s", function);
213 /* push the function name to stack */
214 lua_getglobal(lua_L, func);
216 /* push function parameter to the stack */
217 lua_pushstring(lua_L, arg);
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));
229 /* call a function with args, and return a string from it (must be free'd) */
230 static char *llua_getstring(const char *args)
235 if(!lua_L) return NULL;
237 func = llua_do_call(args, 1);
239 if (!lua_isstring(lua_L, -1)) {
240 NORM_ERR("llua_getstring: function %s didn't return a string, result discarded", func);
242 ret = strdup(lua_tostring(lua_L, -1));
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)
257 if(!lua_L) return NULL;
259 func = llua_do_read_call(function, arg, 1);
261 if(!lua_isstring(lua_L, -1)) {
262 NORM_ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
264 ret = strdup(lua_tostring(lua_L, -1));
273 /* call a function with args, and put the result in ret */
274 static int llua_getnumber(const char *args, double *ret)
280 func = llua_do_call(args, 1);
282 if(!lua_isnumber(lua_L, -1)) {
283 NORM_ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
285 *ret = lua_tonumber(lua_L, -1);
293 void llua_close(void)
295 #ifdef HAVE_SYS_INOTIFY_H
297 #endif /* HAVE_SYS_INOTIFY_H */
302 if (draw_post_hook) {
303 free(draw_post_hook);
311 #ifdef HAVE_SYS_INOTIFY_H
312 struct _lua_notify_s {
314 char name[DEFAULT_TEXT_BUFFER_SIZE];
315 struct _lua_notify_s *next;
317 static struct _lua_notify_s *lua_notifies = 0;
319 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
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);
327 void llua_append_notify(const char *name)
330 struct _lua_notify_s *new_tail = 0;
332 /* empty, fresh new digs */
333 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
335 struct _lua_notify_s *tail = lua_notifies;
339 // should be @ the end now
340 new_tail = llua_notify_list_do_alloc(name);
341 tail->next = new_tail;
343 new_tail->wd = inotify_add_watch(inotify_fd,
348 void llua_rm_notifies(void)
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;
360 inotify_rm_watch(inotify_fd, head->wd);
366 void llua_inotify_query(int wd, int mask)
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
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,
391 #endif /* HAVE_SYS_INOTIFY_H */
393 void llua_set_number(const char *key, double value)
395 lua_pushnumber(lua_L, value);
396 lua_setfield(lua_L, -2, key);
399 void llua_set_startup_hook(const char *args)
401 startup_hook = strdup(args);
404 void llua_set_shutdown_hook(const char *args)
406 shutdown_hook = strdup(args);
409 void llua_startup_hook(void)
411 if (!lua_L || !startup_hook) return;
412 llua_do_call(startup_hook, 0);
415 void llua_shutdown_hook(void)
417 if (!lua_L || !shutdown_hook) return;
418 llua_do_call(shutdown_hook, 0);
422 void llua_draw_pre_hook(void)
424 if (!lua_L || !draw_pre_hook) return;
425 llua_do_call(draw_pre_hook, 0);
428 void llua_draw_post_hook(void)
430 if (!lua_L || !draw_post_hook) return;
431 llua_do_call(draw_post_hook, 0);
434 void llua_set_draw_pre_hook(const char *args)
436 draw_pre_hook = strdup(args);
439 void llua_set_draw_post_hook(const char *args)
441 draw_post_hook = strdup(args);
445 void llua_set_userdata(const char *key, const char *type, void *value)
447 tolua_pushusertype(lua_L, value, type);
448 lua_setfield(lua_L, -2, key);
450 #endif /* LUA_EXTRAS */
452 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
457 if (output_methods & TO_X) {
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 */
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);
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);
476 lua_setglobal(lua_L, "conky_window");
480 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
484 lua_getglobal(lua_L, "conky_window");
485 if (lua_isnil(lua_L, -1)) {
486 /* window table isn't populated yet */
491 llua_set_number("width", window.width);
492 llua_set_number("height", window.height);
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);
499 lua_setglobal(lua_L, "conky_window");
503 void llua_setup_info(struct information *i, double u_interval)
508 llua_set_number("update_interval", u_interval);
509 llua_set_number("uptime", i->uptime);
511 lua_setglobal(lua_L, "conky_info");
514 void llua_update_info(struct information *i, double u_interval)
518 lua_getglobal(lua_L, "conky_info");
519 if (lua_isnil(lua_L, -1)) {
520 /* window table isn't populated yet */
525 llua_set_number("update_interval", u_interval);
526 llua_set_number("uptime", i->uptime);
528 lua_setglobal(lua_L, "conky_info");
531 void print_lua(struct text_object *obj, char *p, int p_max_size)
533 char *str = llua_getstring(obj->data.s);
535 snprintf(p, p_max_size, "%s", str);
540 void print_lua_parse(struct text_object *obj, char *p, int p_max_size)
542 char *str = llua_getstring(obj->data.s);
544 evaluate(str, p, p_max_size);
549 void print_lua_bar(struct text_object *obj, char *p, int p_max_size)
552 if (llua_getnumber(obj->data.s, &per)) {
553 new_bar(obj, p, p_max_size, (per/100.0 * 255));
558 void print_lua_graph(struct text_object *obj, char *p, int p_max_size)
565 if (llua_getnumber(obj->data.s, &per)) {
566 new_graph(obj, p, p_max_size, per);
571 void print_lua_gauge(struct text_object *obj, char *p, int p_max_size)
578 if (llua_getnumber(obj->data.s, &per)) {
579 new_gauge(obj, p, p_max_size, (per/100.0 * 255));