r898: Applied Bernard Jungen's latest patch:
[rox-filer.git] / ROX-Filer / src / main.c
blob3f763e3da059b1ef7e454b802420ea6002a62ef8
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, the ROX-Filer team.
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 <string.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <pwd.h>
33 #include <grp.h>
35 #ifdef HAVE_GETOPT_LONG
36 # include <getopt.h>
37 #endif
39 #include <gtk/gtk.h>
40 #include "collection.h"
42 #include "global.h"
44 #include "main.h"
45 #include "support.h"
46 #include "gui_support.h"
47 #include "filer.h"
48 #include "display.h"
49 #include "mount.h"
50 #include "menu.h"
51 #include "dnd.h"
52 #include "options.h"
53 #include "choices.h"
54 #include "type.h"
55 #include "pixmaps.h"
56 #include "dir.h"
57 #include "action.h"
58 #include "i18n.h"
59 #include "remote.h"
60 #include "pinboard.h"
61 #include "run.h"
62 #include "toolbar.h"
63 #include "bind.h"
64 #include "icon.h"
65 #include "appinfo.h"
66 #include "panel.h"
67 #include "session.h"
69 int number_of_windows = 0; /* Quit when this reaches 0 again... */
70 static int to_wakeup_pipe = -1; /* Write here to get noticed */
72 uid_t euid;
73 gid_t egid;
74 int ngroups; /* Number of supplemental groups */
75 gid_t *supplemental_groups = NULL;
77 /* Message to display at the top of each filer window */
78 guchar *show_user_message = NULL;
80 int home_dir_len;
81 char *home_dir, *app_dir;
83 #define COPYING \
84 N_("Copyright (C) 2001 Thomas Leonard.\n" \
85 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
86 "to the extent permitted by law.\n" \
87 "You may redistribute copies of ROX-Filer\n" \
88 "under the terms of the GNU General Public License.\n" \
89 "For more information about these matters, " \
90 "see the file named COPYING.\n")
92 #ifdef HAVE_GETOPT_LONG
93 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
94 # define SHORT_ONLY_WARNING ""
95 #else
96 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
97 # define SHORT_ONLY_WARNING \
98 _("NOTE: Your system does not support long options - \n" \
99 "you must use the short versions instead.\n\n")
100 #endif
102 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
103 "Open each directory or file listed, or the current working\n" \
104 "directory if no arguments are given.\n\n" \
105 " -b, --bottom=PANEL open PAN as a bottom-edge panel\n" \
106 " -c, --client-id=ID used for session management\n" \
107 " -d, --dir=DIR open DIR as directory (not application)\n" \
108 " -D, --close=DIR close DIR and its subdirectories\n" \
109 " -h, --help display this help and exit\n" \
110 " -l, --left=PANEL open PAN as a left-edge panel\n" \
111 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
112 " -n, --new start a new filer, even if already running\n" \
113 " -o, --override override window manager control of panels\n" \
114 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
115 " -r, --right=PANEL open PAN as a right-edge panel\n" \
116 " -R, --RPC invoke method call read from stdin\n" \
117 " -s, --show=FILE open a directory showing FILE\n" \
118 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
119 " -u, --user show user name in each window \n" \
120 " -v, --version display the version information and exit\n" \
121 " -x, --examine=FILE FILE has changed - re-examine it\n" \
122 "\nThe latest version can be found at:\n" \
123 "\thttp://rox.sourceforge.net\n" \
124 "\nReport bugs to <tal197@users.sourceforge.net>.\n")
126 #define SHORT_OPS "d:t:b:l:r:op:s:hvnux:m:D:R"
128 #ifdef HAVE_GETOPT_LONG
129 static struct option long_opts[] =
131 {"dir", 1, NULL, 'd'},
132 {"top", 1, NULL, 't'},
133 {"bottom", 1, NULL, 'b'},
134 {"left", 1, NULL, 'l'},
135 {"override", 0, NULL, 'o'},
136 {"pinboard", 1, NULL, 'p'},
137 {"right", 1, NULL, 'r'},
138 {"help", 0, NULL, 'h'},
139 {"version", 0, NULL, 'v'},
140 {"user", 0, NULL, 'u'},
141 {"new", 0, NULL, 'n'},
142 {"RPC", 0, NULL, 'R'},
143 {"show", 1, NULL, 's'},
144 {"examine", 1, NULL, 'x'},
145 {"close", 1, NULL, 'D'},
146 {"mime-type", 1, NULL, 'm'},
147 {"client-id", 1, NULL, 'c'},
148 {NULL, 0, NULL, 0},
150 #endif
152 /* Take control of panels away from WM? */
153 gboolean override_redirect = FALSE;
155 /* Always start a new filer, even if one seems to be already running */
156 gboolean new_copy = FALSE;
158 /* Maps child PIDs to Callback pointers */
159 static GHashTable *death_callbacks = NULL;
160 static gboolean child_died_flag = FALSE;
162 /* Static prototypes */
163 static void show_features(void);
164 static xmlDocPtr soap_new(xmlNodePtr *ret_body);
165 static void soap_add(xmlNodePtr body,
166 xmlChar *function,
167 xmlChar *arg1_name, xmlChar *arg1_value,
168 xmlChar *arg2_name, xmlChar *arg2_value);
169 static void child_died(int signum);
170 static void child_died_callback(void);
171 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition);
173 /****************************************************************
174 * EXTERNAL INTERFACE *
175 ****************************************************************/
177 /* The value that goes with an option */
178 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
180 int main(int argc, char **argv)
182 int wakeup_pipe[2];
183 int i;
184 struct sigaction act;
185 guchar *tmp, *dir, *slash;
186 gchar *client_id = NULL;
187 gboolean show_user = FALSE;
188 xmlDocPtr rpc, soap_rpc = NULL;
189 xmlNodePtr body;
191 home_dir = g_get_home_dir();
192 home_dir_len = strlen(home_dir);
193 app_dir = g_strdup(getenv("APP_DIR"));
195 choices_init();
196 i18n_init();
198 if (!app_dir)
200 g_warning("APP_DIR environment variable was unset!\n"
201 "Use the AppRun script to invoke ROX-Filer...\n");
202 app_dir = g_get_current_dir();
204 #ifdef HAVE_UNSETENV
205 else
207 /* Don't pass it on to our child processes... */
208 unsetenv("APP_DIR");
210 #endif
212 death_callbacks = g_hash_table_new(NULL, NULL);
214 #ifdef HAVE_LIBVFS
215 mc_vfs_init();
216 #endif
218 euid = geteuid();
219 egid = getegid();
220 ngroups = getgroups(0, NULL);
221 if (ngroups < 0)
222 ngroups = 0;
223 else if (ngroups > 0)
225 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
226 getgroups(ngroups, supplemental_groups);
229 /* The idea here is to convert the command-line arguments
230 * into a SOAP RPC.
231 * We attempt to invoke the call on an already-running copy of
232 * the filer if possible, or execute it ourselves if not.
234 rpc = soap_new(&body);
236 while (1)
238 int c;
239 #ifdef HAVE_GETOPT_LONG
240 int long_index;
241 c = getopt_long(argc, argv, SHORT_OPS,
242 long_opts, &long_index);
243 #else
244 c = getopt(argc, argv, SHORT_OPS);
245 #endif
247 if (c == EOF)
248 break; /* No more options */
250 switch (c)
252 case 'n':
253 new_copy = TRUE;
254 break;
255 case 'o':
256 override_redirect = TRUE;
257 break;
258 case 'v':
259 fprintf(stderr, "ROX-Filer %s\n", VERSION);
260 fprintf(stderr, _(COPYING));
261 show_features();
262 return EXIT_SUCCESS;
263 case 'h':
264 fprintf(stderr, _(HELP));
265 fprintf(stderr, _(SHORT_ONLY_WARNING));
266 return EXIT_SUCCESS;
267 case 'D':
268 case 'd':
269 case 'x':
270 /* Argument is a path */
271 tmp = pathdup(VALUE);
272 soap_add(body,
273 c == 'D' ? "CloseDir" :
274 c == 'd' ? "OpenDir" :
275 c == 'x' ? "Examine" : "Unknown",
276 "Filename", tmp,
277 NULL, NULL);
278 g_free(tmp);
279 break;
280 case 's':
281 tmp = g_strdup(VALUE);
282 slash = strrchr(tmp, '/');
283 if (slash)
285 *slash = '\0';
286 slash++;
287 dir = pathdup(tmp);
289 else
291 slash = tmp;
292 dir = pathdup(".");
295 soap_add(body, "Show",
296 "Directory", dir,
297 "Leafname", slash);
298 g_free(tmp);
299 g_free(dir);
300 break;
301 case 'l':
302 case 'r':
303 case 't':
304 case 'b':
305 /* Argument is a leaf (or starts with /) */
306 soap_add(body, "Panel", "Name", VALUE,
307 "Side", c == 'l' ? "Left" :
308 c == 'r' ? "Right" :
309 c == 't' ? "Top" :
310 c == 'b' ? "Bottom" :
311 "Unkown");
312 break;
313 case 'p':
314 soap_add(body, "Pinboard",
315 "Name", VALUE, NULL, NULL);
316 break;
317 case 'u':
318 show_user = TRUE;
319 break;
320 case 'm':
322 MIME_type *type;
323 type_init();
324 type = type_get_type(VALUE);
325 printf("%s/%s\n", type->media_type,
326 type->subtype);
328 return EXIT_SUCCESS;
329 case 'c':
330 client_id = g_strdup(VALUE);
331 break;
332 case 'R':
333 soap_rpc = xmlParseFile("-");
334 if (!soap_rpc)
335 g_error("Invalid XML in RPC");
336 break;
337 default:
338 printf(_(USAGE));
339 return EXIT_FAILURE;
343 add_default_styles();
344 gtk_init(&argc, &argv);
346 if (euid == 0 || show_user)
347 show_user_message = g_strdup_printf( _("Running as user '%s'"),
348 user_name(euid));
350 i = optind;
351 while (i < argc)
353 tmp = pathdup(argv[i++]);
355 soap_add(body, "Run", "Filename", tmp, NULL, NULL);
357 g_free(tmp);
360 if (soap_rpc)
362 if (body->xmlChildrenNode)
363 g_error("Can't use -R with other options - sorry!");
364 xmlFreeDoc(rpc);
365 body = NULL;
366 rpc = soap_rpc;
368 else if (!body->xmlChildrenNode)
370 guchar *dir;
372 dir = g_get_current_dir();
373 soap_add(body, "OpenDir", "Filename", dir, NULL, NULL);
374 g_free(dir);
377 option_add_int("dnd_no_hostnames", 1, NULL);
379 gui_support_init();
380 if (remote_init(rpc, new_copy))
381 return EXIT_SUCCESS; /* Already running */
383 /* Put ourselves into the background (so 'rox' always works the
384 * same, whether we're already running or not).
385 * Not for -n, though (helps when debugging).
387 if (!new_copy)
389 pid_t child;
391 child = fork();
392 if (child > 0)
393 _exit(0); /* Parent exits */
394 /* Otherwise we're the child (or an error occurred - ignore
395 * it!).
399 pixmaps_init();
401 dnd_init();
402 bind_init();
403 dir_init();
404 diritem_init();
405 menu_init();
406 minibuffer_init();
407 filer_init();
408 toolbar_init();
409 display_init();
410 mount_init();
411 options_init();
412 type_init();
413 action_init();
414 appinfo_init();
416 icon_init();
417 pinboard_init();
418 panel_init();
420 options_load();
422 pipe(wakeup_pipe);
423 close_on_exec(wakeup_pipe[0], TRUE);
424 close_on_exec(wakeup_pipe[1], TRUE);
425 gdk_input_add(wakeup_pipe[0], GDK_INPUT_READ, wake_up_cb, NULL);
426 to_wakeup_pipe = wakeup_pipe[1];
428 /* If the pipe is full then we're going to get woken up anyway... */
429 set_blocking(to_wakeup_pipe, FALSE);
431 /* Let child processes die */
432 act.sa_handler = child_died;
433 sigemptyset(&act.sa_mask);
434 act.sa_flags = SA_NOCLDSTOP;
435 sigaction(SIGCHLD, &act, NULL);
437 /* Ignore SIGPIPE - check for EPIPE errors instead */
438 act.sa_handler = SIG_IGN;
439 sigemptyset(&act.sa_mask);
440 act.sa_flags = 0;
441 sigaction(SIGPIPE, &act, NULL);
443 /* Set up session managament if available */
444 session_init(client_id);
445 g_free(client_id);
447 run_soap(rpc);
448 xmlFreeDoc(rpc);
450 if (number_of_windows > 0)
451 gtk_main();
453 return EXIT_SUCCESS;
456 /* Register a function to be called when process number 'child' dies. */
457 void on_child_death(gint child, CallbackFn callback, gpointer data)
459 Callback *cb;
461 g_return_if_fail(callback != NULL);
463 cb = g_new(Callback, 1);
465 cb->callback = callback;
466 cb->data = data;
468 g_hash_table_insert(death_callbacks, GINT_TO_POINTER(child), cb);
471 /****************************************************************
472 * INTERNAL FUNCTIONS *
473 ****************************************************************/
475 static void show_features(void)
477 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
478 g_printerr("%s... %s\n", _("VFS support"),
479 #ifdef HAVE_LIBVFS
480 _("Yes")
481 #else
482 _("No (couldn't find a valid libvfs)")
483 #endif
487 /* Create a new SOAP message and return the document and the (empty)
488 * body node.
490 static xmlDocPtr soap_new(xmlNodePtr *ret_body)
492 xmlDocPtr doc;
493 xmlNodePtr root;
494 xmlNs *env_ns;
496 doc = xmlNewDoc("1.0");
497 root = xmlNewDocNode(doc, NULL, "Envelope", NULL);
498 xmlDocSetRootElement(doc, root);
500 env_ns = xmlNewNs(root, SOAP_ENV_NS, "env");
501 xmlSetNs(root, env_ns);
503 *ret_body = xmlNewTextChild(root, env_ns, "Body", NULL);
504 xmlNewNs(*ret_body, ROX_NS, "rox");
506 return doc;
509 static void soap_add(xmlNodePtr body,
510 xmlChar *function,
511 xmlChar *arg1_name, xmlChar *arg1_value,
512 xmlChar *arg2_name, xmlChar *arg2_value)
514 xmlNodePtr node;
515 xmlNs *rox;
517 rox = xmlSearchNsByHref(body->doc, body, ROX_NS);
519 node = xmlNewChild(body, rox, function, NULL);
521 if (arg1_name)
523 xmlNewTextChild(node, rox, arg1_name, arg1_value);
524 if (arg2_name)
525 xmlNewTextChild(node, rox, arg2_name, arg2_value);
529 /* This is called as a signal handler; simply ensures that
530 * child_died_callback() will get called later.
532 static void child_died(int signum)
534 child_died_flag = TRUE;
535 write(to_wakeup_pipe, "\0", 1); /* Wake up! */
538 static void child_died_callback(void)
540 int status;
541 gint child;
543 child_died_flag = FALSE;
545 /* Find out which children exited and allow them to die */
548 Callback *cb;
550 child = waitpid(-1, &status, WNOHANG);
552 if (child == 0 || child == -1)
553 return;
555 cb = g_hash_table_lookup(death_callbacks,
556 GINT_TO_POINTER(child));
557 if (cb)
559 cb->callback(cb->data);
560 g_hash_table_remove(death_callbacks,
561 GINT_TO_POINTER(child));
564 } while (1);
567 #define BUFLEN 40
568 /* When data is written to_wakeup_pipe, this gets called from the event
569 * loop some time later. Useful for getting out of signal handlers, etc.
571 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition)
573 char buf[BUFLEN];
575 read(source, buf, BUFLEN);
577 if (child_died_flag)
578 child_died_callback();