Make SpaceNav disposable, remove lock

This commit is contained in:
Thibaud Gasser 2018-05-30 19:36:43 +02:00
parent 6e3b39a78f
commit 50402abe53

View File

@ -3,13 +3,12 @@ using System.Runtime.InteropServices;
namespace spnavwrapper namespace spnavwrapper
{ {
public sealed class SpaceNav public sealed class SpaceNav : IDisposable
{ {
double _sensitivity = 1.0; double _sensitivity = 1.0;
int _threshold = 5; int _threshold = 5;
static SpaceNav instance = null; static SpaceNav instance;
static readonly object padlock = new object();
const int SPNAV_EVENT_MOTION = 1; const int SPNAV_EVENT_MOTION = 1;
const int SPNAV_EVENT_BUTTON = 2; const int SPNAV_EVENT_BUTTON = 2;
@ -61,17 +60,9 @@ namespace spnavwrapper
spnav_open(SPNAV_VENDOR_ID, SPNAV_PRODUCT_ID); spnav_open(SPNAV_VENDOR_ID, SPNAV_PRODUCT_ID);
} }
~SpaceNav()
{
// TODO : handle retcode
spnav_close();
}
public static SpaceNav Instance public static SpaceNav Instance
{ {
get get
{
lock (padlock)
{ {
if (instance == null) if (instance == null)
{ {
@ -80,20 +71,14 @@ namespace spnavwrapper
return instance; return instance;
} }
} }
}
public void CloseDevice() public void CloseDevice()
{
lock (padlock)
{ {
spnav_close(); spnav_close();
instance = null; instance = null;
} }
}
public SpaceNavEvent WaitEvent() public SpaceNavEvent WaitEvent()
{
lock (padlock)
{ {
SpNavEvent sev = new SpNavEvent(); SpNavEvent sev = new SpNavEvent();
spnav_wait_event(ref sev); spnav_wait_event(ref sev);
@ -108,11 +93,8 @@ namespace spnavwrapper
return null; return null;
} }
} }
}
public SpaceNavEvent WaitEvent(int milliseconds) public SpaceNavEvent WaitEvent(int milliseconds)
{
lock (padlock)
{ {
SpNavEvent sev = new SpNavEvent(); SpNavEvent sev = new SpNavEvent();
int ev_type = spnav_wait_event_timeout(ref sev, milliseconds); int ev_type = spnav_wait_event_timeout(ref sev, milliseconds);
@ -127,7 +109,6 @@ namespace spnavwrapper
return null; return null;
} }
} }
}
public double Sensitivity public double Sensitivity
{ {
@ -137,15 +118,12 @@ namespace spnavwrapper
} }
set set
{
lock (padlock)
{ {
// TODO : handle retcode // TODO : handle retcode
spnav_sensitivity(value); spnav_sensitivity(value);
_sensitivity = value; _sensitivity = value;
} }
} }
}
public int Threshold public int Threshold
{ {
@ -154,14 +132,38 @@ namespace spnavwrapper
return _threshold; return _threshold;
} }
set set
{
lock (padlock)
{ {
// TODO : handle retcode // TODO : handle retcode
spnav_deadzone(value); spnav_deadzone(value);
_threshold = value; _threshold = value;
} }
} }
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
void Dispose(bool disposing)
{
if (!disposedValue)
{
// Free unmanaged resources
spnav_close();
disposedValue = true;
} }
} }
~SpaceNav()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
} }