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,
28 #include <sys/types.h>
30 #include <dbus/dbus-glib.h>
32 #include "browser-switchboard.h"
34 #include "dbus-server-bindings.h"
36 #define DEFAULT_HOMEDIR "/home/user"
37 #define CONFIGFILE_LOC "/.config/browser-switchboard"
40 struct swb_context ctx;
42 static void set_config_defaults(struct swb_context * ctx) {
45 free(ctx->other_browser_cmd);
46 ctx->continuous_mode = 0;
47 ctx->default_browser_launcher = NULL;
48 ctx->other_browser_cmd = NULL;
51 static void waitforzombies(int signalnum) {
52 while (waitpid(-1, NULL, WNOHANG) > 0)
53 printf("Waited for a zombie\n");
56 static void read_config(int signalnum) {
63 char * default_browser = NULL;
66 set_config_defaults(&ctx);
68 if (!(homedir = getenv("HOME")))
69 homedir = DEFAULT_HOMEDIR;
70 len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
71 if (!(configfile = calloc(len, sizeof(char))))
73 strncpy(configfile, homedir, strlen(homedir));
74 strncat(configfile, CONFIGFILE_LOC, strlen(CONFIGFILE_LOC));
76 if (!(fp = fopen(configfile, "r")))
79 /* Read in the config file one line at a time and parse it
80 XXX doesn't deal with lines longer than MAXLINE */
81 while (fgets(buf, MAXLINE, fp)) {
85 /* look for the = in the line */
86 if (!(tmp = strchr(buf, '=')))
89 /* split the line into parameter (before =, in buf) and
90 value (after =, in value) */
91 if (!(value = calloc(strlen(tmp+1)+1, sizeof(char))))
93 strncpy(value, tmp+1, strlen(tmp+1));
94 value[strlen(tmp+1)] = '\0';
95 /* scribble over the = in buf with a \0 -- that makes buf
96 just the parameter name */
98 /* if we find a newline in value, replace that with a \0 too */
99 if ((tmp = strchr(value, '\n')))
102 if (!strcmp(buf, "continuous_mode")) {
103 ctx.continuous_mode = atoi(value);
105 } else if (!strcmp(buf, "default_browser")) {
106 if (!default_browser)
107 default_browser = value;
108 } else if (!strcmp(buf, "other_browser_cmd")) {
109 if (!ctx.other_browser_cmd)
110 ctx.other_browser_cmd = value;
112 /* Don't need this line's contents */
118 printf("continuous_mode: %d\n", ctx.continuous_mode);
119 printf("default_browser: '%s'\n", default_browser?default_browser:"NULL");
120 printf("other_browser_cmd: '%s'\n", ctx.other_browser_cmd?ctx.other_browser_cmd:"NULL");
125 update_default_browser(&ctx, default_browser);
127 free(default_browser);
133 GMainLoop * mainloop;
134 GError * error = NULL;
138 if (ctx.continuous_mode) {
139 struct sigaction act;
140 act.sa_flags = SA_RESTART;
142 act.sa_handler = waitforzombies;
143 if (sigaction(SIGCHLD, &act, NULL) == -1) {
144 printf("Installing signal handler failed\n");
148 act.sa_handler = read_config;
149 if (sigaction(SIGHUP, &act, NULL) == -1) {
150 printf("Installing signal handler failed\n");
157 dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
158 &dbus_glib_osso_browser_object_info);
160 ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
161 if (!ctx.session_bus) {
162 printf("Couldn't get a D-Bus bus connection\n");
165 ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
166 "org.freedesktop.DBus", "/org/freedesktop/DBus",
167 "org.freedesktop.DBus");
168 if (!ctx.dbus_proxy) {
169 printf("Couldn't get an org.freedesktop.DBus proxy\n");
173 dbus_request_osso_browser_name(&ctx);
175 obj = g_object_new(OSSO_BROWSER_TYPE, NULL);
176 dbus_g_connection_register_g_object(ctx.session_bus,
177 "/com/nokia/osso_browser", G_OBJECT(obj));
178 dbus_g_connection_register_g_object(ctx.session_bus,
179 "/com/nokia/osso_browser/request", G_OBJECT(obj));
181 mainloop = g_main_loop_new(NULL, FALSE);
182 printf("Starting main loop\n");
183 g_main_loop_run(mainloop);
184 printf("Main loop completed\n");