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-03 00:14:31 +00:00
|
|
|
|
internal class WinRawInput : NativeWindow, Input.IInputDriver
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-08-03 00:14:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Input event data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private API.RawInput data = new API.RawInput();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The list of keyboards connected to this system.
|
|
|
|
|
/// </summary>
|
2007-08-04 12:09:58 +00:00
|
|
|
|
//internal List<WinRawKeyboard> keyboards = new List<WinRawKeyboard>();
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
private WinRawKeyboard keyboardDriver;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
internal WinRawInput(IntPtr parentHandle)
|
2007-08-03 00:14:31 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
Debug.WriteLine("Initalizing raw input driver.");
|
|
|
|
|
Debug.Indent();
|
|
|
|
|
|
|
|
|
|
AssignHandle(parentHandle);
|
|
|
|
|
Debug.Print("Input window attached to parent {0}", this.Handle);
|
|
|
|
|
keyboardDriver = new WinRawKeyboard(this.Handle);
|
|
|
|
|
|
|
|
|
|
Debug.Unindent();
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-05 00:09:42 +00:00
|
|
|
|
private static int deviceCount;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
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-04 12:09:58 +00:00
|
|
|
|
/*
|
2007-08-03 00:14:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether the Device list has changed, for example
|
|
|
|
|
/// by removing or adding a device.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal static bool DeviceListChanged
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
uint count = 0;
|
|
|
|
|
if (API.GetRawInputDeviceList(null, ref count, API.RawInputDeviceListSize) == 0)
|
|
|
|
|
{
|
|
|
|
|
if (deviceCount == count)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
deviceCount = count;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException(String.Format(
|
|
|
|
|
"Could not retrieve the count of Keyboard devices. Windows error: {0}",
|
|
|
|
|
Marshal.GetLastWin32Error()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-04 12:09:58 +00:00
|
|
|
|
*/
|
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-08-04 12:09:58 +00:00
|
|
|
|
switch (msg.Msg)
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
case API.Constants.WM_INPUT:
|
|
|
|
|
size = 0;
|
|
|
|
|
// Get the size of the input data
|
|
|
|
|
API.GetRawInputData(msg.LParam, API.GetRawInputDataEnum.INPUT,
|
|
|
|
|
IntPtr.Zero, ref size, API.RawInputHeaderSize);
|
|
|
|
|
|
|
|
|
|
//if (data == null || API.RawInputSize < size)
|
|
|
|
|
//{
|
|
|
|
|
// throw new ApplicationException("Critical error when processing raw windows input.");
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
if (size == API.GetRawInputData(msg.LParam, API.GetRawInputDataEnum.INPUT,
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
case API.RawInputDeviceType.KEYBOARD:
|
|
|
|
|
keyboardDriver.ProcessKeyboardEvent(data);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case API.RawInputDeviceType.MOUSE:
|
|
|
|
|
//throw new NotImplementedException();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case API.RawInputDeviceType.HID:
|
|
|
|
|
//throw new NotImplementedException();
|
|
|
|
|
break;
|
|
|
|
|
}
|
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-08-04 12:09:58 +00:00
|
|
|
|
case API.Constants.WM_CLOSE:
|
|
|
|
|
case API.Constants.WM_DESTROY:
|
|
|
|
|
Debug.Print("Input window detached from parent {0}.", Handle);
|
|
|
|
|
ReleaseHandle();
|
2007-08-03 00:14:31 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
case API.Constants.WM_QUIT:
|
|
|
|
|
Debug.WriteLine("Input window quit.");
|
2007-08-03 00:14:31 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
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; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IList<Keyboard> Mouse
|
|
|
|
|
{
|
|
|
|
|
get { throw new Exception("The method or operation is not implemented."); }
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|