├── tests ├── __init__.py ├── observer.py └── test_common.py ├── power ├── version.py ├── win32.py ├── __init__.py ├── common.py ├── freebsd.py ├── linux.py └── darwin.py ├── AUTHORS ├── setup.cfg ├── requirements.txt ├── .gitignore ├── .travis.yml ├── LICENSE.txt ├── README.rst ├── setup.py └── docs ├── linux ├── power_supply_class.txt └── power_supply.h ├── darwin ├── IOPowerSources_h │ └── toc.html └── IOPSKeys_h │ └── toc.html └── win32 ├── SYSTEM_BATTERY_STATE.htm ├── SYSTEM_POWER_STATUS.htm └── Power Setting GUIDs.htm /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /power/version.py: -------------------------------------------------------------------------------- 1 | VERSION = '1.5' -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Ilya Kulakov 2 | Tomasz CEDRO cederom@tlen.pl 3 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [tool:pytest] 2 | addopts= --cov=power --cov-report=term-missing --cov-report=html --cov-branch --ignore=setup.py 3 | testpaths=tests 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | mock==2.0.0; python_version < '3.0' 2 | pyobjc-core==4.1; sys_platform == 'darwin' 3 | pytest==3.5.0 4 | pytest-cov==2.5.1 5 | unittest2==1.1.0; python_version < '3.0' 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | 21 | # Installer logs 22 | pip-log.txt 23 | 24 | # Unit test / coverage reports 25 | .coverage 26 | .tox 27 | nosetests.xml 28 | 29 | # Translations 30 | *.mo 31 | 32 | # Mr Developer 33 | .mr.developer.cfg 34 | .project 35 | .pydevproject 36 | -------------------------------------------------------------------------------- /tests/observer.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import power 3 | 4 | 5 | class TestObserver(power.PowerManagementObserver): 6 | def on_power_sources_change(self, power_management): 7 | print("on_power_sources_change") 8 | 9 | def on_time_remaining_change(self, power_management): 10 | print("on_time_remaining_change") 11 | 12 | 13 | if __name__ == "__main__": 14 | o = TestObserver() 15 | p = power.PowerManagement() 16 | p.add_observer(o) 17 | try: 18 | print("Power management observer is registered") 19 | import time 20 | while True: 21 | time.sleep(1) 22 | finally: 23 | p.remove_observer(o) 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | sudo: false 3 | 4 | os: 5 | - linux 6 | 7 | python: 8 | - 2.7 9 | - 3.4 10 | - 3.5 11 | - 3.6 12 | - pypy 13 | - pypy3 14 | 15 | install: 16 | - pip install .[tests] -c requirements.txt 17 | - pip install codecov flake8 18 | 19 | script: 20 | - python setup.py test 21 | 22 | after_success: 23 | - flake8 power 24 | - codecov 25 | 26 | deploy: 27 | provider: pypi 28 | user: Ilya.Kulakov 29 | password: 30 | secure: "Lm2m4xqLGyqaSVtsHjyJKAjYxHAe+gB8TSmZZOkmSKfG+g61lAj3mtX8sAXiYzVcsFDe8cX4XvS3i/K2LoOh9dnc9EureDylvtfzjdKfLRqVjHAcHEeGLBck5tfgQXi3zKiB2EE3Pmu2pV+U0UvhpV40PgaFCroMJGJKI8shme8=" 31 | on: 32 | tags: true 33 | branch: master 34 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Ilya Kulakov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Power 2 | ===== 3 | .. image:: https://travis-ci.org/Kentzo/Power.svg?branch=master 4 | :target: https://travis-ci.org/Kentzo/Power 5 | .. image:: https://codecov.io/gh/Kentzo/Power/branch/master/graph/badge.svg 6 | :target: https://codecov.io/gh/Kentzo/Power 7 | .. image:: https://img.shields.io/pypi/v/Power.svg 8 | :target: https://pypi.python.org/pypi/Power 9 | .. image:: https://pyup.io/repos/github/Kentzo/Power/shield.svg 10 | :target: https://pyup.io/repos/github/Kentzo/Power/ 11 | :alt: Updates 12 | 13 | Crossplatform (Windows, Linux, Mac OS X, FreeBSD) python module to access power capabilities of the system. 14 | 15 | - Power source type: AC, Battery or UPS 16 | - Battery warning level: no warning (None), less than 22% of battery (Early), less than 10min (Final) 17 | - Time remaining estimate 18 | - Fault tolerant: if for some reason power capabilities cannot be extracted, falls back to AC 19 | - Support for multiple batteries 20 | - Power changes can be observed (Mac OS X only for now) 21 | - Very easy to extend to support new features or new systems 22 | 23 | 24 | 25 | Examples 26 | -------- 27 | 28 | Until we expand the documentation please have a look in power/tests.py 29 | 30 | 31 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | import os 4 | import sys 5 | 6 | from setuptools import setup 7 | from setuptools.command.test import test as TestCommand 8 | 9 | 10 | REQUIREMENTS = [] 11 | 12 | if sys.platform.startswith('darwin'): 13 | REQUIREMENTS.append('pyobjc-core >= 2.5') 14 | 15 | 16 | TEST_REQUIREMENTS = [ 17 | 'pytest', 18 | 'pytest-cov', 19 | ] 20 | 21 | if sys.version_info < (3,): 22 | TEST_REQUIREMENTS.extend([ 23 | 'mock', 24 | 'unittest2' 25 | ]) 26 | 27 | 28 | with open(os.path.join(os.path.dirname(__file__), 'power', 'version.py')) as f: 29 | VERSION = None 30 | code = compile(f.read(), 'version.py', 'exec') 31 | exec(code) 32 | assert VERSION 33 | 34 | 35 | class PyTest(TestCommand): 36 | user_options = [('pytest-args=', 'a', "Arguments to pass to pytest")] 37 | 38 | def initialize_options(self): 39 | TestCommand.initialize_options(self) 40 | self.pytest_args = '' 41 | 42 | def run_tests(self): 43 | import shlex 44 | #import here, cause outside the eggs aren't loaded 45 | import pytest 46 | errno = pytest.main(shlex.split(self.pytest_args)) 47 | sys.exit(errno) 48 | 49 | 50 | setup( 51 | name="power", 52 | version=VERSION, 53 | description="Cross-platform system power status information.", 54 | long_description="Library that allows you get current power source type (AC, Battery or UPS), " 55 | "warning level (none, <22%, <10min) and remaining minutes. " 56 | "You can also observe changes of power source and remaining time.", 57 | author="Ilya Kulakov", 58 | author_email="kulakov.ilya@gmail.com", 59 | url="https://github.com/Kentzo/Power", 60 | platforms=["Mac OS X 10.6+", "Windows XP+", "Linux 2.6+", "FreeBSD"], 61 | packages=['power'], 62 | license="MIT License", 63 | classifiers=[ 64 | 'Intended Audience :: Developers', 65 | 'Intended Audience :: System Administrators', 66 | 'License :: OSI Approved :: MIT License', 67 | 'Natural Language :: English', 68 | 'Operating System :: MacOS :: MacOS X', 69 | 'Operating System :: Microsoft :: Windows', 70 | 'Operating System :: POSIX :: Linux', 71 | 'Programming Language :: Python :: 2.7', 72 | 'Programming Language :: Python :: 3.4', 73 | 'Programming Language :: Python :: 3.5', 74 | 'Programming Language :: Python :: 3.6', 75 | 'Topic :: System :: Monitoring', 76 | 'Topic :: System :: Power (UPS)', 77 | ], 78 | install_requires=REQUIREMENTS, 79 | tests_require=TEST_REQUIREMENTS, 80 | extras_require={ 81 | 'tests': TEST_REQUIREMENTS 82 | }, 83 | cmdclass={'test': PyTest} 84 | ) 85 | -------------------------------------------------------------------------------- /tests/test_common.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | from __future__ import print_function 3 | 4 | try: 5 | import unittest2 as unittest 6 | except ImportError: 7 | import unittest 8 | 9 | try: 10 | import unittest.mock as mock 11 | except ImportError: 12 | import mock 13 | 14 | import power.common 15 | 16 | 17 | class TestPowerManagementCommon(unittest.TestCase): 18 | def test_get_low_battery_warningLevel(self): 19 | level = power.PowerManagement().get_low_battery_warning_level() 20 | self.assertIsNotNone(level) 21 | self.assertIsInstance(level, int) 22 | self.assertIn(level, [power.LOW_BATTERY_WARNING_NONE, power.LOW_BATTERY_WARNING_EARLY, power.LOW_BATTERY_WARNING_FINAL]) 23 | 24 | def test_get_remaining_estimate(self): 25 | estimate = power.PowerManagement().get_time_remaining_estimate() 26 | self.assertIsNotNone(estimate) 27 | self.assertIsInstance(estimate, float) 28 | self.assertTrue(estimate == power.TIME_REMAINING_UNKNOWN or estimate == power.TIME_REMAINING_UNLIMITED or estimate >= 0.0) 29 | 30 | def test_get_providing_power_source(self): 31 | type = power.PowerManagement().get_providing_power_source_type() 32 | self.assertIsNotNone(type) 33 | self.assertIsInstance(type, int) 34 | self.assertIn(type, [power.POWER_TYPE_AC, power.POWER_TYPE_BATTERY, power.POWER_TYPE_UPS]) 35 | 36 | def test_fallback_unsupported_platform(self): 37 | with mock.patch('sys.platform', 'planb'): 38 | self.assertEqual(power.get_power_management_class(), power.common.PowerManagementNoop) 39 | 40 | def test_fallback_importError(self): 41 | with mock.patch('power.get_platform_power_management_class', side_effect=RuntimeError): 42 | self.assertEqual(power.get_power_management_class(), power.common.PowerManagementNoop) 43 | 44 | def test_fallback_usage_error(self): 45 | class PowerManagementFaulty(power.common.PowerManagementBase): 46 | def add_observer(self, observer): 47 | raise RuntimeError() 48 | 49 | def remove_observer(self, observer): 50 | raise RuntimeError() 51 | 52 | def get_providing_power_source_type(self): 53 | raise RuntimeError() 54 | 55 | def get_time_remaining_estimate(self): 56 | raise RuntimeError() 57 | 58 | def get_low_battery_warning_level(self): 59 | raise RuntimeError() 60 | 61 | with mock.patch('power.get_platform_power_management_class', return_value=PowerManagementFaulty): 62 | c = power.get_power_management_class() 63 | self.assertTrue(issubclass(c, PowerManagementFaulty)) 64 | 65 | with self.assertWarns(RuntimeWarning): 66 | pm = c() 67 | self.assertEqual(pm.get_providing_power_source_type(), power.POWER_TYPE_AC) 68 | -------------------------------------------------------------------------------- /power/win32.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | """ 3 | Implements PowerManagement functions using GetSystemPowerStatus. 4 | Requires Windows XP+. 5 | Observing is not supported 6 | """ 7 | import ctypes 8 | from ctypes import wintypes 9 | import warnings 10 | 11 | from power import common 12 | 13 | 14 | # GetSystemPowerStatus 15 | # Returns brief description of current system power status. 16 | # Windows XP+ 17 | # REQUIRED. 18 | GetSystemPowerStatus = None 19 | try: 20 | GetSystemPowerStatus = ctypes.windll.kernel32.GetSystemPowerStatus 21 | 22 | class SYSTEM_POWER_STATUS(ctypes.Structure): 23 | _fields_ = [ 24 | ('ACLineStatus', ctypes.c_ubyte), 25 | ('BatteryFlag', ctypes.c_ubyte), 26 | ('BatteryLifePercent', ctypes.c_ubyte), 27 | ('Reserved1', ctypes.c_ubyte), 28 | ('BatteryLifeTime', wintypes.DWORD), 29 | ('BatteryFullLifeTime', wintypes.DWORD) 30 | ] 31 | 32 | GetSystemPowerStatus.argtypes = [ctypes.POINTER(SYSTEM_POWER_STATUS)] 33 | GetSystemPowerStatus.restype = wintypes.BOOL 34 | except AttributeError as e: 35 | raise RuntimeError("Unable to load GetSystemPowerStatus." 36 | "The system does not provide it (Win XP is required) or kernel32.dll is damaged.") 37 | 38 | 39 | POWER_TYPE_MAP = { 40 | 0: common.POWER_TYPE_BATTERY, 41 | 1: common.POWER_TYPE_AC, 42 | 255: common.POWER_TYPE_AC 43 | } 44 | 45 | 46 | class PowerManagement(common.PowerManagementBase): 47 | def get_providing_power_source_type(self): 48 | """ 49 | Returns GetSystemPowerStatus().ACLineStatus 50 | 51 | @raise: WindowsError if any underlying error occures. 52 | """ 53 | power_status = SYSTEM_POWER_STATUS() 54 | if not GetSystemPowerStatus(ctypes.pointer(power_status)): 55 | raise ctypes.WinError() 56 | return POWER_TYPE_MAP[power_status.ACLineStatus] 57 | 58 | def get_low_battery_warning_level(self): 59 | """ 60 | Returns warning according to GetSystemPowerStatus().BatteryLifeTime/BatteryLifePercent 61 | 62 | @raise WindowsError if any underlying error occures. 63 | """ 64 | power_status = SYSTEM_POWER_STATUS() 65 | if not GetSystemPowerStatus(ctypes.pointer(power_status)): 66 | raise ctypes.WinError() 67 | 68 | if POWER_TYPE_MAP[power_status.ACLineStatus] == common.POWER_TYPE_AC: 69 | return common.LOW_BATTERY_WARNING_NONE 70 | else: 71 | if power_status.BatteryLifeTime != -1 and power_status.BatteryLifeTime <= 600: 72 | return common.LOW_BATTERY_WARNING_FINAL 73 | elif power_status.BatteryLifePercent <= 22: 74 | return common.LOW_BATTERY_WARNING_EARLY 75 | else: 76 | return common.LOW_BATTERY_WARNING_NONE 77 | 78 | def get_time_remaining_estimate(self): 79 | """ 80 | Returns time remaining estimate according to GetSystemPowerStatus().BatteryLifeTime 81 | """ 82 | power_status = SYSTEM_POWER_STATUS() 83 | if not GetSystemPowerStatus(ctypes.pointer(power_status)): 84 | raise ctypes.WinError() 85 | 86 | if POWER_TYPE_MAP[power_status.ACLineStatus] == common.POWER_TYPE_AC: 87 | return common.TIME_REMAINING_UNLIMITED 88 | elif power_status.BatteryLifeTime == -1: 89 | return common.TIME_REMAINING_UNKNOWN 90 | else: 91 | return float(power_status.BatteryLifeTime) / 60.0 92 | 93 | def add_observer(self, observer): 94 | warnings.warn("Current system does not support observing.") 95 | pass 96 | 97 | def remove_observer(self, observer): 98 | warnings.warn("Current system does not support observing.") 99 | pass 100 | -------------------------------------------------------------------------------- /power/__init__.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | """ 3 | Provides crossplatform checking of current power source, battery warning level and battery time remaining estimate. 4 | Allows you to add observer for power notifications if platform supports it. 5 | 6 | Usage: 7 | from power import PowerManagement, PowerManagementObserver # Automatically imports platform-specific implementation 8 | 9 | class Observer(PowerManagementObserver): 10 | def on_power_sources_change(self, power_management): 11 | print("Power sources did change.") 12 | 13 | def on_time_remaining_change(self, power_management): 14 | print("Time remaining did change.") 15 | 16 | # class Observer(object): 17 | # ... 18 | # PowerManagementObserver.register(Observer) 19 | """ 20 | import sys 21 | import traceback 22 | import warnings 23 | 24 | from power.common import * 25 | from power.version import VERSION 26 | 27 | __version__ = VERSION 28 | 29 | 30 | def get_platform_power_management_class(): 31 | if sys.platform.startswith('darwin'): 32 | from power.darwin import PowerManagement as PowerManagementPlatform 33 | elif sys.platform.startswith('freebsd'): 34 | from power.freebsd import PowerManagement as PowerManagementPlatform 35 | elif sys.platform.startswith('win32'): 36 | from power.win32 import PowerManagement as PowerManagementPlatform 37 | elif sys.platform.startswith('linux'): 38 | from power.linux import PowerManagement as PowerManagementPlatform 39 | else: 40 | raise RuntimeError("{0} is not supported.".format(sys.platform)) 41 | 42 | return PowerManagementPlatform 43 | 44 | 45 | def get_power_management_class(): 46 | try: 47 | PowerManagementPlatform = get_platform_power_management_class() 48 | 49 | class PowerManagement(PowerManagementPlatform): 50 | def __init__(self, *args, **kwargs): 51 | super(PowerManagement, self).__init__(*args, **kwargs) 52 | 53 | from power.common import PowerManagementNoop 54 | self._noop = PowerManagementNoop() 55 | 56 | def add_observer(self, observer): 57 | try: 58 | return super(PowerManagement, self).add_observer(observer) 59 | except: 60 | warnings.warn("{0}.add_observer raised:\n{1}".format(PowerManagementPlatform.__name__, traceback.format_exc()), category=RuntimeWarning) 61 | return self._noop.add_observer(observer) 62 | 63 | def remove_observer(self, observer): 64 | try: 65 | return super(PowerManagement, self).remove_observer(observer) 66 | except: 67 | warnings.warn("{0}.remove_observer raised:\n{1}".format(PowerManagementPlatform.__name__, traceback.format_exc()), category=RuntimeWarning) 68 | return self._noop.remove_observer(observer) 69 | 70 | def get_providing_power_source_type(self): 71 | try: 72 | return super(PowerManagement, self).get_providing_power_source_type() 73 | except: 74 | warnings.warn("{0}.get_providing_power_source_type raised:\n{1}".format(PowerManagementPlatform.__name__, traceback.format_exc()), category=RuntimeWarning) 75 | return self._noop.get_providing_power_source_type() 76 | 77 | def get_time_remaining_estimate(self): 78 | try: 79 | return super(PowerManagement, self).get_time_remaining_estimate() 80 | except: 81 | warnings.warn("{0}.get_time_remaining_estimate raised:\n{1}".format(PowerManagementPlatform.__name__, traceback.format_exc()), category=RuntimeWarning) 82 | return self._noop.get_time_remaining_estimate() 83 | 84 | def get_low_battery_warning_level(self): 85 | try: 86 | return super(PowerManagement, self).get_low_battery_warning_level() 87 | except: 88 | warnings.warn("{0}.get_low_battery_warning_level raised:\n{1}".format(PowerManagementPlatform.__name__, traceback.format_exc()), category=RuntimeWarning) 89 | return self._noop.get_low_battery_warning_level() 90 | except (RuntimeError, ImportError) as e: 91 | warnings.warn("Unable to load PowerManagement, no-op PowerManagement class is used instead: {0}".format(e), category=RuntimeWarning) 92 | from power.common import PowerManagementNoop as PowerManagement 93 | 94 | return PowerManagement 95 | 96 | 97 | PowerManagement = get_power_management_class() 98 | -------------------------------------------------------------------------------- /power/common.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | """ 3 | Represents common constants and classes for all platforms. 4 | 5 | @group Power Source Type: POWER_TYPE_AC, POWER_TYPE_BATTERY, POWER_TYPE_UPS 6 | @var POWER_TYPE_AC: The system is connected to the external power source. 7 | @var POWER_TYPE_BATTERY: The system is connected to the battery. 8 | @var POWER_TYPE_UPS: The system is connected to UPS. 9 | @type POWER_TYPE_BATTERY: int 10 | @type POWER_TYPE_AC: int 11 | @type POWER_TYPE_UPS: int 12 | 13 | @group Low Battery Warning Levels: LOW_BATTERY_WARNING_NONE, LOW_BATTERY_WARNING_EARLY, LOW_BATTERY_WARNING_FINAL 14 | @var LOW_BATTERY_WARNING_NONE: The system is connected to the unlimited power source. 15 | @var LOW_BATTERY_WARNING_EARLY: The battery has dropped below 22% remaining power. 16 | @var LOW_BATTERY_WARNING_FINAL: The battery can provide no more than 10 minutes of runtime. 17 | @type LOW_BATTERY_WARNING_EARLY: int 18 | @type LOW_BATTERY_WARNING_NONE: int 19 | @type LOW_BATTERY_WARNING_FINAL: int 20 | 21 | @group Special Values For Time Remaining: TIME_REMAINING_UNKNOWN, TIME_REMAINING_UNLIMITED 22 | @var TIME_REMAINING_UNKNOWN: Indicates the system is connected to a limited power source, but system is still 23 | calculating a time remaining estimate. 24 | @var TIME_REMAINING_UNLIMITED: Indicates that the system is connected to an external power source, without time limit. 25 | @type TIME_REMAINING_UNKNOWN: float 26 | @type TIME_REMAINING_UNLIMITED: float 27 | """ 28 | from abc import ABCMeta, abstractmethod 29 | import weakref 30 | 31 | __all__ = [ 32 | 'POWER_TYPE_AC', 33 | 'POWER_TYPE_BATTERY', 34 | 'POWER_TYPE_UPS', 35 | 'LOW_BATTERY_WARNING_NONE', 36 | 'LOW_BATTERY_WARNING_EARLY', 37 | 'LOW_BATTERY_WARNING_FINAL', 38 | 'TIME_REMAINING_UNKNOWN', 39 | 'TIME_REMAINING_UNLIMITED', 40 | 'PowerManagementObserver' 41 | ] 42 | 43 | 44 | POWER_TYPE_AC = 0 45 | 46 | POWER_TYPE_BATTERY = 1 47 | 48 | POWER_TYPE_UPS = 2 49 | 50 | 51 | LOW_BATTERY_WARNING_NONE = 1 52 | 53 | LOW_BATTERY_WARNING_EARLY = 2 54 | 55 | LOW_BATTERY_WARNING_FINAL = 3 56 | 57 | 58 | TIME_REMAINING_UNKNOWN = -1.0 59 | 60 | TIME_REMAINING_UNLIMITED = -2.0 61 | 62 | 63 | class PowerManagementBase(object): 64 | """ 65 | Base class for platform dependent PowerManagement functions. 66 | 67 | @ivar _weak_observers: List of weak reference to added observers 68 | @note: Platform's implementation may provide additional parameters for initialization 69 | """ 70 | __metaclass__ = ABCMeta 71 | 72 | def __init__(self): 73 | super(PowerManagementBase, self).__init__() 74 | self._weak_observers = [] 75 | 76 | @abstractmethod 77 | def get_providing_power_source_type(self): 78 | """ 79 | Returns type of the providing power source. 80 | 81 | @return: Possible values: 82 | - POWER_TYPE_AC 83 | - POWER_TYPE_BATTERY 84 | - POWER_TYPE_UPS 85 | @rtype: int 86 | """ 87 | pass 88 | 89 | @abstractmethod 90 | def get_low_battery_warning_level(self): 91 | """ 92 | Returns the system battery warning level. 93 | 94 | @return: Possible values: 95 | - LOW_BATTERY_WARNING_NONE 96 | - LOW_BATTERY_WARNING_EARLY 97 | - LOW_BATTERY_WARNING_FINAL 98 | @rtype: int 99 | """ 100 | pass 101 | 102 | @abstractmethod 103 | def get_time_remaining_estimate(self): 104 | """ 105 | Returns the estimated minutes remaining until all power sources (battery and/or UPS) are empty. 106 | 107 | @return: Special values: 108 | - TIME_REMAINING_UNKNOWN 109 | - TIME_REMAINING_UNLIMITED 110 | @rtype: float 111 | """ 112 | pass 113 | 114 | @abstractmethod 115 | def add_observer(self, observer): 116 | """ 117 | Adds weak ref to an observer. 118 | 119 | @param observer: Instance of class registered with PowerManagementObserver 120 | @raise TypeError: If observer is not registered with PowerManagementObserver abstract class 121 | """ 122 | if not isinstance(observer, PowerManagementObserver): 123 | raise TypeError("observer MUST conform to power.PowerManagementObserver") 124 | self._weak_observers.append(weakref.ref(observer)) 125 | 126 | @abstractmethod 127 | def remove_observer(self, observer): 128 | """ 129 | Removes an observer. 130 | 131 | @param observer: Previously added observer 132 | """ 133 | self._weak_observers.remove(weakref.ref(observer)) 134 | 135 | def remove_all_observers(self): 136 | """ 137 | Removes all registered observers. 138 | """ 139 | for weak_observer in self._weak_observers: 140 | observer = weak_observer() 141 | if observer: 142 | self.remove_observer(observer) 143 | 144 | 145 | class PowerManagementObserver: 146 | """ 147 | Base class for PowerManagement observers. 148 | Do not make assumptions in what thread or event loop these methods are called. 149 | """ 150 | __metaclass__ = ABCMeta 151 | 152 | @abstractmethod 153 | def on_power_sources_change(self, power_management): 154 | """ 155 | @param power_management: Instance of PowerManagement posted notification 156 | """ 157 | pass 158 | 159 | @abstractmethod 160 | def on_time_remaining_change(self, power_management): 161 | """ 162 | @param power_management: Instance of PowerManagement posted notification 163 | """ 164 | pass 165 | 166 | 167 | class PowerManagementNoop(PowerManagementBase): 168 | """ 169 | No-op subclass of PowerManagement. 170 | It operates like AC is always attached and power sources are never changed. 171 | """ 172 | def get_providing_power_source_type(self): 173 | """ 174 | @return: Always POWER_TYPE_AC 175 | """ 176 | return POWER_TYPE_AC 177 | 178 | def get_low_battery_warning_level(self): 179 | """ 180 | @return: Always LOW_BATTERY_WARNING_NONE 181 | """ 182 | return LOW_BATTERY_WARNING_NONE 183 | 184 | def get_time_remaining_estimate(self): 185 | """ 186 | @return: Always TIME_REMAINING_UNLIMITED 187 | """ 188 | return TIME_REMAINING_UNLIMITED 189 | 190 | def add_observer(self, observer): 191 | """ 192 | Does nothing. 193 | """ 194 | pass 195 | 196 | def remove_observer(self, observer): 197 | """ 198 | Does nothing. 199 | """ 200 | pass 201 | 202 | def remove_all_observers(self): 203 | """ 204 | Does nothing. 205 | """ 206 | pass 207 | -------------------------------------------------------------------------------- /power/freebsd.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | """ 3 | Implements PowerManagement functions using FreeBSD SYSCTL mechanism. 4 | FreeBSD portion written by Tomasz CEDRO (http://www.tomek.cedro.info) 5 | """ 6 | import os 7 | import warnings 8 | from power import common 9 | import subprocess 10 | 11 | class PowerManagement(common.PowerManagementBase): 12 | @staticmethod 13 | def power_source_type(): 14 | """ 15 | FreeBSD use sysctl hw.acpi.acline to tell if Mains (1) is used or Battery (0). 16 | Beware, that on a Desktop machines this hw.acpi.acline oid may not exist. 17 | @return: One of common.POWER_TYPE_* 18 | @raise: Runtime error if type of power source is not supported 19 | """ 20 | try: 21 | supply=int(subprocess.check_output(["sysctl","-n","hw.acpi.acline"])) 22 | except: 23 | return common.POWER_TYPE_AC 24 | 25 | if supply == 1: 26 | return common.POWER_TYPE_AC 27 | elif supply == 0: 28 | return common.POWER_TYPE_BATTERY 29 | else: 30 | raise RuntimeError("Unknown power source type!") 31 | 32 | 33 | @staticmethod 34 | def is_ac_online(): 35 | """ 36 | @return: True if ac is online. Otherwise False 37 | """ 38 | try: 39 | supply=int(subprocess.check_output(["sysctl","-n","hw.acpi.acline"])) 40 | except: 41 | return True 42 | return supply == 1 43 | 44 | 45 | @staticmethod 46 | def is_battery_present(): 47 | """ 48 | TODO 49 | @return: True if battery is present. Otherwise False 50 | """ 51 | return False 52 | 53 | 54 | @staticmethod 55 | def is_battery_discharging(): 56 | """ 57 | TODO 58 | @return: True if ac is online. Otherwise False 59 | """ 60 | return False 61 | 62 | 63 | @staticmethod 64 | def get_battery_state(): 65 | """ 66 | TODO 67 | @return: Tuple (energy_full, energy_now, power_now) 68 | """ 69 | energy_now = float(100.0) 70 | power_now = float(100.0) 71 | energy_full = float(100.0) 72 | return energy_full, energy_now, power_now 73 | 74 | 75 | def get_providing_power_source_type(self): 76 | """ 77 | Looks through all power supplies in POWER_SUPPLY_PATH. 78 | If there is an AC adapter online returns POWER_TYPE_AC. 79 | If there is a discharging battery, returns POWER_TYPE_BATTERY. 80 | Since the order of supplies is arbitrary, whatever found first is returned. 81 | """ 82 | type = self.power_source_type() 83 | if type == common.POWER_TYPE_AC: 84 | if self.is_ac_online(): 85 | return common.POWER_TYPE_AC 86 | elif type == common.POWER_TYPE_BATTERY: 87 | if self.is_battery_present() and self.is_battery_discharging(): 88 | return common.POWER_TYPE_BATTERY 89 | else: 90 | warnings.warn("UPS is not supported.") 91 | return common.POWER_TYPE_AC 92 | 93 | 94 | def get_low_battery_warning_level(self): 95 | """ 96 | Looks through all power supplies in POWER_SUPPLY_PATH. 97 | If there is an AC adapter online returns POWER_TYPE_AC returns LOW_BATTERY_WARNING_NONE. 98 | Otherwise determines total percentage and time remaining across all attached batteries. 99 | """ 100 | all_energy_full = [] 101 | all_energy_now = [] 102 | all_power_now = [] 103 | try: 104 | type = self.power_source_type() 105 | if type == common.POWER_TYPE_AC: 106 | if self.is_ac_online(): 107 | return common.LOW_BATTERY_WARNING_NONE 108 | elif type == common.POWER_TYPE_BATTERY: 109 | if self.is_battery_present() and self.is_battery_discharging(): 110 | energy_full, energy_now, power_now = self.get_battery_state() 111 | all_energy_full.append(energy_full) 112 | all_energy_now.append(energy_now) 113 | all_power_now.append(power_now) 114 | else: 115 | warnings.warn("UPS is not supported.") 116 | except (RuntimeError, IOError) as e: 117 | warnings.warn("Unable to read system power information!", category=RuntimeWarning) 118 | 119 | try: 120 | total_percentage = sum(all_energy_full) / sum(all_energy_now) 121 | total_time = sum([energy_now / power_now * 60.0 for energy_now, power_now in zip(all_energy_now, all_power_now)]) 122 | if total_time <= 10.0: 123 | return common.LOW_BATTERY_WARNING_FINAL 124 | elif total_percentage <= 22.0: 125 | return common.LOW_BATTERY_WARNING_EARLY 126 | else: 127 | return common.LOW_BATTERY_WARNING_NONE 128 | except ZeroDivisionError as e: 129 | warnings.warn("Unable to calculate low battery level: {0}".format(e), category=RuntimeWarning) 130 | return common.LOW_BATTERY_WARNING_NONE 131 | 132 | 133 | def get_time_remaining_estimate(self): 134 | """ 135 | Looks through all power sources and returns total time remaining estimate 136 | or TIME_REMAINING_UNLIMITED if ac power supply is online. 137 | """ 138 | all_energy_now = [] 139 | all_power_now = [] 140 | try: 141 | type = self.power_source_type() 142 | if type == common.POWER_TYPE_AC: 143 | if self.is_ac_online(supply_path): 144 | return common.TIME_REMAINING_UNLIMITED 145 | elif type == common.POWER_TYPE_BATTERY: 146 | if self.is_battery_present() and self.is_battery_discharging(): 147 | energy_full, energy_now, power_now = self.get_battery_state() 148 | all_energy_now.append(energy_now) 149 | all_power_now.append(power_now) 150 | else: 151 | warnings.warn("UPS is not supported.") 152 | except (RuntimeError, IOError) as e: 153 | warnings.warn("Unable to read system power information!", category=RuntimeWarning) 154 | 155 | if len(all_energy_now) > 0: 156 | try: 157 | return sum([energy_now / power_now * 60.0 for energy_now, power_now in zip(all_energy_now, all_power_now)]) 158 | except ZeroDivisionError as e: 159 | warnings.warn("Unable to calculate time remaining estimate: {0}".format(e), category=RuntimeWarning) 160 | return common.TIME_REMAINING_UNKNOWN 161 | else: 162 | return common.TIME_REMAINING_UNKNOWN 163 | 164 | 165 | def add_observer(self, observer): 166 | warnings.warn("Current system does not support observing.") 167 | pass 168 | 169 | 170 | def remove_observer(self, observer): 171 | warnings.warn("Current system does not support observing.") 172 | pass 173 | -------------------------------------------------------------------------------- /docs/linux/power_supply_class.txt: -------------------------------------------------------------------------------- 1 | Linux power supply class 2 | ======================== 3 | 4 | Synopsis 5 | ~~~~~~~~ 6 | Power supply class used to represent battery, UPS, AC or DC power supply 7 | properties to user-space. 8 | 9 | It defines core set of attributes, which should be applicable to (almost) 10 | every power supply out there. Attributes are available via sysfs and uevent 11 | interfaces. 12 | 13 | Each attribute has well defined meaning, up to unit of measure used. While 14 | the attributes provided are believed to be universally applicable to any 15 | power supply, specific monitoring hardware may not be able to provide them 16 | all, so any of them may be skipped. 17 | 18 | Power supply class is extensible, and allows to define drivers own attributes. 19 | The core attribute set is subject to the standard Linux evolution (i.e. 20 | if it will be found that some attribute is applicable to many power supply 21 | types or their drivers, it can be added to the core set). 22 | 23 | It also integrates with LED framework, for the purpose of providing 24 | typically expected feedback of battery charging/fully charged status and 25 | AC/USB power supply online status. (Note that specific details of the 26 | indication (including whether to use it at all) are fully controllable by 27 | user and/or specific machine defaults, per design principles of LED 28 | framework). 29 | 30 | 31 | Attributes/properties 32 | ~~~~~~~~~~~~~~~~~~~~~ 33 | Power supply class has predefined set of attributes, this eliminates code 34 | duplication across drivers. Power supply class insist on reusing its 35 | predefined attributes *and* their units. 36 | 37 | So, userspace gets predictable set of attributes and their units for any 38 | kind of power supply, and can process/present them to a user in consistent 39 | manner. Results for different power supplies and machines are also directly 40 | comparable. 41 | 42 | See drivers/power/ds2760_battery.c and drivers/power/pda_power.c for the 43 | example how to declare and handle attributes. 44 | 45 | 46 | Units 47 | ~~~~~ 48 | Quoting include/linux/power_supply.h: 49 | 50 | All voltages, currents, charges, energies, time and temperatures in µV, 51 | µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise 52 | stated. It's driver's job to convert its raw values to units in which 53 | this class operates. 54 | 55 | 56 | Attributes/properties detailed 57 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 58 | 59 | ~ ~ ~ ~ ~ ~ ~ Charge/Energy/Capacity - how to not confuse ~ ~ ~ ~ ~ ~ ~ 60 | ~ ~ 61 | ~ Because both "charge" (µAh) and "energy" (µWh) represents "capacity" ~ 62 | ~ of battery, this class distinguish these terms. Don't mix them! ~ 63 | ~ ~ 64 | ~ CHARGE_* attributes represents capacity in µAh only. ~ 65 | ~ ENERGY_* attributes represents capacity in µWh only. ~ 66 | ~ CAPACITY attribute represents capacity in *percents*, from 0 to 100. ~ 67 | ~ ~ 68 | ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 69 | 70 | Postfixes: 71 | _AVG - *hardware* averaged value, use it if your hardware is really able to 72 | report averaged values. 73 | _NOW - momentary/instantaneous values. 74 | 75 | STATUS - this attribute represents operating status (charging, full, 76 | discharging (i.e. powering a load), etc.). This corresponds to 77 | BATTERY_STATUS_* values, as defined in battery.h. 78 | 79 | CHARGE_TYPE - batteries can typically charge at different rates. 80 | This defines trickle and fast charges. For batteries that 81 | are already charged or discharging, 'n/a' can be displayed (or 82 | 'unknown', if the status is not known). 83 | 84 | HEALTH - represents health of the battery, values corresponds to 85 | POWER_SUPPLY_HEALTH_*, defined in battery.h. 86 | 87 | VOLTAGE_MAX_DESIGN, VOLTAGE_MIN_DESIGN - design values for maximal and 88 | minimal power supply voltages. Maximal/minimal means values of voltages 89 | when battery considered "full"/"empty" at normal conditions. Yes, there is 90 | no direct relation between voltage and battery capacity, but some dumb 91 | batteries use voltage for very approximated calculation of capacity. 92 | Battery driver also can use this attribute just to inform userspace 93 | about maximal and minimal voltage thresholds of a given battery. 94 | 95 | VOLTAGE_MAX, VOLTAGE_MIN - same as _DESIGN voltage values except that 96 | these ones should be used if hardware could only guess (measure and 97 | retain) the thresholds of a given power supply. 98 | 99 | CHARGE_FULL_DESIGN, CHARGE_EMPTY_DESIGN - design charge values, when 100 | battery considered full/empty. 101 | 102 | ENERGY_FULL_DESIGN, ENERGY_EMPTY_DESIGN - same as above but for energy. 103 | 104 | CHARGE_FULL, CHARGE_EMPTY - These attributes means "last remembered value 105 | of charge when battery became full/empty". It also could mean "value of 106 | charge when battery considered full/empty at given conditions (temperature, 107 | age)". I.e. these attributes represents real thresholds, not design values. 108 | 109 | CHARGE_COUNTER - the current charge counter (in µAh). This could easily 110 | be negative; there is no empty or full value. It is only useful for 111 | relative, time-based measurements. 112 | 113 | ENERGY_FULL, ENERGY_EMPTY - same as above but for energy. 114 | 115 | CAPACITY - capacity in percents. 116 | CAPACITY_LEVEL - capacity level. This corresponds to 117 | POWER_SUPPLY_CAPACITY_LEVEL_*. 118 | 119 | TEMP - temperature of the power supply. 120 | TEMP_AMBIENT - ambient temperature. 121 | 122 | TIME_TO_EMPTY - seconds left for battery to be considered empty (i.e. 123 | while battery powers a load) 124 | TIME_TO_FULL - seconds left for battery to be considered full (i.e. 125 | while battery is charging) 126 | 127 | 128 | Battery <-> external power supply interaction 129 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 130 | Often power supplies are acting as supplies and supplicants at the same 131 | time. Batteries are good example. So, batteries usually care if they're 132 | externally powered or not. 133 | 134 | For that case, power supply class implements notification mechanism for 135 | batteries. 136 | 137 | External power supply (AC) lists supplicants (batteries) names in 138 | "supplied_to" struct member, and each power_supply_changed() call 139 | issued by external power supply will notify supplicants via 140 | external_power_changed callback. 141 | 142 | 143 | QA 144 | ~~ 145 | Q: Where is POWER_SUPPLY_PROP_XYZ attribute? 146 | A: If you cannot find attribute suitable for your driver needs, feel free 147 | to add it and send patch along with your driver. 148 | 149 | The attributes available currently are the ones currently provided by the 150 | drivers written. 151 | 152 | Good candidates to add in future: model/part#, cycle_time, manufacturer, 153 | etc. 154 | 155 | 156 | Q: I have some very specific attribute (e.g. battery color), should I add 157 | this attribute to standard ones? 158 | A: Most likely, no. Such attribute can be placed in the driver itself, if 159 | it is useful. Of course, if the attribute in question applicable to 160 | large set of batteries, provided by many drivers, and/or comes from 161 | some general battery specification/standard, it may be a candidate to 162 | be added to the core attribute set. 163 | 164 | 165 | Q: Suppose, my battery monitoring chip/firmware does not provides capacity 166 | in percents, but provides charge_{now,full,empty}. Should I calculate 167 | percentage capacity manually, inside the driver, and register CAPACITY 168 | attribute? The same question about time_to_empty/time_to_full. 169 | A: Most likely, no. This class is designed to export properties which are 170 | directly measurable by the specific hardware available. 171 | 172 | Inferring not available properties using some heuristics or mathematical 173 | model is not subject of work for a battery driver. Such functionality 174 | should be factored out, and in fact, apm_power, the driver to serve 175 | legacy APM API on top of power supply class, uses a simple heuristic of 176 | approximating remaining battery capacity based on its charge, current, 177 | voltage and so on. But full-fledged battery model is likely not subject 178 | for kernel at all, as it would require floating point calculation to deal 179 | with things like differential equations and Kalman filters. This is 180 | better be handled by batteryd/libbattery, yet to be written. 181 | -------------------------------------------------------------------------------- /docs/linux/power_supply.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Universal power supply monitor class 3 | * 4 | * Copyright © 2007 Anton Vorontsov 5 | * Copyright © 2004 Szabolcs Gyurko 6 | * Copyright © 2003 Ian Molton 7 | * 8 | * Modified: 2004, Oct Szabolcs Gyurko 9 | * 10 | * You may use this code as per GPL version 2 11 | */ 12 | 13 | #ifndef __LINUX_POWER_SUPPLY_H__ 14 | #define __LINUX_POWER_SUPPLY_H__ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | /* 21 | * All voltages, currents, charges, energies, time and temperatures in uV, 22 | * µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise 23 | * stated. It's driver's job to convert its raw values to units in which 24 | * this class operates. 25 | */ 26 | 27 | /* 28 | * For systems where the charger determines the maximum battery capacity 29 | * the min and max fields should be used to present these values to user 30 | * space. Unused/unknown fields will not appear in sysfs. 31 | */ 32 | 33 | enum { 34 | POWER_SUPPLY_STATUS_UNKNOWN = 0, 35 | POWER_SUPPLY_STATUS_CHARGING, 36 | POWER_SUPPLY_STATUS_DISCHARGING, 37 | POWER_SUPPLY_STATUS_NOT_CHARGING, 38 | POWER_SUPPLY_STATUS_FULL, 39 | }; 40 | 41 | enum { 42 | POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0, 43 | POWER_SUPPLY_CHARGE_TYPE_NONE, 44 | POWER_SUPPLY_CHARGE_TYPE_TRICKLE, 45 | POWER_SUPPLY_CHARGE_TYPE_FAST, 46 | }; 47 | 48 | enum { 49 | POWER_SUPPLY_HEALTH_UNKNOWN = 0, 50 | POWER_SUPPLY_HEALTH_GOOD, 51 | POWER_SUPPLY_HEALTH_OVERHEAT, 52 | POWER_SUPPLY_HEALTH_DEAD, 53 | POWER_SUPPLY_HEALTH_OVERVOLTAGE, 54 | POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, 55 | POWER_SUPPLY_HEALTH_COLD, 56 | }; 57 | 58 | enum { 59 | POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0, 60 | POWER_SUPPLY_TECHNOLOGY_NiMH, 61 | POWER_SUPPLY_TECHNOLOGY_LION, 62 | POWER_SUPPLY_TECHNOLOGY_LIPO, 63 | POWER_SUPPLY_TECHNOLOGY_LiFe, 64 | POWER_SUPPLY_TECHNOLOGY_NiCd, 65 | POWER_SUPPLY_TECHNOLOGY_LiMn, 66 | }; 67 | 68 | enum { 69 | POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0, 70 | POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL, 71 | POWER_SUPPLY_CAPACITY_LEVEL_LOW, 72 | POWER_SUPPLY_CAPACITY_LEVEL_NORMAL, 73 | POWER_SUPPLY_CAPACITY_LEVEL_HIGH, 74 | POWER_SUPPLY_CAPACITY_LEVEL_FULL, 75 | }; 76 | 77 | enum power_supply_property { 78 | /* Properties of type `int' */ 79 | POWER_SUPPLY_PROP_STATUS = 0, 80 | POWER_SUPPLY_PROP_CHARGE_TYPE, 81 | POWER_SUPPLY_PROP_HEALTH, 82 | POWER_SUPPLY_PROP_PRESENT, 83 | POWER_SUPPLY_PROP_ONLINE, 84 | POWER_SUPPLY_PROP_TECHNOLOGY, 85 | POWER_SUPPLY_PROP_CYCLE_COUNT, 86 | POWER_SUPPLY_PROP_VOLTAGE_MAX, 87 | POWER_SUPPLY_PROP_VOLTAGE_MIN, 88 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 89 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 90 | POWER_SUPPLY_PROP_VOLTAGE_NOW, 91 | POWER_SUPPLY_PROP_VOLTAGE_AVG, 92 | POWER_SUPPLY_PROP_CURRENT_MAX, 93 | POWER_SUPPLY_PROP_CURRENT_NOW, 94 | POWER_SUPPLY_PROP_CURRENT_AVG, 95 | POWER_SUPPLY_PROP_POWER_NOW, 96 | POWER_SUPPLY_PROP_POWER_AVG, 97 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 98 | POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN, 99 | POWER_SUPPLY_PROP_CHARGE_FULL, 100 | POWER_SUPPLY_PROP_CHARGE_EMPTY, 101 | POWER_SUPPLY_PROP_CHARGE_NOW, 102 | POWER_SUPPLY_PROP_CHARGE_AVG, 103 | POWER_SUPPLY_PROP_CHARGE_COUNTER, 104 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 105 | POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN, 106 | POWER_SUPPLY_PROP_ENERGY_FULL, 107 | POWER_SUPPLY_PROP_ENERGY_EMPTY, 108 | POWER_SUPPLY_PROP_ENERGY_NOW, 109 | POWER_SUPPLY_PROP_ENERGY_AVG, 110 | POWER_SUPPLY_PROP_CAPACITY, /* in percents! */ 111 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, 112 | POWER_SUPPLY_PROP_TEMP, 113 | POWER_SUPPLY_PROP_TEMP_AMBIENT, 114 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 115 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 116 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 117 | POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 118 | POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */ 119 | /* Properties of type `const char *' */ 120 | POWER_SUPPLY_PROP_MODEL_NAME, 121 | POWER_SUPPLY_PROP_MANUFACTURER, 122 | POWER_SUPPLY_PROP_SERIAL_NUMBER, 123 | }; 124 | 125 | enum power_supply_type { 126 | POWER_SUPPLY_TYPE_BATTERY = 0, 127 | POWER_SUPPLY_TYPE_UPS, 128 | POWER_SUPPLY_TYPE_MAINS, 129 | POWER_SUPPLY_TYPE_USB, /* Standard Downstream Port */ 130 | POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */ 131 | POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */ 132 | POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters */ 133 | }; 134 | 135 | union power_supply_propval { 136 | int intval; 137 | const char *strval; 138 | }; 139 | 140 | struct power_supply { 141 | const char *name; 142 | enum power_supply_type type; 143 | enum power_supply_property *properties; 144 | size_t num_properties; 145 | 146 | char **supplied_to; 147 | size_t num_supplicants; 148 | 149 | int (*get_property)(struct power_supply *psy, 150 | enum power_supply_property psp, 151 | union power_supply_propval *val); 152 | int (*set_property)(struct power_supply *psy, 153 | enum power_supply_property psp, 154 | const union power_supply_propval *val); 155 | int (*property_is_writeable)(struct power_supply *psy, 156 | enum power_supply_property psp); 157 | void (*external_power_changed)(struct power_supply *psy); 158 | void (*set_charged)(struct power_supply *psy); 159 | 160 | /* For APM emulation, think legacy userspace. */ 161 | int use_for_apm; 162 | 163 | /* private */ 164 | struct device *dev; 165 | struct work_struct changed_work; 166 | 167 | #ifdef CONFIG_LEDS_TRIGGERS 168 | struct led_trigger *charging_full_trig; 169 | char *charging_full_trig_name; 170 | struct led_trigger *charging_trig; 171 | char *charging_trig_name; 172 | struct led_trigger *full_trig; 173 | char *full_trig_name; 174 | struct led_trigger *online_trig; 175 | char *online_trig_name; 176 | struct led_trigger *charging_blink_full_solid_trig; 177 | char *charging_blink_full_solid_trig_name; 178 | #endif 179 | }; 180 | 181 | /* 182 | * This is recommended structure to specify static power supply parameters. 183 | * Generic one, parametrizable for different power supplies. Power supply 184 | * class itself does not use it, but that's what implementing most platform 185 | * drivers, should try reuse for consistency. 186 | */ 187 | 188 | struct power_supply_info { 189 | const char *name; 190 | int technology; 191 | int voltage_max_design; 192 | int voltage_min_design; 193 | int charge_full_design; 194 | int charge_empty_design; 195 | int energy_full_design; 196 | int energy_empty_design; 197 | int use_for_apm; 198 | }; 199 | 200 | extern struct power_supply *power_supply_get_by_name(char *name); 201 | extern void power_supply_changed(struct power_supply *psy); 202 | extern int power_supply_am_i_supplied(struct power_supply *psy); 203 | extern int power_supply_set_battery_charged(struct power_supply *psy); 204 | 205 | #if defined(CONFIG_POWER_SUPPLY) || defined(CONFIG_POWER_SUPPLY_MODULE) 206 | extern int power_supply_is_system_supplied(void); 207 | #else 208 | static inline int power_supply_is_system_supplied(void) { return -ENOSYS; } 209 | #endif 210 | 211 | extern int power_supply_register(struct device *parent, 212 | struct power_supply *psy); 213 | extern void power_supply_unregister(struct power_supply *psy); 214 | extern int power_supply_powers(struct power_supply *psy, struct device *dev); 215 | 216 | /* For APM emulation, think legacy userspace. */ 217 | extern struct class *power_supply_class; 218 | 219 | static inline bool power_supply_is_amp_property(enum power_supply_property psp) 220 | { 221 | switch (psp) { 222 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 223 | case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN: 224 | case POWER_SUPPLY_PROP_CHARGE_FULL: 225 | case POWER_SUPPLY_PROP_CHARGE_EMPTY: 226 | case POWER_SUPPLY_PROP_CHARGE_NOW: 227 | case POWER_SUPPLY_PROP_CHARGE_AVG: 228 | case POWER_SUPPLY_PROP_CHARGE_COUNTER: 229 | case POWER_SUPPLY_PROP_CURRENT_MAX: 230 | case POWER_SUPPLY_PROP_CURRENT_NOW: 231 | case POWER_SUPPLY_PROP_CURRENT_AVG: 232 | return 1; 233 | default: 234 | break; 235 | } 236 | 237 | return 0; 238 | } 239 | 240 | static inline bool power_supply_is_watt_property(enum power_supply_property psp) 241 | { 242 | switch (psp) { 243 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 244 | case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN: 245 | case POWER_SUPPLY_PROP_ENERGY_FULL: 246 | case POWER_SUPPLY_PROP_ENERGY_EMPTY: 247 | case POWER_SUPPLY_PROP_ENERGY_NOW: 248 | case POWER_SUPPLY_PROP_ENERGY_AVG: 249 | case POWER_SUPPLY_PROP_VOLTAGE_MAX: 250 | case POWER_SUPPLY_PROP_VOLTAGE_MIN: 251 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 252 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 253 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: 254 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: 255 | case POWER_SUPPLY_PROP_POWER_NOW: 256 | return 1; 257 | default: 258 | break; 259 | } 260 | 261 | return 0; 262 | } 263 | 264 | #endif /* __LINUX_POWER_SUPPLY_H__ */ 265 | -------------------------------------------------------------------------------- /power/linux.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | """ 3 | Implements PowerManagement functions using /sys/class/power_supply/* 4 | See doc/linux for platform-specific details. 5 | """ 6 | import os 7 | import warnings 8 | from power import common 9 | 10 | 11 | POWER_SUPPLY_PATH = '/sys/class/power_supply' 12 | 13 | 14 | if not os.access(POWER_SUPPLY_PATH, os.R_OK): 15 | raise RuntimeError("Unable to read {path}.".format(path=POWER_SUPPLY_PATH)) 16 | 17 | 18 | class PowerManagement(common.PowerManagementBase): 19 | @staticmethod 20 | def power_source_type(supply_path): 21 | """ 22 | @param supply_path: Path to power supply 23 | @return: One of common.POWER_TYPE_* 24 | @raise: Runtime error if type of power source is not supported 25 | """ 26 | with open(os.path.join(supply_path, 'type'), 'r') as type_file: 27 | type = type_file.readline().strip() 28 | if type == 'Mains': 29 | return common.POWER_TYPE_AC 30 | elif type == 'UPS': 31 | return common.POWER_TYPE_UPS 32 | elif type == 'Battery': 33 | return common.POWER_TYPE_BATTERY 34 | else: 35 | raise RuntimeError("Type of {path} ({type}) is not supported".format(path=supply_path, type=type)) 36 | 37 | @staticmethod 38 | def is_ac_online(supply_path): 39 | """ 40 | @param supply_path: Path to power supply 41 | @return: True if ac is online. Otherwise False 42 | """ 43 | with open(os.path.join(supply_path, 'online'), 'r') as online_file: 44 | return online_file.readline().strip() == '1' 45 | 46 | @staticmethod 47 | def is_battery_present(supply_path): 48 | """ 49 | @param supply_path: Path to power supply 50 | @return: True if battery is present. Otherwise False 51 | """ 52 | with open(os.path.join(supply_path, 'present'), 'r') as present_file: 53 | return present_file.readline().strip() == '1' 54 | 55 | @staticmethod 56 | def is_battery_discharging(supply_path): 57 | """ 58 | @param supply_path: Path to power supply 59 | @return: True if ac is online. Otherwise False 60 | """ 61 | with open(os.path.join(supply_path, 'status'), 'r') as status_file: 62 | return status_file.readline().strip() == 'Discharging' 63 | 64 | @staticmethod 65 | def get_battery_state(supply_path): 66 | """ 67 | @param supply_path: Path to power supply 68 | @return: Tuple (energy_full, energy_now, power_now) 69 | """ 70 | try: 71 | energy_now_file = open(os.path.join(supply_path, 'energy_now'), 'r') 72 | except IOError: 73 | energy_now_file = open(os.path.join(supply_path, 'charge_now'), 'r') 74 | 75 | try: 76 | energy_full_file = open(os.path.join(supply_path, 'energy_full'), 'r') 77 | except IOError: 78 | energy_full_file = open(os.path.join(supply_path, 'charge_full'), 'r') 79 | 80 | try: 81 | with open(os.path.join(supply_path, 'power_now'), 'r') as power_now_file: 82 | power_now = float(power_now_file.readline().strip()) 83 | except IOError: 84 | with open(os.path.join(supply_path, 'voltage_now'), 'r') as voltage_now_file: 85 | with open(os.path.join(supply_path, 'current_now'), 'r') as current_now_file: 86 | power_now = float(current_now_file.readline().strip()) * float(voltage_now_file.readline().strip()) / 10000000 87 | 88 | with energy_now_file: 89 | with energy_full_file: 90 | energy_now = float(energy_now_file.readline().strip()) 91 | energy_full = float(energy_full_file.readline().strip()) 92 | return energy_full, energy_now, power_now 93 | 94 | def get_providing_power_source_type(self): 95 | """ 96 | Looks through all power supplies in POWER_SUPPLY_PATH. 97 | If there is an AC adapter online returns POWER_TYPE_AC. 98 | If there is a discharging battery, returns POWER_TYPE_BATTERY. 99 | Since the order of supplies is arbitrary, whatever found first is returned. 100 | """ 101 | for supply in os.listdir(POWER_SUPPLY_PATH): 102 | supply_path = os.path.join(POWER_SUPPLY_PATH, supply) 103 | try: 104 | type = self.power_source_type(supply_path) 105 | if type == common.POWER_TYPE_AC: 106 | if self.is_ac_online(supply_path): 107 | return common.POWER_TYPE_AC 108 | elif type == common.POWER_TYPE_BATTERY: 109 | if self.is_battery_present(supply_path) and self.is_battery_discharging(supply_path): 110 | return common.POWER_TYPE_BATTERY 111 | else: 112 | warnings.warn("UPS is not supported.") 113 | except (RuntimeError, IOError) as e: 114 | warnings.warn("Unable to read properties of {0}: {1}".format(supply_path, e), category=RuntimeWarning) 115 | 116 | return common.POWER_TYPE_AC 117 | 118 | def get_low_battery_warning_level(self): 119 | """ 120 | Looks through all power supplies in POWER_SUPPLY_PATH. 121 | If there is an AC adapter online returns POWER_TYPE_AC returns LOW_BATTERY_WARNING_NONE. 122 | Otherwise determines total percentage and time remaining across all attached batteries. 123 | """ 124 | all_energy_full = [] 125 | all_energy_now = [] 126 | all_power_now = [] 127 | for supply in os.listdir(POWER_SUPPLY_PATH): 128 | supply_path = os.path.join(POWER_SUPPLY_PATH, supply) 129 | try: 130 | type = self.power_source_type(supply_path) 131 | if type == common.POWER_TYPE_AC: 132 | if self.is_ac_online(supply_path): 133 | return common.LOW_BATTERY_WARNING_NONE 134 | elif type == common.POWER_TYPE_BATTERY: 135 | if self.is_battery_present(supply_path) and self.is_battery_discharging(supply_path): 136 | energy_full, energy_now, power_now = self.get_battery_state(supply_path) 137 | all_energy_full.append(energy_full) 138 | all_energy_now.append(energy_now) 139 | all_power_now.append(power_now) 140 | else: 141 | warnings.warn("UPS is not supported.") 142 | except (RuntimeError, IOError) as e: 143 | warnings.warn("Unable to read properties of {0}: {1}".format(supply_path, e), category=RuntimeWarning) 144 | 145 | try: 146 | total_percentage = sum(all_energy_full) / sum(all_energy_now) 147 | total_time = sum([energy_now / power_now * 60.0 for energy_now, power_now in zip(all_energy_now, all_power_now)]) 148 | if total_time <= 10.0: 149 | return common.LOW_BATTERY_WARNING_FINAL 150 | elif total_percentage <= 22.0: 151 | return common.LOW_BATTERY_WARNING_EARLY 152 | else: 153 | return common.LOW_BATTERY_WARNING_NONE 154 | except ZeroDivisionError as e: 155 | warnings.warn("Unable to calculate low battery level: {0}".format(e), category=RuntimeWarning) 156 | return common.LOW_BATTERY_WARNING_NONE 157 | 158 | def get_time_remaining_estimate(self): 159 | """ 160 | Looks through all power sources and returns total time remaining estimate 161 | or TIME_REMAINING_UNLIMITED if ac power supply is online. 162 | """ 163 | all_energy_now = [] 164 | all_energy_not_discharging = [] 165 | all_power_now = [] 166 | for supply in os.listdir(POWER_SUPPLY_PATH): 167 | supply_path = os.path.join(POWER_SUPPLY_PATH, supply) 168 | try: 169 | type = self.power_source_type(supply_path) 170 | if type == common.POWER_TYPE_AC: 171 | if self.is_ac_online(supply_path): 172 | return common.TIME_REMAINING_UNLIMITED 173 | elif type == common.POWER_TYPE_BATTERY: 174 | if self.is_battery_present(supply_path) and self.is_battery_discharging(supply_path): 175 | energy_full, energy_now, power_now = self.get_battery_state(supply_path) 176 | all_energy_now.append(energy_now) 177 | all_power_now.append(power_now) 178 | elif self.is_battery_present(supply_path) and not self.is_battery_discharging(supply_path): 179 | energy_now = self.get_battery_state(supply_path)[1] 180 | all_energy_not_discharging.append(energy_now) 181 | else: 182 | warnings.warn("UPS is not supported.") 183 | except (RuntimeError, IOError) as e: 184 | warnings.warn("Unable to read properties of {0}: {1}".format(supply_path, e), category=RuntimeWarning) 185 | 186 | if len(all_energy_now) > 0: 187 | try: 188 | return sum([energy_now / power_now * 60.0 for energy_now, power_now in zip(all_energy_now, all_power_now)])\ 189 | + sum(all_energy_not_discharging) / (sum(all_power_now) / len(all_power_now)) * 60.0 190 | except ZeroDivisionError as e: 191 | warnings.warn("Unable to calculate time remaining estimate: {0}".format(e), category=RuntimeWarning) 192 | return common.TIME_REMAINING_UNKNOWN 193 | else: 194 | return common.TIME_REMAINING_UNKNOWN 195 | 196 | def add_observer(self, observer): 197 | warnings.warn("Current system does not support observing.") 198 | pass 199 | 200 | def remove_observer(self, observer): 201 | warnings.warn("Current system does not support observing.") 202 | pass 203 | -------------------------------------------------------------------------------- /docs/darwin/IOPowerSources_h/toc.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | Documentation for IOPowerSources.h 6 | 7 | 8 | 9 | 10 | 11 | 14 |
 
15 | 16 | 17 | 71 |
Header:
  IOPowerSources.h
18 | 19 |
20 | 21 | 22 |
Introduction
23 |
24 | 47 |
48 | 51 |
49 | Defined Types 50 |
52 |
56 | 68 | 69 | 70 |

 

72 |

73 | 74 | -------------------------------------------------------------------------------- /power/darwin.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | """ 3 | Implements PowerManagement functions using IOPowerSources. 4 | Requires Mac OS X 10.6+ 5 | See doc/darwin for platform-specific details. 6 | """ 7 | import weakref 8 | import warnings 9 | import objc 10 | from Foundation import * 11 | from power import common 12 | 13 | 14 | # Generated in Mac OS X 10.8.2 using the following command: 15 | # gen_bridge_metadata -c '-l/System/Library/Frameworks/IOKit.framework/IOKit -I/System/Library/Frameworks/IOKit.framework/Headers/ps/' /System/Library/Frameworks/IOKit.framework/Headers/ps/IOPowerSources.h /System/Library/Frameworks/IOKit.framework/Headers/ps/IOPSKeys.h 16 | # 17 | # Following keas are added manually, because public headers misses their definitions: 18 | # http://opensource.apple.com/source/IOKitUser/IOKitUser-514.16.50/pwr_mgt.subproj/IOPMLibPrivate.h 19 | # - kIOPMUPSPowerKey 20 | # - kIOPMBatteryPowerKey 21 | # - kIOPMACPowerKey 22 | # - kIOPSProvidesTimeRemaining 23 | IO_POWER_SOURCES_BRIDGESUPPORT = """ 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | """ 138 | 139 | objc.parseBridgeSupport( 140 | IO_POWER_SOURCES_BRIDGESUPPORT, 141 | globals(), 142 | objc.pathForFramework("/System/Library/Frameworks/IOKit.framework") 143 | ) 144 | 145 | 146 | POWER_TYPE_MAP = { 147 | kIOPMACPowerKey: common.POWER_TYPE_AC, 148 | kIOPMBatteryPowerKey: common.POWER_TYPE_BATTERY, 149 | kIOPMUPSPowerKey: common.POWER_TYPE_UPS 150 | } 151 | 152 | 153 | WARNING_LEVEL_MAP = { 154 | kIOPSLowBatteryWarningNone: common.LOW_BATTERY_WARNING_NONE, 155 | kIOPSLowBatteryWarningEarly: common.LOW_BATTERY_WARNING_EARLY, 156 | kIOPSLowBatteryWarningFinal: common.LOW_BATTERY_WARNING_FINAL 157 | } 158 | 159 | 160 | class PowerSourcesNotificationsObserver(NSObject): 161 | """ 162 | Manages NSThread instance which is used to run NSRunLoop with only source - IOPSNotificationCreateRunLoopSource. 163 | Thread is automatically spawned when first observer is added and stopped when last observer is removed. 164 | Does not keep strong references to observers. 165 | 166 | @note: Method names break PEP8 convention to conform PyObjC naming conventions 167 | """ 168 | def init(self): 169 | self = objc.super(PowerSourcesNotificationsObserver, self).init() 170 | if self is not None: 171 | self._weak_observers = [] 172 | self._thread = None 173 | self._lock = objc.object_lock(self) 174 | return self 175 | 176 | def startThread(self): 177 | """Spawns new NSThread to handle notifications.""" 178 | if self._thread is not None: 179 | return 180 | self._thread = NSThread.alloc().initWithTarget_selector_object_(self, 'runPowerNotificationsThread', None) 181 | self._thread.start() 182 | 183 | def stopThread(self): 184 | """Stops spawned NSThread.""" 185 | if self._thread is not None: 186 | self.performSelector_onThread_withObject_waitUntilDone_('stopPowerNotificationsThread', self._thread, None, objc.YES) 187 | self._thread = None 188 | 189 | def runPowerNotificationsThread(self): 190 | """Main method of the spawned NSThread. Registers run loop source and runs current NSRunLoop.""" 191 | pool = NSAutoreleasePool.alloc().init() 192 | 193 | @objc.callbackFor(IOPSNotificationCreateRunLoopSource) 194 | def on_power_source_notification(context): 195 | with self._lock: 196 | for weak_observer in self._weak_observers: 197 | observer = weak_observer() 198 | if observer: 199 | observer.on_power_source_notification() 200 | 201 | self._source = IOPSNotificationCreateRunLoopSource(on_power_source_notification, None) 202 | CFRunLoopAddSource(NSRunLoop.currentRunLoop().getCFRunLoop(), self._source, kCFRunLoopDefaultMode) 203 | while not NSThread.currentThread().isCancelled(): 204 | NSRunLoop.currentRunLoop().runMode_beforeDate_(NSDefaultRunLoopMode, NSDate.distantFuture()) 205 | del pool 206 | 207 | def stopPowerNotificationsThread(self): 208 | """Removes the only source from NSRunLoop and cancels thread.""" 209 | assert NSThread.currentThread() == self._thread 210 | 211 | CFRunLoopSourceInvalidate(self._source) 212 | self._source = None 213 | NSThread.currentThread().cancel() 214 | 215 | def addObserver_(self, observer): 216 | """ 217 | Adds weak ref to an observer. 218 | 219 | @param observer: Instance of class that implements on_power_source_notification() 220 | """ 221 | with self._lock: 222 | self._weak_observers.append(weakref.ref(observer)) 223 | if len(self._weak_observers) == 1: 224 | self.startThread() 225 | 226 | def removeObserver_(self, observer): 227 | """ 228 | Removes an observer. 229 | 230 | @param observer: Previously added observer 231 | """ 232 | with self._lock: 233 | self._weak_observers.remove(weakref.ref(observer)) 234 | if len(self._weak_observers) == 0: 235 | self.stopThread() 236 | 237 | 238 | class PowerManagement(common.PowerManagementBase): 239 | notifications_observer = PowerSourcesNotificationsObserver.alloc().init() 240 | 241 | def __init__(self, cf_run_loop=None): 242 | """ 243 | @param cf_run_loop: If provided, all notifications are posted within this loop 244 | """ 245 | super(PowerManagement, self).__init__() 246 | self._cf_run_loop = cf_run_loop 247 | 248 | def on_power_source_notification(self): 249 | """ 250 | Called in response to IOPSNotificationCreateRunLoopSource() event. 251 | """ 252 | for weak_observer in self._weak_observers: 253 | observer = weak_observer() 254 | if observer: 255 | observer.on_power_sources_change(self) 256 | observer.on_time_remaining_change(self) 257 | 258 | def get_providing_power_source_type(self): 259 | """ 260 | Uses IOPSCopyPowerSourcesInfo and IOPSGetProvidingPowerSourceType to get providing power source type. 261 | """ 262 | blob = IOPSCopyPowerSourcesInfo() 263 | type = IOPSGetProvidingPowerSourceType(blob) 264 | return POWER_TYPE_MAP[type] 265 | 266 | def get_low_battery_warning_level(self): 267 | """ 268 | Uses IOPSGetBatteryWarningLevel to get battery warning level. 269 | """ 270 | warning_level = IOPSGetBatteryWarningLevel() 271 | return WARNING_LEVEL_MAP[warning_level] 272 | 273 | def get_time_remaining_estimate(self): 274 | """ 275 | In Mac OS X 10.7+ 276 | Uses IOPSGetTimeRemainingEstimate to get time remaining estimate. 277 | 278 | In Mac OS X 10.6 279 | IOPSGetTimeRemainingEstimate is not available. 280 | If providing power source type is AC, returns TIME_REMAINING_UNLIMITED. 281 | Otherwise looks through all power sources returned by IOPSGetProvidingPowerSourceType 282 | and returns total estimate. 283 | """ 284 | if IOPSGetTimeRemainingEstimate is not None: # Mac OS X 10.7+ 285 | estimate = float(IOPSGetTimeRemainingEstimate()) 286 | if estimate == -1.0: 287 | return common.TIME_REMAINING_UNKNOWN 288 | elif estimate == -2.0: 289 | return common.TIME_REMAINING_UNLIMITED 290 | else: 291 | return estimate / 60.0 292 | else: # Mac OS X 10.6 293 | warnings.warn("IOPSGetTimeRemainingEstimate is not preset", RuntimeWarning) 294 | blob = IOPSCopyPowerSourcesInfo() 295 | type = IOPSGetProvidingPowerSourceType(blob) 296 | if type == common.POWER_TYPE_AC: 297 | return common.TIME_REMAINING_UNLIMITED 298 | else: 299 | estimate = 0.0 300 | for source in IOPSCopyPowerSourcesList(blob): 301 | description = IOPSGetPowerSourceDescription(blob, source) 302 | if kIOPSIsPresentKey in description and description[kIOPSIsPresentKey] and kIOPSTimeToEmptyKey in description and description[kIOPSTimeToEmptyKey] > 0.0: 303 | estimate += float(description[kIOPSTimeToEmptyKey]) 304 | if estimate > 0.0: 305 | return float(estimate) 306 | else: 307 | return common.TIME_REMAINING_UNKNOWN 308 | 309 | def add_observer(self, observer): 310 | """ 311 | Spawns thread or adds IOPSNotificationCreateRunLoopSource directly to provided cf_run_loop 312 | @see: __init__ 313 | """ 314 | super(PowerManagement, self).add_observer(observer) 315 | if len(self._weak_observers) == 1: 316 | if not self._cf_run_loop: 317 | PowerManagement.notifications_observer.addObserver_(self) 318 | else: 319 | @objc.callbackFor(IOPSNotificationCreateRunLoopSource) 320 | def on_power_sources_change(context): 321 | self.on_power_source_notification() 322 | 323 | self._source = IOPSNotificationCreateRunLoopSource(on_power_sources_change, None) 324 | CFRunLoopAddSource(self._cf_run_loop, self._source, kCFRunLoopDefaultMode) 325 | 326 | def remove_observer(self, observer): 327 | """ 328 | Stops thread and invalidates source. 329 | """ 330 | super(PowerManagement, self).remove_observer(observer) 331 | if len(self._weak_observers) == 0: 332 | if not self._cf_run_loop: 333 | PowerManagement.notifications_observer.removeObserver_(self) 334 | else: 335 | CFRunLoopSourceInvalidate(self._source) 336 | self._source = None 337 | -------------------------------------------------------------------------------- /docs/darwin/IOPSKeys_h/toc.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | Documentation for IOPSKeys.h 6 | 7 | 8 | 9 | 10 | 11 | 14 |
 
