Be more intelligent about handling tolua++ user types.
authorBrenden Matthews <brenden@rty.ca>
Mon, 27 Jul 2009 20:07:36 +0000 (14:07 -0600)
committerBrenden Matthews <brenden@rty.ca>
Mon, 27 Jul 2009 20:07:36 +0000 (14:07 -0600)
configure.ac.in
doc/lua.xml
src/llua.c

index e8651c3..9746cb2 100644 (file)
@@ -457,6 +457,7 @@ if test x$want_lua = xyes; then
                want_lua_cairo=no
                want_lua_imlib2=no
        else
+               AC_DEFINE(LUA_EXTRAS, 1, [Define if you want Lua extras])
                if test "x$want_x11" != "xyes"; then
                        dnl silently disable if no x11
                        want_lua_cairo=no
@@ -486,7 +487,9 @@ if test x$want_lua = xyes; then
                                                           AC_MSG_ERROR([tolua_error not found]),
                                                           $LUA51_LIBS)
                        fi
+                       conky_LIBS="$conky_LIBS $tolua_LIBS"
                fi
+
        fi
        AC_DEFINE(HAVE_LUA, 1, [Define if you want Lua scripting support])
 fi
index cb7f8f1..c415586 100644 (file)
                 <member>
                     <command>drawable</command>
                     <option>Window's drawable (Xlib
-                    Drawable)</option>
+                    Drawable), requires Lua extras enabled at compile time.</option>
                 </member>
                 <member>
                     <command>visual</command>
-                    <option>Window's visual (Xlib Visual)</option>
+                    <option>Window's visual (Xlib Visual), requires Lua extras enabled at compile time.</option>
                 </member>
                 <member>
                     <command>display</command>
                     <option>Window's display (Xlib
-                    Display)</option>
+                    Display), requires Lua extras enabled at compile time.</option>
                 </member>
                 <member>
                     <command>width</command>
-                    <option>Window width (in pixels)</option>
+                    <option>Window width (in pixels).</option>
                 </member>
                 <member>
                     <command>height</command>
-                    <option>Window height (in pixels)</option>
+                    <option>Window height (in pixels).</option>
                 </member>
                 <member>
                     <command>border_inner_margin</command>
                     <option>Window's inner border margin (in
-                    pixels)</option>
+                    pixels).</option>
                 </member>
                 <member>
                     <command>border_outer_margin</command>
                     <option>Window's outer border margin (in
-                    pixels)</option>
+                    pixels).</option>
                 </member>
                 <member>
                     <command>border_width</command>
                     <option>Window's border width (in
-                    pixels)</option>
+                    pixels).</option>
                 </member>
                 <member>
                     <command>text_start_x</command>
                     <option>The x component of the starting
-                    coordinate of text drawing</option>
+                    coordinate of text drawing.</option>
                 </member>
                 <member>
                     <command>text_start_y</command>
                     <option>The y component of the starting
-                    coordinate of text drawing</option>
+                    coordinate of text drawing.</option>
                 </member>
                 <member>
                     <command>text_width</command>
                     <option>The width of the text drawing
-                    region</option>
+                    region.</option>
                 </member>
                 <member>
                     <command>text_height</command>
                     <option>The height of the text drawing
-                    region</option>
+                    region.</option>
                 </member>
             </simplelist>
             <para>NOTE: This table is only defined when X support
index 99b1685..9b38cfa 100644 (file)
 #include "logging.h"
 #include "build.h"
 
+#ifdef LUA_EXTRAS
+#include <tolua++.h>
+#endif /* LUA_EXTRAS */
+
 #ifdef HAVE_SYS_INOTIFY_H
 #include <sys/inotify.h>
 
@@ -95,6 +99,14 @@ void llua_init(void)
 
        lua_pushcfunction(lua_L, &llua_conky_parse);
        lua_setglobal(lua_L, "conky_parse");
+
+#if defined(X11) && defined(LUA_EXTRAS)
+       /* register tolua++ user types */
+       tolua_open(lua_L);
+       tolua_usertype(lua_L, "Drawable");
+       tolua_usertype(lua_L, "Visual");
+       tolua_usertype(lua_L, "Display");
+#endif /* X11 */
 }
 
 void llua_load(const char *script)
@@ -117,10 +129,10 @@ void llua_load(const char *script)
 }
 
 /*
-       llua_do_call does a flexible call to any Lua function
-       string: <function> [par1] [par2...]
-       retc: the number of return values expected
-*/
+   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)
 {
        static char func[64];
@@ -171,7 +183,7 @@ 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);
 
