pipe-menu helper library?

Mike logan at dct.com
Mon Nov 10 16:07:36 EST 2003


Heyas,

Today I decided to convert my pipe-menu apps to use xmlSaveFormatFileEnc(). Made sense to use this since all of them link against -lobparser (and libxml) anyway. This allows me to get rid of the ob3_string_to_xml_safe() function I had been using for so long, and have unsafe strings automatically converted by libxml.

My apps use the same functions to build up, then print out the xml menu... I'm not sure how many others are making openbox3 pipe-menu apps at the moment, but how about a simple helper library for this?

Here's what I've got right now...

/* -- openbox pipe-menu helper functions ------------------------------------ */

gboolean ob3_pipe_menu_start = FALSE;

xmlDocPtr ob3_pipe_menu_doc;
xmlNodePtr ob3_pipe_menu_node;

/* initialize pipe-menu helpers, setup pipe-menu xmlDocPtr, xmlNodePtr */
void ob3_pipe_menu_startup(void)
{
    g_return_if_fail(ob3_pipe_menu_start != TRUE);

    ob3_pipe_menu_start = TRUE;
    ob3_pipe_menu_doc = xmlNewDoc("1.0");
    ob3_pipe_menu_node = xmlNewDocNode(ob3_pipe_menu_doc, NULL,
                                      "openbox_pipe_menu", NULL);
    xmlDocSetRootElement(ob3_pipe_menu_doc, ob3_pipe_menu_node);
}

/* print xml pipe-menu contents, free pipe-menu xmlDocPtr */
void ob3_pipe_menu_shutdown(void)
{
    g_return_if_fail(ob3_pipe_menu_start != FALSE);

    if(ob3_pipe_menu_node->children != NULL)
        xmlSaveFormatFileEnc("-", ob3_pipe_menu_doc, "UTF-8", 1);

    xmlFreeDoc(ob3_pipe_menu_doc);
}

/* add pipe-menu separator item */
void ob3_pipe_menu_separator_add(void)
{
    g_return_if_fail(ob3_pipe_menu_start != FALSE);

    xmlNewTextChild(ob3_pipe_menu_node, NULL, "separator", NULL);
}

/* add pipe-menu item */
void ob3_pipe_menu_item_add(const gchar *label, const gchar *action)
{
    xmlNodePtr n;

    g_return_if_fail(ob3_pipe_menu_start != FALSE);

    g_return_if_fail(label != NULL && label[0] != '\0');

    n = xmlNewTextChild(ob3_pipe_menu_node, NULL, "item", NULL);
    xmlSetProp(n, "label", label);

    if(action != NULL && action[0] != '\0') {
        xmlNodePtr p = xmlNewTextChild(n, NULL, "action", NULL);
        xmlSetProp(p, "name", "Execute");

        xmlNewTextChild(p, NULL, "execute", action);
    }
}

/* -- openbox pipe-menu helper functions ------------------------------------ */

I don't care much for the global variables, but I also wouldn't care to pass xmlDoc/NodePtr's to/fro throughout my apps. Anyway, it fits in alright with the other openbox libraries.

Some of this could be pretty logan-centric, but I don't think there's too many other things that could be added to a menu item. ob3_pipe_menu_item_add() and ob3_pipe_menu_separator_add() in their current form wouldn't be any good for menus inside menus.

/* -- small example --------------------------------------------------------- */

int main(void)
{
    ob3_pipe_menu_startup();

    ob3_pipe_menu_item_add("Label Here", "Execute Action Here");
    ob3_pipe_menu_separator_add();
    ob3_pipe_menu_item_add("Label With No Execute Action", NULL);

    ob3_pipe_menu_shutdown();

    exit(0);
}

Would print:

<?xml version="1.0" encoding="UTF-8"?>
<openbox_pipe_menu>
  <item label="Label Here">
    <action name="Execute">
      <execute>Execute Action Here</execute>
    </action>
  </item>
  <separator/>
  <item label="Label With No Execute Action"/>
</openbox_pipe_menu>

/* -- small example --------------------------------------------------------- */

Thoughts or suggestions? Having a library such as this included in openbox would be very handy for me. Plus everyone who does pipe-menus in c would be able to use the same stuff. :)

Mike



More information about the openbox mailing list