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>
30 #include <dbus/dbus.h>
31 #include <mce/dbus-names.h>
34 #define TYPE_DISPLAY_BLANKING_STATUS_PLUGIN (display_blanking_status_plugin_get_type ())
36 #define DISPLAY_BLANKING_STATUS_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
37 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPlugin))
39 #define DISPLAY_BLANKING_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), \
40 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPluginClass))
42 #define IS_DISPLAY_BLANKING_STATUS_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
43 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN))
45 #define IS_DISPLAY_BLANKING_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), \
46 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN))
48 #define DISPLAY_BLANKING_STATUS_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
49 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, DisplayBlankingStatusPluginClass))
51 #define STATUS_AREA_DISPLAY_BLANKING_ICON_SIZE 18
53 typedef struct _DisplayBlankingStatusPlugin DisplayBlankingStatusPlugin;
54 typedef struct _DisplayBlankingStatusPluginClass DisplayBlankingStatusPluginClass;
55 typedef struct _DisplayBlankingStatusPluginPrivate DisplayBlankingStatusPluginPrivate;
57 struct _DisplayBlankingStatusPlugin
59 HDStatusMenuItem parent;
61 DisplayBlankingStatusPluginPrivate *priv;
64 struct _DisplayBlankingStatusPluginClass
66 HDStatusMenuItemClass parent;
69 GType display_blanking_status_plugin_get_type (void);
71 #define DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE(obj) \
72 (G_TYPE_INSTANCE_GET_PRIVATE (obj, \
73 TYPE_DISPLAY_BLANKING_STATUS_PLUGIN, \
74 DisplayBlankingStatusPluginPrivate))
76 #define MODE_GCONF_ROOT "/system/osso/dsm/display"
77 #define MODE_GCONF_KEY MODE_GCONF_ROOT "/inhibit_blank_mode"
79 #define HOURS_GCONF_KEY "/apps/Maemo/sadba/timed_inhibit_hours"
80 #define MINUTES_GCONF_KEY "/apps/Maemo/sadba/timed_inhibit_minutes"
82 #define BANNER_DURATION 5000 // in milliseconds
83 #define INHIBIT_MSG_INTERVAL 30 // in seconds
85 #define GETTEXT_DOM "status-area-displayblanking-applet"
86 #define _(str) dgettext (GETTEXT_DOM, (str))
87 #define gettext_noop(str) (str)
88 #define N_(str) gettext_noop(str)
90 // Undocumented blanking modes as reported by David Weinehall from Nokia:
91 // http://www.gossamer-threads.com/lists/maemo/developers/61201#61201
92 #define BLANKING_MODES 5
93 static const char *mode_title[BLANKING_MODES] =
96 N_ ("Both only on battery"),
97 N_ ("Blanking only on battery"),
101 static const char *mode_desc[BLANKING_MODES] =
103 N_ ("Blanking and dimming always enabled"),
104 N_ ("Blanking and dimming enabled only on battery"),
105 N_ ("Always dim, blank only on battery"),
106 N_ ("Blanking and dimming always disabled"),
107 N_ ("Blanking disabled, dimming enabled")
109 static const char *mode_icon_name[BLANKING_MODES] =
111 "display-blanking-icon.0",
112 "display-blanking-icon.1",
113 "display-blanking-icon.2",
114 "display-blanking-icon.3",
115 "display-blanking-icon.4",
117 #define INHIBIT_ICON_NAME "display-blanking-inhibit-icon"
118 #define TIMED_INHIBIT_ICON_NAME "display-blanking-inhibit-icon.timed"
119 #define INHIBIT_STATUS_ICON_NAME "display-blanking-status"
121 struct _DisplayBlankingStatusPluginPrivate
123 DisplayBlankingStatusPlugin* plugin;
124 GConfClient *gconf_client;
125 DBusConnection* dbus_conn;
126 DBusMessage* dbus_msg;
127 GtkWidget *mode_button;
128 GtkWidget *mode_dialog;
129 GtkWidget *inhibit_button;
130 GtkWidget *timed_inhibit_button;
131 GtkWidget *timed_inhibit_dialog;
132 gint inhibit_timer_id; // if == 0
133 gint timed_inhibit_timer_id; // no timer is set
134 // gtk_toggle_button_set_active () triggers the "clicked" signal on the
135 // affected button, since we don't want to process the signal while
136 // changing the "pressed" state (we want just the GUI to change, we use
137 // this flag to ignore the "clicke" signal handler when is TRUE
138 gboolean inhibit_in_signal;
142 HD_DEFINE_PLUGIN_MODULE (DisplayBlankingStatusPlugin,
143 display_blanking_status_plugin, HD_TYPE_STATUS_MENU_ITEM)
146 display_blanking_status_plugin_class_finalize (
147 DisplayBlankingStatusPluginClass *klass)
152 display_blanking_status_plugin_class_init (DisplayBlankingStatusPluginClass *c)
154 g_type_class_add_private (c, sizeof (DisplayBlankingStatusPluginPrivate));
158 update_mode_gui (gint mode, DisplayBlankingStatusPluginPrivate *priv)
160 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[mode],
161 GTK_ICON_SIZE_DIALOG);
162 gtk_button_set_image (GTK_BUTTON (priv->mode_button), icon);
166 disable_timer (gint *timer_id)
168 g_assert (*timer_id != 0);
169 gboolean ok = g_source_remove (*timer_id);
170 g_assert (ok == TRUE);
175 disable_inhibition (DisplayBlankingStatusPluginPrivate *priv)
177 disable_timer (&(priv->inhibit_timer_id));
178 hd_status_plugin_item_set_status_area_icon (
179 HD_STATUS_PLUGIN_ITEM (priv->plugin), NULL);
183 inhibit_display_blanking (DisplayBlankingStatusPluginPrivate *priv)
185 dbus_bool_t ok = dbus_connection_send (priv->dbus_conn, priv->dbus_msg,
187 g_assert (ok == TRUE);
191 on_inhibit_timeout (DisplayBlankingStatusPluginPrivate *priv)
193 inhibit_display_blanking (priv);
199 on_timed_inhibit_timeout (DisplayBlankingStatusPluginPrivate *priv)
201 disable_inhibition (priv);
202 disable_timer (&(priv->timed_inhibit_timer_id));
204 priv->inhibit_in_signal = TRUE;
205 gtk_toggle_button_set_active (
206 GTK_TOGGLE_BUTTON (priv->timed_inhibit_button), FALSE);
207 priv->inhibit_in_signal = FALSE;
209 GtkWidget *banner = hildon_banner_show_information (
210 priv->timed_inhibit_button, NULL,
211 _ ("Display blanking inhibition disabled"));
212 hildon_banner_set_timeout (HILDON_BANNER (banner), BANNER_DURATION);
218 enable_inhibition (DisplayBlankingStatusPluginPrivate *priv)
220 inhibit_display_blanking (priv);
222 g_assert (priv->inhibit_timer_id == 0);
223 priv->inhibit_timer_id = g_timeout_add_seconds (INHIBIT_MSG_INTERVAL,
224 (GSourceFunc) on_inhibit_timeout, priv);
225 g_assert (priv->inhibit_timer_id > 0);
227 GtkIconTheme *icon_theme = gtk_icon_theme_get_default ();
228 GdkPixbuf *pixbuf = gtk_icon_theme_load_icon (icon_theme,
229 INHIBIT_STATUS_ICON_NAME, 18, GTK_ICON_LOOKUP_NO_SVG, NULL);
230 hd_status_plugin_item_set_status_area_icon (
231 HD_STATUS_PLUGIN_ITEM (priv->plugin), pixbuf);
235 on_inhibit_button_clicked (GtkWidget *button,
236 DisplayBlankingStatusPluginPrivate *priv)
238 if (priv->inhibit_in_signal)
241 GtkWidget *parent = gtk_widget_get_ancestor (button, GTK_TYPE_WINDOW);
242 gtk_widget_hide (parent);
244 gboolean self_pressed = gtk_toggle_button_get_active (
245 GTK_TOGGLE_BUTTON (button));
246 gboolean other_pressed = gtk_toggle_button_get_active (
247 GTK_TOGGLE_BUTTON (priv->timed_inhibit_button));
249 if (self_pressed && other_pressed) {
250 g_assert (priv->inhibit_timer_id != 0);
252 disable_timer (&(priv->timed_inhibit_timer_id));
254 priv->inhibit_in_signal = TRUE;
255 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
256 priv->timed_inhibit_button), FALSE);
257 priv->inhibit_in_signal = FALSE;
259 else if (self_pressed && !other_pressed) {
260 g_assert (priv->timed_inhibit_timer_id == 0);
262 enable_inhibition (priv);
264 else if (!self_pressed) {
265 g_assert (!other_pressed);
266 g_assert (priv->timed_inhibit_timer_id == 0);
268 disable_inhibition (priv);
275 timed_inhibit_picker_new (const gchar* title, gsize current, guint max,
278 g_assert (max < 100);
279 static gchar buffer[3]; // 2 for the number + 1 for \0
281 GtkWidget *selector = hildon_touch_selector_entry_new_text ();
283 for (int i = 0; i*step <= max; i++) {
284 if (i*step == current)
286 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
287 g_ascii_formatd (buffer, 3, "%.0f", i*step));
289 hildon_gtk_entry_set_input_mode (GTK_ENTRY (
290 hildon_touch_selector_entry_get_entry (
291 HILDON_TOUCH_SELECTOR_ENTRY (selector))),
292 HILDON_GTK_INPUT_MODE_NUMERIC);
294 GtkWidget *picker = hildon_picker_button_new (HILDON_SIZE_FINGER_HEIGHT,
295 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
296 hildon_button_set_title (HILDON_BUTTON (picker), title);
297 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (picker),
298 HILDON_TOUCH_SELECTOR (selector));
299 hildon_picker_button_set_active (HILDON_PICKER_BUTTON (picker),
301 hildon_button_set_value (HILDON_BUTTON (picker),
302 g_ascii_formatd (buffer, 3, "%.0f", current));
304 g_object_set_data (G_OBJECT (picker), "max", GUINT_TO_POINTER (max));
310 timed_inhibit_picker_get_value (GtkWidget *picker)
312 guint max = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (picker), "max"));
314 return CLAMP (g_ascii_strtod (hildon_button_get_value (
315 HILDON_BUTTON (picker)), NULL), 0, max);
319 timed_inhibit_get_input (DisplayBlankingStatusPluginPrivate *priv)
321 g_assert (priv->timed_inhibit_dialog == NULL);
322 priv->timed_inhibit_dialog = gtk_dialog_new_with_buttons (
323 _ ("Inhibit display blanking for..."), NULL, GTK_DIALOG_MODAL,
324 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
325 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
327 GtkWidget *h_picker = timed_inhibit_picker_new (_ ("Hours"),
328 gconf_client_get_int (priv->gconf_client, HOURS_GCONF_KEY, NULL),
330 GtkWidget *m_picker = timed_inhibit_picker_new (_ ("Minutes"),
331 gconf_client_get_int (priv->gconf_client, MINUTES_GCONF_KEY, NULL),
334 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
335 g_assert (hbox != NULL);
337 gtk_container_add (GTK_CONTAINER (hbox), h_picker);
338 gtk_container_add (GTK_CONTAINER (hbox), m_picker);
340 GtkWidget *content_area = gtk_dialog_get_content_area (
341 GTK_DIALOG (priv->timed_inhibit_dialog));
342 gtk_container_add (GTK_CONTAINER (content_area), hbox);
344 gtk_widget_show_all (priv->timed_inhibit_dialog);
346 gint result = gtk_dialog_run (GTK_DIALOG (priv->timed_inhibit_dialog));
349 if (result == GTK_RESPONSE_ACCEPT) {
350 gint hours = timed_inhibit_picker_get_value (h_picker);
351 gint mins = timed_inhibit_picker_get_value (m_picker);
354 gconf_client_set_int (priv->gconf_client, HOURS_GCONF_KEY, hours, &e);
355 g_assert (e == NULL);
356 gconf_client_set_int (priv->gconf_client, MINUTES_GCONF_KEY, mins, &e);
357 g_assert (e == NULL);
359 timeout = hours*3600 + mins*60;
362 gtk_widget_destroy (priv->timed_inhibit_dialog);
363 priv->timed_inhibit_dialog = NULL;
369 on_timed_inhibit_button_clicked (GtkWidget *button,
370 DisplayBlankingStatusPluginPrivate *priv)
372 if (priv->inhibit_in_signal)
375 GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button),
377 gtk_widget_hide (parent);
379 gboolean self_pressed = gtk_toggle_button_get_active (
380 GTK_TOGGLE_BUTTON (button));
381 gboolean other_pressed = gtk_toggle_button_get_active (
382 GTK_TOGGLE_BUTTON (priv->inhibit_button));
385 g_assert (priv->timed_inhibit_timer_id == 0);
387 g_assert (priv->inhibit_timer_id != 0);
389 guint timeout = timed_inhibit_get_input (priv);
392 priv->inhibit_in_signal = TRUE;
393 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
394 priv->inhibit_button), FALSE);
395 priv->inhibit_in_signal = FALSE;
398 enable_inhibition (priv);
400 priv->timed_inhibit_timer_id = g_timeout_add_seconds (timeout,
401 (GSourceFunc) on_timed_inhibit_timeout, priv);
402 g_assert (priv->timed_inhibit_timer_id > 0);
405 priv->inhibit_in_signal = TRUE;
406 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
407 priv->timed_inhibit_button), FALSE);
408 priv->inhibit_in_signal = FALSE;
411 else { // !self_pressed
412 g_assert (!other_pressed);
414 disable_inhibition (priv);
415 disable_timer (&(priv->timed_inhibit_timer_id));
420 on_mode_dialog_button_clicked (GtkWidget *button, GtkDialog *dialog)
422 const gchar *title = hildon_button_get_title (HILDON_BUTTON (button));
424 gint *mode = (gint *) g_object_get_data (G_OBJECT (dialog), "mode");
425 g_assert (mode != NULL);
427 for (*mode = 0; *mode < BLANKING_MODES; (*mode)++) {
428 if (strcmp (title, mode_title[*mode]) == 0)
431 g_assert (*mode < BLANKING_MODES);
433 gtk_dialog_response (dialog, GTK_RESPONSE_OK);
437 mode_get_input (DisplayBlankingStatusPluginPrivate *priv)
439 g_assert (priv->mode_dialog == NULL);
440 priv->mode_dialog = gtk_dialog_new ();
441 gtk_window_set_modal (GTK_WINDOW (priv->mode_dialog), TRUE);
442 gtk_window_set_title (GTK_WINDOW (priv->mode_dialog),
443 _ ("Select display blanking mode"));
445 GtkWidget *pan_area = hildon_pannable_area_new ();
446 g_assert (pan_area != NULL);
448 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
449 g_assert (vbox != NULL);
451 hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pan_area),
453 GtkWidget *content_area = gtk_dialog_get_content_area (
454 GTK_DIALOG (priv->mode_dialog));
455 gtk_box_pack_start (GTK_BOX (content_area), pan_area, TRUE, TRUE, 0);
457 gtk_widget_set_size_request (pan_area, -1, MIN (350, BLANKING_MODES * 70));
459 gint mode = BLANKING_MODES;
460 for (int i = 0; i < BLANKING_MODES; i++) {
462 hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT,
463 HILDON_BUTTON_ARRANGEMENT_VERTICAL, _ (mode_title[i]),
465 hildon_button_set_style (HILDON_BUTTON (button),
466 HILDON_BUTTON_STYLE_PICKER);
467 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[i],
468 GTK_ICON_SIZE_DIALOG);
469 hildon_button_set_image (HILDON_BUTTON (button), icon);
470 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
471 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
472 g_signal_connect (button, "clicked",
473 G_CALLBACK (on_mode_dialog_button_clicked), priv->mode_dialog);
476 g_object_set_data (G_OBJECT (priv->mode_dialog), "mode", &mode);
477 gtk_widget_show_all (priv->mode_dialog);
478 gtk_dialog_run (GTK_DIALOG (priv->mode_dialog));
480 gtk_widget_destroy (priv->mode_dialog);
481 priv->mode_dialog = NULL;
487 on_mode_button_clicked (GtkWidget *button,
488 DisplayBlankingStatusPluginPrivate *priv)
490 GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button),
492 gtk_widget_hide (parent);
494 gint mode = mode_get_input (priv);
496 if (mode != BLANKING_MODES) {
497 // will trigger the gconf notify signal
498 GError *error = NULL;
499 gconf_client_set_int (priv->gconf_client, MODE_GCONF_KEY, mode, &error);
500 g_assert (error == NULL);
505 on_gconf_notify (GConfClient* client, guint cnxn_id, GConfEntry* entry,
506 DisplayBlankingStatusPluginPrivate* priv)
508 const gchar* key = gconf_entry_get_key (entry);
509 g_assert (key != NULL);
511 // Ignore notification about keys we don't care about
512 if (strcmp (key, MODE_GCONF_KEY) != 0)
515 const GConfValue* value = gconf_entry_get_value (entry);
516 g_assert (value != NULL);
517 g_assert (GCONF_VALUE_TYPE_VALID (value->type));
518 g_assert (value->type == GCONF_VALUE_INT);
520 gint mode = gconf_value_get_int (value);
521 update_mode_gui (mode, priv);
525 init_gconf (DisplayBlankingStatusPluginPrivate *priv)
527 GError* error = NULL;
529 priv->gconf_client = gconf_client_get_default ();
530 g_assert (GCONF_IS_CLIENT (priv->gconf_client));
532 gconf_client_add_dir (priv->gconf_client, MODE_GCONF_ROOT,
533 GCONF_CLIENT_PRELOAD_NONE, &error);
534 g_assert (error == NULL);
536 gconf_client_notify_add (priv->gconf_client, MODE_GCONF_KEY,
537 (GConfClientNotifyFunc) &on_gconf_notify, priv, NULL, &error);
538 g_assert (error == NULL);
542 init_dbus (DisplayBlankingStatusPluginPrivate *priv)
545 dbus_error_init (&error);
547 priv->dbus_conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
548 g_assert (!dbus_error_is_set (&error));
549 g_assert (priv->dbus_conn != NULL);
551 priv->dbus_msg = dbus_message_new_method_call (MCE_SERVICE,
552 MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_PREVENT_BLANK_REQ);
553 g_assert (priv->dbus_msg != NULL);
554 dbus_message_set_no_reply (priv->dbus_msg, TRUE);
558 init_mode_gui (DisplayBlankingStatusPluginPrivate *priv)
560 priv->mode_dialog = NULL;
561 priv->mode_button = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT |
562 HILDON_SIZE_AUTO_WIDTH);
564 GError* error = NULL;
565 gint mode = gconf_client_get_int (priv->gconf_client, MODE_GCONF_KEY,
567 g_assert (error == NULL);
568 update_mode_gui (mode, priv);
570 g_signal_connect (priv->mode_button, "clicked",
571 G_CALLBACK (on_mode_button_clicked), priv);
575 inhibit_button_new (const gchar *icon_name,
576 void (*cb) (GtkWidget *, DisplayBlankingStatusPluginPrivate *),
580 GtkWidget *b = hildon_gtk_toggle_button_new (HILDON_SIZE_FINGER_HEIGHT |
581 HILDON_SIZE_AUTO_WIDTH);
582 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (b), FALSE);
583 GtkWidget *icon = gtk_image_new_from_icon_name (icon_name,
584 GTK_ICON_SIZE_DIALOG);
585 gtk_button_set_image (GTK_BUTTON (b), icon);
586 g_signal_connect (b, "clicked", G_CALLBACK (cb), cb_data);
592 init_inhibit_gui (DisplayBlankingStatusPluginPrivate *priv)
594 priv->inhibit_in_signal = FALSE;
596 priv->inhibit_timer_id = 0;
597 priv->timed_inhibit_timer_id = 0;
599 priv->inhibit_button = inhibit_button_new (INHIBIT_ICON_NAME,
600 on_inhibit_button_clicked, priv);
602 priv->timed_inhibit_button = inhibit_button_new (
603 TIMED_INHIBIT_ICON_NAME, on_timed_inhibit_button_clicked, priv);
607 display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin)
609 DisplayBlankingStatusPluginPrivate *priv;
611 priv = DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE (plugin);
613 priv->plugin = plugin;
617 init_mode_gui (priv);
618 init_inhibit_gui (priv);
620 GtkWidget *hbbox = gtk_hbutton_box_new ();
621 g_assert (hbbox != NULL);
623 gtk_container_add (GTK_CONTAINER (hbbox), priv->mode_button);
624 gtk_container_add (GTK_CONTAINER (hbbox), priv->inhibit_button);
625 gtk_container_add (GTK_CONTAINER (hbbox), priv->timed_inhibit_button);
627 gtk_container_add (GTK_CONTAINER (plugin), hbbox);
629 gtk_widget_show_all (GTK_WIDGET (plugin));