r2313: 2002 -> 2003
[rox-filer.git] / ROX-Filer / src / main.c
blob7e59be8a192d61081456749e4b5e6f52411519c9
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, 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 /* main.c - parses command-line options and parameters, plus some global
23 * housekeeping.
25 * New to the code and feeling lost? Read global.h now.
28 #include "config.h"
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <signal.h>
34 #include <string.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <pwd.h>
39 #include <grp.h>
40 #include <libxml/parser.h>
42 #ifdef HAVE_GETOPT_LONG
43 # include <getopt.h>
44 #endif
46 #include <gtk/gtk.h>
48 #include "global.h"
50 #include "main.h"
51 #include "support.h"
52 #include "gui_support.h"
53 #include "filer.h"
54 #include "display.h"
55 #include "mount.h"
56 #include "menu.h"
57 #include "dnd.h"
58 #include "options.h"
59 #include "choices.h"
60 #include "type.h"
61 #include "pixmaps.h"
62 #include "dir.h"
63 #include "diritem.h"
64 #include "action.h"
65 #include "i18n.h"
66 #include "remote.h"
67 #include "pinboard.h"
68 #include "run.h"
69 #include "toolbar.h"
70 #include "bind.h"
71 #include "panel.h"
72 #include "session.h"
73 #include "minibuffer.h"
75 int number_of_windows = 0; /* Quit when this reaches 0 again... */
76 int to_wakeup_pipe = -1; /* Write here to get noticed */
78 /* Information about the ROX-Filer process */
79 uid_t euid;
80 gid_t egid;
81 int ngroups; /* Number of supplemental groups */
82 gid_t *supplemental_groups = NULL;
84 /* Message to display at the top of each filer window */
85 const gchar *show_user_message = NULL;
87 int home_dir_len;
88 const char *home_dir, *app_dir;
90 GtkTooltips *tooltips = NULL;
92 #define COPYING \
93 N_("Copyright (C) 2003 Thomas Leonard.\n" \
94 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
95 "to the extent permitted by law.\n" \
96 "You may redistribute copies of ROX-Filer\n" \
97 "under the terms of the GNU General Public License.\n" \
98 "For more information about these matters, " \
99 "see the file named COPYING.\n")
101 #ifdef HAVE_GETOPT_LONG
102 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
103 # define SHORT_ONLY_WARNING ""
104 #else
105 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
106 # define SHORT_ONLY_WARNING \
107 _("NOTE: Your system does not support long options - \n" \
108 "you must use the short versions instead.\n\n")
109 #endif
111 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
112 "Open each directory or file listed, or the current working\n" \
113 "directory if no arguments are given.\n\n" \
114 " -b, --bottom=PANEL open PAN as a bottom-edge panel\n" \
115 " -c, --client-id=ID used for session management\n" \
116 " -d, --dir=DIR open DIR as directory (not application)\n" \
117 " -D, --close=DIR close DIR and its subdirectories\n" \
118 " -h, --help display this help and exit\n" \
119 " -l, --left=PANEL open PAN as a left-edge panel\n" \
120 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
121 " -n, --new start new copy; for debugging the filer\n" \
122 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
123 " -r, --right=PANEL open PAN as a right-edge panel\n" \
124 " -R, --RPC invoke method call read from stdin\n" \
125 " -s, --show=FILE open a directory showing FILE\n" \
126 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
127 " -u, --user show user name in each window \n" \
128 " -v, --version display the version information and exit\n" \
129 " -x, --examine=FILE FILE has changed - re-examine it\n" \
130 "\nReport bugs to <tal197@users.sourceforge.net>.\n" \
131 "Home page (including updated versions): http://rox.sourceforge.net/\n")
133 #define SHORT_OPS "c:d:t:b:l:r:op:s:hvnux:m:D:R"
135 #ifdef HAVE_GETOPT_LONG
136 static struct option long_opts[] =
138 {"dir", 1, NULL, 'd'},
139 {"top", 1, NULL, 't'},
140 {"bottom", 1, NULL, 'b'},
141 {"left", 1, NULL, 'l'},
142 {"override", 0, NULL, 'o'},
143 {"pinboard", 1, NULL, 'p'},
144 {"right", 1, NULL, 'r'},
145 {"help", 0, NULL, 'h'},
146 {"version", 0, NULL, 'v'},
147 {"user", 0, NULL, 'u'},
148 {"new", 0, NULL, 'n'},
149 {"RPC", 0, NULL, 'R'},
150 {"show", 1, NULL, 's'},
151 {"examine", 1, NULL, 'x'},
152 {"close", 1, NULL, 'D'},
153 {"mime-type", 1, NULL, 'm'},
154 {"client-id", 1, NULL, 'c'},
155 {NULL, 0, NULL, 0},
157 #endif
159 /* Take control of panels away from WM? */
160 Option o_override_redirect;
162 /* Always start a new filer, even if one seems to be already running */
163 gboolean new_copy = FALSE;
165 /* Maps child PIDs to Callback pointers */
166 static GHashTable *death_callbacks = NULL;
167 static gboolean child_died_flag = FALSE;
169 Option o_dnd_no_hostnames;
171 /* Static prototypes */
172 static void show_features(void);
173 static void soap_add(xmlNodePtr body,
174 xmlChar *function,
175 const xmlChar *arg1_name, const xmlChar *arg1_value,
176 const xmlChar *arg2_name, const xmlChar *arg2_value);
177 static void child_died(int signum);
178 static void child_died_callback(void);
179 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition);
181 /****************************************************************
182 * EXTERNAL INTERFACE *
183 ****************************************************************/
185 /* The value that goes with an option */
186 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
188 /* Parses the command-line to work out what the user wants to do.
189 * Tries to send the request to an already-running copy of the filer.
190 * If that fails, it initialises all the other modules and executes the
191 * request itself.
193 int main(int argc, char **argv)
195 int wakeup_pipe[2];
196 int i;
197 struct sigaction act;
198 guchar *tmp, *dir;
199 gchar *client_id = NULL;
200 gboolean show_user = FALSE;
201 xmlDocPtr rpc, soap_rpc = NULL, reply;
202 xmlNodePtr body;
204 home_dir = g_get_home_dir();
205 home_dir_len = strlen(home_dir);
206 app_dir = g_strdup(getenv("APP_DIR"));
208 /* Get internationalisation up and running. This requires the
209 * choices system, to discover the user's preferred language.
211 choices_init();
212 options_init();
213 i18n_init();
215 if (!app_dir)
217 g_warning("APP_DIR environment variable was unset!\n"
218 "Use the AppRun script to invoke ROX-Filer...\n");
219 app_dir = g_get_current_dir();
221 #ifdef HAVE_UNSETENV
222 else
224 /* Don't pass it on to our child processes... */
225 unsetenv("APP_DIR");
227 #endif
229 /* Sometimes we want to take special action when a child
230 * process exits. This hash table is used to convert the
231 * child's PID to the callback function.
233 death_callbacks = g_hash_table_new(NULL, NULL);
235 /* Find out some information about ourself */
236 euid = geteuid();
237 egid = getegid();
238 ngroups = getgroups(0, NULL);
239 if (ngroups < 0)
240 ngroups = 0;
241 else if (ngroups > 0)
243 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
244 getgroups(ngroups, supplemental_groups);
247 if (argc == 2 && strcmp(argv[1], "-v") == 0)
249 /* This is used by install.sh to test if the filer
250 * compiled OK. Do this test before gtk_init so that
251 * we don't need an X server to install.
253 g_print("ROX-Filer %s\n", VERSION);
254 g_print(_(COPYING));
255 show_features();
256 return EXIT_SUCCESS;
259 option_add_int(&o_override_redirect, "override_redirect", FALSE);
261 /* The idea here is to convert the command-line arguments
262 * into a SOAP RPC.
263 * We attempt to invoke the call on an already-running copy of
264 * the filer if possible, or execute it ourselves if not.
266 rpc = soap_new(&body);
268 /* Note: must do this before checking our options,
269 * otherwise we report an error for Gtk's options.
271 gtk_init(&argc, &argv);
272 /* Set a default style for the collection widget */
273 gtk_rc_parse_string("style \"rox-default-collection-style\" {\n"
274 " bg[NORMAL] = \"#f3f3f3\"\n"
275 " fg[NORMAL] = \"#000000\"\n"
276 " bg[INSENSITIVE] = \"#bfbfbf\"\n"
277 " fg[INSENSITIVE] = \"#000000\"\n"
278 "}\n"
279 "style \"rox-default-pinboard-style\" {\n"
280 " bg[NORMAL] = \"#666666\"\n"
281 "}\n"
282 "widget \"rox-pinboard\" style : gtk "
283 "\"rox-default-pinboard-style\"\n"
285 "class \"Collection\" style : gtk "
286 "\"rox-default-collection-style\"\n");
288 /* Process each option in turn */
289 while (1)
291 int c;
292 #ifdef HAVE_GETOPT_LONG
293 int long_index;
294 c = getopt_long(argc, argv, SHORT_OPS,
295 long_opts, &long_index);
296 #else
297 c = getopt(argc, argv, SHORT_OPS);
298 #endif
300 if (c == EOF)
301 break; /* No more options */
303 switch (c)
305 case 'n':
306 new_copy = TRUE;
307 break;
308 case 'o':
309 info_message(_("The -o argument is no longer "
310 "used. You can turn on override "
311 "redirect from the Options box "
312 "instead."));
313 break;
314 case 'v':
315 g_print("ROX-Filer %s\n", VERSION);
316 g_print("%s", _(COPYING));
317 show_features();
318 return EXIT_SUCCESS;
319 case 'h':
320 g_print("%s", _(HELP));
321 g_print("%s", _(SHORT_ONLY_WARNING));
322 return EXIT_SUCCESS;
323 case 'D':
324 case 'd':
325 case 'x':
326 /* Argument is a path */
327 if (c == 'd' && VALUE[0] == '/')
328 tmp = g_strdup(VALUE);
329 else
330 tmp = pathdup(VALUE);
331 soap_add(body,
332 c == 'D' ? "CloseDir" :
333 c == 'd' ? "OpenDir" :
334 c == 'x' ? "Examine" : "Unknown",
335 "Filename", tmp,
336 NULL, NULL);
337 g_free(tmp);
338 break;
339 case 's':
340 tmp = g_path_get_dirname(VALUE);
342 if (tmp[0] == '/')
343 dir = NULL;
344 else
345 dir = pathdup(tmp);
347 soap_add(body, "Show",
348 "Directory", dir ? dir : tmp,
349 "Leafname", g_basename(VALUE));
350 g_free(tmp);
351 g_free(dir);
352 break;
353 case 'l':
354 case 'r':
355 case 't':
356 case 'b':
357 /* Argument is a leaf (or starts with /) */
358 soap_add(body, "Panel", "Name", VALUE,
359 "Side", c == 'l' ? "Left" :
360 c == 'r' ? "Right" :
361 c == 't' ? "Top" :
362 c == 'b' ? "Bottom" :
363 "Unkown");
364 break;
365 case 'p':
366 soap_add(body, "Pinboard",
367 "Name", VALUE, NULL, NULL);
368 break;
369 case 'u':
370 show_user = TRUE;
371 break;
372 case 'm':
374 MIME_type *type;
375 type_init();
376 type = type_get_type(VALUE);
377 printf("%s/%s\n", type->media_type,
378 type->subtype);
379 return EXIT_SUCCESS;
381 case 'c':
382 client_id = g_strdup(VALUE);
383 break;
384 case 'R':
385 soap_rpc = xmlParseFile("-");
386 if (!soap_rpc)
387 g_error("Invalid XML in RPC");
388 break;
389 default:
390 printf(_(USAGE));
391 return EXIT_FAILURE;
395 tooltips = gtk_tooltips_new();
397 if (euid == 0 || show_user)
398 show_user_message = g_strdup_printf(_("Running as user '%s'"),
399 user_name(euid));
401 /* Add each remaining (non-option) argument to the list of files
402 * to run.
404 i = optind;
405 while (i < argc)
407 tmp = pathdup(argv[i++]);
409 soap_add(body, "Run", "Filename", tmp, NULL, NULL);
411 g_free(tmp);
414 if (soap_rpc)
416 if (body->xmlChildrenNode)
417 g_error("Can't use -R with other options - sorry!");
418 xmlFreeDoc(rpc);
419 body = NULL;
420 rpc = soap_rpc;
422 else if (!body->xmlChildrenNode)
424 /* The user didn't request any action. Open the current
425 * directory.
427 guchar *dir;
429 dir = g_get_current_dir();
430 soap_add(body, "OpenDir", "Filename", dir, NULL, NULL);
431 g_free(dir);
434 option_add_int(&o_dnd_no_hostnames, "dnd_no_hostnames", 1);
436 /* Try to send the request to an already-running copy of the filer */
437 gui_support_init();
438 if (remote_init(rpc, new_copy))
439 return EXIT_SUCCESS; /* It worked - exit */
441 /* Put ourselves into the background (so 'rox' always works the
442 * same, whether we're already running or not).
443 * Not for -n, though (helps when debugging).
445 if (!new_copy)
447 pid_t child;
449 child = fork();
450 if (child > 0)
451 _exit(0); /* Parent exits */
452 /* Otherwise we're the child (or an error occurred - ignore
453 * it!).
457 /* Close stdin. We don't need it, and it can cause problems if
458 * a child process wants a password, etc...
461 int fd;
462 fd = open("/dev/null", O_RDONLY);
463 if (fd > 0)
465 close(0);
466 dup2(fd, 0);
467 close(fd);
471 /* Initialize the rest of the filer... */
473 pixmaps_init();
475 dnd_init();
476 bind_init();
477 dir_init();
478 diritem_init();
479 menu_init();
480 minibuffer_init();
481 filer_init();
482 toolbar_init();
483 display_init();
484 mount_init();
485 type_init();
486 action_init();
488 pinboard_init();
489 panel_init();
491 /* Let everyone update */
492 options_notify();
494 /* When we get a signal, we can't do much right then. Instead,
495 * we send a char down this pipe, which causes the main loop to
496 * deal with the event next time we're idle.
498 pipe(wakeup_pipe);
499 close_on_exec(wakeup_pipe[0], TRUE);
500 close_on_exec(wakeup_pipe[1], TRUE);
501 gtk_input_add_full(wakeup_pipe[0], GDK_INPUT_READ, wake_up_cb,
502 NULL, NULL, NULL);
503 to_wakeup_pipe = wakeup_pipe[1];
505 /* If the pipe is full then we're going to get woken up anyway... */
506 set_blocking(to_wakeup_pipe, FALSE);
508 /* Let child processes die */
509 act.sa_handler = child_died;
510 sigemptyset(&act.sa_mask);
511 act.sa_flags = SA_NOCLDSTOP;
512 sigaction(SIGCHLD, &act, NULL);
514 /* Ignore SIGPIPE - check for EPIPE errors instead */
515 act.sa_handler = SIG_IGN;
516 sigemptyset(&act.sa_mask);
517 act.sa_flags = 0;
518 sigaction(SIGPIPE, &act, NULL);
520 /* Set up session managament if available */
521 session_init(client_id);
522 g_free(client_id);
524 /* Finally, execute the request */
525 reply = run_soap(rpc);
526 xmlFreeDoc(rpc);
527 if (reply)
529 /* Write the result, if any, to stdout */
530 save_xml_file(reply, "-");
531 xmlFreeDoc(reply);
534 /* Enter the main loop, processing events until all our windows
535 * are closed.
537 if (number_of_windows > 0)
538 gtk_main();
540 return EXIT_SUCCESS;
543 /* Register a function to be called when process number 'child' dies. */
544 void on_child_death(gint child, CallbackFn callback, gpointer data)
546 Callback *cb;
548 g_return_if_fail(callback != NULL);
550 cb = g_new(Callback, 1);
552 cb->callback = callback;
553 cb->data = data;
555 g_hash_table_insert(death_callbacks, GINT_TO_POINTER(child), cb);
558 void one_less_window(void)
560 if (--number_of_windows < 1)
561 gtk_main_quit();
564 /****************************************************************
565 * INTERNAL FUNCTIONS *
566 ****************************************************************/
568 static void show_features(void)
570 g_print("\n");
571 g_print(_("Compiled with GTK version %s\n"), GTK_VERSION);
572 g_print(_("Running with GTK version %d.%d.%d\n"),
573 gtk_major_version,
574 gtk_minor_version,
575 gtk_micro_version);
576 g_print("\n-- %s --\n\n", _("features set at compile time"));
577 g_print("%s... %s\n", _("Large File Support"),
578 #ifdef LARGE_FILE_SUPPORT
579 _("Yes")
580 #else
581 _("No")
582 #endif
584 #if 0
585 g_print("%s... %s\n", _("GNOME-VFS library"),
586 # ifdef WITH_GNOMEVFS
587 _("Yes")
588 # else
589 _("No (gnome-vfs-config not found)")
590 # endif
592 #endif
593 g_print("%s... %s\n", _("Dnotify support"),
594 #ifdef USE_DNOTIFY
595 _("Yes")
596 #else
597 _("No")
598 #endif
602 static void soap_add(xmlNodePtr body,
603 xmlChar *function,
604 const xmlChar *arg1_name, const xmlChar *arg1_value,
605 const xmlChar *arg2_name, const xmlChar *arg2_value)
607 xmlNodePtr node;
608 xmlNs *rox;
610 rox = xmlSearchNsByHref(body->doc, body, ROX_NS);
612 node = xmlNewChild(body, rox, function, NULL);
614 if (arg1_name)
616 xmlNewTextChild(node, rox, arg1_name, arg1_value);
617 if (arg2_name)
618 xmlNewTextChild(node, rox, arg2_name, arg2_value);
622 /* This is called as a signal handler; simply ensures that
623 * child_died_callback() will get called later.
625 static void child_died(int signum)
627 child_died_flag = TRUE;
628 write(to_wakeup_pipe, "\0", 1); /* Wake up! */
631 static void child_died_callback(void)
633 int status;
634 gint child;
636 child_died_flag = FALSE;
638 /* Find out which children exited and allow them to die */
641 Callback *cb;
643 child = waitpid(-1, &status, WNOHANG);
645 if (child == 0 || child == -1)
646 return;
648 cb = g_hash_table_lookup(death_callbacks,
649 GINT_TO_POINTER(child));
650 if (cb)
652 cb->callback(cb->data);
653 g_hash_table_remove(death_callbacks,
654 GINT_TO_POINTER(child));
657 } while (1);
660 #define BUFLEN 40
661 /* When data is written to_wakeup_pipe, this gets called from the event
662 * loop some time later. Useful for getting out of signal handlers, etc.
664 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition)
666 char buf[BUFLEN];
668 read(source, buf, BUFLEN);
670 if (child_died_flag)
671 child_died_callback();
672 #ifdef USE_DNOTIFY
673 if (dnotify_wakeup)
674 dnotify_wakeup();
675 #endif