Do not modify device state during the GetState() call. Fixes intermittent crashes.

This commit is contained in:
the_fiddler 2010-10-28 13:00:21 +00:00
parent 03c97e306b
commit c1043d1693
2 changed files with 40 additions and 20 deletions

View file

@ -46,6 +46,7 @@ namespace OpenTK.Platform.Windows
readonly Dictionary<ContextHandle, int> rawids = new Dictionary<ContextHandle, int>();
private List<KeyboardDevice> keyboards_old = new List<KeyboardDevice>();
private IntPtr window;
readonly object UpdateLock = new object();
#region --- Constructors ---
@ -235,9 +236,12 @@ namespace OpenTK.Platform.Windows
break;
}
lock (UpdateLock)
{
keyboards[rawids[handle]] = keyboard;
return processed;
}
}
#endregion
@ -263,6 +267,8 @@ namespace OpenTK.Platform.Windows
}
public KeyboardState GetState()
{
lock (UpdateLock)
{
KeyboardState master = new KeyboardState();
foreach (KeyboardState ks in keyboards)
@ -271,14 +277,18 @@ namespace OpenTK.Platform.Windows
}
return master;
}
}
public KeyboardState GetState(int index)
{
lock (UpdateLock)
{
if (keyboards.Count > index)
return keyboards[index];
else
return new KeyboardState();
}
}
#endregion

View file

@ -44,6 +44,7 @@ namespace OpenTK.Platform.Windows
List<MouseState> mice;
Dictionary<ContextHandle, int> rawids; // ContextHandle instead of IntPtr for fast dictionary access
readonly IntPtr Window;
readonly object UpdateLock = new object();
public WinRawMouse(IntPtr window)
{
@ -64,6 +65,8 @@ namespace OpenTK.Platform.Windows
public IList<MouseDevice> Mouse { get { throw new NotImplementedException(); } }
public MouseState GetState()
{
lock (UpdateLock)
{
MouseState master = new MouseState();
foreach (MouseState ms in mice)
@ -72,14 +75,18 @@ namespace OpenTK.Platform.Windows
}
return master;
}
}
public MouseState GetState(int index)
{
lock (UpdateLock)
{
if (mice.Count > index)
return mice[index];
else
return new MouseState();
}
}
#endregion
@ -226,8 +233,11 @@ namespace OpenTK.Platform.Windows
mouse.Y += raw.LastY;
}
lock (UpdateLock)
{
mice[rawids[handle]] = mouse;
return true;
}
}
}
}