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, BUILD_DATE);
88 lua_setglobal(lua_L, "conky_build_date");
90 lua_pushstring(lua_L, BUILD_ARCH);
91 lua_setglobal(lua_L, "conky_build_arch");
93 lua_pushstring(lua_L, current_config);
94 lua_setglobal(lua_L, "conky_config");
96 lua_pushcfunction(lua_L, &llua_conky_parse);
97 lua_setglobal(lua_L, "conky_parse");
100 void llua_load(const char *script)
103 char path[DEFAULT_TEXT_BUFFER_SIZE];
107 to_real_path(path, script);
108 error = luaL_dofile(lua_L, path);
110 ERR("llua_load: %s", lua_tostring(lua_L, -1));
112 #ifdef HAVE_SYS_INOTIFY_H
113 } else if (!llua_block_notify && inotify_fd != -1) {
114 llua_append_notify(path);
115 #endif /* HAVE_SYS_INOTIFY_H */
120 llua_do_call does a flexible call to any Lua function
121 string: <function> [par1] [par2...]
122 retc: the number of return values expected
124 char *llua_do_call(const char *string, int retc)
126 static char func[64];
129 char *tmp = strdup(string);
130 char *ptr = strtok(tmp, " ");
132 /* proceed only if the function name is present */
138 /* call only conky_ prefixed functions */
139 snprintf(func, 64, "conky_%s", ptr);
141 /* push the function name to stack */
142 lua_getglobal(lua_L, func);
144 /* parse all function parameters from args and push them to the stack */
145 ptr = strtok(NULL, " ");
147 lua_pushstring(lua_L, ptr);
148 ptr = strtok(NULL, " ");
154 if(lua_pcall(lua_L, argc, retc, 0) != 0) {
155 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
164 * same as llua_do_call() except passes everything after func as one arg.
166 char *llua_do_read_call(const char *function, const char *arg, int retc)
168 static char func[64];
169 snprintf(func, 64, "conky_%s", function);
171 /* push the function name to stack */
172 lua_getglobal(lua_L, func);
174 /* push function parameter to the stack */
175 lua_pushstring(lua_L, arg);
177 if (lua_pcall(lua_L, 1, retc, 0) != 0) {
178 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
186 char *llua_getstring(const char *args)
191 if(!lua_L) return NULL;
193 func = llua_do_call(args, 1);
195 if (!lua_isstring(lua_L, -1)) {
196 ERR("llua_getstring: function %s didn't return a string, result discarded", func);
198 ret = strdup(lua_tostring(lua_L, -1));
206 char *llua_getstring_read(const char *function, const char *arg)
211 if(!lua_L) return NULL;
213 func = llua_do_read_call(function, arg, 1);
215 if(!lua_isstring(lua_L, -1)) {
216 ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
218 ret = strdup(lua_tostring(lua_L, -1));
226 int llua_getnumber(const char *args, double *ret)
232 func = llua_do_call(args, 1);
234 if(!lua_isnumber(lua_L, -1)) {
235 ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
237 *ret = lua_tonumber(lua_L, -1);
245 void llua_close(void)
247 #ifdef HAVE_SYS_INOTIFY_H
249 #endif /* HAVE_SYS_INOTIFY_H */
263 #ifdef HAVE_SYS_INOTIFY_H
264 struct _lua_notify_s {
266 char name[DEFAULT_TEXT_BUFFER_SIZE];
267 struct _lua_notify_s *next;
269 static struct _lua_notify_s *lua_notifies = 0;
271 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
273 struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
274 memset(ret, 0, sizeof(struct _lua_notify_s));
275 strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
279 void llua_append_notify(const char *name)
282 struct _lua_notify_s *new_tail = 0;
284 /* empty, fresh new digs */
285 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
287 struct _lua_notify_s *tail = lua_notifies;
291 // should be @ the end now
292 new_tail = llua_notify_list_do_alloc(name);
293 tail->next = new_tail;
295 new_tail->wd = inotify_add_watch(inotify_fd,
300 void llua_rm_notifies(void)
303 struct _lua_notify_s *head = lua_notifies;
304 struct _lua_notify_s *next = 0;
305 if (!lua_notifies) return;
306 inotify_rm_watch(inotify_fd, head->wd);
307 if (head->next) next = head->next;
312 inotify_rm_watch(inotify_fd, head->wd);
318 void llua_inotify_query(int wd, int mask)
320 struct _lua_notify_s *head = lua_notifies;
321 if (mask & IN_MODIFY || mask & IN_IGNORED) {
322 /* for whatever reason, i keep getting IN_IGNORED when the file is
325 if (head->wd == wd) {
326 llua_block_notify = 1;
327 llua_load(head->name);
328 llua_block_notify = 0;
329 ERR("Lua script '%s' reloaded", head->name);
330 if (mask & IN_IGNORED) {
331 /* for some reason we get IN_IGNORED here
332 * sometimes, so we need to re-add the watch */
333 head->wd = inotify_add_watch(inotify_fd,
343 #endif /* HAVE_SYS_INOTIFY_H */
346 void llua_draw_pre_hook(void)
348 if (!lua_L || !draw_pre) return;
349 llua_do_call(draw_pre, 0);
352 void llua_draw_post_hook(void)
354 if (!lua_L || !draw_post) return;
355 llua_do_call(draw_post, 0);
358 void llua_set_draw_pre_hook(const char *args)
360 draw_pre = strdup(args);
363 void llua_set_draw_post_hook(const char *args)
365 draw_post = strdup(args);
368 void llua_set_long(const char *key, long value)
370 lua_pushnumber(lua_L, value);
371 lua_setfield(lua_L, -2, key);
374 /* this function mostly copied from tolua++ source so that we could play nice
375 * with tolua++ libs. tolua++ is provided 'as is'
377 void llua_set_userdata(const char *key, const char *type, void *value)
382 luaL_getmetatable(lua_L, type);
383 lua_pushstring(lua_L,"tolua_ubox");
384 lua_rawget(lua_L,-2); /* stack: mt ubox */
385 if (lua_isnil(lua_L, -1)) {
387 lua_pushstring(lua_L, "tolua_ubox");
388 lua_rawget(lua_L, LUA_REGISTRYINDEX);
390 lua_pushlightuserdata(lua_L,value);
391 lua_rawget(lua_L,-2); /* stack: mt ubox ubox[u] */
392 if (lua_isnil(lua_L,-1)) {
393 lua_pop(lua_L,1); /* stack: mt ubox */
394 lua_pushlightuserdata(lua_L,value);
395 *(void**)lua_newuserdata(lua_L,sizeof(void *)) = value; /* stack: mt ubox u newud */
396 lua_pushvalue(lua_L,-1); /* stack: mt ubox u newud newud */
397 lua_insert(lua_L,-4); /* stack: mt newud ubox u newud */
398 lua_rawset(lua_L,-3); /* stack: mt newud ubox */
399 lua_pop(lua_L,1); /* stack: mt newud */
400 /*luaL_getmetatable(lua_L,type);*/
401 lua_pushvalue(lua_L, -2); /* stack: mt newud mt */
402 lua_setmetatable(lua_L,-2); /* stack: mt newud */
405 /* check the need of updating the metatable to a more specialized class */
406 lua_insert(lua_L,-2); /* stack: mt ubox[u] ubox */
407 lua_pop(lua_L,1); /* stack: mt ubox[u] */
408 lua_pushstring(lua_L,"tolua_super");
409 lua_rawget(lua_L,LUA_REGISTRYINDEX); /* stack: mt ubox[u] super */
410 lua_getmetatable(lua_L,-2); /* stack: mt ubox[u] super mt */
411 lua_rawget(lua_L,-2); /* stack: mt ubox[u] super super[mt] */
412 if (lua_istable(lua_L,-1)) {
413 lua_pushstring(lua_L,type); /* stack: mt ubox[u] super super[mt] type */
414 lua_rawget(lua_L,-2); /* stack: mt ubox[u] super super[mt] flag */
415 if (lua_toboolean(lua_L,-1) == 1) {
417 lua_pop(lua_L,3); /* mt ubox[u]*/
418 lua_remove(lua_L, -2);
422 /* type represents a more specilized type */
423 /*luaL_getmetatable(lua_L,type); // stack: mt ubox[u] super super[mt] flag mt */
424 lua_pushvalue(lua_L, -5); /* stack: mt ubox[u] super super[mt] flag mt */
425 lua_setmetatable(lua_L,-5); /* stack: mt ubox[u] super super[mt] flag */
426 lua_pop(lua_L,3); /* stack: mt ubox[u] */
428 lua_remove(lua_L, -2); /* stack: ubox[u]*/
430 lua_setfield(lua_L, -2, key);
433 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
438 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
439 llua_set_userdata("visual", "Visual", window.visual);
440 llua_set_userdata("display", "Display", display);
443 llua_set_long("width", window.width);
444 llua_set_long("height", window.height);
445 llua_set_long("border_inner_margin", window.border_inner_margin);
446 llua_set_long("border_outer_margin", window.border_outer_margin);
447 llua_set_long("border_width", window.border_width);
449 llua_set_long("text_start_x", text_start_x);
450 llua_set_long("text_start_y", text_start_y);
451 llua_set_long("text_width", text_width);
452 llua_set_long("text_height", text_height);
454 lua_setglobal(lua_L, "conky_window");
457 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
461 lua_getglobal(lua_L, "conky_window");
462 if (lua_isnil(lua_L, -1)) {
463 /* window table isn't populated yet */
467 llua_set_long("width", window.width);
468 llua_set_long("height", window.height);
470 llua_set_long("text_start_x", text_start_x);
471 llua_set_long("text_start_y", text_start_y);
472 llua_set_long("text_width", text_width);
473 llua_set_long("text_height", text_height);
475 lua_setglobal(lua_L, "conky_window");