r3890: Change over from CHOICESPATH to XDG_CONFIG_HOME and XDG_CONFIG_DIRS
[rox-filer.git] / ROX-Filer / src / main.c
blob86b2f73009d633cca3e88fec8c343ed846c9ae15
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, 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 <time.h>
41 #include <libxml/parser.h>
43 #ifdef HAVE_GETOPT_LONG
44 # include <getopt.h>
45 #endif
47 #include <gtk/gtk.h>
48 #include <gdk/gdkx.h> /* For rox_x_error */
50 #include "global.h"
52 #include "main.h"
53 #include "support.h"
54 #include "gui_support.h"
55 #include "filer.h"
56 #include "display.h"
57 #include "mount.h"
58 #include "menu.h"
59 #include "dnd.h"
60 #include "options.h"
61 #include "choices.h"
62 #include "type.h"
63 #include "pixmaps.h"
64 #include "dir.h"
65 #include "diritem.h"
66 #include "action.h"
67 #include "i18n.h"
68 #include "remote.h"
69 #include "pinboard.h"
70 #include "run.h"
71 #include "toolbar.h"
72 #include "bind.h"
73 #include "panel.h"
74 #include "session.h"
75 #include "minibuffer.h"
76 #include "xtypes.h"
78 int number_of_windows = 0; /* Quit when this reaches 0 again... */
79 int to_wakeup_pipe = -1; /* Write here to get noticed */
81 /* Information about the ROX-Filer process */
82 uid_t euid;
83 gid_t egid;
84 int ngroups; /* Number of supplemental groups */
85 gid_t *supplemental_groups = NULL;
87 /* Message to display at the top of each filer window */
88 const gchar *show_user_message = NULL;
90 int home_dir_len;
91 const char *home_dir, *app_dir;
93 GtkTooltips *tooltips = NULL;
95 #define COPYING \
96 N_("Copyright (C) 2005 Thomas Leonard.\n" \
97 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
98 "to the extent permitted by law.\n" \
99 "You may redistribute copies of ROX-Filer\n" \
100 "under the terms of the GNU General Public License.\n" \
101 "For more information about these matters, " \
102 "see the file named COPYING.\n")
104 #ifdef HAVE_GETOPT_LONG
105 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
106 # define SHORT_ONLY_WARNING ""
107 #else
108 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
109 # define SHORT_ONLY_WARNING \
110 _("NOTE: Your system does not support long options - \n" \
111 "you must use the short versions instead.\n\n")
112 #endif
114 #define BUGS_TO "<rox-devel@lists.sourceforge.net>"
116 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
117 "Open each directory or file listed, or the current working\n" \
118 "directory if no arguments are given.\n\n" \
119 " -b, --bottom=PANEL open PAN as a bottom-edge panel\n" \
120 " -c, --client-id=ID used for session management\n" \
121 " -d, --dir=DIR open DIR as directory (not application)\n" \
122 " -D, --close=DIR close DIR and its subdirectories\n" \
123 " -h, --help display this help and exit\n" \
124 " -l, --left=PANEL open PAN as a left-edge panel\n" \
125 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
126 " -n, --new start new copy; for debugging the filer\n" \
127 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
128 " -r, --right=PANEL open PAN as a right-edge panel\n" \
129 " -R, --RPC invoke method call read from stdin\n" \
130 " -s, --show=FILE open a directory showing FILE\n" \
131 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
132 " -u, --user show user name in each window \n" \
133 " -v, --version display the version information and exit\n" \
134 " -x, --examine=FILE FILE has changed - re-examine it\n" \
135 "\nReport bugs to " BUGS_TO ".\n" \
136 "Home page (including updated versions): http://rox.sourceforge.net/\n")
138 #define SHORT_OPS "c:d:t:b:l:r:op:s:hvnux:m:D:R"
140 #ifdef HAVE_GETOPT_LONG
141 static struct option long_opts[] =
143 {"dir", 1, NULL, 'd'},
144 {"top", 1, NULL, 't'},
145 {"bottom", 1, NULL, 'b'},
146 {"left", 1, NULL, 'l'},
147 {"override", 0, NULL, 'o'},
148 {"pinboard", 1, NULL, 'p'},
149 {"right", 1, NULL, 'r'},
150 {"help", 0, NULL, 'h'},
151 {"version", 0, NULL, 'v'},
152 {"user", 0, NULL, 'u'},
153 {"new", 0, NULL, 'n'},
154 {"RPC", 0, NULL, 'R'},
155 {"show", 1, NULL, 's'},
156 {"examine", 1, NULL, 'x'},
157 {"close", 1, NULL, 'D'},
158 {"mime-type", 1, NULL, 'm'},
159 {"client-id", 1, NULL, 'c'},
160 {NULL, 0, NULL, 0},
162 #endif
164 /* Take control of panels away from WM? */
165 Option o_override_redirect;
167 /* Always start a new filer, even if one seems to be already running */
168 gboolean new_copy = FALSE;
170 /* Maps child PIDs to Callback pointers */
171 static GHashTable *death_callbacks = NULL;
172 static gboolean child_died_flag = FALSE;
174 Option o_dnd_no_hostnames;
176 /* Static prototypes */
177 static void show_features(void);
178 static void soap_add(xmlNodePtr body,
179 xmlChar *function,
180 const xmlChar *arg1_name, const xmlChar *arg1_value,
181 const xmlChar *arg2_name, const xmlChar *arg2_value);
182 static void child_died(int signum);
183 static void child_died_callback(void);
184 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition);
185 static void xrandr_size_change(GdkScreen *screen, gpointer user_data);
187 /****************************************************************
188 * EXTERNAL INTERFACE *
189 ****************************************************************/
191 /* The value that goes with an option */
192 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
194 static int rox_x_error(Display *display, XErrorEvent *error)
196 gchar buf[64];
198 XGetErrorText(display, error->error_code, buf, 63);
200 g_warning ("The program '%s' received an X Window System error.\n"
201 "This probably reflects a bug in the program.\n"
202 "The error was '%s'.\n"
203 " (Details: serial %ld error_code %d request_code %d minor_code %d)\n"
204 " (Note to programmers: normally, X errors are reported asynchronously;\n"
205 " that is, you will receive the error a while after causing it.\n"
206 " To debug your program, run it with the --sync command line\n"
207 " option to change this behavior. You can then get a meaningful\n"
208 " backtrace from your debugger.)",
209 g_get_prgname (),
210 buf,
211 error->serial,
212 error->error_code,
213 error->request_code,
214 error->minor_code);
216 /* Try to cope with BadWindow errors */
217 if (error->error_code == 3)
219 static time_t last_error = 0;
220 time_t now;
221 now = time(NULL);
223 if (last_error + 5 > now)
224 abort(); /* Errors coming too fast... */
226 last_error = now;
228 delayed_error(_("We got a BadWindow error from the X server. "
229 "This might be due to this GTK bug (during drag-and-drop?):\n"
230 "http://bugzilla.gnome.org/show_bug.cgi?id=152151\n"
231 "Trying to continue..."));
232 return 0;
235 abort();
238 /* Parses the command-line to work out what the user wants to do.
239 * Tries to send the request to an already-running copy of the filer.
240 * If that fails, it initialises all the other modules and executes the
241 * request itself.
243 int main(int argc, char **argv)
245 int wakeup_pipe[2];
246 int i;
247 struct sigaction act;
248 guchar *tmp, *dir;
249 gchar *client_id = NULL;
250 gboolean show_user = FALSE;
251 xmlDocPtr rpc, soap_rpc = NULL, reply;
252 xmlNodePtr body;
254 home_dir = g_get_home_dir();
255 home_dir_len = strlen(home_dir);
256 app_dir = g_strdup(getenv("APP_DIR"));
258 /* Get internationalisation up and running. This requires the
259 * choices system, to discover the user's preferred language.
261 choices_init();
262 options_init();
263 i18n_init();
264 xtype_init();
266 #ifdef HAVE_GNOME_VFS
267 if (!gnome_vfs_init())
268 g_warning("Failed to init gnomevfs!");
269 #endif
271 if (!app_dir)
273 g_warning("APP_DIR environment variable was unset!\n"
274 "Use the AppRun script to invoke ROX-Filer...\n");
275 app_dir = g_get_current_dir();
277 #ifdef HAVE_UNSETENV
278 else
280 /* Don't pass it on to our child processes... */
281 unsetenv("APP_DIR");
283 #endif
285 /* Sometimes we want to take special action when a child
286 * process exits. This hash table is used to convert the
287 * child's PID to the callback function.
289 death_callbacks = g_hash_table_new(NULL, NULL);
291 /* Find out some information about ourself */
292 euid = geteuid();
293 egid = getegid();
294 ngroups = getgroups(0, NULL);
295 if (ngroups < 0)
296 ngroups = 0;
297 else if (ngroups > 0)
299 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
300 getgroups(ngroups, supplemental_groups);
303 if (argc == 2 && strcmp(argv[1], "-v") == 0)
305 /* This is used by install.sh to test if the filer
306 * compiled OK. Do this test before gtk_init so that
307 * we don't need an X server to install.
309 g_print("ROX-Filer %s\n", VERSION);
310 g_print(_(COPYING));
311 show_features();
312 return EXIT_SUCCESS;
315 option_add_int(&o_override_redirect, "override_redirect", FALSE);
317 /* The idea here is to convert the command-line arguments
318 * into a SOAP RPC.
319 * We attempt to invoke the call on an already-running copy of
320 * the filer if possible, or execute it ourselves if not.
322 rpc = soap_new(&body);
324 /* Note: must do this before checking our options,
325 * otherwise we report an error for Gtk's options.
327 gtk_init(&argc, &argv);
328 /* Set a default style for the collection widget */
329 gtk_rc_parse_string("style \"rox-default-collection-style\" {\n"
330 " bg[NORMAL] = \"#f3f3f3\"\n"
331 " fg[NORMAL] = \"#000000\"\n"
332 " bg[INSENSITIVE] = \"#bfbfbf\"\n"
333 " fg[INSENSITIVE] = \"#000000\"\n"
334 "}\n"
335 "style \"rox-default-pinboard-style\" {\n"
336 " bg[NORMAL] = \"#666666\"\n"
337 "}\n"
338 "widget \"rox-pinboard\" style : gtk "
339 "\"rox-default-pinboard-style\"\n"
341 "class \"Collection\" style : gtk "
342 "\"rox-default-collection-style\"\n");
344 g_signal_connect(gdk_screen_get_default(), "size-changed",
345 G_CALLBACK(xrandr_size_change), NULL);
347 /* Process each option in turn */
348 while (1)
350 int c;
351 #ifdef HAVE_GETOPT_LONG
352 int long_index;
353 c = getopt_long(argc, argv, SHORT_OPS,
354 long_opts, &long_index);
355 #else
356 c = getopt(argc, argv, SHORT_OPS);
357 #endif
359 if (c == EOF)
360 break; /* No more options */
362 switch (c)
364 case 'n':
365 new_copy = TRUE;
366 break;
367 case 'o':
368 info_message(_("The -o argument is no longer "
369 "used. You can turn on override "
370 "redirect from the Options box "
371 "instead."));
372 break;
373 case 'v':
374 g_print("ROX-Filer %s\n", VERSION);
375 g_print("%s", _(COPYING));
376 show_features();
377 return EXIT_SUCCESS;
378 case 'h':
379 g_print("%s", _(HELP));
380 g_print("%s", _(SHORT_ONLY_WARNING));
381 return EXIT_SUCCESS;
382 case 'D':
383 case 'd':
384 case 'x':
385 /* Argument is a path */
386 if (c == 'd' && VALUE[0] == '/')
387 tmp = g_strdup(VALUE);
388 else
389 tmp = pathdup(VALUE);
390 soap_add(body,
391 c == 'D' ? "CloseDir" :
392 c == 'd' ? "OpenDir" :
393 c == 'x' ? "Examine" : "Unknown",
394 "Filename", tmp,
395 NULL, NULL);
396 g_free(tmp);
397 break;
398 case 's':
399 tmp = g_path_get_dirname(VALUE);
401 if (tmp[0] == '/')
402 dir = NULL;
403 else
404 dir = pathdup(tmp);
406 soap_add(body, "Show",
407 "Directory", dir ? dir : tmp,
408 "Leafname", g_basename(VALUE));
409 g_free(tmp);
410 g_free(dir);
411 break;
412 case 'l':
413 case 'r':
414 case 't':
415 case 'b':
416 /* Argument is a leaf (or starts with /) */
417 soap_add(body, "Panel", "Name", VALUE,
418 "Side", c == 'l' ? "Left" :
419 c == 'r' ? "Right" :
420 c == 't' ? "Top" :
421 c == 'b' ? "Bottom" :
422 "Unkown");
423 break;
424 case 'p':
425 soap_add(body, "Pinboard",
426 "Name", VALUE, NULL, NULL);
427 break;
428 case 'u':
429 show_user = TRUE;
430 break;
431 case 'm':
433 MIME_type *type;
434 type_init();
435 diritem_init();
436 pixmaps_init();
437 type = type_get_type(VALUE);
438 printf("%s/%s\n", type->media_type,
439 type->subtype);
440 return EXIT_SUCCESS;
442 case 'c':
443 client_id = g_strdup(VALUE);
444 break;
445 case 'R':
446 soap_rpc = xmlParseFile("-");
447 if (!soap_rpc)
448 g_error("Invalid XML in RPC");
449 break;
450 default:
451 printf(_(USAGE));
452 return EXIT_FAILURE;
456 tooltips = gtk_tooltips_new();
458 if (euid == 0 || show_user)
459 show_user_message = g_strdup_printf(_("Running as user '%s'"),
460 user_name(euid));
462 /* Add each remaining (non-option) argument to the list of files
463 * to run.
465 i = optind;
466 while (i < argc)
468 tmp = pathdup(argv[i++]);
470 soap_add(body, "Run", "Filename", tmp, NULL, NULL);
472 g_free(tmp);
475 if (soap_rpc)
477 if (body->xmlChildrenNode)
478 g_error("Can't use -R with other options - sorry!");
479 xmlFreeDoc(rpc);
480 body = NULL;
481 rpc = soap_rpc;
483 else if (!body->xmlChildrenNode)
485 /* The user didn't request any action. Open the current
486 * directory.
488 guchar *dir;
490 dir = g_get_current_dir();
491 soap_add(body, "OpenDir", "Filename", dir, NULL, NULL);
492 g_free(dir);
495 option_add_int(&o_dnd_no_hostnames, "dnd_no_hostnames", 1);
497 /* Try to send the request to an already-running copy of the filer */
498 gui_support_init();
499 if (remote_init(rpc, new_copy))
500 return EXIT_SUCCESS; /* It worked - exit */
502 /* Put ourselves into the background (so 'rox' always works the
503 * same, whether we're already running or not).
504 * Not for -n, though (helps when debugging).
506 if (!new_copy)
508 int fd;
509 pid_t child;
511 child = fork();
512 if (child > 0)
513 _exit(0); /* Parent exits */
514 /* Otherwise we're the child (or an error occurred - ignore
515 * it!).
518 /* Close stdin. We don't need it, and it can cause problems if
519 * a child process wants a password, etc...
521 fd = open("/dev/null", O_RDONLY);
522 if (fd > 0)
524 close(0);
525 dup2(fd, 0);
526 close(fd);
530 /* Initialize the rest of the filer... */
532 pixmaps_init();
534 dnd_init();
535 bind_init();
536 dir_init();
537 diritem_init();
538 menu_init();
539 minibuffer_init();
540 filer_init();
541 toolbar_init();
542 display_init();
543 mount_init();
544 type_init();
545 action_init();
547 pinboard_init();
548 panel_init();
550 /* Let everyone update */
551 options_notify();
553 /* When we get a signal, we can't do much right then. Instead,
554 * we send a char down this pipe, which causes the main loop to
555 * deal with the event next time we're idle.
557 pipe(wakeup_pipe);
558 close_on_exec(wakeup_pipe[0], TRUE);
559 close_on_exec(wakeup_pipe[1], TRUE);
560 gdk_input_add_full(wakeup_pipe[0], GDK_INPUT_READ, wake_up_cb,
561 NULL, NULL);
562 to_wakeup_pipe = wakeup_pipe[1];
564 /* If the pipe is full then we're going to get woken up anyway... */
565 set_blocking(to_wakeup_pipe, FALSE);
567 /* Let child processes die */
568 act.sa_handler = child_died;
569 sigemptyset(&act.sa_mask);
570 act.sa_flags = SA_NOCLDSTOP;
571 sigaction(SIGCHLD, &act, NULL);
573 /* Ignore SIGPIPE - check for EPIPE errors instead */
574 act.sa_handler = SIG_IGN;
575 sigemptyset(&act.sa_mask);
576 act.sa_flags = 0;
577 sigaction(SIGPIPE, &act, NULL);
579 /* Set up session managament if available */
580 session_init(client_id);
581 g_free(client_id);
583 /* See if we need to migrate the Choices directories*/
584 choices_migrate();
586 /* Finally, execute the request */
587 reply = run_soap(rpc);
588 xmlFreeDoc(rpc);
589 if (reply)
591 /* Write the result, if any, to stdout */
592 save_xml_file(reply, "-");
593 xmlFreeDoc(reply);
596 /* Try to find out why we crash with GTK 2.4 */
597 XSetErrorHandler(rox_x_error);
599 /* Enter the main loop, processing events until all our windows
600 * are closed.
602 if (number_of_windows > 0)
603 gtk_main();
605 return EXIT_SUCCESS;
608 /* Register a function to be called when process number 'child' dies. */
609 void on_child_death(gint child, CallbackFn callback, gpointer data)
611 Callback *cb;
613 g_return_if_fail(callback != NULL);
615 cb = g_new(Callback, 1);
617 cb->callback = callback;
618 cb->data = data;
620 g_hash_table_insert(death_callbacks, GINT_TO_POINTER(child), cb);
623 void one_less_window(void)
625 if (--number_of_windows < 1)
626 gtk_main_quit();
629 /****************************************************************
630 * INTERNAL FUNCTIONS *
631 ****************************************************************/
633 static void show_features(void)
635 g_print("\n");
636 g_print(_("Compiled with GTK version %s\n"), GTK_VERSION);
637 g_print(_("Running with GTK version %d.%d.%d\n"),
638 gtk_major_version,
639 gtk_minor_version,
640 gtk_micro_version);
641 g_print("\n-- %s --\n\n", _("features set at compile time"));
642 g_print("%s... %s\n", _("Large File Support"),
643 #ifdef LARGE_FILE_SUPPORT
644 _("Yes")
645 #else
646 _("No")
647 #endif
649 g_print("%s... %s\n", _("GNOME-VFS library"),
650 # ifdef HAVE_GNOME_VFS
651 _("Yes")
652 # else
653 _("No (need 2.8.0 or later)")
654 # endif
656 g_print("%s... %s\n", _("Dnotify support"),
657 #ifdef USE_DNOTIFY
658 _("Yes")
659 #else
660 _("No")
661 #endif
663 g_print("%s... %s\n", _("Binary compatibility"),
664 #if defined(HAVE_APSYMBOLS_H) || defined(HAVE_APBUILD_APSYMBOLS_H)
665 _("Yes (can run with older glibc versions)")
666 #else
667 _("No (apsymbols.h not found)")
668 #endif
672 static void soap_add(xmlNodePtr body,
673 xmlChar *function,
674 const xmlChar *arg1_name, const xmlChar *arg1_value,
675 const xmlChar *arg2_name, const xmlChar *arg2_value)
677 xmlNodePtr node;
678 xmlNs *rox;
680 rox = xmlSearchNsByHref(body->doc, body, ROX_NS);
682 node = xmlNewChild(body, rox, function, NULL);
684 if (arg1_name)
686 xmlNewTextChild(node, rox, arg1_name, arg1_value);
687 if (arg2_name)
688 xmlNewTextChild(node, rox, arg2_name, arg2_value);
692 /* This is called as a signal handler; simply ensures that
693 * child_died_callback() will get called later.
695 static void child_died(int signum)
697 child_died_flag = TRUE;
698 write(to_wakeup_pipe, "\0", 1); /* Wake up! */
701 static void child_died_callback(void)
703 int status;
704 gint child;
706 child_died_flag = FALSE;
708 /* Find out which children exited and allow them to die */
711 Callback *cb;
713 child = waitpid(-1, &status, WNOHANG);
715 if (child == 0 || child == -1)
716 return;
718 cb = g_hash_table_lookup(death_callbacks,
719 GINT_TO_POINTER(child));
720 if (cb)
722 cb->callback(cb->data);
723 g_hash_table_remove(death_callbacks,
724 GINT_TO_POINTER(child));
727 } while (1);
730 #define BUFLEN 40
731 /* When data is written to_wakeup_pipe, this gets called from the event
732 * loop some time later. Useful for getting out of signal handlers, etc.
734 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition)
736 char buf[BUFLEN];
738 read(source, buf, BUFLEN);
740 if (child_died_flag)
741 child_died_callback();
742 #ifdef USE_DNOTIFY
743 if (dnotify_wakeup)
744 dnotify_wakeup();
745 #endif
748 static void xrandr_size_change(GdkScreen *screen, gpointer user_data)
750 gui_store_screen_geometry(screen);
752 panel_update_size();
753 pinboard_update_size();