f8c8fc09ea0af746b955636e0a8a35e67fd8ebfd
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / event-handler.cpp
1 /*
2  * Copyright (c) 2019 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/public-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 #if 0 and defined DALI_ELDBUS_AVAILABLE
56 namespace
57 {
58
59 // Copied from x server
60 static uint32_t GetCurrentMilliSeconds(void)
61 {
62   struct timeval tv;
63
64   struct timespec tp;
65   static clockid_t clockid;
66
67   if (!clockid)
68   {
69 #ifdef CLOCK_MONOTONIC_COARSE
70     if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
71       (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
72     {
73       clockid = CLOCK_MONOTONIC_COARSE;
74     }
75     else
76 #endif
77     if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
78     {
79       clockid = CLOCK_MONOTONIC;
80     }
81     else
82     {
83       clockid = ~0L;
84     }
85   }
86   if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
87   {
88     return static_cast<uint32_t>( (tp.tv_sec * 1000 ) + (tp.tv_nsec / 1000000L) );
89   }
90
91   gettimeofday(&tv, NULL);
92   return static_cast<uint32_t>( (tv.tv_sec * 1000 ) + (tv.tv_usec / 1000) );
93 }
94
95 } // unnamed namespace
96 #endif
97
98 EventHandler::EventHandler( WindowRenderSurface* surface, DamageObserver& damageObserver )
99 : mStyleMonitor( StyleMonitor::Get() ),
100   mDamageObserver( damageObserver ),
101   mClipboardEventNotifier( ClipboardEventNotifier::Get() ),
102   mClipboard( Clipboard::Get() ),
103   mPaused( false )
104 {
105   if( surface )
106   {
107     WindowBase* windowBase = surface->GetWindowBase();
108
109     // Connect signals
110     windowBase->WindowDamagedSignal().Connect( this, &EventHandler::OnWindowDamaged );
111     windowBase->FocusChangedSignal().Connect( this, &EventHandler::OnFocusChanged );
112     windowBase->RotationSignal().Connect( this, &EventHandler::OnRotation );
113     windowBase->TouchEventSignal().Connect( this, &EventHandler::OnTouchEvent );
114     windowBase->WheelEventSignal().Connect( this, &EventHandler::OnWheelEvent );
115     windowBase->KeyEventSignal().Connect( this, &EventHandler::OnKeyEvent );
116     windowBase->SelectionDataSendSignal().Connect( this, &EventHandler::OnSelectionDataSend );
117     windowBase->SelectionDataReceivedSignal().Connect( this, &EventHandler::OnSelectionDataReceived );
118     windowBase->StyleChangedSignal().Connect( this, &EventHandler::OnStyleChanged );
119   }
120 }
121
122 EventHandler::~EventHandler()
123 {
124 }
125
126 void EventHandler::SendEvent( StyleChange::Type styleChange )
127 {
128   DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
129   GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
130 }
131
132 void EventHandler::SendEvent( const DamageArea& area )
133 {
134   mDamageObserver.OnDamaged( area );
135 }
136
137 void EventHandler::Pause()
138 {
139   mPaused = true;
140 }
141
142 void EventHandler::Resume()
143 {
144   mPaused = false;
145 }
146
147 void EventHandler::OnTouchEvent( Integration::Point& point, uint32_t timeStamp )
148 {
149   for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
150   {
151     (*iter)->OnTouchPoint( point, timeStamp );
152   }
153 }
154
155 void EventHandler::OnWheelEvent( WheelEvent& wheelEvent )
156 {
157   Integration::WheelEvent event( static_cast< Integration::WheelEvent::Type >(wheelEvent.type), wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp );
158
159   for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
160   {
161     (*iter)->OnWheelEvent( event );
162   }
163 }
164
165 void EventHandler::OnKeyEvent( Integration::KeyEvent& keyEvent )
166 {
167   for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
168   {
169     (*iter)->OnKeyEvent( keyEvent );
170   }
171 }
172
173 void EventHandler::OnFocusChanged( bool focusIn )
174 {
175   // If the window gains focus and we hid the keyboard then show it again.
176   if( focusIn )
177   {
178     Dali::Clipboard clipboard = Clipboard::Get();
179     if ( clipboard )
180     {
181       clipboard.HideClipboard();
182     }
183   }
184   else
185   {
186     // Hiding clipboard event will be ignored once because window focus out event is always received on showing clipboard
187     Dali::Clipboard clipboard = Clipboard::Get();
188     if ( clipboard )
189     {
190       Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
191       clipBoardImpl.HideClipboard(true);
192     }
193   }
194 }
195
196 void EventHandler::OnRotation( const RotationEvent& event )
197 {
198   for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
199   {
200     (*iter)->OnRotation( event );
201   }
202 }
203
204 void EventHandler::OnWindowDamaged( const DamageArea& area )
205 {
206   SendEvent( area );
207 }
208
209 void EventHandler::OnSelectionDataSend( void* event )
210 {
211   Dali::Clipboard clipboard = Clipboard::Get();
212   if( clipboard )
213   {
214     Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
215     clipBoardImpl.ExcuteBuffered( true, event );
216   }
217 }
218
219 void EventHandler::OnSelectionDataReceived( void* event )
220 {
221   // We have got the selected content, inform the clipboard event listener (if we have one).
222   Dali::Clipboard clipboard = Clipboard::Get();
223   char* selectionData = NULL;
224   if( clipboard )
225   {
226     Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
227     selectionData = clipBoardImpl.ExcuteBuffered( false, event );
228   }
229
230   if( selectionData && mClipboardEventNotifier )
231   {
232     ClipboardEventNotifier& clipboardEventNotifier( ClipboardEventNotifier::GetImplementation( mClipboardEventNotifier ) );
233     std::string content( selectionData, strlen( selectionData ) );
234
235     clipboardEventNotifier.SetContent( content );
236     clipboardEventNotifier.EmitContentSelectedSignal();
237
238     DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d): %s\n" , strlen(selectionData), selectionData );
239   }
240 }
241
242 void EventHandler::OnStyleChanged( StyleChange::Type styleChange )
243 {
244   SendEvent( styleChange );
245 }
246
247 void EventHandler::AddObserver( Observer& observer )
248 {
249   ObserverContainer::iterator match ( find(mObservers.begin(), mObservers.end(), &observer) );
250
251   if ( match == mObservers.end() )
252   {
253     mObservers.push_back( &observer );
254   }
255 }
256
257 void EventHandler::RemoveObserver( Observer& observer )
258 {
259   ObserverContainer::iterator match ( find(mObservers.begin(), mObservers.end(), &observer) );
260
261   if ( match != mObservers.end() )
262   {
263     mObservers.erase( match );
264   }
265 }
266
267 } // namespace Adaptor
268
269 } // namespace Internal
270
271 } // namespace Dali