Add an action for toggling debug output.
authorDana Jansens <danakj@orodu.net>
Fri, 14 Oct 2011 23:53:06 +0000 (19:53 -0400)
committerDana Jansens <danakj@orodu.net>
Sun, 16 Oct 2011 22:58:03 +0000 (18:58 -0400)
- Rename "Debug" action to "Print" for displaying text.
- Add "Debug" action that sets/toggles showing debug messages. It has the
  standard set:<foo> argument where <foo> is on/off/toggle.

openbox/actions/debug.c
openbox/debug.c
openbox/debug.h

index 90b57b6649818985cd3ecdff8e9604fca4f9b032..3b2ce4c7456c5459cf0f3c31392ffc5b010b9677 100644 (file)
@@ -1,30 +1,44 @@
+/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- */
+
 #include "openbox/action.h"
 #include "openbox/action_list_run.h"
 #include "openbox/config_value.h"
 #include "openbox/client_set.h"
+#include "openbox/debug.h"
 #include <glib.h>
 
 typedef struct {
     gchar   *str;
-} Options;
+} PrintOptions;
+
+typedef struct {
+    gboolean toggle;
+    gboolean set;
+} DebugOptions;
 
-static gpointer setup_func(GHashTable *config);
-static void     free_func(gpointer options);
-static gboolean run_func(const ObClientSet *set,
-                         const ObActionListRun *data, gpointer options);
+static gpointer setup_print_func(GHashTable *config);
+static void     free_print_func(gpointer options);
+static gboolean run_print_func(const ObClientSet *set,
+                               const ObActionListRun *data, gpointer options);
+static gpointer setup_debug_func(GHashTable *config);
+static void     free_debug_func(gpointer options);
+static gboolean run_debug_func(const ObClientSet *set,
+                               const ObActionListRun *data, gpointer options);
 
 void action_debug_startup(void)
 {
+    action_register("Print", OB_ACTION_DEFAULT_FILTER_EMPTY,
+                    setup_print_func, free_print_func, run_print_func);
     action_register("Debug", OB_ACTION_DEFAULT_FILTER_EMPTY,
-                    setup_func, free_func, run_func);
+                    setup_debug_func, free_debug_func, run_debug_func);
 }
 
-static gpointer setup_func(GHashTable *config)
+static gpointer setup_print_func(GHashTable *config)
 {
     ObConfigValue *v;
-    Options *o;
+    PrintOptions *o;
 
-    o = g_slice_new0(Options);
+    o = g_slice_new0(PrintOptions);
 
     v = g_hash_table_lookup(config, "string");
     if (v && config_value_is_string(v))
@@ -32,20 +46,57 @@ static gpointer setup_func(GHashTable *config)
     return o;
 }
 
-static void free_func(gpointer options)
+static void free_print_func(gpointer options)
 {
-    Options *o = options;
+    PrintOptions *o = options;
     g_free(o->str);
-    g_slice_free(Options, o);
+    g_slice_free(PrintOptions, o);
 }
 
 /* Always return FALSE because its not interactive */
-static gboolean run_func(const ObClientSet *set,
+static gboolean run_print_func(const ObClientSet *set,
                          const ObActionListRun *data, gpointer options)
 {
-    Options *o = options;
-
+    PrintOptions *o = options;
     if (o->str) g_print("%s\n", o->str);
-
     return FALSE;
 }
+
+static gpointer setup_debug_func(GHashTable *config)
+{
+    ObConfigValue *v;
+    DebugOptions *o;
+
+    o = g_slice_new0(DebugOptions);
+    o->toggle = TRUE;
+    o->set = TRUE;
+
+    v = g_hash_table_lookup(config, "set");
+    if (v && config_value_is_string(v)) {
+        const gchar *s = config_value_string(v);
+        if (!g_ascii_strcasecmp(s, "on"))
+            o->toggle = FALSE;
+        else if (!g_ascii_strcasecmp(s, "off")) {
+            o->toggle = FALSE;
+            o->set = FALSE;
+        }
+    }
+
+    return o;
+}
+
+static void free_debug_func(gpointer o)
+{
+    g_slice_free(DebugOptions, o);
+}
+
+/* Always return FALSE because its not interactive */
+static gboolean run_debug_func(const ObClientSet *set,
+                         const ObActionListRun *data, gpointer options)
+{
+  gboolean toggle = !ob_debug_get_enabled(OB_DEBUG_NORMAL);
+  if (!toggle) ob_debug("Debugging output disabled");
+  ob_debug_enable(OB_DEBUG_NORMAL, toggle);
+  if (toggle) ob_debug("Debugging output enabled");
+  return FALSE;
+}
index ad083b1712088d9c5a9c10fc430edc9808301ee6..4c66f964ab4c7d3ea34dd0badabc066d503bd8be 100644 (file)
@@ -101,6 +101,12 @@ void ob_debug_enable(ObDebugType type, gboolean enable)
     enabled_types[type] = enable;
 }
 
+gboolean ob_debug_get_enabled(ObDebugType type)
+{
+    g_assert(type < OB_DEBUG_TYPE_NUM);
+    return enabled_types[type];
+}
+
 static inline void log_print(FILE *out, const gchar* log_domain,
                              const gchar *level, const gchar *message)
 {
index 13c55988453207eb179b44d90733a8ae4fb28e3e..eafd223b8ef972157df069d61e7d4700f3388b45 100644 (file)
@@ -38,6 +38,8 @@ void ob_debug_type(ObDebugType type, const gchar *a, ...);
 
 void ob_debug_enable(ObDebugType type, gboolean enable);
 
+gboolean ob_debug_get_enabled(ObDebugType type);
+
 void ob_debug_show_prompts(void);
 
 #endif