public inbox for linux-acpi@vger.kernel.org
 help / color / mirror / Atom feed
* new patch for battstat_applet-2.0.13
@ 2002-09-06 17:21 Troy Schultz
       [not found] ` <1031332874.5080.42.camel-QUVSR2uV0NY@public.gmane.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Troy Schultz @ 2002-09-06 17:21 UTC (permalink / raw)
  To: ACPI Development - Sourceforge

[-- Attachment #1: Type: text/plain, Size: 871 bytes --]

Sorry, but there was a small problem with the patch I posted.  Also I
have included some basic instructions for applying the patch.

Here is the patch for battstat_applet, it now includes proper formatting
for single digit times.

The original battstat_applet source can be downloaded from
http://sourceforge.net/projects/battstat/

Download the battstat_applet-2.0.13 and extract the source tree, then
change to the main directory of the battstat_applet.

To test the patch:
patch -p1 --dry-run -i battstat_applet-2.0.13-patch.diff

To apply the patch if the test was OK:
patch -p1 -i battstat_applet-2.0.13-patch.diff

Then follow the build/install instructions and then you should have a
working battstat_applet under gnome.

Not all notebooks return the necessary information from the battery, but
if your's does then things should work fine.

Best Regards
- Troy




[-- Attachment #2: battstat_applet-2.0.13-patch.diff --]
[-- Type: text/plain, Size: 11496 bytes --]

--- battstat_applet-2.0.13/src/acpi-linux.c.orig	Wed Sep  4 17:40:08 2002
+++ battstat_applet-2.0.13/src/acpi-linux.c	Thu Sep  5 22:35:51 2002
@@ -21,6 +21,12 @@
 // ACPI battery read-out functions for Linux >= 2.4.12
 // October 2001 by Lennart Poettering <lennart-mdGvqq1h2p8n5izryJqWLw@public.gmane.org>
 
+/* Additional changes September 2002 by Troy Schultz <tschultz-zzOxFVvAfJPQT0dZR+AlfA@public.gmane.org>
+   - changed acpi handling for newer format
+   - added support for battery time remaining
+   - added code to prevent division by zero, returns zero result
+*/
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
@@ -91,13 +97,13 @@
 // the ACPI kernel interface in /proc
 gboolean acpi_linux_read(struct apm_info *apminfo)
 {
-  guint32 max_capacity, low_capacity, critical_capacity, remain;
+  guint32 max_capacity, low_capacity, critical_capacity, remain, present_rate;
   gboolean charging, ac_online;
   FILE *f;
 
   // apminfo.ac_line_status must be one when on ac power
   // apminfo.battery_status must be 0 for high, 1 for low, 2 for critical, 3 for charging
-  // apminfo.battery_percentage must contain batter charge percentage
+  // apminfo.battery_percentage must contain battery charge percentage
   // apminfo.battery_flags & 0x8 must be nonzero when charging
   
   g_assert(apminfo);
@@ -106,11 +112,11 @@
   low_capacity = 0;
   critical_capacity = 0;
 
-  if ((f = fopen("/proc/acpi/battery/1/info", "r")))
+  if ((f = fopen("/proc/acpi/battery/BAT1/info", "r")))
     {
-      max_capacity = al_get_field_int(f, "Design Capacity");
-      low_capacity = al_get_field_int(f, "Design Capacity Warning");
-      critical_capacity = al_get_field_int(f, "Design Capacity Low");
+      max_capacity = al_get_field_int(f, "last full capacity");
+      low_capacity = al_get_field_int(f, "design capacity warning");
+      critical_capacity = al_get_field_int(f, "design capacity low");
       
       fclose(f);
     }
@@ -121,35 +127,45 @@
   charging = FALSE;
   remain = 0;
 
-  if ((f = fopen("/proc/acpi/battery/1/status", "r")))
+  if ((f = fopen("/proc/acpi/battery/BAT1/state", "r")))
     {
       gchar *s;
       gchar tmp[256];
 
-      if ((s = al_get_field(f, "State", tmp, sizeof(tmp))))
+      if ((s = al_get_field(f, "charging state", tmp, sizeof(tmp))))
         charging = strcmp(s, "charging") == 0;
       
-      remain = al_get_field_int(f, "Remaining Capacity");
+      present_rate = al_get_field_int(f, "present rate");
+      remain = al_get_field_int(f, "remaining capacity");
 
       fclose(f);
     }
   
   ac_online = FALSE;
   
-  if ((f = fopen("/proc/acpi/ac_adapter/0/status", "r")))
+  if ((f = fopen("/proc/acpi/ac_adapter/ACAD/state", "r")))
     {
       gchar *s;
       gchar tmp[256];      
 
-      if ((s = al_get_field(f, "Status", tmp, sizeof(tmp))))
+      if ((s = al_get_field(f, "state", tmp, sizeof(tmp))))
         ac_online = strcmp(s, "on-line") == 0;
 
       fclose(f);
     }
 
+  if( present_rate > 0 ) {
+     apminfo->battery_time = (remain*60)/present_rate;
+  } else {
+     apminfo->battery_time = 0;
+  }
   apminfo->ac_line_status = ac_online ? 1 : 0;
   apminfo->battery_status = remain < low_capacity ? 1 : remain < critical_capacity ? 2 : 0;
-  apminfo->battery_percentage = (int) (remain/(float)max_capacity*100);
+  if( max_capacity > 0 ){
+     apminfo->battery_percentage = (int) (remain/(float)max_capacity*100);
+  } else {
+     apminfo->battery_percentage = 0;
+  }
   apminfo->battery_flags = charging ? 0x8 : 0;
 
   return TRUE;
--- battstat_applet-2.0.13/src/battstat_applet.c.orig	Thu Sep  5 22:46:40 2002
+++ battstat_applet-2.0.13/src/battstat_applet.c	Fri Sep  6 12:42:21 2002
@@ -20,6 +20,10 @@
  $Id: battstat_applet.c,v 1.23 2001/11/04 23:41:22 jdthood Exp $
  */
 
+/* Additional changes September 2002 by Troy Schultz <tschultz-zzOxFVvAfJPQT0dZR+AlfA@public.gmane.org>
+   - added support for time remaining
+*/
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
@@ -316,6 +320,8 @@
   static guint last_batt_state=1000;
   static guint last_pixmap_index=1000;
   static guint last_charging=1000;
+  guint batt_remain_Hr;
+  guint batt_remain_Min;
   guint batt_life;
   guint acline_status;
   guint batt_state;
@@ -374,6 +380,8 @@
    batt_state = apminfo.battery_status;
    batt_life = (guint) apminfo.battery_percentage;
    charging = (apminfo.battery_flags & 0x8) ? TRUE : FALSE;
+   batt_remain_Hr = (guint) apminfo.battery_time / 60;
+   batt_remain_Min = (guint) apminfo.battery_time % 60;
 #else
    acline_status = 1;
    batt_state = 0;
@@ -415,8 +423,8 @@
    ) {
       /* Warn that battery dropped below red_val */
       if(battery->lowbattnotification) {
-         snprintf(new_label, sizeof(new_label),_("Battery low (%d%%) and AC is offline"),
-                  batt_life);
+         snprintf(new_label, sizeof(new_label),_("Battery low (%d%%) [%d:%02d remain] and AC is offline"),
+                  batt_life,batt_remain_Hr, batt_remain_Min);
          gnome_warning_dialog(new_label);
     
          if(battery->beep)
@@ -457,12 +465,12 @@
       if(!battery->showbattery && !battery->showpercent) {
 	 if(acline_status == 0) {
 	    snprintf(new_label, sizeof(new_label),
-		     _("System is running on battery power\nBattery: %d%% (%s)"),
-		     batt_life, _(status[batt_state]));
+		     _("System is running on battery power.\n Battery: %d%% (%s) [%d:%02d remain]"),
+		     batt_life, _(status[batt_state]),batt_remain_Hr, batt_remain_Min);
 	 } else {
 	    snprintf(new_label, sizeof(new_label),
-		     _("System is running on AC power\nBattery: %d%% (%s)"),
-		     batt_life, _(status[batt_state]));
+		     _("System is running on AC power.\n Battery: %d%% (%s) [%d:%02d remain]"),
+		     batt_life, _(status[batt_state]), batt_remain_Hr, batt_remain_Min);
 	 }
       } else {
 	 if(acline_status == 0) {
@@ -615,16 +623,16 @@
 	       snprintf(new_string, sizeof(new_string), 
 			/* This string will display as a tooltip over the battery frame
 			 when the computer is using battery power.*/
-			_("System is running on battery power. Battery: %d%% (%s)"),
+			_("System is running on battery power.\n Battery: %d%% (%s) [%d:%02d remain]"),
 			batt_life,
-			_(status[batt_state]));
+			_(status[batt_state]),batt_remain_Hr, batt_remain_Min);
 	    } else {
 	       snprintf(new_string, sizeof(new_string), 
 			/* This string will display as a tooltip over the battery frame
 			 when the computer is using AC power.*/
-			_("System is running on AC power. Battery: %d%% (%s)"),
+			_("System is running on AC power. Battery: %d%% (%s) [%d:%02d remain]"),
 			batt_life,
-			_(status[batt_state]));
+			_(status[batt_state]),batt_remain_Hr, batt_remain_Min);
 	    }
 	 } else {
 	    if(acline_status == 0) {
@@ -648,9 +656,9 @@
 		    /* Displayed as a tooltip over the battery meter when there is
 		     a battery present. %d will hold the current charge and %s will
 		     hold the status of the battery, (High, Low, Critical, Charging. */
-		    (_("Battery: %d%% (%s)")),
+		    (_("Battery: %d%% (%s) [%d:%02d remain]")),
 		    batt_life,
-		    _(status[batt_state]));
+		    _(status[batt_state]),batt_remain_Hr, batt_remain_Min);
 	 } else {
 	    snprintf(new_string, sizeof(new_string), 
 		     /* Displayed as a tooltip over the battery meter when no
@@ -668,7 +676,8 @@
 			   gettext(new_string),
 			   NULL);
       
-      if (DEBUG) printf("Percent: %d, Status: %s\n", batt_life, status[batt_state]);
+      if (DEBUG) printf("Percent: %d [%d:%02d remain], Status: %s\n", batt_life, batt_remain_Hr,
+      batt_remain_Min, status[batt_state]);
    }
 
    last_charging = charging;
@@ -795,6 +804,8 @@
    guint acline_status;
    guint batt_state;
    guint batt_life;
+   guint batt_remain_Hr;
+   guint batt_remain_Min;
    gchar *status[]={
       /* The following four messages will be displayed as tooltips over
        the battery meter.  
@@ -824,6 +835,8 @@
    acline_status = apminfo.ac_line_status ? 1 : 0;
    batt_state = apminfo.battery_status;
    batt_life = apminfo.battery_percentage;
+   batt_remain_Hr = (guint) apminfo.battery_time / 60;
+   batt_remain_Min = (guint) apminfo.battery_time % 60;
 #else
    acline_status = 1;
    batt_state = 0;
@@ -893,15 +906,15 @@
 		     /* This string will display as a tooltip over the status frame
 		      when the computer is using battery power and the battery meter
 		      and percent meter is hidden by the user.*/
-		     _("System is running on battery power. Battery: %d%% (%s)"),
-		     batt_life, _(status[batt_state]));
+		     _("System is running on battery power.\n Battery: %d%% (%s) [%d:%02d remain]"),
+		     batt_life, _(status[batt_state]),batt_remain_Hr, batt_remain_Min);
 	 } else {
 	    snprintf(new_label, sizeof(new_label),
 		     /* This string will display as a tooltip over the status frame
 		      when the computer is using AC power and the battery meter
 		      and percent meter is hidden by the user.*/
-		     _("System is running on AC power. Battery: %d%% (%s)"),
-		     batt_life, _(status[batt_state]));      
+		     _("System is running on AC power.\n Battery: %d%% (%s) [%d:%02d remain]"),
+		     batt_life, _(status[batt_state]),batt_remain_Hr, batt_remain_Min);      
 	 }
 	 gtk_tooltips_set_tip (battstat->ac_tip,
 			       battstat->eventstatus,
@@ -973,16 +986,16 @@
 		     /* This string will display as a tooltip over the status frame
 		      when the computer is using battery power and the battery meter
 		      and percent meter is hidden by the user.*/
-		     _("System is running on battery power\nBattery: %d%% (%s)"),
-		     batt_life, _(status[batt_state]));
+		     _("System is running on battery power.\n Battery: %d%% (%s) [%d:%02d remain]"),
+		     batt_life, _(status[batt_state]),batt_remain_Hr, batt_remain_Min);
 	 } else {
 	    /* 1 = AC power. I should really test it explicitly here. */
 	    snprintf(new_label, sizeof(new_label),
 		     /* This string will display as a tooltip over the status frame
 		      when the computer is using AC power and the battery meter
 		      and percent meter is hidden by the user.*/
-		     _("System is running on AC power\nBattery: %d%% (%s)"),
-		     batt_life, _(status[batt_state]));
+		     _("System is running on AC power.\n Battery: %d%% (%s) [%d:%02d remain]"),
+		     batt_life, _(status[batt_state]),batt_remain_Hr, batt_remain_Min);
 	 }
 	 gtk_tooltips_set_tip (battstat->ac_tip,
 			       battstat->eventstatus,
@@ -994,13 +1007,13 @@
 	    snprintf(new_label, sizeof(new_label),
 		     /* This string will display as a tooltip over the status frame
 		      when the computer is using battery power.*/
-		     _("System is running on battery power"));
+		     _("System is running on battery power."));
 	 } else {
 	    /* 1 = AC power. I should really test it explicitly here. */
 	    snprintf(new_label, sizeof(new_label),
 		     /* This string will display as a tooltip over the status frame
 		      when the computer is using AC power.*/
-		     _("System is running on AC power"));
+		     _("System is running on AC power."));
 	 }
 	 gtk_tooltips_set_tip (battstat->ac_tip,
 			       battstat->eventstatus,

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: new patch for battstat_applet-2.0.13
       [not found] ` <1031332874.5080.42.camel-QUVSR2uV0NY@public.gmane.org>
