cec-config-gui: open the connection in the background thread
[deb_libcec.git] / src / cec-config-gui / actions / ConnectToDevice.cs
CommitLineData
4555ed72 1using CecSharp;
006b76b9 2using System.Windows.Forms;
006b76b9
LOK
3
4namespace CecConfigGui.actions
5{
6 class ConnectToDevice : UpdateProcess
7 {
4555ed72 8 public ConnectToDevice(ref LibCecSharp lib, LibCECConfiguration config)
006b76b9
LOK
9 {
10 Lib = lib;
4555ed72 11 Config = config;
006b76b9
LOK
12 }
13
14 public override void Process()
15 {
a70f3777
LOK
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
006b76b9
LOK
27 SendEvent(UpdateEventType.StatusText, "Detecting TV vendor...");
28 SendEvent(UpdateEventType.ProgressBar, 25);
29 SendEvent(UpdateEventType.TVVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.Tv));
30
f976869e
LOK
31 SendEvent(UpdateEventType.StatusText, "Detecting menu language...");
32 SendEvent(UpdateEventType.ProgressBar, 40);
33 SendEvent(UpdateEventType.MenuLanguage, Lib.GetDeviceMenuLanguage(CecLogicalAddress.Tv));
34
006b76b9
LOK
35 SendEvent(UpdateEventType.ProgressBar, 50);
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, 75);
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, 80);
4555ed72
LOK
51 SendEvent(UpdateEventType.StatusText, "Activating the source...");
52 Lib.SetActiveSource(CecDeviceType.Reserved);
006b76b9
LOK
53 }
54
55 SendEvent(UpdateEventType.ProgressBar, 90);
56 SendEvent(UpdateEventType.StatusText, "Reading device configuration...");
57
4555ed72
LOK
58 Lib.GetCurrentConfiguration(Config);
59 SendEvent(Config);
006b76b9
LOK
60
61 SendEvent(UpdateEventType.ProgressBar, 100);
62 SendEvent(UpdateEventType.StatusText, "Ready.");
63 }
64
65 private LibCecSharp Lib;
4555ed72 66 private LibCECConfiguration Config;
006b76b9
LOK
67 }
68}