15 | 16 | 17 | 116 |
Header:
  IOPSKeys.h
18 | 19 |
20 | 21 | 22 |
Introduction
23 |
24 |
25 | 28 |
26 | #defines 27 |
29 |
30 | Battery Failure Mode values 31 | 32 |  
47 | Battery Health values 48 | 49 |  
55 | Internal Keys 56 | 57 |  
62 | IOPSPowerAdapter Keys 63 | 64 |  
72 | Power Source Commands (UPS) 73 | 74 |  
78 | Power Source data keys 79 | 80 |
113 | 114 | 115 |

 

117 |

118 | 119 | -------------------------------------------------------------------------------- /docs/win32/SYSTEM_BATTERY_STATE.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | SYSTEM_BATTERY_STATE structure (Windows) 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 |
42 |
43 |
44 | 45 | Sign in 46 |
47 |
48 |
49 | 59 | 67 |
68 | 69 | 70 | Home 71 | 72 | 73 | 74 | Dashboard 75 | 76 | 77 | 78 | Docs 79 | 80 | 81 | 82 | Samples 83 | 84 | 85 | 86 | Downloads 87 | 88 | 89 | 90 | Support 91 | 92 | 93 | 94 | Community 95 | 96 | 97 |
98 |
99 |
100 | 101 | 102 | 103 | 104 | 115 |

