43d786eccf1df168f9751e447f539c0583e98882
[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.StatusText, "Detecting TV vendor...");
28 SendEvent(UpdateEventType.ProgressBar, 20);
29 SendEvent(UpdateEventType.TVVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.Tv));
30
31 SendEvent(UpdateEventType.StatusText, "Detecting menu language...");
32 SendEvent(UpdateEventType.ProgressBar, 30);
33 SendEvent(UpdateEventType.MenuLanguage, Lib.GetDeviceMenuLanguage(CecLogicalAddress.Tv));
34
35 SendEvent(UpdateEventType.ProgressBar, 40);
36 SendEvent(UpdateEventType.StatusText, "Detecting AVR devices...");
37
38 bool hasAVRDevice = Lib.IsActiveDevice(CecLogicalAddress.AudioSystem);
39 SendEvent(UpdateEventType.HasAVRDevice, hasAVRDevice);
40
41 if (hasAVRDevice)
42 {
43 SendEvent(UpdateEventType.ProgressBar, 50);
44 SendEvent(UpdateEventType.StatusText, "Detecting AVR vendor...");
45 SendEvent(UpdateEventType.AVRVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.AudioSystem));
46 }
47
48 if (!Lib.GetDevicePowerStatus(CecLogicalAddress.Tv).Equals(CecPowerStatus.On))
49 {
50 SendEvent(UpdateEventType.ProgressBar, 60);
51 SendEvent(UpdateEventType.StatusText, "Activating the source...");
52 Lib.SetActiveSource(CecDeviceType.Reserved);
53 }
54
55 SendEvent(UpdateEventType.ProgressBar, 70);
56 SendEvent(UpdateEventType.StatusText, "Reading device configuration...");
57
58 Lib.GetCurrentConfiguration(Config);
59 SendEvent(Config);
60
61 SendEvent(UpdateEventType.ProgressBar, 80);
62 SendEvent(UpdateEventType.StatusText, "Polling active devices");
63 SendEvent(UpdateEventType.PollDevices);
64
65 SendEvent(UpdateEventType.ProgressBar, 100);
66 SendEvent(UpdateEventType.StatusText, "Ready.");
67 }
68
69 private LibCecSharp Lib;
70 private LibCECConfiguration Config;
71 }
72 }