Moved keyboard and mouse code inside X11Input.cs

This commit is contained in:
the_fiddler 2008-02-01 23:28:23 +00:00
parent e5b5af7b11
commit effc907171
5 changed files with 241 additions and 124 deletions

View file

@ -429,7 +429,7 @@ namespace OpenTK.Platform.X11
(uint)SetWindowValuemask.BackPixel | (uint)SetWindowValuemask.BorderPixel;
window.Handle = Functions.XCreateWindow(window.Display, window.RootWindow,
0, 0, mode.Width, mode.Height, 0, window.VisualInfo.depth/*(int)CreateWindowArgs.CopyFromParent*/,
0, 0, width, height, 0, window.VisualInfo.depth/*(int)CreateWindowArgs.CopyFromParent*/,
(int)CreateWindowArgs.InputOutput, window.VisualInfo.visual, (UIntPtr)mask, ref attributes);
if (window.Handle == IntPtr.Zero)
@ -439,8 +439,8 @@ namespace OpenTK.Platform.X11
XSizeHints hints = new XSizeHints();
hints.x = 0;
hints.y = 0;
hints.width = mode.Width;
hints.height = mode.Height;
hints.width = width;
hints.height = height;
hints.flags = (IntPtr)(XSizeHintsFlags.USSize | XSizeHintsFlags.USPosition);
Functions.XSetWMNormalHints(window.Display, window.Handle, ref hints);
@ -493,7 +493,7 @@ namespace OpenTK.Platform.X11
/// </remarks>
public void CreateWindow(int width, int height, DisplayMode mode, out IGraphicsContext glContext)
{
this.CreateWindow(width, height, new GraphicsFormat(), out glContext);
this.CreateWindow(width, height, mode.ToGraphicsFormat(), out glContext);
}
#endregion

View file

