Opentk/Source/OpenTK/Platform/Windows/WMInput.cs
the_fiddler 07cbb9dd8b * Input/Mouse.cs:
* Input/IMouseDriver2.cs:
* Platform/X11/X11Mouse.cs:
* Platform/X11/XI2Mouse.cs:
* Platform/X11/Functions.cs:
* Platform/Windows/WMInput.cs:
* Platform/X11/X11GLNative.cs:
* Platform/Windows/WinRawMouse.cs: Added ability to set the position
  of the mouse cursor.
[X11] Avoid grabbing the pointer, as this causes unexpected
  side-effects (XInput2 stops working, debugging becomes difficult).
  We now use XWarpPointer and try to discard the spurious MouseMove
  events it generates.
[X11] Make cursor visible when window loses focus, to make debugging
  easier. Restore previous state when it regains focus.
2010-11-23 17:17:13 +00:00

178 lines
5 KiB
C#

#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.
//
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Text;
using OpenTK.Input;
namespace OpenTK.Platform.Windows
{
// Input driver for legacy (pre XP) Windows platforms.
// Supports a single mouse and keyboard through async input.
// Supports multiple joysticks through WinMM.
sealed class WMInput : IInputDriver2, IMouseDriver2, IKeyboardDriver2, IGamePadDriver
{
#region Fields
readonly object MouseLock = new object();
readonly object KeyboardLock = new object();
readonly WinMMJoystick gamepad_driver = new WinMMJoystick();
KeyboardState keyboard = new KeyboardState();
MouseState mouse = new MouseState();
readonly WinKeyMap KeyMap = new WinKeyMap();
#endregion
#region Constructor
public WMInput()
: base()
{
Debug.WriteLine("Using WMInput.");
}
#endregion
#region Private Members
void UpdateKeyboard()
{
for (int i = 0; i < 256; i++)
{
VirtualKeys key = (VirtualKeys)i;
bool pressed = (Functions.GetAsyncKeyState(key) >> 8) != 0;
if (KeyMap.ContainsKey(key))
{
keyboard[KeyMap[key]] = pressed;
}
}
}
void UpdateMouse()
{
POINT p = new POINT();
Functions.GetCursorPos(ref p);
mouse.X = p.X;
mouse.Y = p.Y;
// 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;
}
#endregion
#region IInputDriver2 Members
public IKeyboardDriver2 KeyboardDriver
{
get { return this; }
}
public IMouseDriver2 MouseDriver
{
get { return this; }
}
public IGamePadDriver GamePadDriver
{
get { return this; }
}
#endregion
#region IMouseDriver2 Members
public MouseState GetState()
{
lock (MouseLock)
{
UpdateMouse();
return mouse;
}
}
public MouseState GetState(int index)
{
lock (MouseLock)
{
UpdateMouse();
if (index == 0)
return mouse;
else
return new MouseState();
}
}
public void SetPosition(double x, double y)
{
throw new NotImplementedException();
}
#endregion
#region IKeyboardDriver2 Members
KeyboardState IKeyboardDriver2.GetState()
{
lock (KeyboardLock)
{
UpdateKeyboard();
return keyboard;
}
}
KeyboardState IKeyboardDriver2.GetState(int index)
{
lock (KeyboardLock)
{
UpdateKeyboard();
if (index == 0)
return keyboard;
else
return new KeyboardState();
}
}
string IKeyboardDriver2.GetDeviceName(int index)
{
return "Default Windows Keyboard";
}
#endregion
}
}