r1337: Compile with GDK_DISABLE_DEPRECATED.
[rox-filer.git] / ROX-Filer / src / main.c
blobfa544e42b71c0909fdd0737c7faf04640cfc22b5
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 "icon.h"
72 #include "panel.h"
73 #include "session.h"
74 #include "minibuffer.h"
76 int number_of_windows = 0; /* Quit when this reaches 0 again... */
77 static int to_wakeup_pipe = -1; /* Write here to get noticed */
79 /* Information about the ROX-Filer process */
80 uid_t euid;
81 gid_t egid;
82 int ngroups; /* Number of supplemental groups */
83 gid_t *supplemental_groups = NULL;
85 /* Message to display at the top of each filer window */
86 const gchar *show_user_message = NULL;
88 int home_dir_len;
89 const char *home_dir, *app_dir;
91 GtkTooltips *tooltips = NULL;
93 #define COPYING \
94 N_("Copyright (C) 2002 Thomas Leonard.\n" \
95 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
96 "to the extent permitted by law.\n" \
97 "You may redistribute copies of ROX-Filer\n" \
98 "under the terms of the GNU General Public License.\n" \
99 "For more information about these matters, " \
100 "see the file named COPYING.\n")
102 #ifdef HAVE_GETOPT_LONG
103 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
104 # define SHORT_ONLY_WARNING ""
105 #else
106 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
107 # define SHORT_ONLY_WARNING \
108 _("NOTE: Your system does not support long options - \n" \
109 "you must use the short versions instead.\n\n")
110 #endif
112 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
113 "Open each directory or file listed, or the current working\n" \
114 "directory if no arguments are given.\n\n" \
115 " -b, --bottom=PANEL open PAN as a bottom-edge panel\n" \
116 " -c, --client-id=ID used for session management\n" \
117 " -d, --dir=DIR open DIR as directory (not application)\n" \
118 " -D, --close=DIR close DIR and its subdirectories\n" \
119 " -h, --help display this help and exit\n" \
120 " -l, --left=PANEL open PAN as a left-edge panel\n" \
121 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
122 " -n, --new start a new filer, even if already running\n" \
123 " -o, --override override window manager control of panels\n" \
124 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
125 " -r, --right=PANEL open PAN as a right-edge panel\n" \
126 " -R, --RPC invoke method call read from stdin\n" \
127 " -s, --show=FILE open a directory showing FILE\n" \
128 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
129 " -u, --user show user name in each window \n" \
130 " -v, --version display the version information and exit\n" \
131 " -x, --examine=FILE FILE has changed - re-examine it\n" \
132 "\nThe latest version can be found at:\n" \
133 "\thttp://rox.sourceforge.net\n" \
134 "\nReport bugs to <tal197@users.sourceforge.net>.\n")
136 #define SHORT_OPS "d:t:b:l:r:op:s:hvnux:m:D:R"
138 #ifdef HAVE_GETOPT_LONG
139 static struct option long_opts[] =
141 {"dir", 1, NULL, 'd'},
142 {"top", 1, NULL, 't'},
143 {"bottom", 1, NULL, 'b'},
144 {"left", 1, NULL, 'l'},
145 {"override", 0, NULL, 'o'},
146 {"pinboard", 1, NULL, 'p'},
147 {"right", 1, NULL, 'r'},
148 {"help", 0, NULL, 'h'},
149 {"version", 0, NULL, 'v'},
150 {"user", 0, NULL, 'u'},
151 {"new", 0, NULL, 'n'},
152 {"RPC", 0, NULL, 'R'},
153 {"show", 1, NULL, 's'},
154 {"examine", 1, NULL, 'x'},
155 {"close", 1, NULL, 'D'},
156 {"mime-type", 1, NULL, 'm'},
157 {"client-id", 1, NULL, 'c'},
158 {NULL, 0, NULL, 0},
160 #endif
162 /* Take control of panels away from WM? */
163 gboolean override_redirect = FALSE;
165 /* Always start a new filer, even if one seems to be already running */
166 gboolean new_copy = FALSE;
168 /* Maps child PIDs to Callback pointers */
169 static GHashTable *death_callbacks = NULL;
170 static gboolean child_died_flag = FALSE;
172 Option o_dnd_no_hostnames;
174 /* Static prototypes */
175 static void show_features(void);
176 static void soap_add(xmlNodePtr body,
177 xmlChar *function,
178 xmlChar *arg1_name, xmlChar *arg1_value,
179 xmlChar *arg2_name, xmlChar *arg2_value);
180 static void child_died(int signum);
181 static void child_died_callback(void);
182 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition);
184 /****************************************************************
185 * EXTERNAL INTERFACE *
186 ****************************************************************/
188 /* The value that goes with an option */
189 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
191 /* Parses the command-line to work out what the user wants to do.
192 * Tries to send the request to an already-running copy of the filer.
193 * If that fails, it initialises all the other modules and executes the
194 * request itself.
196 int main(int argc, char **argv)
198 int wakeup_pipe[2];
199 int i;
200 struct sigaction act;
201 guchar *tmp, *dir, *slash;
202 gchar *client_id = NULL;
203 gboolean show_user = FALSE;
204 xmlDocPtr rpc, soap_rpc = NULL, reply;
205 xmlNodePtr body;
207 home_dir = g_get_home_dir();
208 home_dir_len = strlen(home_dir);
209 app_dir = g_strdup(getenv("APP_DIR"));
211 /* Get internationalisation up and running. This requires the
212 * choices system, to discover the user's preferred language.
214 choices_init();
215 options_init();
216 i18n_init();
218 if (!app_dir)
220 g_warning("APP_DIR environment variable was unset!\n"
221 "Use the AppRun script to invoke ROX-Filer...\n");
222 app_dir = g_get_current_dir();
224 #ifdef HAVE_UNSETENV
225 else
227 /* Don't pass it on to our child processes... */
228 unsetenv("APP_DIR");
230 #endif
232 /* Sometimes we want to take special action when a child
233 * process exits. This hash table is used to convert the
234 * child's PID to the callback function.
236 death_callbacks = g_hash_table_new(NULL, NULL);
238 /* Find out some information about ourself */
239 euid = geteuid();
240 egid = getegid();
241 ngroups = getgroups(0, NULL);
242 if (ngroups < 0)
243 ngroups = 0;
244 else if (ngroups > 0)
246 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
247 getgroups(ngroups, supplemental_groups);
250 if (argc == 2 && strcmp(argv[1], "-v") == 0)
252 /* This is used by install.sh to test if the filer
253 * compiled OK. Do this test before gtk_init so that
254 * we don't need an X server to install.
256 fprintf(stderr, "ROX-Filer %s\n", VERSION);
257 fprintf(stderr, _(COPYING));
258 show_features();
259 return EXIT_SUCCESS;
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 add_default_styles();
273 gtk_init(&argc, &argv);
275 /* Process each option in turn */
276 while (1)
278 int c;
279 #ifdef HAVE_GETOPT_LONG
280 int long_index;
281 c = getopt_long(argc, argv, SHORT_OPS,
282 long_opts, &long_index);
283 #else
284 c = getopt(argc, argv, SHORT_OPS);
285 #endif
287 if (c == EOF)
288 break; /* No more options */
290 switch (c)
292 case 'n':
293 new_copy = TRUE;
294 break;
295 case 'o':
296 override_redirect = TRUE;
297 break;
298 case 'v':
299 g_printerr("ROX-Filer %s\n", VERSION);
300 g_printerr("%s", _(COPYING));
301 show_features();
302 return EXIT_SUCCESS;
303 case 'h':
304 g_printerr("%s", _(HELP));
305 g_printerr("%s", _(SHORT_ONLY_WARNING));
306 return EXIT_SUCCESS;
307 case 'D':
308 case 'd':
309 case 'x':
310 /* Argument is a path */
311 tmp = pathdup(VALUE);
312 soap_add(body,
313 c == 'D' ? "CloseDir" :
314 c == 'd' ? "OpenDir" :
315 c == 'x' ? "Examine" : "Unknown",
316 "Filename", tmp,
317 NULL, NULL);
318 g_free(tmp);
319 break;
320 case 's':
321 tmp = g_strdup(VALUE);
322 slash = strrchr(tmp, '/');
323 if (slash)
325 *slash = '\0';
326 slash++;
327 dir = pathdup(tmp);
329 else
331 slash = tmp;
332 dir = pathdup(".");
335 soap_add(body, "Show",
336 "Directory", dir,
337 "Leafname", slash);
338 g_free(tmp);
339 g_free(dir);
340 break;
341 case 'l':
342 case 'r':
343 case 't':
344 case 'b':
345 /* Argument is a leaf (or starts with /) */
346 soap_add(body, "Panel", "Name", VALUE,
347 "Side", c == 'l' ? "Left" :
348 c == 'r' ? "Right" :
349 c == 't' ? "Top" :
350 c == 'b' ? "Bottom" :
351 "Unkown");
352 break;
353 case 'p':
354 soap_add(body, "Pinboard",
355 "Name", VALUE, NULL, NULL);
356 break;
357 case 'u':
358 show_user = TRUE;
359 break;
360 case 'm':
362 MIME_type *type;
363 type_init();
364 type = type_get_type(VALUE);
365 printf("%s/%s\n", type->media_type,
366 type->subtype);
368 return EXIT_SUCCESS;
369 case 'c':
370 client_id = g_strdup(VALUE);
371 break;
372 case 'R':
373 soap_rpc = xmlParseFile("-");
374 if (!soap_rpc)
375 g_error("Invalid XML in RPC");
376 break;
377 default:
378 printf(_(USAGE));
379 return EXIT_FAILURE;
383 tooltips = gtk_tooltips_new();
385 if (euid == 0 || show_user)
386 show_user_message = g_strdup_printf( _("Running as user '%s'"),
387 user_name(euid));
389 /* Add each remaining (non-option) argument to the list of files
390 * to run.
392 i = optind;
393 while (i < argc)
395 tmp = pathdup(argv[i++]);
397 soap_add(body, "Run", "Filename", tmp, NULL, NULL);
399 g_free(tmp);
402 if (soap_rpc)
404 if (body->xmlChildrenNode)
405 g_error("Can't use -R with other options - sorry!");
406 xmlFreeDoc(rpc);
407 body = NULL;
408 rpc = soap_rpc;
410 else if (!body->xmlChildrenNode)
412 /* The user didn't request any action. Open the current
413 * directory.
415 guchar *dir;
417 dir = g_get_current_dir();
418 soap_add(body, "OpenDir", "Filename", dir, NULL, NULL);
419 g_free(dir);
422 option_add_int(&o_dnd_no_hostnames, "dnd_no_hostnames", 1);
424 /* Try to send the request to an already-running copy of the filer */
425 gui_support_init();
426 if (remote_init(rpc, new_copy))
427 return EXIT_SUCCESS; /* It worked - exit */
429 /* Put ourselves into the background (so 'rox' always works the
430 * same, whether we're already running or not).
431 * Not for -n, though (helps when debugging).
433 if (!new_copy)
435 pid_t child;
437 child = fork();
438 if (child > 0)
439 _exit(0); /* Parent exits */
440 /* Otherwise we're the child (or an error occurred - ignore
441 * it!).
445 /* Close stdin. We don't need it, and it can cause problems if
446 * a child process wants a password, etc...
449 int fd;
450 fd = open("/dev/null", O_RDONLY);
451 if (fd > 0)
453 close(0);
454 dup2(fd, 0);
455 close(fd);
459 /* Initialize the rest of the filer... */
461 pixmaps_init();
463 dnd_init();
464 bind_init();
465 dir_init();
466 diritem_init();
467 menu_init();
468 minibuffer_init();
469 filer_init();
470 toolbar_init();
471 display_init();
472 mount_init();
473 type_init();
474 action_init();
476 icon_init();
477 pinboard_init();
478 panel_init();
480 /* Let everyone update */
481 options_notify();
483 /* When we get a signal, we can't do much right then. Instead,
484 * we send a char down this pipe, which causes the main loop to
485 * deal with the event next time we're idle.
487 pipe(wakeup_pipe);
488 close_on_exec(wakeup_pipe[0], TRUE);
489 close_on_exec(wakeup_pipe[1], TRUE);
490 gtk_input_add_full(wakeup_pipe[0], GDK_INPUT_READ, wake_up_cb,
491 NULL, NULL, NULL);
492 to_wakeup_pipe = wakeup_pipe[1];
494 /* If the pipe is full then we're going to get woken up anyway... */
495 set_blocking(to_wakeup_pipe, FALSE);
497 /* Let child processes die */
498 act.sa_handler = child_died;
499 sigemptyset(&act.sa_mask);
500 act.sa_flags = SA_NOCLDSTOP;
501 sigaction(SIGCHLD, &act, NULL);
503 /* Ignore SIGPIPE - check for EPIPE errors instead */
504 act.sa_handler = SIG_IGN;
505 sigemptyset(&act.sa_mask);
506 act.sa_flags = 0;
507 sigaction(SIGPIPE, &act, NULL);
509 /* Set up session managament if available */
510 session_init(client_id);
511 g_free(client_id);
513 /* Finally, execute the request */
514 reply = run_soap(rpc);
515 xmlFreeDoc(rpc);
516 if (reply)
518 /* Write the result, if any, to stdout */
519 save_xml_file(reply, "-");
520 xmlFreeDoc(reply);
523 /* Enter the main loop, processing events until all our windows
524 * are closed.
526 if (number_of_windows > 0)
527 gtk_main();
529 return EXIT_SUCCESS;
532 /* Register a function to be called when process number 'child' dies. */
533 void on_child_death(gint child, CallbackFn callback, gpointer data)
535 Callback *cb;
537 g_return_if_fail(callback != NULL);
539 cb = g_new(Callback, 1);
541 cb->callback = callback;
542 cb->data = data;
544 g_hash_table_insert(death_callbacks, GINT_TO_POINTER(child), cb);
547 /****************************************************************
548 * INTERNAL FUNCTIONS *
549 ****************************************************************/
551 static void show_features(void)
553 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
554 g_printerr("%s... %s\n", _("Large File Support"),
555 #ifdef LARGE_FILE_SUPPORT
556 _("Yes")
557 #else
558 _("No")
559 #endif
561 g_printerr("%s... %s\n", _("GNOME-VFS library"),
562 #ifdef WITH_GNOMEVFS
563 _("Yes")
564 #else
565 _("No (gnome-vfs-config not found)")
566 #endif
570 static void soap_add(xmlNodePtr body,
571 xmlChar *function,
572 xmlChar *arg1_name, xmlChar *arg1_value,
573 xmlChar *arg2_name, xmlChar *arg2_value)
575 xmlNodePtr node;
576 xmlNs *rox;
578 rox = xmlSearchNsByHref(body->doc, body, ROX_NS);
580 node = xmlNewChild(body, rox, function, NULL);
582 if (arg1_name)
584 xmlNewTextChild(node, rox, arg1_name, arg1_value);
585 if (arg2_name)
586 xmlNewTextChild(node, rox, arg2_name, arg2_value);
590 /* This is called as a signal handler; simply ensures that
591 * child_died_callback() will get called later.
593 static void child_died(int signum)
595 child_died_flag = TRUE;
596 write(to_wakeup_pipe, "\0", 1); /* Wake up! */
599 static void child_died_callback(void)
601 int status;
602 gint child;
604 child_died_flag = FALSE;
606 /* Find out which children exited and allow them to die */
609 Callback *cb;
611 child = waitpid(-1, &status, WNOHANG);
613 if (child == 0 || child == -1)
614 return;
616 cb = g_hash_table_lookup(death_callbacks,
617 GINT_TO_POINTER(child));
618 if (cb)
620 cb->callback(cb->data);
621 g_hash_table_remove(death_callbacks,
622 GINT_TO_POINTER(child));
625 } while (1);
628 #define BUFLEN 40
629 /* When data is written to_wakeup_pipe, this gets called from the event
630 * loop some time later. Useful for getting out of signal handlers, etc.
632 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition)
634 char buf[BUFLEN];
636 read(source, buf, BUFLEN);
638 if (child_died_flag)
639 child_died_callback();