[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / event-handler.cpp
1 /*
2  * Copyright (c) 2023 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   mPaused(false)
56 {
57   // Connect signals
58   if(windowBase)
59   {
60     windowBase->WindowDamagedSignal().Connect(this, &EventHandler::OnWindowDamaged);
61     windowBase->FocusChangedSignal().Connect(this, &EventHandler::OnFocusChanged);
62     windowBase->RotationSignal().Connect(this, &EventHandler::OnRotation);
63     windowBase->TouchEventSignal().Connect(this, &EventHandler::OnTouchEvent);
64     windowBase->WheelEventSignal().Connect(this, &EventHandler::OnWheelEvent);
65     windowBase->KeyEventSignal().Connect(this, &EventHandler::OnKeyEvent);
66     windowBase->SelectionDataSendSignal().Connect(this, &EventHandler::OnSelectionDataSend);
67     windowBase->SelectionDataReceivedSignal().Connect(this, &EventHandler::OnSelectionDataReceived);
68     windowBase->StyleChangedSignal().Connect(this, &EventHandler::OnStyleChanged);
69   }
70   else
71   {
72     DALI_LOG_ERROR("WindowBase is invalid!!!\n");
73   }
74 }
75
76 EventHandler::~EventHandler()
77 {
78 }
79
80 void EventHandler::SendEvent(StyleChange::Type styleChange)
81 {
82   DALI_ASSERT_DEBUG(mStyleMonitor && "StyleMonitor Not Available");
83   GetImplementation(mStyleMonitor).StyleChanged(styleChange);
84 }
85
86 void EventHandler::SendEvent(const DamageArea& area)
87 {
88   mDamageObserver.OnDamaged(area);
89 }
90
91 void EventHandler::Pause()
92 {
93   mPaused = true;
94 }
95
96 void EventHandler::Resume()
97 {
98   mPaused = false;
99 }
100
101 void EventHandler::OnTouchEvent(Integration::Point& point, uint32_t timeStamp)
102 {
103   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
104   {
105     (*iter)->OnTouchPoint(point, timeStamp);
106   }
107 }
108
109 void EventHandler::OnWheelEvent(Integration::WheelEvent& wheelEvent)
110 {
111   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
112   {
113     (*iter)->OnWheelEvent(wheelEvent);
114   }
115 }
116
117 void EventHandler::OnKeyEvent(Integration::KeyEvent& keyEvent)
118 {
119   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
120   {
121     (*iter)->OnKeyEvent(keyEvent);
122   }
123 }
124
125 void EventHandler::OnFocusChanged(bool focusIn)
126 {
127   // If the window gains focus and we hid the keyboard then show it again.
128   if(Clipboard::IsAvailable())
129   {
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
151 void EventHandler::OnRotation(const RotationEvent& event)
152 {
153   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
154   {
155     (*iter)->OnRotation(event);
156   }
157 }
158
159 void EventHandler::OnWindowDamaged(const DamageArea& area)
160 {
161   SendEvent(area);
162 }
163
164 void EventHandler::OnSelectionDataSend(void* event)
165 {
166   // Note that the clipboard-related operstions previously available have been moved to Clipboard class.
167   // It is advised not to handle any clipboard-specific works within this context.
168   // There are currently no immediate works required in this callback.
169   // But this function is retained for the purpose of handling the event at the window level, if needed.
170 }
171
172 void EventHandler::OnSelectionDataReceived(void* event)
173 {
174   // Note that the clipboard-related operstions previously available have been moved to Clipboard class.
175   // It is advised not to handle any clipboard-specific works within this context.
176   // There are currently no immediate works required in this callback.
177   // But this function is retained for the purpose of handling the event at the window level, if needed.
178 }
179
180 void EventHandler::OnStyleChanged(StyleChange::Type styleChange)
181 {
182   SendEvent(styleChange);
183 }
184
185 void EventHandler::AddObserver(Observer& observer)
186 {
187   ObserverContainer::iterator match(find(mObservers.begin(), mObservers.end(), &observer));
188
189   if(match == mObservers.end())
190   {
191     mObservers.push_back(&observer);
192   }
193 }
194
195 void EventHandler::RemoveObserver(Observer& observer)
196 {
197   ObserverContainer::iterator match(find(mObservers.begin(), mObservers.end(), &observer));
198
199   if(match != mObservers.end())
200   {
201     mObservers.erase(match);
202   }
203 }
204
205 } // namespace Adaptor
206
207 } // namespace Internal
208
209 } // namespace Dali