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