From b5591e1eeefa982953cba5ccfff0757b085f1ff0 Mon Sep 17 00:00:00 2001 From: Robert Rouhani Date: Wed, 16 Jan 2013 17:56:31 -0800 Subject: [PATCH] Started implementing GamePad --- Source/OpenTK/Input/IGamePadDriver.cs | 21 ++++++++++ Source/OpenTK/Platform/Factory.cs | 12 +++++- Source/OpenTK/Platform/IPlatformFactory.cs | 2 + Source/OpenTK/Platform/MacOS/HIDInput.cs | 2 +- Source/OpenTK/Platform/MacOS/MacOSFactory.cs | 5 +++ Source/OpenTK/Platform/Windows/WMInput.cs | 4 +- Source/OpenTK/Platform/Windows/WinFactory.cs | 5 +++ .../OpenTK/Platform/Windows/WinMMJoystick.cs | 18 ++++++++- Source/OpenTK/Platform/X11/X11Factory.cs | 5 +++ Source/OpenTK/Platform/X11/X11Joystick.cs | 38 ++++++++++++++++--- 10 files changed, 101 insertions(+), 11 deletions(-) diff --git a/Source/OpenTK/Input/IGamePadDriver.cs b/Source/OpenTK/Input/IGamePadDriver.cs index 0f536dda..3df61309 100644 --- a/Source/OpenTK/Input/IGamePadDriver.cs +++ b/Source/OpenTK/Input/IGamePadDriver.cs @@ -6,5 +6,26 @@ namespace OpenTK.Input { interface IGamePadDriver { + /// + /// Retrieves the combined for all gamepad devices. + /// + /// A structure containing the combined state for all gamepad devices. + GamePadState GetState(); + + /// + /// Retrieves the for the specified gamepad device. + /// + /// The index of the keyboard device. + /// A structure containing the state of the gamepad device. + GamePadState GetState(int index); + + /// + /// Retrieves the device name for the gamepad device. + /// + /// The index of the gamepad device. + /// A with the name of the specified device or . + /// + /// If no device exists at the specified index, the return value is . + string GetDeviceName(int index); } } diff --git a/Source/OpenTK/Platform/Factory.cs b/Source/OpenTK/Platform/Factory.cs index cb9ab3d9..2ce77edf 100644 --- a/Source/OpenTK/Platform/Factory.cs +++ b/Source/OpenTK/Platform/Factory.cs @@ -124,6 +124,11 @@ namespace OpenTK.Platform return default_implementation.CreateMouseDriver(); } + public OpenTK.Input.IGamePadDriver CreateGamePadDriver() + { + return default_implementation.CreateGamePadDriver(); + } + class UnsupportedPlatform : IPlatformFactory { #region Fields @@ -178,7 +183,12 @@ namespace OpenTK.Platform { throw new PlatformNotSupportedException(error_string); } - + + public OpenTK.Input.IGamePadDriver CreateGamePadDriver() + { + throw new PlatformNotSupportedException(error_string); + } + #endregion } diff --git a/Source/OpenTK/Platform/IPlatformFactory.cs b/Source/OpenTK/Platform/IPlatformFactory.cs index 94761623..4b968cc9 100644 --- a/Source/OpenTK/Platform/IPlatformFactory.cs +++ b/Source/OpenTK/Platform/IPlatformFactory.cs @@ -50,5 +50,7 @@ namespace OpenTK.Platform OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver(); OpenTK.Input.IMouseDriver2 CreateMouseDriver(); + + OpenTK.Input.IGamePadDriver CreateGamePadDriver(); } } diff --git a/Source/OpenTK/Platform/MacOS/HIDInput.cs b/Source/OpenTK/Platform/MacOS/HIDInput.cs index 53e28da5..daa88f3f 100755 --- a/Source/OpenTK/Platform/MacOS/HIDInput.cs +++ b/Source/OpenTK/Platform/MacOS/HIDInput.cs @@ -50,7 +50,7 @@ namespace OpenTK.Platform.MacOS // Requires Mac OS X 10.5 or higher. // Todo: create a driver for older installations. Maybe use CGGetLastMouseDelta for that? - class HIDInput : IInputDriver2, IMouseDriver2, IKeyboardDriver2 + class HIDInput : IInputDriver2, IMouseDriver2, IKeyboardDriver2/*, IGamePadDriver*/ { #region Fields diff --git a/Source/OpenTK/Platform/MacOS/MacOSFactory.cs b/Source/OpenTK/Platform/MacOS/MacOSFactory.cs index 066e2879..3bae0c4c 100644 --- a/Source/OpenTK/Platform/MacOS/MacOSFactory.cs +++ b/Source/OpenTK/Platform/MacOS/MacOSFactory.cs @@ -86,6 +86,11 @@ namespace OpenTK.Platform.MacOS { return InputDriver.MouseDriver; } + + public virtual OpenTK.Input.IGamePadDriver CreateGamePadDriver() + { + return InputDriver.GamePadDriver; + } #endregion } diff --git a/Source/OpenTK/Platform/Windows/WMInput.cs b/Source/OpenTK/Platform/Windows/WMInput.cs index 5d3d2ee3..f0fd2956 100644 --- a/Source/OpenTK/Platform/Windows/WMInput.cs +++ b/Source/OpenTK/Platform/Windows/WMInput.cs @@ -42,7 +42,7 @@ 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 + sealed class WMInput : IInputDriver2, IMouseDriver2, IKeyboardDriver2/*, IGamePadDriver*/ //HACK uncomment and implement { #region Fields @@ -111,7 +111,7 @@ namespace OpenTK.Platform.Windows public IGamePadDriver GamePadDriver { - get { return this; } + get { return null; } //HACK return this when implemented. } #endregion diff --git a/Source/OpenTK/Platform/Windows/WinFactory.cs b/Source/OpenTK/Platform/Windows/WinFactory.cs index aa802fdf..c5d8fcc3 100644 --- a/Source/OpenTK/Platform/Windows/WinFactory.cs +++ b/Source/OpenTK/Platform/Windows/WinFactory.cs @@ -83,6 +83,11 @@ namespace OpenTK.Platform.Windows { return InputDriver.MouseDriver; } + + public virtual OpenTK.Input.IGamePadDriver CreateGamePadDriver() + { + return InputDriver.GamePadDriver; + } #endregion diff --git a/Source/OpenTK/Platform/Windows/WinMMJoystick.cs b/Source/OpenTK/Platform/Windows/WinMMJoystick.cs index ea3c2a5e..32a00c7a 100644 --- a/Source/OpenTK/Platform/Windows/WinMMJoystick.cs +++ b/Source/OpenTK/Platform/Windows/WinMMJoystick.cs @@ -87,7 +87,7 @@ namespace OpenTK.Platform.Windows if ((caps.Capabilities & JoystCapsFlags.HasPov) != 0) num_axes += 2; - stick = new JoystickDevice(number, num_axes, caps.NumButtons); + stick = new JoystickDevice(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. @@ -427,5 +427,21 @@ namespace OpenTK.Platform.Windows } #endregion + + //HACK implement + public GamePadState GetState() + { + throw new NotImplementedException(); + } + + public GamePadState GetState(int index) + { + throw new NotImplementedException(); + } + + public string GetDeviceName(int index) + { + throw new NotImplementedException(); + } } } diff --git a/Source/OpenTK/Platform/X11/X11Factory.cs b/Source/OpenTK/Platform/X11/X11Factory.cs index 6fd9c5eb..e437e119 100644 --- a/Source/OpenTK/Platform/X11/X11Factory.cs +++ b/Source/OpenTK/Platform/X11/X11Factory.cs @@ -91,6 +91,11 @@ namespace OpenTK.Platform.X11 return new X11Mouse(); // Always supported. } + public virtual OpenTK.Input.IGamePadDriver CreateGamePadDriver() + { + return new X11Joystick(); + } + #endregion } } diff --git a/Source/OpenTK/Platform/X11/X11Joystick.cs b/Source/OpenTK/Platform/X11/X11Joystick.cs index 0f88d994..08d6ebbf 100644 --- a/Source/OpenTK/Platform/X11/X11Joystick.cs +++ b/Source/OpenTK/Platform/X11/X11Joystick.cs @@ -29,13 +29,14 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; +using System.Text; using OpenTK.Input; namespace OpenTK.Platform.X11 { struct X11JoyDetails { } - sealed class X11Joystick : IJoystickDriver + sealed class X11Joystick : IJoystickDriver, IGamePadDriver { #region Fields @@ -58,8 +59,8 @@ namespace OpenTK.Platform.X11 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); + //stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons, {3}{0})", + //number, stick.Axis.Count, stick.Button.Count, JoystickPath); sticks.Add(stick); } } @@ -70,8 +71,8 @@ namespace OpenTK.Platform.X11 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); + //stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons, {3}{0})", + //number, stick.Axis.Count, stick.Button.Count, JoystickPathLegacy); sticks.Add(stick); } } @@ -153,6 +154,11 @@ namespace OpenTK.Platform.X11 UnsafeNativeMethods.ioctl(fd, JoystickIoctlCode.Buttons, ref buttons); stick = new JoystickDevice(fd, axes, buttons); + + StringBuilder sb = new StringBuilder(128); + UnsafeNativeMethods.ioctl(fd, JoystickIoctlCode.Name128, sb); + stick.Description = sb.ToString(); + Debug.Print("Found joystick on path {0}", path); } finally @@ -186,7 +192,8 @@ namespace OpenTK.Platform.X11 { Version = 0x80046a01, Axes = 0x80016a11, - Buttons = 0x80016a12 + Buttons = 0x80016a12, + Name128 = (2u << 30) | (0x6A << 8) | (0x13 << 0) | (128 << 16) //JSIOCGNAME(128), which is _IOC(_IO_READ, 'j', 0x13, len) } static readonly string JoystickPath = "/dev/input/js"; @@ -203,6 +210,9 @@ namespace OpenTK.Platform.X11 [DllImport("libc", SetLastError = true)] public static extern int ioctl(int d, JoystickIoctlCode request, ref int data); + [DllImport("libc", SetLastError = true)] + public static extern int ioctl(int d, JoystickIoctlCode request, StringBuilder data); + [DllImport("libc", SetLastError = true)] public static extern int open([MarshalAs(UnmanagedType.LPStr)]string pathname, OpenFlags flags); @@ -248,5 +258,21 @@ namespace OpenTK.Platform.X11 } #endregion + + //HACK implement + public GamePadState GetState() + { + throw new NotImplementedException(); + } + + public GamePadState GetState(int index) + { + throw new NotImplementedException(); + } + + public string GetDeviceName(int index) + { + throw new NotImplementedException(); + } } } -- 2.34.1