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>
32 #define TYPE_DISPLAY_BLANKING_STATUS_PLUGIN (display_blanking_status_plugin_get_type ())
34 #define DISPLAY_BLANKING_STATUS_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
35 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPlugin))
37 #define DISPLAY_BLANKING_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), \
38 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPluginClass))
40 #define IS_DISPLAY_BLANKING_STATUS_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
41 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN))
43 #define IS_DISPLAY_BLANKING_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), \
44 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN))
46 #define DISPLAY_BLANKING_STATUS_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
47 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPluginClass))
49 #define STATUS_AREA_DISPLAY_BLANKING_ICON_SIZE 18
51 typedef struct _DisplayBlankingStatusPlugin DisplayBlankingStatusPlugin;
52 typedef struct _DisplayBlankingStatusPluginClass DisplayBlankingStatusPluginClass;
53 typedef struct _DisplayBlankingStatusPluginPrivate DisplayBlankingStatusPluginPrivate;
55 struct _DisplayBlankingStatusPlugin
57 HDStatusMenuItem parent;
59 DisplayBlankingStatusPluginPrivate *priv;
62 struct _DisplayBlankingStatusPluginClass
64 HDStatusMenuItemClass parent;
67 GType display_blanking_status_plugin_get_type (void);
69 #define DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE(obj) \
70 (G_TYPE_INSTANCE_GET_PRIVATE (obj, \
71 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, \
72 DisplayBlankingStatusPluginPrivate))
74 #define MODE_GCONF_ROOT "/system/osso/dsm/display"
75 #define MODE_GCONF_KEY MODE_GCONF_ROOT "/inhibit_blank_mode"
77 #define INHIBIT_MSG_INTERVAL 30 // in seconds
79 #define GETTEXT_DOM "status-area-displayblanking-applet"
80 #define gettext_noop(str) (str)
82 // Undocumented blanking modes as reported by David Weinehall from Nokia:
83 // http://www.gossamer-threads.com/lists/maemo/developers/61201#61201
84 #define BLANKING_MODES 5
85 static const char *_DisplayBlankingDescription[BLANKING_MODES] =
87 gettext_noop ("Both enabled"),
88 gettext_noop ("Both only on battery"),
89 gettext_noop ("Blanking only on battery"),
90 gettext_noop ("Both disabled"),
91 gettext_noop ("Only dimming")
93 static const char *mode_icon_name[BLANKING_MODES] =
95 "display-blanking-icon.0",
96 "display-blanking-icon.1",
97 "display-blanking-icon.2",
98 "display-blanking-icon.3",
99 "display-blanking-icon.4",
102 struct _DisplayBlankingStatusPluginPrivate
104 GConfClient *gconf_client;
105 GtkWidget *mode_button;
106 GtkWidget *mode_dialog;
110 HD_DEFINE_PLUGIN_MODULE (DisplayBlankingStatusPlugin,
111 display_blanking_status_plugin, HD_TYPE_STATUS_MENU_ITEM)
114 display_blanking_status_plugin_class_finalize (
115 DisplayBlankingStatusPluginClass *klass)
120 display_blanking_status_plugin_class_init (DisplayBlankingStatusPluginClass *c)
122 g_type_class_add_private (c, sizeof (DisplayBlankingStatusPluginPrivate));
126 update_mode_gui (gint mode, DisplayBlankingStatusPluginPrivate *priv)
128 hildon_button_set_value (HILDON_BUTTON (priv->mode_button),
129 dgettext (GETTEXT_DOM, _DisplayBlankingDescription[mode]));
130 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[mode],
131 GTK_ICON_SIZE_DIALOG);
132 hildon_button_set_image (HILDON_BUTTON (priv->mode_button), icon);
136 on_mode_dialog_button_clicked (GtkWidget *button, GtkDialog *dialog)
138 const gchar *title = hildon_button_get_title (HILDON_BUTTON (button));
140 gint *mode = (gint *) g_object_get_data (G_OBJECT(dialog), "mode");
141 g_assert (mode != NULL);
143 for (*mode = 0; *mode < BLANKING_MODES; (*mode)++) {
144 if (strcmp (title, _DisplayBlankingDescription[*mode]) == 0)
147 g_assert (*mode < BLANKING_MODES);
149 gtk_dialog_response (dialog, GTK_RESPONSE_OK);
153 on_mode_button_clicked (GtkWidget *button,
154 DisplayBlankingStatusPluginPrivate *priv)
156 GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button),
158 gtk_widget_hide (parent);
160 g_assert (priv->mode_dialog == NULL);
161 priv->mode_dialog = gtk_dialog_new ();
162 gtk_window_set_modal (GTK_WINDOW (priv->mode_dialog), TRUE);
163 gtk_window_set_title (GTK_WINDOW (priv->mode_dialog),
164 dgettext (GETTEXT_DOM, "Select display blanking mode"));
166 GtkWidget *pan_area = hildon_pannable_area_new ();
167 g_assert (pan_area != NULL);
169 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
170 g_assert (vbox != NULL);
172 hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pan_area),
174 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (priv->mode_dialog)->vbox),
175 pan_area, TRUE, TRUE, 0);
177 gtk_widget_set_size_request (pan_area, -1, MIN (350, BLANKING_MODES * 70));
179 gint mode = BLANKING_MODES;
180 for (int i = 0; i < BLANKING_MODES; i++) {
182 hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT,
183 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
184 dgettext (GETTEXT_DOM, (_DisplayBlankingDescription[i])),
186 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[i],
187 GTK_ICON_SIZE_DIALOG);
188 hildon_button_set_image (HILDON_BUTTON (button), icon);
189 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
190 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
191 g_signal_connect (button, "clicked",
192 G_CALLBACK (on_mode_dialog_button_clicked), priv->mode_dialog);
195 gtk_widget_show_all (priv->mode_dialog);
197 g_object_set_data (G_OBJECT (priv->mode_dialog), "mode", &mode);
198 gtk_dialog_run (GTK_DIALOG (priv->mode_dialog));
200 if (mode != BLANKING_MODES) {
201 // will trigger the gconf notify signal
202 GError *error = NULL;
203 gconf_client_set_int (priv->gconf_client, MODE_GCONF_KEY, mode, &error);
204 g_assert (error == NULL);
207 gtk_widget_destroy (priv->mode_dialog);
208 priv->mode_dialog = NULL;
212 on_gconf_notify (GConfClient* client, guint cnxn_id, GConfEntry* entry,
213 DisplayBlankingStatusPluginPrivate* priv)
215 const gchar* key = gconf_entry_get_key (entry);
216 g_assert (key != NULL);
218 // Ignore notification about keys we don't care about
219 if (strcmp (key, MODE_GCONF_KEY) != 0)
222 const GConfValue* value = gconf_entry_get_value (entry);
223 g_assert (value != NULL);
224 g_assert (GCONF_VALUE_TYPE_VALID (value->type));
225 g_assert (value->type == GCONF_VALUE_INT);
227 gint mode = gconf_value_get_int (value);
228 update_mode_gui (mode, priv);
232 init_gconf (DisplayBlankingStatusPluginPrivate *priv)
234 GError* error = NULL;
236 priv->gconf_client = gconf_client_get_default ();
237 g_assert (GCONF_IS_CLIENT (priv->gconf_client));
239 gconf_client_add_dir (priv->gconf_client, MODE_GCONF_ROOT,
240 GCONF_CLIENT_PRELOAD_NONE, &error);
241 g_assert (error == NULL);
243 gconf_client_notify_add (priv->gconf_client, MODE_GCONF_KEY,
244 (GConfClientNotifyFunc) &on_gconf_notify, priv, NULL, &error);
245 g_assert (error == NULL);
249 init_mode_gui (DisplayBlankingStatusPluginPrivate *priv)
251 priv->mode_dialog = NULL;
252 priv->mode_button = hildon_button_new (HILDON_SIZE_FINGER_HEIGHT |
253 HILDON_SIZE_AUTO_WIDTH, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
254 gtk_button_set_alignment (GTK_BUTTON (priv->mode_button), 0, 0);
255 hildon_button_set_style (HILDON_BUTTON (priv->mode_button),
256 HILDON_BUTTON_STYLE_PICKER);
257 hildon_button_set_title (HILDON_BUTTON (priv->mode_button),
258 dgettext (GETTEXT_DOM, "Display blanking mode"));
260 GError* error = NULL;
261 gint mode = gconf_client_get_int (priv->gconf_client, MODE_GCONF_KEY,
263 g_assert (error == NULL);
264 update_mode_gui (mode, priv);
266 g_signal_connect (priv->mode_button, "clicked",
267 G_CALLBACK (on_mode_button_clicked), priv);
271 display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin)
273 DisplayBlankingStatusPluginPrivate *priv;
275 priv = DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE (plugin);
279 init_mode_gui (priv);
281 gtk_container_add (GTK_CONTAINER (plugin), priv->mode_button);
283 gtk_widget_show_all (priv->mode_button);
285 gtk_widget_show (GTK_WIDGET (plugin));