forked from sharpdx/SharpDX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevice.cs
More file actions
316 lines (290 loc) · 16.5 KB
/
Device.cs
File metadata and controls
316 lines (290 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// 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;
using System.Windows.Forms;
namespace SharpDX.DirectInput
{
public partial class Device
{
private DeviceProperties _properties;
/// <summary>
/// Initializes a new instance of the <see cref="Device"/> class based on a given globally unique identifier (Guid).
/// </summary>
/// <param name="directInput">The direct input.</param>
/// <param name="deviceGuid">The device GUID.</param>
protected Device(DirectInput directInput, Guid deviceGuid)
{
IntPtr temp;
directInput.CreateDevice(deviceGuid, out temp, null);
NativePointer = temp;
}
/// <summary>
/// Gets the device properties.
/// </summary>
/// <value>The device properties.</value>
public DeviceProperties Properties
{
get
{
if (_properties == null)
_properties = new DeviceProperties(this);
return _properties;
}
}
/// <summary>
/// Sends a hardware-specific command to the force-feedback driver.
/// </summary>
/// <param name="command">Driver-specific command number. Consult the driver documentation for a list of valid commands. </param>
/// <param name="inData">Buffer containing the data required to perform the operation. </param>
/// <param name="outData">Buffer in which the operation's output data is returned. </param>
/// <returns>Number of bytes written to the output buffer</returns>
/// <remarks>
/// 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 <see cref="DeviceInstance"/> structure against the value the application is expecting.
/// </remarks>
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;
}
}
}
/// <summary>
/// Gets information about a device image for use in a configuration property sheet.
/// </summary>
/// <returns>A structure that receives information about the device image.</returns>
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;
}
/// <summary>
/// Gets all effects.
/// </summary>
/// <returns>A collection of <see cref="EffectInfo"/></returns>
public IList<EffectInfo> GetEffects()
{
return GetEffects(EffectType.All);
}
/// <summary>
/// Gets the effects for a particular <see cref="EffectType"/>.
/// </summary>
/// <param name="effectType">Effect type.</param>
/// <returns>A collection of <see cref="EffectInfo"/></returns>
public IList<EffectInfo> GetEffects(EffectType effectType)
{
var enumEffectsCallback = new EnumEffectsCallback();
EnumEffects(enumEffectsCallback.NativePointer, IntPtr.Zero, effectType);
return enumEffectsCallback.EffectInfos;
}
/// <summary>
/// Gets the effects stored in a RIFF Force Feedback file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns>A collection of <see cref="EffectFile"/></returns>
public IList<EffectFile> GetEffectsInFile(string fileName)
{
return GetEffectsInFile(fileName, EffectFileFlags.None);
}
/// <summary>
/// Gets the effects stored in a RIFF Force Feedback file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="effectFileFlags">Flags used to filter effects.</param>
/// <returns>A collection of <see cref="EffectFile"/></returns>
public IList<EffectFile> GetEffectsInFile(string fileName, EffectFileFlags effectFileFlags)
{
var enumEffectsInFileCallback = new EnumEffectsInFileCallback();
EnumEffectsInFile(fileName, enumEffectsInFileCallback.NativePointer, IntPtr.Zero, effectFileFlags);
return enumEffectsInFileCallback.EffectsInFile;
}
/// <summary>
/// Gets information about a device object, such as a button or axis.
/// </summary>
/// <param name="objectId">The object type/instance identifier. This identifier is returned in the <see cref="DeviceObjectInstance.ObjectId"/> member of the <see cref="DeviceObjectInstance"/> structure returned from a previous call to the <see cref="GetObjects()"/> method.</param>
/// <returns>A <see cref="DeviceObjectInstance"/> information</returns>
public DeviceObjectInstance GetObjectInfoById(DeviceObjectId objectId)
{
return GetObjectInfo((int)objectId, PropertyHowType.Byid);
}
/// <summary>
/// Gets information about a device object, such as a button or axis.
/// </summary>
/// <param name="usageCode">the HID Usage Page and Usage values.</param>
/// <returns>A <see cref="DeviceObjectInstance"/> information</returns>
public DeviceObjectInstance GetObjectInfoByUsage(int usageCode)
{
return GetObjectInfo(usageCode, PropertyHowType.Byusage);
}
/// <summary>
/// Gets the properties about a device object, such as a button or axis.
/// </summary>
/// <param name="objectId">The object type/instance identifier. This identifier is returned in the <see cref="DeviceObjectInstance.Type"/> member of the <see cref="DeviceObjectInstance"/> structure returned from a previous call to the <see cref="GetObjects()"/> method.</param>
/// <returns>an ObjectProperties</returns>
public ObjectProperties GetObjectPropertiesById(DeviceObjectId objectId)
{
return new ObjectProperties(this, (int)objectId, PropertyHowType.Byid);
}
/// <summary>
/// Gets the properties about a device object, such as a button or axis.
/// </summary>
/// <param name="usageCode">the HID Usage Page and Usage values.</param>
/// <returns>an ObjectProperties</returns>
public ObjectProperties GetObjectPropertiesByUsage(int usageCode)
{
return new ObjectProperties(this, usageCode, PropertyHowType.Byusage);
}
/// <summary>
/// Retrieves a collection of objects on the device.
/// </summary>
/// <returns>A collection of all device objects on the device.</returns>
public IList<DeviceObjectInstance> GetObjects()
{
return GetObjects(DeviceObjectTypeFlags.All);
}
/// <summary>
/// Retrieves a collection of objects on the device.
/// </summary>
/// <param name="deviceObjectTypeFlag">A filter for the returned device objects collection.</param>
/// <returns>A collection of device objects matching the specified filter.</returns>
public IList<DeviceObjectInstance> GetObjects(DeviceObjectTypeFlags deviceObjectTypeFlag)
{
var enumEffectsInFileCallback = new EnumObjectsCallback();
EnumObjects(enumEffectsInFileCallback.NativePointer, IntPtr.Zero, (int)deviceObjectTypeFlag);
return enumEffectsInFileCallback.Objects;
}
/// <summary>
/// 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.
/// </summary>
/// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns>
public void RunControlPanel()
{
RunControlPanel(IntPtr.Zero, 0);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="parent">The parent control.</param>
/// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns>
public void RunControlPanel(Control parent)
{
RunControlPanel(parent.Handle, 0);
}
// Disabled as it seems to be not used anymore
///// <summary>
///// 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.
///// </summary>
///// <param name="data">An array of object data.</param>
///// <param name="overlay">if set to <c>true</c> the device data sent is overlaid on the previously sent device data..</param>
///// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns>
//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);
// }
//}
/// <summary>
/// Requests the cooperative level for this instance of the input device. The cooperative level determines how this instance of the device interacts with other instances of the device and the rest of the system.
/// </summary>
/// <param name="control">Window control to be associated with the device. This parameter must be a valid top-level window handle that belongs to the process. The window associated with the device must not be destroyed while it is still active in a DirectInput device.</param>
/// <param name="level">Flags that specify the cooperative level to associate with the input device.</param>
/// <returns>If the method succeeds, the return value is <see cref="SharpDX.DirectInput.ResultCode.Ok"/>. If the method fails, a <see cref="SharpDXException"/> is raised with the following error code values: <see cref="SharpDX.DirectInput.ResultCode.InvalidParam"/>, <see cref="SharpDX.DirectInput.ResultCode.NotInitialized"/>, <see cref="Result.Handle"/>.</returns>
/// <remarks>
/// <para>Applications cannot specify <see cref="SharpDX.DirectInput.CooperativeLevel.Foreground"/> and <see cref="SharpDX.DirectInput.CooperativeLevel.Background"/> at the same time. This apply as well to <see cref="SharpDX.DirectInput.CooperativeLevel.Exclusive"/> and <see cref="SharpDX.DirectInput.CooperativeLevel.NonExclusive"/>.</para>
/// <para>When the mouse is acquired with exclusive access, the mouse pointer is removed from the screen until the device is unacquired.</para>
/// <para>Applications that select the background exclusive mode cooperative level are not guaranteed to retain access to the device if another application requests exclusive access. When a background exclusive mode application loses access, calls to DirectInput device methods will fail and return <see cref="SharpDX.DirectInput.ResultCode.NotAcquired"/>. The application can regain access to the device by manually unacquiring the device and reacquiring it.</para>
/// <para>Applications must call this method before acquiring the device by using the <see cref="SharpDX.DirectInput.Device"/> method.</para>
/// </remarks>
/// <unmanaged>HRESULT IDirectInputDevice8W::SetCooperativeLevel([In] HWND arg0,[In] DISCL arg1)</unmanaged>
public void SetCooperativeLevel(Control control, CooperativeLevel level)
{
SetCooperativeLevel(control.Handle, level);
}
/// <summary>
/// Specifies an event that is to be set when the device state changes. It is also used to turn off event notification.
/// </summary>
/// <param name="eventHandle">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.</param>
/// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns>
public void SetNotification(WaitHandle eventHandle)
{
SetEventNotification(eventHandle!=null?eventHandle.SafeWaitHandle.DangerousGetHandle():IntPtr.Zero);
}
/// <summary>
/// Writes the effects to a file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="effects">The effects.</param>
/// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns>
public void WriteEffectsToFile(string fileName, EffectFile[] effects)
{
WriteEffectsToFile(fileName, effects, false);
}
/// <summary>
/// Writes the effects to file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="effects">The effects.</param>
/// <param name="includeNonstandardEffects">if set to <c>true</c> [include nonstandard effects].</param>
/// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns>
public void WriteEffectsToFile(string fileName, EffectFile[] effects, bool includeNonstandardEffects)
{
WriteEffectToFile(fileName, effects.Length, effects, (int)(includeNonstandardEffects?EffectFileFlags.IncludeNonStandard:0));
}
/// <summary>
/// Gets the created effects.
/// </summary>
/// <value>The created effects.</value>
public IList<Effect> CreatedEffects
{
get
{
var enumCreatedEffectsCallback = new EnumCreatedEffectsCallback();
EnumCreatedEffectObjects(enumCreatedEffectsCallback.NativePointer, IntPtr.Zero, 0);
return enumCreatedEffectsCallback.Effects;
}
}
}
}