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 INHIBIT_MSG_INTERVAL 30 // in seconds
84 #define GETTEXT_DOM "status-area-displayblanking-applet"
85 #define _(str) dgettext (GETTEXT_DOM, (str))
86 #define gettext_noop(str) (str)
87 #define N_(str) gettext_noop(str)
89 // Undocumented blanking modes as reported by David Weinehall from Nokia:
90 // http://www.gossamer-threads.com/lists/maemo/developers/61201#61201
91 #define BLANKING_MODES 5
92 static const char *mode_title[BLANKING_MODES] =
95 N_ ("Both only on battery"),
96 N_ ("Blanking only on battery"),
100 static const char *mode_desc[BLANKING_MODES] =
102 N_ ("Blanking and dimming always enabled"),
103 N_ ("Blanking and dimming enabled only on battery"),
104 N_ ("Always dim, blank only on battery"),
105 N_ ("Blanking and dimming always disabled"),
106 N_ ("Blanking disabled, dimming enabled")
108 static const char *mode_icon_name[BLANKING_MODES] =
110 "display-blanking-icon.0",
111 "display-blanking-icon.1",
112 "display-blanking-icon.2",
113 "display-blanking-icon.3",
114 "display-blanking-icon.4",
116 #define INHIBIT_ICON_NAME "display-blanking-inhibit-icon"
117 #define TIMED_INHIBIT_ICON_NAME "display-blanking-inhibit-icon.timed"
118 #define INHIBIT_STATUS_ICON_NAME "display-blanking-status"
120 struct _DisplayBlankingStatusPluginPrivate
122 DisplayBlankingStatusPlugin* plugin;
123 GConfClient *gconf_client;
124 DBusConnection* dbus_conn;
125 DBusMessage* dbus_msg;
126 GtkWidget *mode_button;
127 GtkWidget *mode_dialog;
128 GtkWidget *inhibit_button;
129 GtkWidget *timed_inhibit_button;
130 GtkWidget *timed_inhibit_dialog;
131 gint inhibit_timer_id; // if == 0
132 gint timed_inhibit_timer_id; // no timer is set
133 // gtk_toggle_button_set_active () triggers the "clicked" signal on the
134 // affected button, since we don't want to process the signal while
135 // changing the "pressed" state (we want just the GUI to change, we use
136 // this flag to ignore the "clicke" signal handler when is TRUE
137 gboolean inhibit_in_signal;
141 HD_DEFINE_PLUGIN_MODULE (DisplayBlankingStatusPlugin,
142 display_blanking_status_plugin, HD_TYPE_STATUS_MENU_ITEM)
145 display_blanking_status_plugin_class_finalize (
146 DisplayBlankingStatusPluginClass *klass)
151 display_blanking_status_plugin_class_init (DisplayBlankingStatusPluginClass *c)
153 g_type_class_add_private (c, sizeof (DisplayBlankingStatusPluginPrivate));
157 update_mode_gui (gint mode, DisplayBlankingStatusPluginPrivate *priv)
159 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[mode],
160 GTK_ICON_SIZE_DIALOG);
161 gtk_button_set_image (GTK_BUTTON (priv->mode_button), icon);
165 disable_timer (gint *timer_id)
167 g_assert (*timer_id != 0);
168 gboolean ok = g_source_remove (*timer_id);
169 g_assert (ok == TRUE);
174 disable_inhibition (DisplayBlankingStatusPluginPrivate *priv)
176 disable_timer (&(priv->inhibit_timer_id));
177 hd_status_plugin_item_set_status_area_icon (
178 HD_STATUS_PLUGIN_ITEM (priv->plugin), NULL);
182 inhibit_display_blanking (DisplayBlankingStatusPluginPrivate *priv)
184 dbus_bool_t ok = dbus_connection_send (priv->dbus_conn, priv->dbus_msg,
186 g_assert (ok == TRUE);
190 on_inhibit_timeout (DisplayBlankingStatusPluginPrivate *priv)
192 inhibit_display_blanking (priv);
198 on_timed_inhibit_timeout (DisplayBlankingStatusPluginPrivate *priv)
200 disable_inhibition (priv);
201 disable_timer (&(priv->timed_inhibit_timer_id));
203 priv->inhibit_in_signal = TRUE;
204 gtk_toggle_button_set_active (
205 GTK_TOGGLE_BUTTON (priv->timed_inhibit_button), FALSE);
206 priv->inhibit_in_signal = FALSE;
208 GtkWidget *banner = hildon_banner_show_information (
209 priv->timed_inhibit_button, NULL,
210 _ ("Display blanking inhibition disabled"));
211 hildon_banner_set_timeout (HILDON_BANNER (banner), 5000);
217 enable_inhibition (DisplayBlankingStatusPluginPrivate *priv)
219 inhibit_display_blanking (priv);
221 g_assert (priv->inhibit_timer_id == 0);
222 priv->inhibit_timer_id = g_timeout_add_seconds (INHIBIT_MSG_INTERVAL,
223 (GSourceFunc) on_inhibit_timeout, priv);
224 g_assert (priv->inhibit_timer_id > 0);
226 GtkIconTheme *icon_theme = gtk_icon_theme_get_default ();
227 GdkPixbuf *pixbuf = gtk_icon_theme_load_icon (icon_theme,
228 INHIBIT_STATUS_ICON_NAME, 18, GTK_ICON_LOOKUP_NO_SVG, NULL);
229 hd_status_plugin_item_set_status_area_icon (
230 HD_STATUS_PLUGIN_ITEM (priv->plugin), pixbuf);
234 on_inhibit_button_clicked (GtkWidget *button,
235 DisplayBlankingStatusPluginPrivate *priv)
237 if (priv->inhibit_in_signal)
240 GtkWidget *parent = gtk_widget_get_ancestor (button, GTK_TYPE_WINDOW);
241 gtk_widget_hide (parent);
243 gboolean self_pressed = gtk_toggle_button_get_active (
244 GTK_TOGGLE_BUTTON (button));
245 gboolean other_pressed = gtk_toggle_button_get_active (
246 GTK_TOGGLE_BUTTON (priv->timed_inhibit_button));
248 if (self_pressed && other_pressed) {
249 g_assert (priv->inhibit_timer_id != 0);
251 disable_timer (&(priv->timed_inhibit_timer_id));
253 priv->inhibit_in_signal = TRUE;
254 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
255 priv->timed_inhibit_button), FALSE);
256 priv->inhibit_in_signal = FALSE;
258 else if (self_pressed && !other_pressed) {
259 g_assert (priv->timed_inhibit_timer_id == 0);
261 enable_inhibition (priv);
263 else if (!self_pressed) {
264 g_assert (!other_pressed);
265 g_assert (priv->timed_inhibit_timer_id == 0);
267 disable_inhibition (priv);
274 timed_inhibit_picker_new (const gchar* title, gsize current, guint max,
277 g_assert (max < 100);
278 static gchar buffer[3]; // 2 for the number + 1 for \0
280 GtkWidget *selector = hildon_touch_selector_entry_new_text ();
282 for (int i = 0; i*step <= max; i++) {
283 if (i*step == current)
285 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
286 g_ascii_formatd (buffer, 3, "%.0f", i*step));
288 hildon_gtk_entry_set_input_mode (GTK_ENTRY (
289 hildon_touch_selector_entry_get_entry (
290 HILDON_TOUCH_SELECTOR_ENTRY (selector))),
291 HILDON_GTK_INPUT_MODE_NUMERIC);
293 GtkWidget *picker = hildon_picker_button_new (HILDON_SIZE_FINGER_HEIGHT,
294 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
295 hildon_button_set_title (HILDON_BUTTON (picker), title);
296 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (picker),
297 HILDON_TOUCH_SELECTOR (selector));
298 hildon_picker_button_set_active (HILDON_PICKER_BUTTON (picker),
300 hildon_button_set_value (HILDON_BUTTON (picker),
301 g_ascii_formatd (buffer, 3, "%.0f", current));
303 g_object_set_data (G_OBJECT (picker), "max", GUINT_TO_POINTER (max));
309 timed_inhibit_picker_get_value (GtkWidget *picker)
311 guint max = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (picker), "max"));
313 return CLAMP (g_ascii_strtod (hildon_button_get_value (
314 HILDON_BUTTON (picker)), NULL), 0, max);
318 timed_inhibit_get_input (DisplayBlankingStatusPluginPrivate *priv)
320 g_assert (priv->timed_inhibit_dialog == NULL);
321 priv->timed_inhibit_dialog = gtk_dialog_new_with_buttons (
322 _ ("Inhibit display blanking for..."), NULL, GTK_DIALOG_MODAL,
323 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
324 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
326 GtkWidget *h_picker = timed_inhibit_picker_new (_ ("Hours"),
327 gconf_client_get_int (priv->gconf_client, HOURS_GCONF_KEY, NULL),
329 GtkWidget *m_picker = timed_inhibit_picker_new (_ ("Minutes"),
330 gconf_client_get_int (priv->gconf_client, MINUTES_GCONF_KEY, NULL),
333 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
334 g_assert (hbox != NULL);
336 gtk_container_add (GTK_CONTAINER (hbox), h_picker);
337 gtk_container_add (GTK_CONTAINER (hbox), m_picker);
339 GtkWidget *content_area = gtk_dialog_get_content_area (
340 GTK_DIALOG (priv->timed_inhibit_dialog));
341 gtk_container_add (GTK_CONTAINER (content_area), hbox);
343 gtk_widget_show_all (priv->timed_inhibit_dialog);
345 gint result = gtk_dialog_run (GTK_DIALOG (priv->timed_inhibit_dialog));
348 if (result == GTK_RESPONSE_ACCEPT) {
349 gint hours = timed_inhibit_picker_get_value (h_picker);
350 gint mins = timed_inhibit_picker_get_value (m_picker);
353 gconf_client_set_int (priv->gconf_client, HOURS_GCONF_KEY, hours, &e);
354 g_assert (e == NULL);
355 gconf_client_set_int (priv->gconf_client, MINUTES_GCONF_KEY, mins, &e);
356 g_assert (e == NULL);
358 timeout = hours*3600 + mins*60;
361 gtk_widget_destroy (priv->timed_inhibit_dialog);
362 priv->timed_inhibit_dialog = NULL;
368 on_timed_inhibit_button_clicked (GtkWidget *button,
369 DisplayBlankingStatusPluginPrivate *priv)
371 if (priv->inhibit_in_signal)
374 GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button),
376 gtk_widget_hide (parent);
378 gboolean self_pressed = gtk_toggle_button_get_active (
379 GTK_TOGGLE_BUTTON (button));
380 gboolean other_pressed = gtk_toggle_button_get_active (
381 GTK_TOGGLE_BUTTON (priv->inhibit_button));
384 g_assert (priv->timed_inhibit_timer_id == 0);
386 g_assert (priv->inhibit_timer_id != 0);
388 guint timeout = timed_inhibit_get_input (priv);
391 priv->inhibit_in_signal = TRUE;
392 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
393 priv->inhibit_button), FALSE);
394 priv->inhibit_in_signal = FALSE;
397 enable_inhibition (priv);
399 priv->timed_inhibit_timer_id = g_timeout_add_seconds (timeout,
400 (GSourceFunc) on_timed_inhibit_timeout, priv);
401 g_assert (priv->timed_inhibit_timer_id > 0);
404 priv->inhibit_in_signal = TRUE;
405 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
406 priv->timed_inhibit_button), FALSE);
407 priv->inhibit_in_signal = FALSE;
410 else { // !self_pressed
411 g_assert (!other_pressed);
413 disable_inhibition (priv);
414 disable_timer (&(priv->timed_inhibit_timer_id));
419 on_mode_dialog_button_clicked (GtkWidget *button, GtkDialog *dialog)
421 const gchar *title = hildon_button_get_title (HILDON_BUTTON (button));
423 gint *mode = (gint *) g_object_get_data (G_OBJECT (dialog), "mode");
424 g_assert (mode != NULL);
426 for (*mode = 0; *mode < BLANKING_MODES; (*mode)++) {
427 if (strcmp (title, mode_title[*mode]) == 0)
430 g_assert (*mode < BLANKING_MODES);
432 gtk_dialog_response (dialog, GTK_RESPONSE_OK);
436 mode_get_input (DisplayBlankingStatusPluginPrivate *priv)
438 g_assert (priv->mode_dialog == NULL);
439 priv->mode_dialog = gtk_dialog_new ();
440 gtk_window_set_modal (GTK_WINDOW (priv->mode_dialog), TRUE);
441 gtk_window_set_title (GTK_WINDOW (priv->mode_dialog),
442 _ ("Select display blanking mode"));
444 GtkWidget *pan_area = hildon_pannable_area_new ();
445 g_assert (pan_area != NULL);
447 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
448 g_assert (vbox != NULL);
450 hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pan_area),
452 GtkWidget *content_area = gtk_dialog_get_content_area (
453 GTK_DIALOG (priv->mode_dialog));
454 gtk_box_pack_start (GTK_BOX (content_area), pan_area, TRUE, TRUE, 0);
456 gtk_widget_set_size_request (pan_area, -1, MIN (350, BLANKING_MODES * 70));
458 gint mode = BLANKING_MODES;
459 for (int i = 0; i < BLANKING_MODES; i++) {
461 hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT,
462 HILDON_BUTTON_ARRANGEMENT_VERTICAL, _ (mode_title[i]),
464 hildon_button_set_style (HILDON_BUTTON (button),
465 HILDON_BUTTON_STYLE_PICKER);
466 GtkWidget *icon = gtk_image_new_from_icon_name (mode_icon_name[i],
467 GTK_ICON_SIZE_DIALOG);
468 hildon_button_set_image (HILDON_BUTTON (button), icon);
469 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
470 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
471 g_signal_connect (button, "clicked",
472 G_CALLBACK (on_mode_dialog_button_clicked), priv->mode_dialog);
475 g_object_set_data (G_OBJECT (priv->mode_dialog), "mode", &mode);
476 gtk_widget_show_all (priv->mode_dialog);
477 gtk_dialog_run (GTK_DIALOG (priv->mode_dialog));
479 gtk_widget_destroy (priv->mode_dialog);
480 priv->mode_dialog = NULL;
486 on_mode_button_clicked (GtkWidget *button,
487 DisplayBlankingStatusPluginPrivate *priv)
489 GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button),
491 gtk_widget_hide (parent);
493 gint mode = mode_get_input (priv);
495 if (mode != BLANKING_MODES) {
496 // will trigger the gconf notify signal
497 GError *error = NULL;
498 gconf_client_set_int (priv->gconf_client, MODE_GCONF_KEY, mode, &error);
499 g_assert (error == NULL);
504 on_gconf_notify (GConfClient* client, guint cnxn_id, GConfEntry* entry,
505 DisplayBlankingStatusPluginPrivate* priv)
507 const gchar* key = gconf_entry_get_key (entry);
508 g_assert (key != NULL);
510 // Ignore notification about keys we don't care about
511 if (strcmp (key, MODE_GCONF_KEY) != 0)
514 const GConfValue* value = gconf_entry_get_value (entry);
515 g_assert (value != NULL);
516 g_assert (GCONF_VALUE_TYPE_VALID (value->type));
517 g_assert (value->type == GCONF_VALUE_INT);
519 gint mode = gconf_value_get_int (value);
520 update_mode_gui (mode, priv);
524 init_gconf (DisplayBlankingStatusPluginPrivate *priv)
526 GError* error = NULL;
528 priv->gconf_client = gconf_client_get_default ();
529 g_assert (GCONF_IS_CLIENT (priv->gconf_client));
531 gconf_client_add_dir (priv->gconf_client, MODE_GCONF_ROOT,
532 GCONF_CLIENT_PRELOAD_NONE, &error);
533 g_assert (error == NULL);
535 gconf_client_notify_add (priv->gconf_client, MODE_GCONF_KEY,
536 (GConfClientNotifyFunc) &on_gconf_notify, priv, NULL, &error);
537 g_assert (error == NULL);
541 init_dbus (DisplayBlankingStatusPluginPrivate *priv)
544 dbus_error_init (&error);
546 priv->dbus_conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
547 g_assert (!dbus_error_is_set (&error));
548 g_assert (priv->dbus_conn != NULL);
550 priv->dbus_msg = dbus_message_new_method_call (MCE_SERVICE,
551 MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_PREVENT_BLANK_REQ);
552 g_assert (priv->dbus_msg != NULL);
553 dbus_message_set_no_reply (priv->dbus_msg, TRUE);
557 init_mode_gui (DisplayBlankingStatusPluginPrivate *priv)
559 priv->mode_dialog = NULL;
560 priv->mode_button = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT |
561 HILDON_SIZE_AUTO_WIDTH);
563 GError* error = NULL;
564 gint mode = gconf_client_get_int (priv->gconf_client, MODE_GCONF_KEY,
566 g_assert (error == NULL);
567 update_mode_gui (mode, priv);
569 g_signal_connect (priv->mode_button, "clicked",
570 G_CALLBACK (on_mode_button_clicked), priv);
574 inhibit_button_new (const gchar *icon_name,
575 void (*cb) (GtkWidget *, DisplayBlankingStatusPluginPrivate *),
579 GtkWidget *b = hildon_gtk_toggle_button_new (HILDON_SIZE_FINGER_HEIGHT |
580 HILDON_SIZE_AUTO_WIDTH);
581 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (b), FALSE);
582 GtkWidget *icon = gtk_image_new_from_icon_name (icon_name,
583 GTK_ICON_SIZE_DIALOG);
584 gtk_button_set_image (GTK_BUTTON (b), icon);
585 g_signal_connect (b, "clicked", G_CALLBACK (cb), cb_data);
591 init_inhibit_gui (DisplayBlankingStatusPluginPrivate *priv)
593 priv->inhibit_in_signal = FALSE;
595 priv->inhibit_timer_id = 0;
596 priv->timed_inhibit_timer_id = 0;
598 priv->inhibit_button = inhibit_button_new (INHIBIT_ICON_NAME,
599 on_inhibit_button_clicked, priv);
601 priv->timed_inhibit_button = inhibit_button_new (
602 TIMED_INHIBIT_ICON_NAME, on_timed_inhibit_button_clicked, priv);
606 display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin)
608 DisplayBlankingStatusPluginPrivate *priv;
610 priv = DISPLAY_BLANKING_STATUS_PLUGIN_GET_PRIVATE (plugin);
612 priv->plugin = plugin;
616 init_mode_gui (priv);
617 init_inhibit_gui (priv);
619 GtkWidget *hbbox = gtk_hbutton_box_new ();
620 g_assert (hbbox != NULL);
622 gtk_container_add (GTK_CONTAINER (hbbox), priv->mode_button);
623 gtk_container_add (GTK_CONTAINER (hbbox), priv->inhibit_button);
624 gtk_container_add (GTK_CONTAINER (hbbox), priv->timed_inhibit_button);
626 gtk_container_add (GTK_CONTAINER (plugin), hbbox);
628 gtk_widget_show_all (GTK_WIDGET (plugin));