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)
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
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
23 * New to the code and feeling lost? Read global.h now.
30 #include <sys/types.h>
38 #include <libxml/parser.h>
40 #ifdef HAVE_GETOPT_LONG
45 #include <gdk/gdkx.h> /* For rox_x_error */
51 #include "gui_support.h"
72 #include "minibuffer.h"
75 int number_of_windows
= 0; /* Quit when this reaches 0 again... */
76 int to_wakeup_pipe
= -1; /* Write here to get noticed */
78 /* Information about the ROX-Filer process */
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
;
88 const char *home_dir
, *app_dir
;
90 GtkTooltips
*tooltips
= NULL
;
93 N_("Copyright (C) 2005 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 ""
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")
111 #define BUGS_TO "<rox-devel@lists.sourceforge.net>"
113 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
114 "Open each directory or file listed, or the current working\n" \
115 "directory if no arguments are given.\n\n" \
116 " -b, --border=PANEL open PANEL as a border panel\n" \
117 " -B, --bottom=PANEL open PAN as a bottom-edge panel\n" \
118 " -c, --client-id=ID used for session management\n" \
119 " -d, --dir=DIR open DIR as directory (not application)\n" \
120 " -D, --close=DIR close DIR and its subdirectories\n" \
121 " -h, --help display this help and exit\n" \
122 " -l, --left=PANEL open PAN as a left-edge panel\n" \
123 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
124 " -n, --new start new copy; for debugging the filer\n" \
125 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
126 " -r, --right=PANEL open PAN as a right-edge panel\n" \
127 " -R, --RPC invoke method call read from stdin\n" \
128 " -s, --show=FILE open a directory showing FILE\n" \
129 " -S, --rox-session use default panel and pinboard options, and -n\n"\
130 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
131 " -u, --user show user name in each window \n" \
132 " -U, --url=URL open file or directory in URI form\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 %s.\n" \
136 "Home page (including updated versions): http://rox.sourceforge.net/\n")
138 #define SHORT_OPS "c:d:t:b:l:r:B:op:s:hvnux:m:D:RSU:"
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 {"border", 1, NULL
, 'b'},
147 {"left", 1, NULL
, 'l'},
148 {"override", 0, NULL
, 'o'},
149 {"pinboard", 1, NULL
, 'p'},
150 {"right", 1, NULL
, 'r'},
151 {"help", 0, NULL
, 'h'},
152 {"version", 0, NULL
, 'v'},
153 {"user", 0, NULL
, 'u'},
154 {"new", 0, NULL
, 'n'},
155 {"RPC", 0, NULL
, 'R'},
156 {"show", 1, NULL
, 's'},
157 {"rox-session", 0, NULL
, 'S'},
158 {"examine", 1, NULL
, 'x'},
159 {"close", 1, NULL
, 'D'},
160 {"mime-type", 1, NULL
, 'm'},
161 {"client-id", 1, NULL
, 'c'},
162 {"url", 1, NULL
, 'u'},
167 /* Take control of panels away from WM? */
168 Option o_override_redirect
;
170 /* Options used when we are called by ROX-Session */
173 SESSION_PINBOARD_ONLY
,
176 Option o_session_panel_or_pin
;
177 Option o_session_panel_name
;
178 Option o_session_pinboard_name
;
180 /* Always start a new filer, even if one seems to be already running */
181 gboolean new_copy
= FALSE
;
183 /* Maps child PIDs to Callback pointers */
184 static GHashTable
*death_callbacks
= NULL
;
185 static gboolean child_died_flag
= FALSE
;
187 Option o_dnd_no_hostnames
;
189 /* Static prototypes */
190 static void show_features(void);
191 static void soap_add(xmlNodePtr body
,
193 const xmlChar
*arg1_name
, const xmlChar
*arg1_value
,
194 const xmlChar
*arg2_name
, const xmlChar
*arg2_value
);
195 static void soap_reply(xmlDocPtr reply
, gboolean rpc_mode
);
196 static void child_died(int signum
);
197 static void child_died_callback(void);
198 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
);
199 static void xrandr_size_change(GdkScreen
*screen
, gpointer user_data
);
200 static void add_default_panel_and_pinboard(xmlNodePtr body
);
201 static GList
*build_launch(Option
*option
, xmlNode
*node
, guchar
*label
);
203 /****************************************************************
204 * EXTERNAL INTERFACE *
205 ****************************************************************/
207 /* The value that goes with an option */
208 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
210 static int rox_x_error(Display
*display
, XErrorEvent
*error
)
214 XGetErrorText(display
, error
->error_code
, buf
, 63);
216 g_warning ("The program '%s' received an X Window System error.\n"
217 "This probably reflects a bug in the program.\n"
218 "The error was '%s'.\n"
219 " (Details: serial %ld error_code %d request_code %d minor_code %d)\n"
220 " (Note to programmers: normally, X errors are reported asynchronously;\n"
221 " that is, you will receive the error a while after causing it.\n"
222 " To debug your program, run it with the --sync command line\n"
223 " option to change this behavior. You can then get a meaningful\n"
224 " backtrace from your debugger.)",
232 /* Try to cope with BadWindow errors */
233 if (error
->error_code
== BadWindow
|| error
->error_code
== BadDrawable
)
235 g_warning(_("We got a BadWindow error from the X server. "
236 "This might be due to this GTK bug (during drag-and-drop?):\n"
237 "http://bugzilla.gnome.org/show_bug.cgi?id=152151\n"
238 "Trying to continue..."));
245 /* Parses the command-line to work out what the user wants to do.
246 * Tries to send the request to an already-running copy of the filer.
247 * If that fails, it initialises all the other modules and executes the
250 int main(int argc
, char **argv
)
254 struct sigaction act
;
256 gchar
*client_id
= NULL
;
257 gboolean show_user
= FALSE
;
258 gboolean rpc_mode
= FALSE
;
259 xmlDocPtr rpc
, soap_rpc
= NULL
, reply
;
263 /* Relocate stdin. We do need it (-R), but it can cause problems if
264 * a child process wants a password, etc...
265 * Do this BEFORE opening anything (e.g., the X connection), in
266 * case fd 0 isn't open at this point.
268 fd
= open("/dev/null", O_RDONLY
);
277 home_dir
= g_get_home_dir();
278 home_dir_len
= strlen(home_dir
);
279 app_dir
= g_strdup(getenv("APP_DIR"));
281 /* Get internationalisation up and running. This requires the
282 * choices system, to discover the user's preferred language.
291 g_warning("APP_DIR environment variable was unset!\n"
292 "Use the AppRun script to invoke ROX-Filer...\n");
293 app_dir
= g_get_current_dir();
298 /* Don't pass it on to our child processes... */
303 /* Sometimes we want to take special action when a child
304 * process exits. This hash table is used to convert the
305 * child's PID to the callback function.
307 death_callbacks
= g_hash_table_new(NULL
, NULL
);
309 /* Find out some information about ourself */
312 ngroups
= getgroups(0, NULL
);
315 else if (ngroups
> 0)
317 supplemental_groups
= g_malloc(sizeof(gid_t
) * ngroups
);
318 getgroups(ngroups
, supplemental_groups
);
321 if (argc
== 2 && strcmp(argv
[1], "-v") == 0)
323 /* This is used by install.sh to test if the filer
324 * compiled OK. Do this test before gtk_init so that
325 * we don't need an X server to install.
327 g_print("ROX-Filer %s\n", VERSION
);
333 option_add_int(&o_override_redirect
, "override_redirect", FALSE
);
335 option_add_int(&o_session_panel_or_pin
, "session_panel_or_pin",
337 option_add_string(&o_session_panel_name
, "session_panel_name",
339 option_add_string(&o_session_pinboard_name
, "session_pinboard_name",
341 option_register_widget("launch", build_launch
);
343 /* The idea here is to convert the command-line arguments
345 * We attempt to invoke the call on an already-running copy of
346 * the filer if possible, or execute it ourselves if not.
348 rpc
= soap_new(&body
);
350 /* Note: must do this before checking our options,
351 * otherwise we report an error for Gtk's options.
353 gtk_init(&argc
, &argv
);
354 /* Set a default style for the collection widget */
355 gtk_rc_parse_string("style \"rox-default-collection-style\" {\n"
356 " bg[NORMAL] = \"#f3f3f3\"\n"
357 " fg[NORMAL] = \"#000000\"\n"
358 " bg[INSENSITIVE] = \"#bfbfbf\"\n"
359 " fg[INSENSITIVE] = \"#000000\"\n"
361 "style \"rox-default-pinboard-style\" {\n"
362 " bg[NORMAL] = \"#666666\"\n"
364 "widget \"rox-pinboard\" style : gtk "
365 "\"rox-default-pinboard-style\"\n"
367 "class \"Collection\" style : gtk "
368 "\"rox-default-collection-style\"\n");
370 g_signal_connect(gdk_screen_get_default(), "size-changed",
371 G_CALLBACK(xrandr_size_change
), NULL
);
373 /* Process each option in turn */
377 #ifdef HAVE_GETOPT_LONG
379 c
= getopt_long(argc
, argv
, SHORT_OPS
,
380 long_opts
, &long_index
);
382 c
= getopt(argc
, argv
, SHORT_OPS
);
386 break; /* No more options */
394 info_message(_("The -o argument is no longer "
395 "used. You can turn on override "
396 "redirect from the Options box "
400 g_print("ROX-Filer %s\n", VERSION
);
401 g_print("%s", _(COPYING
));
405 g_print(_(HELP
), BUGS_TO
);
406 g_print("%s", _(SHORT_ONLY_WARNING
));
411 /* Argument is a path */
412 if (c
== 'd' && VALUE
[0] == '/')
413 tmp
= g_strdup(VALUE
);
415 tmp
= pathdup(VALUE
);
417 c
== 'D' ? "CloseDir" :
418 c
== 'd' ? "OpenDir" :
419 c
== 'x' ? "Examine" : "Unknown",
425 tmp
= g_path_get_dirname(VALUE
);
432 soap_add(body
, "Show",
433 "Directory", dir
? dir
: tmp
,
434 "Leafname", g_basename(VALUE
));
442 /* Argument is a leaf (or starts with /) */
443 soap_add(body
, "Panel", "Name", VALUE
,
444 "Side", c
== 'l' ? "Left" :
447 c
== 'B' ? "Bottom" :
451 /* Argument is a leaf (or starts with /) */
453 soap_add(body
, "Panel", "Name", VALUE
,
456 soap_add(body
, "Panel",
461 soap_add(body
, "Pinboard",
462 "Name", VALUE
, NULL
, NULL
);
473 type
= type_get_type(VALUE
);
474 printf("%s/%s\n", type
->media_type
,
479 client_id
= g_strdup(VALUE
);
482 /* Reconnect stdin */
487 soap_rpc
= xmlParseFile("-");
489 g_error("Invalid XML in RPC");
490 /* Disconnect stdin again */
491 fd
= open("/dev/null", O_RDONLY
);
498 /* Want to print return uninterpreted */
505 add_default_panel_and_pinboard(body
);
506 session_auto_respawn
= TRUE
;
510 soap_add(body
, "RunURI",
511 "URI", VALUE
, NULL
, NULL
);
520 tooltips
= gtk_tooltips_new();
522 if (euid
== 0 || show_user
)
523 show_user_message
= g_strdup_printf(_("Running as user '%s'"),
526 /* Add each remaining (non-option) argument to the list of files
532 tmp
= pathdup(argv
[i
++]);
534 soap_add(body
, "Run", "Filename", tmp
, NULL
, NULL
);
541 if (body
->xmlChildrenNode
)
542 g_error("Can't use -R with other options - sorry!");
547 else if (!body
->xmlChildrenNode
)
549 /* The user didn't request any action. Open the current
554 dir
= g_get_current_dir();
555 soap_add(body
, "OpenDir", "Filename", dir
, NULL
, NULL
);
559 option_add_int(&o_dnd_no_hostnames
, "dnd_no_hostnames", 1);
561 /* Try to send the request to an already-running copy of the filer */
563 if (remote_init(rpc
, new_copy
))
564 return EXIT_SUCCESS
; /* It worked - exit */
566 /* Put ourselves into the background (so 'rox' always works the
567 * same, whether we're already running or not).
568 * Not for -n, though (helps when debugging).
576 _exit(0); /* Parent exits */
577 /* Otherwise we're the child (or an error occurred - ignore
582 /* Initialize the rest of the filer... */
602 /* Let everyone update */
605 /* When we get a signal, we can't do much right then. Instead,
606 * we send a char down this pipe, which causes the main loop to
607 * deal with the event next time we're idle.
610 close_on_exec(wakeup_pipe
[0], TRUE
);
611 close_on_exec(wakeup_pipe
[1], TRUE
);
612 gdk_input_add_full(wakeup_pipe
[0], GDK_INPUT_READ
, wake_up_cb
,
614 to_wakeup_pipe
= wakeup_pipe
[1];
616 /* If the pipe is full then we're going to get woken up anyway... */
617 set_blocking(to_wakeup_pipe
, FALSE
);
619 /* Let child processes die */
620 act
.sa_handler
= child_died
;
621 sigemptyset(&act
.sa_mask
);
622 act
.sa_flags
= SA_NOCLDSTOP
;
623 sigaction(SIGCHLD
, &act
, NULL
);
625 /* Ignore SIGPIPE - check for EPIPE errors instead */
626 act
.sa_handler
= SIG_IGN
;
627 sigemptyset(&act
.sa_mask
);
629 sigaction(SIGPIPE
, &act
, NULL
);
631 /* Set up session managament if available */
632 session_init(client_id
);
635 /* See if we need to migrate the Choices directories*/
638 /* Finally, execute the request */
639 reply
= run_soap(rpc
);
641 soap_reply(reply
, rpc_mode
);
643 /* Try to find out why we crash with GTK 2.4 */
644 XSetErrorHandler(rox_x_error
);
646 /* Enter the main loop, processing events until all our windows
649 if (number_of_windows
> 0)
655 /* Register a function to be called when process number 'child' dies. */
656 void on_child_death(gint child
, CallbackFn callback
, gpointer data
)
660 g_return_if_fail(callback
!= NULL
);
662 cb
= g_new(Callback
, 1);
664 cb
->callback
= callback
;
667 g_hash_table_insert(death_callbacks
, GINT_TO_POINTER(child
), cb
);
670 void one_less_window(void)
672 if (--number_of_windows
< 1)
676 /****************************************************************
677 * INTERNAL FUNCTIONS *
678 ****************************************************************/
680 static void show_features(void)
683 g_print(_("Compiled with GTK version %s\n"), GTK_VERSION
);
684 g_print(_("Running with GTK version %d.%d.%d\n"),
688 g_print("\n-- %s --\n\n", _("features set at compile time"));
689 g_print("%s... %s\n", _("Large File Support"),
690 #ifdef LARGE_FILE_SUPPORT
696 g_print("%s... %s\n", _("Dnotify support"),
703 g_print("%s... %s\n", _("Binary compatibility"),
704 #if defined(HAVE_APSYMBOLS_H) || defined(HAVE_APBUILD_APSYMBOLS_H)
705 _("Yes (can run with older glibc versions)")
707 _("No (apsymbols.h not found)")
711 g_print("%s... %s\n", _("Extended attribute support"),
712 xattr_supported(NULL
)? _("Yes"): _("No"));
715 static void soap_add(xmlNodePtr body
,
717 const xmlChar
*arg1_name
, const xmlChar
*arg1_value
,
718 const xmlChar
*arg2_name
, const xmlChar
*arg2_value
)
723 rox
= xmlSearchNsByHref(body
->doc
, body
, ROX_NS
);
725 node
= xmlNewChild(body
, rox
, function
, NULL
);
729 xmlNewTextChild(node
, rox
, arg1_name
, arg1_value
);
731 xmlNewTextChild(node
, rox
, arg2_name
, arg2_value
);
735 static void soap_reply(xmlDocPtr reply
, gboolean rpc_mode
)
743 gchar
**errs
=extract_soap_errors(reply
);
750 for(i
=0; errs
[i
]; i
++)
751 fprintf(stderr
, "%s\n", errs
[i
]);
757 /* Write the result, if any, to stdout */
759 save_xml_file(reply
, "-");
763 /* This is called as a signal handler; simply ensures that
764 * child_died_callback() will get called later.
766 static void child_died(int signum
)
768 child_died_flag
= TRUE
;
769 write(to_wakeup_pipe
, "\0", 1); /* Wake up! */
772 static void child_died_callback(void)
777 child_died_flag
= FALSE
;
779 /* Find out which children exited and allow them to die */
784 child
= waitpid(-1, &status
, WNOHANG
);
786 if (child
== 0 || child
== -1)
789 cb
= g_hash_table_lookup(death_callbacks
,
790 GINT_TO_POINTER(child
));
793 cb
->callback(cb
->data
);
794 g_hash_table_remove(death_callbacks
,
795 GINT_TO_POINTER(child
));
802 /* When data is written to_wakeup_pipe, this gets called from the event
803 * loop some time later. Useful for getting out of signal handlers, etc.
805 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
)
809 read(source
, buf
, BUFLEN
);
812 child_died_callback();
814 if (dnotify_wakeup_flag
)
819 static void xrandr_size_change(GdkScreen
*screen
, gpointer user_data
)
821 gui_store_screen_geometry(screen
);
824 pinboard_update_size();
827 static void add_default_panel_and_pinboard(xmlNodePtr body
)
831 if (o_session_panel_or_pin
.int_value
!= SESSION_PANEL_ONLY
)
833 name
=o_session_pinboard_name
.value
;
836 soap_add(body
, "Pinboard","Name", name
, NULL
, NULL
);
839 if (o_session_panel_or_pin
.int_value
!= SESSION_PINBOARD_ONLY
)
841 name
= o_session_panel_name
.value
;
845 soap_add(body
, "Panel", "Name", name
, NULL
, NULL
);
849 static GtkWidget
*launch_button_new(const char *label
, const char *uri
)
856 button
= button_new_mixed(GTK_STOCK_PREFERENCES
, label
);
857 closure
= g_cclosure_new_swap(G_CALLBACK(launch_uri
),
859 (GClosureNotify
) g_free
);
860 g_signal_connect_closure(button
, "clicked", closure
, FALSE
);
862 allow_right_click(button
);
864 slash
= strrchr(uri
, '/');
867 tip
= g_strdup_printf(
868 _("Left-click to run %s.\n"
869 "Right-click for a list of versions."),
872 gtk_tooltips_set_tip(tooltips
, button
, tip
, NULL
);
879 static GList
*build_launch(Option
*option
, xmlNode
*node
, guchar
*label
)
884 g_return_val_if_fail(option
== NULL
, NULL
);
885 g_return_val_if_fail(label
!= NULL
, NULL
);
887 uri
= xmlGetProp(node
, "uri");
889 g_return_val_if_fail(uri
!= NULL
, NULL
);
891 align
= gtk_alignment_new(0, 0.5, 0, 0);
893 gtk_container_add(GTK_CONTAINER(align
),
894 launch_button_new(_(label
), uri
));
898 return g_list_append(NULL
, align
);