[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfdefs_mac.mm
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfdefs_mac.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00029 #include <Cocoa/Cocoa.h>
00030 
00031 #import <IOKit/ps/IOPowerSources.h>
00032 #import <IOKit/ps/IOPSKeys.h>
00033 
00034 #include <klfdefs.h>
00035 #include <klfsysinfo.h>
00036 
00037 
00038 KLF_EXPORT QString klf_defs_sysinfo_arch()
00039 {
00040   static bool is_64 = (sizeof(void*) == 8);
00041 
00042   SInt32 x;
00043   Gestalt(gestaltSysArchitecture, &x);
00044   if (x == gestaltPowerPC) {
00045     return is_64 ? QString::fromLatin1("ppc64") : QString::fromLatin1("ppc");
00046   } else if (x == gestaltIntel) {
00047     return is_64 ? QString::fromLatin1("x86_64") : QString::fromLatin1("x86");
00048   }
00049   return QString::fromLatin1("unknown");
00050 }
00051 
00052 
00053 static bool init_power_sources_info(CFTypeRef *blobptr, CFArrayRef *sourcesptr)
00054 {
00055   *blobptr = IOPSCopyPowerSourcesInfo();
00056   *sourcesptr = IOPSCopyPowerSourcesList(*blobptr);
00057   if (CFArrayGetCount(*sourcesptr) == 0) {
00058     klfDbg("Could not retrieve battery information. May be a system without battery.") ;
00059     return false;  // Could not retrieve battery information.  System may not have a battery.
00060   }
00061   // we have a battery.
00062   return true;
00063 }
00064 
00065 KLF_EXPORT KLFSysInfo::BatteryInfo _klf_mac_battery_info()
00066 {
00067   CFTypeRef blob;
00068   CFArrayRef sources;
00069 
00070   klfDbg("_klf_mac_battery_info()") ;
00071 
00072   bool have_battery = init_power_sources_info(&blob, &sources);
00073 
00074   KLFSysInfo::BatteryInfo info;
00075 
00076   if(!have_battery) {
00077     klfDbg("_klf_mac_battery_info(): unable to get battery info. Probably don't have a battery.") ;
00078     info.islaptop = false;
00079     info.onbatterypower = false;
00080     return info;
00081   }
00082 
00083   CFDictionaryRef pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, 0));
00084 
00085   bool powerConnected = [(NSString*)[(NSDictionary*)pSource objectForKey:@kIOPSPowerSourceStateKey]
00086                                      isEqualToString:@kIOPSACPowerValue];
00087   klfDbg("power is connected?: "<<(bool)powerConnected) ;
00088 
00089   //BOOL outputCharging = [[(NSDictionary*)pSource objectForKey:@kIOPSIsChargingKey] boolValue];
00090 
00091   //   bool outputInstalled = [[(NSDictionary*)pSource objectForKey:@kIOPSIsPresentKey] boolValue];
00092   //   bool outputConnected = [(NSString*)[(NSDictionary*)pSource objectForKey:@kIOPSPowerSourceStateKey] isEqualToString:@kIOPSACPowerValue];
00093   //   //BOOL outputCharging = [[(NSDictionary*)pSource objectForKey:@kIOPSIsChargingKey] boolValue];
00094   //   double outputCurrent = [[(NSDictionary*)pSource objectForKey:@kIOPSCurrentKey] doubleValue];
00095   //   double outputVoltage = [[(NSDictionary*)pSource objectForKey:@kIOPSVoltageKey] doubleValue];
00096   //   double outputCapacity = [[(NSDictionary*)pSource objectForKey:@kIOPSCurrentCapacityKey] doubleValue];
00097   //   double outputMaxCapacity = [[(NSDictionary*)pSource objectForKey:@kIOPSMaxCapacityKey] doubleValue];
00098   
00099   //  klfDbg("battery status: installed="<<outputInstalled<<"; connected="<<outputConnected<<"; charging="<<outputCharging<<"; current="<<outputCurrent<<"; voltage="<<outputVoltage<<"; capacity="<<outputCapacity<<"; outputMaxCapacity="<<outputMaxCapacity);
00100 
00101   CFRelease(blob);
00102   CFRelease(sources);
00103 
00104   info.islaptop = true;
00105   info.onbatterypower = !powerConnected;
00106 
00107   return info;
00108 }
00109 
00110 KLF_EXPORT bool _klf_mac_is_laptop()
00111 {
00112   CFTypeRef blob;
00113   CFArrayRef sources;
00114 
00115   bool have_battery = init_power_sources_info(&blob, &sources);
00116 
00117   CFRelease(blob);
00118   CFRelease(sources);
00119   return have_battery;
00120 }
00121 
00122 KLF_EXPORT bool _klf_mac_is_on_battery_power()
00123 {
00124   KLFSysInfo::BatteryInfo inf = _klf_mac_battery_info();
00125   return inf.onbatterypower;
00126 }
00127 

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net