Infrastructure for reading and displaying battery level.
authorhuceke <gimli@worker.(none)>
Fri, 8 Jul 2011 11:15:33 +0000 (13:15 +0200)
committerEdgar Hucek <ebsi4711@gmail.com>
Sun, 10 Jul 2011 08:29:34 +0000 (10:29 +0200)
Add battery level PowerSyscal interface and to system infos.
Current iOS is supporting this new feature.

24 files changed:
language/English/strings.xml
xbmc/GUIInfoManager.cpp
xbmc/GUIInfoManager.h
xbmc/osx/DarwinUtils.h
xbmc/osx/DarwinUtils.mm
xbmc/osx/ios/XBMCApplication.m
xbmc/powermanagement/IPowerSyscall.h
xbmc/powermanagement/PowerManager.cpp
xbmc/powermanagement/PowerManager.h
xbmc/powermanagement/linux/ConsoleDeviceKitPowerSyscall.cpp
xbmc/powermanagement/linux/ConsoleDeviceKitPowerSyscall.h
xbmc/powermanagement/linux/ConsoleUPowerSyscall.cpp
xbmc/powermanagement/linux/ConsoleUPowerSyscall.h
xbmc/powermanagement/linux/HALPowerSyscall.cpp
xbmc/powermanagement/linux/HALPowerSyscall.h
xbmc/powermanagement/osx/CocoaPowerSyscall.cpp
xbmc/powermanagement/osx/CocoaPowerSyscall.h
xbmc/powermanagement/windows/Win32PowerSyscall.cpp
xbmc/powermanagement/windows/Win32PowerSyscall.h
xbmc/utils/SystemInfo.cpp
xbmc/utils/SystemInfo.h
xbmc/win32/WIN32Util.cpp
xbmc/win32/WIN32Util.h
xbmc/windows/GUIWindowSystemInfo.cpp

index 54b5024..1cd5e86 100644 (file)
   <string id="12392">Hours</string>
   <string id="12393">Days</string>
   <string id="12394">Total uptime</string>
+  <string id="12395">Battery level</string>
 
   <string id="12600">Weather</string>
 
index d6a8862..a67ebd1 100644 (file)
@@ -1270,6 +1270,7 @@ CStdString CGUIInfoManager::GetLabel(int info, int contextWindow)
   case SYSTEM_INTERNET_STATE:
   case SYSTEM_UPTIME:
   case SYSTEM_TOTALUPTIME:
+  case SYSTEM_BATTERY_LEVEL:
     return g_sysinfo.GetInfo(info);
     break;
 
index 4f730ed..62937fd 100644 (file)
@@ -391,6 +391,7 @@ class CDateTime;
 #define SYSTEM_HAS_ADDON            711
 #define SYSTEM_ADDON_TITLE          712
 #define SYSTEM_ADDON_ICON           713
+#define SYSTEM_BATTERY_LEVEL        714
 
 #define LIBRARY_HAS_MUSIC           720
 #define LIBRARY_HAS_VIDEO           721
index 9278b65..795766b 100644 (file)
 extern "C"
 {
 #endif
-  bool    DarwinIsAppleTV2(void);
-  float   GetIOSVersion(void);
-  int     GetDarwinFrameworkPath(bool forPython, char* path, uint32_t *pathsize);
-  int     GetDarwinExecutablePath(char* path, uint32_t *pathsize);
-
-  bool    DarwinHasVideoToolboxDecoder(void);
+  bool  DarwinIsAppleTV2(void);
+  float GetIOSVersion(void);
+  int   GetDarwinFrameworkPath(bool forPython, char* path, uint32_t *pathsize);
+  int   GetDarwinExecutablePath(char* path, uint32_t *pathsize);
+  bool  DarwinHasVideoToolboxDecoder(void);
+  int   DarwinBatteryLevel(void);
 #ifdef __cplusplus
 }
 #endif
