2 * main.c -- config file parsing and main loop for browser-switchboard
4 * Copyright (C) 2009-2010 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"
37 struct swb_context ctx;
39 static void waitforzombies(int signalnum) {
40 while (waitpid(-1, NULL, WNOHANG) > 0)
41 log_msg("Waited for a zombie\n");
44 static void read_config(int signalnum) {
45 struct swb_config cfg;
47 swb_config_init(&cfg);
49 swb_config_load(&cfg);
51 log_config(cfg.logging);
53 /* continuous mode is required on Fremantle */
54 ctx.continuous_mode = 1;
55 if (!cfg.continuous_mode)
56 log_msg("continuous_mode = 0 operation no longer supported, ignoring config setting\n");
58 ctx.continuous_mode = cfg.continuous_mode;
60 free(ctx.other_browser_cmd);
61 if (cfg.other_browser_cmd) {
62 if (!(ctx.other_browser_cmd = strdup(cfg.other_browser_cmd))) {
63 log_perror(errno, "Failed to set other_browser_cmd");
67 ctx.other_browser_cmd = NULL;
68 update_default_browser(&ctx, cfg.default_browser);
70 log_msg("continuous_mode: %d\n", cfg.continuous_mode);
71 log_msg("default_browser: '%s'\n", cfg.default_browser);
72 log_msg("other_browser_cmd: '%s'\n",
73 cfg.other_browser_cmd?cfg.other_browser_cmd:"NULL");
74 log_msg("logging: '%s'\n", cfg.logging);
76 swb_config_free(&cfg);
81 OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
82 OssoBrowser *obj_osso_browser_sys, *obj_osso_browser_sys_req;
89 if (ctx.continuous_mode) {
90 /* Install signal handlers */
92 act.sa_flags = SA_RESTART;
93 sigemptyset(&(act.sa_mask));
95 /* SIGCHLD -- clean up after zombies */
96 act.sa_handler = waitforzombies;
97 if (sigaction(SIGCHLD, &act, NULL) == -1) {
98 log_msg("Installing signal handler failed\n");
102 /* SIGHUP -- reread config file */
103 act.sa_handler = read_config;
104 if (sigaction(SIGHUP, &act, NULL) == -1) {
105 log_msg("Installing signal handler failed\n");
112 dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
113 &dbus_glib_osso_browser_object_info);
115 /* Get a connection to the D-Bus session bus */
116 ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
117 if (!ctx.session_bus) {
118 log_msg("Couldn't get a D-Bus bus connection\n");
121 ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
122 "org.freedesktop.DBus", "/org/freedesktop/DBus",
123 "org.freedesktop.DBus");
124 if (!ctx.dbus_proxy) {
125 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
129 /* Get the org.maemo.garage.browser-switchboard name from D-Bus, as
130 a form of locking to ensure that not more than one
131 browser-switchboard process is active at any time. With
132 DBUS_NAME_FLAG_DO_NOT_QUEUE set and DBUS_NAME_FLAG_REPLACE_EXISTING
133 not set, getting the name succeeds if and only if no other
134 process owns the name. */
135 if (!dbus_g_proxy_call(ctx.dbus_proxy, "RequestName", &error,
136 G_TYPE_STRING, "org.maemo.garage.browser-switchboard",
137 G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
139 G_TYPE_UINT, &reqname_result,
141 log_msg("Couldn't acquire browser-switchboard lock: %s\n",
145 if (reqname_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
146 log_msg("Another browser-switchboard already running\n");
150 /* Get a connection to the D-Bus system bus */
151 ctx.system_bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
152 if (!ctx.system_bus) {
153 log_msg("Couldn't get a D-Bus system bus connection\n");
156 ctx.dbus_system_proxy = dbus_g_proxy_new_for_name(ctx.system_bus,
157 "org.freedesktop.DBus", "/org/freedesktop/DBus",
158 "org.freedesktop.DBus");
159 if (!ctx.dbus_system_proxy) {
160 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
164 dbus_request_osso_browser_name(&ctx);
166 /* Register ourselves to handle the osso_browser D-Bus methods */
167 obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
168 obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
169 obj_osso_browser_sys = g_object_new(OSSO_BROWSER_TYPE, NULL);
170 obj_osso_browser_sys_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
171 dbus_g_connection_register_g_object(ctx.session_bus,
172 "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
173 dbus_g_connection_register_g_object(ctx.session_bus,
174 "/com/nokia/osso_browser/request",
175 G_OBJECT(obj_osso_browser_req));
176 dbus_g_connection_register_g_object(ctx.system_bus,
177 "/com/nokia/osso_browser",
178 G_OBJECT(obj_osso_browser_sys));
179 dbus_g_connection_register_g_object(ctx.system_bus,
180 "/com/nokia/osso_browser/request",
181 G_OBJECT(obj_osso_browser_sys_req));
183 mainloop = g_main_loop_new(NULL, FALSE);
184 log_msg("Starting main loop\n");
185 g_main_loop_run(mainloop);
186 log_msg("Main loop completed\n");