Skip to content

Commit

Permalink
Update to LeapCSharp 5.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SDraw committed Jun 1, 2022
1 parent 987aa79 commit d748e10
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 52 deletions.
21 changes: 11 additions & 10 deletions ml_lme_cvr/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class LeapMotionExtension : MelonLoader.MelonMod

public override void OnApplicationStart()
{
ms_instance = this;
if(ms_instance == null)
ms_instance = this;

DependenciesHandler.ExtractDependencies();

Expand Down Expand Up @@ -137,11 +138,11 @@ void OnSettingsEnableChange()
if(Settings.Enabled)
{
m_leapController.StartConnection();
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP);
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP, null);
if(Settings.HmdMode)
m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
else
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
}
else
m_leapController.StopConnection();
Expand Down Expand Up @@ -177,11 +178,11 @@ void OnSettingsHmdModeChange()
{
if(m_leapController != null)
{
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP);
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP, null);
if(Settings.HmdMode)
m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
else
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
}

if(m_leapControllerModel != null)
Expand Down Expand Up @@ -249,11 +250,11 @@ void OnLeapDeviceInitialized(object p_sender, Leap.DeviceEventArgs p_args)
{
if(Settings.Enabled && (m_leapController != null))
{
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP);
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP, null);
if(Settings.HmdMode)
m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
else
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, null);
}

if(CohtmlHud.Instance != null)
Expand Down
6 changes: 3 additions & 3 deletions ml_lme_cvr/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Reflection;

[assembly: AssemblyTitle("LeapMotionExtension")]
[assembly: AssemblyVersion("1.0.4")]
[assembly: AssemblyFileVersion("1.0.4")]
[assembly: AssemblyVersion("1.0.5")]
[assembly: AssemblyFileVersion("1.0.5")]

[assembly: MelonLoader.MelonInfo(typeof(ml_lme_cvr.LeapMotionExtension), "LeapMotionExtension", "1.0.4", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonInfo(typeof(ml_lme_cvr.LeapMotionExtension), "LeapMotionExtension", "1.0.5", "SDraw", "https://github.com/SDraw/ml_mods_cvr")]
[assembly: MelonLoader.MelonGame(null, "ChilloutVR")]
[assembly: MelonLoader.MelonPlatform(MelonLoader.MelonPlatformAttribute.CompatiblePlatforms.WINDOWS_X64)]
[assembly: MelonLoader.MelonPlatformDomain(MelonLoader.MelonPlatformDomainAttribute.CompatibleDomains.MONO)]
2 changes: 1 addition & 1 deletion ml_lme_cvr/ml_lme_cvr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,6 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>copy /y "$(TargetPath)" "C:\Games\Steam\steamapps\common\ChilloutVR\Mods\</PostBuildEvent>
<PostBuildEvent>copy /y "$(TargetPath)" "C:\Games\Steam\common\ChilloutVR\Mods\</PostBuildEvent>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion ml_lme_cvr/ml_lme_cvr.csproj.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ReferencePath>C:\Games\Steam\steamapps\common\ChilloutVR\MelonLoader\;C:\Games\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\</ReferencePath>
<ReferencePath>C:\Games\Steam\common\ChilloutVR\MelonLoader\;C:\Games\Steam\common\ChilloutVR\ChilloutVR_Data\Managed\</ReferencePath>
</PropertyGroup>
</Project>
159 changes: 140 additions & 19 deletions ml_lme_cvr/vendor/LeapCSharp/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,21 @@ static Connection()
private int _frameBufferLength = 60; //TODO, surface this value in LeapC, currently hardcoded!

private IntPtr _leapConnection;
private bool _isRunning = false;
private volatile bool _isRunning = false;
private Thread _polster;

//Policy and enabled features
private UInt64 _activePolicies = 0;
/// <summary>
/// Has the connection been set up in multi device aware mode
/// </summary>
private bool _multiDeviceAwareConnection = false;

/// <summary>
/// Minimum service version that support setting the tracking mode on a per dervice basis
/// </summary>
private static LEAP_VERSION MinServiceVersionForMultiModeSupport = new LEAP_VERSION() { major = 5, minor = 4, patch = 4 };

//Policy and enabled features, indexed by device ID
private Dictionary<uint, UInt64> _activePolicies = new Dictionary<uint, ulong>();

//Config change status
private Dictionary<uint, string> _configRequests = new Dictionary<uint, string>();
Expand Down Expand Up @@ -173,6 +183,7 @@ public void Start(string serverNamespace = "Leap Service", bool multiDeviceAware
config.server_namespace = Marshal.StringToHGlobalAnsi(serverNamespace);
config.flags = multiDeviceAware ? (uint)eLeapConnectionFlag.eLeapConnectionFlag_MultipleDevicesAware : 0;
config.size = (uint)Marshal.SizeOf(config);
_multiDeviceAwareConnection = multiDeviceAware;
Start(config);
}

Expand Down Expand Up @@ -279,6 +290,7 @@ private void processMessages()

LEAP_CONNECTION_MESSAGE _msg = new LEAP_CONNECTION_MESSAGE();
uint timeout = 150;

result = LeapC.PollConnection(_leapConnection, timeout, ref _msg);

if (result != eLeapRS.eLeapRS_Success)
Expand Down Expand Up @@ -325,9 +337,8 @@ private void processMessages()
case eLeapEventType.eLeapEventType_Policy:
LEAP_POLICY_EVENT policy_evt;
StructMarshal<LEAP_POLICY_EVENT>.PtrToStruct(_msg.eventStructPtr, out policy_evt);
handlePolicyChange(ref policy_evt);
handlePolicyChange(ref policy_evt, _msg.deviceID);
break;

case eLeapEventType.eLeapEventType_Tracking:
LEAP_TRACKING_EVENT tracking_evt;
StructMarshal<LEAP_TRACKING_EVENT>.PtrToStruct(_msg.eventStructPtr, out tracking_evt);
Expand Down Expand Up @@ -482,7 +493,16 @@ public void GetInterpolatedFrameFromTime(Frame toFill, Int64 time, Int64 sourceT

public Frame GetInterpolatedFrame(Int64 time, Device device = null)
{
Frame frame = new Frame();
Frame frame;
if (device == null)
{
frame = new Frame();
}
else
{
frame = new Frame(device.DeviceID);
}

GetInterpolatedFrame(frame, time, device);
return frame;
}
Expand Down Expand Up @@ -638,7 +658,8 @@ private void handleDevice(ref LEAP_DEVICE_EVENT deviceMsg)
(Device.DeviceType)deviceInfo.type,
deviceInfo.status == (uint)eLeapDeviceStatus.eLeapDeviceStatus_Streaming,
deviceInfo.status,
Marshal.PtrToStringAnsi(deviceInfo.serial));
Marshal.PtrToStringAnsi(deviceInfo.serial),
deviceMsg.device.id);

