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/>.
33 #endif /* LUA_EXTRAS */
35 #ifdef HAVE_SYS_INOTIFY_H
36 #include <sys/inotify.h>
38 void llua_append_notify(const char *name);
39 void llua_rm_notifies(void);
40 static int llua_block_notify = 0;
41 #endif /* HAVE_SYS_INOTIFY_H */
43 #define MIN(a, b) ( (a) < (b) ? (a) : (b) )
45 static char *draw_pre_hook = 0;
46 static char *draw_post_hook = 0;
47 static char *startup_hook = 0;
48 static char *shutdown_hook = 0;
50 lua_State *lua_L = NULL;
52 static int llua_conky_parse(lua_State *L)
54 int n = lua_gettop(L); /* number of arguments */
56 char *buf = calloc(1, max_user_text);
58 lua_pushstring(L, "incorrect arguments, conky_parse(string) takes exactly 1 argument");
61 if (!lua_isstring(L, 1)) {
62 lua_pushstring(L, "incorrect argument (expecting a string)");
65 str = strdup(lua_tostring(L, 1));
66 evaluate(str, buf, max_user_text);
67 lua_pushstring(L, buf);
70 return 1; /* number of results */
73 static int llua_conky_set_update_interval(lua_State *L)
75 int n = lua_gettop(L); /* number of arguments */
78 lua_pushstring(L, "incorrect arguments, conky_set_update_interval(number) takes exactly 1 argument");
81 if (!lua_isnumber(L, 1)) {
82 lua_pushstring(L, "incorrect argument (expecting a number)");
85 value = lua_tonumber(L, 1);
86 set_update_interval(value);
87 return 0; /* number of results */
92 const char *libs = PACKAGE_LIBDIR"/lib?.so;";
93 char *old_path, *new_path;
97 /* add our library path to the lua package.cpath global var */
99 lua_getglobal(lua_L, "package");
100 lua_getfield(lua_L, -1, "cpath");
101 old_path = strdup(lua_tostring(lua_L, -1));
102 new_path = malloc(strlen(old_path) + strlen(libs) + 1);
103 strcpy(new_path, libs);
104 strcat(new_path, old_path);
105 lua_pushstring(lua_L, new_path);
106 lua_setfield(lua_L, -3, "cpath");
111 lua_pushstring(lua_L, PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH);
112 lua_setglobal(lua_L, "conky_build_info");
114 lua_pushstring(lua_L, VERSION);
115 lua_setglobal(lua_L, "conky_version");
117 lua_pushstring(lua_L, BUILD_DATE);
118 lua_setglobal(lua_L, "conky_build_date");
120 lua_pushstring(lua_L, BUILD_ARCH);
121 lua_setglobal(lua_L, "conky_build_arch");
123 lua_pushstring(lua_L, current_config);
124 lua_setglobal(lua_L, "conky_config");
126 lua_pushcfunction(lua_L, &llua_conky_parse);
127 lua_setglobal(lua_L, "conky_parse");
129 lua_pushcfunction(lua_L, &llua_conky_set_update_interval);
130 lua_setglobal(lua_L, "conky_set_update_interval");
132 #if defined(X11) && defined(LUA_EXTRAS)
133 /* register tolua++ user types */
135 tolua_usertype(lua_L, "Drawable");
136 tolua_usertype(lua_L, "Visual");
137 tolua_usertype(lua_L, "Display");
141 void llua_load(const char *script)
144 char path[DEFAULT_TEXT_BUFFER_SIZE];
148 to_real_path(path, script);
149 error = luaL_dofile(lua_L, path);
151 NORM_ERR("llua_load: %s", lua_tostring(lua_L, -1));
153 #ifdef HAVE_SYS_INOTIFY_H
154 } else if (!llua_block_notify && inotify_fd != -1) {
155 llua_append_notify(path);
156 #endif /* HAVE_SYS_INOTIFY_H */
161 * Returns the first space-delimited token of the string starting at position *len.
162 * On return *len contains the length of the token. Spaces inside brackets are ignored, so that
163 * eg. '${foo bar}' is treated as a single token. Sets *len to zero and *str points to the end of
164 * the string when there are no more tokens.
166 static const char* tokenize(const char *str, size_t *len)
170 while(str && isspace(*str))
174 while(str[*len] && (level > 0 || !isspace(str[*len]))) {
176 case '{': ++level; break;
177 case '}': --level; break;
182 if(!str[*len] && level > 0)
183 NORM_ERR("tokenize: improperly nested token: %s", str);
189 llua_do_call does a flexible call to any Lua function
190 string: <function> [par1] [par2...]
191 retc: the number of return values expected
193 static char *llua_do_call(const char *string, int retc)
195 static char func[64];
200 const char *ptr = tokenize(string, &len);
202 /* proceed only if the function name is present */
207 /* call only conky_ prefixed functions */
208 if(strncmp(ptr, LUAPREFIX, strlen(LUAPREFIX)) != 0) {
209 snprintf(func, sizeof func, "%s", LUAPREFIX);
212 strncat(func, ptr, MIN(len, sizeof(func) - strlen(func) - 1));
214 /* push the function name to stack */
215 lua_getglobal(lua_L, func);
217 /* parse all function parameters from args and push them to the stack */
218 while( ptr = tokenize(ptr, &len), len) {
219 lua_pushlstring(lua_L, ptr, len);
223 if(lua_pcall(lua_L, argc, retc, 0) != 0) {
224 NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
234 * same as llua_do_call() except passes everything after func as one arg.
236 static char *llua_do_read_call(const char *function, const char *arg, int retc)
238 static char func[64];
239 snprintf(func, 64, "conky_%s", function);
241 /* push the function name to stack */
242 lua_getglobal(lua_L, func);
244 /* push function parameter to the stack */
245 lua_pushstring(lua_L, arg);
247 if (lua_pcall(lua_L, 1, retc, 0) != 0) {
248 NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
257 /* call a function with args, and return a string from it (must be free'd) */
258 static char *llua_getstring(const char *args)
263 if(!lua_L) return NULL;
265 func = llua_do_call(args, 1);
267 if (!lua_isstring(lua_L, -1)) {
268 NORM_ERR("llua_getstring: function %s didn't return a string, result discarded", func);
270 ret = strdup(lua_tostring(lua_L, -1));
279 /* call a function with args, and return a string from it (must be free'd) */
280 static char *llua_getstring_read(const char *function, const char *arg)
285 if(!lua_L) return NULL;
287 func = llua_do_read_call(function, arg, 1);
289 if(!lua_isstring(lua_L, -1)) {
290 NORM_ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
292 ret = strdup(lua_tostring(lua_L, -1));
301 /* call a function with args, and put the result in ret */
302 static int llua_getnumber(const char *args, double *ret)
308 func = llua_do_call(args, 1);
310 if(!lua_isnumber(lua_L, -1)) {
311 NORM_ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
313 *ret = lua_tonumber(lua_L, -1);
321 void llua_close(void)
323 #ifdef HAVE_SYS_INOTIFY_H
325 #endif /* HAVE_SYS_INOTIFY_H */
330 if (draw_post_hook) {
331 free(draw_post_hook);
347 #ifdef HAVE_SYS_INOTIFY_H
348 struct _lua_notify_s {
350 char name[DEFAULT_TEXT_BUFFER_SIZE];
351 struct _lua_notify_s *next;
353 static struct _lua_notify_s *lua_notifies = 0;
355 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
357 struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
358 memset(ret, 0, sizeof(struct _lua_notify_s));
359 strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
363 void llua_append_notify(const char *name)
366 struct _lua_notify_s *new_tail = 0;
368 /* empty, fresh new digs */
369 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
371 struct _lua_notify_s *tail = lua_notifies;
375 // should be @ the end now
376 new_tail = llua_notify_list_do_alloc(name);
377 tail->next = new_tail;
379 new_tail->wd = inotify_add_watch(inotify_fd,
384 void llua_rm_notifies(void)
387 struct _lua_notify_s *head = lua_notifies;
388 struct _lua_notify_s *next = 0;
389 if (!lua_notifies) return;
390 inotify_rm_watch(inotify_fd, head->wd);
391 if (head->next) next = head->next;
396 inotify_rm_watch(inotify_fd, head->wd);
402 void llua_inotify_query(int wd, int mask)
404 struct _lua_notify_s *head = lua_notifies;
405 if (mask & IN_MODIFY || mask & IN_IGNORED) {
406 /* for whatever reason, i keep getting IN_IGNORED when the file is
409 if (head->wd == wd) {
410 llua_block_notify = 1;
411 llua_load(head->name);
412 llua_block_notify = 0;
413 NORM_ERR("Lua script '%s' reloaded", head->name);
414 if (mask & IN_IGNORED) {
415 /* for some reason we get IN_IGNORED here
416 * sometimes, so we need to re-add the watch */
417 head->wd = inotify_add_watch(inotify_fd,
427 #endif /* HAVE_SYS_INOTIFY_H */
429 void llua_set_number(const char *key, double value)
431 lua_pushnumber(lua_L, value);
432 lua_setfield(lua_L, -2, key);
435 void llua_set_startup_hook(const char *args)
437 if (startup_hook) free(startup_hook);
438 startup_hook = strdup(args);
441 void llua_set_shutdown_hook(const char *args)
443 if (shutdown_hook) free(shutdown_hook);
444 shutdown_hook = strdup(args);
447 void llua_startup_hook(void)
449 if (!lua_L || !startup_hook) return;
450 llua_do_call(startup_hook, 0);
453 void llua_shutdown_hook(void)
455 if (!lua_L || !shutdown_hook) return;
456 llua_do_call(shutdown_hook, 0);
460 void llua_draw_pre_hook(void)
462 if (!lua_L || !draw_pre_hook) return;
463 llua_do_call(draw_pre_hook, 0);
466 void llua_draw_post_hook(void)
468 if (!lua_L || !draw_post_hook) return;
469 llua_do_call(draw_post_hook, 0);
472 void llua_set_draw_pre_hook(const char *args)
474 draw_pre_hook = strdup(args);
477 void llua_set_draw_post_hook(const char *args)
479 draw_post_hook = strdup(args);
483 void llua_set_userdata(const char *key, const char *type, void *value)
485 tolua_pushusertype(lua_L, value, type);
486 lua_setfield(lua_L, -2, key);
488 #endif /* LUA_EXTRAS */
490 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
495 if (output_methods & TO_X) {
497 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
498 llua_set_userdata("visual", "Visual", window.visual);
499 llua_set_userdata("display", "Display", display);
500 #endif /* LUA_EXTRAS */
503 llua_set_number("width", window.width);
504 llua_set_number("height", window.height);
505 llua_set_number("border_inner_margin", window.border_inner_margin);
506 llua_set_number("border_outer_margin", window.border_outer_margin);
507 llua_set_number("border_width", window.border_width);
509 llua_set_number("text_start_x", text_start_x);
510 llua_set_number("text_start_y", text_start_y);
511 llua_set_number("text_width", text_width);
512 llua_set_number("text_height", text_height);
514 lua_setglobal(lua_L, "conky_window");
518 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
522 lua_getglobal(lua_L, "conky_window");
523 if (lua_isnil(lua_L, -1)) {
524 /* window table isn't populated yet */
529 llua_set_number("width", window.width);
530 llua_set_number("height", window.height);
532 llua_set_number("text_start_x", text_start_x);
533 llua_set_number("text_start_y", text_start_y);
534 llua_set_number("text_width", text_width);
535 llua_set_number("text_height", text_height);
537 lua_setglobal(lua_L, "conky_window");
541 void llua_setup_info(struct information *i, double u_interval)
546 llua_set_number("update_interval", u_interval);
547 llua_set_number("uptime", i->uptime);
549 lua_setglobal(lua_L, "conky_info");
552 void llua_update_info(struct information *i, double u_interval)
556 lua_getglobal(lua_L, "conky_info");
557 if (lua_isnil(lua_L, -1)) {
558 /* window table isn't populated yet */
563 llua_set_number("update_interval", u_interval);
564 llua_set_number("uptime", i->uptime);
566 lua_setglobal(lua_L, "conky_info");
569 void print_lua(struct text_object *obj, char *p, int p_max_size)
571 char *str = llua_getstring(obj->data.s);
573 snprintf(p, p_max_size, "%s", str);
578 void print_lua_parse(struct text_object *obj, char *p, int p_max_size)
580 char *str = llua_getstring(obj->data.s);
582 evaluate(str, p, p_max_size);
587 void print_lua_bar(struct text_object *obj, char *p, int p_max_size)
590 if (llua_getnumber(obj->data.s, &per)) {
591 new_bar(obj, p, p_max_size, (per/100.0 * 255));
596 void print_lua_graph(struct text_object *obj, char *p, int p_max_size)
603 if (llua_getnumber(obj->data.s, &per)) {
604 new_graph(obj, p, p_max_size, per);
609 void print_lua_gauge(struct text_object *obj, char *p, int p_max_size)
616 if (llua_getnumber(obj->data.s, &per)) {
617 new_gauge(obj, p, p_max_size, (per/100.0 * 255));