2009-05-15 18:08:51 +00:00
|
|
|
/* Conky, a system monitor, based on torsmo
|
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
2009-05-15 18:48:11 +00:00
|
|
|
* Copyright (c) 2009 Toni Spets
|
2009-05-15 18:08:51 +00:00
|
|
|
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
|
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "conky.h"
|
2009-05-15 18:48:11 +00:00
|
|
|
#include "logging.h"
|
2009-05-15 18:08:51 +00:00
|
|
|
|
2009-05-20 04:49:42 +00:00
|
|
|
#ifdef HAVE_SYS_INOTIFY_H
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
|
|
|
|
void llua_append_notify(const char *name);
|
|
|
|
void llua_rm_notifies(void);
|
|
|
|
static int llua_block_notify = 0;
|
|
|
|
#endif /* HAVE_SYS_INOTIFY_H */
|
|
|
|
|
2009-05-15 18:08:51 +00:00
|
|
|
lua_State *lua_L = NULL;
|
|
|
|
|
2009-05-16 23:04:26 +00:00
|
|
|
void llua_init(void)
|
2009-05-15 18:08:51 +00:00
|
|
|
{
|
|
|
|
if(lua_L) return;
|
|
|
|
lua_L = lua_open();
|
|
|
|
luaL_openlibs(lua_L);
|
|
|
|
}
|
|
|
|
|
|
|
|
void llua_load(const char *script)
|
|
|
|
{
|
|
|
|
int error;
|
2009-05-15 20:35:18 +00:00
|
|
|
if(!lua_L) return;
|
2009-05-16 20:55:05 +00:00
|
|
|
error = luaL_dofile(lua_L, script);
|
|
|
|
if (error) {
|
2009-05-15 20:01:08 +00:00
|
|
|
ERR("llua_load: %s", lua_tostring(lua_L, -1));
|
2009-05-15 18:08:51 +00:00
|
|
|
lua_pop(lua_L, 1);
|
2009-05-20 04:49:42 +00:00
|
|
|
} else if (!llua_block_notify) {
|
|
|
|
llua_append_notify(script);
|
2009-05-15 18:08:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 17:57:43 +00:00
|
|
|
/*
|
|
|
|
llua_do_call does a flexible call to any Lua function
|
|
|
|
string: <function> [par1] [par2...]
|
|
|
|
retc: the number of return values expected
|
|
|
|
*/
|
|
|
|
char *llua_do_call(const char *string, int retc)
|
2009-05-15 18:08:51 +00:00
|
|
|
{
|
2009-05-16 17:57:43 +00:00
|
|
|
static char func[64];
|
|
|
|
int argc = 0;
|
2009-05-15 18:08:51 +00:00
|
|
|
|
2009-05-16 17:57:43 +00:00
|
|
|
char *tmp = strdup(string);
|
2009-05-15 18:08:51 +00:00
|
|
|
char *ptr = strtok(tmp, " ");
|
2009-05-16 17:57:43 +00:00
|
|
|
|
|
|
|
/* proceed only if the function name is present */
|
|
|
|
if(!ptr) {
|
|
|
|
free(tmp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call only conky_ prefixed functions */
|
2009-05-15 18:08:51 +00:00
|
|
|
snprintf(func, 64, "conky_%s", ptr);
|
|
|
|
|
2009-05-16 17:57:43 +00:00
|
|
|
/* push the function name to stack */
|
2009-05-15 18:08:51 +00:00
|
|
|
lua_getglobal(lua_L, func);
|
|
|
|
|
2009-05-16 17:57:43 +00:00
|
|
|
/* parse all function parameters from args and push them to the stack */
|
2009-05-15 18:08:51 +00:00
|
|
|
ptr = strtok(NULL, " ");
|
|
|
|
while(ptr) {
|
|
|
|
lua_pushstring(lua_L, ptr);
|
|
|
|
ptr = strtok(NULL, " ");
|
2009-05-16 17:57:43 +00:00
|
|
|
argc++;
|
2009-05-15 18:08:51 +00:00
|
|
|
}
|
|
|
|
|
2009-05-16 17:57:43 +00:00
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
if(lua_pcall(lua_L, argc, retc, 0) != 0) {
|
|
|
|
ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
|
2009-05-15 18:08:51 +00:00
|
|
|
lua_pop(lua_L, -1);
|
2009-05-16 17:57:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2009-05-16 23:04:26 +00:00
|
|
|
/*
|
|
|
|
* same as llua_do_call() except passes everything after func as one arg.
|
|
|
|
*/
|
|
|
|
char *llua_do_read_call(const char *function, const char *arg, int retc)
|
|
|
|
{
|
|
|
|
static char func[64];
|
|
|
|
snprintf(func, 64, "conky_%s", function);
|
|
|
|
|
|
|
|
/* push the function name to stack */
|
|
|
|
lua_getglobal(lua_L, func);
|
|
|
|
|
|
|
|
/* push function parameter to the stack */
|
|
|
|
lua_pushstring(lua_L, arg);
|
|
|
|
|
|
|
|
if (lua_pcall(lua_L, 1, retc, 0) != 0) {
|
|
|
|
ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
|
|
|
|
lua_pop(lua_L, -1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2009-05-16 17:57:43 +00:00
|
|
|
char *llua_getstring(const char *args)
|
|
|
|
{
|
|
|
|
char *func;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
if(!lua_L) return NULL;
|
|
|
|
|
|
|
|
func = llua_do_call(args, 1);
|
|
|
|
if(func) {
|
2009-05-15 18:08:51 +00:00
|
|
|
if(!lua_isstring(lua_L, -1)) {
|
2009-05-15 20:01:08 +00:00
|
|
|
ERR("llua_getstring: function %s didn't return a string, result discarded", func);
|
2009-05-15 18:08:51 +00:00
|
|
|
} else {
|
2009-05-16 23:04:26 +00:00
|
|
|
ret = strdup(lua_tostring(lua_L, -1));
|
|
|
|
lua_pop(lua_L, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *llua_getstring_read(const char *function, const char *arg)
|
|
|
|
{
|
|
|
|
char *func;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
if(!lua_L) return NULL;
|
|
|
|
|
|
|
|
func = llua_do_read_call(function, arg, 1);
|
|
|
|
if (func) {
|
|
|
|
if(!lua_isstring(lua_L, -1)) {
|
|
|
|
ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
|
|
|
|
} else {
|
|
|
|
ret = strdup(lua_tostring(lua_L, -1));
|
2009-05-15 18:08:51 +00:00
|
|
|
lua_pop(lua_L, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-05-16 20:55:05 +00:00
|
|
|
int llua_getinteger(const char *args, int *per)
|
2009-05-15 18:08:51 +00:00
|
|
|
{
|
2009-05-16 17:57:43 +00:00
|
|
|
char *func;
|
2009-05-15 18:08:51 +00:00
|
|
|
|
|
|
|
if(!lua_L) return 0;
|
|
|
|
|
2009-05-16 17:57:43 +00:00
|
|
|
func = llua_do_call(args, 1);
|
|
|
|
if(func) {
|
2009-05-15 18:08:51 +00:00
|
|
|
if(!lua_isnumber(lua_L, -1)) {
|
2009-05-16 20:55:05 +00:00
|
|
|
ERR("llua_getinteger: function %s didn't return an integer, result discarded", func);
|
2009-05-15 18:08:51 +00:00
|
|
|
} else {
|
2009-05-16 17:37:53 +00:00
|
|
|
*per = lua_tointeger(lua_L, -1);
|
2009-05-15 18:08:51 +00:00
|
|
|
lua_pop(lua_L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-16 23:04:26 +00:00
|
|
|
void llua_close(void)
|
2009-05-15 18:08:51 +00:00
|
|
|
{
|
2009-05-20 04:49:42 +00:00
|
|
|
llua_rm_notifies();
|
2009-05-15 18:08:51 +00:00
|
|
|
if(!lua_L) return;
|
|
|
|
lua_close(lua_L);
|
|
|
|
lua_L = NULL;
|
|
|
|
}
|
2009-05-20 04:49:42 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_INOTIFY_H
|
|
|
|
struct _lua_notify_s {
|
|
|
|
int wd;
|
|
|
|
char name[DEFAULT_TEXT_BUFFER_SIZE];
|
|
|
|
struct _lua_notify_s *next;
|
|
|
|
};
|
|
|
|
static struct _lua_notify_s *lua_notifies = 0;
|
|
|
|
|
|
|
|
static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
|
|
|
|
{
|
|
|
|
struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
|
|
|
|
memset(ret, 0, sizeof(struct _lua_notify_s));
|
|
|
|
strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void llua_append_notify(const char *name)
|
|
|
|
{
|
|
|
|
/* do it */
|
|
|
|
struct _lua_notify_s *new_tail = 0;
|
|
|
|
if (!lua_notifies) {
|
|
|
|
/* empty, fresh new digs */
|
|
|
|
new_tail = lua_notifies = llua_notify_list_do_alloc(name);
|
|
|
|
} else {
|
|
|
|
struct _lua_notify_s *tail = lua_notifies;
|
|
|
|
while (tail->next) {
|
|
|
|
tail = tail->next;
|
|
|
|
}
|
|
|
|
// should be @ the end now
|
|
|
|
new_tail = llua_notify_list_do_alloc(name);
|
|
|
|
tail->next = new_tail;
|
|
|
|
}
|
|
|
|
new_tail->wd = inotify_add_watch(inotify_fd,
|
|
|
|
new_tail->name,
|
|
|
|
IN_MODIFY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void llua_rm_notifies(void)
|
|
|
|
{
|
|
|
|
/* git 'er done */
|
|
|
|
struct _lua_notify_s *head = lua_notifies;
|
|
|
|
struct _lua_notify_s *next = head->next;
|
|
|
|
if (!lua_notifies) return;
|
|
|
|
inotify_rm_watch(inotify_fd, head->wd);
|
|
|
|
free(head);
|
|
|
|
while (next) {
|
|
|
|
head = next;
|
|
|
|
next = head->next;
|
|
|
|
inotify_rm_watch(inotify_fd, head->wd);
|
|
|
|
free(head);
|
|
|
|
}
|
|
|
|
lua_notifies = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void llua_inotify_query(int wd, int mask)
|
|
|
|
{
|
|
|
|
struct _lua_notify_s *head = lua_notifies;
|
|
|
|
if (mask & IN_MODIFY || mask & IN_IGNORED) {
|
|
|
|
/* for whatever reason, i keep getting IN_IGNORED when the file is
|
|
|
|
* modified */
|
|
|
|
while (head) {
|
|
|
|
if (head->wd == wd) {
|
|
|
|
llua_block_notify = 1;
|
|
|
|
llua_load(head->name);
|
|
|
|
llua_block_notify = 0;
|
|
|
|
if (mask & IN_IGNORED) {
|
|
|
|
/* for some reason we get IN_IGNORED here
|
|
|
|
* sometimes, so we need to re-add the watch */
|
|
|
|
head->wd = inotify_add_watch(inotify_fd,
|
|
|
|
head->name,
|
|
|
|
IN_MODIFY);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
head = head->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_SYS_INOTIFY_H */
|
|
|
|
|