r114: Removed an obsolete comment.
[rox-filer.git] / ROX-Filer / src / main.c
blob9bdbad84c2a62253781efc43d64b0033be29bd3f
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"
50 int number_of_windows = 0; /* Quit when this reaches 0 again... */
51 int to_error_log = -1; /* Write here to log errors */
53 #define VERSION "ROX-Filer 0.1.4\n" \
54 "Copyright (C) 1999 Thomas Leonard.\n" \
55 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
56 "to the extent permitted by law.\n" \
57 "You may redistribute copies of ROX-Filer\n" \
58 "under the terms of the GNU General Public License.\n" \
59 "For more information about these matters, " \
60 "see the file named COPYING.\n"
62 #ifdef HAVE_GETOPT_LONG
63 # define USAGE "Try `ROX-Filer/AppRun --help' for more information.\n"
64 # define SHORT_ONLY_WARNING ""
65 #else
66 # define USAGE "Try `ROX-Filer/AppRun -h' for more information.\n"
67 # define SHORT_ONLY_WARNING "NOTE: Your system does not support long options - \n" \
68 "you must use the short versions instead.\n\n"
69 #endif \
71 #define HELP "Usage: ROX-Filer/AppRun [OPTION]... [DIR]...\n" \
72 "Open filer windows showing each directory listed, or $HOME \n" \
73 "if no directories are given.\n\n" \
74 " -h, --help display this help and exit\n" \
75 " -v, --version display the version information and exit\n" \
76 " -t, --top [DIR] open DIR as a top-edge panel\n" \
77 " -b, --bottom [DIR] open DIR as a bottom-edge panel\n" \
78 " -l, --left [DIR] open DIR as a left-edge panel\n" \
79 " -r, --right [DIR] open DIR as a right-edge panel\n" \
80 " -o, --override override window manager control of panels\n" \
81 "\n" SHORT_ONLY_WARNING \
82 "Report bugs to <tal197@ecs.soton.ac.uk>.\n"
84 #ifdef HAVE_GETOPT_LONG
85 static struct option long_opts[] =
87 {"top", 1, NULL, 't'},
88 {"bottom", 1, NULL, 'b'},
89 {"left", 1, NULL, 'l'},
90 {"right", 1, NULL, 'r'},
91 {"override", 0, NULL, 'o'},
92 {"help", 0, NULL, 'h'},
93 {"version", 0, NULL, 'v'},
94 {NULL, 0, NULL, 0},
96 #endif
98 /* Take control of panels away from WM? */
99 gboolean override_redirect = FALSE;
101 static void child_died(int signum)
103 int status;
104 int child;
106 /* Find out which children exited and allow them to die */
109 child = waitpid(-1, &status, WNOHANG);
111 if (child == 0 || child == -1)
112 return;
114 /* fprintf(stderr, "Child %d exited\n", child); */
116 } while (1);
119 #define BUFLEN 40
120 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
122 char buf[BUFLEN];
123 static GtkWidget *log = NULL;
124 static GtkWidget *window = NULL;
125 ssize_t len;
127 if (!window)
129 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
130 gtk_window_set_title(GTK_WINDOW(window), "ROX-Filer error log");
131 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
132 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
133 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
134 gtk_widget_hide, GTK_OBJECT(window));
135 log = gtk_text_new(NULL, NULL);
136 gtk_container_add(GTK_CONTAINER(window), log);
139 if (!GTK_WIDGET_MAPPED(window))
140 gtk_widget_show_all(window);
142 len = read(source, buf, BUFLEN);
143 if (len > 0)
144 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
147 int main(int argc, char **argv)
149 int stderr_pipe[2];
150 struct sigaction act;
151 GList *panel_dirs = NULL;
152 GList *panel_sides = NULL;
154 gtk_init(&argc, &argv);
156 while (1)
158 int c;
159 #ifdef HAVE_GETOPT_LONG
160 int long_index;
161 c = getopt_long(argc, argv, "t:b:l:r:ohv",
162 long_opts, &long_index);
163 #else
164 c = getopt(argc, argv, "t:b:l:r:ohv");
165 #endif
167 if (c == EOF)
168 break; /* No more options */
170 switch (c)
172 case 'o':
173 override_redirect = TRUE;
174 break;
175 case 'v':
176 printf(VERSION);
177 return EXIT_SUCCESS;
178 case 'h':
179 printf(HELP);
180 return EXIT_SUCCESS;
181 case 't':
182 case 'b':
183 case 'l':
184 case 'r':
185 panel_sides = g_list_prepend(panel_sides,
186 (gpointer) c);
187 panel_dirs = g_list_prepend(panel_dirs,
188 *optarg == '=' ? optarg + 1
189 : optarg);
190 break;
191 default:
192 printf(USAGE);
193 return EXIT_FAILURE;
197 choices_init("ROX-Filer");
199 gui_support_init();
200 menu_init();
201 dnd_init();
202 filer_init();
203 mount_init();
204 options_init();
205 savebox_init();
206 type_init();
208 options_load();
210 /* Let child processes die */
211 act.sa_handler = child_died;
212 sigemptyset(&act.sa_mask);
213 act.sa_flags = SA_NOCLDSTOP;
214 sigaction(SIGCHLD, &act, NULL);
216 if (geteuid() == 0)
218 if (get_choice("!!!DANGER!!!",
219 "Running ROX-Filer as root is VERY dangerous. If I "
220 "had a warranty (I don't) then doing this would "
221 "void it", 2,
222 "Don't click here", "Quit") != 0)
223 exit(EXIT_SUCCESS);
226 if (optind == argc && !panel_dirs)
227 filer_opendir(getenv("HOME"), FALSE, BOTTOM);
228 else
230 int i = optind;
231 GList *dir = panel_dirs;
232 GList *side = panel_sides;
234 while (dir)
236 int c = (int) side->data;
238 filer_opendir((char *) dir->data, TRUE,
239 c == 't' ? TOP :
240 c == 'b' ? BOTTOM :
241 c == 'l' ? LEFT :
242 RIGHT);
243 dir = dir->next;
244 side = side->next;
247 g_list_free(dir);
248 g_list_free(side);
250 while (i < argc)
251 filer_opendir(argv[i++], FALSE, BOTTOM);
254 pipe(stderr_pipe);
255 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
256 to_error_log = stderr_pipe[1];
258 gtk_main();
260 return EXIT_SUCCESS;