summaryrefslogtreecommitdiff
path: root/grapher/Models/Devices/DeviceIDManager.cs
blob: 39856a168c15cff4517473353d8d968db9a92c4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace grapher.Models.Devices
{
    public class DeviceIDManager
    {
        public DeviceIDManager(ToolStripMenuItem deviceIDs)
        {
            DeviceIDsMenuItem = deviceIDs;
            DeviceIDsMenuItem.Checked = false;
        }

        public ToolStripMenuItem DeviceIDsMenuItem { get; }

        public string ID { get => SelectedDeviceID.ID; }

        public DeviceIDItem SelectedDeviceID { get; private set; }

        public Dictionary<string, DeviceIDItem> DeviceIDs { get; private set; }

        public void SetActive(DeviceIDItem deviceIDItem)
        {
            if (SelectedDeviceID != null)
            {
                SelectedDeviceID.SetDeactivated();
            }

            SelectedDeviceID = deviceIDItem;
            SelectedDeviceID.SetActivated();
        }

        public void Update(string devID)
        {
            DeviceIDsMenuItem.DropDownItems.Clear();

            bool found = string.IsNullOrEmpty(devID);

            var anyDevice = new DeviceIDItem("Any", string.Empty, this);

            if (found) SetActive(anyDevice);

            foreach (string id in RawInputInterop.GetDeviceIDs())
            {
                var deviceItem = new DeviceIDItem(string.Empty, id, this);
                if (!found && deviceItem.ID.Equals(devID))
                {
                    SetActive(deviceItem);
                    found = true;
                }
            }

            if (!found)
            {
                var deviceItem = new DeviceIDItem(string.Empty, devID, this);
                deviceItem.SetDisconnected();
                SetActive(deviceItem);
            }
        }

    }
}