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_sys;
135 OssoBrowser *obj_osso_browser_req, *obj_osso_browser_sys_req;
136 OssoBrowser *obj_osso_browser_root, *obj_osso_browser_sys_root;
138 GError *error = NULL;
144 if (ctx.continuous_mode) {
145 /* Install signal handlers */
146 struct sigaction act;
147 act.sa_flags = SA_RESTART;
148 sigemptyset(&(act.sa_mask));
150 /* SIGCHLD -- clean up after zombies */
151 act.sa_handler = waitforzombies;
152 if (sigaction(SIGCHLD, &act, NULL) == -1) {
153 log_msg("Installing signal handler failed\n");
157 /* All other signals are handled via the GLib main loop */
158 if (pipe(eventpipe) == -1) {
159 log_perror(errno, "Creating event pipe failed");
162 act.sa_handler = handle_signal;
163 if (sigaction(SIGHUP, &act, NULL) == -1) {
164 log_msg("Installing signal handler failed\n");
171 dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
172 &dbus_glib_osso_browser_object_info);
174 /* Get a connection to the D-Bus session bus */
175 ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
176 if (!ctx.session_bus) {
177 log_msg("Couldn't get a D-Bus bus connection\n");
180 ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
181 "org.freedesktop.DBus", "/org/freedesktop/DBus",
182 "org.freedesktop.DBus");
183 if (!ctx.dbus_proxy) {
184 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
188 /* Get the org.maemo.garage.browser-switchboard name from D-Bus, as
189 a form of locking to ensure that not more than one
190 browser-switchboard process is active at any time. With
191 DBUS_NAME_FLAG_DO_NOT_QUEUE set and DBUS_NAME_FLAG_REPLACE_EXISTING
192 not set, getting the name succeeds if and only if no other
193 process owns the name. */
194 if (!dbus_g_proxy_call(ctx.dbus_proxy, "RequestName", &error,
195 G_TYPE_STRING, "org.maemo.garage.browser-switchboard",
196 G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
198 G_TYPE_UINT, &reqname_result,
200 log_msg("Couldn't acquire browser-switchboard lock: %s\n",
204 if (reqname_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
205 log_msg("Another browser-switchboard already running\n");
209 /* Get a connection to the D-Bus system bus */
210 ctx.system_bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
211 if (!ctx.system_bus) {
212 log_msg("Couldn't get a D-Bus system bus connection\n");
215 ctx.dbus_system_proxy = dbus_g_proxy_new_for_name(ctx.system_bus,
216 "org.freedesktop.DBus", "/org/freedesktop/DBus",
217 "org.freedesktop.DBus");
218 if (!ctx.dbus_system_proxy) {
219 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
223 dbus_request_osso_browser_name(&ctx);
225 /* Register ourselves to handle the osso_browser D-Bus methods */
226 obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
227 obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
228 obj_osso_browser_root = g_object_new(OSSO_BROWSER_TYPE, NULL);
229 obj_osso_browser_sys = g_object_new(OSSO_BROWSER_TYPE, NULL);
230 obj_osso_browser_sys_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
231 obj_osso_browser_sys_root = g_object_new(OSSO_BROWSER_TYPE, NULL);
232 dbus_g_connection_register_g_object(ctx.session_bus,
233 "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
234 dbus_g_connection_register_g_object(ctx.session_bus,
235 "/com/nokia/osso_browser/request",
236 G_OBJECT(obj_osso_browser_req));
237 dbus_g_connection_register_g_object(ctx.session_bus,
238 "/", G_OBJECT(obj_osso_browser_root));
239 dbus_g_connection_register_g_object(ctx.system_bus,
240 "/com/nokia/osso_browser",
241 G_OBJECT(obj_osso_browser_sys));
242 dbus_g_connection_register_g_object(ctx.system_bus,
243 "/com/nokia/osso_browser/request",
244 G_OBJECT(obj_osso_browser_sys_req));
245 dbus_g_connection_register_g_object(ctx.system_bus,
246 "/", G_OBJECT(obj_osso_browser_sys_root));
248 mainloop = g_main_loop_new(NULL, FALSE);
250 /* Hook up event pipe to the main loop */
251 if (ctx.continuous_mode) {
252 fdevents = g_source_new(&fdevents_funcs, sizeof(GSource));
253 g_source_set_priority(fdevents, G_PRIORITY_HIGH);
254 fdevents_pfd.fd = eventpipe[0];
255 fdevents_pfd.events = G_IO_IN;
256 fdevents_pfd.revents = 0;
257 g_source_add_poll(fdevents, &fdevents_pfd);
258 g_source_attach(fdevents, NULL);
261 log_msg("Starting main loop\n");
262 g_main_loop_run(mainloop);
263 log_msg("Main loop completed\n");