cec-config-gui: make overriding the physical address optional
[deb_libcec.git] / src / cec-config-gui / actions / ConnectToDevice.cs
1 using CecSharp;
2 using System.Windows.Forms;
3
4 namespace CecConfigGui.actions
5 {
6 class ConnectToDevice : UpdateProcess
7 {
8 public ConnectToDevice(ref LibCecSharp lib, LibCECConfiguration config)
9 {
10 Lib = lib;
11 Config = config;
12 }
13
14 public override void Process()
15 {
16 SendEvent(UpdateEventType.StatusText, "Opening connection...");
17 SendEvent(UpdateEventType.ProgressBar, 10);
18
19 //TODO read the com port setting from the configuration
20 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
21 if (adapters.Length == 0 || !Lib.Open(adapters[0].ComPort, 10000))
22 {
23 MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration and try again.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK);
24 Application.Exit();
25 }
26
27 SendEvent(UpdateEventType.ProgressBar, 20);
28 SendEvent(UpdateEventType.StatusText, "Sending power on commands...");
29 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
30
31 SendEvent(UpdateEventType.StatusText, "Detecting TV vendor...");
32 SendEvent(UpdateEventType.ProgressBar, 30);
33 SendEvent(UpdateEventType.TVVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.Tv));
34
35 SendEvent(UpdateEventType.StatusText, "Detecting menu language...");
36 SendEvent(UpdateEventType.ProgressBar, 40);
37 SendEvent(UpdateEventType.MenuLanguage, Lib.GetDeviceMenuLanguage(CecLogicalAddress.Tv));
38
39 SendEvent(UpdateEventType.ProgressBar, 50);
40 SendEvent(UpdateEventType.StatusText, "Detecting AVR devices...");
41
42 bool hasAVRDevice = Lib.IsActiveDevice(CecLogicalAddress.AudioSystem);
43 SendEvent(UpdateEventType.HasAVRDevice, hasAVRDevice);
44
45 if (hasAVRDevice)
46 {
47 SendEvent(UpdateEventType.ProgressBar, 60);
48 SendEvent(UpdateEventType.StatusText, "Detecting AVR vendor...");
49 SendEvent(UpdateEventType.AVRVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.AudioSystem));
50 }
51
52 if (!Lib.GetDevicePowerStatus(CecLogicalAddress.Tv).Equals(CecPowerStatus.On))
53 {
54 SendEvent(UpdateEventType.ProgressBar, 70);
55 SendEvent(UpdateEventType.StatusText, "Activating the source...");
56 Lib.SetActiveSource(CecDeviceType.Reserved);
57 }
58
59 SendEvent(UpdateEventType.ProgressBar, 80);
60 SendEvent(UpdateEventType.StatusText, "Reading device configuration...");
61
62 Lib.GetCurrentConfiguration(Config);
63 SendEvent(Config);
64
65 SendEvent(UpdateEventType.ProgressBar, 90);
66 SendEvent(UpdateEventType.StatusText, "Polling active devices");
67 SendEvent(UpdateEventType.PollDevices);
68
69 SendEvent(UpdateEventType.ProgressBar, 100);
70 SendEvent(UpdateEventType.StatusText, "Ready.");
71 }
72
73 private LibCecSharp Lib;
74 private LibCECConfiguration Config;
75 }
76 }