* Source/OpenTK/OpenTK.csproj:

* Source/OpenTK/Input/Keyboard.cs:
* Source/OpenTK/Input/InputDriver.cs:
* Source/OpenTK/Input/KeyboardState.cs:
* Source/OpenTK/Input/IKeyboardDriver.cs:
* Source/OpenTK/Platform/X11/X11Input.cs:
* Source/OpenTK/Platform/X11/Functions.cs:
* Source/OpenTK/Platform/X11/X11Factory.cs:
* Source/OpenTK/Platform/Windows/WMInput.cs:
* Source/OpenTK/Platform/X11/X11Keyboard.cs:
* Source/OpenTK/Platform/MacOS/CarbonInput.cs:
* Source/OpenTK/Platform/Windows/WinGLNative.cs:
* Source/OpenTK/Platform/Windows/WinRawInput.cs:
* Source/OpenTK/Platform/Windows/WinRawKeyboard.cs: Added initial
  OpenTK.Input.Keyboard implementation for X11.
This commit is contained in:
the_fiddler 2010-10-20 14:33:23 +00:00
parent 33529aff63
commit 7e3182b1fc
14 changed files with 277 additions and 30 deletions

View file

@ -19,5 +19,18 @@ namespace OpenTK.Input
/// Gets the list of available KeyboardDevices.
/// </summary>
IList<KeyboardDevice> Keyboard { get; }
/// <summary>
/// Retrieves the KeyboardState for the default keyboard device.
/// </summary>
/// <returns>A <see cref="OpenTK.Input.KeyboardState"/> structure containing the state of the keyboard device.</returns>
KeyboardState GetState();
/// <summary>
/// Retrieves the KeyboardState for the specified keyboard device.
/// </summary>
/// <param name="index">The index of the keyboard device.</param>
/// <returns>A <see cref="OpenTK.Input.KeyboardState"/> structure containing the state of the keyboard device.</returns>
KeyboardState GetState(int index);
}
}

View file

@ -73,6 +73,16 @@ namespace OpenTK
get { return inputDriver.Keyboard; }
}
public KeyboardState GetState()
{
return inputDriver.GetState();
}
public KeyboardState GetState(int index)
{
return inputDriver.GetState(index);
}
#endregion
#region --- IMouseDriver Members ---

View file

@ -38,17 +38,8 @@ namespace OpenTK.Input
{
#region Fields
//static IKeyboardDriver driver;
#endregion
#region Constructors
static Keyboard()
{
throw new NotImplementedException();
//driver = Platform.Factory.Default.CreateKeyboardDriver();
}
static readonly IKeyboardDriver driver =
Platform.Factory.Default.CreateKeyboardDriver();
#endregion
@ -60,7 +51,7 @@ namespace OpenTK.Input
/// <returns>A <see cref="OpenTK.Input.KeyboardState"/> structure containing the state of the keyboard device.</returns>
public static KeyboardState GetState()
{
return new KeyboardState();
return driver.GetState();
}
/// <summary>
@ -70,7 +61,7 @@ namespace OpenTK.Input
/// <returns>A <see cref="OpenTK.Input.KeyboardState"/> structure containing the state of the keyboard device.</returns>
public static KeyboardState GetState(int index)
{
return new KeyboardState();
return driver.GetState(index);
}
#endregion

View file

@ -26,7 +26,7 @@
#endregion
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
namespace OpenTK.Input
@ -39,9 +39,8 @@ namespace OpenTK.Input
#region Fields
const int NumKeys = ((int)Key.LastKey + 16) / 32;
// Todo: The following line triggers bogus CS0214 in gmcs 2.0.1, sigh...
// Need to add a workaround using either ExplicitLayout or another trick.
//unsafe fixed int Keys[NumKeys];
// The following line triggers bogus CS0214 in gmcs 2.0.1, sigh...
unsafe fixed int Keys[NumKeys];
#endregion
@ -57,7 +56,7 @@ namespace OpenTK.Input
/// <param name="key">The <see cref="OpenTK.Input.Key"/> to check.</param>
public bool IsKeyDown(Key key)
{
return ReadBit((int)key) != 0;
return ReadBit((int)key);
}
/// <summary>
@ -66,24 +65,50 @@ namespace OpenTK.Input
/// <param name="key">The <see cref="OpenTK.Input.Key"/> to check.</param>
public bool IsKeyUp(Key key)
{
return ReadBit((int)key) == 0;
return !ReadBit((int)key);
}
#endregion
#region Internal Members
internal int ReadBit(int offset)
internal bool ReadBit(int offset)
{
return 0;
//unsafe { return (Keys[(offset / 32)] & (1 << (offset % 32))); }
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
{
fixed (int* k = Keys)
{
return (*(k + int_offset) & (1 << bit_offset)) != 0u;
}
}
}
internal enum BitValue { Zero = 0, One = 1 }
internal void WriteBit(int offset, BitValue bit)
internal void EnableBit(int offset)
{
// Todo: this is completely broken.
//unsafe { Keys[offset / 32] = Keys[offset / 32] ^ (~(int)bit << (offset % 32)); }
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
{
fixed (int* k = Keys)
{
*(k + int_offset) |= 1 << bit_offset;
}
}
}
internal void DisableBit(int offset)
{
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
{
fixed (int* k = Keys)
{
*(k + int_offset) &= ~(1 << bit_offset);
}
}
}
#endregion
@ -97,7 +122,17 @@ namespace OpenTK.Input
/// <returns>True, if both instances are equal; false otherwise.</returns>
public bool Equals(KeyboardState other)
{
throw new NotImplementedException();
bool equal = true;
unsafe
{
fixed (int* k1 = Keys)
fixed (int* k2 = Keys)
{
for (int i = 0; equal && i < NumKeys; i++)
equal &= *(k1 + i) == *(k2 + i);
}
}
return equal;
}
#endregion

View file

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<ProjectType>Local</ProjectType>
@ -756,6 +756,7 @@
<None Include="OpenTK.dll.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Compile Include="Platform\X11\X11Keyboard.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View file

@ -35,6 +35,16 @@ namespace OpenTK.Platform.MacOS
get { return dummy_keyboard_list; }
}
public KeyboardState GetState()
{
throw new NotImplementedException();
}
public KeyboardState GetState(int index)
{
throw new NotImplementedException();
}
#endregion
#region IMouseDriver Members

View file

@ -246,6 +246,16 @@ namespace OpenTK.Platform.Windows
get { return keyboards; }
}
public KeyboardState GetState()
{
throw new NotImplementedException();
}
public KeyboardState GetState(int index)
{
throw new NotImplementedException();
}
#endregion
#region IMouseDriver Members

View file

@ -844,7 +844,7 @@ namespace OpenTK.Platform.Windows
public bool CursorVisible
{
get { return cursor_visible_count > 0; }
get { return cursor_visible_count > 0; } // Not used
set
{
if (value && cursor_visible_count < 0)
@ -1186,6 +1186,16 @@ namespace OpenTK.Platform.Windows
get { return keyboards; }
}
public KeyboardState GetState()
{
throw new NotImplementedException();
}
public KeyboardState GetState(int index)
{
throw new NotImplementedException();
}
#endregion
#region IMouseDriver Members

View file

@ -207,6 +207,16 @@ namespace OpenTK.Platform.Windows
get { return keyboardDriver.Keyboard; }
}
public KeyboardState GetState()
{
throw new NotImplementedException();
}
public KeyboardState GetState(int index)
{
throw new NotImplementedException();
}
#endregion
#region IMouseDriver Members

View file

@ -237,6 +237,16 @@ namespace OpenTK.Platform.Windows
get { return keyboards; }
}
public KeyboardState GetState()
{
throw new NotImplementedException();
}
public KeyboardState GetState(int index)
{
throw new NotImplementedException();
}
#endregion
#region --- IDisposable Members ---

View file

