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 // Shoud contain one, and only one "%d"
78 #define ICON_TEMPLATE "display-blanking-icon.%d"
80 #define GETTEXT_DOM "status-area-displayblanking-applet"
81 #define gettext_noop(str) (str)
83 #define BLANKING_MODES 5
84 static const char *_DisplayBlankingDescription[BLANKING_MODES] =
86 gettext_noop ("Both enabled"),
87 gettext_noop ("Both only on battery"),
88 gettext_noop ("Blanking only on battery"),
89 gettext_noop ("Both disabled"),
90 gettext_noop ("Only dimming")
93 struct _DisplayBlankingStatusPluginPrivate
96 GConfClient *gconf_client;
101 HD_DEFINE_PLUGIN_MODULE (DisplayBlankingStatusPlugin,
102 display_blanking_status_plugin, HD_TYPE_STATUS_MENU_ITEM)
105 display_blanking_status_plugin_class_finalize (
106 DisplayBlankingStatusPluginClass *klass)
111 display_blanking_status_plugin_class_init (DisplayBlankingStatusPluginClass *c)
113 g_type_class_add_private (c, sizeof (DisplayBlankingStatusPluginPrivate));
117 update_gui (DisplayBlankingStatusPluginPrivate *priv)
119 // Should be enough if BLANKING_MODES stays in 1 digit, "%d"
120 // provides space for that digit and '\0'
121 static char icon_name[sizeof (ICON_TEMPLATE)];
123 // Update button text and status bar icon
124 gint mode = priv->blanking_mode;
125 hildon_button_set_value (HILDON_BUTTON (priv->button),
126 dgettext (GETTEXT_DOM, _DisplayBlankingDescription[mode]));
127 int r = snprintf (icon_name, sizeof (icon_name), ICON_TEMPLATE, mode);
128 g_assert (r < sizeof (icon_name)); // otherwise it was truncated
129 GtkWidget *icon = gtk_image_new_from_icon_name (icon_name,
130 GTK_ICON_SIZE_DIALOG);
131 hildon_button_set_image (HILDON_BUTTON (priv->button), icon);
135 on_button_clicked (GtkWidget *button, DisplayBlankingStatusPluginPrivate *priv)
137 // Update display blanking mode
138 priv->blanking_mode = (priv->blanking_mode + 1) % BLANKING_MODES;
140 gconf_client_set_int (priv->gconf_client, MODE_GCONF_KEY,
141 priv->blanking_mode, NULL);
143 // Show a notification banner (only if updating)
144 GtkWidget *banner = hildon_banner_show_informationf (priv->button, NULL,
145 dgettext (GETTEXT_DOM, "Changed display blanking mode to: %s"),
146 _DisplayBlankingDescription[priv->blanking_mode]);
147 hildon_banner_set_timeout (HILDON_BANNER (banner), 5000);
151 on_gconf_notify (GConfClient* client, guint cnxn_id, GConfEntry* entry,
152 DisplayBlankingStatusPluginPrivate* priv)
154 const gchar* key = gconf_entry_get_key (entry);
155 g_assert (key != NULL);
157 // Ignore notification about keys we don't care about
158 if (strcmp (key, MODE_GCONF_KEY) != 0)
161 const GConfValue* value = gconf_entry_get_value (entry);
162 g_assert (value != NULL);
163 g_assert (GCONF_VALUE_TYPE_VALID (value->type));
165 priv->blanking_mode = gconf_value_get_int (value);
170 display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin)
172 GError* error = NULL;
173 DisplayBlankingStatusPluginPrivate *priv;
175 priv = DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE (plugin);
178 priv->gconf_client = gconf_client_get_default ();
179 g_assert (GCONF_IS_CLIENT (priv->gconf_client));
181 priv->button = hildon_button_new (HILDON_SIZE_FINGER_HEIGHT |
182 HILDON_SIZE_AUTO_WIDTH, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
183 gtk_button_set_alignment (GTK_BUTTON (priv->button), 0, 0);
184 hildon_button_set_style (HILDON_BUTTON (priv->button),
185 HILDON_BUTTON_STYLE_PICKER);
186 hildon_button_set_title (HILDON_BUTTON (priv->button),
187 dgettext (GETTEXT_DOM, "Display blanking mode"));
189 priv->blanking_mode = gconf_client_get_int (priv->gconf_client,
190 MODE_GCONF_KEY, &error);
191 g_assert (error == NULL);
195 g_signal_connect (priv->button, "clicked", G_CALLBACK (on_button_clicked),
198 gconf_client_add_dir (priv->gconf_client, MODE_GCONF_ROOT,
199 GCONF_CLIENT_PRELOAD_NONE, &error);
200 g_assert (error == NULL);
201 gconf_client_notify_add (priv->gconf_client, MODE_GCONF_KEY,
202 (GConfClientNotifyFunc) &on_gconf_notify, priv, NULL, &error);
203 g_assert (error == NULL);
205 gtk_container_add (GTK_CONTAINER (plugin), priv->button);
207 gtk_widget_show_all (priv->button);
209 gtk_widget_show (GTK_WIDGET (plugin));