2007-07-27 01:37:12 +00:00
|
|
|
|
#region --- License ---
|
2007-08-03 00:14:31 +00:00
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
2007-07-27 01:37:12 +00:00
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Using directives ---
|
|
|
|
|
|
|
|
|
|
using System;
|
2007-07-27 01:20:55 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using OpenTK.Input;
|
2007-07-27 01:20:55 +00:00
|
|
|
|
|
2007-07-27 01:37:12 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-27 01:20:55 +00:00
|
|
|
|
namespace OpenTK.Platform.Windows
|
|
|
|
|
{
|
2007-08-05 09:03:22 +00:00
|
|
|
|
internal class WinRawInput : NativeWindow, IInputDriver
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-08-03 00:14:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Input event data.
|
|
|
|
|
/// </summary>
|
2007-09-02 00:16:22 +00:00
|
|
|
|
private RawInput data = new RawInput();
|
2007-08-03 00:14:31 +00:00
|
|
|
|
/// <summary>
|
2007-08-05 13:42:31 +00:00
|
|
|
|
/// The total number of input devices connected to this system.
|
2007-08-03 00:14:31 +00:00
|
|
|
|
/// </summary>
|
2007-08-05 13:42:31 +00:00
|
|
|
|
private static int deviceCount;
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
private WinRawKeyboard keyboardDriver;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
2007-08-05 13:42:31 +00:00
|
|
|
|
#region --- Constructors ---
|
|
|
|
|
|
2007-08-06 11:22:18 +00:00
|
|
|
|
internal WinRawInput(WindowInfo parent)
|
2007-08-03 00:14:31 +00:00
|
|
|
|
{
|
2007-08-05 13:42:31 +00:00
|
|
|
|
Debug.WriteLine("Initalizing windows raw input driver.");
|
2007-08-04 12:09:58 +00:00
|
|
|
|
Debug.Indent();
|
2007-08-06 11:22:18 +00:00
|
|
|
|
|
|
|
|
|
AssignHandle(parent.Handle);
|
|
|
|
|
Debug.Print("Input window attached to parent {0}", parent);
|
2007-08-04 12:09:58 +00:00
|
|
|
|
keyboardDriver = new WinRawKeyboard(this.Handle);
|
|
|
|
|
|
|
|
|
|
Debug.Unindent();
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-05 13:42:31 +00:00
|
|
|
|
#endregion
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
2007-08-06 11:22:18 +00:00
|
|
|
|
#region internal static int DeviceCount
|
|
|
|
|
|
2007-08-05 00:09:42 +00:00
|
|
|
|
internal static int DeviceCount
|
2007-08-03 00:14:31 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
API.GetRawInputDeviceList(null, ref deviceCount, API.RawInputDeviceListSize);
|
|
|
|
|
return deviceCount;
|
|
|
|
|
}
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
2007-08-05 13:42:31 +00:00
|
|
|
|
|
2007-08-06 11:22:18 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
#region protected override void WndProc(ref Message msg)
|
|
|
|
|
|
2007-08-05 00:09:42 +00:00
|
|
|
|
int size = 0;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes the input Windows Message, routing the data to the correct Keyboard, Mouse or HID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="msg">The WM_INPUT message, containing the data on the input event.</param>
|
|
|
|
|
protected override void WndProc(ref Message msg)
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
switch ((WindowMessage)msg.Msg)
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
case WindowMessage.INPUT:
|
2007-08-04 12:09:58 +00:00
|
|
|
|
size = 0;
|
|
|
|
|
// Get the size of the input data
|
2007-09-02 00:16:22 +00:00
|
|
|
|
API.GetRawInputData(msg.LParam, GetRawInputDataEnum.INPUT,
|
2007-08-04 12:09:58 +00:00
|
|
|
|
IntPtr.Zero, ref size, API.RawInputHeaderSize);
|
|
|
|
|
|
|
|
|
|
//if (data == null || API.RawInputSize < size)
|
|
|
|
|
//{
|
|
|
|
|
// throw new ApplicationException("Critical error when processing raw windows input.");
|
|
|
|
|
//}
|
|
|
|
|
|
2007-09-02 00:16:22 +00:00
|
|
|
|
if (size == API.GetRawInputData(msg.LParam, GetRawInputDataEnum.INPUT,
|
2007-08-04 12:09:58 +00:00
|
|
|
|
data, ref size, API.RawInputHeaderSize))
|
2007-08-03 00:14:31 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
switch (data.Header.Type)
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
case RawInputDeviceType.KEYBOARD:
|
2007-08-22 00:30:16 +00:00
|
|
|
|
if (!keyboardDriver.ProcessKeyboardEvent(data))
|
|
|
|
|
API.DefRawInputProc(ref data, 1, (uint)API.RawInputHeaderSize);
|
|
|
|
|
return;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
|
2007-09-02 00:16:22 +00:00
|
|
|
|
case RawInputDeviceType.MOUSE:
|
2007-08-22 00:30:16 +00:00
|
|
|
|
API.DefRawInputProc(ref data, 1, (uint)API.RawInputHeaderSize);
|
|
|
|
|
return;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
|
2007-09-02 00:16:22 +00:00
|
|
|
|
case RawInputDeviceType.HID:
|
2007-08-22 00:30:16 +00:00
|
|
|
|
API.DefRawInputProc(ref data, 1, (uint)API.RawInputHeaderSize);
|
|
|
|
|
return;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
}
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
2007-08-04 12:09:58 +00:00
|
|
|
|
else
|
2007-08-03 00:14:31 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
throw new ApplicationException(String.Format(
|
|
|
|
|
"GetRawInputData returned invalid data. Windows error {0}. Please file a bug at http://opentk.sourceforge.net",
|
|
|
|
|
Marshal.GetLastWin32Error()));
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2007-09-02 00:16:22 +00:00
|
|
|
|
case WindowMessage.CLOSE:
|
|
|
|
|
case WindowMessage.DESTROY:
|
2007-08-22 00:30:16 +00:00
|
|
|
|
Debug.Print("Input window detached from parent {0}.", Handle);
|
|
|
|
|
ReleaseHandle();
|
|
|
|
|
break;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
2007-09-02 00:16:22 +00:00
|
|
|
|
case WindowMessage.QUIT:
|
2007-08-22 00:30:16 +00:00
|
|
|
|
Debug.WriteLine("Input window quit.");
|
|
|
|
|
this.Dispose();
|
|
|
|
|
break;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
2007-08-04 12:09:58 +00:00
|
|
|
|
|
|
|
|
|
base.WndProc(ref msg);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IInputDriver Members ---
|
|
|
|
|
|
|
|
|
|
IList<Input.IInputDevice> Input.IInputDriver.InputDevices
|
|
|
|
|
{
|
|
|
|
|
get { throw new Exception("The method or operation is not implemented."); }
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
2007-08-04 13:28:16 +00:00
|
|
|
|
public IList<Keyboard> Keyboard
|
2007-08-03 00:14:31 +00:00
|
|
|
|
{
|
2007-08-04 13:28:16 +00:00
|
|
|
|
get { return keyboardDriver.Keyboard; }
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-22 00:30:16 +00:00
|
|
|
|
public IList<Mouse> Mouse
|
2007-08-04 13:28:16 +00:00
|
|
|
|
{
|
|
|
|
|
get { throw new Exception("The method or operation is not implemented."); }
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-05 18:26:14 +00:00
|
|
|
|
public void ProcessEvents()
|
|
|
|
|
{
|
2007-08-05 18:51:07 +00:00
|
|
|
|
// Do nothing, the WndProc is automatically notified of new events (sub-classing magic).
|
2007-08-05 18:26:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
#endregion
|
2007-08-05 09:03:22 +00:00
|
|
|
|
|
2007-08-22 00:30:16 +00:00
|
|
|
|
#region --- IDisposable Members ---
|
2007-08-05 09:03:22 +00:00
|
|
|
|
|
2007-08-22 00:30:16 +00:00
|
|
|
|
private bool disposed;
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
2007-08-05 09:03:22 +00:00
|
|
|
|
{
|
2007-08-22 00:30:16 +00:00
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Dispose(bool manual)
|
|
|
|
|
{
|
|
|
|
|
if (!disposed)
|
|
|
|
|
{
|
|
|
|
|
if (manual)
|
|
|
|
|
{
|
|
|
|
|
keyboardDriver.Dispose();
|
2007-09-02 00:16:22 +00:00
|
|
|
|
this.ReleaseHandle();
|
2007-08-22 00:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~WinRawInput()
|
|
|
|
|
{
|
|
|
|
|
Dispose(false);
|
2007-08-05 09:03:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|