forked from Ben-Barron/PS3BluMote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsForm.cs
438 lines (355 loc) · 16.8 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
/*
Copyright (c) 2011 Ben Barron
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Principal;
using System.Windows.Forms;
using System.Xml;
using WindowsAPI;
namespace PS3BluMote
{
public partial class SettingsForm : Form
{
private readonly String SETTINGS_DIRECTORY = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) + "\\PS3BluMote\\";
private readonly String SETTINGS_FILE = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) + "\\PS3BluMote\\settings.ini";
private const String SETTINGS_VERSION = "2.0";
private ButtonMapping[] buttonMappings = new ButtonMapping[56];
private PS3Remote remote = null;
private SendInputAPI.Keyboard keyboard = null;
private System.Timers.Timer timerRepeat = null;
public SettingsForm()
{
for (int i = 0; i < buttonMappings.Length; i++)
{
buttonMappings[i] = new ButtonMapping();
}
InitializeComponent();
ListViewItem lvItem;
foreach (PS3Remote.Button button in Enum.GetValues(typeof(PS3Remote.Button)))
{
lvItem = new ListViewItem();
lvItem.SubItems.Add(button.ToString());
lvItem.SubItems.Add("");
lvButtons.Items.Add(lvItem);
}
foreach (SendInputAPI.Keyboard.KeyCode key in Enum.GetValues(typeof(SendInputAPI.Keyboard.KeyCode)))
{
lvKeys.Items.Add(new ListViewItem(key.ToString()));
}
if (!loadSettings())
{
saveSettings();
}
timerRepeat = new System.Timers.Timer();
timerRepeat.Interval = int.Parse(txtRepeatInterval.Text);
timerRepeat.Elapsed += new System.Timers.ElapsedEventHandler(timerRepeat_Elapsed);
try
{
remote = new PS3Remote(int.Parse(txtVendorId.Text.Remove(0, 2), System.Globalization.NumberStyles.HexNumber), int.Parse(txtProductId.Text.Remove(0, 2), System.Globalization.NumberStyles.HexNumber), cbHibernation.Checked);
remote.BatteryLifeChanged += new EventHandler<EventArgs>(remote_BatteryLifeChanged);
remote.ButtonDown += new EventHandler<PS3Remote.ButtonData>(remote_ButtonDown);
remote.ButtonReleased += new EventHandler<PS3Remote.ButtonData>(remote_ButtonReleased);
remote.Connected += new EventHandler<EventArgs>(remote_Connected);
remote.Disconnected += new EventHandler<EventArgs>(remote_Disconnected);
remote.connect();
}
catch
{
MessageBox.Show("An error occured whilst attempting to load the remote.", "PS3BluMote: Remote error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
keyboard = new SendInputAPI.Keyboard(cbSms.Checked);
}
private void cbDebugMode_CheckedChanged(object sender, EventArgs e)
{
DebugLog.isLogging = cbDebugMode.Checked;
}
private void cbHibernation_CheckedChanged(object sender, EventArgs e)
{
remote.hibernationEnabled = cbHibernation.Checked;
}
private void cbSms_CheckedChanged(object sender, EventArgs e)
{
if (keyboard != null) keyboard.isSmsEnabled = cbSms.Checked;
}
private void llblOpenFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = "explorer.exe";
prc.StartInfo.Arguments = SETTINGS_DIRECTORY;
prc.Start();
}
private bool loadSettings()
{
String errorMessage;
if (File.Exists(SETTINGS_FILE))
{
XmlDocument rssDoc = new XmlDocument();
try
{
rssDoc.Load(SETTINGS_FILE);
XmlNode rssNode = rssDoc.SelectSingleNode("PS3BluMote");
if (rssNode.Attributes["version"].InnerText == SETTINGS_VERSION)
{
cbSms.Checked = (rssNode.SelectSingleNode("settings/smsinput").InnerText.ToLower() == "true") ? true : false;
cbHibernation.Checked = (rssNode.SelectSingleNode("settings/hibernation").InnerText.ToLower() == "true") ? true : false;
txtVendorId.Text = rssNode.SelectSingleNode("settings/vendorid").InnerText;
txtProductId.Text = rssNode.SelectSingleNode("settings/productid").InnerText;
try
{
txtRepeatInterval.Text = rssNode.SelectSingleNode("settings/repeatinterval").InnerText;
}
catch
{ }
if (cbHibernation.Checked &&
!(new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator)))
{
MessageBox.Show("Admin/UAC elevated rights are required to use the hibernation feature! Please enable them!", "PS3BluMote: No admin rights found!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
foreach (XmlNode buttonNode in rssNode.SelectNodes("mappings/button"))
{
int index = (int)Enum.Parse(typeof(PS3Remote.Button), buttonNode.Attributes["name"].InnerText, true);
buttonMappings[index].repeat = (buttonNode.Attributes["repeat"].InnerText.ToLower() == "true") ? true : false;
lvButtons.Items[index].Checked = buttonMappings[index].repeat;
List<SendInputAPI.Keyboard.KeyCode> mappedKeys = buttonMappings[index].keysMapped;
if (buttonNode.InnerText.Length > 0)
{
foreach (string keyCode in buttonNode.InnerText.Split(','))
{
mappedKeys.Add((SendInputAPI.Keyboard.KeyCode)Enum.Parse(typeof(SendInputAPI.Keyboard.KeyCode), keyCode, true));
}
lvButtons.Items[index].SubItems[2].Text = buttonNode.InnerText.Replace(",", " + ");
}
}
return true;
}
errorMessage = "Incorrect settings file version.";
}
catch
{
errorMessage = "An error occured whilst attempting to load settings.";
}
}
else
{
errorMessage = "Unable to locate the settings file.";
}
MessageBox.Show(errorMessage + " A fresh settings file has been created.", "PS3BluMote: Settings load error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
private void lvButtons_ItemChecked(object sender, ItemCheckedEventArgs e)
{
buttonMappings[e.Item.Index].repeat = e.Item.Checked;
}
private void lvButtons_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvButtons.SelectedItems.Count == 0) return;
lvButtons.Tag = true;
int index = (int)Enum.Parse(typeof(PS3Remote.Button), lvButtons.SelectedItems[0].SubItems[1].Text, true);
List<SendInputAPI.Keyboard.KeyCode> mappedKeys = buttonMappings[index].keysMapped;
foreach (ListViewItem lvItem in lvKeys.Items)
{
if (mappedKeys.Contains((SendInputAPI.Keyboard.KeyCode)Enum.Parse(typeof(SendInputAPI.Keyboard.KeyCode), lvItem.Text, true)))
{
lvItem.Checked = true;
}
else
{
lvItem.Checked = false;
}
}
lvButtons.Tag = false;
}
private void lvKeys_ItemCheck(object sender, ItemCheckEventArgs e)
{
if ((bool)lvButtons.Tag) return;
int index = (int)Enum.Parse(typeof(PS3Remote.Button), lvButtons.SelectedItems[0].SubItems[1].Text, true);
List<SendInputAPI.Keyboard.KeyCode> mappedKeys = buttonMappings[index].keysMapped;
SendInputAPI.Keyboard.KeyCode code = (SendInputAPI.Keyboard.KeyCode)Enum.Parse(typeof(SendInputAPI.Keyboard.KeyCode), lvKeys.Items[e.Index].Text, true);
if (e.NewValue == CheckState.Checked && !mappedKeys.Contains(code))
{
mappedKeys.Add(code);
}
else
{
mappedKeys.Remove(code);
}
String text = "";
foreach (SendInputAPI.Keyboard.KeyCode key in mappedKeys)
{
text += key.ToString() + " + ";
}
lvButtons.SelectedItems[0].SubItems[2].Text = (mappedKeys.Count > 0) ? text.Substring(0, text.Length - 3) : "";
}
private void menuNotifyIcon_ItemClick(object sender, EventArgs e)
{
if (sender.Equals(mitemSettings))
{
this.Show();
}
else if (sender.Equals(mitemExit))
{
if (DebugLog.isLogging) DebugLog.outputToFile(SETTINGS_DIRECTORY + "log.txt");
Application.Exit();
}
}
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
this.Show();
}
private void remote_BatteryLifeChanged(object sender, EventArgs e)
{
notifyIcon.Text = "PS3BluMote: Connected + (Battery: " + remote.getBatteryLife.ToString() + "%).";
if (DebugLog.isLogging) DebugLog.write("Battery life: " + remote.getBatteryLife.ToString() + "%");
}
private void remote_ButtonDown(object sender, PS3Remote.ButtonData e)
{
if (DebugLog.isLogging) DebugLog.write("Button down: " + e.button.ToString());
ButtonMapping mapping = buttonMappings[(int)e.button];
if (mapping.repeat)
{
keyboard.sendKeysDown(mapping.keysMapped);
keyboard.releaseLastKeys();
if (DebugLog.isLogging) DebugLog.write("Keys repeat send on : { " + String.Join(",", mapping.keysMapped.ToArray()) + " }");
timerRepeat.Enabled = true;
return;
}
keyboard.sendKeysDown(mapping.keysMapped);
if (DebugLog.isLogging) DebugLog.write("Keys down: { " + String.Join(",", mapping.keysMapped.ToArray()) + " }");
}
private void remote_ButtonReleased(object sender, PS3Remote.ButtonData e)
{
if (DebugLog.isLogging) DebugLog.write("Button released: " + e.button.ToString());
if (timerRepeat.Enabled)
{
if (DebugLog.isLogging) DebugLog.write("Keys repeat send off: { " + String.Join(",", keyboard.lastKeysDown.ToArray()) + " }");
timerRepeat.Enabled = false;
return;
}
if (DebugLog.isLogging) DebugLog.write("Keys up: { " + String.Join(",", keyboard.lastKeysDown.ToArray()) + " }");
keyboard.releaseLastKeys();
}
private void remote_Connected(object sender, EventArgs e)
{
if (DebugLog.isLogging) DebugLog.write("Remote connected");
notifyIcon.Text = "PS3BluMote: Connected + (Battery: " + remote.getBatteryLife.ToString() + "%).";
notifyIcon.Icon = Properties.Resources.Icon_Connected;
}
private void remote_Disconnected(object sender, EventArgs e)
{
if (DebugLog.isLogging) DebugLog.write("Remote disconnected");
notifyIcon.Text = "PS3BluMote: Disconnected.";
notifyIcon.Icon = Properties.Resources.Icon_Disconnected;
}
private bool saveSettings()
{
string text = "<PS3BluMote version=\"" + SETTINGS_VERSION + "\">\r\n";
text += "\t<settings>\r\n";
text += "\t\t<vendorid>" + txtVendorId.Text.ToLower() + "</vendorid>\r\n";
text += "\t\t<productid>" + txtProductId.Text.ToLower() + "</productid>\r\n";
text += "\t\t<smsinput>" + cbSms.Checked.ToString().ToLower() + "</smsinput>\r\n";
text += "\t\t<hibernation>" + cbHibernation.Checked.ToString().ToLower() + "</hibernation>\r\n";
text += "\t\t<repeatinterval>" + txtRepeatInterval.Text + "</repeatinterval>\r\n";
text += "\t</settings>\r\n";
text += "\t<mappings>\r\n";
for (int i = 0; i < buttonMappings.Length; i++)
{
text += "\t\t<button name=\"" + ((PS3Remote.Button)i).ToString() + "\" repeat=\""
+ buttonMappings[i].repeat.ToString().ToLower() + "\">"
+ String.Join(",", buttonMappings[i].keysMapped.ToArray()) + "</button>\r\n";
}
text += "\t</mappings>\r\n";
text += "</PS3BluMote>";
try
{
if (!Directory.Exists(SETTINGS_DIRECTORY))
{
Directory.CreateDirectory(SETTINGS_DIRECTORY);
}
TextWriter tw = new StreamWriter(SETTINGS_FILE, false);
tw.WriteLine(text);
tw.Close();
return true;
}
catch
{
MessageBox.Show("An error occured whilst attempting to save settings.", "PS3BluMote: Saving settings error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return false;
}
private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
{
saveSettings();
if (e.CloseReason != CloseReason.UserClosing) return;
e.Cancel = true;
this.Hide();
}
private void SettingsForm_Shown(object sender, EventArgs e)
{
lvButtons.Items[0].Selected = true;
}
private void timerRepeat_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
keyboard.sendKeysDown(keyboard.lastKeysDown);
keyboard.releaseLastKeys();
}
private void txtProductId_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
int i = int.Parse(txtProductId.Text.Remove(0, 2), System.Globalization.NumberStyles.HexNumber);
}
catch
{
e.Cancel = true;
}
}
private void txtRepeatInterval_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
int i = int.Parse(txtRepeatInterval.Text);
}
catch
{
e.Cancel = true;
}
}
private void txtVendorId_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
int i = int.Parse(txtVendorId.Text.Remove(0, 2), System.Globalization.NumberStyles.HexNumber);
}
catch
{
e.Cancel = true;
}
}
private class ButtonMapping
{
public List<SendInputAPI.Keyboard.KeyCode> keysMapped;
public bool repeat;
public ButtonMapping()
{
keysMapped = new List<SendInputAPI.Keyboard.KeyCode>();
repeat = false;
}
}
}
}