b8c2b54fa68ca7031ef3b410c72639a63f9e732c
[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 : Form
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.", "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 "physical_address":
95 {
96 ushort physicalAddress = 0;
97 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
98 config.PhysicalAddress = physicalAddress;
99 }
100 break;
101 case "device_type":
102 {
103 ushort iType;
104 if (ushort.TryParse(value, out iType))
105 config.DeviceTypes.Types[0] = (CecDeviceType)iType;
106 }
107 break;
108 case "cec_power_on_startup":
109 config.PowerOnStartup = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
110 break;
111 case "cec_power_off_shutdown":
112 config.PowerOffShutdown = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
113 break;
114 case "cec_standby_screensaver":
115 config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
116 break;
117 case "standby_pc_on_tv_standby":
118 config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
119 break;
120 case "use_tv_menu_language":
121 config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
122 break;
123 case "enabled":
124 break;
125 case "port":
126 //TODO
127 break;
128 default:
129 break;
130 }
131 }
132 break;
133 default:
134 break;
135 }
136 }
137 }
138 return gotConfig;
139 }
140
141 private void LoadButtonConfiguration()
142 {
143 //TODO load the real configuration
144 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select", (new CecSharp.CecKeypress() { Keycode = 0x00 }), string.Empty));
145 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Up", (new CecSharp.CecKeypress() { Keycode = 0x01 }), string.Empty));
146 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Down", (new CecSharp.CecKeypress() { Keycode = 0x02 }), string.Empty));
147 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left", (new CecSharp.CecKeypress() { Keycode = 0x03 }), string.Empty));
148 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right", (new CecSharp.CecKeypress() { Keycode = 0x04 }), string.Empty));
149 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Up", (new CecSharp.CecKeypress() { Keycode = 0x05 }), string.Empty));
150 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Down", (new CecSharp.CecKeypress() { Keycode = 0x06 }), string.Empty));
151 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Up", (new CecSharp.CecKeypress() { Keycode = 0x07 }), string.Empty));
152 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Down", (new CecSharp.CecKeypress() { Keycode = 0x08 }), string.Empty));
153 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Root menu", (new CecSharp.CecKeypress() { Keycode = 0x09 }), string.Empty));
154 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Setup menu", (new CecSharp.CecKeypress() { Keycode = 0x0A }), string.Empty));
155 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Contents menu", (new CecSharp.CecKeypress() { Keycode = 0x0B }), string.Empty));
156 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Favourite menu", (new CecSharp.CecKeypress() { Keycode = 0x0C }), string.Empty));
157 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Exit", (new CecSharp.CecKeypress() { Keycode = 0x0D }), string.Empty));
158 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("0", (new CecSharp.CecKeypress() { Keycode = 0x20 }), string.Empty));
159 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("1", (new CecSharp.CecKeypress() { Keycode = 0x21 }), string.Empty));
160 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("2", (new CecSharp.CecKeypress() { Keycode = 0x22 }), string.Empty));
161 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("3", (new CecSharp.CecKeypress() { Keycode = 0x23 }), string.Empty));
162 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("4", (new CecSharp.CecKeypress() { Keycode = 0x24 }), string.Empty));
163 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("5", (new CecSharp.CecKeypress() { Keycode = 0x25 }), string.Empty));
164 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("6", (new CecSharp.CecKeypress() { Keycode = 0x26 }), string.Empty));
165 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("7", (new CecSharp.CecKeypress() { Keycode = 0x27 }), string.Empty));
166 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("8", (new CecSharp.CecKeypress() { Keycode = 0x28 }), string.Empty));
167 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("9", (new CecSharp.CecKeypress() { Keycode = 0x29 }), string.Empty));
168 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem(".", (new CecSharp.CecKeypress() { Keycode = 0x2A }), string.Empty));
169 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Enter", (new CecSharp.CecKeypress() { Keycode = 0x2B }), string.Empty));
170 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Clear", (new CecSharp.CecKeypress() { Keycode = 0x2C }), string.Empty));
171 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Next favourite", (new CecSharp.CecKeypress() { Keycode = 0x2F }), string.Empty));
172 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel up", (new CecSharp.CecKeypress() { Keycode = 0x30 }), string.Empty));
173 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel down", (new CecSharp.CecKeypress() { Keycode = 0x31 }), string.Empty));
174 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Previous channel", (new CecSharp.CecKeypress() { Keycode = 0x32 }), string.Empty));
175 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sound select", (new CecSharp.CecKeypress() { Keycode = 0x33 }), string.Empty));
176 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Input select", (new CecSharp.CecKeypress() { Keycode = 0x34 }), string.Empty));
177 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Display information", (new CecSharp.CecKeypress() { Keycode = 0x35 }), string.Empty));
178 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Help", (new CecSharp.CecKeypress() { Keycode = 0x36 }), string.Empty));
179 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page up", (new CecSharp.CecKeypress() { Keycode = 0x37 }), string.Empty));
180 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page down", (new CecSharp.CecKeypress() { Keycode = 0x38 }), string.Empty));
181 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power", (new CecSharp.CecKeypress() { Keycode = 0x40 }), string.Empty));
182 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume up", (new CecSharp.CecKeypress() { Keycode = 0x41 }), string.Empty));
183 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume down", (new CecSharp.CecKeypress() { Keycode = 0x42 }), string.Empty));
184 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute", (new CecSharp.CecKeypress() { Keycode = 0x43 }), string.Empty));
185 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play", (new CecSharp.CecKeypress() { Keycode = 0x44 }), string.Empty));
186 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop", (new CecSharp.CecKeypress() { Keycode = 0x45 }), string.Empty));
187 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause", (new CecSharp.CecKeypress() { Keycode = 0x46 }), string.Empty));
188 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record", (new CecSharp.CecKeypress() { Keycode = 0x47 }), string.Empty));
189 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Rewind", (new CecSharp.CecKeypress() { Keycode = 0x48 }), string.Empty));
190 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Fast forward", (new CecSharp.CecKeypress() { Keycode = 0x49 }), string.Empty));
191 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Eject", (new CecSharp.CecKeypress() { Keycode = 0x4A }), string.Empty));
192 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Forward", (new CecSharp.CecKeypress() { Keycode = 0x4B }), string.Empty));
193 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Backward", (new CecSharp.CecKeypress() { Keycode = 0x4C }), string.Empty));
194 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop record", (new CecSharp.CecKeypress() { Keycode = 0x4D }), string.Empty));
195 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record", (new CecSharp.CecKeypress() { Keycode = 0x4E }), string.Empty));
196 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Angle", (new CecSharp.CecKeypress() { Keycode = 0x50 }), string.Empty));
197 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sub picture", (new CecSharp.CecKeypress() { Keycode = 0x51 }), string.Empty));
198 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Video on demand", (new CecSharp.CecKeypress() { Keycode = 0x52 }), string.Empty));
199 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Electronic program guide", (new CecSharp.CecKeypress() { Keycode = 0x53 }), string.Empty));
200 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Timer programming", (new CecSharp.CecKeypress() { Keycode = 0x54 }), string.Empty));
201 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Initial configuration", (new CecSharp.CecKeypress() { Keycode = 0x55 }), string.Empty));
202 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play (function)", (new CecSharp.CecKeypress() { Keycode = 0x60 }), string.Empty));
203 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause play (function)", (new CecSharp.CecKeypress() { Keycode = 0x61 }), string.Empty));
204 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record (function)", (new CecSharp.CecKeypress() { Keycode = 0x62 }), string.Empty));
205 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record (function)", (new CecSharp.CecKeypress() { Keycode = 0x63 }), string.Empty));
206 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop (function)", (new CecSharp.CecKeypress() { Keycode = 0x64 }), string.Empty));
207 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute (function)", (new CecSharp.CecKeypress() { Keycode = 0x65 }), string.Empty));
208 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Restore volume", (new CecSharp.CecKeypress() { Keycode = 0x66 }), string.Empty));
209 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Tune", (new CecSharp.CecKeypress() { Keycode = 0x67 }), string.Empty));
210 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select media", (new CecSharp.CecKeypress() { Keycode = 0x68 }), string.Empty));
211 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select AV input", (new CecSharp.CecKeypress() { Keycode = 0x69 }), string.Empty));
212 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select audio input", (new CecSharp.CecKeypress() { Keycode = 0x6A }), string.Empty));
213 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power toggle", (new CecSharp.CecKeypress() { Keycode = 0x6B }), string.Empty));
214 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power off", (new CecSharp.CecKeypress() { Keycode = 0x6C }), string.Empty));
215 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power on", (new CecSharp.CecKeypress() { Keycode = 0x6D }), string.Empty));
216 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F1 (blue)", (new CecSharp.CecKeypress() { Keycode = 0x71 }), string.Empty));
217 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F2 (red)", (new CecSharp.CecKeypress() { Keycode = 0x72 }), string.Empty));
218 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F3 (green)", (new CecSharp.CecKeypress() { Keycode = 0x73 }), string.Empty));
219 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F4 (yellow)", (new CecSharp.CecKeypress() { Keycode = 0x74 }), string.Empty));
220 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F5", (new CecSharp.CecKeypress() { Keycode = 0x75 }), string.Empty));
221 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Data", (new CecSharp.CecKeypress() { Keycode = 0x76 }), string.Empty));
222 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecSharp.CecKeypress() { Keycode = 0x91 }), string.Empty));
223 }
224
225 public int ReceiveCommand(CecCommand command)
226 {
227 bool bGetNewPhysicalAddress = false;
228 if (command.Opcode == CecOpcode.ReportPhysicalAddress)
229 bGetNewPhysicalAddress = true;
230
231 if (bGetNewPhysicalAddress)
232 {
233 LibCECConfiguration config = new LibCECConfiguration();
234 Lib.GetCurrentConfiguration(config);
235 SetControlText(this.tbPhysicalAddress, String.Format("{0,4:X}", config.PhysicalAddress));
236 }
237 return 1;
238 }
239
240 public int ReceiveKeypress(CecKeypress key)
241 {
242 SelectKeypressRow(key);
243 return 1;
244 }
245
246 delegate void SelectKeypressRowCallback(CecKeypress key);
247 private void SelectKeypressRow(CecKeypress key)
248 {
249 if (dgButtons.InvokeRequired)
250 {
251 SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
252 try
253 {
254 this.Invoke(d, new object[] { key });
255 }
256 catch (Exception) { }
257 }
258 else
259 {
260 int rowIndex = -1;
261 foreach (DataGridViewRow row in dgButtons.Rows)
262 {
263 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
264 if (item != null && item.Key.Keycode == key.Keycode)
265 {
266 rowIndex = row.Index;
267 row.Selected = true;
268 item.Enabled = true;
269 }
270 else
271 {
272 row.Selected = false;
273 }
274 }
275 if (rowIndex > -1)
276 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
277 }
278 }
279
280 delegate void AddLogMessageCallback(CecLogMessage message);
281 private void AddLogMessage(CecLogMessage message)
282 {
283 if (tbLog.InvokeRequired)
284 {
285 AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage);
286 try
287 {
288 this.Invoke(d, new object[] { message });
289 }
290 catch (Exception) { }
291 }
292 else
293 {
294 string strLevel = "";
295 bool display = false;
296 switch (message.Level)
297 {
298 case CecLogLevel.Error:
299 strLevel = "ERROR: ";
300 display = cbLogError.Checked;
301 break;
302 case CecLogLevel.Warning:
303 strLevel = "WARNING: ";
304 display = cbLogWarning.Checked;
305 break;
306 case CecLogLevel.Notice:
307 strLevel = "NOTICE: ";
308 display = cbLogNotice.Checked;
309 break;
310 case CecLogLevel.Traffic:
311 strLevel = "TRAFFIC: ";
312 display = cbLogTraffic.Checked;
313 break;
314 case CecLogLevel.Debug:
315 strLevel = "DEBUG: ";
316 display = cbLogDebug.Checked;
317 break;
318 default:
319 break;
320 }
321
322 if (display)
323 {
324 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
325 tbLog.Text += strLog;
326 tbLog.Select(tbLog.Text.Length, 0);
327 tbLog.ScrollToCaret();
328 }
329 }
330 }
331
332 public int ReceiveLogMessage(CecLogMessage message)
333 {
334 AddLogMessage(message);
335 return 1;
336 }
337
338 delegate void SetControlEnabledCallback(Control control, bool val);
339 private void SetControlEnabled(Control control, bool val)
340 {
341 if (control.InvokeRequired)
342 {
343 SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled);
344 try
345 {
346 this.Invoke(d, new object[] { control, val });
347 }
348 catch (Exception) { }
349 }
350 else
351 {
352 control.Enabled = val;
353 }
354 }
355
356 private void SetControlsEnabled(bool val)
357 {
358 SetControlEnabled(cbPortNumber, val);
359 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
360 SetControlEnabled(tbPhysicalAddress, val);
361 SetControlEnabled(cbDeviceType, false); // TODO not implemented yet
362 SetControlEnabled(cbUseTVMenuLanguage, val);
363 SetControlEnabled(cbPowerOnStartup, val);
364 SetControlEnabled(cbPowerOffShutdown, val);
365 SetControlEnabled(cbPowerOffScreensaver, val);
366 SetControlEnabled(cbPowerOffOnStandby, val);
367 SetControlEnabled(cbWakeDevices, false); // TODO not implemented yet
368 SetControlEnabled(bClose, val);
369 SetControlEnabled(bSave, val);
370
371 SetControlEnabled(bSendImageViewOn, val);
372 SetControlEnabled(bStandby, val);
373 SetControlEnabled(bActivateSource, val);
374 SetControlEnabled(bScan, val);
375
376 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
377 SetControlEnabled(bVolUp, enableVolumeButtons);
378 SetControlEnabled(bVolDown, enableVolumeButtons);
379 SetControlEnabled(bMute, enableVolumeButtons);
380 }
381
382 delegate void SetControlTextCallback(Control control, string val);
383 private void SetControlText(Control control, string val)
384 {
385 if (control.InvokeRequired)
386 {
387 SetControlTextCallback d = new SetControlTextCallback(SetControlText);
388 try
389 {
390 this.Invoke(d, new object[] { control, val });
391 }
392 catch (Exception) { }
393 }
394 else
395 {
396 control.Text = val;
397 }
398 }
399
400 delegate void SetCheckboxCheckedCallback(CheckBox control, bool val);
401 private void SetCheckboxChecked(CheckBox control, bool val)
402 {
403 if (control.InvokeRequired)
404 {
405 SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked);
406 try
407 {
408 this.Invoke(d, new object[] { control, val });
409 }
410 catch (Exception) { }
411 }
412 else
413 {
414 control.Checked = val;
415 }
416 }
417
418 delegate void SetProgressValueCallback(ProgressBar control, int val);
419 private void SetProgressValue(ProgressBar control, int val)
420 {
421 if (control.InvokeRequired)
422 {
423 SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue);
424 try
425 {
426 this.Invoke(d, new object[] { control, val });
427 }
428 catch (Exception) { }
429 }
430 else
431 {
432 control.Value = val;
433 }
434 }
435
436 delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val);
437 private void SetComboBoxItems(ComboBox control, string selectedText, object[] val)
438 {
439 if (control.InvokeRequired)
440 {
441 SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems);
442 try
443 {
444 this.Invoke(d, new object[] { control, selectedText, val });
445 }
446 catch (Exception) { }
447 }
448 else
449 {
450 control.Items.Clear();
451 control.Items.AddRange(val);
452 control.Text = selectedText;
453 }
454 }
455
456 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
457 {
458 switch (updateEvent.Type)
459 {
460 case UpdateEventType.StatusText:
461 SetControlText(lStatus, updateEvent.StringValue);
462 break;
463 case UpdateEventType.PhysicalAddress:
464 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
465 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
466 break;
467 case UpdateEventType.ProgressBar:
468 SetProgressValue(pProgress, updateEvent.IntValue);
469 break;
470 case UpdateEventType.TVVendorId:
471 TVVendor = (CecVendorId)updateEvent.IntValue;
472 UpdateSelectedDevice();
473 break;
474 case UpdateEventType.BaseDevicePhysicalAddress:
475 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
476 break;
477 case UpdateEventType.BaseDevice:
478 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
479 break;
480 case UpdateEventType.HDMIPort:
481 Config.HDMIPort = (byte)updateEvent.IntValue;
482 break;
483 case UpdateEventType.MenuLanguage:
484 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
485 break;
486 case UpdateEventType.HasAVRDevice:
487 if (HasAVRDevice != updateEvent.BoolValue)
488 {
489 HasAVRDevice = updateEvent.BoolValue;
490 UpdateSelectedDevice();
491 }
492 break;
493 case UpdateEventType.AVRVendorId:
494 AVRVendor = (CecVendorId)updateEvent.IntValue;
495 UpdateSelectedDevice();
496 break;
497 case UpdateEventType.Configuration:
498 Config = updateEvent.ConfigValue;
499 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
500 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
501 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
502 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
503 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
504 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
505 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
506 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
507 UpdateSelectedDevice();
508 break;
509 case UpdateEventType.ProcessCompleted:
510 ActiveProcess = null;
511 SetControlsEnabled(true);
512 break;
513 }
514 }
515
516 private void UpdateSelectedDevice()
517 {
518 if (HasAVRDevice)
519 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
520 else
521 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
522 }
523
524 public string TVVendorString
525 {
526 get
527 {
528 return TVVendor != CecVendorId.Unknown ?
529 "Television (" + Lib.ToString(TVVendor) + ")" :
530 "Television";
531 }
532 }
533
534 public string AVRVendorString
535 {
536 get
537 {
538 return AVRVendor != CecVendorId.Unknown ?
539 "AVR (" + Lib.ToString(AVRVendor) + ")" :
540 "AVR";
541 }
542 }
543
544 protected bool HasAVRDevice;
545 protected CecVendorId TVVendor = CecVendorId.Unknown;
546 protected CecVendorId AVRVendor = CecVendorId.Unknown;
547 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
548
549 protected LibCECConfiguration Config;
550 protected LibCecSharp Lib;
551 private CecCallbackWrapper Callbacks;
552 private UpdateProcess ActiveProcess = null;
553
554 public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
555 {
556 if (ActiveProcess == null)
557 {
558 SetControlsEnabled(false);
559 ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber);
560 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
561 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
562 }
563 }
564
565 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
566 {
567 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
568 int iPortNumber = 0;
569 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
570 iPortNumber = 1;
571 SetConnectedDevice(SelectedConnectedDevice, iPortNumber);
572 }
573
574 private void bCancel_Click(object sender, EventArgs e)
575 {
576 this.Dispose();
577 }
578
579 private void bSave_Click(object sender, EventArgs e)
580 {
581 SetControlsEnabled(false);
582
583 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
584 Config.PowerOnStartup = cbPowerOnStartup.Checked;
585 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
586 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
587 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
588
589 if (!Lib.CanPersistConfiguration())
590 {
591 if (ActiveProcess == null)
592 {
593 SetControlsEnabled(false);
594 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
595 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
596
597 SaveFileDialog dialog = new SaveFileDialog()
598 {
599 Title = "Where do you want to store the settings?",
600 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
601 FileName = "usb_2548_1001.xml",
602 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
603 FilterIndex = 1
604 };
605
606 if (dialog.ShowDialog() == DialogResult.OK)
607 {
608 FileStream fs = (FileStream)dialog.OpenFile();
609 if (fs == null)
610 {
611 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
612 }
613 else
614 {
615 StreamWriter writer = new StreamWriter(fs);
616 StringBuilder output = new StringBuilder();
617 output.AppendLine("<settings>");
618 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
619 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
620 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
621 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
622 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
623 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
624 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
625 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
626 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
627 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
628 output.AppendLine("<setting id=\"port\" value=\"\" />");
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 }
649
650 public void SetPhysicalAddress(ushort physicalAddress)
651 {
652 if (ActiveProcess == null)
653 {
654 SetControlsEnabled(false);
655 SetControlText(cbPortNumber, string.Empty);
656 SetControlText(cbConnectedDevice, string.Empty);
657 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
658 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
659 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
660 }
661 }
662
663 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
664 {
665 if (tbPhysicalAddress.Text.Length != 4)
666 return;
667 ushort physicalAddress = 0;
668 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
669 return;
670
671 SetPhysicalAddress(physicalAddress);
672 }
673
674 private void bClearLog_Click(object sender, EventArgs e)
675 {
676 tbLog.Text = string.Empty;
677 }
678
679 private void bSaveLog_Click(object sender, EventArgs e)
680 {
681 SaveFileDialog dialog = new SaveFileDialog()
682 {
683 Title = "Where do you want to store the log file?",
684 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
685 FileName = "cec-log.txt",
686 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
687 FilterIndex = 1
688 };
689
690 if (dialog.ShowDialog() == DialogResult.OK)
691 {
692 FileStream fs = (FileStream)dialog.OpenFile();
693 if (fs == null)
694 {
695 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
696 }
697 else
698 {
699 StreamWriter writer = new StreamWriter(fs);
700 writer.Write(tbLog.Text);
701 writer.Close();
702 fs.Close();
703 fs.Dispose();
704 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
705 }
706 }
707 }
708
709 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
710 {
711 DataGridView grid = sender as DataGridView;
712 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
713 if (data == null || !data.Enabled)
714 e.CellStyle.ForeColor = Color.Gray;
715 }
716
717 delegate CecLogicalAddress GetTargetDeviceCallback();
718 private CecLogicalAddress GetTargetDevice()
719 {
720 if (this.cbCommandDestination.InvokeRequired)
721 {
722 GetTargetDeviceCallback d = new GetTargetDeviceCallback(GetTargetDevice);
723 CecLogicalAddress retval = CecLogicalAddress.Unknown;
724 try
725 {
726 retval = (CecLogicalAddress)this.Invoke(d, new object[] { });
727 }
728 catch (Exception) { }
729 return retval;
730 }
731
732 switch (this.cbCommandDestination.Text.Substring(0, 1).ToLower())
733 {
734 case "0":
735 return CecLogicalAddress.Tv;
736 case "1":
737 return CecLogicalAddress.RecordingDevice1;
738 case "2":
739 return CecLogicalAddress.RecordingDevice2;
740 case "3":
741 return CecLogicalAddress.Tuner1;
742 case "4":
743 return CecLogicalAddress.PlaybackDevice1;
744 case "5":
745 return CecLogicalAddress.AudioSystem;
746 case "6":
747 return CecLogicalAddress.Tuner2;
748 case "7":
749 return CecLogicalAddress.Tuner3;
750 case "8":
751 return CecLogicalAddress.PlaybackDevice2;
752 case "9":
753 return CecLogicalAddress.RecordingDevice3;
754 case "a":
755 return CecLogicalAddress.Tuner4;
756 case "b":
757 return CecLogicalAddress.PlaybackDevice3;
758 case "c":
759 return CecLogicalAddress.Reserved1;
760 case "d":
761 return CecLogicalAddress.Reserved2;
762 case "e":
763 return CecLogicalAddress.FreeUse;
764 case "f":
765 return CecLogicalAddress.Broadcast;
766 default:
767 return CecLogicalAddress.Unknown;
768 }
769 }
770
771 public void SendImageViewOn(CecLogicalAddress address)
772 {
773 if (ActiveProcess == null)
774 {
775 SetControlsEnabled(false);
776 ActiveProcess = new SendImageViewOn(ref Lib, address);
777 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
778 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
779 }
780 }
781
782 private void bSendImageViewOn_Click(object sender, EventArgs e)
783 {
784 SendImageViewOn(GetTargetDevice());
785 }
786
787 public void SendStandby(CecLogicalAddress address)
788 {
789 if (ActiveProcess == null)
790 {
791 SetControlsEnabled(false);
792 ActiveProcess = new SendStandby(ref Lib, address);
793 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
794 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
795 }
796 }
797
798 private void bStandby_Click(object sender, EventArgs e)
799 {
800 SendStandby(GetTargetDevice());
801 }
802
803 public void ShowDeviceInfo(CecLogicalAddress address)
804 {
805 if (ActiveProcess == null)
806 {
807 SetControlsEnabled(false);
808 ActiveProcess = new ShowDeviceInfo(this, ref Lib, address);
809 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
810 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
811 }
812 }
813
814 private void bScan_Click(object sender, EventArgs e)
815 {
816 ShowDeviceInfo(GetTargetDevice());
817 }
818
819 public void ActivateSource(CecLogicalAddress address)
820 {
821 if (ActiveProcess == null)
822 {
823 SetControlsEnabled(false);
824 ActiveProcess = new SendActivateSource(ref Lib, address);
825 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
826 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
827 }
828 }
829
830 private void bActivateSource_Click(object sender, EventArgs e)
831 {
832 ActivateSource(GetTargetDevice());
833 }
834
835 private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e)
836 {
837 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
838 this.bVolUp.Enabled = enableVolumeButtons;
839 this.bVolDown.Enabled = enableVolumeButtons;
840 this.bMute.Enabled = enableVolumeButtons;
841 }
842
843 private void bVolUp_Click(object sender, EventArgs e)
844 {
845 SetControlsEnabled(false);
846 Lib.VolumeUp(true);
847 SetControlsEnabled(true);
848 }
849
850 private void bVolDown_Click(object sender, EventArgs e)
851 {
852 SetControlsEnabled(false);
853 Lib.VolumeDown(true);
854 SetControlsEnabled(true);
855 }
856
857 private void bMute_Click(object sender, EventArgs e)
858 {
859 SetControlsEnabled(false);
860 Lib.MuteAudio(true);
861 SetControlsEnabled(true);
862 }
863 }
864
865 internal class CecCallbackWrapper : CecCallbackMethods
866 {
867 public CecCallbackWrapper(CecConfigGUI gui)
868 {
869 Gui = gui;
870 }
871
872 public override int ReceiveCommand(CecCommand command)
873 {
874 return Gui.ReceiveCommand(command);
875 }
876
877 public override int ReceiveKeypress(CecKeypress key)
878 {
879 return Gui.ReceiveKeypress(key);
880 }
881
882 public override int ReceiveLogMessage(CecLogMessage message)
883 {
884 return Gui.ReceiveLogMessage(message);
885 }
886
887 private CecConfigGUI Gui;
888 }
889 }