diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2023-04-18 10:20:59 -0700 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2023-04-18 10:20:59 -0700 |
commit | 4a327a6183ac2d5d8a812357f239a244f16f8211 (patch) | |
tree | 51b2d3a29f79cc5e19d4c5295409b251d5fa95cb | |
parent | efd7ed3d2c4aca57226572b2a81e5d7ebb9f3b8b (diff) | |
download | psutil-4a327a6183ac2d5d8a812357f239a244f16f8211.tar.gz |
Windows / refact: new sensors.c file
-rw-r--r-- | psutil/_psutil_windows.c | 26 | ||||
-rw-r--r-- | psutil/arch/windows/sensors.c | 32 | ||||
-rw-r--r-- | psutil/arch/windows/sensors.h | 9 | ||||
-rwxr-xr-x | setup.py | 3 |
4 files changed, 44 insertions, 26 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 8e51c0bd..d7ffbfe3 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -35,6 +35,7 @@ #include "arch/windows/cpu.h" #include "arch/windows/mem.h" #include "arch/windows/net.h" +#include "arch/windows/sensors.h" #include "arch/windows/services.h" #include "arch/windows/socks.h" #include "arch/windows/wmi.h" @@ -1417,31 +1418,6 @@ error: } -/* - * Return battery usage stats. - */ -static PyObject * -psutil_sensors_battery(PyObject *self, PyObject *args) { - SYSTEM_POWER_STATUS sps; - - if (GetSystemPowerStatus(&sps) == 0) { - PyErr_SetFromWindowsErr(0); - return NULL; - } - return Py_BuildValue( - "iiiI", - sps.ACLineStatus, // whether AC is connected: 0=no, 1=yes, 255=unknown - // status flag: - // 1, 2, 4 = high, low, critical - // 8 = charging - // 128 = no battery - sps.BatteryFlag, - sps.BatteryLifePercent, // percent - sps.BatteryLifeTime // remaining secs - ); -} - - // ------------------------ Python init --------------------------- static PyMethodDef diff --git a/psutil/arch/windows/sensors.c b/psutil/arch/windows/sensors.c new file mode 100644 index 00000000..fbe2c2fe --- /dev/null +++ b/psutil/arch/windows/sensors.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <Python.h> +#include <windows.h> + + +// Added in https://github.com/giampaolo/psutil/commit/109f873 in 2017. +// Moved in here in 2023. +PyObject * +psutil_sensors_battery(PyObject *self, PyObject *args) { + SYSTEM_POWER_STATUS sps; + + if (GetSystemPowerStatus(&sps) == 0) { + PyErr_SetFromWindowsErr(0); + return NULL; + } + return Py_BuildValue( + "iiiI", + sps.ACLineStatus, // whether AC is connected: 0=no, 1=yes, 255=unknown + // status flag: + // 1, 2, 4 = high, low, critical + // 8 = charging + // 128 = no battery + sps.BatteryFlag, + sps.BatteryLifePercent, // percent + sps.BatteryLifeTime // remaining secs + ); +} diff --git a/psutil/arch/windows/sensors.h b/psutil/arch/windows/sensors.h new file mode 100644 index 00000000..edace25d --- /dev/null +++ b/psutil/arch/windows/sensors.h @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <Python.h> + +PyObject *psutil_sensors_battery(PyObject *self, PyObject *args); @@ -213,11 +213,12 @@ if WINDOWS: 'psutil/arch/windows/process_utils.c', 'psutil/arch/windows/process_info.c', 'psutil/arch/windows/process_handles.c', + 'psutil/arch/windows/cpu.c', 'psutil/arch/windows/disk.c', 'psutil/arch/windows/mem.c', 'psutil/arch/windows/net.c', - 'psutil/arch/windows/cpu.c', 'psutil/arch/windows/security.c', + 'psutil/arch/windows/sensors.c', 'psutil/arch/windows/services.c', 'psutil/arch/windows/socks.c', 'psutil/arch/windows/wmi.c', |