| 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 | LoadButtonConfiguration(); |
| 32 | |
| 33 | ActiveProcess = new ConnectToDevice(ref Lib); |
| 34 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 35 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 36 | } |
| 37 | |
| 38 | private void LoadButtonConfiguration() |
| 39 | { |
| 40 | //TODO load the real configuration |
| 41 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select", (new CecSharp.CecKeypress() { Keycode = 0x00 }), string.Empty)); |
| 42 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Up", (new CecSharp.CecKeypress() { Keycode = 0x01 }), string.Empty)); |
| 43 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Down", (new CecSharp.CecKeypress() { Keycode = 0x02 }), string.Empty)); |
| 44 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left", (new CecSharp.CecKeypress() { Keycode = 0x03 }), string.Empty)); |
| 45 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right", (new CecSharp.CecKeypress() { Keycode = 0x04 }), string.Empty)); |
| 46 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Up", (new CecSharp.CecKeypress() { Keycode = 0x05 }), string.Empty)); |
| 47 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Down", (new CecSharp.CecKeypress() { Keycode = 0x06 }), string.Empty)); |
| 48 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Up", (new CecSharp.CecKeypress() { Keycode = 0x07 }), string.Empty)); |
| 49 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Down", (new CecSharp.CecKeypress() { Keycode = 0x08 }), string.Empty)); |
| 50 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Root menu", (new CecSharp.CecKeypress() { Keycode = 0x09 }), string.Empty)); |
| 51 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Setup menu", (new CecSharp.CecKeypress() { Keycode = 0x0A }), string.Empty)); |
| 52 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Contents menu", (new CecSharp.CecKeypress() { Keycode = 0x0B }), string.Empty)); |
| 53 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Favourite menu", (new CecSharp.CecKeypress() { Keycode = 0x0C }), string.Empty)); |
| 54 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Exit", (new CecSharp.CecKeypress() { Keycode = 0x0D }), string.Empty)); |
| 55 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("0", (new CecSharp.CecKeypress() { Keycode = 0x20 }), string.Empty)); |
| 56 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("1", (new CecSharp.CecKeypress() { Keycode = 0x21 }), string.Empty)); |
| 57 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("2", (new CecSharp.CecKeypress() { Keycode = 0x22 }), string.Empty)); |
| 58 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("3", (new CecSharp.CecKeypress() { Keycode = 0x23 }), string.Empty)); |
| 59 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("4", (new CecSharp.CecKeypress() { Keycode = 0x24 }), string.Empty)); |
| 60 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("5", (new CecSharp.CecKeypress() { Keycode = 0x25 }), string.Empty)); |
| 61 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("6", (new CecSharp.CecKeypress() { Keycode = 0x26 }), string.Empty)); |
| 62 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("7", (new CecSharp.CecKeypress() { Keycode = 0x27 }), string.Empty)); |
| 63 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("8", (new CecSharp.CecKeypress() { Keycode = 0x28 }), string.Empty)); |
| 64 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("9", (new CecSharp.CecKeypress() { Keycode = 0x29 }), string.Empty)); |
| 65 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem(".", (new CecSharp.CecKeypress() { Keycode = 0x2A }), string.Empty)); |
| 66 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Enter", (new CecSharp.CecKeypress() { Keycode = 0x2B }), string.Empty)); |
| 67 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Clear", (new CecSharp.CecKeypress() { Keycode = 0x2C }), string.Empty)); |
| 68 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Next favourite", (new CecSharp.CecKeypress() { Keycode = 0x2F }), string.Empty)); |
| 69 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel up", (new CecSharp.CecKeypress() { Keycode = 0x30 }), string.Empty)); |
| 70 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel down", (new CecSharp.CecKeypress() { Keycode = 0x31 }), string.Empty)); |
| 71 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Previous channel", (new CecSharp.CecKeypress() { Keycode = 0x32 }), string.Empty)); |
| 72 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sound select", (new CecSharp.CecKeypress() { Keycode = 0x33 }), string.Empty)); |
| 73 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Input select", (new CecSharp.CecKeypress() { Keycode = 0x34 }), string.Empty)); |
| 74 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Display information", (new CecSharp.CecKeypress() { Keycode = 0x35 }), string.Empty)); |
| 75 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Help", (new CecSharp.CecKeypress() { Keycode = 0x36 }), string.Empty)); |
| 76 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page up", (new CecSharp.CecKeypress() { Keycode = 0x37 }), string.Empty)); |
| 77 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page down", (new CecSharp.CecKeypress() { Keycode = 0x38 }), string.Empty)); |
| 78 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power", (new CecSharp.CecKeypress() { Keycode = 0x40 }), string.Empty)); |
| 79 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume up", (new CecSharp.CecKeypress() { Keycode = 0x41 }), string.Empty)); |
| 80 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume down", (new CecSharp.CecKeypress() { Keycode = 0x42 }), string.Empty)); |
| 81 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute", (new CecSharp.CecKeypress() { Keycode = 0x43 }), string.Empty)); |
| 82 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play", (new CecSharp.CecKeypress() { Keycode = 0x44 }), string.Empty)); |
| 83 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop", (new CecSharp.CecKeypress() { Keycode = 0x45 }), string.Empty)); |
| 84 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause", (new CecSharp.CecKeypress() { Keycode = 0x46 }), string.Empty)); |
| 85 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record", (new CecSharp.CecKeypress() { Keycode = 0x47 }), string.Empty)); |
| 86 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Rewind", (new CecSharp.CecKeypress() { Keycode = 0x48 }), string.Empty)); |
| 87 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Fast forward", (new CecSharp.CecKeypress() { Keycode = 0x49 }), string.Empty)); |
| 88 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Eject", (new CecSharp.CecKeypress() { Keycode = 0x4A }), string.Empty)); |
| 89 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Forward", (new CecSharp.CecKeypress() { Keycode = 0x4B }), string.Empty)); |
| 90 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Backward", (new CecSharp.CecKeypress() { Keycode = 0x4C }), string.Empty)); |
| 91 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop record", (new CecSharp.CecKeypress() { Keycode = 0x4D }), string.Empty)); |
| 92 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record", (new CecSharp.CecKeypress() { Keycode = 0x4E }), string.Empty)); |
| 93 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Angle", (new CecSharp.CecKeypress() { Keycode = 0x50 }), string.Empty)); |
| 94 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sub picture", (new CecSharp.CecKeypress() { Keycode = 0x51 }), string.Empty)); |
| 95 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Video on demand", (new CecSharp.CecKeypress() { Keycode = 0x52 }), string.Empty)); |
| 96 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Electronic program guide", (new CecSharp.CecKeypress() { Keycode = 0x53 }), string.Empty)); |
| 97 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Timer programming", (new CecSharp.CecKeypress() { Keycode = 0x54 }), string.Empty)); |
| 98 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Initial configuration", (new CecSharp.CecKeypress() { Keycode = 0x55 }), string.Empty)); |
| 99 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play (function)", (new CecSharp.CecKeypress() { Keycode = 0x60 }), string.Empty)); |
| 100 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause play (function)", (new CecSharp.CecKeypress() { Keycode = 0x61 }), string.Empty)); |
| 101 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record (function)", (new CecSharp.CecKeypress() { Keycode = 0x62 }), string.Empty)); |
| 102 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record (function)", (new CecSharp.CecKeypress() { Keycode = 0x63 }), string.Empty)); |
| 103 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop (function)", (new CecSharp.CecKeypress() { Keycode = 0x64 }), string.Empty)); |
| 104 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute (function)", (new CecSharp.CecKeypress() { Keycode = 0x65 }), string.Empty)); |
| 105 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Restore volume", (new CecSharp.CecKeypress() { Keycode = 0x66 }), string.Empty)); |
| 106 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Tune", (new CecSharp.CecKeypress() { Keycode = 0x67 }), string.Empty)); |
| 107 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select media", (new CecSharp.CecKeypress() { Keycode = 0x68 }), string.Empty)); |
| 108 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select AV input", (new CecSharp.CecKeypress() { Keycode = 0x69 }), string.Empty)); |
| 109 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select audio input", (new CecSharp.CecKeypress() { Keycode = 0x6A }), string.Empty)); |
| 110 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power toggle", (new CecSharp.CecKeypress() { Keycode = 0x6B }), string.Empty)); |
| 111 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power off", (new CecSharp.CecKeypress() { Keycode = 0x6C }), string.Empty)); |
| 112 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power on", (new CecSharp.CecKeypress() { Keycode = 0x6D }), string.Empty)); |
| 113 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F1 (blue)", (new CecSharp.CecKeypress() { Keycode = 0x71 }), string.Empty)); |
| 114 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F2 (red)", (new CecSharp.CecKeypress() { Keycode = 0x72 }), string.Empty)); |
| 115 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F3 (green)", (new CecSharp.CecKeypress() { Keycode = 0x73 }), string.Empty)); |
| 116 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F4 (yellow)", (new CecSharp.CecKeypress() { Keycode = 0x74 }), string.Empty)); |
| 117 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F5", (new CecSharp.CecKeypress() { Keycode = 0x75 }), string.Empty)); |
| 118 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Data", (new CecSharp.CecKeypress() { Keycode = 0x76 }), string.Empty)); |
| 119 | this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecSharp.CecKeypress() { Keycode = 0x91 }), string.Empty)); |
| 120 | } |
| 121 | |
| 122 | public int ReceiveCommand(CecCommand command) |
| 123 | { |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | public int ReceiveKeypress(CecKeypress key) |
| 128 | { |
| 129 | SelectKeypressRow(key); |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | delegate void SelectKeypressRowCallback(CecKeypress key); |
| 134 | private void SelectKeypressRow(CecKeypress key) |
| 135 | { |
| 136 | if (dgButtons.InvokeRequired) |
| 137 | { |
| 138 | SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow); |
| 139 | try |
| 140 | { |
| 141 | this.Invoke(d, new object[] { key }); |
| 142 | } |
| 143 | catch (Exception) { } |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | int rowIndex = -1; |
| 148 | foreach (DataGridViewRow row in dgButtons.Rows) |
| 149 | { |
| 150 | CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem; |
| 151 | if (item != null && item.Key.Keycode == key.Keycode) |
| 152 | { |
| 153 | rowIndex = row.Index; |
| 154 | row.Selected = true; |
| 155 | item.Enabled = true; |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | row.Selected = false; |
| 160 | } |
| 161 | } |
| 162 | if (rowIndex > -1) |
| 163 | dgButtons.FirstDisplayedScrollingRowIndex = rowIndex; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | delegate void AddLogMessageCallback(CecLogMessage message); |
| 168 | private void AddLogMessage(CecLogMessage message) |
| 169 | { |
| 170 | if (tbLog.InvokeRequired) |
| 171 | { |
| 172 | AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage); |
| 173 | try |
| 174 | { |
| 175 | this.Invoke(d, new object[] { message }); |
| 176 | } |
| 177 | catch (Exception) { } |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | string strLevel = ""; |
| 182 | bool display = false; |
| 183 | switch (message.Level) |
| 184 | { |
| 185 | case CecLogLevel.Error: |
| 186 | strLevel = "ERROR: "; |
| 187 | display = cbLogError.Checked; |
| 188 | break; |
| 189 | case CecLogLevel.Warning: |
| 190 | strLevel = "WARNING: "; |
| 191 | display = cbLogWarning.Checked; |
| 192 | break; |
| 193 | case CecLogLevel.Notice: |
| 194 | strLevel = "NOTICE: "; |
| 195 | display = cbLogNotice.Checked; |
| 196 | break; |
| 197 | case CecLogLevel.Traffic: |
| 198 | strLevel = "TRAFFIC: "; |
| 199 | display = cbLogTraffic.Checked; |
| 200 | break; |
| 201 | case CecLogLevel.Debug: |
| 202 | strLevel = "DEBUG: "; |
| 203 | display = cbLogDebug.Checked; |
| 204 | break; |
| 205 | default: |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | if (display) |
| 210 | { |
| 211 | string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine; |
| 212 | tbLog.Text += strLog; |
| 213 | tbLog.Select(tbLog.Text.Length, 0); |
| 214 | tbLog.ScrollToCaret(); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | public int ReceiveLogMessage(CecLogMessage message) |
| 220 | { |
| 221 | AddLogMessage(message); |
| 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | delegate void SetControlEnabledCallback(Control control, bool val); |
| 226 | private void SetControlEnabled(Control control, bool val) |
| 227 | { |
| 228 | if (control.InvokeRequired) |
| 229 | { |
| 230 | SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled); |
| 231 | try |
| 232 | { |
| 233 | this.Invoke(d, new object[] { control, val }); |
| 234 | } |
| 235 | catch (Exception) { } |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | control.Enabled = val; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | private void SetControlsEnabled(bool val) |
| 244 | { |
| 245 | SetControlEnabled(cbPortNumber, val); |
| 246 | SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false); |
| 247 | SetControlEnabled(tbPhysicalAddress, val); |
| 248 | SetControlEnabled(cbDeviceType, val); |
| 249 | SetControlEnabled(cbUseTVMenuLanguage, val); |
| 250 | SetControlEnabled(cbPowerOnStartup, val); |
| 251 | SetControlEnabled(cbPowerOffShutdown, val); |
| 252 | SetControlEnabled(cbPowerOffScreensaver, val); |
| 253 | SetControlEnabled(cbPowerOffOnStandby, val); |
| 254 | SetControlEnabled(bClose, val); |
| 255 | SetControlEnabled(bSave, val); |
| 256 | } |
| 257 | |
| 258 | delegate void SetControlTextCallback(Control control, string val); |
| 259 | private void SetControlText(Control control, string val) |
| 260 | { |
| 261 | if (control.InvokeRequired) |
| 262 | { |
| 263 | SetControlTextCallback d = new SetControlTextCallback(SetControlText); |
| 264 | try |
| 265 | { |
| 266 | this.Invoke(d, new object[] { control, val }); |
| 267 | } |
| 268 | catch (Exception) { } |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | control.Text = val; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | delegate void SetCheckboxCheckedCallback(CheckBox control, bool val); |
| 277 | private void SetCheckboxChecked(CheckBox control, bool val) |
| 278 | { |
| 279 | if (control.InvokeRequired) |
| 280 | { |
| 281 | SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked); |
| 282 | try |
| 283 | { |
| 284 | this.Invoke(d, new object[] { control, val }); |
| 285 | } |
| 286 | catch (Exception) { } |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | control.Checked = val; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | delegate void SetProgressValueCallback(ProgressBar control, int val); |
| 295 | private void SetProgressValue(ProgressBar control, int val) |
| 296 | { |
| 297 | if (control.InvokeRequired) |
| 298 | { |
| 299 | SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue); |
| 300 | try |
| 301 | { |
| 302 | this.Invoke(d, new object[] { control, val }); |
| 303 | } |
| 304 | catch (Exception) { } |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | control.Value = val; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val); |
| 313 | private void SetComboBoxItems(ComboBox control, string selectedText, object[] val) |
| 314 | { |
| 315 | if (control.InvokeRequired) |
| 316 | { |
| 317 | SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems); |
| 318 | try |
| 319 | { |
| 320 | this.Invoke(d, new object[] { control, selectedText, val }); |
| 321 | } |
| 322 | catch (Exception) { } |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | control.Items.Clear(); |
| 327 | control.Items.AddRange(val); |
| 328 | control.Text = selectedText; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | private void ProcessEventHandler(object src, UpdateEvent updateEvent) |
| 333 | { |
| 334 | switch (updateEvent.Type) |
| 335 | { |
| 336 | case UpdateEventType.StatusText: |
| 337 | SetControlText(lStatus, updateEvent.StringValue); |
| 338 | break; |
| 339 | case UpdateEventType.PhysicalAddress: |
| 340 | Config.PhysicalAddress = (ushort)updateEvent.IntValue; |
| 341 | SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue)); |
| 342 | break; |
| 343 | case UpdateEventType.ProgressBar: |
| 344 | SetProgressValue(pProgress, updateEvent.IntValue); |
| 345 | break; |
| 346 | case UpdateEventType.TVVendorId: |
| 347 | TVVendor = (CecVendorId)updateEvent.IntValue; |
| 348 | UpdateSelectedDevice(); |
| 349 | break; |
| 350 | case UpdateEventType.BaseDevicePhysicalAddress: |
| 351 | SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue)); |
| 352 | break; |
| 353 | case UpdateEventType.BaseDevice: |
| 354 | Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue; |
| 355 | break; |
| 356 | case UpdateEventType.HDMIPort: |
| 357 | Config.HDMIPort = (byte)updateEvent.IntValue; |
| 358 | break; |
| 359 | case UpdateEventType.MenuLanguage: |
| 360 | SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : "")); |
| 361 | break; |
| 362 | case UpdateEventType.HasAVRDevice: |
| 363 | if (HasAVRDevice != updateEvent.BoolValue) |
| 364 | { |
| 365 | HasAVRDevice = updateEvent.BoolValue; |
| 366 | UpdateSelectedDevice(); |
| 367 | } |
| 368 | break; |
| 369 | case UpdateEventType.AVRVendorId: |
| 370 | AVRVendor = (CecVendorId)updateEvent.IntValue; |
| 371 | UpdateSelectedDevice(); |
| 372 | break; |
| 373 | case UpdateEventType.Configuration: |
| 374 | Config = updateEvent.ConfigValue; |
| 375 | SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress)); |
| 376 | SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString); |
| 377 | SetControlText(cbPortNumber, Config.HDMIPort.ToString()); |
| 378 | SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage); |
| 379 | SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup); |
| 380 | SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown); |
| 381 | SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver); |
| 382 | SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby); |
| 383 | UpdateSelectedDevice(); |
| 384 | break; |
| 385 | case UpdateEventType.ProcessCompleted: |
| 386 | ActiveProcess = null; |
| 387 | SetControlsEnabled(true); |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | private void UpdateSelectedDevice() |
| 393 | { |
| 394 | if (HasAVRDevice) |
| 395 | SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString }); |
| 396 | else |
| 397 | SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString }); |
| 398 | } |
| 399 | |
| 400 | public string TVVendorString |
| 401 | { |
| 402 | get |
| 403 | { |
| 404 | return TVVendor != CecVendorId.Unknown ? |
| 405 | "Television (" + Lib.ToString(TVVendor) + ")" : |
| 406 | "Television"; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | public string AVRVendorString |
| 411 | { |
| 412 | get |
| 413 | { |
| 414 | return AVRVendor != CecVendorId.Unknown ? |
| 415 | "AVR (" + Lib.ToString(AVRVendor) + ")" : |
| 416 | "AVR"; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | protected bool HasAVRDevice; |
| 421 | protected CecVendorId TVVendor = CecVendorId.Unknown; |
| 422 | protected CecVendorId AVRVendor = CecVendorId.Unknown; |
| 423 | protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown; |
| 424 | |
| 425 | protected LibCECConfiguration Config; |
| 426 | protected LibCecSharp Lib; |
| 427 | private CecCallbackWrapper Callbacks; |
| 428 | private UpdateProcess ActiveProcess = null; |
| 429 | |
| 430 | private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e) |
| 431 | { |
| 432 | if (ActiveProcess == null) |
| 433 | { |
| 434 | SetControlsEnabled(false); |
| 435 | SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv; |
| 436 | int iPortNumber = 0; |
| 437 | if (!int.TryParse(cbPortNumber.Text, out iPortNumber)) |
| 438 | iPortNumber = 1; |
| 439 | ActiveProcess = new UpdateConnectedDevice(ref Lib, cbConnectedDevice.Text.Equals(AVRVendorString) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv, iPortNumber); |
| 440 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 441 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | private void bCancel_Click(object sender, EventArgs e) |
| 446 | { |
| 447 | this.Dispose(); |
| 448 | } |
| 449 | |
| 450 | private void bSave_Click(object sender, EventArgs e) |
| 451 | { |
| 452 | SetControlsEnabled(false); |
| 453 | |
| 454 | Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked; |
| 455 | Config.PowerOnStartup = cbPowerOnStartup.Checked; |
| 456 | Config.PowerOffShutdown = cbPowerOffShutdown.Checked; |
| 457 | Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked; |
| 458 | Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked; |
| 459 | |
| 460 | if (!Lib.CanPersistConfiguration()) |
| 461 | { |
| 462 | if (ActiveProcess == null) |
| 463 | { |
| 464 | SetControlsEnabled(false); |
| 465 | string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data"; |
| 466 | string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); |
| 467 | |
| 468 | SaveFileDialog dialog = new SaveFileDialog() |
| 469 | { |
| 470 | Title = "Where do you want to store the settings?", |
| 471 | InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir, |
| 472 | FileName = "usb_2548_1001.xml", |
| 473 | Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*", |
| 474 | FilterIndex = 1 |
| 475 | }; |
| 476 | |
| 477 | if (dialog.ShowDialog() == DialogResult.OK) |
| 478 | { |
| 479 | FileStream fs = (FileStream)dialog.OpenFile(); |
| 480 | if (fs == null) |
| 481 | { |
| 482 | MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | StreamWriter writer = new StreamWriter(fs); |
| 487 | StringBuilder output = new StringBuilder(); |
| 488 | output.AppendLine("<settings>"); |
| 489 | output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />"); |
| 490 | output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />"); |
| 491 | output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />"); |
| 492 | output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />"); |
| 493 | output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />"); |
| 494 | output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />"); |
| 495 | output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />"); |
| 496 | output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />"); |
| 497 | output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />"); |
| 498 | output.AppendLine("<setting id=\"enabled\" value=\"1\" />"); |
| 499 | output.AppendLine("<setting id=\"port\" value=\"\" />"); |
| 500 | output.AppendLine("</settings>"); |
| 501 | writer.Write(output.ToString()); |
| 502 | writer.Close(); |
| 503 | fs.Close(); |
| 504 | fs.Dispose(); |
| 505 | MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 506 | } |
| 507 | } |
| 508 | SetControlsEnabled(true); |
| 509 | } |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | if (!Lib.PersistConfiguration(Config)) |
| 514 | MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 515 | else |
| 516 | MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 517 | } |
| 518 | SetControlsEnabled(true); |
| 519 | } |
| 520 | |
| 521 | private void tbPhysicalAddress_TextChanged(object sender, EventArgs e) |
| 522 | { |
| 523 | if (ActiveProcess == null) |
| 524 | { |
| 525 | if (tbPhysicalAddress.Text.Length != 4) |
| 526 | return; |
| 527 | ushort physicalAddress = 0; |
| 528 | if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress)) |
| 529 | return; |
| 530 | SetControlsEnabled(false); |
| 531 | SetControlText(cbPortNumber, string.Empty); |
| 532 | SetControlText(cbConnectedDevice, string.Empty); |
| 533 | ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress); |
| 534 | ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler); |
| 535 | (new Thread(new ThreadStart(ActiveProcess.Run))).Start(); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | private void bClearLog_Click(object sender, EventArgs e) |
| 540 | { |
| 541 | tbLog.Text = string.Empty; |
| 542 | } |
| 543 | |
| 544 | private void bSaveLog_Click(object sender, EventArgs e) |
| 545 | { |
| 546 | SaveFileDialog dialog = new SaveFileDialog() |
| 547 | { |
| 548 | Title = "Where do you want to store the log file?", |
| 549 | InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), |
| 550 | FileName = "cec-log.txt", |
| 551 | Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*", |
| 552 | FilterIndex = 1 |
| 553 | }; |
| 554 | |
| 555 | if (dialog.ShowDialog() == DialogResult.OK) |
| 556 | { |
| 557 | FileStream fs = (FileStream)dialog.OpenFile(); |
| 558 | if (fs == null) |
| 559 | { |
| 560 | MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 561 | } |
| 562 | else |
| 563 | { |
| 564 | StreamWriter writer = new StreamWriter(fs); |
| 565 | writer.Write(tbLog.Text); |
| 566 | writer.Close(); |
| 567 | fs.Close(); |
| 568 | fs.Dispose(); |
| 569 | MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) |
| 575 | { |
| 576 | DataGridView grid = sender as DataGridView; |
| 577 | CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem; |
| 578 | if (data == null || !data.Enabled) |
| 579 | e.CellStyle.ForeColor = Color.Gray; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | internal class CecCallbackWrapper : CecCallbackMethods |
| 584 | { |
| 585 | public CecCallbackWrapper(CecConfigGUI gui) |
| 586 | { |
| 587 | Gui = gui; |
| 588 | } |
| 589 | |
| 590 | public override int ReceiveCommand(CecCommand command) |
| 591 | { |
| 592 | return Gui.ReceiveCommand(command); |
| 593 | } |
| 594 | |
| 595 | public override int ReceiveKeypress(CecKeypress key) |
| 596 | { |
| 597 | return Gui.ReceiveKeypress(key); |
| 598 | } |
| 599 | |
| 600 | public override int ReceiveLogMessage(CecLogMessage message) |
| 601 | { |
| 602 | return Gui.ReceiveLogMessage(message); |
| 603 | } |
| 604 | |
| 605 | private CecConfigGUI Gui; |
| 606 | } |
| 607 | } |