Browse Source

WIP: Implementing the SDL power mangement functions.

* 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
parent
commit
d31ae8e1bf
  1. 11
      DotSDL.sln
  2. 31
      DotSDL/Power/BatteryStatus.cs
  3. 25
      DotSDL/Power/PowerState.cs
  4. 21
      DotSDL/Power/PowerStatus.cs
  5. 22
      DotSDL/Sdl/Power.cs
  6. 2
      README.md
  7. 10
      Samples/Sample.Power/Program.cs
  8. 9
      Samples/Sample.Power/Sample.Power.csproj
  9. 24
      Samples/Sample.Power/Window.cs

11
DotSDL.sln

@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.BasicPixels", "Sampl
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Audio", "Samples\Sample.Audio\Sample.Audio.csproj", "{B71D69F1-3BB1-4CC8-AB6A-2A0F2DAE9FE5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Power", "Samples\Sample.Power\Sample.Power.csproj", "{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@ -49,6 +51,14 @@ Global
{B71D69F1-3BB1-4CC8-AB6A-2A0F2DAE9FE5}.Release|x64.Build.0 = Release|Any CPU
{B71D69F1-3BB1-4CC8-AB6A-2A0F2DAE9FE5}.Release|x86.ActiveCfg = Release|Any CPU
{B71D69F1-3BB1-4CC8-AB6A-2A0F2DAE9FE5}.Release|x86.Build.0 = Release|Any CPU
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Debug|x64.ActiveCfg = Debug|Any CPU
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Debug|x64.Build.0 = Debug|Any CPU
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Debug|x86.ActiveCfg = Debug|Any CPU
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Debug|x86.Build.0 = Debug|Any CPU
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Release|x64.ActiveCfg = Release|Any CPU
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Release|x64.Build.0 = Release|Any CPU
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Release|x86.ActiveCfg = Release|Any CPU
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -56,5 +66,6 @@ Global
GlobalSection(NestedProjects) = preSolution
{51055215-1F3D-436F-B88C-02A5C58C6989} = {70DA3135-B76E-421D-B9CF-E49CD6440B0A}
{B71D69F1-3BB1-4CC8-AB6A-2A0F2DAE9FE5} = {70DA3135-B76E-421D-B9CF-E49CD6440B0A}
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40} = {70DA3135-B76E-421D-B9CF-E49CD6440B0A}
EndGlobalSection
EndGlobal

31
DotSDL/Power/BatteryStatus.cs

@ -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
}
}

25
DotSDL/Power/PowerState.cs

@ -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)
};
}
}
}
}

21
DotSDL/Power/PowerStatus.cs

@ -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; }
}
}

22
DotSDL/Sdl/Power.cs

@ -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);
}
}

2
README.md

@ -18,6 +18,8 @@ At this time, DotSDL supports the following features:
* Window events.
* Graphics
* A single 32-bit ARGB canvas (useful for simple pixel plotting).
* Power
* Battery state.
### How To Use DotSDL

10
Samples/Sample.Power/Program.cs

@ -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);
}
}
}

9
Samples/Sample.Power/Sample.Power.csproj

@ -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>

24
Samples/Sample.Power/Window.cs

@ -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…
Cancel
Save