SYSTEM_BATTERY_STATE structure (Windows)

116 | 117 | 118 |
119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 |
132 | 133 | 134 | 135 | 136 |
137 | 140 | 143 |
144 | System Services 145 |
146 |
147 | Power Management 148 |
149 | 152 | 155 | 158 | 161 | 164 | 167 | 170 | 173 | 176 | 179 | 182 | 185 | 188 | 191 | 194 | 197 | 200 | 203 | 206 | 209 | 212 | 215 | 218 | 221 | 224 | 227 | 230 | 233 | 236 | 239 | 242 | 245 |
246 | 247 | 248 | 249 | 250 | 251 | 252 | Expand 253 | Minimize 254 | 255 | 256 | 257 | 258 |
259 |
260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 |
274 | 275 | 1 out of 1 rated this helpful - Rate this topic 276 | 277 | 278 |
279 | 280 | 281 | 282 |
283 | 284 |
285 |
286 | 287 |
288 |

Contains information about the current state of the system battery.

289 |

Syntax

290 | 291 |
292 |
293 | 294 |
295 |
296 |
297 |
298 | Copy 299 |
300 |
301 |
302 |
303 | 
304 | typedef struct {
305 |   BOOLEAN AcOnLine;
306 |   BOOLEAN BatteryPresent;
307 |   BOOLEAN Charging;
308 |   BOOLEAN Discharging;
309 |   BOOLEAN Spare1[4];
310 |   DWORD   MaxCapacity;
311 |   DWORD   RemainingCapacity;
312 |   DWORD   Rate;
313 |   DWORD   EstimatedTime;
314 |   DWORD   DefaultAlert1;
315 |   DWORD   DefaultAlert2;
316 | } SYSTEM_BATTERY_STATE, *PSYSTEM_BATTERY_STATE;
317 | 
318 | 
319 | 320 |
321 |
322 |
323 | 324 |

