1 /* Conky, a system monitor, based on torsmo
3 * Copyright (c) 2009 Toni Spets
4 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #ifdef HAVE_SYS_INOTIFY_H
27 #include <sys/inotify.h>
29 void llua_append_notify(const char *name);
30 void llua_rm_notifies(void);
31 static int llua_block_notify = 0;
32 #endif /* HAVE_SYS_INOTIFY_H */
34 static char *draw_pre = 0;
35 static char *draw_post = 0;
37 lua_State *lua_L = NULL;
39 static int llua_conky_parse(lua_State *L)
41 int n = lua_gettop(L); /* number of arguments */
43 char *buf = calloc(1, max_user_text);
45 lua_pushstring(L, "incorrect arguments, conky_parse(string) takes exactly 1 argument");
48 if (!lua_isstring(L, 1)) {
49 lua_pushstring(L, "incorrect argument (expecting a string)");
52 str = strdup(lua_tostring(L, 1));
54 lua_pushstring(L, buf);
57 return 1; /* number of results */
62 const char *libs = PACKAGE_LIBDIR"/lib?.so;";
63 char *old_path, *new_path;
67 /* add our library path to the lua package.cpath global var */
69 lua_getglobal(lua_L, "package");
70 lua_getfield(lua_L, -1, "cpath");
71 old_path = strdup(lua_tostring(lua_L, -1));
72 new_path = malloc(strlen(old_path) + strlen(libs) + 1);
73 strcpy(new_path, libs);
74 strcat(new_path, old_path);
75 lua_pushstring(lua_L, new_path);
76 lua_setfield(lua_L, -3, "cpath");
81 lua_pushstring(lua_L, PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH);
82 lua_setglobal(lua_L, "conky_build_info");
84 lua_pushstring(lua_L, VERSION);
85 lua_setglobal(lua_L, "conky_version");
87 lua_pushstring(lua_L, current_config);
88 lua_setglobal(lua_L, "conky_config");
90 lua_pushcfunction(lua_L, &llua_conky_parse);
91 lua_setglobal(lua_L, "conky_parse");
94 void llua_load(const char *script)
97 char path[DEFAULT_TEXT_BUFFER_SIZE];
101 to_real_path(path, script);
102 error = luaL_dofile(lua_L, path);
104 ERR("llua_load: %s", lua_tostring(lua_L, -1));
106 #ifdef HAVE_SYS_INOTIFY_H
107 } else if (!llua_block_notify && inotify_fd != -1) {
108 llua_append_notify(path);
109 #endif /* HAVE_SYS_INOTIFY_H */
114 llua_do_call does a flexible call to any Lua function
115 string: <function> [par1] [par2...]
116 retc: the number of return values expected
118 char *llua_do_call(const char *string, int retc)
120 static char func[64];
123 char *tmp = strdup(string);
124 char *ptr = strtok(tmp, " ");
126 /* proceed only if the function name is present */
132 /* call only conky_ prefixed functions */
133 snprintf(func, 64, "conky_%s", ptr);
135 /* push the function name to stack */
136 lua_getglobal(lua_L, func);
138 /* parse all function parameters from args and push them to the stack */
139 ptr = strtok(NULL, " ");
141 lua_pushstring(lua_L, ptr);
142 ptr = strtok(NULL, " ");
148 if(lua_pcall(lua_L, argc, retc, 0) != 0) {
149 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
158 * same as llua_do_call() except passes everything after func as one arg.
160 char *llua_do_read_call(const char *function, const char *arg, int retc)
162 static char func[64];
163 snprintf(func, 64, "conky_%s", function);
165 /* push the function name to stack */
166 lua_getglobal(lua_L, func);
168 /* push function parameter to the stack */
169 lua_pushstring(lua_L, arg);
171 if (lua_pcall(lua_L, 1, retc, 0) != 0) {
172 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
180 char *llua_getstring(const char *args)
185 if(!lua_L) return NULL;
187 func = llua_do_call(args, 1);
189 if (!lua_isstring(lua_L, -1)) {
190 ERR("llua_getstring: function %s didn't return a string, result discarded", func);
192 ret = strdup(lua_tostring(lua_L, -1));
200 char *llua_getstring_read(const char *function, const char *arg)
205 if(!lua_L) return NULL;
207 func = llua_do_read_call(function, arg, 1);
209 if(!lua_isstring(lua_L, -1)) {
210 ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
212 ret = strdup(lua_tostring(lua_L, -1));
220 int llua_getnumber(const char *args, double *ret)
226 func = llua_do_call(args, 1);
228 if(!lua_isnumber(lua_L, -1)) {
229 ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
231 *ret = lua_tonumber(lua_L, -1);
239 void llua_close(void)
241 #ifdef HAVE_SYS_INOTIFY_H
243 #endif /* HAVE_SYS_INOTIFY_H */
257 #ifdef HAVE_SYS_INOTIFY_H
258 struct _lua_notify_s {
260 char name[DEFAULT_TEXT_BUFFER_SIZE];
261 struct _lua_notify_s *next;
263 static struct _lua_notify_s *lua_notifies = 0;
265 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
267 struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
268 memset(ret, 0, sizeof(struct _lua_notify_s));
269 strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
273 void llua_append_notify(const char *name)
276 struct _lua_notify_s *new_tail = 0;
278 /* empty, fresh new digs */
279 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
281 struct _lua_notify_s *tail = lua_notifies;
285 // should be @ the end now
286 new_tail = llua_notify_list_do_alloc(name);
287 tail->next = new_tail;
289 new_tail->wd = inotify_add_watch(inotify_fd,
294 void llua_rm_notifies(void)
297 struct _lua_notify_s *head = lua_notifies;
298 struct _lua_notify_s *next = 0;
299 if (!lua_notifies) return;
300 inotify_rm_watch(inotify_fd, head->wd);
301 if (head->next) next = head->next;
306 inotify_rm_watch(inotify_fd, head->wd);
312 void llua_inotify_query(int wd, int mask)
314 struct _lua_notify_s *head = lua_notifies;
315 if (mask & IN_MODIFY || mask & IN_IGNORED) {
316 /* for whatever reason, i keep getting IN_IGNORED when the file is
319 if (head->wd == wd) {
320 llua_block_notify = 1;
321 llua_load(head->name);
322 llua_block_notify = 0;
323 ERR("Lua script '%s' reloaded", head->name);
324 if (mask & IN_IGNORED) {
325 /* for some reason we get IN_IGNORED here
326 * sometimes, so we need to re-add the watch */
327 head->wd = inotify_add_watch(inotify_fd,
337 #endif /* HAVE_SYS_INOTIFY_H */
340 void llua_draw_pre_hook(void)
342 if (!lua_L || !draw_pre) return;
343 llua_do_call(draw_pre, 0);
346 void llua_draw_post_hook(void)
348 if (!lua_L || !draw_post) return;
349 llua_do_call(draw_post, 0);
352 void llua_set_draw_pre_hook(const char *args)
354 draw_pre = strdup(args);
357 void llua_set_draw_post_hook(const char *args)
359 draw_post = strdup(args);
362 void llua_set_long(const char *key, long value)
364 lua_pushnumber(lua_L, value);
365 lua_setfield(lua_L, -2, key);
368 /* this function mostly copied from tolua++ source so that we could play nice
369 * with tolua++ libs. tolua++ is provided 'as is'
371 void llua_set_userdata(const char *key, const char *type, void *value)
376 luaL_getmetatable(lua_L, type);
377 lua_pushstring(lua_L,"tolua_ubox");
378 lua_rawget(lua_L,-2); /* stack: mt ubox */
379 if (lua_isnil(lua_L, -1)) {
381 lua_pushstring(lua_L, "tolua_ubox");
382 lua_rawget(lua_L, LUA_REGISTRYINDEX);
384 lua_pushlightuserdata(lua_L,value);
385 lua_rawget(lua_L,-2); /* stack: mt ubox ubox[u] */
386 if (lua_isnil(lua_L,-1)) {
387 lua_pop(lua_L,1); /* stack: mt ubox */
388 lua_pushlightuserdata(lua_L,value);
389 *(void**)lua_newuserdata(lua_L,sizeof(void *)) = value; /* stack: mt ubox u newud */
390 lua_pushvalue(lua_L,-1); /* stack: mt ubox u newud newud */
391 lua_insert(lua_L,-4); /* stack: mt newud ubox u newud */
392 lua_rawset(lua_L,-3); /* stack: mt newud ubox */
393 lua_pop(lua_L,1); /* stack: mt newud */
394 /*luaL_getmetatable(lua_L,type);*/
395 lua_pushvalue(lua_L, -2); /* stack: mt newud mt */
396 lua_setmetatable(lua_L,-2); /* stack: mt newud */
399 /* check the need of updating the metatable to a more specialized class */
400 lua_insert(lua_L,-2); /* stack: mt ubox[u] ubox */
401 lua_pop(lua_L,1); /* stack: mt ubox[u] */
402 lua_pushstring(lua_L,"tolua_super");
403 lua_rawget(lua_L,LUA_REGISTRYINDEX); /* stack: mt ubox[u] super */
404 lua_getmetatable(lua_L,-2); /* stack: mt ubox[u] super mt */
405 lua_rawget(lua_L,-2); /* stack: mt ubox[u] super super[mt] */
406 if (lua_istable(lua_L,-1)) {
407 lua_pushstring(lua_L,type); /* stack: mt ubox[u] super super[mt] type */
408 lua_rawget(lua_L,-2); /* stack: mt ubox[u] super super[mt] flag */
409 if (lua_toboolean(lua_L,-1) == 1) {
411 lua_pop(lua_L,3); /* mt ubox[u]*/
412 lua_remove(lua_L, -2);
416 /* type represents a more specilized type */
417 /*luaL_getmetatable(lua_L,type); // stack: mt ubox[u] super super[mt] flag mt */
418 lua_pushvalue(lua_L, -5); /* stack: mt ubox[u] super super[mt] flag mt */
419 lua_setmetatable(lua_L,-5); /* stack: mt ubox[u] super super[mt] flag */
420 lua_pop(lua_L,3); /* stack: mt ubox[u] */
422 lua_remove(lua_L, -2); /* stack: ubox[u]*/
424 lua_setfield(lua_L, -2, key);
427 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
431 /* TODO fix this: (segfaults)
432 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
433 llua_set_userdata("visual", "Visual", window.visual);
434 llua_set_userdata("display", "Display", display);
437 llua_set_long("width", window.width);
438 llua_set_long("height", window.height);
439 llua_set_long("border_inner_margin", window.border_inner_margin);
440 llua_set_long("border_outer_margin", window.border_outer_margin);
441 llua_set_long("border_width", window.border_width);
443 llua_set_long("text_start_x", text_start_x);
444 llua_set_long("text_start_y", text_start_y);
445 llua_set_long("text_width", text_width);
446 llua_set_long("text_height", text_height);
448 lua_setglobal(lua_L, "conky_window");
451 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
453 llua_init(); //needed because sometimes lua isn't initialised resulting in segfaults
454 lua_getglobal(lua_L, "conky_window");
455 if (lua_isnil(lua_L, -1)) {
456 /* window table isn't populated yet */
460 llua_set_long("width", window.width);
461 llua_set_long("height", window.height);
463 llua_set_long("text_start_x", text_start_x);
464 llua_set_long("text_start_y", text_start_y);
465 llua_set_long("text_width", text_width);
466 llua_set_long("text_height", text_height);
468 lua_setglobal(lua_L, "conky_window");