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))
51 if (!WEXITSTATUS(status)) {
53 tear_proxy = dbus_g_proxy_new_for_name(ctx->session_bus,
54 "com.nokia.tear", "/com/nokia/tear",
56 dbus_g_proxy_call(tear_proxy, "OpenAddress",
60 if (!ctx->continuous_mode)
63 if (ctx->continuous_mode) {
64 if ((pid = fork()) != 0) {
65 /* Parent process or error in fork() */
66 printf("child: %d\n", (int)pid);
72 execl("/usr/bin/tear", "/usr/bin/tear", uri, (char *)NULL);
76 void launch_microb(struct swb_context *ctx, char *uri) {
77 int kill_browserd = 0;
84 status = system("pidof /usr/sbin/browserd > /dev/null");
85 if (!WIFEXITED(status))
87 if (WEXITSTATUS(status)) {
89 system("/usr/sbin/browserd -d");
92 dbus_release_osso_browser_name(ctx);
94 if ((pid = fork()) == -1) {
100 waitpid(pid, &status, 0);
103 if (!strcmp(uri, "new_window")) {
104 execl("/usr/bin/maemo-invoker",
105 "browser", (char *)NULL);
107 execl("/usr/bin/maemo-invoker",
108 "browser", "--url", uri, (char *)NULL);
113 system("kill `pidof /usr/sbin/browserd`");
115 if (!ctx || !ctx->continuous_mode)
118 dbus_request_osso_browser_name(ctx);
121 static void launch_other_browser(struct swb_context *ctx, char *uri) {
123 char *quoted_uri, *quote;
125 size_t cmdlen, urilen;
127 if (!uri || !strcmp(uri, "new_window"))
129 urilen = strlen(uri);
132 /* urilen+3 = length of URI + 2x \' + \0 */
133 if (!(quoted_uri = calloc(urilen+3, sizeof(char))))
135 strncpy(quoted_uri+1, uri, urilen);
136 quoted_uri[0] = quoted_uri[urilen+1] = '\'';
137 /* calloc zeroes the memory, so string is automatically
140 /* If there are any 's in the original URI, URL-escape them
141 (replace them with %27) */
142 quote = quoted_uri + 1;
143 while ((quote = strchr(quote, '\'')) &&
144 (quote-quoted_uri) < strlen(quoted_uri)-1) {
145 /* 3 = strlen("%27")-strlen("'") + \0 */
146 if (!(quoted_uri = realloc(quoted_uri,
147 strlen(quoted_uri)+3)))
149 /* Move the string after the ', including the \0,
151 memmove(quote+3, quote+1, strlen(quote)+1);
152 memcpy(quote, "%27", 3);
155 urilen = strlen(quoted_uri);
159 cmdlen = strlen(ctx->other_browser_cmd);
161 /* cmdlen+urilen+1 is normally two bytes longer than we need (uri will
162 replace "%s"), but is needed in the case other_browser_cmd has no %s
164 if (!(command = calloc(cmdlen+urilen+1, sizeof(char))))
166 snprintf(command, cmdlen+urilen+1, ctx->other_browser_cmd, quoted_uri);
167 printf("command: '%s'\n", command);
169 if (ctx->continuous_mode) {
171 /* Parent process or error in fork() */
180 execl("/bin/sh", "/bin/sh", "-c", command, (char *)NULL);
183 static void use_other_browser_cmd(struct swb_context *ctx, char *cmd) {
184 size_t len = strlen(cmd);
186 free(ctx->other_browser_cmd);
187 ctx->other_browser_cmd = calloc(len+1, sizeof(char));
188 if (!ctx->other_browser_cmd) {
189 printf("malloc failed!\n");
190 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
192 ctx->other_browser_cmd = strncpy(ctx->other_browser_cmd,
194 ctx->other_browser_cmd[len] = '\0';
195 ctx->default_browser_launcher = launch_other_browser;
199 void update_default_browser(struct swb_context *ctx, char *default_browser) {
203 if (!default_browser) {
204 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
208 if (!strcmp(default_browser, "tear"))
209 ctx->default_browser_launcher = launch_tear;
210 else if (!strcmp(default_browser, "microb"))
211 ctx->default_browser_launcher = launch_microb;
212 else if (!strcmp(default_browser, "fennec"))
213 use_other_browser_cmd(ctx, "fennec %s");
214 else if (!strcmp(default_browser, "midori"))
215 use_other_browser_cmd(ctx, "midori %s");
216 else if (!strcmp(default_browser, "other")) {
217 if (ctx->other_browser_cmd)
218 ctx->default_browser_launcher = launch_other_browser;
220 printf("default_browser is 'other', but no other_browser_cmd set -- using default\n");
221 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
224 printf("Unknown default_browser %s, using default", default_browser);
225 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
229 void launch_browser(struct swb_context *ctx, char *uri) {
230 if (ctx && ctx->default_browser_launcher)
231 ctx->default_browser_launcher(ctx, uri);