2014-01-17 00:27:07 +00:00
|
|
|
|
#region License
|
|
|
|
|
//
|
|
|
|
|
// WinRawJoystick.cs
|
|
|
|
|
//
|
|
|
|
|
// Author:
|
|
|
|
|
// Stefanos A. <stapostol@gmail.com>
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2014 Stefanos Apostolopoulos
|
|
|
|
|
//
|
|
|
|
|
// 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 System.Diagnostics;
|
2014-01-17 18:11:16 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2014-01-17 00:27:07 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using OpenTK.Input;
|
2014-01-17 18:11:16 +00:00
|
|
|
|
using OpenTK.Platform.Common;
|
2014-01-17 00:27:07 +00:00
|
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.Windows
|
|
|
|
|
{
|
|
|
|
|
class WinRawJoystick : IJoystickDriver2
|
|
|
|
|
{
|
2014-01-17 18:11:16 +00:00
|
|
|
|
class Device
|
|
|
|
|
{
|
|
|
|
|
public IntPtr Handle;
|
|
|
|
|
JoystickCapabilities Capabilities;
|
|
|
|
|
JoystickState State;
|
|
|
|
|
Guid Guid;
|
|
|
|
|
|
2014-07-31 20:50:33 +00:00
|
|
|
|
internal readonly List<HidProtocolValueCaps> AxisCaps =
|
|
|
|
|
new List<HidProtocolValueCaps>();
|
|
|
|
|
internal readonly List<HidProtocolButtonCaps> ButtonCaps =
|
|
|
|
|
new List<HidProtocolButtonCaps>();
|
|
|
|
|
|
2014-01-17 18:11:16 +00:00
|
|
|
|
readonly Dictionary<int, JoystickAxis> axes =
|
|
|
|
|
new Dictionary<int,JoystickAxis>();
|
|
|
|
|
readonly Dictionary<int, JoystickButton> buttons =
|
|
|
|
|
new Dictionary<int, JoystickButton>();
|
2014-07-31 18:44:30 +00:00
|
|
|
|
readonly Dictionary<int, JoystickHat> hats =
|
|
|
|
|
new Dictionary<int, JoystickHat>();
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
2014-07-31 18:44:30 +00:00
|
|
|
|
public Device(IntPtr handle, Guid guid)
|
2014-01-17 18:11:16 +00:00
|
|
|
|
{
|
|
|
|
|
Handle = handle;
|
|
|
|
|
Guid = guid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Public Members
|
|
|
|
|
|
2014-07-31 18:44:30 +00:00
|
|
|
|
public void ClearButtons()
|
|
|
|
|
{
|
|
|
|
|
State.ClearButtons();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-17 18:11:16 +00:00
|
|
|
|
public void SetAxis(HIDPage page, short usage, short value)
|
|
|
|
|
{
|
|
|
|
|
JoystickAxis axis = GetAxis(page, usage);
|
|
|
|
|
State.SetAxis(axis, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetButton(HIDPage page, short usage, bool value)
|
|
|
|
|
{
|
|
|
|
|
JoystickButton button = GetButton(page, usage);
|
|
|
|
|
State.SetButton(button, value);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 18:44:30 +00:00
|
|
|
|
public void SetHat(HIDPage page, short usage, HatPosition pos)
|
|
|
|
|
{
|
|
|
|
|
JoystickHat hat = GetHat(page, usage);
|
|
|
|
|
State.SetHat(hat, new JoystickHatState(pos));
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-18 15:00:27 +00:00
|
|
|
|
public void SetConnected(bool value)
|
|
|
|
|
{
|
|
|
|
|
Capabilities.SetIsConnected(value);
|
|
|
|
|
State.SetIsConnected(value);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-17 18:11:16 +00:00
|
|
|
|
public JoystickCapabilities GetCapabilities()
|
|
|
|
|
{
|
2014-07-31 18:44:30 +00:00
|
|
|
|
Capabilities = new JoystickCapabilities(
|
|
|
|
|
axes.Count, buttons.Count, hats.Count,
|
|
|
|
|
Capabilities.IsConnected);
|
2014-01-17 18:11:16 +00:00
|
|
|
|
return Capabilities;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 18:44:30 +00:00
|
|
|
|
internal void SetCapabilities(JoystickCapabilities caps)
|
|
|
|
|
{
|
|
|
|
|
Capabilities = caps;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-17 18:11:16 +00:00
|
|
|
|
public Guid GetGuid()
|
|
|
|
|
{
|
|
|
|
|
return Guid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JoystickState GetState()
|
|
|
|
|
{
|
|
|
|
|
return State;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Private Members
|
|
|
|
|
|
|
|
|
|
static int MakeKey(HIDPage page, short usage)
|
|
|
|
|
{
|
|
|
|
|
return ((ushort)page << 16) | unchecked((ushort)usage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JoystickAxis GetAxis(HIDPage page, short usage)
|
|
|
|
|
{
|
|
|
|
|
int key = MakeKey(page, usage);
|
|
|
|
|
if (!axes.ContainsKey(key))
|
|
|
|
|
{
|
2014-07-31 20:28:23 +00:00
|
|
|
|
JoystickAxis axis = HidHelper.TranslateJoystickAxis(page, usage);
|
|
|
|
|
axes.Add(key, axis);
|
2014-01-17 18:11:16 +00:00
|
|
|
|
}
|
|
|
|
|
return axes[key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JoystickButton GetButton(HIDPage page, short usage)
|
|
|
|
|
{
|
|
|
|
|
int key = MakeKey(page, usage);
|
|
|
|
|
if (!buttons.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
buttons.Add(key, JoystickButton.Button0 + buttons.Count);
|
|
|
|
|
}
|
|
|
|
|
return buttons[key];
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 18:44:30 +00:00
|
|
|
|
JoystickHat GetHat(HIDPage page, short usage)
|
|
|
|
|
{
|
|
|
|
|
int key = MakeKey(page, usage);
|
|
|
|
|
if (!hats.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
hats.Add(key, JoystickHat.Hat0 + hats.Count);
|
|
|
|
|
}
|
|
|
|
|
return hats[key];
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-17 18:11:16 +00:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Defines which types of HID devices we are interested in
|
|
|
|
|
readonly RawInputDevice[] DeviceTypes;
|
|
|
|
|
|
2014-01-17 00:27:07 +00:00
|
|
|
|
readonly IntPtr Window;
|
|
|
|
|
readonly object UpdateLock = new object();
|
2014-07-31 12:02:51 +00:00
|
|
|
|
readonly DeviceCollection<Device> Devices = new DeviceCollection<Device>();
|
2014-01-17 00:27:07 +00:00
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
byte[] HIDData = new byte[1024];
|
2014-01-18 15:00:27 +00:00
|
|
|
|
byte[] PreparsedData = new byte[1024];
|
|
|
|
|
HidProtocolData[] DataBuffer = new HidProtocolData[16];
|
|
|
|
|
|
2014-01-17 00:27:07 +00:00
|
|
|
|
public WinRawJoystick(IntPtr window)
|
|
|
|
|
{
|
2014-01-17 18:11:16 +00:00
|
|
|
|
Debug.WriteLine("Using WinRawJoystick.");
|
2014-01-17 00:27:07 +00:00
|
|
|
|
Debug.Indent();
|
|
|
|
|
|
|
|
|
|
if (window == IntPtr.Zero)
|
|
|
|
|
throw new ArgumentNullException("window");
|
|
|
|
|
|
|
|
|
|
Window = window;
|
2014-01-17 18:11:16 +00:00
|
|
|
|
DeviceTypes = new RawInputDevice[]
|
|
|
|
|
{
|
2014-07-31 20:52:50 +00:00
|
|
|
|
new RawInputDevice(HIDUsageGD.Joystick, RawInputDeviceFlags.DEVNOTIFY | RawInputDeviceFlags.INPUTSINK, window),
|
|
|
|
|
new RawInputDevice(HIDUsageGD.GamePad, RawInputDeviceFlags.DEVNOTIFY | RawInputDeviceFlags.INPUTSINK, window),
|
2014-01-17 18:11:16 +00:00
|
|
|
|
};
|
2014-01-18 15:00:27 +00:00
|
|
|
|
|
|
|
|
|
if (!Functions.RegisterRawInputDevices(DeviceTypes, DeviceTypes.Length, API.RawInputDeviceSize))
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Warning] Raw input registration failed with error: {0}.",
|
|
|
|
|
Marshal.GetLastWin32Error());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] Registered for raw input");
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-17 00:27:07 +00:00
|
|
|
|
RefreshDevices();
|
|
|
|
|
|
|
|
|
|
Debug.Unindent();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-17 18:11:16 +00:00
|
|
|
|
#region Public Members
|
|
|
|
|
|
2014-01-17 00:27:07 +00:00
|
|
|
|
public void RefreshDevices()
|
|
|
|
|
{
|
2014-01-18 15:00:27 +00:00
|
|
|
|
// Mark all devices as disconnected. We will check which of those
|
2014-07-31 12:02:51 +00:00
|
|
|
|
// are connected below.
|
|
|
|
|
foreach (var device in Devices)
|
2014-01-17 18:11:16 +00:00
|
|
|
|
{
|
2014-07-31 12:02:51 +00:00
|
|
|
|
device.SetConnected(false);
|
2014-01-17 18:11:16 +00:00
|
|
|
|
}
|
2014-01-18 15:00:27 +00:00
|
|
|
|
|
2014-07-31 12:02:51 +00:00
|
|
|
|
// Discover joystick devices
|
2014-01-18 15:00:27 +00:00
|
|
|
|
foreach (RawInputDeviceList dev in WinRawInput.GetDeviceList())
|
2014-01-17 18:11:16 +00:00
|
|
|
|
{
|
2014-07-31 12:02:51 +00:00
|
|
|
|
// Skip non-joystick devices
|
2014-01-18 15:00:27 +00:00
|
|
|
|
if (dev.Type != RawInputDeviceType.HID)
|
|
|
|
|
continue;
|
|
|
|
|
|
2014-07-31 21:03:24 +00:00
|
|
|
|
// We use the device handle as the hardware id.
|
|
|
|
|
// This works, but the handle will change whenever the
|
|
|
|
|
// device is unplugged/replugged. We compensate for this
|
|
|
|
|
// by checking device GUIDs, below.
|
|
|
|
|
// Note: we cannot use the GUID as the hardware id,
|
|
|
|
|
// because it is costly to query (and we need to query
|
|
|
|
|
// that every time we process a device event.)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
IntPtr handle = dev.Device;
|
|
|
|
|
Guid guid = GetDeviceGuid(handle);
|
2014-07-31 13:44:11 +00:00
|
|
|
|
long hardware_id = handle.ToInt64();
|
2014-01-18 15:00:27 +00:00
|
|
|
|
|
2014-07-31 12:02:51 +00:00
|
|
|
|
Device device = Devices.FromHardwareId(hardware_id);
|
|
|
|
|
if (device != null)
|
|
|
|
|
{
|
|
|
|
|
// We have already opened this device, mark it as connected
|
|
|
|
|
device.SetConnected(true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-07-31 18:44:30 +00:00
|
|
|
|
device = new Device(handle, guid);
|
|
|
|
|
|
2014-07-31 12:02:51 +00:00
|
|
|
|
// This is a new device, query its capabilities and add it
|
|
|
|
|
// to the device list
|
2014-07-31 18:44:30 +00:00
|
|
|
|
QueryDeviceCaps(device);
|
2014-07-31 12:02:51 +00:00
|
|
|
|
|
|
|
|
|
device.SetConnected(true);
|
2014-07-31 21:03:24 +00:00
|
|
|
|
|
|
|
|
|
// Check if a disconnected device with identical GUID already exists.
|
|
|
|
|
// If so, replace that device with this instance.
|
|
|
|
|
Device match = null;
|
|
|
|
|
foreach (Device candidate in Devices)
|
|
|
|
|
{
|
|
|
|
|
if (candidate.GetGuid() == guid && !candidate.GetCapabilities().IsConnected)
|
|
|
|
|
{
|
|
|
|
|
match = candidate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (match != null)
|
|
|
|
|
{
|
|
|
|
|
Devices.Remove(match.Handle.ToInt64());
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 12:02:51 +00:00
|
|
|
|
Devices.Add(hardware_id, device);
|
2014-01-18 15:00:27 +00:00
|
|
|
|
|
2014-07-31 18:44:30 +00:00
|
|
|
|
Debug.Print("[{0}] Connected joystick {1} ({2})",
|
|
|
|
|
GetType().Name, device.GetGuid(), device.GetCapabilities());
|
2014-07-31 12:02:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-17 18:11:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
public unsafe bool ProcessEvent(IntPtr raw)
|
2014-01-17 18:11:16 +00:00
|
|
|
|
{
|
2014-07-31 13:44:11 +00:00
|
|
|
|
// Query the size of the raw HID data buffer
|
|
|
|
|
int size = 0;
|
|
|
|
|
Functions.GetRawInputData(raw, GetRawInputDataEnum.INPUT, IntPtr.Zero, ref size, RawInputHeader.SizeInBytes);
|
|
|
|
|
if (size > HIDData.Length)
|
2014-01-17 18:11:16 +00:00
|
|
|
|
{
|
2014-07-31 13:44:11 +00:00
|
|
|
|
Array.Resize(ref HIDData, size);
|
2014-01-18 15:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
// Retrieve the raw HID data buffer
|
|
|
|
|
if (Functions.GetRawInputData(raw, HIDData) > 0)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
2014-07-31 13:44:11 +00:00
|
|
|
|
fixed (byte* pdata = HIDData)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
2014-07-31 13:44:11 +00:00
|
|
|
|
RawInput* rin = (RawInput*)pdata;
|
|
|
|
|
|
|
|
|
|
IntPtr handle = rin->Header.Device;
|
|
|
|
|
Device stick = GetDevice(handle);
|
|
|
|
|
if (stick == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] Unknown device {0}", handle);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!GetPreparsedData(handle, ref PreparsedData))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Query current state
|
|
|
|
|
// Allocate enough storage to hold the data of the current report
|
|
|
|
|
int report_count = HidProtocol.MaxDataListLength(HidProtocolReportType.Input, PreparsedData);
|
|
|
|
|
if (report_count == 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] HidProtocol.MaxDataListLength() failed with {0}",
|
|
|
|
|
Marshal.GetLastWin32Error());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fill the data buffer
|
|
|
|
|
if (DataBuffer.Length < report_count)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref DataBuffer, report_count);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 21:47:18 +00:00
|
|
|
|
UpdateAxes(rin, stick);
|
2014-07-31 20:50:33 +00:00
|
|
|
|
UpdateButtons(rin, stick);
|
2014-07-31 13:44:11 +00:00
|
|
|
|
return true;
|
2014-01-18 15:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
return false;
|
2014-01-18 15:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 21:47:18 +00:00
|
|
|
|
HatPosition GetHatPosition(uint value, HidProtocolValueCaps caps)
|
2014-07-31 20:28:23 +00:00
|
|
|
|
{
|
|
|
|
|
if (caps.LogicalMax == 8)
|
|
|
|
|
return (HatPosition)value;
|
|
|
|
|
else
|
|
|
|
|
return HatPosition.Centered;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 21:47:18 +00:00
|
|
|
|
unsafe void UpdateAxes(RawInput* rin, Device stick)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < stick.AxisCaps.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (stick.AxisCaps[i].IsRange)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[{0}] Axis range collections not implemented. Please report your controller type at http://www.opentk.com",
|
|
|
|
|
GetType().Name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HIDPage page = stick.AxisCaps[i].UsagePage;
|
|
|
|
|
short usage = stick.AxisCaps[i].NotRange.Usage;
|
|
|
|
|
uint value = 0;
|
|
|
|
|
|
|
|
|
|
HidProtocolStatus status = HidProtocol.GetUsageValue(
|
|
|
|
|
HidProtocolReportType.Input,
|
|
|
|
|
page, 0, usage, ref value,
|
|
|
|
|
PreparsedData,
|
|
|
|
|
new IntPtr((void*)&rin->Data.HID.RawData),
|
|
|
|
|
rin->Data.HID.Size);
|
|
|
|
|
|
|
|
|
|
if (status != HidProtocolStatus.Success)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[{0}] HidProtocol.GetScaledUsageValue() failed. Error: {1}",
|
|
|
|
|
GetType().Name, status);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (page == HIDPage.GenericDesktop && (HIDUsageGD)usage == HIDUsageGD.Hatswitch)
|
|
|
|
|
{
|
|
|
|
|
stick.SetHat(page, usage, GetHatPosition(value, stick.AxisCaps[i]));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
short scaled_value = (short)HidHelper.ScaleValue(
|
|
|
|
|
(int)((long)value + stick.AxisCaps[i].LogicalMin),
|
|
|
|
|
stick.AxisCaps[i].LogicalMin, stick.AxisCaps[i].LogicalMax,
|
|
|
|
|
Int16.MinValue, Int16.MaxValue);
|
|
|
|
|
stick.SetAxis(page, usage, scaled_value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 20:50:33 +00:00
|
|
|
|
unsafe void UpdateButtons(RawInput* rin, Device stick)
|
2014-07-31 18:44:30 +00:00
|
|
|
|
{
|
|
|
|
|
stick.ClearButtons();
|
|
|
|
|
|
2014-07-31 20:50:33 +00:00
|
|
|
|
for (int i = 0; i < stick.ButtonCaps.Count; i++)
|
2014-07-31 18:44:30 +00:00
|
|
|
|
{
|
2014-07-31 20:28:23 +00:00
|
|
|
|
short* usage_list = stackalloc short[(int)JoystickButton.Last + 1];
|
2014-07-31 18:44:30 +00:00
|
|
|
|
int usage_length = (int)JoystickButton.Last;
|
2014-07-31 20:50:33 +00:00
|
|
|
|
HIDPage page = stick.ButtonCaps[i].UsagePage;
|
2014-07-31 18:44:30 +00:00
|
|
|
|
|
|
|
|
|
HidProtocolStatus status = HidProtocol.GetUsages(
|
|
|
|
|
HidProtocolReportType.Input,
|
|
|
|
|
page, 0, usage_list, ref usage_length,
|
|
|
|
|
PreparsedData,
|
|
|
|
|
new IntPtr((void*)&rin->Data.HID.RawData),
|
|
|
|
|
rin->Data.HID.Size);
|
|
|
|
|
|
|
|
|
|
if (status != HidProtocolStatus.Success)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] HidProtocol.GetUsages() failed with {0}",
|
|
|
|
|
Marshal.GetLastWin32Error());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < usage_length; j++)
|
|
|
|
|
{
|
2014-07-31 20:28:23 +00:00
|
|
|
|
short usage = *(usage_list + j);
|
|
|
|
|
stick.SetButton(page, usage, true);
|
2014-07-31 18:44:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-18 15:53:30 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Private Members
|
|
|
|
|
|
2014-01-18 15:00:27 +00:00
|
|
|
|
static bool GetPreparsedData(IntPtr handle, ref byte[] prepared_data)
|
|
|
|
|
{
|
2014-01-17 18:11:16 +00:00
|
|
|
|
// Query the size of the _HIDP_PREPARSED_DATA structure for this event.
|
|
|
|
|
int preparsed_size = 0;
|
2014-01-18 15:00:27 +00:00
|
|
|
|
Functions.GetRawInputDeviceInfo(handle, RawInputDeviceInfoEnum.PREPARSEDDATA,
|
2014-01-17 18:11:16 +00:00
|
|
|
|
IntPtr.Zero, ref preparsed_size);
|
|
|
|
|
if (preparsed_size == 0)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] Functions.GetRawInputDeviceInfo(PARSEDDATA) failed with {0}",
|
|
|
|
|
Marshal.GetLastWin32Error());
|
2014-01-17 18:11:16 +00:00
|
|
|
|
return false;
|
2014-01-18 15:00:27 +00:00
|
|
|
|
}
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
|
|
|
|
// Allocate space for _HIDP_PREPARSED_DATA.
|
|
|
|
|
// This is an untyped blob of data.
|
2014-01-18 15:00:27 +00:00
|
|
|
|
if (prepared_data.Length < preparsed_size)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref prepared_data, preparsed_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Functions.GetRawInputDeviceInfo(handle, RawInputDeviceInfoEnum.PREPARSEDDATA,
|
|
|
|
|
prepared_data, ref preparsed_size) < 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] Functions.GetRawInputDeviceInfo(PARSEDDATA) failed with {0}",
|
|
|
|
|
Marshal.GetLastWin32Error());
|
2014-01-17 18:11:16 +00:00
|
|
|
|
return false;
|
2014-01-18 15:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 18:44:30 +00:00
|
|
|
|
void QueryDeviceCaps(Device stick)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
|
|
|
|
HidProtocolCaps caps;
|
2014-07-31 07:49:43 +00:00
|
|
|
|
|
2014-07-31 18:44:30 +00:00
|
|
|
|
// Discovered elements
|
|
|
|
|
int axes = 0;
|
|
|
|
|
int dpads = 0;
|
|
|
|
|
int buttons = 0;
|
|
|
|
|
|
|
|
|
|
if (GetPreparsedData(stick.Handle, ref PreparsedData) &&
|
2014-07-31 20:50:33 +00:00
|
|
|
|
GetDeviceCaps(stick, PreparsedData, out caps))
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
2014-07-31 20:50:33 +00:00
|
|
|
|
for (int i = 0; i < stick.AxisCaps.Count; i++)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
2014-07-31 20:50:33 +00:00
|
|
|
|
if (stick.AxisCaps[i].IsRange)
|
2014-07-31 07:49:43 +00:00
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] Range axis elements not implemented.");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-01-18 15:00:27 +00:00
|
|
|
|
|
2014-07-31 20:50:33 +00:00
|
|
|
|
HIDPage page = stick.AxisCaps[i].UsagePage;
|
2014-07-31 18:44:30 +00:00
|
|
|
|
switch (page)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
|
|
|
|
case HIDPage.GenericDesktop:
|
2014-07-31 20:50:33 +00:00
|
|
|
|
switch ((HIDUsageGD)stick.AxisCaps[i].NotRange.Usage)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
|
|
|
|
case HIDUsageGD.X:
|
|
|
|
|
case HIDUsageGD.Y:
|
|
|
|
|
case HIDUsageGD.Z:
|
|
|
|
|
case HIDUsageGD.Rx:
|
|
|
|
|
case HIDUsageGD.Ry:
|
|
|
|
|
case HIDUsageGD.Rz:
|
|
|
|
|
case HIDUsageGD.Slider:
|
|
|
|
|
case HIDUsageGD.Dial:
|
|
|
|
|
case HIDUsageGD.Wheel:
|
2014-07-31 20:50:33 +00:00
|
|
|
|
stick.SetAxis(page, stick.AxisCaps[i].NotRange.Usage, 0);
|
2014-01-18 15:00:27 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case HIDUsageGD.Hatswitch:
|
2014-07-31 20:50:33 +00:00
|
|
|
|
stick.SetHat(page, stick.AxisCaps[i].NotRange.Usage, HatPosition.Centered);
|
2014-01-18 15:00:27 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case HIDPage.Simulation:
|
2014-07-31 20:50:33 +00:00
|
|
|
|
switch ((HIDUsageSim)stick.AxisCaps[i].NotRange.Usage)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
|
|
|
|
case HIDUsageSim.Rudder:
|
|
|
|
|
case HIDUsageSim.Throttle:
|
2014-07-31 20:50:33 +00:00
|
|
|
|
stick.SetAxis(page, stick.AxisCaps[i].NotRange.Usage, 0);
|
2014-01-18 15:00:27 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 20:50:33 +00:00
|
|
|
|
for (int i = 0; i < stick.ButtonCaps.Count; i++)
|
2014-07-31 07:49:43 +00:00
|
|
|
|
{
|
2014-07-31 20:50:33 +00:00
|
|
|
|
bool is_range = stick.ButtonCaps[i].IsRange;
|
|
|
|
|
HIDPage page = stick.ButtonCaps[i].UsagePage;
|
2014-07-31 07:49:43 +00:00
|
|
|
|
switch (page)
|
|
|
|
|
{
|
|
|
|
|
case HIDPage.Button:
|
|
|
|
|
if (is_range)
|
|
|
|
|
{
|
2014-07-31 20:50:33 +00:00
|
|
|
|
for (short usage = stick.ButtonCaps[i].Range.UsageMin; usage <= stick.ButtonCaps[i].Range.UsageMax; usage++)
|
2014-07-31 18:44:30 +00:00
|
|
|
|
{
|
|
|
|
|
buttons++;
|
|
|
|
|
stick.SetButton(page, usage, false);
|
|
|
|
|
}
|
2014-07-31 07:49:43 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
buttons++;
|
2014-07-31 20:50:33 +00:00
|
|
|
|
stick.SetButton(page, stick.ButtonCaps[i].NotRange.Usage, false);
|
2014-07-31 07:49:43 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
Debug.Print("[WinRawJoystick] Unknown HIDPage {0} for button.", page);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-18 15:00:27 +00:00
|
|
|
|
}
|
2014-07-31 18:44:30 +00:00
|
|
|
|
|
|
|
|
|
stick.SetCapabilities(new JoystickCapabilities(axes, buttons, dpads, true));
|
2014-01-18 15:00:27 +00:00
|
|
|
|
}
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
2014-07-31 20:50:33 +00:00
|
|
|
|
static bool GetDeviceCaps(Device stick, byte[] preparsed_data, out HidProtocolCaps caps)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
2014-07-31 20:50:33 +00:00
|
|
|
|
int axis_caps_count = 0;
|
|
|
|
|
int button_caps_count = 0;
|
2014-07-31 07:49:43 +00:00
|
|
|
|
|
2014-01-17 18:11:16 +00:00
|
|
|
|
// Query joystick capabilities
|
2014-01-18 15:00:27 +00:00
|
|
|
|
caps = new HidProtocolCaps();
|
2014-01-17 18:11:16 +00:00
|
|
|
|
if (HidProtocol.GetCaps(preparsed_data, ref caps) != HidProtocolStatus.Success)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] HidProtocol.GetCaps() failed with {0}",
|
|
|
|
|
Marshal.GetLastWin32Error());
|
2014-01-17 18:11:16 +00:00
|
|
|
|
return false;
|
2014-01-18 15:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure our caps arrays are big enough
|
2014-07-31 20:50:33 +00:00
|
|
|
|
HidProtocolValueCaps[] axis_caps = new HidProtocolValueCaps[caps.NumberInputValueCaps];
|
|
|
|
|
HidProtocolButtonCaps[] button_caps = new HidProtocolButtonCaps[caps.NumberInputButtonCaps];
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
|
|
|
|
// Axis capabilities
|
2014-07-31 07:49:43 +00:00
|
|
|
|
ushort axis_count = (ushort)axis_caps.Length;
|
2014-01-17 18:11:16 +00:00
|
|
|
|
if (HidProtocol.GetValueCaps(HidProtocolReportType.Input,
|
2014-07-31 07:49:43 +00:00
|
|
|
|
axis_caps, ref axis_count, preparsed_data) !=
|
2014-01-17 18:11:16 +00:00
|
|
|
|
HidProtocolStatus.Success)
|
|
|
|
|
{
|
2014-01-18 15:00:27 +00:00
|
|
|
|
Debug.Print("[WinRawJoystick] HidProtocol.GetValueCaps() failed with {0}",
|
|
|
|
|
Marshal.GetLastWin32Error());
|
2014-01-17 18:11:16 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-07-31 07:49:43 +00:00
|
|
|
|
axis_caps_count = (int)axis_count;
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
|
|
|
|
// Button capabilities
|
2014-07-31 07:49:43 +00:00
|
|
|
|
ushort button_count = (ushort)button_caps.Length;
|
2014-01-17 18:11:16 +00:00
|
|
|
|
if (HidProtocol.GetButtonCaps(HidProtocolReportType.Input,
|
2014-07-31 07:49:43 +00:00
|
|
|
|
button_caps, ref button_count, preparsed_data) !=
|
2014-01-17 18:11:16 +00:00
|
|
|
|
HidProtocolStatus.Success)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] HidProtocol.GetButtonCaps() failed with {0}",
|
|
|
|
|
Marshal.GetLastWin32Error());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-07-31 07:49:43 +00:00
|
|
|
|
button_caps_count = (int)button_count;
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
2014-07-31 20:50:33 +00:00
|
|
|
|
stick.AxisCaps.Clear();
|
|
|
|
|
stick.AxisCaps.AddRange(axis_caps);
|
|
|
|
|
|
|
|
|
|
stick.ButtonCaps.Clear();
|
|
|
|
|
stick.ButtonCaps.AddRange(button_caps);
|
|
|
|
|
|
2014-01-18 15:00:27 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
2014-01-18 15:00:27 +00:00
|
|
|
|
// Retrieves the GUID of a device, which is stored
|
|
|
|
|
// in the last part of the DEVICENAME string
|
|
|
|
|
Guid GetDeviceGuid(IntPtr handle)
|
|
|
|
|
{
|
|
|
|
|
Guid guid = new Guid();
|
|
|
|
|
|
|
|
|
|
unsafe
|
2014-01-17 18:11:16 +00:00
|
|
|
|
{
|
2014-01-18 15:00:27 +00:00
|
|
|
|
// Find out how much memory we need to allocate
|
|
|
|
|
// for the DEVICENAME string
|
|
|
|
|
int size = 0;
|
|
|
|
|
if (Functions.GetRawInputDeviceInfo(handle, RawInputDeviceInfoEnum.DEVICENAME, IntPtr.Zero, ref size) < 0 || size == 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] Functions.GetRawInputDeviceInfo(DEVICENAME) failed with error {0}",
|
|
|
|
|
Marshal.GetLastWin32Error());
|
|
|
|
|
return guid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allocate memory and retrieve the DEVICENAME string
|
2014-07-31 12:02:51 +00:00
|
|
|
|
sbyte* pname = stackalloc sbyte[size + 1];
|
2014-01-18 15:00:27 +00:00
|
|
|
|
if (Functions.GetRawInputDeviceInfo(handle, RawInputDeviceInfoEnum.DEVICENAME, (IntPtr)pname, ref size) < 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] Functions.GetRawInputDeviceInfo(DEVICENAME) failed with error {0}",
|
|
|
|
|
Marshal.GetLastWin32Error());
|
|
|
|
|
return guid;
|
|
|
|
|
}
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
2014-01-18 15:00:27 +00:00
|
|
|
|
// Convert the buffer to a .Net string, and split it into parts
|
|
|
|
|
string name = new string(pname);
|
|
|
|
|
if (String.IsNullOrEmpty(name))
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[WinRawJoystick] Failed to construct device name");
|
|
|
|
|
return guid;
|
|
|
|
|
}
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
2014-01-18 15:00:27 +00:00
|
|
|
|
// The GUID is stored in the last part of the string
|
|
|
|
|
string[] parts = name.Split('#');
|
2014-07-31 12:02:51 +00:00
|
|
|
|
if (parts.Length > 3)
|
2014-01-18 15:00:27 +00:00
|
|
|
|
{
|
2014-07-31 12:02:51 +00:00
|
|
|
|
guid = new Guid(parts[3]);
|
2014-01-18 15:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
2014-01-18 15:00:27 +00:00
|
|
|
|
return guid;
|
2014-01-17 18:11:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Device GetDevice(IntPtr handle)
|
|
|
|
|
{
|
2014-07-31 12:02:51 +00:00
|
|
|
|
long hardware_id = handle.ToInt64();
|
2014-01-17 18:11:16 +00:00
|
|
|
|
bool is_device_known = false;
|
|
|
|
|
|
|
|
|
|
lock (UpdateLock)
|
|
|
|
|
{
|
2014-07-31 12:02:51 +00:00
|
|
|
|
is_device_known = Devices.FromHardwareId(hardware_id) != null;
|
2014-01-17 18:11:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_device_known)
|
|
|
|
|
{
|
|
|
|
|
RefreshDevices();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock (UpdateLock)
|
|
|
|
|
{
|
2014-07-31 12:02:51 +00:00
|
|
|
|
return Devices.FromHardwareId(hardware_id);
|
2014-01-17 18:11:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IsValid(int index)
|
|
|
|
|
{
|
2014-07-31 12:02:51 +00:00
|
|
|
|
return Devices.FromIndex(index) != null;
|
2014-01-17 18:11:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IJoystickDriver2 Members
|
|
|
|
|
|
2014-01-17 00:27:07 +00:00
|
|
|
|
public JoystickState GetState(int index)
|
|
|
|
|
{
|
2014-01-17 18:11:16 +00:00
|
|
|
|
lock (UpdateLock)
|
|
|
|
|
{
|
|
|
|
|
if (IsValid(index))
|
|
|
|
|
{
|
2014-07-31 12:02:51 +00:00
|
|
|
|
return Devices.FromIndex(index).GetState();
|
2014-01-17 18:11:16 +00:00
|
|
|
|
}
|
|
|
|
|
return new JoystickState();
|
|
|
|
|
}
|
2014-01-17 00:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JoystickCapabilities GetCapabilities(int index)
|
|
|
|
|
{
|
2014-01-17 18:11:16 +00:00
|
|
|
|
lock (UpdateLock)
|
|
|
|
|
{
|
|
|
|
|
if (IsValid(index))
|
|
|
|
|
{
|
2014-07-31 12:02:51 +00:00
|
|
|
|
return Devices.FromIndex(index).GetCapabilities();
|
2014-01-17 18:11:16 +00:00
|
|
|
|
}
|
|
|
|
|
return new JoystickCapabilities();
|
|
|
|
|
}
|
2014-01-17 00:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Guid GetGuid(int index)
|
|
|
|
|
{
|
2014-01-17 18:11:16 +00:00
|
|
|
|
lock (UpdateLock)
|
|
|
|
|
{
|
|
|
|
|
if (IsValid(index))
|
|
|
|
|
{
|
2014-07-31 12:02:51 +00:00
|
|
|
|
return Devices.FromIndex(index).GetGuid();
|
2014-01-17 18:11:16 +00:00
|
|
|
|
}
|
|
|
|
|
return new Guid();
|
|
|
|
|
}
|
2014-01-17 00:27:07 +00:00
|
|
|
|
}
|
2014-01-17 18:11:16 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
2014-01-17 00:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|