-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_3DconnexionDevice.cs
290 lines (247 loc) · 8.08 KB
/
_3DconnexionDevice.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using _3Dconnexion;
using System.Threading;
using System.Runtime.InteropServices;
namespace _3DconnexionDriver
{
public class _3DconnexionDevice : IDisposable
{
IntPtr _deviceHandle = IntPtr.Zero;
int _leds = -1;
/// <summary>
/// Event when Device is at 0 Point
/// </summary>
public event EventHandler ZeroPoint;
/// <summary>
/// Event when Device is moved
/// </summary>
public event EventHandler<MotionEventArgs> Motion;
/// <summary>
/// Event when Device changes. Doesn't work yet
/// </summary>
//public event EventHandler<DeviceChangeEventArgs> DeviceChange;
private readonly object _locker = new object();
private bool _triggerZeroPoint;
private MotionEventArgs _triggerMotionEvent;
public _3DconnexionDevice(string appName)
{
this.AppName = appName;
var eventThread = new System.Threading.Thread(EventThreadLoop);
eventThread.IsBackground = true;
eventThread.Name = "3Dconnexion-Event-Dispatcher";
eventThread.Start();
}
~_3DconnexionDevice()
{
Dispose();
}
/// <summary>
/// Device is available (initialized)
/// </summary>
public bool IsAvailable
{
get { return _deviceHandle != IntPtr.Zero; }
}
/// <summary>
/// Device is disposed
/// </summary>
public bool IsDisposed
{
get;
private set;
}
/// <summary>
/// The name of the app using this device
/// </summary>
public string AppName
{
get;
private set;
}
/// <summary>
/// The Name of the Device (e.g. Space Pilot)
/// </summary>
public string DeviceName
{
get;
private set;
}
/// <summary>
/// The DeviceID of the device
/// </summary>
public int DeviceID
{
get;
private set;
}
/// <summary>
/// The Firmware version of the device
/// </summary>
public string FirmwareVersion
{
get;
private set;
}
/// <summary>
/// Bitwise Flags for the LEDs of the Device
/// </summary>
public int LEDs
{
get { return _leds; }
set
{
_leds = value;
if (IsAvailable)
{
SiApp.SiSetLEDs(_deviceHandle, _leds);
}
}
}
/// <summary>
/// Path to an icon for that device, provided by the 3Dx Ware Driver
/// </summary>
public string IconPath
{
get
{
if (IsAvailable)
{
string path;
var ret = SiApp.SiGetDeviceImageFileName(_deviceHandle, out path);
return path;
}
return null;
}
}
/// <summary>
/// Initialize the Device
/// </summary>
/// <param name="windowHandle">Handle to window using the device</param>
public void InitDevice(IntPtr windowHandle)
{
if (this.IsDisposed)
throw new ObjectDisposedException("");
if (IsAvailable)
return; //Init already done.
var v = SiApp.SiInitialize();
if (v == SiApp.SpwRetVal.SPW_DLL_LOAD_ERROR)
throw new _3DxException("Unable to load SiApp DLL");
SiApp.SiOpenData o = default(SiApp.SiOpenData);
SiApp.SiOpenWinInit(ref o, windowHandle);
_deviceHandle = SiApp.SiOpen(this.AppName, SiApp.SI_ANY_DEVICE, IntPtr.Zero, SiApp.SI_EVENT, ref o);
if (_deviceHandle == IntPtr.Zero)
{
SiApp.SiTerminate();
throw new _3DxException("Unable to open device");
}
string devName;
SiApp.SiGetDeviceName(_deviceHandle, out devName);
this.DeviceName = devName;
this.DeviceID = SiApp.SiGetDeviceID(_deviceHandle);
SiApp.SiDevInfo info = default(SiApp.SiDevInfo);
SiApp.SiGetDeviceInfo(_deviceHandle, ref info);
this.FirmwareVersion = info.firmware;
}
/// <summary>
/// Close the Device
/// </summary>
public void CloseDevice()
{
if (_deviceHandle != IntPtr.Zero)
{
SiApp.SiClose(_deviceHandle);
_deviceHandle = IntPtr.Zero;
}
}
/// <summary>
/// Called in WndPrc to process the Window Messages
/// </summary>
/// <param name="msg">Message Number</param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
public void ProcessWindowMessage(int msg, IntPtr wParam, IntPtr lParam)
{
if (this.IsDisposed) //We don't throw an Exception in the Message Loop, just return
return;
SiApp.SiGetEventData evd = default(SiApp.SiGetEventData);
SiApp.SiGetEventWinInit(ref evd, msg, wParam, lParam);
SiApp.SiSpwEvent_SpwData edata = default(SiApp.SiSpwEvent_SpwData);
var t = SiApp.SiGetEvent(_deviceHandle, SiApp.SI_AVERAGE_EVENTS, ref evd, ref edata);
if (t == SiApp.SpwRetVal.SI_IS_EVENT)
{
switch (edata.type)
{
case SiApp.SiEventType.SI_ZERO_EVENT:
lock (_locker)
{
_triggerZeroPoint = true;
Monitor.Pulse(_locker);
}
break;
case SiApp.SiEventType.SI_MOTION_EVENT:
lock (_locker)
{
_triggerMotionEvent = MotionEventArgs.FromEventArray(edata.spwData.mData);
Monitor.Pulse(_locker);
}
break;
}
}
}
protected virtual void OnZeroPoint()
{
if (ZeroPoint != null)
ZeroPoint(this, EventArgs.Empty);
}
protected virtual void OnMotion(MotionEventArgs args)
{
if (Motion != null)
Motion(this, args);
}
//protected virtual void OnDeviceChange(DeviceChangeEventArgs args)
//{
// if (DeviceChange != null)
// DeviceChange(this, args);
//}
#region IDisposable Member
public void Dispose()
{
if (IsDisposed) return;
this.IsDisposed = true;
CloseDevice();
ZeroPoint = null;
Motion = null;
lock (_locker)
Monitor.Pulse(_locker);
}
#endregion
private void EventThreadLoop()
{
while (!this.IsDisposed)
{
MotionEventArgs triggerMotionLocal;
bool triggerZeroLocal;
lock (_locker)
{
while (!_triggerZeroPoint && _triggerMotionEvent == null)
{
if (IsDisposed) return;
Monitor.Wait(_locker);
}
triggerMotionLocal = _triggerMotionEvent;
triggerZeroLocal = _triggerZeroPoint;
_triggerMotionEvent = null;
_triggerZeroPoint = false;
}
if (triggerMotionLocal != null)
OnMotion(triggerMotionLocal);
if (triggerZeroLocal)
OnZeroPoint();
Thread.Sleep(50);
}
}
}
}