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 GCONF_KEY_DISPLAY_BLANKING "/system/osso/dsm/display/inhibit_blank_mode"
75 // Shoud contain one, and only one "%d"
76 #define DISPLAY_BLANKING_ICON_TEMPLATE "display-blanking-icon.%d"
78 #define GETTEXT_DOM "status-area-displayblanking-applet"
79 #define gettext_noop(str) (str)
81 #define DISPLAY_BLANKING_MODES 5
82 static const char *_DisplayBlankingDescription[DISPLAY_BLANKING_MODES] =
84 gettext_noop ("Both enabled"),
85 gettext_noop ("Both only on battery"),
86 gettext_noop ("Blanking only on battery"),
87 gettext_noop ("Both disabled"),
88 gettext_noop ("Only dimming")
91 struct _DisplayBlankingStatusPluginPrivate
94 GConfClient *gconf_client;
98 HD_DEFINE_PLUGIN_MODULE (DisplayBlankingStatusPlugin,
99 display_blanking_status_plugin, HD_TYPE_STATUS_MENU_ITEM);
102 display_blanking_status_plugin_class_finalize (
103 DisplayBlankingStatusPluginClass *klass)
108 display_blanking_status_plugin_class_init (DisplayBlankingStatusPluginClass *c)
110 g_type_class_add_private (c, sizeof (DisplayBlankingStatusPluginPrivate));
114 display_blanking_status_plugin_mode_set (DisplayBlankingStatusPluginPrivate *priv,
117 // Should be enough if DISPLAY_BLANKING_MODES stays in 1 digit, "%d"
118 // provides space for that digit and '\0'
119 static char icon_name[sizeof (DISPLAY_BLANKING_ICON_TEMPLATE)];
121 // Get the mode to set
122 gint mode = gconf_client_get_int (priv->gconf_client,
123 GCONF_KEY_DISPLAY_BLANKING, NULL);
125 mode = (mode + 1) % DISPLAY_BLANKING_MODES;
127 // Toggle display blanking
128 gconf_client_set_int (priv->gconf_client, GCONF_KEY_DISPLAY_BLANKING,
131 // Update button text and status bar icon
132 hildon_button_set_value (HILDON_BUTTON (priv->button),
133 dgettext (GETTEXT_DOM, _DisplayBlankingDescription[mode]));
134 int r = snprintf (icon_name, sizeof (icon_name),
135 DISPLAY_BLANKING_ICON_TEMPLATE, mode);
136 g_assert(r < sizeof (icon_name)); // otherwise it was truncated
137 GtkWidget *icon = gtk_image_new_from_icon_name (icon_name,
138 GTK_ICON_SIZE_DIALOG);
139 hildon_button_set_image (HILDON_BUTTON (priv->button), icon);
141 // 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[mode]);
146 hildon_banner_set_timeout (HILDON_BANNER (banner), 5000);
151 display_blanking_status_plugin_on_button_clicked (GtkWidget *button,
152 DisplayBlankingStatusPlugin *plugin)
154 display_blanking_status_plugin_mode_set (
155 DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE (plugin), TRUE);
159 display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin)
161 plugin->priv = DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE (plugin);
163 plugin->priv->gconf_client = gconf_client_get_default();
164 g_assert(GCONF_IS_CLIENT(plugin->priv->gconf_client));
166 plugin->priv->button = hildon_button_new (HILDON_SIZE_FINGER_HEIGHT |
167 HILDON_SIZE_AUTO_WIDTH, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
168 gtk_button_set_alignment (GTK_BUTTON (plugin->priv->button), 0, 0);
169 hildon_button_set_style (HILDON_BUTTON (plugin->priv->button),
170 HILDON_BUTTON_STYLE_PICKER);
171 hildon_button_set_title (HILDON_BUTTON (plugin->priv->button),
172 dgettext (GETTEXT_DOM, "Display blanking mode"));
174 display_blanking_status_plugin_mode_set (plugin->priv, FALSE);
176 g_signal_connect (plugin->priv->button, "clicked",
177 G_CALLBACK (display_blanking_status_plugin_on_button_clicked),
180 gtk_container_add (GTK_CONTAINER (plugin), plugin->priv->button);
182 gtk_widget_show_all (plugin->priv->button);
184 gtk_widget_show (GTK_WIDGET (plugin));