Skip to content
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

win: implement battery (Draft) #4271

Draft
wants to merge 2 commits into
base: v1.x
Choose a base branch
from
Draft

win: implement battery (Draft) #4271

wants to merge 2 commits into from

Conversation

oluan
Copy link
Contributor

@oluan oluan commented Dec 31, 2023

This is my initial implementation (WIP) of the battery API on Windows.

I left some comments in the code.

Signed-off-by: Juan José Arboleda <soyjuanarbol@gmail.com>
@oluan
Copy link
Contributor Author

oluan commented Dec 31, 2023

The results in my VM (no battery) are the following:

uv_battery_info:
  is_chaging: 1
  charge_time_in_secs: -1
  discharge_time_in_secs: -1
  level: -1

I also ran the tests on a Windows laptop, which resulted in:

uv_battery_info:
  is_chaging: 1
  charge_time_in_secs: -1
  discharge_time_in_secs: -1
  level: 6

Which is essentialy the same data I get on the browser API.

@oluan oluan marked this pull request as draft December 31, 2023 00:57
Initial implementation of uv battery api for Win32
#include "winapi.h"

int uv__estimate_remaining_charging_time_in_seconds(SYSTEM_POWER_STATUS* sys_power_status) {
int remaining;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nits:

  • 2 space indent, not 4
  • lines should be <= 80 columns

This function is only used in this file so it should be static. That sys_power_status argument is used a lot so I'd give it a short name like sps or simply just p.

Comment on lines +28 to +31
if (sys_power_status->ACLineStatus != 1) {
remaining = -1;
goto out;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to goto here, you can just return early, likewise below:

if (sys_power_status->ACLineStatus != 1)
  return -1;

(for some reason I can't post it as a diff)

int err;

if (!GetSystemPowerStatus(&sys_power_status)) {
err = /* Not sure... */1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should call GetLastError() and convert with uv_translate_sys_error()?

@oluan
Copy link
Contributor Author

oluan commented Feb 2, 2024

I will update this PR soon

@ptlomholt
Copy link
Contributor

ptlomholt commented Feb 27, 2024

@oluan : do you intend to add implementations for other OS'es?

@ptlomholt
Copy link
Contributor

Are the maintainers likely to accept this new API?

// (battery and/or UPS's) are empty
// Convert minutes into seconds
if (info->charging > 0) info->charging *= 60;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function only populates the charging field (which doesn't exist, see below) of the info structure, the other fields should be given meaningful values

// Anything else the estimated minutes remaining until all power sources
// (battery and/or UPS's) are empty
// Convert minutes into seconds
if (info->charging > 0) info->charging *= 60;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no field named charging, did you intend this to be the discharge_time_in_secs field?

@ptlomholt
Copy link
Contributor

Looks like I missed the corresponding #4258 for Linux/macos so my comments here do not appear to be relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants