Revert "Uhh..ansohus"
[monky] / src / llua.c
index 47dc120..f27bdb2 100644 (file)
@@ -39,8 +39,10 @@ void llua_rm_notifies(void);
 static int llua_block_notify = 0;
 #endif /* HAVE_SYS_INOTIFY_H */
 
-static char *draw_pre = 0;
-static char *draw_post = 0;
+static char *draw_pre_hook = 0;
+static char *draw_post_hook = 0;
+static char *startup_hook = 0;
+static char *shutdown_hook = 0;
 
 lua_State *lua_L = NULL;
 
@@ -143,7 +145,7 @@ void llua_load(const char *script)
        to_real_path(path, script);
        error = luaL_dofile(lua_L, path);
        if (error) {
-               ERR("llua_load: %s", lua_tostring(lua_L, -1));
+               NORM_ERR("llua_load: %s", lua_tostring(lua_L, -1));
                lua_pop(lua_L, 1);
 #ifdef HAVE_SYS_INOTIFY_H
        } else if (!llua_block_notify && inotify_fd != -1) {
@@ -192,7 +194,7 @@ char *llua_do_call(const char *string, int retc)
        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));
+               NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
                lua_pop(lua_L, -1);
                return NULL;
        }
@@ -215,7 +217,7 @@ char *llua_do_read_call(const char *function, const char *arg, int retc)
        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));
+               NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
                lua_pop(lua_L, -1);
                return NULL;
        }
@@ -233,7 +235,7 @@ char *llua_getstring(const char *args)
        func = llua_do_call(args, 1);
        if (func) {
                if (!lua_isstring(lua_L, -1)) {
-                       ERR("llua_getstring: function %s didn't return a string, result discarded", func);
+                       NORM_ERR("llua_getstring: function %s didn't return a string, result discarded", func);
                } else {
                        ret = strdup(lua_tostring(lua_L, -1));
                        lua_pop(lua_L, 1);
@@ -253,7 +255,7 @@ char *llua_getstring_read(const char *function, const char *arg)
        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);
+                       NORM_ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
                } else {
                        ret = strdup(lua_tostring(lua_L, -1));
                        lua_pop(lua_L, 1);
@@ -272,7 +274,7 @@ int llua_getnumber(const char *args, double *ret)
        func = llua_do_call(args, 1);
        if(func) {
                if(!lua_isnumber(lua_L, -1)) {
-                       ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
+                       NORM_ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
                } else {
                        *ret = lua_tonumber(lua_L, -1);
                        lua_pop(lua_L, 1);
@@ -287,13 +289,13 @@ void llua_close(void)
 #ifdef HAVE_SYS_INOTIFY_H
        llua_rm_notifies();
 #endif /* HAVE_SYS_INOTIFY_H */
-       if (draw_pre) {
-               free(draw_pre);
-               draw_pre = 0;
+       if (draw_pre_hook) {
+               free(draw_pre_hook);
+               draw_pre_hook = 0;
        }
-       if (draw_post) {
-               free(draw_post);
-               draw_post = 0;
+       if (draw_post_hook) {
+               free(draw_post_hook);
+               draw_post_hook = 0;
        }
        if(!lua_L) return;
        lua_close(lua_L);
@@ -366,7 +368,7 @@ void llua_inotify_query(int wd, int mask)
                                llua_block_notify = 1;
                                llua_load(head->name);
                                llua_block_notify = 0;
-                               ERR("Lua script '%s' reloaded", head->name);
+                               NORM_ERR("Lua script '%s' reloaded", head->name);
                                if (mask & IN_IGNORED) {
                                        /* for some reason we get IN_IGNORED here
                                         * sometimes, so we need to re-add the watch */
@@ -388,27 +390,49 @@ void llua_set_number(const char *key, double value)
        lua_setfield(lua_L, -2, key);
 }
 
+void llua_set_startup_hook(const char *args)
+{
+       startup_hook = strdup(args);
+}
+
+void llua_set_shutdown_hook(const char *args)
+{
+       shutdown_hook = strdup(args);
+}
+
+void llua_startup_hook(void)
+{
+       if (!lua_L || !startup_hook) return;
+       llua_do_call(startup_hook, 0);
+}
+
+void llua_shutdown_hook(void)
+{
+       if (!lua_L || !shutdown_hook) return;
+       llua_do_call(shutdown_hook, 0);
+}
+
 #ifdef X11
 void llua_draw_pre_hook(void)
 {
-       if (!lua_L || !draw_pre) return;
-       llua_do_call(draw_pre, 0);
+       if (!lua_L || !draw_pre_hook) return;
+       llua_do_call(draw_pre_hook, 0);
 }
 
 void llua_draw_post_hook(void)
 {
-       if (!lua_L || !draw_post) return;
-       llua_do_call(draw_post, 0);
+       if (!lua_L || !draw_post_hook) return;
+       llua_do_call(draw_post_hook, 0);
 }
 
 void llua_set_draw_pre_hook(const char *args)
 {
-       draw_pre = strdup(args);
+       draw_pre_hook = strdup(args);
 }
 
 void llua_set_draw_post_hook(const char *args)
 {
-       draw_post = strdup(args);
+       draw_post_hook = strdup(args);
 }
 
 #ifdef LUA_EXTRAS