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