cec-config-gui: added implementations for "wake devices" and "standby devices"
[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:
274 SetProgressValue(pProgress, updateEvent.IntValue);
275 break;
276 case UpdateEventType.TVVendorId:
277 TVVendor = (CecVendorId)updateEvent.IntValue;
278 UpdateSelectedDevice();
279 break;
280 case UpdateEventType.BaseDevicePhysicalAddress:
281 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
282 break;
283 case UpdateEventType.BaseDevice:
284 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
285 break;
286 case UpdateEventType.HDMIPort:
287 Config.HDMIPort = (byte)updateEvent.IntValue;
288 break;
289 case UpdateEventType.MenuLanguage:
290 SetControlText(cbUseTVMenuLanguage, "Use the TV's language setting" + (updateEvent.StringValue.Length > 0 ? " (" + updateEvent.StringValue + ")" : ""));
291 break;
292 case UpdateEventType.HasAVRDevice:
293 if (HasAVRDevice != updateEvent.BoolValue)
8674df6a 294 {
75af24f1
LOK
295 HasAVRDevice = updateEvent.BoolValue;
296 UpdateSelectedDevice();
8674df6a 297 }
75af24f1
LOK
298 break;
299 case UpdateEventType.AVRVendorId:
300 AVRVendor = (CecVendorId)updateEvent.IntValue;
301 UpdateSelectedDevice();
302 break;
303 case UpdateEventType.Configuration:
304 SuppressUpdates = true;
305 ConfigurationChanged(updateEvent.ConfigValue);
306 SuppressUpdates = false;
307 break;
6d866874
LOK
308 case UpdateEventType.PollDevices:
309 CheckActiveDevices();
310 break;
75af24f1
LOK
311 case UpdateEventType.ProcessCompleted:
312 ActiveProcess = null;
313 SetControlsEnabled(true);
6d866874
LOK
314 if (UpdatingInfoPanel != null)
315 {
316 UpdatingInfoPanel.SetControlEnabled(UpdatingInfoPanel.bUpdate, true);
317 UpdatingInfoPanel = null;
318 }
75af24f1 319 break;
8674df6a
LOK
320 }
321 }
322
5de1dde4
LOK
323 private void SetControlsEnabled(bool val)
324 {
325 SetControlEnabled(cbPortNumber, val);
326 SetControlEnabled(cbConnectedDevice, cbConnectedDevice.Items.Count > 1 ? val : false);
327 SetControlEnabled(tbPhysicalAddress, val);
6d866874 328 SetControlEnabled(cbDeviceType, val);
5de1dde4
LOK
329 SetControlEnabled(cbUseTVMenuLanguage, val);
330 SetControlEnabled(cbActivateSource, val);
331 SetControlEnabled(cbPowerOffScreensaver, val);
332 SetControlEnabled(cbPowerOffOnStandby, val);
2b3c67ec
LOK
333 SetControlEnabled(cbWakeDevices, val);
334 SetControlEnabled(cbPowerOffDevices, val);
5de1dde4
LOK
335 SetControlEnabled(cbVendorOverride, val);
336 SetControlEnabled(cbVendorId, val && cbVendorOverride.Checked);
337 SetControlEnabled(bClose, val);
338 SetControlEnabled(bSaveConfig, val);
339 SetControlEnabled(bReloadConfig, val);
6d866874 340 SetControlEnabled(bRescanDevices, val);
5de1dde4
LOK
341
342 SetControlEnabled(bSendImageViewOn, val);
343 SetControlEnabled(bStandby, val);
344 SetControlEnabled(bActivateSource, val);
345 SetControlEnabled(bScan, val);
346
347 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem) && val;
348 SetControlEnabled(bVolUp, enableVolumeButtons);
349 SetControlEnabled(bVolDown, enableVolumeButtons);
350 SetControlEnabled(bMute, enableVolumeButtons);
351 }
352
353 private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
354 {
355 switch (tabControl1.SelectedIndex)
356 {
357 case 0:
358 SelectedTab = ConfigTab.Configuration;
359 break;
360 case 1:
361 SelectedTab = ConfigTab.KeyConfiguration;
362 break;
363 case 2:
364 SelectedTab = ConfigTab.Tester;
365 break;
366 case 3:
367 SelectedTab = ConfigTab.Log;
368 UpdateLog();
369 break;
370 default:
371 SelectedTab = ConfigTab.Configuration;
372 break;
373 }
374 }
375
376 #region Actions
377 public void ReloadXMLConfiguration()
378 {
379 LoadXMLConfiguration(ref Config);
380 Lib.SetConfiguration(Config);
381 ConfigurationChanged(Config);
382 }
383
6d866874
LOK
384 public void UpdateInfoPanel(DeviceInformation panel)
385 {
386 if (!SuppressUpdates && ActiveProcess == null)
387 {
388 SetControlsEnabled(false);
389 UpdatingInfoPanel = panel;
390 panel.SetControlEnabled(panel.bUpdate, false);
391 ActiveProcess = new UpdateDeviceInfo(this, ref Lib, panel);
392 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
393 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
394 }
395 }
396
75af24f1 397 public void SetPhysicalAddress(ushort physicalAddress)
006b76b9 398 {
75af24f1 399 if (!SuppressUpdates && ActiveProcess == null)
006b76b9 400 {
75af24f1
LOK
401 SetControlsEnabled(false);
402 SetControlText(cbPortNumber, string.Empty);
403 SetControlText(cbConnectedDevice, string.Empty);
404 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
405 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
406 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
006b76b9 407 }
75af24f1 408 }
6b92c1c4 409
6d866874
LOK
410 public void UpdateConfigurationAsync()
411 {
412 if (!SuppressUpdates && ActiveProcess == null)
413 {
414 SetControlsEnabled(false);
415 ActiveProcess = new UpdateConfiguration(ref Lib, Config);
416 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
417 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
418 }
419 }
420
75af24f1
LOK
421 public void SendImageViewOn(CecLogicalAddress address)
422 {
423 if (!SuppressUpdates && ActiveProcess == null)
424 {
425 SetControlsEnabled(false);
426 ActiveProcess = new SendImageViewOn(ref Lib, address);
427 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
428 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
006b76b9
LOK
429 }
430 }
431
75af24f1 432 public void ActivateSource(CecLogicalAddress address)
006b76b9 433 {
75af24f1
LOK
434 if (!SuppressUpdates && ActiveProcess == null)
435 {
436 SetControlsEnabled(false);
437 ActiveProcess = new SendActivateSource(ref Lib, address);
438 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
439 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
440 }
006b76b9
LOK
441 }
442
75af24f1 443 public void SendStandby(CecLogicalAddress address)
006b76b9 444 {
75af24f1 445 if (!SuppressUpdates && ActiveProcess == null)
006b76b9 446 {
75af24f1
LOK
447 SetControlsEnabled(false);
448 ActiveProcess = new SendStandby(ref Lib, address);
449 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
450 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
006b76b9 451 }
75af24f1
LOK
452 }
453
454 public void ShowDeviceInfo(CecLogicalAddress address)
455 {
456 if (!SuppressUpdates && ActiveProcess == null)
006b76b9 457 {
75af24f1
LOK
458 SetControlsEnabled(false);
459 ActiveProcess = new ShowDeviceInfo(this, ref Lib, address);
460 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
461 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
006b76b9
LOK
462 }
463 }
5de1dde4 464 #endregion
006b76b9 465
75af24f1
LOK
466 #region Configuration tab
467 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
006b76b9 468 {
75af24f1
LOK
469 if (tbPhysicalAddress.Text.Length != 4)
470 return;
471 ushort physicalAddress = 0;
472 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
473 return;
474
475 SetPhysicalAddress(physicalAddress);
006b76b9
LOK
476 }
477
75af24f1 478 private void UpdateSelectedDevice()
006b76b9 479 {
75af24f1
LOK
480 if (HasAVRDevice)
481 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
006b76b9 482 else
75af24f1 483 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
006b76b9
LOK
484 }
485
75af24f1 486 public void SetConnectedDevice(CecLogicalAddress address, int portnumber)
006b76b9 487 {
75af24f1 488 if (!SuppressUpdates && ActiveProcess == null)
006b76b9 489 {
75af24f1
LOK
490 SetControlsEnabled(false);
491 ActiveProcess = new UpdateConnectedDevice(ref Lib, address, portnumber);
492 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
493 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
006b76b9
LOK
494 }
495 }
496
75af24f1 497 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
006b76b9 498 {
75af24f1 499 SetConnectedDevice(SelectedConnectedDevice, SelectedPortNumber);
ece1582e
LOK
500 }
501
006b76b9
LOK
502 private void bCancel_Click(object sender, EventArgs e)
503 {
504 this.Dispose();
505 }
506
507 private void bSave_Click(object sender, EventArgs e)
508 {
509 SetControlsEnabled(false);
510
511 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
75af24f1 512 Config.ActivateSource = cbActivateSource.Checked;
006b76b9
LOK
513 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
514 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
2b3c67ec
LOK
515 Config.WakeDevices = WakeDevices;
516 Config.PowerOffDevices = PowerOffDevices;
006b76b9
LOK
517
518 if (!Lib.CanPersistConfiguration())
519 {
520 if (ActiveProcess == null)
521 {
522 SetControlsEnabled(false);
523 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
524 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
525
526 SaveFileDialog dialog = new SaveFileDialog()
527 {
528 Title = "Where do you want to store the settings?",
529 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
530 FileName = "usb_2548_1001.xml",
531 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
532 FilterIndex = 1
533 };
534
535 if (dialog.ShowDialog() == DialogResult.OK)
536 {
de90f347
LOK
537 FileStream fs = null;
538 string error = string.Empty;
539 try
540 {
541 fs = (FileStream)dialog.OpenFile();
542 }
543 catch (Exception ex)
544 {
545 error = ex.Message;
546 }
006b76b9
LOK
547 if (fs == null)
548 {
de90f347 549 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
550 }
551 else
552 {
553 StreamWriter writer = new StreamWriter(fs);
554 StringBuilder output = new StringBuilder();
555 output.AppendLine("<settings>");
556 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
557 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
75af24f1
LOK
558 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.ActivateSource ? 1 : 0) + "\" />");
559 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffDevices.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />");
006b76b9
LOK
560 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
561 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
562 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
563 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
564 output.AppendLine("<setting id=\"port\" value=\"\" />");
75af24f1
LOK
565
566 // only supported by 1.5.0+ clients
6d866874 567 output.AppendLine("<!-- the following lines are only supported by v1.5.0+ clients -->");
75af24f1
LOK
568 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
569 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
2b3c67ec 570 output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", (int)Config.TvVendor).Trim() + "\" />");
75af24f1
LOK
571
572 output.Append("<setting id=\"wake_devices\" value=\"");
573 foreach (CecLogicalAddress addr in Config.WakeDevices.Addresses)
2b3c67ec
LOK
574 if (addr != CecLogicalAddress.Unknown)
575 output.Append(" " + (int)addr);
75af24f1
LOK
576 output.AppendLine("\" />");
577
578 output.Append("<setting id=\"standby_devices\" value=\"");
579 foreach (CecLogicalAddress addr in Config.PowerOffDevices.Addresses)
2b3c67ec
LOK
580 if (addr != CecLogicalAddress.Unknown)
581 output.Append(" " + (int)addr);
75af24f1
LOK
582 output.AppendLine("\" />");
583
006b76b9
LOK
584 output.AppendLine("</settings>");
585 writer.Write(output.ToString());
586 writer.Close();
587 fs.Close();
588 fs.Dispose();
589 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
590 }
591 }
592 SetControlsEnabled(true);
593 }
594 }
595 else
596 {
597 if (!Lib.PersistConfiguration(Config))
598 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
599 else
600 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
601 }
602 SetControlsEnabled(true);
603 }
de90f347 604
5de1dde4
LOK
605 private void bReloadConfig_Click(object sender, EventArgs e)
606 {
607 if (Lib.CanPersistConfiguration())
608 {
609 Lib.GetCurrentConfiguration(Config);
610 ConfigurationChanged(Config);
611 }
612 else
613 {
614 ReloadXMLConfiguration();
615 }
616 }
617
de90f347
LOK
618 private void cbVendorOverride_CheckedChanged(object sender, EventArgs e)
619 {
620 if (cbVendorOverride.Checked)
621 {
622 cbVendorId.Enabled = true;
623 switch (cbVendorId.Text)
624 {
625 case "LG":
626 Config.TvVendor = CecVendorId.LG;
627 break;
628 case "Onkyo":
629 Config.TvVendor = CecVendorId.Onkyo;
630 break;
631 case "Panasonic":
632 Config.TvVendor = CecVendorId.Panasonic;
633 break;
634 case "Philips":
635 Config.TvVendor = CecVendorId.Philips;
636 break;
637 case "Pioneer":
638 Config.TvVendor = CecVendorId.Pioneer;
639 break;
640 case "Samsung":
641 Config.TvVendor = CecVendorId.Samsung;
642 break;
643 case "Sony":
644 Config.TvVendor = CecVendorId.Sony;
645 break;
646 case "Yamaha":
647 Config.TvVendor = CecVendorId.Yamaha;
648 break;
649 default:
650 Config.TvVendor = CecVendorId.Unknown;
651 break;
652 }
653 }
654 else
655 {
656 cbVendorId.Enabled = false;
657 Config.TvVendor = CecVendorId.Unknown;
658 }
659 }
6d866874
LOK
660
661 private void cbDeviceType_SelectedIndexChanged(object sender, EventArgs e)
662 {
663 CecDeviceType type = SelectedDeviceType;
664 if (type != Config.DeviceTypes.Types[0])
665 {
666 Config.DeviceTypes.Types[0] = type;
667 if (!DeviceChangeWarningDisplayed)
668 {
669 DeviceChangeWarningDisplayed = true;
670 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);
671 }
672 }
673 }
75af24f1 674 #endregion
006b76b9 675
75af24f1
LOK
676 #region Key configuration tab
677 delegate void SelectKeypressRowCallback(CecKeypress key);
678 private void SelectKeypressRow(CecKeypress key)
6b92c1c4 679 {
75af24f1 680 if (dgButtons.InvokeRequired)
6b92c1c4 681 {
75af24f1
LOK
682 SelectKeypressRowCallback d = new SelectKeypressRowCallback(SelectKeypressRow);
683 try
6b92c1c4 684 {
75af24f1 685 this.Invoke(d, new object[] { key });
6b92c1c4 686 }
75af24f1
LOK
687 catch (Exception) { }
688 }
689 else
690 {
691 int rowIndex = -1;
692 foreach (DataGridViewRow row in dgButtons.Rows)
6b92c1c4 693 {
75af24f1
LOK
694 CecButtonConfigItem item = row.DataBoundItem as CecButtonConfigItem;
695 if (item != null && item.Key.Keycode == key.Keycode)
696 {
697 rowIndex = row.Index;
698 row.Selected = true;
699 item.Enabled = true;
700 }
701 else
702 {
703 row.Selected = false;
704 }
6b92c1c4 705 }
75af24f1
LOK
706 if (rowIndex > -1)
707 dgButtons.FirstDisplayedScrollingRowIndex = rowIndex;
6b92c1c4
LOK
708 }
709 }
8674df6a
LOK
710
711 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
712 {
713 DataGridView grid = sender as DataGridView;
714 CecButtonConfigItem data = grid.Rows[e.RowIndex].DataBoundItem as CecButtonConfigItem;
715 if (data == null || !data.Enabled)
716 e.CellStyle.ForeColor = Color.Gray;
717 }
75af24f1 718 #endregion
96fa7764 719
75af24f1 720 #region CEC Tester tab
6d866874
LOK
721 public void CheckActiveDevices()
722 {
723 CecLogicalAddresses activeDevices = Lib.GetActiveDevices();
724 List<string> deviceList = new List<string>();
725 foreach (CecLogicalAddress activeDevice in activeDevices.Addresses)
726 {
2b3c67ec 727 if (activeDevice != CecLogicalAddress.Unknown)
6d866874
LOK
728 deviceList.Add(string.Format("{0,1:X} : {1}", (int)activeDevice, Lib.ToString(activeDevice)));
729 }
730 deviceList.Add(string.Format("{0,1:X} : {1}", (int)CecLogicalAddress.Broadcast, Lib.ToString(CecLogicalAddress.Broadcast)));
731
732 SetActiveDevices(deviceList.ToArray());
733 }
734
735 delegate void SetActiveDevicesCallback(string[] activeDevices);
736 private void SetActiveDevices(string[] activeDevices)
737 {
738 if (this.cbCommandDestination.InvokeRequired)
739 {
740 SetActiveDevicesCallback d = new SetActiveDevicesCallback(SetActiveDevices);
741 try
742 {
743 this.Invoke(d, new object[] { activeDevices });
744 }
745 catch (Exception) { }
746 }
747 else
748 {
749 this.cbCommandDestination.Items.Clear();
750 foreach (string item in activeDevices)
751 this.cbCommandDestination.Items.Add(item);
752 }
753 }
754
ece1582e 755 delegate CecLogicalAddress GetTargetDeviceCallback();
96fa7764
LOK
756 private CecLogicalAddress GetTargetDevice()
757 {
ece1582e
LOK
758 if (this.cbCommandDestination.InvokeRequired)
759 {
760 GetTargetDeviceCallback d = new GetTargetDeviceCallback(GetTargetDevice);
761 CecLogicalAddress retval = CecLogicalAddress.Unknown;
762 try
763 {
764 retval = (CecLogicalAddress)this.Invoke(d, new object[] { });
765 }
766 catch (Exception) { }
767 return retval;
768 }
769
2b3c67ec
LOK
770 return GetLogicalAddressFromString(this.cbCommandDestination.Text);
771 }
772
773 private CecLogicalAddress GetLogicalAddressFromString(string name)
774 {
775 switch (name.Substring(0, 1).ToLower())
96fa7764
LOK
776 {
777 case "0":
778 return CecLogicalAddress.Tv;
779 case "1":
780 return CecLogicalAddress.RecordingDevice1;
781 case "2":
782 return CecLogicalAddress.RecordingDevice2;
783 case "3":
784 return CecLogicalAddress.Tuner1;
785 case "4":
786 return CecLogicalAddress.PlaybackDevice1;
787 case "5":
788 return CecLogicalAddress.AudioSystem;
789 case "6":
790 return CecLogicalAddress.Tuner2;
791 case "7":
792 return CecLogicalAddress.Tuner3;
793 case "8":
794 return CecLogicalAddress.PlaybackDevice2;
795 case "9":
796 return CecLogicalAddress.RecordingDevice3;
797 case "a":
798 return CecLogicalAddress.Tuner4;
799 case "b":
800 return CecLogicalAddress.PlaybackDevice3;
801 case "c":
802 return CecLogicalAddress.Reserved1;
803 case "d":
804 return CecLogicalAddress.Reserved2;
805 case "e":
806 return CecLogicalAddress.FreeUse;
807 case "f":
808 return CecLogicalAddress.Broadcast;
809 default:
810 return CecLogicalAddress.Unknown;
811 }
812 }
813
ece1582e
LOK
814 private void bSendImageViewOn_Click(object sender, EventArgs e)
815 {
816 SendImageViewOn(GetTargetDevice());
817 }
818
ece1582e
LOK
819 private void bStandby_Click(object sender, EventArgs e)
820 {
821 SendStandby(GetTargetDevice());
822 }
823
ece1582e
LOK
824 private void bScan_Click(object sender, EventArgs e)
825 {
826 ShowDeviceInfo(GetTargetDevice());
827 }
828
ece1582e
LOK
829 private void bActivateSource_Click(object sender, EventArgs e)
830 {
831 ActivateSource(GetTargetDevice());
832 }
833
834 private void cbCommandDestination_SelectedIndexChanged(object sender, EventArgs e)
835 {
836 bool enableVolumeButtons = (GetTargetDevice() == CecLogicalAddress.AudioSystem);
837 this.bVolUp.Enabled = enableVolumeButtons;
838 this.bVolDown.Enabled = enableVolumeButtons;
839 this.bMute.Enabled = enableVolumeButtons;
6d866874
LOK
840 this.bActivateSource.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
841 this.bScan.Enabled = (GetTargetDevice() != CecLogicalAddress.Broadcast);
ece1582e
LOK
842 }
843
844 private void bVolUp_Click(object sender, EventArgs e)
845 {
846 SetControlsEnabled(false);
847 Lib.VolumeUp(true);
848 SetControlsEnabled(true);
849 }
850
851 private void bVolDown_Click(object sender, EventArgs e)
852 {
853 SetControlsEnabled(false);
854 Lib.VolumeDown(true);
855 SetControlsEnabled(true);
856 }
857
858 private void bMute_Click(object sender, EventArgs e)
859 {
860 SetControlsEnabled(false);
861 Lib.MuteAudio(true);
862 SetControlsEnabled(true);
863 }
6d866874
LOK
864
865 private void bRescanDevices_Click(object sender, EventArgs e)
866 {
867 if (!SuppressUpdates && ActiveProcess == null)
868 {
869 SetControlsEnabled(false);
870 ActiveProcess = new RescanDevices();
871 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
872 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
873 }
874 }
75af24f1
LOK
875 #endregion
876
877 #region Log tab
5de1dde4
LOK
878 delegate void UpdateLogCallback();
879 private void UpdateLog()
75af24f1
LOK
880 {
881 if (tbLog.InvokeRequired)
882 {
5de1dde4 883 UpdateLogCallback d = new UpdateLogCallback(UpdateLog);
75af24f1
LOK
884 try
885 {
5de1dde4 886 this.Invoke(d, new object[] { });
75af24f1
LOK
887 }
888 catch (Exception) { }
889 }
890 else
891 {
5de1dde4
LOK
892 tbLog.Text = Log;
893 tbLog.Select(tbLog.Text.Length, 0);
894 tbLog.ScrollToCaret();
895 }
896 }
75af24f1 897
5de1dde4
LOK
898 private void AddLogMessage(CecLogMessage message)
899 {
900 string strLevel = "";
901 bool display = false;
902 switch (message.Level)
903 {
904 case CecLogLevel.Error:
905 strLevel = "ERROR: ";
906 display = cbLogError.Checked;
907 break;
908 case CecLogLevel.Warning:
909 strLevel = "WARNING: ";
910 display = cbLogWarning.Checked;
911 break;
912 case CecLogLevel.Notice:
913 strLevel = "NOTICE: ";
914 display = cbLogNotice.Checked;
915 break;
916 case CecLogLevel.Traffic:
917 strLevel = "TRAFFIC: ";
918 display = cbLogTraffic.Checked;
919 break;
920 case CecLogLevel.Debug:
921 strLevel = "DEBUG: ";
922 display = cbLogDebug.Checked;
923 break;
924 default:
925 break;
926 }
927
928 if (display)
929 {
930 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
931 Log += strLog;
75af24f1 932 }
5de1dde4
LOK
933
934 if (SelectedTab == ConfigTab.Log)
935 UpdateLog();
75af24f1
LOK
936 }
937
938 private void bClearLog_Click(object sender, EventArgs e)
939 {
6d866874
LOK
940 Log = string.Empty;
941 UpdateLog();
75af24f1
LOK
942 }
943
944 private void bSaveLog_Click(object sender, EventArgs e)
945 {
946 SaveFileDialog dialog = new SaveFileDialog()
947 {
948 Title = "Where do you want to store the log file?",
949 InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
950 FileName = "cec-log.txt",
951 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
952 FilterIndex = 1
953 };
954
955 if (dialog.ShowDialog() == DialogResult.OK)
956 {
957 FileStream fs = (FileStream)dialog.OpenFile();
958 if (fs == null)
959 {
960 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
961 }
962 else
963 {
964 StreamWriter writer = new StreamWriter(fs);
6d866874 965 writer.Write(Log);
75af24f1
LOK
966 writer.Close();
967 fs.Close();
968 fs.Dispose();
969 MessageBox.Show("The log file was stored as '" + dialog.FileName + "'.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
970 }
971 }
972 }
973 #endregion
974
975 #region LibCecSharp callbacks
976 public int ConfigurationChanged(LibCECConfiguration config)
977 {
978 Config = config;
979 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
980 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
981 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
de90f347
LOK
982 switch (config.DeviceTypes.Types[0])
983 {
984 case CecDeviceType.RecordingDevice:
985 SetControlText(cbDeviceType, "Recorder");
986 break;
987 case CecDeviceType.PlaybackDevice:
988 SetControlText(cbDeviceType, "Player");
989 break;
990 case CecDeviceType.Tuner:
991 SetControlText(cbDeviceType, "Tuner");
992 break;
993 default:
994 SetControlText(cbDeviceType, "Recorder");
995 break;
996 }
997 if (config.TvVendor != CecVendorId.Unknown)
998 {
999 SetCheckboxChecked(cbVendorOverride, true);
1000 SetControlText(cbVendorId, Lib.ToString(config.TvVendor));
1001 }
1002 else
1003 {
1004 SetCheckboxChecked(cbVendorOverride, false);
1005 SetControlText(cbVendorId, Lib.ToString(TVVendor));
1006 }
1007
75af24f1
LOK
1008 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
1009 SetCheckboxChecked(cbActivateSource, Config.ActivateSource);
1010 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
1011 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
1012 UpdateSelectedDevice();
2b3c67ec
LOK
1013
1014 for (int iPtr = 0; iPtr < 15; iPtr++)
1015 SetCheckboxItemChecked(cbWakeDevices, iPtr, Config.WakeDevices.IsSet((CecLogicalAddress)iPtr));
1016 for (int iPtr = 0; iPtr < 15; iPtr++)
1017 SetCheckboxItemChecked(cbPowerOffDevices, iPtr, Config.PowerOffDevices.IsSet((CecLogicalAddress)iPtr));
75af24f1
LOK
1018 return 1;
1019 }
1020
1021 public int ReceiveCommand(CecCommand command)
1022 {
1023 return 1;
1024 }
1025
1026 public int ReceiveKeypress(CecKeypress key)
1027 {
1028 SelectKeypressRow(key);
1029 return 1;
1030 }
1031
1032 public int ReceiveLogMessage(CecLogMessage message)
1033 {
2b3c67ec
LOK
1034 try
1035 {
1036 AddLogMessage(message);
1037 }
1038 catch (Exception) { }
75af24f1
LOK
1039 return 1;
1040 }
1041 #endregion
1042
1043 #region Class members
1044 public bool HasAVRDevice { get; private set; }
1045 #region TV Vendor
1046 private CecVendorId _tvVendor = CecVendorId.Unknown;
1047 public CecVendorId TVVendor
1048 {
1049 get { return _tvVendor;}
1050 private set { _tvVendor = value; }
1051 }
1052 public string TVVendorString
1053 {
1054 get
1055 {
1056 return TVVendor != CecVendorId.Unknown ?
1057 "Television (" + Lib.ToString(TVVendor) + ")" :
1058 "Television";
1059 }
1060 }
1061 #endregion
1062 #region AVR Vendor
1063 private CecVendorId _avrVendor = CecVendorId.Unknown;
1064 public CecVendorId AVRVendor
1065 {
1066 get { return _avrVendor; }
1067 private set { _avrVendor = value; }
1068 }
1069 public string AVRVendorString
1070 {
1071 get
1072 {
1073 return AVRVendor != CecVendorId.Unknown ?
1074 "AVR (" + Lib.ToString(AVRVendor) + ")" :
1075 "AVR";
1076 }
1077 }
1078 #endregion
1079 public CecLogicalAddress SelectedConnectedDevice
1080 {
1081 get
1082 {
1083 return (cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
1084 }
1085 }
6d866874
LOK
1086 public CecDeviceType SelectedDeviceType
1087 {
1088 get
1089 {
1090 switch (cbDeviceType.Text.ToLower())
1091 {
1092 case "player":
1093 return CecDeviceType.PlaybackDevice;
1094 case "tuner":
1095 return CecDeviceType.Tuner;
1096 default:
1097 return CecDeviceType.RecordingDevice;
1098 }
1099 }
1100 }
75af24f1
LOK
1101 public int SelectedPortNumber
1102 {
1103 get
1104 {
1105 int iPortNumber = 0;
1106 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
1107 iPortNumber = 1;
1108 return iPortNumber;
1109 }
1110 }
1111 protected LibCECConfiguration Config;
1112 protected LibCecSharp Lib;
1113 private CecCallbackWrapper Callbacks;
1114 private UpdateProcess ActiveProcess = null;
5b8c2761 1115 private bool SuppressUpdates = true;
5de1dde4
LOK
1116 private ConfigTab SelectedTab = ConfigTab.Configuration;
1117 private string Log = string.Empty;
6d866874
LOK
1118 private DeviceInformation UpdatingInfoPanel = null;
1119 private bool DeviceChangeWarningDisplayed = false;
2b3c67ec
LOK
1120 public CecLogicalAddresses WakeDevices
1121 {
1122 get
1123 {
1124 CecLogicalAddresses addr = new CecLogicalAddresses();
1125 foreach (object item in this.cbWakeDevices.CheckedItems)
1126 {
1127 string c = item as string;
1128 addr.Set(GetLogicalAddressFromString(c));
1129 }
1130 return addr;
1131 }
1132 }
1133 public CecLogicalAddresses PowerOffDevices
1134 {
1135 get
1136 {
1137 CecLogicalAddresses addr = new CecLogicalAddresses();
1138 foreach (object item in this.cbPowerOffDevices.CheckedItems)
1139 {
1140 string c = item as string;
1141 addr.Set(GetLogicalAddressFromString(c));
1142 }
1143 return addr;
1144 }
1145 }
75af24f1 1146 #endregion
006b76b9
LOK
1147 }
1148
75af24f1
LOK
1149 /// <summary>
1150 /// A little wrapper that is needed because we already inherit form
1151 /// </summary>
006b76b9
LOK
1152 internal class CecCallbackWrapper : CecCallbackMethods
1153 {
1154 public CecCallbackWrapper(CecConfigGUI gui)
1155 {
1156 Gui = gui;
1157 }
1158
1159 public override int ReceiveCommand(CecCommand command)
1160 {
1161 return Gui.ReceiveCommand(command);
1162 }
1163
1164 public override int ReceiveKeypress(CecKeypress key)
1165 {
1166 return Gui.ReceiveKeypress(key);
1167 }
1168
1169 public override int ReceiveLogMessage(CecLogMessage message)
1170 {
1171 return Gui.ReceiveLogMessage(message);
1172 }
1173
32403cc3
LOK
1174 public override int ConfigurationChanged(LibCECConfiguration config)
1175 {
1176 return Gui.ConfigurationChanged(config);
1177 }
1178
006b76b9
LOK
1179 private CecConfigGUI Gui;
1180 }
1181}