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