-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMainWindow.xaml.cs
executable file
·283 lines (268 loc) · 13.7 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using TCD.System.ApplicationExtensions;
using TCD.System.TouchInjection;
using TCD.Sys.TUIO;
using System.Drawing;
using TUIO;
namespace TouchInjector
{
public partial class MainWindow : Window
{
//TODO: in the app.manifest (in the Properties folder), the uiaccess-variable is set to true. In theory this should be the basis to the ability to send touch events
//to applications running under the secure desktop (such as the Universal Access Control aka. UAC). According to an MSDN article, UIAccess-applications need to be certified
//however, I was unable to get it working using a self-signed (and self-trusted) certificate.
//If anyone succeeds in enabling UIAccess, please contact Michael Osthege via [email protected]
#region Backend Variables
TuioChannel channel;
private uint maxTouchPoints = 256;
private uint frameCount = 0;
private const int checkScreenEvery = 100;
Rectangle targetArea;
#endregion
#region UI Variables
private NotifyIcon trayIcon;
private bool closeIt;
#endregion
#region Lifecycle
public MainWindow()
{
InitializeComponent();
channel = new TuioChannel();
TuioChannel.OnTuioRefresh += TuioChannel_OnTuioRefresh;
InitTrayIcon();
SetStatus(TouchInjectorStatus.Initializing);
}
public async void Initialize()
{
ScanScreens();
feedbackCheckBox.IsChecked = Properties.Settings.Default.TouchFeedback;
portBox.Text = Properties.Settings.Default.ListeningPort.ToString();
bool touch = await InitTouch();
bool tuio = await Connect();
touchInjectionStatus.Content = (touch) ? Properties.Resources.TouchReady : Properties.Resources.TouchError;
SetStatus((touch && tuio) ? TouchInjectorStatus.Ready : TouchInjectorStatus.Error);
autostartCheckBox.IsChecked = await ApplicationAutostart.IsAutostartAsync("TouchInjector");
autostartCheckBox.Checked += CheckBox_Changed;
autostartCheckBox.Unchecked += CheckBox_Changed;
}
private void MainWindow_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Minimized)
this.Hide();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if (this.WindowState == WindowState.Normal && !closeIt)//the user clicked the '[X]' control
{
e.Cancel = true;//only close via the tray context menu
this.Hide();//minimize to tray
}
else
{
trayIcon.Visible = false;//delete tray icon
channel.Disconnect();//disconnect tuio
base.OnClosing(e);
}
}
#endregion
#region Initialization
private void InitTrayIcon()
{
trayIcon = new NotifyIcon();
trayIcon.Click += new EventHandler(delegate
{
this.Show();
this.Activate();
});
System.Windows.Forms.MenuItem quitItem = new System.Windows.Forms.MenuItem(Properties.Resources.TrayQuit, new EventHandler(delegate
{
closeIt = true;
this.Close();
})) { Shortcut = Shortcut.CtrlQ, ShowShortcut = false };
System.Windows.Forms.MenuItem feedbackItem = new System.Windows.Forms.MenuItem(Properties.Resources.TrayFeedback, new EventHandler(delegate
{
System.Diagnostics.Process.Start("mailto:" + Properties.Resources.DeveloperEmail + "?subject=TouchInjector Feedback");
})) { Shortcut = Shortcut.CtrlF, ShowShortcut = false };
System.Windows.Forms.MenuItem helpItem = new System.Windows.Forms.MenuItem(Properties.Resources.TrayHelp, new EventHandler(delegate
{
Version v = Assembly.GetExecutingAssembly().GetName().Version;
System.Windows.MessageBox.Show(string.Format("Version: {0}\n", v) + Properties.Resources.HelpText, Properties.Resources.TrayHelp, MessageBoxButton.OK);
})) { Shortcut = Shortcut.CtrlH, ShowShortcut = false };
trayIcon.ContextMenu = new System.Windows.Forms.ContextMenu(new System.Windows.Forms.MenuItem[] { helpItem, feedbackItem, quitItem });
trayIcon.Visible = true;
}
private async Task<bool> Connect()
{
await Task.Delay(0);
portBox.IsEnabled = false;//lock the port box
if (string.IsNullOrWhiteSpace(portBox.Text)) portBox.Text = "3333";
Properties.Settings.Default.ListeningPort = Convert.ToInt32(portBox.Text);
Properties.Settings.Default.Save();
return channel.Connect(Properties.Settings.Default.ListeningPort);
}
private async Task<bool> InitTouch()
{
await Task.Delay(0);
feedbackCheckBox.IsEnabled = false;//lock the feedback box
Boolean showFeedback = Convert.ToBoolean(feedbackCheckBox.IsChecked);
return TCD.System.TouchInjection.TouchInjector.InitializeTouchInjection((uint)maxTouchPoints, showFeedback ? TouchFeedback.INDIRECT : TouchFeedback.NONE);
}
#endregion
#region UI/Management
//Refreshing
private void SetStatus(TouchInjectorStatus status)
{
tuioStatus.Content = (channel.IsRunning) ? Properties.Resources.TouchReady : (status == TouchInjectorStatus.Deactivated) ? Properties.Resources.TuioDeactivated : (status == TouchInjectorStatus.Initializing) ? Properties.Resources.TuioInitializing : Properties.Resources.TuioError;
startStopButton.IsEnabled = (status != TouchInjectorStatus.Initializing);
portBox.IsEnabled = !channel.IsRunning;
feedbackCheckBox.IsEnabled = !channel.IsRunning;
trayIcon.Text = "TouchInjector - " + status;
switch (status)
{
case TouchInjectorStatus.Deactivated:
this.Icon = BitmapFrame.Create(new Uri("pack://application:,,,/Resources/iconGray.ico"));
trayIcon.Icon = Properties.Resources.iconGray;
startStopButton.Content = Properties.Resources.ControlStart;
break;
case TouchInjectorStatus.Error:
this.Icon = BitmapFrame.Create(new Uri("pack://application:,,,/Resources/iconRed.ico"));
trayIcon.Icon = Properties.Resources.iconRed;
startStopButton.Content = Properties.Resources.ControlRetry;
break;
case TouchInjectorStatus.Initializing:
this.Icon = BitmapFrame.Create(new Uri("pack://application:,,,/Resources/iconYellow.ico"));
trayIcon.Icon = Properties.Resources.iconYellow;
startStopButton.Content = Properties.Resources.ControlStart;
break;
case TouchInjectorStatus.Ready:
this.Icon = BitmapFrame.Create(new Uri("pack://application:,,,/Resources/iconGreen.ico"));
trayIcon.Icon = Properties.Resources.iconGreen;
startStopButton.Content = Properties.Resources.ControlStop;
break;
}
}
//Start/Stop
private async void StartStop_Click(object sender, RoutedEventArgs e)
{
bool error = false;
if (channel.IsRunning)
channel.Disconnect();
else
{
error = !await InitTouch();
error = !await Connect();
}
SetStatus((channel.IsRunning) ? TouchInjectorStatus.Ready : (error) ? TouchInjectorStatus.Error : TouchInjectorStatus.Deactivated);
}
//Autostart
private async void CheckBox_Changed(object sender, RoutedEventArgs e)
{
autostartCheckBox.Checked -= CheckBox_Changed;
autostartCheckBox.Unchecked -= CheckBox_Changed;
//
bool succ = await ApplicationAutostart.SetAutostartAsync((bool)autostartCheckBox.IsChecked, "TouchInjector", Properties.Resources.AutostartTaskDescription, "-minimized", true);
autostartCheckBox.IsChecked = (autostartCheckBox.IsChecked == succ);
//
autostartCheckBox.Checked += CheckBox_Changed;
autostartCheckBox.Unchecked += CheckBox_Changed;
}
#endregion
#region Screens, TUIO and Touch
private void screenSelector_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
targetArea = ((screenSelector.SelectedItem as System.Windows.Controls.ComboBoxItem).Tag as Screen).Bounds;
}
private void ScanScreens()
{
System.Windows.Controls.ComboBoxItem autoItem = new System.Windows.Controls.ComboBoxItem() { Content = "Primary" };
foreach (MonitorInfo mi in MonitorInfo.GetAllMonitors())
{
screenSelector.Items.Add(new System.Windows.Controls.ComboBoxItem() { Content = mi.FriendlyName, Tag = mi.Screen });
if (mi.Screen.Primary)
autoItem.Tag = mi.Screen;
}
screenSelector.Items.Insert(0, autoItem);
screenSelector.SelectedIndex = 0;
//TODO: Inject to all screens
//screenSelector.Items.Add(new System.Windows.Controls.ComboBoxItem() { Content = "All" });
}
private void TuioChannel_OnTuioRefresh(TuioTime t)
{
//TODO: re-enable frequent screen monitoring
//if (frameCount % checkScreenEvery == 0)
// ScanScreens();
//loop through the TuioObjects
List<PointerTouchInfo> toFire = new List<PointerTouchInfo>();
List<long> removeList = new List<long>();
foreach (var kvp in channel.CursorList)
{
TuioCursor cur = kvp.Value.TuioCursor;
IncomingType type = kvp.Value.Type;
int[] injectionCoordinates = ToInjectionCoordinates(cur.X, cur.Y);
int radius = 12;
//make a new pointertouchinfo with all neccessary information
PointerTouchInfo contact = new PointerTouchInfo();
contact.PointerInfo.pointerType = PointerInputType.TOUCH;
contact.TouchFlags = TouchFlags.NONE;
//contact.Orientation = (uint)cur.getAngleDegrees();//this is only valid for TuioObjects
contact.Pressure = 1024;
contact.TouchMasks = TouchMask.CONTACTAREA | TouchMask.ORIENTATION | TouchMask.PRESSURE;
contact.PointerInfo.PtPixelLocation.X = injectionCoordinates[0];
contact.PointerInfo.PtPixelLocation.Y = injectionCoordinates[1];
contact.PointerInfo.PointerId = (uint)cur.CursorID;
contact.ContactArea.left = injectionCoordinates[0] - radius;
contact.ContactArea.right = injectionCoordinates[0] + radius;
contact.ContactArea.top = injectionCoordinates[1] - radius;
contact.ContactArea.bottom = injectionCoordinates[1] + radius;
//contact.PointerInfo.FrameId = frameCount;
//set the right flags
if (type == IncomingType.New)
{
contact.PointerInfo.PointerFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
kvp.Value.Type = IncomingType.Update;
}
else if (type == IncomingType.Update)
contact.PointerInfo.PointerFlags = PointerFlags.UPDATE | PointerFlags.INRANGE | PointerFlags.INCONTACT;
else if (type == IncomingType.Remove)
{
contact.PointerInfo.PointerFlags = PointerFlags.UP;
removeList.Add(kvp.Key);
}
//add it to 'toFire'
toFire.Add(contact);
}
//fire the events
bool success = TCD.System.TouchInjection.TouchInjector.InjectTouchInput(toFire.Count, toFire.ToArray());
//remove those with type == IncomingType.Remove
foreach (long key in removeList)
channel.CursorList.Remove(key);//remove from the tuio channel
//count up
frameCount++;
}
private int[] ToInjectionCoordinates(float x, float y)
{
int[] result = new int[2];
result[0] = targetArea.X + (int)Math.Round(x * targetArea.Width);
result[1] = targetArea.Y + (int)Math.Round(y * targetArea.Height);
return result;
}
#endregion
private void feedbackCheckBox_Changed(object sender, RoutedEventArgs e)
{
Boolean showFeedback = Convert.ToBoolean(feedbackCheckBox.IsChecked);
Properties.Settings.Default.TouchFeedback = showFeedback;
Properties.Settings.Default.Save();
}
}
enum TouchInjectorStatus
{
Initializing, Ready, Deactivated, Error
}
}