* OpenTK.csproj:

* Input/MouseState.cs:
* Input/ButtonState.cs:
* Platform/X11/X11Mouse.cs:
* Platform/X11/X11Factory.cs: Added initial implementation of
  OpenTK.Input.Mouse for X11.
This commit is contained in:
the_fiddler 2010-10-21 07:56:37 +00:00
parent 3c238a01f6
commit 1a8f589f5c
5 changed files with 370 additions and 4 deletions

View file

@ -0,0 +1,46 @@
#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
namespace OpenTK.Input
{
/// <summary>
/// Enumerates possible mouse button states.
/// </summary>
public enum ButtonState
{
/// <summary>
/// Indicates that a mouse button is released.
/// </summary>
Released = 0,
/// <summary>
/// Indicates that a mouse button is pressed.
/// </summary>
Pressed = 1
}
}

View file

@ -42,6 +42,8 @@ namespace OpenTK.Input
const int NumInts = ((int)MouseButton.LastButton + 31) / 32; const int NumInts = ((int)MouseButton.LastButton + 31) / 32;
// The following line triggers bogus CS0214 in gmcs 2.0.1, sigh... // The following line triggers bogus CS0214 in gmcs 2.0.1, sigh...
unsafe fixed int Buttons[NumInts]; unsafe fixed int Buttons[NumInts];
int x, y;
float wheel;
#endregion #endregion
@ -51,7 +53,7 @@ namespace OpenTK.Input
/// Gets a <see cref="System.Boolean"/> indicating whether this button is down. /// Gets a <see cref="System.Boolean"/> indicating whether this button is down.
/// </summary> /// </summary>
/// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param> /// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param>
public bool IsKeyDown(MouseButton button) public bool IsButtonDown(MouseButton button)
{ {
return ReadBit((int)button); return ReadBit((int)button);
} }
@ -60,11 +62,151 @@ namespace OpenTK.Input
/// Gets a <see cref="System.Boolean"/> indicating whether this button is up. /// Gets a <see cref="System.Boolean"/> indicating whether this button is up.
/// </summary> /// </summary>
/// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param> /// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param>
public bool IsKeyUp(MouseButton button) public bool IsButtonUp(MouseButton button)
{ {
return !ReadBit((int)button); return !ReadBit((int)button);
} }
/// <summary>
/// Gets the absolute wheel position in integer units.
/// To support high-precision mice, it is recommended to use <see cref="WheelPrecise"/> instead.
/// </summary>
public int Wheel
{
get { return (int)Math.Round(wheel, MidpointRounding.AwayFromZero); }
}
/// <summary>
/// Gets the absolute wheel position in floating-point units.
/// </summary>
public float WheelPrecise
{
get { return wheel; }
internal set
{
wheel = value;
}
}
/// <summary>
/// Gets an integer representing the absolute x position of the pointer, in window pixel coordinates.
/// </summary>
public int X
{
get { return x; }
internal set { x = value; }
}
/// <summary>
/// Gets an integer representing the absolute y position of the pointer, in window pixel coordinates.
/// </summary>
public int Y
{
get { return y; }
internal set { y = value; }
}
/// <summary>
/// Gets a System.Boolean indicating the state of the specified MouseButton.
/// </summary>
/// <param name="button">The MouseButton to check.</param>
/// <returns>True if the MouseButton is pressed, false otherwise.</returns>
public bool this[MouseButton button]
{
get
{
return IsButtonDown(button);
}
}
/// <summary>
/// Gets a <see cref="System.Boolean" indicating whether the left mouse button is pressed.
/// This property is intended for XNA compatibility.
/// </summary>
public ButtonState LeftButton
{
get { return IsButtonDown(MouseButton.Left) ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets a <see cref="System.Boolean" indicating whether the middle mouse button is pressed.
/// This property is intended for XNA compatibility.
/// </summary>
public ButtonState MiddleButton
{
get { return IsButtonDown(MouseButton.Middle) ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets a <see cref="System.Boolean" indicating whether the right mouse button is pressed.
/// This property is intended for XNA compatibility.
/// </summary>
public ButtonState RightButton
{
get { return IsButtonDown(MouseButton.Right) ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets a <see cref="System.Boolean" indicating whether the first extra mouse button is pressed.
/// This property is intended for XNA compatibility.
/// </summary>
public ButtonState XButton1
{
get { return IsButtonDown(MouseButton.Button1) ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets a <see cref="System.Boolean" indicating whether the second extra mouse button is pressed.
/// This property is intended for XNA compatibility.
/// </summary>
public ButtonState XButton2
{
get { return IsButtonDown(MouseButton.Button2) ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets the absolute wheel position in integer units. This property is intended for XNA compatibility.
/// To support high-precision mice, it is recommended to use <see cref="WheelPrecise"/> instead.
/// </summary>
public int ScrollWheelValue
{
get { return Wheel; }
}
/// <summary>
/// Checks whether two <see cref="MouseState" /> instances are equal.
/// </summary>
/// <param name="left">
/// A <see cref="MouseState"/> instance.
/// </param>
/// <param name="right">
/// A <see cref="MouseState"/> instance.
/// </param>
/// <returns>
/// True if both left is equal to right; false otherwise.
/// </returns>
public static bool operator ==(MouseState left, MouseState right)
{
return left.Equals(right);
}
/// <summary>
/// Checks whether two <see cref="MouseState" /> instances are not equal.
/// </summary>
/// <param name="left">
/// A <see cref="MouseState"/> instance.
/// </param>
/// <param name="right">
/// A <see cref="MouseState"/> instance.
/// </param>
/// <returns>
/// True if both left is not equal to right; false otherwise.
/// </returns>
public static bool operator !=(MouseState left, MouseState right)
{
return !left.Equals(right);
}
#endregion #endregion
#region Internal Members #region Internal Members
@ -128,6 +270,7 @@ namespace OpenTK.Input
for (int i = 0; equal && i < NumInts; i++) for (int i = 0; equal && i < NumInts; i++)
equal &= *(b1 + i) == *(b2 + i); equal &= *(b1 + i) == *(b2 + i);
} }
equal &= X == other.X && Y == other.Y && WheelPrecise == other.WheelPrecise;
} }
return equal; return equal;
} }

View file

@ -76,6 +76,9 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Documentation|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Documentation|AnyCPU'">
<OutputPath>..\..\Binaries\OpenTK\Release\</OutputPath> <OutputPath>..\..\Binaries\OpenTK\Release\</OutputPath>
<DebugType>none</DebugType>
<WarningLevel>4</WarningLevel>
<Optimize>true</Optimize>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Nsis|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Nsis|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@ -757,6 +760,8 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<Compile Include="Platform\X11\X11Keyboard.cs" /> <Compile Include="Platform\X11\X11Keyboard.cs" />
<Compile Include="Platform\X11\X11Mouse.cs" />
<Compile Include="Input\ButtonState.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup> <PropertyGroup>

View file

@ -85,8 +85,7 @@ namespace OpenTK.Platform.X11
public virtual OpenTK.Input.IMouseDriver CreateMouseDriver() public virtual OpenTK.Input.IMouseDriver CreateMouseDriver()
{ {
//return new X11Mouse(null); return new X11Mouse(null);
throw new NotImplementedException();
} }
#endregion #endregion

View file

@ -0,0 +1,173 @@
#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 OpenTK.Input;
namespace OpenTK.Platform.X11
{
sealed class X11Mouse : IMouseDriver
{
MouseState mouse = new MouseState();
X11WindowInfo window;
// Can either attach itself to the specified window or can hook the root window.
public X11Mouse(X11WindowInfo win)
{
if (win != null)
{
window = win;
}
else
{
using (new XLock(API.DefaultDisplay))
{
window = new X11WindowInfo();
window.Display = API.DefaultDisplay;
window.Screen = Functions.XDefaultScreen(window.Display);
window.RootWindow = Functions.XRootWindow(window.Display, window.Screen);
window.WindowHandle = window.RootWindow;
window.EventMask = EventMask.ButtonMotionMask |
EventMask.ButtonPressMask | EventMask.ButtonReleaseMask;
//Functions.XGrabPointer(window.Display, window.RootWindow, true,
// window.EventMask, GrabMode.GrabModeAsync, GrabMode.GrabModeAsync, IntPtr.Zero,
// IntPtr.Zero, IntPtr.Zero);
//Functions.XSelectInput(window.Display, window.RootWindow, new IntPtr((int)window.EventMask));
}
}
}
// Todo: remove this
public IList<MouseDevice> Mouse { get { throw new NotSupportedException(); } }
public MouseState GetState()
{
ProcessEvents();
return mouse;
}
public MouseState GetState(int index)
{
// X11Mouse supports a single mouse only
if (index < 0 || index > 1)
throw new ArgumentOutOfRangeException("index");
ProcessEvents();
return mouse;
}
void WriteBit(MouseButton offset, int enabled)
{
if (enabled != 0)
mouse.EnableBit((int)offset);
else
mouse.DisableBit((int)offset);
}
void ProcessEvents()
{
IntPtr root, child;
int root_x, root_y, win_x, win_y;
int buttons;
Functions.XQueryPointer(window.Display, window.WindowHandle, out root, out child,
out root_x, out root_y, out win_x, out win_y, out buttons);
mouse.X = root_x;
mouse.Y = root_y;
WriteBit(MouseButton.Left, buttons & (int)MouseMask.Button1Mask);
WriteBit(MouseButton.Middle, buttons & (int)MouseMask.Button2Mask);
WriteBit(MouseButton.Right, buttons & (int)MouseMask.Button3Mask);
if ((buttons & (int)MouseMask.Button4Mask) != 0)
mouse.WheelPrecise++;
if ((buttons & (int)MouseMask.Button5Mask) != 0)
mouse.WheelPrecise--;
WriteBit(MouseButton.Button1, buttons & (int)MouseMask.Button6Mask);
WriteBit(MouseButton.Button2, buttons & (int)MouseMask.Button7Mask);
WriteBit(MouseButton.Button3, buttons & (int)MouseMask.Button8Mask);
// XEvent e = new XEvent();
//
// while (true)
// {
// using (new XLock(window.Display))
// {
// if (!Functions.XCheckWindowEvent(window.Display, window.WindowHandle, window.EventMask, ref e))
// break;
//
// switch (e.type)
// {
// case XEventName.ButtonPress:
// switch (e.ButtonEvent.button)
// {
// case 1: mouse.EnableBit((int)MouseButton.Left); break;
// case 2: mouse.EnableBit((int)MouseButton.Middle); break;
// case 3: mouse.EnableBit((int)MouseButton.Right); break;
// case 4: mouse.WheelPrecise++; break;
// case 5: mouse.WheelPrecise--; break;
// case 6: mouse.EnableBit((int)MouseButton.Button1); break;
// case 7: mouse.EnableBit((int)MouseButton.Button2); break;
// case 8: mouse.EnableBit((int)MouseButton.Button3); break;
// case 9: mouse.EnableBit((int)MouseButton.Button4); break;
// case 10: mouse.EnableBit((int)MouseButton.Button5); break;
// case 11: mouse.EnableBit((int)MouseButton.Button6); break;
// case 12: mouse.EnableBit((int)MouseButton.Button7); break;
// case 13: mouse.EnableBit((int)MouseButton.Button8); break;
// case 15: mouse.EnableBit((int)MouseButton.Button9); break;
// }
// break;
//
// case XEventName.ButtonRelease:
// switch (e.ButtonEvent.button)
// {
// case 1: mouse.DisableBit((int)MouseButton.Left); break;
// case 2: mouse.DisableBit((int)MouseButton.Middle); break;
// case 3: mouse.DisableBit((int)MouseButton.Right); break;
// case 6: mouse.DisableBit((int)MouseButton.Button1); break;
// case 7: mouse.DisableBit((int)MouseButton.Button2); break;
// case 8: mouse.DisableBit((int)MouseButton.Button3); break;
// case 9: mouse.DisableBit((int)MouseButton.Button4); break;
// case 10: mouse.DisableBit((int)MouseButton.Button5); break;
// case 11: mouse.DisableBit((int)MouseButton.Button6); break;
// case 12: mouse.DisableBit((int)MouseButton.Button7); break;
// case 13: mouse.DisableBit((int)MouseButton.Button8); break;
// case 15: mouse.DisableBit((int)MouseButton.Button9); break;
// }
// break;
//
// case XEventName.MotionNotify:
// mouse.X = e.MotionEvent.x;
// mouse.Y = e.MotionEvent.y;
// break;
// }
// }
// }
}
}
}