| 1 |
/* |
| 2 |
* Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style license that can be |
| 4 |
* found in the LICENSE file. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <Python.h> |
| 8 |
#include <windows.h> |
| 9 |
|
| 10 |
#include "../../arch/all/init.h" |
| 11 |
|
| 12 |
|
| 13 |
// Added in https://github.com/giampaolo/psutil/commit/109f873 in 2017. |
| 14 |
// Moved in here in 2023. |
| 15 |
PyObject * |
| 16 |
psutil_sensors_battery(PyObject *self, PyObject *args) { |
| 17 |
SYSTEM_POWER_STATUS sps; |
| 18 |
|
| 19 |
if (GetSystemPowerStatus(&sps) == 0) { |
| 20 |
PyErr_SetFromWindowsErr(0); |
| 21 |
return NULL; |
| 22 |
} |
| 23 |
return Py_BuildValue( |
| 24 |
"iiiI", |
| 25 |
sps.ACLineStatus, // whether AC is connected: 0=no, 1=yes, 255=unknown |
| 26 |
// status flag: |
| 27 |
// 1, 2, 4 = high, low, critical |
| 28 |
// 8 = charging |
| 29 |
// 128 = no battery |
| 30 |
sps.BatteryFlag, |
| 31 |
sps.BatteryLifePercent, // percent |
| 32 |
sps.BatteryLifeTime // remaining secs |
| 33 |
); |
| 34 |
} |