Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules / event / include / dpl / event / main_event_dispatcher.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        main_event_dispatcher.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of main event dispatcher
21  * for EFL
22  */
23 #ifndef DPL_MAIN_EVENT_DISPATCHER_H
24 #define DPL_MAIN_EVENT_DISPATCHER_H
25
26 #include <dpl/event/abstract_event_dispatcher.h>
27 #include <dpl/event/abstract_event_call.h>
28 #include <dpl/waitable_event.h>
29 #include <dpl/exception.h>
30 #include <dpl/singleton.h>
31 #include <dpl/framework_efl.h>
32 #include <list>
33 #include <mutex>
34
35 namespace DPL {
36 namespace Event {
37 class MainEventDispatcher :
38     public AbstractEventDispatcher
39 {
40   public:
41     class Exception
42     {
43       public:
44         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
45         DECLARE_EXCEPTION_TYPE(Base, CreateFailed)
46         DECLARE_EXCEPTION_TYPE(Base, AddEventFailed)
47         DECLARE_EXCEPTION_TYPE(Base, AddTimedEventFailed)
48     };
49
50   protected:
51     struct WrappedEventCall
52     {
53         AbstractEventCall *abstractEventCall;
54         bool timed;
55         double dueTime;
56
57         WrappedEventCall(AbstractEventCall *abstractEventCallArg,
58                          bool timedArg,
59                          double dueTimeArg) :
60             abstractEventCall(abstractEventCallArg),
61             timed(timedArg),
62             dueTime(dueTimeArg)
63         {}
64     };
65
66     typedef std::list<WrappedEventCall> WrappedEventCallList;
67
68     // Cross thread send support
69     WrappedEventCallList m_wrappedCrossEventCallList;
70     std::mutex m_crossEventCallMutex;
71     WaitableEvent* m_crossEventCallInvoker;
72
73     Ecore_Event_Handler *m_eventCallHandler;
74     Ecore_Fd_Handler *m_crossEventCallHandler;
75
76     int m_eventId;
77
78     // Timed event support
79     struct TimedEventStruct
80     {
81         AbstractEventCall *abstractEventCall;
82         MainEventDispatcher *This;
83
84         TimedEventStruct(AbstractEventCall *abstractEventCallArg,
85                          MainEventDispatcher *ThisArg) :
86             abstractEventCall(abstractEventCallArg),
87             This(ThisArg)
88         {}
89     };
90
91     void InternalAddEvent(AbstractEventCall *abstractEventCall,
92                           bool timed,
93                           double dueTime);
94
95     static void StaticDeleteEvent(void *data, void *event);
96     static Eina_Bool StaticDispatchEvent(void *data, int type, void *event);
97     static Eina_Bool StaticDispatchTimedEvent(void *event);
98     static Eina_Bool StaticDispatchCrossInvoker(void *data,
99                                                 Ecore_Fd_Handler *fd_handler);
100
101     void DeleteEvent(AbstractEventCall *abstractEventCall);
102     void DispatchEvent(AbstractEventCall *abstractEventCall);
103     void DispatchTimedEvent(AbstractEventCall *abstractEventCall);
104     void DispatchCrossInvoker();
105
106   public:
107     explicit MainEventDispatcher();
108     virtual ~MainEventDispatcher();
109
110     virtual void AddEventCall(AbstractEventCall *abstractEventCall);
111     virtual void AddTimedEventCall(AbstractEventCall *abstractEventCall,
112                                    double dueTime);
113     virtual void ResetCrossEventCallHandler();
114 };
115
116 MainEventDispatcher& GetMainEventDispatcherInstance();
117 }
118 } // namespace DPL
119
120 #endif // DPL_MAIN_EVENT_DISPATCHER_H