Marshal.FreeCoTaskMem(deviceInfo.serial);
_devices.AddOrUpdate(apiDevice);
Expand All @@ -663,6 +684,11 @@ private void handleLostDevice(ref LEAP_DEVICE_EVENT deviceMsg)
{
_devices.Remove(lost);

if (_activePolicies.ContainsKey(deviceMsg.device.id))
{
_activePolicies.Remove(deviceMsg.device.id);
}

if (LeapDeviceLost != null)
{
LeapDeviceLost.DispatchOnContext(this, EventContext, new DeviceEventArgs(lost));
Expand Down Expand Up @@ -856,38 +882,117 @@ private void handleImage(ref LEAP_IMAGE_EVENT imageMsg, UInt32 deviceID)
}
}

private void handlePolicyChange(ref LEAP_POLICY_EVENT policyMsg)
private void handlePolicyChange(ref LEAP_POLICY_EVENT policyMsg, UInt32 deviceID)
{
// Avoid raising spurious policy change signals.
if (policyMsg.current_policy == _activePolicies) return;
if (_activePolicies.ContainsKey(deviceID))
{
if (policyMsg.current_policy == _activePolicies[deviceID])
{
return;
}
}

if (LeapPolicyChange != null)
{
LeapPolicyChange.DispatchOnContext(this, EventContext, new PolicyEventArgs(policyMsg.current_policy, _activePolicies));
if (_activePolicies.ContainsKey(deviceID))
{
LeapPolicyChange.DispatchOnContext(this, EventContext,
new PolicyEventArgs(policyMsg.current_policy, _activePolicies[deviceID], true, _devices.FindDeviceByID(deviceID)));
}
else
{
// We should get a policy flags event on device connection. This tells us the current policy. From our perspective we don't
// have a record of a 'previous' policy, so assume it's zero and raise an event
LeapPolicyChange.DispatchOnContext(this, EventContext,
new PolicyEventArgs(policyMsg.current_policy, 0, false, _devices.FindDeviceByID(deviceID)));
}
}

_activePolicies = policyMsg.current_policy;
_activePolicies[deviceID] = policyMsg.current_policy;
}

