-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRDPStateSaver.cs
213 lines (179 loc) · 7.45 KB
/
RDPStateSaver.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace RDPStateSaver
{
[Serializable]
public enum SaveSystemEvent
{
RemoteDisconnect,
SessionLock
}
public partial class RDPStateSaver : Form
{
private WindowWorker worker;
private WindowSavedState disconnectState;
private WindowSavedState manualSavedState;
public RDPStateSaver()
{
InitializeComponent();
sysTrayIcon.Text = Application.ProductName;
sysTrayIcon.Icon = Properties.Resources.Icon;
sysTrayMenu_AutoManageState.Checked = Properties.Settings.Default.AutoManageState;
sysTrayMenu_AutoSaveInterval.Text = Properties.Settings.Default.AutoSaveInterval.ToString();
sysTrayMenu_SystemEventRemote.Checked = Properties.Settings.Default.SaveSystemEvent == SaveSystemEvent.RemoteDisconnect;
sysTrayMenu_SystemEventSession.Checked = Properties.Settings.Default.SaveSystemEvent == SaveSystemEvent.SessionLock;
worker = new WindowWorker();
disconnectState = new WindowSavedState();
manualSavedState = new WindowSavedState();
Log.Clear();
}
private void RDPStateSaver_Load(object sender, EventArgs e)
{
SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
if (Properties.Settings.Default.AutoManageState)
{
//It's not ideal running this constantly but I can't find any event that occurs on RDP
//disconnect that doesn't fire until after the number of monitors has changed (and thereby
//stores incorrect positions). I went through every event covered by SystemEvents but they
//are all too late (even DisplaySettingsChanging). So until I have time to look for a
//better solution, just store off the positions periodically.
worker.Start();
}
}
private void RDPStateSaver_FormClosing(object sender, FormClosingEventArgs e)
{
SystemEvents.SessionSwitch -= new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
base.OnLoad(e);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
private void HandleDisconnect()
{
if (Properties.Settings.Default.AutoManageState)
{
worker.Pause();
disconnectState.Set(worker.SavedState);
LogState("State on disconnect", disconnectState);
sysTrayMenu_RestoreDisconnectState.Enabled = disconnectState.HasState;
}
}
private void HandleConnect()
{
if (Properties.Settings.Default.AutoManageState)
{
Task.Run(async delegate
{
await Task.Delay(500);
disconnectState.Restore();
LogState("State after (re)connect restore:");
worker.Unpause();
});
}
}
private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
Log.Add("Session Switch Event: " + e.Reason.ToString());
if ((e.Reason == SessionSwitchReason.RemoteDisconnect && Properties.Settings.Default.SaveSystemEvent == SaveSystemEvent.RemoteDisconnect) ||
(e.Reason == SessionSwitchReason.SessionLock && Properties.Settings.Default.SaveSystemEvent == SaveSystemEvent.SessionLock))
{
HandleDisconnect();
}
else if ((e.Reason == SessionSwitchReason.RemoteConnect && Properties.Settings.Default.SaveSystemEvent == SaveSystemEvent.RemoteDisconnect) ||
(e.Reason == SessionSwitchReason.SessionUnlock && Properties.Settings.Default.SaveSystemEvent == SaveSystemEvent.SessionLock))
{
HandleConnect();
}
}
private void sysTrayMenu_AutoManageState_Click(object sender, EventArgs e)
{
Properties.Settings.Default.AutoManageState = !Properties.Settings.Default.AutoManageState;
Properties.Settings.Default.Save();
if (Properties.Settings.Default.AutoManageState)
{
sysTrayMenu_AutoManageState.Checked = true;
worker.Start();
}
else
{
sysTrayMenu_AutoManageState.Checked = false;
worker.Stop();
}
}
private void sysTrayMenu_ManualSaveState_Click(object sender, EventArgs e)
{
worker.SaveWindowsState(manualSavedState);
sysTrayMenu_ManualRestoreState.Enabled = manualSavedState.HasState;
LogState("State on manual save:", manualSavedState);
}
private void sysTrayMenu_ManualRestoreState_Click(object sender, EventArgs e)
{
manualSavedState.Restore();
LogState("State after manual restore:");
}
private void sysTrayMenu_Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void sysTrayMenu_SystemEventRemote_Click(object sender, EventArgs e)
{
Properties.Settings.Default.SaveSystemEvent = SaveSystemEvent.RemoteDisconnect;
Properties.Settings.Default.Save();
sysTrayMenu_SystemEventRemote.Checked = true;
sysTrayMenu_SystemEventSession.Checked = false;
}
private void sysTrayMenu_SystemEventSession_Click(object sender, EventArgs e)
{
Properties.Settings.Default.SaveSystemEvent = SaveSystemEvent.SessionLock;
Properties.Settings.Default.Save();
sysTrayMenu_SystemEventRemote.Checked = false;
sysTrayMenu_SystemEventSession.Checked = true;
}
private void sysTrayMenu_RestoreDisconnectState_Click(object sender, EventArgs e)
{
if (disconnectState.HasState)
{
disconnectState.Restore();
LogState("State after restore state on disconnect:");
}
}
private void LogState(string text, WindowSavedState state = null)
{
//If we have a specific state in mind then log that.
//Otherwise grab the current state and log it.
if (state == null)
{
state = new WindowSavedState();
worker.SaveWindowsState(state);
}
state.LogState(text);
}
private void sysTrayMenu_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
if (Int32.TryParse(sysTrayMenu_AutoSaveInterval.Text, out int newValue))
{
Properties.Settings.Default.AutoSaveInterval = Math.Max(1000, newValue);
Properties.Settings.Default.Save();
}
else
{
sysTrayMenu_AutoSaveInterval.Text = Properties.Settings.Default.AutoSaveInterval.ToString();
}
}
}
}