9cbceec5231e1426a96b5e70e7e30faa047694f2
[platform/framework/web/crosswalk-tizen.git] / src / runtime / vibration_manager.cc
1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "runtime/vibration_manager.h"
6
7 #include <dd-haptic.h>
8
9 #include "common/logger.h"
10
11
12 namespace wrt {
13 namespace platform {
14
15 class VibrationImpl : public VibrationManager {
16  public:
17   VibrationImpl();
18   virtual ~VibrationImpl();
19   virtual void Start(int ms);
20   virtual void Stop();
21  private:
22   bool Initialize();
23   // haptic_devce_h was declared as int
24   haptic_device_h handle_;
25 };
26
27
28 VibrationImpl::VibrationImpl()
29     : handle_(0) {
30 }
31
32 VibrationImpl::~VibrationImpl() {
33   if (handle_ != 0) {
34     haptic_close(handle_);
35     handle_ = 0;
36   }
37 }
38
39 bool VibrationImpl::Initialize() {
40   if (handle_ != 0)
41     return true;
42
43   int ret = haptic_open(HAPTIC_DEVICE_0, &handle_);
44   if (ret != HAPTIC_ERROR_NONE) {
45     LOGGER(ERROR) << "Fail to open haptic device";
46     handle_ = 0;
47     return false;
48   }
49   return true;
50 }
51
52 void VibrationImpl::Start(int ms) {
53   if (Initialize()) {
54     haptic_vibrate_monotone(handle_, ms, NULL);
55   }
56 }
57
58 void VibrationImpl::Stop() {
59   if (Initialize()) {
60     haptic_stop_all_effects(handle_);
61   }
62 }
63
64 VibrationManager* VibrationManager::GetInstance() {
65   static VibrationImpl instance;
66   return &instance;
67 }
68
69
70 }  // namespace platform
71 }  // namespace wrt
72
73