cec: added a callback that is called when libCEC's configuration changed.
[deb_libcec.git] / src / cec-config-gui / CecConfigGUI.cs
index 15d3f0de7fb684cd566c1e90ee81bbfa90c4b8de..e41c292be80901d16658a6f00ad4189a54cb2bc3 100644 (file)
@@ -10,6 +10,7 @@ using CecSharp;
 using CecConfigGui.actions;
 using System.Globalization;
 using System.IO;
+using System.Xml;
 
 namespace CecConfigGui
 {
@@ -24,15 +25,209 @@ namespace CecConfigGui
       Config.ClientVersion = CecClientVersion.Version1_5_0;
       Callbacks = new CecCallbackWrapper(this);
       Config.SetCallbacks(Callbacks);
+      LoadXMLConfiguration(ref Config);
+      Lib = new LibCecSharp(Config);
 
       InitializeComponent();
-      Lib = new LibCecSharp(Config);
+      LoadButtonConfiguration();
+
+      //TODO read the com port setting from the configuration
+      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 and try again.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
+        Application.Exit();
+      }
 
-      ActiveProcess = new ConnectToDevice(ref Lib);
+      ActiveProcess = new ConnectToDevice(ref Lib, Config);
       ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
       (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
     }
 
+    private bool LoadXMLConfiguration(ref LibCECConfiguration config)
+    {
+      bool gotConfig = false;
+      string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
+      string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
+      string file = defaultDir + @"\usb_2548_1001.xml";
+      if (File.Exists(xbmcDir + @"\usb_2548_1001.xml"))
+        file = xbmcDir + @"\usb_2548_1001.xml";
+
+      if (File.Exists(file))
+      {
+        XmlTextReader reader = new XmlTextReader(file);
+        while (reader.Read())
+        {
+          gotConfig = true;
+          switch (reader.NodeType)
+          {
+            case XmlNodeType.Element:
+              if (reader.Name.ToLower() == "setting")
+              {
+                string name = string.Empty;
+                string value = string.Empty;
+
+                while (reader.MoveToNextAttribute())
+                {
+                  if (reader.Name.ToLower().Equals("id"))
+                    name = reader.Value.ToLower();
+                  if (reader.Name.ToLower().Equals("value"))
+                    value = reader.Value;
+                }
+
+                switch (name)
+                {
+                  case "cec_hdmi_port":
+                    {
+                      byte iPort;
+                      if (byte.TryParse(value, out iPort))
+                        config.HDMIPort = iPort;
+                    }
+                    break;
+                  case "connected_device":
+                    {
+                      ushort iDevice;
+                      if (ushort.TryParse(value, out iDevice))
+                        config.BaseDevice = (CecLogicalAddress)iDevice;
+                    }
+                    break;
+                  case "physical_address":
+                    {
+                      ushort physicalAddress = 0;
+                      if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
+                        config.PhysicalAddress = physicalAddress;
+                    }
+                    break;
+                  case "device_type":
+                    {
+                      ushort iType;
+                      if (ushort.TryParse(value, out iType))
+                        config.DeviceTypes.Types[0] = (CecDeviceType)iType;
+                    }
+                    break;
+                  case "cec_power_on_startup":
+                    config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
+                    break;
+                  case "cec_power_off_shutdown":
+                    config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
+                    break;
+                  case "cec_standby_screensaver":
+                    config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
+                    break;
+                  case "standby_pc_on_tv_standby":
+                    config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
+                    break;
+                  case "use_tv_menu_language":
+                    config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
+                    break;
+                  case "enabled":
+                    break;
+                  case "port":
+                    //TODO
+                    break;
+                  default:
+                    break;
+                }
+              }
+              break;
+            default:
+              break;
+          }
+        }
+      }
+      return gotConfig;
+    }
+
+    private void LoadButtonConfiguration()
+    {
+      //TODO load the real configuration
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select", (new CecSharp.CecKeypress() { Keycode = 0x00 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Up", (new CecSharp.CecKeypress() { Keycode = 0x01 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Down", (new CecSharp.CecKeypress() { Keycode = 0x02 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left", (new CecSharp.CecKeypress() { Keycode = 0x03 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right", (new CecSharp.CecKeypress() { Keycode = 0x04 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Up", (new CecSharp.CecKeypress() { Keycode = 0x05 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Down", (new CecSharp.CecKeypress() { Keycode = 0x06 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Up", (new CecSharp.CecKeypress() { Keycode = 0x07 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Down", (new CecSharp.CecKeypress() { Keycode = 0x08 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Root menu", (new CecSharp.CecKeypress() { Keycode = 0x09 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Setup menu", (new CecSharp.CecKeypress() { Keycode = 0x0A }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Contents menu", (new CecSharp.CecKeypress() { Keycode = 0x0B }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Favourite menu", (new CecSharp.CecKeypress() { Keycode = 0x0C }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Exit", (new CecSharp.CecKeypress() { Keycode = 0x0D }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("0", (new CecSharp.CecKeypress() { Keycode = 0x20 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("1", (new CecSharp.CecKeypress() { Keycode = 0x21 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("2", (new CecSharp.CecKeypress() { Keycode = 0x22 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("3", (new CecSharp.CecKeypress() { Keycode = 0x23 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("4", (new CecSharp.CecKeypress() { Keycode = 0x24 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("5", (new CecSharp.CecKeypress() { Keycode = 0x25 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("6", (new CecSharp.CecKeypress() { Keycode = 0x26 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("7", (new CecSharp.CecKeypress() { Keycode = 0x27 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("8", (new CecSharp.CecKeypress() { Keycode = 0x28 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("9", (new CecSharp.CecKeypress() { Keycode = 0x29 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem(".", (new CecSharp.CecKeypress() { Keycode = 0x2A }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Enter", (new CecSharp.CecKeypress() { Keycode = 0x2B }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Clear", (new CecSharp.CecKeypress() { Keycode = 0x2C }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Next favourite", (new CecSharp.CecKeypress() { Keycode = 0x2F }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel up", (new CecSharp.CecKeypress() { Keycode = 0x30 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel down", (new CecSharp.CecKeypress() { Keycode = 0x31 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Previous channel", (new CecSharp.CecKeypress() { Keycode = 0x32 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sound select", (new CecSharp.CecKeypress() { Keycode = 0x33 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Input select", (new CecSharp.CecKeypress() { Keycode = 0x34 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Display information", (new CecSharp.CecKeypress() { Keycode = 0x35 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Help", (new CecSharp.CecKeypress() { Keycode = 0x36 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page up", (new CecSharp.CecKeypress() { Keycode = 0x37 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page down", (new CecSharp.CecKeypress() { Keycode = 0x38 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power", (new CecSharp.CecKeypress() { Keycode = 0x40 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume up", (new CecSharp.CecKeypress() { Keycode = 0x41 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume down", (new CecSharp.CecKeypress() { Keycode = 0x42 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute", (new CecSharp.CecKeypress() { Keycode = 0x43 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play", (new CecSharp.CecKeypress() { Keycode = 0x44 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop", (new CecSharp.CecKeypress() { Keycode = 0x45 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause", (new CecSharp.CecKeypress() { Keycode = 0x46 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record", (new CecSharp.CecKeypress() { Keycode = 0x47 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Rewind", (new CecSharp.CecKeypress() { Keycode = 0x48 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Fast forward", (new CecSharp.CecKeypress() { Keycode = 0x49 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Eject", (new CecSharp.CecKeypress() { Keycode = 0x4A }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Forward", (new CecSharp.CecKeypress() { Keycode = 0x4B }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Backward", (new CecSharp.CecKeypress() { Keycode = 0x4C }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop record", (new CecSharp.CecKeypress() { Keycode = 0x4D }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record", (new CecSharp.CecKeypress() { Keycode = 0x4E }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Angle", (new CecSharp.CecKeypress() { Keycode = 0x50 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sub picture", (new CecSharp.CecKeypress() { Keycode = 0x51 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Video on demand", (new CecSharp.CecKeypress() { Keycode = 0x52 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Electronic program guide", (new CecSharp.CecKeypress() { Keycode = 0x53 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Timer programming", (new CecSharp.CecKeypress() { Keycode = 0x54 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Initial configuration", (new CecSharp.CecKeypress() { Keycode = 0x55 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play (function)", (new CecSharp.CecKeypress() { Keycode = 0x60 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause play (function)", (new CecSharp.CecKeypress() { Keycode = 0x61 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record (function)", (new CecSharp.CecKeypress() { Keycode = 0x62 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record (function)", (new CecSharp.CecKeypress() { Keycode = 0x63 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop (function)", (new CecSharp.CecKeypress() { Keycode = 0x64 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute (function)", (new CecSharp.CecKeypress() { Keycode = 0x65 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Restore volume", (new CecSharp.CecKeypress() { Keycode = 0x66 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Tune", (new CecSharp.CecKeypress() { Keycode = 0x67 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select media", (new CecSharp.CecKeypress() { Keycode = 0x68 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select AV input", (new CecSharp.CecKeypress() { Keycode = 0x69 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select audio input", (new CecSharp.CecKeypress() { Keycode = 0x6A }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power toggle", (new CecSharp.CecKeypress() { Keycode = 0x6B }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power off", (new CecSharp.CecKeypress() { Keycode = 0x6C }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power on", (new CecSharp.CecKeypress() { Keycode = 0x6D }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F1 (blue)", (new CecSharp.CecKeypress() { Keycode = 0x71 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F2 (red)", (new CecSharp.CecKeypress() { Keycode = 0x72 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F3 (green)", (new CecSharp.CecKeypress() { Keycode = 0x73 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F4 (yellow)", (new CecSharp.CecKeypress() { Keycode = 0x74 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F5", (new CecSharp.CecKeypress() { Keycode = 0x75 }), string.Empty));
+      this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Data", (new CecSharp.CecKeypress() { Keycode = 0x76 }), string.Empty));
+      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;
@@ -40,9 +235,44 @@ namespace CecConfigGui
 
     public int ReceiveKeypress(CecKeypress key)
     {
+      SelectKeypressRow(key);
       return 1;
     }
 
+    delegate void SelectKeypressRowCallback(CecKeypress key);
+    private void SelectKeypressRow(CecKeypress key)
+    {
+      if (dgButtons.InvokeRequired)
+      {
+        SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
+        try
+        {
+          this.Invoke(d, new object[] { key });
+        }
+        catch (Exception) { }
+      }
+      else
+      {
+        int rowIndex = -1;
+        foreach (DataGridViewRow row in dgButtons.Rows)
+        {
+          CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
+          if (item != null && item.Key.Keycode == key.Keycode)
+          {
+            rowIndex = row.Index;
+            row.Selected = true;
+            item.Enabled = true;
+          }
+          else
+          {
+            row.Selected = false;
+          }
+        }
+        if (rowIndex > -1)
+          dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
+      }
+    }
+
     delegate void AddLogMessageCallback(CecLogMessage message);
     private void AddLogMessage(CecLogMessage message)
     {
@@ -57,31 +287,40 @@ namespace CecConfigGui
       }
       else
       {
-        if (((int)message.Level & LogLevel) == (int)message.Level)
+        string strLevel = "";
+        bool display = false;
+        switch (message.Level)
+        {
+          case CecLogLevel.Error:
+            strLevel = "ERROR:   ";
+            display = cbLogError.Checked;
+            break;
+          case CecLogLevel.Warning:
+            strLevel = "WARNING: ";
+            display = cbLogWarning.Checked;
+            break;
+          case CecLogLevel.Notice:
+            strLevel = "NOTICE:  ";
+            display = cbLogNotice.Checked;
+            break;
+          case CecLogLevel.Traffic:
+            strLevel = "TRAFFIC: ";
+            display = cbLogTraffic.Checked;
+            break;
+          case CecLogLevel.Debug:
+            strLevel = "DEBUG:   ";
+            display = cbLogDebug.Checked;
+            break;
+          default:
+            break;
+        }
+
+        if (display)
         {
-          string strLevel = "";
-          switch (message.Level)
-          {
-            case CecLogLevel.Error:
-              strLevel = "ERROR:   ";
-              break;
-            case CecLogLevel.Warning:
-              strLevel = "WARNING: ";
-              break;
-            case CecLogLevel.Notice:
-              strLevel = "NOTICE:  ";
-              break;
-            case CecLogLevel.Traffic:
-              strLevel = "TRAFFIC: ";
-              break;
-            case CecLogLevel.Debug:
-              strLevel = "DEBUG:   ";
-              break;
-            default:
-              break;
-          }
           string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
           tbLog.Text += strLog;
+          tbLog.Select(tbLog.Text.Length, 0);
+          tbLog.ScrollToCaret();
         }
       }
     }
@@ -113,16 +352,27 @@ namespace CecConfigGui
     private void SetControlsEnabled(bool val)
     {
       SetControlEnabled(cbPortNumber, val);
-      SetControlEnabled(cbConnectedDevice, 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);
@@ -226,6 +476,9 @@ namespace CecConfigGui
         case UpdateEventType.HDMIPort:
           Config.HDMIPort = (byte)updateEvent.IntValue;
           break;
+        case UpdateEventType.MenuLanguage:
+          SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
+          break;
         case UpdateEventType.HasAVRDevice:
           if (HasAVRDevice != updateEvent.BoolValue)
           {
@@ -288,28 +541,32 @@ namespace CecConfigGui
     protected CecVendorId TVVendor = CecVendorId.Unknown;
     protected CecVendorId AVRVendor = CecVendorId.Unknown;
     protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
-    protected int LogLevel = (int)CecLogLevel.All;
 
     protected LibCECConfiguration Config;
     protected LibCecSharp Lib;
     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();
@@ -386,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);
@@ -403,6 +655,207 @@ namespace CecConfigGui
         (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
       }
     }
+
+    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;
+    }
+
+    private void bSaveLog_Click(object sender, EventArgs e)
+    {
+      SaveFileDialog dialog = new SaveFileDialog()
+      {
+        Title = "Where do you want to store the log file?",
+        InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
+        FileName = "cec-log.txt",
+        Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
+        FilterIndex = 1
+      };
+
+      if (dialog.ShowDialog() == DialogResult.OK)
+      {
+        FileStream fs = (FileStream)dialog.OpenFile();
+        if (fs == null)
+        {
+          MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
+        }
+        else
+        {
+          StreamWriter writer = new StreamWriter(fs);
+          writer.Write(tbLog.Text);
+          writer.Close();
+          fs.Close();
+          fs.Dispose();
+          MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
+        }
+      }
+    }
+
+    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
+    {
+      DataGridView grid = sender as DataGridView;
+      CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
+      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
@@ -427,6 +880,11 @@ namespace CecConfigGui
       return Gui.ReceiveLogMessage(message);
     }
 
+    public override int ConfigurationChanged(LibCECConfiguration config)
+    {
+      return Gui.ConfigurationChanged(config);
+    }
+
     private CecConfigGUI Gui;
   }
 }