Browse Source
* The power state is currently exposed via a property. Events (firing on state changes, alarms at certain percentages, etc) will come later. * A simple sample project (only outputs to console so far) is included.improved_timing
Ian Burgmyer
6 years ago
9 changed files with 155 additions and 0 deletions
@ -0,0 +1,31 @@
|
||||
namespace DotSDL.Power { |
||||
/// <summary> |
||||
/// The status of the system's battery. |
||||
/// </summary> |
||||
public enum BatteryStatus { |
||||
/// <summary> |
||||
/// Cannot determine power status. |
||||
/// </summary> |
||||
Unknown = 0x00, |
||||
|
||||
/// <summary> |
||||
/// Not plugged in, running on the battery. |
||||
/// </summary> |
||||
OnBattery = 0x01, |
||||
|
||||
/// <summary> |
||||
/// Plugged in, no battery available. |
||||
/// </summary> |
||||
NoBattery = 0x02, |
||||
|
||||
/// <summary> |
||||
/// Plugged in, charging battery. |
||||
/// </summary> |
||||
Charging = 0x03, |
||||
|
||||
/// <summary> |
||||
/// Plugged in, battery charged. |
||||
/// </summary> |
||||
Charged = 0x04 |
||||
} |
||||
} |
@ -0,0 +1,25 @@
|
||||
using System; |
||||
|
||||
namespace DotSDL.Power { |
||||
/// <summary> |
||||
/// Contains functions for handling a system's power supply. This is most |
||||
/// useful on laptops, though it may also be useful for desktop PCs that |
||||
/// are connected to a UPS. |
||||
/// </summary> |
||||
public static class PowerState { |
||||
/// <summary> |
||||
/// Retrieves the current power state of the system as a |
||||
/// <see cref="PowerStatus"/> object. |
||||
/// </summary> |
||||
public static PowerStatus CurrentPowerState { |
||||
get { |
||||
var state = Sdl.Power.GetPowerInfo(out var secs, out var pct); |
||||
return new PowerStatus { |
||||
BatteryStatus = state, |
||||
BatteryPercent = pct, |
||||
TimeRemaining = TimeSpan.FromSeconds(secs) |
||||
}; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,21 @@
|
||||
using System; |
||||
|
||||
namespace DotSDL.Power { |
||||
public class PowerStatus { |
||||
/// <summary> |
||||
/// The current state of the system's battery. |
||||
/// </summary> |
||||
public BatteryStatus BatteryStatus { get; internal set; } |
||||
|
||||
/// <summary> |
||||
/// The approximate percentage of battery power remaining. |
||||
/// </summary> |
||||
public int BatteryPercent { get; internal set; } |
||||
|
||||
/// <summary> |
||||
/// The approximate amount of time that the system can be powered by the |
||||
/// battery. |
||||
/// </summary> |
||||
public TimeSpan TimeRemaining { get; internal set; } |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
using DotSDL.Power; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
namespace DotSDL.Sdl { |
||||
/// <summary> |
||||
/// Contains the necessary constants and function imports from SDL_power.h. |
||||
/// </summary> |
||||
internal static class Power { |
||||
/// <summary> |
||||
/// Get the current power supply details. |
||||
/// </summary> |
||||
/// <param name="secs">Seconds of battery life left. You can pass a NULL |
||||
/// here if you don't care. Will return -1 if we can't determine a value, |
||||
/// or we're not running on a battery.</param> |
||||
/// <param name="pct">Percentage of battery life left, between 0 and 100. |
||||
/// You can pass a NULL here if you don't care. Will return -1 if we |
||||
/// can't determine a value, or we're not running on a battery.</param> |
||||
/// <returns>The state of the battery (if any).</returns> |
||||
[DllImport(Meta.DllName, EntryPoint = "SDL_GetPowerInfo", CallingConvention = CallingConvention.Cdecl)] |
||||
internal static extern BatteryStatus GetPowerInfo(out int secs, out int pct); |
||||
} |
||||
} |
@ -0,0 +1,10 @@
|
||||
using System; |
||||
|
||||
namespace Sample.Power { |
||||
class Program { |
||||
static void Main(string[] args) { |
||||
var window = new Window(256, 128); |
||||
window.Start(100, 16); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
<PropertyGroup> |
||||
<OutputType>Exe</OutputType> |
||||
<TargetFramework>netcoreapp2.0</TargetFramework> |
||||
</PropertyGroup> |
||||
<ItemGroup> |
||||
<ProjectReference Include="..\..\DotSDL\DotSDL.csproj" /> |
||||
</ItemGroup> |
||||
</Project> |
@ -0,0 +1,24 @@
|
||||
using System; |
||||
using DotSDL.Events; |
||||
using DotSDL.Graphics; |
||||
using DotSDL.Input.Keyboard; |
||||
|
||||
namespace Sample.Power { |
||||
public class Window : SdlWindow { |
||||
public Window(int width, int height) : base("Power Test", |
||||
new Point { X = WindowPosUndefined, Y = WindowPosUndefined }, |
||||
width, height, width, height) { |
||||
KeyPressed += Window_KeyPressed; |
||||
} |
||||
|
||||
private void Window_KeyPressed(object sender, KeyboardEvent e) { |
||||
if(e.Keycode == Keycode.Escape) |
||||
Stop(); |
||||
|
||||
if(e.Keycode == Keycode.P) { |
||||
var power = DotSDL.Power.PowerState.CurrentPowerState; |
||||
Console.WriteLine($"Status: {power.BatteryStatus}; percent: {power.BatteryPercent}; minutes: {power.TimeRemaining.TotalMinutes}"); |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue