]> Dogcows Code - chaz/openbox/blobdiff - openbox/actions.c
add code for interactive actions
[chaz/openbox] / openbox / actions.c
index 48778faf05bc9dfcdbd752e96d671f50f210ff73..7087f37d71557e196608308be4a2454ded875cd8 100644 (file)
 */
 
 #include "actions.h"
+#include "gettext.h"
+#include "grab.h"
 
-static void actions_unregister(ObActionsDefinition *def);
+static void     actions_definition_ref(ObActionsDefinition *def);
+static void     actions_definition_unref(ObActionsDefinition *def);
+static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
+static void     actions_interactive_end_act();
+
+static ObActionsAct *interactive_act = NULL;
+static guint         interactive_initial_state = 0;
 
 struct _ObActionsDefinition {
+    guint ref;
+
     gchar *name;
-    gboolean interactive;
+    ObActionsType type;
 
-    ObActionsDataParseFunc parse;
+    ObActionsDataSetupFunc setup;
     ObActionsDataFreeFunc free;
     ObActionsRunFunc run;
+    ObActionsInteractiveInputFunc i_input;
+    ObActionsInteractiveCancelFunc i_cancel;
+};
 
-    gpointer action_data;
+struct _ObActionsAct {
+    guint ref;
+
+    ObActionsDefinition *def;
+    gpointer options;
 };
 
 static GSList *registered = NULL;
@@ -47,17 +64,18 @@ void actions_shutdown(gboolean reconfig)
 
     /* free all the registered actions */
     while (registered) {
-        actions_unregister(registered->data);
+        actions_definition_unref(registered->data);
         registered = g_slist_delete_link(registered, registered);
     }
 }
 
 gboolean actions_register(const gchar *name,
-                          gboolean interactive,
+                          ObActionsType type,
                           ObActionsDataSetupFunc setup,
-                          ObActionsDataParseFunc parse,
                           ObActionsDataFreeFunc free,
-                          ObActionsRunFunc run)
+                          ObActionsRunFunc run,
+                          ObActionsInteractiveInputFunc i_input,
+                          ObActionsInteractiveCancelFunc i_cancel)
 {
     GSList *it;
     ObActionsDefinition *def;
@@ -68,21 +86,210 @@ gboolean actions_register(const gchar *name,
             return FALSE;
     }
 
+    g_assert((i_input == NULL) == (i_cancel == NULL));
+
     def = g_new(ObActionsDefinition, 1);
+    def->ref = 1;
     def->name = g_strdup(name);
-    def->interactive = interactive;
-    def->parse = parse;
+    def->type = type;
+    def->setup = setup;
     def->free = free;
     def->run = run;
-    def->action_data = setup();
+    def->i_input = i_input;
+    def->i_cancel = i_cancel;
     return TRUE;
 }
 
