Moved substitution code into a separate function
[rox-filer/th.git] / ROX-Filer / src / main.c
blobe0f71b1e3a70211894b67e489545858c42921792
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 <sys/wait.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <pwd.h>
37 #include <grp.h>
38 #include <libxml/parser.h>
40 #ifdef HAVE_GETOPT_LONG
41 # include <getopt.h>
42 #endif
44 #include <gtk/gtk.h>
45 #include <gdk/gdkx.h> /* For rox_x_error */
47 #include "global.h"
49 #include "main.h"
50 #include "log.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"
74 #include "xtypes.h"
75 #include "bulk_rename.h"
77 int number_of_windows = 0; /* Quit when this reaches 0 again... */
78 int to_wakeup_pipe = -1; /* Write here to get noticed */
80 /* Information about the ROX-Filer process */
81 uid_t euid;
82 gid_t egid;
83 int ngroups; /* Number of supplemental groups */
84 gid_t *supplemental_groups = NULL;
86 /* Message to display at the top of each filer window */
87 const gchar *show_user_message = NULL;
89 int home_dir_len;
90 const char *home_dir, *app_dir;
92 GtkTooltips *tooltips = NULL;
94 #define COPYING \
95 N_("Copyright (C) 2005 Thomas Leonard.\n" \
96 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
97 "to the extent permitted by law.\n" \
98 "You may redistribute copies of ROX-Filer\n" \
99 "under the terms of the GNU General Public License.\n" \
100 "For more information about these matters, " \
101 "see the file named COPYING.\n")
103 #ifdef HAVE_GETOPT_LONG
104 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
105 # define SHORT_ONLY_WARNING ""
106 #else
107 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
108 # define SHORT_ONLY_WARNING \
109 _("NOTE: Your system does not support long options - \n" \
110 "you must use the short versions instead.\n\n")
111 #endif
113 #define BUGS_TO "<rox-devel@lists.sourceforge.net>"
115 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
116 "Open each directory or file listed, or the current working\n" \
117 "directory if no arguments are given.\n\n" \
118 " -b, --border=PANEL open PANEL as a border panel\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 " -S, --rox-session use default panel and pinboard options, and -n\n"\
132 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
133 " -u, --user show user name in each window \n" \
134 " -U, --url=URL open file or directory in URI form\n" \
135 " -v, --version display the version information and exit\n" \
136 " -x, --examine=FILE FILE has changed - re-examine it\n" \
137 "\nReport bugs to %s.\n" \
138 "Home page (including updated versions): http://rox.sourceforge.net/\n")
140 #define SHORT_OPS "c:d:t:b:l:r:B:op:s:hvnux:m:D:RSU:"
142 #ifdef HAVE_GETOPT_LONG
143 static struct option long_opts[] =
145 {"dir", 1, NULL, 'd'},
146 {"top", 1, NULL, 't'},
147 {"bottom", 1, NULL, 'B'},
148 {"border", 1, NULL, 'b'},
149 {"left", 1, NULL, 'l'},
150 {"override", 0, NULL, 'o'},
151 {"pinboard", 1, NULL, 'p'},
152 {"right", 1, NULL, 'r'},
153 {"help", 0, NULL, 'h'},
154 {"version", 0, NULL, 'v'},
155 {"user", 0, NULL, 'u'},
156 {"new", 0, NULL, 'n'},
157 {"RPC", 0, NULL, 'R'},
158 {"show", 1, NULL, 's'},
159 {"rox-session", 0, NULL, 'S'},
160 {"examine", 1, NULL, 'x'},
161 {"close", 1, NULL, 'D'},
162 {"mime-type", 1, NULL, 'm'},
163 {"client-id", 1, NULL, 'c'},
164 {"url", 1, NULL, 'u'},
165 {NULL, 0, NULL, 0},
167 #endif
169 /* Take control of panels away from WM? */
170 Option o_override_redirect;
172 /* Options used when we are called by ROX-Session */
173 enum {
174 SESSION_PANEL_ONLY,
175 SESSION_PINBOARD_ONLY,
176 SESSION_BOTH,
178 Option o_session_panel_or_pin;
179 Option o_session_panel_name; /* Now a comma-separated list */
180 Option o_session_pinboard_name;
182 /* Always start a new filer, even if one seems to be already running */
183 gboolean new_copy = FALSE;
185 /* Maps child PIDs to Callback pointers */
186 static GHashTable *death_callbacks = NULL;
187 static gboolean child_died_flag = FALSE;
189 Option o_dnd_no_hostnames;
191 /* Static prototypes */
192 static void show_features(void);
193 static void soap_add(xmlNodePtr body,
194 xmlChar *function,
195 const xmlChar *arg1_name, const xmlChar *arg1_value,
196 const xmlChar *arg2_name, const xmlChar *arg2_value);
197 static void soap_reply(xmlDocPtr reply, gboolean rpc_mode);
198 static void child_died(int signum);
199 static void child_died_callback(void);
200 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition);
201 static void xrandr_size_change(GdkScreen *screen, gpointer user_data);
202 static void add_default_panel_and_pinboard(xmlNodePtr body);
203 static GList *build_launch(Option *option, xmlNode *node, guchar *label);
205 /****************************************************************
206 * EXTERNAL INTERFACE *
207 ****************************************************************/
209 /* The value that goes with an option */
210 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
212 static int rox_x_error(Display *display, XErrorEvent *error)
214 gchar buf[64];
216 XGetErrorText(display, error->error_code, buf, 63);
218 g_warning ("The program '%s' received an X Window System error.\n"
219 "This probably reflects a bug in the program.\n"
220 "The error was '%s'.\n"
221 " (Details: serial %ld error_code %d request_code %d minor_code %d)\n"
222 " (Note to programmers: normally, X errors are reported asynchronously;\n"
223 " that is, you will receive the error a while after causing it.\n"
224 " To debug your program, run it with the --sync command line\n"
225 " option to change this behavior. You can then get a meaningful\n"
226 " backtrace from your debugger.)",
227 g_get_prgname (),
228 buf,
229 error->serial,
230 error->error_code,
231 error->request_code,
232 error->minor_code);
234 /* Try to cope with BadWindow errors */
235 if (error->error_code == BadWindow || error->error_code == BadDrawable)
237 g_warning(_("We got a BadWindow error from the X server. "
238 "This might be due to this GTK bug (during drag-and-drop?):\n"
239 "http://bugzilla.gnome.org/show_bug.cgi?id=152151\n"
240 "Trying to continue..."));
241 return 0;
244 abort();
247 /* Parses the command-line to work out what the user wants to do.
248 * Tries to send the request to an already-running copy of the filer.
249 * If that fails, it initialises all the other modules and executes the
250 * request itself.
252 int main(int argc, char **argv)
254 int wakeup_pipe[2];
255 int i;
256 struct sigaction act;
257 guchar *tmp, *dir;
258 gchar *client_id = NULL;
259 gboolean show_user = FALSE;
260 gboolean rpc_mode = FALSE;
261 xmlDocPtr rpc, soap_rpc = NULL, reply;
262 xmlNodePtr body;
263 int fd, ofd0=-1;
265 /* Relocate stdin. We do need it (-R), but it can cause problems if
266 * a child process wants a password, etc...
267 * Do this BEFORE opening anything (e.g., the X connection), in
268 * case fd 0 isn't open at this point.
270 fd = open("/dev/null", O_RDONLY);
271 if (fd > 0)
273 ofd0=dup(0);
274 close(0);
275 dup2(fd, 0);
276 close(fd);
279 home_dir = g_get_home_dir();
280 home_dir_len = strlen(home_dir);
281 app_dir = g_strdup(getenv("APP_DIR"));
283 /* Get internationalisation up and running. This requires the
284 * choices system, to discover the user's preferred language.
286 choices_init();
287 options_init();
288 i18n_init();
289 xattr_init();
291 if (!app_dir)
293 g_warning("APP_DIR environment variable was unset!\n"
294 "Use the AppRun script to invoke ROX-Filer...\n");
295 app_dir = g_get_current_dir();
297 #ifdef HAVE_UNSETENV
298 else
300 /* Don't pass it on to our child processes... */
301 unsetenv("APP_DIR");
303 #endif
305 /* Sometimes we want to take special action when a child
306 * process exits. This hash table is used to convert the
307 * child's PID to the callback function.
309 death_callbacks = g_hash_table_new(NULL, NULL);
311 /* Find out some information about ourself */
312 euid = geteuid();
313 egid = getegid();
314 ngroups = getgroups(0, NULL);
315 if (ngroups < 0)
316 ngroups = 0;
317 else if (ngroups > 0)
319 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
320 getgroups(ngroups, supplemental_groups);
323 if (argc == 2 && strcmp(argv[1], "-v") == 0)
325 /* This is used by install.sh to test if the filer
326 * compiled OK. Do this test before gtk_init so that
327 * we don't need an X server to install.
329 g_print("ROX-Filer %s\n", VERSION);
330 g_print(_(COPYING));
331 show_features();
332 return EXIT_SUCCESS;
335 option_add_int(&o_override_redirect, "override_redirect", FALSE);
337 option_add_int(&o_session_panel_or_pin, "session_panel_or_pin",
338 SESSION_BOTH);
339 option_add_string(&o_session_panel_name, "session_panel_name",
340 "Default");
341 option_add_string(&o_session_pinboard_name, "session_pinboard_name",
342 "Default");
343 option_register_widget("launch", build_launch);
345 #ifdef UNIT_TESTS
346 bulk_rename_tests();
347 #endif
349 /* The idea here is to convert the command-line arguments
350 * into a SOAP RPC.
351 * We attempt to invoke the call on an already-running copy of
352 * the filer if possible, or execute it ourselves if not.
354 rpc = soap_new(&body);
356 /* Note: must do this before checking our options,
357 * otherwise we report an error for Gtk's options.
359 gtk_init(&argc, &argv);
360 /* Set a default style for the collection widget */
361 gtk_rc_parse_string("style \"rox-default-collection-style\" {\n"
362 " bg[NORMAL] = \"#f3f3f3\"\n"
363 " fg[NORMAL] = \"#000000\"\n"
364 " bg[INSENSITIVE] = \"#bfbfbf\"\n"
365 " fg[INSENSITIVE] = \"#000000\"\n"
366 "}\n"
367 "style \"rox-default-pinboard-style\" {\n"
368 " bg[NORMAL] = \"#666666\"\n"
369 "}\n"
370 "widget \"rox-pinboard\" style : gtk "
371 "\"rox-default-pinboard-style\"\n"
373 "class \"Collection\" style : gtk "
374 "\"rox-default-collection-style\"\n");
376 g_signal_connect(gdk_screen_get_default(), "size-changed",
377 G_CALLBACK(xrandr_size_change), NULL);
379 /* Process each option in turn */
380 while (1)
382 int c;
383 #ifdef HAVE_GETOPT_LONG
384 int long_index;
385 c = getopt_long(argc, argv, SHORT_OPS,
386 long_opts, &long_index);
387 #else
388 c = getopt(argc, argv, SHORT_OPS);
389 #endif
391 if (c == EOF)
392 break; /* No more options */
394 switch (c)
396 case 'n':
397 new_copy = TRUE;
398 break;
399 case 'o':
400 info_message(_("The -o argument is no longer "
401 "used. You can turn on override "
402 "redirect from the Options box "
403 "instead."));
404 break;
405 case 'v':
406 g_print("ROX-Filer %s\n", VERSION);
407 g_print("%s", _(COPYING));
408 show_features();
409 return EXIT_SUCCESS;
410 case 'h':
411 g_print(_(HELP), BUGS_TO);
412 g_print("%s", _(SHORT_ONLY_WARNING));
413 return EXIT_SUCCESS;
414 case 'D':
415 case 'd':
416 case 'x':
417 /* Argument is a path */
418 if (c == 'd' && VALUE[0] == '/')
419 tmp = g_strdup(VALUE);
420 else
421 tmp = pathdup(VALUE);
422 soap_add(body,
423 c == 'D' ? "CloseDir" :
424 c == 'd' ? "OpenDir" :
425 c == 'x' ? "Examine" : "Unknown",
426 "Filename", tmp,
427 NULL, NULL);
428 g_free(tmp);
429 break;
430 case 's':
431 tmp = g_path_get_dirname(VALUE);
433 if (tmp[0] == '/')
434 dir = NULL;
435 else
436 dir = pathdup(tmp);
438 soap_add(body, "Show",
439 "Directory", dir ? dir : tmp,
440 "Leafname", g_basename(VALUE));
441 g_free(tmp);
442 g_free(dir);
443 break;
444 case 'l':
445 case 'r':
446 case 't':
447 case 'B':
448 /* Argument is a leaf (or starts with /) */
449 soap_add(body, "Panel", "Name", VALUE,
450 "Side", c == 'l' ? "Left" :
451 c == 'r' ? "Right" :
452 c == 't' ? "Top" :
453 c == 'B' ? "Bottom" :
454 "Unkown");
455 break;
456 case 'b':
457 /* Argument is a leaf (or starts with /) */
458 if (*VALUE)
459 soap_add(body, "Panel", "Name", VALUE,
460 NULL, NULL);
461 else
462 soap_add(body, "Panel",
463 "Side", "Bottom",
464 NULL, NULL);
465 break;
466 case 'p':
467 soap_add(body, "Pinboard",
468 "Name", VALUE, NULL, NULL);
469 break;
470 case 'u':
471 show_user = TRUE;
472 break;
473 case 'm':
475 MIME_type *type;
476 type_init();
477 diritem_init();
478 pixmaps_init();
479 type = type_get_type(VALUE);
480 printf("%s/%s\n", type->media_type,
481 type->subtype);
482 return EXIT_SUCCESS;
484 case 'c':
485 client_id = g_strdup(VALUE);
486 break;
487 case 'R':
488 /* Reconnect stdin */
489 if(ofd0>-1) {
490 close(0);
491 dup2(ofd0, 0);
493 soap_rpc = xmlParseFile("-");
494 if (!soap_rpc)
495 g_error("Invalid XML in RPC");
496 /* Disconnect stdin again */
497 fd = open("/dev/null", O_RDONLY);
498 if (fd > 0)
500 close(0);
501 dup2(fd, 0);
502 close(fd);
504 /* Want to print return uninterpreted */
505 rpc_mode=TRUE;
507 break;
509 case 'S':
510 new_copy = TRUE;
511 add_default_panel_and_pinboard(body);
512 session_auto_respawn = TRUE;
513 break;
515 case 'U':
516 soap_add(body, "RunURI",
517 "URI", VALUE, NULL, NULL);
518 break;
520 default:
521 printf(_(USAGE));
522 return EXIT_FAILURE;
526 tooltips = gtk_tooltips_new();
528 if (euid == 0 || show_user)
529 show_user_message = g_strdup_printf(_("Running as user '%s'"),
530 user_name(euid));
532 /* Add each remaining (non-option) argument to the list of files
533 * to run.
535 i = optind;
536 while (i < argc)
538 tmp = pathdup(argv[i++]);
540 soap_add(body, "Run", "Filename", tmp, NULL, NULL);
542 g_free(tmp);
545 if (soap_rpc)
547 if (body->xmlChildrenNode)
548 g_error("Can't use -R with other options - sorry!");
549 xmlFreeDoc(rpc);
550 body = NULL;
551 rpc = soap_rpc;
553 else if (!body->xmlChildrenNode)
555 /* The user didn't request any action. Open the current
556 * directory.
558 guchar *dir;
560 dir = g_get_current_dir();
561 soap_add(body, "OpenDir", "Filename", dir, NULL, NULL);
562 g_free(dir);
565 option_add_int(&o_dnd_no_hostnames, "dnd_no_hostnames", 1);
567 /* Try to send the request to an already-running copy of the filer */
568 gui_support_init();
569 if (remote_init(rpc, new_copy))
570 return EXIT_SUCCESS; /* It worked - exit */
572 /* Put ourselves into the background (so 'rox' always works the
573 * same, whether we're already running or not).
574 * Not for -n, though (helps when debugging).
576 if (!new_copy)
578 pid_t child;
580 child = fork();
581 if (child > 0)
582 _exit(0); /* Parent exits */
583 /* Otherwise we're the child (or an error occurred - ignore
584 * it!).
588 /* Initialize the rest of the filer... */
590 pixmaps_init();
592 log_init();
593 dnd_init();
594 bind_init();
595 dir_init();
596 diritem_init();
597 menu_init();
598 minibuffer_init();
599 filer_init();
600 toolbar_init();
601 display_init();
602 mount_init();
603 type_init();
604 action_init();
606 pinboard_init();
607 panel_init();
609 /* Let everyone update */
610 options_notify();
612 /* When we get a signal, we can't do much right then. Instead,
613 * we send a char down this pipe, which causes the main loop to
614 * deal with the event next time we're idle.
616 pipe(wakeup_pipe);
617 close_on_exec(wakeup_pipe[0], TRUE);
618 close_on_exec(wakeup_pipe[1], TRUE);
619 gdk_input_add_full(wakeup_pipe[0], GDK_INPUT_READ, wake_up_cb,
620 NULL, NULL);
621 to_wakeup_pipe = wakeup_pipe[1];
623 /* If the pipe is full then we're going to get woken up anyway... */
624 set_blocking(to_wakeup_pipe, FALSE);
626 /* Let child processes die */
627 act.sa_handler = child_died;
628 sigemptyset(&act.sa_mask);
629 act.sa_flags = SA_NOCLDSTOP;
630 sigaction(SIGCHLD, &act, NULL);
632 /* Ignore SIGPIPE - check for EPIPE errors instead */
633 act.sa_handler = SIG_IGN;
634 sigemptyset(&act.sa_mask);
635 act.sa_flags = 0;
636 sigaction(SIGPIPE, &act, NULL);
638 /* Set up session managament if available */
639 session_init(client_id);
640 g_free(client_id);
642 /* See if we need to migrate the Choices directories*/
643 choices_migrate();
645 /* Finally, execute the request */
646 reply = run_soap(rpc);
647 xmlFreeDoc(rpc);
648 soap_reply(reply, rpc_mode);
650 /* Try to find out why we crash with GTK 2.4 */
651 XSetErrorHandler(rox_x_error);
653 /* Enter the main loop, processing events until all our windows
654 * are closed.
656 if (number_of_windows > 0)
657 gtk_main();
659 return EXIT_SUCCESS;
662 /* Register a function to be called when process number 'child' dies. */
663 void on_child_death(gint child, CallbackFn callback, gpointer data)
665 Callback *cb;
667 g_return_if_fail(callback != NULL);
669 cb = g_new(Callback, 1);
671 cb->callback = callback;
672 cb->data = data;
674 g_hash_table_insert(death_callbacks, GINT_TO_POINTER(child), cb);
677 void one_less_window(void)
679 if (--number_of_windows < 1)
680 gtk_main_quit();
683 /****************************************************************
684 * INTERNAL FUNCTIONS *
685 ****************************************************************/
687 static void show_features(void)
689 g_print("\n");
690 g_print(_("Compiled with GTK version %s\n"), GTK_VERSION);
691 g_print(_("Running with GTK version %d.%d.%d\n"),
692 gtk_major_version,
693 gtk_minor_version,
694 gtk_micro_version);
695 g_print("\n-- %s --\n\n", _("features set at compile time"));
696 g_print("%s... %s\n", _("Large File Support"),
697 #ifdef LARGE_FILE_SUPPORT
698 _("Yes")
699 #else
700 _("No")
701 #endif
703 g_print("%s... %s\n", _("Inotify support"),
704 #ifdef USE_INOTIFY
705 _("Yes")
706 #else
707 _("No")
708 #endif
710 g_print("%s... %s\n", _("Dnotify support"),
711 #ifdef USE_DNOTIFY
712 _("Yes")
713 #else
714 _("No")
715 #endif
717 g_print("%s... %s\n", _("Binary compatibility"),
718 #if defined(HAVE_APSYMBOLS_H) || defined(HAVE_APBUILD_APSYMBOLS_H)
719 _("Yes (can run with older glibc versions)")
720 #else
721 _("No (apsymbols.h not found)")
722 #endif
725 g_print("%s... %s\n", _("Extended attribute support"),
726 xattr_supported(NULL)? _("Yes"): _("No"));
729 static void soap_add(xmlNodePtr body,
730 xmlChar *function,
731 const xmlChar *arg1_name, const xmlChar *arg1_value,
732 const xmlChar *arg2_name, const xmlChar *arg2_value)
734 xmlNodePtr node;
735 xmlNs *rox;
737 rox = xmlSearchNsByHref(body->doc, body, ROX_NS);
739 node = xmlNewChild(body, rox, function, NULL);
741 if (arg1_name)
743 xmlNewTextChild(node, rox, arg1_name, arg1_value);
744 if (arg2_name)
745 xmlNewTextChild(node, rox, arg2_name, arg2_value);
749 static void soap_reply(xmlDocPtr reply, gboolean rpc_mode)
751 gboolean print=TRUE;
753 if(!reply)
754 return;
756 if(!rpc_mode) {
757 gchar **errs=extract_soap_errors(reply);
759 if(errs) {
760 int i;
762 print=FALSE;
764 for(i=0; errs[i]; i++)
765 fprintf(stderr, "%s\n", errs[i]);
767 g_strfreev(errs);
771 /* Write the result, if any, to stdout */
772 if(print)
773 save_xml_file(reply, "-");
774 xmlFreeDoc(reply);
777 /* This is called as a signal handler; simply ensures that
778 * child_died_callback() will get called later.
780 static void child_died(int signum)
782 child_died_flag = TRUE;
783 write(to_wakeup_pipe, "\0", 1); /* Wake up! */
786 static void child_died_callback(void)
788 int status;
789 gint child;
791 child_died_flag = FALSE;
793 /* Find out which children exited and allow them to die */
796 Callback *cb;
798 child = waitpid(-1, &status, WNOHANG);
800 if (child == 0 || child == -1)
801 return;
803 cb = g_hash_table_lookup(death_callbacks,
804 GINT_TO_POINTER(child));
805 if (cb)
807 cb->callback(cb->data);
808 g_hash_table_remove(death_callbacks,
809 GINT_TO_POINTER(child));
812 } while (1);
815 #define BUFLEN 40
816 /* When data is written to_wakeup_pipe, this gets called from the event
817 * loop some time later. Useful for getting out of signal handlers, etc.
819 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition)
821 char buf[BUFLEN];
823 read(source, buf, BUFLEN);
825 if (child_died_flag)
826 child_died_callback();
827 #ifdef USE_DNOTIFY
828 if (dnotify_wakeup_flag)
829 dnotify_wakeup();
830 #endif
833 static void xrandr_size_change(GdkScreen *screen, gpointer user_data)
835 gui_store_screen_geometry(screen);
837 panel_update_size();
838 pinboard_update_size();
841 static void add_default_panel_and_pinboard(xmlNodePtr body)
843 const char *name;
845 if (o_session_panel_or_pin.int_value != SESSION_PANEL_ONLY)
847 name=o_session_pinboard_name.value;
848 if (!name[0])
849 name="Default";
850 soap_add(body, "Pinboard","Name", name, NULL, NULL);
853 if (o_session_panel_or_pin.int_value != SESSION_PINBOARD_ONLY)
855 gboolean use_old_option = TRUE;
856 GIOChannel *fp = NULL;
857 GError *err = NULL;
858 char *line = NULL;
859 gsize term;
860 char *filename = choices_find_xdg_path_load("panels",
861 "ROX-Filer", "rox.sourceforge.net");
863 if (filename)
864 fp = g_io_channel_new_file(filename, "r", &err);
865 while (fp && g_io_channel_read_line(fp, &line, NULL, &term, &err) ==
866 G_IO_STATUS_NORMAL)
868 if (line && (line[term] = 0, line[0]))
870 soap_add(body, "Panel", "Name", line, NULL, NULL);
871 use_old_option = FALSE;
874 if (err)
876 g_critical(_("Unable to read '%s': %s"),
877 filename, err->message);
878 g_error_free(err);
880 if (fp)
881 g_io_channel_shutdown(fp, FALSE, NULL);
882 if (use_old_option)
884 name = o_session_panel_name.value;
885 if (!name[0])
886 name="Default";
887 soap_add(body, "Panel", "Name", name, NULL, NULL);
889 g_free(filename);
893 static GtkWidget *launch_button_new(const char *label, const char *uri,
894 const char *appname)
896 GtkWidget *button;
897 GClosure *closure;
898 const gchar *slash;
899 gchar *tip;
901 button = button_new_mixed(GTK_STOCK_PREFERENCES, label);
902 closure = g_cclosure_new(G_CALLBACK(launch_uri),
903 g_strdup(uri),
904 (GClosureNotify) g_free);
905 g_signal_connect_closure(button, "clicked", closure, FALSE);
906 if(appname) {
907 g_object_set_data_full(G_OBJECT(button), "appname",
908 g_strdup(appname),
909 (GDestroyNotify) g_free);
912 allow_right_click(button);
914 slash = strrchr(uri, '/');
915 if (!slash)
916 slash = uri - 1;
917 tip = g_strdup_printf(
918 _("Left-click to run %s.\n"
919 "Right-click for a list of versions."),
920 slash + 1);
922 gtk_tooltips_set_tip(tooltips, button, tip, NULL);
924 g_free(tip);
926 return button;
929 static GList *build_launch(Option *option, xmlNode *node, guchar *label)
931 GtkWidget *align;
932 char *uri;
933 char *appname;
935 g_return_val_if_fail(option == NULL, NULL);
936 g_return_val_if_fail(label != NULL, NULL);
938 uri = xmlGetProp(node, "uri");
939 appname = xmlGetProp(node, "appname");
941 g_return_val_if_fail(uri != NULL, NULL);
943 align = gtk_alignment_new(0, 0.5, 0, 0);
945 gtk_container_add(GTK_CONTAINER(align),
946 launch_button_new(_(label), uri, appname));
948 g_free(uri);
949 if(appname)
950 g_free(appname);
952 return g_list_append(NULL, align);