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
201
202
203
204
205
|
using Newtonsoft.Json;
using System;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Text;
using System.Drawing;
using grapher.Models.Devices;
using System.Collections.Generic;
namespace grapher.Models.Serialized
{
public class SettingsManager
{
#region Constructors
public SettingsManager(
Field dpiField,
Field pollRateField,
ToolStripMenuItem autoWrite,
ToolStripMenuItem showLastMouseMove,
ToolStripMenuItem showVelocityAndGain,
ToolStripMenuItem streamingMode,
ToolStripMenuItem deviceMenuItem)
{
DpiField = dpiField;
PollRateField = pollRateField;
AutoWriteMenuItem = autoWrite;
ShowLastMouseMoveMenuItem = showLastMouseMove;
ShowVelocityAndGainMoveMenuItem = showVelocityAndGain;
StreamingModeMenuItem = streamingMode;
deviceMenuItem.Click += (s, e) => new DeviceMenuForm(this).ShowDialog();
SystemDevices = new List<MultiHandleDevice>();
ActiveHandles = new List<IntPtr>();
// TODO - remove ActiveConfig/AutoWrite entirely?
// shouldn't be needed with internal profiles support
ActiveConfig = DriverConfig.GetActive();
GuiSettings = GUISettings.MaybeLoad();
if (GuiSettings is null)
{
GuiSettings = MakeGUISettingsFromFields();
GuiSettings.Save();
}
else
{
UpdateFieldsFromGUISettings();
}
UserConfig = InitUserSettings();
}
#endregion Constructors
#region Properties
public GUISettings GuiSettings { get; private set; }
public DriverConfig ActiveConfig { get; private set; }
public Profile ActiveProfile
{
get => ActiveConfig.profiles[0];
}
public ManagedAccel ActiveAccel
{
get => ActiveConfig.accels[0];
}
public DriverConfig UserConfig { get; private set; }
public Profile UserProfile
{
get => UserConfig.profiles[0];
}
public Field DpiField { get; private set; }
public Field PollRateField { get; private set; }
public IList<MultiHandleDevice> SystemDevices { get; private set; }
public List<IntPtr> ActiveHandles { get; private set; }
private ToolStripMenuItem AutoWriteMenuItem { get; set; }
private ToolStripMenuItem ShowLastMouseMoveMenuItem { get; set; }
private ToolStripMenuItem ShowVelocityAndGainMoveMenuItem { get; set; }
private ToolStripMenuItem StreamingModeMenuItem{ get; set; }
#endregion Properties
#region Methods
public void DisableDriver()
{
ActiveConfig = DriverConfig.GetDefault();
new Thread(() => DriverConfig.Deactivate()).Start();
}
public void UpdateFieldsFromGUISettings()
{
DpiField.SetToEntered(GuiSettings.DPI);
PollRateField.SetToEntered(GuiSettings.PollRate);
ShowLastMouseMoveMenuItem.Checked = GuiSettings.ShowLastMouseMove;
ShowVelocityAndGainMoveMenuItem.Checked = GuiSettings.ShowVelocityAndGain;
StreamingModeMenuItem.Checked = GuiSettings.StreamingMode;
AutoWriteMenuItem.Checked = GuiSettings.AutoWriteToDriverOnStartup;
}
public bool TryActivate(DriverConfig settings, out string errors)
{
errors = settings.Errors();
if (errors == null)
{
GuiSettings = MakeGUISettingsFromFields();
GuiSettings.Save();
UserConfig = settings;
ActiveConfig = settings;
File.WriteAllText(Constants.DefaultSettingsFileName, settings.ToJSON());
new Thread(() => ActiveConfig.Activate()).Start();
}
return errors == null;
}
public void SetHiddenOptions(Profile settings)
{
settings.snap = UserProfile.snap;
settings.maximumSpeed = UserProfile.maximumSpeed;
settings.minimumSpeed = UserProfile.minimumSpeed;
settings.directionalMultipliers = UserProfile.directionalMultipliers;
}
public GUISettings MakeGUISettingsFromFields()
{
return new GUISettings
{
DPI = (int)DpiField.Data,
PollRate = (int)PollRateField.Data,
ShowLastMouseMove = ShowLastMouseMoveMenuItem.Checked,
ShowVelocityAndGain = ShowVelocityAndGainMoveMenuItem.Checked,
AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked,
StreamingMode = StreamingModeMenuItem.Checked
};
}
private void SetActiveHandles()
{
ActiveHandles.Clear();
// TODO
foreach (var sysDev in SystemDevices)
{
ActiveHandles.AddRange(sysDev.handles);
}
}
private void OnProfileSwitch()
{
SetActiveHandles();
}
public void OnDeviceChangeMessage()
{
SystemDevices = MultiHandleDevice.GetList();
SetActiveHandles();
}
private DriverConfig InitUserSettings()
{
var path = Constants.DefaultSettingsFileName;
if (File.Exists(path))
{
try
{
var (cfg, err) = DriverConfig.Convert(File.ReadAllText(path));
if (!GuiSettings.AutoWriteToDriverOnStartup ||
(err == null && TryActivate(cfg, out string _)))
{
return cfg;
}
}
catch (JsonException e)
{
System.Diagnostics.Debug.WriteLine($"bad settings: {e}");
}
}
File.WriteAllText(path, ActiveConfig.ToJSON());
return ActiveConfig;
}
#endregion Methods
}
}
|