MacHidManager.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #region License
  2. /* Copyright 2012 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. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. namespace HidSharp.Platform.MacOS
  18. {
  19. class MacHidManager : HidManager
  20. {
  21. protected override object[] Refresh()
  22. {
  23. var paths = new List<NativeMethods.io_string_t>();
  24. var matching = NativeMethods.IOServiceMatching("IOHIDDevice").ToCFType(); // Consumed by IOServiceGetMatchingServices, so DON'T Dispose().
  25. if (matching.IsSet)
  26. {
  27. int iteratorObj;
  28. if (NativeMethods.IOReturn.Success == NativeMethods.IOServiceGetMatchingServices(0, matching, out iteratorObj))
  29. {
  30. using (var iterator = iteratorObj.ToIOObject())
  31. {
  32. while (true)
  33. {
  34. using (var handle = NativeMethods.IOIteratorNext(iterator).ToIOObject())
  35. {
  36. if (!handle.IsSet) { break; }
  37. NativeMethods.io_string_t path;
  38. if (NativeMethods.IOReturn.Success == NativeMethods.IORegistryEntryGetPath(handle, "IOService", out path))
  39. {
  40. paths.Add(path);
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47. return paths.Cast<object>().ToArray();
  48. }
  49. protected override bool TryCreateDevice(object key, out HidDevice device, out object creationState)
  50. {
  51. creationState = null;
  52. var path = (NativeMethods.io_string_t)key; var hidDevice = new MacHidDevice(path);
  53. using (var handle = NativeMethods.IORegistryEntryFromPath(0, ref path).ToIOObject())
  54. {
  55. if (!handle.IsSet || !hidDevice.GetInfo(handle)) { device = null; return false; }
  56. device = hidDevice; return true;
  57. }
  58. }
  59. protected override void CompleteDevice(object key, HidDevice device, object creationState)
  60. {
  61. }
  62. public override bool IsSupported
  63. {
  64. get
  65. {
  66. try
  67. {
  68. IntPtr major; NativeMethods.OSErr majorErr = NativeMethods.Gestalt(NativeMethods.OSType.gestaltSystemVersionMajor, out major);
  69. IntPtr minor; NativeMethods.OSErr minorErr = NativeMethods.Gestalt(NativeMethods.OSType.gestaltSystemVersionMinor, out minor);
  70. if (majorErr == NativeMethods.OSErr.noErr && minorErr == NativeMethods.OSErr.noErr)
  71. {
  72. return (long)major >= 10 || ((long)major == 10 && (long)minor >= 5);
  73. }
  74. }
  75. catch
  76. {
  77. }
  78. return false;
  79. }
  80. }
  81. }
  82. }