add the --config-file option, and use the OB_CONFIG_FILE property on root
authorDana Jansens <danakj@orodu.net>
Sat, 2 Feb 2008 14:39:20 +0000 (09:39 -0500)
committerDana Jansens <danakj@orodu.net>
Sat, 2 Feb 2008 14:42:03 +0000 (09:42 -0500)
src/main.c
src/main.h
src/tree.c

index 5769384..2a42d3a 100644 (file)
@@ -41,6 +41,7 @@ GladeXML *glade;
 xmlDocPtr doc;
 xmlNodePtr root;
 RrInstance *rrinst;
+gchar *obc_config_file = NULL;
 
 static gchar *obc_theme_install = NULL;
 static gchar *obc_theme_archive = NULL;
@@ -84,6 +85,7 @@ static void print_help()
     g_print(_("  --version             Display the version and exit\n"));
     g_print(_("  --install ARCHIVE.obt Install the given theme archive and select it\n"));
     g_print(_("  --archive THEME       Create a theme archive from the given theme directory\n"));
+    g_print(_("  --config-file FILE    Specify the path to the config file to use\n"));
     g_print(_("\nPlease report bugs at %s\n\n"), PACKAGE_BUGREPORT);
     
     exit(EXIT_SUCCESS);
@@ -109,11 +111,78 @@ static void parse_args(int argc, char **argv)
                 g_printerr(_("--archive requires an argument\n"));
             else
                 obc_theme_archive = argv[++i];
+        }
+        else if (!strcmp(argv[i], "--config-file")) {
+            if (i == argc - 1) /* no args left */
+                g_printerr(_("--config-file requires an argument\n"));
+            else
+                obc_config_file = argv[++i];
         } else
             obc_theme_install = argv[i];
     }
 }
 
+static gboolean get_all(Window win, Atom prop, Atom type, gint size,
+                        guchar **data, guint *num)
+{
+    gboolean ret = FALSE;
+    gint res;
+    guchar *xdata = NULL;
+    Atom ret_type;
+    gint ret_size;
+    gulong ret_items, bytes_left;
+
+    res = XGetWindowProperty(GDK_DISPLAY(), win, prop, 0l, G_MAXLONG,
+                             FALSE, type, &ret_type, &ret_size,
+                             &ret_items, &bytes_left, &xdata);
+    if (res == Success) {
+        if (ret_size == size && ret_items > 0) {
+            guint i;
+
+            *data = g_malloc(ret_items * (size / 8));
+            for (i = 0; i < ret_items; ++i)
+                switch (size) {
+                case 8:
+                    (*data)[i] = xdata[i];
+                    break;
+                case 16:
+                    ((guint16*)*data)[i] = ((gushort*)xdata)[i];
+                    break;
+                case 32:
+                    ((guint32*)*data)[i] = ((gulong*)xdata)[i];
+                    break;
+                default:
+                    g_assert_not_reached(); /* unhandled size */
+                }
+            *num = ret_items;
+            ret = TRUE;
+        }
+        XFree(xdata);
+    }
+    return ret;
+}
+
+static gboolean prop_get_string_utf8(Window win, Atom prop, gchar **ret)
+{
+    gchar *raw;
+    gchar *str;
+    guint num;
+
+    if (get_all(win, prop,
+                gdk_x11_get_xatom_by_name("UTF8_STRING"),
+                8,(guchar**)&raw, &num))
+    {
+        str = g_strndup(raw, num); /* grab the first string from the list */
+        g_free(raw);
+        if (g_utf8_validate(str, -1, NULL)) {
+            *ret = str;
+            return TRUE;
+        }
+        g_free(str);
+    }
+    return FALSE;
+}
+
 int main(int argc, char **argv)
 {
     gchar *p;
@@ -143,8 +212,20 @@ int main(int argc, char **argv)
     parse_paths_startup();
     rrinst = RrInstanceNew(GDK_DISPLAY(), gdk_x11_get_default_screen());
 
+    if (!obc_config_file) {
+        gchar *p;
+
+        if (prop_get_string_utf8(GDK_ROOT_WINDOW(),
+                                 gdk_x11_get_xatom_by_name("_OB_CONFIG_FILE"),
+                                 &p))
+        {
+            obc_config_file = g_filename_from_utf8(p, -1, NULL, NULL, NULL);
+            g_free(p);
+        }
+    }
+
     xmlIndentTreeOutput = 1;
-    if (!parse_load_rc(NULL, &doc, &root)) {
+    if (!parse_load_rc(obc_config_file, &doc, &root)) {
         obconf_error("Failed to load an rc.xml. You have probably failed to "
                      "install Openbox properly.");
         return 1;
index 53fe189..91c5342 100644 (file)
@@ -31,6 +31,7 @@ extern xmlDocPtr doc;
 extern xmlNodePtr root;
 extern RrInstance *rrinst;
 extern GtkWidget *mainwin;
+extern gchar *obc_config_file;
 
 #define get_widget(s) glade_xml_get_widget(glade, s)
 
index 4b1eb3b..14214a9 100644 (file)
@@ -78,15 +78,19 @@ xmlNodePtr tree_get_node(const gchar *path, const gchar *def)
 
 void tree_apply()
 {
-    gchar *p;
+    gchar *p, *d;
     gboolean err;
 
-    p = g_build_filename(parse_xdg_config_home_path(), "openbox", NULL);
-    parse_mkdir_path(p, 0700);
-    g_free(p);
+    if (obc_config_file)
+        p = g_strdup(obc_config_file);
+    else
+        p = g_build_filename(parse_xdg_config_home_path(), "openbox",
+                             "rc.xml", NULL);
+
+    d = g_path_get_dirname(p);
+    parse_mkdir_path(d, 0700);
+    g_free(d);
 
-    p = g_build_filename(parse_xdg_config_home_path(), "openbox",
-                         "rc.xml", NULL);
     err = xmlSaveFormatFile(p, doc, 1) == -1;
     if (err) {
         gchar *s;