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