Added some Lua API stuff.
authorBrenden Matthews <brenden@rty.ca>
Tue, 28 Jul 2009 18:38:43 +0000 (12:38 -0600)
committerBrenden Matthews <brenden@rty.ca>
Tue, 28 Jul 2009 18:38:43 +0000 (12:38 -0600)
Added conky_set_update_interval() API call, which allows you to change
Conky's update interval from a Lua script.  Added the 'conky_info' table
to global Lua context, which still needs populating with stuff (right
now it only contains the current update interval and the system uptime).

doc/lua.xml
src/conky.c
src/conky.h
src/llua.c
src/llua.h

index c415586..2c397ef 100644 (file)
 <variablelist>
-    <varlistentry>
-        <term>
-            <command>
-                <option>conky_parse(string)</option>
-            </command>
-            <option>function</option>
-        </term>
-        <listitem>
-            <para>This function takes a string that is evaluated as
-            per Conky's TEXT section, and then returns a string
-            with the result.</para>
-        </listitem>
-    </varlistentry>
-    <varlistentry>
-        <term>
-            <command>
-                <option>conky_window</option>
-            </command>
-            <option>table</option>
-        </term>
-        <listitem>
-            <para>This table contains some information about
-            Conky's window. The following table describes the
-            values contained:</para>
-            <simplelist>
-                <member>
-                    <command>drawable</command>
-                    <option>Window's drawable (Xlib
-                    Drawable), requires Lua extras enabled at compile time.</option>
-                </member>
-                <member>
-                    <command>visual</command>
-                    <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), requires Lua extras enabled at compile time.</option>
-                </member>
-                <member>
-                    <command>width</command>
-                    <option>Window width (in pixels).</option>
-                </member>
-                <member>
-                    <command>height</command>
-                    <option>Window height (in pixels).</option>
-                </member>
-                <member>
-                    <command>border_inner_margin</command>
-                    <option>Window's inner border margin (in
-                    pixels).</option>
-                </member>
-                <member>
-                    <command>border_outer_margin</command>
-                    <option>Window's outer border margin (in
-                    pixels).</option>
-                </member>
-                <member>
-                    <command>border_width</command>
-                    <option>Window's border width (in
-                    pixels).</option>
-                </member>
-                <member>
-                    <command>text_start_x</command>
-                    <option>The x component of the starting
-                    coordinate of text drawing.</option>
-                </member>
-                <member>
-                    <command>text_start_y</command>
-                    <option>The y component of the starting
-                    coordinate of text drawing.</option>
-                </member>
-                <member>
-                    <command>text_width</command>
-                    <option>The width of the text drawing
-                    region.</option>
-                </member>
-                <member>
-                    <command>text_height</command>
-                    <option>The height of the text drawing
-                    region.</option>
-                </member>
-            </simplelist>
-            <para>NOTE: This table is only defined when X support
-            is enabled.</para>
-        </listitem>
-    </varlistentry>
-    <varlistentry>
-        <term>
-            <command>
-                <option>conky_build_info</option>
-            </command>
-            <option>string</option>
-        </term>
-        <listitem>
-            <para>A string containing the build info for this
-            particular instance of Conky, including the version,
-            build date, and architecture.</para>
-        </listitem>
-    </varlistentry>
-    <varlistentry>
-        <term>
-            <command>
-                <option>conky_build_date</option>
-            </command>
-            <option>string</option>
-        </term>
-        <listitem>
-            <para>A string containing the build date for this
-            particular instance of Conky.</para>
-        </listitem>
-    </varlistentry>
-    <varlistentry>
-        <term>
-            <command>
-                <option>conky_build_arch</option>
-            </command>
-            <option>string</option>
-        </term>
-        <listitem>
-            <para>A string containing the build architecture for
-            this particular instance of Conky.</para>
-        </listitem>
-    </varlistentry>
-    <varlistentry>
-        <term>
-            <command>
-                <option>conky_version</option>
-            </command>
-            <option>string</option>
-        </term>
-        <listitem>
-            <para>A string containing the version of the current
-            instance of Conky.</para>
-        </listitem>
-    </varlistentry>
-    <varlistentry>
-        <term>
-            <command>
-                <option>conky_config</option>
-            </command>
-            <option>string</option>
-        </term>
-        <listitem>
-            <para>A string containing the path of the current Conky
-            configuration file.</para>
-        </listitem>
-    </varlistentry>
+       <varlistentry>
+               <term>
+                       <command>
+                               <option>conky_parse(string)</option>
+                       </command>
+                       <option>function</option>
+               </term>
+               <listitem>
+                       <para>This function takes a string that is evaluated as
+                               per Conky's TEXT section, and then returns a string
+                               with the result.</para>
+               </listitem>
+       </varlistentry>
+       <varlistentry>
+               <term>
+                       <command>
+                               <option>conky_set_update_interval(number)</option>
+                       </command>
+                       <option>function</option>
+               </term>
+               <listitem>
+                       <para>
+                               Sets Conky's update interval (in seconds) to 'number'.
+                       </para>
+               </listitem>
+       </varlistentry>
+       <varlistentry>
+               <term>
+                       <command>
+                               <option>conky_window</option>
+                       </command>
+                       <option>table</option>
+               </term>
+               <listitem>
+                       <para>This table contains some information about
+                               Conky's window. The following table describes the
+                               values contained:</para>
+                       <simplelist>
+                               <member>
+                                       <command>drawable</command>
+                                       <option>Window's drawable (Xlib
+                                               Drawable), requires Lua extras enabled at compile time.</option>
+                               </member>
+                               <member>
+                                       <command>visual</command>
+                                       <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), requires Lua extras enabled at compile time.</option>
+                               </member>
+                               <member>
+                                       <command>width</command>
+                                       <option>Window width (in pixels).</option>
+                               </member>
+                               <member>
+                                       <command>height</command>
+                                       <option>Window height (in pixels).</option>
+                               </member>
+                               <member>
+                                       <command>border_inner_margin</command>
+                                       <option>Window's inner border margin (in
+                                               pixels).</option>
+                               </member>
+                               <member>
+                                       <command>border_outer_margin</command>
+                                       <option>Window's outer border margin (in
+                                               pixels).</option>
+                               </member>
+                               <member>
+                                       <command>border_width</command>
+                                       <option>Window's border width (in
+                                               pixels).</option>
+                               </member>
+                               <member>
+                                       <command>text_start_x</command>
+                                       <option>The x component of the starting
+                                               coordinate of text drawing.</option>
+                               </member>
+                               <member>
+                                       <command>text_start_y</command>
+                                       <option>The y component of the starting
+                                               coordinate of text drawing.</option>
+                               </member>
+                               <member>
+                                       <command>text_width</command>
+                                       <option>The width of the text drawing
+                                               region.</option>
+                               </member>
+                               <member>
+                                       <command>text_height</command>
+                                       <option>The height of the text drawing
+                                               region.</option>
+                               </member>
+                       </simplelist>
+                       <para>NOTE: This table is only defined when X support
+                               is enabled.</para>
+               </listitem>
+       </varlistentry>
+       <varlistentry>
+               <term>
+                       <command>
+                               <option>conky_info</option>
+                       </command>
+                       <option>table</option>
+               </term>
+               <listitem>
+                       <para>This table contains some information about
+                               Conky's internal data. The following table describes the
+                               values contained:</para>
+                       <simplelist>
+                               <member>
+                                       <command>update_interval</command>
+                                       <option>Conky's update interval (in seconds).
+                                       </option>
+                               </member>
+                               <member>
+                                       <command>uptime</command>
+                                       <option>System uptime, in seconds.
+                                       </option>
+                               </member>
+                       </simplelist>
+               </listitem>
+       </varlistentry>
+       <varlistentry>
+               <term>
+                       <command>
+                               <option>conky_build_info</option>
+                       </command>
+                       <option>string</option>
+               </term>
+               <listitem>
+                       <para>A string containing the build info for this
+                               particular instance of Conky, including the version,
+                               build date, and architecture.</para>
+               </listitem>
+       </varlistentry>
+       <varlistentry>
+               <term>
+                       <command>
+                               <option>conky_build_date</option>
+                       </command>
+                       <option>string</option>
+               </term>
+               <listitem>
+                       <para>A string containing the build date for this
+                               particular instance of Conky.</para>
+               </listitem>
+       </varlistentry>
+       <varlistentry>
+               <term>
+                       <command>
+                               <option>conky_build_arch</option>
+                       </command>
+                       <option>string</option>
+               </term>
+               <listitem>
+                       <para>A string containing the build architecture for
+                               this particular instance of Conky.</para>
+               </listitem>
+       </varlistentry>
+       <varlistentry>
+               <term>
+                       <command>
+                               <option>conky_version</option>
+                       </command>
+                       <option>string</option>
+               </term>
+               <listitem>
+                       <para>A string containing the version of the current
+                               instance of Conky.</para>
+               </listitem>
+       </varlistentry>
+       <varlistentry>
+               <term>
+                       <command>
+                               <option>conky_config</option>
+                       </command>
+                       <option>string</option>
+               </term>
+               <listitem>
+                       <para>A string containing the path of the current Conky
+                               configuration file.</para>
+               </listitem>
+       </varlistentry>
 </variablelist>
