r2228: Made 'Automatic' an icon size, rather than a separate option.
[rox-filer.git] / ROX-Filer / src / main.c
blobdc03ecdca11c518a9adab83205cc3f20398ed643
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, 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 static 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) 2002 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 " -o, --override override window manager control of panels\n" \
123 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
124 " -r, --right=PANEL open PAN as a right-edge panel\n" \
125 " -R, --RPC invoke method call read from stdin\n" \
126 " -s, --show=FILE open a directory showing FILE\n" \
127 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
128 " -u, --user show user name in each window \n" \
129 " -v, --version display the version information and exit\n" \
130 " -x, --examine=FILE FILE has changed - re-examine it\n" \
131 "\nReport bugs to <tal197@users.sourceforge.net>.\n" \
132 "Home page (including updated versions): http://rox.sourceforge.net/\n")
134 #define SHORT_OPS "c:d:t:b:l:r:op:s:hvnux:m:D:R"
136 #ifdef HAVE_GETOPT_LONG
137 static struct option long_opts[] =
139 {"dir", 1, NULL, 'd'},
140 {"top", 1, NULL, 't'},
141 {"bottom", 1, NULL, 'b'},
142 {"left", 1, NULL, 'l'},
143 {"override", 0, NULL, 'o'},
144 {"pinboard", 1, NULL, 'p'},
145 {"right", 1, NULL, 'r'},
146 {"help", 0, NULL, 'h'},
147 {"version", 0, NULL, 'v'},
148 {"user", 0, NULL, 'u'},
149 {"new", 0, NULL, 'n'},
150 {"RPC", 0, NULL, 'R'},
151 {"show", 1, NULL, 's'},
152 {"examine", 1, NULL, 'x'},
153 {"close", 1, NULL, 'D'},
154 {"mime-type", 1, NULL, 'm'},
155 {"client-id", 1, NULL, 'c'},
156 {NULL, 0, NULL, 0},
158 #endif
160 /* Take control of panels away from WM? */
161 Option o_override_redirect;
163 /* Always start a new filer, even if one seems to be already running */
164 gboolean new_copy = FALSE;
166 /* Maps child PIDs to Callback pointers */
167 static GHashTable *death_callbacks = NULL;
168 static gboolean child_died_flag = FALSE;
170 Option o_dnd_no_hostnames;
172 /* Static prototypes */
173 static void show_features(void);
174 static void soap_add(xmlNodePtr body,
175 xmlChar *function,
176 const xmlChar *arg1_name, const xmlChar *arg1_value,
177 const xmlChar *arg2_name, const xmlChar *arg2_value);
178 static void child_died(int signum);
179 static void child_died_callback(void);
180 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition);
182 /****************************************************************
183 * EXTERNAL INTERFACE *
184 ****************************************************************/
186 /* The value that goes with an option */
187 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
189 /* Parses the command-line to work out what the user wants to do.
190 * Tries to send the request to an already-running copy of the filer.
191 * If that fails, it initialises all the other modules and executes the
192 * request itself.
194 int main(int argc, char **argv)
196 int wakeup_pipe[2];
197 int i;
198 struct sigaction act;
199 guchar *tmp, *dir;
200 gchar *client_id = NULL;
201 gboolean show_user = FALSE;
202 xmlDocPtr rpc, soap_rpc = NULL, reply;
203 xmlNodePtr body;
205 home_dir = g_get_home_dir();
206 home_dir_len = strlen(home_dir);
207 app_dir = g_strdup(getenv("APP_DIR"));
209 /* Get internationalisation up and running. This requires the
210 * choices system, to discover the user's preferred language.
212 choices_init();
213 options_init();
214 i18n_init();
216 if (!app_dir)
218 g_warning("APP_DIR environment variable was unset!\n"
219 "Use the AppRun script to invoke ROX-Filer...\n");
220 app_dir = g_get_current_dir();
222 #ifdef HAVE_UNSETENV
223 else
225 /* Don't pass it on to our child processes... */
226 unsetenv("APP_DIR");
228 #endif
230 /* Sometimes we want to take special action when a child
231 * process exits. This hash table is used to convert the
232 * child's PID to the callback function.
234 death_callbacks = g_hash_table_new(NULL, NULL);
236 /* Find out some information about ourself */
237 euid = geteuid();
238 egid = getegid();
239 ngroups = getgroups(0, NULL);
240 if (ngroups < 0)
241 ngroups = 0;
242 else if (ngroups > 0)
244 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
245 getgroups(ngroups, supplemental_groups);
248 if (argc == 2 && strcmp(argv[1], "-v") == 0)
250 /* This is used by install.sh to test if the filer
251 * compiled OK. Do this test before gtk_init so that
252 * we don't need an X server to install.
254 g_print("ROX-Filer %s\n", VERSION);
255 g_print(_(COPYING));
256 show_features();
257 return EXIT_SUCCESS;
260 option_add_int(&o_override_redirect, "override_redirect", FALSE);
262 /* The idea here is to convert the command-line arguments
263 * into a SOAP RPC.
264 * We attempt to invoke the call on an already-running copy of
265 * the filer if possible, or execute it ourselves if not.
267 rpc = soap_new(&body);
269 /* Note: must do this before checking our options,
270 * otherwise we report an error for Gtk's options.
272 gtk_init(&argc, &argv);
273 /* Set a default style for the collection widget */
274 gtk_rc_parse_string("style \"rox-default-collection-style\" {\n"
275 " bg[NORMAL] = \"#f3f3f3\"\n"
276 " fg[NORMAL] = \"#000000\"\n"
277 " bg[INSENSITIVE] = \"#bfbfbf\"\n"
278 " fg[INSENSITIVE] = \"#000000\"\n"
279 "}\n"
280 "style \"rox-default-pinboard-style\" {\n"
281 " bg[NORMAL] = \"#666666\"\n"
282 "}\n"
283 "widget \"rox-pinboard\" style : gtk "
284 "\"rox-default-pinboard-style\"\n"
286 "class \"Collection\" style : gtk "
287 "\"rox-default-collection-style\"\n");
289 /* Process each option in turn */
290 while (1)
292 int c;
293 #ifdef HAVE_GETOPT_LONG
294 int long_index;
295 c = getopt_long(argc, argv, SHORT_OPS,
296 long_opts, &long_index);
297 #else
298 c = getopt(argc, argv, SHORT_OPS);
299 #endif
301 if (c == EOF)
302 break; /* No more options */
304 switch (c)
306 case 'n':
307 new_copy = TRUE;
308 break;
309 case 'o':
310 info_message(_("The -o argument is no longer "
311 "used. You can turn on override "
312 "redirect from the Options box "
313 "instead."));
314 break;
315 case 'v':
316 g_print("ROX-Filer %s\n", VERSION);
317 g_print("%s", _(COPYING));
318 show_features();
319 return EXIT_SUCCESS;
320 case 'h':
321 g_print("%s", _(HELP));
322 g_print("%s", _(SHORT_ONLY_WARNING));
323 return EXIT_SUCCESS;
324 case 'D':
325 case 'd':
326 case 'x':
327 /* Argument is a path */
328 if (c == 'd' && VALUE[0] == '/')
329 tmp = g_strdup(VALUE);
330 else
331 tmp = pathdup(VALUE);
332 soap_add(body,
333 c == 'D' ? "CloseDir" :
334 c == 'd' ? "OpenDir" :
335 c == 'x' ? "Examine" : "Unknown",
336 "Filename", tmp,
337 NULL, NULL);
338 g_free(tmp);
339 break;
340 case 's':
341 tmp = g_path_get_dirname(VALUE);
343 if (tmp[0] == '/')
344 dir = NULL;
345 else
346 dir = pathdup(tmp);
348 soap_add(body, "Show",
349 "Directory", dir ? dir : tmp,
350 "Leafname", g_basename(VALUE));
351 g_free(tmp);
352 g_free(dir);
353 break;
354 case 'l':
355 case 'r':
356 case 't':
357 case 'b':
358 /* Argument is a leaf (or starts with /) */
359 soap_add(body, "Panel", "Name", VALUE,
360 "Side", c == 'l' ? "Left" :
361 c == 'r' ? "Right" :
362 c == 't' ? "Top" :
363 c == 'b' ? "Bottom" :
364 "Unkown");
365 break;
366 case 'p':
367 soap_add(body, "Pinboard",
368 "Name", VALUE, NULL, NULL);
369 break;
370 case 'u':
371 show_user = TRUE;
372 break;
373 case 'm':
375 MIME_type *type;
376 type_init();
377 type = type_get_type(VALUE);
378 printf("%s/%s\n", type->media_type,
379 type->subtype);
380 return EXIT_SUCCESS;
382 case 'c':
383 client_id = g_strdup(VALUE);
384 break;
385 case 'R':
386 soap_rpc = xmlParseFile("-");
387 if (!soap_rpc)
388 g_error("Invalid XML in RPC");
389 break;
390 default:
391 printf(_(USAGE));
392 return EXIT_FAILURE;
396 tooltips = gtk_tooltips_new();
398 if (euid == 0 || show_user)
399 show_user_message = g_strdup_printf(_("Running as user '%s'"),
400 user_name(euid));
402 /* Add each remaining (non-option) argument to the list of files
403 * to run.
405 i = optind;
406 while (i < argc)
408 tmp = pathdup(argv[i++]);
410 soap_add(body, "Run", "Filename", tmp, NULL, NULL);
412 g_free(tmp);
415 if (soap_rpc)
417 if (body->xmlChildrenNode)
418 g_error("Can't use -R with other options - sorry!");
419 xmlFreeDoc(rpc);
420 body = NULL;
421 rpc = soap_rpc;
423 else if (!body->xmlChildrenNode)
425 /* The user didn't request any action. Open the current
426 * directory.
428 guchar *dir;
430 dir = g_get_current_dir();
431 soap_add(body, "OpenDir", "Filename", dir, NULL, NULL);
432 g_free(dir);
435 option_add_int(&o_dnd_no_hostnames, "dnd_no_hostnames", 1);
437 /* Try to send the request to an already-running copy of the filer */
438 gui_support_init();
439 if (remote_init(rpc, new_copy))
440 return EXIT_SUCCESS; /* It worked - exit */
442 /* Put ourselves into the background (so 'rox' always works the
443 * same, whether we're already running or not).
444 * Not for -n, though (helps when debugging).
446 if (!new_copy)
448 pid_t child;
450 child = fork();
451 if (child > 0)
452 _exit(0); /* Parent exits */
453 /* Otherwise we're the child (or an error occurred - ignore
454 * it!).
458 /* Close stdin. We don't need it, and it can cause problems if
459 * a child process wants a password, etc...
462 int fd;
463 fd = open("/dev/null", O_RDONLY);
464 if (fd > 0)
466 close(0);
467 dup2(fd, 0);
468 close(fd);
472 /* Initialize the rest of the filer... */
474 pixmaps_init();
476 dnd_init();
477 bind_init();
478 dir_init();
479 diritem_init();
480 menu_init();
481 minibuffer_init();
482 filer_init();
483 toolbar_init();
484 display_init();
485 mount_init();
486 type_init();
487 action_init();
489 pinboard_init();
490 panel_init();
492 /* Let everyone update */
493 options_notify();
495 /* When we get a signal, we can't do much right then. Instead,
496 * we send a char down this pipe, which causes the main loop to
497 * deal with the event next time we're idle.
499 pipe(wakeup_pipe);
500 close_on_exec(wakeup_pipe[0], TRUE);
501 close_on_exec(wakeup_pipe[1], TRUE);
502 gtk_input_add_full(wakeup_pipe[0], GDK_INPUT_READ, wake_up_cb,
503 NULL, NULL, NULL);
504 to_wakeup_pipe = wakeup_pipe[1];
506 /* If the pipe is full then we're going to get woken up anyway... */
507 set_blocking(to_wakeup_pipe, FALSE);
509 /* Let child processes die */
510 act.sa_handler = child_died;
511 sigemptyset(&act.sa_mask);
512 act.sa_flags = SA_NOCLDSTOP;
513 sigaction(SIGCHLD, &act, NULL);
515 /* Ignore SIGPIPE - check for EPIPE errors instead */
516 act.sa_handler = SIG_IGN;
517 sigemptyset(&act.sa_mask);
518 act.sa_flags = 0;
519 sigaction(SIGPIPE, &act, NULL);
521 /* Set up session managament if available */
522 session_init(client_id);
523 g_free(client_id);
525 /* Finally, execute the request */
526 reply = run_soap(rpc);
527 xmlFreeDoc(rpc);
528 if (reply)
530 /* Write the result, if any, to stdout */
531 save_xml_file(reply, "-");
532 xmlFreeDoc(reply);
535 /* Enter the main loop, processing events until all our windows
536 * are closed.
538 if (number_of_windows > 0)
539 gtk_main();
541 return EXIT_SUCCESS;
544 /* Register a function to be called when process number 'child' dies. */
545 void on_child_death(gint child, CallbackFn callback, gpointer data)
547 Callback *cb;
549 g_return_if_fail(callback != NULL);
551 cb = g_new(Callback, 1);
553 cb->callback = callback;
554 cb->data = data;
556 g_hash_table_insert(death_callbacks, GINT_TO_POINTER(child), cb);
559 void one_less_window(void)
561 if (--number_of_windows < 1)
562 gtk_main_quit();
565 /****************************************************************
566 * INTERNAL FUNCTIONS *
567 ****************************************************************/
569 static void show_features(void)
571 g_print("\n-- %s --\n\n", _("features set at compile time"));
572 g_print("%s... %s\n", _("Large File Support"),
573 #ifdef LARGE_FILE_SUPPORT
574 _("Yes")
575 #else
576 _("No")
577 #endif
579 #if 0
580 g_print("%s... %s\n", _("GNOME-VFS library"),
581 # ifdef WITH_GNOMEVFS
582 _("Yes")
583 # else
584 _("No (gnome-vfs-config not found)")
585 # endif
587 #endif
590 static void soap_add(xmlNodePtr body,
591 xmlChar *function,
592 const xmlChar *arg1_name, const xmlChar *arg1_value,
593 const xmlChar *arg2_name, const xmlChar *arg2_value)
595 xmlNodePtr node;
596 xmlNs *rox;
598 rox = xmlSearchNsByHref(body->doc, body, ROX_NS);
600 node = xmlNewChild(body, rox, function, NULL);
602 if (arg1_name)
604 xmlNewTextChild(node, rox, arg1_name, arg1_value);
605 if (arg2_name)
606 xmlNewTextChild(node, rox, arg2_name, arg2_value);
610 /* This is called as a signal handler; simply ensures that
611 * child_died_callback() will get called later.
613 static void child_died(int signum)
615 child_died_flag = TRUE;
616 write(to_wakeup_pipe, "\0", 1); /* Wake up! */
619 static void child_died_callback(void)
621 int status;
622 gint child;
624 child_died_flag = FALSE;
626 /* Find out which children exited and allow them to die */
629 Callback *cb;
631 child = waitpid(-1, &status, WNOHANG);
633 if (child == 0 || child == -1)
634 return;
636 cb = g_hash_table_lookup(death_callbacks,
637 GINT_TO_POINTER(child));
638 if (cb)
640 cb->callback(cb->data);
641 g_hash_table_remove(death_callbacks,
642 GINT_TO_POINTER(child));
645 } while (1);
648 #define BUFLEN 40
649 /* When data is written to_wakeup_pipe, this gets called from the event
650 * loop some time later. Useful for getting out of signal handlers, etc.
652 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition)
654 char buf[BUFLEN];
656 read(source, buf, BUFLEN);
658 if (child_died_flag)
659 child_died_callback();