| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.ComponentModel; |
| 4 | using System.Data; |
| 5 | using System.Drawing; |
| 6 | using System.Text; |
| 7 | using System.Windows.Forms; |
| 8 | using System.Threading; |
| 9 | using CecSharp; |
| 10 | using CecConfigGui.actions; |
| 11 | using System.Globalization; |
| 12 | using System.IO; |
| 13 | using System.Xml; |
| 14 | |
| 15 | namespace CecConfigGui |
| 16 | { |
| 17 | internal enum ConfigTab |
| 18 | { |
| 19 | Configuration, |
| 20 | KeyConfiguration, |
| 21 | Tester, |
| 22 | Log |
| 23 | } |
| 24 | |
| 25 | public partial class CecConfigGUI : AsyncForm |
| 26 | { |
| 27 | public CecConfigGUI() |
| 28 | { |
| 29 | Config = new LibCECConfiguration(); |
| 30 | Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice; |
| 31 | Config.DeviceName = "CEC Config"; |
| 32 | Config.GetSettingsFromROM = true; |
| 33 | Config.ClientVersion = CecClientVersion.Version1_5_0; |
| 34 | Callbacks = new CecCallbackWrapper(this); |
| 35 | Config.SetCallbacks(Callbacks); |
| 36 | LoadXMLConfiguration(ref Config); |
| 37 | Lib = new LibCecSharp(Config); |
| 38 | |
| 39 | InitializeComponent(); |
| 40 | LoadButtonConfiguration(); |
| 41 | |
| 42 | ActiveProcess = new ConnectToDevice(ref Lib, Config); |
| 43 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 44 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 45 | } |
| 46 | |
| 47 | private bool LoadXMLConfiguration(ref LibCECConfiguration config) |
| 48 | { |
| 49 | bool gotConfig = false; |
| 50 | string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data"; |
| 51 | string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); |
| 52 | string file = defaultDir + @"\usb_2548_1001.xml"; |
| 53 | if (File.Exists(xbmcDir + @"\usb_2548_1001.xml")) |
| 54 | file = xbmcDir + @"\usb_2548_1001.xml"; |
| 55 | |
| 56 | if (File.Exists(file)) |
| 57 | { |
| 58 | XmlTextReader reader = new XmlTextReader(file); |
| 59 | while (reader.Read()) |
| 60 | { |
| 61 | gotConfig = true; |
| 62 | switch (reader.NodeType) |
| 63 | { |
| 64 | case XmlNodeType.Element: |
| 65 | if (reader.Name.ToLower() == "setting") |
| 66 | { |
| 67 | string name = string.Empty; |
| 68 | string value = string.Empty; |
| 69 | |
| 70 | while (reader.MoveToNextAttribute()) |
| 71 | { |
| 72 | if (reader.Name.ToLower().Equals("id")) |
| 73 | name = reader.Value.ToLower(); |
| 74 | if (reader.Name.ToLower().Equals("value")) |
| 75 | value = reader.Value; |
| 76 | } |
| 77 | |
| 78 | switch (name) |
| 79 | { |
| 80 | case "cec_hdmi_port": |
| 81 | { |
| 82 | byte iPort; |
| 83 | if (byte.TryParse(value, out iPort)) |
| 84 | config.HDMIPort = iPort; |
| 85 | } |
| 86 | break; |
| 87 | case "connected_device": |
| 88 | { |
| 89 | ushort iDevice; |
| 90 | if (ushort.TryParse(value, out iDevice)) |
| 91 | config.BaseDevice = (CecLogicalAddress)iDevice; |
| 92 | } |
| 93 | break; |
| 94 | case "cec_power_on_startup": |
| 95 | if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes")) |
| 96 | { |
| 97 | config.ActivateSource = true; |
| 98 | config.WakeDevices.Set(CecLogicalAddress.Tv); |
| 99 | } |
| 100 | break; |
| 101 | case "cec_power_off_shutdown": |
| 102 | if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes")) |
| 103 | config.PowerOffDevices.Set(CecLogicalAddress.Broadcast); |
| 104 | break; |
| 105 | case "cec_standby_screensaver": |
| 106 | config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); |
| 107 | break; |
| 108 | case "standby_pc_on_tv_standby": |
| 109 | config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); |
| 110 | break; |
| 111 | case "use_tv_menu_language": |
| 112 | config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"); |
| 113 | break; |
| 114 | // 1.5.0+ settings |
| 115 | case "physical_address": |
| 116 | { |
| 117 | ushort physicalAddress = 0; |
| 118 | if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress)) |
| 119 | config.PhysicalAddress = physicalAddress; |
| 120 | } |
| 121 | break; |
| 122 | case "device_type": |
| 123 | { |
| 124 | ushort iType; |
| 125 | if (ushort.TryParse(value, out iType)) |
| 126 | config.DeviceTypes.Types[0] = (CecDeviceType)iType; |
| 127 | } |
| 128 | break; |
| 129 | case "tv_vendor": |
| 130 | { |
| 131 | UInt64 iVendor; |
| 132 | if (UInt64.TryParse(value, out iVendor)) |
| 133 | config.TvVendor = (CecVendorId)iVendor; |
| 134 | } |
| 135 | break; |
| 136 | case "wake_devices": |
| 137 | { |
| 138 | config.WakeDevices.Clear(); |
| 139 | string[] split = value.Split(new char[] { ' ' }); |
| 140 | foreach (string dev in split) |
| 141 | { |
| 142 | byte iLogicalAddress; |
| 143 | if (byte.TryParse(dev, out iLogicalAddress)) |
| 144 | config.WakeDevices.Set((CecLogicalAddress)iLogicalAddress); |
| 145 | } |
| 146 | } |
| 147 | break; |
| 148 | case "standby_devices": |
| 149 | { |
| 150 | config.PowerOffDevices.Clear(); |
| 151 | string[] split = value.Split(new char[] { ' ' }); |
| 152 | foreach (string dev in split) |
| 153 | { |
| 154 | byte iLogicalAddress; |
| 155 | if (byte.TryParse(dev, out iLogicalAddress)) |
| 156 | config.PowerOffDevices.Set((CecLogicalAddress)iLogicalAddress); |
| 157 | } |
| 158 | } |
| 159 | break; |
| 160 | case "enabled": |
| 161 | break; |
| 162 | case "port": |
| 163 | //TODO |
| 164 | break; |
| 165 | default: |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | break; |
| 170 | default: |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | return gotConfig; |
| 176 | } |
| 177 | |
| 178 | private void LoadButtonConfiguration() |
| 179 | { |
| 180 | //TODO load the real configuration |
| 181 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select", (new CecSharp.CecKeypress() { Keycode = 0x00 }), string.Empty)); |
| 182 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Up", (new CecSharp.CecKeypress() { Keycode = 0x01 }), string.Empty)); |
| 183 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Down", (new CecSharp.CecKeypress() { Keycode = 0x02 }), string.Empty)); |
| 184 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left", (new CecSharp.CecKeypress() { Keycode = 0x03 }), string.Empty)); |
| 185 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right", (new CecSharp.CecKeypress() { Keycode = 0x04 }), string.Empty)); |
| 186 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Up", (new CecSharp.CecKeypress() { Keycode = 0x05 }), string.Empty)); |
| 187 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Down", (new CecSharp.CecKeypress() { Keycode = 0x06 }), string.Empty)); |
| 188 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Up", (new CecSharp.CecKeypress() { Keycode = 0x07 }), string.Empty)); |
| 189 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Down", (new CecSharp.CecKeypress() { Keycode = 0x08 }), string.Empty)); |
| 190 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Root menu", (new CecSharp.CecKeypress() { Keycode = 0x09 }), string.Empty)); |
| 191 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Setup menu", (new CecSharp.CecKeypress() { Keycode = 0x0A }), string.Empty)); |
| 192 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Contents menu", (new CecSharp.CecKeypress() { Keycode = 0x0B }), string.Empty)); |
| 193 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Favourite menu", (new CecSharp.CecKeypress() { Keycode = 0x0C }), string.Empty)); |
| 194 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Exit", (new CecSharp.CecKeypress() { Keycode = 0x0D }), string.Empty)); |
| 195 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("0", (new CecSharp.CecKeypress() { Keycode = 0x20 }), string.Empty)); |
| 196 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("1", (new CecSharp.CecKeypress() { Keycode = 0x21 }), string.Empty)); |
| 197 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("2", (new CecSharp.CecKeypress() { Keycode = 0x22 }), string.Empty)); |
| 198 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("3", (new CecSharp.CecKeypress() { Keycode = 0x23 }), string.Empty)); |
| 199 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("4", (new CecSharp.CecKeypress() { Keycode = 0x24 }), string.Empty)); |
| 200 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("5", (new CecSharp.CecKeypress() { Keycode = 0x25 }), string.Empty)); |
| 201 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("6", (new CecSharp.CecKeypress() { Keycode = 0x26 }), string.Empty)); |
| 202 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("7", (new CecSharp.CecKeypress() { Keycode = 0x27 }), string.Empty)); |
| 203 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("8", (new CecSharp.CecKeypress() { Keycode = 0x28 }), string.Empty)); |
| 204 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("9", (new CecSharp.CecKeypress() { Keycode = 0x29 }), string.Empty)); |
| 205 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem(".", (new CecSharp.CecKeypress() { Keycode = 0x2A }), string.Empty)); |
| 206 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Enter", (new CecSharp.CecKeypress() { Keycode = 0x2B }), string.Empty)); |
| 207 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Clear", (new CecSharp.CecKeypress() { Keycode = 0x2C }), string.Empty)); |
| 208 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Next favourite", (new CecSharp.CecKeypress() { Keycode = 0x2F }), string.Empty)); |
| 209 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel up", (new CecSharp.CecKeypress() { Keycode = 0x30 }), string.Empty)); |
| 210 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel down", (new CecSharp.CecKeypress() { Keycode = 0x31 }), string.Empty)); |
| 211 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Previous channel", (new CecSharp.CecKeypress() { Keycode = 0x32 }), string.Empty)); |
| 212 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sound select", (new CecSharp.CecKeypress() { Keycode = 0x33 }), string.Empty)); |
| 213 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Input select", (new CecSharp.CecKeypress() { Keycode = 0x34 }), string.Empty)); |
| 214 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Display information", (new CecSharp.CecKeypress() { Keycode = 0x35 }), string.Empty)); |
| 215 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Help", (new CecSharp.CecKeypress() { Keycode = 0x36 }), string.Empty)); |
| 216 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page up", (new CecSharp.CecKeypress() { Keycode = 0x37 }), string.Empty)); |
| 217 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page down", (new CecSharp.CecKeypress() { Keycode = 0x38 }), string.Empty)); |
| 218 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power", (new CecSharp.CecKeypress() { Keycode = 0x40 }), string.Empty)); |
| 219 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume up", (new CecSharp.CecKeypress() { Keycode = 0x41 }), string.Empty)); |
| 220 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume down", (new CecSharp.CecKeypress() { Keycode = 0x42 }), string.Empty)); |
| 221 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute", (new CecSharp.CecKeypress() { Keycode = 0x43 }), string.Empty)); |
| 222 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play", (new CecSharp.CecKeypress() { Keycode = 0x44 }), string.Empty)); |
| 223 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop", (new CecSharp.CecKeypress() { Keycode = 0x45 }), string.Empty)); |
| 224 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause", (new CecSharp.CecKeypress() { Keycode = 0x46 }), string.Empty)); |
| 225 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record", (new CecSharp.CecKeypress() { Keycode = 0x47 }), string.Empty)); |
| 226 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Rewind", (new CecSharp.CecKeypress() { Keycode = 0x48 }), string.Empty)); |
| 227 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Fast forward", (new CecSharp.CecKeypress() { Keycode = 0x49 }), string.Empty)); |
| 228 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Eject", (new CecSharp.CecKeypress() { Keycode = 0x4A }), string.Empty)); |
| 229 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Forward", (new CecSharp.CecKeypress() { Keycode = 0x4B }), string.Empty)); |
| 230 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Backward", (new CecSharp.CecKeypress() { Keycode = 0x4C }), string.Empty)); |
| 231 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop record", (new CecSharp.CecKeypress() { Keycode = 0x4D }), string.Empty)); |
| 232 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record", (new CecSharp.CecKeypress() { Keycode = 0x4E }), string.Empty)); |
| 233 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Angle", (new CecSharp.CecKeypress() { Keycode = 0x50 }), string.Empty)); |
| 234 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sub picture", (new CecSharp.CecKeypress() { Keycode = 0x51 }), string.Empty)); |
| 235 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Video on demand", (new CecSharp.CecKeypress() { Keycode = 0x52 }), string.Empty)); |
| 236 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Electronic program guide", (new CecSharp.CecKeypress() { Keycode = 0x53 }), string.Empty)); |
| 237 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Timer programming", (new CecSharp.CecKeypress() { Keycode = 0x54 }), string.Empty)); |
| 238 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Initial configuration", (new CecSharp.CecKeypress() { Keycode = 0x55 }), string.Empty)); |
| 239 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play (function)", (new CecSharp.CecKeypress() { Keycode = 0x60 }), string.Empty)); |
| 240 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause play (function)", (new CecSharp.CecKeypress() { Keycode = 0x61 }), string.Empty)); |
| 241 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record (function)", (new CecSharp.CecKeypress() { Keycode = 0x62 }), string.Empty)); |
| 242 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record (function)", (new CecSharp.CecKeypress() { Keycode = 0x63 }), string.Empty)); |
| 243 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop (function)", (new CecSharp.CecKeypress() { Keycode = 0x64 }), string.Empty)); |
| 244 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute (function)", (new CecSharp.CecKeypress() { Keycode = 0x65 }), string.Empty)); |
| 245 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Restore volume", (new CecSharp.CecKeypress() { Keycode = 0x66 }), string.Empty)); |
| 246 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Tune", (new CecSharp.CecKeypress() { Keycode = 0x67 }), string.Empty)); |
| 247 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select media", (new CecSharp.CecKeypress() { Keycode = 0x68 }), string.Empty)); |
| 248 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select AV input", (new CecSharp.CecKeypress() { Keycode = 0x69 }), string.Empty)); |
| 249 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select audio input", (new CecSharp.CecKeypress() { Keycode = 0x6A }), string.Empty)); |
| 250 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power toggle", (new CecSharp.CecKeypress() { Keycode = 0x6B }), string.Empty)); |
| 251 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power off", (new CecSharp.CecKeypress() { Keycode = 0x6C }), string.Empty)); |
| 252 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power on", (new CecSharp.CecKeypress() { Keycode = 0x6D }), string.Empty)); |
| 253 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F1 (blue)", (new CecSharp.CecKeypress() { Keycode = 0x71 }), string.Empty)); |
| 254 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F2 (red)", (new CecSharp.CecKeypress() { Keycode = 0x72 }), string.Empty)); |
| 255 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F3 (green)", (new CecSharp.CecKeypress() { Keycode = 0x73 }), string.Empty)); |
| 256 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F4 (yellow)", (new CecSharp.CecKeypress() { Keycode = 0x74 }), string.Empty)); |
| 257 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F5", (new CecSharp.CecKeypress() { Keycode = 0x75 }), string.Empty)); |
| 258 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Data", (new CecSharp.CecKeypress() { Keycode = 0x76 }), string.Empty)); |
| 259 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecSharp.CecKeypress() { Keycode = 0x91 }), string.Empty)); |
| 260 | } |
| 261 | |
| 262 | private void ProcessEventHandler(object src, UpdateEvent updateEvent) |
| 263 | { |
| 264 | switch (updateEvent.Type) |
| 265 | { |
| 266 | case UpdateEventType.StatusText: |
| 267 | SetControlText(lStatus, updateEvent.StringValue); |
| 268 | break; |
| 269 | case UpdateEventType.PhysicalAddress: |
| 270 | Config.PhysicalAddress = (ushort)updateEvent.IntValue; |
| 271 | SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue)); |
| 272 | break; |
| 273 | case UpdateEventType.ProgressBar: |
| 274 | SetControlVisible(pProgress, true); |
| 275 | SetProgressValue(pProgress, updateEvent.IntValue); |
| 276 | break; |
| 277 | case UpdateEventType.TVVendorId: |
| 278 | TVVendor = (CecVendorId)updateEvent.IntValue; |
| 279 | UpdateSelectedDevice(); |
| 280 | break; |
| 281 | case UpdateEventType.BaseDevicePhysicalAddress: |
| 282 | SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue)); |
| 283 | break; |
| 284 | case UpdateEventType.BaseDevice: |
| 285 | Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue; |
| 286 | break; |
| 287 | case UpdateEventType.HDMIPort: |
| 288 | Config.HDMIPort = (byte)updateEvent.IntValue; |
| 289 | break; |
| 290 | case UpdateEventType.MenuLanguage: |
| 291 | SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : "")); |
| 292 | break; |
| 293 | case UpdateEventType.HasAVRDevice: |
| 294 | if (HasAVRDevice != updateEvent.BoolValue) |
| 295 | { |
| 296 | HasAVRDevice = updateEvent.BoolValue; |
| 297 | UpdateSelectedDevice(); |
| 298 | } |
| 299 | break; |
| 300 | case UpdateEventType.AVRVendorId: |
| 301 | AVRVendor = (CecVendorId)updateEvent.IntValue; |
| 302 | UpdateSelectedDevice(); |
| 303 | break; |
| 304 | case UpdateEventType.Configuration: |
| 305 | SuppressUpdates = true; |
| 306 | ConfigurationChanged(updateEvent.ConfigValue); |
| 307 | SuppressUpdates = false; |
| 308 | break; |
| 309 | case UpdateEventType.PollDevices: |
| 310 | CheckActiveDevices(); |
| 311 | break; |
| 312 | case UpdateEventType.ProcessCompleted: |
| 313 | ActiveProcess = null; |
| 314 | SetControlsEnabled(true); |
| 315 | if (UpdatingInfoPanel != null) |
| 316 | { |
| 317 | UpdatingInfoPanel.SetControlEnabled(UpdatingInfoPanel.bUpdate, true); |
| 318 | UpdatingInfoPanel = null; |
| 319 | } |
| 320 | SetControlVisible(pProgress, false); |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | private void SetControlsEnabled(bool val) |
| 326 | { |
| 327 | SetControlEnabled(cbPortNumber, val && !Config.AutodetectAddress); |
| 328 | SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 && !Config.AutodetectAddress ? val : false); |
| 329 | SetControlEnabled(cbOverrideAddress, val); |
| 330 | SetControlEnabled(tbPhysicalAddress, val && !Config.AutodetectAddress && cbOverrideAddress.Checked); |
| 331 | SetControlEnabled(cbDeviceType, val); |
| 332 | SetControlEnabled(cbUseTVMenuLanguage, val); |
| 333 | SetControlEnabled(cbActivateSource, val); |
| 334 | SetControlEnabled(cbPowerOffScreensaver, val); |
| 335 | SetControlEnabled(cbPowerOffOnStandby, val); |
| 336 | SetControlEnabled(cbWakeDevices, val); |
| 337 | SetControlEnabled(cbPowerOffDevices, val); |
| 338 | SetControlEnabled(cbVendorOverride, val); |
| 339 | SetControlEnabled(cbVendorId, val && cbVendorOverride.Checked); |
| 340 | SetControlEnabled(bClose, val); |
| 341 | SetControlEnabled(bSaveConfig, val); |
| 342 | SetControlEnabled(bReloadConfig, val); |
| 343 | SetControlEnabled(bRescanDevices, val); |
| 344 | |
| 345 | SetControlEnabled(bSendImageViewOn, val); |
| 346 | SetControlEnabled(bStandby, val); |
| 347 | SetControlEnabled(bActivateSource, val); |
| 348 | SetControlEnabled(bScan, val); |
| 349 | |
| 350 | bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val; |
| 351 | SetControlEnabled(bVolUp, enableVolumeButtons); |
| 352 | SetControlEnabled(bVolDown, enableVolumeButtons); |
| 353 | SetControlEnabled(bMute, enableVolumeButtons); |
| 354 | } |
| 355 | |
| 356 | private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) |
| 357 | { |
| 358 | switch (tabControl1.SelectedIndex) |
| 359 | { |
| 360 | case 0: |
| 361 | SelectedTab = ConfigTab.Configuration; |
| 362 | break; |
| 363 | case 1: |
| 364 | SelectedTab = ConfigTab.KeyConfiguration; |
| 365 | break; |
| 366 | case 2: |
| 367 | SelectedTab = ConfigTab.Tester; |
| 368 | break; |
| 369 | case 3: |
| 370 | SelectedTab = ConfigTab.Log; |
| 371 | UpdateLog(); |
| 372 | break; |
| 373 | default: |
| 374 | SelectedTab = ConfigTab.Configuration; |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | protected override void Dispose(bool disposing) |
| 380 | { |
| 381 | if (disposing) |
| 382 | { |
| 383 | Lib.DisableCallbacks(); |
| 384 | Lib.StandbyDevices(CecLogicalAddress.Broadcast); |
| 385 | Lib.Close(); |
| 386 | } |
| 387 | if (disposing && (components != null)) |
| 388 | { |
| 389 | components.Dispose(); |
| 390 | } |
| 391 | base.Dispose(disposing); |
| 392 | } |
| 393 | |
| 394 | #region Actions |
| 395 | public void ReloadXMLConfiguration() |
| 396 | { |
| 397 | LoadXMLConfiguration(ref Config); |
| 398 | Lib.SetConfiguration(Config); |
| 399 | ConfigurationChanged(Config); |
| 400 | } |
| 401 | |
| 402 | public void UpdateInfoPanel(DeviceInformation panel) |
| 403 | { |
| 404 | if (!SuppressUpdates && ActiveProcess == null) |
| 405 | { |
| 406 | SetControlsEnabled(false); |
| 407 | UpdatingInfoPanel = panel; |
| 408 | panel.SetControlEnabled(panel.bUpdate, false); |
| 409 | ActiveProcess = new UpdateDeviceInfo(this, ref Lib, panel); |
| 410 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 411 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | public void SetPhysicalAddress(ushort physicalAddress) |
| 416 | { |
| 417 | if (!SuppressUpdates && ActiveProcess == null && cbOverrideAddress.Checked) |
| 418 | { |
| 419 | SetControlsEnabled(false); |
| 420 | SetControlText(cbPortNumber, string.Empty); |
| 421 | SetControlText(cbConnectedDevice, string.Empty); |
| 422 | ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress); |
| 423 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 424 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | public void UpdateConfigurationAsync() |
| 429 | { |
| 430 | if (!SuppressUpdates && ActiveProcess == null) |
| 431 | { |
| 432 | SetControlsEnabled(false); |
| 433 | ActiveProcess = new UpdateConfiguration(ref Lib, Config); |
| 434 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 435 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | public void SendImageViewOn(CecLogicalAddress address) |
| 440 | { |
| 441 | if (!SuppressUpdates && ActiveProcess == null) |
| 442 | { |
| 443 | SetControlsEnabled(false); |
| 444 | ActiveProcess = new SendImageViewOn(ref Lib, address); |
| 445 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 446 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | public void ActivateSource(CecLogicalAddress address) |
| 451 | { |
| 452 | if (!SuppressUpdates && ActiveProcess == null) |
| 453 | { |
| 454 | SetControlsEnabled(false); |
| 455 | ActiveProcess = new SendActivateSource(ref Lib, address); |
| 456 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 457 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | public void SendStandby(CecLogicalAddress address) |
| 462 | { |
| 463 | if (!SuppressUpdates && ActiveProcess == null) |
| 464 | { |
| 465 | SetControlsEnabled(false); |
| 466 | ActiveProcess = new SendStandby(ref Lib, address); |
| 467 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 468 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | public void ShowDeviceInfo(CecLogicalAddress address) |
| 473 | { |
| 474 | if (!SuppressUpdates && ActiveProcess == null) |
| 475 | { |
| 476 | SetControlsEnabled(false); |
| 477 | ActiveProcess = new ShowDeviceInfo(this, ref Lib, address); |
| 478 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 479 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 480 | } |
| 481 | } |
| 482 | #endregion |
| 483 | |
| 484 | #region Configuration tab |
| 485 | private void cbOverrideAddress_CheckedChanged(object sender, EventArgs e) |
| 486 | { |
| 487 | SetControlEnabled(tbPhysicalAddress, ((CheckBox)sender).Checked); |
| 488 | } |
| 489 | |
| 490 | private void tbPhysicalAddress_TextChanged(object sender, EventArgs e) |
| 491 | { |
| 492 | if (tbPhysicalAddress.Text.Length != 4 || |
| 493 | cbOverrideAddress.Checked) |
| 494 | return; |
| 495 | ushort physicalAddress = 0; |
| 496 | if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress)) |
| 497 | return; |
| 498 | |
| 499 | SetPhysicalAddress(physicalAddress); |
| 500 | } |
| 501 | |
| 502 | private void UpdateSelectedDevice() |
| 503 | { |
| 504 | if (HasAVRDevice) |
| 505 | SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString }); |
| 506 | else |
| 507 | SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString }); |
| 508 | } |
| 509 | |
| 510 | public void SetConnectedDevice(CecLogicalAddress address, int portnumber) |
| 511 | { |
| 512 | if (!SuppressUpdates && ActiveProcess == null) |
| 513 | { |
| 514 | SetControlsEnabled(false); |
| 515 | ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber); |
| 516 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 517 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e) |
| 522 | { |
| 523 | SetConnectedDevice(SelectedConnectedDevice, SelectedPortNumber); |
| 524 | } |
| 525 | |
| 526 | private void bCancel_Click(object sender, EventArgs e) |
| 527 | { |
| 528 | this.Dispose(); |
| 529 | } |
| 530 | |
| 531 | private void bSave_Click(object sender, EventArgs e) |
| 532 | { |
| 533 | SetControlsEnabled(false); |
| 534 | |
| 535 | Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked; |
| 536 | Config.ActivateSource = cbActivateSource.Checked; |
| 537 | Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked; |
| 538 | Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked; |
| 539 | Config.WakeDevices = WakeDevices; |
| 540 | Config.PowerOffDevices = PowerOffDevices; |
| 541 | |
| 542 | if (!Lib.CanPersistConfiguration()) |
| 543 | { |
| 544 | if (ActiveProcess == null) |
| 545 | { |
| 546 | SetControlsEnabled(false); |
| 547 | string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data"; |
| 548 | string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); |
| 549 | |
| 550 | SaveFileDialog dialog = new SaveFileDialog() |
| 551 | { |
| 552 | Title = "Where do you want to store the settings?", |
| 553 | InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir, |
| 554 | FileName = "usb_2548_1001.xml", |
| 555 | Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*", |
| 556 | FilterIndex = 1 |
| 557 | }; |
| 558 | |
| 559 | if (dialog.ShowDialog() == DialogResult.OK) |
| 560 | { |
| 561 | FileStream fs = null; |
| 562 | string error = string.Empty; |
| 563 | try |
| 564 | { |
| 565 | fs = (FileStream)dialog.OpenFile(); |
| 566 | } |
| 567 | catch (Exception ex) |
| 568 | { |
| 569 | error = ex.Message; |
| 570 | } |
| 571 | if (fs == null) |
| 572 | { |
| 573 | MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing" + (error.Length > 0 ? ": " + error : string.Empty ), "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 574 | } |
| 575 | else |
| 576 | { |
| 577 | StreamWriter writer = new StreamWriter(fs); |
| 578 | StringBuilder output = new StringBuilder(); |
| 579 | output.AppendLine("<settings>"); |
| 580 | output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />"); |
| 581 | output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 0) + "\" />"); |
| 582 | output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.ActivateSource ? 1 : 0) + "\" />"); |
| 583 | output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffDevices.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />"); |
| 584 | output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />"); |
| 585 | output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />"); |
| 586 | output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />"); |
| 587 | output.AppendLine("<setting id=\"enabled\" value=\"1\" />"); |
| 588 | output.AppendLine("<setting id=\"port\" value=\"\" />"); |
| 589 | |
| 590 | // only supported by 1.5.0+ clients |
| 591 | output.AppendLine("<!-- the following lines are only supported by v1.5.0+ clients -->"); |
| 592 | output.AppendLine("<setting id=\"activate_source\" value=\"" + (Config.ActivateSource ? 1 : 0) + "\" />"); |
| 593 | output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", cbOverrideAddress.Checked ? Config.PhysicalAddress : 0).Trim() + "\" />"); |
| 594 | output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />"); |
| 595 | output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", (int)Config.TvVendor).Trim() + "\" />"); |
| 596 | |
| 597 | output.Append("<setting id=\"wake_devices\" value=\""); |
| 598 | StringBuilder strWakeDevices = new StringBuilder(); |
| 599 | foreach (CecLogicalAddress addr in Config.WakeDevices.Addresses) |
| 600 | if (addr != CecLogicalAddress.Unknown) |
| 601 | strWakeDevices.Append(" " + (int)addr); |
| 602 | output.Append(strWakeDevices.ToString().Trim()); |
| 603 | output.AppendLine("\" />"); |
| 604 | |
| 605 | output.Append("<setting id=\"standby_devices\" value=\""); |
| 606 | StringBuilder strSleepDevices = new StringBuilder(); |
| 607 | foreach (CecLogicalAddress addr in Config.PowerOffDevices.Addresses) |
| 608 | if (addr != CecLogicalAddress.Unknown) |
| 609 | strSleepDevices.Append(" " + (int)addr); |
| 610 | output.Append(strSleepDevices.ToString().Trim()); |
| 611 | output.AppendLine("\" />"); |
| 612 | |
| 613 | output.AppendLine("</settings>"); |
| 614 | writer.Write(output.ToString()); |
| 615 | writer.Close(); |
| 616 | fs.Close(); |
| 617 | fs.Dispose(); |
| 618 | MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 619 | } |
| 620 | } |
| 621 | SetControlsEnabled(true); |
| 622 | } |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | if (!Lib.PersistConfiguration(Config)) |
| 627 | MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 628 | else |
| 629 | MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 630 | } |
| 631 | SetControlsEnabled(true); |
| 632 | } |
| 633 | |
| 634 | private void bReloadConfig_Click(object sender, EventArgs e) |
| 635 | { |
| 636 | if (Lib.CanPersistConfiguration()) |
| 637 | { |
| 638 | Lib.GetCurrentConfiguration(Config); |
| 639 | ConfigurationChanged(Config); |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | ReloadXMLConfiguration(); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | private void cbVendorOverride_CheckedChanged(object sender, EventArgs e) |
| 648 | { |
| 649 | if (cbVendorOverride.Checked) |
| 650 | { |
| 651 | cbVendorId.Enabled = true; |
| 652 | switch (cbVendorId.Text) |
| 653 | { |
| 654 | case "LG": |
| 655 | Config.TvVendor = CecVendorId.LG; |
| 656 | break; |
| 657 | case "Onkyo": |
| 658 | Config.TvVendor = CecVendorId.Onkyo; |
| 659 | break; |
| 660 | case "Panasonic": |
| 661 | Config.TvVendor = CecVendorId.Panasonic; |
| 662 | break; |
| 663 | case "Philips": |
| 664 | Config.TvVendor = CecVendorId.Philips; |
| 665 | break; |
| 666 | case "Pioneer": |
| 667 | Config.TvVendor = CecVendorId.Pioneer; |
| 668 | break; |
| 669 | case "Samsung": |
| 670 | Config.TvVendor = CecVendorId.Samsung; |
| 671 | break; |
| 672 | case "Sony": |
| 673 | Config.TvVendor = CecVendorId.Sony; |
| 674 | break; |
| 675 | case "Yamaha": |
| 676 | Config.TvVendor = CecVendorId.Yamaha; |
| 677 | break; |
| 678 | default: |
| 679 | Config.TvVendor = CecVendorId.Unknown; |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | else |
| 684 | { |
| 685 | cbVendorId.Enabled = false; |
| 686 | Config.TvVendor = CecVendorId.Unknown; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | private void cbDeviceType_SelectedIndexChanged(object sender, EventArgs e) |
| 691 | { |
| 692 | CecDeviceType type = SelectedDeviceType; |
| 693 | if (type != Config.DeviceTypes.Types[0]) |
| 694 | { |
| 695 | Config.DeviceTypes.Types[0] = type; |
| 696 | if (!DeviceChangeWarningDisplayed) |
| 697 | { |
| 698 | DeviceChangeWarningDisplayed = true; |
| 699 | MessageBox.Show("You have changed the device type. Save the configuration, and restart the application to use the new setting.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | #endregion |
| 704 | |
| 705 | #region Key configuration tab |
| 706 | delegate void SelectKeypressRowCallback(CecKeypress key); |
| 707 | private void SelectKeypressRow(CecKeypress key) |
| 708 | { |
| 709 | if (dgButtons.InvokeRequired) |
| 710 | { |
| 711 | SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow); |
| 712 | try |
| 713 | { |
| 714 | this.Invoke(d, new object[] { key }); |
| 715 | } |
| 716 | catch (Exception) { } |
| 717 | } |
| 718 | else |
| 719 | { |
| 720 | int rowIndex = -1; |
| 721 | foreach (DataGridViewRow row in dgButtons.Rows) |
| 722 | { |
| 723 | CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem; |
| 724 | if (item != null && item.Key.Keycode == key.Keycode) |
| 725 | { |
| 726 | rowIndex = row.Index; |
| 727 | row.Selected = true; |
| 728 | item.Enabled = true; |
| 729 | } |
| 730 | else |
| 731 | { |
| 732 | row.Selected = false; |
| 733 | } |
| 734 | } |
| 735 | if (rowIndex > -1) |
| 736 | dgButtons.FirstDisplayedScrollingRowIndex = rowIndex; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) |
| 741 | { |
| 742 | DataGridView grid = sender as DataGridView; |
| 743 | CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem; |
| 744 | if (data == null || !data.Enabled) |
| 745 | e.CellStyle.ForeColor = Color.Gray; |
| 746 | } |
| 747 | #endregion |
| 748 | |
| 749 | #region CEC Tester tab |
| 750 | public void CheckActiveDevices() |
| 751 | { |
| 752 | CecLogicalAddresses activeDevices = Lib.GetActiveDevices(); |
| 753 | List<string> deviceList = new List<string>(); |
| 754 | foreach (CecLogicalAddress activeDevice in activeDevices.Addresses) |
| 755 | { |
| 756 | if (activeDevice != CecLogicalAddress.Unknown) |
| 757 | deviceList.Add(string.Format("{0,1:X} : {1}", (int)activeDevice, Lib.ToString(activeDevice))); |
| 758 | } |
| 759 | deviceList.Add(string.Format("{0,1:X} : {1}", (int)CecLogicalAddress.Broadcast, Lib.ToString(CecLogicalAddress.Broadcast))); |
| 760 | |
| 761 | SetActiveDevices(deviceList.ToArray()); |
| 762 | } |
| 763 | |
| 764 | delegate void SetActiveDevicesCallback(string[] activeDevices); |
| 765 | private void SetActiveDevices(string[] activeDevices) |
| 766 | { |
| 767 | if (this.cbCommandDestination.InvokeRequired) |
| 768 | { |
| 769 | SetActiveDevicesCallback d = new SetActiveDevicesCallback(SetActiveDevices); |
| 770 | try |
| 771 | { |
| 772 | this.Invoke(d, new object[] { activeDevices }); |
| 773 | } |
| 774 | catch (Exception) { } |
| 775 | } |
| 776 | else |
| 777 | { |
| 778 | this.cbCommandDestination.Items.Clear(); |
| 779 | foreach (string item in activeDevices) |
| 780 | this.cbCommandDestination.Items.Add(item); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | delegate CecLogicalAddress GetTargetDeviceCallback(); |
| 785 | private CecLogicalAddress GetTargetDevice() |
| 786 | { |
| 787 | if (this.cbCommandDestination.InvokeRequired) |
| 788 | { |
| 789 | GetTargetDeviceCallback d = new GetTargetDeviceCallback(GetTargetDevice); |
| 790 | CecLogicalAddress retval = CecLogicalAddress.Unknown; |
| 791 | try |
| 792 | { |
| 793 | retval = (CecLogicalAddress)this.Invoke(d, new object[] { }); |
| 794 | } |
| 795 | catch (Exception) { } |
| 796 | return retval; |
| 797 | } |
| 798 | |
| 799 | return GetLogicalAddressFromString(this.cbCommandDestination.Text); |
| 800 | } |
| 801 | |
| 802 | private CecLogicalAddress GetLogicalAddressFromString(string name) |
| 803 | { |
| 804 | switch (name.Substring(0, 1).ToLower()) |
| 805 | { |
| 806 | case "0": |
| 807 | return CecLogicalAddress.Tv; |
| 808 | case "1": |
| 809 | return CecLogicalAddress.RecordingDevice1; |
| 810 | case "2": |
| 811 | return CecLogicalAddress.RecordingDevice2; |
| 812 | case "3": |
| 813 | return CecLogicalAddress.Tuner1; |
| 814 | case "4": |
| 815 | return CecLogicalAddress.PlaybackDevice1; |
| 816 | case "5": |
| 817 | return CecLogicalAddress.AudioSystem; |
| 818 | case "6": |
| 819 | return CecLogicalAddress.Tuner2; |
| 820 | case "7": |
| 821 | return CecLogicalAddress.Tuner3; |
| 822 | case "8": |
| 823 | return CecLogicalAddress.PlaybackDevice2; |
| 824 | case "9": |
| 825 | return CecLogicalAddress.RecordingDevice3; |
| 826 | case "a": |
| 827 | return CecLogicalAddress.Tuner4; |
| 828 | case "b": |
| 829 | return CecLogicalAddress.PlaybackDevice3; |
| 830 | case "c": |
| 831 | return CecLogicalAddress.Reserved1; |
| 832 | case "d": |
| 833 | return CecLogicalAddress.Reserved2; |
| 834 | case "e": |
| 835 | return CecLogicalAddress.FreeUse; |
| 836 | case "f": |
| 837 | return CecLogicalAddress.Broadcast; |
| 838 | default: |
| 839 | return CecLogicalAddress.Unknown; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | private void bSendImageViewOn_Click(object sender, EventArgs e) |
| 844 | { |
| 845 | SendImageViewOn(GetTargetDevice()); |
| 846 | } |
| 847 | |
| 848 | private void bStandby_Click(object sender, EventArgs e) |
| 849 | { |
| 850 | SendStandby(GetTargetDevice()); |
| 851 | } |
| 852 | |
| 853 | private void bScan_Click(object sender, EventArgs e) |
| 854 | { |
| 855 | ShowDeviceInfo(GetTargetDevice()); |
| 856 | } |
| 857 | |
| 858 | private void bActivateSource_Click(object sender, EventArgs e) |
| 859 | { |
| 860 | ActivateSource(GetTargetDevice()); |
| 861 | } |
| 862 | |
| 863 | private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e) |
| 864 | { |
| 865 | bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem); |
| 866 | this.bVolUp.Enabled = enableVolumeButtons; |
| 867 | this.bVolDown.Enabled = enableVolumeButtons; |
| 868 | this.bMute.Enabled = enableVolumeButtons; |
| 869 | this.bActivateSource.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast); |
| 870 | this.bScan.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast); |
| 871 | } |
| 872 | |
| 873 | private void bVolUp_Click(object sender, EventArgs e) |
| 874 | { |
| 875 | SetControlsEnabled(false); |
| 876 | Lib.VolumeUp(true); |
| 877 | SetControlsEnabled(true); |
| 878 | } |
| 879 | |
| 880 | private void bVolDown_Click(object sender, EventArgs e) |
| 881 | { |
| 882 | SetControlsEnabled(false); |
| 883 | Lib.VolumeDown(true); |
| 884 | SetControlsEnabled(true); |
| 885 | } |
| 886 | |
| 887 | private void bMute_Click(object sender, EventArgs e) |
| 888 | { |
| 889 | SetControlsEnabled(false); |
| 890 | Lib.MuteAudio(true); |
| 891 | SetControlsEnabled(true); |
| 892 | } |
| 893 | |
| 894 | private void bRescanDevices_Click(object sender, EventArgs e) |
| 895 | { |
| 896 | if (!SuppressUpdates && ActiveProcess == null) |
| 897 | { |
| 898 | SetControlsEnabled(false); |
| 899 | ActiveProcess = new RescanDevices(ref Lib); |
| 900 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 901 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 902 | } |
| 903 | } |
| 904 | #endregion |
| 905 | |
| 906 | #region Log tab |
| 907 | delegate void UpdateLogCallback(); |
| 908 | private void UpdateLog() |
| 909 | { |
| 910 | if (tbLog.InvokeRequired) |
| 911 | { |
| 912 | UpdateLogCallback d = new UpdateLogCallback(UpdateLog); |
| 913 | try |
| 914 | { |
| 915 | this.Invoke(d, new object[] { }); |
| 916 | } |
| 917 | catch (Exception) { } |
| 918 | } |
| 919 | else |
| 920 | { |
| 921 | tbLog.Text = Log; |
| 922 | tbLog.Select(tbLog.Text.Length, 0); |
| 923 | tbLog.ScrollToCaret(); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | private void AddLogMessage(CecLogMessage message) |
| 928 | { |
| 929 | string strLevel = ""; |
| 930 | bool display = false; |
| 931 | switch (message.Level) |
| 932 | { |
| 933 | case CecLogLevel.Error: |
| 934 | strLevel = "ERROR: "; |
| 935 | display = cbLogError.Checked; |
| 936 | break; |
| 937 | case CecLogLevel.Warning: |
| 938 | strLevel = "WARNING: "; |
| 939 | display = cbLogWarning.Checked; |
| 940 | break; |
| 941 | case CecLogLevel.Notice: |
| 942 | strLevel = "NOTICE: "; |
| 943 | display = cbLogNotice.Checked; |
| 944 | break; |
| 945 | case CecLogLevel.Traffic: |
| 946 | strLevel = "TRAFFIC: "; |
| 947 | display = cbLogTraffic.Checked; |
| 948 | break; |
| 949 | case CecLogLevel.Debug: |
| 950 | strLevel = "DEBUG: "; |
| 951 | display = cbLogDebug.Checked; |
| 952 | break; |
| 953 | default: |
| 954 | break; |
| 955 | } |
| 956 | |
| 957 | if (display) |
| 958 | { |
| 959 | string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine; |
| 960 | Log += strLog; |
| 961 | } |
| 962 | |
| 963 | if (SelectedTab == ConfigTab.Log) |
| 964 | UpdateLog(); |
| 965 | } |
| 966 | |
| 967 | private void bClearLog_Click(object sender, EventArgs e) |
| 968 | { |
| 969 | Log = string.Empty; |
| 970 | UpdateLog(); |
| 971 | } |
| 972 | |
| 973 | private void bSaveLog_Click(object sender, EventArgs e) |
| 974 | { |
| 975 | SaveFileDialog dialog = new SaveFileDialog() |
| 976 | { |
| 977 | Title = "Where do you want to store the log file?", |
| 978 | InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), |
| 979 | FileName = "cec-log.txt", |
| 980 | Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*", |
| 981 | FilterIndex = 1 |
| 982 | }; |
| 983 | |
| 984 | if (dialog.ShowDialog() == DialogResult.OK) |
| 985 | { |
| 986 | FileStream fs = (FileStream)dialog.OpenFile(); |
| 987 | if (fs == null) |
| 988 | { |
| 989 | MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 990 | } |
| 991 | else |
| 992 | { |
| 993 | StreamWriter writer = new StreamWriter(fs); |
| 994 | writer.Write(Log); |
| 995 | writer.Close(); |
| 996 | fs.Close(); |
| 997 | fs.Dispose(); |
| 998 | MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | #endregion |
| 1003 | |
| 1004 | #region LibCecSharp callbacks |
| 1005 | public int ConfigurationChanged(LibCECConfiguration config) |
| 1006 | { |
| 1007 | Config = config; |
| 1008 | SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress)); |
| 1009 | SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString); |
| 1010 | SetControlText(cbPortNumber, Config.HDMIPort.ToString()); |
| 1011 | switch (config.DeviceTypes.Types[0]) |
| 1012 | { |
| 1013 | case CecDeviceType.RecordingDevice: |
| 1014 | SetControlText(cbDeviceType, "Recorder"); |
| 1015 | break; |
| 1016 | case CecDeviceType.PlaybackDevice: |
| 1017 | SetControlText(cbDeviceType, "Player"); |
| 1018 | break; |
| 1019 | case CecDeviceType.Tuner: |
| 1020 | SetControlText(cbDeviceType, "Tuner"); |
| 1021 | break; |
| 1022 | default: |
| 1023 | SetControlText(cbDeviceType, "Recorder"); |
| 1024 | break; |
| 1025 | } |
| 1026 | if (config.TvVendor != CecVendorId.Unknown) |
| 1027 | { |
| 1028 | SetCheckboxChecked(cbVendorOverride, true); |
| 1029 | SetControlText(cbVendorId, Lib.ToString(config.TvVendor)); |
| 1030 | } |
| 1031 | else |
| 1032 | { |
| 1033 | SetCheckboxChecked(cbVendorOverride, false); |
| 1034 | SetControlText(cbVendorId, Lib.ToString(TVVendor)); |
| 1035 | } |
| 1036 | |
| 1037 | SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage); |
| 1038 | SetCheckboxChecked(cbActivateSource, Config.ActivateSource); |
| 1039 | SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver); |
| 1040 | SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby); |
| 1041 | UpdateSelectedDevice(); |
| 1042 | |
| 1043 | for (int iPtr = 0; iPtr < 15; iPtr++) |
| 1044 | SetCheckboxItemChecked(cbWakeDevices, iPtr, Config.WakeDevices.IsSet((CecLogicalAddress)iPtr)); |
| 1045 | for (int iPtr = 0; iPtr < 15; iPtr++) |
| 1046 | SetCheckboxItemChecked(cbPowerOffDevices, iPtr, Config.PowerOffDevices.IsSet((CecLogicalAddress)iPtr)); |
| 1047 | |
| 1048 | SetControlText(this, "Pulse-Eight USB-CEC Adapter - libCEC " + Lib.ToString(Config.ServerVersion)); |
| 1049 | return 1; |
| 1050 | } |
| 1051 | |
| 1052 | public int ReceiveCommand(CecCommand command) |
| 1053 | { |
| 1054 | return 1; |
| 1055 | } |
| 1056 | |
| 1057 | public int ReceiveKeypress(CecKeypress key) |
| 1058 | { |
| 1059 | SelectKeypressRow(key); |
| 1060 | return 1; |
| 1061 | } |
| 1062 | |
| 1063 | public int ReceiveLogMessage(CecLogMessage message) |
| 1064 | { |
| 1065 | try |
| 1066 | { |
| 1067 | AddLogMessage(message); |
| 1068 | } |
| 1069 | catch (Exception) { } |
| 1070 | return 1; |
| 1071 | } |
| 1072 | #endregion |
| 1073 | |
| 1074 | #region Class members |
| 1075 | public bool HasAVRDevice { get; private set; } |
| 1076 | #region TV Vendor |
| 1077 | private CecVendorId _tvVendor = CecVendorId.Unknown; |
| 1078 | public CecVendorId TVVendor |
| 1079 | { |
| 1080 | get { return _tvVendor;} |
| 1081 | private set { _tvVendor = value; } |
| 1082 | } |
| 1083 | public string TVVendorString |
| 1084 | { |
| 1085 | get |
| 1086 | { |
| 1087 | return TVVendor != CecVendorId.Unknown ? |
| 1088 | "Television (" + Lib.ToString(TVVendor) + ")" : |
| 1089 | "Television"; |
| 1090 | } |
| 1091 | } |
| 1092 | #endregion |
| 1093 | #region AVR Vendor |
| 1094 | private CecVendorId _avrVendor = CecVendorId.Unknown; |
| 1095 | public CecVendorId AVRVendor |
| 1096 | { |
| 1097 | get { return _avrVendor; } |
| 1098 | private set { _avrVendor = value; } |
| 1099 | } |
| 1100 | public string AVRVendorString |
| 1101 | { |
| 1102 | get |
| 1103 | { |
| 1104 | return AVRVendor != CecVendorId.Unknown ? |
| 1105 | "AVR (" + Lib.ToString(AVRVendor) + ")" : |
| 1106 | "AVR"; |
| 1107 | } |
| 1108 | } |
| 1109 | #endregion |
| 1110 | public CecLogicalAddress SelectedConnectedDevice |
| 1111 | { |
| 1112 | get |
| 1113 | { |
| 1114 | return (cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv; |
| 1115 | } |
| 1116 | } |
| 1117 | public CecDeviceType SelectedDeviceType |
| 1118 | { |
| 1119 | get |
| 1120 | { |
| 1121 | switch (cbDeviceType.Text.ToLower()) |
| 1122 | { |
| 1123 | case "player": |
| 1124 | return CecDeviceType.PlaybackDevice; |
| 1125 | case "tuner": |
| 1126 | return CecDeviceType.Tuner; |
| 1127 | default: |
| 1128 | return CecDeviceType.RecordingDevice; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | public int SelectedPortNumber |
| 1133 | { |
| 1134 | get |
| 1135 | { |
| 1136 | int iPortNumber = 0; |
| 1137 | if (!int.TryParse(cbPortNumber.Text, out iPortNumber)) |
| 1138 | iPortNumber = 1; |
| 1139 | return iPortNumber; |
| 1140 | } |
| 1141 | } |
| 1142 | protected LibCECConfiguration Config; |
| 1143 | protected LibCecSharp Lib; |
| 1144 | private CecCallbackWrapper Callbacks; |
| 1145 | private UpdateProcess ActiveProcess = null; |
| 1146 | private bool SuppressUpdates = true; |
| 1147 | private ConfigTab SelectedTab = ConfigTab.Configuration; |
| 1148 | private string Log = string.Empty; |
| 1149 | private DeviceInformation UpdatingInfoPanel = null; |
| 1150 | private bool DeviceChangeWarningDisplayed = false; |
| 1151 | public CecLogicalAddresses WakeDevices |
| 1152 | { |
| 1153 | get |
| 1154 | { |
| 1155 | CecLogicalAddresses addr = new CecLogicalAddresses(); |
| 1156 | foreach (object item in this.cbWakeDevices.CheckedItems) |
| 1157 | { |
| 1158 | string c = item as string; |
| 1159 | addr.Set(GetLogicalAddressFromString(c)); |
| 1160 | } |
| 1161 | return addr; |
| 1162 | } |
| 1163 | } |
| 1164 | public CecLogicalAddresses PowerOffDevices |
| 1165 | { |
| 1166 | get |
| 1167 | { |
| 1168 | CecLogicalAddresses addr = new CecLogicalAddresses(); |
| 1169 | foreach (object item in this.cbPowerOffDevices.CheckedItems) |
| 1170 | { |
| 1171 | string c = item as string; |
| 1172 | addr.Set(GetLogicalAddressFromString(c)); |
| 1173 | } |
| 1174 | return addr; |
| 1175 | } |
| 1176 | } |
| 1177 | #endregion |
| 1178 | } |
| 1179 | |
| 1180 | /// <summary> |
| 1181 | /// A little wrapper that is needed because we already inherit form |
| 1182 | /// </summary> |
| 1183 | internal class CecCallbackWrapper : CecCallbackMethods |
| 1184 | { |
| 1185 | public CecCallbackWrapper(CecConfigGUI gui) |
| 1186 | { |
| 1187 | Gui = gui; |
| 1188 | } |
| 1189 | |
| 1190 | public override int ReceiveCommand(CecCommand command) |
| 1191 | { |
| 1192 | return Gui.ReceiveCommand(command); |
| 1193 | } |
| 1194 | |
| 1195 | public override int ReceiveKeypress(CecKeypress key) |
| 1196 | { |
| 1197 | return Gui.ReceiveKeypress(key); |
| 1198 | } |
| 1199 | |
| 1200 | public override int ReceiveLogMessage(CecLogMessage message) |
| 1201 | { |
| 1202 | return Gui.ReceiveLogMessage(message); |
| 1203 | } |
| 1204 | |
| 1205 | public override int ConfigurationChanged(LibCECConfiguration config) |
| 1206 | { |
| 1207 | return Gui.ConfigurationChanged(config); |
| 1208 | } |
| 1209 | |
| 1210 | private CecConfigGUI Gui; |
| 1211 | } |
| 1212 | } |