Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / event-handler.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
18 // CLASS HEADER
19 #include <dali/internal/window-system/common/event-handler.h>
20
21 // EXTERNAL INCLUDES
22 #include <sys/time.h>
23 #include <cstring>
24
25 #include <dali/devel-api/events/touch-point.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/integration-api/events/hover-event-integ.h>
28 #include <dali/integration-api/events/key-event-integ.h>
29 #include <dali/integration-api/events/touch-event-integ.h>
30 #include <dali/integration-api/events/wheel-event-integ.h>
31 #include <dali/public-api/events/key-event.h>
32 #include <dali/public-api/events/wheel-event.h>
33
34 // INTERNAL INCLUDES
35 #include <dali/internal/clipboard/common/clipboard-impl.h>
36 #include <dali/internal/styling/common/style-monitor-impl.h>
37 #include <dali/internal/window-system/common/window-render-surface.h>
38
39 namespace Dali
40 {
41 namespace Internal
42 {
43 namespace Adaptor
44 {
45 #if defined(DEBUG_ENABLED)
46 namespace
47 {
48 Integration::Log::Filter* gSelectionEventLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_SELECTION");
49 } // unnamed namespace
50 #endif
51
52 EventHandler::EventHandler(WindowBase* windowBase, DamageObserver& damageObserver)
53 : mStyleMonitor(StyleMonitor::Get()),
54   mDamageObserver(damageObserver),
55   mClipboardEventNotifier(ClipboardEventNotifier::Get()),
56   mClipboard(Clipboard::Get()),
57   mPaused(false)
58 {
59   // Connect signals
60   if(windowBase)
61   {
62     windowBase->WindowDamagedSignal().Connect(this, &EventHandler::OnWindowDamaged);
63     windowBase->FocusChangedSignal().Connect(this, &EventHandler::OnFocusChanged);
64     windowBase->RotationSignal().Connect(this, &EventHandler::OnRotation);
65     windowBase->TouchEventSignal().Connect(this, &EventHandler::OnTouchEvent);
66     windowBase->WheelEventSignal().Connect(this, &EventHandler::OnWheelEvent);
67     windowBase->KeyEventSignal().Connect(this, &EventHandler::OnKeyEvent);
68     windowBase->SelectionDataSendSignal().Connect(this, &EventHandler::OnSelectionDataSend);
69     windowBase->SelectionDataReceivedSignal().Connect(this, &EventHandler::OnSelectionDataReceived);
70     windowBase->StyleChangedSignal().Connect(this, &EventHandler::OnStyleChanged);
71   }
72   else
73   {
74     DALI_LOG_ERROR("WindowBase is invalid!!!\n");
75   }
76 }
77
78 EventHandler::~EventHandler()
79 {
80 }
81
82 void EventHandler::SendEvent(StyleChange::Type styleChange)
83 {
84   DALI_ASSERT_DEBUG(mStyleMonitor && "StyleMonitor Not Available");
85   GetImplementation(mStyleMonitor).StyleChanged(styleChange);
86 }
87
88 void EventHandler::SendEvent(const DamageArea& area)
89 {
90   mDamageObserver.OnDamaged(area);
91 }
92
93 void EventHandler::Pause()
94 {
95   mPaused = true;
96 }
97
98 void EventHandler::Resume()
99 {
100   mPaused = false;
101 }
102
103 void EventHandler::OnTouchEvent(Integration::Point& point, uint32_t timeStamp)
104 {
105   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
106   {
107     (*iter)->OnTouchPoint(point, timeStamp);
108   }
109 }
110
111 void EventHandler::OnWheelEvent(Integration::WheelEvent& wheelEvent)
112 {
113   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
114   {
115     (*iter)->OnWheelEvent(wheelEvent);
116   }
117 }
118
119 void EventHandler::OnKeyEvent(Integration::KeyEvent& keyEvent)
120 {
121   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
122   {
123     (*iter)->OnKeyEvent(keyEvent);
124   }
125 }
126
127 void EventHandler::OnFocusChanged(bool focusIn)
128 {
129   // If the window gains focus and we hid the keyboard then show it again.
130   if(focusIn)
131   {
132     Dali::Clipboard clipboard = Clipboard::Get();
133     if(clipboard)
134     {
135       clipboard.HideClipboard();
136     }
137   }
138   else
139   {
140     // Hiding clipboard event will be ignored once because window focus out event is always received on showing clipboard
141     Dali::Clipboard clipboard = Clipboard::Get();
142     if(clipboard)
143     {
144       Clipboard& clipBoardImpl(GetImplementation(clipboard));
145       clipBoardImpl.HideClipboard(true);
146     }
147   }
148 }
149
150 void EventHandler::OnRotation(const RotationEvent& event)
151 {
152   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
153   {
154     (*iter)->OnRotation(event);
155   }
156 }
157
158 void EventHandler::OnWindowDamaged(const DamageArea& area)
159 {
160   SendEvent(area);
161 }
162
163 void EventHandler::OnSelectionDataSend(void* event)
164 {
165   Dali::Clipboard clipboard = Clipboard::Get();
166   if(clipboard)
167   {
168     Clipboard& clipBoardImpl(GetImplementation(clipboard));
169     clipBoardImpl.ExcuteBuffered(true, event);
170   }
171 }
172
173 void EventHandler::OnSelectionDataReceived(void* event)
174 {
175   // We have got the selected content, inform the clipboard event listener (if we have one).
176   Dali::Clipboard clipboard     = Clipboard::Get();
177   char*           selectionData = NULL;
178   if(clipboard)
179   {
180     Clipboard& clipBoardImpl(GetImplementation(clipboard));
181     selectionData = clipBoardImpl.ExcuteBuffered(false, event);
182   }
183
184   if(selectionData && mClipboardEventNotifier)
185   {
186     ClipboardEventNotifier& clipboardEventNotifier(ClipboardEventNotifier::GetImplementation(mClipboardEventNotifier));
187     std::string             content(selectionData, strlen(selectionData));
188
189     clipboardEventNotifier.SetContent(content);
190     clipboardEventNotifier.EmitContentSelectedSignal();
191
192     DALI_LOG_INFO(gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d): %s\n", strlen(selectionData), selectionData);
193   }
194 }
195
196 void EventHandler::OnStyleChanged(StyleChange::Type styleChange)
197 {
198   SendEvent(styleChange);
199 }
200
201 void EventHandler::AddObserver(Observer& observer)
202 {
203   ObserverContainer::iterator match(find(mObservers.begin(), mObservers.end(), &observer));
204
205   if(match == mObservers.end())
206   {
207     mObservers.push_back(&observer);
208   }
209 }
210
211 void EventHandler::RemoveObserver(Observer& observer)
212 {
213   ObserverContainer::iterator match(find(mObservers.begin(), mObservers.end(), &observer));
214
215   if(match != mObservers.end())
216   {
217     mObservers.erase(match);
218   }
219 }
220
221 } // namespace Adaptor
222
223 } // namespace Internal
224
225 } // namespace Dali