944c40c99f0741462ab72a9b1b5bdb852a647b09
[flashlight-appl] / src / flashlight_applet.c
1 /*
2  *  Flashlight applet (widget) for Maemo.
3  *  Copyright (C) 2009 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         LibHalContext *hal;
55 };
56
57 HD_DEFINE_PLUGIN_MODULE (FlashlightPlugin, flashlight_status_plugin, HD_TYPE_STATUS_MENU_ITEM)
58
59 static gboolean flashlight_status_plugin_status (gpointer data);
60 static void flashlight_status_plugin_finalize (GObject *object);
61
62 static void
63 flashlight_status_plugin_show_notification (FlashlightPlugin *plugin,
64                                             const gchar *text)
65 {
66         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
67         GtkWidget *banner;
68
69         g_return_if_fail (priv);
70
71         banner = hildon_banner_show_information (GTK_WIDGET (priv->button), NULL, text);
72         hildon_banner_set_timeout (HILDON_BANNER (banner), 3000);
73 }
74
75 static void
76 flashlight_status_plugin_enable (FlashlightPlugin *plugin,
77                                  gboolean enable)
78 {
79         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
80
81         g_return_if_fail (priv);
82
83         if (enable) {
84                 if (flashlight_open (priv->flashlight, "/dev/video0") < 0) {
85                         flashlight_status_plugin_show_notification (plugin,
86                                 _("Unable to initialize flashlight.\nCamera in use by another application."));
87                         flashlight_close (priv->flashlight);
88                         return;
89                 }
90
91                 if (flashlight_set_intensity (priv->flashlight, 1) < 0) {
92                         flashlight_status_plugin_show_notification (plugin,
93                                 _("Unable to turn on flashlight."));
94                         flashlight_close (priv->flashlight);
95                         return;
96                 }
97
98                 hildon_button_set_value (HILDON_BUTTON (priv->button), MSG_FLASHLIGHT_ON);
99                 hildon_button_set_image (HILDON_BUTTON (priv->button),
100                                          gtk_image_new_from_icon_name (ICON_FLASHLIGHT_ON, -1));
101
102                 /* check status of controller every 1s */
103                 priv->status_timer = g_timeout_add (1000, flashlight_status_plugin_status, plugin);
104         } else {
105                 /* cancel status timer */
106                 if (priv->status_timer) {
107                         g_source_remove (priv->status_timer);
108                         priv->status_timer = 0;
109                 }
110
111                 /* set intensity to 0 */
112                 if (flashlight_set_intensity (priv->flashlight, 0) < 0) {
113                         flashlight_status_plugin_show_notification (plugin,
114                                 _("Unable to turn off flashlight."));
115                         return;
116                 }
117
118                 if (flashlight_close (priv->flashlight) < 0) {
119                         return;
120                 }
121
122                 hildon_button_set_value (HILDON_BUTTON (priv->button), MSG_FLASHLIGHT_OFF);
123                 hildon_button_set_image (HILDON_BUTTON (priv->button),
124                                          gtk_image_new_from_icon_name (ICON_FLASHLIGHT_OFF, -1));
125         }
126 }
127
128 static void
129 flashlight_status_plugin_on_hal_property_modified (LibHalContext *ctx,
130                                                    const char *udi,
131                                                    const char *key,
132                                                    dbus_bool_t is_removed,
133                                                    dbus_bool_t is_added)
134 {
135         FlashlightPlugin *plugin = libhal_ctx_get_user_data (ctx);
136         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
137         gboolean is_open;
138         int intensity = 0;
139
140         g_return_if_fail (priv);
141
142         if (strcmp (udi, CAM_COVER_UDI) != 0)
143                 return;
144
145         if (strcmp (key, CAM_COVER_STATE) != 0)
146                 return;
147
148         is_open = !libhal_device_get_property_bool (ctx, udi, key, NULL);
149
150         if (is_open) {
151                 /* show widget */
152                 gtk_widget_show_all (GTK_WIDGET (plugin));
153         } else {
154                 /* turn off flashlight if flashlight is enabled */
155                 if (flashlight_get_intensity (priv->flashlight, &intensity) == 0) {
156                         if (intensity > 0) {
157                                 flashlight_status_plugin_enable (plugin, FALSE);
158                         }
159                 }
160
161                 /* hide widget */
162                 gtk_widget_hide_all (GTK_WIDGET (plugin));
163         }
164 }
165
166 static gboolean
167 flashlight_status_plugin_status (gpointer data)
168 {
169         FlashlightPlugin *plugin = data;
170         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
171         int status = 0;
172
173         if (flashlight_get_status (priv->flashlight, &status) < 0) {
174                 flashlight_status_plugin_show_notification (plugin,
175                                                             _("Unable to read status from driver."));
176                 return FALSE;
177         }
178
179         /* ops, something is wrong */
180         if (status) {
181                 /* turn off flashlight */
182                 flashlight_status_plugin_enable (plugin, FALSE);
183
184                 if (status & FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT) {
185                         flashlight_status_plugin_show_notification (plugin,
186                                 _("Short-circut fault detected!\nTurning off flashlight."));
187                 } else if (status & FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT) {
188                         flashlight_status_plugin_show_notification (plugin,
189                                 _("Overtemperature fault detected!\nTurning off flashlight."));
190                 } else if (status & FLASHLIGHT_STATUS_TIMEOUT_FAULT) {
191                         flashlight_status_plugin_show_notification (plugin,
192                                 _("Timeout fault detected!\nTurning off flashlight."));
193                 } else if (status & FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT) {
194                         flashlight_status_plugin_show_notification (plugin,
195                                 _("Overvoltage fault detected!\nTurning off flashlight."));
196                 }
197         }
198
199         return TRUE;
200 }
201
202 static void
203 flashlight_status_plugin_on_clicked (HildonButton *button,
204                                      gpointer data)
205 {
206         FlashlightPlugin *plugin = data;
207         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
208
209         g_return_if_fail (priv);
210
211         if (!strcmp (hildon_button_get_value (button), MSG_FLASHLIGHT_ON)) {
212                 flashlight_status_plugin_enable (plugin, FALSE);
213         } else {
214                 flashlight_status_plugin_enable (plugin, TRUE);
215         }
216 }
217
218 static GtkWidget *
219 flashlight_status_plugin_ui (FlashlightPlugin *plugin)
220 {
221         GtkWidget *button;
222
223         g_return_val_if_fail (plugin, NULL);
224
225         button = hildon_button_new (HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
226         gtk_button_set_alignment (GTK_BUTTON (button), 0.0, 0.5);
227         hildon_button_set_title (HILDON_BUTTON (button), _("Flashlight"));
228         hildon_button_set_value (HILDON_BUTTON (button), MSG_FLASHLIGHT_OFF);
229         hildon_button_set_image (HILDON_BUTTON (button),
230                                  gtk_image_new_from_icon_name (ICON_FLASHLIGHT_OFF, -1));
231         hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_LEFT);
232
233         g_signal_connect (button, "clicked",
234                                         G_CALLBACK (flashlight_status_plugin_on_clicked), plugin);
235
236         return button;
237 }
238
239 static void
240 flashlight_status_plugin_class_init (FlashlightPluginClass *class)
241 {
242         GObjectClass *object_class = G_OBJECT_CLASS (class);
243
244         object_class->finalize = flashlight_status_plugin_finalize;
245
246         g_type_class_add_private (class, sizeof (FlashlightPluginPrivate));
247 }
248
249 static void
250 flashlight_status_plugin_class_finalize (FlashlightPluginClass *class)
251 {
252 }
253
254 static void
255 flashlight_status_plugin_init (FlashlightPlugin *plugin)
256 {
257         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
258         DBusConnection *dbus_connection;
259         DBusError error;
260
261         /* initialize dbus */
262         dbus_error_init (&error);
263         dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
264         if (dbus_error_is_set (&error)) {
265                 g_critical ("flashlight_status_plugin_init: Could not get the system DBus connection, %s",
266                             error.message);
267                 dbus_error_free (&error);
268                 return;
269         }
270
271         /* initialize flashlight */
272         if (flashlight_init (&priv->flashlight) < 0) {
273                 g_critical ("flashlight_status_plugin_init: Unable to create Flashlight context\n");
274                 return;
275         }
276
277         /* initialize hal */
278         priv->hal = libhal_ctx_new ();
279         if (!priv->hal) {
280                 g_critical ("flashlight_status_plugin_init: Unable to create HAL context\n");
281                 return;
282         }
283
284         libhal_ctx_set_dbus_connection (priv->hal, dbus_connection);
285         libhal_ctx_set_user_data (priv->hal, plugin);
286         libhal_ctx_set_device_property_modified (priv->hal,
287                                                  flashlight_status_plugin_on_hal_property_modified);
288
289         if (!libhal_ctx_init (priv->hal, &error)) {
290                 if (dbus_error_is_set (&error)) {
291                         g_critical ("Could not initialize the HAL context, %s",
292                                     error.message);
293                         dbus_error_free (&error);
294                 } else {
295                         g_critical ("Could not initialize the HAL context, "
296                                     "no error, is hald running?");
297                 }
298                 libhal_ctx_set_user_data (priv->hal, NULL);
299                 libhal_ctx_free (priv->hal);
300                 return;
301         }
302
303         libhal_device_add_property_watch (priv->hal, CAM_COVER_UDI, NULL);
304
305         /* create plugin ui */
306         priv->button = flashlight_status_plugin_ui (plugin);
307         gtk_container_add (GTK_CONTAINER (plugin), priv->button);
308
309         /* show widget if camera cover is open */
310         if ( !libhal_device_get_property_bool (priv->hal, CAM_COVER_UDI, CAM_COVER_STATE, NULL))
311                 gtk_widget_show_all (GTK_WIDGET (plugin));
312
313 }
314
315 static void
316 flashlight_status_plugin_finalize (GObject *object)
317 {
318         FlashlightPlugin *plugin = FLASHLIGHT_STATUS_PLUGIN (object);
319         FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
320
321         g_return_if_fail (priv);
322
323         /* deinitialize hal */
324         if (priv->hal) {
325                 libhal_device_remove_property_watch (priv->hal, CAM_COVER_UDI, NULL);
326                 libhal_ctx_set_user_data (priv->hal, NULL);
327                 libhal_ctx_free (priv->hal);
328         }
329
330         /* cancel status timer */
331         if (priv->status_timer) {
332                 g_source_remove (priv->status_timer);
333         }
334
335         /* deinitialize flashlight */
336         if (priv->flashlight) {
337                 flashlight_deinit (priv->flashlight);
338         }
339
340         G_OBJECT_CLASS (flashlight_status_plugin_parent_class)->finalize (object);
341 }