public static PowerPolicy GetPowerPolicy(PowerType powerType) { var powerPolicy = new POWER_POLICY(); var globalPowerPolicy = new GLOBAL_POWER_POLICY(); if (!PlatformInvokerPowrprof.GetCurrentPowerPolicies(ref globalPowerPolicy, ref powerPolicy)) { Log.Warning("Unable to get power management state"); return(null); } var userPolicy = powerPolicy.user; switch (powerType) { case PowerType.AC: return(new PowerPolicy { IdleTimeout = userPolicy.IdleTimeoutAc == 0 || userPolicy.IdleAc.Action == POWER_ACTION.PowerActionNone ? (TimeSpan?)null : TimeSpan.FromSeconds(userPolicy.IdleTimeoutAc), VideoTimeout = userPolicy.VideoTimeoutAc == 0 ? (TimeSpan?)null : TimeSpan.FromSeconds(userPolicy.VideoTimeoutAc) }); case PowerType.DC: return(new PowerPolicy { IdleTimeout = userPolicy.IdleTimeoutDc == 0 || userPolicy.IdleDc.Action == POWER_ACTION.PowerActionNone ? (TimeSpan?)null : TimeSpan.FromSeconds(userPolicy.IdleTimeoutDc), VideoTimeout = userPolicy.VideoTimeoutDc == 0 ? (TimeSpan?)null : TimeSpan.FromSeconds(userPolicy.VideoTimeoutDc) }); default: throw new ArgumentOutOfRangeException(nameof(powerType)); } }
internal static extern bool GetCurrentPowerPolicies(ref GLOBAL_POWER_POLICY pGlobalPowerPolicy, ref POWER_POLICY pPowerPolicy);