@ -176,6 +176,12 @@ namespace OpenTK.Platform.X11
[DllImport("libX11", EntryPoint = "XTranslateCoordinates")]
public extern static bool XTranslateCoordinates(IntPtr display, IntPtr src_w, IntPtr dest_w, int src_x, int src_y, out int intdest_x_return, out int dest_y_return, out IntPtr child_return);
[DllImport("libX11", EntryPoint = "XGrabKeyboard")]
public extern static int XGrabKeyboard(IntPtr display, IntPtr window, bool owner_events, GrabMode pointer_mode, GrabMode keyboard_mode, IntPtr timestamp);
[DllImport("libX11", EntryPoint = "XUngrabKeyboard")]
public extern static int XUngrabKeyboard(IntPtr display, IntPtr timestamp);
[DllImport("libX11", EntryPoint = "XGetGeometry")]
public extern static bool XGetGeometry(IntPtr display, IntPtr window, out IntPtr root, out int x, out int y, out int width, out int height, out int border_width, out int depth);

View file

@ -80,7 +80,7 @@ namespace OpenTK.Platform.X11
public virtual OpenTK.Input.IKeyboardDriver CreateKeyboardDriver()
{
throw new NotImplementedException();
return new X11Keyboard(null);
}
#endregion

View file

@ -219,6 +219,16 @@ namespace OpenTK.Platform.X11
get { return dummy_keyboard_list; }//return keyboardDriver.Keyboard;
}
public KeyboardState GetState()
{
throw new NotImplementedException();
}
public KeyboardState GetState(int index)
{
throw new NotImplementedException();
}
#endregion
#region public IList<Mouse> Mouse

View file

@ -0,0 +1,131 @@
#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2010 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.X11
{
// Standard keyboard driver that relies on xlib input events.
// Only one keyboard supported.
sealed class X11Keyboard : IKeyboardDriver
{
readonly X11WindowInfo window = new X11WindowInfo();
readonly X11KeyMap keymap = new X11KeyMap();
KeyboardState state = new KeyboardState();
// Can either attach itself to the specified window or can hook the root window.
public X11Keyboard(X11WindowInfo win)
{
if (win != null)
{
window = win;
}
else
{
using (new XLock(API.DefaultDisplay))
{
window.Display = API.DefaultDisplay;
window.Screen = Functions.XDefaultScreen(window.Display);
window.RootWindow = Functions.XRootWindow(window.Display, window.Screen);
window.WindowHandle = window.RootWindow;
window.EventMask =EventMask.KeyPressMask | EventMask.KeyReleaseMask | EventMask.KeymapStateMask;
Functions.XGrabKeyboard(window.Display, window.RootWindow, true,
GrabMode.GrabModeAsync, GrabMode.GrabModeAsync, IntPtr.Zero);
Functions.XSelectInput(window.Display, window.RootWindow, new IntPtr((int)window.EventMask));
}
}
}
// Todo: remove this
public IList<KeyboardDevice> Keyboard { get { throw new NotSupportedException(); } }
public KeyboardState GetState()
{
ProcessEvents();
return state;
}
public KeyboardState GetState(int index)
{
ProcessEvents();
return state;
}
void ProcessEvents()
{
XEvent e = new XEvent();
while (true)
{
using (new XLock(window.Display))
{
if (!Functions.XCheckWindowEvent(window.Display, window.WindowHandle, window.EventMask, ref e))
break;
switch (e.type)
{
case XEventName.KeyPress:
case XEventName.KeyRelease:
bool pressed = e.type == XEventName.KeyPress;
IntPtr keysym = API.LookupKeysym(ref e.KeyEvent, 0);
IntPtr keysym2 = API.LookupKeysym(ref e.KeyEvent, 1);
if (keymap.ContainsKey((XKey)keysym))
{
if (pressed)
state.EnableBit((int)keymap[(XKey)keysym]);
else
state.DisableBit((int)keymap[(XKey)keysym]);
}
else if (keymap.ContainsKey((XKey)keysym2))
{
if (pressed)
state.EnableBit((int)keymap[(XKey)keysym2]);
else
state.DisableBit((int)keymap[(XKey)keysym2]);
}
else
{
System.Diagnostics.Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.",
e.KeyEvent.keycode, (XKey)keysym, (XKey)keysym2);
}
break;
case XEventName.KeymapNotify:
System.Diagnostics.Debug.Print("Keymap event: {0}", e.KeymapEvent.key_vector0);
break;
}
}
}
}
}
}