don't lock up on exit in cectray when receiving log messages while the window is...
[deb_libcec.git] / src / LibCecTray / ui / CECTray.cs
index 6703e5f8d76e6a9a22c825678bf7b7ccb0a4fcd8..197ce32818c0d7537d2349f2f4102ce4b58f0854 100644 (file)
@@ -38,6 +38,9 @@ using LibCECTray.Properties;
 using LibCECTray.controller;
 using LibCECTray.controller.applications;
 using LibCECTray.settings;
+using Microsoft.Win32;
+using System.Security.Permissions;
+using System.Runtime.InteropServices;
 
 namespace LibCECTray.ui
 {
@@ -70,6 +73,91 @@ namespace LibCECTray.ui
                          else
                            OnShow();
                        };
+
+      SystemEvents.SessionEnding += new SessionEndingEventHandler(OnSessionEnding);
+    }
+
+    public void OnSessionEnding(object sender, SessionEndingEventArgs e)
+    {
+      Controller.CECActions.SuppressUpdates = true;
+      Controller.Close();
+    }
+
+    #region Power state change window messages
+    private const int WM_POWERBROADCAST      = 0x0218;
+    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");
+
+    [StructLayout(LayoutKind.Sequential, Pack = 4)]
+    internal struct POWERBROADCAST_SETTING
+    {
+      public Guid PowerSetting;
+      public uint DataLength;
+      public byte Data;
+    }
+    #endregion
+
+    /// <summary>
+    /// Check for power state changes, and pass up when it's something we don't care about
+    /// </summary>
+    /// <param name="msg">The incoming window message</param>
+    protected override void WndProc(ref Message msg)
+    {
+      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 OnWake()
+    {
+      Controller.Initialise();
+    }
+
+    private void OnSleep()
+    {
+      Controller.Close();
     }
 
     public override sealed string Text
@@ -78,7 +166,7 @@ namespace LibCECTray.ui
       set { base.Text = value; }
     }
 
-    private void CECTrayLoad(object sender, EventArgs e)
+    public void Initialise()
     {
       Controller.Initialise();
     }
@@ -86,8 +174,10 @@ namespace LibCECTray.ui
     protected override void Dispose(bool disposing)
     {
       Hide();
+      SuppressLogUpdates = true;
       if (disposing)
       {
+        Controller.CECActions.SuppressUpdates = true;
         Controller.Close();
       }
       if (disposing && (components != null))
@@ -222,6 +312,9 @@ namespace LibCECTray.ui
     delegate void UpdateLogCallback();
     private void UpdateLog()
     {
+      if (SuppressLogUpdates)
+        return;
+
       if (tbLog.InvokeRequired)
       {
         UpdateLogCallback d = UpdateLog;
@@ -479,12 +572,16 @@ namespace LibCECTray.ui
     #endregion
 
     #region Class members
+    private bool SuppressLogUpdates = false;
     private ConfigTab _selectedTab = ConfigTab.Configuration;
     private string _log = string.Empty;
     private CECController _controller;
     public CECController Controller
     {
-      get { return _controller ?? (_controller = new CECController(this)); }
+      get
+      {
+        return _controller ?? (_controller = new CECController(this));
+      }
     }
     public Control.ControlCollection TabControls
     {