Release 2.11
[rox-filer.git] / ROX-Filer / src / main.c
blobf5a193bc6a027b2d6912eb62f9babedfa4478ac6
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* main.c - parses command-line options and parameters, plus some global
21 * housekeeping.
23 * New to the code and feeling lost? Read global.h now.
26 #include "config.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <signal.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <pwd.h>
38 #include <grp.h>
39 #include <libxml/parser.h>
41 #ifdef HAVE_GETOPT_LONG
42 # include <getopt.h>
43 #endif
45 #include <gtk/gtk.h>
46 #include <gdk/gdkx.h> /* For rox_x_error */
48 #include "global.h"
50 #include "main.h"
51 #include "log.h"
52 #include "support.h"
53 #include "gui_support.h"
54 #include "filer.h"
55 #include "display.h"
56 #include "mount.h"
57 #include "menu.h"
58 #include "dnd.h"
59 #include "options.h"
60 #include "choices.h"
61 #include "type.h"
62 #include "pixmaps.h"
63 #include "dir.h"
64 #include "diritem.h"
65 #include "action.h"
66 #include "i18n.h"
67 #include "remote.h"
68 #include "pinboard.h"
69 #include "run.h"
70 #include "toolbar.h"
71 #include "bind.h"
72 #include "panel.h"
73 #include "session.h"
74 #include "minibuffer.h"
75 #include "xtypes.h"
76 #include "bulk_rename.h"
77 #include "gtksavebox.h"
79 int number_of_windows = 0; /* Quit when this reaches 0 again... */
80 int to_wakeup_pipe = -1; /* Write here to get noticed */
82 /* Information about the ROX-Filer process */
83 uid_t euid;
84 gid_t egid;
85 int ngroups; /* Number of supplemental groups */
86 gid_t *supplemental_groups = NULL;
88 /* Message to display at the top of each filer window */
89 const gchar *show_user_message = NULL;
91 int home_dir_len;
92 const char *home_dir, *app_dir;
94 GtkTooltips *tooltips = NULL;
96 #define COPYING \
97 N_("Copyright (C) 2005 Thomas Leonard.\n" \
98 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
99 "to the extent permitted by law.\n" \
100 "You may redistribute copies of ROX-Filer\n" \
101 "under the terms of the GNU General Public License.\n" \
102 "For more information about these matters, " \
103 "see the file named COPYING.\n")
105 #ifdef HAVE_GETOPT_LONG
106 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
107 # define SHORT_ONLY_WARNING ""
108 #else
109 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
110 # define SHORT_ONLY_WARNING \
111 _("NOTE: Your system does not support long options - \n" \
112 "you must use the short versions instead.\n\n")
113 #endif
115 #define BUGS_TO "<rox-devel@lists.sourceforge.net>"
117 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
118 "Open each directory or file listed, or the current working\n" \
119 "directory if no arguments are given.\n\n" \
120 " -b, --border=PANEL open PANEL as a border panel\n" \
121 " -B, --bottom=PANEL open PAN as a bottom-edge panel\n" \
122 " -c, --client-id=ID used for session management\n" \
123 " -d, --dir=DIR open DIR as directory (not application)\n" \
124 " -D, --close=DIR close DIR and its subdirectories\n" \
125 " -h, --help display this help and exit\n" \
126 " -l, --left=PANEL open PAN as a left-edge panel\n" \
127 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
128 " -n, --new start new copy; for debugging the filer\n" \
129 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
130 " -r, --right=PANEL open PAN as a right-edge panel\n" \
131 " -R, --RPC invoke method call read from stdin\n" \
132 " -s, --show=FILE open a directory showing FILE\n" \
133 " -S, --rox-session use default panel and pinboard options, and -n\n"\
134 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
135 " -u, --user show user name in each window \n" \
136 " -U, --url=URL open file or directory in URI form\n" \
137 " -v, --version display the version information and exit\n" \
138 " -x, --examine=FILE FILE has changed - re-examine it\n" \
139 "\nReport bugs to %s.\n" \
140 "Home page (including updated versions): http://rox.sourceforge.net/\n")
142 #define SHORT_OPS "c:d:t:b:l:r:B:op:s:hvnux:m:D:RSU:"
144 #ifdef HAVE_GETOPT_LONG
145 static struct option long_opts[] =
147 {"dir", 1, NULL, 'd'},
148 {"top", 1, NULL, 't'},
149 {"bottom", 1, NULL, 'B'},
150 {"border", 1, NULL, 'b'},
151 {"left", 1, NULL, 'l'},
152 {"override", 0, NULL, 'o'},
153 {"pinboard", 1, NULL, 'p'},
154 {"right", 1, NULL, 'r'},
155 {"help", 0, NULL, 'h'},
156 {"version", 0, NULL, 'v'},
157 {"user", 0, NULL, 'u'},
158 {"new", 0, NULL, 'n'},
159 {"RPC", 0, NULL, 'R'},
160 {"show", 1, NULL, 's'},
161 {"rox-session", 0, NULL, 'S'},
162 {"examine", 1, NULL, 'x'},
163 {"close", 1, NULL, 'D'},
164 {"mime-type", 1, NULL, 'm'},
165 {"client-id", 1, NULL, 'c'},
166 {"url", 1, NULL, 'u'},
167 {NULL, 0, NULL, 0},
169 #endif
171 /* Take control of panels away from WM? */
172 Option o_override_redirect;
174 /* Options used when we are called by ROX-Session */
175 enum {
176 SESSION_PANEL_ONLY,
177 SESSION_PINBOARD_ONLY,
178 SESSION_BOTH,
180 Option o_session_panel_or_pin;
181 Option o_session_panel_name; /* Now a comma-separated list */
182 Option o_session_pinboard_name;
184 /* Always start a new filer, even if one seems to be already running */
185 gboolean new_copy = FALSE;
187 /* Maps child PIDs to Callback pointers */
188 static GHashTable *death_callbacks = NULL;
189 static gboolean child_died_flag = FALSE;
191 Option o_dnd_no_hostnames;
193 /* Static prototypes */
194 static void show_features(void);
195 static void soap_add(xmlNodePtr body,
196 xmlChar *function,
197 const xmlChar *arg1_name, const xmlChar *arg1_value,
198 const xmlChar *arg2_name, const xmlChar *arg2_value);
199 static void soap_reply(xmlDocPtr reply, gboolean rpc_mode);
200 static void child_died(int signum);
201 static void child_died_callback(void);
202 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition);
203 static void xrandr_size_change(GdkScreen *screen, gpointer user_data);
204 static void add_default_panel_and_pinboard(xmlNodePtr body);
205 static GList *build_launch(Option *option, xmlNode *node, guchar *label);
206 static GList *build_make_script(Option *option, xmlNode *node, guchar *label);
208 /****************************************************************
209 * EXTERNAL INTERFACE *
210 ****************************************************************/
212 /* The value that goes with an option */
213 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
215 static int rox_x_error(Display *display, XErrorEvent *error)
217 gchar buf[64];
219 XGetErrorText(display, error->error_code, buf, 63);
221 g_warning ("The program '%s' received an X Window System error.\n"
222 "This probably reflects a bug in the program.\n"
223 "The error was '%s'.\n"
224 " (Details: serial %ld error_code %d request_code %d minor_code %d)\n"
225 " (Note to programmers: normally, X errors are reported asynchronously;\n"
226 " that is, you will receive the error a while after causing it.\n"
227 " To debug your program, run it with the --sync command line\n"
228 " option to change this behavior. You can then get a meaningful\n"
229 " backtrace from your debugger.)",
230 g_get_prgname (),
231 buf,
232 error->serial,
233 error->error_code,
234 error->request_code,
235 error->minor_code);
237 /* Try to cope with BadWindow errors */
238 if (error->error_code == BadWindow || error->error_code == BadDrawable)
240 g_warning(_("We got a BadWindow error from the X server. "
241 "This might be due to this GTK bug (during drag-and-drop?):\n"
242 "http://bugzilla.gnome.org/show_bug.cgi?id=152151\n"
243 "Trying to continue..."));
244 return 0;
247 abort();
250 /* Parses the command-line to work out what the user wants to do.
251 * Tries to send the request to an already-running copy of the filer.
252 * If that fails, it initialises all the other modules and executes the
253 * request itself.
255 int main(int argc, char **argv)
257 int wakeup_pipe[2];
258 int i;
259 struct sigaction act;
260 guchar *tmp, *dir;
261 gchar *client_id = NULL;
262 gboolean show_user = FALSE;
263 gboolean rpc_mode = FALSE;
264 xmlDocPtr rpc, soap_rpc = NULL, reply;
265 xmlNodePtr body;
266 int fd, ofd0=-1;
268 /* Relocate stdin. We do need it (-R), but it can cause problems if
269 * a child process wants a password, etc...
270 * Do this BEFORE opening anything (e.g., the X connection), in
271 * case fd 0 isn't open at this point.
273 fd = open("/dev/null", O_RDONLY);
274 if (fd > 0)
276 ofd0=dup(0);
277 close(0);
278 dup2(fd, 0);
279 close(fd);
282 home_dir = g_get_home_dir();
283 home_dir_len = strlen(home_dir);
284 app_dir = g_strdup(getenv("APP_DIR"));
286 /* Get internationalisation up and running. This requires the
287 * choices system, to discover the user's preferred language.
289 choices_init();
290 options_init();
291 i18n_init();
292 xattr_init();
294 if (!app_dir)
296 g_warning("APP_DIR environment variable was unset!\n"
297 "Use the AppRun script to invoke ROX-Filer...\n");
298 app_dir = g_get_current_dir();
300 #ifdef HAVE_UNSETENV
301 else
303 /* Don't pass it on to our child processes... */
304 unsetenv("APP_DIR");
306 #endif
308 /* Sometimes we want to take special action when a child
309 * process exits. This hash table is used to convert the
310 * child's PID to the callback function.
312 death_callbacks = g_hash_table_new(NULL, NULL);
314 /* Find out some information about ourself */
315 euid = geteuid();
316 egid = getegid();
317 ngroups = getgroups(0, NULL);
318 if (ngroups < 0)
319 ngroups = 0;
320 else if (ngroups > 0)
322 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
323 getgroups(ngroups, supplemental_groups);
326 if (argc == 2 && strcmp(argv[1], "-v") == 0)
328 /* This is used by install.sh to test if the filer
329 * compiled OK. Do this test before gtk_init so that
330 * we don't need an X server to install.
332 g_print("ROX-Filer %s\n", VERSION);
333 g_print(_(COPYING));
334 show_features();
335 return EXIT_SUCCESS;
338 option_add_int(&o_override_redirect, "override_redirect", FALSE);
340 option_add_int(&o_session_panel_or_pin, "session_panel_or_pin",
341 SESSION_BOTH);
342 option_add_string(&o_session_panel_name, "session_panel_name",
343 "Default");
344 option_add_string(&o_session_pinboard_name, "session_pinboard_name",
345 "Default");
346 option_register_widget("launch", build_launch);
347 option_register_widget("make-script", build_make_script);
349 #ifdef UNIT_TESTS
350 bulk_rename_tests();
351 #endif
353 /* The idea here is to convert the command-line arguments
354 * into a SOAP RPC.
355 * We attempt to invoke the call on an already-running copy of
356 * the filer if possible, or execute it ourselves if not.
358 rpc = soap_new(&body);
360 /* Note: must do this before checking our options,
361 * otherwise we report an error for Gtk's options.
363 gtk_init(&argc, &argv);
364 /* Set a default style for the collection widget */
365 gtk_rc_parse_string("style \"rox-default-collection-style\" {\n"
366 " bg[NORMAL] = \"#f3f3f3\"\n"
367 " fg[NORMAL] = \"#000000\"\n"
368 " bg[INSENSITIVE] = \"#bfbfbf\"\n"
369 " fg[INSENSITIVE] = \"#000000\"\n"
370 "}\n"
371 "style \"rox-default-pinboard-style\" {\n"
372 " bg[NORMAL] = \"#666666\"\n"
373 "}\n"
374 "widget \"rox-pinboard\" style : gtk "
375 "\"rox-default-pinboard-style\"\n"
377 "class \"Collection\" style : gtk "
378 "\"rox-default-collection-style\"\n");
380 g_signal_connect(gdk_screen_get_default(), "size-changed",
381 G_CALLBACK(xrandr_size_change), NULL);
383 /* Process each option in turn */
384 while (1)
386 int c;
387 #ifdef HAVE_GETOPT_LONG
388 int long_index;
389 c = getopt_long(argc, argv, SHORT_OPS,
390 long_opts, &long_index);
391 #else
392 c = getopt(argc, argv, SHORT_OPS);
393 #endif
395 if (c == EOF)
396 break; /* No more options */
398 switch (c)
400 case 'n':
401 new_copy = TRUE;
402 break;
403 case 'o':
404 info_message(_("The -o argument is no longer "
405 "used. You can turn on override "
406 "redirect from the Options box "
407 "instead."));
408 break;
409 case 'v':
410 g_print("ROX-Filer %s\n", VERSION);
411 g_print("%s", _(COPYING));
412 show_features();
413 return EXIT_SUCCESS;
414 case 'h':
415 g_print(_(HELP), BUGS_TO);
416 g_print("%s", _(SHORT_ONLY_WARNING));
417 return EXIT_SUCCESS;
418 case 'D':
419 case 'd':
420 case 'x':
421 /* Argument is a path */
422 if (c == 'd' && VALUE[0] == '/')
423 tmp = g_strdup(VALUE);
424 else
425 tmp = pathdup(VALUE);
426 soap_add(body,
427 c == 'D' ? "CloseDir" :
428 c == 'd' ? "OpenDir" :
429 c == 'x' ? "Examine" : "Unknown",
430 "Filename", tmp,
431 NULL, NULL);
432 g_free(tmp);
433 break;
434 case 's':
435 tmp = g_path_get_dirname(VALUE);
437 if (tmp[0] == '/')
438 dir = NULL;
439 else
440 dir = pathdup(tmp);
442 soap_add(body, "Show",
443 "Directory", dir ? dir : tmp,
444 "Leafname", g_basename(VALUE));
445 g_free(tmp);
446 g_free(dir);
447 break;
448 case 'l':
449 case 'r':
450 case 't':
451 case 'B':
452 /* Argument is a leaf (or starts with /) */
453 soap_add(body, "Panel", "Name", VALUE,
454 "Side", c == 'l' ? "Left" :
455 c == 'r' ? "Right" :
456 c == 't' ? "Top" :
457 c == 'B' ? "Bottom" :
458 "Unkown");
459 break;
460 case 'b':
461 /* Argument is a leaf (or starts with /) */
462 if (*VALUE)
463 soap_add(body, "Panel", "Name", VALUE,
464 NULL, NULL);
465 else
466 soap_add(body, "Panel",
467 "Side", "Bottom",
468 NULL, NULL);
469 break;
470 case 'p':
471 soap_add(body, "Pinboard",
472 "Name", VALUE, NULL, NULL);
473 break;
474 case 'u':
475 show_user = TRUE;
476 break;
477 case 'm':
479 MIME_type *type;
480 type_init();
481 diritem_init();
482 pixmaps_init();
483 type = type_get_type(VALUE);
484 printf("%s/%s\n", type->media_type,
485 type->subtype);
486 return EXIT_SUCCESS;
488 case 'c':
489 client_id = g_strdup(VALUE);
490 break;
491 case 'R':
492 /* Reconnect stdin */
493 if(ofd0>-1) {
494 close(0);
495 dup2(ofd0, 0);
497 soap_rpc = xmlParseFile("-");
498 if (!soap_rpc)
499 g_error("Invalid XML in RPC");
500 /* Disconnect stdin again */
501 fd = open("/dev/null", O_RDONLY);
502 if (fd > 0)
504 close(0);
505 dup2(fd, 0);
506 close(fd);
508 /* Want to print return uninterpreted */
509 rpc_mode=TRUE;
511 break;
513 case 'S':
514 new_copy = TRUE;
515 add_default_panel_and_pinboard(body);
516 session_auto_respawn = TRUE;
517 break;
519 case 'U':
520 soap_add(body, "RunURI",
521 "URI", VALUE, NULL, NULL);
522 break;
524 default:
525 printf(_(USAGE));
526 return EXIT_FAILURE;
530 tooltips = gtk_tooltips_new();
532 if (euid == 0 || show_user)
533 show_user_message = g_strdup_printf(_("Running as user '%s'"),
534 user_name(euid));
536 /* Add each remaining (non-option) argument to the list of files
537 * to run.
539 i = optind;
540 while (i < argc)
542 tmp = pathdup(argv[i++]);
544 soap_add(body, "Run", "Filename", tmp, NULL, NULL);
546 g_free(tmp);
549 if (soap_rpc)
551 if (body->xmlChildrenNode)
552 g_error("Can't use -R with other options - sorry!");
553 xmlFreeDoc(rpc);
554 body = NULL;
555 rpc = soap_rpc;
557 else if (!body->xmlChildrenNode)
559 /* The user didn't request any action. Open the current
560 * directory.
562 guchar *dir;
564 dir = g_get_current_dir();
565 soap_add(body, "OpenDir", "Filename", dir, NULL, NULL);
566 g_free(dir);
569 option_add_int(&o_dnd_no_hostnames, "dnd_no_hostnames", 1);
571 /* Try to send the request to an already-running copy of the filer */
572 gui_support_init();
573 if (remote_init(rpc, new_copy))
574 return EXIT_SUCCESS; /* It worked - exit */
576 /* Put ourselves into the background (so 'rox' always works the
577 * same, whether we're already running or not).
578 * Not for -n, though (helps when debugging).
580 if (!new_copy)
582 pid_t child;
584 child = fork();
585 if (child > 0)
586 _exit(0); /* Parent exits */
587 /* Otherwise we're the child (or an error occurred - ignore
588 * it!).
592 /* Initialize the rest of the filer... */
594 pixmaps_init();
596 log_init();
597 dnd_init();
598 bind_init();
599 dir_init();
600 diritem_init();
601 menu_init();
602 minibuffer_init();
603 filer_init();
604 toolbar_init();
605 display_init();
606 mount_init();
607 type_init();
608 action_init();
610 pinboard_init();
611 panel_init();
613 /* Let everyone update */
614 options_notify();
616 /* When we get a signal, we can't do much right then. Instead,
617 * we send a char down this pipe, which causes the main loop to
618 * deal with the event next time we're idle.
620 pipe(wakeup_pipe);
621 close_on_exec(wakeup_pipe[0], TRUE);
622 close_on_exec(wakeup_pipe[1], TRUE);
623 gdk_input_add_full(wakeup_pipe[0], GDK_INPUT_READ, wake_up_cb,
624 NULL, NULL);
625 to_wakeup_pipe = wakeup_pipe[1];
627 /* If the pipe is full then we're going to get woken up anyway... */
628 set_blocking(to_wakeup_pipe, FALSE);
630 /* Let child processes die */
631 act.sa_handler = child_died;
632 sigemptyset(&act.sa_mask);
633 act.sa_flags = SA_NOCLDSTOP;
634 sigaction(SIGCHLD, &act, NULL);
636 /* Ignore SIGPIPE - check for EPIPE errors instead */
637 act.sa_handler = SIG_IGN;
638 sigemptyset(&act.sa_mask);
639 act.sa_flags = 0;
640 sigaction(SIGPIPE, &act, NULL);
642 /* Set up session managament if available */
643 session_init(client_id);
644 g_free(client_id);
646 /* See if we need to migrate the Choices directories*/
647 choices_migrate();
649 /* Finally, execute the request */
650 reply = run_soap(rpc);
651 xmlFreeDoc(rpc);
652 soap_reply(reply, rpc_mode);
654 /* Try to find out why we crash with GTK 2.4 */
655 XSetErrorHandler(rox_x_error);
657 /* Enter the main loop, processing events until all our windows
658 * are closed.
660 if (number_of_windows > 0)
661 gtk_main();
663 return EXIT_SUCCESS;
666 /* Register a function to be called when process number 'child' dies. */
667 void on_child_death(gint child, CallbackFn callback, gpointer data)
669 Callback *cb;
671 g_return_if_fail(callback != NULL);
673 cb = g_new(Callback, 1);
675 cb->callback = callback;
676 cb->data = data;
678 g_hash_table_insert(death_callbacks, GINT_TO_POINTER(child), cb);
681 void one_less_window(void)
683 if (--number_of_windows < 1)
684 gtk_main_quit();
687 /****************************************************************
688 * INTERNAL FUNCTIONS *
689 ****************************************************************/
691 static void show_features(void)
693 g_print("\n");
694 g_print(_("Compiled with GTK version %s\n"), GTK_VERSION);
695 g_print(_("Running with GTK version %d.%d.%d\n"),
696 gtk_major_version,
697 gtk_minor_version,
698 gtk_micro_version);
699 g_print("\n-- %s --\n\n", _("features set at compile time"));
700 g_print("%s... %s\n", _("Large File Support"),
701 #ifdef LARGE_FILE_SUPPORT
702 _("Yes")
703 #else
704 _("No")
705 #endif
707 g_print("%s... %s\n", _("Inotify support"),
708 #ifdef USE_INOTIFY
709 _("Yes")
710 #else
711 _("No")
712 #endif
714 g_print("%s... %s\n", _("Dnotify support"),
715 #ifdef USE_DNOTIFY
716 _("Yes")
717 #else
718 _("No")
719 #endif
721 g_print("%s... %s\n", _("Binary compatibility"),
722 #if defined(HAVE_APSYMBOLS_H) || defined(HAVE_APBUILD_APSYMBOLS_H)
723 _("Yes (can run with older glibc versions)")
724 #else
725 _("No (apsymbols.h not found)")
726 #endif
729 g_print("%s... %s\n", _("Extended attribute support"),
730 xattr_supported(NULL)? _("Yes"): _("No"));
733 static void soap_add(xmlNodePtr body,
734 xmlChar *function,
735 const xmlChar *arg1_name, const xmlChar *arg1_value,
736 const xmlChar *arg2_name, const xmlChar *arg2_value)
738 xmlNodePtr node;
739 xmlNs *rox;
741 rox = xmlSearchNsByHref(body->doc, body, ROX_NS);
743 node = xmlNewChild(body, rox, function, NULL);
745 if (arg1_name)
747 xmlNewTextChild(node, rox, arg1_name, arg1_value);
748 if (arg2_name)
749 xmlNewTextChild(node, rox, arg2_name, arg2_value);
753 static void soap_reply(xmlDocPtr reply, gboolean rpc_mode)
755 gboolean print=TRUE;
757 if(!reply)
758 return;
760 if(!rpc_mode) {
761 gchar **errs=extract_soap_errors(reply);
763 if(errs) {
764 int i;
766 print=FALSE;
768 for(i=0; errs[i]; i++)
769 fprintf(stderr, "%s\n", errs[i]);
771 g_strfreev(errs);
775 /* Write the result, if any, to stdout */
776 if(print)
777 save_xml_file(reply, "-");
778 xmlFreeDoc(reply);
781 /* This is called as a signal handler; simply ensures that
782 * child_died_callback() will get called later.
784 static void child_died(int signum)
786 child_died_flag = TRUE;
787 write(to_wakeup_pipe, "\0", 1); /* Wake up! */
790 static void child_died_callback(void)
792 int status;
793 gint child;
795 child_died_flag = FALSE;
797 /* Find out which children exited and allow them to die */
800 Callback *cb;
802 child = waitpid(-1, &status, WNOHANG);
804 if (child == 0 || child == -1)
805 return;
807 cb = g_hash_table_lookup(death_callbacks,
808 GINT_TO_POINTER(child));
809 if (cb)
811 cb->callback(cb->data);
812 g_hash_table_remove(death_callbacks,
813 GINT_TO_POINTER(child));
816 } while (1);
819 #define BUFLEN 40
820 /* When data is written to_wakeup_pipe, this gets called from the event
821 * loop some time later. Useful for getting out of signal handlers, etc.
823 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition)
825 char buf[BUFLEN];
827 read(source, buf, BUFLEN);
829 if (child_died_flag)
830 child_died_callback();
831 #ifdef USE_DNOTIFY
832 if (dnotify_wakeup_flag)
833 dnotify_wakeup();
834 #endif
837 static void xrandr_size_change(GdkScreen *screen, gpointer user_data)
839 gui_store_screen_geometry(screen);
841 panel_update_size();
842 pinboard_update_size();
845 static void add_default_panel_and_pinboard(xmlNodePtr body)
847 const char *name;
849 if (o_session_panel_or_pin.int_value != SESSION_PANEL_ONLY)
851 name=o_session_pinboard_name.value;
852 if (!name[0])
853 name="Default";
854 soap_add(body, "Pinboard","Name", name, NULL, NULL);
857 if (o_session_panel_or_pin.int_value != SESSION_PINBOARD_ONLY)
859 gboolean use_old_option = TRUE;
860 GIOChannel *fp = NULL;
861 GError *err = NULL;
862 char *line = NULL;
863 gsize term;
864 char *filename = choices_find_xdg_path_load("panels",
865 "ROX-Filer", "rox.sourceforge.net");
867 if (filename)
868 fp = g_io_channel_new_file(filename, "r", &err);
869 while (fp && g_io_channel_read_line(fp, &line, NULL, &term, &err) ==
870 G_IO_STATUS_NORMAL)
872 if (line && (line[term] = 0, line[0]))
874 soap_add(body, "Panel", "Name", line, NULL, NULL);
875 use_old_option = FALSE;
878 if (err)
880 g_critical(_("Unable to read '%s': %s"),
881 filename, err->message);
882 g_error_free(err);
884 if (fp)
885 g_io_channel_shutdown(fp, FALSE, NULL);
886 if (use_old_option)
888 name = o_session_panel_name.value;
889 if (!name[0])
890 name="Default";
891 soap_add(body, "Panel", "Name", name, NULL, NULL);
893 g_free(filename);
897 static GtkWidget *launch_button_new(const char *label, const char *uri,
898 const char *appname)
900 GtkWidget *button;
901 GClosure *closure;
902 const gchar *slash;
903 gchar *tip;
905 button = button_new_mixed(GTK_STOCK_PREFERENCES, label);
906 closure = g_cclosure_new(G_CALLBACK(launch_uri),
907 g_strdup(uri),
908 (GClosureNotify) g_free);
909 g_signal_connect_closure(button, "clicked", closure, FALSE);
910 if(appname) {
911 g_object_set_data_full(G_OBJECT(button), "appname",
912 g_strdup(appname),
913 (GDestroyNotify) g_free);
916 allow_right_click(button);
918 slash = strrchr(uri, '/');
919 if (!slash)
920 slash = uri - 1;
921 tip = g_strdup_printf(
922 _("Left-click to run %s.\n"
923 "Right-click for a list of versions."),
924 slash + 1);
926 gtk_tooltips_set_tip(tooltips, button, tip, NULL);
928 g_free(tip);
930 return button;
933 static GList *build_launch(Option *option, xmlNode *node, guchar *label)
935 GtkWidget *align;
936 char *uri;
937 char *appname;
939 g_return_val_if_fail(option == NULL, NULL);
940 g_return_val_if_fail(label != NULL, NULL);
942 uri = xmlGetProp(node, "uri");
943 appname = xmlGetProp(node, "appname");
945 g_return_val_if_fail(uri != NULL, NULL);
947 align = gtk_alignment_new(0, 0.5, 0, 0);
949 gtk_container_add(GTK_CONTAINER(align),
950 launch_button_new(_(label), uri, appname));
952 g_free(uri);
953 if(appname)
954 g_free(appname);
956 return g_list_append(NULL, align);
959 /* Call back from save box to create a rox script */
960 static gint new_script_cb(GObject *savebox,
961 const gchar *path, gpointer data)
963 FILE *fp;
965 fp = fopen(path, "w");
967 if (fp == NULL)
969 report_error(_("Error creating '%s': %s"),
970 path, g_strerror(errno));
971 return GTK_XDS_SAVE_ERROR;
974 fprintf(fp, "#!/bin/sh\n");
975 fprintf(fp, "exec %s/AppRun \"$@\"\n", app_dir);
977 fclose(fp);
978 chmod(path, 0755);
980 dir_check_this(path);
982 return GTK_XDS_SAVED;
985 /* Option button to create the rox script clicked */
986 static void make_script_clicked(GtkWidget *button, gpointer udata)
988 const gchar *filename;
989 const gchar *action;
990 GtkWidget *savebox;
991 MaskedPixmap *image;
993 /* Default to saving in current filer window */
994 if(window_with_focus)
995 filename=make_path(window_with_focus->sym_path, "rox");
996 else
997 filename="rox";
998 action = _("Start script");
999 image = type_to_icon(application_x_shellscript);
1001 /* Create a save box to save the script */
1002 savebox = gtk_savebox_new(_("Save"));
1003 gtk_savebox_set_action(GTK_SAVEBOX(savebox), GDK_ACTION_COPY);
1004 g_signal_connect(savebox, "save_to_file",
1005 G_CALLBACK(new_script_cb), NULL);
1007 gtk_window_set_title(GTK_WINDOW(savebox), _("Start script"));
1009 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), filename);
1010 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1011 g_object_unref(image);
1013 gtk_widget_show(savebox);
1016 /* Build option button to create rox script */
1017 static GList *build_make_script(Option *option, xmlNode *node, guchar *label)
1019 GtkWidget *align;
1020 GtkWidget *button;
1021 gchar *tip;
1023 g_return_val_if_fail(option == NULL, NULL);
1024 g_return_val_if_fail(label != NULL, NULL);
1026 align = gtk_alignment_new(0, 0.5, 0, 0);
1028 button = gtk_button_new_with_label(_(label));
1029 g_signal_connect(button, "clicked", G_CALLBACK(make_script_clicked),
1030 NULL);
1032 tip = _("Click to save a script to run ROX-Filer.\n"
1033 "If you are using Zero Install you should use 0alias "
1034 "instead.");
1035 gtk_tooltips_set_tip(tooltips, button, tip, NULL);
1037 gtk_container_add(GTK_CONTAINER(align), button);
1039 return g_list_append(NULL, align);