File size: 16,797 Bytes
18a519f | 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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | using System;
using UnityEngine.InputSystem.Utilities;
////TODO: add a 'devicePath' property that platforms can use to relay their internal device locators
//// (but do *not* take it into account when comparing descriptions for disconnected devices)
namespace UnityEngine.InputSystem.Layouts
{
/// <summary>
/// Metadata for an input device.
/// </summary>
/// <remarks>
/// Device descriptions are mainly used to determine which <see cref="InputControlLayout"/>
/// to create an actual <see cref="InputDevice"/> instance from. Each description is comprised
/// of a set of properties that each are individually optional. However, for a description
/// to be usable, at least some need to be set. Generally, the minimum viable description
/// for a device is one with <see cref="deviceClass"/> filled out.
///
/// <example>
/// <code>
/// // Device description equivalent to a generic gamepad with no
/// // further information about the device.
/// new InputDeviceDescription
/// {
/// deviceClass = "Gamepad"
/// };
/// </code>
/// </example>
///
/// Device descriptions will usually be supplied by the Unity runtime but can also be manually
/// fed into the system using <see cref="InputSystem.AddDevice(InputDeviceDescription)"/>. The
/// system will remember each device description it has seen regardless of whether it was
/// able to successfully create a device from the description. To query the list of descriptions
/// that for whatever reason did not result in a device being created, call <see
/// cref="InputSystem.GetUnsupportedDevices()"/>.
///
/// Whenever layout registrations in the system are changed (e.g. by calling <see
/// cref="InputSystem.RegisterLayout{T}"/> or whenever <see cref="InputSettings.supportedDevices"/>
/// is changed, the system will go through the list of unsupported devices itself and figure out
/// if there are device descriptions that now it can turn into devices. The same also applies
/// in reverse; if, for example, a layout is removed that is currently used a device, the
/// device will be removed and its description (if any) will be placed on the list of
/// unsupported devices.
/// </remarks>
/// <seealso cref="InputDevice.description"/>
/// <seealso cref="InputDeviceMatcher"/>
[Serializable]
public struct InputDeviceDescription : IEquatable<InputDeviceDescription>
{
/// <summary>
/// How we talk to the device; usually name of the underlying backend that feeds
/// state for the device (e.g. "HID" or "XInput").
/// </summary>
/// <value>Name of interface through which the device is reported.</value>
/// <see cref="InputDeviceMatcher.WithInterface"/>
public string interfaceName
{
get => m_InterfaceName;
set => m_InterfaceName = value;
}
/// <summary>
/// What the interface thinks the device classifies as.
/// </summary>
/// <value>Broad classification of device.</value>
/// <remarks>
/// If there is no layout specifically matching a device description,
/// the device class is used as as fallback. If, for example, this field
/// is set to "Gamepad", the "Gamepad" layout is used as a fallback.
/// </remarks>
/// <seealso cref="InputDeviceMatcher.WithDeviceClass"/>
public string deviceClass
{
get => m_DeviceClass;
set => m_DeviceClass = value;
}
/// <summary>
/// Name of the vendor that produced the device.
/// </summary>
/// <value>Name of manufacturer.</value>
/// <seealso cref="InputDeviceMatcher.WithManufacturer"/>
public string manufacturer
{
get => m_Manufacturer;
set => m_Manufacturer = value;
}
/// <summary>
/// Name of the product assigned by the vendor to the device.
/// </summary>
/// <value>Name of product.</value>
/// <seealso cref="InputDeviceMatcher.WithProduct"/>
public string product
{
get => m_Product;
set => m_Product = value;
}
/// <summary>
/// If available, serial number for the device.
/// </summary>
/// <value>Serial number of device.</value>
public string serial
{
get => m_Serial;
set => m_Serial = value;
}
/// <summary>
/// Version string of the device and/or driver.
/// </summary>
/// <value>Version of device and/or driver.</value>
/// <seealso cref="InputDeviceMatcher.WithVersion"/>
public string version
{
get => m_Version;
set => m_Version = value;
}
/// <summary>
/// An optional JSON string listing device-specific capabilities.
/// </summary>
/// <value>Interface-specific listing of device capabilities.</value>
/// <remarks>
/// The primary use of this field is to allow custom layout factories
/// to create layouts on the fly from in-depth device descriptions delivered
/// by external APIs.
///
/// In the case of HID, for example, this field contains a JSON representation
/// of the HID descriptor (see <see cref="HID.HID.HIDDeviceDescriptor"/>) as
/// reported by the device driver. This descriptor contains information about
/// all I/O elements on the device which can be used to determine the control
/// setup and data format used by the device.
/// </remarks>
/// <seealso cref="InputDeviceMatcher.WithCapability{T}"/>
public string capabilities
{
get => m_Capabilities;
set => m_Capabilities = value;
}
/// <summary>
/// Whether any of the properties in the description are set.
/// </summary>
/// <value>True if any of <see cref="interfaceName"/>, <see cref="deviceClass"/>,
/// <see cref="manufacturer"/>, <see cref="product"/>, <see cref="serial"/>,
/// <see cref="version"/>, or <see cref="capabilities"/> is not <c>null</c> and
/// not empty.</value>
public bool empty =>
string.IsNullOrEmpty(m_InterfaceName) &&
string.IsNullOrEmpty(m_DeviceClass) &&
string.IsNullOrEmpty(m_Manufacturer) &&
string.IsNullOrEmpty(m_Product) &&
string.IsNullOrEmpty(m_Serial) &&
string.IsNullOrEmpty(m_Version) &&
string.IsNullOrEmpty(m_Capabilities);
/// <summary>
/// Return a string representation of the description useful for
/// debugging.
/// </summary>
/// <returns>A script representation of the description.</returns>
public override string ToString()
{
var haveProduct = !string.IsNullOrEmpty(product);
var haveManufacturer = !string.IsNullOrEmpty(manufacturer);
var haveInterface = !string.IsNullOrEmpty(interfaceName);
if (haveProduct && haveManufacturer)
{
if (haveInterface)
return $"{manufacturer} {product} ({interfaceName})";
return $"{manufacturer} {product}";
}
if (haveProduct)
{
if (haveInterface)
return $"{product} ({interfaceName})";
return product;
}
if (!string.IsNullOrEmpty(deviceClass))
{
if (haveInterface)
return $"{deviceClass} ({interfaceName})";
return deviceClass;
}
// For some HIDs on Windows, we don't get a product and manufacturer string even though
// the HID is guaranteed to have a product and vendor ID. Resort to printing capabilities
// which for HIDs at least include the product and vendor ID.
if (!string.IsNullOrEmpty(capabilities))
{
const int kMaxCapabilitiesLength = 40;
var caps = capabilities;
if (capabilities.Length > kMaxCapabilitiesLength)
caps = caps.Substring(0, kMaxCapabilitiesLength) + "...";
if (haveInterface)
return $"{caps} ({interfaceName})";
return caps;
}
if (haveInterface)
return interfaceName;
return "<Empty Device Description>";
}
/// <summary>
/// Compare the description to the given <paramref name="other"/> description.
/// </summary>
/// <param name="other">Another device description.</param>
/// <returns>True if the two descriptions are equivalent.</returns>
/// <remarks>
/// Two descriptions are equivalent if all their properties are equal
/// (ignore case).
/// </remarks>
public bool Equals(InputDeviceDescription other)
{
return m_InterfaceName.InvariantEqualsIgnoreCase(other.m_InterfaceName) &&
m_DeviceClass.InvariantEqualsIgnoreCase(other.m_DeviceClass) &&
m_Manufacturer.InvariantEqualsIgnoreCase(other.m_Manufacturer) &&
m_Product.InvariantEqualsIgnoreCase(other.m_Product) &&
m_Serial.InvariantEqualsIgnoreCase(other.m_Serial) &&
m_Version.InvariantEqualsIgnoreCase(other.m_Version) &&
////REVIEW: this would ideally compare JSON contents not just the raw string
m_Capabilities.InvariantEqualsIgnoreCase(other.m_Capabilities);
}
/// <summary>
/// Compare the description to the given object.
/// </summary>
/// <param name="obj">An object.</param>
/// <returns>True if <paramref name="obj"/> is an InputDeviceDescription
/// equivalent to this one.</returns>
/// <seealso cref="Equals(InputDeviceDescription)"/>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
return obj is InputDeviceDescription description && Equals(description);
}
/// <summary>
/// Compute a hash code for the device description.
/// </summary>
/// <returns>A hash code.</returns>
public override int GetHashCode()
{
unchecked
{
var hashCode = m_InterfaceName != null ? m_InterfaceName.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (m_DeviceClass != null ? m_DeviceClass.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (m_Manufacturer != null ? m_Manufacturer.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (m_Product != null ? m_Product.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (m_Serial != null ? m_Serial.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (m_Version != null ? m_Version.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (m_Capabilities != null ? m_Capabilities.GetHashCode() : 0);
return hashCode;
}
}
/// <summary>
/// Compare the two device descriptions.
/// </summary>
/// <param name="left">First device description.</param>
/// <param name="right">Second device description.</param>
/// <returns>True if the two descriptions are equivalent.</returns>
/// <seealso cref="Equals(InputDeviceDescription)"/>
public static bool operator==(InputDeviceDescription left, InputDeviceDescription right)
{
return left.Equals(right);
}
/// <summary>
/// Compare the two device descriptions for inequality.
/// </summary>
/// <param name="left">First device description.</param>
/// <param name="right">Second device description.</param>
/// <returns>True if the two descriptions are not equivalent.</returns>
/// <seealso cref="Equals(InputDeviceDescription)"/>
public static bool operator!=(InputDeviceDescription left, InputDeviceDescription right)
{
return !left.Equals(right);
}
/// <summary>
/// Return a JSON representation of the device description.
/// </summary>
/// <returns>A JSON representation of the description.</returns>
/// <remarks>
/// <example>
/// The result can be converted back into an InputDeviceDescription
/// using <see cref="FromJson"/>.
///
/// <code>
/// var description = new InputDeviceDescription
/// {
/// interfaceName = "HID",
/// product = "SomeDevice",
/// capabilities = @"
/// {
/// ""vendorId"" : 0xABA,
/// ""productId"" : 0xEFE
/// }
/// "
/// };
///
/// Debug.Log(description.ToJson());
/// // Prints
/// // {
/// // "interface" : "HID",
/// // "product" : "SomeDevice",
/// // "capabilities" : "{ \"vendorId\" : 0xABA, \"productId\" : 0xEFF }"
/// // }
/// </code>
/// </example>
/// </remarks>
/// <seealso cref="FromJson"/>
public string ToJson()
{
var data = new DeviceDescriptionJson
{
@interface = interfaceName,
type = deviceClass,
product = product,
manufacturer = manufacturer,
serial = serial,
version = version,
capabilities = capabilities
};
return JsonUtility.ToJson(data, true);
}
/// <summary>
/// Read an InputDeviceDescription from its JSON representation.
/// </summary>
/// <param name="json">String in JSON format.</param>
/// <exception cref="ArgumentNullException"><paramref name="json"/> is <c>null</c>.</exception>
/// <returns>The converted </returns>
/// <exception cref="ArgumentException">There as a parse error in <paramref name="json"/>.
/// </exception>
/// <remarks>
/// <example>
/// <code>
/// InputDeviceDescription.FromJson(@"
/// {
/// ""interface"" : ""HID"",
/// ""product"" : ""SomeDevice""
/// }
/// ");
/// </code>
/// </example>
/// </remarks>
/// <seealso cref="ToJson"/>
public static InputDeviceDescription FromJson(string json)
{
if (json == null)
throw new ArgumentNullException(nameof(json));
var data = JsonUtility.FromJson<DeviceDescriptionJson>(json);
return new InputDeviceDescription
{
interfaceName = data.@interface,
deviceClass = data.type,
product = data.product,
manufacturer = data.manufacturer,
serial = data.serial,
version = data.version,
capabilities = data.capabilities
};
}
internal static bool ComparePropertyToDeviceDescriptor(string propertyName, string propertyValue, string deviceDescriptor)
{
// We use JsonParser instead of JsonUtility.Parse in order to not allocate GC memory here.
var json = new JsonParser(deviceDescriptor);
if (!json.NavigateToProperty(propertyName))
{
if (string.IsNullOrEmpty(propertyValue))
return true;
return false;
}
return json.CurrentPropertyHasValueEqualTo(propertyValue);
}
[SerializeField] private string m_InterfaceName;
[SerializeField] private string m_DeviceClass;
[SerializeField] private string m_Manufacturer;
[SerializeField] private string m_Product;
[SerializeField] private string m_Serial;
[SerializeField] private string m_Version;
[SerializeField] private string m_Capabilities;
private struct DeviceDescriptionJson
{
public string @interface;
public string type;
public string product;
public string serial;
public string version;
public string manufacturer;
public string capabilities;
}
}
}
|