4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, 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)
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
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
26 #include <sys/types.h>
35 #ifdef HAVE_GETOPT_LONG
40 #include "collection.h"
46 #include "gui_support.h"
69 int number_of_windows
= 0; /* Quit when this reaches 0 again... */
70 static int to_wakeup_pipe
= -1; /* Write here to get noticed */
74 int ngroups
; /* Number of supplemental groups */
75 gid_t
*supplemental_groups
= NULL
;
77 /* Message to display at the top of each filer window */
78 guchar
*show_user_message
= NULL
;
81 char *home_dir
, *app_dir
;
83 GtkTooltips
*tooltips
= NULL
;
86 N_("Copyright (C) 2001 Thomas Leonard.\n" \
87 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
88 "to the extent permitted by law.\n" \
89 "You may redistribute copies of ROX-Filer\n" \
90 "under the terms of the GNU General Public License.\n" \
91 "For more information about these matters, " \
92 "see the file named COPYING.\n")
94 #ifdef HAVE_GETOPT_LONG
95 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
96 # define SHORT_ONLY_WARNING ""
98 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
99 # define SHORT_ONLY_WARNING \
100 _("NOTE: Your system does not support long options - \n" \
101 "you must use the short versions instead.\n\n")
104 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
105 "Open each directory or file listed, or the current working\n" \
106 "directory if no arguments are given.\n\n" \
107 " -b, --bottom=PANEL open PAN as a bottom-edge panel\n" \
108 " -c, --client-id=ID used for session management\n" \
109 " -d, --dir=DIR open DIR as directory (not application)\n" \
110 " -D, --close=DIR close DIR and its subdirectories\n" \
111 " -h, --help display this help and exit\n" \
112 " -l, --left=PANEL open PAN as a left-edge panel\n" \
113 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
114 " -n, --new start a new filer, even if already running\n" \
115 " -o, --override override window manager control of panels\n" \
116 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
117 " -r, --right=PANEL open PAN as a right-edge panel\n" \
118 " -R, --RPC invoke method call read from stdin\n" \
119 " -s, --show=FILE open a directory showing FILE\n" \
120 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
121 " -u, --user show user name in each window \n" \
122 " -v, --version display the version information and exit\n" \
123 " -x, --examine=FILE FILE has changed - re-examine it\n" \
124 "\nThe latest version can be found at:\n" \
125 "\thttp://rox.sourceforge.net\n" \
126 "\nReport bugs to <tal197@users.sourceforge.net>.\n")
128 #define SHORT_OPS "d:t:b:l:r:op:s:hvnux:m:D:R"
130 #ifdef HAVE_GETOPT_LONG
131 static struct option long_opts
[] =
133 {"dir", 1, NULL
, 'd'},
134 {"top", 1, NULL
, 't'},
135 {"bottom", 1, NULL
, 'b'},
136 {"left", 1, NULL
, 'l'},
137 {"override", 0, NULL
, 'o'},
138 {"pinboard", 1, NULL
, 'p'},
139 {"right", 1, NULL
, 'r'},
140 {"help", 0, NULL
, 'h'},
141 {"version", 0, NULL
, 'v'},
142 {"user", 0, NULL
, 'u'},
143 {"new", 0, NULL
, 'n'},
144 {"RPC", 0, NULL
, 'R'},
145 {"show", 1, NULL
, 's'},
146 {"examine", 1, NULL
, 'x'},
147 {"close", 1, NULL
, 'D'},
148 {"mime-type", 1, NULL
, 'm'},
149 {"client-id", 1, NULL
, 'c'},
154 /* Take control of panels away from WM? */
155 gboolean override_redirect
= FALSE
;
157 /* Always start a new filer, even if one seems to be already running */
158 gboolean new_copy
= FALSE
;
160 /* Maps child PIDs to Callback pointers */
161 static GHashTable
*death_callbacks
= NULL
;
162 static gboolean child_died_flag
= FALSE
;
164 /* Static prototypes */
165 static void show_features(void);
166 static void soap_add(xmlNodePtr body
,
168 xmlChar
*arg1_name
, xmlChar
*arg1_value
,
169 xmlChar
*arg2_name
, xmlChar
*arg2_value
);
170 static void child_died(int signum
);
171 static void child_died_callback(void);
172 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
);
174 /****************************************************************
175 * EXTERNAL INTERFACE *
176 ****************************************************************/
178 /* The value that goes with an option */
179 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
181 int main(int argc
, char **argv
)
185 struct sigaction act
;
186 guchar
*tmp
, *dir
, *slash
;
187 gchar
*client_id
= NULL
;
188 gboolean show_user
= FALSE
;
189 xmlDocPtr rpc
, soap_rpc
= NULL
, reply
;
192 home_dir
= g_get_home_dir();
193 home_dir_len
= strlen(home_dir
);
194 app_dir
= g_strdup(getenv("APP_DIR"));
201 g_warning("APP_DIR environment variable was unset!\n"
202 "Use the AppRun script to invoke ROX-Filer...\n");
203 app_dir
= g_get_current_dir();
208 /* Don't pass it on to our child processes... */
213 death_callbacks
= g_hash_table_new(NULL
, NULL
);
221 ngroups
= getgroups(0, NULL
);
224 else if (ngroups
> 0)
226 supplemental_groups
= g_malloc(sizeof(gid_t
) * ngroups
);
227 getgroups(ngroups
, supplemental_groups
);
230 /* The idea here is to convert the command-line arguments
232 * We attempt to invoke the call on an already-running copy of
233 * the filer if possible, or execute it ourselves if not.
235 rpc
= soap_new(&body
);
240 #ifdef HAVE_GETOPT_LONG
242 c
= getopt_long(argc
, argv
, SHORT_OPS
,
243 long_opts
, &long_index
);
245 c
= getopt(argc
, argv
, SHORT_OPS
);
249 break; /* No more options */
257 override_redirect
= TRUE
;
260 fprintf(stderr
, "ROX-Filer %s\n", VERSION
);
261 fprintf(stderr
, _(COPYING
));
265 fprintf(stderr
, _(HELP
));
266 fprintf(stderr
, _(SHORT_ONLY_WARNING
));
271 /* Argument is a path */
272 tmp
= pathdup(VALUE
);
274 c
== 'D' ? "CloseDir" :
275 c
== 'd' ? "OpenDir" :
276 c
== 'x' ? "Examine" : "Unknown",
282 tmp
= g_strdup(VALUE
);
283 slash
= strrchr(tmp
, '/');
296 soap_add(body
, "Show",
306 /* Argument is a leaf (or starts with /) */
307 soap_add(body
, "Panel", "Name", VALUE
,
308 "Side", c
== 'l' ? "Left" :
311 c
== 'b' ? "Bottom" :
315 soap_add(body
, "Pinboard",
316 "Name", VALUE
, NULL
, NULL
);
325 type
= type_get_type(VALUE
);
326 printf("%s/%s\n", type
->media_type
,
331 client_id
= g_strdup(VALUE
);
334 soap_rpc
= xmlParseFile("-");
336 g_error("Invalid XML in RPC");
344 add_default_styles();
345 gtk_init(&argc
, &argv
);
346 tooltips
= gtk_tooltips_new();
348 if (euid
== 0 || show_user
)
349 show_user_message
= g_strdup_printf( _("Running as user '%s'"),
355 tmp
= pathdup(argv
[i
++]);
357 soap_add(body
, "Run", "Filename", tmp
, NULL
, NULL
);
364 if (body
->xmlChildrenNode
)
365 g_error("Can't use -R with other options - sorry!");
370 else if (!body
->xmlChildrenNode
)
374 dir
= g_get_current_dir();
375 soap_add(body
, "OpenDir", "Filename", dir
, NULL
, NULL
);
379 option_add_int("dnd_no_hostnames", 1, NULL
);
382 if (remote_init(rpc
, new_copy
))
383 return EXIT_SUCCESS
; /* Already running */
385 /* Put ourselves into the background (so 'rox' always works the
386 * same, whether we're already running or not).
387 * Not for -n, though (helps when debugging).
395 _exit(0); /* Parent exits */
396 /* Otherwise we're the child (or an error occurred - ignore
401 /* Close stdin. We don't need it, and it can cause problems if
402 * a child process wants a password, etc...
406 fd
= open("/dev/null", O_RDONLY
);
439 close_on_exec(wakeup_pipe
[0], TRUE
);
440 close_on_exec(wakeup_pipe
[1], TRUE
);
441 gdk_input_add(wakeup_pipe
[0], GDK_INPUT_READ
, wake_up_cb
, NULL
);
442 to_wakeup_pipe
= wakeup_pipe
[1];
444 /* If the pipe is full then we're going to get woken up anyway... */
445 set_blocking(to_wakeup_pipe
, FALSE
);
447 /* Let child processes die */
448 act
.sa_handler
= child_died
;
449 sigemptyset(&act
.sa_mask
);
450 act
.sa_flags
= SA_NOCLDSTOP
;
451 sigaction(SIGCHLD
, &act
, NULL
);
453 /* Ignore SIGPIPE - check for EPIPE errors instead */
454 act
.sa_handler
= SIG_IGN
;
455 sigemptyset(&act
.sa_mask
);
457 sigaction(SIGPIPE
, &act
, NULL
);
459 /* Set up session managament if available */
460 session_init(client_id
);
463 reply
= run_soap(rpc
);
467 save_xml_file(reply
, "-");
471 if (number_of_windows
> 0)
477 /* Register a function to be called when process number 'child' dies. */
478 void on_child_death(gint child
, CallbackFn callback
, gpointer data
)
482 g_return_if_fail(callback
!= NULL
);
484 cb
= g_new(Callback
, 1);
486 cb
->callback
= callback
;
489 g_hash_table_insert(death_callbacks
, GINT_TO_POINTER(child
), cb
);
492 /****************************************************************
493 * INTERNAL FUNCTIONS *
494 ****************************************************************/
496 static void show_features(void)
498 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
499 g_printerr("%s... %s\n", _("VFS support"),
503 _("No (couldn't find a valid libvfs)")
508 static void soap_add(xmlNodePtr body
,
510 xmlChar
*arg1_name
, xmlChar
*arg1_value
,
511 xmlChar
*arg2_name
, xmlChar
*arg2_value
)
516 rox
= xmlSearchNsByHref(body
->doc
, body
, ROX_NS
);
518 node
= xmlNewChild(body
, rox
, function
, NULL
);
522 xmlNewTextChild(node
, rox
, arg1_name
, arg1_value
);
524 xmlNewTextChild(node
, rox
, arg2_name
, arg2_value
);
528 /* This is called as a signal handler; simply ensures that
529 * child_died_callback() will get called later.
531 static void child_died(int signum
)
533 child_died_flag
= TRUE
;
534 write(to_wakeup_pipe
, "\0", 1); /* Wake up! */
537 static void child_died_callback(void)
542 child_died_flag
= FALSE
;
544 /* Find out which children exited and allow them to die */
549 child
= waitpid(-1, &status
, WNOHANG
);
551 if (child
== 0 || child
== -1)
554 cb
= g_hash_table_lookup(death_callbacks
,
555 GINT_TO_POINTER(child
));
558 cb
->callback(cb
->data
);
559 g_hash_table_remove(death_callbacks
,
560 GINT_TO_POINTER(child
));
567 /* When data is written to_wakeup_pipe, this gets called from the event
568 * loop some time later. Useful for getting out of signal handlers, etc.
570 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
)
574 read(source
, buf
, BUFLEN
);
577 child_died_callback();