-
Notifications
You must be signed in to change notification settings - Fork 1
/
SettingsForm.cs
513 lines (441 loc) · 16.2 KB
/
SettingsForm.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
using Gma.System.MouseKeyHook;
using Microsoft.Win32;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using PushToTalk.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
namespace PushToTalk
{
enum State
{
None,
Active,
Query
}
public partial class SettingsForm : Form, IDisposable
{
private static string None = "<None>";
private bool active;
private ManualResetEvent quit = new ManualResetEvent(false);
private ManualResetEvent trigger = new ManualResetEvent(false);
// Icon that displays in the tray area, it changes colours based on the state of the microphone
private System.Windows.Forms.NotifyIcon notifier;
private IKeyboardMouseEvents m_GlobalHook;
private State mouse;
private State key;
private HashSet<Keys> keys = new HashSet<Keys>();
private HashSet<Keys> currentKeys = new HashSet<Keys>();
private HashSet<MouseButtons> buttons = new HashSet<MouseButtons>();
private HashSet<MouseButtons> currentButtons = new HashSet<MouseButtons>();
private Task loop;
private WaveOut wave = new WaveOut();
private RegistryKey app = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("FearTheCowboy").CreateSubKey("PushToTalk");
private MicState micState;
private bool loading = false;
public SettingsForm()
{
InitializeComponent();
micState = new MicState();
CreateTrayIcon();
m_GlobalHook = Hook.GlobalEvents();
m_GlobalHook.MouseDownExt += mouseDown;
m_GlobalHook.KeyDown += keyDown;
m_GlobalHook.MouseUp += mouseUp;
m_GlobalHook.KeyUp += keyUp;
loop = Task.Run(worker);
keyText.Text = None;
mouseText.Text = None;
micState.LocationChanged += MicState_LocationChanged;
this.Load += SettingsForm_Load;
}
private void SettingsForm_Load(object sender, EventArgs e)
{
load();
}
private void MicState_LocationChanged(object sender, EventArgs e)
{
save();
}
private void save()
{
if( loading )
{
return;
}
app.SetValue("Key",keyText.Text, RegistryValueKind.String);
app.SetValue("Buttons",mouseText.Text, RegistryValueKind.String);
app.SetValue("Delay", (int)delay.Value, RegistryValueKind.DWord);
app.SetValue("CancelMouseEvents", cancelMouse.Checked ? 1 : 0, RegistryValueKind.DWord);
app.SetValue("ShowMicState", showMicState.Checked ? 1: 0, RegistryValueKind.DWord);
app.SetValue("Opacity", (int)opacity.Value, RegistryValueKind.DWord);
app.SetValue("MicStateX", (int)micState.Location.X, RegistryValueKind.DWord);
app.SetValue("MicStateY", (int)micState.Location.Y, RegistryValueKind.DWord);
app.SetValue("SettingsX", (int)Location.X, RegistryValueKind.DWord);
app.SetValue("SettingsY", (int)Location.Y, RegistryValueKind.DWord);
}
private T[] parse<T>( string value )
{
if( string.IsNullOrEmpty(value) || value == None)
{
return Enumerable.Empty<T>().ToArray();
}
return value.Split('+').Select(each => (T)Enum.Parse(typeof(T), each)).ToArray();
}
private void load()
{
loading = true;
keys.Clear();
foreach ( var each in parse<Keys>(app.GetValue("Key")?.ToString() ?? None) ) {
keys.Add(each);
}
keyText.Text =keys.Count == 0 ? None: keys.Select(each => each.ToString()).Aggregate((c, a) => $"{c}+{a}");
buttons.Clear();
foreach (var each in parse<MouseButtons>(app.GetValue("Buttons")?.ToString() ?? None))
{
buttons.Add(each);
}
mouseText.Text = buttons.Count == 0 ? None : buttons.Select(each => each.ToString()).Aggregate((c, a) => $"{c}+{a}");
delay.Value = Convert.ToInt32(app.GetValue("Delay", 250));
cancelMouse.Checked = app.GetValue("CancelMouseEvents", 1) == (object)1;
SetDesktopLocation(Convert.ToInt32(app.GetValue("SettingsX", 250)), Convert.ToInt32(app.GetValue("SettingsY", 250)));
micState.SetDesktopLocation(Convert.ToInt32(app.GetValue("MicStateX", Screen.PrimaryScreen.WorkingArea.Width - 300)), Convert.ToInt32(app.GetValue("MicStateY", 25)));
var o = Convert.ToInt32(app.GetValue("Opacity", 50));
micState.Opacity = ((float)o) / 100;
int sms = Convert.ToInt32(app.GetValue("ShowMicState", 1));
showMicState.Checked = sms != 0;
opacity.Value = o;
update();
if (keyText.Text == None && mouseText.Text == None)
{
Show();
}
else
{
Task.Delay(2).ContinueWith((p) => Invoke(new Action(() => Hide())));
Hide();
mouse = mouseText.Text == None ? State.None : State.Active;
key = keyText.Text == None ? State.None : State.Active;
}
loading = false;
}
private IAudioEndpointVolume volumeControl
{
get
{
IMMDeviceEnumerator deviceEnumerator = null;
IMMDevice device = null;
try
{
deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia, out device);
Guid IID_IAudioEndpointVolume = typeof(IAudioEndpointVolume).GUID;
object o;
device.Activate(ref IID_IAudioEndpointVolume, 0, IntPtr.Zero, out o);
IAudioEndpointVolume masterVol = (IAudioEndpointVolume)o;
return masterVol;
}
finally
{
if (device != null) {
Marshal.ReleaseComObject(device);
};
if (deviceEnumerator != null) {
Marshal.ReleaseComObject(deviceEnumerator);
}
}
}
}
private bool MicrophoneMuted {
get {
IAudioEndpointVolume masterVol = null;
try
{
bool isMuted;
masterVol = volumeControl;
if (masterVol == null)
{
return false;
}
masterVol.GetMute(out isMuted);
return isMuted;
}
finally
{
if (masterVol != null)
{
Marshal.ReleaseComObject(masterVol);
}
}
}
set {
IAudioEndpointVolume masterVol = null;
try
{
masterVol = volumeControl;
if (masterVol == null)
{
return;
}
masterVol.SetMute(value, Guid.Empty);
}
finally
{
if (masterVol != null)
Marshal.ReleaseComObject(masterVol);
}
}
}
private void CreateTrayIcon()
{
notifier = new System.Windows.Forms.NotifyIcon();
notifier.Icon = Properties.Resources.mic_off;
notifier.Visible = true;
var trayContext = new System.Windows.Forms.ContextMenuStrip();
var show = new System.Windows.Forms.ToolStripMenuItem("Settings");
show.Click += (x, y) =>
{
this.Show();
};
trayContext.Items.Add(show);
var exit = new System.Windows.Forms.ToolStripMenuItem("Exit");
exit.Click += (x, y) =>
{
quit.Set();
this.Close();
};
trayContext.Items.Add(exit);
notifier.MouseClick += Notifier_Click;
notifier.ContextMenuStrip = trayContext;
}
private void Notifier_Click(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if( this.Visible)
{
this.Hide();
} else
{
this.Show();
}
}
}
private void worker()
{
var a = new Action(activate);
var d = new Action(deactivate);
while (true)
{
ManualResetEvent.WaitAny(new WaitHandle[] { trigger, quit });
if(quit.WaitOne(0) )
{
return;
}
trigger.Reset();
if ((currentKeys.Count > 0 && currentKeys.SetEquals(keys)) || (currentButtons.Count > 0 && currentButtons.SetEquals(buttons)))
{
this.Invoke(a);
}
else
{
if (active)
{
Task.Delay((int)delay.Value).ContinueWith(p => { this.Invoke(d); });
}
}
}
}
private void PlaySound(UnmanagedMemoryStream sound)
{
using (var provider = new RawSourceWaveStream(sound, new WaveFormat()))
{
wave.Init(provider);
wave.Play();
}
}
private void activate()
{
if (!active)
{
PlaySound(Properties.Resources.on);
active = true;
notifier.Icon = Resources.mic_on;
MicrophoneMuted = false;
micState.on();
}
}
private void deactivate()
{
active = false;
notifier.Icon = Resources.mic_off;
PlaySound(Properties.Resources.off);
MicrophoneMuted = true;
micState.off();
}
private void keyDown(object sender, KeyEventArgs e)
{
switch (key)
{
case State.Active:
if (keys.Count > 0)
{
currentKeys.Add(e.KeyCode);
trigger.Set();
// for single-key hotkey, cancel then keypress (so pause, capslock, scroll-lock, etc work )
if (keys.Count == 0 && currentKeys.SetEquals(keys) )
{
e.Handled = true;
}
}
break;
case State.Query:
keys.Add(e.KeyCode);
e.Handled = true;
keyText.Text = keys.Select(each => each.ToString()).Aggregate((c,a)=> $"{c}+{a}" );
break;
}
}
private void keyUp(object sender, KeyEventArgs e)
{
switch (key)
{
case State.Active:
currentKeys.Remove(e.KeyCode);
trigger.Set();
break;
case State.Query:
key = State.Active;
save();
break;
}
}
private void mouseDown(object sender, MouseEventExtArgs e)
{
// skip button one and two
if( e.Button == MouseButtons.Left || e.Button == MouseButtons.Right )
{
return;
}
switch (mouse)
{
case State.Active:
currentButtons.Add(e.Button);
e.Handled = cancelMouse.Checked && buttons.Contains(e.Button);
trigger.Set();
break;
case State.Query:
buttons.Add(e.Button);
mouseText.Text = buttons.Select(each => each.ToString()).Aggregate((c, a) => $"{c}+{a}");
break;
}
}
private void mouseUp(object sender, MouseEventArgs e)
{
// skip button one and two
if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
{
return;
}
switch (mouse)
{
case State.Active:
currentButtons.Remove(e.Button);
trigger.Set();
break;
case State.Query:
mouse = State.Active;
save();
break;
}
}
private void setKey_Click(object sender, EventArgs e)
{
keyText.Text = "Press and hold a key combination";
key = State.Query;
keys.Clear();
currentKeys.Clear();
}
private void clearKey_Click(object sender, EventArgs e)
{
keyText.Text = None;
key = State.None;
keys.Clear();
currentKeys.Clear();
save();
}
private void setMouse_Click(object sender, EventArgs e)
{
mouseText.Text = "Press a mouse button combination";
mouse = State.Query;
buttons.Clear();
currentButtons.Clear();
}
private void clearMouse_Click(object sender, EventArgs e)
{
mouseText.Text = None;
mouse = State.None;
buttons.Clear();
currentButtons.Clear();
save();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// quit only when really quitting
e.Cancel = !quit.WaitOne(0);
if( e.Cancel)
{
// hide the form then
this.Hide();
}
}
private void delay_ValueChanged(object sender, EventArgs e)
{
save();
}
private void cancelMouse_CheckedChanged(object sender, EventArgs e)
{
save();
}
private void update()
{
if (showMicState.Checked)
{
micState.Show();
micState.SetDesktopLocation(Convert.ToInt32(app.GetValue("MicStateX", Screen.PrimaryScreen.WorkingArea.Width - 300)), Convert.ToInt32(app.GetValue("MicStateY", 25)));
}
else
{
micState.Hide();
}
micState.Opacity = ((double)opacity.Value) / 100;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
update();
save();
}
private void opacity_ValueChanged(object sender, EventArgs e)
{
update();
save();
}
private void button1_Click(object sender, EventArgs e)
{
load();
}
}
}