c907c1c2a7e6760a4d0df6fd22d615a851bcb038
[flashlight-appl] / src / flashlight_applet.c
1 /*
2  *  Flashlight applet (widget) for Maemo.
3  *  Copyright (C) 2009, 2010 Roman Moravcik
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <hildon/hildon.h>
29 #include <glib/gi18n-lib.h>
30 #include <libhal.h>
31 #include <dbus/dbus.h>
32
33 #include "flashlight_applet.h"
34 #include "flashlight_lib.h"
35
36 #define MSG_FLASHLIGHT_ON _("On")
37 #define MSG_FLASHLIGHT_OFF _("Off")
38
39 #define ICON_FLASHLIGHT_ON "statusarea_flashlight_on"
40 #define ICON_FLASHLIGHT_OFF "statusarea_flashlight_off"
41
42 #define CAM_COVER_UDI "/org/freedesktop/Hal/devices/platform_cam_shutter"
43 #define CAM_COVER_STATE "button.state.value"
44
45 #define FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,   \
46                             TYPE_FLASHLIGHT_STATUS_PLUGIN, FlashlightPluginPrivate))
47
48 struct _FlashlightPluginPrivate
49 {
50         GtkWidget *button;
51         guint status_timer;
52
53         FlashlightContext_t *flashlight;
54         DBusConnection *dbus_connection;
55         LibHalContext *hal;
56 };
57
58 HD_DEFINE_PLUGIN_MODULE (FlashlightPlugin, flashlight_status_plugin, HD_TYPE_STATUS_MENU_ITEM)
59
60 static gboolean flashlight_status_plugin_status (gpointer data);
61 static void flashlight_status_plugin_finalize (GObject *object);
62
63 static void
64 flashlight_status_plugin_show_notification (FlashlightPlugin *plugin,
65                                             const gchar *text)
66 {
67         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
68         GtkWidget *banner;
69
70         g_return_if_fail (priv);
71
72         banner = hildon_banner_show_information (GTK_WIDGET (priv->button), NULL, text);
73         hildon_banner_set_timeout (HILDON_BANNER (banner), 3000);
74 }
75
76 static void
77 flashlight_status_plugin_enable (FlashlightPlugin *plugin,
78                                  gboolean enable)
79 {
80         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
81
82         g_return_if_fail (priv);
83
84         if (enable) {
85                 if (flashlight_open (priv->flashlight, "/dev/video0") < 0) {
86                         flashlight_status_plugin_show_notification (plugin,
87                                 _("Unable to initialize flashlight.\nCamera in use by another application."));
88                         flashlight_close (priv->flashlight);
89                         return;
90                 }
91
92                 if (flashlight_set_intensity (priv->flashlight, 1) < 0) {
93                         flashlight_status_plugin_show_notification (plugin,
94                                 _("Unable to turn on flashlight."));
95                         flashlight_close (priv->flashlight);
96                         return;
97                 }
98
99                 hildon_button_set_value (HILDON_BUTTON (priv->button), MSG_FLASHLIGHT_ON);
100                 hildon_button_set_image (HILDON_BUTTON (priv->button),
101                                          gtk_image_new_from_icon_name (ICON_FLASHLIGHT_ON, -1));
102
103                 /* check status of controller every 1s */
104                 priv->status_timer = g_timeout_add (1000, flashlight_status_plugin_status, plugin);
105         } else {
106                 /* cancel status timer */
107                 if (priv->status_timer) {
108                         g_source_remove (priv->status_timer);
109                         priv->status_timer = 0;
110                 }
111
112                 /* set intensity to 0 */
113                 if (flashlight_set_intensity (priv->flashlight, 0) < 0) {
114                         flashlight_status_plugin_show_notification (plugin,
115                                 _("Unable to turn off flashlight."));
116                         return;
117                 }
118
119                 if (flashlight_close (priv->flashlight) < 0) {
120                         return;
121                 }
122
123                 hildon_button_set_value (HILDON_BUTTON (priv->button), MSG_FLASHLIGHT_OFF);
124                 hildon_button_set_image (HILDON_BUTTON (priv->button),
125                                          gtk_image_new_from_icon_name (ICON_FLASHLIGHT_OFF, -1));
126         }
127 }
128
129 static void
130 flashlight_status_plugin_on_hal_property_modified (LibHalContext *ctx,
131                                                    const char *udi,
132                                                    const char *key,
133                                                    dbus_bool_t is_removed,
134                                                    dbus_bool_t is_added)
135 {
136         FlashlightPlugin *plugin = libhal_ctx_get_user_data (ctx);
137         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
138         gboolean is_open;
139         int intensity = 0;
140
141         g_return_if_fail (priv);
142
143         if (strcmp (udi, CAM_COVER_UDI) != 0)
144                 return;
145
146         if (strcmp (key, CAM_COVER_STATE) != 0)
147                 return;
148
149         is_open = !libhal_device_get_property_bool (ctx, udi, key, NULL);
150
151         if (is_open) {
152                 /* show widget */
153                 gtk_widget_show_all (GTK_WIDGET (plugin));
154         } else {
155                 /* turn off flashlight if flashlight is enabled */
156                 if (flashlight_get_intensity (priv->flashlight, &intensity) == 0) {
157                         if (intensity > 0) {
158                                 flashlight_status_plugin_enable (plugin, FALSE);
159                         }
160                 }
161
162                 /* hide widget */
163                 gtk_widget_hide_all (GTK_WIDGET (plugin));
164         }
165 }
166
167 static gboolean
168 flashlight_status_plugin_status (gpointer data)
169 {
170         FlashlightPlugin *plugin = data;
171         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
172         int status = 0;
173
174         if (flashlight_get_status (priv->flashlight, &status) < 0) {
175                 flashlight_status_plugin_show_notification (plugin,
176                                                             _("Unable to read status from driver."));
177                 return FALSE;
178         }
179
180         /* ops, something is wrong */
181         if (status) {
182                 /* turn off flashlight */
183                 flashlight_status_plugin_enable (plugin, FALSE);
184
185                 if (status & FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT) {
186                         flashlight_status_plugin_show_notification (plugin,
187                                 _("Short-circut fault detected!\nTurning off flashlight."));
188                 } else if (status & FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT) {
189                         flashlight_status_plugin_show_notification (plugin,
190                                 _("Overtemperature fault detected!\nTurning off flashlight."));
191                 } else if (status & FLASHLIGHT_STATUS_TIMEOUT_FAULT) {
192                         flashlight_status_plugin_show_notification (plugin,
193                                 _("Timeout fault detected!\nTurning off flashlight."));
194                 } else if (status & FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT) {
195                         flashlight_status_plugin_show_notification (plugin,
196                                 _("Overvoltage fault detected!\nTurning off flashlight."));
197                 }
198         }
199
200         return TRUE;
201 }
202
203 static void
204 flashlight_status_plugin_on_clicked (HildonButton *button,
205                                      gpointer data)
206 {
207         FlashlightPlugin *plugin = data;
208         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
209
210         g_return_if_fail (priv);
211
212         if (!strcmp (hildon_button_get_value (button), MSG_FLASHLIGHT_ON)) {
213                 flashlight_status_plugin_enable (plugin, FALSE);
214         } else {
215                 flashlight_status_plugin_enable (plugin, TRUE);
216         }
217 }
218
219 static GtkWidget *
220 flashlight_status_plugin_ui (FlashlightPlugin *plugin)
221 {
222         GtkWidget *button;
223
224         g_return_val_if_fail (plugin, NULL);
225
226         button = hildon_button_new (HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
227         gtk_button_set_alignment (GTK_BUTTON (button), 0.0, 0.5);
228         hildon_button_set_title (HILDON_BUTTON (button), _("Flashlight"));
229         hildon_button_set_value (HILDON_BUTTON (button), MSG_FLASHLIGHT_OFF);
230         hildon_button_set_image (HILDON_BUTTON (button),
231                                  gtk_image_new_from_icon_name (ICON_FLASHLIGHT_OFF, -1));
232         hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_LEFT);
233
234         g_signal_connect (button, "clicked",
235                                         G_CALLBACK (flashlight_status_plugin_on_clicked), plugin);
236
237         return button;
238 }
239
240 static void
241 flashlight_status_plugin_class_init (FlashlightPluginClass *class)
242 {
243         GObjectClass *object_class = G_OBJECT_CLASS (class);
244
245         object_class->finalize = flashlight_status_plugin_finalize;
246
247         g_type_class_add_private (class, sizeof (FlashlightPluginPrivate));
248 }
249
250 static void
251 flashlight_status_plugin_class_finalize (FlashlightPluginClass *class)
252 {
253 }
254
255 static void
256 flashlight_status_plugin_init (FlashlightPlugin *plugin)
257 {
258         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
259         DBusConnection *dbus_connection;
260         DBusError error;
261
262         /* initialize dbus */
263         dbus_error_init (&error);
264         priv->dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
265         if (dbus_error_is_set (&error)) {
266                 g_critical ("flashlight_status_plugin_init: Could not get the system DBus connection, %s",
267                             error.message);
268                 dbus_error_free (&error);
269                 return;
270         }
271
272         /* initialize flashlight */
273         if (flashlight_init (&priv->flashlight) < 0) {
274                 g_critical ("flashlight_status_plugin_init: Unable to create Flashlight context\n");
275                 return;
276         }
277
278         /* initialize hal */
279         priv->hal = libhal_ctx_new ();
280         if (!priv->hal) {
281                 g_critical ("flashlight_status_plugin_init: Unable to create HAL context\n");
282                 return;
283         }
284
285         libhal_ctx_set_dbus_connection (priv->hal, priv->dbus_connection);
286         libhal_ctx_set_user_data (priv->hal, plugin);
287         libhal_ctx_set_device_property_modified (priv->hal,
288                                                  flashlight_status_plugin_on_hal_property_modified);
289
290         if (!libhal_ctx_init (priv->hal, &error)) {
291                 if (dbus_error_is_set (&error)) {
292                         g_critical ("Could not initialize the HAL context, %s",
293                                     error.message);
294                         dbus_error_free (&error);
295                 } else {
296                         g_critical ("Could not initialize the HAL context, "
297                                     "no error, is hald running?");
298                 }
299                 libhal_ctx_set_user_data (priv->hal, NULL);
300                 libhal_ctx_free (priv->hal);
301                 priv->hal = NULL;
302                 return;
303         }
304
305         libhal_device_add_property_watch (priv->hal, CAM_COVER_UDI, NULL);
306
307         /* create plugin ui */
308         priv->button = flashlight_status_plugin_ui (plugin);
309         gtk_container_add (GTK_CONTAINER (plugin), priv->button);
310
311         /* show widget if camera cover is open */
312         if ( !libhal_device_get_property_bool (priv->hal, CAM_COVER_UDI, CAM_COVER_STATE, NULL))
313                 gtk_widget_show_all (GTK_WIDGET (plugin));
314
315 }
316
317 static void
318 flashlight_status_plugin_finalize (GObject *object)
319 {
320         FlashlightPlugin *plugin = FLASHLIGHT_STATUS_PLUGIN (object);
321         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
322
323         g_return_if_fail (priv);
324
325         /* deinitialize hal */
326         if (priv->hal) {
327                 libhal_device_remove_property_watch (priv->hal, CAM_COVER_UDI, NULL);
328                 libhal_ctx_set_user_data (priv->hal, NULL);
329                 libhal_ctx_shutdown (priv->hal, NULL);
330                 libhal_ctx_free (priv->hal);
331         }
332         priv->hal = NULL;
333
334         /* unreference dbus connection */
335         if (priv->dbus_connection) {
336                 dbus_connection_unref (priv->dbus_connection);
337         }
338         priv->dbus_connection = NULL;
339
340         /* cancel status timer */
341         if (priv->status_timer) {
342                 g_source_remove (priv->status_timer);
343         }
344
345         /* deinitialize flashlight */
346         if (priv->flashlight) {
347                 flashlight_deinit (priv->flashlight);
348         }
349
350         G_OBJECT_CLASS (flashlight_status_plugin_parent_class)->finalize (object);
351 }