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 #define MIN(a, b) ( (a) < (b) ? (a) : (b) )
43 static char *draw_pre_hook = 0;
44 static char *draw_post_hook = 0;
45 static char *startup_hook = 0;
46 static char *shutdown_hook = 0;
48 lua_State *lua_L = NULL;
50 static int llua_conky_parse(lua_State *L)
52 int n = lua_gettop(L); /* number of arguments */
54 char *buf = calloc(1, max_user_text);
56 lua_pushstring(L, "incorrect arguments, conky_parse(string) takes exactly 1 argument");
59 if (!lua_isstring(L, 1)) {
60 lua_pushstring(L, "incorrect argument (expecting a string)");
63 str = strdup(lua_tostring(L, 1));
64 evaluate(str, buf, max_user_text);
65 lua_pushstring(L, buf);
68 return 1; /* number of results */
71 static int llua_conky_set_update_interval(lua_State *L)
73 int n = lua_gettop(L); /* number of arguments */
76 lua_pushstring(L, "incorrect arguments, conky_set_update_interval(number) takes exactly 1 argument");
79 if (!lua_isnumber(L, 1)) {
80 lua_pushstring(L, "incorrect argument (expecting a number)");
83 value = lua_tonumber(L, 1);
84 set_update_interval(value);
85 return 0; /* number of results */
90 const char *libs = PACKAGE_LIBDIR"/lib?.so;";
91 char *old_path, *new_path;
95 /* add our library path to the lua package.cpath global var */
97 lua_getglobal(lua_L, "package");
98 lua_getfield(lua_L, -1, "cpath");
99 old_path = strdup(lua_tostring(lua_L, -1));
100 new_path = malloc(strlen(old_path) + strlen(libs) + 1);
101 strcpy(new_path, libs);
102 strcat(new_path, old_path);
103 lua_pushstring(lua_L, new_path);
104 lua_setfield(lua_L, -3, "cpath");
109 lua_pushstring(lua_L, PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH);
110 lua_setglobal(lua_L, "conky_build_info");
112 lua_pushstring(lua_L, VERSION);
113 lua_setglobal(lua_L, "conky_version");
115 lua_pushstring(lua_L, BUILD_DATE);
116 lua_setglobal(lua_L, "conky_build_date");
118 lua_pushstring(lua_L, BUILD_ARCH);
119 lua_setglobal(lua_L, "conky_build_arch");
121 lua_pushstring(lua_L, current_config);
122 lua_setglobal(lua_L, "conky_config");
124 lua_pushcfunction(lua_L, &llua_conky_parse);
125 lua_setglobal(lua_L, "conky_parse");
127 lua_pushcfunction(lua_L, &llua_conky_set_update_interval);
128 lua_setglobal(lua_L, "conky_set_update_interval");
130 #if defined(X11) && defined(LUA_EXTRAS)
131 /* register tolua++ user types */
133 tolua_usertype(lua_L, "Drawable");
134 tolua_usertype(lua_L, "Visual");
135 tolua_usertype(lua_L, "Display");
139 void llua_load(const char *script)
142 char path[DEFAULT_TEXT_BUFFER_SIZE];
146 to_real_path(path, script);
147 error = luaL_dofile(lua_L, path);
149 NORM_ERR("llua_load: %s", lua_tostring(lua_L, -1));
151 #ifdef HAVE_SYS_INOTIFY_H
152 } else if (!llua_block_notify && inotify_fd != -1) {
153 llua_append_notify(path);
154 #endif /* HAVE_SYS_INOTIFY_H */
159 * Returns the first space-delimited token of the string starting at position *len.
160 * On return *len contains the length of the token. Spaces inside brackets are ignored, so that
161 * eg. '${foo bar}' is treated as a single token. Sets *len to zero and *str points to the end of
162 * the string when there are no more tokens.
164 static const char* tokenize(const char *str, size_t *len)
168 while(str && isspace(*str))
172 while(str[*len] && (level > 0 || !isspace(str[*len]))) {
174 case '{': ++level; break;
175 case '}': --level; break;
180 if(!str[*len] && level > 0)
181 NORM_ERR("tokenize: improperly nested token: %s", str);
187 llua_do_call does a flexible call to any Lua function
188 string: <function> [par1] [par2...]
189 retc: the number of return values expected
191 static char *llua_do_call(const char *string, int retc)
193 static char func[64];
198 const char *ptr = tokenize(string, &len);
200 /* proceed only if the function name is present */
205 /* call only conky_ prefixed functions */
206 if(strncmp(ptr, LUAPREFIX, strlen(LUAPREFIX)) != 0) {
207 snprintf(func, sizeof func, "%s", LUAPREFIX);
210 strncat(func, ptr, MIN(len, sizeof(func) - strlen(func) - 1));
212 /* push the function name to stack */
213 lua_getglobal(lua_L, func);
215 /* parse all function parameters from args and push them to the stack */
216 while( ptr = tokenize(ptr, &len), len) {
217 lua_pushlstring(lua_L, ptr, len);
221 if(lua_pcall(lua_L, argc, retc, 0) != 0) {
222 NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
232 * same as llua_do_call() except passes everything after func as one arg.
234 static char *llua_do_read_call(const char *function, const char *arg, int retc)
236 static char func[64];
237 snprintf(func, 64, "conky_%s", function);
239 /* push the function name to stack */
240 lua_getglobal(lua_L, func);
242 /* push function parameter to the stack */
243 lua_pushstring(lua_L, arg);
245 if (lua_pcall(lua_L, 1, retc, 0) != 0) {
246 NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
255 /* call a function with args, and return a string from it (must be free'd) */
256 static char *llua_getstring(const char *args)
261 if(!lua_L) return NULL;
263 func = llua_do_call(args, 1);
265 if (!lua_isstring(lua_L, -1)) {
266 NORM_ERR("llua_getstring: function %s didn't return a string, result discarded", func);
268 ret = strdup(lua_tostring(lua_L, -1));
277 /* call a function with args, and return a string from it (must be free'd) */
278 static char *llua_getstring_read(const char *function, const char *arg)
283 if(!lua_L) return NULL;
285 func = llua_do_read_call(function, arg, 1);
287 if(!lua_isstring(lua_L, -1)) {
288 NORM_ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
290 ret = strdup(lua_tostring(lua_L, -1));
299 /* call a function with args, and put the result in ret */
300 static int llua_getnumber(const char *args, double *ret)
306 func = llua_do_call(args, 1);
308 if(!lua_isnumber(lua_L, -1)) {
309 NORM_ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
311 *ret = lua_tonumber(lua_L, -1);
319 void llua_close(void)
321 #ifdef HAVE_SYS_INOTIFY_H
323 #endif /* HAVE_SYS_INOTIFY_H */
328 if (draw_post_hook) {
329 free(draw_post_hook);
345 #ifdef HAVE_SYS_INOTIFY_H
346 struct _lua_notify_s {
348 char name[DEFAULT_TEXT_BUFFER_SIZE];
349 struct _lua_notify_s *next;
351 static struct _lua_notify_s *lua_notifies = 0;
353 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
355 struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
356 memset(ret, 0, sizeof(struct _lua_notify_s));
357 strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
361 void llua_append_notify(const char *name)
364 struct _lua_notify_s *new_tail = 0;
366 /* empty, fresh new digs */
367 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
369 struct _lua_notify_s *tail = lua_notifies;
373 // should be @ the end now
374 new_tail = llua_notify_list_do_alloc(name);
375 tail->next = new_tail;
377 new_tail->wd = inotify_add_watch(inotify_fd,
382 void llua_rm_notifies(void)
385 struct _lua_notify_s *head = lua_notifies;
386 struct _lua_notify_s *next = 0;
387 if (!lua_notifies) return;
388 inotify_rm_watch(inotify_fd, head->wd);
389 if (head->next) next = head->next;
394 inotify_rm_watch(inotify_fd, head->wd);
400 void llua_inotify_query(int wd, int mask)
402 struct _lua_notify_s *head = lua_notifies;
403 if (mask & IN_MODIFY || mask & IN_IGNORED) {
404 /* for whatever reason, i keep getting IN_IGNORED when the file is
407 if (head->wd == wd) {
408 llua_block_notify = 1;
409 llua_load(head->name);
410 llua_block_notify = 0;
411 NORM_ERR("Lua script '%s' reloaded", head->name);
412 if (mask & IN_IGNORED) {
413 /* for some reason we get IN_IGNORED here
414 * sometimes, so we need to re-add the watch */
415 head->wd = inotify_add_watch(inotify_fd,
425 #endif /* HAVE_SYS_INOTIFY_H */
427 void llua_set_number(const char *key, double value)
429 lua_pushnumber(lua_L, value);
430 lua_setfield(lua_L, -2, key);
433 void llua_set_startup_hook(const char *args)
435 if (startup_hook) free(startup_hook);
436 startup_hook = strdup(args);
439 void llua_set_shutdown_hook(const char *args)
441 if (shutdown_hook) free(shutdown_hook);
442 shutdown_hook = strdup(args);
445 void llua_startup_hook(void)
447 if (!lua_L || !startup_hook) return;
448 llua_do_call(startup_hook, 0);
451 void llua_shutdown_hook(void)
453 if (!lua_L || !shutdown_hook) return;
454 llua_do_call(shutdown_hook, 0);
458 void llua_draw_pre_hook(void)
460 if (!lua_L || !draw_pre_hook) return;
461 llua_do_call(draw_pre_hook, 0);
464 void llua_draw_post_hook(void)
466 if (!lua_L || !draw_post_hook) return;
467 llua_do_call(draw_post_hook, 0);
470 void llua_set_draw_pre_hook(const char *args)
472 draw_pre_hook = strdup(args);
475 void llua_set_draw_post_hook(const char *args)
477 draw_post_hook = strdup(args);
481 void llua_set_userdata(const char *key, const char *type, void *value)
483 tolua_pushusertype(lua_L, value, type);
484 lua_setfield(lua_L, -2, key);
486 #endif /* LUA_EXTRAS */
488 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
493 if (output_methods & TO_X) {
495 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
496 llua_set_userdata("visual", "Visual", window.visual);
497 llua_set_userdata("display", "Display", display);
498 #endif /* LUA_EXTRAS */
501 llua_set_number("width", window.width);
502 llua_set_number("height", window.height);
503 llua_set_number("border_inner_margin", window.border_inner_margin);
504 llua_set_number("border_outer_margin", window.border_outer_margin);
505 llua_set_number("border_width", window.border_width);
507 llua_set_number("text_start_x", text_start_x);
508 llua_set_number("text_start_y", text_start_y);
509 llua_set_number("text_width", text_width);
510 llua_set_number("text_height", text_height);
512 lua_setglobal(lua_L, "conky_window");
516 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
520 lua_getglobal(lua_L, "conky_window");
521 if (lua_isnil(lua_L, -1)) {
522 /* window table isn't populated yet */
527 llua_set_number("width", window.width);
528 llua_set_number("height", window.height);
530 llua_set_number("text_start_x", text_start_x);
531 llua_set_number("text_start_y", text_start_y);
532 llua_set_number("text_width", text_width);
533 llua_set_number("text_height", text_height);
535 lua_setglobal(lua_L, "conky_window");
539 void llua_setup_info(struct information *i, double u_interval)
544 llua_set_number("update_interval", u_interval);
545 llua_set_number("uptime", i->uptime);
547 lua_setglobal(lua_L, "conky_info");
550 void llua_update_info(struct information *i, double u_interval)
554 lua_getglobal(lua_L, "conky_info");
555 if (lua_isnil(lua_L, -1)) {
556 /* window table isn't populated yet */
561 llua_set_number("update_interval", u_interval);
562 llua_set_number("uptime", i->uptime);
564 lua_setglobal(lua_L, "conky_info");
567 void print_lua(struct text_object *obj, char *p, int p_max_size)
569 char *str = llua_getstring(obj->data.s);
571 snprintf(p, p_max_size, "%s", str);
576 void print_lua_parse(struct text_object *obj, char *p, int p_max_size)
578 char *str = llua_getstring(obj->data.s);
580 evaluate(str, p, p_max_size);
585 void print_lua_bar(struct text_object *obj, char *p, int p_max_size)
588 if (llua_getnumber(obj->data.s, &per)) {
589 new_bar(obj, p, p_max_size, (per/100.0 * 255));
594 void print_lua_graph(struct text_object *obj, char *p, int p_max_size)
601 if (llua_getnumber(obj->data.s, &per)) {
602 new_graph(obj, p, p_max_size, per);
607 void print_lua_gauge(struct text_object *obj, char *p, int p_max_size)
614 if (llua_getnumber(obj->data.s, &per)) {
615 new_gauge(obj, p, p_max_size, (per/100.0 * 255));