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