1 /***********************************************************************************
2 * Display blanking status area plugin
3 * Copyright (C) 2012 Leandro Lucarella
4 * Based on status-area-orientationlock-applet by Mohammad Abu-Garbeyyeh.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ***********************************************************************************/
27 #include <hildon/hildon.h>
28 #include <libhildondesktop/libhildondesktop.h>
29 #include <gconf/gconf-client.h>
30 #include <dbus/dbus.h>
31 #include <mce/dbus-names.h>
34 #define TYPE_DISPLAY_BLANKING_STATUS_PLUGIN (display_blanking_status_plugin_get_type ())
36 #define DISPLAY_BLANKING_STATUS_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
37 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPlugin))
39 #define DISPLAY_BLANKING_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), \
40 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPluginClass))
42 #define IS_DISPLAY_BLANKING_STATUS_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
43 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN))
45 #define IS_DISPLAY_BLANKING_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), \
46 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN))
48 #define DISPLAY_BLANKING_STATUS_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
49 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPluginClass))
51 #define STATUS_AREA_DISPLAY_BLANKING_ICON_SIZE 18
53 typedef struct _DisplayBlankingStatusPlugin DisplayBlankingStatusPlugin;
54 typedef struct _DisplayBlankingStatusPluginClass DisplayBlankingStatusPluginClass;
55 typedef struct _DisplayBlankingStatusPluginPrivate DisplayBlankingStatusPluginPrivate;
57 struct _DisplayBlankingStatusPlugin
59 HDStatusMenuItem parent;
61 DisplayBlankingStatusPluginPrivate *priv;
64 struct _DisplayBlankingStatusPluginClass
66 HDStatusMenuItemClass parent;
69 GType display_blanking_status_plugin_get_type (void);
71 #define DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE(obj) \
72 (G_TYPE_INSTANCE_GET_PRIVATE (obj, \
73 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, \
74 DisplayBlankingStatusPluginPrivate))
76 #define MODE_GCONF_ROOT "/system/osso/dsm/display"
77 #define MODE_GCONF_KEY MODE_GCONF_ROOT "/inhibit_blank_mode"
79 #define INHIBIT_MSG_INTERVAL 30 // in seconds
81 #define GETTEXT_DOM "status-area-displayblanking-applet"
82 #define gettext_noop(str) (str)
84 // Undocumented blanking modes as reported by David Weinehall from Nokia:
85 // http://www.gossamer-threads.com/lists/maemo/developers/61201#61201
86 #define BLANKING_MODES 5
87 static const char *_DisplayBlankingDescription[BLANKING_MODES] =
89 gettext_noop ("Both enabled"),
90 gettext_noop ("Both only on battery"),
91 gettext_noop ("Blanking only on battery"),
92 gettext_noop ("Both disabled"),
93 gettext_noop ("Only dimming")
95 static const char *mode_icon_name[BLANKING_MODES] =
97 "display-blanking-icon.0",
98 "display-blanking-icon.1",
99 "display-blanking-icon.2",
100 "display-blanking-icon.3",
101 "display-blanking-icon.4",
103 #define INHIBIT_ICON_NAME "display-blanking-inhibit-icon"
105 struct _DisplayBlankingStatusPluginPrivate
107 GConfClient *gconf_client;
108 DBusConnection* dbus_conn;
109 DBusMessage* dbus_msg;
110 GtkWidget *mode_button;
111 GtkWidget *mode_dialog;
112 gint inhibit_timer_id; // if == 0, no timer is set
116 HD_DEFINE_PLUGIN_MODULE (DisplayBlankingStatusPlugin,
117 display_blanking_status_plugin, HD_TYPE_STATUS_MENU_ITEM)
120 display_blanking_status_plugin_class_finalize (
121 DisplayBlankingStatusPluginClass *klass)
126 display_blanking_status_plugin_class_init (DisplayBlankingStatusPluginClass *c)
128 g_type_class_add_private (c, sizeof (DisplayBlankingStatusPluginPrivate));
132 update_mode_gui (gint mode, DisplayBlankingStatusPluginPrivate *priv)
134 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[mode],
135 GTK_ICON_SIZE_DIALOG);
136 gtk_button_set_image (GTK_BUTTON (priv->mode_button), icon);
140 on_timeout (DisplayBlankingStatusPluginPrivate *priv)
142 dbus_bool_t ok = dbus_connection_send (priv->dbus_conn, priv->dbus_msg,
144 g_assert (ok == TRUE);
150 on_mode_dialog_button_clicked (GtkWidget *button, GtkDialog *dialog)
152 const gchar *title = hildon_button_get_title (HILDON_BUTTON (button));
154 gint *mode = (gint *) g_object_get_data (G_OBJECT(dialog), "mode");
155 g_assert (mode != NULL);
157 for (*mode = 0; *mode < BLANKING_MODES; (*mode)++) {
158 if (strcmp (title, _DisplayBlankingDescription[*mode]) == 0)
161 g_assert (*mode < BLANKING_MODES);
163 gtk_dialog_response (dialog, GTK_RESPONSE_OK);
167 mode_get_input (DisplayBlankingStatusPluginPrivate *priv)
169 g_assert (priv->mode_dialog == NULL);
170 priv->mode_dialog = gtk_dialog_new ();
171 gtk_window_set_modal (GTK_WINDOW (priv->mode_dialog), TRUE);
172 gtk_window_set_title (GTK_WINDOW (priv->mode_dialog),
173 dgettext (GETTEXT_DOM, "Select display blanking mode"));
175 GtkWidget *pan_area = hildon_pannable_area_new ();
176 g_assert (pan_area != NULL);
178 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
179 g_assert (vbox != NULL);
181 hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pan_area),
183 GtkWidget *content_area = gtk_dialog_get_content_area (
184 GTK_DIALOG (priv->mode_dialog));
185 gtk_box_pack_start (GTK_BOX (content_area), pan_area, TRUE, TRUE, 0);
187 gtk_widget_set_size_request (pan_area, -1, MIN (350, BLANKING_MODES * 70));
189 gint mode = BLANKING_MODES;
190 for (int i = 0; i < BLANKING_MODES; i++) {
192 hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT,
193 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
194 dgettext (GETTEXT_DOM, (_DisplayBlankingDescription[i])),
196 hildon_button_set_style (HILDON_BUTTON (button),
197 HILDON_BUTTON_STYLE_PICKER);
198 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[i],
199 GTK_ICON_SIZE_DIALOG);
200 hildon_button_set_image (HILDON_BUTTON (button), icon);
201 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
202 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
203 g_signal_connect (button, "clicked",
204 G_CALLBACK (on_mode_dialog_button_clicked), priv->mode_dialog);
207 g_object_set_data (G_OBJECT (priv->mode_dialog), "mode", &mode);
208 gtk_widget_show_all (priv->mode_dialog);
209 gtk_dialog_run (GTK_DIALOG (priv->mode_dialog));
211 gtk_widget_destroy (priv->mode_dialog);
212 priv->mode_dialog = NULL;
218 on_mode_button_clicked (GtkWidget *button,
219 DisplayBlankingStatusPluginPrivate *priv)
221 GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button),
223 gtk_widget_hide (parent);
225 gint mode = mode_get_input (priv);
227 if (mode != BLANKING_MODES) {
228 // will trigger the gconf notify signal
229 GError *error = NULL;
230 gconf_client_set_int (priv->gconf_client, MODE_GCONF_KEY, mode, &error);
231 g_assert (error == NULL);
236 on_inhibit_button_clicked (GtkWidget *button,
237 DisplayBlankingStatusPluginPrivate *priv)
239 GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button),
241 gtk_widget_hide (parent);
243 if (priv->inhibit_timer_id) {
244 gboolean ok = g_source_remove (priv->inhibit_timer_id);
245 g_assert (ok == TRUE);
246 priv->inhibit_timer_id = 0;
249 priv->inhibit_timer_id = g_timeout_add_seconds (INHIBIT_MSG_INTERVAL,
250 (GSourceFunc) on_timeout, priv);
251 g_assert (priv->inhibit_timer_id > 0);
256 on_gconf_notify (GConfClient* client, guint cnxn_id, GConfEntry* entry,
257 DisplayBlankingStatusPluginPrivate* priv)
259 const gchar* key = gconf_entry_get_key (entry);
260 g_assert (key != NULL);
262 // Ignore notification about keys we don't care about
263 if (strcmp (key, MODE_GCONF_KEY) != 0)
266 const GConfValue* value = gconf_entry_get_value (entry);
267 g_assert (value != NULL);
268 g_assert (GCONF_VALUE_TYPE_VALID (value->type));
269 g_assert (value->type == GCONF_VALUE_INT);
271 gint mode = gconf_value_get_int (value);
272 update_mode_gui (mode, priv);
276 init_gconf (DisplayBlankingStatusPluginPrivate *priv)
278 GError* error = NULL;
280 priv->gconf_client = gconf_client_get_default ();
281 g_assert (GCONF_IS_CLIENT (priv->gconf_client));
283 gconf_client_add_dir (priv->gconf_client, MODE_GCONF_ROOT,
284 GCONF_CLIENT_PRELOAD_NONE, &error);
285 g_assert (error == NULL);
287 gconf_client_notify_add (priv->gconf_client, MODE_GCONF_KEY,
288 (GConfClientNotifyFunc) &on_gconf_notify, priv, NULL, &error);
289 g_assert (error == NULL);
293 init_dbus (DisplayBlankingStatusPluginPrivate *priv)
296 dbus_error_init (&error);
298 priv->dbus_conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
299 g_assert (!dbus_error_is_set (&error));
300 g_assert (priv->dbus_conn != NULL);
302 priv->dbus_msg = dbus_message_new_method_call (MCE_SERVICE,
303 MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_PREVENT_BLANK_REQ);
304 g_assert (priv->dbus_msg != NULL);
305 dbus_message_set_no_reply (priv->dbus_msg, TRUE);
309 init_mode_gui (DisplayBlankingStatusPluginPrivate *priv)
311 priv->mode_dialog = NULL;
312 priv->mode_button = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT |
313 HILDON_SIZE_AUTO_WIDTH);
315 GError* error = NULL;
316 gint mode = gconf_client_get_int (priv->gconf_client, MODE_GCONF_KEY,
318 g_assert (error == NULL);
319 update_mode_gui (mode, priv);
321 g_signal_connect (priv->mode_button, "clicked",
322 G_CALLBACK (on_mode_button_clicked), priv);
326 display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin)
328 DisplayBlankingStatusPluginPrivate *priv;
330 priv = DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE (plugin);
335 init_mode_gui (priv);
337 priv->inhibit_timer_id = 0;
338 GtkWidget *inhibit_button = hildon_gtk_toggle_button_new (
339 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
340 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON(inhibit_button), FALSE);
341 GtkWidget *icon = gtk_image_new_from_icon_name (INHIBIT_ICON_NAME,
342 GTK_ICON_SIZE_DIALOG);
343 gtk_button_set_image (GTK_BUTTON (inhibit_button), icon);
344 g_signal_connect (inhibit_button, "clicked",
345 G_CALLBACK (on_inhibit_button_clicked), priv);
347 GtkWidget *hbbox = gtk_hbutton_box_new ();
348 g_assert (hbbox != NULL);
350 gtk_container_add (GTK_CONTAINER (hbbox), priv->mode_button);
351 gtk_container_add (GTK_CONTAINER (hbbox), inhibit_button);
353 gtk_container_add (GTK_CONTAINER (plugin), hbbox);
355 gtk_widget_show_all (GTK_WIDGET (plugin));