MacHidDevice.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #region License
  2. /* Copyright 2012-2013 James F. Bellinger <http://www.zer7.com/software/hidsharp>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  13. #endregion
  14. namespace HidSharp.Platform.MacOS
  15. {
  16. class MacHidDevice : HidDevice
  17. {
  18. string _manufacturer;
  19. string _productName;
  20. string _serialNumber;
  21. int _vid, _pid, _version;
  22. int _maxInput, _maxOutput, _maxFeature;
  23. NativeMethods.io_string_t _path;
  24. internal MacHidDevice(NativeMethods.io_string_t path)
  25. {
  26. _path = path;
  27. }
  28. public override HidStream Open()
  29. {
  30. var stream = new MacHidStream();
  31. try { stream.Init(_path, this); return stream; }
  32. catch { stream.Close(); throw; }
  33. }
  34. internal bool GetInfo(int handle)
  35. {
  36. int? vid = NativeMethods.IORegistryEntryGetCFProperty_Int(handle, NativeMethods.kIOHIDVendorIDKey);
  37. int? pid = NativeMethods.IORegistryEntryGetCFProperty_Int(handle, NativeMethods.kIOHIDProductIDKey);
  38. int? version = NativeMethods.IORegistryEntryGetCFProperty_Int(handle, NativeMethods.kIOHIDVersionNumberKey);
  39. if (vid == null || pid == null || version == null) { return false; }
  40. _vid = (int)vid;
  41. _pid = (int)pid;
  42. _version = (int)version;
  43. _maxInput = NativeMethods.IORegistryEntryGetCFProperty_Int(handle, NativeMethods.kIOHIDMaxInputReportSizeKey) ?? 0;
  44. _maxOutput = NativeMethods.IORegistryEntryGetCFProperty_Int(handle, NativeMethods.kIOHIDMaxOutputReportSizeKey) ?? 0;
  45. _maxFeature = NativeMethods.IORegistryEntryGetCFProperty_Int(handle, NativeMethods.kIOHIDMaxFeatureReportSizeKey) ?? 0;
  46. _manufacturer = NativeMethods.IORegistryEntryGetCFProperty_String(handle, NativeMethods.kIOHIDManufacturerKey) ?? "";
  47. _productName = NativeMethods.IORegistryEntryGetCFProperty_String(handle, NativeMethods.kIOHIDProductKey) ?? "";
  48. _serialNumber = NativeMethods.IORegistryEntryGetCFProperty_String(handle, NativeMethods.kIOHIDSerialNumberKey) ?? "";
  49. return true;
  50. }
  51. public override string DevicePath
  52. {
  53. get { return _path.ToString(); }
  54. }
  55. public override int MaxInputReportLength
  56. {
  57. get { return _maxInput; }
  58. }
  59. public override int MaxOutputReportLength
  60. {
  61. get { return _maxOutput; }
  62. }
  63. public override int MaxFeatureReportLength
  64. {
  65. get { return _maxFeature; }
  66. }
  67. public override string Manufacturer
  68. {
  69. get { return _manufacturer; }
  70. }
  71. public override int ProductID
  72. {
  73. get { return _pid; }
  74. }
  75. public override string ProductName
  76. {
  77. get { return _productName; }
  78. }
  79. public override int ProductVersion
  80. {
  81. get { return _version; }
  82. }
  83. public override string SerialNumber
  84. {
  85. get { return _serialNumber; }
  86. }
  87. public override int VendorID
  88. {
  89. get { return _vid; }
  90. }
  91. }
  92. }