@ 2002-09-06 19:12   ` Federico Di Gregorio
  0 siblings, 0 replies; 2+ messages in thread
From: Federico Di Gregorio @ 2002-09-06 19:12 UTC (permalink / raw)
  To: ACPI Development - Sourceforge

[-- Attachment #1: Type: text/plain, Size: 765 bytes --]

Il ven, 2002-09-06 alle 19:21, Troy Schultz ha scritto:
> Sorry, but there was a small problem with the patch I posted.  Also I
> have included some basic instructions for applying the patch.

unfortunately you can bet on having BAT1 or anything like that. i have
BAT0. the right thing to do is to scan the directory and use the names
you find at runtime, not hardcoded ones.

-- 
Federico Di Gregorio
Debian GNU/Linux Developer & Italian Press Contact        fog-8fiUuRrzOP0dnm+yROfE0A@public.gmane.org
INIT.D Developer                                           fog-NGVKUo/i/6DYtjvyW6yDsg@public.gmane.org
  Spesso crescere ed andare a vivere da soli è l'unico modo di restare
   bambini.                                             -- Alice Fontana

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-09-06 19:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-09-06 17:21 new patch for battstat_applet-2.0.13 Troy Schultz
     [not found] ` <1031332874.5080.42.camel-QUVSR2uV0NY@public.gmane.org>
2002-09-06 19:12   ` Federico Di Gregorio

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox