Opentk/Source/OpenTK/Platform/X11/X11Input.cs
the_fiddler 42c1affe34 Added X11 Mouse support. X11 Keyboard support is faster now.
Made GameWindow.Dispose() virtual.
2007-09-25 16:45:12 +00:00

213 lines
6.6 KiB
C#

#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Input;
using System.Diagnostics;
using System.Threading;
namespace OpenTK.Platform.X11
{
/// <summary>
/// Drives the InputDriver on X11.
/// This class supports OpenTK, and is not intended for users of OpenTK.
/// </summary>
internal sealed class X11Input : IInputDriver
{
X11Keyboard keyboardDriver;
X11Mouse mouseDriver;
X11.WindowInfo window;
XEvent e = new XEvent();
Thread pollingThread;
bool disposed, disposing;
#region --- Constructors ---
/// <summary>
/// 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).
/// </summary>
/// <param name="attach">The window which the InputDriver will attach itself on.</param>
public X11Input(X11.WindowInfo 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 X11.WindowInfo(attach);
window.Parent = attach;
/*
Debug.Print("Creating hidden input window.");
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;
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.");
Functions.XMapWindow(window.Display, window.Handle);
*/
window = attach;
keyboardDriver = new X11Keyboard(window);
mouseDriver = new X11Mouse(window);
Thread pollingThread = new Thread(InternalPoll);
//pollingThread.Priority = ThreadPriority.BelowNormal;
pollingThread.IsBackground = true;
pollingThread.Start();
Debug.Unindent();
}
#endregion
private void InternalPoll()
{
try
{
while (!disposed)
{
Functions.XMaskEvent(window.Display,
EventMask.PointerMotionMask | EventMask.PointerMotionHintMask |
EventMask.ButtonPressMask | EventMask.ButtonReleaseMask |
EventMask.KeyPressMask | EventMask.KeyReleaseMask |
EventMask.StructureNotifyMask, ref e);
if (disposed || disposing)
return;
switch (e.type)
{
case XEventName.KeyPress:
case XEventName.KeyRelease:
keyboardDriver.ProcessKeyboardEvent(ref e.KeyEvent);
break;
case XEventName.ButtonPress:
case XEventName.ButtonRelease:
mouseDriver.ProcessButton(ref e.ButtonEvent);
break;
case XEventName.MotionNotify:
mouseDriver.ProcessMotion(ref e.MotionEvent);
break;
case XEventName.DestroyNotify:
Functions.XPutBackEvent(window.Display, ref e);
//pollingThread.Abort();
return;
}
}
}
catch (ThreadAbortException expt)
{
Functions.XUnmapWindow(window.Display, window.Handle);
Functions.XDestroyWindow(window.Display, window.Handle);
return;
}
}
#region --- IInputDriver Members ---
#region public IList<IInputDevice> InputDevices
public IList<IInputDevice> InputDevices
{
get { throw new Exception("The method or operation is not implemented."); }
}
#endregion
#region public IList<Keyboard> Keyboard
public IList<Keyboard> Keyboard
{
get { return keyboardDriver.Keyboard; }
}
#endregion
#region public IList<Mouse> Mouse
public IList<Mouse> Mouse
{
get { return mouseDriver.Mouse; }
}
#endregion
#region public void Poll()
/// <summary>
/// Consumes to keyboard, mouse, etc events, routing them to their
/// respective drivers.
/// </summary>
public void Poll()
{
}
#endregion
#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
}
}