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 ***********************************************************************************/
26 #include <hildon/hildon.h>
27 #include <libhildondesktop/libhildondesktop.h>
28 #include <gconf/gconf-client.h>
31 #define TYPE_DISPLAY_BLANKING_STATUS_PLUGIN (display_blanking_status_plugin_get_type ())
33 #define DISPLAY_BLANKING_STATUS_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
34 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPlugin))
36 #define DISPLAY_BLANKING_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), \
37 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPluginClass))
39 #define IS_DISPLAY_BLANKING_STATUS_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
40 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN))
42 #define IS_DISPLAY_BLANKING_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), \
43 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN))
45 #define DISPLAY_BLANKING_STATUS_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
46 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPluginClass))
48 #define STATUS_AREA_DISPLAY_BLANKING_ICON_SIZE 18
50 typedef struct _DisplayBlankingStatusPlugin DisplayBlankingStatusPlugin;
51 typedef struct _DisplayBlankingStatusPluginClass DisplayBlankingStatusPluginClass;
52 typedef struct _DisplayBlankingStatusPluginPrivate DisplayBlankingStatusPluginPrivate;
54 struct _DisplayBlankingStatusPlugin
56 HDStatusMenuItem parent;
58 DisplayBlankingStatusPluginPrivate *priv;
61 struct _DisplayBlankingStatusPluginClass
63 HDStatusMenuItemClass parent;
66 GType display_blanking_status_plugin_get_type (void);
68 #define DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE(obj) \
69 (G_TYPE_INSTANCE_GET_PRIVATE (obj, \
70 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, \
71 DisplayBlankingStatusPluginPrivate))
73 #define MODE_GCONF_ROOT "/system/osso/dsm/display"
74 #define MODE_GCONF_KEY MODE_GCONF_ROOT "/inhibit_blank_mode"
76 // Shoud contain one, and only one "%d"
77 #define ICON_TEMPLATE "display-blanking-icon.%d"
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")
92 struct _DisplayBlankingStatusPluginPrivate
95 GConfClient *gconf_client;
100 HD_DEFINE_PLUGIN_MODULE (DisplayBlankingStatusPlugin,
101 display_blanking_status_plugin, HD_TYPE_STATUS_MENU_ITEM);
104 display_blanking_status_plugin_class_finalize (
105 DisplayBlankingStatusPluginClass *klass)
110 display_blanking_status_plugin_class_init (DisplayBlankingStatusPluginClass *c)
112 g_type_class_add_private (c, sizeof (DisplayBlankingStatusPluginPrivate));
116 update_gui (DisplayBlankingStatusPluginPrivate *priv)
118 // Should be enough if BLANKING_MODES stays in 1 digit, "%d"
119 // provides space for that digit and '\0'
120 static char icon_name[sizeof (ICON_TEMPLATE)];
122 // Update button text and status bar icon
123 gint mode = priv->blanking_mode;
124 hildon_button_set_value (HILDON_BUTTON (priv->button),
125 dgettext (GETTEXT_DOM, _DisplayBlankingDescription[mode]));
126 int r = snprintf (icon_name, sizeof (icon_name), ICON_TEMPLATE, mode);
127 g_assert (r < sizeof (icon_name)); // otherwise it was truncated
128 GtkWidget *icon = gtk_image_new_from_icon_name (icon_name,
129 GTK_ICON_SIZE_DIALOG);
130 hildon_button_set_image (HILDON_BUTTON (priv->button), icon);
134 on_button_clicked (GtkWidget *button, DisplayBlankingStatusPluginPrivate *priv)
136 // Update display blanking mode
137 priv->blanking_mode = (priv->blanking_mode + 1) % BLANKING_MODES;
139 gconf_client_set_int (priv->gconf_client, MODE_GCONF_KEY,
140 priv->blanking_mode, NULL);
142 // Show a notification banner (only if updating)
143 GtkWidget *banner = hildon_banner_show_informationf (priv->button, NULL,
144 dgettext (GETTEXT_DOM, "Changed display blanking mode to: %s"),
145 _DisplayBlankingDescription[priv->blanking_mode]);
146 hildon_banner_set_timeout (HILDON_BANNER (banner), 5000);
150 on_gconf_notify (GConfClient* client, guint cnxn_id, GConfEntry* entry,
151 DisplayBlankingStatusPluginPrivate* priv)
153 const gchar* key = gconf_entry_get_key (entry);
154 g_assert (key != NULL);
156 // Ignore notification about keys we don't care about
157 if (strcmp (key, MODE_GCONF_KEY) != 0)
160 const GConfValue* value = gconf_entry_get_value (entry);
161 g_assert (value != NULL);
162 g_assert (GCONF_VALUE_TYPE_VALID (value->type));
164 priv->blanking_mode = gconf_value_get_int (value);
169 display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin)
171 GError* error = NULL;
172 DisplayBlankingStatusPluginPrivate *priv;
174 priv = DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE (plugin);
177 priv->gconf_client = gconf_client_get_default ();
178 g_assert (GCONF_IS_CLIENT (priv->gconf_client));
180 priv->button = hildon_button_new (HILDON_SIZE_FINGER_HEIGHT |
181 HILDON_SIZE_AUTO_WIDTH, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
182 gtk_button_set_alignment (GTK_BUTTON (priv->button), 0, 0);
183 hildon_button_set_style (HILDON_BUTTON (priv->button),
184 HILDON_BUTTON_STYLE_PICKER);
185 hildon_button_set_title (HILDON_BUTTON (priv->button),
186 dgettext (GETTEXT_DOM, "Display blanking mode"));
188 priv->blanking_mode = gconf_client_get_int (priv->gconf_client,
189 MODE_GCONF_KEY, &error);
190 g_assert (error == NULL);
194 g_signal_connect (priv->button, "clicked", G_CALLBACK (on_button_clicked),
197 gconf_client_add_dir (priv->gconf_client, MODE_GCONF_ROOT,
198 GCONF_CLIENT_PRELOAD_NONE, &error);
199 g_assert (error == NULL);
200 gconf_client_notify_add (priv->gconf_client, MODE_GCONF_KEY, &on_gconf_notify,
202 g_assert (error == NULL);
204 gtk_container_add (GTK_CONTAINER (plugin), priv->button);
206 gtk_widget_show_all (priv->button);
208 gtk_widget_show (GTK_WIDGET (plugin));