2 * launcher.c -- functions for launching web browsers for browser-switchboard
4 * Copyright (C) 2009 Steven Luo
5 * Derived from a Python implementation by Jason Simpson and Steven Luo
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 #include <sys/types.h>
29 #include <dbus/dbus-glib.h>
31 #include "browser-switchboard.h"
33 #include "dbus-server-bindings.h"
35 #define LAUNCH_DEFAULT_BROWSER launch_tear
37 static void launch_tear(struct swb_context *ctx, char *uri) {
39 static DBusGProxy *tear_proxy = NULL;
46 printf("launch_tear with uri '%s'\n", uri);
48 status = system("pidof tear > /dev/null");
49 if (WIFEXITED(status) && !WEXITSTATUS(status)) {
51 tear_proxy = dbus_g_proxy_new_for_name(ctx->session_bus,
52 "com.nokia.tear", "/com/nokia/tear",
54 dbus_g_proxy_call(tear_proxy, "OpenAddress", &error,
55 G_TYPE_STRING, uri, G_TYPE_INVALID);
56 if (!ctx->continuous_mode)
59 if (ctx->continuous_mode) {
60 if ((pid = fork()) != 0) {
61 /* Parent process or error in fork() */
62 printf("child: %d\n", (int)pid);
68 execl("/usr/bin/tear", "/usr/bin/tear", uri, (char *)NULL);
72 void launch_microb(struct swb_context *ctx, char *uri) {
73 int kill_browserd = 0;
80 status = system("pidof /usr/sbin/browserd > /dev/null");
81 if (WIFEXITED(status) && WEXITSTATUS(status)) {
83 system("/usr/sbin/browserd -d");
86 dbus_release_osso_browser_name(ctx);
88 if ((pid = fork()) == -1) {
94 waitpid(pid, &status, 0);
97 if (!strcmp(uri, "new_window")) {
98 execl("/usr/bin/maemo-invoker",
99 "browser", (char *)NULL);
101 execl("/usr/bin/maemo-invoker",
102 "browser", "--url", uri, (char *)NULL);
107 system("kill `pidof /usr/sbin/browserd`");
109 if (!ctx || !ctx->continuous_mode)
112 dbus_request_osso_browser_name(ctx);
115 static void launch_other_browser(struct swb_context *ctx, char *uri) {
117 char *quoted_uri, *quote;
119 size_t cmdlen, urilen;
120 size_t quoted_uri_size;
123 if (!uri || !strcmp(uri, "new_window"))
125 urilen = strlen(uri);
128 /* urilen+3 = length of URI + 2x \' + \0 */
129 if (!(quoted_uri = calloc(urilen+3, sizeof(char))))
131 snprintf(quoted_uri, urilen+3, "'%s'", uri);
133 /* If there are any 's in the original URI, URL-escape them
134 (replace them with %27) */
135 quoted_uri_size = urilen + 3;
136 quote = quoted_uri + 1;
137 while ((quote = strchr(quote, '\'')) &&
138 (offset = quote-quoted_uri) < strlen(quoted_uri)-1) {
139 /* Check to make sure we don't shrink the memory area
140 as a result of integer overflow */
141 if (quoted_uri_size+2 <= quoted_uri_size)
144 /* Grow the memory area;
145 2 = strlen("%27")-strlen("'") */
146 if (!(quoted_uri = realloc(quoted_uri,
149 quoted_uri_size = quoted_uri_size + 2;
151 /* Recalculate the location of the ' character --
152 realloc() may have moved the string in memory */
153 quote = quoted_uri + offset;
155 /* Move the string after the ', including the \0,
157 memmove(quote+3, quote+1, strlen(quote)+1);
158 memcpy(quote, "%27", 3);
161 urilen = strlen(quoted_uri);
165 cmdlen = strlen(ctx->other_browser_cmd);
167 /* cmdlen+urilen+1 is normally two bytes longer than we need (uri will
168 replace "%s"), but is needed in the case other_browser_cmd has no %s
170 if (!(command = calloc(cmdlen+urilen+1, sizeof(char))))
172 snprintf(command, cmdlen+urilen+1, ctx->other_browser_cmd, quoted_uri);
173 printf("command: '%s'\n", command);
175 if (ctx->continuous_mode) {
177 /* Parent process or error in fork() */
186 execl("/bin/sh", "/bin/sh", "-c", command, (char *)NULL);
189 static void use_other_browser_cmd(struct swb_context *ctx, char *cmd) {
190 size_t len = strlen(cmd);
192 free(ctx->other_browser_cmd);
193 ctx->other_browser_cmd = calloc(len+1, sizeof(char));
194 if (!ctx->other_browser_cmd) {
195 printf("malloc failed!\n");
196 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
198 ctx->other_browser_cmd = strncpy(ctx->other_browser_cmd,
200 ctx->default_browser_launcher = launch_other_browser;
204 void update_default_browser(struct swb_context *ctx, char *default_browser) {
208 if (!default_browser) {
209 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
213 if (!strcmp(default_browser, "tear"))
214 ctx->default_browser_launcher = launch_tear;
215 else if (!strcmp(default_browser, "microb"))
216 ctx->default_browser_launcher = launch_microb;
217 else if (!strcmp(default_browser, "fennec"))
218 use_other_browser_cmd(ctx, "fennec %s");
219 else if (!strcmp(default_browser, "midori"))
220 use_other_browser_cmd(ctx, "midori %s");
221 else if (!strcmp(default_browser, "other")) {
222 if (ctx->other_browser_cmd)
223 ctx->default_browser_launcher = launch_other_browser;
225 printf("default_browser is 'other', but no other_browser_cmd set -- using default\n");
226 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
229 printf("Unknown default_browser %s, using default", default_browser);
230 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
234 void launch_browser(struct swb_context *ctx, char *uri) {
235 if (ctx && ctx->default_browser_launcher)
236 ctx->default_browser_launcher(ctx, uri);