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 #define BLANKING_MODES 5
83 static const char *_DisplayBlankingDescription[BLANKING_MODES] =
85 gettext_noop ("Both enabled"),
86 gettext_noop ("Both only on battery"),
87 gettext_noop ("Blanking only on battery"),
88 gettext_noop ("Both disabled"),
89 gettext_noop ("Only dimming")
91 static const char *mode_icon_name[BLANKING_MODES] =
93 "display-blanking-icon.0",
94 "display-blanking-icon.1",
95 "display-blanking-icon.2",
96 "display-blanking-icon.3",
97 "display-blanking-icon.4",
100 struct _DisplayBlankingStatusPluginPrivate
102 GConfClient *gconf_client;
103 GtkWidget *mode_button;
104 GtkWidget *mode_dialog;
108 HD_DEFINE_PLUGIN_MODULE (DisplayBlankingStatusPlugin,
109 display_blanking_status_plugin, HD_TYPE_STATUS_MENU_ITEM)
112 display_blanking_status_plugin_class_finalize (
113 DisplayBlankingStatusPluginClass *klass)
118 display_blanking_status_plugin_class_init (DisplayBlankingStatusPluginClass *c)
120 g_type_class_add_private (c, sizeof (DisplayBlankingStatusPluginPrivate));
124 update_mode_gui (gint mode, DisplayBlankingStatusPluginPrivate *priv)
126 hildon_button_set_value (HILDON_BUTTON (priv->mode_button),
127 dgettext (GETTEXT_DOM, _DisplayBlankingDescription[mode]));
128 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[mode],
129 GTK_ICON_SIZE_DIALOG);
130 hildon_button_set_image (HILDON_BUTTON (priv->mode_button), icon);
134 on_mode_dialog_button_clicked (GtkWidget *button, GtkDialog *dialog)
136 const gchar *title = hildon_button_get_title (HILDON_BUTTON (button));
138 gint *mode = (gint *) g_object_get_data (G_OBJECT(dialog), "mode");
139 g_assert (mode != NULL);
141 for (*mode = 0; *mode < BLANKING_MODES; (*mode)++) {
142 if (strcmp (title, _DisplayBlankingDescription[*mode]) == 0)
145 g_assert (*mode < BLANKING_MODES);
147 gtk_dialog_response (dialog, GTK_RESPONSE_OK);
151 on_mode_button_clicked (GtkWidget *button,
152 DisplayBlankingStatusPluginPrivate *priv)
154 GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button),
156 gtk_widget_hide (parent);
158 g_assert (priv->mode_dialog == NULL);
159 priv->mode_dialog = gtk_dialog_new ();
160 gtk_window_set_modal (GTK_WINDOW (priv->mode_dialog), TRUE);
161 gtk_window_set_title (GTK_WINDOW (priv->mode_dialog),
162 dgettext (GETTEXT_DOM, "Select display blanking mode"));
164 GtkWidget *pan_area = hildon_pannable_area_new ();
165 g_assert (pan_area != NULL);
167 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
168 g_assert (vbox != NULL);
170 hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pan_area),
172 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (priv->mode_dialog)->vbox),
173 pan_area, TRUE, TRUE, 0);
175 g_object_set (G_OBJECT (pan_area), "height-request",
176 MIN (350, BLANKING_MODES * 70), NULL);
178 gint mode = BLANKING_MODES;
179 for (int i = 0; i < BLANKING_MODES; i++) {
181 hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT,
182 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
183 _DisplayBlankingDescription[i], NULL);
184 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[i],
185 GTK_ICON_SIZE_DIALOG);
186 hildon_button_set_image (HILDON_BUTTON (button), icon);
187 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
188 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
189 g_signal_connect (button, "clicked",
190 G_CALLBACK (on_mode_dialog_button_clicked), priv->mode_dialog);
193 gtk_widget_show_all (priv->mode_dialog);
195 g_object_set_data (G_OBJECT (priv->mode_dialog), "mode", &mode);
196 gtk_dialog_run (GTK_DIALOG (priv->mode_dialog));
198 if (mode != BLANKING_MODES) {
199 // will trigger the gconf notify signal
200 GError *error = NULL;
201 gconf_client_set_int (priv->gconf_client, MODE_GCONF_KEY, mode, &error);
202 g_assert (error == NULL);
205 gtk_widget_destroy (priv->mode_dialog);
206 priv->mode_dialog = NULL;
210 on_gconf_notify (GConfClient* client, guint cnxn_id, GConfEntry* entry,
211 DisplayBlankingStatusPluginPrivate* priv)
213 const gchar* key = gconf_entry_get_key (entry);
214 g_assert (key != NULL);
216 // Ignore notification about keys we don't care about
217 if (strcmp (key, MODE_GCONF_KEY) != 0)
220 const GConfValue* value = gconf_entry_get_value (entry);
221 g_assert (value != NULL);
222 g_assert (GCONF_VALUE_TYPE_VALID (value->type));
223 g_assert (value->type == GCONF_VALUE_INT);
225 gint mode = gconf_value_get_int (value);
226 update_mode_gui (mode, priv);
230 init_gconf (DisplayBlankingStatusPluginPrivate *priv)
232 GError* error = NULL;
234 priv->gconf_client = gconf_client_get_default ();
235 g_assert (GCONF_IS_CLIENT (priv->gconf_client));
237 gconf_client_add_dir (priv->gconf_client, MODE_GCONF_ROOT,
238 GCONF_CLIENT_PRELOAD_NONE, &error);
239 g_assert (error == NULL);
241 gconf_client_notify_add (priv->gconf_client, MODE_GCONF_KEY,
242 (GConfClientNotifyFunc) &on_gconf_notify, priv, NULL, &error);
243 g_assert (error == NULL);
247 init_mode_gui (DisplayBlankingStatusPluginPrivate *priv)
249 priv->mode_dialog = NULL;
250 priv->mode_button = hildon_button_new (HILDON_SIZE_FINGER_HEIGHT |
251 HILDON_SIZE_AUTO_WIDTH, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
252 gtk_button_set_alignment (GTK_BUTTON (priv->mode_button), 0, 0);
253 hildon_button_set_style (HILDON_BUTTON (priv->mode_button),
254 HILDON_BUTTON_STYLE_PICKER);
255 hildon_button_set_title (HILDON_BUTTON (priv->mode_button),
256 dgettext (GETTEXT_DOM, "Display blanking mode"));
258 GError* error = NULL;
259 gint mode = gconf_client_get_int (priv->gconf_client, MODE_GCONF_KEY,
261 g_assert (error == NULL);
262 update_mode_gui (mode, priv);
264 g_signal_connect (priv->mode_button, "clicked",
265 G_CALLBACK (on_mode_button_clicked), priv);
269 display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin)
271 DisplayBlankingStatusPluginPrivate *priv;
273 priv = DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE (plugin);
277 init_mode_gui (priv);
279 gtk_container_add (GTK_CONTAINER (plugin), priv->mode_button);
281 gtk_widget_show_all (priv->mode_button);
283 gtk_widget_show (GTK_WIDGET (plugin));