r339: The menu key bindings are now only saved if they actually changed.
[rox-filer.git] / ROX-Filer / src / main.c
blob97b3693bc8a23ba169bc84e2640f5f7f3cb4e6fe
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@users.sourceforge.net>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <signal.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <pwd.h>
32 #include <grp.h>
34 #ifdef HAVE_GETOPT_LONG
35 # include <getopt.h>
36 #endif
38 #include <gtk/gtk.h>
39 #include "collection.h"
41 #include "global.h"
43 #include "main.h"
44 #include "support.h"
45 #include "gui_support.h"
46 #include "filer.h"
47 #include "display.h"
48 #include "mount.h"
49 #include "menu.h"
50 #include "dnd.h"
51 #include "options.h"
52 #include "choices.h"
53 #include "type.h"
54 #include "pixmaps.h"
55 #include "dir.h"
56 #include "action.h"
57 #include "i18n.h"
58 #include "remote.h"
59 #include "pinboard.h"
60 #include "run.h"
61 #include "toolbar.h"
63 int number_of_windows = 0; /* Quit when this reaches 0 again... */
64 int to_error_log = -1; /* Write here to log errors */
66 uid_t euid;
67 gid_t egid;
68 int ngroups; /* Number of supplemental groups */
69 gid_t *supplemental_groups = NULL;
70 char *home_dir, *app_dir;
72 /* Static prototypes */
73 static void show_features(void);
76 #define COPYING \
77 N_("Copyright (C) 2000 Thomas Leonard.\n" \
78 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
79 "to the extent permitted by law.\n" \
80 "You may redistribute copies of ROX-Filer\n" \
81 "under the terms of the GNU General Public License.\n" \
82 "For more information about these matters, " \
83 "see the file named COPYING.\n")
85 #ifdef HAVE_GETOPT_LONG
86 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
87 # define SHORT_ONLY_WARNING ""
88 #else
89 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
90 # define SHORT_ONLY_WARNING \
91 _("NOTE: Your system does not support long options - \n" \
92 "you must use the short versions instead.\n\n")
93 #endif
95 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
96 "Open each directory or file listed, or the current working\n" \
97 "directory if no arguments are given.\n\n" \
98 " -b, --bottom=DIR open DIR as a bottom-edge panel\n" \
99 " -h, --help display this help and exit\n" \
100 " -n, --new start a new filer, even if already running\n" \
101 " -o, --override override window manager control of panels\n" \
102 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
103 " -t, --top=DIR open DIR as a top-edge panel\n" \
104 " -v, --version display the version information and exit\n" \
105 "\nThe latest version can be found at:\n" \
106 "\thttp://rox.sourceforge.net\n" \
107 "\nReport bugs to <tal197@users.sourceforge.net>.\n")
109 #define SHORT_OPS "t:b:op:hvn"
111 #ifdef HAVE_GETOPT_LONG
112 static struct option long_opts[] =
114 {"top", 1, NULL, 't'},
115 {"bottom", 1, NULL, 'b'},
116 {"override", 0, NULL, 'o'},
117 {"pinboard", 1, NULL, 'p'},
118 {"help", 0, NULL, 'h'},
119 {"version", 0, NULL, 'v'},
120 {"new", 0, NULL, 'n'},
121 {NULL, 0, NULL, 0},
123 #endif
125 /* Take control of panels away from WM? */
126 gboolean override_redirect = FALSE;
128 /* Always start a new filer, even if one seems to be already running */
129 gboolean new_copy = FALSE;
131 /* Maps child PIDs to Callback pointers */
132 static GHashTable *death_callbacks = NULL;
133 static gboolean child_died_flag = FALSE;
135 /* This is called as a signal handler; simply ensures that
136 * child_died_callback() will get called later.
138 static void child_died(int signum)
140 child_died_flag = TRUE;
141 write(to_error_log, '\0', 1); /* Wake up! */
144 static void child_died_callback(void)
146 int status;
147 int child;
149 child_died_flag = FALSE;
151 /* Find out which children exited and allow them to die */
154 Callback *cb;
156 child = waitpid(-1, &status, WNOHANG);
158 if (child == 0 || child == -1)
159 return;
161 cb = g_hash_table_lookup(death_callbacks, (gpointer) child);
162 if (cb)
164 cb->callback(cb->data);
165 g_hash_table_remove(death_callbacks, (gpointer) child);
168 } while (1);
171 #define BUFLEN 40
172 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
174 char buf[BUFLEN];
175 static GtkWidget *log = NULL;
176 static GtkWidget *window = NULL;
177 ssize_t len;
179 if (!window)
181 GtkWidget *hbox, *scrollbar;
183 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
184 gtk_window_set_title(GTK_WINDOW(window),
185 _("ROX-Filer message log"));
186 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
187 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
188 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
189 gtk_widget_hide, GTK_OBJECT(window));
192 hbox = gtk_hbox_new(FALSE, 0);
193 gtk_container_add(GTK_CONTAINER(window), hbox);
194 scrollbar = gtk_vscrollbar_new(NULL);
196 log = gtk_text_new(NULL,
197 gtk_range_get_adjustment(GTK_RANGE(scrollbar)));
198 gtk_box_pack_start(GTK_BOX(hbox), log, TRUE, TRUE, 0);
199 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, TRUE, 0);
202 len = read(source, buf, BUFLEN);
204 if (child_died_flag)
206 child_died_callback();
207 if (len == 1 && !*buf)
208 return;
211 if (len > 0)
213 if (!GTK_WIDGET_MAPPED(window))
214 gtk_widget_show_all(window);
215 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
219 /* The value that goes with an option */
220 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
222 int main(int argc, char **argv)
224 int stderr_pipe[2];
225 int i;
226 struct sigaction act;
227 guchar *tmp;
229 /* This is a list of \0 separated strings. Each string starts with a
230 * character indicating what kind of operation to perform:
232 * fFILE open this file (or directory)
233 * dDIR open this dir (even if it looks like an app)
234 * pPIN display this pinboard
235 * tDIR open DIR as a top-panel
236 * bDIR open DIR as a bottom-panel
238 GString *to_open;
240 to_open = g_string_new(NULL);
242 choices_init();
243 i18n_init();
245 home_dir = g_get_home_dir();
246 app_dir = g_strdup(getenv("APP_DIR"));
247 if (app_dir)
248 unsetenv("APP_DIR");
249 else
251 g_warning("APP_DIR environment variable was unset!\n"
252 "Use the AppRun script to invoke ROX-Filer...\n");
253 app_dir = g_get_current_dir();
256 death_callbacks = g_hash_table_new(NULL, NULL);
258 #ifdef HAVE_LIBVFS
259 mc_vfs_init();
260 #endif
261 gtk_init(&argc, &argv);
263 while (1)
265 int c;
266 #ifdef HAVE_GETOPT_LONG
267 int long_index;
268 c = getopt_long(argc, argv, SHORT_OPS,
269 long_opts, &long_index);
270 #else
271 c = getopt(argc, argv, SHORT_OPS);
272 #endif
274 if (c == EOF)
275 break; /* No more options */
277 switch (c)
279 case 'n':
280 new_copy = TRUE;
281 break;
282 case 'o':
283 override_redirect = TRUE;
284 break;
285 case 'v':
286 fprintf(stderr, "ROX-Filer %s\n", VERSION);
287 fprintf(stderr, _(COPYING));
288 show_features();
289 return EXIT_SUCCESS;
290 case 'h':
291 fprintf(stderr, _(HELP));
292 fprintf(stderr, _(SHORT_ONLY_WARNING));
293 return EXIT_SUCCESS;
294 case 't':
295 case 'b':
296 g_string_append_c(to_open, '<');
297 g_string_append_c(to_open, c);
298 g_string_append_c(to_open, '>');
299 tmp = pathdup(VALUE);
300 g_string_append(to_open, tmp);
301 g_free(tmp);
302 break;
303 case 'p':
304 g_string_append(to_open, "<p>");
305 g_string_append(to_open, VALUE);
306 break;
307 default:
308 printf(_(USAGE));
309 return EXIT_FAILURE;
313 i = optind;
314 while (i < argc)
316 tmp = pathdup(argv[i++]);
318 g_string_append(to_open, "<f>");
319 g_string_append(to_open, tmp);
322 if (to_open->len == 0)
324 guchar *dir;
326 dir = g_get_current_dir();
327 g_string_sprintf(to_open, "<d>%s", dir);
328 g_free(dir);
331 gui_support_init();
332 if (remote_init(to_open, new_copy))
333 return EXIT_SUCCESS; /* Already running */
334 pixmaps_init();
336 dir_init();
337 menu_init();
338 dnd_init();
339 filer_init();
340 toolbar_init();
341 display_init();
342 mount_init();
343 options_init();
344 type_init();
345 action_init();
346 pinboard_init();
348 options_load();
350 /* Let child processes die */
351 act.sa_handler = child_died;
352 sigemptyset(&act.sa_mask);
353 act.sa_flags = SA_NOCLDSTOP;
354 sigaction(SIGCHLD, &act, NULL);
356 /* Ignore SIGPIPE - check for EPIPE errors instead */
357 act.sa_handler = SIG_IGN;
358 sigemptyset(&act.sa_mask);
359 act.sa_flags = 0;
360 sigaction(SIGPIPE, &act, NULL);
362 euid = geteuid();
363 egid = getegid();
364 ngroups = getgroups(0, NULL);
365 if (ngroups < 0)
366 ngroups = 0;
367 else if (ngroups > 0)
369 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
370 getgroups(ngroups, supplemental_groups);
373 if (euid == 0)
375 if (get_choice(_("!!!DANGER!!!"),
376 _("Running ROX-Filer as root is VERY dangerous. If I "
377 "had a warranty (I don't) then doing this would "
378 "void it"), 2,
379 _("Don't click here"), _("Quit")) != 0)
380 exit(EXIT_SUCCESS);
383 run_list(to_open->str);
384 g_string_free(to_open, TRUE);
386 pipe(stderr_pipe);
387 close_on_exec(stderr_pipe[0], TRUE);
388 close_on_exec(stderr_pipe[1], TRUE);
389 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
390 to_error_log = stderr_pipe[1];
392 if (number_of_windows > 0)
393 gtk_main();
395 return EXIT_SUCCESS;
398 static void show_features(void)
400 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
401 g_printerr("%s... %s\n", _("VFS support"),
402 #ifdef HAVE_LIBVFS
403 _("Yes")
404 #else
405 _("No (couldn't find a valid libvfs)")
406 #endif
408 g_printerr("%s... %s\n", _("ImLib support"),
409 #ifdef HAVE_IMLIB
410 _("Yes")
411 #else
412 _("No (the imlib-config command didn't work)")
413 #endif
417 /* Register a function to be called when process number 'child' dies. */
418 void on_child_death(int child, CallbackFn callback, gpointer data)
420 Callback *cb;
422 g_return_if_fail(callback != NULL);
424 cb = g_new(Callback, 1);
426 cb->callback = callback;
427 cb->data = data;
429 g_hash_table_insert(death_callbacks, (gpointer) child, cb);