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/>.
28 #endif /* LUA_EXTRAS */
30 #ifdef HAVE_SYS_INOTIFY_H
31 #include <sys/inotify.h>
33 void llua_append_notify(const char *name);
34 void llua_rm_notifies(void);
35 static int llua_block_notify = 0;
36 #endif /* HAVE_SYS_INOTIFY_H */
38 static char *draw_pre = 0;
39 static char *draw_post = 0;
41 lua_State *lua_L = NULL;
43 static int llua_conky_parse(lua_State *L)
45 int n = lua_gettop(L); /* number of arguments */
47 char *buf = calloc(1, max_user_text);
49 lua_pushstring(L, "incorrect arguments, conky_parse(string) takes exactly 1 argument");
52 if (!lua_isstring(L, 1)) {
53 lua_pushstring(L, "incorrect argument (expecting a string)");
56 str = strdup(lua_tostring(L, 1));
58 lua_pushstring(L, buf);
61 return 1; /* number of results */
66 const char *libs = PACKAGE_LIBDIR"/lib?.so;";
67 char *old_path, *new_path;
71 /* add our library path to the lua package.cpath global var */
73 lua_getglobal(lua_L, "package");
74 lua_getfield(lua_L, -1, "cpath");
75 old_path = strdup(lua_tostring(lua_L, -1));
76 new_path = malloc(strlen(old_path) + strlen(libs) + 1);
77 strcpy(new_path, libs);
78 strcat(new_path, old_path);
79 lua_pushstring(lua_L, new_path);
80 lua_setfield(lua_L, -3, "cpath");
85 lua_pushstring(lua_L, PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH);
86 lua_setglobal(lua_L, "conky_build_info");
88 lua_pushstring(lua_L, VERSION);
89 lua_setglobal(lua_L, "conky_version");
91 lua_pushstring(lua_L, BUILD_DATE);
92 lua_setglobal(lua_L, "conky_build_date");
94 lua_pushstring(lua_L, BUILD_ARCH);
95 lua_setglobal(lua_L, "conky_build_arch");
97 lua_pushstring(lua_L, current_config);
98 lua_setglobal(lua_L, "conky_config");
100 lua_pushcfunction(lua_L, &llua_conky_parse);
101 lua_setglobal(lua_L, "conky_parse");
103 #if defined(X11) && defined(LUA_EXTRAS)
104 /* register tolua++ user types */
106 tolua_usertype(lua_L, "Drawable");
107 tolua_usertype(lua_L, "Visual");
108 tolua_usertype(lua_L, "Display");
112 void llua_load(const char *script)
115 char path[DEFAULT_TEXT_BUFFER_SIZE];
119 to_real_path(path, script);
120 error = luaL_dofile(lua_L, path);
122 ERR("llua_load: %s", lua_tostring(lua_L, -1));
124 #ifdef HAVE_SYS_INOTIFY_H
125 } else if (!llua_block_notify && inotify_fd != -1) {
126 llua_append_notify(path);
127 #endif /* HAVE_SYS_INOTIFY_H */
132 llua_do_call does a flexible call to any Lua function
133 string: <function> [par1] [par2...]
134 retc: the number of return values expected
136 char *llua_do_call(const char *string, int retc)
138 static char func[64];
141 char *tmp = strdup(string);
142 char *ptr = strtok(tmp, " ");
144 /* proceed only if the function name is present */
150 /* call only conky_ prefixed functions */
151 if(strncmp(ptr, LUAPREFIX, strlen(LUAPREFIX)) == 0) {
152 snprintf(func, 64, "%s", ptr);
154 snprintf(func, 64, "%s%s", LUAPREFIX, ptr);
157 /* push the function name to stack */
158 lua_getglobal(lua_L, func);
160 /* parse all function parameters from args and push them to the stack */
161 ptr = strtok(NULL, " ");
163 lua_pushstring(lua_L, ptr);
164 ptr = strtok(NULL, " ");
170 if(lua_pcall(lua_L, argc, retc, 0) != 0) {
171 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
180 * same as llua_do_call() except passes everything after func as one arg.
182 char *llua_do_read_call(const char *function, const char *arg, int retc)
184 static char func[64];
185 snprintf(func, 64, "conky_%s", function);
187 /* push the function name to stack */
188 lua_getglobal(lua_L, func);
190 /* push function parameter to the stack */
191 lua_pushstring(lua_L, arg);
193 if (lua_pcall(lua_L, 1, retc, 0) != 0) {
194 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
202 char *llua_getstring(const char *args)
207 if(!lua_L) return NULL;
209 func = llua_do_call(args, 1);
211 if (!lua_isstring(lua_L, -1)) {
212 ERR("llua_getstring: function %s didn't return a string, result discarded", func);
214 ret = strdup(lua_tostring(lua_L, -1));
222 char *llua_getstring_read(const char *function, const char *arg)
227 if(!lua_L) return NULL;
229 func = llua_do_read_call(function, arg, 1);
231 if(!lua_isstring(lua_L, -1)) {
232 ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
234 ret = strdup(lua_tostring(lua_L, -1));
242 int llua_getnumber(const char *args, double *ret)
248 func = llua_do_call(args, 1);
250 if(!lua_isnumber(lua_L, -1)) {
251 ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
253 *ret = lua_tonumber(lua_L, -1);
261 void llua_close(void)
263 #ifdef HAVE_SYS_INOTIFY_H
265 #endif /* HAVE_SYS_INOTIFY_H */
279 #ifdef HAVE_SYS_INOTIFY_H
280 struct _lua_notify_s {
282 char name[DEFAULT_TEXT_BUFFER_SIZE];
283 struct _lua_notify_s *next;
285 static struct _lua_notify_s *lua_notifies = 0;
287 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
289 struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
290 memset(ret, 0, sizeof(struct _lua_notify_s));
291 strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
295 void llua_append_notify(const char *name)
298 struct _lua_notify_s *new_tail = 0;
300 /* empty, fresh new digs */
301 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
303 struct _lua_notify_s *tail = lua_notifies;
307 // should be @ the end now
308 new_tail = llua_notify_list_do_alloc(name);
309 tail->next = new_tail;
311 new_tail->wd = inotify_add_watch(inotify_fd,
316 void llua_rm_notifies(void)
319 struct _lua_notify_s *head = lua_notifies;
320 struct _lua_notify_s *next = 0;
321 if (!lua_notifies) return;
322 inotify_rm_watch(inotify_fd, head->wd);
323 if (head->next) next = head->next;
328 inotify_rm_watch(inotify_fd, head->wd);
334 void llua_inotify_query(int wd, int mask)
336 struct _lua_notify_s *head = lua_notifies;
337 if (mask & IN_MODIFY || mask & IN_IGNORED) {
338 /* for whatever reason, i keep getting IN_IGNORED when the file is
341 if (head->wd == wd) {
342 llua_block_notify = 1;
343 llua_load(head->name);
344 llua_block_notify = 0;
345 ERR("Lua script '%s' reloaded", head->name);
346 if (mask & IN_IGNORED) {
347 /* for some reason we get IN_IGNORED here
348 * sometimes, so we need to re-add the watch */
349 head->wd = inotify_add_watch(inotify_fd,
359 #endif /* HAVE_SYS_INOTIFY_H */
362 void llua_draw_pre_hook(void)
364 if (!lua_L || !draw_pre) return;
365 llua_do_call(draw_pre, 0);
368 void llua_draw_post_hook(void)
370 if (!lua_L || !draw_post) return;
371 llua_do_call(draw_post, 0);
374 void llua_set_draw_pre_hook(const char *args)
376 draw_pre = strdup(args);
379 void llua_set_draw_post_hook(const char *args)
381 draw_post = strdup(args);
384 void llua_set_long(const char *key, long value)
386 lua_pushnumber(lua_L, value);
387 lua_setfield(lua_L, -2, key);
390 void llua_set_userdata(const char *key, const char *type, void *value)
392 tolua_pushusertype(lua_L, value, type);
393 lua_setfield(lua_L, -2, key);
396 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
401 if (output_methods & TO_X) {
403 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
404 llua_set_userdata("visual", "Visual", window.visual);
405 llua_set_userdata("display", "Display", display);
406 #endif /* LUA_EXTRAS */
409 llua_set_long("width", window.width);
410 llua_set_long("height", window.height);
411 llua_set_long("border_inner_margin", window.border_inner_margin);
412 llua_set_long("border_outer_margin", window.border_outer_margin);
413 llua_set_long("border_width", window.border_width);
415 llua_set_long("text_start_x", text_start_x);
416 llua_set_long("text_start_y", text_start_y);
417 llua_set_long("text_width", text_width);
418 llua_set_long("text_height", text_height);
420 lua_setglobal(lua_L, "conky_window");
424 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
428 lua_getglobal(lua_L, "conky_window");
429 if (lua_isnil(lua_L, -1)) {
430 /* window table isn't populated yet */
435 llua_set_long("width", window.width);
436 llua_set_long("height", window.height);
438 llua_set_long("text_start_x", text_start_x);
439 llua_set_long("text_start_y", text_start_y);
440 llua_set_long("text_width", text_width);
441 llua_set_long("text_height", text_height);
443 lua_setglobal(lua_L, "conky_window");