]> git.saurik.com Git - apple/xnu.git/blobdiff - iokit/Kernel/IOPMPowerSource.cpp
xnu-2422.110.17.tar.gz
[apple/xnu.git] / iokit / Kernel / IOPMPowerSource.cpp
index 281377d3f1d99cbe21f6137720cefd543b5fc3eb..614f4caa3bea998746d41b41cd862fbe20bf8440 100644 (file)
@@ -132,6 +132,9 @@ void IOPMPowerSource::updateStatus (void)
     OSObject                        *iteratorKey;
     OSObject                        *obj;
 
+    // do nothing if settings haven't changed
+    if(!settingsChangedSinceUpdate) return;
+
     iterator = OSCollectionIterator::withCollection(properties);
     if(!iterator) return;
 
@@ -146,6 +149,8 @@ void IOPMPowerSource::updateStatus (void)
     }
     iterator->release();
 
+    settingsChangedSinceUpdate = false;
+
     // And up goes the flare
     messageClients(kIOPMMessageBatteryStatusHasChanged);
 }
@@ -157,125 +162,132 @@ void IOPMPowerSource::updateStatus (void)
  *
  ******************************************************************************/
  
+void IOPMPowerSource::setPSProperty(const OSSymbol *key, OSObject *val)
+{
+    OSObject    *lastVal;
+
+    if(!key || !val) return;
+
+    // Compare new setting with existing setting; update 
+    // 'settingsChangedSinceUpdate' if the setting has changed.
+    // If values are OSNumbers, do equality comparison.
+    // Otherwise, just compare pointers.
+    
+    if( (lastVal = properties->getObject(key)) ) {
+       if(val->isEqualTo(lastVal)) {
+           // settings didn't change
+       } else {
+           // num val is not equal to last val
+           settingsChangedSinceUpdate = true;
+       }
+    } else {
+        // new setting; no last value
+        settingsChangedSinceUpdate = true;
+    }
+    
+    // here's the part where we go crazy.
+    properties->setObject(key, val);
+}
+
+
 void IOPMPowerSource::setExternalConnected(bool b) {
-    properties->setObject(
-                externalConnectedKey,
-                b ? kOSBooleanTrue:kOSBooleanFalse);    
+    setPSProperty(externalConnectedKey,
+            b ? kOSBooleanTrue : kOSBooleanFalse);    
 }
 
 void IOPMPowerSource::setExternalChargeCapable(bool b) {
-    properties->setObject(
-                externalChargeCapableKey,
-                b ? kOSBooleanTrue:kOSBooleanFalse);    
+    setPSProperty(externalChargeCapableKey,
+            b ? kOSBooleanTrue : kOSBooleanFalse);    
 }
 
 void IOPMPowerSource::setBatteryInstalled(bool b) {
-    properties->setObject(
-                batteryInstalledKey,
-                b ? kOSBooleanTrue:kOSBooleanFalse);    
+    setPSProperty(batteryInstalledKey,
+            b ? kOSBooleanTrue : kOSBooleanFalse);    
 }
 
 void IOPMPowerSource::setIsCharging(bool b) {
-    properties->setObject(
-                chargingKey,
-                b ? kOSBooleanTrue:kOSBooleanFalse);    
+    setPSProperty(chargingKey,
+            b ? kOSBooleanTrue : kOSBooleanFalse);    
 }
 
 void IOPMPowerSource::setAtWarnLevel(bool b) {
-    properties->setObject(
-                warnLevelKey,
-                b ? kOSBooleanTrue:kOSBooleanFalse);    
+    setPSProperty(warnLevelKey,
+            b ? kOSBooleanTrue : kOSBooleanFalse);    
 }
 
 void IOPMPowerSource::setAtCriticalLevel(bool b) {
-    properties->setObject(
-                criticalLevelKey,
-                b ? kOSBooleanTrue:kOSBooleanFalse);    
+    setPSProperty(criticalLevelKey,
+            b ? kOSBooleanTrue : kOSBooleanFalse);    
 }
 
 
 void IOPMPowerSource::setCurrentCapacity(unsigned int val) {
     OSNumber *n = OSNumber::withNumber(val, 32);
-    properties->setObject(
-                currentCapacityKey,
-                n);
+    setPSProperty(currentCapacityKey, n);
     n->release();
 }
 
 void IOPMPowerSource::setMaxCapacity(unsigned int val) {
     OSNumber *n = OSNumber::withNumber(val, 32);
-    properties->setObject(
-                maxCapacityKey,
-                n);
+    setPSProperty(maxCapacityKey, n);
     n->release();
 }
 
 void IOPMPowerSource::setTimeRemaining(int val) {
     OSNumber *n = OSNumber::withNumber(val, 32);
-    properties->setObject(
-                timeRemainingKey,
-                n);
+    setPSProperty(timeRemainingKey, n);
     n->release();
 }
 
 void IOPMPowerSource::setAmperage(int val) {
     OSNumber *n = OSNumber::withNumber(val, 32);
-    properties->setObject(
-                amperageKey,
-                n);
+    setPSProperty(amperageKey, n);
     n->release();
 }
 
 void IOPMPowerSource::setVoltage(unsigned int val) {
     OSNumber *n = OSNumber::withNumber(val, 32);
-    properties->setObject(
-                voltageKey,
-                n);
+    setPSProperty(voltageKey, n);
     n->release();
 }
 
 void IOPMPowerSource::setCycleCount(unsigned int val) {
     OSNumber *n = OSNumber::withNumber(val, 32);
-    properties->setObject(
-                cycleCountKey,
-                n);
+    setPSProperty(cycleCountKey, n);
     n->release();
 }
 
 void IOPMPowerSource::setAdapterInfo(int val) {
     OSNumber *n = OSNumber::withNumber(val, 32);
-    properties->setObject(
-                adapterInfoKey,
-                n);
+    setPSProperty(adapterInfoKey, n);
     n->release();
 }
 
 void IOPMPowerSource::setLocation(int val) {
     OSNumber *n = OSNumber::withNumber(val, 32);
-    properties->setObject(
-                locationKey,
-                n);
+    setPSProperty(locationKey, n);
     n->release();
 }
 
 void IOPMPowerSource::setErrorCondition(OSSymbol *s) {
-    properties->setObject(errorConditionKey, s);
+    setPSProperty(errorConditionKey, s);
 }
 
 void IOPMPowerSource::setManufacturer(OSSymbol *s) {
-    properties->setObject(manufacturerKey, s);
+    setPSProperty(manufacturerKey, s);
 }
 
 void IOPMPowerSource::setModel(OSSymbol *s) {
-    properties->setObject(modelKey, s);
+    setPSProperty(modelKey, s);
 }
 
 void IOPMPowerSource::setSerial(OSSymbol *s) {
-    properties->setObject(serialKey, s);
+    setPSProperty(serialKey, s);
 }
 
 void IOPMPowerSource::setLegacyIOBatteryInfo(OSDictionary *d) {
-    properties->setObject(batteryInfoKey, d);
+    setPSProperty(batteryInfoKey, d);
 }
 
 
@@ -287,6 +299,11 @@ void IOPMPowerSource::setLegacyIOBatteryInfo(OSDictionary *d) {
  *
  ******************************************************************************/
 
+OSObject *IOPMPowerSource::getPSProperty(const OSSymbol *symmie) {
+    if(!symmie) return NULL;
+    return properties->getObject(symmie);
+}
+
 bool IOPMPowerSource::externalConnected(void) {
     return (kOSBooleanTrue == properties->getObject(externalConnectedKey));
 }