// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// 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.
using System;
using System.Collections.Generic;
using System.Threading;
namespace SharpDX.DirectInput
{
public partial class Device
{
private DeviceProperties _properties;
///
/// Initializes a new instance of the class based on a given globally unique identifier (Guid).
///
/// The direct input.
/// The device GUID.
protected Device(DirectInput directInput, Guid deviceGuid)
{
IntPtr temp;
directInput.CreateDevice(deviceGuid, out temp, null);
NativePointer = temp;
}
///
/// Gets the device properties.
///
/// The device properties.
public DeviceProperties Properties
{
get
{
if (_properties == null)
_properties = new DeviceProperties(this);
return _properties;
}
}
///
/// Sends a hardware-specific command to the force-feedback driver.
///
/// Driver-specific command number. Consult the driver documentation for a list of valid commands.
/// Buffer containing the data required to perform the operation.
/// Buffer in which the operation's output data is returned.
/// Number of bytes written to the output buffer
///
/// Because each driver implements different escapes, it is the application's responsibility to ensure that it is sending the escape to the correct driver by comparing the value of the guidFFDriver member of the structure against the value the application is expecting.
///
public int Escape(int command, byte[] inData, byte[] outData)
{
var effectEscape = new EffectEscape();
unsafe
{
fixed (void* pInData = &inData[0])
fixed (void* pOutData = &outData[0])
{
effectEscape.Size = sizeof (EffectEscape);
effectEscape.Command = command;
effectEscape.InBufferPointer = (IntPtr) pInData;
effectEscape.InBufferSize = inData.Length;
effectEscape.OutBufferPointer = (IntPtr) pOutData;
effectEscape.OutBufferSize = outData.Length;
Escape(ref effectEscape);
return effectEscape.OutBufferSize;
}
}
}
///
/// Gets information about a device image for use in a configuration property sheet.
///
/// A structure that receives information about the device image.
public DeviceImageHeader GetDeviceImages()
{
var imageHeader = new DeviceImageHeader();
GetImageInfo(imageHeader);
if (imageHeader.BufferUsed > 0)
{
unsafe
{
imageHeader.BufferSize = imageHeader.BufferUsed;
var pImages = stackalloc DeviceImage.__Native[imageHeader.BufferSize/sizeof (DeviceImage.__Native)];
imageHeader.ImageInfoArrayPointer = (IntPtr)pImages;
}
GetImageInfo(imageHeader);
}
return imageHeader;
}
///
/// Gets all effects.
///
/// A collection of
public IList GetEffects()
{
return GetEffects(EffectType.All);
}
///
/// Gets the effects for a particular .
///
/// Effect type.
/// A collection of
public IList GetEffects(EffectType effectType)
{
var enumEffectsCallback = new EnumEffectsCallback();
EnumEffects(enumEffectsCallback.NativePointer, IntPtr.Zero, effectType);
return enumEffectsCallback.EffectInfos;
}
///
/// Gets the effects stored in a RIFF Force Feedback file.
///
/// Name of the file.
/// A collection of
public IList GetEffectsInFile(string fileName)
{
return GetEffectsInFile(fileName, EffectFileFlags.None);
}
///
/// Gets the effects stored in a RIFF Force Feedback file.
///
/// Name of the file.
/// Flags used to filter effects.
/// A collection of
public IList GetEffectsInFile(string fileName, EffectFileFlags effectFileFlags)
{
var enumEffectsInFileCallback = new EnumEffectsInFileCallback();
EnumEffectsInFile(fileName, enumEffectsInFileCallback.NativePointer, IntPtr.Zero, effectFileFlags);
return enumEffectsInFileCallback.EffectsInFile;
}
///
/// Gets information about a device object, such as a button or axis.
///
/// The object type/instance identifier. This identifier is returned in the member of the structure returned from a previous call to the method.
/// A information
public DeviceObjectInstance GetObjectInfoById(DeviceObjectId objectId)
{
return GetObjectInfo((int)objectId, PropertyHowType.Byid);
}
///
/// Gets information about a device object, such as a button or axis.
///
/// the HID Usage Page and Usage values.
/// A information
public DeviceObjectInstance GetObjectInfoByUsage(int usageCode)
{
return GetObjectInfo(usageCode, PropertyHowType.Byusage);
}
///
/// Gets the properties about a device object, such as a button or axis.
///
/// The object type/instance identifier. This identifier is returned in the member of the structure returned from a previous call to the method.
/// an ObjectProperties
public ObjectProperties GetObjectPropertiesById(DeviceObjectId objectId)
{
return new ObjectProperties(this, (int)objectId, PropertyHowType.Byid);
}
///
/// Gets the properties about a device object, such as a button or axis.
///
/// the HID Usage Page and Usage values.
/// an ObjectProperties
public ObjectProperties GetObjectPropertiesByUsage(int usageCode)
{
return new ObjectProperties(this, usageCode, PropertyHowType.Byusage);
}
///
/// Retrieves a collection of objects on the device.
///
/// A collection of all device objects on the device.
public IList GetObjects()
{
return GetObjects(DeviceObjectTypeFlags.All);
}
///
/// Retrieves a collection of objects on the device.
///
/// A filter for the returned device objects collection.
/// A collection of device objects matching the specified filter.
public IList GetObjects(DeviceObjectTypeFlags deviceObjectTypeFlag)
{
var enumEffectsInFileCallback = new EnumObjectsCallback();
EnumObjects(enumEffectsInFileCallback.NativePointer, IntPtr.Zero, (int)deviceObjectTypeFlag);
return enumEffectsInFileCallback.Objects;
}
///
/// Runs the DirectInput control panel associated with this device. If the device does not have a control panel associated with it, the default device control panel is launched.
///
/// A object describing the result of the operation.
public void RunControlPanel()
{
RunControlPanel(IntPtr.Zero, 0);
}
///
/// Runs the DirectInput control panel associated with this device. If the device does not have a control panel associated with it, the default device control panel is launched.
///
/// The parent control.
/// A object describing the result of the operation.
public void RunControlPanel(IntPtr parentHwnd)
{
RunControlPanel(parentHwnd, 0);
}
// Disabled as it seems to be not used anymore
/////
///// Sends data to a device that accepts output. Applications should not use this method. Force Feedback is the recommended way to send data to a device. If you want to send other data to a device, such as changing LED or internal device states, the HID application programming interface (API) is the recommended way.
/////
///// An array of object data.
///// if set to true the device data sent is overlaid on the previously sent device data..
///// A object describing the result of the operation.
//public Result SendData(ObjectData[] data, bool overlay)
//{
// unsafe
// {
// int count = data==null?0:data.Length;
// return SendDeviceData(sizeof (ObjectData), data, ref count, overlay ? 1 : 0);
// }
//}
///
/// Specifies an event that is to be set when the device state changes. It is also used to turn off event notification.
///
/// Handle to the event that is to be set when the device state changes. DirectInput uses the Microsoft Win32 SetEvent function on the handle when the state of the device changes. If the eventHandle parameter is null, notification is disabled.
/// A object describing the result of the operation.
public void SetNotification(WaitHandle eventHandle)
{
Microsoft.Win32.SafeHandles.SafeWaitHandle safeHandle;
#if NET45 || BEFORE_NET45
safeHandle = eventHandle?.SafeWaitHandle;
#else
safeHandle = eventHandle?.GetSafeWaitHandle();
#endif
SetEventNotification(safeHandle?.DangerousGetHandle() ?? IntPtr.Zero);
}
///
/// Writes the effects to a file.
///
/// Name of the file.
/// The effects.
/// A object describing the result of the operation.
public void WriteEffectsToFile(string fileName, EffectFile[] effects)
{
WriteEffectsToFile(fileName, effects, false);
}
///
/// Writes the effects to file.
///
/// Name of the file.
/// The effects.
/// if set to true [include nonstandard effects].
/// A object describing the result of the operation.
public void WriteEffectsToFile(string fileName, EffectFile[] effects, bool includeNonstandardEffects)
{
WriteEffectToFile(fileName, effects.Length, effects, (int)(includeNonstandardEffects?EffectFileFlags.IncludeNonStandard:0));
}
///
/// Gets the created effects.
///
/// The created effects.
public IList CreatedEffects
{
get
{
var enumCreatedEffectsCallback = new EnumCreatedEffectsCallback();
EnumCreatedEffectObjects(enumCreatedEffectsCallback.NativePointer, IntPtr.Zero, 0);
return enumCreatedEffectsCallback.Effects;
}
}
}
}