cec: added RescanDevices()/cec_rescan_devices() to the interface, to let libCEC force...
[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{
5de1dde4
LOK
17 internal enum ConfigTab
18 {
19 Configuration,
20 KeyConfiguration,
21 Tester,
22 Log
23 }
24
75af24f1 25 public partial class CecConfigGUI : AsyncForm
006b76b9
LOK
26 {
27 public CecConfigGUI()
28 {
29 Config = new LibCECConfiguration();
30 Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
31 Config.DeviceName = "CEC Config";
32 Config.GetSettingsFromROM = true;
33 Config.ClientVersion = CecClientVersion.Version1_5_0;
34 Callbacks = new CecCallbackWrapper(this);
35 Config.SetCallbacks(Callbacks);
4555ed72 36 LoadXMLConfiguration(ref Config);
006b76b9
LOK
37 Lib = new LibCecSharp(Config);
38
4555ed72 39 InitializeComponent();
8674df6a
LOK
40 LoadButtonConfiguration();
41
4555ed72 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;
75af24f1
LOK
94 case "cec_power_on_startup":
95 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
96 {
97 config.ActivateSource = true;
98 config.WakeDevices.Set(CecLogicalAddress.Tv);
99 }
100 break;
101 case "cec_power_off_shutdown":
102 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
103 config.PowerOffDevices.Set(CecLogicalAddress.Broadcast);
104 break;
105 case "cec_standby_screensaver":
106 config.PowerOffScreensaver = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
107 break;
108 case "standby_pc_on_tv_standby":
109 config.PowerOffOnStandby = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
110 break;
111 case "use_tv_menu_language":
112 config.UseTVMenuLanguage = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
113 break;
114 // 1.5.0+ settings
4555ed72
LOK
115 case "physical_address":
116 {
117 ushort physicalAddress = 0;
118 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
119 config.PhysicalAddress = physicalAddress;
120 }
121 break;
122 case "device_type":
123 {
124 ushort iType;
125 if (ushort.TryParse(value, out iType))
126 config.DeviceTypes.Types[0] = (CecDeviceType)iType;
127 }
128 break;
75af24f1
LOK
129 case "tv_vendor":
130 {
131 UInt64 iVendor;
132 if (UInt64.TryParse(value, out iVendor))
133 config.TvVendor = (CecVendorId)iVendor;
134 }
4555ed72 135 break;
75af24f1
LOK
136 case "wake_devices":
137 {
138 config.WakeDevices.Clear();
139 string[] split = value.Split(new char[] { ' ' });
140 foreach (string dev in split)
141 {
142 byte iLogicalAddress;
143 if (byte.TryParse(dev, out iLogicalAddress))
144 config.WakeDevices.Set((CecLogicalAddress)iLogicalAddress);
145 }
146 }
4555ed72 147 break;
75af24f1
LOK
148 case "standby_devices":
149 {
150 config.PowerOffDevices.Clear();
151 string[] split = value.Split(new char[] { ' ' });
152 foreach (string dev in split)
153 {
154 byte iLogicalAddress;
155 if (byte.TryParse(dev, out iLogicalAddress))
156 config.PowerOffDevices.Set((CecLogicalAddress)iLogicalAddress);
157 }
158 }
4555ed72
LOK
159 break;
160 case "enabled":
161 break;
162 case "port":
163 //TODO
164 break;
165 default:
166 break;
167 }
168 }
169 break;
170 default:
171 break;
172 }
173 }
174 }
175 return gotConfig;
176 }
177
8674df6a
LOK
178 private void LoadButtonConfiguration()
179 {
180 //TODO load the real configuration
181 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select", (new CecSharp.CecKeypress() { Keycode = 0x00 }), string.Empty));
182 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Up", (new CecSharp.CecKeypress() { Keycode = 0x01 }), string.Empty));
183 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Down", (new CecSharp.CecKeypress() { Keycode = 0x02 }), string.Empty));
184 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left", (new CecSharp.CecKeypress() { Keycode = 0x03 }), string.Empty));
185 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right", (new CecSharp.CecKeypress() { Keycode = 0x04 }), string.Empty));
186 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Up", (new CecSharp.CecKeypress() { Keycode = 0x05 }), string.Empty));
187 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Right+Down", (new CecSharp.CecKeypress() { Keycode = 0x06 }), string.Empty));
188 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Up", (new CecSharp.CecKeypress() { Keycode = 0x07 }), string.Empty));
189 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Left+Down", (new CecSharp.CecKeypress() { Keycode = 0x08 }), string.Empty));
190 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Root menu", (new CecSharp.CecKeypress() { Keycode = 0x09 }), string.Empty));
191 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Setup menu", (new CecSharp.CecKeypress() { Keycode = 0x0A }), string.Empty));
192 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Contents menu", (new CecSharp.CecKeypress() { Keycode = 0x0B }), string.Empty));
193 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Favourite menu", (new CecSharp.CecKeypress() { Keycode = 0x0C }), string.Empty));
194 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Exit", (new CecSharp.CecKeypress() { Keycode = 0x0D }), string.Empty));
195 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("0", (new CecSharp.CecKeypress() { Keycode = 0x20 }), string.Empty));
196 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("1", (new CecSharp.CecKeypress() { Keycode = 0x21 }), string.Empty));
197 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("2", (new CecSharp.CecKeypress() { Keycode = 0x22 }), string.Empty));
198 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("3", (new CecSharp.CecKeypress() { Keycode = 0x23 }), string.Empty));
199 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("4", (new CecSharp.CecKeypress() { Keycode = 0x24 }), string.Empty));
200 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("5", (new CecSharp.CecKeypress() { Keycode = 0x25 }), string.Empty));
201 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("6", (new CecSharp.CecKeypress() { Keycode = 0x26 }), string.Empty));
202 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("7", (new CecSharp.CecKeypress() { Keycode = 0x27 }), string.Empty));
203 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("8", (new CecSharp.CecKeypress() { Keycode = 0x28 }), string.Empty));
204 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("9", (new CecSharp.CecKeypress() { Keycode = 0x29 }), string.Empty));
205 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem(".", (new CecSharp.CecKeypress() { Keycode = 0x2A }), string.Empty));
206 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Enter", (new CecSharp.CecKeypress() { Keycode = 0x2B }), string.Empty));
207 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Clear", (new CecSharp.CecKeypress() { Keycode = 0x2C }), string.Empty));
208 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Next favourite", (new CecSharp.CecKeypress() { Keycode = 0x2F }), string.Empty));
209 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel up", (new CecSharp.CecKeypress() { Keycode = 0x30 }), string.Empty));
210 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Channel down", (new CecSharp.CecKeypress() { Keycode = 0x31 }), string.Empty));
211 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Previous channel", (new CecSharp.CecKeypress() { Keycode = 0x32 }), string.Empty));
212 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sound select", (new CecSharp.CecKeypress() { Keycode = 0x33 }), string.Empty));
213 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Input select", (new CecSharp.CecKeypress() { Keycode = 0x34 }), string.Empty));
214 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Display information", (new CecSharp.CecKeypress() { Keycode = 0x35 }), string.Empty));
215 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Help", (new CecSharp.CecKeypress() { Keycode = 0x36 }), string.Empty));
216 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page up", (new CecSharp.CecKeypress() { Keycode = 0x37 }), string.Empty));
217 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Page down", (new CecSharp.CecKeypress() { Keycode = 0x38 }), string.Empty));
218 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power", (new CecSharp.CecKeypress() { Keycode = 0x40 }), string.Empty));
219 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume up", (new CecSharp.CecKeypress() { Keycode = 0x41 }), string.Empty));
220 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Volume down", (new CecSharp.CecKeypress() { Keycode = 0x42 }), string.Empty));
221 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute", (new CecSharp.CecKeypress() { Keycode = 0x43 }), string.Empty));
222 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play", (new CecSharp.CecKeypress() { Keycode = 0x44 }), string.Empty));
223 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop", (new CecSharp.CecKeypress() { Keycode = 0x45 }), string.Empty));
224 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause", (new CecSharp.CecKeypress() { Keycode = 0x46 }), string.Empty));
225 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record", (new CecSharp.CecKeypress() { Keycode = 0x47 }), string.Empty));
226 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Rewind", (new CecSharp.CecKeypress() { Keycode = 0x48 }), string.Empty));
227 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Fast forward", (new CecSharp.CecKeypress() { Keycode = 0x49 }), string.Empty));
228 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Eject", (new CecSharp.CecKeypress() { Keycode = 0x4A }), string.Empty));
229 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Forward", (new CecSharp.CecKeypress() { Keycode = 0x4B }), string.Empty));
230 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Backward", (new CecSharp.CecKeypress() { Keycode = 0x4C }), string.Empty));
231 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop record", (new CecSharp.CecKeypress() { Keycode = 0x4D }), string.Empty));
232 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record", (new CecSharp.CecKeypress() { Keycode = 0x4E }), string.Empty));
233 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Angle", (new CecSharp.CecKeypress() { Keycode = 0x50 }), string.Empty));
234 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Sub picture", (new CecSharp.CecKeypress() { Keycode = 0x51 }), string.Empty));
235 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Video on demand", (new CecSharp.CecKeypress() { Keycode = 0x52 }), string.Empty));
236 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Electronic program guide", (new CecSharp.CecKeypress() { Keycode = 0x53 }), string.Empty));
237 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Timer programming", (new CecSharp.CecKeypress() { Keycode = 0x54 }), string.Empty));
238 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Initial configuration", (new CecSharp.CecKeypress() { Keycode = 0x55 }), string.Empty));
239 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Play (function)", (new CecSharp.CecKeypress() { Keycode = 0x60 }), string.Empty));
240 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause play (function)", (new CecSharp.CecKeypress() { Keycode = 0x61 }), string.Empty));
241 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Record (function)", (new CecSharp.CecKeypress() { Keycode = 0x62 }), string.Empty));
242 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Pause record (function)", (new CecSharp.CecKeypress() { Keycode = 0x63 }), string.Empty));
243 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Stop (function)", (new CecSharp.CecKeypress() { Keycode = 0x64 }), string.Empty));
244 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Mute (function)", (new CecSharp.CecKeypress() { Keycode = 0x65 }), string.Empty));
245 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Restore volume", (new CecSharp.CecKeypress() { Keycode = 0x66 }), string.Empty));
246 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Tune", (new CecSharp.CecKeypress() { Keycode = 0x67 }), string.Empty));
247 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select media", (new CecSharp.CecKeypress() { Keycode = 0x68 }), string.Empty));
248 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select AV input", (new CecSharp.CecKeypress() { Keycode = 0x69 }), string.Empty));
249 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Select audio input", (new CecSharp.CecKeypress() { Keycode = 0x6A }), string.Empty));
250 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power toggle", (new CecSharp.CecKeypress() { Keycode = 0x6B }), string.Empty));
251 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power off", (new CecSharp.CecKeypress() { Keycode = 0x6C }), string.Empty));
252 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Power on", (new CecSharp.CecKeypress() { Keycode = 0x6D }), string.Empty));
253 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F1 (blue)", (new CecSharp.CecKeypress() { Keycode = 0x71 }), string.Empty));
254 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F2 (red)", (new CecSharp.CecKeypress() { Keycode = 0x72 }), string.Empty));
255 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F3 (green)", (new CecSharp.CecKeypress() { Keycode = 0x73 }), string.Empty));
256 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F4 (yellow)", (new CecSharp.CecKeypress() { Keycode = 0x74 }), string.Empty));
257 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("F5", (new CecSharp.CecKeypress() { Keycode = 0x75 }), string.Empty));
258 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("Data", (new CecSharp.CecKeypress() { Keycode = 0x76 }), string.Empty));
259 this.cecButtonConfigBindingSource.Add(new CecButtonConfigItem("(Samsung) Return", (new CecSharp.CecKeypress() { Keycode = 0x91 }), string.Empty));
260 }
261
75af24f1 262 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
8674df6a 263 {
75af24f1 264 switch (updateEvent.Type)
8674df6a 265 {
75af24f1
LOK
266 case UpdateEventType.StatusText:
267 SetControlText(lStatus, updateEvent.StringValue);
268 break;
269 case UpdateEventType.PhysicalAddress:
270 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
271 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
272 break;
273 case UpdateEventType.ProgressBar:
68b94c34 274 SetControlVisible(pProgress, true);
75af24f1
LOK
275 SetProgressValue(pProgress, updateEvent.IntValue);
276 break;
277 case UpdateEventType.TVVendorId:
278 TVVendor = (CecVendorId)updateEvent.IntValue;
279 UpdateSelectedDevice();
280 break;
281 case UpdateEventType.BaseDevicePhysicalAddress:
282 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
283 break;
284 case UpdateEventType.BaseDevice:
285 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
286 break;
287 case UpdateEventType.HDMIPort:
288 Config.HDMIPort = (byte)updateEvent.IntValue;
289 break;
290 case UpdateEventType.MenuLanguage:
291 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
292 break;
293 case UpdateEventType.HasAVRDevice:
294 if (HasAVRDevice != updateEvent.BoolValue)
8674df6a 295 {
75af24f1
LOK
296 HasAVRDevice = updateEvent.BoolValue;
297 UpdateSelectedDevice();
8674df6a 298 }
75af24f1
LOK
299 break;
300 case UpdateEventType.AVRVendorId:
301 AVRVendor = (CecVendorId)updateEvent.IntValue;
302 UpdateSelectedDevice();
303 break;
304 case UpdateEventType.Configuration:
305 SuppressUpdates = true;
306 ConfigurationChanged(updateEvent.ConfigValue);
307 SuppressUpdates = false;
308 break;
6d866874
LOK
309 case UpdateEventType.PollDevices:
310 CheckActiveDevices();
311 break;
75af24f1
LOK
312 case UpdateEventType.ProcessCompleted:
313 ActiveProcess = null;
314 SetControlsEnabled(true);
6d866874
LOK
315 if (UpdatingInfoPanel != null)
316 {
317 UpdatingInfoPanel.SetControlEnabled(UpdatingInfoPanel.bUpdate, true);
318 UpdatingInfoPanel = null;
319 }
68b94c34 320 SetControlVisible(pProgress, false);
75af24f1 321 break;
8674df6a
LOK
322 }
323 }
324
5de1dde4
LOK
325 private void SetControlsEnabled(bool val)
326 {
68b94c34
LOK
327 SetControlEnabled(cbPortNumber, val && !Config.AutodetectAddress);
328 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 && !Config.AutodetectAddress ? val : false);
329 SetControlEnabled(tbPhysicalAddress, val && !Config.AutodetectAddress);
6d866874 330 SetControlEnabled(cbDeviceType, val);
5de1dde4
LOK
331 SetControlEnabled(cbUseTVMenuLanguage, val);
332 SetControlEnabled(cbActivateSource, val);
333 SetControlEnabled(cbPowerOffScreensaver, val);
334 SetControlEnabled(cbPowerOffOnStandby, val);
2b3c67ec
LOK
335 SetControlEnabled(cbWakeDevices, val);
336 SetControlEnabled(cbPowerOffDevices, val);
5de1dde4
LOK
337 SetControlEnabled(cbVendorOverride, val);
338 SetControlEnabled(cbVendorId, val && cbVendorOverride.Checked);
339 SetControlEnabled(bClose, val);
340 SetControlEnabled(bSaveConfig, val);
341 SetControlEnabled(bReloadConfig, val);
6d866874 342 SetControlEnabled(bRescanDevices, val);
5de1dde4
LOK
343
344 SetControlEnabled(bSendImageViewOn, val);
345 SetControlEnabled(bStandby, val);
346 SetControlEnabled(bActivateSource, val);
347 SetControlEnabled(bScan, val);
348
349 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
350 SetControlEnabled(bVolUp, enableVolumeButtons);
351 SetControlEnabled(bVolDown, enableVolumeButtons);
352 SetControlEnabled(bMute, enableVolumeButtons);
353 }
354
355 private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
356 {
357 switch (tabControl1.SelectedIndex)
358 {
359 case 0:
360 SelectedTab = ConfigTab.Configuration;
361 break;
362 case 1:
363 SelectedTab = ConfigTab.KeyConfiguration;
364 break;
365 case 2:
366 SelectedTab = ConfigTab.Tester;
367 break;
368 case 3:
369 SelectedTab = ConfigTab.Log;
370 UpdateLog();
371 break;
372 default:
373 SelectedTab = ConfigTab.Configuration;
374 break;
375 }
376 }
377
68b94c34
LOK
378 protected override void Dispose(bool disposing)
379 {
380 if (disposing)
381 {
382 Lib.DisableCallbacks();
383 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
384 Lib.Close();
385 }
386 if (disposing && (components != null))
387 {
388 components.Dispose();
389 }
390 base.Dispose(disposing);
391 }
392
5de1dde4
LOK
393 #region Actions
394 public void ReloadXMLConfiguration()
395 {
396 LoadXMLConfiguration(ref Config);
397 Lib.SetConfiguration(Config);
398 ConfigurationChanged(Config);
399 }
400
6d866874
LOK
401 public void UpdateInfoPanel(DeviceInformation panel)
402 {
403 if (!SuppressUpdates && ActiveProcess == null)
404 {
405 SetControlsEnabled(false);
406 UpdatingInfoPanel = panel;
407 panel.SetControlEnabled(panel.bUpdate, false);
408 ActiveProcess = new UpdateDeviceInfo(this, ref Lib, panel);
409 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
410 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
411 }
412 }
413
75af24f1 414 public void SetPhysicalAddress(ushort physicalAddress)
006b76b9 415 {
75af24f1 416 if (!SuppressUpdates && ActiveProcess == null)
006b76b9 417 {
75af24f1
LOK
418 SetControlsEnabled(false);
419 SetControlText(cbPortNumber, string.Empty);
420 SetControlText(cbConnectedDevice, string.Empty);
421 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
422 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
423 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
006b76b9 424 }
75af24f1 425 }
6b92c1c4 426
6d866874
LOK
427 public void UpdateConfigurationAsync()
428 {
429 if (!SuppressUpdates && ActiveProcess == null)
430 {
431 SetControlsEnabled(false);
432 ActiveProcess = new UpdateConfiguration(ref Lib, Config);
433 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
434 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
435 }
436 }
437
75af24f1
LOK
438 public void SendImageViewOn(CecLogicalAddress address)
439 {
440 if (!SuppressUpdates && ActiveProcess == null)
441 {
442 SetControlsEnabled(false);
443 ActiveProcess = new SendImageViewOn(ref Lib, address);
444 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
445 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
006b76b9
LOK
446 }
447 }
448
75af24f1 449 public void ActivateSource(CecLogicalAddress address)
006b76b9 450 {
75af24f1
LOK
451 if (!SuppressUpdates && ActiveProcess == null)
452 {
453 SetControlsEnabled(false);
454 ActiveProcess = new SendActivateSource(ref Lib, address);
455 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
456 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
457 }
006b76b9
LOK
458 }
459
75af24f1 460 public void SendStandby(CecLogicalAddress address)
006b76b9 461 {
75af24f1 462 if (!SuppressUpdates && ActiveProcess == null)
006b76b9 463 {
75af24f1
LOK
464 SetControlsEnabled(false);
465 ActiveProcess = new SendStandby(ref Lib, address);
466 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
467 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
006b76b9 468 }
75af24f1
LOK
469 }
470
471 public void ShowDeviceInfo(CecLogicalAddress address)
472 {
473 if (!SuppressUpdates && ActiveProcess == null)
006b76b9 474 {
75af24f1
LOK
475 SetControlsEnabled(false);
476 ActiveProcess = new ShowDeviceInfo(this, ref Lib, address);
477 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
478 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
006b76b9
LOK
479 }
480 }
5de1dde4 481 #endregion
006b76b9 482
75af24f1
LOK
483 #region Configuration tab
484 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
006b76b9 485 {
75af24f1
LOK
486 if (tbPhysicalAddress.Text.Length != 4)
487 return;
488 ushort physicalAddress = 0;
489 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
490 return;
491
492 SetPhysicalAddress(physicalAddress);
006b76b9
LOK
493 }
494
75af24f1 495 private void UpdateSelectedDevice()
006b76b9 496 {
75af24f1
LOK
497 if (HasAVRDevice)
498 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
006b76b9 499 else
75af24f1 500 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
006b76b9
LOK
501 }
502
75af24f1 503 public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
006b76b9 504 {
75af24f1 505 if (!SuppressUpdates && ActiveProcess == null)
006b76b9 506 {
75af24f1
LOK
507 SetControlsEnabled(false);
508 ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber);
509 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
510 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
006b76b9
LOK
511 }
512 }
513
75af24f1 514 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
006b76b9 515 {
75af24f1 516 SetConnectedDevice(SelectedConnectedDevice, SelectedPortNumber);
ece1582e
LOK
517 }
518
006b76b9
LOK
519 private void bCancel_Click(object sender, EventArgs e)
520 {
521 this.Dispose();
522 }
523
524 private void bSave_Click(object sender, EventArgs e)
525 {
526 SetControlsEnabled(false);
527
528 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
75af24f1 529 Config.ActivateSource = cbActivateSource.Checked;
006b76b9
LOK
530 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
531 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
2b3c67ec
LOK
532 Config.WakeDevices = WakeDevices;
533 Config.PowerOffDevices = PowerOffDevices;
006b76b9
LOK
534
535 if (!Lib.CanPersistConfiguration())
536 {
537 if (ActiveProcess == null)
538 {
539 SetControlsEnabled(false);
540 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
541 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
542
543 SaveFileDialog dialog = new SaveFileDialog()
544 {
545 Title = "Where do you want to store the settings?",
546 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
547 FileName = "usb_2548_1001.xml",
548 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
549 FilterIndex = 1
550 };
551
552 if (dialog.ShowDialog() == DialogResult.OK)
553 {
de90f347
LOK
554 FileStream fs = null;
555 string error = string.Empty;
556 try
557 {
558 fs = (FileStream)dialog.OpenFile();
559 }
560 catch (Exception ex)
561 {
562 error = ex.Message;
563 }
006b76b9
LOK
564 if (fs == null)
565 {
de90f347 566 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing" + (error.Length > 0 ? ": " + error : string.Empty ), "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
006b76b9
LOK
567 }
568 else
569 {
570 StreamWriter writer = new StreamWriter(fs);
571 StringBuilder output = new StringBuilder();
572 output.AppendLine("<settings>");
573 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
574 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
75af24f1
LOK
575 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.ActivateSource ? 1 : 0) + "\" />");
576 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffDevices.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />");
006b76b9
LOK
577 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
578 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
579 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
580 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
581 output.AppendLine("<setting id=\"port\" value=\"\" />");
75af24f1
LOK
582
583 // only supported by 1.5.0+ clients
6d866874 584 output.AppendLine("<!-- the following lines are only supported by v1.5.0+ clients -->");
75af24f1
LOK
585 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
586 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
2b3c67ec 587 output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", (int)Config.TvVendor).Trim() + "\" />");
75af24f1
LOK
588
589 output.Append("<setting id=\"wake_devices\" value=\"");
590 foreach (CecLogicalAddress addr in Config.WakeDevices.Addresses)
2b3c67ec
LOK
591 if (addr != CecLogicalAddress.Unknown)
592 output.Append(" " + (int)addr);
75af24f1
LOK
593 output.AppendLine("\" />");
594
595 output.Append("<setting id=\"standby_devices\" value=\"");
596 foreach (CecLogicalAddress addr in Config.PowerOffDevices.Addresses)
2b3c67ec
LOK
597 if (addr != CecLogicalAddress.Unknown)
598 output.Append(" " + (int)addr);
75af24f1
LOK
599 output.AppendLine("\" />");
600
006b76b9
LOK
601 output.AppendLine("</settings>");
602 writer.Write(output.ToString());
603 writer.Close();
604 fs.Close();
605 fs.Dispose();
606 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
607 }
608 }
609 SetControlsEnabled(true);
610 }
611 }
612 else
613 {
614 if (!Lib.PersistConfiguration(Config))
615 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
616 else
617 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
618 }
619 SetControlsEnabled(true);
620 }
de90f347 621
5de1dde4
LOK
622 private void bReloadConfig_Click(object sender, EventArgs e)
623 {
624 if (Lib.CanPersistConfiguration())
625 {
626 Lib.GetCurrentConfiguration(Config);
627 ConfigurationChanged(Config);
628 }
629 else
630 {
631 ReloadXMLConfiguration();
632 }
633 }
634
de90f347
LOK
635 private void cbVendorOverride_CheckedChanged(object sender, EventArgs e)
636 {
637 if (cbVendorOverride.Checked)
638 {
639 cbVendorId.Enabled = true;
640 switch (cbVendorId.Text)
641 {
642 case "LG":
643 Config.TvVendor = CecVendorId.LG;
644 break;
645 case "Onkyo":
646 Config.TvVendor = CecVendorId.Onkyo;
647 break;
648 case "Panasonic":
649 Config.TvVendor = CecVendorId.Panasonic;
650 break;
651 case "Philips":
652 Config.TvVendor = CecVendorId.Philips;
653 break;
654 case "Pioneer":
655 Config.TvVendor = CecVendorId.Pioneer;
656 break;
657 case "Samsung":
658 Config.TvVendor = CecVendorId.Samsung;
659 break;
660 case "Sony":
661 Config.TvVendor = CecVendorId.Sony;
662 break;
663 case "Yamaha":
664 Config.TvVendor = CecVendorId.Yamaha;
665 break;
666 default:
667 Config.TvVendor = CecVendorId.Unknown;
668 break;
669 }
670 }
671 else
672 {
673 cbVendorId.Enabled = false;
674 Config.TvVendor = CecVendorId.Unknown;
675 }
676 }
6d866874
LOK
677
678 private void cbDeviceType_SelectedIndexChanged(object sender, EventArgs e)
679 {
680 CecDeviceType type = SelectedDeviceType;
681 if (type != Config.DeviceTypes.Types[0])
682 {
683 Config.DeviceTypes.Types[0] = type;
684 if (!DeviceChangeWarningDisplayed)
685 {
686 DeviceChangeWarningDisplayed = true;
687 MessageBox.Show("You have changed the device type. Save the configuration, and restart the application to use the new setting.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Warning);
688 }
689 }
690 }
75af24f1 691 #endregion
006b76b9 692
75af24f1
LOK
693 #region Key configuration tab
694 delegate void SelectKeypressRowCallback(CecKeypress key);
695 private void SelectKeypressRow(CecKeypress key)
6b92c1c4 696 {
75af24f1 697 if (dgButtons.InvokeRequired)
6b92c1c4 698 {
75af24f1
LOK
699 SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
700 try
6b92c1c4 701 {
75af24f1 702 this.Invoke(d, new object[] { key });
6b92c1c4 703 }
75af24f1
LOK
704 catch (Exception) { }
705 }
706 else
707 {
708 int rowIndex = -1;
709 foreach (DataGridViewRow row in dgButtons.Rows)
6b92c1c4 710 {
75af24f1
LOK
711 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
712 if (item != null && item.Key.Keycode == key.Keycode)
713 {
714 rowIndex = row.Index;
715 row.Selected = true;
716 item.Enabled = true;
717 }
718 else
719 {
720 row.Selected = false;
721 }
6b92c1c4 722 }
75af24f1
LOK
723 if (rowIndex > -1)
724 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
6b92c1c4
LOK
725 }
726 }
8674df6a
LOK
727
728 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
729 {
730 DataGridView grid = sender as DataGridView;
731 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
732 if (data == null || !data.Enabled)
733 e.CellStyle.ForeColor = Color.Gray;
734 }
75af24f1 735 #endregion
96fa7764 736
75af24f1 737 #region CEC Tester tab
6d866874
LOK
738 public void CheckActiveDevices()
739 {
740 CecLogicalAddresses activeDevices = Lib.GetActiveDevices();
741 List<string> deviceList = new List<string>();
742 foreach (CecLogicalAddress activeDevice in activeDevices.Addresses)
743 {
2b3c67ec 744 if (activeDevice != CecLogicalAddress.Unknown)
6d866874
LOK
745 deviceList.Add(string.Format("{0,1:X} : {1}", (int)activeDevice, Lib.ToString(activeDevice)));
746 }
747 deviceList.Add(string.Format("{0,1:X} : {1}", (int)CecLogicalAddress.Broadcast, Lib.ToString(CecLogicalAddress.Broadcast)));
748
749 SetActiveDevices(deviceList.ToArray());
750 }
751
752 delegate void SetActiveDevicesCallback(string[] activeDevices);
753 private void SetActiveDevices(string[] activeDevices)
754 {
755 if (this.cbCommandDestination.InvokeRequired)
756 {
757 SetActiveDevicesCallback d = new SetActiveDevicesCallback(SetActiveDevices);
758 try
759 {
760 this.Invoke(d, new object[] { activeDevices });
761 }
762 catch (Exception) { }
763 }
764 else
765 {
766 this.cbCommandDestination.Items.Clear();
767 foreach (string item in activeDevices)
768 this.cbCommandDestination.Items.Add(item);
769 }
770 }
771
ece1582e 772 delegate CecLogicalAddress GetTargetDeviceCallback();
96fa7764
LOK
773 private CecLogicalAddress GetTargetDevice()
774 {
ece1582e
LOK
775 if (this.cbCommandDestination.InvokeRequired)
776 {
777 GetTargetDeviceCallback d = new GetTargetDeviceCallback(GetTargetDevice);
778 CecLogicalAddress retval = CecLogicalAddress.Unknown;
779 try
780 {
781 retval = (CecLogicalAddress)this.Invoke(d, new object[] { });
782 }
783 catch (Exception) { }
784 return retval;
785 }
786
2b3c67ec
LOK
787 return GetLogicalAddressFromString(this.cbCommandDestination.Text);
788 }
789
790 private CecLogicalAddress GetLogicalAddressFromString(string name)
791 {
792 switch (name.Substring(0, 1).ToLower())
96fa7764
LOK
793 {
794 case "0":
795 return CecLogicalAddress.Tv;
796 case "1":
797 return CecLogicalAddress.RecordingDevice1;
798 case "2":
799 return CecLogicalAddress.RecordingDevice2;
800 case "3":
801 return CecLogicalAddress.Tuner1;
802 case "4":
803 return CecLogicalAddress.PlaybackDevice1;
804 case "5":
805 return CecLogicalAddress.AudioSystem;
806 case "6":
807 return CecLogicalAddress.Tuner2;
808 case "7":
809 return CecLogicalAddress.Tuner3;
810 case "8":
811 return CecLogicalAddress.PlaybackDevice2;
812 case "9":
813 return CecLogicalAddress.RecordingDevice3;
814 case "a":
815 return CecLogicalAddress.Tuner4;
816 case "b":
817 return CecLogicalAddress.PlaybackDevice3;
818 case "c":
819 return CecLogicalAddress.Reserved1;
820 case "d":
821 return CecLogicalAddress.Reserved2;
822 case "e":
823 return CecLogicalAddress.FreeUse;
824 case "f":
825 return CecLogicalAddress.Broadcast;
826 default:
827 return CecLogicalAddress.Unknown;
828 }
829 }
830
ece1582e
LOK
831 private void bSendImageViewOn_Click(object sender, EventArgs e)
832 {
833 SendImageViewOn(GetTargetDevice());
834 }
835
ece1582e
LOK
836 private void bStandby_Click(object sender, EventArgs e)
837 {
838 SendStandby(GetTargetDevice());
839 }
840
ece1582e
LOK
841 private void bScan_Click(object sender, EventArgs e)
842 {
843 ShowDeviceInfo(GetTargetDevice());
844 }
845
ece1582e
LOK
846 private void bActivateSource_Click(object sender, EventArgs e)
847 {
848 ActivateSource(GetTargetDevice());
849 }
850
851 private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e)
852 {
853 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
854 this.bVolUp.Enabled = enableVolumeButtons;
855 this.bVolDown.Enabled = enableVolumeButtons;
856 this.bMute.Enabled = enableVolumeButtons;
6d866874
LOK
857 this.bActivateSource.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
858 this.bScan.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
ece1582e
LOK
859 }
860
861 private void bVolUp_Click(object sender, EventArgs e)
862 {
863 SetControlsEnabled(false);
864 Lib.VolumeUp(true);
865 SetControlsEnabled(true);
866 }
867
868 private void bVolDown_Click(object sender, EventArgs e)
869 {
870 SetControlsEnabled(false);
871 Lib.VolumeDown(true);
872 SetControlsEnabled(true);
873 }
874
875 private void bMute_Click(object sender, EventArgs e)
876 {
877 SetControlsEnabled(false);
878 Lib.MuteAudio(true);
879 SetControlsEnabled(true);
880 }
6d866874
LOK
881
882 private void bRescanDevices_Click(object sender, EventArgs e)
883 {
884 if (!SuppressUpdates && ActiveProcess == null)
885 {
886 SetControlsEnabled(false);
3efda01a 887 ActiveProcess = new RescanDevices(ref Lib);
6d866874
LOK
888 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
889 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
890 }
891 }
75af24f1
LOK
892 #endregion
893
894 #region Log tab
5de1dde4
LOK
895 delegate void UpdateLogCallback();
896 private void UpdateLog()
75af24f1
LOK
897 {
898 if (tbLog.InvokeRequired)
899 {
5de1dde4 900 UpdateLogCallback d = new UpdateLogCallback(UpdateLog);
75af24f1
LOK
901 try
902 {
5de1dde4 903 this.Invoke(d, new object[] { });
75af24f1
LOK
904 }
905 catch (Exception) { }
906 }
907 else
908 {
5de1dde4
LOK
909 tbLog.Text = Log;
910 tbLog.Select(tbLog.Text.Length, 0);
911 tbLog.ScrollToCaret();
912 }
913 }
75af24f1 914
5de1dde4
LOK
915 private void AddLogMessage(CecLogMessage message)
916 {
917 string strLevel = "";
918 bool display = false;
919 switch (message.Level)
920 {
921 case CecLogLevel.Error:
922 strLevel = "ERROR: ";
923 display = cbLogError.Checked;
924 break;
925 case CecLogLevel.Warning:
926 strLevel = "WARNING: ";
927 display = cbLogWarning.Checked;
928 break;
929 case CecLogLevel.Notice:
930 strLevel = "NOTICE: ";
931 display = cbLogNotice.Checked;
932 break;
933 case CecLogLevel.Traffic:
934 strLevel = "TRAFFIC: ";
935 display = cbLogTraffic.Checked;
936 break;
937 case CecLogLevel.Debug:
938 strLevel = "DEBUG: ";
939 display = cbLogDebug.Checked;
940 break;
941 default:
942 break;
943 }
944
945 if (display)
946 {
947 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
948 Log += strLog;
75af24f1 949 }
5de1dde4
LOK
950
951 if (SelectedTab == ConfigTab.Log)
952 UpdateLog();
75af24f1
LOK
953 }
954
955 private void bClearLog_Click(object sender, EventArgs e)
956 {
6d866874
LOK
957 Log = string.Empty;
958 UpdateLog();
75af24f1
LOK
959 }
960
961 private void bSaveLog_Click(object sender, EventArgs e)
962 {
963 SaveFileDialog dialog = new SaveFileDialog()
964 {
965 Title = "Where do you want to store the log file?",
966 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
967 FileName = "cec-log.txt",
968 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
969 FilterIndex = 1
970 };
971
972 if (dialog.ShowDialog() == DialogResult.OK)
973 {
974 FileStream fs = (FileStream)dialog.OpenFile();
975 if (fs == null)
976 {
977 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
978 }
979 else
980 {
981 StreamWriter writer = new StreamWriter(fs);
6d866874 982 writer.Write(Log);
75af24f1
LOK
983 writer.Close();
984 fs.Close();
985 fs.Dispose();
986 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
987 }
988 }
989 }
990 #endregion
991
992 #region LibCecSharp callbacks
993 public int ConfigurationChanged(LibCECConfiguration config)
994 {
995 Config = config;
996 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
997 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
998 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
de90f347
LOK
999 switch (config.DeviceTypes.Types[0])
1000 {
1001 case CecDeviceType.RecordingDevice:
1002 SetControlText(cbDeviceType, "Recorder");
1003 break;
1004 case CecDeviceType.PlaybackDevice:
1005 SetControlText(cbDeviceType, "Player");
1006 break;
1007 case CecDeviceType.Tuner:
1008 SetControlText(cbDeviceType, "Tuner");
1009 break;
1010 default:
1011 SetControlText(cbDeviceType, "Recorder");
1012 break;
1013 }
1014 if (config.TvVendor != CecVendorId.Unknown)
1015 {
1016 SetCheckboxChecked(cbVendorOverride, true);
1017 SetControlText(cbVendorId, Lib.ToString(config.TvVendor));
1018 }
1019 else
1020 {
1021 SetCheckboxChecked(cbVendorOverride, false);
1022 SetControlText(cbVendorId, Lib.ToString(TVVendor));
1023 }
1024
75af24f1
LOK
1025 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
1026 SetCheckboxChecked(cbActivateSource, Config.ActivateSource);
1027 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
1028 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
1029 UpdateSelectedDevice();
2b3c67ec
LOK
1030
1031 for (int iPtr = 0; iPtr < 15; iPtr++)
1032 SetCheckboxItemChecked(cbWakeDevices, iPtr, Config.WakeDevices.IsSet((CecLogicalAddress)iPtr));
1033 for (int iPtr = 0; iPtr < 15; iPtr++)
1034 SetCheckboxItemChecked(cbPowerOffDevices, iPtr, Config.PowerOffDevices.IsSet((CecLogicalAddress)iPtr));
3efda01a
LOK
1035
1036 SetControlText(this, "Pulse-Eight USB-CEC Adapter - libCEC " + Lib.ToString(Config.ServerVersion));
75af24f1
LOK
1037 return 1;
1038 }
1039
1040 public int ReceiveCommand(CecCommand command)
1041 {
1042 return 1;
1043 }
1044
1045 public int ReceiveKeypress(CecKeypress key)
1046 {
1047 SelectKeypressRow(key);
1048 return 1;
1049 }
1050
1051 public int ReceiveLogMessage(CecLogMessage message)
1052 {
2b3c67ec
LOK
1053 try
1054 {
1055 AddLogMessage(message);
1056 }
1057 catch (Exception) { }
75af24f1
LOK
1058 return 1;
1059 }
1060 #endregion
1061
1062 #region Class members
1063 public bool HasAVRDevice { get; private set; }
1064 #region TV Vendor
1065 private CecVendorId _tvVendor = CecVendorId.Unknown;
1066 public CecVendorId TVVendor
1067 {
1068 get { return _tvVendor;}
1069 private set { _tvVendor = value; }
1070 }
1071 public string TVVendorString
1072 {
1073 get
1074 {
1075 return TVVendor != CecVendorId.Unknown ?
1076 "Television (" + Lib.ToString(TVVendor) + ")" :
1077 "Television";
1078 }
1079 }
1080 #endregion
1081 #region AVR Vendor
1082 private CecVendorId _avrVendor = CecVendorId.Unknown;
1083 public CecVendorId AVRVendor
1084 {
1085 get { return _avrVendor; }
1086 private set { _avrVendor = value; }
1087 }
1088 public string AVRVendorString
1089 {
1090 get
1091 {
1092 return AVRVendor != CecVendorId.Unknown ?
1093 "AVR (" + Lib.ToString(AVRVendor) + ")" :
1094 "AVR";
1095 }
1096 }
1097 #endregion
1098 public CecLogicalAddress SelectedConnectedDevice
1099 {
1100 get
1101 {
1102 return (cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
1103 }
1104 }
6d866874
LOK
1105 public CecDeviceType SelectedDeviceType
1106 {
1107 get
1108 {
1109 switch (cbDeviceType.Text.ToLower())
1110 {
1111 case "player":
1112 return CecDeviceType.PlaybackDevice;
1113 case "tuner":
1114 return CecDeviceType.Tuner;
1115 default:
1116 return CecDeviceType.RecordingDevice;
1117 }
1118 }
1119 }
75af24f1
LOK
1120 public int SelectedPortNumber
1121 {
1122 get
1123 {
1124 int iPortNumber = 0;
1125 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
1126 iPortNumber = 1;
1127 return iPortNumber;
1128 }
1129 }
1130 protected LibCECConfiguration Config;
1131 protected LibCecSharp Lib;
1132 private CecCallbackWrapper Callbacks;
1133 private UpdateProcess ActiveProcess = null;
5b8c2761 1134 private bool SuppressUpdates = true;
5de1dde4
LOK
1135 private ConfigTab SelectedTab = ConfigTab.Configuration;
1136 private string Log = string.Empty;
6d866874
LOK
1137 private DeviceInformation UpdatingInfoPanel = null;
1138 private bool DeviceChangeWarningDisplayed = false;
2b3c67ec
LOK
1139 public CecLogicalAddresses WakeDevices
1140 {
1141 get
1142 {
1143 CecLogicalAddresses addr = new CecLogicalAddresses();
1144 foreach (object item in this.cbWakeDevices.CheckedItems)
1145 {
1146 string c = item as string;
1147 addr.Set(GetLogicalAddressFromString(c));
1148 }
1149 return addr;
1150 }
1151 }
1152 public CecLogicalAddresses PowerOffDevices
1153 {
1154 get
1155 {
1156 CecLogicalAddresses addr = new CecLogicalAddresses();
1157 foreach (object item in this.cbPowerOffDevices.CheckedItems)
1158 {
1159 string c = item as string;
1160 addr.Set(GetLogicalAddressFromString(c));
1161 }
1162 return addr;
1163 }
1164 }
75af24f1 1165 #endregion
006b76b9
LOK
1166 }
1167
75af24f1
LOK
1168 /// <summary>
1169 /// A little wrapper that is needed because we already inherit form
1170 /// </summary>
006b76b9
LOK
1171 internal class CecCallbackWrapper : CecCallbackMethods
1172 {
1173 public CecCallbackWrapper(CecConfigGUI gui)
1174 {
1175 Gui = gui;
1176 }
1177
1178 public override int ReceiveCommand(CecCommand command)
1179 {
1180 return Gui.ReceiveCommand(command);
1181 }
1182
1183 public override int ReceiveKeypress(CecKeypress key)
1184 {
1185 return Gui.ReceiveKeypress(key);
1186 }
1187
1188 public override int ReceiveLogMessage(CecLogMessage message)
1189 {
1190 return Gui.ReceiveLogMessage(message);
1191 }
1192
32403cc3
LOK
1193 public override int ConfigurationChanged(LibCECConfiguration config)
1194 {
1195 return Gui.ConfigurationChanged(config);
1196 }
1197
006b76b9
LOK
1198 private CecConfigGUI Gui;
1199 }
1200}