index 155411a..e0fbfe2 100644 (file)
@@ -33,6 +33,8 @@
   #import <sys/sysctl.h>
 #else
   #import <Cocoa/Cocoa.h>
+  #import <IOKit/ps/IOPowerSources.h>
+  #import <IOKit/ps/IOPSKeys.h>
 #endif
 
 #import "AutoPool.h"
@@ -222,4 +224,40 @@ bool DarwinHasVideoToolboxDecoder(void)
   return bDecoderAvailable;
 }
 
+int DarwinBatteryLevel(void)
+{
+  float batteryLevel = 0;
+#if defined(TARGET_DARWIN_IOS)
+  if(!DarwinIsAppleTV2())
+    batteryLevel = [[UIDevice currentDevice] batteryLevel];
+#else
+       CFTypeRef powerSourceInfo = IOPSCopyPowerSourcesInfo();
+       CFArrayRef powerSources = IOPSCopyPowerSourcesList(powerSourceInfo);
+       
+       CFDictionaryRef powerSource = NULL;
+       const void *powerSourceVal;
+       
+       for (int i = 0 ; i < CFArrayGetCount(powerSources) ; i++)
+       {
+               powerSource = IOPSGetPowerSourceDescription(powerSourceInfo, CFArrayGetValueAtIndex(powerSources, i));
+               if (!powerSource) break;
+               
+               powerSourceVal = (CFStringRef)CFDictionaryGetValue(powerSource, CFSTR(kIOPSNameKey));
+               
+               int curLevel = 0;
+               int maxLevel = 0;
+               
+               powerSourceVal = CFDictionaryGetValue(powerSource, CFSTR(kIOPSCurrentCapacityKey));
+               CFNumberGetValue((CFNumberRef)powerSourceVal, kCFNumberSInt32Type, &curLevel);
+               
+               powerSourceVal = CFDictionaryGetValue(powerSource, CFSTR(kIOPSMaxCapacityKey));
+               CFNumberGetValue((CFNumberRef)powerSourceVal, kCFNumberSInt32Type, &maxLevel);
+               
+               batteryLevel = (int)((double)curLevel/(double)maxLevel);
+               
+       }
+#endif
+  return batteryLevel * 100;  
+}
+
 #endif
