[Goodies-dev] [xfce4-battery-plugin][Patch] Show current power usage

David Rosca nowrep at gmail.com
Thu Nov 15 09:16:56 CET 2012


Hi,
I have made a small patch that adds support for showing current power usage
(in Watts)
next to approximate remaining time. So in panel, it will be shown like:

                                    04:25 (9 W)

It is using data from libacpi.c (from
/sys/class/power_supply/BAT{x}/power_now).
It is working great for my laptop (Thinkpad X220) running 3.5.7 kernel. It
may however need
some tweaking to get it working properly on every laptop.

Current power usage will only be shown when on battery (I used !acline
check, so I hope it's correct).
I have integrated it into remaining time box because i didn't want to
create a new box just for this.

I'm not really sure if I can send attachements to mailing lists, so I will
append the patch at the end
of the mail.

Please let me know what you think about this patch.

Thanks,
David Rosca



diff --git a/panel-plugin/battery.c b/panel-plugin/battery.c
index f77ba92..3640cd3 100644
--- a/panel-plugin/battery.c
+++ b/panel-plugin/battery.c
@@ -75,7 +75,8 @@ typedef struct
     gboolean    display_power;    /* Options */
     gboolean    display_percentage;    /* Options */
     gboolean    display_bar;    /* Options */
-    gboolean    display_time;
+    gboolean    display_time;   /* Options */
+    gboolean    display_powernow;   /* Options */
     gboolean    hide_when_full;
     gboolean    tooltip_display_percentage;
     gboolean    tooltip_display_time;
@@ -119,6 +120,7 @@ typedef struct
     GtkWidget        *cb_disp_percentage;
     GtkWidget        *cb_disp_bar;
     GtkWidget        *cb_disp_time;
+    GtkWidget        *cb_disp_powernow;
     GtkWidget        *cb_hide_when_full;
     GtkWidget        *cb_disp_tooltip_percentage;
     GtkWidget        *cb_disp_tooltip_time;
@@ -155,6 +157,7 @@ init_options(t_battmon_options *options)
     options->display_percentage = TRUE;
     options->display_bar = TRUE;
     options->display_time = FALSE;
+    options->display_powernow = FALSE;
     options->tooltip_display_percentage = FALSE;
     options->tooltip_display_time = FALSE;
     options->low_percentage = 10;
@@ -520,6 +523,14 @@ battmon.c:241: for each function it appears in.)
         gtk_widget_show(GTK_WIDGET(battmon->rtime));
         gtk_widget_show(GTK_WIDGET(battmon->timechargealignment));
         g_snprintf(buffer, sizeof(buffer),"%02d:%02d
",time_remaining/60,time_remaining%60);
+
+        if (battmon->options.display_powernow && rate > 0 && !acline) {
+            gchar buffer2[255];
+            g_snprintf(buffer2, sizeof(buffer2), "%s(%d W) ",
buffer,acpistate->prate/1000000);
+            g_snprintf(buffer, sizeof(buffer), "%s ", buffer2);
+        }
+
+
         gtk_label_set_text(battmon->rtime,buffer);

     } else {
@@ -874,6 +885,8 @@ battmon_read_config(XfcePanelPlugin *plugin, t_battmon
*battmon)

     battmon->options.display_time = xfce_rc_read_bool_entry (rc,
"display_time", FALSE);

+    battmon->options.display_powernow = xfce_rc_read_bool_entry (rc,
"display_powernow", FALSE);
+
     battmon->options.tooltip_display_percentage = xfce_rc_read_bool_entry
(rc, "tooltip_display_percentage", FALSE);

     battmon->options.tooltip_display_time = xfce_rc_read_bool_entry (rc,
"tooltip_display_time", FALSE);
@@ -937,6 +950,8 @@ battmon_write_config(XfcePanelPlugin *plugin, t_battmon
*battmon)

     xfce_rc_write_bool_entry (rc, "display_time",
battmon->options.display_time);

+    xfce_rc_write_bool_entry (rc, "display_powernow",
battmon->options.display_powernow);
+
     xfce_rc_write_bool_entry (rc, "tooltip_display_percentage",
battmon->options.tooltip_display_percentage);

     xfce_rc_write_bool_entry (rc, "tooltip_display_time",
battmon->options.tooltip_display_time);
@@ -1043,6 +1058,7 @@ static void refresh_dialog(t_battmon_dialog *dialog)

gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_disp_percentage),
battmon->options.display_percentage);
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_disp_bar),
battmon->options.display_bar);
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_disp_time),
battmon->options.display_time);
+
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_disp_powernow),
battmon->options.display_powernow);

gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_disp_tooltip_percentage),
battmon->options.tooltip_display_percentage);

gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_disp_tooltip_time),
battmon->options.tooltip_display_time);

gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_hide_when_full),
battmon->options.hide_when_full);
@@ -1078,6 +1094,15 @@ set_disp_time(GtkToggleButton *tb, t_battmon_dialog
*dialog)
 }

 static void
+set_disp_powernow(GtkToggleButton *tb, t_battmon_dialog *dialog)
+{
+    t_battmon *battmon = dialog->battmon;
+
+    battmon->options.display_powernow = gtk_toggle_button_get_active(tb);
+    update_apm_status(dialog->battmon);
+}
+
+static void
 set_hide_when_full(GtkToggleButton *tb, t_battmon_dialog *dialog)
 {
     t_battmon *battmon = dialog->battmon;
@@ -1567,6 +1592,9 @@ battmon_create_options(XfcePanelPlugin *plugin,
t_battmon *battmon)
     dialog->cb_disp_time = gtk_check_button_new_with_mnemonic(_("Display
time"));
     gtk_box_pack_start(GTK_BOX(vbox2), dialog->cb_disp_time, FALSE, FALSE,
0);

+    dialog->cb_disp_powernow =
gtk_check_button_new_with_mnemonic(_("Display current power"));
+    gtk_box_pack_start(GTK_BOX(vbox2), dialog->cb_disp_powernow, FALSE,
FALSE, 0);
+
     dialog->cb_disp_power = gtk_check_button_new_with_mnemonic(_("Display
power"));
     gtk_box_pack_start(GTK_BOX(vbox2), dialog->cb_disp_power, FALSE,
FALSE, 0);

@@ -1595,6 +1623,7 @@ battmon_create_options(XfcePanelPlugin *plugin,
t_battmon *battmon)
     g_signal_connect(dialog->cb_disp_percentage, "toggled",
G_CALLBACK(set_disp_percentage), dialog);
     g_signal_connect(dialog->cb_disp_bar, "toggled",
G_CALLBACK(set_disp_bar), dialog);
     g_signal_connect(dialog->cb_disp_time, "toggled",
G_CALLBACK(set_disp_time), dialog);
+    g_signal_connect(dialog->cb_disp_powernow, "toggled",
G_CALLBACK(set_disp_powernow), dialog);
     g_signal_connect(dialog->cb_hide_when_full, "toggled",
G_CALLBACK(set_hide_when_full), dialog);
     g_signal_connect(dialog->cb_disp_tooltip_percentage, "toggled",
G_CALLBACK(set_tooltip_disp_percentage), dialog);
     g_signal_connect(dialog->cb_disp_power, "toggled",
G_CALLBACK(set_disp_power), dialog);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.xfce.org/pipermail/goodies-dev/attachments/20121115/4c001c31/attachment.html>


More information about the Goodies-dev mailing list