1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2009 Toni Spets
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #ifdef HAVE_SYS_INOTIFY_H
32 #include <sys/inotify.h>
34 void llua_append_notify(const char *name);
35 void llua_rm_notifies(void);
36 static int llua_block_notify = 0;
37 #endif /* HAVE_SYS_INOTIFY_H */
39 lua_State *lua_L = NULL;
48 void llua_load(const char *script)
52 error = luaL_dofile(lua_L, script);
54 ERR("llua_load: %s", lua_tostring(lua_L, -1));
56 } else if (!llua_block_notify) {
57 llua_append_notify(script);
62 llua_do_call does a flexible call to any Lua function
63 string: <function> [par1] [par2...]
64 retc: the number of return values expected
66 char *llua_do_call(const char *string, int retc)
71 char *tmp = strdup(string);
72 char *ptr = strtok(tmp, " ");
74 /* proceed only if the function name is present */
80 /* call only conky_ prefixed functions */
81 snprintf(func, 64, "conky_%s", ptr);
83 /* push the function name to stack */
84 lua_getglobal(lua_L, func);
86 /* parse all function parameters from args and push them to the stack */
87 ptr = strtok(NULL, " ");
89 lua_pushstring(lua_L, ptr);
90 ptr = strtok(NULL, " ");
96 if(lua_pcall(lua_L, argc, retc, 0) != 0) {
97 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
106 * same as llua_do_call() except passes everything after func as one arg.
108 char *llua_do_read_call(const char *function, const char *arg, int retc)
110 static char func[64];
111 snprintf(func, 64, "conky_%s", function);
113 /* push the function name to stack */
114 lua_getglobal(lua_L, func);
116 /* push function parameter to the stack */
117 lua_pushstring(lua_L, arg);
119 if (lua_pcall(lua_L, 1, retc, 0) != 0) {
120 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
128 char *llua_getstring(const char *args)
133 if(!lua_L) return NULL;
135 func = llua_do_call(args, 1);
137 if(!lua_isstring(lua_L, -1)) {
138 ERR("llua_getstring: function %s didn't return a string, result discarded", func);
140 ret = strdup(lua_tostring(lua_L, -1));
148 char *llua_getstring_read(const char *function, const char *arg)
153 if(!lua_L) return NULL;
155 func = llua_do_read_call(function, arg, 1);
157 if(!lua_isstring(lua_L, -1)) {
158 ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
160 ret = strdup(lua_tostring(lua_L, -1));
168 int llua_getinteger(const char *args, int *per)
174 func = llua_do_call(args, 1);
176 if(!lua_isnumber(lua_L, -1)) {
177 ERR("llua_getinteger: function %s didn't return an integer, result discarded", func);
179 *per = lua_tointeger(lua_L, -1);
187 void llua_close(void)
195 #ifdef HAVE_SYS_INOTIFY_H
196 struct _lua_notify_s {
198 char name[DEFAULT_TEXT_BUFFER_SIZE];
199 struct _lua_notify_s *next;
201 static struct _lua_notify_s *lua_notifies = 0;
203 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
205 struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
206 memset(ret, 0, sizeof(struct _lua_notify_s));
207 strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
211 void llua_append_notify(const char *name)
214 struct _lua_notify_s *new_tail = 0;
216 /* empty, fresh new digs */
217 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
219 struct _lua_notify_s *tail = lua_notifies;
223 // should be @ the end now
224 new_tail = llua_notify_list_do_alloc(name);
225 tail->next = new_tail;
227 new_tail->wd = inotify_add_watch(inotify_fd,
232 void llua_rm_notifies(void)
235 struct _lua_notify_s *head = lua_notifies;
236 struct _lua_notify_s *next = head->next;
237 if (!lua_notifies) return;
238 inotify_rm_watch(inotify_fd, head->wd);
243 inotify_rm_watch(inotify_fd, head->wd);
249 void llua_inotify_query(int wd, int mask)
251 struct _lua_notify_s *head = lua_notifies;
252 if (mask & IN_MODIFY || mask & IN_IGNORED) {
253 /* for whatever reason, i keep getting IN_IGNORED when the file is
256 if (head->wd == wd) {
257 llua_block_notify = 1;
258 llua_load(head->name);
259 llua_block_notify = 0;
260 if (mask & IN_IGNORED) {
261 /* for some reason we get IN_IGNORED here
262 * sometimes, so we need to re-add the watch */
263 head->wd = inotify_add_watch(inotify_fd,
273 #endif /* HAVE_SYS_INOTIFY_H */