2010-10-22 14:57:06 +00:00
|
|
|
|
#region License
|
|
|
|
|
//
|
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2006 - 2010 the Open Toolkit library.
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
//
|
2009-02-22 10:43:35 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2010-11-08 21:43:29 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Threading;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
using OpenTK.Input;
|
2010-11-08 21:43:29 +00:00
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.Windows
|
|
|
|
|
{
|
2009-02-28 23:11:27 +00:00
|
|
|
|
// Input driver for legacy (pre XP) Windows platforms.
|
2010-11-08 22:19:44 +00:00
|
|
|
|
// Supports a single mouse and keyboard through async input.
|
2010-11-08 21:43:29 +00:00
|
|
|
|
// Supports multiple joysticks through WinMM.
|
2010-11-08 22:19:44 +00:00
|
|
|
|
sealed class WMInput : IInputDriver2, IMouseDriver2, IKeyboardDriver2, IGamePadDriver
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-11-08 21:43:29 +00:00
|
|
|
|
#region Fields
|
|
|
|
|
|
2010-11-08 22:19:44 +00:00
|
|
|
|
readonly object MouseLock = new object();
|
|
|
|
|
readonly object KeyboardLock = new object();
|
2010-11-08 21:43:29 +00:00
|
|
|
|
readonly WinMMJoystick gamepad_driver = new WinMMJoystick();
|
|
|
|
|
KeyboardState keyboard = new KeyboardState();
|
|
|
|
|
MouseState mouse = new MouseState();
|
2009-02-28 23:11:27 +00:00
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
readonly WinKeyMap KeyMap = new WinKeyMap();
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-02-28 23:11:27 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
#region Constructor
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
public WMInput()
|
|
|
|
|
: base()
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-11-08 21:43:29 +00:00
|
|
|
|
Debug.WriteLine("Using WMInput.");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
#region Private Members
|
2010-11-08 22:19:44 +00:00
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
void UpdateKeyboard()
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-11-08 21:43:29 +00:00
|
|
|
|
for (int i = 0; i < 256; i++)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-11-08 21:43:29 +00:00
|
|
|
|
VirtualKeys key = (VirtualKeys)i;
|
|
|
|
|
bool pressed = (Functions.GetAsyncKeyState(key) >> 8) != 0;
|
|
|
|
|
if (KeyMap.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
keyboard[KeyMap[key]] = pressed;
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
2010-11-08 21:43:29 +00:00
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
void UpdateMouse()
|
|
|
|
|
{
|
|
|
|
|
POINT p = new POINT();
|
|
|
|
|
Functions.GetCursorPos(ref p);
|
2010-11-08 22:19:44 +00:00
|
|
|
|
mouse.X = p.X;
|
|
|
|
|
mouse.Y = p.Y;
|
2010-11-08 21:43:29 +00:00
|
|
|
|
// Note: we cannot poll the mouse wheel
|
|
|
|
|
mouse[MouseButton.Left] = (Functions.GetAsyncKeyState(VirtualKeys.LBUTTON) >> 8) != 0;
|
|
|
|
|
mouse[MouseButton.Middle] = (Functions.GetAsyncKeyState(VirtualKeys.RBUTTON) >> 8) != 0;
|
|
|
|
|
mouse[MouseButton.Right] = (Functions.GetAsyncKeyState(VirtualKeys.MBUTTON) >> 8) != 0;
|
|
|
|
|
mouse[MouseButton.Button1] = (Functions.GetAsyncKeyState(VirtualKeys.XBUTTON1) >> 8) != 0;
|
|
|
|
|
mouse[MouseButton.Button2] = (Functions.GetAsyncKeyState(VirtualKeys.XBUTTON2) >> 8) != 0;
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
#region IInputDriver2 Members
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-11-08 22:19:44 +00:00
|
|
|
|
public IKeyboardDriver2 KeyboardDriver
|
2010-11-08 21:43:29 +00:00
|
|
|
|
{
|
|
|
|
|
get { return this; }
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-11-08 22:19:44 +00:00
|
|
|
|
public IMouseDriver2 MouseDriver
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-11-08 21:43:29 +00:00
|
|
|
|
get { return this; }
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-08 22:19:44 +00:00
|
|
|
|
public IGamePadDriver GamePadDriver
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-11-08 21:43:29 +00:00
|
|
|
|
get { return this; }
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IMouseDriver2 Members
|
|
|
|
|
|
|
|
|
|
public MouseState GetState()
|
|
|
|
|
{
|
2010-11-08 22:19:44 +00:00
|
|
|
|
lock (MouseLock)
|
|
|
|
|
{
|
|
|
|
|
UpdateMouse();
|
|
|
|
|
return mouse;
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
public MouseState GetState(int index)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-11-08 22:19:44 +00:00
|
|
|
|
lock (MouseLock)
|
|
|
|
|
{
|
|
|
|
|
UpdateMouse();
|
|
|
|
|
if (index == 0)
|
|
|
|
|
return mouse;
|
|
|
|
|
else
|
|
|
|
|
return new MouseState();
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2010-10-22 14:57:06 +00:00
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
#region IKeyboardDriver2 Members
|
|
|
|
|
|
|
|
|
|
KeyboardState IKeyboardDriver2.GetState()
|
2010-10-22 14:57:06 +00:00
|
|
|
|
{
|
2010-11-08 22:19:44 +00:00
|
|
|
|
lock (KeyboardLock)
|
|
|
|
|
{
|
|
|
|
|
UpdateKeyboard();
|
|
|
|
|
return keyboard;
|
|
|
|
|
}
|
2010-10-22 14:57:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
KeyboardState IKeyboardDriver2.GetState(int index)
|
2010-10-22 14:57:06 +00:00
|
|
|
|
{
|
2010-11-08 22:19:44 +00:00
|
|
|
|
lock (KeyboardLock)
|
|
|
|
|
{
|
|
|
|
|
UpdateKeyboard();
|
|
|
|
|
if (index == 0)
|
|
|
|
|
return keyboard;
|
|
|
|
|
else
|
|
|
|
|
return new KeyboardState();
|
|
|
|
|
}
|
2010-10-22 14:57:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-08 21:43:29 +00:00
|
|
|
|
string IKeyboardDriver2.GetDeviceName(int index)
|
2010-10-22 14:57:06 +00:00
|
|
|
|
{
|
2010-11-08 21:43:29 +00:00
|
|
|
|
return "Default Windows Keyboard";
|
2010-10-22 14:57:06 +00:00
|
|
|
|
}
|
2010-11-08 21:43:29 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|