r267: Running a second copy of the filer passes the request to the first.
[rox-filer/ma.git] / ROX-Filer / src / main.c
blobb2b96f2ed351790efdba3d3e4c812fca09014875
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
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 "main.h"
42 #include "support.h"
43 #include "gui_support.h"
44 #include "filer.h"
45 #include "mount.h"
46 #include "menu.h"
47 #include "dnd.h"
48 #include "options.h"
49 #include "choices.h"
50 #include "type.h"
51 #include "pixmaps.h"
52 #include "dir.h"
53 #include "action.h"
54 #include "i18n.h"
55 #include "remote.h"
57 int number_of_windows = 0; /* Quit when this reaches 0 again... */
58 int to_error_log = -1; /* Write here to log errors */
60 uid_t euid;
61 gid_t egid;
62 int ngroups; /* Number of supplemental groups */
63 gid_t *supplemental_groups = NULL;
64 char *home_dir;
66 /* Static prototypes */
67 static void show_features(void);
70 #define COPYING \
71 N_("Copyright (C) 2000 Thomas Leonard.\n" \
72 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
73 "to the extent permitted by law.\n" \
74 "You may redistribute copies of ROX-Filer\n" \
75 "under the terms of the GNU General Public License.\n" \
76 "For more information about these matters, " \
77 "see the file named COPYING.\n")
79 #ifdef HAVE_GETOPT_LONG
80 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
81 # define SHORT_ONLY_WARNING ""
82 #else
83 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
84 # define SHORT_ONLY_WARNING \
85 _("NOTE: Your system does not support long options - \n" \
86 "you must use the short versions instead.\n\n")
87 #endif
89 #define SHORT_OPS "t:b:ohvn"
91 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [DIR]...\n" \
92 "Open filer windows showing each directory listed, or $HOME \n" \
93 "if no directories are given.\n\n" \
94 " -b, --bottom=DIR open DIR as a bottom-edge panel\n" \
95 " -h, --help display this help and exit\n" \
96 " -n, --new start a new filer, even if already running\n" \
97 " -o, --override override window manager control of panels\n" \
98 " -t, --top=DIR open DIR as a top-edge panel\n" \
99 " -v, --version display the version information and exit\n" \
100 "\nReport bugs to <tal197@ecs.soton.ac.uk>.\n")
102 #ifdef HAVE_GETOPT_LONG
103 static struct option long_opts[] =
105 {"top", 1, NULL, 't'},
106 {"bottom", 1, NULL, 'b'},
107 {"override", 0, NULL, 'o'},
108 {"help", 0, NULL, 'h'},
109 {"version", 0, NULL, 'v'},
110 {"new", 0, NULL, 'n'},
111 {NULL, 0, NULL, 0},
113 #endif
115 /* Take control of panels away from WM? */
116 gboolean override_redirect = FALSE;
118 /* Always start a new filer, even if one seems to be already running */
119 gboolean new_copy = FALSE;
121 /* Maps child PIDs to Callback pointers */
122 static GHashTable *death_callbacks = NULL;
123 static gboolean child_died_flag = FALSE;
125 /* This is called as a signal handler; simply ensures that
126 * child_died_callback() will get called later.
128 static void child_died(int signum)
130 child_died_flag = TRUE;
131 write(to_error_log, '\0', 1); /* Wake up! */
134 static void child_died_callback(void)
136 int status;
137 int child;
139 child_died_flag = FALSE;
141 /* Find out which children exited and allow them to die */
144 Callback *cb;
146 child = waitpid(-1, &status, WNOHANG);
148 if (child == 0 || child == -1)
149 return;
151 cb = g_hash_table_lookup(death_callbacks, (gpointer) child);
152 if (cb)
154 cb->callback(cb->data);
155 g_hash_table_remove(death_callbacks, (gpointer) child);
158 } while (1);
161 #define BUFLEN 40
162 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
164 char buf[BUFLEN];
165 static GtkWidget *log = NULL;
166 static GtkWidget *window = NULL;
167 ssize_t len;
169 if (!window)
171 GtkWidget *hbox, *scrollbar;
173 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
174 gtk_window_set_title(GTK_WINDOW(window),
175 _("ROX-Filer message log"));
176 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
177 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
178 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
179 gtk_widget_hide, GTK_OBJECT(window));
182 hbox = gtk_hbox_new(FALSE, 0);
183 gtk_container_add(GTK_CONTAINER(window), hbox);
184 scrollbar = gtk_vscrollbar_new(NULL);
186 log = gtk_text_new(NULL,
187 gtk_range_get_adjustment(GTK_RANGE(scrollbar)));
188 gtk_box_pack_start(GTK_BOX(hbox), log, TRUE, TRUE, 0);
189 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, TRUE, 0);
192 len = read(source, buf, BUFLEN);
194 if (child_died_flag)
196 child_died_callback();
197 if (len == 1 && !*buf)
198 return;
201 if (len > 0)
203 if (!GTK_WIDGET_MAPPED(window))
204 gtk_widget_show_all(window);
205 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
209 int main(int argc, char **argv)
211 int stderr_pipe[2];
212 struct sigaction act;
213 GList *panel_dirs = NULL;
214 GList *panel_sides = NULL;
216 choices_init();
217 i18n_init();
219 home_dir = g_get_home_dir();
220 death_callbacks = g_hash_table_new(NULL, NULL);
222 #ifdef HAVE_LIBVFS
223 mc_vfs_init();
224 #endif
225 gtk_init(&argc, &argv);
227 while (1)
229 int c;
230 #ifdef HAVE_GETOPT_LONG
231 int long_index;
232 c = getopt_long(argc, argv, SHORT_OPS,
233 long_opts, &long_index);
234 #else
235 c = getopt(argc, argv, SHORT_OPS);
236 #endif
238 if (c == EOF)
239 break; /* No more options */
241 switch (c)
243 case 'n':
244 new_copy = TRUE;
245 break;
246 case 'o':
247 override_redirect = TRUE;
248 break;
249 case 'v':
250 printf("ROX-Filer %s\n", VERSION);
251 printf(_(COPYING));
252 show_features();
253 return EXIT_SUCCESS;
254 case 'h':
255 printf(_(HELP));
256 printf(_(SHORT_ONLY_WARNING));
257 return EXIT_SUCCESS;
258 case 't':
259 case 'b':
260 panel_sides = g_list_prepend(panel_sides,
261 (gpointer) c);
262 panel_dirs = g_list_prepend(panel_dirs,
263 *optarg == '=' ? optarg + 1
264 : optarg);
265 break;
266 default:
267 printf(_(USAGE));
268 return EXIT_FAILURE;
272 gui_support_init();
273 if (remote_init(argc - optind, argv + optind, new_copy))
274 return EXIT_SUCCESS; /* Already running */
275 pixmaps_init();
276 dir_init();
277 menu_init();
278 dnd_init();
279 filer_init();
280 mount_init();
281 options_init();
282 type_init();
283 action_init();
285 options_load();
287 /* Let child processes die */
288 act.sa_handler = child_died;
289 sigemptyset(&act.sa_mask);
290 act.sa_flags = SA_NOCLDSTOP;
291 sigaction(SIGCHLD, &act, NULL);
293 /* Ignore SIGPIPE - check for EPIPE errors instead */
294 act.sa_handler = SIG_IGN;
295 sigemptyset(&act.sa_mask);
296 act.sa_flags = 0;
297 sigaction(SIGPIPE, &act, NULL);
299 euid = geteuid();
300 egid = getegid();
301 ngroups = getgroups(0, NULL);
302 if (ngroups < 0)
303 ngroups = 0;
304 else if (ngroups > 0)
306 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
307 getgroups(ngroups, supplemental_groups);
310 if (euid == 0)
312 if (get_choice(_("!!!DANGER!!!"),
313 _("Running ROX-Filer as root is VERY dangerous. If I "
314 "had a warranty (I don't) then doing this would "
315 "void it"), 2,
316 _("Don't click here"), _("Quit")) != 0)
317 exit(EXIT_SUCCESS);
320 if (optind == argc && !panel_dirs)
321 filer_opendir(home_dir, PANEL_NO);
322 else
324 int i = optind;
325 GList *dir = panel_dirs;
326 GList *side = panel_sides;
328 while (dir)
330 int c = (int) side->data;
332 filer_opendir((char *) dir->data,
333 c == 't' ? PANEL_TOP : PANEL_BOTTOM);
334 dir = dir->next;
335 side = side->next;
338 g_list_free(dir);
339 g_list_free(side);
341 while (i < argc)
342 filer_opendir(argv[i++], PANEL_NO);
345 pipe(stderr_pipe);
346 close_on_exec(stderr_pipe[0], TRUE);
347 close_on_exec(stderr_pipe[1], TRUE);
348 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
349 to_error_log = stderr_pipe[1];
351 gtk_main();
353 return EXIT_SUCCESS;
356 static void show_features(void)
358 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
359 g_printerr("%s... %s\n", _("VFS support"),
360 #ifdef HAVE_LIBVFS
361 _("Yes")
362 #else
363 _("No (couldn't find a valid libvfs)")
364 #endif
366 g_printerr("%s... %s\n", _("ImLib support"),
367 #ifdef HAVE_IMLIB
368 _("Yes")
369 #else
370 _("No (the imlib-config command didn't work)")
371 #endif
375 /* Register a function to be called when process number 'child' dies. */
376 void on_child_death(int child, CallbackFn callback, gpointer data)
378 Callback *cb;
380 g_return_if_fail(callback != NULL);
382 cb = g_new(Callback, 1);
384 cb->callback = callback;
385 cb->data = data;
387 g_hash_table_insert(death_callbacks, (gpointer) child, cb);