forked from arkane-systems/mousejiggler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
266 lines (244 loc) · 9.82 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#region header
// ScreenAlive - MainForm.cs
//
// Aaron Cutright
// Copyright Cutright Industries 2017.
//
// Forked from
// https://mousejiggler.codeplex.com
// Alistair J. R. Young
// Arkane Systems
// Copyright Arkane Systems 2012-2013.
#endregion
using System;
using System.Windows.Forms;
using Microsoft.Win32;
namespace CutrightIndustries.ScreenAlive
{
public partial class MainForm : Form
{
internal class REGISTRY
{
public const string location = @"Software\Cutright Industries\ScreenAlive";
public const string enabled = "ScreenAliveEnabled";
public const string zen = "ZenJiggleEnabled";
public const string partialMatching = "PartialTitleMatchingEnabled";
public const string windowTitles = "WindowTitles";
public const string titleSeparator = @"<||;||;||>";
}
private const int MOUSEMOVE = 8;
protected bool zig = true;
protected bool titleMatch;
public MainForm()
{
InitializeComponent();
}
private void jiggleTimer_Tick(object sender, EventArgs ev)
{
Logger.Debug("jiggle tick, zen:"+cbZenJiggle.Checked, " partial:"+cbPartialTitleMatching.Checked);
if (cbEnabled.Checked)
{
//check if there are any window title matches, if so, enable the jiggle timer
string[] lines = tbWindowTitles.Text.Split('\n');
if (WindowDetection.IsAnyWindowOpen(lines, cbPartialTitleMatching.Checked))
{
Logger.Debug("jiggling cursor (zen:", cbZenJiggle.Checked.ToString(), ")");
//jiggle mouse cursor
if (cbZenJiggle.Checked)
Jiggler.Jiggle(0, 0);
else
{
if (zig)
Jiggler.Jiggle(4, 4);
else
Jiggler.Jiggle(-4, -4);
}
zig = !zig;
}
}
}
private void cbEnabled_CheckedChanged(object sender, EventArgs ev)
{
try
{
enableScreenAliveToolStripMenuItem.Checked = cbEnabled.Checked;
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY.location,
RegistryKeyPermissionCheck.ReadWriteSubTree);
if (cbEnabled.Checked)
key.SetValue(REGISTRY.enabled, 1);
else
key.SetValue(REGISTRY.enabled, 0);
}
catch (Exception ex)
{
Logger.Debug("Error writing to registry cbEnabled: " + ex.GetBaseException());
}
}
private void cmdAbout_Click(object sender, EventArgs ev)
{
using (var a = new AboutBox())
a.ShowDialog();
}
private void MainForm_Load(object sender, EventArgs ev)
{
try
{
Logger.Debug("initializing...");
//check registry key for settings
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY.location,
RegistryKeyPermissionCheck.ReadWriteSubTree);
var enabled = (int)key.GetValue(REGISTRY.enabled, 0);
var zen = (int)key.GetValue(REGISTRY.zen, 0);
var partialMatching = (int)key.GetValue(REGISTRY.partialMatching, 0);
var titles = (string)key.GetValue(REGISTRY.windowTitles, "");
Logger.DebugWithoutTimestamp("\tloaded all registry values");
Logger.DebugWithoutTimestamp("\tenabled: " + enabled);
Logger.DebugWithoutTimestamp("\tzen: " + zen);
Logger.DebugWithoutTimestamp("\tpartial matching: " + partialMatching);
Logger.DebugWithoutTimestamp("\ttitles: " + titles);
if (enabled == 0)
cbEnabled.Checked = false;
else
cbEnabled.Checked = true;
if (zen == 0)
cbZenJiggle.Checked = false;
else
cbZenJiggle.Checked = true;
if (partialMatching == 0)
cbPartialTitleMatching.Checked = false;
else
cbPartialTitleMatching.Checked = true;
if (titles != "")
{
//decode window titles from registry
string formattedTitles = "";
string[] titleArray = titles.Split(new[] { REGISTRY.titleSeparator }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < titleArray.Length; i++)
{
if (i < titleArray.Length - 1)
formattedTitles += titleArray[i].Trim() + "\r\n";
else
formattedTitles += titleArray[i].Trim();
}
//store in textbox
tbWindowTitles.Text = formattedTitles;
}
jiggleTimer.Enabled = true; //automatically start jiggle timer
jiggleTimer_Tick(null, null); //initialize timer (unnecessary, but guarantees no screen timeout)
}
catch (Exception ex)
{
Logger.Debug("Error during initalization: " + ex.GetBaseException());
}
if (Program.ZenJiggling)
cbZenJiggle.Checked = true;
if (Program.Enabled)
cbEnabled.Checked = true;
if (Program.PartialTitleMatching)
cbPartialTitleMatching.Checked = true;
if (Program.StartMinimized)
cmdToTray_Click(this, null);
}
private void cbZenJiggle_CheckedChanged(object sender, EventArgs ev)
{
try
{
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY.location,
RegistryKeyPermissionCheck.ReadWriteSubTree);
if (cbZenJiggle.Checked)
key.SetValue(REGISTRY.zen, 1);
else
key.SetValue(REGISTRY.zen, 0);
}
catch (Exception ex)
{
Logger.Debug("Error writing to registry cbZenJiggle: " + ex.GetBaseException());
}
}
private void cmdToTray_Click(object sender, EventArgs ev)
{
//hide the window
Visible = false;
//show tray icon
notifyIcon.Visible = true;
//remove taskbar icon
ShowInTaskbar = false;
}
private void notifyIcon_DoubleClick(object sender, EventArgs ev)
{
//restore the window
Visible = true;
//hide tray icon
notifyIcon.Visible = false;
//place icon to taskbar
ShowInTaskbar = true;
}
private void cbPartialTitleMatching_CheckedChanged(object sender, EventArgs ev)
{
try
{
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY.location,
RegistryKeyPermissionCheck.ReadWriteSubTree);
if (cbPartialTitleMatching.Checked)
key.SetValue(REGISTRY.partialMatching, 1);
else
key.SetValue(REGISTRY.partialMatching, 0);
}
catch (Exception ex)
{
Logger.Debug("Error writing to registry cbPatialMatching: " + ex.GetBaseException());
}
}
private void tbWindowTitles_TextChanged(object sender, EventArgs ev)
{
//do nothing
}
private void bTitlesUpdated_Click(object sender, EventArgs ev)
{
try
{
string[] lines = tbWindowTitles.Text.Split('\n');
if (cbEnabled.Checked)
{
//encode window titles array
string encodedLines = "";
for (int i = 0; i < lines.Length; i++)
{
if (i < lines.Length - 1)
encodedLines += lines[i] + REGISTRY.titleSeparator;
else
encodedLines += lines[i];
}
//store window titles in registry
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY.location,
RegistryKeyPermissionCheck.ReadWriteSubTree);
key.SetValue(REGISTRY.windowTitles, encodedLines, RegistryValueKind.String);
//update
jiggleTimer_Tick(null, null);
}
}
catch (Exception ex)
{
Logger.Debug("Error writing to registry bTitlesUpdated: " + ex.GetBaseException());
}
}
private void bContextMenuClose_Click(object sender, EventArgs e)
{
Close();
}
private void bConextMenuOpen_Click(object sender, EventArgs e)
{
//restore the window
Visible = true;
//hide tray icon
notifyIcon.Visible = false;
//place icon in taskbar
ShowInTaskbar = true;
}
private void enableScreenAliveToolStripMenuItem_Click(object sender, EventArgs e)
{
cbEnabled.Checked = enableScreenAliveToolStripMenuItem.Checked;
cbEnabled_CheckedChanged(null, null);
}
}
}