@ -7,10 +7,10 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using OpenTK.Input;
using System.Diagnostics;
using System.Threading;
namespace OpenTK.Platform.X11
{
@ -20,13 +20,16 @@ namespace OpenTK.Platform.X11
/// </summary>
internal sealed class X11Input : IInputDriver
{
X11Keyboard keyboardDriver;
X11Mouse mouseDriver;
X11.WindowInfo window;
KeyboardDevice keyboard = new KeyboardDevice();
MouseDevice mouse = new MouseDevice();
List<KeyboardDevice> dummy_keyboard_list = new List<KeyboardDevice>(1);
List<MouseDevice> dummy_mice_list = new List<MouseDevice>(1);
//XEvent e = new XEvent();
Thread pollingThread = null;
X11KeyMap keymap = new X11KeyMap();
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;
@ -47,43 +50,34 @@ namespace OpenTK.Platform.X11
throw new ArgumentException("A valid parent window must be defined, in order to create an X11Input driver.");
window = new X11.WindowInfo(attach);
/*
window = new WindowInfo();
window.Parent = attach;
Debug.Print("Creating hidden input window.");
// Init mouse
mouse.Description = "Default X11 mouse";
mouse.DeviceID = IntPtr.Zero;
mouse.NumberOfButtons = 5;
mouse.NumberOfWheels = 1;
dummy_mice_list.Add(mouse);
XSetWindowAttributes wnd_attr = new XSetWindowAttributes();
wnd_attr.background_pixel = IntPtr.Zero;
wnd_attr.border_pixel = IntPtr.Zero;
//wnd_attr.colormap = IntPtr.Zero;
wnd_attr.event_mask = (IntPtr)
(EventMask.StructureNotifyMask |
EventMask.PointerMotionMask | EventMask.PointerMotionHintMask |
EventMask.ButtonPressMask | EventMask.ButtonReleaseMask |
EventMask.KeyPressMask | EventMask.KeyReleaseMask);
uint cw =
(uint)SetWindowValuemask.ColorMap | (uint)SetWindowValuemask.EventMask |
(uint)SetWindowValuemask.BackPixel | (uint)SetWindowValuemask.BorderPixel;
// Init keyboard
API.DisplayKeycodes(window.Display, ref firstKeyCode, ref lastKeyCode);
Debug.Print("First keycode: {0}, last {1}", firstKeyCode, lastKeyCode);
window.Handle = Functions.XCreateWindow(window.Display, window.Parent.Handle,
0, 0, 30, 30, 0, Constants.CopyFromParent, Constants.InputOutput,
IntPtr.Zero, (UIntPtr)cw, ref wnd_attr);
if (window.Handle == IntPtr.Zero)
throw new ApplicationException("Could not create hidden input window.");
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);
Functions.XMapWindow(window.Display, window.Handle);
*/
//window = attach;
keyboardDriver = new X11Keyboard(window);
mouseDriver = new X11Mouse(window);
keysyms = new IntPtr[(lastKeyCode - firstKeyCode + 1) * keysyms_per_keycode];
Marshal.PtrToStructure(keysym_ptr, keysyms);
//keysyms = (IntPtr[])Marshal.PtrToStructure(keysym_ptr, typeof(IntPtr[]));
API.Free(keysym_ptr);
KeyboardDevice kb = new KeyboardDevice();
keyboard.Description = "Default X11 keyboard";
keyboard.NumberOfKeys = lastKeyCode - firstKeyCode + 1;
keyboard.DeviceID = IntPtr.Zero;
dummy_keyboard_list.Add(keyboard);
//pollingThread = new Thread(InternalPoll);
//pollingThread.Priority = ThreadPriority.BelowNormal;
//pollingThread.IsBackground = true;
//pollingThread.Start();
Debug.Unindent();
}
@ -148,19 +142,38 @@ namespace OpenTK.Platform.X11
{
case XEventName.KeyPress:
case XEventName.KeyRelease:
//Debug.Print("Keyboard press");
keyboardDriver.ProcessKeyboardEvent(ref e.KeyEvent);
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))
keyboard[keymap[(XKey)keysym]] = pressed;
else if (keymap.ContainsKey((XKey)keysym2))
keyboard[keymap[(XKey)keysym2]] = pressed;
else
Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.KeyEvent.keycode, (XKey)keysym, (XKey)keysym2);
break;
// See MouseDriver.Poll() instead.
case XEventName.ButtonPress:
if (e.ButtonEvent.button == (int)MouseButton.Button1) mouse[OpenTK.Input.MouseButton.Left] = true;
else if (e.ButtonEvent.button == (int)MouseButton.Button2) mouse[OpenTK.Input.MouseButton.Middle] = true;
else if (e.ButtonEvent.button == (int)MouseButton.Button3) mouse[OpenTK.Input.MouseButton.Right] = true;
else if (e.ButtonEvent.button == (int)MouseButton.Button4) mouse.Wheel++;
else if (e.ButtonEvent.button == (int)MouseButton.Button5) mouse.Wheel--;
//if ((e.state & (int)X11.MouseMask.Button4Mask) != 0) m.Wheel++;
//if ((e.state & (int)X11.MouseMask.Button5Mask) != 0) m.Wheel--;
break;
case XEventName.ButtonRelease:
//Debug.Print("Button");
mouseDriver.ProcessButton(ref e.ButtonEvent);
if (e.ButtonEvent.button == (int)MouseButton.Button1) mouse[OpenTK.Input.MouseButton.Left] = false;
else if (e.ButtonEvent.button == (int)MouseButton.Button2) mouse[OpenTK.Input.MouseButton.Middle] = false;
else if (e.ButtonEvent.button == (int)MouseButton.Button3) mouse[OpenTK.Input.MouseButton.Right] = false;
break;
case XEventName.MotionNotify:
//Debug.Print("Mouse move");
mouseDriver.ProcessMotion(ref e.MotionEvent);
mouse.X = e.MotionEvent.x;
mouse.Y = e.MotionEvent.y;
break;
}
}
@ -182,7 +195,7 @@ namespace OpenTK.Platform.X11
public IList<KeyboardDevice> Keyboard
{
get { return keyboardDriver.Keyboard; }
get { return dummy_keyboard_list; }//return keyboardDriver.Keyboard;
}
#endregion
@ -191,7 +204,7 @@ namespace OpenTK.Platform.X11
public IList<MouseDevice> Mouse
{
get { return mouseDriver.Mouse; }
get { return (IList<MouseDevice>)dummy_mice_list; } //return mouseDriver.Mouse;
}
#endregion
@ -214,30 +227,30 @@ namespace OpenTK.Platform.X11
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
//this.Dispose(true);
//GC.SuppressFinalize(this);
}
private void Dispose(bool manual)
{
if (!disposed)
{
//disposing = true;
if (pollingThread != null && pollingThread.IsAlive)
pollingThread.Abort();
//private void Dispose(bool manual)
//{
// if (!disposed)
// {
// //disposing = true;
// if (pollingThread != null && pollingThread.IsAlive)
// pollingThread.Abort();
if (manual)
{
}
// if (manual)
// {
// }
disposed = true;
}
}
// disposed = true;
// }
//}
~X11Input()
{
this.Dispose(false);
}
//~X11Input()
//{
// this.Dispose(false);
//}
#endregion
}

