Merge branch 'master' into release
[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)
22 {
23 DialogResult result = MessageBox.Show("Could not detect to any CEC adapter. Please check your configuration. Do you want to try again?", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.YesNo);
24 if (result == DialogResult.No)
25 Application.Exit();
26 else
27 adapters = Lib.FindAdapters(string.Empty);
28 }
29
30 while (!Lib.Open(adapters[0].ComPort, 10000))
31 {
32 DialogResult result = MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration. Do you want to try again?", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.YesNo);
33 if (result == DialogResult.No)
34 Application.Exit();
35 }
36
37 SendEvent(UpdateEventType.ProgressBar, 20);
38 SendEvent(UpdateEventType.StatusText, "Sending power on commands...");
39 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
40
41 SendEvent(UpdateEventType.StatusText, "Detecting TV vendor...");
42 SendEvent(UpdateEventType.ProgressBar, 30);
43 SendEvent(UpdateEventType.TVVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.Tv));
44
45 SendEvent(UpdateEventType.StatusText, "Detecting menu language...");
46 SendEvent(UpdateEventType.ProgressBar, 40);
47 SendEvent(UpdateEventType.MenuLanguage, Lib.GetDeviceMenuLanguage(CecLogicalAddress.Tv));
48
49 SendEvent(UpdateEventType.ProgressBar, 50);
50 SendEvent(UpdateEventType.StatusText, "Detecting AVR devices...");
51
52 bool hasAVRDevice = Lib.IsActiveDevice(CecLogicalAddress.AudioSystem);
53 SendEvent(UpdateEventType.HasAVRDevice, hasAVRDevice);
54
55 if (hasAVRDevice)
56 {
57 SendEvent(UpdateEventType.ProgressBar, 60);
58 SendEvent(UpdateEventType.StatusText, "Detecting AVR vendor...");
59 SendEvent(UpdateEventType.AVRVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.AudioSystem));
60 }
61
62 if (!Lib.GetDevicePowerStatus(CecLogicalAddress.Tv).Equals(CecPowerStatus.On))
63 {
64 SendEvent(UpdateEventType.ProgressBar, 70);
65 SendEvent(UpdateEventType.StatusText, "Activating the source...");
66 Lib.SetActiveSource(CecDeviceType.Reserved);
67 }
68
69 SendEvent(UpdateEventType.ProgressBar, 80);
70 SendEvent(UpdateEventType.StatusText, "Reading device configuration...");
71
72 Lib.GetCurrentConfiguration(Config);
73 SendEvent(Config);
74
75 SendEvent(UpdateEventType.ProgressBar, 90);
76 SendEvent(UpdateEventType.StatusText, "Polling active devices");
77 SendEvent(UpdateEventType.PollDevices);
78
79 SendEvent(UpdateEventType.ProgressBar, 100);
80 SendEvent(UpdateEventType.StatusText, "Ready.");
81 }
82
83 private LibCecSharp Lib;
84 private LibCECConfiguration Config;
85 }
86 }