index f44be46..55fefb6 100644 (file)
@@ -6136,6 +6136,12 @@ static void generate_text(void)
        total_updates++;
 }
 
+void set_update_interval(double interval)
+{
+       update_interval = interval;
+       update_interval_old = interval;
+}
+
 static inline int get_string_width(const char *s)
 {
 #ifdef X11
@@ -7104,6 +7110,9 @@ static void update_text(void)
                clear_text(1);
 #endif /* X11 */
        need_to_update = 1;
+#ifdef HAVE_LUA
+       llua_update_info(&info, update_interval);
+#endif /* HAVE_LUA */
 }
 
 #ifdef HAVE_SYS_INOTIFY_H
@@ -7552,6 +7561,9 @@ static void main_loop(void)
                }
 #endif /* HAVE_SYS_INOTIFY_H */
 
+#ifdef HAVE_LUA
+       llua_update_info(&info, update_interval);
+#endif /* HAVE_LUA */
                g_signal_pending = 0;
        }
        clean_up(NULL, NULL);
@@ -7884,8 +7896,7 @@ static void set_default_configurations(void)
        }
 
        no_buffers = 1;
-       update_interval = 3.0;
-       update_interval_old = update_interval;
+       set_update_interval(3);
        update_interval_bat = NOBATTERY;
        info.music_player_interval = 1.0;
        stuff_in_uppercase = 0;
@@ -8653,8 +8664,7 @@ static void load_config_file(const char *f)
                }
                CONF("update_interval") {
                        if (value) {
-                               update_interval = strtod(value, 0);
-                               update_interval_old = update_interval;
+                               set_update_interval(strtod(value, 0));
                        } else {
                                CONF_ERR;
                        }
@@ -9298,6 +9308,9 @@ void initialisation(int argc, char **argv) {
        xargv = argv;
        X11_create_window();
 #endif /* X11 */
+#ifdef HAVE_LUA
+       llua_setup_info(&info, update_interval);
+#endif /* HAVE_LUA */
 
        /* Set signal handlers */
        act.sa_handler = signal_handler;
index 3189311..0f7bbcc 100644 (file)
@@ -107,10 +107,6 @@ char *strndup(const char *s, size_t n);
 #include "weather.h"
 #endif /* WEATHER */
 
-#ifdef HAVE_LUA
-#include "llua.h"
-#endif /* HAVE_LUA */
-
 #ifdef TCP_PORT_MONITOR
 #include "tcp-portmon.h"
 #endif
@@ -309,6 +305,10 @@ struct information {
        short kflags;   /* kernel settings, see enum KFLAG */
 };
 
+#ifdef HAVE_LUA
+#include "llua.h"
+#endif /* HAVE_LUA */
+
 /* needed by linux.c and top.c -> outsource somewhere */
 enum {
        /* set to true if kernel uses "long" format for /proc/stats */
@@ -374,6 +374,8 @@ enum x_initialiser_state {
 extern int output_methods;
 extern enum x_initialiser_state x_initialised;
 
+void set_update_interval(double interval);
+
 #define DEFAULT_TEXT_BUFFER_SIZE_S "##DEFAULT_TEXT_BUFFER_SIZE"
 
 #define NOBATTERY 0
index 442608c..5c1468f 100644 (file)
@@ -63,6 +63,23 @@ static int llua_conky_parse(lua_State *L)
        return 1;                 /* number of results */
 }
 
+static int llua_conky_set_update_interval(lua_State *L)
+{
+       int n = lua_gettop(L);    /* number of arguments */
+       double value;
+       if (n != 1) {
+               lua_pushstring(L, "incorrect arguments, conky_set_update_interval(number) takes exactly 1 argument");
+               lua_error(L);
+       }
+       if (!lua_isnumber(L, 1)) {
+               lua_pushstring(L, "incorrect argument (expecting a string)");
+               lua_error(L);
+       }
+       value = lua_tonumber(L, 1);
+       set_update_interval(value);
+       return 0;                 /* number of results */
+}
+
 void llua_init(void)
 {
        const char *libs = PACKAGE_LIBDIR"/lib?.so;";
@@ -102,6 +119,9 @@ void llua_init(void)
        lua_pushcfunction(lua_L, &llua_conky_parse);
        lua_setglobal(lua_L, "conky_parse");
 
+       lua_pushcfunction(lua_L, &llua_conky_set_update_interval);
+       lua_setglobal(lua_L, "conky_set_update_interval");
+
 #if defined(X11) && defined(LUA_EXTRAS)
        /* register tolua++ user types */
        tolua_open(lua_L);
@@ -360,6 +380,12 @@ void llua_inotify_query(int wd, int mask)
 }
 #endif /* HAVE_SYS_INOTIFY_H */
 
+void llua_set_number(const char *key, double value)
+{
+       lua_pushnumber(lua_L, value);
+       lua_setfield(lua_L, -2, key);
+}
+
 #ifdef X11
 void llua_draw_pre_hook(void)
 {
@@ -383,12 +409,6 @@ void llua_set_draw_post_hook(const char *args)
        draw_post = strdup(args);
 }
 
-void llua_set_long(const char *key, long value)
-{
-       lua_pushnumber(lua_L, value);
-       lua_setfield(lua_L, -2, key);
-}
-
 #ifdef LUA_EXTRAS
 void llua_set_userdata(const char *key, const char *type, void *value)
 {
@@ -410,16 +430,16 @@ void llua_setup_window_table(int text_start_x, int text_start_y, int text_width,
 #endif /* LUA_EXTRAS */
 
 
-               llua_set_long("width", window.width);
-               llua_set_long("height", window.height);
-               llua_set_long("border_inner_margin", window.border_inner_margin);
-               llua_set_long("border_outer_margin", window.border_outer_margin);
-               llua_set_long("border_width", window.border_width);
+               llua_set_number("width", window.width);
+               llua_set_number("height", window.height);
+               llua_set_number("border_inner_margin", window.border_inner_margin);
+               llua_set_number("border_outer_margin", window.border_outer_margin);
+               llua_set_number("border_width", window.border_width);
 
-               llua_set_long("text_start_x", text_start_x);
-               llua_set_long("text_start_y", text_start_y);
-               llua_set_long("text_width", text_width);
-               llua_set_long("text_height", text_height);
+               llua_set_number("text_start_x", text_start_x);
+               llua_set_number("text_start_y", text_start_y);
+               llua_set_number("text_width", text_width);
+               llua_set_number("text_height", text_height);
 
                lua_setglobal(lua_L, "conky_window");
        }
@@ -436,15 +456,43 @@ void llua_update_window_table(int text_start_x, int text_start_y, int text_width
                return;
        }
 
-       llua_set_long("width", window.width);
-       llua_set_long("height", window.height);
+       llua_set_number("width", window.width);
+       llua_set_number("height", window.height);
 
-       llua_set_long("text_start_x", text_start_x);
-       llua_set_long("text_start_y", text_start_y);
-       llua_set_long("text_width", text_width);
-       llua_set_long("text_height", text_height);
+       llua_set_number("text_start_x", text_start_x);
+       llua_set_number("text_start_y", text_start_y);
+       llua_set_number("text_width", text_width);
+       llua_set_number("text_height", text_height);
 
        lua_setglobal(lua_L, "conky_window");
 }
 #endif /* X11 */
 
+void llua_setup_info(struct information *i, double u_interval)
+{
+       if (!lua_L) return;
+       lua_newtable(lua_L);
+
+       llua_set_number("update_interval", u_interval);
+       llua_set_number("uptime", i->uptime);
+
+       lua_setglobal(lua_L, "conky_info");
+}
+
+void llua_update_info(struct information *i, double u_interval)
+{
+       if (!lua_L) return;
+
+       lua_getglobal(lua_L, "conky_info");
+       if (lua_isnil(lua_L, -1)) {
+               /* window table isn't populated yet */
+               lua_pop(lua_L, 1);
+               return;
+       }
+
+       llua_set_number("update_interval", u_interval);
+       llua_set_number("uptime", i->uptime);
+
+       lua_setglobal(lua_L, "conky_info");
+}
+
index f19da18..5d5123d 100644 (file)
@@ -59,4 +59,7 @@ void llua_setup_window_table(int text_start_x, int text_start_y, int text_width,
 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height);
 #endif /* X11 */
 
+void llua_setup_info(struct information *i, double u_interval);
+void llua_update_info(struct information *i, double u_interval);
+
 #endif /* LUA_H_*/