cec: added a callback that is called when libCEC's configuration changed.
[deb_libcec.git] / src / cec-config-gui / CecConfigGUI.cs
index 031c7e8fe4cb32f6506ae1a19b1329a1cd620719..e41c292be80901d16658a6f00ad4189a54cb2bc3 100644 (file)
@@ -35,7 +35,7 @@ namespace CecConfigGui
       CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
       if (adapters.Length == 0 || !Lib.Open(adapters[0].ComPort, 10000))
       {
-        MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
+        MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration and try again.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
         Application.Exit();
       }
 
@@ -222,6 +222,12 @@ namespace CecConfigGui
       this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecSharp.CecKeypress() { Keycode = 0x91 }), string.Empty));
     }
 
+    public int ConfigurationChanged(LibCECConfiguration config)
+    {
+      SetControlText(this.tbPhysicalAddress, String.Format("{0,4:X}", config.PhysicalAddress));
+      return 1;
+    }
+
     public int ReceiveCommand(CecCommand command)
     {
       return 1;
@@ -348,14 +354,25 @@ namespace CecConfigGui
       SetControlEnabled(cbPortNumber, val);
       SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
       SetControlEnabled(tbPhysicalAddress, val);
-      SetControlEnabled(cbDeviceType, val);
+      SetControlEnabled(cbDeviceType, false); // TODO not implemented yet
       SetControlEnabled(cbUseTVMenuLanguage, val);
       SetControlEnabled(cbPowerOnStartup, val);
       SetControlEnabled(cbPowerOffShutdown, val);
       SetControlEnabled(cbPowerOffScreensaver, val);
       SetControlEnabled(cbPowerOffOnStandby, val);
+      SetControlEnabled(cbWakeDevices, false); // TODO not implemented yet
       SetControlEnabled(bClose, val);
       SetControlEnabled(bSave, val);
+
+      SetControlEnabled(bSendImageViewOn, val);
+      SetControlEnabled(bStandby, val);
+      SetControlEnabled(bActivateSource, val);
+      SetControlEnabled(bScan, val);
+
+      bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
+      SetControlEnabled(bVolUp, enableVolumeButtons);
+      SetControlEnabled(bVolDown, enableVolumeButtons);
+      SetControlEnabled(bMute, enableVolumeButtons);
     }
 
     delegate void SetControlTextCallback(Control control, string val);
@@ -530,21 +547,26 @@ namespace CecConfigGui
     private CecCallbackWrapper Callbacks;
     private UpdateProcess ActiveProcess = null;
 
-    private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
+    public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
     {
       if (ActiveProcess == null)
       {
         SetControlsEnabled(false);
-        SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
-        int iPortNumber = 0;
-        if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
-          iPortNumber = 1;
-        ActiveProcess = new UpdateConnectedDevice(ref Lib, cbConnectedDevice.Text.Equals(AVRVendorString) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv, iPortNumber);
+        ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber);
         ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
         (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
       }
     }
 
+    private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
+    {
+      SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
+      int iPortNumber = 0;
+      if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
+        iPortNumber = 1;
+      SetConnectedDevice(SelectedConnectedDevice, iPortNumber);
+    }
+
     private void bCancel_Click(object sender, EventArgs e)
     {
       this.Dispose();
@@ -621,15 +643,10 @@ namespace CecConfigGui
       SetControlsEnabled(true);
     }
 
-    private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
+    public void SetPhysicalAddress(ushort physicalAddress)
     {
       if (ActiveProcess == null)
       {
-        if (tbPhysicalAddress.Text.Length != 4)
-          return;
-        ushort physicalAddress = 0;
-        if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
-          return;
         SetControlsEnabled(false);
         SetControlText(cbPortNumber, string.Empty);
         SetControlText(cbConnectedDevice, string.Empty);
@@ -639,6 +656,17 @@ namespace CecConfigGui
       }
     }
 
+    private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
+    {
+      if (tbPhysicalAddress.Text.Length != 4)
+        return;
+      ushort physicalAddress = 0;
+      if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
+        return;
+
+      SetPhysicalAddress(physicalAddress);
+    }
+
     private void bClearLog_Click(object sender, EventArgs e)
     {
       tbLog.Text = string.Empty;
@@ -681,6 +709,153 @@ namespace CecConfigGui
       if (data == null || !data.Enabled)
         e.CellStyle.ForeColor = Color.Gray;
     }
