Remove unnecessray ClipBoard creation
[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   mClipboardEventNotifier(ClipboardEventNotifier::Get()),
56   mPaused(false)
57 {
58   // Connect signals
59   if(windowBase)
60   {
61     windowBase->WindowDamagedSignal().Connect(this, &EventHandler::OnWindowDamaged);
62     windowBase->FocusChangedSignal().Connect(this, &EventHandler::OnFocusChanged);
63     windowBase->RotationSignal().Connect(this, &EventHandler::OnRotation);
64     windowBase->TouchEventSignal().Connect(this, &EventHandler::OnTouchEvent);
65     windowBase->WheelEventSignal().Connect(this, &EventHandler::OnWheelEvent);
66     windowBase->KeyEventSignal().Connect(this, &EventHandler::OnKeyEvent);
67     windowBase->SelectionDataSendSignal().Connect(this, &EventHandler::OnSelectionDataSend);
68     windowBase->SelectionDataReceivedSignal().Connect(this, &EventHandler::OnSelectionDataReceived);
69     windowBase->StyleChangedSignal().Connect(this, &EventHandler::OnStyleChanged);
70   }
71   else
72   {
73     DALI_LOG_ERROR("WindowBase is invalid!!!\n");
74   }
75 }
76
77 EventHandler::~EventHandler()
78 {
79 }
80
81 void EventHandler::SendEvent(StyleChange::Type styleChange)
82 {
83   DALI_ASSERT_DEBUG(mStyleMonitor && "StyleMonitor Not Available");
84   GetImplementation(mStyleMonitor).StyleChanged(styleChange);
85 }
86
87 void EventHandler::SendEvent(const DamageArea& area)
88 {
89   mDamageObserver.OnDamaged(area);
90 }
91
92 void EventHandler::Pause()
93 {
94   mPaused = true;
95 }
96
97 void EventHandler::Resume()
98 {
99   mPaused = false;
100 }
101
102 void EventHandler::OnTouchEvent(Integration::Point& point, uint32_t timeStamp)
103 {
104   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
105   {
106     (*iter)->OnTouchPoint(point, timeStamp);
107   }
108 }
109
110 void EventHandler::OnWheelEvent(Integration::WheelEvent& wheelEvent)
111 {
112   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
113   {
114     (*iter)->OnWheelEvent(wheelEvent);
115   }
116 }
117
118 void EventHandler::OnKeyEvent(Integration::KeyEvent& keyEvent)
119 {
120   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
121   {
122     (*iter)->OnKeyEvent(keyEvent);
123   }
124 }
125
126 void EventHandler::OnFocusChanged(bool focusIn)
127 {
128   // If the window gains focus and we hid the keyboard then show it again.
129   if(focusIn)
130   {
131     Dali::Clipboard clipboard = Clipboard::Get();
132     if(clipboard)
133     {
134       clipboard.HideClipboard();
135     }
136   }
137   else
138   {
139     // Hiding clipboard event will be ignored once because window focus out event is always received on showing clipboard
140     Dali::Clipboard clipboard = Clipboard::Get();
141     if(clipboard)
142     {
143       Clipboard& clipBoardImpl(GetImplementation(clipboard));
144       clipBoardImpl.HideClipboard(true);
145     }
146   }
147 }
148
149 void EventHandler::OnRotation(const RotationEvent& event)
150 {
151   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
152   {
153     (*iter)->OnRotation(event);
154   }
155 }
156
157 void EventHandler::OnWindowDamaged(const DamageArea& area)
158 {
159   SendEvent(area);
160 }
161
162 void EventHandler::OnSelectionDataSend(void* event)
163 {
164   Dali::Clipboard clipboard = Clipboard::Get();
165   if(clipboard)
166   {
167     Clipboard& clipBoardImpl(GetImplementation(clipboard));
168     clipBoardImpl.ExcuteBuffered(true, event);
169   }
170 }
171
172 void EventHandler::OnSelectionDataReceived(void* event)
173 {
174   // We have got the selected content, inform the clipboard event listener (if we have one).
175   Dali::Clipboard clipboard     = Clipboard::Get();
176   char*           selectionData = NULL;
177   if(clipboard)
178   {
179     Clipboard& clipBoardImpl(GetImplementation(clipboard));
180     selectionData = clipBoardImpl.ExcuteBuffered(false, event);
181   }
182
183   if(!mClipboardEventNotifier)
184   {
185     mClipboardEventNotifier = ClipboardEventNotifier::Get();
186   }
187
188   if(selectionData && mClipboardEventNotifier)
189   {
190     ClipboardEventNotifier& clipboardEventNotifier(ClipboardEventNotifier::GetImplementation(mClipboardEventNotifier));
191     std::string             content(selectionData, strlen(selectionData));
192
193     clipboardEventNotifier.SetContent(content);
194     clipboardEventNotifier.EmitContentSelectedSignal();
195
196     DALI_LOG_INFO(gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d): %s\n", strlen(selectionData), selectionData);
197   }
198 }
199
200 void EventHandler::OnStyleChanged(StyleChange::Type styleChange)
201 {
202   SendEvent(styleChange);
203 }
204
205 void EventHandler::AddObserver(Observer& observer)
206 {
207   ObserverContainer::iterator match(find(mObservers.begin(), mObservers.end(), &observer));
208
209   if(match == mObservers.end())
210   {
211     mObservers.push_back(&observer);
212   }
213 }
214
215 void EventHandler::RemoveObserver(Observer& observer)
216 {
217   ObserverContainer::iterator match(find(mObservers.begin(), mObservers.end(), &observer));
218
219   if(match != mObservers.end())
220   {
221     mObservers.erase(match);
222   }
223 }
224
225 } // namespace Adaptor
226
227 } // namespace Internal
228
229 } // namespace Dali