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
;
84 N_("Copyright (C) 2001 Thomas Leonard.\n" \
85 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
86 "to the extent permitted by law.\n" \
87 "You may redistribute copies of ROX-Filer\n" \
88 "under the terms of the GNU General Public License.\n" \
89 "For more information about these matters, " \
90 "see the file named COPYING.\n")
92 #ifdef HAVE_GETOPT_LONG
93 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
94 # define SHORT_ONLY_WARNING ""
96 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
97 # define SHORT_ONLY_WARNING \
98 _("NOTE: Your system does not support long options - \n" \
99 "you must use the short versions instead.\n\n")
102 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
103 "Open each directory or file listed, or the current working\n" \
104 "directory if no arguments are given.\n\n" \
105 " -b, --bottom=PANEL open PAN as a bottom-edge panel\n" \
106 " -c, --client-id=ID used for session management\n" \
107 " -d, --dir=DIR open DIR as directory (not application)\n" \
108 " -D, --close=DIR close DIR and its subdirectories\n" \
109 " -h, --help display this help and exit\n" \
110 " -l, --left=PANEL open PAN as a left-edge panel\n" \
111 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
112 " -n, --new start a new filer, even if already running\n" \
113 " -o, --override override window manager control of panels\n" \
114 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
115 " -r, --right=PANEL open PAN as a right-edge panel\n" \
116 " -R, --RPC invoke method call read from stdin\n" \
117 " -s, --show=FILE open a directory showing FILE\n" \
118 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
119 " -u, --user show user name in each window \n" \
120 " -v, --version display the version information and exit\n" \
121 " -x, --examine=FILE FILE has changed - re-examine it\n" \
122 "\nThe latest version can be found at:\n" \
123 "\thttp://rox.sourceforge.net\n" \
124 "\nReport bugs to <tal197@users.sourceforge.net>.\n")
126 #define SHORT_OPS "d:t:b:l:r:op:s:hvnux:m:D:R"
128 #ifdef HAVE_GETOPT_LONG
129 static struct option long_opts
[] =
131 {"dir", 1, NULL
, 'd'},
132 {"top", 1, NULL
, 't'},
133 {"bottom", 1, NULL
, 'b'},
134 {"left", 1, NULL
, 'l'},
135 {"override", 0, NULL
, 'o'},
136 {"pinboard", 1, NULL
, 'p'},
137 {"right", 1, NULL
, 'r'},
138 {"help", 0, NULL
, 'h'},
139 {"version", 0, NULL
, 'v'},
140 {"user", 0, NULL
, 'u'},
141 {"new", 0, NULL
, 'n'},
142 {"RPC", 0, NULL
, 'R'},
143 {"show", 1, NULL
, 's'},
144 {"examine", 1, NULL
, 'x'},
145 {"close", 1, NULL
, 'D'},
146 {"mime-type", 1, NULL
, 'm'},
147 {"client-id", 1, NULL
, 'c'},
152 /* Take control of panels away from WM? */
153 gboolean override_redirect
= FALSE
;
155 /* Always start a new filer, even if one seems to be already running */
156 gboolean new_copy
= FALSE
;
158 /* Maps child PIDs to Callback pointers */
159 static GHashTable
*death_callbacks
= NULL
;
160 static gboolean child_died_flag
= FALSE
;
162 /* Static prototypes */
163 static void show_features(void);
164 static xmlDocPtr
soap_new(xmlNodePtr
*ret_body
);
165 static void soap_add(xmlNodePtr body
,
167 xmlChar
*arg1_name
, xmlChar
*arg1_value
,
168 xmlChar
*arg2_name
, xmlChar
*arg2_value
);
169 static void child_died(int signum
);
170 static void child_died_callback(void);
171 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
);
173 /****************************************************************
174 * EXTERNAL INTERFACE *
175 ****************************************************************/
177 /* The value that goes with an option */
178 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
180 int main(int argc
, char **argv
)
184 struct sigaction act
;
185 guchar
*tmp
, *dir
, *slash
;
186 gchar
*client_id
= NULL
;
187 gboolean show_user
= FALSE
;
188 xmlDocPtr rpc
, soap_rpc
= NULL
;
191 home_dir
= g_get_home_dir();
192 home_dir_len
= strlen(home_dir
);
193 app_dir
= g_strdup(getenv("APP_DIR"));
200 g_warning("APP_DIR environment variable was unset!\n"
201 "Use the AppRun script to invoke ROX-Filer...\n");
202 app_dir
= g_get_current_dir();
207 /* Don't pass it on to our child processes... */
212 death_callbacks
= g_hash_table_new(NULL
, NULL
);
220 ngroups
= getgroups(0, NULL
);
223 else if (ngroups
> 0)
225 supplemental_groups
= g_malloc(sizeof(gid_t
) * ngroups
);
226 getgroups(ngroups
, supplemental_groups
);
229 /* The idea here is to convert the command-line arguments
231 * We attempt to invoke the call on an already-running copy of
232 * the filer if possible, or execute it ourselves if not.
234 rpc
= soap_new(&body
);
239 #ifdef HAVE_GETOPT_LONG
241 c
= getopt_long(argc
, argv
, SHORT_OPS
,
242 long_opts
, &long_index
);
244 c
= getopt(argc
, argv
, SHORT_OPS
);
248 break; /* No more options */
256 override_redirect
= TRUE
;
259 fprintf(stderr
, "ROX-Filer %s\n", VERSION
);
260 fprintf(stderr
, _(COPYING
));
264 fprintf(stderr
, _(HELP
));
265 fprintf(stderr
, _(SHORT_ONLY_WARNING
));
270 /* Argument is a path */
271 tmp
= pathdup(VALUE
);
273 c
== 'D' ? "CloseDir" :
274 c
== 'd' ? "OpenDir" :
275 c
== 'x' ? "Examine" : "Unknown",
281 tmp
= g_strdup(VALUE
);
282 slash
= strrchr(tmp
, '/');
295 soap_add(body
, "Show",
305 /* Argument is a leaf (or starts with /) */
306 soap_add(body
, "Panel", "Name", VALUE
,
307 "Side", c
== 'l' ? "Left" :
310 c
== 'b' ? "Bottom" :
314 soap_add(body
, "Pinboard",
315 "Name", VALUE
, NULL
, NULL
);
324 type
= type_get_type(VALUE
);
325 printf("%s/%s\n", type
->media_type
,
330 client_id
= g_strdup(VALUE
);
333 soap_rpc
= xmlParseFile("-");
335 g_error("Invalid XML in RPC");
343 add_default_styles();
344 gtk_init(&argc
, &argv
);
346 if (euid
== 0 || show_user
)
347 show_user_message
= g_strdup_printf( _("Running as user '%s'"),
353 tmp
= pathdup(argv
[i
++]);
355 soap_add(body
, "Run", "Filename", tmp
, NULL
, NULL
);
362 if (body
->xmlChildrenNode
)
363 g_error("Can't use -R with other options - sorry!");
368 else if (!body
->xmlChildrenNode
)
372 dir
= g_get_current_dir();
373 soap_add(body
, "OpenDir", "Filename", dir
, NULL
, NULL
);
377 option_add_int("dnd_no_hostnames", 1, NULL
);
380 if (remote_init(rpc
, new_copy
))
381 return EXIT_SUCCESS
; /* Already running */
383 /* Put ourselves into the background (so 'rox' always works the
384 * same, whether we're already running or not).
385 * Not for -n, though (helps when debugging).
393 _exit(0); /* Parent exits */
394 /* Otherwise we're the child (or an error occurred - ignore
423 close_on_exec(wakeup_pipe
[0], TRUE
);
424 close_on_exec(wakeup_pipe
[1], TRUE
);
425 gdk_input_add(wakeup_pipe
[0], GDK_INPUT_READ
, wake_up_cb
, NULL
);
426 to_wakeup_pipe
= wakeup_pipe
[1];
428 /* If the pipe is full then we're going to get woken up anyway... */
429 set_blocking(to_wakeup_pipe
, FALSE
);
431 /* Let child processes die */
432 act
.sa_handler
= child_died
;
433 sigemptyset(&act
.sa_mask
);
434 act
.sa_flags
= SA_NOCLDSTOP
;
435 sigaction(SIGCHLD
, &act
, NULL
);
437 /* Ignore SIGPIPE - check for EPIPE errors instead */
438 act
.sa_handler
= SIG_IGN
;
439 sigemptyset(&act
.sa_mask
);
441 sigaction(SIGPIPE
, &act
, NULL
);
443 /* Set up session managament if available */
444 session_init(client_id
);
450 if (number_of_windows
> 0)
456 /* Register a function to be called when process number 'child' dies. */
457 void on_child_death(gint child
, CallbackFn callback
, gpointer data
)
461 g_return_if_fail(callback
!= NULL
);
463 cb
= g_new(Callback
, 1);
465 cb
->callback
= callback
;
468 g_hash_table_insert(death_callbacks
, GINT_TO_POINTER(child
), cb
);
471 /****************************************************************
472 * INTERNAL FUNCTIONS *
473 ****************************************************************/
475 static void show_features(void)
477 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
478 g_printerr("%s... %s\n", _("VFS support"),
482 _("No (couldn't find a valid libvfs)")
487 /* Create a new SOAP message and return the document and the (empty)
490 static xmlDocPtr
soap_new(xmlNodePtr
*ret_body
)
496 doc
= xmlNewDoc("1.0");
497 root
= xmlNewDocNode(doc
, NULL
, "Envelope", NULL
);
498 xmlDocSetRootElement(doc
, root
);
500 env_ns
= xmlNewNs(root
, SOAP_ENV_NS
, "env");
501 xmlSetNs(root
, env_ns
);
503 *ret_body
= xmlNewTextChild(root
, env_ns
, "Body", NULL
);
504 xmlNewNs(*ret_body
, ROX_NS
, "rox");
509 static void soap_add(xmlNodePtr body
,
511 xmlChar
*arg1_name
, xmlChar
*arg1_value
,
512 xmlChar
*arg2_name
, xmlChar
*arg2_value
)
517 rox
= xmlSearchNsByHref(body
->doc
, body
, ROX_NS
);
519 node
= xmlNewChild(body
, rox
, function
, NULL
);
523 xmlNewTextChild(node
, rox
, arg1_name
, arg1_value
);
525 xmlNewTextChild(node
, rox
, arg2_name
, arg2_value
);
529 /* This is called as a signal handler; simply ensures that
530 * child_died_callback() will get called later.
532 static void child_died(int signum
)
534 child_died_flag
= TRUE
;
535 write(to_wakeup_pipe
, "\0", 1); /* Wake up! */
538 static void child_died_callback(void)
543 child_died_flag
= FALSE
;
545 /* Find out which children exited and allow them to die */
550 child
= waitpid(-1, &status
, WNOHANG
);
552 if (child
== 0 || child
== -1)
555 cb
= g_hash_table_lookup(death_callbacks
,
556 GINT_TO_POINTER(child
));
559 cb
->callback(cb
->data
);
560 g_hash_table_remove(death_callbacks
,
561 GINT_TO_POINTER(child
));
568 /* When data is written to_wakeup_pipe, this gets called from the event
569 * loop some time later. Useful for getting out of signal handlers, etc.
571 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
)
575 read(source
, buf
, BUFLEN
);
578 child_died_callback();