View file

@ -0,0 +1,140 @@
#region --- License ---
/* Licensed under the MIT/X11 license.
* Copyright (c) 2006-2008 the OpenTK team.
* This notice may not be removed.
* See license.txt for licensing detailed licensing details.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using OpenTK.Input;
namespace OpenTK.Platform.X11
{
internal class X11KeyMap : Dictionary<XKey, Key>
{
internal X11KeyMap()
{
try
{
this.Add(XKey.Escape, Key.Escape);
this.Add(XKey.Return, Key.Enter);
this.Add(XKey.space, Key.Space);
this.Add(XKey.BackSpace, Key.BackSpace);
this.Add(XKey.Shift_L, Key.ShiftLeft);
this.Add(XKey.Shift_R, Key.ShiftRight);
this.Add(XKey.Alt_L, Key.AltLeft);
this.Add(XKey.Alt_R, Key.AltRight);
this.Add(XKey.Control_L, Key.ControlLeft);
this.Add(XKey.Control_R, Key.ControlRight);
this.Add(XKey.Super_L, Key.WinLeft);
this.Add(XKey.Super_R, Key.WinRight);
this.Add(XKey.Meta_L, Key.WinLeft);
this.Add(XKey.Meta_R, Key.WinRight);
this.Add(XKey.Menu, Key.Menu);
this.Add(XKey.Tab, Key.Tab);
this.Add(XKey.minus, Key.Minus);
this.Add(XKey.plus, Key.Plus);
this.Add(XKey.equal, Key.Plus);
this.Add(XKey.Caps_Lock, Key.CapsLock);
this.Add(XKey.Num_Lock, Key.NumLock);
for (int i = (int)XKey.F1; i <= (int)XKey.F35; i++)
{
this.Add((XKey)i, (Key)((int)Key.F1 + (i - (int)XKey.F1)));
}
for (int i = (int)XKey.a; i <= (int)XKey.z; i++)
{
this.Add((XKey)i, (Key)((int)Key.A + (i - (int)XKey.a)));
}
for (int i = (int)XKey.A; i <= (int)XKey.Z; i++)
{
this.Add((XKey)i, (Key)((int)Key.A + (i - (int)XKey.A)));
}
for (int i = (int)XKey.Number0; i <= (int)XKey.Number9; i++)
{
this.Add((XKey)i, (Key)((int)Key.Number0 + (i - (int)XKey.Number0)));
}
for (int i = (int)XKey.KP_0; i <= (int)XKey.KP_9; i++)
{
this.Add((XKey)i, (Key)((int)Key.Keypad0 + (i - (int)XKey.KP_0)));
}
this.Add(XKey.Pause, Key.Pause);
this.Add(XKey.Break, Key.Pause);
this.Add(XKey.Scroll_Lock, Key.Pause);
this.Add(XKey.Insert, Key.PrintScreen);
this.Add(XKey.Print, Key.PrintScreen);
this.Add(XKey.Sys_Req, Key.PrintScreen);
this.Add(XKey.backslash, Key.BackSlash);
this.Add(XKey.bar, Key.BackSlash);
this.Add(XKey.braceleft, Key.BracketLeft);
this.Add(XKey.bracketleft, Key.BracketLeft);
this.Add(XKey.braceright, Key.BracketRight);
this.Add(XKey.bracketright, Key.BracketRight);
this.Add(XKey.colon, Key.Semicolon);
this.Add(XKey.semicolon, Key.Semicolon);
this.Add(XKey.quoteright, Key.Quote);
this.Add(XKey.quotedbl, Key.Quote);
this.Add(XKey.quoteleft, Key.Tilde);
this.Add(XKey.asciitilde, Key.Tilde);
this.Add(XKey.comma, Key.Comma);
this.Add(XKey.less, Key.Comma);
this.Add(XKey.period, Key.Period);
this.Add(XKey.greater, Key.Period);
this.Add(XKey.slash, Key.Slash);
this.Add(XKey.question, Key.Slash);
this.Add(XKey.Left, Key.Left);
this.Add(XKey.Down, Key.Down);
this.Add(XKey.Right, Key.Right);
this.Add(XKey.Up, Key.Up);
this.Add(XKey.Delete, Key.Delete);
this.Add(XKey.Home, Key.Home);
this.Add(XKey.End, Key.End);
//this.Add(XKey.Prior, Key.PageUp); // XKey.Prior == XKey.Page_Up
this.Add(XKey.Page_Up, Key.PageUp);
this.Add(XKey.Page_Down, Key.PageDown);
//this.Add(XKey.Next, Key.PageDown); // XKey.Next == XKey.Page_Down
this.Add(XKey.KP_Add, Key.KeypadAdd);
this.Add(XKey.KP_Subtract, Key.KeypadSubtract);
this.Add(XKey.KP_Multiply, Key.KeypadMultiply);
this.Add(XKey.KP_Divide, Key.KeypadDivide);
this.Add(XKey.KP_Decimal, Key.KeypadDecimal);
this.Add(XKey.KP_Insert, Key.Keypad0);
this.Add(XKey.KP_End, Key.Keypad1);
this.Add(XKey.KP_Down, Key.Keypad2);
this.Add(XKey.KP_Page_Down, Key.Keypad3);
this.Add(XKey.KP_Left, Key.Keypad4);
this.Add(XKey.KP_Right, Key.Keypad6);
this.Add(XKey.KP_Home, Key.Keypad7);
this.Add(XKey.KP_Up, Key.Keypad8);
this.Add(XKey.KP_Page_Up, Key.Keypad9);
this.Add(XKey.KP_Delete, Key.KeypadDecimal);
this.Add(XKey.KP_Enter, Key.Enter);
}
catch (ArgumentException e)
{
Debug.Print("Exception while creating keymap: '{0}'.", e.ToString());
System.Windows.Forms.MessageBox.Show(
String.Format("Exception while creating keymap: '{0}'.", e.ToString()));
}
}
}
}

View file

@ -16,6 +16,8 @@ using System.Diagnostics;
namespace OpenTK.Platform.X11
{
// THIS CLASS IS NOT USED ANYMORE.
/// <summary>
/// Drives Keyboard devices on X11.
/// This class supports OpenTK, and is not intended for use by OpenTK programs.
@ -173,25 +175,9 @@ namespace OpenTK.Platform.X11
//Debug.Print("Info: {0}", window.ToString());
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);
//keysyms = (IntPtr[])Marshal.PtrToStructure(keysym_ptr, typeof(IntPtr[]));
API.Free(keysym_ptr);
KeyboardDevice kb = new KeyboardDevice();
kb.Description = "Default X11 keyboard";
kb.NumberOfKeys = lastKeyCode - firstKeyCode + 1;
kb.DeviceID = IntPtr.Zero;
keyboards.Add(kb);
Debug.Print("Keyboard added: {0}", kb.ToString());
//keyboards.Add(kb);
//Debug.Print("Keyboard added: {0}", kb.ToString());
}
#endregion
@ -205,38 +191,9 @@ namespace OpenTK.Platform.X11
/// <returns>True if the event was processed, false otherwise.</returns>
internal bool ProcessKeyboardEvent(ref X11.XKeyEvent e)
{
return false;
//int keysym = keysyms[(e.keycode - firstKeyCode) * keysyms_per_keycode].ToInt32();
//int keysym2 = keysyms[(e.keycode - firstKeyCode) * keysyms_per_keycode].ToInt32();
bool pressed = e.type == XEventName.KeyPress;
IntPtr keysym = API.LookupKeysym(ref e, 0);
IntPtr keysym2 = API.LookupKeysym(ref e, 1);
//Debug.Print("Key down: {0}", e.ToString());
int index = keyboards.FindIndex(delegate(KeyboardDevice kb)
{
return kb.DeviceID == IntPtr.Zero;
});
switch (keysym.ToInt64())
{
default:
if (keymap.ContainsKey((XKey)keysym))
{
keyboards[index][keymap[(XKey)keysym]] = pressed;
}
else if (keymap.ContainsKey((XKey)keysym2))
{
keyboards[index][keymap[(XKey)keysym2]] = pressed;
}
else
{
//Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.keycode, (XKey)keysym, (XKey)keysym2);
return false;
}
return true;
}
}
#endregion

View file

@ -11,6 +11,8 @@ using OpenTK.Input;
namespace OpenTK.Platform.X11
{
// THIS CLASS IS NOT USED ANYMORE.
/// <summary>
/// Drives Mouse devices on X11.
/// This class supports OpenTK, and is not intended for use by OpenTK programs.
@ -56,11 +58,16 @@ namespace OpenTK.Platform.X11
internal bool ProcessButton(ref X11.XButtonEvent e)
{
MouseDevice m = mice[0];
bool pressed = e.type == XEventName.ButtonPress;
//e.
if ((e.state & (int)X11.MouseMask.Button1Mask) != 0) m[OpenTK.Input.MouseButton.Left] = pressed;
if ((e.state & (int)X11.MouseMask.Button2Mask) != 0) m[OpenTK.Input.MouseButton.Middle] = pressed;
if ((e.state & (int)X11.MouseMask.Button3Mask) != 0) m[OpenTK.Input.MouseButton.Right] = pressed;
//bool pressed = e.type == XEventName.ButtonPress;
//if ((e.state & (int)X11.MouseMask.Button1Mask) != 0) m[OpenTK.Input.MouseButton.Left] = pressed;
//if ((e.state & (int)X11.MouseMask.Button2Mask) != 0) m[OpenTK.Input.MouseButton.Middle] = pressed;
//if ((e.state & (int)X11.MouseMask.Button3Mask) != 0) m[OpenTK.Input.MouseButton.Right] = pressed;
//if ((e.state & (int)X11.MouseMask.Button4Mask) != 0) m.Wheel++;
//if ((e.state & (int)X11.MouseMask.Button5Mask) != 0) m.Wheel--;
if (e.button == (int)MouseButton.Button1) m[OpenTK.Input.MouseButton.Left] = true;
else if (e.button == (int)MouseButton.Button2) m[OpenTK.Input.MouseButton.Middle] = true;
else if (e.button == (int)MouseButton.Button3) m[OpenTK.Input.MouseButton.Right] = true;
if ((e.state & (int)X11.MouseMask.Button4Mask) != 0) m.Wheel++;
if ((e.state & (int)X11.MouseMask.Button5Mask) != 0) m.Wheel--;
return true;