cec: added a callback that is called when libCEC's configuration changed.
[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 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 "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 ConfigurationChanged(LibCECConfiguration config)
226 {
227 SetControlText(this.tbPhysicalAddress, String.Format("{0,4:X}", config.PhysicalAddress));
228 return 1;
229 }
230
231 public int ReceiveCommand(CecCommand command)
232 {
233 return 1;
234 }
235
236 public int ReceiveKeypress(CecKeypress key)
237 {
238 SelectKeypressRow(key);
239 return 1;
240 }
241
242 delegate void SelectKeypressRowCallback(CecKeypress key);
243 private void SelectKeypressRow(CecKeypress key)
244 {
245 if (dgButtons.InvokeRequired)
246 {
247 SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
248 try
249 {
250 this.Invoke(d, new object[] { key });
251 }
252 catch (Exception) { }
253 }
254 else
255 {
256 int rowIndex = -1;
257 foreach (DataGridViewRow row in dgButtons.Rows)
258 {
259 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
260 if (item != null && item.Key.Keycode == key.Keycode)
261 {
262 rowIndex = row.Index;
263 row.Selected = true;
264 item.Enabled = true;
265 }
266 else
267 {
268 row.Selected = false;
269 }
270 }
271 if (rowIndex > -1)
272 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
273 }
274 }
275
276 delegate void AddLogMessageCallback(CecLogMessage message);
277 private void AddLogMessage(CecLogMessage message)
278 {
279 if (tbLog.InvokeRequired)
280 {
281 AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage);
282 try
283 {
284 this.Invoke(d, new object[] { message });
285 }
286 catch (Exception) { }
287 }
288 else
289 {
290 string strLevel = "";
291 bool display = false;
292 switch (message.Level)
293 {
294 case CecLogLevel.Error:
295 strLevel = "ERROR: ";
296 display = cbLogError.Checked;
297 break;
298 case CecLogLevel.Warning:
299 strLevel = "WARNING: ";
300 display = cbLogWarning.Checked;
301 break;
302 case CecLogLevel.Notice:
303 strLevel = "NOTICE: ";
304 display = cbLogNotice.Checked;
305 break;
306 case CecLogLevel.Traffic:
307 strLevel = "TRAFFIC: ";
308 display = cbLogTraffic.Checked;
309 break;
310 case CecLogLevel.Debug:
311 strLevel = "DEBUG: ";
312 display = cbLogDebug.Checked;
313 break;
314 default:
315 break;
316 }
317
318 if (display)
319 {
320 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
321 tbLog.Text += strLog;
322 tbLog.Select(tbLog.Text.Length, 0);
323 tbLog.ScrollToCaret();
324 }
325 }
326 }
327
328 public int ReceiveLogMessage(CecLogMessage message)
329 {
330 AddLogMessage(message);
331 return 1;
332 }
333
334 delegate void SetControlEnabledCallback(Control control, bool val);
335 private void SetControlEnabled(Control control, bool val)
336 {
337 if (control.InvokeRequired)
338 {
339 SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled);
340 try
341 {
342 this.Invoke(d, new object[] { control, val });
343 }
344 catch (Exception) { }
345 }
346 else
347 {
348 control.Enabled = val;
349 }
350 }
351
352 private void SetControlsEnabled(bool val)
353 {
354 SetControlEnabled(cbPortNumber, val);
355 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
356 SetControlEnabled(tbPhysicalAddress, val);
357 SetControlEnabled(cbDeviceType, false); // TODO not implemented yet
358 SetControlEnabled(cbUseTVMenuLanguage, val);
359 SetControlEnabled(cbPowerOnStartup, val);
360 SetControlEnabled(cbPowerOffShutdown, val);
361 SetControlEnabled(cbPowerOffScreensaver, val);
362 SetControlEnabled(cbPowerOffOnStandby, val);
363 SetControlEnabled(cbWakeDevices, false); // TODO not implemented yet
364 SetControlEnabled(bClose, val);
365 SetControlEnabled(bSave, val);
366
367 SetControlEnabled(bSendImageViewOn, val);
368 SetControlEnabled(bStandby, val);
369 SetControlEnabled(bActivateSource, val);
370 SetControlEnabled(bScan, val);
371
372 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
373 SetControlEnabled(bVolUp, enableVolumeButtons);
374 SetControlEnabled(bVolDown, enableVolumeButtons);
375 SetControlEnabled(bMute, enableVolumeButtons);
376 }
377
378 delegate void SetControlTextCallback(Control control, string val);
379 private void SetControlText(Control control, string val)
380 {
381 if (control.InvokeRequired)
382 {
383 SetControlTextCallback d = new SetControlTextCallback(SetControlText);
384 try
385 {
386 this.Invoke(d, new object[] { control, val });
387 }
388 catch (Exception) { }
389 }
390 else
391 {
392 control.Text = val;
393 }
394 }
395
396 delegate void SetCheckboxCheckedCallback(CheckBox control, bool val);
397 private void SetCheckboxChecked(CheckBox control, bool val)
398 {
399 if (control.InvokeRequired)
400 {
401 SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked);
402 try
403 {
404 this.Invoke(d, new object[] { control, val });
405 }
406 catch (Exception) { }
407 }
408 else
409 {
410 control.Checked = val;
411 }
412 }
413
414 delegate void SetProgressValueCallback(ProgressBar control, int val);
415 private void SetProgressValue(ProgressBar control, int val)
416 {
417 if (control.InvokeRequired)
418 {
419 SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue);
420 try
421 {
422 this.Invoke(d, new object[] { control, val });
423 }
424 catch (Exception) { }
425 }
426 else
427 {
428 control.Value = val;
429 }
430 }
431
432 delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val);
433 private void SetComboBoxItems(ComboBox control, string selectedText, object[] val)
434 {
435 if (control.InvokeRequired)
436 {
437 SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems);
438 try
439 {
440 this.Invoke(d, new object[] { control, selectedText, val });
441 }
442 catch (Exception) { }
443 }
444 else
445 {
446 control.Items.Clear();
447 control.Items.AddRange(val);
448 control.Text = selectedText;
449 }
450 }
451
452 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
453 {
454 switch (updateEvent.Type)
455 {
456 case UpdateEventType.StatusText:
457 SetControlText(lStatus, updateEvent.StringValue);
458 break;
459 case UpdateEventType.PhysicalAddress:
460 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
461 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
462 break;
463 case UpdateEventType.ProgressBar:
464 SetProgressValue(pProgress, updateEvent.IntValue);
465 break;
466 case UpdateEventType.TVVendorId:
467 TVVendor = (CecVendorId)updateEvent.IntValue;
468 UpdateSelectedDevice();
469 break;
470 case UpdateEventType.BaseDevicePhysicalAddress:
471 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
472 break;
473 case UpdateEventType.BaseDevice:
474 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
475 break;
476 case UpdateEventType.HDMIPort:
477 Config.HDMIPort = (byte)updateEvent.IntValue;
478 break;
479 case UpdateEventType.MenuLanguage:
480 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
481 break;
482 case UpdateEventType.HasAVRDevice:
483 if (HasAVRDevice != updateEvent.BoolValue)
484 {
485 HasAVRDevice = updateEvent.BoolValue;
486 UpdateSelectedDevice();
487 }
488 break;
489 case UpdateEventType.AVRVendorId:
490 AVRVendor = (CecVendorId)updateEvent.IntValue;
491 UpdateSelectedDevice();
492 break;
493 case UpdateEventType.Configuration:
494 Config = updateEvent.ConfigValue;
495 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
496 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
497 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
498 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
499 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
500 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
501 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
502 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
503 UpdateSelectedDevice();
504 break;
505 case UpdateEventType.ProcessCompleted:
506 ActiveProcess = null;
507 SetControlsEnabled(true);
508 break;
509 }
510 }
511
512 private void UpdateSelectedDevice()
513 {
514 if (HasAVRDevice)
515 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
516 else
517 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
518 }
519
520 public string TVVendorString
521 {
522 get
523 {
524 return TVVendor != CecVendorId.Unknown ?
525 "Television (" + Lib.ToString(TVVendor) + ")" :
526 "Television";
527 }
528 }
529
530 public string AVRVendorString
531 {
532 get
533 {
534 return AVRVendor != CecVendorId.Unknown ?
535 "AVR (" + Lib.ToString(AVRVendor) + ")" :
536 "AVR";
537 }
538 }
539
540 protected bool HasAVRDevice;
541 protected CecVendorId TVVendor = CecVendorId.Unknown;
542 protected CecVendorId AVRVendor = CecVendorId.Unknown;
543 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
544
545 protected LibCECConfiguration Config;
546 protected LibCecSharp Lib;
547 private CecCallbackWrapper Callbacks;
548 private UpdateProcess ActiveProcess = null;
549
550 public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
551 {
552 if (ActiveProcess == null)
553 {
554 SetControlsEnabled(false);
555 ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber);
556 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
557 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
558 }
559 }
560
561 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
562 {
563 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
564 int iPortNumber = 0;
565 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
566 iPortNumber = 1;
567 SetConnectedDevice(SelectedConnectedDevice, iPortNumber);
568 }
569
570 private void bCancel_Click(object sender, EventArgs e)
571 {
572 this.Dispose();
573 }
574
575 private void bSave_Click(object sender, EventArgs e)
576 {
577 SetControlsEnabled(false);
578
579 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
580 Config.PowerOnStartup = cbPowerOnStartup.Checked;
581 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
582 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
583 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
584
585 if (!Lib.CanPersistConfiguration())
586 {
587 if (ActiveProcess == null)
588 {
589 SetControlsEnabled(false);
590 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
591 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
592
593 SaveFileDialog dialog = new SaveFileDialog()
594 {
595 Title = "Where do you want to store the settings?",
596 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
597 FileName = "usb_2548_1001.xml",
598 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
599 FilterIndex = 1
600 };
601
602 if (dialog.ShowDialog() == DialogResult.OK)
603 {
604 FileStream fs = (FileStream)dialog.OpenFile();
605 if (fs == null)
606 {
607 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
608 }
609 else
610 {
611 StreamWriter writer = new StreamWriter(fs);
612 StringBuilder output = new StringBuilder();
613 output.AppendLine("<settings>");
614 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
615 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
616 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
617 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
618 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
619 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
620 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
621 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
622 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
623 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
624 output.AppendLine("<setting id=\"port\" value=\"\" />");
625 output.AppendLine("</settings>");
626 writer.Write(output.ToString());
627 writer.Close();
628 fs.Close();
629 fs.Dispose();
630 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
631 }
632 }
633 SetControlsEnabled(true);
634 }
635 }
636 else
637 {
638 if (!Lib.PersistConfiguration(Config))
639 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
640 else
641 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
642 }
643 SetControlsEnabled(true);
644 }
645
646 public void SetPhysicalAddress(ushort physicalAddress)
647 {
648 if (ActiveProcess == null)
649 {
650 SetControlsEnabled(false);
651 SetControlText(cbPortNumber, string.Empty);
652 SetControlText(cbConnectedDevice, string.Empty);
653 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
654 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
655 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
656 }
657 }
658
659 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
660 {
661 if (tbPhysicalAddress.Text.Length != 4)
662 return;
663 ushort physicalAddress = 0;
664 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
665 return;
666
667 SetPhysicalAddress(physicalAddress);
668 }
669
670 private void bClearLog_Click(object sender, EventArgs e)
671 {
672 tbLog.Text = string.Empty;
673 }
674
675 private void bSaveLog_Click(object sender, EventArgs e)
676 {
677 SaveFileDialog dialog = new SaveFileDialog()
678 {
679 Title = "Where do you want to store the log file?",
680 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
681 FileName = "cec-log.txt",
682 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
683 FilterIndex = 1
684 };
685
686 if (dialog.ShowDialog() == DialogResult.OK)
687 {
688 FileStream fs = (FileStream)dialog.OpenFile();
689 if (fs == null)
690 {
691 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
692 }
693 else
694 {
695 StreamWriter writer = new StreamWriter(fs);
696 writer.Write(tbLog.Text);
697 writer.Close();
698 fs.Close();
699 fs.Dispose();
700 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
701 }
702 }
703 }
704
705 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
706 {
707 DataGridView grid = sender as DataGridView;
708 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
709 if (data == null || !data.Enabled)
710 e.CellStyle.ForeColor = Color.Gray;
711 }
712
713 delegate CecLogicalAddress GetTargetDeviceCallback();
714 private CecLogicalAddress GetTargetDevice()
715 {
716 if (this.cbCommandDestination.InvokeRequired)
717 {
718 GetTargetDeviceCallback d = new GetTargetDeviceCallback(GetTargetDevice);
719 CecLogicalAddress retval = CecLogicalAddress.Unknown;
720 try
721 {
722 retval = (CecLogicalAddress)this.Invoke(d, new object[] { });
723 }
724 catch (Exception) { }
725 return retval;
726 }
727
728 switch (this.cbCommandDestination.Text.Substring(0, 1).ToLower())
729 {
730 case "0":
731 return CecLogicalAddress.Tv;
732 case "1":
733 return CecLogicalAddress.RecordingDevice1;
734 case "2":
735 return CecLogicalAddress.RecordingDevice2;
736 case "3":
737 return CecLogicalAddress.Tuner1;
738 case "4":
739 return CecLogicalAddress.PlaybackDevice1;
740 case "5":
741 return CecLogicalAddress.AudioSystem;
742 case "6":
743 return CecLogicalAddress.Tuner2;
744 case "7":
745 return CecLogicalAddress.Tuner3;
746 case "8":
747 return CecLogicalAddress.PlaybackDevice2;
748 case "9":
749 return CecLogicalAddress.RecordingDevice3;
750 case "a":
751 return CecLogicalAddress.Tuner4;
752 case "b":
753 return CecLogicalAddress.PlaybackDevice3;
754 case "c":
755 return CecLogicalAddress.Reserved1;
756 case "d":
757 return CecLogicalAddress.Reserved2;
758 case "e":
759 return CecLogicalAddress.FreeUse;
760 case "f":
761 return CecLogicalAddress.Broadcast;
762 default:
763 return CecLogicalAddress.Unknown;
764 }
765 }
766
767 public void SendImageViewOn(CecLogicalAddress address)
768 {
769 if (ActiveProcess == null)
770 {
771 SetControlsEnabled(false);
772 ActiveProcess = new SendImageViewOn(ref Lib, address);
773 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
774 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
775 }
776 }
777
778 private void bSendImageViewOn_Click(object sender, EventArgs e)
779 {
780 SendImageViewOn(GetTargetDevice());
781 }
782
783 public void SendStandby(CecLogicalAddress address)
784 {
785 if (ActiveProcess == null)
786 {
787 SetControlsEnabled(false);
788 ActiveProcess = new SendStandby(ref Lib, address);
789 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
790 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
791 }
792 }
793
794 private void bStandby_Click(object sender, EventArgs e)
795 {
796 SendStandby(GetTargetDevice());
797 }
798
799 public void ShowDeviceInfo(CecLogicalAddress address)
800 {
801 if (ActiveProcess == null)
802 {
803 SetControlsEnabled(false);
804 ActiveProcess = new ShowDeviceInfo(this, ref Lib, address);
805 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
806 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
807 }
808 }
809
810 private void bScan_Click(object sender, EventArgs e)
811 {
812 ShowDeviceInfo(GetTargetDevice());
813 }
814
815 public void ActivateSource(CecLogicalAddress address)
816 {
817 if (ActiveProcess == null)
818 {
819 SetControlsEnabled(false);
820 ActiveProcess = new SendActivateSource(ref Lib, address);
821 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
822 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
823 }
824 }
825
826 private void bActivateSource_Click(object sender, EventArgs e)
827 {
828 ActivateSource(GetTargetDevice());
829 }
830
831 private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e)
832 {
833 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
834 this.bVolUp.Enabled = enableVolumeButtons;
835 this.bVolDown.Enabled = enableVolumeButtons;
836 this.bMute.Enabled = enableVolumeButtons;
837 }
838
839 private void bVolUp_Click(object sender, EventArgs e)
840 {
841 SetControlsEnabled(false);
842 Lib.VolumeUp(true);
843 SetControlsEnabled(true);
844 }
845
846 private void bVolDown_Click(object sender, EventArgs e)
847 {
848 SetControlsEnabled(false);
849 Lib.VolumeDown(true);
850 SetControlsEnabled(true);
851 }
852
853 private void bMute_Click(object sender, EventArgs e)
854 {
855 SetControlsEnabled(false);
856 Lib.MuteAudio(true);
857 SetControlsEnabled(true);
858 }
859 }
860
861 internal class CecCallbackWrapper : CecCallbackMethods
862 {
863 public CecCallbackWrapper(CecConfigGUI gui)
864 {
865 Gui = gui;
866 }
867
868 public override int ReceiveCommand(CecCommand command)
869 {
870 return Gui.ReceiveCommand(command);
871 }
872
873 public override int ReceiveKeypress(CecKeypress key)
874 {
875 return Gui.ReceiveKeypress(key);
876 }
877
878 public override int ReceiveLogMessage(CecLogMessage message)
879 {
880 return Gui.ReceiveLogMessage(message);
881 }
882
883 public override int ConfigurationChanged(LibCECConfiguration config)
884 {
885 return Gui.ConfigurationChanged(config);
886 }
887
888 private CecConfigGUI Gui;
889 }
890 }