diff --git a/Source/OpenTK/Input/KeyboardState.cs b/Source/OpenTK/Input/KeyboardState.cs index 15c42ade..49860eb0 100644 --- a/Source/OpenTK/Input/KeyboardState.cs +++ b/Source/OpenTK/Input/KeyboardState.cs @@ -67,8 +67,8 @@ namespace OpenTK.Input /// Gets a indicating whether the specified /// is pressed. /// - /// The to check. - /// True if key is pressed; false otherwise. + /// The scancode to check. + /// True if code is pressed; false otherwise. public bool this[short code] { get { return IsKeyDown(code); } diff --git a/Source/OpenTK/OpenTK.csproj b/Source/OpenTK/OpenTK.csproj index 2e8f4bff..76716737 100644 --- a/Source/OpenTK/OpenTK.csproj +++ b/Source/OpenTK/OpenTK.csproj @@ -790,7 +790,7 @@ - + @@ -806,7 +806,5 @@ - - - + \ No newline at end of file diff --git a/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs b/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs index 4ac2eda3..9113311c 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs @@ -33,6 +33,8 @@ namespace OpenTK.Platform.SDL2 { class Sdl2Factory : IPlatformFactory { + readonly IInputDriver2 InputDriver = new Sdl2InputDriver(); + public Sdl2Factory() { } @@ -86,17 +88,17 @@ namespace OpenTK.Platform.SDL2 public IKeyboardDriver2 CreateKeyboardDriver() { - return new Sdl2Keyboard(); + return InputDriver.KeyboardDriver; } public IMouseDriver2 CreateMouseDriver() { - return new Sdl2Mouse(); + return InputDriver.MouseDriver; } public IGamePadDriver CreateGamePadDriver() { - return new Sdl2Joystick(); + return InputDriver.GamePadDriver; } #endregion diff --git a/Source/OpenTK/Platform/SDL2/Sdl2InputBase.cs b/Source/OpenTK/Platform/SDL2/Sdl2InputDriver.cs similarity index 56% rename from Source/OpenTK/Platform/SDL2/Sdl2InputBase.cs rename to Source/OpenTK/Platform/SDL2/Sdl2InputDriver.cs index 178b1656..cffb38a3 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2InputBase.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2InputDriver.cs @@ -27,33 +27,57 @@ using System; using System.Collections.Generic; +using System.Runtime.InteropServices; using System.Threading; using OpenTK.Input; namespace OpenTK.Platform.SDL2 { - class Sdl2InputBase : IInputDriver2 + class Sdl2InputDriver : IInputDriver2 { - Sdl2WindowInfo window; - readonly Thread thread; - readonly AutoResetEvent ready = new AutoResetEvent(false); + readonly Sdl2Keyboard keyboard_driver = new Sdl2Keyboard(); + readonly Sdl2Mouse mouse_driver = new Sdl2Mouse(); + readonly SDL.SDL_EventFilter EventFilterDelegate; - public Sdl2InputBase() + public Sdl2InputDriver() { - window = new Sdl2WindowInfo( - SDL.SDL_CreateWindow("Hidden Input Window", 0, 0, 1, 1, - SDL.SDL_WindowFlags.SDL_WINDOW_HIDDEN), - null); + EventFilterDelegate = FilterInputEvents; + SDL.SDL_AddEventWatch(EventFilterDelegate, IntPtr.Zero); } #region Private Members - void ProcessEvents() + int FilterInputEvents(IntPtr user_data, IntPtr e) { - SDL.SDL_Event e; - while (SDL.SDL_WaitEvent(out e) != 0) + SDL.SDL_Event ev; + unsafe { + ev = *(SDL.SDL_Event*)e; } + + var type = (SDL.SDL_EventType)Marshal.ReadInt32(e); + switch (type) + { + case SDL.SDL_EventType.SDL_KEYDOWN: + case SDL.SDL_EventType.SDL_KEYUP: + keyboard_driver.ProcessKeyboardEvent(ev.key); + break; + + case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: + case SDL.SDL_EventType.SDL_MOUSEBUTTONUP: + mouse_driver.ProcessMouseEvent(ev.button); + break; + + case SDL.SDL_EventType.SDL_MOUSEMOTION: + mouse_driver.ProcessMouseEvent(ev.motion); + break; + + case SDL.SDL_EventType.SDL_MOUSEWHEEL: + mouse_driver.ProcessWheelEvent(ev.wheel); + break; + } + + return 0; } #endregion @@ -64,7 +88,7 @@ namespace OpenTK.Platform.SDL2 { get { - throw new NotImplementedException(); + return mouse_driver; } } @@ -72,7 +96,7 @@ namespace OpenTK.Platform.SDL2 { get { - throw new NotImplementedException(); + return keyboard_driver; } } diff --git a/Source/OpenTK/Platform/SDL2/Sdl2KeyMap.cs b/Source/OpenTK/Platform/SDL2/Sdl2KeyMap.cs index 3f3e2ab8..1e633825 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2KeyMap.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2KeyMap.cs @@ -31,92 +31,93 @@ using OpenTK.Input; namespace OpenTK.Platform.SDL2 { - using Code = SDL.SDL_Keycode; + using Code = SDL.SDL_Scancode; - class Sdl2KeyMap : Dictionary + class Sdl2KeyMap : Dictionary { public Sdl2KeyMap() { - Add(Code.SDLK_ESCAPE, Key.Escape); + Add(Code.SDL_SCANCODE_ESCAPE, Key.Escape); // Function keys for (int i = 0; i < 12; i++) { - Add(Code.SDLK_F1 + i, Key.F1 + i); + Add(Code.SDL_SCANCODE_F1 + i, Key.F1 + i); } // Number keys (0-9) - for (int i = 0; i <= 9; i++) + Add(Code.SDL_SCANCODE_0, Key.Number0); + for (int i = 1; i < 9; i++) { - Add(Code.SDLK_0 + i, Key.Number0 + i); + Add(Code.SDL_SCANCODE_1 + i, Key.Number1 + i); } // Letters (A-Z) for (int i = 0; i < 26; i++) { - Add(Code.SDLK_a + i, Key.A + i); + Add(Code.SDL_SCANCODE_A + i, Key.A + i); } - Add(Code.SDLK_TAB, Key.Tab); - Add(Code.SDLK_CAPSLOCK, Key.CapsLock); - Add(Code.SDLK_LCTRL, Key.ControlLeft); - Add(Code.SDLK_LSHIFT, Key.ShiftLeft); - Add(Code.SDLK_LALT, Key.WinLeft); - Add(Code.SDLK_MENU, Key.AltLeft); - Add(Code.SDLK_SPACE, Key.Space); - Add(Code.SDLK_RALT, Key.AltRight); + Add(Code.SDL_SCANCODE_TAB, Key.Tab); + Add(Code.SDL_SCANCODE_CAPSLOCK, Key.CapsLock); + Add(Code.SDL_SCANCODE_LCTRL, Key.ControlLeft); + Add(Code.SDL_SCANCODE_LSHIFT, Key.ShiftLeft); + Add(Code.SDL_SCANCODE_LALT, Key.WinLeft); + Add(Code.SDL_SCANCODE_MENU, Key.AltLeft); + Add(Code.SDL_SCANCODE_SPACE, Key.Space); + Add(Code.SDL_SCANCODE_RALT, Key.AltRight); //Add(Code., Key.WinRight); - Add(Code.SDLK_APPLICATION, Key.Menu); - Add(Code.SDLK_RCTRL, Key.ControlRight); - Add(Code.SDLK_RSHIFT, Key.ShiftRight); - Add(Code.SDLK_RETURN, Key.Enter); - Add(Code.SDLK_BACKSPACE, Key.BackSpace); + Add(Code.SDL_SCANCODE_APPLICATION, Key.Menu); + Add(Code.SDL_SCANCODE_RCTRL, Key.ControlRight); + Add(Code.SDL_SCANCODE_RSHIFT, Key.ShiftRight); + Add(Code.SDL_SCANCODE_RETURN, Key.Enter); + Add(Code.SDL_SCANCODE_BACKSPACE, Key.BackSpace); - Add(Code.SDLK_SEMICOLON, Key.Semicolon); // Varies by keyboard, ;: on Win2K/US - Add(Code.SDLK_SLASH, Key.Slash); // Varies by keyboard, /? on Win2K/US + Add(Code.SDL_SCANCODE_SEMICOLON, Key.Semicolon); // Varies by keyboard, ;: on Win2K/US + Add(Code.SDL_SCANCODE_SLASH, Key.Slash); // Varies by keyboard, /? on Win2K/US //Add(Code., Key.Tilde); // Varies by keyboard, `~ on Win2K/US - Add(Code.SDLK_LEFTBRACKET, Key.BracketLeft); // Varies by keyboard, [{ on Win2K/US - Add(Code.SDLK_BACKSLASH, Key.BackSlash); // Varies by keyboard, \| on Win2K/US - Add(Code.SDLK_RIGHTBRACKET, Key.BracketRight); // Varies by keyboard, ]} on Win2K/US - Add(Code.SDLK_QUOTE, Key.Quote); // Varies by keyboard, '" on Win2K/US - Add(Code.SDLK_PLUS, Key.Plus); // Invariant: + - Add(Code.SDLK_COMMA, Key.Comma); // Invariant: , - Add(Code.SDLK_MINUS, Key.Minus); // Invariant: - - Add(Code.SDLK_PERIOD, Key.Period); // Invariant: . + Add(Code.SDL_SCANCODE_LEFTBRACKET, Key.BracketLeft); // Varies by keyboard, [{ on Win2K/US + Add(Code.SDL_SCANCODE_BACKSLASH, Key.BackSlash); // Varies by keyboard, \| on Win2K/US + Add(Code.SDL_SCANCODE_RIGHTBRACKET, Key.BracketRight); // Varies by keyboard, ]} on Win2K/US + Add(Code.SDL_SCANCODE_APOSTROPHE, Key.Quote); // Varies by keyboard, '" on Win2K/US + Add(Code.SDL_SCANCODE_EQUALS, Key.Plus); + Add(Code.SDL_SCANCODE_COMMA, Key.Comma); // Invariant: , + Add(Code.SDL_SCANCODE_MINUS, Key.Minus); // Invariant: - + Add(Code.SDL_SCANCODE_PERIOD, Key.Period); // Invariant: . - Add(Code.SDLK_HOME, Key.Home); - Add(Code.SDLK_END, Key.End); - Add(Code.SDLK_DELETE, Key.Delete); - Add(Code.SDLK_PAGEUP, Key.PageUp); - Add(Code.SDLK_PAGEDOWN, Key.PageDown); - Add(Code.SDLK_PAUSE, Key.Pause); - Add(Code.SDLK_NUMLOCKCLEAR, Key.NumLock); + Add(Code.SDL_SCANCODE_HOME, Key.Home); + Add(Code.SDL_SCANCODE_END, Key.End); + Add(Code.SDL_SCANCODE_DELETE, Key.Delete); + Add(Code.SDL_SCANCODE_PAGEUP, Key.PageUp); + Add(Code.SDL_SCANCODE_PAGEDOWN, Key.PageDown); + Add(Code.SDL_SCANCODE_PAUSE, Key.Pause); + Add(Code.SDL_SCANCODE_NUMLOCKCLEAR, Key.NumLock); - Add(Code.SDLK_SCROLLLOCK, Key.ScrollLock); - Add(Code.SDLK_PRINTSCREEN, Key.PrintScreen); - Add(Code.SDLK_CLEAR, Key.Clear); - Add(Code.SDLK_INSERT, Key.Insert); + Add(Code.SDL_SCANCODE_SCROLLLOCK, Key.ScrollLock); + Add(Code.SDL_SCANCODE_PRINTSCREEN, Key.PrintScreen); + Add(Code.SDL_SCANCODE_CLEAR, Key.Clear); + Add(Code.SDL_SCANCODE_INSERT, Key.Insert); - Add(Code.SDLK_SLEEP, Key.Sleep); + Add(Code.SDL_SCANCODE_SLEEP, Key.Sleep); // Keypad for (int i = 0; i < 9; i++) { - Add(Code.SDLK_KP_1 + i, Key.Keypad1 + i); + Add(Code.SDL_SCANCODE_KP_1 + i, Key.Keypad1 + i); } - Add(Code.SDLK_KP_0, Key.Keypad0); // Note: SDL2 goes KP_1..KP_9, then KP_0 + Add(Code.SDL_SCANCODE_KP_0, Key.Keypad0); // Note: SDL2 goes KP_1..KP_9, then KP_0 - Add(Code.SDLK_KP_DECIMAL, Key.KeypadDecimal); - Add(Code.SDLK_KP_PLUS, Key.KeypadAdd); - Add(Code.SDLK_KP_MINUS, Key.KeypadSubtract); - Add(Code.SDLK_KP_DIVIDE, Key.KeypadDivide); - Add(Code.SDLK_KP_MULTIPLY, Key.KeypadMultiply); + Add(Code.SDL_SCANCODE_KP_DECIMAL, Key.KeypadDecimal); + Add(Code.SDL_SCANCODE_KP_PLUS, Key.KeypadAdd); + Add(Code.SDL_SCANCODE_KP_MINUS, Key.KeypadSubtract); + Add(Code.SDL_SCANCODE_KP_DIVIDE, Key.KeypadDivide); + Add(Code.SDL_SCANCODE_KP_MULTIPLY, Key.KeypadMultiply); // Navigation - Add(Code.SDLK_UP, Key.Up); - Add(Code.SDLK_DOWN, Key.Down); - Add(Code.SDLK_LEFT, Key.Left); - Add(Code.SDLK_RIGHT, Key.Right); + Add(Code.SDL_SCANCODE_UP, Key.Up); + Add(Code.SDL_SCANCODE_DOWN, Key.Down); + Add(Code.SDL_SCANCODE_LEFT, Key.Left); + Add(Code.SDL_SCANCODE_RIGHT, Key.Right); } } } diff --git a/Source/OpenTK/Platform/SDL2/Sdl2Keyboard.cs b/Source/OpenTK/Platform/SDL2/Sdl2Keyboard.cs index e81ac048..20693083 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2Keyboard.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2Keyboard.cs @@ -32,25 +32,76 @@ namespace OpenTK.Platform.SDL2 { class Sdl2Keyboard : IKeyboardDriver2 { + static readonly Sdl2KeyMap KeyMap = new Sdl2KeyMap(); + KeyboardState state; + public Sdl2Keyboard() { + state.IsConnected = true; } + #region Private Members + + // Unfortunately, SDL does not report KeyDown events + // when a modifier (e.g. shift, alt, etc) is first pressed. + // It reports a keydown+keyup event pair when the modifier + // is *released* - which means that we cannot use modifiers + // for regular input (e.g. press control to fire a weapon.) + // For that reason, we should also poll the keyboard directly + // as necessary. + // Fixme: this does not appear to work as expected. + void UpdateModifiers() + { + SDL.SDL_Keymod mod = SDL.SDL_GetModState(); + + state.SetKeyState(Key.LAlt, (byte)SDL.SDL_Scancode.SDL_SCANCODE_LALT, (mod & SDL.SDL_Keymod.KMOD_LALT) != 0); + state.SetKeyState(Key.RAlt, (byte)SDL.SDL_Scancode.SDL_SCANCODE_RALT, (mod & SDL.SDL_Keymod.KMOD_RALT) != 0); + state.SetKeyState(Key.LControl, (byte)SDL.SDL_Scancode.SDL_SCANCODE_LCTRL, (mod & SDL.SDL_Keymod.KMOD_LCTRL) != 0); + state.SetKeyState(Key.RControl, (byte)SDL.SDL_Scancode.SDL_SCANCODE_RCTRL, (mod & SDL.SDL_Keymod.KMOD_RCTRL) != 0); + state.SetKeyState(Key.LShift, (byte)SDL.SDL_Scancode.SDL_SCANCODE_LSHIFT, (mod & SDL.SDL_Keymod.KMOD_LSHIFT) != 0); + state.SetKeyState(Key.RShift, (byte)SDL.SDL_Scancode.SDL_SCANCODE_RSHIFT, (mod & SDL.SDL_Keymod.KMOD_RSHIFT) != 0); + state.SetKeyState(Key.Menu, (byte)SDL.SDL_Scancode.SDL_SCANCODE_APPLICATION, (mod & SDL.SDL_Keymod.KMOD_GUI) != 0); + state.SetKeyState(Key.CapsLock, (byte)SDL.SDL_Scancode.SDL_SCANCODE_CAPSLOCK, (mod & SDL.SDL_Keymod.KMOD_CAPS) != 0); + state.SetKeyState(Key.NumLock, (byte)SDL.SDL_Scancode.SDL_SCANCODE_NUMLOCKCLEAR, (mod & SDL.SDL_Keymod.KMOD_NUM) != 0); + //state.SetKeyState(Key., (byte)SDL.SDL_Scancode.SDL_SCANCODE_MODE, (mod & SDL.SDL_Keymod.KMOD_MODE) != 0); + } + + #endregion + + #region Internal Members + + internal void ProcessKeyboardEvent(SDL.SDL_KeyboardEvent e) + { + Key key; + bool pressed = e.state != 0; + if (KeyMap.TryGetValue(e.keysym.scancode, out key)) + { + state.SetKeyState(key, (byte)e.keysym.scancode, pressed); + } + } + + #endregion + #region IKeyboardDriver2 Members public KeyboardState GetState() { - return new KeyboardState(); + //UpdateModifiers(); // Fixme + return state; } public KeyboardState GetState(int index) { - return new KeyboardState(); + //UpdateModifiers(); // Fixme + if (index == 0) + return state; + else + return new KeyboardState(); } public string GetDeviceName(int index) { - return String.Empty; + return "SDL2 Default Keyboard"; } #endregion diff --git a/Source/OpenTK/Platform/SDL2/Sdl2Mouse.cs b/Source/OpenTK/Platform/SDL2/Sdl2Mouse.cs index 882b0e2c..b58fefa7 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2Mouse.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2Mouse.cs @@ -26,33 +26,100 @@ #endregion using System; +using System.Diagnostics; using OpenTK.Input; namespace OpenTK.Platform.SDL2 { class Sdl2Mouse : IMouseDriver2 { + MouseState state; + public Sdl2Mouse() { + state.IsConnected = true; } + #region Private Members + + MouseButton TranslateButton(uint button) + { + switch (button) + { + case SDL.SDL_BUTTON_LEFT: + return MouseButton.Left; + + case SDL.SDL_BUTTON_RIGHT: + return MouseButton.Right; + + case SDL.SDL_BUTTON_MIDDLE: + return MouseButton.Middle; + + case SDL.SDL_BUTTON_X1: + return MouseButton.Button1; + + case SDL.SDL_BUTTON_X2: + return MouseButton.Button2; + + default: + Debug.Print("SDL2 unknown button {0}", button); + return MouseButton.Left; + } + } + + void SetButtonState(MouseButton button, bool pressed) + { + if (pressed) + { + state.EnableBit((int)button); + } + else + { + state.DisableBit((int)button); + } + } + + #endregion + + #region Public Members + + public void ProcessWheelEvent(SDL.SDL_MouseWheelEvent wheel) + { + state.WheelPrecise += wheel.y; + } + + public void ProcessMouseEvent(SDL.SDL_MouseMotionEvent motion) + { + state.X += motion.xrel; + state.Y += motion.yrel; + } + + public void ProcessMouseEvent(SDL.SDL_MouseButtonEvent button) + { + bool pressed = button.state == SDL.SDL_PRESSED; + SetButtonState(TranslateButton(button.button), pressed); + } + + #endregion + #region IMouseDriver2 Members public MouseState GetState() { - int x, y; - uint b = SDL.SDL_GetMouseState(out x, out y); - return new MouseState(); + return state; } public MouseState GetState(int index) { - return new MouseState(); + if (index == 0) + return state; + else + return new MouseState(); } public void SetPosition(double x, double y) { - //SDL2.mouse + SDL.SDL_WarpMouseInWindow(IntPtr.Zero, (int)x, (int)y); } #endregion diff --git a/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs b/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs index bcd5dd18..8e1142c4 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs @@ -65,7 +65,7 @@ namespace OpenTK.Platform.SDL2 static Sdl2NativeWindow() { // store the filter delegate to protect it from the GC - SDL.SDL_SetEventFilter(EventFilterDelegate, IntPtr.Zero); + SDL.SDL_AddEventWatch(EventFilterDelegate, IntPtr.Zero); } public Sdl2NativeWindow(int x, int y, int width, int height, @@ -114,16 +114,22 @@ namespace OpenTK.Platform.SDL2 } } - static Key TranslateKey(SDL.SDL_Keycode key) + static Key TranslateKey(SDL.SDL_Scancode scan) { Key result = Key.Unknown; - if (map.ContainsKey(key)) + if (map.ContainsKey(scan)) { - result = map[key]; + result = map[scan]; } return result; } + static Key TranslateKey(SDL.SDL_Keycode key) + { + SDL.SDL_Scancode scan = SDL.SDL_GetScancodeFromKey(key); + return TranslateKey(scan); + } + static int FilterEvents(IntPtr user_data, IntPtr e) { var type = (SDL.SDL_EventType)Marshal.ReadInt32(e); @@ -324,7 +330,7 @@ namespace OpenTK.Platform.SDL2 case SDL.SDL_EventType.SDL_KEYUP: bool key_pressed = e.key.state == SDL.SDL_PRESSED; var key = e.key.keysym; - keyboard.SetKey(TranslateKey(key.sym), (uint)key.scancode, key_pressed); + keyboard.SetKey(TranslateKey(key.scancode), (uint)key.scancode, key_pressed); break; case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: diff --git a/Source/OpenTK/Platform/SDL2/sdl2-cs/src/SDL2.cs b/Source/OpenTK/Platform/SDL2/sdl2-cs/src/SDL2.cs index bd389ace..e4c3a348 100644 --- a/Source/OpenTK/Platform/SDL2/sdl2-cs/src/SDL2.cs +++ b/Source/OpenTK/Platform/SDL2/sdl2-cs/src/SDL2.cs @@ -36,7 +36,7 @@ namespace OpenTK.Platform.SDL2 /// /// Entry point for all SDL-related (non-extension) types and methods /// - public static class SDL + static class SDL { #region SDL2# Variables @@ -4047,11 +4047,11 @@ namespace OpenTK.Platform.SDL2 * with the current keyboard layout. */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void SDL_GetKeyFromScancode(SDL_Scancode scancode); + public static extern SDL_Keycode SDL_GetKeyFromScancode(SDL_Scancode scancode); /* Get the scancode for the given keycode */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void SDL_GetScancodeFromKey(SDL_Keycode key); + public static extern SDL_Scancode SDL_GetScancodeFromKey(SDL_Keycode key); /* Wrapper for SDL_GetScancodeName */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]