r109: Option processing is now done with getopt_long.
[rox-filer.git] / ROX-Filer / src / main.c
blob30e7656d42ad9e5eddce0db5841ca875c3d13a0c
1 /* vi: set cindent:
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 <stdlib.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <signal.h>
26 #include <sys/wait.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <getopt.h>
31 #include <gtk/gtk.h>
32 #include "collection.h"
34 #include "main.h"
35 #include "gui_support.h"
36 #include "filer.h"
37 #include "mount.h"
38 #include "menu.h"
39 #include "dnd.h"
40 #include "options.h"
41 #include "choices.h"
42 #include "savebox.h"
43 #include "type.h"
45 int number_of_windows = 0; /* Quit when this reaches 0 again... */
46 int to_error_log = -1; /* Write here to log errors */
48 #define USAGE "Try `ROX-Filer/AppRun --help' for more information.\n"
50 #define VERSION "ROX-Filer 0.1.4\n" \
51 "Copyright (C) 1999 Thomas Leonard.\n" \
52 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
53 "to the extent permitted by law.\n" \
54 "You may redistribute copies of ROX-Filer\n" \
55 "under the terms of the GNU General Public License.\n" \
56 "For more information about these matters, " \
57 "see the file named COPYING.\n"
59 #define HELP "Usage: ROX-Filer/AppRun [OPTION]... [DIR]...\n" \
60 "Open filer windows showing each directory listed, or $HOME \n" \
61 "if no directories are given.\n\n" \
62 " -h, --help display this help and exit\n" \
63 " -v, --version display the version information and exit\n" \
64 " -t, --top [DIR] open DIR as a top-edge panel\n" \
65 " -b, --bottom [DIR] open DIR as a bottom-edge panel\n" \
66 " -l, --left [DIR] open DIR as a left-edge panel\n" \
67 " -r, --right [DIR] open DIR as a right-edge panel\n" \
68 " -o, --override override window manager control of panels\n" \
69 "\n" \
70 "Report bugs to <tal197@ecs.soton.ac.uk>.\n"
72 static struct option long_opts[] =
74 {"top", 1, NULL, 't'},
75 {"bottom", 1, NULL, 'b'},
76 {"left", 1, NULL, 'l'},
77 {"right", 1, NULL, 'r'},
78 {"override", 0, NULL, 'o'},
79 {"help", 0, NULL, 'h'},
80 {"version", 0, NULL, 'v'},
81 {NULL, 0, NULL, 0},
84 /* Take control of panels away from WM? */
85 gboolean override_redirect = FALSE;
87 static void child_died(int signum)
89 int status;
90 int child;
92 /* Find out which children exited and allow them to die */
95 child = waitpid(-1, &status, WNOHANG);
97 if (child == 0 || child == -1)
98 return;
100 /* fprintf(stderr, "Child %d exited\n", child); */
102 } while (1);
105 #define BUFLEN 40
106 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
108 char buf[BUFLEN];
109 static GtkWidget *log = NULL;
110 static GtkWidget *window = NULL;
111 ssize_t len;
113 if (!window)
115 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
116 gtk_window_set_title(GTK_WINDOW(window), "ROX-Filer error log");
117 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
118 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
119 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
120 gtk_widget_hide, GTK_OBJECT(window));
121 log = gtk_text_new(NULL, NULL);
122 gtk_container_add(GTK_CONTAINER(window), log);
125 if (!GTK_WIDGET_MAPPED(window))
126 gtk_widget_show_all(window);
128 len = read(source, buf, BUFLEN);
129 if (len > 0)
130 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
133 int main(int argc, char **argv)
135 int stderr_pipe[2];
136 struct sigaction act;
137 GList *panel_dirs = NULL;
138 GList *panel_sides = NULL;
140 gtk_init(&argc, &argv);
142 while (1)
144 int long_index;
145 int c;
147 c = getopt_long(argc, argv, "t:b:l:r:ohv",
148 long_opts, &long_index);
150 if (c == EOF)
151 break; /* No more options */
153 switch (c)
155 case 'o':
156 override_redirect = TRUE;
157 break;
158 case 'v':
159 printf(VERSION);
160 return EXIT_SUCCESS;
161 case 'h':
162 printf(HELP);
163 return EXIT_SUCCESS;
164 case 't':
165 case 'b':
166 case 'l':
167 case 'r':
168 panel_sides = g_list_prepend(panel_sides,
169 (gpointer) c);
170 panel_dirs = g_list_prepend(panel_dirs,
171 *optarg == '=' ? optarg + 1
172 : optarg);
173 break;
174 default:
175 printf(USAGE);
176 return EXIT_FAILURE;
180 choices_init("ROX-Filer");
182 gui_support_init();
183 menu_init();
184 dnd_init();
185 filer_init();
186 mount_init();
187 options_init();
188 savebox_init();
189 type_init();
191 options_load();
193 /* Let child processes die */
194 act.sa_handler = child_died;
195 sigemptyset(&act.sa_mask);
196 act.sa_flags = SA_NOCLDSTOP;
197 sigaction(SIGCHLD, &act, NULL);
199 if (geteuid() == 0)
201 if (get_choice("!!!DANGER!!!",
202 "Running ROX-Filer as root is VERY dangerous. If I "
203 "had a warranty (I don't) then doing this would "
204 "void it", 2,
205 "Don't click here", "Quit") != 0)
206 exit(EXIT_SUCCESS);
209 if (optind == argc && !panel_dirs)
210 filer_opendir(getenv("HOME"), FALSE, BOTTOM);
211 else
213 int i = optind;
214 GList *dir = panel_dirs;
215 GList *side = panel_sides;
217 while (dir)
219 int c = (int) side->data;
221 filer_opendir((char *) dir->data, TRUE,
222 c == 't' ? TOP :
223 c == 'b' ? BOTTOM :
224 c == 'l' ? LEFT :
225 RIGHT);
226 dir = dir->next;
227 side = side->next;
230 g_list_free(dir);
231 g_list_free(side);
233 while (i < argc)
234 filer_opendir(argv[i++], FALSE, BOTTOM);
237 pipe(stderr_pipe);
238 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
239 to_error_log = stderr_pipe[1];
241 gtk_main();
243 return EXIT_SUCCESS;