ff11ef9fe93f4b9c75f1d2b79396f9ed2c93b337
[dana/obconf.git] / src / main.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    main.c for ObConf, the configuration tool for Openbox
4    Copyright (c) 2003-2008   Dana Jansens
5    Copyright (c) 2003        Tim Riley
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "main.h"
21 #include "archive.h"
22 #include "theme.h"
23 #include "appearance.h"
24 #include "windows.h"
25 #include "margins.h"
26 #include "mouse.h"
27 #include "desktops.h"
28 #include "dock.h"
29 #include "preview_update.h"
30 #include "gettext.h"
31
32 #include <gdk/gdkx.h>
33 #define SN_API_NOT_YET_FROZEN
34 #include <libsn/sn.h>
35 #undef SN_API_NOT_YET_FROZEN
36 #include <stdlib.h>
37
38 GtkWidget *mainwin = NULL;
39 GtkWidget *tabstrip = NULL;
40
41 GtkBuilder *builder;
42 xmlDocPtr doc;
43 xmlNodePtr root;
44 RrInstance *rrinst;
45 gchar *obc_config_file = NULL;
46 gint obc_tab;
47 ObtPaths *paths;
48 ObtXmlInst *parse_i;
49
50 static gchar *obc_theme_install = NULL;
51 static gchar *obc_theme_archive = NULL;
52
53 void obconf_error(gchar *msg, gboolean modal)
54 {
55     GtkWidget *d;
56
57     d = gtk_message_dialog_new(mainwin ? GTK_WINDOW(mainwin) : NULL,
58                                GTK_DIALOG_DESTROY_WITH_PARENT,
59                                GTK_MESSAGE_ERROR,
60                                GTK_BUTTONS_CLOSE,
61                                "%s", msg);
62     gtk_window_set_title(GTK_WINDOW(d), _("ObConf Error"));
63     if (modal)
64         gtk_dialog_run(GTK_DIALOG(d));
65     else {
66         g_signal_connect_swapped(G_OBJECT(d), "response",
67                                  G_CALLBACK(gtk_widget_destroy),
68                                  G_OBJECT(d));
69         gtk_widget_show(d);
70     }
71 }
72
73 static void print_version()
74 {
75     g_print("ObConf %s\n", PACKAGE_VERSION);
76     g_print(_("Copyright (c)"));
77     g_print(" 2003-2008   Dana Jansens\n");
78     g_print(_("Copyright (c)"));
79     g_print(" 2003        Tim Riley\n");
80     g_print(_("Copyright (c)"));
81     g_print(" 2007        Javeed Shaikh\n\n");
82     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
83     g_print("This is free software, and you are welcome to redistribute it\n");
84     g_print("under certain conditions. See the file COPYING for details.\n\n");
85
86     exit(EXIT_SUCCESS);
87 }
88
89 static void print_help()
90 {
91     g_print(_("Syntax: obconf [options] [ARCHIVE.obt]\n"));
92     g_print(_("\nOptions:\n"));
93     g_print(_("  --help                Display this help and exit\n"));
94     g_print(_("  --version             Display the version and exit\n"));
95     g_print(_("  --install ARCHIVE.obt Install the given theme archive and select it\n"));
96     g_print(_("  --archive THEME       Create a theme archive from the given theme directory\n"));
97     g_print(_("  --config-file FILE    Specify the path to the config file to use\n"));
98     g_print(_("  --tab NUMBER          Switch to tab number NUMBER on startup\n"));
99     g_print(_("\nPlease report bugs at %s\n\n"), PACKAGE_BUGREPORT);
100
101     exit(EXIT_SUCCESS);
102 }
103
104 static void parse_args(int argc, char **argv)
105 {
106     int i;
107
108     for (i = 1; i < argc; ++i) {
109         if (!strcmp(argv[i], "--help"))
110             print_help();
111         if (!strcmp(argv[i], "--version"))
112             print_version();
113         else if (!strcmp(argv[i], "--install")) {
114             if (i == argc - 1) /* no args left */
115                 g_printerr(_("--install requires an argument\n"));
116             else
117                 obc_theme_install = argv[++i];
118         }
119         else if (!strcmp(argv[i], "--archive")) {
120             if (i == argc - 1) /* no args left */
121                 g_printerr(_("--archive requires an argument\n"));
122             else
123                 obc_theme_archive = argv[++i];
124         }
125         else if (!strcmp(argv[i], "--config-file")) {
126             if (i == argc - 1) /* no args left */
127                 g_printerr(_("--config-file requires an argument\n"));
128             else
129                 obc_config_file = argv[++i];
130         }
131         else if (!strcmp(argv[i], "--tab")) {
132             if (i == argc - 1) /* no args left */
133                 g_printerr(_("--tab requires an argument\n"));
134             else
135                 obc_tab = MAX(atoi(argv[++i]) - 1, 0);
136         }
137         else
138             obc_theme_install = argv[i];
139     }
140 }
141
142 static gboolean get_all(Window win, Atom prop, Atom type, gint size,
143                         guchar **data, guint *num)
144 {
145     gboolean ret = FALSE;
146     gint res;
147     guchar *xdata = NULL;
148     Atom ret_type;
149     gint ret_size;
150     gulong ret_items, bytes_left;
151
152     res = XGetWindowProperty(gdk_x11_get_default_xdisplay(), win, prop, 0l, G_MAXLONG,
153                              FALSE, type, &ret_type, &ret_size,
154                              &ret_items, &bytes_left, &xdata);
155     if (res == Success) {
156         if (ret_size == size && ret_items > 0) {
157             guint i;
158
159             *data = g_malloc(ret_items * (size / 8));
160             for (i = 0; i < ret_items; ++i)
161                 switch (size) {
162                 case 8:
163                     (*data)[i] = xdata[i];
164                     break;
165                 case 16:
166                     ((guint16*)*data)[i] = ((gushort*)xdata)[i];
167                     break;
168                 case 32:
169                     ((guint32*)*data)[i] = ((gulong*)xdata)[i];
170                     break;
171                 default:
172                     g_assert_not_reached(); /* unhandled size */
173                 }
174             *num = ret_items;
175             ret = TRUE;
176         }
177         XFree(xdata);
178     }
179     return ret;
180 }
181
182 static gboolean prop_get_string_utf8(Window win, Atom prop, gchar **ret)
183 {
184     gchar *raw;
185     gchar *str;
186     guint num;
187
188     if (get_all(win, prop,
189                 gdk_x11_get_xatom_by_name("UTF8_STRING"),
190                 8,(guchar**)&raw, &num))
191     {
192         str = g_strndup(raw, num); /* grab the first string from the list */
193         g_free(raw);
194         if (g_utf8_validate(str, -1, NULL)) {
195             *ret = str;
196             return TRUE;
197         }
198         g_free(str);
199     }
200     return FALSE;
201 }
202
203 int main(int argc, char **argv)
204 {
205     gchar *p;
206     GError *error = NULL;
207     gboolean exit_with_error = FALSE;
208
209     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
210     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
211     textdomain(PACKAGE_NAME);
212
213     gtk_init(&argc, &argv);
214     parse_args(argc, argv);
215
216     if (obc_theme_archive) {
217         archive_create(obc_theme_archive);
218         return 0;
219     }
220
221     p = g_build_filename(GLADEDIR, "obconf.ui", NULL);
222     builder = gtk_builder_new();
223     gtk_builder_add_from_file(builder, p, &error);
224     g_free(p);
225
226     if (error) {
227         obconf_error(_("Failed to load the obconf.ui interface file. You have probably failed to install ObConf properly."), TRUE);
228         g_printerr("%s\n", error->message);
229         g_error_free(error);
230         exit_with_error = TRUE;
231     }
232
233     paths = obt_paths_new();
234     parse_i = obt_xml_instance_new();
235     rrinst = RrInstanceNew(gdk_x11_get_default_xdisplay(), gdk_x11_get_default_screen());
236
237     if (!obc_config_file) {
238         gchar *p;
239
240         if (prop_get_string_utf8(GDK_ROOT_WINDOW(),
241                                  gdk_x11_get_xatom_by_name("_OB_CONFIG_FILE"),
242                                  &p))
243         {
244             obc_config_file = g_filename_from_utf8(p, -1, NULL, NULL, NULL);
245             g_free(p);
246         }
247     }
248
249     xmlIndentTreeOutput = 1;
250     if (!((obc_config_file &&
251            obt_xml_load_file(parse_i, obc_config_file, "openbox_config")) ||
252           obt_xml_load_config_file(parse_i, "openbox", "rc.xml",
253                                    "openbox_config")))
254     {
255         obconf_error(_("Failed to load an rc.xml. You have probably failed to install Openbox properly."), TRUE);
256         exit_with_error = TRUE;
257     }
258     else {
259         doc = obt_xml_doc(parse_i);
260         root = obt_xml_root(parse_i);
261     }
262
263     /* look for parsing errors */
264     {
265         xmlErrorPtr e = xmlGetLastError();
266         if (e) {
267             char *a = g_strdup_printf
268                 (_("Error while parsing the Openbox configuration file.  Your configuration file is not valid XML.\n\nMessage: %s"),
269                  e->message);
270             obconf_error(a, TRUE);
271             g_free(a);
272             exit_with_error = TRUE;
273         }
274     }
275
276     if (!exit_with_error) {
277         gtk_builder_connect_signals(builder, NULL);
278
279         {
280             gchar *s = g_strdup_printf
281                 ("<span weight=\"bold\" size=\"xx-large\">ObConf %s</span>",
282                  PACKAGE_VERSION);
283             gtk_label_set_markup(GTK_LABEL
284                                  (gtk_builder_get_object(builder, "title_label")),
285                                  s);
286             g_free(s);
287         }
288
289         theme_setup_tab();
290         appearance_setup_tab();
291         windows_setup_tab();
292         moveresize_setup_tab();
293         mouse_setup_tab();
294         desktops_setup_tab();
295         margins_setup_tab();
296         dock_setup_tab();
297
298         mainwin = get_widget("main_window");
299         tabstrip = get_widget("tabstrip");
300
301         if (obc_theme_install)
302             theme_install(obc_theme_install);
303         else
304             theme_load_all();
305
306         /* the main window is not shown here ! it is shown when the theme
307            previews are completed */
308         gtk_main();
309
310         preview_update_set_active_font(NULL);
311         preview_update_set_inactive_font(NULL);
312         preview_update_set_menu_header_font(NULL);
313         preview_update_set_menu_item_font(NULL);
314         preview_update_set_osd_active_font(NULL);
315         preview_update_set_osd_inactive_font(NULL);
316         preview_update_set_title_layout(NULL);
317     }
318
319     RrInstanceFree(rrinst);
320     obt_xml_instance_unref(parse_i);
321     obt_paths_unref(paths);
322
323     xmlFreeDoc(doc);
324     return 0;
325 }
326
327 gboolean on_main_window_delete_event(GtkWidget *w, GdkEvent *e, gpointer d)
328 {
329     gtk_main_quit();
330     return FALSE;
331 }
332
333 void on_close_clicked()
334 {
335     gtk_main_quit();
336 }
337
338 void obconf_show_main()
339 {
340     SnDisplay *sn_d;
341     SnLauncheeContext *sn_cx;
342
343     if (gtk_widget_get_visible(mainwin)) return;
344
345     gtk_widget_show_all(mainwin);
346
347     /* Focus on the tab number specified by --tab. */
348     if (obc_tab)
349         gtk_notebook_set_current_page(GTK_NOTEBOOK(tabstrip), obc_tab);
350
351
352     sn_d = sn_display_new(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
353                           NULL, NULL);
354
355     sn_cx = sn_launchee_context_new_from_environment
356         (sn_d, gdk_screen_get_number(gdk_display_get_default_screen
357                                      (gdk_display_get_default())));
358
359     if (sn_cx)
360         sn_launchee_context_setup_window
361             (sn_cx, GDK_WINDOW_XID(gtk_widget_get_window(mainwin)));
362
363     if (sn_cx)
364         sn_launchee_context_complete(sn_cx);
365
366     if (sn_cx)
367         sn_launchee_context_unref(sn_cx);
368     sn_display_unref(sn_d);
369 }