summaryrefslogtreecommitdiff
path: root/grapher/Models/Serialized/SettingsManager.cs
blob: 3adbc8bb54e8701eefb4e39c8ef1a42781bebfb8 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using Newtonsoft.Json;
using System;
using System.Windows.Forms;
using System.Threading;
using System.Text;
using System.Drawing;

namespace grapher.Models.Serialized
{
    public class SettingsManager
    {
        #region Constructors

        public SettingsManager(
            ManagedAccel activeAccel,
            Field dpiField,
            Field pollRateField,
            ToolStripMenuItem autoWrite,
            ToolStripMenuItem useSpecificDevice,
            ToolStripMenuItem showLastMouseMove,
            ToolStripMenuItem showVelocityAndGain)
        {
            ActiveAccel = activeAccel;
            DpiField = dpiField;
            PollRateField = pollRateField;
            AutoWriteMenuItem = autoWrite;
            UseSpecificDeviceMenuItem = useSpecificDevice;
            ShowLastMouseMoveMenuItem = showLastMouseMove;
            ShowVelocityAndGainMoveMenuItem = showVelocityAndGain;
        }

        #endregion Constructors

        #region Properties

        public ManagedAccel ActiveAccel { get; }

        public RawAccelSettings RawAccelSettings { get; private set; }

        public Field DpiField { get; private set; }

        public Field PollRateField { get; private set; }

        private ToolStripMenuItem AutoWriteMenuItem { get; set; }

        private ToolStripMenuItem UseSpecificDeviceMenuItem { get; set; }

        private ToolStripMenuItem ShowLastMouseMoveMenuItem { get; set; }

        private ToolStripMenuItem ShowVelocityAndGainMoveMenuItem { get; set; }

        #endregion Properties

        #region Methods
        public SettingsErrors TryUpdateActiveSettings(DriverSettings settings)
        {
            var errors = TryUpdateAccel(settings);

            if (errors.Empty())
            {
                RawAccelSettings.AccelerationSettings = settings;
                RawAccelSettings.GUISettings = MakeGUISettingsFromFields();
                RawAccelSettings.Save();
            }

            return errors;
        }

        private void SpecificDeviceClickHnadler(Object o, EventArgs a, string hwid)
        {
            var item = (ToolStripMenuItem)o;
            foreach (ToolStripMenuItem i in UseSpecificDeviceMenuItem.DropDownItems)
            {
                i.Checked = false;
            }
            item.Checked = true;
            if (hwid == null || hwid == "")
            {
                UseSpecificDeviceMenuItem.Checked = false;
            } else
            {
                UseSpecificDeviceMenuItem.Checked = true;
            }
            RawAccelSettings.AccelerationSettings.deviceHardwareID = hwid;

            TryUpdateActiveSettings(RawAccelSettings.AccelerationSettings);
            
        }
        private void UpdateUseSpecificDeviceMenu()
        {
            var hwid = RawAccelSettings.AccelerationSettings.deviceHardwareID;
            if (hwid == null) { hwid = ""; }

            UseSpecificDeviceMenuItem.Checked = hwid.Length > 0;
            UseSpecificDeviceMenuItem.DropDownItems.Clear();

            var any_device = new ToolStripMenuItem();
            any_device.Text = "";
            any_device.Checked = hwid.Length == 0;
            any_device.Click += new EventHandler(delegate(Object o, EventArgs a) { SpecificDeviceClickHnadler(o, a, ""); });
            UseSpecificDeviceMenuItem.DropDownItems.Add(any_device);

            var hwid_not_found = true;

            foreach (Tuple<string,string> device in Models.Devices.DeviceList.GetDeviceHardwareIDs())
            {
                if (hwid == device.Item2)
                {
                    hwid_not_found = false;
                }
                var dev = new ToolStripMenuItem();
                dev.Text = device.Item1;
                dev.Checked = device.Item2 == RawAccelSettings.AccelerationSettings.deviceHardwareID;
                dev.Click += new EventHandler(delegate (Object o, EventArgs a) { SpecificDeviceClickHnadler(o, a, device.Item2); });
                UseSpecificDeviceMenuItem.DropDownItems.Add(dev);
            }

            if (hwid.Length > 0 && hwid_not_found)
            {
                var current_hwid = new ToolStripMenuItem();
                current_hwid.Text = "Disconnected (" + hwid + ")";
                current_hwid.ForeColor = Color.DarkGray;
                current_hwid.Checked = true;
                current_hwid.Click += new EventHandler(delegate (Object o, EventArgs a) { SpecificDeviceClickHnadler(o, a, hwid); });
                UseSpecificDeviceMenuItem.DropDownItems.Add(current_hwid);
            }
        }

        public void UpdateFieldsFromGUISettings()
        {
            DpiField.SetToEntered(RawAccelSettings.GUISettings.DPI);
            PollRateField.SetToEntered(RawAccelSettings.GUISettings.PollRate);
            ShowLastMouseMoveMenuItem.Checked = RawAccelSettings.GUISettings.ShowLastMouseMove;
            ShowVelocityAndGainMoveMenuItem.Checked = RawAccelSettings.GUISettings.ShowVelocityAndGain;
            AutoWriteMenuItem.Checked = RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup;
        }

        public SettingsErrors TryUpdateAccel(DriverSettings settings)
        {
            var errors = SendToDriverSafe(settings);
            if (errors.Empty()) ActiveAccel.UpdateFromSettings(settings);
            return errors;
        }

        public static void SendToDriver(DriverSettings settings)
        {
            new Thread(() => DriverInterop.Write(settings)).Start();
        }

        public static SettingsErrors SendToDriverSafe(DriverSettings settings)
        {
            var errors = DriverInterop.GetSettingsErrors(settings);
            if (errors.Empty()) SendToDriver(settings);
            return errors;
        }

        public GUISettings MakeGUISettingsFromFields()
        {
            return new GUISettings
            {
                DPI = (int)DpiField.Data,
                PollRate = (int)PollRateField.Data,
                ShowLastMouseMove = ShowLastMouseMoveMenuItem.Checked,
                ShowVelocityAndGain = ShowVelocityAndGainMoveMenuItem.Checked,
                AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked
            };
        }

        // Returns true when file settings are active
        public bool Startup()
        {
            if (RawAccelSettings.Exists())
            {
                try
                {
                    RawAccelSettings = RawAccelSettings.Load(() => MakeGUISettingsFromFields());
                    UpdateFieldsFromGUISettings();
                    UpdateUseSpecificDeviceMenu();
                    if (RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup)
                    {
                        TryUpdateAccel(RawAccelSettings.AccelerationSettings);
                    }
                    return RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup;
                }
                catch (JsonException e)
                {
                    Console.WriteLine($"bad settings: {e}");
                }
            }

            RawAccelSettings = new RawAccelSettings(
                DriverInterop.GetActiveSettings(),
                MakeGUISettingsFromFields());
            RawAccelSettings.Save();
            return true;
        }

        #endregion Methods
    }
}