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 on_mode_button_clicked (GtkWidget *button,
168 DisplayBlankingStatusPluginPrivate *priv)
170 GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button),
172 gtk_widget_hide (parent);
174 g_assert (priv->mode_dialog == NULL);
175 priv->mode_dialog = gtk_dialog_new ();
176 gtk_window_set_modal (GTK_WINDOW (priv->mode_dialog), TRUE);
177 gtk_window_set_title (GTK_WINDOW (priv->mode_dialog),
178 dgettext (GETTEXT_DOM, "Select display blanking mode"));
180 GtkWidget *pan_area = hildon_pannable_area_new ();
181 g_assert (pan_area != NULL);
183 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
184 g_assert (vbox != NULL);
186 hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pan_area),
188 GtkWidget *content_area = gtk_dialog_get_content_area (
189 GTK_DIALOG (priv->mode_dialog));
190 gtk_box_pack_start (GTK_BOX (content_area), pan_area, TRUE, TRUE, 0);
192 gtk_widget_set_size_request (pan_area, -1, MIN (350, BLANKING_MODES * 70));
194 gint mode = BLANKING_MODES;
195 for (int i = 0; i < BLANKING_MODES; i++) {
197 hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT,
198 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
199 dgettext (GETTEXT_DOM, (_DisplayBlankingDescription[i])),
201 hildon_button_set_style (HILDON_BUTTON (button),
202 HILDON_BUTTON_STYLE_PICKER);
203 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[i],
204 GTK_ICON_SIZE_DIALOG);
205 hildon_button_set_image (HILDON_BUTTON (button), icon);
206 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
207 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
208 g_signal_connect (button, "clicked",
209 G_CALLBACK (on_mode_dialog_button_clicked), priv->mode_dialog);
212 gtk_widget_show_all (priv->mode_dialog);
214 g_object_set_data (G_OBJECT (priv->mode_dialog), "mode", &mode);
215 gtk_dialog_run (GTK_DIALOG (priv->mode_dialog));
217 if (mode != BLANKING_MODES) {
218 // will trigger the gconf notify signal
219 GError *error = NULL;
220 gconf_client_set_int (priv->gconf_client, MODE_GCONF_KEY, mode, &error);
221 g_assert (error == NULL);
224 gtk_widget_destroy (priv->mode_dialog);
225 priv->mode_dialog = NULL;
229 on_inhibit_button_clicked (GtkWidget *button,
230 DisplayBlankingStatusPluginPrivate *priv)
232 GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button),
234 gtk_widget_hide (parent);
236 if (priv->inhibit_timer_id) {
237 gboolean ok = g_source_remove (priv->inhibit_timer_id);
238 g_assert (ok == TRUE);
239 priv->inhibit_timer_id = 0;
242 priv->inhibit_timer_id = g_timeout_add_seconds (INHIBIT_MSG_INTERVAL,
243 (GSourceFunc) on_timeout, priv);
244 g_assert (priv->inhibit_timer_id > 0);
249 on_gconf_notify (GConfClient* client, guint cnxn_id, GConfEntry* entry,
250 DisplayBlankingStatusPluginPrivate* priv)
252 const gchar* key = gconf_entry_get_key (entry);
253 g_assert (key != NULL);
255 // Ignore notification about keys we don't care about
256 if (strcmp (key, MODE_GCONF_KEY) != 0)
259 const GConfValue* value = gconf_entry_get_value (entry);
260 g_assert (value != NULL);
261 g_assert (GCONF_VALUE_TYPE_VALID (value->type));
262 g_assert (value->type == GCONF_VALUE_INT);
264 gint mode = gconf_value_get_int (value);
265 update_mode_gui (mode, priv);
269 init_gconf (DisplayBlankingStatusPluginPrivate *priv)
271 GError* error = NULL;
273 priv->gconf_client = gconf_client_get_default ();
274 g_assert (GCONF_IS_CLIENT (priv->gconf_client));
276 gconf_client_add_dir (priv->gconf_client, MODE_GCONF_ROOT,
277 GCONF_CLIENT_PRELOAD_NONE, &error);
278 g_assert (error == NULL);
280 gconf_client_notify_add (priv->gconf_client, MODE_GCONF_KEY,
281 (GConfClientNotifyFunc) &on_gconf_notify, priv, NULL, &error);
282 g_assert (error == NULL);
286 init_dbus (DisplayBlankingStatusPluginPrivate *priv)
289 dbus_error_init (&error);
291 priv->dbus_conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
292 g_assert (!dbus_error_is_set (&error));
293 g_assert (priv->dbus_conn != NULL);
295 priv->dbus_msg = dbus_message_new_method_call (MCE_SERVICE,
296 MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_PREVENT_BLANK_REQ);
297 g_assert (priv->dbus_msg != NULL);
298 dbus_message_set_no_reply (priv->dbus_msg, TRUE);
302 init_mode_gui (DisplayBlankingStatusPluginPrivate *priv)
304 priv->mode_dialog = NULL;
305 priv->mode_button = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT |
306 HILDON_SIZE_AUTO_WIDTH);
308 GError* error = NULL;
309 gint mode = gconf_client_get_int (priv->gconf_client, MODE_GCONF_KEY,
311 g_assert (error == NULL);
312 update_mode_gui (mode, priv);
314 g_signal_connect (priv->mode_button, "clicked",
315 G_CALLBACK (on_mode_button_clicked), priv);
319 display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin)
321 DisplayBlankingStatusPluginPrivate *priv;
323 priv = DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE (plugin);
328 init_mode_gui (priv);
330 priv->inhibit_timer_id = 0;
331 GtkWidget *inhibit_button = hildon_gtk_toggle_button_new (
332 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
333 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON(inhibit_button), FALSE);
334 GtkWidget *icon = gtk_image_new_from_icon_name (INHIBIT_ICON_NAME,
335 GTK_ICON_SIZE_DIALOG);
336 gtk_button_set_image (GTK_BUTTON (inhibit_button), icon);
337 g_signal_connect (inhibit_button, "clicked",
338 G_CALLBACK (on_inhibit_button_clicked), priv);
340 GtkWidget *hbbox = gtk_hbutton_box_new ();
341 g_assert (hbbox != NULL);
343 gtk_container_add (GTK_CONTAINER (hbbox), priv->mode_button);
344 gtk_container_add (GTK_CONTAINER (hbbox), inhibit_button);
346 gtk_container_add (GTK_CONTAINER (plugin), hbbox);
348 gtk_widget_show_all (GTK_WIDGET (plugin));