-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainForm.cs
229 lines (198 loc) · 8.42 KB
/
MainForm.cs
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace Magic.Samples.DisplaySettings
{
/// <summary>
/// The start-up form
/// </summary>
public partial class MainForm : Form
{
/// <summary>
/// Encapsulates the display settings. Initialized by the constructor of the form.
/// </summary>
private readonly DisplaySettings _originalSettings;
private delegate void SafeCallDelegate();
/// <summary>
/// Initializes a new instance of MainForm.
/// </summary>
public MainForm()
{
InitializeComponent();
this.Text = Application.ProductName;
_originalSettings = DisplayManager.GetCurrentSettings();
Thread thread = new Thread(() => ReadAndFollow(@"C:\ProgramData\NVIDIA Corporation\NvStream\NvStreamerCurrent.log"));
thread.Start();
}
private void MainForm_Load(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
GetCurrentSettings();
ListAllModes();
this.Cursor = Cursors.Default;
}
/// <summary>
/// Loads current display settings and refreshes the current settings labels.
/// </summary>
protected void GetCurrentSettings()
{
if (this.widthLabel.InvokeRequired)
{
var d = new SafeCallDelegate(GetCurrentSettings);
Invoke(d);
}
else
{
DisplaySettings set = DisplayManager.GetCurrentSettings();
this.widthLabel.Text = set.Width.ToString(CultureInfo.InvariantCulture);
this.heightLabel.Text = set.Height.ToString(CultureInfo.InvariantCulture);
this.orientationLabel.Text = ((int)set.Orientation * 90).ToString(CultureInfo.InvariantCulture);
this.bitsLabel.Text = set.BitCount.ToString(CultureInfo.InvariantCulture);
this.freqLabel.Text = set.Frequency.ToString(CultureInfo.InvariantCulture);
}
}
/// <summary>
/// Loads all supported display modes and lists them in the modes list view.
/// </summary>
protected void ListAllModes()
{
this.modesListView.BeginUpdate();
this.modesListView.Items.Clear();
IEnumerator<DisplaySettings> enumerator = DisplayManager.GetModesEnumerator();
DisplaySettings set;
ListViewItem itm;
while (enumerator.MoveNext())
{
set = enumerator.Current;
itm = new ListViewItem(set.Index.ToString(CultureInfo.InvariantCulture));
itm.SubItems.Add(set.Width.ToString(CultureInfo.InvariantCulture));
itm.SubItems.Add(set.Height.ToString(CultureInfo.InvariantCulture));
itm.SubItems.Add(((int)set.Orientation * 90).ToString(CultureInfo.InvariantCulture));
itm.SubItems.Add(set.BitCount.ToString(CultureInfo.InvariantCulture));
itm.SubItems.Add(set.Frequency.ToString(CultureInfo.InvariantCulture));
itm.Tag = set;
this.modesListView.Items.Add(itm);
}
this.modesListView.EndUpdate();
}
private void rotateClockwiseButton_Click(object sender, EventArgs e)
{
DisplayManager.RotateScreen(true);
GetCurrentSettings();
ListAllModes();
}
private void rotateAntiClockwiseButton_Click(object sender, EventArgs e)
{
DisplayManager.RotateScreen(false);
GetCurrentSettings();
ListAllModes();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
DisplayManager.SetDisplaySettings(_originalSettings);
}
private void modesListView_DoubleClick(object sender, EventArgs e)
{
if (this.modesListView.SelectedItems.Count == 0) return;
DisplaySettings set = (DisplaySettings)this.modesListView.SelectedItems[0].Tag;
DisplayManager.SetDisplaySettings(set);
GetCurrentSettings();
ListAllModes();
}
private void resetButton_Click(object sender, EventArgs e)
{
DisplayManager.SetDisplaySettings(_originalSettings);
GetCurrentSettings();
ListAllModes();
}
public void ReadAndFollow(string filename)
{
using (StreamReader reader = new StreamReader(new FileStream(filename,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
// start at the start of the file
long lastMaxOffset = 0;
while (true)
{
Thread.Sleep(500);
//if the file size has not changed, idle
if (reader.BaseStream.Length == lastMaxOffset)
continue;
//seek to the last max offset
reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin);
//read out of the file until the EOF
bool updateResolution = false;
string line;
int width = 0;
int height = 0;
int refresh = 0;
bool useHdr = false;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains("clientViewport"))
{
Console.WriteLine(line);
string[] subs = line.Split(':');
if (subs.Length >= 2)
{
updateResolution = true;
if (subs[subs.Length - 2].Contains("clientViewportWd"))
{
width = int.Parse(subs[subs.Length - 1].Trim());
}
else
{
height = int.Parse(subs[subs.Length - 1].Trim());
}
}
}
if (line.Contains("maxFPS"))
{
Console.WriteLine(line);
string[] subs = line.Split(':');
if (subs.Length >= 2)
{
updateResolution = true;
refresh = int.Parse(subs[subs.Length - 1].Trim());
}
}
if (line.Contains("dynamicRangeMode"))
{
Console.WriteLine(line);
string[] subs = line.Split(':');
if (subs.Length >= 2)
{
updateResolution = true;
useHdr = bool.Parse(subs[subs.Length - 1].Trim());
}
}
}
if (updateResolution)
{
IEnumerator<DisplaySettings> enumerator = DisplayManager.GetModesEnumerator();
DisplaySettings set;
DisplaySettings bestSet = DisplayManager.GetCurrentSettings();
while (enumerator.MoveNext())
{
set = enumerator.Current;
if (set.Width == width && set.Height == height)
{
if (bestSet.Width != set.Width || bestSet.Height != set.Height || set.Frequency > bestSet.Frequency)
{
bestSet = set;
}
}
}
DisplayManager.SetDisplaySettings(bestSet);
GetCurrentSettings();
}
// update the last max offset
lastMaxOffset = reader.BaseStream.Position;
}
}
}
}
}