@@ -375,62 +387,9 @@ void llua_set_long(const char *key, long value)
        lua_setfield(lua_L, -2, key);
 }
 
-/* this function mostly copied from tolua++ source so that we could play nice
- * with tolua++ libs.  tolua++ is provided 'as is'
- */
 void llua_set_userdata(const char *key, const char *type, void *value)
 {
-       if (value == NULL) {
-               lua_pushnil(lua_L);
-       } else {
-               luaL_getmetatable(lua_L, type);
-               lua_pushstring(lua_L,"tolua_ubox");
-               lua_rawget(lua_L,-2);        /* stack: mt ubox */
-               if (lua_isnil(lua_L, -1)) {
-                       lua_pop(lua_L, 1);
-                       lua_pushstring(lua_L, "tolua_ubox");
-                       lua_rawget(lua_L, LUA_REGISTRYINDEX);
-               }
-               lua_pushlightuserdata(lua_L,value);
-               lua_rawget(lua_L,-2);                       /* stack: mt ubox ubox[u] */
-               if (lua_isnil(lua_L,-1)) {
-                       lua_pop(lua_L,1);                          /* stack: mt ubox */
-                       lua_pushlightuserdata(lua_L,value);
-                       *(void**)lua_newuserdata(lua_L,sizeof(void *)) = value;   /* stack: mt ubox u newud */
-                       lua_pushvalue(lua_L,-1);                   /* stack: mt ubox u newud newud */
-                       lua_insert(lua_L,-4);                      /* stack: mt newud ubox u newud */
-                       lua_rawset(lua_L,-3);                      /* stack: mt newud ubox */
-                       lua_pop(lua_L,1);                          /* stack: mt newud */
-                       /*luaL_getmetatable(lua_L,type);*/
-                       lua_pushvalue(lua_L, -2);                       /* stack: mt newud mt */
-                       lua_setmetatable(lua_L,-2);                     /* stack: mt newud */
-
-               } else {
-                       /* check the need of updating the metatable to a more specialized class */
-                       lua_insert(lua_L,-2);                       /* stack: mt ubox[u] ubox */
-                       lua_pop(lua_L,1);                           /* stack: mt ubox[u] */
-                       lua_pushstring(lua_L,"tolua_super");
-                       lua_rawget(lua_L,LUA_REGISTRYINDEX);        /* stack: mt ubox[u] super */
-                       lua_getmetatable(lua_L,-2);                 /* stack: mt ubox[u] super mt */
-                       lua_rawget(lua_L,-2);                       /* stack: mt ubox[u] super super[mt] */
-                       if (lua_istable(lua_L,-1)) {
-                               lua_pushstring(lua_L,type);                 /* stack: mt ubox[u] super super[mt] type */
-                               lua_rawget(lua_L,-2);                       /* stack: mt ubox[u] super super[mt] flag */
-                               if (lua_toboolean(lua_L,-1) == 1) {
-                                       /* if true */
-                                       lua_pop(lua_L,3);       /* mt ubox[u]*/
-                                       lua_remove(lua_L, -2);
-                                       return;
-                               }
-                       }
-                       /* type represents a more specilized type */
-                       /*luaL_getmetatable(lua_L,type);             // stack: mt ubox[u] super super[mt] flag mt */
-                       lua_pushvalue(lua_L, -5);                                       /* stack: mt ubox[u] super super[mt] flag mt */
-                       lua_setmetatable(lua_L,-5);                /* stack: mt ubox[u] super super[mt] flag */
-                       lua_pop(lua_L,3);                          /* stack: mt ubox[u] */
-               }
-               lua_remove(lua_L, -2);  /* stack: ubox[u]*/
-       }
+       tolua_pushusertype(lua_L, value, type);
        lua_setfield(lua_L, -2, key);
 }
 
@@ -438,11 +397,13 @@ void llua_setup_window_table(int text_start_x, int text_start_y, int text_width,
 {
        if (!lua_L) return;
        lua_newtable(lua_L);
-       
+
        if (output_methods & TO_X) {
+#ifdef LUA_EXTRAS
                llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
                llua_set_userdata("visual", "Visual", window.visual);
                llua_set_userdata("display", "Display", display);
+#endif /* LUA_EXTRAS */
 
 
                llua_set_long("width", window.width);
@@ -467,7 +428,7 @@ void llua_update_window_table(int text_start_x, int text_start_y, int text_width
        lua_getglobal(lua_L, "conky_window");
        if (lua_isnil(lua_L, -1)) {
                /* window table isn't populated yet */
-               lua_pop(lua_L, 1);
+               lua_pop(lua_L, 1);
                return;
        }