daemon: ignore SIGCHLD to prevent zombies
[espeaktime] / src / daemon.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <glib.h>
5 #include <dbus/dbus-glib.h>
6 #include <dbus/dbus-glib-lowlevel.h>
7 #include <hal/libhal.h>
8 #include "config.h"
9
10 #include <mce/dbus-names.h>
11 #include <mce/mode-names.h>
12
13 #define HAL_COND_BUTTONPRESSED  "ButtonPressed"
14 #define HAL_BUTTON_POWER        "power"
15 #define HAL_INPUTDEV_PATH       "/org/freedesktop/Hal/devices/computer_logicaldev_input"
16
17 struct app_data {
18         DBusGConnection *bus;
19         LibHalContext *hal;
20         GConfClient *gconf;
21
22         gboolean mode_locked;
23         gboolean display_on;
24         time_t last_press;
25         unsigned press_count;
26 };
27
28 static void speak_time(struct app_data *app)
29 {
30         struct espeaktime_settings cfg;
31         cfg_read(app->gconf, &cfg);
32         cfg_speak(&cfg, FALSE);
33         cfg_free(&cfg);
34 }
35
36 static void sig_tklock_mode(DBusGProxy *proxy, const char *mode, gpointer user_data)
37 {
38         struct app_data *app = user_data;
39         g_debug("sig_tklock_mode [%s]", mode);
40         app->mode_locked = !strcmp(mode, MCE_TK_LOCKED);
41 }
42
43 static void sig_display_status(DBusGProxy *proxy, const char *status, gpointer user_data)
44 {
45         struct app_data *app = user_data;
46         g_debug("sig_display_status [%s]", status);
47         app->display_on = !strcmp(status, MCE_DISPLAY_ON_STRING);
48
49         /* Double-pressing the button at a normal rate will usually result
50          * in the two ButtonPress events being received before the display_status==on
51          * event.  Check here if that's the case.
52          */
53         if (app->display_on && app->press_count > 1 && time(NULL) - app->last_press <= 1)
54                 speak_time(app);
55 }
56
57 static void debug_log(const gchar *log_domain,
58         GLogLevelFlags log_level, const gchar *message, gpointer unused_data)
59 {
60         g_print("# %s\n", message);
61 }
62
63 static void power_button(struct app_data *app)
64 {
65         time_t now;
66
67         time(&now);
68         if (now - app->last_press > 1)
69                 app->press_count = 0;
70         app->press_count++;
71         app->last_press = now;
72
73         g_debug("power button: count=%d", app->press_count);
74         if (app->mode_locked && app->display_on)
75                 speak_time(app);
76 }
77
78 /**
79  * Call a method with no input arguments and one string output argument.
80  * Return the allocaed result string on success, NULL on failure.
81  */
82 static char *mce_call_getstr(DBusGProxy *proxy, const char *method)
83 {
84         GError *err = NULL;
85         char *s = NULL;
86
87         if (dbus_g_proxy_call(proxy, method, &err,
88                         G_TYPE_INVALID,
89                         G_TYPE_STRING, &s, G_TYPE_INVALID))
90                 return s;
91
92         g_error("Couldn't call MCE (%s): %s\n", method, err->message);
93         g_error_free(err);
94         return NULL;
95 }
96
97 static gboolean prefill_status(struct app_data *app)
98 {
99         DBusGProxy *mce_req;
100         char *mode, *status;
101
102         mce_req = dbus_g_proxy_new_for_name(app->bus, MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF);
103         g_assert(mce_req);
104
105         mode = mce_call_getstr(mce_req, MCE_TKLOCK_MODE_GET);
106         status = mce_call_getstr(mce_req, MCE_DISPLAY_STATUS_GET);
107         if (mode)
108                 sig_tklock_mode(NULL, mode, app);
109         if (status)
110                 sig_display_status(NULL, status, app);
111         g_free(mode);
112         g_free(status);
113         g_object_unref(mce_req);
114
115         return mode && status;
116 }
117
118 static void connect_signals(struct app_data *app)
119 {
120         DBusGProxy *mce_sig;
121
122         mce_sig = dbus_g_proxy_new_for_name(app->bus, MCE_SERVICE, MCE_SIGNAL_PATH, MCE_SIGNAL_IF);
123         g_assert(mce_sig);
124         dbus_g_proxy_add_signal(mce_sig, MCE_TKLOCK_MODE_SIG, G_TYPE_STRING, G_TYPE_INVALID);
125         dbus_g_proxy_add_signal(mce_sig, MCE_DISPLAY_SIG, G_TYPE_STRING, G_TYPE_INVALID);
126         dbus_g_proxy_connect_signal(mce_sig, MCE_TKLOCK_MODE_SIG, G_CALLBACK(sig_tklock_mode), app, NULL);
127         dbus_g_proxy_connect_signal(mce_sig, MCE_DISPLAY_SIG, G_CALLBACK(sig_display_status), app, NULL);
128 }
129
130
131 static void device_condition(LibHalContext *ctx,
132         const char *udi,
133         const char *condition_name,
134         const char *condition_detail)
135 {
136         g_debug("device_condition: name [%s] detail [%s]",
137                 condition_name, condition_detail);
138         if (!strcmp(condition_name, HAL_COND_BUTTONPRESSED) && !strcmp(condition_detail, HAL_BUTTON_POWER)) {
139                 struct app_data *app = libhal_ctx_get_user_data(ctx);
140                 power_button(app);
141         }
142 }
143
144
145 static gboolean init_hal(struct app_data *app)
146 {
147         DBusError err;
148
149         app->hal = libhal_ctx_new();
150         if (!app->hal) {
151                 g_error("Couldn't get a HAL context");
152                 return FALSE;
153         }
154         libhal_ctx_set_dbus_connection(app->hal,
155                 dbus_g_connection_get_connection(app->bus));
156         libhal_ctx_set_device_condition(app->hal, device_condition);
157         libhal_ctx_set_user_data(app->hal, app);
158
159         dbus_error_init(&err);
160         if (!libhal_ctx_init(app->hal, &err)) {
161                 g_error("Couldn't initialize HAL context: %s", err.message);
162                 dbus_error_free(&err);
163                 libhal_ctx_free(app->hal);
164                 return FALSE;
165         }
166
167         if (!libhal_device_add_property_watch(app->hal, HAL_INPUTDEV_PATH, &err)) {
168                 g_error("Couldn't add HAL watch: %s", err.message);
169                 dbus_error_free(&err);
170                 libhal_ctx_free(app->hal);
171         }
172         return TRUE;
173 }
174
175 int main(int argc, char *argv[])
176 {
177         GMainLoop *loop;
178         GError *err = NULL;
179         struct app_data app;
180
181         if (argc > 1 && !strcmp(argv[1], "-v"))
182                 g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, debug_log, NULL);
183
184         g_debug("init");
185         memset(&app, 0, sizeof(app));
186         signal(SIGCHLD, SIG_IGN);
187
188         g_type_init();
189         loop = g_main_loop_new(NULL, FALSE);
190
191         app.bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &err);
192         if (!app.bus) {
193                 g_error("Couldn't get DBUS connection: %s\n", err->message);
194                 g_error_free(err);
195                 return 1;
196         }
197
198         app.gconf = gconf_client_get_default();
199         g_assert(app.gconf);
200
201         if (!init_hal(&app))
202                 return 1;
203
204         if (!prefill_status(&app))
205                 return 1;
206         connect_signals(&app);
207
208         g_debug("running");
209         g_main_loop_run(loop);
210         dbus_g_connection_unref(app.bus);
211         return 0;
212 }