mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-16 02:56:09 -04:00
Remove unused joystick code.
This commit is contained in:
parent
40ce65ab09
commit
50209b22ad
@ -455,18 +455,6 @@ namespace OpenTK
|
||||
|
||||
#endregion
|
||||
|
||||
#region Joysticks
|
||||
|
||||
/// <summary>
|
||||
/// Gets a readonly IList containing all available OpenTK.Input.JoystickDevices.
|
||||
/// </summary>
|
||||
public IList<JoystickDevice> Joysticks
|
||||
{
|
||||
get { return InputDriver.Joysticks; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Keyboard
|
||||
|
||||
/// <summary>
|
||||
|
@ -5,7 +5,6 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
|
||||
namespace OpenTK.Input {
|
||||
@ -23,30 +22,17 @@ namespace OpenTK.Input {
|
||||
Keyboard,
|
||||
/// <summary> Device is a mouse. </summary>
|
||||
Mouse,
|
||||
/// <summary> Device is a Human Interface Device. Joysticks, joypads, pens
|
||||
/// and some specific usb keyboards/mice fall into this category. </summary>
|
||||
Hid
|
||||
}
|
||||
|
||||
/// <summary> Defines the interface for an input driver. </summary>
|
||||
public interface IInputDriver : IJoystickDriver, IDisposable {
|
||||
public interface IInputDriver : IDisposable {
|
||||
|
||||
/// <summary> Gets the list of available KeyboardDevices. </summary>
|
||||
/// <summary> Gets the available KeyboardDevice. </summary>
|
||||
KeyboardDevice Keyboard { get; }
|
||||
|
||||
/// <summary> Gets the list of available MouseDevices. </summary>
|
||||
/// <summary> Gets the available MouseDevice. </summary>
|
||||
MouseDevice Mouse { get; }
|
||||
|
||||
/// <summary> Updates the state of the driver. </summary>
|
||||
void Poll();
|
||||
|
||||
Point DesktopCursorPos { get; set; }
|
||||
}
|
||||
|
||||
/// <summary> Defines the interface for JoystickDevice drivers. </summary>
|
||||
public interface IJoystickDriver {
|
||||
|
||||
/// <summary> Gets the list of available JoystickDevices. </summary>
|
||||
IList<JoystickDevice> Joysticks { get; }
|
||||
}
|
||||
}
|
||||
|
@ -1,471 +0,0 @@
|
||||
#region License
|
||||
//
|
||||
// The Open Toolkit Library License
|
||||
//
|
||||
// Copyright (c) 2006 - 2008 the Open Toolkit library, except where noted.
|
||||
//
|
||||
// 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;
|
||||
|
||||
namespace OpenTK.Input
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a joystick device and provides methods to query its status.
|
||||
/// </summary>
|
||||
public abstract class JoystickDevice : IInputDevice
|
||||
{
|
||||
#region Fields
|
||||
|
||||
int id;
|
||||
string description;
|
||||
JoystickAxisCollection axis_collection;
|
||||
JoystickButtonCollection button_collection;
|
||||
JoystickMoveEventArgs move_args = new JoystickMoveEventArgs(0, 0, 0);
|
||||
JoystickButtonEventArgs button_args = new JoystickButtonEventArgs(0, false);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal JoystickDevice(int id, int axes, int buttons)
|
||||
{
|
||||
if (axes < 0)
|
||||
throw new ArgumentOutOfRangeException("axes");
|
||||
|
||||
if (buttons < 0)
|
||||
throw new ArgumentOutOfRangeException("buttons");
|
||||
|
||||
Id = id;
|
||||
axis_collection = new JoystickAxisCollection(axes);
|
||||
button_collection = new JoystickButtonCollection(buttons);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Members
|
||||
|
||||
/// <summary>
|
||||
/// Gets a JoystickAxisCollection containing the state of each axis on this instance. Values are normalized in the [-1, 1] range.
|
||||
/// </summary>
|
||||
public JoystickAxisCollection Axis { get { return axis_collection; } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets JoystickButtonCollection containing the state of each button on this instance. True indicates that the button is pressed.
|
||||
/// </summary>
|
||||
public JoystickButtonCollection Button { get { return button_collection; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region IInputDevice Members
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.String containing a unique description for this instance.
|
||||
/// </summary>
|
||||
public string Description
|
||||
{
|
||||
get { return description; }
|
||||
internal set { description = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating the InputDeviceType of this InputDevice.
|
||||
/// </summary>
|
||||
public InputDeviceType DeviceType
|
||||
{
|
||||
get { return InputDeviceType.Hid; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when an axis of this JoystickDevice instance is moved.
|
||||
/// </summary>
|
||||
public EventHandler<JoystickMoveEventArgs> Move =
|
||||
delegate(object sender, JoystickMoveEventArgs e) { };
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a button of this JoystickDevice instance is pressed.
|
||||
/// </summary>
|
||||
public EventHandler<JoystickButtonEventArgs> ButtonDown =
|
||||
delegate(object sender, JoystickButtonEventArgs e) { };
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a button of this JoystickDevice is released.
|
||||
/// </summary>
|
||||
public EventHandler<JoystickButtonEventArgs> ButtonUp =
|
||||
delegate(object sender, JoystickButtonEventArgs e) { };
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internal Members
|
||||
|
||||
internal int Id
|
||||
{
|
||||
get { return id; }
|
||||
set { id = value; }
|
||||
}
|
||||
|
||||
internal void SetAxis(JoystickAxis axis, float @value)
|
||||
{
|
||||
move_args.Axis = axis;
|
||||
move_args.Delta = move_args.Value - @value;
|
||||
axis_collection[axis] = move_args.Value = @value;
|
||||
Move(this, move_args);
|
||||
}
|
||||
|
||||
internal void SetButton(JoystickButton button, bool @value)
|
||||
{
|
||||
if (button_collection[button] != @value)
|
||||
{
|
||||
button_args.Button = button;
|
||||
button_collection[button] = button_args.Pressed = @value;
|
||||
if (@value)
|
||||
ButtonDown(this, button_args);
|
||||
else
|
||||
ButtonUp(this, button_args);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region JoystickDevice<TDetail> : JoystickDevice
|
||||
|
||||
// Provides platform-specific information about the relevant JoystickDevice.
|
||||
internal sealed class JoystickDevice<TDetail> : JoystickDevice
|
||||
{
|
||||
internal JoystickDevice(int id, int axes, int buttons)
|
||||
: base(id, axes, buttons)
|
||||
{ }
|
||||
|
||||
internal TDetail Details;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Arguments
|
||||
|
||||
/// <summary>
|
||||
/// The base class for JoystickDevice event arguments.
|
||||
/// </summary>
|
||||
public class JoystickEventArgs : EventArgs
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides data for the <see cref="JoystickDevice.ButtonDown"/> and <see cref="JoystickDevice.ButtonUp"/> events.
|
||||
/// This class is cached for performance reasons - avoid storing references outside the scope of the event.
|
||||
/// </summary>
|
||||
public class JoystickButtonEventArgs : EventArgs
|
||||
{
|
||||
#region Fields
|
||||
|
||||
JoystickButton button;
|
||||
bool pressed;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JoystickButtonEventArgs"/> class.
|
||||
/// </summary>
|
||||
/// <param name="button">The index of the joystick button for the event.</param>
|
||||
/// <param name="pressed">The current state of the button.</param>
|
||||
internal JoystickButtonEventArgs(JoystickButton button, bool pressed)
|
||||
{
|
||||
this.button = button;
|
||||
this.pressed = pressed;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Members
|
||||
|
||||
/// <summary>
|
||||
/// The index of the joystick button for the event.
|
||||
/// </summary>
|
||||
public JoystickButton Button { get { return this.button; } internal set { this.button = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.Boolean representing the state of the button for the event.
|
||||
/// </summary>
|
||||
public bool Pressed { get { return pressed; } internal set { this.pressed = value; } }
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides data for the <see cref="JoystickDevice.Move"/> event.
|
||||
/// This class is cached for performance reasons - avoid storing references outside the scope of the event.
|
||||
/// </summary>
|
||||
public class JoystickMoveEventArgs : JoystickEventArgs
|
||||
{
|
||||
#region Fields
|
||||
|
||||
JoystickAxis axis;
|
||||
float value;
|
||||
float delta;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JoystickMoveEventArgs"/> class.
|
||||
/// </summary>
|
||||
/// <param name="axis">The index of the joystick axis that was moved.</param>
|
||||
/// <param name="value">The absolute value of the joystick axis.</param>
|
||||
/// <param name="delta">The relative change in value of the joystick axis.</param>
|
||||
public JoystickMoveEventArgs(JoystickAxis axis, float value, float delta)
|
||||
{
|
||||
this.axis = axis;
|
||||
this.value = value;
|
||||
this.delta = delta;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Members
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.Int32 representing the index of the axis that was moved.
|
||||
/// </summary>
|
||||
public JoystickAxis Axis { get { return axis; } internal set { this.axis = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.Single representing the absolute position of the axis.
|
||||
/// </summary>
|
||||
public float Value { get { return value; } internal set { this.value = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.Single representing the relative change in the position of the axis.
|
||||
/// </summary>
|
||||
public float Delta { get { return delta; } internal set { this.delta = value; } }
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JoystickButton
|
||||
|
||||
/// <summary>
|
||||
/// Defines available JoystickDevice buttons.
|
||||
/// </summary>
|
||||
public enum JoystickButton
|
||||
{
|
||||
/// <summary>The first button of the JoystickDevice.</summary>
|
||||
Button0 = 0,
|
||||
/// <summary>The second button of the JoystickDevice.</summary>
|
||||
Button1,
|
||||
/// <summary>The third button of the JoystickDevice.</summary>
|
||||
Button2,
|
||||
/// <summary>The fourth button of the JoystickDevice.</summary>
|
||||
Button3,
|
||||
/// <summary>The fifth button of the JoystickDevice.</summary>
|
||||
Button4,
|
||||
/// <summary>The sixth button of the JoystickDevice.</summary>
|
||||
Button5,
|
||||
/// <summary>The seventh button of the JoystickDevice.</summary>
|
||||
Button6,
|
||||
/// <summary>The eighth button of the JoystickDevice.</summary>
|
||||
Button7,
|
||||
/// <summary>The ninth button of the JoystickDevice.</summary>
|
||||
Button8,
|
||||
/// <summary>The tenth button of the JoystickDevice.</summary>
|
||||
Button9,
|
||||
/// <summary>The eleventh button of the JoystickDevice.</summary>
|
||||
Button10,
|
||||
/// <summary>The twelfth button of the JoystickDevice.</summary>
|
||||
Button11,
|
||||
/// <summary>The thirteenth button of the JoystickDevice.</summary>
|
||||
Button12,
|
||||
/// <summary>The fourteenth button of the JoystickDevice.</summary>
|
||||
Button13,
|
||||
/// <summary>The fifteenth button of the JoystickDevice.</summary>
|
||||
Button14,
|
||||
/// <summary>The sixteenth button of the JoystickDevice.</summary>
|
||||
Button15,
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JoystickButtonCollection
|
||||
|
||||
/// <summary>
|
||||
/// Defines a collection of JoystickButtons.
|
||||
/// </summary>
|
||||
public sealed class JoystickButtonCollection
|
||||
{
|
||||
#region Fields
|
||||
|
||||
bool[] button_state;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal JoystickButtonCollection(int numButtons)
|
||||
{
|
||||
if (numButtons < 0)
|
||||
throw new ArgumentOutOfRangeException("numButtons");
|
||||
|
||||
button_state = new bool[numButtons];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Members
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.Boolean indicating whether the JoystickButton with the specified index is pressed.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the JoystickButton to check.</param>
|
||||
/// <returns>True if the JoystickButton is pressed; false otherwise.</returns>
|
||||
public bool this[int index]
|
||||
{
|
||||
get { return button_state[index]; }
|
||||
internal set { button_state[index] = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.Boolean indicating whether the specified JoystickButton is pressed.
|
||||
/// </summary>
|
||||
/// <param name="button">The JoystickButton to check.</param>
|
||||
/// <returns>True if the JoystickButton is pressed; false otherwise.</returns>
|
||||
public bool this[JoystickButton button]
|
||||
{
|
||||
get { return button_state[(int)button]; }
|
||||
internal set { button_state[(int)button] = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.Int32 indicating the available amount of JoystickButtons.
|
||||
/// </summary>
|
||||
public int Count
|
||||
{
|
||||
get { return button_state.Length; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JoystickAxis
|
||||
|
||||
/// <summary>
|
||||
/// Defines available JoystickDevice axes.
|
||||
/// </summary>
|
||||
public enum JoystickAxis
|
||||
{
|
||||
/// <summary>The first axis of the JoystickDevice.</summary>
|
||||
Axis0 = 0,
|
||||
/// <summary>The second axis of the JoystickDevice.</summary>
|
||||
Axis1,
|
||||
/// <summary>The third axis of the JoystickDevice.</summary>
|
||||
Axis2,
|
||||
/// <summary>The fourth axis of the JoystickDevice.</summary>
|
||||
Axis3,
|
||||
/// <summary>The fifth axis of the JoystickDevice.</summary>
|
||||
Axis4,
|
||||
/// <summary>The sixth axis of the JoystickDevice.</summary>
|
||||
Axis5,
|
||||
/// <summary>The seventh axis of the JoystickDevice.</summary>
|
||||
Axis6,
|
||||
/// <summary>The eighth axis of the JoystickDevice.</summary>
|
||||
Axis7,
|
||||
/// <summary>The ninth axis of the JoystickDevice.</summary>
|
||||
Axis8,
|
||||
/// <summary>The tenth axis of the JoystickDevice.</summary>
|
||||
Axis9,
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region JoystickAxisCollection
|
||||
|
||||
/// <summary>
|
||||
/// Defines a collection of JoystickAxes.
|
||||
/// </summary>
|
||||
public sealed class JoystickAxisCollection
|
||||
{
|
||||
#region Fields
|
||||
|
||||
float[] axis_state;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal JoystickAxisCollection(int numAxes)
|
||||
{
|
||||
if (numAxes < 0)
|
||||
throw new ArgumentOutOfRangeException("numAxes");
|
||||
|
||||
axis_state = new float[numAxes];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Members
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.Single indicating the absolute position of the JoystickAxis with the specified index.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the JoystickAxis to check.</param>
|
||||
/// <returns>A System.Single in the range [-1, 1].</returns>
|
||||
public float this[int index]
|
||||
{
|
||||
get { return axis_state[index]; }
|
||||
internal set { axis_state[index] = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.Single indicating the absolute position of the JoystickAxis.
|
||||
/// </summary>
|
||||
/// <param name="axis">The JoystickAxis to check.</param>
|
||||
/// <returns>A System.Single in the range [-1, 1].</returns>
|
||||
public float this[JoystickAxis axis]
|
||||
{
|
||||
get { return axis_state[(int)axis]; }
|
||||
internal set { axis_state[(int)axis] = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a System.Int32 indicating the available amount of JoystickAxes.
|
||||
/// </summary>
|
||||
public int Count
|
||||
{
|
||||
get { return axis_state.Length; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
@ -73,7 +73,6 @@
|
||||
<Compile Include="Graphics\OpenGL\GLEnums.cs" />
|
||||
<Compile Include="Graphics\OpenGL\GLHelper.cs" />
|
||||
<Compile Include="Input\Interfaces.cs" />
|
||||
<Compile Include="Input\JoystickDevice.cs" />
|
||||
<Compile Include="Input\Key.cs" />
|
||||
<Compile Include="Input\KeyboardDevice.cs" />
|
||||
<Compile Include="Input\MouseButton.cs" />
|
||||
@ -105,7 +104,6 @@
|
||||
<Compile Include="Platform\Windows\WinGLContext.cs" />
|
||||
<Compile Include="Platform\Windows\WinGLNative.cs" />
|
||||
<Compile Include="Platform\Windows\WinKeyMap.cs" />
|
||||
<Compile Include="Platform\Windows\WinMMJoystick.cs" />
|
||||
<Compile Include="Platform\Windows\WinWindowInfo.cs" />
|
||||
<Compile Include="Platform\X11\API.cs" />
|
||||
<Compile Include="Platform\X11\Functions.cs" />
|
||||
@ -116,7 +114,6 @@
|
||||
<Compile Include="Platform\X11\X11GLNative.cs" />
|
||||
<Compile Include="Platform\X11\X11GraphicsMode.cs" />
|
||||
<Compile Include="Platform\X11\X11Input.cs" />
|
||||
<Compile Include="Platform\X11\X11Joystick.cs" />
|
||||
<Compile Include="Platform\X11\X11KeyMap.cs" />
|
||||
<Compile Include="Platform\X11\X11WindowInfo.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
@ -106,7 +106,6 @@ namespace OpenTK.Platform.MacOS
|
||||
new Rect((short)x, (short)y, (short)width, (short)height));
|
||||
|
||||
mDisplayDevice = device;
|
||||
dummy_joystick_list.Add(new JoystickDevice<object>(0, 0, 0));
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -1066,10 +1065,6 @@ namespace OpenTK.Platform.MacOS
|
||||
|
||||
KeyboardDevice keyboard = new KeyboardDevice();
|
||||
MouseDevice mouse = new MouseDevice();
|
||||
List<JoystickDevice> dummy_joystick_list = new List<JoystickDevice>(1);
|
||||
|
||||
public void Poll() {
|
||||
}
|
||||
|
||||
public KeyboardDevice Keyboard {
|
||||
get { return keyboard; }
|
||||
@ -1091,10 +1086,6 @@ namespace OpenTK.Platform.MacOS
|
||||
set { System.Windows.Forms.Cursor.Position = value; }
|
||||
}
|
||||
|
||||
public IList<JoystickDevice> Joysticks {
|
||||
get { return dummy_joystick_list; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +75,6 @@ namespace OpenTK.Platform.Windows
|
||||
Icon icon;
|
||||
|
||||
// Used for IInputDriver implementation
|
||||
WinMMJoystick joystick_driver = new WinMMJoystick();
|
||||
KeyboardDevice keyboard = new KeyboardDevice();
|
||||
MouseDevice mouse = new MouseDevice();
|
||||
static readonly WinKeyMap KeyMap = new WinKeyMap();
|
||||
@ -1111,10 +1110,6 @@ namespace OpenTK.Platform.Windows
|
||||
|
||||
#region IInputDriver Members
|
||||
|
||||
public void Poll() {
|
||||
joystick_driver.Poll();
|
||||
}
|
||||
|
||||
public KeyboardDevice Keyboard {
|
||||
get { return keyboard; }
|
||||
}
|
||||
@ -1136,15 +1131,6 @@ namespace OpenTK.Platform.Windows
|
||||
|
||||
#endregion
|
||||
|
||||
#region IJoystickDriver Members
|
||||
|
||||
public IList<JoystickDevice> Joysticks
|
||||
{
|
||||
get { return joystick_driver.Joysticks; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
public void Dispose()
|
||||
|
@ -1,431 +0,0 @@
|
||||
#region License
|
||||
//
|
||||
// The Open Toolkit Library License
|
||||
//
|
||||
// Copyright (c) 2006 - 2008 the Open Toolkit library, except where noted.
|
||||
//
|
||||
// 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.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
using OpenTK.Input;
|
||||
using System.Security;
|
||||
using Microsoft.Win32;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace OpenTK.Platform.Windows
|
||||
{
|
||||
sealed class WinMMJoystick : IJoystickDriver
|
||||
{
|
||||
#region Fields
|
||||
|
||||
List<JoystickDevice> sticks = new List<JoystickDevice>();
|
||||
IList<JoystickDevice> sticks_readonly;
|
||||
|
||||
// Todo: Read the joystick name from the registry.
|
||||
//static readonly string RegistryJoyConfig = @"Joystick%dConfiguration";
|
||||
//static readonly string RegistryJoyName = @"Joystick%dOEMName";
|
||||
//static readonly string RegstryJoyCurrent = @"CurrentJoystickSettings";
|
||||
|
||||
bool disposed;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public WinMMJoystick()
|
||||
{
|
||||
sticks_readonly = sticks.AsReadOnly();
|
||||
|
||||
// WinMM supports up to 16 joysticks.
|
||||
int number = 0;
|
||||
while (number < UnsafeNativeMethods.joyGetNumDevs())
|
||||
{
|
||||
JoystickDevice<WinMMJoyDetails> stick = OpenJoystick(number++);
|
||||
if (stick != null)
|
||||
{
|
||||
sticks.Add(stick);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Members
|
||||
|
||||
JoystickDevice<WinMMJoyDetails> OpenJoystick(int number)
|
||||
{
|
||||
JoystickDevice<WinMMJoyDetails> stick = null;
|
||||
|
||||
JoyCaps caps;
|
||||
JoystickError result = UnsafeNativeMethods.joyGetDevCaps(number, out caps, JoyCaps.SizeInBytes);
|
||||
if (result != JoystickError.NoError)
|
||||
return null;
|
||||
|
||||
int num_axes = caps.NumAxes;
|
||||
if ((caps.Capabilities & JoystCapsFlags.HasPov) != 0)
|
||||
num_axes += 2;
|
||||
|
||||
stick = new JoystickDevice<WinMMJoyDetails>(number, num_axes, caps.NumButtons);
|
||||
stick.Details = new WinMMJoyDetails(num_axes);
|
||||
|
||||
// Make sure to reverse the vertical axes, so that +1 points up and -1 points down.
|
||||
int axis = 0;
|
||||
if (axis < caps.NumAxes)
|
||||
{ stick.Details.Min[axis] = caps.XMin; stick.Details.Max[axis] = caps.XMax; axis++; }
|
||||
if (axis < caps.NumAxes)
|
||||
{ stick.Details.Min[axis] = caps.YMax; stick.Details.Max[axis] = caps.YMin; axis++; }
|
||||
if (axis < caps.NumAxes)
|
||||
{ stick.Details.Min[axis] = caps.ZMax; stick.Details.Max[axis] = caps.ZMin; axis++; }
|
||||
if (axis < caps.NumAxes)
|
||||
{ stick.Details.Min[axis] = caps.RMin; stick.Details.Max[axis] = caps.RMax; axis++; }
|
||||
if (axis < caps.NumAxes)
|
||||
{ stick.Details.Min[axis] = caps.UMin; stick.Details.Max[axis] = caps.UMax; axis++; }
|
||||
if (axis < caps.NumAxes)
|
||||
{ stick.Details.Min[axis] = caps.VMax; stick.Details.Max[axis] = caps.VMin; axis++; }
|
||||
|
||||
if ((caps.Capabilities & JoystCapsFlags.HasPov) != 0)
|
||||
{
|
||||
stick.Details.PovType = PovType.Exists;
|
||||
if ((caps.Capabilities & JoystCapsFlags.HasPov4Dir) != 0)
|
||||
stick.Details.PovType |= PovType.Discrete;
|
||||
if ((caps.Capabilities & JoystCapsFlags.HasPovContinuous) != 0)
|
||||
stick.Details.PovType |= PovType.Continuous;
|
||||
}
|
||||
|
||||
#warning "Implement joystick name detection for WinMM."
|
||||
stick.Description = String.Format("Joystick/Joystick #{0} ({1} axes, {2} buttons)", number, stick.Axis.Count, stick.Button.Count);
|
||||
// Todo: Try to get the device name from the registry. Oh joy!
|
||||
//string key_path = String.Format("{0}\\{1}\\{2}", RegistryJoyConfig, caps.RegKey, RegstryJoyCurrent);
|
||||
//RegistryKey key = Registry.LocalMachine.OpenSubKey(key_path, false);
|
||||
//if (key == null)
|
||||
// key = Registry.CurrentUser.OpenSubKey(key_path, false);
|
||||
//if (key == null)
|
||||
// stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons)", number, stick.Axis.Count, stick.Button.Count);
|
||||
//else
|
||||
//{
|
||||
// key.Close();
|
||||
//}
|
||||
|
||||
if (stick != null)
|
||||
Debug.Print("Found joystick on device number {0}", number);
|
||||
return stick;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IJoystickDriver
|
||||
|
||||
public int DeviceCount
|
||||
{
|
||||
get { return sticks.Count; }
|
||||
}
|
||||
|
||||
public IList<JoystickDevice> Joysticks
|
||||
{
|
||||
get { return sticks_readonly; }
|
||||
}
|
||||
|
||||
public void Poll()
|
||||
{
|
||||
foreach (JoystickDevice<WinMMJoyDetails> js in sticks)
|
||||
{
|
||||
JoyInfoEx info = new JoyInfoEx();
|
||||
info.Size = JoyInfoEx.SizeInBytes;
|
||||
info.Flags = JoystickFlags.All;
|
||||
UnsafeNativeMethods.joyGetPosEx(js.Id, ref info);
|
||||
|
||||
int num_axes = js.Axis.Count;
|
||||
if ((js.Details.PovType & PovType.Exists) != 0)
|
||||
num_axes -= 2; // Because the last two axis are used for POV input.
|
||||
|
||||
int axis = 0;
|
||||
if (axis < num_axes)
|
||||
{ js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.XPos, axis)); axis++; }
|
||||
if (axis < num_axes)
|
||||
{ js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.YPos, axis)); axis++; }
|
||||
if (axis < num_axes)
|
||||
{ js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.ZPos, axis)); axis++; }
|
||||
if (axis < num_axes)
|
||||
{ js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.RPos, axis)); axis++; }
|
||||
if (axis < num_axes)
|
||||
{ js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.UPos, axis)); axis++; }
|
||||
if (axis < num_axes)
|
||||
{ js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.VPos, axis)); axis++; }
|
||||
|
||||
if ((js.Details.PovType & PovType.Exists) != 0)
|
||||
{
|
||||
float x = 0, y = 0;
|
||||
|
||||
// A discrete POV returns specific values for left, right, etc.
|
||||
// A continuous POV returns an integer indicating an angle in degrees * 100, e.g. 18000 == 180.00 degrees.
|
||||
// The vast majority of joysticks have discrete POVs, so we'll treat all of them as discrete for simplicity.
|
||||
if ((JoystickPovPosition)info.Pov != JoystickPovPosition.Centered)
|
||||
{
|
||||
if (info.Pov > (int)JoystickPovPosition.Left || info.Pov < (int)JoystickPovPosition.Right)
|
||||
{ y = 1; }
|
||||
if ((info.Pov > (int)JoystickPovPosition.Forward) && (info.Pov < (int)JoystickPovPosition.Backward))
|
||||
{ x = 1; }
|
||||
if ((info.Pov > (int)JoystickPovPosition.Right) && (info.Pov < (int)JoystickPovPosition.Left))
|
||||
{ y = -1; }
|
||||
if (info.Pov > (int)JoystickPovPosition.Backward)
|
||||
{ x = -1; }
|
||||
}
|
||||
//if ((js.Details.PovType & PovType.Discrete) != 0)
|
||||
//{
|
||||
// if ((JoystickPovPosition)info.Pov == JoystickPovPosition.Centered)
|
||||
// { x = 0; y = 0; }
|
||||
// else if ((JoystickPovPosition)info.Pov == JoystickPovPosition.Forward)
|
||||
// { x = 0; y = -1; }
|
||||
// else if ((JoystickPovPosition)info.Pov == JoystickPovPosition.Left)
|
||||
// { x = -1; y = 0; }
|
||||
// else if ((JoystickPovPosition)info.Pov == JoystickPovPosition.Backward)
|
||||
// { x = 0; y = 1; }
|
||||
// else if ((JoystickPovPosition)info.Pov == JoystickPovPosition.Right)
|
||||
// { x = 1; y = 0; }
|
||||
//}
|
||||
//else if ((js.Details.PovType & PovType.Continuous) != 0)
|
||||
//{
|
||||
// if ((JoystickPovPosition)info.Pov == JoystickPovPosition.Centered)
|
||||
// {
|
||||
// // This approach moves the hat on a circle, not a square as it should.
|
||||
// float angle = (float)(System.Math.PI * info.Pov / 18000.0 + System.Math.PI / 2);
|
||||
// x = (float)System.Math.Cos(angle);
|
||||
// y = (float)System.Math.Sin(angle);
|
||||
// if (x < 0.001)
|
||||
// x = 0;
|
||||
// if (y < 0.001)
|
||||
// y = 0;
|
||||
// }
|
||||
//}
|
||||
//else
|
||||
// throw new NotImplementedException("Please post an issue report at http://www.opentk.com/issues");
|
||||
|
||||
js.SetAxis((JoystickAxis)axis++, x);
|
||||
js.SetAxis((JoystickAxis)axis++, y);
|
||||
}
|
||||
|
||||
int button = 0;
|
||||
while (button < js.Button.Count)
|
||||
{
|
||||
js.SetButton((JoystickButton)button, (info.Buttons & (1 << button)) != 0);
|
||||
button++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
void Dispose(bool manual)
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
if (manual)
|
||||
{
|
||||
}
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
~WinMMJoystick()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region UnsafeNativeMethods
|
||||
|
||||
[Flags]
|
||||
enum JoystickFlags
|
||||
{
|
||||
X = 0x1,
|
||||
Y = 0x2,
|
||||
Z = 0x4,
|
||||
R = 0x8,
|
||||
U = 0x10,
|
||||
V = 0x20,
|
||||
Pov = 0x40,
|
||||
Buttons = 0x80,
|
||||
All = X | Y | Z | R | U | V | Pov | Buttons
|
||||
}
|
||||
|
||||
enum JoystickError : uint
|
||||
{
|
||||
NoError = 0,
|
||||
InvalidParameters = 165,
|
||||
NoCanDo = 166,
|
||||
Unplugged = 167
|
||||
//MM_NoDriver = 6,
|
||||
//MM_InvalidParameter = 11
|
||||
}
|
||||
|
||||
[Flags]
|
||||
enum JoystCapsFlags
|
||||
{
|
||||
HasZ = 0x1,
|
||||
HasR = 0x2,
|
||||
HasU = 0x4,
|
||||
HasV = 0x8,
|
||||
HasPov = 0x16,
|
||||
HasPov4Dir = 0x32,
|
||||
HasPovContinuous = 0x64
|
||||
}
|
||||
|
||||
enum JoystickPovPosition : ushort
|
||||
{
|
||||
Centered = 0xFFFF,
|
||||
Forward = 0,
|
||||
Right = 9000,
|
||||
Backward = 18000,
|
||||
Left = 27000
|
||||
}
|
||||
struct JoyCaps
|
||||
{
|
||||
public ushort Mid;
|
||||
public ushort ProductId;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
||||
public string ProductName;
|
||||
public int XMin;
|
||||
public int XMax;
|
||||
public int YMin;
|
||||
public int YMax;
|
||||
public int ZMin;
|
||||
public int ZMax;
|
||||
public int NumButtons;
|
||||
public int PeriodMin;
|
||||
public int PeriodMax;
|
||||
public int RMin;
|
||||
public int RMax;
|
||||
public int UMin;
|
||||
public int UMax;
|
||||
public int VMin;
|
||||
public int VMax;
|
||||
public JoystCapsFlags Capabilities;
|
||||
public int MaxAxes;
|
||||
public int NumAxes;
|
||||
public int MaxButtons;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
||||
public string RegKey;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
|
||||
public string OemVxD;
|
||||
|
||||
public static readonly int SizeInBytes;
|
||||
|
||||
static JoyCaps()
|
||||
{
|
||||
SizeInBytes = Marshal.SizeOf(default(JoyCaps));
|
||||
}
|
||||
}
|
||||
|
||||
struct JoyInfoEx
|
||||
{
|
||||
public int Size;
|
||||
[MarshalAs(UnmanagedType.I4)]
|
||||
public JoystickFlags Flags;
|
||||
public int XPos;
|
||||
public int YPos;
|
||||
public int ZPos;
|
||||
public int RPos;
|
||||
public int UPos;
|
||||
public int VPos;
|
||||
public uint Buttons;
|
||||
public uint ButtonNumber;
|
||||
public int Pov;
|
||||
uint Reserved1;
|
||||
uint Reserved2;
|
||||
|
||||
public static readonly int SizeInBytes;
|
||||
|
||||
static JoyInfoEx()
|
||||
{
|
||||
SizeInBytes = Marshal.SizeOf(default(JoyInfoEx));
|
||||
}
|
||||
}
|
||||
|
||||
static class UnsafeNativeMethods
|
||||
{
|
||||
[DllImport("Winmm.dll"), SuppressUnmanagedCodeSecurity]
|
||||
public static extern JoystickError joyGetDevCaps(int uJoyID, out JoyCaps pjc, int cbjc);
|
||||
[DllImport("Winmm.dll"), SuppressUnmanagedCodeSecurity]
|
||||
public static extern uint joyGetPosEx(int uJoyID, ref JoyInfoEx pji);
|
||||
[DllImport("Winmm.dll"), SuppressUnmanagedCodeSecurity]
|
||||
public static extern int joyGetNumDevs();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region enum PovType
|
||||
|
||||
[Flags]
|
||||
enum PovType
|
||||
{
|
||||
None = 0x0,
|
||||
Exists = 0x1,
|
||||
Discrete = 0x2,
|
||||
Continuous = 0x4
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region struct WinMMJoyDetails
|
||||
|
||||
struct WinMMJoyDetails
|
||||
{
|
||||
public readonly float[] Min, Max; // Minimum and maximum offset of each axis.
|
||||
public PovType PovType;
|
||||
|
||||
public WinMMJoyDetails(int num_axes)
|
||||
{
|
||||
Min = new float[num_axes];
|
||||
Max = new float[num_axes];
|
||||
PovType = PovType.None;
|
||||
}
|
||||
|
||||
public float CalculateOffset(float pos, int axis)
|
||||
{
|
||||
float offset = (2 * (pos - Min[axis])) / (Max[axis] - Min[axis]) - 1;
|
||||
if (offset > 1)
|
||||
return 1;
|
||||
else if (offset < -1)
|
||||
return -1;
|
||||
else if (offset < 0.001f && offset > -0.001f)
|
||||
return 0;
|
||||
else
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -20,7 +20,6 @@ namespace OpenTK.Platform.X11
|
||||
/// </summary>
|
||||
internal sealed class X11Input : IInputDriver
|
||||
{
|
||||
X11Joystick joystick_driver = new X11Joystick();
|
||||
KeyboardDevice keyboard = new KeyboardDevice();
|
||||
MouseDevice mouse = new MouseDevice();
|
||||
|
||||
@ -151,15 +150,6 @@ namespace OpenTK.Platform.X11
|
||||
set { System.Windows.Forms.Cursor.Position = value; }
|
||||
}
|
||||
|
||||
public IList<JoystickDevice> Joysticks {
|
||||
get { return joystick_driver.Joysticks; }
|
||||
}
|
||||
|
||||
/// <summary> Polls and updates state of all keyboard, mouse and joystick devices. </summary>
|
||||
public void Poll() {
|
||||
joystick_driver.Poll();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Dispose() {
|
||||
|
@ -1,252 +0,0 @@
|
||||
#region License
|
||||
//
|
||||
// The Open Toolkit Library License
|
||||
//
|
||||
// Copyright (c) 2006 - 2008 the Open Toolkit library, except where noted.
|
||||
//
|
||||
// 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.Runtime.InteropServices;
|
||||
using OpenTK.Input;
|
||||
|
||||
namespace OpenTK.Platform.X11
|
||||
{
|
||||
struct X11JoyDetails { }
|
||||
|
||||
sealed class X11Joystick : IJoystickDriver
|
||||
{
|
||||
#region Fields
|
||||
|
||||
List<JoystickDevice> sticks = new List<JoystickDevice>();
|
||||
IList<JoystickDevice> sticks_readonly;
|
||||
|
||||
bool disposed;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public X11Joystick()
|
||||
{
|
||||
sticks_readonly = sticks.AsReadOnly();
|
||||
|
||||
int number = 0, max_sticks = 25;
|
||||
while (number < max_sticks)
|
||||
{
|
||||
JoystickDevice stick = OpenJoystick(JoystickPath, number++);
|
||||
if (stick != null)
|
||||
{
|
||||
stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons, {3}{0})",
|
||||
number, stick.Axis.Count, stick.Button.Count, JoystickPath);
|
||||
sticks.Add(stick);
|
||||
}
|
||||
}
|
||||
|
||||
number = 0;
|
||||
while (number < max_sticks)
|
||||
{
|
||||
JoystickDevice stick = OpenJoystick(JoystickPathLegacy, number++);
|
||||
if (stick != null)
|
||||
{
|
||||
stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons, {3}{0})",
|
||||
number, stick.Axis.Count, stick.Button.Count, JoystickPathLegacy);
|
||||
sticks.Add(stick);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IJoystickDriver
|
||||
|
||||
public int DeviceCount
|
||||
{
|
||||
get { return sticks.Count; }
|
||||
}
|
||||
|
||||
public IList<JoystickDevice> Joysticks
|
||||
{
|
||||
get { return sticks_readonly; }
|
||||
}
|
||||
|
||||
public void Poll()
|
||||
{
|
||||
JoystickEvent e;
|
||||
|
||||
foreach (JoystickDevice js in sticks)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
while ((long)UnsafeNativeMethods.read(js.Id, (void*)&e, (UIntPtr)sizeof(JoystickEvent)) > 0)
|
||||
{
|
||||
e.Type &= ~JoystickEventType.Init;
|
||||
|
||||
switch (e.Type)
|
||||
{
|
||||
case JoystickEventType.Axis:
|
||||
// Flip vertical axes so that +1 point up.
|
||||
if (e.Number % 2 == 0)
|
||||
js.SetAxis((JoystickAxis)e.Number, e.Value / 32767.0f);
|
||||
else
|
||||
js.SetAxis((JoystickAxis)e.Number, -e.Value / 32767.0f);
|
||||
break;
|
||||
|
||||
case JoystickEventType.Button:
|
||||
js.SetButton((JoystickButton)e.Number, e.Value != 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Members
|
||||
|
||||
JoystickDevice<X11JoyDetails> OpenJoystick(string base_path, int number)
|
||||
{
|
||||
string path = base_path + number.ToString();
|
||||
JoystickDevice<X11JoyDetails> stick = null;
|
||||
|
||||
int fd = -1;
|
||||
try
|
||||
{
|
||||
fd = UnsafeNativeMethods.open(path, OpenFlags.NonBlock);
|
||||
if (fd == -1)
|
||||
return null;
|
||||
|
||||
// Check joystick driver version (must be 1.0+)
|
||||
int driver_version = 0x00000800;
|
||||
UnsafeNativeMethods.ioctl(fd, JoystickIoctlCode.Version, ref driver_version);
|
||||
if (driver_version < 0x00010000)
|
||||
return null;
|
||||
|
||||
// Get number of joystick axes
|
||||
int axes = 0;
|
||||
UnsafeNativeMethods.ioctl(fd, JoystickIoctlCode.Axes, ref axes);
|
||||
|
||||
// Get number of joystick buttons
|
||||
int buttons = 0;
|
||||
UnsafeNativeMethods.ioctl(fd, JoystickIoctlCode.Buttons, ref buttons);
|
||||
|
||||
stick = new JoystickDevice<X11JoyDetails>(fd, axes, buttons);
|
||||
Debug.Print("Found joystick on path {0}", path);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (stick == null && fd != -1)
|
||||
UnsafeNativeMethods.close(fd);
|
||||
}
|
||||
|
||||
return stick;
|
||||
}
|
||||
|
||||
#region UnsafeNativeMethods
|
||||
|
||||
struct JoystickEvent
|
||||
{
|
||||
public uint Time; // (u32) event timestamp in milliseconds
|
||||
public short Value; // (s16) value
|
||||
public JoystickEventType Type; // (u8) event type
|
||||
public byte Number; // (u8) axis/button number
|
||||
}
|
||||
|
||||
[Flags]
|
||||
enum JoystickEventType : byte
|
||||
{
|
||||
Button = 0x01, // button pressed/released
|
||||
Axis = 0x02, // joystick moved
|
||||
Init = 0x80 // initial state of device
|
||||
}
|
||||
|
||||
enum JoystickIoctlCode : uint
|
||||
{
|
||||
Version = 0x80046a01,
|
||||
Axes = 0x80016a11,
|
||||
Buttons = 0x80016a12
|
||||
}
|
||||
|
||||
static readonly string JoystickPath = "/dev/input/js";
|
||||
static readonly string JoystickPathLegacy = "/dev/js";
|
||||
|
||||
[Flags]
|
||||
enum OpenFlags
|
||||
{
|
||||
NonBlock = 0x00000800
|
||||
}
|
||||
|
||||
static class UnsafeNativeMethods
|
||||
{
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
public static extern int ioctl(int d, JoystickIoctlCode request, ref int data);
|
||||
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
public static extern int open([MarshalAs(UnmanagedType.LPStr)]string pathname, OpenFlags flags);
|
||||
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
public static extern int close(int fd);
|
||||
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
unsafe public static extern IntPtr read(int fd, void* buffer, UIntPtr count);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
void Dispose(bool manual)
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
if (manual)
|
||||
{
|
||||
}
|
||||
|
||||
foreach (JoystickDevice js in sticks)
|
||||
{
|
||||
UnsafeNativeMethods.close(js.Id);
|
||||
}
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
~X11Joystick()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user