Windows Mobile Video Power States

All in all, development on Klaxon has given me some great insight into how power states affect program execution. One of the changes I am making to the newest version of Klaxon is to turn the device's video off when the user hits snooze.

Since the earliest versions of Klaxon, I have been using the SystemIdleTimerReset call to prevent the device from going to sleep. This behavior is necessary because the application needs to continue running to pick up sensor events. However, using SystemIdleTimerReset has the unintended effect of leaving the display on: it should be turned off so as not to drain the battery or have a distracting bright light on in the room while the user is trying to sleep. So I did some searching to see if there was a way to explicitly turn off the video, but allow program execution to continue. The behavior I wanted is similar to how Windows Media Player can turn off the video but the songs keep playing, and the device does not go to sleep until Windows Media Player is stopped.

It turns out that Windows Mobile has several video power states, which are described quite nicely in an MSDN article. I ended up wrapping these native power management calls into a bow tied package for usage in C# for your reusing pleasure:

using System;
using System.Runtime.InteropServices;

namespace WindowsMobile.Utilities
{
public enum VideoPowerState
{
VideoPowerOn = 1,
VideoPowerStandBy,
VideoPowerSuspend,
VideoPowerOff
};

public static class Device
{
const int SETPOWERMANAGEMENT = 6147;
const int GETPOWERMANAGEMENT = 6148;

[DllImport("coredll")]
extern static IntPtr GetDC(IntPtr hwnd);

[DllImport("coredll")]
extern static int ExtEscape(IntPtr hdc, int nEscape, int cbInput, ref VideoPowerManagement vpm, int zero, IntPtr empty);

[DllImport("coredll")]
extern static int ExtEscape(IntPtr hdc, int nEscape, int zero, IntPtr empty, int cbOutput, ref VideoPowerManagement outData);

struct VideoPowerManagement
{
public int Length;
public int DPMSVersion;
public VideoPowerState PowerState;
}

public static VideoPowerState VideoPowerState
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
VideoPowerManagement ret = new VideoPowerManagement();
ExtEscape(hdc, GETPOWERMANAGEMENT, 0, IntPtr.Zero, 12, ref ret);
return ret.PowerState;
}
set
{
IntPtr hdc = GetDC(IntPtr.Zero);
VideoPowerManagement vpm = new VideoPowerManagement();
vpm.Length = 12;
vpm.DPMSVersion = 1;
vpm.PowerState = value;
ExtEscape(hdc, SETPOWERMANAGEMENT, vpm.Length, ref vpm, 0, IntPtr.Zero);
}
}
}
}

Usage:

Just get or set the Device.VideoPowerState property to do whatever you need!

0 comments: