Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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;
00060 }
00061
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
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
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