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,
28 #include <sys/types.h>
31 #include <dbus/dbus-glib.h>
33 #include "browser-switchboard.h"
35 #include "dbus-server-bindings.h"
39 struct swb_context ctx;
41 static void read_config(void);
43 /* Wait for zombies on SIGCHLD */
44 static void waitforzombies(int signalnum) {
45 while (waitpid(-1, NULL, WNOHANG) > 0);
48 /* Handle other signals in event loop by writing signal number to pipe */
50 static void handle_signal(int signalnum) {
51 write(eventpipe[1], &signalnum, sizeof signalnum);
54 /* Callbacks for polling the event pipe in the GLib event loop */
55 static GPollFD fdevents_pfd;
56 /* Called before entering the poll() */
57 static gboolean fdevents_prepare(GSource *source, gint *timeout) {
58 /* No timeout for poll() */
62 /* Check to see whether a handled event has happened */
63 static gboolean fdevents_check(GSource *source) {
64 return !!(fdevents_pfd.revents & G_IO_IN);
66 /* Read the event from the pipe and handle it */
67 static gboolean fdevents_dispatch(GSource *source,
68 GSourceFunc callback, gpointer user_data) {
71 if (read(eventpipe[0], &eventnum, sizeof eventnum) == (sizeof eventnum)) {
72 /* Handle the event passed to us */
74 /* SIGHUP received -- reread config file */
86 static GSourceFuncs fdevents_funcs = {
87 .prepare = fdevents_prepare,
88 .check = fdevents_check,
89 .dispatch = fdevents_dispatch,
94 static void read_config(void) {
95 struct swb_config cfg;
97 swb_config_init(&cfg);
99 swb_config_load(&cfg);
101 log_config(cfg.logging);
103 /* continuous mode is required on Fremantle */
104 ctx.continuous_mode = 1;
105 if (!cfg.continuous_mode)
106 log_msg("continuous_mode = 0 operation no longer supported, ignoring config setting\n");
108 ctx.continuous_mode = cfg.continuous_mode;
110 free(ctx.other_browser_cmd);
111 if (cfg.other_browser_cmd) {
112 if (!(ctx.other_browser_cmd = strdup(cfg.other_browser_cmd))) {
113 log_perror(errno, "Failed to set other_browser_cmd");
117 ctx.other_browser_cmd = NULL;
118 update_default_browser(&ctx, cfg.default_browser);
120 ctx.autostart_microb = cfg.autostart_microb;
123 log_msg("continuous_mode: %d\n", cfg.continuous_mode);
124 log_msg("default_browser: '%s'\n", cfg.default_browser);
125 log_msg("other_browser_cmd: '%s'\n",
126 cfg.other_browser_cmd?cfg.other_browser_cmd:"NULL");
127 log_msg("logging: '%s'\n", cfg.logging);
129 swb_config_free(&cfg);
134 OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
135 OssoBrowser *obj_osso_browser_sys, *obj_osso_browser_sys_req;
137 GError *error = NULL;
143 if (ctx.continuous_mode) {
144 /* Install signal handlers */
145 struct sigaction act;
146 act.sa_flags = SA_RESTART;
147 sigemptyset(&(act.sa_mask));
149 /* SIGCHLD -- clean up after zombies */
150 act.sa_handler = waitforzombies;
151 if (sigaction(SIGCHLD, &act, NULL) == -1) {
152 log_msg("Installing signal handler failed\n");
156 /* All other signals are handled via the GLib main loop */
157 if (pipe(eventpipe) == -1) {
158 log_perror(errno, "Creating event pipe failed");
161 act.sa_handler = handle_signal;
162 if (sigaction(SIGHUP, &act, NULL) == -1) {
163 log_msg("Installing signal handler failed\n");
170 dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
171 &dbus_glib_osso_browser_object_info);
173 /* Get a connection to the D-Bus session bus */
174 ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
175 if (!ctx.session_bus) {
176 log_msg("Couldn't get a D-Bus bus connection\n");
179 ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
180 "org.freedesktop.DBus", "/org/freedesktop/DBus",
181 "org.freedesktop.DBus");
182 if (!ctx.dbus_proxy) {
183 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
187 /* Get the org.maemo.garage.browser-switchboard name from D-Bus, as
188 a form of locking to ensure that not more than one
189 browser-switchboard process is active at any time. With
190 DBUS_NAME_FLAG_DO_NOT_QUEUE set and DBUS_NAME_FLAG_REPLACE_EXISTING
191 not set, getting the name succeeds if and only if no other
192 process owns the name. */
193 if (!dbus_g_proxy_call(ctx.dbus_proxy, "RequestName", &error,
194 G_TYPE_STRING, "org.maemo.garage.browser-switchboard",
195 G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
197 G_TYPE_UINT, &reqname_result,
199 log_msg("Couldn't acquire browser-switchboard lock: %s\n",
203 if (reqname_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
204 log_msg("Another browser-switchboard already running\n");
208 /* Get a connection to the D-Bus system bus */
209 ctx.system_bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
210 if (!ctx.system_bus) {
211 log_msg("Couldn't get a D-Bus system bus connection\n");
214 ctx.dbus_system_proxy = dbus_g_proxy_new_for_name(ctx.system_bus,
215 "org.freedesktop.DBus", "/org/freedesktop/DBus",
216 "org.freedesktop.DBus");
217 if (!ctx.dbus_system_proxy) {
218 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
222 dbus_request_osso_browser_name(&ctx);
224 /* Register ourselves to handle the osso_browser D-Bus methods */
225 obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
226 obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
227 obj_osso_browser_sys = g_object_new(OSSO_BROWSER_TYPE, NULL);
228 obj_osso_browser_sys_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
229 dbus_g_connection_register_g_object(ctx.session_bus,
230 "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
231 dbus_g_connection_register_g_object(ctx.session_bus,
232 "/com/nokia/osso_browser/request",
233 G_OBJECT(obj_osso_browser_req));
234 dbus_g_connection_register_g_object(ctx.system_bus,
235 "/com/nokia/osso_browser",
236 G_OBJECT(obj_osso_browser_sys));
237 dbus_g_connection_register_g_object(ctx.system_bus,
238 "/com/nokia/osso_browser/request",
239 G_OBJECT(obj_osso_browser_sys_req));
241 mainloop = g_main_loop_new(NULL, FALSE);
243 /* Hook up event pipe to the main loop */
244 if (ctx.continuous_mode) {
245 fdevents = g_source_new(&fdevents_funcs, sizeof(GSource));
246 g_source_set_priority(fdevents, G_PRIORITY_HIGH);
247 fdevents_pfd.fd = eventpipe[0];
248 fdevents_pfd.events = G_IO_IN;
249 fdevents_pfd.revents = 0;
250 g_source_add_poll(fdevents, &fdevents_pfd);
251 g_source_attach(fdevents, NULL);
254 log_msg("Starting main loop\n");
255 g_main_loop_run(mainloop);
256 log_msg("Main loop completed\n");