From af67a967591340a9bac901abf35ad810df57b52e Mon Sep 17 00:00:00 2001 From: Stefanos A Date: Fri, 4 Oct 2013 01:39:22 +0200 Subject: [PATCH] Initial implementation of IJoystickDriver API --- Source/OpenTK/OpenTK.csproj | 2 +- Source/OpenTK/Platform/SDL2/Sdl2Joystick.cs | 76 ---------- .../Platform/SDL2/Sdl2JoystickDriver.cs | 137 ++++++++++++++++++ 3 files changed, 138 insertions(+), 77 deletions(-) delete mode 100644 Source/OpenTK/Platform/SDL2/Sdl2Joystick.cs create mode 100644 Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs diff --git a/Source/OpenTK/OpenTK.csproj b/Source/OpenTK/OpenTK.csproj index fb5ce9f0..d5473991 100644 --- a/Source/OpenTK/OpenTK.csproj +++ b/Source/OpenTK/OpenTK.csproj @@ -791,7 +791,6 @@ - @@ -800,6 +799,7 @@ + diff --git a/Source/OpenTK/Platform/SDL2/Sdl2Joystick.cs b/Source/OpenTK/Platform/SDL2/Sdl2Joystick.cs deleted file mode 100644 index d407faf3..00000000 --- a/Source/OpenTK/Platform/SDL2/Sdl2Joystick.cs +++ /dev/null @@ -1,76 +0,0 @@ -#region License -// -// The Open Toolkit Library License -// -// Copyright (c) 2006 - 2013 Stefanos Apostolopoulos for 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.SDL2 -{ - class Sdl2Joystick : IJoystickDriver, IGamePadDriver - { - readonly List joysticks = new List(); - IList joysticks_readonly; - - public Sdl2Joystick() - { - joysticks_readonly = joysticks.AsReadOnly(); - } - - #region IJoystickDriver Members - - public IList Joysticks - { - get - { - return joysticks_readonly; - } - } - - #endregion - - #region IGamePadDriver Members - - public GamePadState GetState() - { - throw new NotImplementedException(); - } - - public GamePadState GetState(int index) - { - throw new NotImplementedException(); - } - - public string GetDeviceName(int index) - { - throw new NotImplementedException(); - } - - #endregion - } -} - diff --git a/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs b/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs new file mode 100644 index 00000000..3fc0a9a4 --- /dev/null +++ b/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs @@ -0,0 +1,137 @@ +#region License +// +// The Open Toolkit Library License +// +// Copyright (c) 2006 - 2013 Stefanos Apostolopoulos for 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.SDL2 +{ + using Sdl2Joystick = JoystickDevice; + + class Sdl2JoystickDetails + { + public IntPtr Handle { get; set; } + public float RangeMultiplier { get { return 1.0f / 32768.0f; } } + } + + class Sdl2JoystickDriver : IJoystickDriver, IGamePadDriver + { + readonly List joysticks = new List(); + IList joysticks_readonly; + + public Sdl2JoystickDriver() + { + joysticks_readonly = (IList)joysticks.AsReadOnly(); + + } + + #region Private Members + + void RefreshJoysticks() + { + joysticks.Clear(); + + int count = SDL.SDL_NumJoysticks(); + for (int i = 0; i < count; i++) + { + JoystickDevice joystick = null; + int num_axes = 0; + int num_buttons = 0; + IntPtr handle = SDL.SDL_JoystickOpen(i); + if (handle != IntPtr.Zero) + { + num_axes = SDL.SDL_JoystickNumAxes(handle); + num_buttons = SDL.SDL_JoystickNumButtons(handle); + joystick = new JoystickDevice(i, num_axes, num_buttons); + joysticks.Add(joystick); + } + } + + } + + #endregion + + #region IJoystickDriver Members + + public IList Joysticks + { + get + { + return joysticks_readonly; + } + } + + public void Poll() + { + SDL.SDL_JoystickUpdate(); + foreach (Sdl2Joystick joystick in joysticks) + { + IntPtr handle = joystick.Details.Handle; + + for (int i = 0; i < joystick.Axis.Count; i++) + { + var axis = JoystickAxis.Axis0 + i; + joystick.SetAxis(axis, SDL.SDL_JoystickGetAxis(handle, i) * joystick.Details.RangeMultiplier); + } + + for (int i = 0; i < joystick.Button.Count; i++) + { + var button = JoystickButton.Button0 + i; + joystick.SetButton(button, SDL.SDL_JoystickGetButton(handle, i) != 0); + } + } + } + + #endregion + + #region IGamePadDriver Members + + public GamePadState GetState() + { + return new GamePadState(); + } + + public GamePadState GetState(int index) + { + if (joysticks.Count >= index) + { + + } + + return new GamePadState(); + } + + public string GetDeviceName(int index) + { + return String.Empty; + } + + #endregion + } +} +