-
-
Notifications
You must be signed in to change notification settings - Fork 611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix AC/battery detection logic on FreeBSD. #1972
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -427,24 +427,50 @@ double get_acpi_temperature(int fd) { | |||||
return 0.0; | ||||||
} | ||||||
|
||||||
// If a leaf MIB in the sysctl tree returns ENOENT, that means the entry does | ||||||
// not exist. On the contrary, if a non-leaf entry *does exist*, then EISDIR | ||||||
// errno is returned, meaning it exists, but it is an array/directory with | ||||||
// more elements hanging from it. | ||||||
static int sysctl_mib_exists(const char *mib) | ||||||
{ | ||||||
size_t len; | ||||||
void *p = NULL; | ||||||
sysctlbyname(mib, p, &len, NULL, 0); | ||||||
return !(errno == ENOENT); | ||||||
} | ||||||
|
||||||
static void get_battery_stats(int *battime, int *batcapacity, int *batstate, | ||||||
int *ac) { | ||||||
if (battime && GETSYSCTL("hw.acpi.battery.time", *battime)) { | ||||||
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n"); | ||||||
} | ||||||
if (batcapacity && GETSYSCTL("hw.acpi.battery.life", *batcapacity)) { | ||||||
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n"); | ||||||
int battery_present = sysctl_mib_exists("hw.acpi.battery"); | ||||||
int ac_present = sysctl_mib_exists("hw.acpi.acline"); | ||||||
|
||||||
if (!battery_present && !ac_present) { | ||||||
// According to acpi(4), hw.acpi.acline is optional and only present | ||||||
// if supported by the hardware. If no battery and acline is detected, | ||||||
// for sure we are running on an AC line. | ||||||
*ac = 1; | ||||||
*batstate = 7; | ||||||
return; | ||||||
} | ||||||
if (batstate && GETSYSCTL("hw.acpi.battery.state", *batstate)) { | ||||||
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n"); | ||||||
|
||||||
if (battery_present) { | ||||||
if (battime && GETSYSCTL("hw.acpi.battery.time", *battime)) { | ||||||
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n"); | ||||||
} | ||||||
if (batcapacity && GETSYSCTL("hw.acpi.battery.life", *batcapacity)) { | ||||||
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
if (batstate && GETSYSCTL("hw.acpi.battery.state", *batstate)) { | ||||||
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
if (ac && GETSYSCTL("hw.acpi.acline", *ac)) { | ||||||
if (ac_present && ac && GETSYSCTL("hw.acpi.acline", *ac)) { | ||||||
fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n"); | ||||||
} | ||||||
} | ||||||
|
||||||
void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item) { | ||||||
int battime, batcapacity, batstate, ac; | ||||||
int battime = 0, batcapacity = 0, batstate = 0, ac = 0; | ||||||
(void)bat; | ||||||
|
||||||
get_battery_stats(&battime, &batcapacity, &batstate, &ac); | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.