index c996559..f9a890b 100644 (file)
@@ -55,6 +55,8 @@ UIWindow *m_window;
 
 - (void)applicationDidFinishLaunching:(UIApplication *)application 
 {
+  [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
+  
   m_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
   /* Turn off autoresizing */
index 5a09dd4..ff2bd3a 100644 (file)
@@ -46,6 +46,9 @@ public:
   virtual bool CanSuspend()   = 0;
   virtual bool CanHibernate() = 0;
   virtual bool CanReboot()    = 0;
+  
+// Battery related functions
+  virtual int  BatteryLevel() = 0;
 
   /*!
    \brief Pump power related events back to xbmc.
index 203a050..d2843dd 100644 (file)
@@ -169,7 +169,10 @@ bool CPowerManager::CanReboot()
 {
   return m_instance->CanReboot();
 }
-
+int CPowerManager::BatteryLevel()
+{
+  return m_instance->BatteryLevel();
+}
 void CPowerManager::ProcessEvents()
 {
   m_instance->PumpPowerEvents(this);
index b06b6cd..deb4b66 100644 (file)
@@ -37,6 +37,9 @@ public:
   virtual bool CanHibernate() { return true; }
   virtual bool CanReboot()    { return true; }
 
+  virtual int  BatteryLevel() { return 0; }
+
+
   virtual bool PumpPowerEvents(IPowerEventsCallback *callback) { return false; }
 };
 
@@ -60,6 +63,8 @@ public:
   bool CanSuspend();
   bool CanHibernate();
   bool CanReboot();
+  
+  int  BatteryLevel();
 
   void ProcessEvents();
 private:
index 6048743..fa8ac64 100644 (file)
@@ -80,6 +80,11 @@ bool CConsoleDeviceKitPowerSyscall::CanReboot()
   return m_CanReboot;
 }
 
+int CConsoleDeviceKitPowerSyscall::BatteryLevel()
+{
+  return 0;
+}
+
 bool CConsoleDeviceKitPowerSyscall::HasDeviceConsoleKit()
 {
   bool hasConsoleKitManager = false;
index 0431931..7b849f6 100644 (file)
@@ -39,6 +39,7 @@ public:
   virtual bool CanSuspend();
   virtual bool CanHibernate();
   virtual bool CanReboot();
+  virtual int  BatteryLevel();
 
   static bool HasDeviceConsoleKit();
 private:
index e77b603..5639dde 100644 (file)
@@ -120,6 +120,11 @@ bool CConsoleUPowerSyscall::CanReboot()
   return m_CanReboot;
 }
 
+int CConsoleUPowerSyscall::BatteryLevel()
+{
+  return 0;
+}
+
 bool CConsoleUPowerSyscall::HasDeviceConsoleKit()
 {
   bool hasConsoleKitManager = false;
index 932aa56..4fd1ebf 100644 (file)
@@ -38,6 +38,7 @@ public:
   virtual bool CanSuspend();
   virtual bool CanHibernate();
   virtual bool CanReboot();
+  virtual int  BatteryLevel();
 
   virtual bool PumpPowerEvents(IPowerEventsCallback *callback);
 
index ba1dc49..480371c 100644 (file)
@@ -72,6 +72,11 @@ bool CHALPowerSyscall::CanReboot()
   return m_CanReboot;
 }
 
+int CHALPowerSyscall::BatteryLevel()
+{
+  return 0;
+}
+
 bool CHALPowerSyscall::doPowerCall(const char *powerstate)
 {
   DBusMessage* msg;
index 973716b..97b7fea 100644 (file)
@@ -37,6 +37,7 @@ public:
   virtual bool CanSuspend();
   virtual bool CanHibernate();
   virtual bool CanReboot();
+  virtual int  BatteryLevel();
 
 private:
   bool QueryCapability(const char *capability);
index 11703c2..943f85a 100644 (file)
@@ -35,6 +35,8 @@ typedef unsigned char   BYTE;
 #include <IOKit/ps/IOPSKeys.h>
 #endif
 
+#include "osx/DarwinUtils.h"
+
 // missing in 10.4/10.5 SDKs.
 #if (MAC_OS_X_VERSION_MAX_ALLOWED < 1060)
 #define kIOPSNotifyLowBattery   "com.apple.system.powersources.lowbattery"
@@ -222,6 +224,11 @@ bool CCocoaPowerSyscall::HasBattery(void)
   return result;
 }
 
+int CCocoaPowerSyscall::BatteryLevel(void)
+{
+  return DarwinBatteryLevel();
+}
+
 bool CCocoaPowerSyscall::PumpPowerEvents(IPowerEventsCallback *callback)
 {
   if (m_OnSuspend)
index 38e778f..bc51ea6 100644 (file)
@@ -48,6 +48,7 @@ public:
   virtual bool CanHibernate(void);
   virtual bool CanReboot(void);
           bool HasBattery(void);
+  virtual int  BatteryLevel(void);
 
   virtual bool PumpPowerEvents(IPowerEventsCallback *callback);
 private:
index 7593c4d..c03f303 100644 (file)
@@ -73,6 +73,11 @@ bool CWin32PowerSyscall::CanReboot()
   return true;
 }
 
+int CWin32PowerSyscall::BatteryLevel()
+{
+  return CWIN32Util::BatteryLevel();
+}
+
 bool CWin32PowerSyscall::PumpPowerEvents(IPowerEventsCallback *callback)
 {
   if (m_OnSuspend)
index b72d7f6..c40b85d 100644 (file)
@@ -42,6 +42,7 @@ public:
   virtual bool CanSuspend();
   virtual bool CanHibernate();
   virtual bool CanReboot();
+  virtual int  BatteryLevel();
 
   virtual bool PumpPowerEvents(IPowerEventsCallback *callback);
 
index 5f34454..1711f2d 100644 (file)
@@ -43,6 +43,7 @@
 #include "osx/DarwinUtils.h"
 #include "osx/CocoaInterface.h"
 #endif
+#include "powermanagement/PowerManager.h"
 
 CSysInfo g_sysinfo;
 
@@ -59,6 +60,7 @@ bool CSysInfoJob::DoWork()
   m_info.cpuFrequency      = GetCPUFreqInfo();
   m_info.kernelVersion     = CSysInfo::GetKernelVersion();
   m_info.macAddress        = GetMACAddress();
+  m_info.batteryLevel      = GetBatteryLevel();
   return true;
 }
 
@@ -101,6 +103,13 @@ CStdString CSysInfoJob::GetVideoEncoder()
   return "GPU: " + g_Windowing.GetRenderRenderer();
 }
 
+CStdString CSysInfoJob::GetBatteryLevel()
+{
+  CStdString strVal;
+  strVal.Format("%d%%", g_powerManager.BatteryLevel());
+  return strVal;
+}
+
 double CSysInfoJob::GetCPUFrequency()
 {
 #if defined (_LINUX) || defined(_WIN32)
@@ -188,6 +197,8 @@ CStdString CSysInfo::TranslateInfo(int info) const
       return g_localizeStrings.Get(13274);
     else
       return g_localizeStrings.Get(13297);
+  case SYSTEM_BATTERY_LEVEL:
+    return m_info.batteryLevel;
   default:
     return "";
   }
index c6d54a4..89298ce 100644 (file)
@@ -53,6 +53,7 @@ public:
   CStdString cpuFrequency;
   CStdString kernelVersion;
   CStdString macAddress;
+  CStdString batteryLevel;
 };
 
 class CSysInfoJob : public CJob
@@ -71,6 +72,7 @@ private:
   CStdString GetCPUFreqInfo();
   CStdString GetMACAddress();
   CStdString GetVideoEncoder();
+  CStdString GetBatteryLevel();
 
   CSysData m_info;
 };
index 7a2f39c..e5351b4 100644 (file)
@@ -279,6 +279,16 @@ bool CWIN32Util::PowerManagement(PowerState State)
 #endif
 }
 
+int CWIN32Util::BatteryLevel()
+{
+  SYSTEM_POWER_STATUS SystemPowerStatus;
+
+  if (GetSystemPowerStatus(&SystemPowerStatus) && SystemPowerStatus.BatteryLifePercent != 255)
+      return SystemPowerStatus.BatteryLifePercent;
+
+  return 0;
+}
+
 bool CWIN32Util::XBMCShellExecute(const CStdString &strPath, bool bWaitForScriptExit)
 {
   CStdString strCommand = strPath;
index 904378d..d1b1586 100644 (file)
@@ -49,6 +49,7 @@ public:
   static char FirstDriveFromMask (ULONG unitmask);
   static int GetDriveStatus(const CStdString &strPath);
   static bool PowerManagement(PowerState State);
+  static int BatteryLevel();
   static bool XBMCShellExecute(const CStdString &strPath, bool bWaitForScriptExit=false);
   static std::vector<CStdString> GetDiskUsage();
   static CStdString GetResInfoString();
@@ -89,4 +90,4 @@ class CWinIdleTimer : public CStopWatch
 {
 public:
   void StartZero();
-};
\ No newline at end of file
+};
index b855f4b..1840016 100644 (file)
@@ -95,6 +95,7 @@ void CGUIWindowSystemInfo::FrameMove()
 #endif
     SetControlLabel(i++, "%s: %s", 12390, SYSTEM_UPTIME);
     SetControlLabel(i++, "%s: %s", 12394, SYSTEM_TOTALUPTIME);
+    SetControlLabel(i++, "%s: %s", 12395, SYSTEM_BATTERY_LEVEL);
   }
   else if (m_section == CONTROL_BT_STORAGE)
   {