From 4a327a6183ac2d5d8a812357f239a244f16f8211 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Tue, 18 Apr 2023 10:20:59 -0700 Subject: Windows / refact: new sensors.c file --- psutil/_psutil_windows.c | 26 +------------------------- psutil/arch/windows/sensors.c | 32 ++++++++++++++++++++++++++++++++ psutil/arch/windows/sensors.h | 9 +++++++++ setup.py | 3 ++- 4 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 psutil/arch/windows/sensors.c create mode 100644 psutil/arch/windows/sensors.h 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 +#include + + +// 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 + +PyObject *psutil_sensors_battery(PyObject *self, PyObject *args); diff --git a/setup.py b/setup.py index 9fda1b30..434522d2 100755 --- a/setup.py +++ b/setup.py @@ -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', -- cgit v1.2.1