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/>.
20 * vim: ts=4 sw=4 noet ai cindent syntax=c
30 #endif /* LUA_EXTRAS */
32 #ifdef HAVE_SYS_INOTIFY_H
33 #include <sys/inotify.h>
35 void llua_append_notify(const char *name);
36 void llua_rm_notifies(void);
37 static int llua_block_notify = 0;
38 #endif /* HAVE_SYS_INOTIFY_H */
40 static char *draw_pre = 0;
41 static char *draw_post = 0;
43 lua_State *lua_L = NULL;
45 static int llua_conky_parse(lua_State *L)
47 int n = lua_gettop(L); /* number of arguments */
49 char *buf = calloc(1, max_user_text);
51 lua_pushstring(L, "incorrect arguments, conky_parse(string) takes exactly 1 argument");
54 if (!lua_isstring(L, 1)) {
55 lua_pushstring(L, "incorrect argument (expecting a string)");
58 str = strdup(lua_tostring(L, 1));
60 lua_pushstring(L, buf);
63 return 1; /* number of results */
68 const char *libs = PACKAGE_LIBDIR"/lib?.so;";
69 char *old_path, *new_path;
73 /* add our library path to the lua package.cpath global var */
75 lua_getglobal(lua_L, "package");
76 lua_getfield(lua_L, -1, "cpath");
77 old_path = strdup(lua_tostring(lua_L, -1));
78 new_path = malloc(strlen(old_path) + strlen(libs) + 1);
79 strcpy(new_path, libs);
80 strcat(new_path, old_path);
81 lua_pushstring(lua_L, new_path);
82 lua_setfield(lua_L, -3, "cpath");
87 lua_pushstring(lua_L, PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH);
88 lua_setglobal(lua_L, "conky_build_info");
90 lua_pushstring(lua_L, VERSION);
91 lua_setglobal(lua_L, "conky_version");
93 lua_pushstring(lua_L, BUILD_DATE);
94 lua_setglobal(lua_L, "conky_build_date");
96 lua_pushstring(lua_L, BUILD_ARCH);
97 lua_setglobal(lua_L, "conky_build_arch");
99 lua_pushstring(lua_L, current_config);
100 lua_setglobal(lua_L, "conky_config");
102 lua_pushcfunction(lua_L, &llua_conky_parse);
103 lua_setglobal(lua_L, "conky_parse");
105 #if defined(X11) && defined(LUA_EXTRAS)
106 /* register tolua++ user types */
108 tolua_usertype(lua_L, "Drawable");
109 tolua_usertype(lua_L, "Visual");
110 tolua_usertype(lua_L, "Display");
114 void llua_load(const char *script)
117 char path[DEFAULT_TEXT_BUFFER_SIZE];
121 to_real_path(path, script);
122 error = luaL_dofile(lua_L, path);
124 ERR("llua_load: %s", lua_tostring(lua_L, -1));
126 #ifdef HAVE_SYS_INOTIFY_H
127 } else if (!llua_block_notify && inotify_fd != -1) {
128 llua_append_notify(path);
129 #endif /* HAVE_SYS_INOTIFY_H */
134 llua_do_call does a flexible call to any Lua function
135 string: <function> [par1] [par2...]
136 retc: the number of return values expected
138 char *llua_do_call(const char *string, int retc)
140 static char func[64];
143 char *tmp = strdup(string);
144 char *ptr = strtok(tmp, " ");
146 /* proceed only if the function name is present */
152 /* call only conky_ prefixed functions */
153 if(strncmp(ptr, LUAPREFIX, strlen(LUAPREFIX)) == 0) {
154 snprintf(func, 64, "%s", ptr);
156 snprintf(func, 64, "%s%s", LUAPREFIX, ptr);
159 /* push the function name to stack */
160 lua_getglobal(lua_L, func);
162 /* parse all function parameters from args and push them to the stack */
163 ptr = strtok(NULL, " ");
165 lua_pushstring(lua_L, ptr);
166 ptr = strtok(NULL, " ");
172 if(lua_pcall(lua_L, argc, retc, 0) != 0) {
173 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
182 * same as llua_do_call() except passes everything after func as one arg.
184 char *llua_do_read_call(const char *function, const char *arg, int retc)
186 static char func[64];
187 snprintf(func, 64, "conky_%s", function);
189 /* push the function name to stack */
190 lua_getglobal(lua_L, func);
192 /* push function parameter to the stack */
193 lua_pushstring(lua_L, arg);
195 if (lua_pcall(lua_L, 1, retc, 0) != 0) {
196 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
204 char *llua_getstring(const char *args)
209 if(!lua_L) return NULL;
211 func = llua_do_call(args, 1);
213 if (!lua_isstring(lua_L, -1)) {
214 ERR("llua_getstring: function %s didn't return a string, result discarded", func);
216 ret = strdup(lua_tostring(lua_L, -1));
224 char *llua_getstring_read(const char *function, const char *arg)
229 if(!lua_L) return NULL;
231 func = llua_do_read_call(function, arg, 1);
233 if(!lua_isstring(lua_L, -1)) {
234 ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
236 ret = strdup(lua_tostring(lua_L, -1));
244 int llua_getnumber(const char *args, double *ret)
250 func = llua_do_call(args, 1);
252 if(!lua_isnumber(lua_L, -1)) {
253 ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
255 *ret = lua_tonumber(lua_L, -1);
263 void llua_close(void)
265 #ifdef HAVE_SYS_INOTIFY_H
267 #endif /* HAVE_SYS_INOTIFY_H */
281 #ifdef HAVE_SYS_INOTIFY_H
282 struct _lua_notify_s {
284 char name[DEFAULT_TEXT_BUFFER_SIZE];
285 struct _lua_notify_s *next;
287 static struct _lua_notify_s *lua_notifies = 0;
289 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
291 struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
292 memset(ret, 0, sizeof(struct _lua_notify_s));
293 strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
297 void llua_append_notify(const char *name)
300 struct _lua_notify_s *new_tail = 0;
302 /* empty, fresh new digs */
303 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
305 struct _lua_notify_s *tail = lua_notifies;
309 // should be @ the end now
310 new_tail = llua_notify_list_do_alloc(name);
311 tail->next = new_tail;
313 new_tail->wd = inotify_add_watch(inotify_fd,
318 void llua_rm_notifies(void)
321 struct _lua_notify_s *head = lua_notifies;
322 struct _lua_notify_s *next = 0;
323 if (!lua_notifies) return;
324 inotify_rm_watch(inotify_fd, head->wd);
325 if (head->next) next = head->next;
330 inotify_rm_watch(inotify_fd, head->wd);
336 void llua_inotify_query(int wd, int mask)
338 struct _lua_notify_s *head = lua_notifies;
339 if (mask & IN_MODIFY || mask & IN_IGNORED) {
340 /* for whatever reason, i keep getting IN_IGNORED when the file is
343 if (head->wd == wd) {
344 llua_block_notify = 1;
345 llua_load(head->name);
346 llua_block_notify = 0;
347 ERR("Lua script '%s' reloaded", head->name);
348 if (mask & IN_IGNORED) {
349 /* for some reason we get IN_IGNORED here
350 * sometimes, so we need to re-add the watch */
351 head->wd = inotify_add_watch(inotify_fd,
361 #endif /* HAVE_SYS_INOTIFY_H */
364 void llua_draw_pre_hook(void)
366 if (!lua_L || !draw_pre) return;
367 llua_do_call(draw_pre, 0);
370 void llua_draw_post_hook(void)
372 if (!lua_L || !draw_post) return;
373 llua_do_call(draw_post, 0);
376 void llua_set_draw_pre_hook(const char *args)
378 draw_pre = strdup(args);
381 void llua_set_draw_post_hook(const char *args)
383 draw_post = strdup(args);
386 void llua_set_long(const char *key, long value)
388 lua_pushnumber(lua_L, value);
389 lua_setfield(lua_L, -2, key);
393 void llua_set_userdata(const char *key, const char *type, void *value)
395 tolua_pushusertype(lua_L, value, type);
396 lua_setfield(lua_L, -2, key);
398 #endif /* LUA_EXTRAS */
400 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
405 if (output_methods & TO_X) {
407 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
408 llua_set_userdata("visual", "Visual", window.visual);
409 llua_set_userdata("display", "Display", display);
410 #endif /* LUA_EXTRAS */
413 llua_set_long("width", window.width);
414 llua_set_long("height", window.height);
415 llua_set_long("border_inner_margin", window.border_inner_margin);
416 llua_set_long("border_outer_margin", window.border_outer_margin);
417 llua_set_long("border_width", window.border_width);
419 llua_set_long("text_start_x", text_start_x);
420 llua_set_long("text_start_y", text_start_y);
421 llua_set_long("text_width", text_width);
422 llua_set_long("text_height", text_height);
424 lua_setglobal(lua_L, "conky_window");
428 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
432 lua_getglobal(lua_L, "conky_window");
433 if (lua_isnil(lua_L, -1)) {
434 /* window table isn't populated yet */
439 llua_set_long("width", window.width);
440 llua_set_long("height", window.height);
442 llua_set_long("text_start_x", text_start_x);
443 llua_set_long("text_start_y", text_start_y);
444 llua_set_long("text_width", text_width);
445 llua_set_long("text_height", text_height);
447 lua_setglobal(lua_L, "conky_window");