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