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