From a3979cc6e4b1651d8ae1eef5c73c073457ad4c76 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Sun, 29 Jul 2012 20:26:02 +0200 Subject: [PATCH] Add timed inhibit display blanking button --- data/display-blanking-inhibit-icon.timed.png | Bin 0 -> 1927 bytes debian/install | 1 + src/lib-display-blanking-status-menu-widget.c | 286 +++++++++++++++--- 3 files changed, 253 insertions(+), 34 deletions(-) create mode 100644 data/display-blanking-inhibit-icon.timed.png diff --git a/data/display-blanking-inhibit-icon.timed.png b/data/display-blanking-inhibit-icon.timed.png new file mode 100644 index 0000000000000000000000000000000000000000..e961c51b03d938464b34f8401df118e163b0ee23 GIT binary patch literal 1927 zcmV;22YC32P)2NX#} zK~!ko?O1~;&`|h6SzW00H_x*r%Teo%lzcz9+fY5c_ae2$uk08r+08lzU zx@uh9O#q$?H9gpU6@0d~<^&=?eD)sZ^F5yH{52(zI6YL8c;~NwF(TrmY@Q*kVd$8G z+jjg0$z;Vszj;^FeeiyI^KarnZQX7F@1(ikDjgsF+BK_?$K$ylKvTZJ3z3Mq?#DlQ znp3Ius*e^Fh~DQ9yrLGnlx3!!YHn_RKO+H;$J28D{Q0+rhK7(#Ci6mI@A(U842uTe z;rh?wf}?23`^{!EHgDdHix)5MJ#ys8(G~YVFc^HMzrP=6&YXdwC=dhz)z#Igs;Zj5 zM^O}vjEq1O#q5#}A=p=1j>b9TK7F&;iM}Zx0)fDi^Q=}YLZJ{eO?wK!$^>R+W(xd% zKNKakl9W<-y~6rKI3v>@APGZZ_;MPQ4f(z#!A+fy=UtcsveW z*RxZ!d=n5`ix!a41@OVr%<`|O+f7)1iUEh-z0w$9Q2d4b6Y0TjN zdD*SFFg<;96VP=XUsRHdB(tyVHvBsjT$MDg2?53!;_)~tIUYZlF(#}@s|DSv0##M7 z`)Xe^0Zr2oi9`?#22tv8pfeCQjK?{G?&=MQL?Zc->0BqOstQ?_4N|zZm3-N0T z6s4)oD^(S+CS)k0xMG>W^E_HxTk+6CUk1nV5KLyoRRN4@Xt?JNY}vX4k3Re*{PdTv zuATtLagbb_5PRwbaIguO{?DR*000C*KwFU$b+I{v|91rzyfHP2xw*NOmtPRj-QA6v zni>EEj4?>DB%Xa+17i%o_^Jk*87MBl124bah2Gv?`@q0JMgj?o#e%}AF~klw0h4C{ zV9AE&-i>ZNIx8AFQ!hvGqJJ9xP$*-8*3r=cx7!U$DL5__Z;2#DJVH~=lqP^N0*>QA z2tj*$ySt^OrLL)|>62v%^!4?fcDY*`obmD zG##;M^vZ85G)At5Tf$|o1cU%4D!|T}QgW-N?xh-_sw&AT z6N_!%dphYE*lXAm#l@#eKK^h}2RK3qTQvby`LC{Nn<=I9dqCHL$C_N2o=(+M`=6-6 z1KUbKKmY>7WL4)lj`)1O$(#rz|8}Y~{Uq6O_@(ayn8*e^07R+f_gp;I2^i~-ZS}X{ zw=XoN!0{X+QA0;Z7-JY79v;fA8fDCI_GfvdbVi{w3RO*}jj5QLn)2K2_6yfsPIDVJ{K4_!!IOlLoQnLa%E~#9$J5Ig1Eo}_ zlp@2WGsZA5FwoQ4*%{91cs)>Cm(q8eZS=O`f9=jCbkc3lo;@$@+qdtV3p%xhd13xH z&+|BR=+GaJA3y$l#^d!HZSC#ttvh$_{BA`>MG4RIiyEd&mo5bd2M2q)y1I^KJYHWb zYinyO@%el^8X6kzcRHQ503eFu_}Q~(-*>y+r+a#OE@R!+ZQT~x{{UenN@`AeQ(*uA N002ovPDHLkV1g9pjV%BG literal 0 HcmV?d00001 diff --git a/debian/install b/debian/install index 22ccaf1..8494d44 100644 --- a/debian/install +++ b/debian/install @@ -4,5 +4,6 @@ data/display-blanking-icon.2.png usr/share/icons/hicolor/48x48/hildon data/display-blanking-icon.3.png usr/share/icons/hicolor/48x48/hildon data/display-blanking-icon.4.png usr/share/icons/hicolor/48x48/hildon data/display-blanking-inhibit-icon.png usr/share/icons/hicolor/48x48/hildon +data/display-blanking-inhibit-icon.timed.png usr/share/icons/hicolor/48x48/hildon src/lib-displayblanking-status-menu.so usr/lib/hildon-desktop data/status-area-displayblanking-applet.desktop usr/share/applications/hildon-status-menu diff --git a/src/lib-display-blanking-status-menu-widget.c b/src/lib-display-blanking-status-menu-widget.c index 2daaff6..a3fbb2c 100644 --- a/src/lib-display-blanking-status-menu-widget.c +++ b/src/lib-display-blanking-status-menu-widget.c @@ -100,7 +100,8 @@ static const char *mode_icon_name[BLANKING_MODES] = "display-blanking-icon.3", "display-blanking-icon.4", }; -#define INHIBIT_ICON_NAME "display-blanking-inhibit-icon" +#define INHIBIT_ICON_NAME "display-blanking-inhibit-icon" +#define TIMED_INHIBIT_ICON_NAME "display-blanking-inhibit-icon.timed" struct _DisplayBlankingStatusPluginPrivate { @@ -109,7 +110,16 @@ struct _DisplayBlankingStatusPluginPrivate DBusMessage* dbus_msg; GtkWidget *mode_button; GtkWidget *mode_dialog; - gint inhibit_timer_id; // if == 0, no timer is set + GtkWidget *inhibit_button; + GtkWidget *timed_inhibit_button; + GtkWidget *timed_inhibit_dialog; + gint inhibit_timer_id; // if == 0 + gint timed_inhibit_timer_id; // no timer is set + // gtk_toggle_button_set_active () triggers the "clicked" signal on the + // affected button, since we don't want to process the signal while + // changing the "pressed" state (we want just the GUI to change, we use + // this flag to ignore the "clicke" signal handler when is TRUE + gboolean inhibit_in_signal; gpointer data; }; @@ -136,8 +146,17 @@ update_mode_gui (gint mode, DisplayBlankingStatusPluginPrivate *priv) gtk_button_set_image (GTK_BUTTON (priv->mode_button), icon); } +static void +disable_timer (gint *timer_id) +{ + g_assert (*timer_id != 0); + gboolean ok = g_source_remove (*timer_id); + g_assert (ok == TRUE); + *timer_id = 0; +} + static gboolean -on_timeout (DisplayBlankingStatusPluginPrivate *priv) +on_inhibit_timeout (DisplayBlankingStatusPluginPrivate *priv) { dbus_bool_t ok = dbus_connection_send (priv->dbus_conn, priv->dbus_msg, NULL); @@ -146,6 +165,201 @@ on_timeout (DisplayBlankingStatusPluginPrivate *priv) return TRUE; } +static gboolean +on_timed_inhibit_timeout (DisplayBlankingStatusPluginPrivate *priv) +{ + disable_timer (&(priv->inhibit_timer_id)); + disable_timer (&(priv->timed_inhibit_timer_id)); + + priv->inhibit_in_signal = TRUE; + gtk_toggle_button_set_active ( + GTK_TOGGLE_BUTTON (priv->timed_inhibit_button), FALSE); + priv->inhibit_in_signal = FALSE; + + GtkWidget *banner = hildon_banner_show_information ( + priv->timed_inhibit_button, NULL, + dgettext (GETTEXT_DOM, "Display blanking inhibition disabled")); + hildon_banner_set_timeout (HILDON_BANNER (banner), 5000); + + return FALSE; +} + +static void +enable_inhibit_timer (DisplayBlankingStatusPluginPrivate *priv) +{ + g_assert (priv->inhibit_timer_id == 0); + priv->inhibit_timer_id = g_timeout_add_seconds (INHIBIT_MSG_INTERVAL, + (GSourceFunc) on_inhibit_timeout, priv); + g_assert (priv->inhibit_timer_id > 0); +} + +static void +on_inhibit_button_clicked (GtkWidget *button, + DisplayBlankingStatusPluginPrivate *priv) +{ + if (priv->inhibit_in_signal) + return; + + GtkWidget *parent = gtk_widget_get_ancestor (button, GTK_TYPE_WINDOW); + gtk_widget_hide (parent); + + gboolean self_pressed = gtk_toggle_button_get_active ( + GTK_TOGGLE_BUTTON (button)); + gboolean other_pressed = gtk_toggle_button_get_active ( + GTK_TOGGLE_BUTTON (priv->timed_inhibit_button)); + + if (self_pressed && other_pressed) { + g_assert (priv->inhibit_timer_id != 0); + + disable_timer (&(priv->timed_inhibit_timer_id)); + + priv->inhibit_in_signal = TRUE; + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON ( + priv->timed_inhibit_button), FALSE); + priv->inhibit_in_signal = FALSE; + } + else if (self_pressed && !other_pressed) { + g_assert (priv->timed_inhibit_timer_id == 0); + + enable_inhibit_timer (priv); + } + else if (!self_pressed) { + g_assert (!other_pressed); + g_assert (priv->timed_inhibit_timer_id == 0); + + disable_timer (&(priv->inhibit_timer_id)); + } + else + g_assert (FALSE); +} + +static GtkWidget * +timed_inhibit_picker_new (const gchar* title, gsize selected, guint max, + guint step) +{ + g_assert (max < 100); + static gchar buffer[3]; // 2 for the number + 1 for \0 + + GtkWidget *selector = hildon_touch_selector_entry_new_text (); + for (int i = 0; i <= max; i += step) + hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), + g_ascii_formatd (buffer, 3, "%.0f", i)); + hildon_gtk_entry_set_input_mode (GTK_ENTRY ( + hildon_touch_selector_entry_get_entry ( + HILDON_TOUCH_SELECTOR_ENTRY (selector))), + HILDON_GTK_INPUT_MODE_NUMERIC); + + GtkWidget *picker = hildon_picker_button_new (HILDON_SIZE_FINGER_HEIGHT, + HILDON_BUTTON_ARRANGEMENT_VERTICAL); + hildon_button_set_title (HILDON_BUTTON (picker), title); + hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (picker), + HILDON_TOUCH_SELECTOR (selector)); + hildon_picker_button_set_active (HILDON_PICKER_BUTTON (picker), selected); + + g_object_set_data (G_OBJECT (picker), "max", GUINT_TO_POINTER (max)); + + return picker; +} + +static guint +timed_inhibit_picker_get_value (GtkWidget *picker) +{ + guint max = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (picker), "max")); + + return CLAMP (g_ascii_strtod (hildon_button_get_value ( + HILDON_BUTTON (picker)), NULL), 0, max); +} + +static guint +timed_inhibit_get_input (DisplayBlankingStatusPluginPrivate *priv) +{ + g_assert (priv->timed_inhibit_dialog == NULL); + priv->timed_inhibit_dialog = gtk_dialog_new_with_buttons ( + dgettext (GETTEXT_DOM, "Inhibit display blanking for..."), NULL, + GTK_DIALOG_MODAL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, + GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL); + + GtkWidget *h_picker = timed_inhibit_picker_new ( + dgettext (GETTEXT_DOM, "Hours"), 1, 24, 1); + GtkWidget *m_picker = timed_inhibit_picker_new ( + dgettext (GETTEXT_DOM, "Minutes"), 0, 60, 10); + + GtkWidget *hbox = gtk_hbox_new (FALSE, 0); + g_assert (hbox != NULL); + + gtk_container_add (GTK_CONTAINER (hbox), h_picker); + gtk_container_add (GTK_CONTAINER (hbox), m_picker); + + GtkWidget *content_area = gtk_dialog_get_content_area ( + GTK_DIALOG (priv->timed_inhibit_dialog)); + gtk_container_add (GTK_CONTAINER (content_area), hbox); + + gtk_widget_show_all (priv->timed_inhibit_dialog); + + gint result = gtk_dialog_run (GTK_DIALOG (priv->timed_inhibit_dialog)); + + guint timeout = 0; + if (result == GTK_RESPONSE_ACCEPT) + timeout = timed_inhibit_picker_get_value (h_picker) * 3600 + + timed_inhibit_picker_get_value (m_picker) * 60; + + gtk_widget_destroy (priv->timed_inhibit_dialog); + priv->timed_inhibit_dialog = NULL; + + return timeout; +} + +static void +on_timed_inhibit_button_clicked (GtkWidget *button, + DisplayBlankingStatusPluginPrivate *priv) +{ + if (priv->inhibit_in_signal) + return; + + GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button), + GTK_TYPE_WINDOW); + gtk_widget_hide (parent); + + gboolean self_pressed = gtk_toggle_button_get_active ( + GTK_TOGGLE_BUTTON (button)); + gboolean other_pressed = gtk_toggle_button_get_active ( + GTK_TOGGLE_BUTTON (priv->inhibit_button)); + + if (self_pressed) { + g_assert (priv->timed_inhibit_timer_id == 0); + if (other_pressed) + g_assert (priv->inhibit_timer_id != 0); + + guint timeout = timed_inhibit_get_input (priv); + if (timeout) { + if (other_pressed) { + priv->inhibit_in_signal = TRUE; + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON ( + priv->inhibit_button), FALSE); + priv->inhibit_in_signal = FALSE; + } + else + enable_inhibit_timer (priv); + + priv->timed_inhibit_timer_id = g_timeout_add_seconds (timeout, + (GSourceFunc) on_timed_inhibit_timeout, priv); + g_assert (priv->timed_inhibit_timer_id > 0); + } + else { + priv->inhibit_in_signal = TRUE; + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON ( + priv->timed_inhibit_button), FALSE); + priv->inhibit_in_signal = FALSE; + } + } + else { // !self_pressed + g_assert (!other_pressed); + + disable_timer (&(priv->inhibit_timer_id)); + disable_timer (&(priv->timed_inhibit_timer_id)); + } +} + static void on_mode_dialog_button_clicked (GtkWidget *button, GtkDialog *dialog) { @@ -232,26 +446,6 @@ on_mode_button_clicked (GtkWidget *button, } } -static void -on_inhibit_button_clicked (GtkWidget *button, - DisplayBlankingStatusPluginPrivate *priv) -{ - GtkWidget *parent = gtk_widget_get_ancestor (GTK_WIDGET (priv->mode_button), - GTK_TYPE_WINDOW); - gtk_widget_hide (parent); - - if (priv->inhibit_timer_id) { - gboolean ok = g_source_remove (priv->inhibit_timer_id); - g_assert (ok == TRUE); - priv->inhibit_timer_id = 0; - } - else { - priv->inhibit_timer_id = g_timeout_add_seconds (INHIBIT_MSG_INTERVAL, - (GSourceFunc) on_timeout, priv); - g_assert (priv->inhibit_timer_id > 0); - } -} - static void on_gconf_notify (GConfClient* client, guint cnxn_id, GConfEntry* entry, DisplayBlankingStatusPluginPrivate* priv) @@ -322,6 +516,38 @@ init_mode_gui (DisplayBlankingStatusPluginPrivate *priv) G_CALLBACK (on_mode_button_clicked), priv); } +static GtkWidget * +inhibit_button_new (const gchar *icon_name, + void (*cb) (GtkWidget *, DisplayBlankingStatusPluginPrivate *), + gpointer cb_data) +{ + + GtkWidget *b = hildon_gtk_toggle_button_new (HILDON_SIZE_FINGER_HEIGHT | + HILDON_SIZE_AUTO_WIDTH); + gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON(b), FALSE); + GtkWidget *icon = gtk_image_new_from_icon_name (icon_name, + GTK_ICON_SIZE_DIALOG); + gtk_button_set_image (GTK_BUTTON (b), icon); + g_signal_connect (b, "clicked", G_CALLBACK (cb), cb_data); + + return b; +} + +static void +init_inhibit_gui (DisplayBlankingStatusPluginPrivate *priv) +{ + priv->inhibit_in_signal = FALSE; + + priv->inhibit_timer_id = 0; + priv->timed_inhibit_timer_id = 0; + + priv->inhibit_button = inhibit_button_new (INHIBIT_ICON_NAME, + on_inhibit_button_clicked, priv); + + priv->timed_inhibit_button = inhibit_button_new ( + TIMED_INHIBIT_ICON_NAME, on_timed_inhibit_button_clicked, priv); +} + static void display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin) { @@ -333,22 +559,14 @@ display_blanking_status_plugin_init (DisplayBlankingStatusPlugin *plugin) init_gconf (priv); init_dbus (priv); init_mode_gui (priv); - - priv->inhibit_timer_id = 0; - GtkWidget *inhibit_button = hildon_gtk_toggle_button_new ( - HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH); - gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON(inhibit_button), FALSE); - GtkWidget *icon = gtk_image_new_from_icon_name (INHIBIT_ICON_NAME, - GTK_ICON_SIZE_DIALOG); - gtk_button_set_image (GTK_BUTTON (inhibit_button), icon); - g_signal_connect (inhibit_button, "clicked", - G_CALLBACK (on_inhibit_button_clicked), priv); + init_inhibit_gui (priv); GtkWidget *hbbox = gtk_hbutton_box_new (); g_assert (hbbox != NULL); gtk_container_add (GTK_CONTAINER (hbbox), priv->mode_button); - gtk_container_add (GTK_CONTAINER (hbbox), inhibit_button); + gtk_container_add (GTK_CONTAINER (hbbox), priv->inhibit_button); + gtk_container_add (GTK_CONTAINER (hbbox), priv->timed_inhibit_button); gtk_container_add (GTK_CONTAINER (plugin), hbbox); -- 2.43.0