2 * main.c -- config file parsing and main loop 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"
34 #include "configfile.h"
36 struct swb_context ctx;
38 static void set_config_defaults(struct swb_context *ctx) {
41 free(ctx->other_browser_cmd);
42 ctx->continuous_mode = 0;
43 ctx->default_browser_launcher = NULL;
44 ctx->other_browser_cmd = NULL;
47 static void waitforzombies(int signalnum) {
48 while (waitpid(-1, NULL, WNOHANG) > 0)
49 printf("Waited for a zombie\n");
52 static void read_config(int signalnum) {
54 int continuous_mode_seen = 0;
55 struct swb_config_line line;
56 char *default_browser = NULL;
58 set_config_defaults(&ctx);
60 if (!(fp = open_config_file()))
63 /* Parse the config file
64 TODO: should we handle errors differently than EOF? */
65 if (!parse_config_file_begin())
67 while (!parse_config_file_line(fp, &line)) {
69 if (!strcmp(line.key, "continuous_mode")) {
70 if (!continuous_mode_seen) {
71 ctx.continuous_mode = atoi(line.value);
72 continuous_mode_seen = 1;
75 } else if (!strcmp(line.key, "default_browser")) {
77 default_browser = line.value;
78 } else if (!strcmp(line.key, "other_browser_cmd")) {
79 if (!ctx.other_browser_cmd)
80 ctx.other_browser_cmd = line.value;
82 /* Don't need this line's contents */
88 parse_config_file_end();
90 printf("continuous_mode: %d\n", ctx.continuous_mode);
91 printf("default_browser: '%s'\n", default_browser?default_browser:"NULL");
92 printf("other_browser_cmd: '%s'\n", ctx.other_browser_cmd?ctx.other_browser_cmd:"NULL");
97 update_default_browser(&ctx, default_browser);
98 free(default_browser);
103 OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
105 GError *error = NULL;
109 if (ctx.continuous_mode) {
110 /* Install signal handlers */
111 struct sigaction act;
112 act.sa_flags = SA_RESTART;
113 sigemptyset(&(act.sa_mask));
115 /* SIGCHLD -- clean up after zombies */
116 act.sa_handler = waitforzombies;
117 if (sigaction(SIGCHLD, &act, NULL) == -1) {
118 printf("Installing signal handler failed\n");
122 /* SIGHUP -- reread config file */
123 act.sa_handler = read_config;
124 if (sigaction(SIGHUP, &act, NULL) == -1) {
125 printf("Installing signal handler failed\n");
132 dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
133 &dbus_glib_osso_browser_object_info);
135 /* Get a connection to the D-Bus session bus */
136 ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
137 if (!ctx.session_bus) {
138 printf("Couldn't get a D-Bus bus connection\n");
141 ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
142 "org.freedesktop.DBus", "/org/freedesktop/DBus",
143 "org.freedesktop.DBus");
144 if (!ctx.dbus_proxy) {
145 printf("Couldn't get an org.freedesktop.DBus proxy\n");
149 dbus_request_osso_browser_name(&ctx);
151 /* Register ourselves to handle the osso_browser D-Bus methods */
152 obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
153 obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
154 dbus_g_connection_register_g_object(ctx.session_bus,
155 "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
156 dbus_g_connection_register_g_object(ctx.session_bus,
157 "/com/nokia/osso_browser/request",
158 G_OBJECT(obj_osso_browser_req));
160 mainloop = g_main_loop_new(NULL, FALSE);
161 printf("Starting main loop\n");
162 g_main_loop_run(mainloop);
163 printf("Main loop completed\n");