Members

325 |
326 |
AcOnLine
327 |
328 |

If this member is TRUE, the system battery charger is currently operating on external 329 | power.

330 |
331 |
BatteryPresent
332 |
333 |

If this member is TRUE, at least one battery is present in the system.

334 |
335 |
Charging
336 |
337 |

If this member is TRUE, a battery is currently charging.

338 |
339 |
Discharging
340 |
341 |

If this member is TRUE, a battery is currently discharging.

342 |
343 |
Spare1
344 |
345 |

Reserved.

346 |
347 |
MaxCapacity
348 |
349 |

The theoretical capacity of the battery when new, in mWh.

350 |
351 |
RemainingCapacity
352 |
353 |

The estimated remaining capacity of the battery, in mWh.

354 |
355 |
Rate
356 |
357 |

The current rate of discharge of the battery, in mW. A nonzero, positive rate indicates charging; a 358 | negative rate indicates discharging. Some batteries report only discharging rates. This value should be treated 359 | as a LONG as it can contain negative values (with the high bit set).

360 |
361 |
EstimatedTime
362 |
363 |

The estimated time remaining on the battery, in seconds.

364 |
365 |
DefaultAlert1
366 |
367 |

The manufacturer's suggestion of a capacity, in mWh, at which a low battery alert should occur. Definitions 368 | of low vary from manufacturer to manufacturer. In general, a warning state will occur before a low state, but 369 | you should not assume that it always will. To reduce risk of data loss, this value is usually used as the 370 | default setting for the critical battery alarm.

371 |
372 |
DefaultAlert2
373 |
374 |

The manufacturer's suggestion of a capacity, in mWh, at which a warning battery alert should occur. 375 | Definitions of warning vary from manufacturer to manufacturer. In general, a warning state will occur before a 376 | low state, but you should not assume that it always will. To reduce risk of data loss, this value is usually 377 | used as the default setting for the low battery alarm.

378 |
379 |
380 |

Requirements

381 | 382 | 385 | 388 | 395 |
383 |

Minimum supported client

384 |
Windows XP [desktop apps only]
386 |

Minimum supported server

387 |
Windows Server 2003 [desktop apps only]
389 |

Header

390 |
391 |
392 |
WinNT.h (include Windows.h)
393 |
394 |
396 |

See also

397 |
398 |
399 | CallNtPowerInformation 400 |
401 |
402 |

 

403 |

 

404 |

Send comments about this topic to Microsoft

405 |

Build date: 10/16/2012

406 |
407 |
408 | 409 | 410 | 411 | 412 | 413 |
414 |
415 | 416 |
417 |
418 | 419 | Did you find this helpful? 420 | 421 | 422 | 423 |
424 | 425 |
426 | 427 |
428 | 429 |
430 | 431 | 432 |
433 | 434 |
435 | 436 | 437 |
438 | 439 |
440 | 441 | 442 |
443 | 444 |
445 | 446 |
447 | 448 |
449 | (1500 characters remaining) 450 | 451 | 452 |
453 |
454 | 455 |
456 |
457 |
458 | 459 | 460 | 461 | 462 | 463 | 464 | 502 | 503 |
504 |
505 |
506 |

