2013-09-27 21:07:23 +00:00
|
|
|
#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
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-10-01 20:01:27 +00:00
|
|
|
using System.Diagnostics;
|
2013-09-30 10:22:25 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2013-09-27 12:41:37 +00:00
|
|
|
using System.Threading;
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.SDL2
|
|
|
|
{
|
2013-10-04 08:02:19 +00:00
|
|
|
class Sdl2InputDriver : IInputDriver2, IInputDriver
|
2013-09-27 12:41:37 +00:00
|
|
|
{
|
2013-10-01 23:14:26 +00:00
|
|
|
readonly static Dictionary<IntPtr, Sdl2InputDriver> DriverHandles =
|
|
|
|
new Dictionary<IntPtr, Sdl2InputDriver>();
|
|
|
|
|
2013-10-04 08:02:19 +00:00
|
|
|
readonly IntPtr driver_handle;
|
|
|
|
|
2013-09-30 10:22:25 +00:00
|
|
|
readonly Sdl2Keyboard keyboard_driver = new Sdl2Keyboard();
|
|
|
|
readonly Sdl2Mouse mouse_driver = new Sdl2Mouse();
|
2013-10-04 08:02:19 +00:00
|
|
|
readonly Sdl2JoystickDriver joystick_driver = new Sdl2JoystickDriver();
|
|
|
|
|
2013-11-20 08:10:12 +00:00
|
|
|
readonly EventFilter EventFilterDelegate_GCUnsafe = FilterInputEvents;
|
|
|
|
readonly IntPtr EventFilterDelegate;
|
2013-10-01 23:14:26 +00:00
|
|
|
|
|
|
|
static int count;
|
2013-10-01 20:01:27 +00:00
|
|
|
bool disposed;
|
2013-09-27 12:41:37 +00:00
|
|
|
|
2013-09-30 11:13:02 +00:00
|
|
|
public Sdl2InputDriver()
|
2013-09-27 12:41:37 +00:00
|
|
|
{
|
2013-10-01 23:14:26 +00:00
|
|
|
lock (SDL.Sync)
|
|
|
|
{
|
2013-12-30 16:08:42 +00:00
|
|
|
SDL.GameControllerEventState(EventState.Enable);
|
|
|
|
SDL.JoystickEventState(EventState.Enable);
|
|
|
|
|
2013-11-20 08:10:12 +00:00
|
|
|
EventFilterDelegate = Marshal.GetFunctionPointerForDelegate(EventFilterDelegate_GCUnsafe);
|
2013-10-04 08:02:19 +00:00
|
|
|
driver_handle = new IntPtr(count++);
|
2013-10-01 23:14:26 +00:00
|
|
|
DriverHandles.Add(driver_handle, this);
|
2013-10-23 23:45:13 +00:00
|
|
|
SDL.AddEventWatch(EventFilterDelegate, driver_handle);
|
2013-12-22 22:55:28 +00:00
|
|
|
if (SDL.InitSubSystem(SystemFlags.JOYSTICK) < 0)
|
|
|
|
{
|
|
|
|
Debug.Print("[SDL2] InputDriver failed to init Joystick subsystem. Error: {0}", SDL.GetError());
|
|
|
|
}
|
|
|
|
if (SDL.InitSubSystem(SystemFlags.GAMECONTROLLER) < 0)
|
|
|
|
{
|
|
|
|
Debug.Print("[SDL2] InputDriver failed to init GameController subsystem. Error: {0}", SDL.GetError());
|
|
|
|
}
|
2013-10-01 23:14:26 +00:00
|
|
|
}
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#region Private Members
|
|
|
|
|
2013-10-01 23:14:26 +00:00
|
|
|
unsafe static int FilterInputEvents(IntPtr driver_handle, IntPtr e)
|
2013-09-27 12:41:37 +00:00
|
|
|
{
|
2013-10-01 23:14:26 +00:00
|
|
|
try
|
2013-09-27 12:41:37 +00:00
|
|
|
{
|
2013-10-23 23:45:13 +00:00
|
|
|
Event ev = *(Event*)e;
|
2013-09-30 10:22:25 +00:00
|
|
|
|
2013-10-01 23:14:26 +00:00
|
|
|
Sdl2InputDriver driver;
|
|
|
|
if (DriverHandles.TryGetValue(driver_handle, out driver))
|
|
|
|
{
|
2013-10-23 23:45:13 +00:00
|
|
|
switch (ev.Type)
|
2013-10-01 23:14:26 +00:00
|
|
|
{
|
2013-10-23 23:45:13 +00:00
|
|
|
case EventType.KEYDOWN:
|
|
|
|
case EventType.KEYUP:
|
|
|
|
driver.keyboard_driver.ProcessKeyboardEvent(ev.Key);
|
2013-10-01 23:14:26 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-23 23:45:13 +00:00
|
|
|
case EventType.MOUSEBUTTONDOWN:
|
|
|
|
case EventType.MOUSEBUTTONUP:
|
|
|
|
driver.mouse_driver.ProcessMouseEvent(ev.Button);
|
2013-10-01 23:14:26 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-23 23:45:13 +00:00
|
|
|
case EventType.MOUSEMOTION:
|
|
|
|
driver.mouse_driver.ProcessMouseEvent(ev.Motion);
|
2013-10-01 23:14:26 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-23 23:45:13 +00:00
|
|
|
case EventType.MOUSEWHEEL:
|
|
|
|
driver.mouse_driver.ProcessWheelEvent(ev.Wheel);
|
2013-10-01 23:14:26 +00:00
|
|
|
break;
|
2013-12-18 16:16:29 +00:00
|
|
|
|
|
|
|
case EventType.JOYDEVICEADDED:
|
|
|
|
case EventType.JOYDEVICEREMOVED:
|
|
|
|
driver.joystick_driver.ProcessJoystickEvent(ev.JoyDevice);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EventType.JOYAXISMOTION:
|
|
|
|
driver.joystick_driver.ProcessJoystickEvent(ev.JoyAxis);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EventType.JOYBALLMOTION:
|
|
|
|
driver.joystick_driver.ProcessJoystickEvent(ev.JoyBall);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EventType.JOYBUTTONDOWN:
|
|
|
|
case EventType.JOYBUTTONUP:
|
|
|
|
driver.joystick_driver.ProcessJoystickEvent(ev.JoyButton);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EventType.JOYHATMOTION:
|
|
|
|
driver.joystick_driver.ProcessJoystickEvent(ev.JoyHat);
|
|
|
|
break;
|
|
|
|
|
2013-12-31 10:46:40 +00:00
|
|
|
#if USE_SDL2_GAMECONTROLLER
|
2013-12-18 16:16:29 +00:00
|
|
|
case EventType.CONTROLLERDEVICEADDED:
|
|
|
|
case EventType.CONTROLLERDEVICEREMOVED:
|
|
|
|
driver.joystick_driver.ProcessControllerEvent(ev.ControllerDevice);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EventType.CONTROLLERAXISMOTION:
|
|
|
|
driver.joystick_driver.ProcessControllerEvent(ev.ControllerAxis);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EventType.CONTROLLERBUTTONDOWN:
|
|
|
|
case EventType.CONTROLLERBUTTONUP:
|
|
|
|
driver.joystick_driver.ProcessControllerEvent(ev.ControllerButton);
|
|
|
|
break;
|
2013-12-31 10:46:40 +00:00
|
|
|
#endif
|
2013-10-01 23:14:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
2013-09-30 10:22:25 +00:00
|
|
|
{
|
2013-10-01 23:14:26 +00:00
|
|
|
Debug.Print(ex.ToString());
|
2013-09-30 10:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2013-10-04 08:02:19 +00:00
|
|
|
#region IInputDriver Members
|
|
|
|
|
|
|
|
public void Poll()
|
|
|
|
{
|
|
|
|
joystick_driver.Poll();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IJoystickDriver Members
|
|
|
|
|
|
|
|
public IList<JoystickDevice> Joysticks
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return joystick_driver.Joysticks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IMouseDriver Members
|
|
|
|
|
|
|
|
public IList<MouseDevice> Mouse
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return mouse_driver.Mouse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IKeyboardDriver Members
|
|
|
|
|
|
|
|
public IList<KeyboardDevice> Keyboard
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return keyboard_driver.Keyboard;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
#region IInputDriver2 Members
|
|
|
|
|
|
|
|
public IMouseDriver2 MouseDriver
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-09-30 10:22:25 +00:00
|
|
|
return mouse_driver;
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IKeyboardDriver2 KeyboardDriver
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-09-30 10:22:25 +00:00
|
|
|
return keyboard_driver;
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IGamePadDriver GamePadDriver
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-12-18 16:16:29 +00:00
|
|
|
return joystick_driver;
|
2013-12-24 02:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IJoystickDriver2 JoystickDriver
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return joystick_driver;
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2013-10-01 20:01:27 +00:00
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
|
|
|
|
void Dispose(bool manual)
|
|
|
|
{
|
|
|
|
if (!disposed)
|
|
|
|
{
|
|
|
|
if (manual)
|
|
|
|
{
|
2013-10-01 23:14:26 +00:00
|
|
|
Debug.Print("Disposing {0}", GetType());
|
2013-10-04 08:02:19 +00:00
|
|
|
joystick_driver.Dispose();
|
2013-10-01 23:14:26 +00:00
|
|
|
lock (SDL.Sync)
|
|
|
|
{
|
2013-11-20 08:10:12 +00:00
|
|
|
SDL.DelEventWatch(EventFilterDelegate, driver_handle);
|
2013-10-01 23:14:26 +00:00
|
|
|
}
|
2013-10-04 08:02:19 +00:00
|
|
|
DriverHandles.Remove(driver_handle);
|
2013-10-01 20:01:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.WriteLine("Sdl2InputDriver leaked, did you forget to call Dispose()?");
|
|
|
|
}
|
|
|
|
disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Sdl2InputDriver()
|
|
|
|
{
|
|
|
|
Dispose(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|