+
+    delegate CecLogicalAddress GetTargetDeviceCallback();
+    private CecLogicalAddress GetTargetDevice()
+    {
+      if (this.cbCommandDestination.InvokeRequired)
+      {
+        GetTargetDeviceCallback d = new GetTargetDeviceCallback(GetTargetDevice);
+        CecLogicalAddress retval = CecLogicalAddress.Unknown;
+        try
+        {
+          retval = (CecLogicalAddress)this.Invoke(d, new object[] { });
+        }
+        catch (Exception) { }
+        return retval;
+      }
+
+      switch (this.cbCommandDestination.Text.Substring(0, 1).ToLower())
+      {
+        case "0":
+          return CecLogicalAddress.Tv;
+        case "1":
+          return CecLogicalAddress.RecordingDevice1;
+        case "2":
+          return CecLogicalAddress.RecordingDevice2;
+        case "3":
+          return CecLogicalAddress.Tuner1;
+        case "4":
+          return CecLogicalAddress.PlaybackDevice1;
+        case "5":
+          return CecLogicalAddress.AudioSystem;
+        case "6":
+          return CecLogicalAddress.Tuner2;
+        case "7":
+          return CecLogicalAddress.Tuner3;
+        case "8":
+          return CecLogicalAddress.PlaybackDevice2;
+        case "9":
+          return CecLogicalAddress.RecordingDevice3;
+        case "a":
+          return CecLogicalAddress.Tuner4;
+        case "b":
+          return CecLogicalAddress.PlaybackDevice3;
+        case "c":
+          return CecLogicalAddress.Reserved1;
+        case "d":
+          return CecLogicalAddress.Reserved2;
+        case "e":
+          return CecLogicalAddress.FreeUse;
+        case "f":
+          return CecLogicalAddress.Broadcast;
+        default:
+          return CecLogicalAddress.Unknown;
+      }
+    }
+
+    public void SendImageViewOn(CecLogicalAddress address)
+    {
+      if (ActiveProcess == null)
+      {
+        SetControlsEnabled(false);
+        ActiveProcess = new SendImageViewOn(ref Lib, address);
+        ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
+        (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
+      }
+    }
+
+    private void bSendImageViewOn_Click(object sender, EventArgs e)
+    {
+      SendImageViewOn(GetTargetDevice());
+    }
+
+    public void SendStandby(CecLogicalAddress address)
+    {
+      if (ActiveProcess == null)
+      {
+        SetControlsEnabled(false);
+        ActiveProcess = new SendStandby(ref Lib, address);
+        ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
+        (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
+      }
+    }
+
+    private void bStandby_Click(object sender, EventArgs e)
+    {
+      SendStandby(GetTargetDevice());
+    }
+
+    public void ShowDeviceInfo(CecLogicalAddress address)
+    {
+      if (ActiveProcess == null)
+      {
+        SetControlsEnabled(false);
+        ActiveProcess = new ShowDeviceInfo(this, ref Lib, address);
+        ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
+        (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
+      }
+    }
+
+    private void bScan_Click(object sender, EventArgs e)
+    {
+      ShowDeviceInfo(GetTargetDevice());
+    }
+
+    public void ActivateSource(CecLogicalAddress address)
+    {
+      if (ActiveProcess == null)
+      {
+        SetControlsEnabled(false);
+        ActiveProcess = new SendActivateSource(ref Lib, address);
+        ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
+        (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
+      }
+    }
+
+    private void bActivateSource_Click(object sender, EventArgs e)
+    {
+      ActivateSource(GetTargetDevice());
+    }
+
+    private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e)
+    {
+      bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
+      this.bVolUp.Enabled = enableVolumeButtons;
+      this.bVolDown.Enabled = enableVolumeButtons;
+      this.bMute.Enabled = enableVolumeButtons;
+    }
+
+    private void bVolUp_Click(object sender, EventArgs e)
+    {
+      SetControlsEnabled(false);
+      Lib.VolumeUp(true);
+      SetControlsEnabled(true);
+    }
+
+    private void bVolDown_Click(object sender, EventArgs e)
+    {
+      SetControlsEnabled(false);
+      Lib.VolumeDown(true);
+      SetControlsEnabled(true);
+    }
+
+    private void bMute_Click(object sender, EventArgs e)
+    {
+      SetControlsEnabled(false);
+      Lib.MuteAudio(true);
+      SetControlsEnabled(true);
+    }
   }
 
   internal class CecCallbackWrapper : CecCallbackMethods
@@ -705,6 +880,11 @@ namespace CecConfigGui
       return Gui.ReceiveLogMessage(message);
     }
 
+    public override int ConfigurationChanged(LibCECConfiguration config)
+    {
+      return Gui.ConfigurationChanged(config);
+    }
+
     private CecConfigGUI Gui;
   }
 }