sync with tizen_2.0
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRt_EventManager.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FBaseRt_EventManager.h
20  * @brief       This is the header file for the _EventManager class.
21  *
22  * This file contains the declarations of _EventManager.
23  */
24
25 #ifndef _FBASE_RT_INTERNAL_EVENT_MANAGER_H_
26 #define _FBASE_RT_INTERNAL_EVENT_MANAGER_H_
27
28 #include <tr1/memory>
29 #include <glib.h>
30
31 #include <FOspConfig.h>
32 #include <FBaseResult.h>
33 #include <FBaseObject.h>
34 #include <FBaseColLinkedListT.h>
35 #include <FBaseColArrayListT.h>
36
37 #include "FBase_HandleT.h"
38
39 namespace Tizen { namespace Base { namespace Runtime
40 {
41
42 class Mutex;
43 class _Event;
44 class IEventArg;
45 class IEventListener;
46
47 /**
48 * @class   _EventManager
49 * @brief   This class provides methods for managingasynchronous event.
50 * @since 2.0
51 *
52 * This class provides methods for managing asynchronous event.
53 */
54 class _EventManager
55         : public Tizen::Base::Object
56 {
57 public:
58         /**
59          *      This is the default constructor for this class.
60          *      value.
61          *
62          *      @since 2.0
63          */
64         _EventManager(void);
65
66         /**
67          * This is the destructor for this class
68          * @since 2.0
69          */
70         virtual ~_EventManager(void);
71
72         /**
73          * Initializes this instanc of %_EventManager.
74          *
75          * @since 2.0
76          *
77          * @return An error code
78          * @param[in] pGmainContext     A GMainLoopContext
79          * @exception E_SUCCESS         The method is successful.
80          * @exception E_SYSTEM          A system error has occurred.
81          */
82         result Construct(GMainContext* pGmainContext);
83
84         /**
85          * Registers an event.
86          *
87          * @since 2.0
88          * @return An error code
89          * @param[in] event                                     The handle of an event to register
90          * @exception E_SUCCESS                         The method is successful.
91          * @exception E_OBJ_ALREADY_EXIST       The event has already been registered.
92          * @exception E_SYSTEM                          A system error has occurred.
93          */
94         result RegisterEvent(const _HandleT< _Event >& event);
95
96         /**
97          * Unregisters an event.
98          *
99          * @since 2.0
100          *
101          * @return An error code
102          * @param[in] event                     The handle of an event to unregister
103          * @exception E_SUCCESS                         The method is successful.
104          * @exception E_OBJ_NOT_FOUND           The event has not been registered.
105          */
106         result UnregisterEvent(const _HandleT< _Event >& event);
107
108         /**
109          * Returns the %_EventManager of current thread.
110          *
111          * @since 2.0
112          *
113          * @return The pointer to _EventManager
114          *
115          * @remark null will be returned if this is called by a worker thread.
116          */
117         static _EventManager* GetCurrentEventManager(void);
118
119         /**
120          * Sends an event asynchronously.
121          *
122          * @since 2.0
123          *
124          * @return An error code
125          * @param[in] event The event to send
126          * @param[in] arg       The event arg
127          * @exception E_SUCCESS The method is successful.
128          * @remark This takes ownership of the arg. Therefore the caller must not delete the arg instance.
129          */
130         result FireEventAsync(const _HandleT< _Event >& event, std::tr1::shared_ptr< IEventArg > arg);
131
132         /**
133          * Calls a listener added to an event asynchronously.
134          *
135          * @since 2.0
136          *
137          * @return An error code
138          * @param[in] event                     The event
139          * @param[in] arg                       The event arg
140          * @param[in] listener          The listener to call
141          * @exception E_SUCCESS         The method is successful.
142          * @remark This takes ownership of the arg. Therefore the caller must not delete the arg instance.
143          */
144         result CallListenerAsync(const _HandleT< _Event >& event, std::tr1::shared_ptr< IEventArg > arg, const _HandleT< IEventListener >& listener);
145
146         /**
147          * Clears all pending event messages of an event.
148          *
149          * @since 2.0
150          * @param[in] event The event to clear
151          */
152         void ClearAllPendingEventMessages(const _HandleT< _Event >& event);
153
154         /**
155          * Returns the handle
156          *
157          * @since 2.0
158          * @param[in] event The event to clear
159          */
160         _HandleT< _EventManager > GetHandle(void) const;
161
162         /**
163          * Gets _EventManager using given handle
164          *
165          * @since 2.0
166          *
167          * @param[in] handle The handle of _EventManager
168          */
169         static _EventManager* GetEventManagerByHandle(const _HandleT< _EventManager >& handle);
170
171         /**
172          * Gets object manager for _EventManager
173          *
174          * @since 2.0
175          */
176         static _ObjectManagerT< _EventManager >& GetEventManagerObjectManager(void);
177
178         /**
179          * Gets object manager for _Event
180          *
181          * @since 2.0
182          */
183         static _ObjectManagerT< _Event >& GetEventObjectManager(void);
184
185         /**
186          * Gets object manager for IEventListener
187          *
188          * @since 2.0
189          */
190         static _ObjectManagerT< IEventListener >& GetEventListenerObjectManager(void);
191
192 private:
193         _EventManager(const _EventManager& rhs);
194         _EventManager& operator =(const _EventManager& rhs);
195
196         static gboolean OnEventReceived(GIOChannel* pChannel, GIOCondition condition, gpointer data);
197
198 private:
199         enum _MessageType
200         {
201                 MESSAGE_TYPE_FIRE_EVENT,
202                 MESSAGE_TYPE_CALL_LISTENER
203         };
204
205         struct _EventMessage
206         {
207                 _EventMessage(void)
208                         : id(0)
209                         , type(MESSAGE_TYPE_FIRE_EVENT)
210                 {
211                 }
212
213                 int id;
214                 _MessageType type;
215                 _HandleT< _Event > event;
216                 std::tr1::shared_ptr< IEventArg > arg;
217                 _HandleT< IEventListener > listener;
218
219                 bool operator ==(const _EventMessage& rhs) const;
220                 bool operator !=(const _EventMessage& rhs) const;
221
222                 _EventMessage& operator =(const _EventMessage& rhs);
223         };
224
225 private:
226         Mutex* __pMutex;
227         GSource* __pSource;
228         GIOChannel* __pChannel;
229         GMainContext* __pGmainContext;
230         _HandleT< _EventManager > __handle;
231         Tizen::Base::Collection::LinkedListT< _HandleT< _Event > > __events;
232         Tizen::Base::Collection::ArrayListT< _EventMessage > __messages1;
233         Tizen::Base::Collection::ArrayListT< _EventMessage > __messages2;
234         Tizen::Base::Collection::ArrayListT< _EventMessage >* __pActive;
235         Tizen::Base::Collection::ArrayListT< _EventMessage >* __pReady;
236 };
237
238 } } } // Tizen::Base::Runtime
239
240 #endif // _FBASE_RT_INTERNAL_EVENT_MANAGER_H_