[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    kdenonbeta/kbatterymonitor/linux-apm [POSSIBLY UNSAFE]
From:       Chris Howells <howells () kde ! org>
Date:       2004-11-01 20:40:48
Message-ID: 20041101204048.0730616C24 () office ! kde ! org
[Download RAW message or body]

CVS commit by howells: 

Should work


  M +93 -9     linux_apm.cpp   1.3 [POSSIBLY UNSAFE: scanf]
  M +5 -2      linux_apm.h   1.3


--- kdenonbeta/kbatterymonitor/linux-apm/linux_apm.cpp  #1.2:1.3
@@ -23,6 +23,8 @@
 #include <qdir.h>
 
-#include "../kbatterymonitor/monitor.h"
+#include <sys/utsname.h>
+#include <sys/types.h>
 
+#include "../kbatterymonitor/monitor.h"
 #include "linux_apm.h"
 
@@ -43,5 +45,19 @@ bool LinuxAPM::init()
         }
         
-        // do other init stuff
+        struct utsname sysinfo;
+        if( uname( &sysinfo ) == -1 )
+        {
+                return false;
+        }
+
+        if (strcmp(sysinfo.sysname, "Linux") == 0)
+        {
+                kdDebug() << "Found a Linux system" << endl;
+                return true;
+        }
+        else
+        {
+                return false;
+        }
         
         return true;
@@ -53,17 +69,85 @@ int LinuxAPM::numberOfBatteries()
 }
 
-int LinuxAPM::timeRemaining()
+batteryStatusStruct LinuxAPM::batteryStatus()
 {
-        return 0;
+        readStatus();
+        return mStatus;
 }
 
-int LinuxAPM::percentageRemaining()
+bool LinuxAPM::ACOnline()
 {
-        return 0;
+        return false;
 }
 
-bool LinuxAPM::ACOnline()
+/** A little bit of documentation on /proc/apm
+ 
+Stolen from http://www.redhat.com/docs/manuals/linux/RHL-7.3-Manual/ref-guide/s1-proc-topfiles.html
+
+/proc/apm consists of nine fields:
+
+1.16 1.2 0x03 0x01 0x03 0x09 100% -1 ?
+
+e.g.
+
+1: float -- APM kernel driver version
+2: float -- APM bios version
+3: int (hex) -- "apm flags"
+4: int (hex) -- ac line status
+5: int (hex) -- battery status
+6: int (hex) -- battery flags
+7: int -- percentage battery charge
+8: int -- battery remaining time
+9: string -- units for battery remaining
+*/
+
+bool LinuxAPM::readStatus()
 {
+
+        typedef struct apm_info
+        {
+                unsigned int apm_flags;
+                unsigned int ac_line_status;
+                int          battery_percentage;
+                int          battery_time;
+        } apm_info;
+
+        apm_info ap;
+        
+        FILE    *f = 0;
+        char    tmp2[10];
+        int     tmp, s;
+        unsigned int utmp;
+        char    version[256];
+
+        f = fopen("/proc/apm", "r");
+        if (f == NULL)
         return false;
+        s = fscanf(f, "%s %d.%d %x %x %x %x %d%% %d %s\n",
+                        (char*)version,
+                        &tmp,
+                        &tmp,
+                        &ap.apm_flags,
+                        &ap.ac_line_status,
+                        &utmp,
+                        &utmp,
+                        &ap.battery_percentage,
+                        &ap.battery_time,
+                        tmp2);
+        if (s < 9)
+                return false;   
+        if (version[0] == 'B') {
+                fclose(f);
+                return(2);
+        }
+        if (ap.battery_percentage > 100) 
+                ap.battery_percentage = -1;
+        if (strcmp(tmp2, "sec") == 0)
+                ap.battery_time /= 60;
+
+        mStatus.timeRemaining = ap.battery_time;
+        mStatus.percentageRemaining = ap.battery_percentage;
+        
+        fclose(f);
+        return true;
 }
 

--- kdenonbeta/kbatterymonitor/linux-apm/linux_apm.h  #1.2:1.3
@@ -34,7 +34,10 @@ class LinuxAPM : public Monitor
                 virtual bool init();
                 virtual int numberOfBatteries();
-                virtual int timeRemaining();
-                virtual int percentageRemaining();
+                virtual batteryStatusStruct batteryStatus();
                 virtual bool ACOnline();
+
+        private:
+                bool readStatus();
+                batteryStatusStruct mStatus;
 };
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic