]> Dogcows Code - chaz/openbox/blobdiff - plugins/menu/timed_menu.c
changes to the timer api, pass the timer to the callback function.
[chaz/openbox] / plugins / menu / timed_menu.c
index e7665b5642049e49fea033090936472504c241bf..844a56419f387f94a47b7a2a223a691268cc0d61 100644 (file)
@@ -15,8 +15,8 @@
 #include "kernel/action.h"
 #include "kernel/event.h"
 
-#define TIMED_MENU(m) ((Menu *)m)
-#define TIMED_MENU_DATA(m) ((Timed_Menu_Data *)((Menu *)m)->plugin_data)
+#define TIMED_MENU(m) ((ObMenu *)m)
+#define TIMED_MENU_DATA(m) ((Timed_Menu_Data *)((ObMenu *)m)->plugin_data)
 static char *PLUGIN_NAME = "timed_menu";
 
 typedef enum {
@@ -41,7 +41,7 @@ void plugin_startup()
 { }
 void plugin_shutdown() { }
 
-void timed_menu_clean_up(Menu *m) {
+void timed_menu_clean_up(ObMenu *m) {
     if (TIMED_MENU_DATA(m)->buf != NULL) {
         fprintf(stderr, "%s", TIMED_MENU_DATA(m)->buf);
         g_free(TIMED_MENU_DATA(m)->buf);
@@ -66,7 +66,7 @@ void timed_menu_clean_up(Menu *m) {
 
 void timed_menu_read_pipe(int fd, void *d)
 {
-    Menu *menu = d;
+    ObMenu *menu = d;
     char *tmpbuf = NULL;
     unsigned long num_read;
 #ifdef DEBUG
@@ -96,23 +96,24 @@ void timed_menu_read_pipe(int fd, void *d)
                     TIMED_MENU_DATA(menu)->buf + TIMED_MENU_DATA(menu)->buflen,
                     num_realloc);
     if (num_read == 0) {
-        unsigned long count = 0;
-        char *found = NULL;
+        xmlDocPtr doc;
+        xmlNodePtr node;
+
         menu->invalid = TRUE;
         menu_clear(menu);
 
-        /* TEMP: list them */
-        while (NULL !=
-               (found = strchr(&TIMED_MENU_DATA(menu)->buf[count], '\n'))) {
-            TIMED_MENU_DATA(menu)->buf
-                [found - TIMED_MENU_DATA(menu)->buf] = '\0';
-            menu_add_entry(menu,
-                menu_entry_new_separator
-                (&TIMED_MENU_DATA(menu)->buf[count]));
-            count = found - TIMED_MENU_DATA(menu)->buf + 1;
+        TIMED_MENU_DATA(menu)->buf[TIMED_MENU_DATA(menu)->buflen] = '\0';
+
+        doc = xmlParseMemory(TIMED_MENU_DATA(menu)->buf,
+                                       TIMED_MENU_DATA(menu)->buflen);
+
+        node = xmlDocGetRootElement(doc);
+
+        if (!xmlStrcasecmp(node->name, (const xmlChar*) "timed_menu")) {
+            if ((node = parse_find_node("item", node->xmlChildrenNode)))
+                parse_menu_full(doc, node, menu, FALSE);
         }
 
-        TIMED_MENU_DATA(menu)->buf[TIMED_MENU_DATA(menu)->buflen] = '\0';
         timed_menu_clean_up(menu);
     } else if (num_read > 0) {
         TIMED_MENU_DATA(menu)->buflen += num_read;
@@ -123,9 +124,9 @@ void timed_menu_read_pipe(int fd, void *d)
     }
 }
 
-void timed_menu_timeout_handler(void *d)
+void timed_menu_timeout_handler(ObTimer *t, void *d)
 {
-    Menu *data = d;
+    ObMenu *data = d;
     if (!data->shown && TIMED_MENU_DATA(data)->fd == -1) {
         switch (TIMED_MENU_DATA(data)->type) {
             case (TIMED_MENU_PIPE): {
@@ -184,16 +185,44 @@ void timed_menu_timeout_handler(void *d)
     }
 }
 
-void *plugin_create()
+void *plugin_create(PluginMenuCreateData *data)
 {
-    Timed_Menu_Data *d = g_new(Timed_Menu_Data, 1);
-    Menu *m = menu_new("", PLUGIN_NAME, NULL);
+    char *id;
+    char *label;
+    char *timeout;
+    Timed_Menu_Data *d;
+    ObMenu *m;
     
+    parse_attr_string("id", data->node, &id);
+    parse_attr_string("label", data->node, &label);
+    
+    d = g_new(Timed_Menu_Data, 1);
+        
+    m = menu_new( (label != NULL ? label : ""),
+                  (id != NULL ? id : PLUGIN_NAME),
+                  data->parent);
+
     m->plugin = PLUGIN_NAME;
 
-    d->type = TIMED_MENU_STAT;
-    d->timer = timer_start(6000000, &timed_menu_timeout_handler, m);
-    d->command = "/home/woodblock/timed_menu_stat";
+    if (data->parent)
+        menu_add_entry(data->parent, menu_entry_new_submenu(
+                           (label != NULL ? label : ""),
+                           m));
+
+    if (!parse_attr_string("command", data->node, &d->command)) {
+        d->command = g_strdup("");
+    }
+
+    if (parse_attr_string("timeout", data->node, &timeout)) {
+        char *endptr;
+        gdouble timeout_val = g_strtod(timeout, &endptr);
+        g_free(timeout);
+        d->timer = timer_start(timeout_val * 1000000,
+                               &timed_menu_timeout_handler, m);
+    } else
+        d->timer = timer_start(600 * 1000000, &timed_menu_timeout_handler, m);
+
+    d->type = TIMED_MENU_PIPE;
     d->buf = NULL;
     d->buflen = 0;
     d->fd = -1;
@@ -202,7 +231,7 @@ void *plugin_create()
     
     m->plugin_data = (void *)d;
 
-    timed_menu_timeout_handler(m);
+    timed_menu_timeout_handler(NULL, m);
     return (void *)m;
 }
 
This page took 0.02312 seconds and 4 git commands to generate.