public void SetAndClearPolicy(Controller.PolicyFlag set, Controller.PolicyFlag clear)
public void SetAndClearPolicy(Controller.PolicyFlag set, Controller.PolicyFlag clear, Device device = null)
{
UInt64 setFlags = (ulong)FlagForPolicy(set);
UInt64 clearFlags = (ulong)FlagForPolicy(clear);
eLeapRS result = LeapC.SetPolicyFlags(_leapConnection, setFlags, clearFlags);
eLeapRS result;

if (device == null || !_multiDeviceAwareConnection)
{
result = LeapC.SetPolicyFlags(_leapConnection, setFlags, clearFlags);
}
else
{
if (!Controller.CheckRequiredServiceVersion(MinServiceVersionForMultiModeSupport, this))
{
UnityEngine.Debug.LogWarning(String.Format("Your current tracking service does not support setting policy flags on a per device basis (min version is {0}.{1}.{2}). Please update your service: https://developer.leapmotion.com/tracking-software-download",
MinServiceVersionForMultiModeSupport.major,
MinServiceVersionForMultiModeSupport.minor,
MinServiceVersionForMultiModeSupport.patch));

return;
}

result = LeapC.SetPolicyFlagsEx(_leapConnection, device.Handle, setFlags, clearFlags);
}

reportAbnormalResults("LeapC SetAndClearPolicy call was ", result);
}

public void SetPolicy(Controller.PolicyFlag policy)
public void SetPolicy(Controller.PolicyFlag policy, Device device = null)
{
UInt64 setFlags = (ulong)FlagForPolicy(policy);
eLeapRS result = LeapC.SetPolicyFlags(_leapConnection, setFlags, 0);

eLeapRS result;

if (device == null || !_multiDeviceAwareConnection)
{
result = LeapC.SetPolicyFlags(_leapConnection, setFlags, 0);
}
else
{
if (!Controller.CheckRequiredServiceVersion(MinServiceVersionForMultiModeSupport, this))
{
UnityEngine.Debug.LogWarning(String.Format("Your current tracking service does not support setting policy flags on a per device basis (min version is {0}.{1}.{2}). Please update your service: https://developer.leapmotion.com/tracking-software-download",
MinServiceVersionForMultiModeSupport.major,
MinServiceVersionForMultiModeSupport.minor,
MinServiceVersionForMultiModeSupport.patch));

return;
}

result = LeapC.SetPolicyFlagsEx(_leapConnection, device.Handle, setFlags, 0);
}

reportAbnormalResults("LeapC SetPolicyFlags call was ", result);
}

public void ClearPolicy(Controller.PolicyFlag policy)
public void ClearPolicy(Controller.PolicyFlag policy, Device device = null)
{
UInt64 clearFlags = (ulong)FlagForPolicy(policy);
eLeapRS result = LeapC.SetPolicyFlags(_leapConnection, 0, clearFlags);

eLeapRS result;

if (device == null || !_multiDeviceAwareConnection)
{
result = LeapC.SetPolicyFlags(_leapConnection, 0, clearFlags);
}
else
{
if (!Controller.CheckRequiredServiceVersion(MinServiceVersionForMultiModeSupport, this))
{
UnityEngine.Debug.LogWarning(String.Format("Your current tracking service does not support clearing policy flags on a per device basis (min version is {0}.{1}.{2}). Please update your service: https://developer.leapmotion.com/tracking-software-download",
MinServiceVersionForMultiModeSupport.major,
MinServiceVersionForMultiModeSupport.minor,
MinServiceVersionForMultiModeSupport.patch));

return;
}

result = LeapC.SetPolicyFlagsEx(_leapConnection, device.Handle, 0, clearFlags);
}

reportAbnormalResults("LeapC SetPolicyFlags call was ", result);
}

Expand Down Expand Up @@ -929,10 +1034,26 @@ static public eLeapPolicyFlag FlagForPolicy(Controller.PolicyFlag singlePolicy)
///
/// @since 2.1.6
/// </summary>
public bool IsPolicySet(Controller.PolicyFlag policy)
public bool IsPolicySet(Controller.PolicyFlag policy, Device device = null)
{
UInt64 policyToCheck = (ulong)FlagForPolicy(policy);
return (_activePolicies & policyToCheck) == policyToCheck;

uint deviceID = 0;
if (device != null)
{
deviceID = device.DeviceID;
}

if (_activePolicies.ContainsKey(deviceID))
{
return (_activePolicies[deviceID] & policyToCheck) == policyToCheck;
}
else
{
Logger.Log("Warning: an attempt has been made to check whether a policy flag is set for an unknown device");
}

return false;
}

public uint GetConfigValue(string config_key)
Expand Down
Loading

0 comments on commit d748e10

Please sign in to comment.