DeviceMotionClientManx.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2010 Apple Inc. All rights reserved.
  3. * Copyright (C) 2012 Samsung Electronics. All rights reserved.
  4. * Copyright (C) 2014 Sony Computer Entertainment Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "config.h"
  28. #if ENABLE(DEVICE_ORIENTATION)
  29. #include "DeviceMotionClientManx.h"
  30. #include "DeviceMotionController.h"
  31. #include <wtf/MainThread.h>
  32. namespace WebCore {
  33. DeviceMotionClientManx::DeviceMotionClientManx()
  34. {
  35. m_provider = motion::MotionProvider::create(this);
  36. }
  37. DeviceMotionClientManx::~DeviceMotionClientManx()
  38. {
  39. if (m_provider)
  40. motion::MotionProvider::destroy(m_provider);
  41. cancelCallOnMainThread(handleDidChangeDeviceMotionOnMainThread, this);
  42. }
  43. void DeviceMotionClientManx::deviceMotionControllerDestroyed()
  44. {
  45. delete this;
  46. }
  47. void DeviceMotionClientManx::setController(DeviceMotionController* controller)
  48. {
  49. m_controller = controller;
  50. }
  51. void DeviceMotionClientManx::startUpdating()
  52. {
  53. if (m_provider)
  54. m_provider->startUpdating();
  55. }
  56. void DeviceMotionClientManx::stopUpdating()
  57. {
  58. if (m_provider)
  59. m_provider->stopUpdating();
  60. cancelCallOnMainThread(handleDidChangeDeviceMotionOnMainThread, this);
  61. }
  62. DeviceMotionData* DeviceMotionClientManx::lastMotion() const
  63. {
  64. MutexLocker locker(m_mutex);
  65. return m_lastMotion.get();
  66. }
  67. void DeviceMotionClientManx::onUpdate(motion::MotionData data)
  68. {
  69. MutexLocker locker(m_mutex);
  70. m_lastMotion = WebCore::DeviceMotionData::create(
  71. WebCore::DeviceMotionData::Acceleration::create(
  72. data.acceleration.canProvideX, data.acceleration.x,
  73. data.acceleration.canProvideY, data.acceleration.y,
  74. data.acceleration.canProvideZ, data.acceleration.z
  75. ),
  76. WebCore::DeviceMotionData::Acceleration::create(
  77. data.accelerationIncludingGravity.canProvideX, data.accelerationIncludingGravity.x,
  78. data.accelerationIncludingGravity.canProvideY, data.accelerationIncludingGravity.y,
  79. data.accelerationIncludingGravity.canProvideZ, data.accelerationIncludingGravity.z
  80. ),
  81. WebCore::DeviceMotionData::RotationRate::create(
  82. data.rotationRate.canProvideAlpha, data.rotationRate.alpha,
  83. data.rotationRate.canProvideBeta, data.rotationRate.beta,
  84. data.rotationRate.canProvideGamma, data.rotationRate.gamma
  85. ),
  86. data.canProvideInterval,
  87. data.interval
  88. );
  89. callOnMainThread(handleDidChangeDeviceMotionOnMainThread, this);
  90. }
  91. void DeviceMotionClientManx::handleDidChangeDeviceMotionOnMainThread(void* arg)
  92. {
  93. DeviceMotionClientManx* that = reinterpret_cast<DeviceMotionClientManx*>(arg);
  94. that->m_mutex.lock();
  95. WTF::RefPtr<WebCore::DeviceMotionData> data = that->m_lastMotion;
  96. that->m_mutex.unlock();
  97. if (data)
  98. that->m_controller->didChangeDeviceMotion(data.get());
  99. }
  100. } // namespece WebCore
  101. #endif