-static void actions_unregister(ObActionsDefinition *def)
+static void actions_definition_ref(ObActionsDefinition *def)
 {
-    if (def) {
-        def->free(def->action_data);
+    ++def->ref;
+}
+
+static void actions_definition_unref(ObActionsDefinition *def)
+{
+    if (def && --def->ref == 0) {
         g_free(def->name);
         g_free(def);
     }
 }
+
+ObActionsAct* actions_parse_string(const gchar *name)
+{
+    GSList *it;
+    ObActionsDefinition *def;
+    ObActionsAct *act = NULL;
+
+    /* find the requested action */
+    for (it = registered; it; it = g_slist_next(it)) {
+        def = it->data;
+        if (!g_ascii_strcasecmp(name, def->name))
+            break;
+        def = NULL;
+    }
+
+    /* if we found the action */
+    if (def) {
+        act = g_new(ObActionsAct, 1);
+        act->ref = 1;
+        act->def = def;
+        actions_definition_ref(act->def);
+        act->options = NULL;
+    } else
+        g_message(_("Invalid action '%s' requested. No such action exists."),
+                  name);
+
+    return act;
+}
+
+ObActionsAct* actions_parse(ObParseInst *i,
+                            xmlDocPtr doc,
+                            xmlNodePtr node)
+{
+    gchar *name;
+    ObActionsAct *act = NULL;
+
+    if (parse_attr_string("name", node, &name)) {
+        if ((act = actions_parse_string(name)))
+            /* there is more stuff to parse here */
+            act->options = act->def->setup(i, doc, node->children);
+
+        g_free(name);
+    }
+
+    return act;
+}
+
+gboolean actions_act_is_interactive(ObActionsAct *act)
+{
+    return act->def->i_cancel != NULL;
+}
+
+void actions_act_ref(ObActionsAct *act)
+{
+    ++act->ref;
+}
+
+void actions_act_unref(ObActionsAct *act)
+{
+    if (act && --act->ref == 0) {
+        /* free the action specific options */
+        act->def->free(act->options);
+        /* unref the definition */
+        actions_definition_unref(act->def);
+        g_free(act);
+    }
+}
+
+static void actions_setup_data(ObActionsData *data,
+                               ObUserAction uact,
+                               Time time,
+                               guint state,
+                               gint x,
+                               gint y)
+{
+    data->any.uact = uact;
+    data->any.time = time;
+    data->any.state = state;
+    data->any.x = x;
+    data->any.y = y;
+}
+
+void actions_run_acts(GSList *acts,
+                      ObUserAction uact,
+                      Time time,
+                      guint state,
+                      gint x,
+                      gint y,
+                      ObFrameContext con,
+                      struct _ObClient *client)
+{
+    GSList *it;
+
+    for (it = acts; it; it = g_slist_next(it)) {
+        ObActionsData data;
+        ObActionsAct *act = it->data;
+        gboolean ok = TRUE;
+
+        data.type = act->def->type;
+        actions_setup_data(&data, uact, time, state, x, y);
+        switch (data.type) {
+        case OB_ACTION_TYPE_GLOBAL:
+            break;
+        case OB_ACTION_TYPE_CLIENT:
+            data.client.context = con;
+            data.client.c = client;
+            break;
+        default:
+            g_assert_not_reached();
+        }
+
+        if (actions_act_is_interactive(act) &&
+            (!interactive_act || interactive_act->def != act->def))
+        {
+            ok = actions_interactive_begin_act(act, state);
+        }
+
+        /* fire the action's run function with this data */
+        if (ok) {
+            if (!act->def->run(&data, act->options))
+                actions_interactive_end_act();
+            else
+                break; /* no actions are run after the interactive one */
+        }
+    }
+}
+
+gboolean actions_interactive_act_running()
+{
+    return interactive_act != NULL;
+}
+
+void actions_interactive_cancel_act()
+{
+    if (interactive_act) {
+        interactive_act->def->i_cancel(interactive_act->options);
+        actions_interactive_end_act();
+    }
+}
+
+static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
+{
+    /* cancel the old one */
+    if (interactive_act)
+        actions_interactive_cancel_act();
+
+    if (grab_keyboard()) {
+        interactive_act = act;
+        actions_act_ref(interactive_act);
+
+        interactive_initial_state = state;
+        return TRUE;
+    }
+    else
+        return FALSE;
+}
+
+static void actions_interactive_end_act()
+{
+    if (interactive_act) {
+        ungrab_keyboard();
+
+        actions_act_unref(interactive_act);
+        interactive_act = NULL;
+    }
+}
+
+gboolean actions_interactive_input_event(XEvent *e)
+{
+    gboolean used = FALSE;
+    if (interactive_act) {
+        if (!interactive_act->def->i_input(interactive_initial_state, e,
+                                           interactive_act->options, &used))
+        {
+            used = TRUE; /* if it cancelled the action then it has to of
+                            been used */
+            actions_interactive_end_act();
+        }
+    }
+    return used;
+}
This page took 0.025029 seconds and 4 git commands to generate.