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