HidManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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
  15. {
  16. internal abstract class HidManager
  17. {
  18. private Dictionary<object, HidDevice> _deviceList;
  19. private object _syncRoot;
  20. protected HidManager()
  21. {
  22. _deviceList = new Dictionary<object, HidDevice>();
  23. _syncRoot = new object();
  24. }
  25. public virtual void Init()
  26. {
  27. }
  28. public virtual void Run()
  29. {
  30. while (true) { Thread.Sleep(Timeout.Infinite); }
  31. }
  32. internal void RunImpl(object readyEvent)
  33. {
  34. Init();
  35. ((ManualResetEvent)readyEvent).Set();
  36. Run();
  37. }
  38. public IEnumerable<HidDevice> GetDevices()
  39. {
  40. lock (SyncRoot)
  41. {
  42. object[] devices = Refresh();
  43. object[] additions = devices.Except(_deviceList.Keys).ToArray();
  44. object[] removals = _deviceList.Keys.Except(devices).ToArray();
  45. if (additions.Length > 0)
  46. {
  47. int completedAdditions = 0;
  48. foreach (object addition in additions)
  49. {
  50. ThreadPool.QueueUserWorkItem(new WaitCallback(addition_ =>
  51. {
  52. HidDevice device; object creationState;
  53. bool created = TryCreateDevice(addition_, out device, out creationState);
  54. if (created)
  55. {
  56. // By not adding on failure, we'll end up retrying every time.
  57. lock (_deviceList) { _deviceList.Add(addition_, device); }
  58. }
  59. lock (_deviceList)
  60. {
  61. completedAdditions++; Monitor.Pulse(_deviceList);
  62. }
  63. if (created)
  64. {
  65. CompleteDevice(addition_, device, creationState);
  66. }
  67. }), addition);
  68. }
  69. lock (_deviceList)
  70. {
  71. while (completedAdditions != additions.Length) { Monitor.Wait(_deviceList); }
  72. }
  73. }
  74. foreach (object removal in removals)
  75. {
  76. _deviceList.Remove(removal);
  77. }
  78. return _deviceList.Values.ToArray();
  79. }
  80. }
  81. protected abstract object[] Refresh();
  82. protected abstract bool TryCreateDevice(object key, out HidDevice device, out object creationState);
  83. protected abstract void CompleteDevice(object key, HidDevice device, object creationState);
  84. public abstract bool IsSupported
  85. {
  86. get;
  87. }
  88. protected object SyncRoot
  89. {
  90. get { return _syncRoot; }
  91. }
  92. }
  93. }