[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfdefs_mac.mm
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfdefs_mac.cpp
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfdefs_mac.mm 844 2012-12-22 15:30:14Z phfaist $ */
23 
29 #include <Cocoa/Cocoa.h>
30 
31 #import <IOKit/ps/IOPowerSources.h>
32 #import <IOKit/ps/IOPSKeys.h>
33 
34 #include <klfdefs.h>
35 #include <klfsysinfo.h>
36 
37 
39 {
40  static bool is_64 = (sizeof(void*) == 8);
41 
42  SInt32 x;
43  Gestalt(gestaltSysArchitecture, &x);
44  if (x == gestaltPowerPC) {
45  return is_64 ? QString::fromLatin1("ppc64") : QString::fromLatin1("ppc");
46  } else if (x == gestaltIntel) {
47  return is_64 ? QString::fromLatin1("x86_64") : QString::fromLatin1("x86");
48  }
49  return QString::fromLatin1("unknown");
50 }
51 
52 
53 static bool init_power_sources_info(CFTypeRef *blobptr, CFArrayRef *sourcesptr)
54 {
55  *blobptr = IOPSCopyPowerSourcesInfo();
56  *sourcesptr = IOPSCopyPowerSourcesList(*blobptr);
57  if (CFArrayGetCount(*sourcesptr) == 0) {
58  klfDbg("Could not retrieve battery information. May be a system without battery.") ;
59  return false; // Could not retrieve battery information. System may not have a battery.
60  }
61  // we have a battery.
62  return true;
63 }
64 
66 {
67  CFTypeRef blob;
68  CFArrayRef sources;
69 
70  bool have_battery = init_power_sources_info(&blob, &sources);
71 
73 
74  if(!have_battery) {
75  info.islaptop = false;
76  info.onbatterypower = false;
77  return info;
78  }
79 
80  CFDictionaryRef pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, 0));
81 
82  bool powerConnected = [(NSString*)[(NSDictionary*)pSource objectForKey:@kIOPSPowerSourceStateKey]
83  isEqualToString:@kIOPSACPowerValue];
84  klfDbg("power is connected: "<<(bool)powerConnected) ;
85 
86  //BOOL outputCharging = [[(NSDictionary*)pSource objectForKey:@kIOPSIsChargingKey] boolValue];
87 
88  // bool outputInstalled = [[(NSDictionary*)pSource objectForKey:@kIOPSIsPresentKey] boolValue];
89  // bool outputConnected = [(NSString*)[(NSDictionary*)pSource objectForKey:@kIOPSPowerSourceStateKey] isEqualToString:@kIOPSACPowerValue];
90  // //BOOL outputCharging = [[(NSDictionary*)pSource objectForKey:@kIOPSIsChargingKey] boolValue];
91  // double outputCurrent = [[(NSDictionary*)pSource objectForKey:@kIOPSCurrentKey] doubleValue];
92  // double outputVoltage = [[(NSDictionary*)pSource objectForKey:@kIOPSVoltageKey] doubleValue];
93  // double outputCapacity = [[(NSDictionary*)pSource objectForKey:@kIOPSCurrentCapacityKey] doubleValue];
94  // double outputMaxCapacity = [[(NSDictionary*)pSource objectForKey:@kIOPSMaxCapacityKey] doubleValue];
95 
96  // klfDbg("battery status: installed="<<outputInstalled<<"; connected="<<outputConnected<<"; charging="<<outputCharging<<"; current="<<outputCurrent<<"; voltage="<<outputVoltage<<"; capacity="<<outputCapacity<<"; outputMaxCapacity="<<outputMaxCapacity);
97 
98  CFRelease(blob);
99  CFRelease(sources);
100 
101  info.islaptop = true;
102  info.onbatterypower = !powerConnected;
103 
104  return info;
105 }
106 
108 {
109  CFTypeRef blob;
110  CFArrayRef sources;
111 
112  bool have_battery = init_power_sources_info(&blob, &sources);
113 
114  CFRelease(blob);
115  CFRelease(sources);
116  return have_battery;
117 }
118 
120 {
122  return inf.onbatterypower;
123 }
124 
KLF_EXPORT QString klf_defs_sysinfo_arch()
Definition: klfdefs_mac.mm:38
KLF_EXPORT bool _klf_mac_is_on_battery_power()
Definition: klfdefs_mac.mm:119
Base declarations for klatexformula and some utilities.
#define klfDbg(streamableItems)
print debug stream items
#define KLF_EXPORT
Definition: klfdefs.h:41
QString fromLatin1(const char *str, int size)
KLF_EXPORT KLFSysInfo::BatteryInfo _klf_mac_battery_info()
Definition: klfdefs_mac.mm:65
KLF_EXPORT bool _klf_mac_is_laptop()
Definition: klfdefs_mac.mm:107

Generated by doxygen 1.8.13