cec-config-gui: added log level configuration and a 'save' and 'clear' button for...
[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;
13
14namespace CecConfigGui
15{
16 public partial class CecConfigGUI : Form
17 {
18 public CecConfigGUI()
19 {
20 Config = new LibCECConfiguration();
21 Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
22 Config.DeviceName = "CEC Config";
23 Config.GetSettingsFromROM = true;
24 Config.ClientVersion = CecClientVersion.Version1_5_0;
25 Callbacks = new CecCallbackWrapper(this);
26 Config.SetCallbacks(Callbacks);
27
28 InitializeComponent();
29 Lib = new LibCecSharp(Config);
30
31 ActiveProcess = new ConnectToDevice(ref Lib);
32 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
33 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
34 }
35
36 public int ReceiveCommand(CecCommand command)
37 {
38 return 1;
39 }
40
41 public int ReceiveKeypress(CecKeypress key)
42 {
43 return 1;
44 }
45
46 delegate void AddLogMessageCallback(CecLogMessage message);
47 private void AddLogMessage(CecLogMessage message)
48 {
49 if (tbLog.InvokeRequired)
50 {
51 AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage);
52 try
53 {
54 this.Invoke(d, new object[] { message });
55 }
56 catch (Exception) { }
57 }
58 else
59 {
6b92c1c4
LOK
60 string strLevel = "";
61 bool display = false;
62 switch (message.Level)
63 {
64 case CecLogLevel.Error:
65 strLevel = "ERROR: ";
66 display = cbLogError.Checked;
67 break;
68 case CecLogLevel.Warning:
69 strLevel = "WARNING: ";
70 display = cbLogWarning.Checked;
71 break;
72 case CecLogLevel.Notice:
73 strLevel = "NOTICE: ";
74 display = cbLogNotice.Checked;
75 break;
76 case CecLogLevel.Traffic:
77 strLevel = "TRAFFIC: ";
78 display = cbLogTraffic.Checked;
79 break;
80 case CecLogLevel.Debug:
81 strLevel = "DEBUG: ";
82 display = cbLogDebug.Checked;
83 break;
84 default:
85 break;
86 }
87
88 if (display)
006b76b9 89 {
006b76b9
LOK
90 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
91 tbLog.Text += strLog;
6b92c1c4
LOK
92 tbLog.Select(tbLog.Text.Length, 0);
93 tbLog.ScrollToCaret();
006b76b9
LOK
94 }
95 }
96 }
97
98 public int ReceiveLogMessage(CecLogMessage message)
99 {
100 AddLogMessage(message);
101 return 1;
102 }
103
104 delegate void SetControlEnabledCallback(Control control, bool val);
105 private void SetControlEnabled(Control control, bool val)
106 {
107 if (control.InvokeRequired)
108 {
109 SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled);
110 try
111 {
112 this.Invoke(d, new object[] { control, val });
113 }
114 catch (Exception) { }
115 }
116 else
117 {
118 control.Enabled = val;
119 }
120 }
121
122 private void SetControlsEnabled(bool val)
123 {
124 SetControlEnabled(cbPortNumber, val);
f2b8f0c0 125 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
006b76b9
LOK
126 SetControlEnabled(tbPhysicalAddress, val);
127 SetControlEnabled(cbDeviceType, val);
128 SetControlEnabled(cbUseTVMenuLanguage, val);
129 SetControlEnabled(cbPowerOnStartup, val);
130 SetControlEnabled(cbPowerOffShutdown, val);
131 SetControlEnabled(cbPowerOffScreensaver, val);
132 SetControlEnabled(cbPowerOffOnStandby, val);
133 SetControlEnabled(bClose, val);
134 SetControlEnabled(bSave, val);
135 }
136
137 delegate void SetControlTextCallback(Control control, string val);
138 private void SetControlText(Control control, string val)
139 {
140 if (control.InvokeRequired)
141 {
142 SetControlTextCallback d = new SetControlTextCallback(SetControlText);
143 try
144 {
145 this.Invoke(d, new object[] { control, val });
146 }
147 catch (Exception) { }
148 }
149 else
150 {
151 control.Text = val;
152 }
153 }
154
155 delegate void SetCheckboxCheckedCallback(CheckBox control, bool val);
156 private void SetCheckboxChecked(CheckBox control, bool val)
157 {
158 if (control.InvokeRequired)
159 {
160 SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked);
161 try
162 {
163 this.Invoke(d, new object[] { control, val });
164 }
165 catch (Exception) { }
166 }
167 else
168 {
169 control.Checked = val;
170 }
171 }
172
173 delegate void SetProgressValueCallback(ProgressBar control, int val);
174 private void SetProgressValue(ProgressBar control, int val)
175 {
176 if (control.InvokeRequired)
177 {
178 SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue);
179 try
180 {
181 this.Invoke(d, new object[] { control, val });
182 }
183 catch (Exception) { }
184 }
185 else
186 {
187 control.Value = val;
188 }
189 }
190
191 delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val);
192 private void SetComboBoxItems(ComboBox control, string selectedText, object[] val)
193 {
194 if (control.InvokeRequired)
195 {
196 SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems);
197 try
198 {
199 this.Invoke(d, new object[] { control, selectedText, val });
200 }
201 catch (Exception) { }
202 }
203 else
204 {
205 control.Items.Clear();
206 control.Items.AddRange(val);
207 control.Text = selectedText;
208 }
209 }
210
211 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
212 {
213 switch (updateEvent.Type)
214 {
215 case UpdateEventType.StatusText:
216 SetControlText(lStatus, updateEvent.StringValue);
217 break;
218 case UpdateEventType.PhysicalAddress:
219 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
220 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
221 break;
222 case UpdateEventType.ProgressBar:
223 SetProgressValue(pProgress, updateEvent.IntValue);
224 break;
225 case UpdateEventType.TVVendorId:
226 TVVendor = (CecVendorId)updateEvent.IntValue;
227 UpdateSelectedDevice();
228 break;
229 case UpdateEventType.BaseDevicePhysicalAddress:
230 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
231 break;
232 case UpdateEventType.BaseDevice:
233 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
234 break;
235 case UpdateEventType.HDMIPort:
236 Config.HDMIPort = (byte)updateEvent.IntValue;
237 break;
f976869e
LOK
238 case UpdateEventType.MenuLanguage:
239 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
240 break;
006b76b9
LOK
241 case UpdateEventType.HasAVRDevice:
242 if (HasAVRDevice != updateEvent.BoolValue)
243 {
244 HasAVRDevice = updateEvent.BoolValue;
245 UpdateSelectedDevice();
246 }
247 break;
248 case UpdateEventType.AVRVendorId:
249 AVRVendor = (CecVendorId)updateEvent.IntValue;
250 UpdateSelectedDevice();
251 break;
252 case UpdateEventType.Configuration:
253 Config = updateEvent.ConfigValue;
254 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
255 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
256 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
257 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
258 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
259 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
260 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
261 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
262 UpdateSelectedDevice();
263 break;
264 case UpdateEventType.ProcessCompleted:
265 ActiveProcess = null;
266 SetControlsEnabled(true);
267 break;
268 }
269 }
270
271 private void UpdateSelectedDevice()
272 {
273 if (HasAVRDevice)
274 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
275 else
276 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
277 }
278
279 public string TVVendorString
280 {
281 get
282 {
283 return TVVendor != CecVendorId.Unknown ?
284 "Television (" + Lib.ToString(TVVendor) + ")" :
285 "Television";
286 }
287 }
288
289 public string AVRVendorString
290 {
291 get
292 {
293 return AVRVendor != CecVendorId.Unknown ?
294 "AVR (" + Lib.ToString(AVRVendor) + ")" :
295 "AVR";
296 }
297 }
298
299 protected bool HasAVRDevice;
300 protected CecVendorId TVVendor = CecVendorId.Unknown;
301 protected CecVendorId AVRVendor = CecVendorId.Unknown;
302 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
006b76b9
LOK
303
304 protected LibCECConfiguration Config;
305 protected LibCecSharp Lib;
306 private CecCallbackWrapper Callbacks;
307 private UpdateProcess ActiveProcess = null;
308
309 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
310 {
311 if (ActiveProcess == null)
312 {
313 SetControlsEnabled(false);
314 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
315 int iPortNumber = 0;
316 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
317 iPortNumber = 1;
318 ActiveProcess = new UpdateConnectedDevice(ref Lib, cbConnectedDevice.Text.Equals(AVRVendorString) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv, iPortNumber);
319 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
320 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
321 }
322 }
323
324 private void bCancel_Click(object sender, EventArgs e)
325 {
326 this.Dispose();
327 }
328
329 private void bSave_Click(object sender, EventArgs e)
330 {
331 SetControlsEnabled(false);
332
333 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
334 Config.PowerOnStartup = cbPowerOnStartup.Checked;
335 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
336 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
337 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
338
339 if (!Lib.CanPersistConfiguration())
340 {
341 if (ActiveProcess == null)
342 {
343 SetControlsEnabled(false);
344 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
345 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
346
347 SaveFileDialog dialog = new SaveFileDialog()
348 {
349 Title = "Where do you want to store the settings?",
350 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
351 FileName = "usb_2548_1001.xml",
352 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
353 FilterIndex = 1
354 };
355
356 if (dialog.ShowDialog() == DialogResult.OK)
357 {
358 FileStream fs = (FileStream)dialog.OpenFile();
359 if (fs == null)
360 {
361 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
362 }
363 else
364 {
365 StreamWriter writer = new StreamWriter(fs);
366 StringBuilder output = new StringBuilder();
367 output.AppendLine("<settings>");
368 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
369 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
370 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
371 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
372 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
373 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
374 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
375 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
376 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
377 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
378 output.AppendLine("<setting id=\"port\" value=\"\" />");
379 output.AppendLine("</settings>");
380 writer.Write(output.ToString());
381 writer.Close();
382 fs.Close();
383 fs.Dispose();
384 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
385 }
386 }
387 SetControlsEnabled(true);
388 }
389 }
390 else
391 {
392 if (!Lib.PersistConfiguration(Config))
393 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
394 else
395 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
396 }
397 SetControlsEnabled(true);
398 }
399
400 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
401 {
402 if (ActiveProcess == null)
403 {
404 if (tbPhysicalAddress.Text.Length != 4)
405 return;
406 ushort physicalAddress = 0;
407 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
408 return;
409 SetControlsEnabled(false);
410 SetControlText(cbPortNumber, string.Empty);
411 SetControlText(cbConnectedDevice, string.Empty);
412 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
413 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
414 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
415 }
416 }
6b92c1c4
LOK
417
418 private void bClearLog_Click(object sender, EventArgs e)
419 {
420 tbLog.Text = string.Empty;
421 }
422
423 private void bSaveLog_Click(object sender, EventArgs e)
424 {
425 SaveFileDialog dialog = new SaveFileDialog()
426 {
427 Title = "Where do you want to store the log file?",
428 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
429 FileName = "cec-log.txt",
430 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
431 FilterIndex = 1
432 };
433
434 if (dialog.ShowDialog() == DialogResult.OK)
435 {
436 FileStream fs = (FileStream)dialog.OpenFile();
437 if (fs == null)
438 {
439 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
440 }
441 else
442 {
443 StreamWriter writer = new StreamWriter(fs);
444 writer.Write(tbLog.Text);
445 writer.Close();
446 fs.Close();
447 fs.Dispose();
448 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
449 }
450 }
451 }
006b76b9
LOK
452 }
453
454 internal class CecCallbackWrapper : CecCallbackMethods
455 {
456 public CecCallbackWrapper(CecConfigGUI gui)
457 {
458 Gui = gui;
459 }
460
461 public override int ReceiveCommand(CecCommand command)
462 {
463 return Gui.ReceiveCommand(command);
464 }
465
466 public override int ReceiveKeypress(CecKeypress key)
467 {
468 return Gui.ReceiveKeypress(key);
469 }
470
471 public override int ReceiveLogMessage(CecLogMessage message)
472 {
473 return Gui.ReceiveLogMessage(message);
474 }
475
476 private CecConfigGUI Gui;
477 }
478}