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/>.
25 #ifdef HAVE_SYS_INOTIFY_H
26 #include <sys/inotify.h>
28 void llua_append_notify(const char *name);
29 void llua_rm_notifies(void);
30 static int llua_block_notify = 0;
31 #endif /* HAVE_SYS_INOTIFY_H */
33 lua_State *lua_L = NULL;
42 void llua_load(const char *script)
46 error = luaL_dofile(lua_L, script);
48 ERR("llua_load: %s", lua_tostring(lua_L, -1));
50 #ifdef HAVE_SYS_INOTIFY_H
51 } else if (!llua_block_notify) {
52 llua_append_notify(script);
53 #endif /* HAVE_SYS_INOTIFY_H */
58 llua_do_call does a flexible call to any Lua function
59 string: <function> [par1] [par2...]
60 retc: the number of return values expected
62 char *llua_do_call(const char *string, int retc)
67 char *tmp = strdup(string);
68 char *ptr = strtok(tmp, " ");
70 /* proceed only if the function name is present */
76 /* call only conky_ prefixed functions */
77 snprintf(func, 64, "conky_%s", ptr);
79 /* push the function name to stack */
80 lua_getglobal(lua_L, func);
82 /* parse all function parameters from args and push them to the stack */
83 ptr = strtok(NULL, " ");
85 lua_pushstring(lua_L, ptr);
86 ptr = strtok(NULL, " ");
92 if(lua_pcall(lua_L, argc, retc, 0) != 0) {
93 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
102 * same as llua_do_call() except passes everything after func as one arg.
104 char *llua_do_read_call(const char *function, const char *arg, int retc)
106 static char func[64];
107 snprintf(func, 64, "conky_%s", function);
109 /* push the function name to stack */
110 lua_getglobal(lua_L, func);
112 /* push function parameter to the stack */
113 lua_pushstring(lua_L, arg);
115 if (lua_pcall(lua_L, 1, retc, 0) != 0) {
116 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
124 char *llua_getstring(const char *args)
129 if(!lua_L) return NULL;
131 func = llua_do_call(args, 1);
133 if(!lua_isstring(lua_L, -1)) {
134 ERR("llua_getstring: function %s didn't return a string, result discarded", func);
136 ret = strdup(lua_tostring(lua_L, -1));
144 char *llua_getstring_read(const char *function, const char *arg)
149 if(!lua_L) return NULL;
151 func = llua_do_read_call(function, arg, 1);
153 if(!lua_isstring(lua_L, -1)) {
154 ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
156 ret = strdup(lua_tostring(lua_L, -1));
164 int llua_getinteger(const char *args, int *per)
170 func = llua_do_call(args, 1);
172 if(!lua_isnumber(lua_L, -1)) {
173 ERR("llua_getinteger: function %s didn't return an integer, result discarded", func);
175 *per = lua_tointeger(lua_L, -1);
183 void llua_close(void)
185 #ifdef HAVE_SYS_INOTIFY_H
187 #endif /* HAVE_SYS_INOTIFY_H */
193 #ifdef HAVE_SYS_INOTIFY_H
194 struct _lua_notify_s {
196 char name[DEFAULT_TEXT_BUFFER_SIZE];
197 struct _lua_notify_s *next;
199 static struct _lua_notify_s *lua_notifies = 0;
201 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
203 struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
204 memset(ret, 0, sizeof(struct _lua_notify_s));
205 strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
209 void llua_append_notify(const char *name)
212 struct _lua_notify_s *new_tail = 0;
214 /* empty, fresh new digs */
215 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
217 struct _lua_notify_s *tail = lua_notifies;
221 // should be @ the end now
222 new_tail = llua_notify_list_do_alloc(name);
223 tail->next = new_tail;
225 new_tail->wd = inotify_add_watch(inotify_fd,
230 void llua_rm_notifies(void)
233 struct _lua_notify_s *head = lua_notifies;
234 struct _lua_notify_s *next = 0;
235 if (!lua_notifies) return;
236 inotify_rm_watch(inotify_fd, head->wd);
237 if (head->next) next = head->next;
242 inotify_rm_watch(inotify_fd, head->wd);
248 void llua_inotify_query(int wd, int mask)
250 struct _lua_notify_s *head = lua_notifies;
251 if (mask & IN_MODIFY || mask & IN_IGNORED) {
252 /* for whatever reason, i keep getting IN_IGNORED when the file is
255 if (head->wd == wd) {
256 llua_block_notify = 1;
257 llua_load(head->name);
258 llua_block_notify = 0;
259 ERR("Lua script '%s' reloaded", head->name);
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 */