-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs: #4172 Signed-off-by: Juan José Arboleda <[email protected]>
- Loading branch information
Showing
7 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/* Copyright libuv project contributors. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to | ||
* deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
*/ | ||
|
||
#include "uv.h" | ||
|
||
#include <dlfcn.h> | ||
|
||
#if defined(__APPLE__) || TARGET_OS_IPHONE | ||
#include "darwin-stub.h" | ||
#endif // defined(__APPLE__) || TARGET_OS_IPHONE | ||
|
||
int uv_battery_info(uv_battery_info_t* info) { | ||
int err; | ||
#if defined(__APPLE__) || TARGET_OS_IPHONE | ||
CFTypeRef (*pIOPSCopyPowerSourcesInfo)(void); | ||
CFArrayRef (*pIOPSCopyPowerSourcesList)(CFTypeRef); | ||
CFIndex (*pCFArrayGetCount)(CFArrayRef); | ||
CFArrayRef (*pIOPSGetPowerSourceDescription)(CFTypeRef, CFTypeRef); | ||
void (*pCFRelease)(CFTypeRef); | ||
void (*pCFNumberGetValue)(CFNumberRef, int, void *); | ||
void* (*pCFArrayGetValueAtIndex)(CFArrayRef, CFIndex); | ||
void* (*pCFDictionaryGetValue)(CFDictionaryRef, const void*); | ||
int (*pCFBooleanGetValue)(CFBooleanRef); | ||
CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef, | ||
const char*, | ||
CFStringEncoding); | ||
|
||
CFBooleanRef isChargingValue; | ||
CFDictionaryRef powerSource; | ||
CFNumberRef currentCapacity; | ||
CFNumberRef timeRemainingValue; | ||
CFArrayRef sources = NULL; | ||
CFTypeRef blob = NULL; | ||
|
||
int holder = 0; | ||
int is_charging = 0; | ||
void* core_foundation_handle; | ||
void* iokit_framekwork_handle; | ||
|
||
err = UV_ENOENT; | ||
iokit_framekwork_handle = dlopen("/System/Library/Frameworks/" | ||
"IOKit.framework/IOKit", | ||
RTLD_LAZY | RTLD_LOCAL); | ||
|
||
core_foundation_handle = dlopen("/System/Library/Frameworks/" | ||
"CoreFoundation.framework/CoreFoundation", | ||
RTLD_LAZY | RTLD_LOCAL); | ||
|
||
if (iokit_framekwork_handle == NULL || core_foundation_handle == NULL) | ||
goto out; | ||
|
||
*(void **)(&pCFRelease) = dlsym(core_foundation_handle, "CFRelease"); | ||
*(void **)(&pCFStringCreateWithCString) = | ||
dlsym(core_foundation_handle, "CFStringCreateWithCString"); | ||
*(void **)(&pCFArrayGetCount) = | ||
dlsym(core_foundation_handle, "CFArrayGetCount"); | ||
*(void **)(&pCFArrayGetValueAtIndex) = | ||
dlsym(core_foundation_handle, "CFArrayGetValueAtIndex"); | ||
*(void **)(&pCFNumberGetValue) = | ||
dlsym(core_foundation_handle, "CFNumberGetValue"); | ||
*(void **)(&pCFBooleanGetValue) = | ||
dlsym(core_foundation_handle, "CFBooleanGetValue"); | ||
*(void **)(&pCFDictionaryGetValue) = | ||
dlsym(core_foundation_handle, "CFDictionaryGetValue"); | ||
*(void **)(&pIOPSCopyPowerSourcesInfo) = | ||
dlsym(iokit_framekwork_handle, "IOPSCopyPowerSourcesInfo"); | ||
*(void **)(&pIOPSCopyPowerSourcesList) = | ||
dlsym(iokit_framekwork_handle, "IOPSCopyPowerSourcesList"); | ||
*(void **)(&pIOPSGetPowerSourceDescription) = | ||
dlsym(iokit_framekwork_handle, "IOPSGetPowerSourceDescription"); | ||
|
||
if (pCFRelease == NULL || | ||
pCFStringCreateWithCString == NULL || | ||
pCFArrayGetCount == NULL || | ||
pCFArrayGetValueAtIndex == NULL || | ||
pCFNumberGetValue == NULL || | ||
pCFDictionaryGetValue == NULL || | ||
pIOPSCopyPowerSourcesInfo == NULL || | ||
pIOPSCopyPowerSourcesList == NULL || | ||
pIOPSGetPowerSourceDescription == NULL) { | ||
goto out; | ||
} | ||
|
||
#define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8) | ||
|
||
blob = pIOPSCopyPowerSourcesInfo(); | ||
if (blob == NULL) | ||
goto out; | ||
|
||
sources = pIOPSCopyPowerSourcesList(blob); | ||
if (sources == NULL) | ||
goto out; | ||
|
||
if (pCFArrayGetCount(sources) == 0) | ||
goto out; | ||
|
||
powerSource = | ||
pIOPSGetPowerSourceDescription(blob, pCFArrayGetValueAtIndex(sources, 0)); | ||
if (powerSource == NULL) | ||
goto out; | ||
|
||
err = 0; | ||
|
||
// kIOPSCurrentCapacityKey | ||
currentCapacity = pCFDictionaryGetValue(powerSource, S("Current Capacity")); | ||
if (currentCapacity != NULL) { | ||
pCFNumberGetValue(currentCapacity, pkCFNumberIntType, &holder); | ||
info->level = holder; | ||
} | ||
|
||
|
||
// Retrieve is_charging status | ||
// kIOPSIsChargingKey | ||
isChargingValue = pCFDictionaryGetValue(powerSource, S("Is Charging")); | ||
if (isChargingValue != NULL) { | ||
is_charging = pCFBooleanGetValue(isChargingValue); | ||
info->is_charging = is_charging; | ||
} | ||
|
||
// kIOPSTimeToFullChargeKey and kIOPSTimeToEmptyKey | ||
timeRemainingValue = | ||
pCFDictionaryGetValue(powerSource, | ||
is_charging ? S("Time to Full Charge") : S("Time to Empty")); | ||
if (timeRemainingValue != NULL) { | ||
pCFNumberGetValue(timeRemainingValue, pkCFNumberIntType, &holder); | ||
} | ||
|
||
// A value of -1 indicates "Still Calculating the Time" | ||
if (holder == -1) | ||
goto out; | ||
|
||
holder = holder * 60; // Default unit is minutes, convert to seconds | ||
if (is_charging == 1) { | ||
info->charge_time_in_secs = holder; | ||
info->discharge_time_in_secs = 0; | ||
} else { | ||
info->discharge_time_in_secs = holder; | ||
info->charge_time_in_secs = 0; | ||
} | ||
out: | ||
if (iokit_framekwork_handle != NULL) { | ||
dlclose(iokit_framekwork_handle); | ||
} | ||
|
||
if (core_foundation_handle != NULL) { | ||
dlclose(core_foundation_handle); | ||
} | ||
|
||
if (blob != NULL) { | ||
pCFRelease(blob); | ||
} | ||
|
||
if (sources != NULL) { | ||
pCFRelease(sources); | ||
} | ||
|
||
#elif defined(__LINUX__) | ||
// To be implemented | ||
err = UV_ENOSYS; | ||
#endif // defined(__APPLE__) || TARGET_OS_IPHONE | ||
return err; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* Copyright libuv project contributors. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to | ||
* deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
*/ | ||
#include "uv.h" | ||
|
||
int uv_battery_info(uv_battery_info_t* info) { | ||
// To be implemented | ||
return UV_ENOSYS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters