X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=inline;f=src%2FLibCecTray%2Fui%2FCECTray.cs;h=3f7c09422aa0a86e9aa21ab2da6714c135ccdf44;hb=8fad0fe870edcd90e4788d8b56da80ab29c9e016;hp=6a04d62f2301da982ecb970cf78547c9031351bc;hpb=fec04a82ac869da26cd02a64be354cbd08ecdc1b;p=deb_libcec.git
diff --git a/src/LibCecTray/ui/CECTray.cs b/src/LibCecTray/ui/CECTray.cs
index 6a04d62..3f7c094 100644
--- a/src/LibCecTray/ui/CECTray.cs
+++ b/src/LibCecTray/ui/CECTray.cs
@@ -40,22 +40,11 @@ using LibCECTray.controller.applications;
using LibCECTray.settings;
using Microsoft.Win32;
using System.Security.Permissions;
+using System.Runtime.InteropServices;
+using System.Threading;
namespace LibCECTray.ui
{
- ///
- /// The tab pages in this application
- ///
- internal enum ConfigTab
- {
- Configuration,
- KeyConfiguration,
- Tester,
- Log,
- WMC,
- XBMC
- }
-
///
/// Main LibCecTray GUI
///
@@ -65,6 +54,11 @@ namespace LibCECTray.ui
{
Text = Resources.app_name;
InitializeComponent();
+
+ _sstimer.Interval = 5000;
+ _sstimer.Tick += ScreensaverActiveCheck;
+ _sstimer.Enabled = false;
+
VisibleChanged += delegate
{
if (!Visible)
@@ -72,30 +66,129 @@ namespace LibCECTray.ui
else
OnShow();
};
- SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChanged);
+
SystemEvents.SessionEnding += new SessionEndingEventHandler(OnSessionEnding);
}
public void OnSessionEnding(object sender, SessionEndingEventArgs e)
{
+ Controller.CECActions.SuppressUpdates = true;
Controller.Close();
}
- public void OnPowerModeChanged(Object sender, PowerModeChangedEventArgs e)
+ #region Power state change window messages
+ private const int WM_POWERBROADCAST = 0x0218;
+ private const int WM_SYSCOMMAND = 0x0112;
+
+ private const int PBT_APMSUSPEND = 0x0004;
+ private const int PBT_APMRESUMESUSPEND = 0x0007;
+ private const int PBT_APMRESUMECRITICAL = 0x0006;
+ private const int PBT_APMRESUMEAUTOMATIC = 0x0012;
+ private const int PBT_POWERSETTINGCHANGE = 0x8013;
+
+ private static Guid GUID_SYSTEM_AWAYMODE = new Guid("98a7f580-01f7-48aa-9c0f-44352c29e5c0");
+
+ private const int SC_SCREENSAVE = 0xF140;
+ private const int SPI_GETSCREENSAVERRUNNING = 0x0072;
+
+ [DllImport("user32.dll", SetLastError = true)]
+ static extern bool SystemParametersInfo(int action, int param, ref int retval, int updini);
+
+ [StructLayout(LayoutKind.Sequential, Pack = 4)]
+ internal struct POWERBROADCAST_SETTING
+ {
+ public Guid PowerSetting;
+ public uint DataLength;
+ public byte Data;
+ }
+ #endregion
+
+ ///
+ /// Check for power state changes, and pass up when it's something we don't care about
+ ///
+ /// The incoming window message
+ protected override void WndProc(ref Message msg)
{
- switch (e.Mode)
+ if (msg.Msg == WM_SYSCOMMAND && (msg.WParam.ToInt32() & 0xfff0) == SC_SCREENSAVE)
{
- case PowerModes.Resume:
- Controller.Initialise();
- break;
- case PowerModes.Suspend:
- Controller.Close();
- break;
- case PowerModes.StatusChange:
- break;
+ // there's no event for screensaver exit
+ if (!_sstimer.Enabled)
+ {
+ _sstimer.Enabled = true;
+ Controller.CECActions.SendStandby(CecLogicalAddress.Broadcast);
+ }
+ }
+ else if (msg.Msg == WM_POWERBROADCAST)
+ {
+ switch (msg.WParam.ToInt32())
+ {
+ case PBT_APMSUSPEND:
+ OnSleep();
+ return;
+
+ case PBT_APMRESUMESUSPEND:
+ case PBT_APMRESUMECRITICAL:
+ case PBT_APMRESUMEAUTOMATIC:
+ OnWake();
+ return;
+
+ case PBT_POWERSETTINGCHANGE:
+ {
+ POWERBROADCAST_SETTING pwr = (POWERBROADCAST_SETTING)Marshal.PtrToStructure(msg.LParam, typeof(POWERBROADCAST_SETTING));
+ if (pwr.PowerSetting == GUID_SYSTEM_AWAYMODE && pwr.DataLength == Marshal.SizeOf(typeof(Int32)))
+ {
+ switch (pwr.Data)
+ {
+ case 0:
+ // do _not_ wake the pc when away mode is deactivated
+ //OnWake();
+ //return;
+ case 1:
+ OnSleep();
+ return;
+ default:
+ break;
+ }
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ // pass up when not handled
+ base.WndProc(ref msg);
+ }
+
+ private void ScreensaverActiveCheck(object sender, EventArgs e)
+ {
+ if (!IsScreensaverActive())
+ {
+ _sstimer.Enabled = false;
+ Controller.CECActions.ActivateSource();
}
}
+ private bool IsScreensaverActive()
+ {
+ int active = 1;
+ SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref active, 0);
+ return active == 1;
+ }
+
+ private void OnWake()
+ {
+ Controller.Initialise();
+ }
+
+ private void OnSleep()
+ {
+ Controller.CECActions.SuppressUpdates = true;
+ AsyncDisconnect dc = new AsyncDisconnect(Controller);
+ (new Thread(dc.Process)).Start();
+ }
+
public override sealed string Text
{
get { return base.Text; }
@@ -112,7 +205,7 @@ namespace LibCECTray.ui
Hide();
if (disposing)
{
- Controller.Close();
+ OnSleep();
}
if (disposing && (components != null))
{
@@ -208,7 +301,7 @@ namespace LibCECTray.ui
private void BActivateSourceClick(object sender, EventArgs e)
{
- Controller.CECActions.ActivateSource(GetTargetDevice());
+ Controller.CECActions.SetStreamPath(GetTargetDevice());
}
private void CbCommandDestinationSelectedIndexChanged(object sender, EventArgs e)
@@ -521,6 +614,8 @@ namespace LibCECTray.ui
{
get { return GetSelectedTabName(tabPanel, tabPanel.TabPages); }
}
+
+ private System.Windows.Forms.Timer _sstimer = new System.Windows.Forms.Timer();
#endregion
private void AddNewApplicationToolStripMenuItemClick(object sender, EventArgs e)
@@ -529,4 +624,32 @@ namespace LibCECTray.ui
Controller.DisplayDialog(appConfig, false);
}
}
+
+ ///
+ /// The tab pages in this application
+ ///
+ internal enum ConfigTab
+ {
+ Configuration,
+ KeyConfiguration,
+ Tester,
+ Log,
+ WMC,
+ XBMC
+ }
+
+ class AsyncDisconnect
+ {
+ public AsyncDisconnect(CECController controller)
+ {
+ _controller = controller;
+ }
+
+ public void Process()
+ {
+ _controller.Close();
+ }
+
+ private CECController _controller;
+ }
}