From: <mic...@us...> - 2012-02-09 16:59:04
|
Revision: 4444 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=4444&view=rev Author: michael-t Date: 2012-02-09 16:58:50 +0000 (Thu, 09 Feb 2012) Log Message: ----------- Version 1.2.5.12 Modified Paths: -------------- trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/Common.Utils.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/Core.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/PluginBase.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/SetupControls.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TVDatabase.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TVLibrary.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TvBusinessLayer.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TvControl.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TvLibrary.Interfaces.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TvService.exe trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/Utils.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IEpgHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IPowerScheduler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IPowerSettings.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/PowerManager.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/PowerScheduler.Interfaces.csproj trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/PowerSettings.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/WaitableTimer.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/MPEI/PowerScheduler++.xmp2 trunk/plugins/PowerScheduler++/Test/1.2.5.12/PowerScheduler++.sln trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/RemoteClientStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/XmlTvImportWakeupHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/PowerScheduler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/PowerScheduler.csproj trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/PowerSchedulerFactory.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/PowerSchedulerPlugin.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/WindowsController.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/VersionInfo.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/Handlers/WakeableStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/Handlers/WakeableWakeupHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/PowerScheduler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/PowerSchedulerClientPlugin.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/PowerSchedulerClientPlugin.csproj trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/UnattendedWindow.cs Added Paths: ----------- trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/PowerScheduler.Interfaces.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/PowerScheduler.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/PowerSchedulerClientPlugin.dll trunk/plugins/PowerScheduler++/Test/1.2.5.12/MPEI/PowerScheduler++ 1.2.5.12.mpe1 trunk/plugins/PowerScheduler++/Test/1.2.5.12/MPEI/update.xml trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ActiveNetworkStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ActiveSharesStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ActiveStreamsStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ControllerActiveStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/EpgGrabbingStandbyWakeupHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/LocalClientStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/LocalClientWakeupHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/PowerControllerStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/PowerControllerWakeupHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ProcessActiveStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/RebootWakeupHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ScheduledRecordingsWakeupHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Setup/ trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/Handlers/ActiveNetworkStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/Handlers/ActiveSharesStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/Handlers/ProcessActiveStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/Handlers/RebootWakeupHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/MasterVolume.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/PowerSchedulerFactory.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/Setup/ Removed Paths: ------------- trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/bin/ trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/obj/ trunk/plugins/PowerScheduler++/Test/1.2.5.12/MPEI/PowerScheduler++ 1.1.7.1.mpe1 trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ActiveNetworkHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ActiveSharesHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ActiveStreamsHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ControllerActiveHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/EpgGrabbingHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/GenericStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/GenericWakeupHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ProcessActiveHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/RebootHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/RemoteStandbyHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/RemoteWakeupHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/Handlers/ScheduledRecordingsHandler.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/PowerSchedulerMasterSetup.Designer.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/PowerSchedulerMasterSetup.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/PowerSchedulerMasterSetup.resx trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/SelectProcessForm.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/SelectProcessForm.designer.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/SelectProcessForm.resx trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/SelectShareForm.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/SelectShareForm.designer.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/SelectShareForm.resx trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/bin/ trunk/plugins/PowerScheduler++/Test/1.2.5.12/TvEngine3/TVLibrary/Plugins/PowerScheduler/obj/ trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/PowerSchedulerClientSetup.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/PowerSchedulerClientSetup.designer.cs trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/PowerSchedulerClientSetup.resx trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/bin/ trunk/plugins/PowerScheduler++/Test/1.2.5.12/mediaportal/PowerSchedulerClientPlugin/obj/ Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/Common.Utils.dll =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/Core.dll =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/PluginBase.dll =================================================================== (Binary files differ) Copied: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/PowerScheduler.Interfaces.dll (from rev 4442, trunk/plugins/PowerScheduler++/Test/test/Binaries/PowerScheduler.Interfaces.dll) =================================================================== (Binary files differ) Copied: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/PowerScheduler.dll (from rev 4442, trunk/plugins/PowerScheduler++/Test/test/Binaries/PowerScheduler.dll) =================================================================== (Binary files differ) Copied: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/PowerSchedulerClientPlugin.dll (from rev 4442, trunk/plugins/PowerScheduler++/Test/test/Binaries/PowerSchedulerClientPlugin.dll) =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/SetupControls.dll =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TVDatabase.dll =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TVLibrary.dll =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TvBusinessLayer.dll =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TvControl.dll =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TvLibrary.Interfaces.dll =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/TvService.exe =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Binaries/Utils.dll =================================================================== (Binary files differ) Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IEpgHandler.cs =================================================================== --- trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IEpgHandler.cs 2012-02-09 16:58:34 UTC (rev 4443) +++ trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IEpgHandler.cs 2012-02-09 16:58:50 UTC (rev 4444) @@ -29,7 +29,8 @@ public delegate void EPGScheduleHandler(); /// <summary> - /// Provides access to PowerScheduler's EPG interface for EPG plugins. + /// Provides access to PowerScheduler's EPG interface for + /// EPG plugins. /// </summary> public interface IEpgHandler { Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IPowerScheduler.cs =================================================================== --- trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IPowerScheduler.cs 2012-02-09 16:58:34 UTC (rev 4443) +++ trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IPowerScheduler.cs 2012-02-09 16:58:50 UTC (rev 4444) @@ -27,7 +27,7 @@ namespace TvEngine.PowerScheduler.Interfaces { /// <summary> - /// EventHandler delegate for PowerScheduler events + /// EvenHandler delegate for PowerScheduler events /// </summary> /// <param name="args"></param> public delegate void PowerSchedulerEventHandler(PowerSchedulerEventArgs args); @@ -114,6 +114,7 @@ void GetCurrentState(bool refresh, out bool unattended, out bool disAllowShutdown, out String disAllowShutdownHandler, out DateTime nextWakeupTime, out String nextWakeupHandler); + /// <summary> /// Checks if a suspend request is in progress /// </summary> Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IPowerSettings.cs =================================================================== --- trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IPowerSettings.cs 2012-02-09 16:58:34 UTC (rev 4443) +++ trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IPowerSettings.cs 2012-02-09 16:58:50 UTC (rev 4444) @@ -25,27 +25,6 @@ namespace TvEngine.PowerScheduler.Interfaces { /// <summary> - /// PowerScheduler shutdown mode enumeration - /// </summary> - public enum ShutdownMode - { - Sleep, - Hibernate, - StayOn, - ShutDown - } - - /// <summary> - /// PowerScheduler++ standby mode enumeration - /// </summary> - public enum StandbyMode - { - StandbyAllowed, - StandbyPrevented, - AwayModeRequested - } - - /// <summary> /// Provides access to PowerScheduler's settings /// </summary> public interface IPowerSettings @@ -83,20 +62,14 @@ int PreWakeupTime { get; } /// <summary> - /// If WakeupEnabled, the time (in seconds) the system is not allowed to - /// go to shutdown before the next wakeup time - /// </summary> - int PreNoShutdownTime { get; } - - /// <summary> /// Controls the granularity of the standby/wakeup checks in seconds /// </summary> int CheckInterval { get; } /// <summary> - /// <summary> - /// How should put the system into standby? suspend/hibernate/stayon/shutdown - /// suspend uses S3, hibernate uses S4, stayon doesn't do anyting, shutdown uses S5 (soft power off) + /// How should put the system into standby? suspend/hibernate/stayon + /// suspend uses S3, hibernate uses S4, stayon is for debugging purposes and + /// doesn't put the system into standby at all /// </summary> ShutdownMode ShutdownMode { get; } } Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IStandbyHandler.cs =================================================================== --- trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IStandbyHandler.cs 2012-02-09 16:58:34 UTC (rev 4443) +++ trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/Interfaces/IStandbyHandler.cs 2012-02-09 16:58:50 UTC (rev 4444) @@ -46,6 +46,16 @@ } /// <summary> + /// PowerScheduler++ standby mode enumeration + /// </summary> + public enum StandbyMode + { + StandbyAllowed, + StandbyPrevented, + AwayModeRequested + } + + /// <summary> /// Extension to the IStandbyHandler interface for PowerScheduler++ /// </summary> public interface IStandbyHandlerEx : IStandbyHandler Modified: trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/PowerManager.cs =================================================================== --- trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/PowerManager.cs 2012-02-09 16:58:34 UTC (rev 4443) +++ trunk/plugins/PowerScheduler++/Test/1.2.5.12/Common-MP-TVE3/PowerScheduler.Interfaces/PowerManager.cs 2012-02-09 16:58:50 UTC (rev 4444) @@ -33,13 +33,22 @@ /// </summary> public static class PowerManager { - #region System power constants and enumerations + #region Variables /// <summary> + /// Handle to unregister for power setting change notification + /// </summary> + private static IntPtr _hAwayMode = (IntPtr)null; + + #endregion + + #region System power structures and enumerations + + /// <summary> /// The thread's execution requirements /// </summary> [Flags] - private enum ExecutionState : uint + enum ExecutionState : uint { /// <summary> /// Some error. @@ -76,8 +85,8 @@ /// <summary> /// Flags to register for power setting change notification /// </summary> - private const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000; - private const int DEVICE_NOTIFY_SERVICE_HANDLE = 0x00000001; + const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000; + const int DEVICE_NOTIFY_SERVICE_HANDLE = 0x00000001; /// <summary> /// Constants for power notifications @@ -105,11 +114,6 @@ } /// <summary> - /// Handle to unregister for power setting change notification - /// </summary> - private static IntPtr _hAwayMode = (IntPtr)null; - - /// <summary> /// Guid to register for power setting change notification /// </summary> public static Guid GUID_SYSTEM_AWAYMODE = new Guid("98a7f580-01f7-48aa-9c0f-44352c29e5C0"); @@ -117,88 +121,329 @@ /// <summary> /// System power setting guids /// </summary> - private static Guid NO_SUBGROUP_GUID = new Guid("fea3413e-7e05-4911-9a71-700331f1c294"); - private static Guid GUID_LOCK_CONSOLE_ON_WAKE = new Guid("0e796bdb-100d-47d6-a2d5-f7d2daa51f51"); - private static Guid GUID_SUB_SLEEP = new Guid("238c9fa8-0aad-41ed-83f4-97be242c8f20"); - private static Guid GUID_ALLOW_AWAY_MODE = new Guid("25dfa149-5dd1-4736-b5ab-e8a37b5b8187"); - private static Guid GUID_SLEEP_AFTER = new Guid("29f6c1db-86da-48c5-9fdb-f2b67b1f44da"); - private static Guid GUID_ALLOW_HYBRID_SLEEP = new Guid("94ac6d29-73ce-41a6-809f-6363ba21b47e"); - private static Guid GUID_HIBERNATE_AFTER = new Guid("9d7815a6-7ee4-497e-8888-515a05f02364"); - private static Guid GUID_ALLOW_RTC_WAKE = new Guid("bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d"); - private static Guid GUID_SUB_POWER_BUTTONS_AND_LID = new Guid("4f971e89-eebd-4455-a8de-9e59040e7347"); - private static Guid GUID_LID_CLOSE_ACTION = new Guid("5ca83367-6e45-459f-a27b-476b1d01c936"); - private static Guid GUID_POWER_BUTTON_ACTION = new Guid("7648efa3-dd9c-4e3e-b566-50f929386280"); - private static Guid GUID_SLEEP_BUTTON_ACTION = new Guid("96996bc0-ad50-47ec-923b-6f41874dd9eb"); - private static Guid GUID_SUB_MULTIMEDIA = new Guid("9596fb26-9850-41fd-ac3e-f7c3c00afd4b"); - private static Guid GUID_WHEN_SHARING_MEDIA = new Guid("03680956-93bc-4294-bba6-4e0f09bb717f"); + static Guid NO_SUBGROUP_GUID = new Guid("fea3413e-7e05-4911-9a71-700331f1c294"); + static Guid GUID_LOCK_CONSOLE_ON_WAKE = new Guid("0e796bdb-100d-47d6-a2d5-f7d2daa51f51"); + static Guid GUID_SUB_SLEEP = new Guid("238c9fa8-0aad-41ed-83f4-97be242c8f20"); + static Guid GUID_ALLOW_AWAY_MODE = new Guid("25dfa149-5dd1-4736-b5ab-e8a37b5b8187"); + static Guid GUID_SLEEP_AFTER = new Guid("29f6c1db-86da-48c5-9fdb-f2b67b1f44da"); + static Guid GUID_ALLOW_HYBRID_SLEEP = new Guid("94ac6d29-73ce-41a6-809f-6363ba21b47e"); + static Guid GUID_HIBERNATE_AFTER = new Guid("9d7815a6-7ee4-497e-8888-515a05f02364"); + static Guid GUID_ALLOW_RTC_WAKE = new Guid("bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d"); + static Guid GUID_SUB_POWER_BUTTONS_AND_LID = new Guid("4f971e89-eebd-4455-a8de-9e59040e7347"); + static Guid GUID_LID_CLOSE_ACTION = new Guid("5ca83367-6e45-459f-a27b-476b1d01c936"); + static Guid GUID_POWER_BUTTON_ACTION = new Guid("7648efa3-dd9c-4e3e-b566-50f929386280"); + static Guid GUID_SLEEP_BUTTON_ACTION = new Guid("96996bc0-ad50-47ec-923b-6f41874dd9eb"); + static Guid GUID_SUB_MULTIMEDIA = new Guid("9596fb26-9850-41fd-ac3e-f7c3c00afd4b"); + static Guid GUID_WHEN_SHARING_MEDIA = new Guid("03680956-93bc-4294-bba6-4e0f09bb717f"); /// <summary> - /// System power setting structure + /// Contains information about the power capabilities of the system. /// </summary> - private struct SystemPowerSetting + [StructLayout(LayoutKind.Sequential)] + struct SYSTEM_POWER_CAPABILITIES { - public Guid settingGuid; - public Guid subgroupGuid; + [MarshalAs(UnmanagedType.I1)] + public bool PowerButtonPresent; + [MarshalAs(UnmanagedType.I1)] + public bool SleepButtonPresent; + [MarshalAs(UnmanagedType.I1)] + public bool LidPresent; + [MarshalAs(UnmanagedType.I1)] + public bool SystemS1; + [MarshalAs(UnmanagedType.I1)] + public bool SystemS2; + [MarshalAs(UnmanagedType.I1)] + public bool SystemS3; + [MarshalAs(UnmanagedType.I1)] + public bool SystemS4; + [MarshalAs(UnmanagedType.I1)] + public bool SystemS5; + [MarshalAs(UnmanagedType.I1)] + public bool HiberFilePresent; + [MarshalAs(UnmanagedType.I1)] + public bool FullWake; + [MarshalAs(UnmanagedType.I1)] + public bool VideoDimPresent; + [MarshalAs(UnmanagedType.I1)] + public bool ApmPresent; + [MarshalAs(UnmanagedType.I1)] + public bool UpsPresent; + [MarshalAs(UnmanagedType.I1)] + public bool ThermalControl; + [MarshalAs(UnmanagedType.I1)] + public bool ProcessorThrottle; + public byte ProcessorMinimumThrottle; + public byte ProcessorMaximumThrottle; + [MarshalAs(UnmanagedType.I1)] + public bool FastSystemS4; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public byte[] spare2; + [MarshalAs(UnmanagedType.I1)] + public bool DiskSpinDown; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] + public byte[] spare3; + [MarshalAs(UnmanagedType.I1)] + public bool SystemBatteriesPresent; + [MarshalAs(UnmanagedType.I1)] + public bool BatteriesAreShortTerm; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public BATTERY_REPORTING_SCALE[] BatteryScale; + public SYSTEM_POWER_STATE AcOnlineWake; + public SYSTEM_POWER_STATE SoftLidWake; + public SYSTEM_POWER_STATE RtcWake; + public SYSTEM_POWER_STATE MinimumDeviceWakeState; + public SYSTEM_POWER_STATE DefaultLowLatencyWake; } /// <summary> - /// Array of system power settings (index is SystemPowerSettingsType) + /// Contains the granularity of the battery capacity that is reported by IOCTL_BATTERY_QUERY_STATUS. /// </summary> - static SystemPowerSetting[] SystemPowerSettings = new SystemPowerSetting[] + [StructLayout(LayoutKind.Sequential)] + struct BATTERY_REPORTING_SCALE { - new SystemPowerSetting // LOCK_CONSOLE_ON_WAKE - { - settingGuid = GUID_LOCK_CONSOLE_ON_WAKE, - subgroupGuid = NO_SUBGROUP_GUID, - }, - new SystemPowerSetting // ALLOW_AWAY_MODE - { - settingGuid = GUID_ALLOW_AWAY_MODE, - subgroupGuid = GUID_SUB_SLEEP, - }, - new SystemPowerSetting // SLEEP_AFTER - { - settingGuid = GUID_SLEEP_AFTER, - subgroupGuid = GUID_SUB_SLEEP, - }, - new SystemPowerSetting // ALLOW_HYBRID_SLEEP - { - settingGuid = GUID_ALLOW_HYBRID_SLEEP, - subgroupGuid = GUID_SUB_SLEEP, - }, - new SystemPowerSetting // HIBERNATE_AFTER - { - settingGuid = GUID_HIBERNATE_AFTER, - subgroupGuid = GUID_SUB_SLEEP, - }, - new SystemPowerSetting // ALLOW_RTC_WAKE - { - settingGuid = GUID_ALLOW_RTC_WAKE, - subgroupGuid = GUID_SUB_SLEEP, - }, - new SystemPowerSetting // LID_CLOSE_ACTION - { - settingGuid = GUID_LID_CLOSE_ACTION, - subgroupGuid = GUID_SUB_POWER_BUTTONS_AND_LID, - }, - new SystemPowerSetting // POWER_BUTTON_ACTION - { - settingGuid = GUID_POWER_BUTTON_ACTION, - subgroupGuid = GUID_SUB_POWER_BUTTONS_AND_LID, - }, - new SystemPowerSetting // SLEEP_BUTTON_ACTION - { - settingGuid = GUID_SLEEP_BUTTON_ACTION, - subgroupGuid = GUID_SUB_POWER_BUTTONS_AND_LID, - }, - new SystemPowerSetting // WHEN_SHARING_MEDIA - { - settingGuid = GUID_WHEN_SHARING_MEDIA, - subgroupGuid = GUID_SUB_MULTIMEDIA, - }, - }; + public UInt32 Granularity; + public UInt32 Capacity; + } /// <summary> + /// Contains information about the power status of the system. + /// </summary> + struct SYSTEM_POWER_STATUS + { + public ACLineStatus ACLineStatus; + public BatteryFlag BatteryFlag; + public Byte BatteryLifePercent; + public Byte Reserved1; + public Int32 BatteryLifeTime; + public Int32 BatteryFullLifeTime; + } + + /// <summary> + /// The AC power status. + /// </summary> + enum ACLineStatus : byte + { + Offline = 0, + Online = 1, + Unknown = 255 + } + + /// <summary> + /// The battery charge status. + /// </summary> + enum BatteryFlag : byte + { + High = 1, + Low = 2, + Critical = 4, + Charging = 8, + NoSystemBattery = 128, + Unknown = 255 + } + + /// <summary> + /// Contains information used to set the system power state. + /// </summary> + [StructLayout(LayoutKind.Sequential, Pack=1)] + struct POWER_ACTION_POLICY + { + public POWER_ACTION Action; + public PowerActionFlags Flags; + public PowerActionEventCode EventCode; + } + + /// <summary> + /// Defines values that are used to specify system power action types. + /// </summary> + enum POWER_ACTION : uint + { + PowerActionNone = 0, // No system power action. + PowerActionReserved, // Reserved; do not use. + PowerActionSleep, // Sleep. + PowerActionHibernate, // Hibernate. + PowerActionShutdown, // Shutdown. + PowerActionShutdownReset, // Shutdown and reset. + PowerActionShutdownOff, // Shutdown and power off. + PowerActionWarmEject, // Warm eject. + } + + /// <summary> + /// + /// </summary> + [Flags] + enum PowerActionFlags : uint + { + POWER_ACTION_QUERY_ALLOWED = 0x00000001, // Broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation. + POWER_ACTION_UI_ALLOWED = 0x00000002, // Applications can prompt the user for directions on how to prepare for suspension. Sets bit 0 in the Flags parameter passed in the lParam parameter of WM_POWERBROADCAST. + POWER_ACTION_OVERRIDE_APPS = 0x00000004, // Ignores applications that do not respond to the PBT_APMQUERYSUSPEND event broadcast in the WM_POWERBROADCAST message. + POWER_ACTION_LIGHTEST_FIRST = 0x10000000, // Uses the first lightest available sleep state. + POWER_ACTION_LOCK_CONSOLE = 0x20000000, // Requires entry of the system password upon resume from one of the system standby states. + POWER_ACTION_DISABLE_WAKES = 0x40000000, // Disables all wake events. + POWER_ACTION_CRITICAL = 0x80000000, // Forces a critical suspension. + } + + [Flags] + enum PowerActionEventCode : uint + { + POWER_LEVEL_USER_NOTIFY_TEXT = 0x00000001, // User notified using the UI. + POWER_LEVEL_USER_NOTIFY_SOUND = 0x00000002, // User notified using sound. + POWER_LEVEL_USER_NOTIFY_EXEC = 0x00000004, // Specifies a program to be executed. + POWER_USER_NOTIFY_BUTTON = 0x00000008, // Indicates that the power action is in response to a user power button press. + POWER_USER_NOTIFY_SHUTDOWN = 0x00000010, // Indicates a power action of shutdown/off. + POWER_FORCE_TRIGGER_RESET = 0x80000000, // Clears a user power button press. + } + + /// <summary> + /// The global flags constants are used to enable or disable user power policy options + /// </summary> + [Flags] + enum GlobalFlags : uint + { + EnableMultiBatteryDisplay = 0x02, // Enables or disables multiple battery display in the system Power Meter. + EnablePasswordLogon = 0x04, // Enables or disables requiring password logon when the system resumes from standby or hibernate. + EnableSysTrayBatteryMeter = 0x01, // Enables or disables the battery meter icon in the system tray. When this flag is cleared, the battery meter icon is not displayed. + EnableVideoDimDisplay = 0x10, // Enables or disables support for dimming the video display when the system changes from running on AC power to running on battery power. + EnableWakeOnRing = 0x08, // Enables or disables wake on ring support. + } + + /// <summary> + /// Contains power policy settings that are unique to each power scheme. + /// </summary> + struct POWER_POLICY + { + public USER_POWER_POLICY user; + public MACHINE_POWER_POLICY mach; + } + + /// <summary> + /// Contains power policy settings that are unique to each power scheme for a user. + /// </summary> + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] + struct USER_POWER_POLICY { + public uint Revision; + public POWER_ACTION_POLICY IdleAc; + public POWER_ACTION_POLICY IdleDc; + public uint IdleTimeoutAc; + public uint IdleTimeoutDc; + public byte IdleSensitivityAc; + public byte IdleSensitivityDc; + public byte ThrottlePolicyAc; + public byte ThrottlePolicyDc; + public SYSTEM_POWER_STATE MaxSleepAc; + public SYSTEM_POWER_STATE MaxSleepDc; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] + public uint[] Reserved; + public uint VideoTimeoutAc; + public uint VideoTimeoutDc; + public uint SpindownTimeoutAc; + public uint SpindownTimeoutDc; + [MarshalAs(UnmanagedType.I1)] + public bool OptimizeForPowerAc; + [MarshalAs(UnmanagedType.I1)] + public bool OptimizeForPowerDc; + public byte FanThrottleToleranceAc; + public byte FanThrottleToleranceDc; + public byte ForcedThrottleAc; + public byte ForcedThrottleDc; + } + + /// <summary> + /// Contains computer power policy settings that are unique to each power scheme on the computer. + /// </summary> + struct MACHINE_POWER_POLICY { + public uint Revision; + public SYSTEM_POWER_STATE MinSleepAc; + public SYSTEM_POWER_STATE MinSleepDc; + public SYSTEM_POWER_STATE ReducedLatencySleepAc; + public SYSTEM_POWER_STATE ReducedLatencySleepDc; + public uint DozeTimeoutAc; + public uint DozeTimeoutDc; + public uint DozeS4TimeoutAc; + public uint DozeS4TimeoutDc; + public byte MinThrottleAc; + public byte MinThrottleDc; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] + public byte[] pad1; + public POWER_ACTION_POLICY OverThrottledAc; + public POWER_ACTION_POLICY OverThrottledDc; + } + + /// <summary> + /// Contains global power policy settings that apply to all power schemes. + /// </summary> + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct GLOBAL_POWER_POLICY + { + public GLOBAL_USER_POWER_POLICY user; + public GLOBAL_MACHINE_POWER_POLICY mach; + } + + /// <summary> + /// Contains global user power policy settings that apply to all power schemes for a user. + /// </summary> + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct GLOBAL_USER_POWER_POLICY + { + public const int NUM_DISCHARGE_POLICIES = 4; + + public uint Revision; + public POWER_ACTION_POLICY PowerButtonAc; + public POWER_ACTION_POLICY PowerButtonDc; + public POWER_ACTION_POLICY SleepButtonAc; + public POWER_ACTION_POLICY SleepButtonDc; + public POWER_ACTION_POLICY LidCloseAc; + public POWER_ACTION_POLICY LidCloseDc; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = NUM_DISCHARGE_POLICIES)] + public SYSTEM_POWER_LEVEL[] DischargePolicy; + public GlobalFlags GlobalFlags; + } + + /// <summary> + /// Contains information about system battery drain policy settings. + /// </summary> + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct SYSTEM_POWER_LEVEL + { + public bool Enable; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public byte[] Spare; + public uint BatteryLevel; + public POWER_ACTION_POLICY PowerPolicy; + public SYSTEM_POWER_STATE MinSystemState; + } + + /// <summary> + /// Contains global computer power policy settings that apply to all power schemes for all users. + /// </summary> + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct GLOBAL_MACHINE_POWER_POLICY + { + public uint Revision; + public SYSTEM_POWER_STATE LidOpenWakeAc; + public SYSTEM_POWER_STATE LidOpenWakeDc; + public uint BroadcastCapacityResolution; + } + + /// <summary> + /// Defines values that are used to specify system power states. + /// </summary> + enum SYSTEM_POWER_STATE + { + PowerSystemUnspecified = 0, + PowerSystemWorking = 1, + PowerSystemSleeping1 = 2, + PowerSystemSleeping2 = 3, + PowerSystemSleeping3 = 4, + PowerSystemHibernate = 5, + PowerSystemShutdown = 6, + PowerSystemMaximum = 7 + } + + #endregion + + #region Public methods + + /// <summary> /// Power setting type (index to PowerSettings) /// </summary> public enum SystemPowerSettingType @@ -214,15 +459,10 @@ SLEEP_BUTTON_ACTION, WHEN_SHARING_MEDIA } - - #endregion - - #region Public power management methods - + /// <summary> /// Reset the system idle timeout to prevent standby /// </summary> - /// <returns>bool indicating whether or not standby is prevented</returns> public static void ResetIdleTimer() { // ES_SYSTEM_REQUIRED without ES_CONTINUOS resets the idle timeout @@ -273,65 +513,44 @@ } /// <summary> - /// Retrieves guid of active power scheme + /// Get power setting value for active power status (OS independent) /// </summary> - /// <returns>Active power scheme guid</returns> - public static Guid GetActivePowerScheme() + /// <param name="settingType"></param> + /// <returns>Returns value of power setting requested. Returns 0 on errors.</returns> + public static int GetActivePowerSetting(SystemPowerSettingType settingType) { - try - { - IntPtr ptr = IntPtr.Zero; - PowerGetActiveScheme(IntPtr.Zero, ref ptr); - Guid activePowerScheme = (Guid)Marshal.PtrToStructure(ptr, typeof(Guid)); - return activePowerScheme; - } - catch (Exception) { } - - return Guid.Empty; + if (Environment.OSVersion.Version.Major >= 6) + return GetSystemPowerSetting(settingType, ACPowerPluggedIn); + else + return GetPowerPolicySetting(settingType, ACPowerPluggedIn); } /// <summary> - /// Get system power setting + /// Get power setting value for indicated power status (OS independent) /// </summary> - /// <param name="scheme"></param> /// <param name="settingType"></param> - /// <returns>System power setting AC value</returns> - public static UInt32 GetSystemPowerSetting(Guid scheme, SystemPowerSettingType settingType) + /// <param name="AC"></param> + /// <returns>Returns value of power setting requested. Returns 0 on errors.</returns> + public static int GetPowerSetting(SystemPowerSettingType settingType, bool AC) { - UInt32 systemPowerSetting = 0; - - try - { - SystemPowerSetting ps = SystemPowerSettings[(int)settingType]; - systemPowerSetting = ReadACPowerValueIndex(scheme, ps.subgroupGuid, ps.settingGuid); - } - catch (Exception) { } - - return systemPowerSetting; + if (Environment.OSVersion.Version.Major >= 6) + return GetSystemPowerSetting(settingType, AC); + else + return GetPowerPolicySetting(settingType, AC); } /// <summary> - /// Set system power setting AC and DC value + /// Set power setting value (OS independent) /// </summary> - /// <param name="scheme"></param> /// <param name="settingType"></param> + /// <param name="AC"></param> /// <param name="value"></param> - public static void SetSystemPowerSetting(Guid scheme, SystemPowerSettingType settingType, UInt32 value) + public static void SetPowerSetting(SystemPowerSettingType settingType, bool AC, uint value) { - UInt32 systemPowerSetting; - - try - { - SystemPowerSetting ps = SystemPowerSettings[(int)settingType]; - systemPowerSetting = ReadACPowerValueIndex(scheme, ps.subgroupGuid, ps.settingGuid); - if (systemPowerSetting != value) - WriteACPowerValueIndex(scheme, ps.subgroupGuid, ps.settingGuid, value); - systemPowerSetting = ReadDCPowerValueIndex(scheme, ps.subgroupGuid, ps.settingGuid); - if (systemPowerSetting != value) - WriteDCPowerValueIndex(scheme, ps.subgroupGuid, ps.settingGuid, value); - } - catch (Exception) { } - + if (Environment.OSVersion.Version.Major >= 6) + SetSystemPowerSetting(settingType, AC, value); + else + SetPowerPolicySetting(settingType, AC, value); } /// <summary> @@ -357,6 +576,94 @@ #endregion + #region Public properties + + /// <summary> + /// Check if running on AC Power + /// </summary> + /// <returns>Returns true if running on AC Power</returns> + public static bool RunningOnAC + { + get + { + SYSTEM_POWER_STATUS sps = new SYSTEM_POWER_STATUS(); + try + { + GetSystemPowerStatus(ref sps); + return (sps.ACLineStatus != ACLineStatus.Offline); + } + catch (Exception) + { + return true; // Fallback: running on AC power + } + } + } + + /// <summary> + /// Check DC Power source + /// </summary> + /// <returns>Returns true if there is a DC Power source</returns> + public static bool HasDCPowerSource + { + get + { + SYSTEM_POWER_STATUS sps = new SYSTEM_POWER_STATUS(); + try + { + GetSystemPowerStatus(ref sps); + return (((byte)sps.BatteryFlag & (byte)BatteryFlag.NoSystemBattery) == 0); + } + catch (Exception) + { + return false; // Fallback: no DC power source + } + } + } + + /// <summary> + /// Check system lid + /// </summary> + /// <returns>Returns true if lid is present</returns> + public static bool HasLid + { + get + { + SYSTEM_POWER_CAPABILITIES spc = new SYSTEM_POWER_CAPABILITIES(); + try + { + GetPwrCapabilities(ref spc); + return (spc.LidPresent); + } + catch (Exception) + { + return false; // Fallback: No lid + } + } + } + + /// <summary> + /// Check if system can hibernate + /// </summary> + /// <returns>Returns true if lid is present</returns> + public static bool CanHibernate + { + get + { + SYSTEM_POWER_CAPABILITIES spc = new SYSTEM_POWER_CAPABILITIES(); + try + { + GetPwrCapabilities(ref spc); + return (spc.SystemS4 && spc.HiberFilePresent); + } + catch (Exception) + { + return false; // Fallback: Cannot hibernate + } + } + } + + #endregion + #region System power management functions imports /// <summary> @@ -391,10 +698,26 @@ private static extern bool UnregisterPowerSettingNotification(IntPtr handle); /// <summary> + /// Retrieves the power status of the system. + /// </summary> + /// <param name="__out LPSYSTEM_POWER_STATUS lpSystemPowerStatus">A pointer to a SYSTEM_POWER_STATUS structure that receives status information.</param> + /// <returns></returns> + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool GetSystemPowerStatus(ref SYSTEM_POWER_STATUS lpSystemPowerStatus); + + /// <summary> + /// Retrieves information about the system power capabilities. + /// </summary> + /// <param name="__out PSYSTEM_POWER_CAPABILITIES lpSystemPowerCapabilities"></param> + /// <returns></returns> + [DllImport("powrprof.dll", SetLastError = true)] + static extern bool GetPwrCapabilities(ref SYSTEM_POWER_CAPABILITIES lpSystemPowerCapabilities); + + /// <summary> /// Retrieves the active power scheme and returns a GUID that identifies the scheme. /// </summary> - /// <param name="userRootPowerKey"></param> - /// <param name="activePolicyGuid"></param> + /// <param name="__in_opt HKEY UserRootPowerKey">This parameter is reserved for future use and must be set to NULL.</param> + /// <param name="__out GUID **ActivePolicyGuid">A pointer that receives a pointer to a GUID structure. Use the LocalFree function to free this memory.</param> /// <returns></returns> [DllImport("powrprof.dll", SetLastError = true)] private static extern UInt32 PowerGetActiveScheme(IntPtr RootPowerKey, ref IntPtr activePolicyGuid); @@ -441,91 +764,505 @@ /// <summary> /// Writes the DC index of the specified power setting. /// </summary> - /// <param name="rootPowerKey"></param> - /// <param name="schemeGuid"></param> - /// <param name="subgroupOfPowerSettingsGuid"></param> - /// <param name="powerSettingGuid"></param> - /// <param name="valueIndex"></param> + /// <param name="__in_opt HKEY RootPowerKey"></param> + /// <param name="__in const GUID *SchemeGuid"></param> + /// <param name="__in_opt const GUID *SubGroupOfPowerSettingsGuid"></param> + /// <param name="__in_opt const GUID *PowerSettingGuid"></param> + /// <param name="__in DWORD DcValueIndex"></param> /// <returns></returns> [DllImport("powrprof.dll", SetLastError = true)] private static extern UInt32 PowerWriteDCValueIndex(IntPtr RootPowerKey, ref Guid SchemeGuid, ref Guid SubGroupOfPowerSettingsGuid, ref Guid PowerSettingGuid, UInt32 DcValueIndex); + [DllImport("powrprof.dll", SetLastError = true)] + private static extern bool GetCurrentPowerPolicies(ref GLOBAL_POWER_POLICY pGlobalPowerPolicy, ref POWER_POLICY pPowerPolicy); + + [DllImport("powrprof.dll", SetLastError = true)] + private static extern bool GetActivePwrScheme(ref UInt32 puiID); + + [DllImport("powrprof.dll", SetLastError = true)] + private static extern bool SetActivePwrScheme(uint uiID, ref GLOBAL_POWER_POLICY lpGlobalPowerPolicy, ref POWER_POLICY lpPowerPolicy); + #endregion - #region Private power management wrapper methods + #region Private power management helper methods /// <summary> - /// Wrapper for PowerReadACValueIndex + /// System power setting structure /// </summary> - /// <param name="schemeGuid"></param> - /// <param name="subgroupGuid"></param> - /// <param name="settingGuid"></param> - /// <returns></returns> - private static UInt32 ReadACPowerValueIndex(Guid schemeGuid, Guid subgroupGuid, Guid settingGuid) + struct SystemPowerSetting { - UInt32 value = 0; + public Guid settingGuid; + public Guid subgroupGuid; + } + + /// <summary> + /// Array of system power settings (index is SystemPowerSettingsType) + /// </summary> + static SystemPowerSetting[] SystemPowerSettings = new SystemPowerSetting[] + { + new SystemPowerSetting // LOCK_CONSOLE_ON_WAKE + { + settingGuid = GUID_LOCK_CONSOLE_ON_WAKE, + subgroupGuid = NO_SUBGROUP_GUID, + }, + new SystemPowerSetting // ALLOW_AWAY_MODE + { + settingGuid = GUID_ALLOW_AWAY_MODE, + subgroupGuid = GUID_SUB_SLEEP, + }, + new SystemPowerSetting // SLEEP_AFTER + { + settingGuid = GUID_SLEEP_AFTER, + subgroupGuid = GUID_SUB_SLEEP, + }, + new SystemPowerSetting // ALLOW_HYBRID_SLEEP + { + settingGuid = GUID_ALLOW_HYBRID_SLEEP, + subgroupGuid = GUID_SUB_SLEEP, + }, + new SystemPowerSetting // HIBERNATE_AFTER + { + settingGuid = GUID_HIBERNATE_AFTER, + subgroupGuid = GUID_SUB_SLEEP, + }, + new SystemPowerSetting // ALLOW_RTC_WAKE + { + settingGuid = GUID_ALLOW_RTC_WAKE, + subgroupGuid = GUID_SUB_SLEEP, + }, + new SystemPowerSetting // LID_CLOSE_ACTION + { + settingGuid = GUID_LID_CLOSE_ACTION, + subgroupGuid = GUID_SUB_POWER_BUTTONS_AND_LID, + }, + new SystemPowerSetting // POWER_BUTTON_ACTION + { + settingGuid = GUID_POWER_BUTTON_ACTION, + subgroupGuid = GUID_SUB_POWER_BUTTONS_AND_LID, + }, + new SystemPowerSetting // SLEEP_BUTTON_ACTION + { + settingGuid = GUID_SLEEP_BUTTON_ACTION, + subgroupGuid = GUID_SUB_POWER_BUTTONS_AND_LID, + }, + new SystemPowerSetting // WHEN_SHARING_MEDIA + { + settingGuid = GUID_WHEN_SHARING_MEDIA, + subgroupGuid = GUID_SUB_MULTIMEDIA, + }, + }; + + /// <summary> + /// Check AC power status + /// </summary> + /// <returns>Returns true if AC power is plugged in</returns> + private static bool ACPowerPluggedIn + { + get + { + SYSTEM_POWER_STATUS sps = new SYSTEM_POWER_STATUS(); + try + { + GetSystemPowerStatus(ref sps); + return (sps.ACLineStatus == ACLineStatus.Online); + } + catch (Exception) + { + return true; // Fallback: AC power is plugged in + } + } + } + + /// <summary> + /// Get system power setting for indicated power status (Vista / Windows 7) + /// </summary> + /// <param name="settingType"></param> + /// <returns>Returns value of power setting requested. Returns -1 on errors.</returns> + private static int GetSystemPowerSetting(SystemPowerSettingType settingType, bool AC) + { try { - PowerReadACValueIndex(IntPtr.Zero, ref schemeGuid, ref subgroupGuid, ref settingGuid, ref value); + IntPtr ptr = IntPtr.Zero; + Guid scheme; + UInt32 value = 0; + UInt32 success; + + PowerGetActiveScheme(IntPtr.Zero, ref ptr); + scheme = (Guid)Marshal.PtrToStructure(ptr, typeof(Guid)); + SystemPowerSetting ps = SystemPowerSettings[(int)settingType]; + if (AC) + success = PowerReadACValueIndex(IntPtr.Zero, ref scheme, ref ps.subgroupGuid, ref ps.settingGuid, ref value); + else + success = PowerReadDCValueIndex(IntPtr.Zero, ref scheme, ref ps.subgroupGuid, ref ps.settingGuid, ref value); + if (success == 0) + return (int)value; + return -1; } - catch (Exception) { } - - return value; + catch (Exception) + { + return -1; + } } /// <summary> - /// Wrapper for PowerWriteACValueIndex + /// Set system power setting value for indicated power status /// </summary> - /// <param name="schemeGuid"></param> - /// <param name="subgroupGuid"></param> - /// <param name="settingGuid"></param> + /// <param name="settingType"></param> + /// <param name="AC"></param> /// <param name="value"></param> - private static void WriteACPowerValueIndex(Guid schemeGuid, Guid subgroupGuid, Guid settingGuid, UInt32 value) + private static void SetSystemPowerSetting(SystemPowerSettingType settingType, bool AC, uint value) { try { - PowerWriteACValueIndex(IntPtr.Zero, ref schemeGuid, ref subgroupGuid, ref settingGuid, value); + IntPtr ptr = IntPtr.Zero; + PowerGetActiveScheme(IntPtr.Zero, ref ptr); + Guid scheme = (Guid)Marshal.PtrToStructure(ptr, typeof(Guid)); + + SystemPowerSetting ps = SystemPowerSettings[(int)settingType]; + if (AC) + PowerWriteACValueIndex(IntPtr.Zero, ref scheme, ref ps.subgroupGuid, ref ps.settingGuid, value); + else + PowerWriteDCValueIndex(IntPtr.Zero, ref scheme, ref ps.subgroupGuid, ref ps.settingGuid, value); } catch (Exception) { } } /// <summary> - /// Wrapper for PowerReadDCValueIndex + /// Get power policy setting for indicated power status (Windows XP) /// </summary> - /// <param name="schemeGuid"></param> - /// <param name="subgroupGuid"></param> - /// <param name="settingGuid"></param> + /// <param name="settingType">Returns value of power setting requested. Returns -1 on errors.</param> /// <returns></returns> - private static UInt32 ReadDCPowerValueIndex(Guid schemeGuid, Guid subgroupGuid, Guid settingGuid) + private static int GetPowerPolicySetting(SystemPowerSettingType settingType, bool AC) { - UInt32 value = 0; try { - PowerReadDCValueIndex(IntPtr.Zero, ref schemeGuid, ref subgroupGuid, ref settingGuid, ref value); + GLOBAL_POWER_POLICY gpp = new GLOBAL_POWER_POLICY(); + POWER_POLICY pp = new POWER_POLICY(); + POWER_ACTION action; + PowerActionEventCode eventCode; + + if (!GetCurrentPowerPolicies(ref gpp, ref pp)) + { + return -1; + } + + switch (settingType) + { + case SystemPowerSettingType.STANDBYIDLE: + if (AC) + { + if (pp.user.IdleAc.Action == POWER_ACTION.PowerActionSleep) + return (int)pp.user.IdleTimeoutAc; + } + else + { + if (pp.user.IdleDc.Action == POWER_ACTION.PowerActionSleep) + return (int)pp.user.IdleTimeoutDc; + } + return 0; + + case SystemPowerSettingType.HIBERNATE_AFTER: + if (AC) + { + if (pp.user.IdleAc.Action == POWER_ACTION.PowerActionHibernate) + return (int)pp.user.IdleTimeoutAc; + if (pp.mach.DozeS4TimeoutAc != 0) + return (int)(pp.mach.DozeS4TimeoutAc + pp.user.IdleTimeoutAc); + } + else + { + if (pp.user.IdleDc.Action == POWER_ACTION.PowerActionHibernate) + return (int)pp.user.IdleTimeoutDc; + if (pp.mach.DozeS4TimeoutDc != 0) + return (int)(pp.mach.DozeS4TimeoutDc + pp.user.IdleTimeoutDc); + } + return 0; + + case SystemPowerSettingType.LID_CLOSE_ACTION: + if (AC) + { + action = gpp.user.LidCloseAc.Action; + eventCode = gpp.user.LidCloseAc.EventCode; + } + else + { + action = gpp.user.LidCloseDc.Action; + eventCode = gpp.user.LidCloseDc.EventCode; + } + switch (action) + { + case POWER_ACTION.PowerActionSleep: + return 1; // Sleep + case POWER_ACTION.PowerActionHibernate: + return 2; // Hibernate + default: + if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_SHUTDOWN) != 0) + return 3; // Shutdown + if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_BUTTON) != 0) + return 4; // Ask User + return 0; //Do nothing + } + + case SystemPowerSettingType.POWER_BUTTON_ACTION: + if (AC) + { + action = gpp.user.PowerButtonAc.Action; + eventCode = gpp.user.PowerButtonAc.EventCode; + } + else + { + action = gpp.user.PowerButtonDc.Action; + eventCode = gpp.user.PowerButtonDc.EventCode; + } + switch (action) + { + case POWER_ACTION.PowerActionSleep: + return 1; // Sleep + case POWER_ACTION.PowerActionHibernate: + return 2; // Hibernate + default: + if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_SHUTDOWN) != 0) + return 3; // Shutdown + if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_BUTTON) != 0) + return 4; // Ask User + return 0; //Do nothing + } + + case SystemPowerSettingType.SLEEP_BUTTON_ACTION: + if (AC) + { + action = gpp.user.SleepButtonAc.Action; + eventCode = gpp.user.SleepButtonAc.EventCode; + } + else + { + action = gpp.user.SleepButtonDc.Action; + eventCode = gpp.user.SleepButtonDc.EventCode; + } + switch (action) + { + case POWER_ACTION.PowerActionSleep: + return 1; // Sleep + case POWER_ACTION.PowerActionHibernate: + return 2; // Hibernate + default: + if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_SHUTDOWN) != 0) + return 3; // Shutdown + if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_BUTTON) != 0) + return 4; // Ask User + return 0; //Do nothing + } + + default: + return 0; + } } - catch (Exception) { } - - return value; + catch (Exception) + { + return -1; + } } /// <summary> - /// Wrapper for PowerWriteDCValueIndex + /// Set power policy setting value for indicated power status (Windows XP) /// </summary> - /// <param name="schemeGuid"></param> - /// <param name="subgroupGuid"></param> - /// <param name="settingGuid"></param> + /// <param name="settingType"></param> + /// <param name="AC"></param> /// <param name="value"></param> - private static void WriteDCPowerValueIndex(Guid schemeGuid, Guid subgroupGuid, Guid settingGuid, UInt32 value) + private static void SetPowerPolicySetting(SystemPowerSettingType settingType, bool AC, uint value) { try { - PowerWriteDCValueIndex(IntPtr.Zero, ref schemeGuid, ref subgroupGuid, ref settingGuid, value); + GLOBAL_POWER_POLICY gpp = new GLOBAL_POWER_POLICY(); + POWER_POLICY pp = new POWER_POLICY(); + UInt32 index = 0; + POWER_ACTION action; + PowerActionEventCode eventCode; + + if (!GetCurrentPowerPolicies(ref gpp, ref pp)) + return; + + GetActivePwrScheme(ref index); + + switch (settingType) + { + case SystemPowerSettingType.STANDBYIDLE: + if (AC) + { + pp.user.IdleTimeoutAc = value; + if (value == 0 && pp.mach.DozeS4TimeoutAc != 0) + pp.user.IdleAc.Action = POWER_ACTION.PowerActionHibernate; + else + pp.user.IdleAc.Action = POWER_ACTION.PowerActionSleep; + } + else + { + pp.user.IdleTimeoutDc = value; + if (value == 0 && pp.mach.DozeS4TimeoutDc != 0) + pp.user.IdleDc.Action = POWER_ACTION.PowerActionHibernate; + else + pp.user.IdleDc.Action = POWER_ACTION.PowerActionSleep; + } + break; + + case SystemPowerSettingType.HIBERNATE_AFTER: + if (AC) + { + if (pp.user.IdleTimeoutAc == 0) + { + pp.user.IdleTimeoutAc = value; + pp.mach.DozeS4TimeoutAc = 0; + pp.user.IdleAc.Action = POWER_ACTION.PowerActionHibernate; + } + else + { + if (value > pp.user.IdleTimeoutAc) + pp.mach.DozeS4TimeoutAc = value - pp.user.IdleTimeoutAc; + else + pp.mach.DozeS4TimeoutAc = value; + pp.user.IdleAc.Action = POWER_ACTION.PowerActionSleep; + } + } + else + { + if (pp.user.IdleTimeoutDc == 0) + { + pp.user.IdleTimeoutDc = value; + pp.mach.DozeS4TimeoutDc = 0; + pp.user.IdleDc.Action = POWER_ACTION.PowerActionHibernate; + } + else + { + if (value > pp.user.IdleTimeoutDc) + pp.mach.DozeS4TimeoutDc = value - pp.user.IdleTimeoutDc; + else + pp.mach.DozeS4TimeoutDc = value; + pp.user.IdleDc.Action = POWER_ACTION.PowerActionSleep; + } + } + break; + + case SystemPowerSettingType.LID_CLOSE_ACTION: + eventCode = 0; + switch (value) + { + case 0: // Do nothing + action = POWER_ACTION.PowerActionNone; + eventCode = PowerActionEventCode.POWER_FORCE_TRIGGER_RESET; + break; + case 1: // Sleep + action = POWER_ACTION.PowerActionSleep; + break; + case 2: // Hibernate + action = POWER_ACTION.PowerActionHibernate; + break; + case 3: // Shutdown + action = POWER_ACTION.PowerActionNone; + eventCode = PowerActionEventCode.POWER_USER_NOTIFY_SHUTDOWN; + break; + case 4: // Ask User + action = POWER_ACTION.PowerActionNone; + eventCode = PowerActionEventCode.POWER_USER_NOTIFY_BUTTON; + break; + default: + return; + } + if (AC) + { + gpp.user.LidCloseAc.Action = action; + gpp.user.LidCloseAc.EventCode = eventCode; + } + else + { + gpp.user.LidCloseDc.Action = action; + gpp.user.LidCloseDc.EventCode = eventCo... [truncated message content] |