Community Additions

507 | 508 | 509 | ADD 510 | 511 | 512 |
513 |
514 |
515 | 516 |
517 |
518 | 519 | 520 | 521 |
522 | 523 | 524 |
525 |
526 | 527 | 528 | 529 | 607 | 608 |
609 |
© 2012 Microsoft. All rights reserved.
610 |
611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 641 | 642 | 643 |
644 | 660 | -------------------------------------------------------------------------------- /docs/win32/SYSTEM_POWER_STATUS.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | SYSTEM_POWER_STATUS structure (Windows) 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 |
42 |
43 |
44 | 45 | Sign in 46 |
47 |
48 |
49 | 59 | 67 |
68 | 69 | 70 | Home 71 | 72 | 73 | 74 | Dashboard 75 | 76 | 77 | 78 | Docs 79 | 80 | 81 | 82 | Samples 83 | 84 | 85 | 86 | Downloads 87 | 88 | 89 | 90 | Support 91 | 92 | 93 | 94 | Community 95 | 96 | 97 |
98 |
99 |
100 | 101 | 102 | 103 | 104 | 115 |

SYSTEM_POWER_STATUS structure (Windows)

116 | 117 | 118 |
119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 |
132 | 133 | 134 | 135 | 136 |
137 | 140 | 143 |
144 | System Services 145 |
146 |
147 | Power Management 148 |
149 | 152 | 155 | 158 | 161 | 164 | 167 | 170 | 173 | 176 | 179 | 182 | 185 | 188 | 191 | 194 | 197 | 200 | 203 | 206 | 209 | 212 | 215 | 218 | 221 | 224 | 227 | 230 | 233 | 236 | 239 | 242 | 245 |
246 | 247 | 248 | 249 | 250 | 251 | 252 | Expand 253 | Minimize 254 | 255 | 256 | 257 | 258 |
259 |
260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 |
274 | 275 | 8 out of 13 rated this helpful - Rate this topic 276 | 277 | 278 |
279 | 280 | 281 | 282 |
283 | 284 |
285 |
286 | 287 |
288 |

Contains information about the power status of the system.

289 |

Syntax

290 | 291 |
292 |
293 | 294 |
295 |
296 |
297 |
298 | Copy 299 |
300 |
301 |
302 |
303 | 
304 | typedef struct _SYSTEM_POWER_STATUS {
305 |   BYTE  ACLineStatus;
306 |   BYTE  BatteryFlag;
307 |   BYTE  BatteryLifePercent;
308 |   BYTE  Reserved1;
309 |   DWORD BatteryLifeTime;
310 |   DWORD BatteryFullLifeTime;
311 | } SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS;
312 | 
313 | 
314 | 315 |
316 |
317 |
318 | 319 |

Members

320 |
321 |
ACLineStatus
322 |
323 |

The AC power status. This member can be one of the following values. 324 | 325 |

326 | 327 | 328 | 335 | 342 | 349 |
ValueMeaning
329 |
330 |
0
331 |
332 |
333 |

Offline

334 |
336 |
337 |
1
338 |
339 |
340 |

Online

341 |
343 |
344 |
255
345 |
346 |
347 |

Unknown status

348 |
350 |

 

351 |
352 |
BatteryFlag
353 |
354 |

The battery charge status. This member can contain one or more of the following flags. 355 | 356 |

357 | 358 | 359 | 366 | 373 | 380 | 387 | 394 | 401 |
ValueMeaning
360 |
361 |
1
362 |
363 |
364 |

High—the battery capacity is at more than 66 percent

365 |
367 |
368 |
2
369 |
370 |
371 |

Low—the battery capacity is at less than 33 percent

372 |
374 |
375 |
4
376 |
377 |
378 |

Critical—the battery capacity is at less than five percent

379 |
381 |
382 |
8
383 |
384 |
385 |

Charging

386 |
388 |
389 |
128
390 |
391 |
392 |

No system battery

393 |
395 |
396 |
255
397 |
398 |
399 |

Unknown status—unable to read the battery flag information

400 |
402 |

 

403 |

The value is zero if the battery is not being charged and the battery capacity is between low and high.

404 |
405 |
BatteryLifePercent
406 |
407 |

The percentage of full battery charge remaining. This member can be a value in the range 0 to 100, or 255 if status is unknown.

408 |
409 |
Reserved1
410 |
411 |

Reserved; must be zero.

412 |
413 |
BatteryLifeTime
414 |
415 |

The number of seconds of battery life remaining, or –1 if remaining seconds are unknown.

416 |
417 |
BatteryFullLifeTime
418 |
419 |

The number of seconds of battery life when at full charge, or –1 if full battery lifetime is unknown.

420 |
421 |
422 |

Remarks

423 |

The system is only capable of estimating BatteryFullLifeTime based on calculations on BatteryLifeTime and BatteryLifePercent. Without smart battery subsystems, this value may not be accurate enough to be useful.

424 |

Requirements

425 | 426 | 429 | 432 | 439 |
427 |

