Tanoda
Plugins/LeapMotion/Core/Plugins/LeapCSharp/Controller.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9namespace Leap {
10 using System;
11 using System.Threading;
12
13 using LeapInternal;
14
38 public class Controller :
40 Connection _connection;
41 bool _disposed = false;
42 Config _config;
43
50 public SynchronizationContext EventContext {
51 get {
52 return _connection.EventContext;
53 }
54 set {
55 _connection.EventContext = value;
56 }
57 }
58
65 public event EventHandler<LeapEventArgs> Init {
66 add {
67 if (_hasInitialized)
68 value(this, new LeapEventArgs(LeapEvent.EVENT_INIT));
69 _init += value;
70 }
71 remove { _init -= value; }
72 }
73
74 private bool _hasInitialized = false;
75 private EventHandler<LeapEventArgs> _init;
76
81 public event EventHandler<ConnectionEventArgs> Connect {
82 add {
83 if (_hasConnected)
84 value(this, new ConnectionEventArgs());
85 _connect += value;
86 }
87 remove { _connect -= value; }
88 }
89
90 private bool _hasConnected = false;
91 private EventHandler<ConnectionEventArgs> _connect;
92
97 public event EventHandler<ConnectionLostEventArgs> Disconnect {
98 add {
99 _connection.LeapConnectionLost += value;
100 }
101 remove {
102 _connection.LeapConnectionLost -= value;
103 }
104 }
105
110 public event EventHandler<FrameEventArgs> FrameReady {
111 add {
112 _connection.LeapFrame += value;
113 }
114 remove {
115 _connection.LeapFrame -= value;
116 }
117 }
118
123 public event EventHandler<InternalFrameEventArgs> InternalFrameReady {
124 add {
125 _connection.LeapInternalFrame += value;
126 }
127 remove {
128 _connection.LeapInternalFrame -= value;
129 }
130 }
131
136 public event EventHandler<DeviceEventArgs> Device {
137 add {
138 _connection.LeapDevice += value;
139 }
140 remove {
141 _connection.LeapDevice -= value;
142 }
143 }
144
149 public event EventHandler<DeviceEventArgs> DeviceLost {
150 add {
151 _connection.LeapDeviceLost += value;
152 }
153 remove {
154 _connection.LeapDeviceLost -= value;
155 }
156 }
157
162 public event EventHandler<DeviceFailureEventArgs> DeviceFailure {
163 add {
164 _connection.LeapDeviceFailure += value;
165 }
166 remove {
167 _connection.LeapDeviceFailure -= value;
168 }
169 }
170
175 public event EventHandler<LogEventArgs> LogMessage {
176 add {
177 _connection.LeapLogEvent += value;
178 }
179 remove {
180 _connection.LeapLogEvent -= value;
181 }
182 }
183
188 public event EventHandler<PolicyEventArgs> PolicyChange {
189 add {
190 _connection.LeapPolicyChange += value;
191 }
192 remove {
193 _connection.LeapPolicyChange -= value;
194 }
195 }
196
201 public event EventHandler<ConfigChangeEventArgs> ConfigChange {
202 add {
203 _connection.LeapConfigChange += value;
204 }
205 remove {
206 _connection.LeapConfigChange -= value;
207 }
208 }
209
216 public event EventHandler<DistortionEventArgs> DistortionChange {
217 add {
218 _connection.LeapDistortionChange += value;
219 }
220 remove {
221 _connection.LeapDistortionChange -= value;
222 }
223 }
224
228 public event EventHandler<DroppedFrameEventArgs> DroppedFrame {
229 add {
230 _connection.LeapDroppedFrame += value;
231 }
232 remove {
233 _connection.LeapDroppedFrame -= value;
234 }
235 }
236
241 public event EventHandler<ImageEventArgs> ImageReady {
242 add {
243 _connection.LeapImage += value;
244 }
245 remove {
246 _connection.LeapImage -= value;
247 }
248 }
249
259 public event Action<BeginProfilingForThreadArgs> BeginProfilingForThread {
260 add {
261 _connection.LeapBeginProfilingForThread += value;
262 }
263 remove {
264 _connection.LeapBeginProfilingForThread -= value;
265 }
266 }
267
274 public event Action<EndProfilingForThreadArgs> EndProfilingForThread {
275 add {
276 _connection.LeapEndProfilingForThread += value;
277 }
278 remove {
279 _connection.LeapEndProfilingForThread -= value;
280 }
281 }
282
291 public event Action<BeginProfilingBlockArgs> BeginProfilingBlock {
292 add {
293 _connection.LeapBeginProfilingBlock += value;
294 }
295 remove {
296 _connection.LeapBeginProfilingBlock -= value;
297 }
298 }
299
308 public event Action<EndProfilingBlockArgs> EndProfilingBlock {
309 add {
310 _connection.LeapEndProfilingBlock += value;
311 }
312 remove {
313 _connection.LeapEndProfilingBlock -= value;
314 }
315 }
316
321 public event EventHandler<PointMappingChangeEventArgs> PointMappingChange {
322 add {
323 _connection.LeapPointMappingChange += value;
324 }
325 remove {
326 _connection.LeapPointMappingChange -= value;
327 }
328 }
329
333 public event EventHandler<HeadPoseEventArgs> HeadPoseChange {
334 add {
335 _connection.LeapHeadPoseChange += value;
336 }
337 remove {
338 _connection.LeapHeadPoseChange -= value;
339 }
340 }
341
342 public void Dispose() {
343 Dispose(true);
344 GC.SuppressFinalize(this);
345 }
346
347 // Protected implementation of Dispose pattern.
348 protected virtual void Dispose(bool disposing) {
349 if (_disposed) {
350 return;
351 }
352 _connection.Dispose();
353 _disposed = true;
354 }
355
363 public Controller() : this(0, null) { }
364
377 public Controller(int connectionKey, string serverNamespace = null) {
378 _connection = Connection.GetConnection(new Connection.Key(connectionKey, serverNamespace));
379 _connection.EventContext = SynchronizationContext.Current;
380
381 _connection.LeapInit += OnInit;
382 _connection.LeapConnection += OnConnect;
383 _connection.LeapConnectionLost += OnDisconnect;
384
385 _connection.Start();
386 }
387
396 public void StartConnection() {
397 _connection.Start();
398 }
399
408 public void StopConnection() {
409 _connection.Stop();
410 }
411
417 public bool IsServiceConnected {
418 get {
419 return _connection.IsServiceConnected;
420 }
421 }
422
436 public void SetPolicy(PolicyFlag policy) {
437 _connection.SetPolicy(policy);
438 }
439
449 public void ClearPolicy(PolicyFlag policy) {
450 _connection.ClearPolicy(policy);
451 }
452
467 public bool IsPolicySet(PolicyFlag policy) {
468 return _connection.IsPolicySet(policy);
469 }
470
490 public Frame Frame(int history = 0) {
491 Frame frame = new Frame();
492 Frame(frame, history);
493 return frame;
494 }
495
500 public void Frame(Frame toFill, int history = 0) {
501 LEAP_TRACKING_EVENT trackingEvent;
502 _connection.Frames.Get(out trackingEvent, history);
503 toFill.CopyFrom(ref trackingEvent);
504 }
505
513 public long FrameTimestamp(int history = 0) {
514 LEAP_TRACKING_EVENT trackingEvent;
515 _connection.Frames.Get(out trackingEvent, history);
516 return trackingEvent.info.timestamp;
517 }
518
523 public Frame GetTransformedFrame(LeapTransform trs, int history = 0) {
524 return new Frame().CopyFrom(Frame(history)).Transform(trs);
525 }
526
530 public Frame GetInterpolatedFrame(Int64 time) {
531 return _connection.GetInterpolatedFrame(time);
532 }
533
537 public void GetInterpolatedFrame(Frame toFill, Int64 time) {
538 _connection.GetInterpolatedFrame(toFill, time);
539 }
540
545 return _connection.GetInterpolatedHeadPose(time);
546 }
547
548 public void GetInterpolatedHeadPose(ref LEAP_HEAD_POSE_EVENT toFill, Int64 time) {
549 _connection.GetInterpolatedHeadPose(ref toFill, time);
550 }
551
552 public void TelemetryProfiling(ref LEAP_TELEMETRY_DATA telemetryData) {
553 _connection.TelemetryProfiling(ref telemetryData);
554 }
555
556 public UInt64 TelemetryGetNow() {
557 return LeapC.TelemetryGetNow();
558 }
559
560 public void GetPointMapping(ref PointMapping pointMapping) {
561 _connection.GetPointMapping(ref pointMapping);
562 }
563
573 public void GetInterpolatedLeftRightTransform(Int64 time,
574 Int64 sourceTime,
575 int leftId,
576 int rightId,
577 out LeapTransform leftTransform,
578 out LeapTransform rightTransform) {
579 _connection.GetInterpolatedLeftRightTransform(time, sourceTime, leftId, rightId, out leftTransform, out rightTransform);
580 }
581
582 public void GetInterpolatedFrameFromTime(Frame toFill, Int64 time, Int64 sourceTime) {
583 _connection.GetInterpolatedFrameFromTime(toFill, time, sourceTime);
584 }
585
592 public long Now() {
593 return LeapC.GetNow();
594 }
595
611 public bool IsConnected {
612 get {
613 return IsServiceConnected && Devices.Count > 0;
614 }
615 }
616
623 public Config Config {
624 get {
625 if (_config == null)
626 _config = new Config(this._connection.ConnectionKey);
627 return _config;
628 }
629 }
630
644 get {
645 return _connection.Devices;
646 }
647 }
648
658 return _connection.FailedDevices;
659 }
660
689 public enum PolicyFlag {
693 POLICY_DEFAULT = 0,
697 POLICY_BACKGROUND_FRAMES = (1 << 0),
701 POLICY_IMAGES = (1 << 1),
705 POLICY_OPTIMIZE_HMD = (1 << 2),
709 POLICY_ALLOW_PAUSE_RESUME = (1 << 3),
713 POLICY_MAP_POINTS = (1 << 7),
718 POLICY_OPTIMIZE_SCREENTOP = (1 << 8),
719 }
720
721 protected virtual void OnInit(object sender, LeapEventArgs eventArgs) {
722 _hasInitialized = true;
723 }
724
725 protected virtual void OnConnect(object sender, ConnectionEventArgs eventArgs) {
726 _hasConnected = true;
727 }
728
729 protected virtual void OnDisconnect(object sender, ConnectionLostEventArgs eventArgs) {
730 _hasInitialized = false;
731 _hasConnected = false;
732 }
733 }
734}
The Config class provides access to Leap Motion system configuration information.
Definition: Config.cs:20
Dispatched when the connection is established.
Definition: Events.cs:163
Dispatched when the connection is lost.
Definition: Events.cs:171
The Controller class is your main interface to the Leap Motion Controller.
EventHandler< PolicyEventArgs > PolicyChange
Dispatched when a policy changes.
virtual void OnDisconnect(object sender, ConnectionLostEventArgs eventArgs)
EventHandler< InternalFrameEventArgs > InternalFrameReady
Dispatched when an internal tracking frame is ready.
void TelemetryProfiling(ref LEAP_TELEMETRY_DATA telemetryData)
virtual void OnInit(object sender, LeapEventArgs eventArgs)
void GetInterpolatedLeftRightTransform(Int64 time, Int64 sourceTime, int leftId, int rightId, out LeapTransform leftTransform, out LeapTransform rightTransform)
This is a special variant of GetInterpolatedFrameFromTime, for use with special features that only re...
Config Config
Returns a Config object, which you can use to query the Leap Motion system for configuration informat...
EventHandler< PointMappingChangeEventArgs > PointMappingChange
Dispatched when point mapping change events are generated by the service.
EventHandler< DeviceEventArgs > DeviceLost
Dispatched when a Leap Motion device is disconnected.
Action< EndProfilingForThreadArgs > EndProfilingForThread
Dispatched whenever a thread is finished profiling. The event is always dispatched from the thread it...
EventHandler< DistortionEventArgs > DistortionChange
Dispatched when the image distortion map changes. The distortion map can change when the Leap device ...
void GetInterpolatedHeadPose(ref LEAP_HEAD_POSE_EVENT toFill, Int64 time)
long FrameTimestamp(int history=0)
Returns the timestamp of a recent tracking frame. Use the optional history parameter to specify how m...
EventHandler< LogEventArgs > LogMessage
Dispatched when the system generates a loggable event.
void GetInterpolatedFrame(Frame toFill, Int64 time)
Fills the Frame with data taken at the specified time, interpolating the data between existing frames...
Action< EndProfilingBlockArgs > EndProfilingBlock
Dispatched whenever a thread ends a profiling block. The event is always dispatched from the thread i...
Action< BeginProfilingForThreadArgs > BeginProfilingForThread
Dispatched whenever a thread wants to start profiling for a custom thread. The event is always dispat...
void GetPointMapping(ref PointMapping pointMapping)
LEAP_HEAD_POSE_EVENT GetInterpolatedHeadPose(Int64 time)
Returns the Head pose at the specified time, interpolating the data between existing frames,...
long Now()
Returns a timestamp value as close as possible to the current time. Values are in microseconds,...
Action< BeginProfilingBlockArgs > BeginProfilingBlock
Dispatched whenever a thread enters a profiling block. The event is always dispatched from the thread...
void ClearPolicy(PolicyFlag policy)
Requests clearing a policy.
void SetPolicy(PolicyFlag policy)
Requests setting a policy.
bool IsServiceConnected
Reports whether your application has a connection to the Leap Motion daemon/service....
EventHandler< HeadPoseEventArgs > HeadPoseChange
Dispatched when a new HeadPose is available.
DeviceList Devices
The list of currently attached and recognized Leap Motion controller devices.
EventHandler< ConfigChangeEventArgs > ConfigChange
Dispatched when a configuration setting changes.
Frame GetInterpolatedFrame(Int64 time)
Returns the Frame at the specified time, interpolating the data between existing frames,...
bool IsConnected
Reports whether this Controller is connected to the Leap Motion service and the Leap Motion hardware ...
FailedDeviceList FailedDevices()
A list of any Leap Motion hardware devices that are physically connected to the client computer,...
EventHandler< ConnectionEventArgs > Connect
Dispatched when the connection to the service is established.
Frame Frame(int history=0)
In most cases you should get Frame objects using the LeapProvider.CurrentFrame property....
EventHandler< ConnectionLostEventArgs > Disconnect
Dispatched if the connection to the service is lost.
Frame GetTransformedFrame(LeapTransform trs, int history=0)
Returns the frame object with all hands transformed by the specified transform matrix.
EventHandler< DroppedFrameEventArgs > DroppedFrame
Dispatched when the service drops a tracking frame.
void Frame(Frame toFill, int history=0)
Identical to Frame(history) but instead of constructing a new frame and returning it,...
EventHandler< ImageEventArgs > ImageReady
Dispatched when an unrequested image is ready.
void GetInterpolatedFrameFromTime(Frame toFill, Int64 time, Int64 sourceTime)
EventHandler< LeapEventArgs > Init
Dispatched when the connection is initialized (but not necessarily connected).
EventHandler< DeviceFailureEventArgs > DeviceFailure
Dispatched when a Leap device fails to initialize.
bool IsPolicySet(PolicyFlag policy)
Gets the active setting for a specific policy.
EventHandler< FrameEventArgs > FrameReady
Dispatched when a tracking frame is ready.
Controller(int connectionKey, string serverNamespace=null)
Constructs a Controller object using the specified connection key.
SynchronizationContext EventContext
The SynchronizationContext used for dispatching events.
virtual void OnConnect(object sender, ConnectionEventArgs eventArgs)
The Device class represents a physically connected device.
Definition: Device.cs:29
The DeviceList class represents a list of Device objects.
Definition: DeviceList.cs:20
The list of FailedDevice objects contains an entry for every failed Leap Motion hardware device conne...
The Frame class represents a set of hand and finger tracking data detected in a single frame.
Definition: Frame.cs:24
A generic object with no arguments beyond the event type.
Definition: Events.cs:42
void GetInterpolatedLeftRightTransform(Int64 time, Int64 sourceTime, Int64 leftId, Int64 rightId, out LeapTransform leftTransform, out LeapTransform rightTransform)
Definition: Connection.cs:405
SynchronizationContext EventContext
Definition: Connection.cs:78
void TelemetryProfiling(ref LEAP_TELEMETRY_DATA telemetryData)
Definition: Connection.cs:854
Action< EndProfilingBlockArgs > LeapEndProfilingBlock
Definition: Connection.cs:118
bool IsServiceConnected
Reports whether your application has a connection to the Leap Motion daemon/service....
Definition: Connection.cs:784
EventHandler< ConnectionLostEventArgs > LeapConnectionLost
Definition: Connection.cs:99
Action< BeginProfilingForThreadArgs > LeapBeginProfilingForThread
Definition: Connection.cs:115
Action< BeginProfilingBlockArgs > LeapBeginProfilingBlock
Definition: Connection.cs:117
EventHandler< DeviceEventArgs > LeapDeviceLost
Definition: Connection.cs:101
Action< EndProfilingForThreadArgs > LeapEndProfilingForThread
Definition: Connection.cs:116
EventHandler< LogEventArgs > LeapLogEvent
Definition: Connection.cs:106
EventHandler< LeapEventArgs > LeapInit
Definition: Connection.cs:81
static Connection GetConnection(int connectionId=0)
Definition: Connection.cs:30
void GetInterpolatedFrame(Frame toFill, Int64 time)
Definition: Connection.cs:362
EventHandler< FrameEventArgs > LeapFrame
Definition: Connection.cs:104
bool IsPolicySet(Controller.PolicyFlag policy)
Gets the active setting for a specific policy.
Definition: Connection.cs:746
EventHandler< ConfigChangeEventArgs > LeapConfigChange
Definition: Connection.cs:108
EventHandler< ConnectionEventArgs > LeapConnection
Definition: Connection.cs:91
EventHandler< DroppedFrameEventArgs > LeapDroppedFrame
Definition: Connection.cs:110
EventHandler< ImageEventArgs > LeapImage
Definition: Connection.cs:111
void GetInterpolatedFrameFromTime(Frame toFill, Int64 time, Int64 sourceTime)
Definition: Connection.cs:375
EventHandler< DeviceFailureEventArgs > LeapDeviceFailure
Definition: Connection.cs:102
void GetInterpolatedHeadPose(ref LEAP_HEAD_POSE_EVENT toFill, Int64 time)
Definition: Connection.cs:394
CircularObjectBuffer< LEAP_TRACKING_EVENT > Frames
Definition: Connection.cs:57
void SetPolicy(Controller.PolicyFlag policy)
Definition: Connection.cs:693
DeviceList Devices
The list of currently attached and recognized Leap Motion controller devices.
Definition: Connection.cs:814
EventHandler< DistortionEventArgs > LeapDistortionChange
Definition: Connection.cs:109
EventHandler< PolicyEventArgs > LeapPolicyChange
Definition: Connection.cs:103
void GetPointMapping(ref PointMapping pm)
Definition: Connection.cs:859
EventHandler< InternalFrameEventArgs > LeapInternalFrame
Definition: Connection.cs:105
void ClearPolicy(Controller.PolicyFlag policy)
Definition: Connection.cs:703
EventHandler< PointMappingChangeEventArgs > LeapPointMappingChange
Definition: Connection.cs:112
EventHandler< DeviceEventArgs > LeapDevice
Definition: Connection.cs:100
FailedDeviceList FailedDevices
Definition: Connection.cs:824
EventHandler< HeadPoseEventArgs > LeapHeadPoseChange
Definition: Connection.cs:113
static UInt64 TelemetryGetNow()
static long GetNow()
LeapEvent
An enumeration defining the types of Leap Motion events.
Definition: Events.cs:17
The LeapTransform class represents a transform in three dimensional space.
LEAP_FRAME_HEADER info
Definition: LeapC.cs:526