listen to screensaver events in cectray. bugzid: 1375
authorLars Op den Kamp <lars@opdenkamp.eu>
Fri, 13 Dec 2013 12:36:37 +0000 (13:36 +0100)
committerLars Op den Kamp <lars@opdenkamp.eu>
Fri, 13 Dec 2013 14:45:04 +0000 (15:45 +0100)
src/LibCecTray/ui/CECTray.cs

index 740033b386539f65670bf317872fbe0116c04fa1..bf4e05a746af9a0282af435e19f08a4d289767eb 100644 (file)
@@ -54,6 +54,11 @@ namespace LibCECTray.ui
     {
       Text = Resources.app_name;
       InitializeComponent();
+
+      _sstimer.Interval = 1000;
+      _sstimer.Tick += ScreensaverActiveCheck;
+      _sstimer.Enabled = false;
+
       VisibleChanged += delegate
                        {
                          if (!Visible)
@@ -73,13 +78,22 @@ namespace LibCECTray.ui
 
     #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
     {
@@ -95,7 +109,14 @@ namespace LibCECTray.ui
     /// <param name="msg">The incoming window message</param>
     protected override void WndProc(ref Message msg)
     {
-      if (msg.Msg == WM_POWERBROADCAST)
+      if (msg.Msg == WM_SYSCOMMAND && (msg.WParam.ToInt32() & 0xfff0) == SC_SCREENSAVE)
+      {
+        // there's no event for screensaver exit
+        _sstimer.Enabled = true;
+        OnSleep();
+        return;
+      }
+      else if (msg.Msg == WM_POWERBROADCAST)
       {
         switch (msg.WParam.ToInt32())
         {
@@ -138,6 +159,22 @@ namespace LibCECTray.ui
       base.WndProc(ref msg);
     }
 
+    private void ScreensaverActiveCheck(object sender, EventArgs e)
+    {
+      if (!IsScreensaverActive())
+      {
+        _sstimer.Enabled = false;
+        OnWake();
+      }
+    }
+
+    private bool IsScreensaverActive()
+    {
+      int active = 1;
+      SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref active, 0);
+      return active == 1;
+    }
+
     private void OnWake()
     {
       Controller.Initialise();
@@ -575,6 +612,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)