From ff46455e562b09c7698f2f55e33c21b2c5090eb8 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Sun, 4 May 2014 08:32:08 +0200 Subject: [PATCH] [X11] Removed legacy X11Input driver Its functionality has been moved directly into X11GLNative and X11KeyMap. --- Source/OpenTK/OpenTK.csproj | 3 - Source/OpenTK/Platform/X11/X11GLNative.cs | 107 ++++++---- Source/OpenTK/Platform/X11/X11Input.cs | 235 ---------------------- Source/OpenTK/Platform/X11/X11KeyMap.cs | 17 ++ 4 files changed, 81 insertions(+), 281 deletions(-) delete mode 100644 Source/OpenTK/Platform/X11/X11Input.cs diff --git a/Source/OpenTK/OpenTK.csproj b/Source/OpenTK/OpenTK.csproj index f82f6998..2d033d82 100644 --- a/Source/OpenTK/OpenTK.csproj +++ b/Source/OpenTK/OpenTK.csproj @@ -366,9 +366,6 @@ Code - - Code - Code diff --git a/Source/OpenTK/Platform/X11/X11GLNative.cs b/Source/OpenTK/Platform/X11/X11GLNative.cs index 4109da30..f52ea305 100644 --- a/Source/OpenTK/Platform/X11/X11GLNative.cs +++ b/Source/OpenTK/Platform/X11/X11GLNative.cs @@ -57,11 +57,6 @@ namespace OpenTK.Platform.X11 X11WindowInfo window = new X11WindowInfo(); - // Legacy input support - X11Input driver; - KeyboardDevice keyboard; - MouseDevice mouse; - // Window manager hints for fullscreen windows. // Not used right now (the code is written, but is not 64bit-correct), but could be useful for older WMs which // are not ICCM compliant, but may support MOTIF hints. @@ -124,9 +119,6 @@ namespace OpenTK.Platform.X11 // Keyboard input readonly byte[] ascii = new byte[16]; readonly char[] chars = new char[16]; - readonly KeyPressEventArgs KPEventArgs = new KeyPressEventArgs('\0'); - readonly KeyboardKeyEventArgs KeyDownEventArgs = new KeyboardKeyEventArgs(); - readonly KeyboardKeyEventArgs KeyUpEventArgs = new KeyboardKeyEventArgs(); readonly IntPtr EmptyCursor; @@ -224,15 +216,18 @@ namespace OpenTK.Platform.X11 e.ConfigureEvent.height = height; RefreshWindowBounds(ref e); - driver = new X11Input(window); - keyboard = driver.Keyboard[0]; - mouse = driver.Mouse[0]; - EmptyCursor = CreateEmptyCursor(window); Debug.WriteLine(String.Format("X11GLNative window created successfully (id: {0}).", Handle)); Debug.Unindent(); + // Request that auto-repeat is only set on devices that support it physically. + // This typically means that it's turned off for keyboards (which is what we want). + // We prefer this method over XAutoRepeatOff/On, because the latter needs to + // be reset before the program exits. + bool supported; + Functions.XkbSetDetectableAutoRepeat(window.Display, true, out supported); + exists = true; } @@ -768,7 +763,7 @@ namespace OpenTK.Platform.X11 return cursor; } - static void SetMouseClamped(MouseDevice mouse, int x, int y, + void SetMouseClamped(int x, int y, int left, int top, int width, int height) { // Clamp mouse to the specified rectangle. @@ -776,7 +771,8 @@ namespace OpenTK.Platform.X11 x = Math.Min(x, width); y = Math.Max(y, top); y = Math.Min(y, height); - mouse.Position = new Point(x, y); + MouseState.X = x; + MouseState.Y = y; } #endregion @@ -860,23 +856,23 @@ namespace OpenTK.Platform.X11 case XEventName.KeyRelease: bool pressed = e.type == XEventName.KeyPress; Key key; - if (driver.TranslateKey(ref e.KeyEvent, out key)) + if (X11KeyMap.TranslateKey(ref e.KeyEvent, out key)) { if (pressed) { // Raise KeyDown event - KeyDownEventArgs.Key = key; - KeyDownEventArgs.ScanCode = (uint)e.KeyEvent.keycode; - KeyDownEventArgs.Modifiers = keyboard.GetModifiers(); - KeyDown(this, KeyDownEventArgs); + KeyDownArgs.Key = key; + KeyDownArgs.ScanCode = (uint)e.KeyEvent.keycode; + //KeyDownArgs.Modifiers = keyboard.GetModifiers(); + OnKeyDown(KeyDownArgs); } else { // Raise KeyUp event - KeyUpEventArgs.Key = key; - KeyUpEventArgs.ScanCode = (uint)e.KeyEvent.keycode; - KeyUpEventArgs.Modifiers = keyboard.GetModifiers(); - KeyUp(this, KeyUpEventArgs); + KeyUpArgs.Key = key; + KeyUpArgs.ScanCode = (uint)e.KeyEvent.keycode; + //KeyUpArgs.Modifiers = keyboard.GetModifiers(); + OnKeyUp(KeyUpArgs); } if (pressed) @@ -892,8 +888,8 @@ namespace OpenTK.Platform.X11 { if (!Char.IsControl(chars[i])) { - KPEventArgs.KeyChar = chars[i]; - KeyPress(this, KPEventArgs); + KeyPressArgs.KeyChar = chars[i]; + OnKeyPress(KeyPressArgs); } } } @@ -907,7 +903,7 @@ namespace OpenTK.Platform.X11 // to the dead center of the window. Fortunately, this situation // is very very uncommon. Todo: Can this be remedied? int x = e.MotionEvent.x; - int y =e.MotionEvent.y; + int y = e.MotionEvent.y; // TODO: Have offset as a stored field, only update it when the window moves // The middle point cannot be the average of the Bounds.left/right/top/bottom, // because these fields take into account window decoration (borders, etc), @@ -926,9 +922,9 @@ namespace OpenTK.Platform.X11 } else if (!CursorVisible) { - SetMouseClamped(mouse, - mouse.X + x - mouse_rel_x, - mouse.Y + y - mouse_rel_y, + SetMouseClamped( + MouseState.X + x - mouse_rel_x, + MouseState.Y + y - mouse_rel_y, 0, 0, Width, Height); mouse_rel_x = x; mouse_rel_y = y; @@ -939,16 +935,53 @@ namespace OpenTK.Platform.X11 } else { - SetMouseClamped(mouse, x, y, 0, 0, Width, Height); + SetMouseClamped(x, y, 0, 0, Width, Height); mouse_rel_x = x; mouse_rel_y = y; } + + OnMouseMove(); break; } case XEventName.ButtonPress: + switch (e.ButtonEvent.button) + { + case 1: MouseState.EnableBit((int)MouseButton.Left); break; + case 2: MouseState.EnableBit((int)MouseButton.Middle); break; + case 3: MouseState.EnableBit((int)MouseButton.Right); break; + case 4: MouseState.SetScrollRelative(0, 1); break; + case 5: MouseState.SetScrollRelative(0, -1); break; + case 6: MouseState.EnableBit((int)MouseButton.Button1); break; + case 7: MouseState.EnableBit((int)MouseButton.Button2); break; + case 8: MouseState.EnableBit((int)MouseButton.Button3); break; + case 9: MouseState.EnableBit((int)MouseButton.Button4); break; + case 10: MouseState.EnableBit((int)MouseButton.Button5); break; + case 11: MouseState.EnableBit((int)MouseButton.Button6); break; + case 12: MouseState.EnableBit((int)MouseButton.Button7); break; + case 13: MouseState.EnableBit((int)MouseButton.Button8); break; + case 14: MouseState.EnableBit((int)MouseButton.Button9); break; + } + OnMouseDown(); + break; + case XEventName.ButtonRelease: - driver.ProcessEvent(ref e); + switch (e.ButtonEvent.button) + { + case 1: MouseState.DisableBit((int)MouseButton.Left); break; + case 2: MouseState.DisableBit((int)MouseButton.Middle); break; + case 3: MouseState.DisableBit((int)MouseButton.Right); break; + case 6: MouseState.DisableBit((int)MouseButton.Button1); break; + case 7: MouseState.DisableBit((int)MouseButton.Button2); break; + case 8: MouseState.DisableBit((int)MouseButton.Button3); break; + case 9: MouseState.DisableBit((int)MouseButton.Button4); break; + case 10: MouseState.DisableBit((int)MouseButton.Button5); break; + case 11: MouseState.DisableBit((int)MouseButton.Button6); break; + case 12: MouseState.DisableBit((int)MouseButton.Button7); break; + case 13: MouseState.DisableBit((int)MouseButton.Button8); break; + case 14: MouseState.DisableBit((int)MouseButton.Button9); break; + } + OnMouseUp(); break; case XEventName.FocusIn: @@ -1532,18 +1565,6 @@ namespace OpenTK.Platform.X11 #region --- INativeGLWindow Members --- - #region public IInputDriver InputDriver - - public IInputDriver InputDriver - { - get - { - return driver; - } - } - - #endregion - #region public bool Exists /// diff --git a/Source/OpenTK/Platform/X11/X11Input.cs b/Source/OpenTK/Platform/X11/X11Input.cs deleted file mode 100644 index 2a2371a3..00000000 --- a/Source/OpenTK/Platform/X11/X11Input.cs +++ /dev/null @@ -1,235 +0,0 @@ -#region --- License --- -/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos - * See license.txt for license info - */ -#endregion - -using System; -using System.Collections.Generic; -#if !MINIMAL -using System.Drawing; -#endif -using System.Text; -using System.Diagnostics; -using System.Runtime.InteropServices; - -using OpenTK.Input; - -namespace OpenTK.Platform.X11 -{ - /// \internal - /// - /// Drives the InputDriver on X11. - /// This class supports OpenTK, and is not intended for users of OpenTK. - /// - internal sealed class X11Input : IInputDriver - { - KeyboardDevice keyboard = new KeyboardDevice(); - MouseDevice mouse = new MouseDevice(); - List dummy_keyboard_list = new List(1); - List dummy_mice_list = new List(1); - - int firstKeyCode, lastKeyCode; // The smallest and largest KeyCode supported by the X server. - int keysyms_per_keycode; // The number of KeySyms for each KeyCode. - IntPtr[] keysyms; - - //bool disposed; - - #region --- Constructors --- - - /// - /// Constructs a new X11Input driver. Creates a hidden InputOnly window, child to - /// the main application window, which selects input events and routes them to - /// the device specific drivers (Keyboard, Mouse, Hid). - /// - /// The window which the InputDriver will attach itself on. - public X11Input(IWindowInfo attach) - { - Debug.WriteLine("Initalizing X11 input driver."); - Debug.Indent(); - - if (attach == null) - throw new ArgumentException("A valid parent window must be defined, in order to create an X11Input driver."); - - //window = new X11WindowInfo(attach); - X11WindowInfo window = (X11WindowInfo)attach; - - // Init mouse - mouse.Description = "Default X11 mouse"; - mouse.DeviceID = IntPtr.Zero; - mouse.NumberOfButtons = 5; - mouse.NumberOfWheels = 1; - dummy_mice_list.Add(mouse); - - using (new XLock(window.Display)) - { - // Init keyboard - API.DisplayKeycodes(window.Display, ref firstKeyCode, ref lastKeyCode); - Debug.Print("First keycode: {0}, last {1}", firstKeyCode, lastKeyCode); - - IntPtr keysym_ptr = API.GetKeyboardMapping(window.Display, (byte)firstKeyCode, - lastKeyCode - firstKeyCode + 1, ref keysyms_per_keycode); - Debug.Print("{0} keysyms per keycode.", keysyms_per_keycode); - - keysyms = new IntPtr[(lastKeyCode - firstKeyCode + 1) * keysyms_per_keycode]; - Marshal.PtrToStructure(keysym_ptr, keysyms); - API.Free(keysym_ptr); - - keyboard.Description = "Default X11 keyboard"; - keyboard.NumberOfKeys = lastKeyCode - firstKeyCode + 1; - keyboard.DeviceID = IntPtr.Zero; - dummy_keyboard_list.Add(keyboard); - - // Request that auto-repeat is only set on devices that support it physically. - // This typically means that it's turned off for keyboards (which is what we want). - // We prefer this method over XAutoRepeatOff/On, because the latter needs to - // be reset before the program exits. - bool supported; - Functions.XkbSetDetectableAutoRepeat(window.Display, true, out supported); - } - - Debug.Unindent(); - } - - #endregion - - #region TranslateKey - - internal bool TranslateKey(ref XKeyEvent e, out Key key) - { - XKey keysym = (XKey)API.LookupKeysym(ref e, 0); - XKey keysym2 = (XKey)API.LookupKeysym(ref e, 1); - key = X11KeyMap.GetKey(keysym); - if (key == Key.Unknown) - { - key = X11KeyMap.GetKey(keysym2); - } - if (key == Key.Unknown) - { - Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.keycode, (XKey)keysym, (XKey)keysym2); - } - - return key != Key.Unknown; - } - - #endregion - - #region internal void ProcessEvent(ref XEvent e) - - internal void ProcessEvent(ref XEvent e) - { - switch (e.type) - { - case XEventName.ButtonPress: - if (e.ButtonEvent.button == 1) mouse[OpenTK.Input.MouseButton.Left] = true; - else if (e.ButtonEvent.button == 2) mouse[OpenTK.Input.MouseButton.Middle] = true; - else if (e.ButtonEvent.button == 3) mouse[OpenTK.Input.MouseButton.Right] = true; - else if (e.ButtonEvent.button == 4) mouse.Wheel++; - else if (e.ButtonEvent.button == 5) mouse.Wheel--; - else if (e.ButtonEvent.button == 6) mouse[OpenTK.Input.MouseButton.Button1] = true; - else if (e.ButtonEvent.button == 7) mouse[OpenTK.Input.MouseButton.Button2] = true; - else if (e.ButtonEvent.button == 8) mouse[OpenTK.Input.MouseButton.Button3] = true; - else if (e.ButtonEvent.button == 9) mouse[OpenTK.Input.MouseButton.Button4] = true; - else if (e.ButtonEvent.button == 10) mouse[OpenTK.Input.MouseButton.Button5] = true; - else if (e.ButtonEvent.button == 11) mouse[OpenTK.Input.MouseButton.Button6] = true; - else if (e.ButtonEvent.button == 12) mouse[OpenTK.Input.MouseButton.Button7] = true; - else if (e.ButtonEvent.button == 13) mouse[OpenTK.Input.MouseButton.Button8] = true; - else if (e.ButtonEvent.button == 14) mouse[OpenTK.Input.MouseButton.Button9] = true; - //if ((e.state & (int)X11.MouseMask.Button4Mask) != 0) m.Wheel++; - //if ((e.state & (int)X11.MouseMask.Button5Mask) != 0) m.Wheel--; - //Debug.Print("Button pressed: {0}", e.ButtonEvent.button); - break; - - case XEventName.ButtonRelease: - if (e.ButtonEvent.button == 1) mouse[OpenTK.Input.MouseButton.Left] = false; - else if (e.ButtonEvent.button == 2) mouse[OpenTK.Input.MouseButton.Middle] = false; - else if (e.ButtonEvent.button == 3) mouse[OpenTK.Input.MouseButton.Right] = false; - else if (e.ButtonEvent.button == 6) mouse[OpenTK.Input.MouseButton.Button1] = false; - else if (e.ButtonEvent.button == 7) mouse[OpenTK.Input.MouseButton.Button2] = false; - else if (e.ButtonEvent.button == 8) mouse[OpenTK.Input.MouseButton.Button3] = false; - else if (e.ButtonEvent.button == 9) mouse[OpenTK.Input.MouseButton.Button4] = false; - else if (e.ButtonEvent.button == 10) mouse[OpenTK.Input.MouseButton.Button5] = false; - else if (e.ButtonEvent.button == 11) mouse[OpenTK.Input.MouseButton.Button6] = false; - else if (e.ButtonEvent.button == 12) mouse[OpenTK.Input.MouseButton.Button7] = false; - else if (e.ButtonEvent.button == 13) mouse[OpenTK.Input.MouseButton.Button8] = false; - else if (e.ButtonEvent.button == 14) mouse[OpenTK.Input.MouseButton.Button9] = false; - break; - - case XEventName.MotionNotify: - mouse.Position = new Point(e.MotionEvent.x, e.MotionEvent.y); - break; - } - } - - #endregion - - #region --- IInputDriver Members --- - - #region public IList Keyboard - - public IList Keyboard - { - get { return dummy_keyboard_list; }//return keyboardDriver.Keyboard; - } - - #endregion - - #region public IList Mouse - - public IList Mouse - { - get { return (IList)dummy_mice_list; } //return mouseDriver.Mouse; - } - - #endregion - - public IList Joysticks - { - get { throw new NotImplementedException(); } - } - - #endregion - - #region public void Poll() - - /// - /// Polls and updates state of all keyboard, mouse and joystick devices. - /// - public void Poll() - { - } - - #endregion - - #region --- IDisposable Members --- - - public void Dispose() - { - //this.Dispose(true); - //GC.SuppressFinalize(this); - } - - //private void Dispose(bool manual) - //{ - // if (!disposed) - // { - // //disposing = true; - // if (pollingThread != null && pollingThread.IsAlive) - // pollingThread.Abort(); - - // if (manual) - // { - // } - - // disposed = true; - // } - //} - - //~X11Input() - //{ - // this.Dispose(false); - //} - - #endregion - } -} diff --git a/Source/OpenTK/Platform/X11/X11KeyMap.cs b/Source/OpenTK/Platform/X11/X11KeyMap.cs index e8cb1f21..d9325f8f 100644 --- a/Source/OpenTK/Platform/X11/X11KeyMap.cs +++ b/Source/OpenTK/Platform/X11/X11KeyMap.cs @@ -370,5 +370,22 @@ namespace OpenTK.Platform.X11 return Key.Unknown; } } + + internal static bool TranslateKey(ref XKeyEvent e, out Key key) + { + XKey keysym = (XKey)API.LookupKeysym(ref e, 0); + XKey keysym2 = (XKey)API.LookupKeysym(ref e, 1); + key = X11KeyMap.GetKey(keysym); + if (key == Key.Unknown) + { + key = X11KeyMap.GetKey(keysym2); + } + if (key == Key.Unknown) + { + Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.keycode, (XKey)keysym, (XKey)keysym2); + } + + return key != Key.Unknown; + } } }