Minimum supported client

428 |
Windows XP [desktop apps only]
430 |

Minimum supported server

431 |
Windows Server 2003 [desktop apps only]
433 |

Header

434 |
435 |
436 |
Winbase.h (include Windows.h)
437 |
438 |
440 |

See also

441 |
442 |
443 | GetSystemPowerStatus 444 |
445 |
446 | PBT_APMPOWERSTATUSCHANGE 447 |
448 |
449 |

 

450 |

 

451 |

Send comments about this topic to Microsoft

452 |

Build date: 10/16/2012

453 |
454 |
455 | 456 | 457 | 458 | 459 | 460 |
461 |
462 | 463 |
464 |
465 | 466 | Did you find this helpful? 467 | 468 | 469 | 470 |
471 | 472 |
473 | 474 |
475 | 476 |
477 | 478 | 479 |
480 | 481 |
482 | 483 | 484 |
485 | 486 |
487 | 488 | 489 |
490 | 491 |
492 | 493 |
494 | 495 |
496 | (1500 characters remaining) 497 | 498 | 499 |
500 |
501 | 502 |
503 |
504 |
505 | 506 | 507 | 508 | 509 | 510 | 511 | 549 | 550 |
551 |
552 |
553 |

Community Additions

554 | 555 | 556 | ADD 557 | 558 | 559 |
560 |
561 |
562 | 563 |
564 |
565 | 566 | 567 | 568 |
569 | 570 | 571 |
572 |
573 | 574 | 575 | 576 | 654 | 655 |
656 |
© 2012 Microsoft. All rights reserved.
657 |
658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 688 | 689 | 690 |
691 | 707 | -------------------------------------------------------------------------------- /docs/win32/Power Setting GUIDs.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Power Setting GUIDs (Windows) 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
34 | 35 | 36 | 37 | 38 | 49 | 66 | 67 |
39 |
40 | Home 41 | Library 42 | Learn 43 | Downloads 44 | Support 45 | Community 46 | 47 |
48 |
50 |
51 | 52 | Sign in 53 | | 54 | United States - English 55 | | 56 | 57 | | 58 | 59 | 60 |
61 | 62 |
63 | 64 |
65 |
68 | 69 |
70 |
71 | 72 | 73 | 228 | 234 |
235 | 236 | 237 | 238 | 243 | 244 |
245 | 246 | 247 | 248 | 249 | 250 | 251 |
252 | 253 | 0 out of 2 rated this helpful - Rate this topic 254 | 255 | 256 |
257 | 258 |
259 |

Power Setting GUIDs

260 | 261 |
262 |
263 | 264 |
265 |

Power setting GUIDs identify power change events. This topic 266 | lists power setting GUIDs for notifications that are most useful to applications. An 267 | application should register for each power change event that might impact its behavior. Notification is sent each 268 | time a setting changes.

269 |

Power setting GUIDs are defined in 270 | WinNT.h.

271 |

272 |
273 |
GUID_ACDC_POWER_SOURCE
274 |
275 |
276 |
5d3e9a59-e9D5-4b00-a6bd-ff34ff516548
277 |
278 |

The system power source has changed. The Data member is a 279 | DWORD with values from the 280 | SYSTEM_POWER_CONDITION enumeration that indicates 281 | the current power source.

282 |
283 |
PoAc (0) - The computer is powered by an AC power source (or similar, such as a 284 | laptop powered by a 12V automotive adapter).
285 |
PoDc (1) - The computer is powered by an onboard battery power source.
286 |
PoHot (2) - The computer is powered by a short-term power source such as a UPS 287 | device.
288 |
289 |
290 |
291 |
292 |
GUID_BATTERY_PERCENTAGE_REMAINING
293 |
294 |
295 |
a7ad8041-b45a-4cae-87a3-eecbb468a9e1
296 |
297 |

The remaining battery capacity has changed. The granularity varies from system to system but the finest 298 | granularity is 1 percent. The Data member is a DWORD that 299 | indicates the current battery capacity remaining as a percentage from 0 through 100.

300 |
301 |
302 |
303 |
GUID_CONSOLE_DISPLAY_STATE
304 |
305 |
306 |
6fe69556-704a-47a0-8f24-c28d936fda47
307 |
308 |

The current monitor's display state has changed.

309 |
Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008:  This notification is available starting with Windows 8 and 310 | Windows Server 2012.
311 |

The 312 | Data member is a DWORD with one of the following 313 | values.

314 |
315 |
0x0 - The display is off.
316 |
0x1 - The display is on.
317 |
0x2 - The display is dimmed.
318 |
319 |
320 |
321 |
322 |
GUID_GLOBAL_USER_PRESENCE
323 |
324 |
325 |
786E8A1D-B427-4344-9207-09E70BDCBEA9
326 |
327 |

The user status associated with any session has changed. This represents the combined status of user presence 328 | across all local and remote sessions on the system.

329 |

This notification is sent only services and other programs running in session 0. User-mode applications should 330 | register for GUID_SESSION_USER_PRESENCE instead.

331 |
Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008:  This notification is available starting with Windows 8 and 332 | Windows Server 2012.
333 |

The Data member is a DWORD with one of the following 334 | values.

335 |
336 |
PowerUserPresent (0) - The user is present in any local or remote session on 337 | the system.
338 |
PowerUserInactive (2) - The user is not present in any local or remote session 339 | on the system.
340 |
341 |
342 |
343 |
344 |
GUID_IDLE_BACKGROUND_TASK
345 |
346 |
347 |
515c31d8-f734-163d-a0fd-11a0-8c91e8f1
348 |
349 |

The system is busy. This indicates that the system will not be moving into an idle state in the near future 350 | and that the current time is a good time for components to perform background or idle tasks that would otherwise 351 | prevent the computer from entering an idle state.

352 |

There is no notification when the system is able to move into an idle state. The idle background task 353 | notification does not indicate whether a user is present at the computer. The Data 354 | member has no information and can be ignored.

355 |
356 |
357 |
358 |
GUID_MONITOR_POWER_ON
359 |
360 |
361 |
02731015-4510-4526-99e6-e5a17ebd1aea
362 |
363 |

The primary system monitor has been powered on or off. This notification is useful for components that 364 | actively render content to the display device, such as media visualization. These applications should register 365 | for this notification and stop rendering graphics content when the monitor is off to reduce system power 366 | consumption. The Data member is a DWORD that indicates 367 | the current monitor state. 368 |

369 |
370 |
0x0 - The monitor is off.
371 |
0x1 - The monitor is on.
372 |
373 |
Windows 8 and Windows Server 2012:  New applications should use GUID_CONSOLE_DISPLAY_STATE instead of this notification.
374 |
375 |
376 |
377 |
GUID_POWERSCHEME_PERSONALITY
378 |
379 |
380 |
245d8541-3943-4422-b025-13A7-84F679B7
381 |
382 |

The active power scheme personality has changed. All power schemes map to one of these personalities. The 383 | Data member is a GUID that indicates the new active power 384 | scheme personality. 385 |

386 |
387 |
388 |

GUID_MIN_POWER_SAVINGS (8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c)

389 |

High Performance - The scheme is designed to deliver maximum performance at the expense of power consumption 390 | savings.

391 |
392 |
393 |

GUID_MAX_POWER_SAVINGS (a1841308-3541-4fab-bc81-f71556f20b4a)

394 |

Power Saver - The scheme is designed to deliver maximum power consumption savings at the expense of system 395 | performance and responsiveness.

396 |
397 |
398 |

GUID_TYPICAL_POWER_SAVINGS (381b4222-f694-41f0-9685-ff5bb260df2e)

399 |

Automatic - The scheme is designed to automatically balance performance and power consumption savings.

400 |
401 |
402 |
403 |
404 |
405 |
GUID_SESSION_DISPLAY_STATUS
406 |
407 |
408 |
2B84C20E-AD23-4ddf-93DB-05FFBD7EFCA5
409 |
410 |

The display associated with the application's session has been powered on or off.

411 |
Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008:  This notification is available starting with Windows 8 and 412 | Windows Server 2012.
413 |

This notification is sent only to user-mode applications. Services and other programs running in session 0 do 414 | not receive this notification. The Data member is a 415 | DWORD with one of the following values.

416 |
417 |
0x0 - The display is off.
418 |
0x1 - The display is on.
419 |
0x2 - The display is dimmed.
420 |
421 |
422 |
423 |
424 |
GUID_SESSION_USER_PRESENCE
425 |
426 |
427 |
3C0F4548-C03F-4c4d-B9F2-237EDE686376
428 |
429 |

The user status associated with the application's session has changed. 430 |

431 |
Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008:  This notification is available starting with Windows 8 and 432 | Windows Server 2012.
433 |

This notification is sent only to user-mode applications running in an interactive session. Services and other 434 | programs running in session 0 should register for GUID_GLOBAL_USER_PRESENCE. The 435 | Data member is a DWORD with one of the following 436 | values.

437 |
438 |
PowerUserPresent (0) - The user is providing input to the session.
439 |
PowerUserInactive (2) - The user activity timeout has elapsed with no 440 | interaction from the user.
441 |
442 |

Note  All applications that run in an interactive user-mode session should use this setting. When kernel mode applications register for monitor status they should use GUID_CONSOLE_DISPLAY_STATUS instead.

443 |
444 |
445 |
446 |
GUID_SYSTEM_AWAYMODE
447 |
448 |
449 |
98a7f580-01f7-48aa-9c0f-44352c29e5C0
450 |
451 |

The system is entering or exiting away mode. The Data member is a 452 | DWORD that indicates the current away mode state. 453 |

454 |
455 |
0x0 - The computer is exiting away mode.
456 |
0x1 - The computer is entering away mode.
457 |
458 |
459 |
460 |
461 |
462 |

Requirements

463 | 464 | 471 |
465 |

Header

466 |
467 |
468 |
WinNT.h
469 |
470 |
472 |

 

473 |

 

474 |

Send comments about this topic to Microsoft

475 |

Build date: 10/16/2012

476 |
477 |
478 | 479 | 480 | 481 | 482 | 483 |
484 |
485 | 486 |
487 |
488 | 489 | Did you find this helpful? 490 | 491 | 492 | 493 |
494 | 495 |
496 | 497 |
498 | 499 |
500 | 501 | 502 |
503 | 504 |
505 | 506 | 507 |
508 | 509 |
510 | 511 | 512 |
513 | 514 |
515 | 516 |
517 | 518 |
519 | (1500 characters remaining) 520 | 521 | 522 |
523 |
524 | 525 |
526 |
527 |
528 | 529 |
530 | 531 |
532 |
533 |
534 | 535 |
536 |
537 |
538 | Community Content 539 | 540 | Add 541 | 542 |
543 |
544 | Annotations 545 | FAQ 546 |
547 |
548 |
549 | 550 | 551 |
552 | 553 |
554 | GUID for any power scheme change 555 | 556 |
557 |
558 | Hi,
Is there any GUID for detecting a change between a user created power scheme and the Windows power scheme used for duplicating?

For example. I create a scheme called "MyScheme" from "Balanced". When I use GUID_POWERSCHEME_PERSONALITY, calling RegisterPowerSettingNotification,
it doesn't detect the change between "MyScheme" and "Balanced". It's like they are the same. It would like an event to detect a change between Myscheme and Balance.

Thanks in advance. 559 |
560 |
561 | History 562 |
563 | 564 |
565 | 566 |
567 |
568 |
569 | 570 | 571 | 575 |
576 | 577 | 578 |
579 | 580 |
581 | 582 | 583 |
584 |
585 | 586 |
587 | 588 |
589 | 686 | 687 | 688 |
689 | 690 |
691 | 692 | 693 | 694 | 695 | 696 | 697 |
698 | 699 |
700 | 701 | 702 | 703 | 708 | 709 | 710 |
711 | 712 |
713 | 714 | 715 | 716 | 730 | 731 | --------------------------------------------------------------------------------