r246: Added support for i18n. No translations yet, though!
[rox-filer.git] / ROX-Filer / src / main.c
blob1709cead1413de3e58608376b496fa3048973f50
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"
55 int number_of_windows = 0; /* Quit when this reaches 0 again... */
56 int to_error_log = -1; /* Write here to log errors */
58 uid_t euid;
59 gid_t egid;
60 int ngroups; /* Number of supplemental groups */
61 gid_t *supplemental_groups = NULL;
62 char *home_dir;
64 /* Static prototypes */
65 static void show_features(void);
68 #define VERSION "ROX-Filer 0.1.20\n" \
69 "Copyright (C) 2000 Thomas Leonard.\n" \
70 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
71 "to the extent permitted by law.\n" \
72 "You may redistribute copies of ROX-Filer\n" \
73 "under the terms of the GNU General Public License.\n" \
74 "For more information about these matters, " \
75 "see the file named COPYING.\n"
77 #ifdef HAVE_GETOPT_LONG
78 # define USAGE "Try `ROX-Filer/AppRun --help' for more information.\n"
79 # define SHORT_ONLY_WARNING ""
80 #else
81 # define USAGE "Try `ROX-Filer/AppRun -h' for more information.\n"
82 # define SHORT_ONLY_WARNING \
83 "NOTE: Your system does not support long options - \n" \
84 "you must use the short versions instead.\n\n"
85 #endif
87 #define SHORT_OPS "t:b:ohv"
89 #define HELP "Usage: ROX-Filer/AppRun [OPTION]... [DIR]...\n" \
90 "Open filer windows showing each directory listed, or $HOME \n" \
91 "if no directories are given.\n\n" \
92 " -h, --help display this help and exit\n" \
93 " -v, --version display the version information and exit\n" \
94 " -t, --top [DIR] open DIR as a top-edge panel\n" \
95 " -b, --bottom [DIR] open DIR as a bottom-edge panel\n" \
96 " -o, --override override window manager control of panels\n" \
97 "\n" SHORT_ONLY_WARNING \
98 "Report bugs to <tal197@ecs.soton.ac.uk>.\n"
100 #ifdef HAVE_GETOPT_LONG
101 static struct option long_opts[] =
103 {"top", 1, NULL, 't'},
104 {"bottom", 1, NULL, 'b'},
105 {"override", 0, NULL, 'o'},
106 {"help", 0, NULL, 'h'},
107 {"version", 0, NULL, 'v'},
108 {NULL, 0, NULL, 0},
110 #endif
112 /* Take control of panels away from WM? */
113 gboolean override_redirect = FALSE;
115 /* Maps child PIDs to Callback pointers */
116 static GHashTable *death_callbacks = NULL;
117 static gboolean child_died_flag = FALSE;
119 /* This is called as a signal handler; simply ensures that
120 * child_died_callback() will get called later.
122 static void child_died(int signum)
124 child_died_flag = TRUE;
125 write(to_error_log, '\0', 1); /* Wake up! */
128 static void child_died_callback(void)
130 int status;
131 int child;
133 child_died_flag = FALSE;
135 /* Find out which children exited and allow them to die */
138 Callback *cb;
140 child = waitpid(-1, &status, WNOHANG);
142 if (child == 0 || child == -1)
143 return;
145 cb = g_hash_table_lookup(death_callbacks, (gpointer) child);
146 if (cb)
148 cb->callback(cb->data);
149 g_hash_table_remove(death_callbacks, (gpointer) child);
152 } while (1);
155 #define BUFLEN 40
156 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
158 char buf[BUFLEN];
159 static GtkWidget *log = NULL;
160 static GtkWidget *window = NULL;
161 ssize_t len;
163 if (!window)
165 GtkWidget *hbox, *scrollbar;
167 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
168 gtk_window_set_title(GTK_WINDOW(window),
169 "ROX-Filer message log");
170 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
171 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
172 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
173 gtk_widget_hide, GTK_OBJECT(window));
176 hbox = gtk_hbox_new(FALSE, 0);
177 gtk_container_add(GTK_CONTAINER(window), hbox);
178 scrollbar = gtk_vscrollbar_new(NULL);
180 log = gtk_text_new(NULL,
181 gtk_range_get_adjustment(GTK_RANGE(scrollbar)));
182 gtk_box_pack_start(GTK_BOX(hbox), log, TRUE, TRUE, 0);
183 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, TRUE, 0);
186 len = read(source, buf, BUFLEN);
188 if (child_died_flag)
190 child_died_callback();
191 if (len == 1 && !*buf)
192 return;
195 if (len > 0)
197 if (!GTK_WIDGET_MAPPED(window))
198 gtk_widget_show_all(window);
199 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
203 int main(int argc, char **argv)
205 int stderr_pipe[2];
206 struct sigaction act;
207 GList *panel_dirs = NULL;
208 GList *panel_sides = NULL;
210 i18n_init();
212 home_dir = g_get_home_dir();
213 death_callbacks = g_hash_table_new(NULL, NULL);
215 #ifdef HAVE_LIBVFS
216 mc_vfs_init();
217 #endif
218 gtk_init(&argc, &argv);
220 while (1)
222 int c;
223 #ifdef HAVE_GETOPT_LONG
224 int long_index;
225 c = getopt_long(argc, argv, SHORT_OPS,
226 long_opts, &long_index);
227 #else
228 c = getopt(argc, argv, SHORT_OPS);
229 #endif
231 if (c == EOF)
232 break; /* No more options */
234 switch (c)
236 case 'o':
237 override_redirect = TRUE;
238 break;
239 case 'v':
240 printf(VERSION);
241 show_features();
242 return EXIT_SUCCESS;
243 case 'h':
244 printf(HELP);
245 return EXIT_SUCCESS;
246 case 't':
247 case 'b':
248 panel_sides = g_list_prepend(panel_sides,
249 (gpointer) c);
250 panel_dirs = g_list_prepend(panel_dirs,
251 *optarg == '=' ? optarg + 1
252 : optarg);
253 break;
254 default:
255 printf(USAGE);
256 return EXIT_FAILURE;
260 choices_init();
262 gui_support_init();
263 pixmaps_init();
264 dir_init();
265 menu_init();
266 dnd_init();
267 filer_init();
268 mount_init();
269 options_init();
270 type_init();
271 action_init();
273 options_load();
275 /* Let child processes die */
276 act.sa_handler = child_died;
277 sigemptyset(&act.sa_mask);
278 act.sa_flags = SA_NOCLDSTOP;
279 sigaction(SIGCHLD, &act, NULL);
281 /* Ignore SIGPIPE - check for EPIPE errors instead */
282 act.sa_handler = SIG_IGN;
283 sigemptyset(&act.sa_mask);
284 act.sa_flags = 0;
285 sigaction(SIGPIPE, &act, NULL);
287 euid = geteuid();
288 egid = getegid();
289 ngroups = getgroups(0, NULL);
290 if (ngroups < 0)
291 ngroups = 0;
292 else if (ngroups > 0)
294 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
295 getgroups(ngroups, supplemental_groups);
298 if (euid == 0)
300 if (get_choice("!!!DANGER!!!",
301 "Running ROX-Filer as root is VERY dangerous. If I "
302 "had a warranty (I don't) then doing this would "
303 "void it", 2,
304 "Don't click here", "Quit") != 0)
305 exit(EXIT_SUCCESS);
308 if (optind == argc && !panel_dirs)
309 filer_opendir(home_dir, PANEL_NO);
310 else
312 int i = optind;
313 GList *dir = panel_dirs;
314 GList *side = panel_sides;
316 while (dir)
318 int c = (int) side->data;
320 filer_opendir((char *) dir->data,
321 c == 't' ? PANEL_TOP : PANEL_BOTTOM);
322 dir = dir->next;
323 side = side->next;
326 g_list_free(dir);
327 g_list_free(side);
329 while (i < argc)
330 filer_opendir(argv[i++], PANEL_NO);
333 pipe(stderr_pipe);
334 close_on_exec(stderr_pipe[0], TRUE);
335 close_on_exec(stderr_pipe[1], TRUE);
336 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
337 to_error_log = stderr_pipe[1];
339 gtk_main();
341 return EXIT_SUCCESS;
344 static void show_features(void)
346 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
347 g_printerr("%s... %s\n", _("VFS support"),
348 #ifdef HAVE_LIBVFS
349 _("Yes")
350 #else
351 _("No (couldn't find a valid libvfs)")
352 #endif
354 g_printerr("%s... %s\n", _("ImLib support"),
355 #ifdef HAVE_IMLIB
356 _("Yes")
357 #else
358 _("No (the imlib-config command didn't work)")
359 #endif
361 g_printerr("%s... %s\n", _("Internationalisation support"),
362 #ifdef HAVE_GETTEXT
363 _("Yes")
364 #else
365 _("No (could not find gettext() function)")
366 #endif
370 /* Register a function to be called when process number 'child' dies. */
371 void on_child_death(int child, CallbackFn callback, gpointer data)
373 Callback *cb;
375 g_return_if_fail(callback != NULL);
377 cb = g_new(Callback, 1);
379 cb->callback = callback;
380 cb->data = data;
382 g_hash_table_insert(death_callbacks, (gpointer) child, cb);