-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
221 lines (209 loc) · 11.1 KB
/
MainWindow.xaml.cs
File metadata and controls
221 lines (209 loc) · 11.1 KB
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
using Microsoft.UI; // NuGet -> Microsoft.Windows.SDK.BuildTools & Microsoft.WindowsAppSDK
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using SharpDX.DirectInput; // NuGet -> SharpDX.DirectInput 4.2.0
using Gma.System.MouseKeyHook; // Nuget -> MouseKeyHook 5.7.1
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Devices.Enumeration;
using System.Diagnostics;
namespace Control3
{
public sealed partial class MainWindow : Window
{
public static TextBlock MessageControl { get; set; } // Object to be able to set a message from other windows
private Remote remoteInstance = null; // Create object for remote window so we can open/close it from the main window
public static Mouse mouse; // Used for the mouse movement on system level (SharpDX)
private IKeyboardMouseEvents globalHook; // Used for all the mouse and keyboard event capture
private CH9329 MyCH9329; // The object which sends the data to us CH9329
List<VideoSourceInfo> videoSourceList = new List<VideoSourceInfo>(); // Used to fill the combobox and obtain the ID to open the remote window
public MainWindow()
{
// Initialize Window
this.AppWindow.MoveAndResize(new Windows.Graphics.RectInt32(200, 100, 600, 350));
this.AppWindow.SetIcon(@"Assets\control.ico");
this.InitializeComponent();
this.Closed += OnWindowClosed;
PopulateComboBox();
MainWindow.MessageControl = message;
// SharpDX Mouse initialization to get raw mouse movement
var directInput = new DirectInput();
var firstMouseInstance = directInput.GetDevices(DeviceType.Mouse, DeviceEnumerationFlags.AllDevices).FirstOrDefault();
mouse = new Mouse(directInput);
mouse.Properties.AxisMode = DeviceAxisMode.Relative;
mouse.Acquire();
// Initialize CH3929 and Mouse hooks
if (SetPort())
{
MyCH9329 = new CH9329(App.Flag.COMPort);
globalHook = Hook.GlobalEvents();
globalHook.KeyDown += GlobalHook_KeyDown;
globalHook.KeyUp += GlobalHook_KeyUp;
globalHook.MouseMove += GlobalHook_MouseMove;
globalHook.MouseDown += GlobalHook_MouseDown;
globalHook.MouseUp += GlobalHook_MouseUp;
globalHook.MouseWheel += GlobalHook_MouseWheel;
}
}
private async void PopulateComboBox()
{
string vSource = SettingsManager.GetSetting();
Debug.WriteLine("From registry: "+vSource);
var devices = await DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);
if (devices.Any())
{
Int32 i = 0;
Int32 si = 0;
foreach (var device in devices)
{
string name = device.Name.ToString();
string id = device.Id;
VideoSourceInfo videoSourceInfo = new VideoSourceInfo { Name = name, Id = id };
videoSourceList.Add(videoSourceInfo);
if (id == vSource) si = i;
i++;
}
videoSource.ItemsSource = videoSourceList;
videoSource.SelectedIndex = si;
}
}
private void sourceSelected(object sender, SelectionChangedEventArgs e)
{
if (videoSource.SelectedItem != null)
{
VideoSourceInfo selectedVideoSource = (VideoSourceInfo)videoSource.SelectedItem;
App.Flag.selectedSource = selectedVideoSource.Id;
SettingsManager.SaveSetting(selectedVideoSource.Id);
}
}
private void NoVideo_Click(object sender, RoutedEventArgs e)
{
if (App.Flag.COMPort != null)
{
MouseUtility.SetMouseCursorAside();
mouse.GetCurrentState();
App.Flag.Decoration = 0;
App.Flag.isRemote = true;
SetMessage("Remote Session Active", Colors.Blue);
} else SetMessage("No KVM cable present", Colors.Red);
}
private void Video_Click(object sender, RoutedEventArgs e)
{
if (App.Flag.selectedSource != null)
{
if (App.Flag.COMPort!= null)
{
App.Flag.Decoration = 0;
App.Flag.isRemote = true;
App.Flag.isFullScreen = (sender as FrameworkElement)?.Tag?.ToString() == "True" || false; // Fullscreen button clicked?
if (remoteInstance == null || remoteInstance.IsClosed) { remoteInstance = new Remote(); }
remoteInstance.Activate();
remoteInstance.SetFullScreen(App.Flag.isFullScreen);
MouseUtility.SetMouseCursorAside();
mouse.GetCurrentState();
SetMessage("Remote Session Active", Colors.Blue);
} else SetMessage("No KVM cable present", Colors.Red);
} else SetMessage("No Videosource available", Colors.Red);
}
private void GlobalHook_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (!App.Flag.isRemote) { return; }
if (e.KeyCode == System.Windows.Forms.Keys.RWin) { App.Flag.Decoration = (byte)(App.Flag.Decoration | (1 << 7)); }
else if (e.KeyCode == System.Windows.Forms.Keys.RMenu) { App.Flag.Decoration = (byte)(App.Flag.Decoration | (1 << 6)); }
else if (e.KeyCode == System.Windows.Forms.Keys.RShiftKey) { App.Flag.Decoration = (byte)(App.Flag.Decoration | (1 << 5)); }
else if (e.KeyCode == System.Windows.Forms.Keys.RControlKey) { App.Flag.Decoration = (byte)(App.Flag.Decoration | (1 << 4)); }
else if (e.KeyCode == System.Windows.Forms.Keys.LWin) { App.Flag.Decoration = (byte)(App.Flag.Decoration | (1 << 3)); }
else if (e.KeyCode == System.Windows.Forms.Keys.LMenu) { App.Flag.Decoration = (byte)(App.Flag.Decoration | (1 << 2)); }
else if (e.KeyCode == System.Windows.Forms.Keys.LShiftKey) { App.Flag.Decoration = (byte)(App.Flag.Decoration | (1 << 1)); ; }
else if (e.KeyCode == System.Windows.Forms.Keys.LControlKey) { App.Flag.Decoration = (byte)(App.Flag.Decoration | (1 << 0)); }
try
{
byte value = Flags.KeyMap[(byte)e.KeyValue];
MyCH9329.charKeyType(App.Flag.Decoration, value);
}
catch (Exception ex) { if (ex != null) { } }
e.Handled = true;
}
private static void GlobalHook_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (!App.Flag.isRemote) { return; }
if (e.KeyCode == System.Windows.Forms.Keys.RWin) { App.Flag.Decoration = (byte)(App.Flag.Decoration & ~(1 << 7)); }
else if (e.KeyCode == System.Windows.Forms.Keys.RMenu) { App.Flag.Decoration = (byte)(App.Flag.Decoration & ~(1 << 6)); }
else if (e.KeyCode == System.Windows.Forms.Keys.RShiftKey) { App.Flag.Decoration = (byte)(App.Flag.Decoration & ~(1 << 5)); }
else if (e.KeyCode == System.Windows.Forms.Keys.RControlKey) { App.Flag.Decoration = (byte)(App.Flag.Decoration & ~(1 << 4)); }
else if (e.KeyCode == System.Windows.Forms.Keys.LWin) { App.Flag.Decoration = (byte)(App.Flag.Decoration & ~(1 << 3)); }
else if (e.KeyCode == System.Windows.Forms.Keys.LMenu) { App.Flag.Decoration = (byte)(App.Flag.Decoration & ~(1 << 2)); }
else if (e.KeyCode == System.Windows.Forms.Keys.LShiftKey) { App.Flag.Decoration = (byte)(App.Flag.Decoration & ~(1 << 1)); }
else if (e.KeyCode == System.Windows.Forms.Keys.LControlKey) { App.Flag.Decoration = (byte)(App.Flag.Decoration & ~(1 << 0)); }
e.Handled = true;
}
private void GlobalHook_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (!App.Flag.isRemote) { return; }
MyCH9329.mouseScroll(e.Delta < 0 ? -1 : 1);
((MouseEventExtArgs)e).Handled = true;
}
private void GlobalHook_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (!App.Flag.isRemote) { return; }
MouseUtility.SetMouseCursorAside();
if (!App.Flag.isMoving)
{
App.Flag.isMoving = true; // In CH9329 the flag is set to false again after the package is sent to remote. Prevents queue of movement on remote
var mouseState = mouse.GetCurrentState();
int X = mouseState.X; int Y = mouseState.Y;
MyCH9329.mouseMoveRel(X, Y);
}
((MouseEventExtArgs)e).Handled = true;
}
private void GlobalHook_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (!App.Flag.isRemote) { return; }
if (e.Button == System.Windows.Forms.MouseButtons.Left) { MyCH9329.mouseButtonDown(CH9329.MouseButtonCode.LEFT); }
else if (e.Button == System.Windows.Forms.MouseButtons.Right) { MyCH9329.mouseButtonDown(CH9329.MouseButtonCode.RIGHT); }
else if (e.Button == System.Windows.Forms.MouseButtons.Middle)
{
if (App.Flag.isFullScreen) { remoteInstance.SetFullScreen(false); } else
{
App.Flag.isRemote = false;
SetMessage("", Colors.Blue);
}
}
((MouseEventExtArgs)e).Handled = true;
}
private void GlobalHook_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (!App.Flag.isRemote) { return; }
MyCH9329.mouseButtonUpAll();
((MouseEventExtArgs)e).Handled = true;
}
public void OnWindowClosed(object sender, WindowEventArgs e)
{
if (remoteInstance != null && !remoteInstance.IsClosed) { remoteInstance.Close(); }
}
public static void SetMessage(string line, object color)
{
if (MessageControl != null)
{
MessageControl.Text = line;
MessageControl.Foreground = new SolidColorBrush((Windows.UI.Color)color);
}
}
public bool SetPort()
{
Dictionary<string, string> serialPorts = SerialPortUtility.GetSerialPorts();
foreach (var port in serialPorts)
{
if (port.Value.Contains("VID_1A86&PID_7523"))
{
App.Flag.COMPort = port.Key;
return true;
}
}
SetMessage("No KVM cable present", Colors.Red);
return false;
}
}
}