tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Orientation / Orientation.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file          Orientation.h
18  * @author      Qi Xiangguo (xiangguo.qi@samsung.com)
19  * @version     0.1
20  * @brief
21  */
22
23 #ifndef WRTDEVICEAPIS_ORIENTATION_ORIENTATION_H_
24 #define WRTDEVICEAPIS_ORIENTATION_ORIENTATION_H_
25
26 #include <map>
27 #include <dpl/shared_ptr.h>
28 #include <dpl/noncopyable.h>
29 #include <sensor.h>
30 #include <sensor_accel.h>
31 #include <Orientation/IOrientation.h>
32 #include <Orientation/OrientationFactory.h>
33 #include <Orientation/EventOrientationChanged.h>
34 #include <Commons/Emitters.h>
35
36 namespace WrtDeviceApis {
37 namespace Orientation {
38
39 class Orientation : public Api::IOrientation
40 {
41     int m_handle;
42     gulong m_signalId;
43     /**
44      * @See: Api::Orientation.
45      */
46     friend class Api::OrientationFactory;
47
48   public:
49     class Watcher
50     {
51       private:
52         int m_handle;
53         Api::EventOrientationChangedEmitterPtr m_emitter;
54
55       public:
56         Watcher(
57                 int handle,
58                 const Api::EventOrientationChangedEmitterPtr&
59                 emitter) :
60             m_handle(handle),
61             m_emitter(emitter)
62         {
63         }
64
65         int& getHandle()
66         {
67             return m_handle;
68         }
69
70         Api::EventOrientationChangedEmitterPtr getEmitter()
71         {
72             return m_emitter;
73         }
74
75         void emit(const Api::EventOrientationChangedPtr& event)
76         {
77             m_emitter->emit(event);
78         }
79         void getCurrentOrientationForWatch();
80         void orientationHasChanged(sensor_event_data_t *sensorEvent);
81     };
82     typedef DPL::SharedPtr<Watcher> WatcherPtr;
83
84   public:
85     virtual ~Orientation();
86
87     /**
88      * @See: IOrientation::getCurrentOrientation
89      */
90     virtual void getCurrentOrientation(
91             const Api::EventGetCurrentOrientationPtr& event);
92
93     /**
94      * @See: IOrientation::watchOrientation
95      */
96     virtual long watchOrientation(
97             const Api::EventOrientationChangedEmitterPtr&
98             emitter,
99             long minNotificationInterval);
100
101     /**
102      * @See: IOrientation::clearWatch
103      */
104     virtual void clearWatch(
105             Api::EventOrientationChangedEmitter::IdType id);
106
107     /**
108      * Method to run from platform's callback function
109      */
110     void OrientationHasChanged(sensor_event_data_t *sensorEvent);
111
112     /**
113      * Method to get current orientation for watch function
114      */
115     void getCurrentOrientationForWatch();
116
117   protected:
118     Orientation();
119     virtual void OnRequestReceived(
120             const Api::EventGetCurrentOrientationPtr& event);
121
122   private:
123     typedef Commons::Emitters<Api::EventOrientationChangedEmitter>
124                 OrientationChangedEmitters;
125
126     /**
127      * Initialize orientation sensor.  This method returns when platform error occurs.
128      */
129     int initialize();
130
131     /**
132      * Start orientation sensor. This method sends a start command to sensor server.
133      */
134     void start(int handle);
135
136     /**
137      *  Stop orientation sensor. This method sends a stop command to sensor server.
138      */
139     void stop(int& handle,
140             bool isWatcherHandle = false);
141
142     /**
143      * Method to restart orientation sensor server.
144      */
145     void restart(int& handle);
146
147     OrientationChangedEmitters m_OrientationEmitters;
148     DPL::Mutex m_initializationMutex;
149     bool m_initialized;
150     bool m_isWatch;
151     std::vector<WatcherPtr> m_watchers;
152 };
153
154 }
155 }
156
157 #endif // WRTDEVICEAPIS_ORIENTATION_ORIENTATION_H_