| 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 | |
| 14 | namespace CecConfigGui |
| 15 | { |
| 16 | public partial class CecConfigGUI : Form |
| 17 | { |
| 18 | public CecConfigGUI() |
| 19 | { |
| 20 | Config = new LibCECConfiguration(); |
| 21 | Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice; |
| 22 | Config.DeviceName = "CEC Config"; |
| 23 | Config.GetSettingsFromROM = true; |
| 24 | Config.ClientVersion = CecClientVersion.Version1_5_0; |
| 25 | Callbacks = new CecCallbackWrapper(this); |
| 26 | Config.SetCallbacks(Callbacks); |
| 27 | |
| 28 | InitializeComponent(); |
| 29 | Lib = new LibCecSharp(Config); |
| 30 | |
| 31 | ActiveProcess = new ConnectToDevice(ref Lib); |
| 32 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 33 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 34 | } |
| 35 | |
| 36 | public int ReceiveCommand(CecCommand command) |
| 37 | { |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | public int ReceiveKeypress(CecKeypress key) |
| 42 | { |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | delegate void AddLogMessageCallback(CecLogMessage message); |
| 47 | private void AddLogMessage(CecLogMessage message) |
| 48 | { |
| 49 | if (tbLog.InvokeRequired) |
| 50 | { |
| 51 | AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage); |
| 52 | try |
| 53 | { |
| 54 | this.Invoke(d, new object[] { message }); |
| 55 | } |
| 56 | catch (Exception) { } |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | if (((int)message.Level & LogLevel) == (int)message.Level) |
| 61 | { |
| 62 | string strLevel = ""; |
| 63 | switch (message.Level) |
| 64 | { |
| 65 | case CecLogLevel.Error: |
| 66 | strLevel = "ERROR: "; |
| 67 | break; |
| 68 | case CecLogLevel.Warning: |
| 69 | strLevel = "WARNING: "; |
| 70 | break; |
| 71 | case CecLogLevel.Notice: |
| 72 | strLevel = "NOTICE: "; |
| 73 | break; |
| 74 | case CecLogLevel.Traffic: |
| 75 | strLevel = "TRAFFIC: "; |
| 76 | break; |
| 77 | case CecLogLevel.Debug: |
| 78 | strLevel = "DEBUG: "; |
| 79 | break; |
| 80 | default: |
| 81 | break; |
| 82 | } |
| 83 | string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine; |
| 84 | tbLog.Text += strLog; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public int ReceiveLogMessage(CecLogMessage message) |
| 90 | { |
| 91 | AddLogMessage(message); |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | delegate void SetControlEnabledCallback(Control control, bool val); |
| 96 | private void SetControlEnabled(Control control, bool val) |
| 97 | { |
| 98 | if (control.InvokeRequired) |
| 99 | { |
| 100 | SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled); |
| 101 | try |
| 102 | { |
| 103 | this.Invoke(d, new object[] { control, val }); |
| 104 | } |
| 105 | catch (Exception) { } |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | control.Enabled = val; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private void SetControlsEnabled(bool val) |
| 114 | { |
| 115 | SetControlEnabled(cbPortNumber, val); |
| 116 | SetControlEnabled(cbConnectedDevice, val); |
| 117 | SetControlEnabled(tbPhysicalAddress, val); |
| 118 | SetControlEnabled(cbDeviceType, val); |
| 119 | SetControlEnabled(cbUseTVMenuLanguage, val); |
| 120 | SetControlEnabled(cbPowerOnStartup, val); |
| 121 | SetControlEnabled(cbPowerOffShutdown, val); |
| 122 | SetControlEnabled(cbPowerOffScreensaver, val); |
| 123 | SetControlEnabled(cbPowerOffOnStandby, val); |
| 124 | SetControlEnabled(bClose, val); |
| 125 | SetControlEnabled(bSave, val); |
| 126 | } |
| 127 | |
| 128 | delegate void SetControlTextCallback(Control control, string val); |
| 129 | private void SetControlText(Control control, string val) |
| 130 | { |
| 131 | if (control.InvokeRequired) |
| 132 | { |
| 133 | SetControlTextCallback d = new SetControlTextCallback(SetControlText); |
| 134 | try |
| 135 | { |
| 136 | this.Invoke(d, new object[] { control, val }); |
| 137 | } |
| 138 | catch (Exception) { } |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | control.Text = val; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | delegate void SetCheckboxCheckedCallback(CheckBox control, bool val); |
| 147 | private void SetCheckboxChecked(CheckBox control, bool val) |
| 148 | { |
| 149 | if (control.InvokeRequired) |
| 150 | { |
| 151 | SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked); |
| 152 | try |
| 153 | { |
| 154 | this.Invoke(d, new object[] { control, val }); |
| 155 | } |
| 156 | catch (Exception) { } |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | control.Checked = val; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | delegate void SetProgressValueCallback(ProgressBar control, int val); |
| 165 | private void SetProgressValue(ProgressBar control, int val) |
| 166 | { |
| 167 | if (control.InvokeRequired) |
| 168 | { |
| 169 | SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue); |
| 170 | try |
| 171 | { |
| 172 | this.Invoke(d, new object[] { control, val }); |
| 173 | } |
| 174 | catch (Exception) { } |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | control.Value = val; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val); |
| 183 | private void SetComboBoxItems(ComboBox control, string selectedText, object[] val) |
| 184 | { |
| 185 | if (control.InvokeRequired) |
| 186 | { |
| 187 | SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems); |
| 188 | try |
| 189 | { |
| 190 | this.Invoke(d, new object[] { control, selectedText, val }); |
| 191 | } |
| 192 | catch (Exception) { } |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | control.Items.Clear(); |
| 197 | control.Items.AddRange(val); |
| 198 | control.Text = selectedText; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private void ProcessEventHandler(object src, UpdateEvent updateEvent) |
| 203 | { |
| 204 | switch (updateEvent.Type) |
| 205 | { |
| 206 | case UpdateEventType.StatusText: |
| 207 | SetControlText(lStatus, updateEvent.StringValue); |
| 208 | break; |
| 209 | case UpdateEventType.PhysicalAddress: |
| 210 | Config.PhysicalAddress = (ushort)updateEvent.IntValue; |
| 211 | SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue)); |
| 212 | break; |
| 213 | case UpdateEventType.ProgressBar: |
| 214 | SetProgressValue(pProgress, updateEvent.IntValue); |
| 215 | break; |
| 216 | case UpdateEventType.TVVendorId: |
| 217 | TVVendor = (CecVendorId)updateEvent.IntValue; |
| 218 | UpdateSelectedDevice(); |
| 219 | break; |
| 220 | case UpdateEventType.BaseDevicePhysicalAddress: |
| 221 | SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue)); |
| 222 | break; |
| 223 | case UpdateEventType.BaseDevice: |
| 224 | Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue; |
| 225 | break; |
| 226 | case UpdateEventType.HDMIPort: |
| 227 | Config.HDMIPort = (byte)updateEvent.IntValue; |
| 228 | break; |
| 229 | case UpdateEventType.MenuLanguage: |
| 230 | SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : "")); |
| 231 | break; |
| 232 | case UpdateEventType.HasAVRDevice: |
| 233 | if (HasAVRDevice != updateEvent.BoolValue) |
| 234 | { |
| 235 | HasAVRDevice = updateEvent.BoolValue; |
| 236 | UpdateSelectedDevice(); |
| 237 | } |
| 238 | break; |
| 239 | case UpdateEventType.AVRVendorId: |
| 240 | AVRVendor = (CecVendorId)updateEvent.IntValue; |
| 241 | UpdateSelectedDevice(); |
| 242 | break; |
| 243 | case UpdateEventType.Configuration: |
| 244 | Config = updateEvent.ConfigValue; |
| 245 | SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress)); |
| 246 | SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString); |
| 247 | SetControlText(cbPortNumber, Config.HDMIPort.ToString()); |
| 248 | SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage); |
| 249 | SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup); |
| 250 | SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown); |
| 251 | SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver); |
| 252 | SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby); |
| 253 | UpdateSelectedDevice(); |
| 254 | break; |
| 255 | case UpdateEventType.ProcessCompleted: |
| 256 | ActiveProcess = null; |
| 257 | SetControlsEnabled(true); |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | private void UpdateSelectedDevice() |
| 263 | { |
| 264 | if (HasAVRDevice) |
| 265 | SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString }); |
| 266 | else |
| 267 | SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString }); |
| 268 | } |
| 269 | |
| 270 | public string TVVendorString |
| 271 | { |
| 272 | get |
| 273 | { |
| 274 | return TVVendor != CecVendorId.Unknown ? |
| 275 | "Television (" + Lib.ToString(TVVendor) + ")" : |
| 276 | "Television"; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | public string AVRVendorString |
| 281 | { |
| 282 | get |
| 283 | { |
| 284 | return AVRVendor != CecVendorId.Unknown ? |
| 285 | "AVR (" + Lib.ToString(AVRVendor) + ")" : |
| 286 | "AVR"; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | protected bool HasAVRDevice; |
| 291 | protected CecVendorId TVVendor = CecVendorId.Unknown; |
| 292 | protected CecVendorId AVRVendor = CecVendorId.Unknown; |
| 293 | protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown; |
| 294 | protected int LogLevel = (int)CecLogLevel.All; |
| 295 | |
| 296 | protected LibCECConfiguration Config; |
| 297 | protected LibCecSharp Lib; |
| 298 | private CecCallbackWrapper Callbacks; |
| 299 | private UpdateProcess ActiveProcess = null; |
| 300 | |
| 301 | private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e) |
| 302 | { |
| 303 | if (ActiveProcess == null) |
| 304 | { |
| 305 | SetControlsEnabled(false); |
| 306 | SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv; |
| 307 | int iPortNumber = 0; |
| 308 | if (!int.TryParse(cbPortNumber.Text, out iPortNumber)) |
| 309 | iPortNumber = 1; |
| 310 | ActiveProcess = new UpdateConnectedDevice(ref Lib, cbConnectedDevice.Text.Equals(AVRVendorString) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv, iPortNumber); |
| 311 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 312 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | private void bCancel_Click(object sender, EventArgs e) |
| 317 | { |
| 318 | this.Dispose(); |
| 319 | } |
| 320 | |
| 321 | private void bSave_Click(object sender, EventArgs e) |
| 322 | { |
| 323 | SetControlsEnabled(false); |
| 324 | |
| 325 | Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked; |
| 326 | Config.PowerOnStartup = cbPowerOnStartup.Checked; |
| 327 | Config.PowerOffShutdown = cbPowerOffShutdown.Checked; |
| 328 | Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked; |
| 329 | Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked; |
| 330 | |
| 331 | if (!Lib.CanPersistConfiguration()) |
| 332 | { |
| 333 | if (ActiveProcess == null) |
| 334 | { |
| 335 | SetControlsEnabled(false); |
| 336 | string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data"; |
| 337 | string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); |
| 338 | |
| 339 | SaveFileDialog dialog = new SaveFileDialog() |
| 340 | { |
| 341 | Title = "Where do you want to store the settings?", |
| 342 | InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir, |
| 343 | FileName = "usb_2548_1001.xml", |
| 344 | Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*", |
| 345 | FilterIndex = 1 |
| 346 | }; |
| 347 | |
| 348 | if (dialog.ShowDialog() == DialogResult.OK) |
| 349 | { |
| 350 | FileStream fs = (FileStream)dialog.OpenFile(); |
| 351 | if (fs == null) |
| 352 | { |
| 353 | MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | StreamWriter writer = new StreamWriter(fs); |
| 358 | StringBuilder output = new StringBuilder(); |
| 359 | output.AppendLine("<settings>"); |
| 360 | output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />"); |
| 361 | output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />"); |
| 362 | output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />"); |
| 363 | output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />"); |
| 364 | output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />"); |
| 365 | output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />"); |
| 366 | output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />"); |
| 367 | output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />"); |
| 368 | output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />"); |
| 369 | output.AppendLine("<setting id=\"enabled\" value=\"1\" />"); |
| 370 | output.AppendLine("<setting id=\"port\" value=\"\" />"); |
| 371 | output.AppendLine("</settings>"); |
| 372 | writer.Write(output.ToString()); |
| 373 | writer.Close(); |
| 374 | fs.Close(); |
| 375 | fs.Dispose(); |
| 376 | MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 377 | } |
| 378 | } |
| 379 | SetControlsEnabled(true); |
| 380 | } |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | if (!Lib.PersistConfiguration(Config)) |
| 385 | MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 386 | else |
| 387 | MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 388 | } |
| 389 | SetControlsEnabled(true); |
| 390 | } |
| 391 | |
| 392 | private void tbPhysicalAddress_TextChanged(object sender, EventArgs e) |
| 393 | { |
| 394 | if (ActiveProcess == null) |
| 395 | { |
| 396 | if (tbPhysicalAddress.Text.Length != 4) |
| 397 | return; |
| 398 | ushort physicalAddress = 0; |
| 399 | if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress)) |
| 400 | return; |
| 401 | SetControlsEnabled(false); |
| 402 | SetControlText(cbPortNumber, string.Empty); |
| 403 | SetControlText(cbConnectedDevice, string.Empty); |
| 404 | ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress); |
| 405 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 406 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | internal class CecCallbackWrapper : CecCallbackMethods |
| 412 | { |
| 413 | public CecCallbackWrapper(CecConfigGUI gui) |
| 414 | { |
| 415 | Gui = gui; |
| 416 | } |
| 417 | |
| 418 | public override int ReceiveCommand(CecCommand command) |
| 419 | { |
| 420 | return Gui.ReceiveCommand(command); |
| 421 | } |
| 422 | |
| 423 | public override int ReceiveKeypress(CecKeypress key) |
| 424 | { |
| 425 | return Gui.ReceiveKeypress(key); |
| 426 | } |
| 427 | |
| 428 | public override int ReceiveLogMessage(CecLogMessage message) |
| 429 | { |
| 430 | return Gui.ReceiveLogMessage(message); |
| 431 | } |
| 432 | |
| 433 | private CecConfigGUI Gui; |
| 434 | } |
| 435 | } |