r123: Updated version number for new release.
[rox-filer.git] / ROX-Filer / src / main.c
blob92e2bd8ea8c3f9256b84884dfda67a2f3f37b291
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 1999, 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>
32 #ifdef HAVE_GETOPT_LONG
33 # include <getopt.h>
34 #endif
36 #include <gtk/gtk.h>
37 #include "collection.h"
39 #include "main.h"
40 #include "gui_support.h"
41 #include "filer.h"
42 #include "mount.h"
43 #include "menu.h"
44 #include "dnd.h"
45 #include "options.h"
46 #include "choices.h"
47 #include "savebox.h"
48 #include "type.h"
49 #include "pixmaps.h"
51 int number_of_windows = 0; /* Quit when this reaches 0 again... */
52 int to_error_log = -1; /* Write here to log errors */
54 #define VERSION "ROX-Filer 0.1.6\n" \
55 "Copyright (C) 1999 Thomas Leonard.\n" \
56 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
57 "to the extent permitted by law.\n" \
58 "You may redistribute copies of ROX-Filer\n" \
59 "under the terms of the GNU General Public License.\n" \
60 "For more information about these matters, " \
61 "see the file named COPYING.\n"
63 #ifdef HAVE_GETOPT_LONG
64 # define USAGE "Try `ROX-Filer/AppRun --help' for more information.\n"
65 # define SHORT_ONLY_WARNING ""
66 #else
67 # define USAGE "Try `ROX-Filer/AppRun -h' for more information.\n"
68 # define SHORT_ONLY_WARNING "NOTE: Your system does not support long options - \n" \
69 "you must use the short versions instead.\n\n"
70 #endif \
72 #define HELP "Usage: ROX-Filer/AppRun [OPTION]... [DIR]...\n" \
73 "Open filer windows showing each directory listed, or $HOME \n" \
74 "if no directories are given.\n\n" \
75 " -h, --help display this help and exit\n" \
76 " -v, --version display the version information and exit\n" \
77 " -t, --top [DIR] open DIR as a top-edge panel\n" \
78 " -b, --bottom [DIR] open DIR as a bottom-edge panel\n" \
79 " -l, --left [DIR] open DIR as a left-edge panel\n" \
80 " -r, --right [DIR] open DIR as a right-edge panel\n" \
81 " -o, --override override window manager control of panels\n" \
82 "\n" SHORT_ONLY_WARNING \
83 "Report bugs to <tal197@ecs.soton.ac.uk>.\n"
85 #ifdef HAVE_GETOPT_LONG
86 static struct option long_opts[] =
88 {"top", 1, NULL, 't'},
89 {"bottom", 1, NULL, 'b'},
90 {"left", 1, NULL, 'l'},
91 {"right", 1, NULL, 'r'},
92 {"override", 0, NULL, 'o'},
93 {"help", 0, NULL, 'h'},
94 {"version", 0, NULL, 'v'},
95 {NULL, 0, NULL, 0},
97 #endif
99 /* Take control of panels away from WM? */
100 gboolean override_redirect = FALSE;
102 static void child_died(int signum)
104 int status;
105 int child;
107 /* Find out which children exited and allow them to die */
110 child = waitpid(-1, &status, WNOHANG);
112 if (child == 0 || child == -1)
113 return;
115 /* fprintf(stderr, "Child %d exited\n", child); */
117 } while (1);
120 #define BUFLEN 40
121 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
123 char buf[BUFLEN];
124 static GtkWidget *log = NULL;
125 static GtkWidget *window = NULL;
126 ssize_t len;
128 if (!window)
130 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
131 gtk_window_set_title(GTK_WINDOW(window), "ROX-Filer error log");
132 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
133 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
134 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
135 gtk_widget_hide, GTK_OBJECT(window));
136 log = gtk_text_new(NULL, NULL);
137 gtk_container_add(GTK_CONTAINER(window), log);
140 if (!GTK_WIDGET_MAPPED(window))
141 gtk_widget_show_all(window);
143 len = read(source, buf, BUFLEN);
144 if (len > 0)
145 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
148 int main(int argc, char **argv)
150 int stderr_pipe[2];
151 struct sigaction act;
152 GList *panel_dirs = NULL;
153 GList *panel_sides = NULL;
155 gtk_init(&argc, &argv);
157 while (1)
159 int c;
160 #ifdef HAVE_GETOPT_LONG
161 int long_index;
162 c = getopt_long(argc, argv, "t:b:l:r:ohv",
163 long_opts, &long_index);
164 #else
165 c = getopt(argc, argv, "t:b:l:r:ohv");
166 #endif
168 if (c == EOF)
169 break; /* No more options */
171 switch (c)
173 case 'o':
174 override_redirect = TRUE;
175 break;
176 case 'v':
177 printf(VERSION);
178 return EXIT_SUCCESS;
179 case 'h':
180 printf(HELP);
181 return EXIT_SUCCESS;
182 case 't':
183 case 'b':
184 case 'l':
185 case 'r':
186 panel_sides = g_list_prepend(panel_sides,
187 (gpointer) c);
188 panel_dirs = g_list_prepend(panel_dirs,
189 *optarg == '=' ? optarg + 1
190 : optarg);
191 break;
192 default:
193 printf(USAGE);
194 return EXIT_FAILURE;
198 choices_init("ROX-Filer");
200 gui_support_init();
201 pixmaps_init();
202 menu_init();
203 dnd_init();
204 filer_init();
205 mount_init();
206 options_init();
207 savebox_init();
208 type_init();
210 options_load();
212 /* Let child processes die */
213 act.sa_handler = child_died;
214 sigemptyset(&act.sa_mask);
215 act.sa_flags = SA_NOCLDSTOP;
216 sigaction(SIGCHLD, &act, NULL);
218 if (geteuid() == 0)
220 if (get_choice("!!!DANGER!!!",
221 "Running ROX-Filer as root is VERY dangerous. If I "
222 "had a warranty (I don't) then doing this would "
223 "void it", 2,
224 "Don't click here", "Quit") != 0)
225 exit(EXIT_SUCCESS);
228 if (optind == argc && !panel_dirs)
229 filer_opendir(getenv("HOME"), FALSE, BOTTOM);
230 else
232 int i = optind;
233 GList *dir = panel_dirs;
234 GList *side = panel_sides;
236 while (dir)
238 int c = (int) side->data;
240 filer_opendir((char *) dir->data, TRUE,
241 c == 't' ? TOP :
242 c == 'b' ? BOTTOM :
243 c == 'l' ? LEFT :
244 RIGHT);
245 dir = dir->next;
246 side = side->next;
249 g_list_free(dir);
250 g_list_free(side);
252 while (i < argc)
253 filer_opendir(argv[i++], FALSE, BOTTOM);
256 pipe(stderr_pipe);
257 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
258 to_error_log = stderr_pipe[1];
260 gtk_main();
262 return EXIT_SUCCESS;