2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/window-system/common/event-handler.h>
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>
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>
48 #if defined(DEBUG_ENABLED)
51 Integration::Log::Filter* gSelectionEventLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_SELECTION");
52 } // unnamed namespace
55 #ifdef DALI_ELDBUS_AVAILABLE
59 // Copied from x server
60 static uint32_t GetCurrentMilliSeconds(void)
65 static clockid_t clockid;
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)
73 clockid = CLOCK_MONOTONIC_COARSE;
77 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
79 clockid = CLOCK_MONOTONIC;
86 if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
88 return static_cast<uint32_t>( (tp.tv_sec * 1000 ) + (tp.tv_nsec / 1000000L) );
91 gettimeofday(&tv, NULL);
92 return static_cast<uint32_t>( (tv.tv_sec * 1000 ) + (tv.tv_usec / 1000) );
95 } // unnamed namespace
98 EventHandler::EventHandler( WindowRenderSurface* surface, DamageObserver& damageObserver )
99 : mStyleMonitor( StyleMonitor::Get() ),
100 mDamageObserver( damageObserver ),
101 mAccessibilityAdaptor( AccessibilityAdaptor::Get() ),
102 mClipboardEventNotifier( ClipboardEventNotifier::Get() ),
103 mClipboard( Clipboard::Get() ),
108 WindowBase* windowBase = surface->GetWindowBase();
111 windowBase->WindowDamagedSignal().Connect( this, &EventHandler::OnWindowDamaged );
112 windowBase->FocusChangedSignal().Connect( this, &EventHandler::OnFocusChanged );
113 windowBase->RotationSignal().Connect( this, &EventHandler::OnRotation );
114 windowBase->TouchEventSignal().Connect( this, &EventHandler::OnTouchEvent );
115 windowBase->WheelEventSignal().Connect( this, &EventHandler::OnWheelEvent );
116 windowBase->KeyEventSignal().Connect( this, &EventHandler::OnKeyEvent );
117 windowBase->SelectionDataSendSignal().Connect( this, &EventHandler::OnSelectionDataSend );
118 windowBase->SelectionDataReceivedSignal().Connect( this, &EventHandler::OnSelectionDataReceived );
119 windowBase->StyleChangedSignal().Connect( this, &EventHandler::OnStyleChanged );
120 windowBase->AccessibilitySignal().Connect( this, &EventHandler::OnAccessibilityNotification );
124 EventHandler::~EventHandler()
128 void EventHandler::SendEvent( StyleChange::Type styleChange )
130 DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
131 GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
134 void EventHandler::SendEvent( const DamageArea& area )
136 mDamageObserver.OnDamaged( area );
139 void EventHandler::Pause()
144 void EventHandler::Resume()
149 void EventHandler::OnTouchEvent( Integration::Point& point, uint32_t timeStamp )
151 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
153 (*iter)->OnTouchPoint( point, timeStamp );
157 void EventHandler::OnWheelEvent( Integration::WheelEvent& wheelEvent )
159 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
161 (*iter)->OnWheelEvent( wheelEvent );
165 void EventHandler::OnKeyEvent( Integration::KeyEvent& keyEvent )
167 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
169 (*iter)->OnKeyEvent( keyEvent );
173 void EventHandler::OnFocusChanged( bool focusIn )
175 // If the window gains focus and we hid the keyboard then show it again.
178 Dali::Clipboard clipboard = Clipboard::Get();
181 clipboard.HideClipboard();
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();
190 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
191 clipBoardImpl.HideClipboard(true);
196 void EventHandler::OnRotation( const RotationEvent& event )
198 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
200 (*iter)->OnRotation( event );
204 void EventHandler::OnWindowDamaged( const DamageArea& area )
209 void EventHandler::OnSelectionDataSend( void* event )
211 Dali::Clipboard clipboard = Clipboard::Get();
214 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
215 clipBoardImpl.ExcuteBuffered( true, event );
219 void EventHandler::OnSelectionDataReceived( void* event )
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;
226 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
227 selectionData = clipBoardImpl.ExcuteBuffered( false, event );
230 if( selectionData && mClipboardEventNotifier )
232 ClipboardEventNotifier& clipboardEventNotifier( ClipboardEventNotifier::GetImplementation( mClipboardEventNotifier ) );
233 std::string content( selectionData, strlen( selectionData ) );
235 clipboardEventNotifier.SetContent( content );
236 clipboardEventNotifier.EmitContentSelectedSignal();
238 DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d): %s\n" , strlen(selectionData), selectionData );
242 void EventHandler::OnStyleChanged( StyleChange::Type styleChange )
244 SendEvent( styleChange );
247 void EventHandler::OnAccessibilityNotification( const WindowBase::AccessibilityInfo& info )
249 #ifdef DALI_ELDBUS_AVAILABLE
255 if( !mAccessibilityAdaptor )
257 DALI_LOG_ERROR( "Invalid accessibility adaptor\n" );
261 AccessibilityAdaptor* accessibilityAdaptor( &AccessibilityAdaptor::GetImplementation( mAccessibilityAdaptor ) );
262 if( !accessibilityAdaptor )
264 DALI_LOG_ERROR( "Cannot access accessibility adaptor\n" );
268 // Create a touch point object.
269 TouchPoint::State touchPointState( TouchPoint::Down );
270 if( info.state == 0 )
272 touchPointState = TouchPoint::Down; // Mouse down.
274 else if( info.state == 1 )
276 touchPointState = TouchPoint::Motion; // Mouse move.
278 else if( info.state == 2 )
280 touchPointState = TouchPoint::Up; // Mouse up.
284 touchPointState = TouchPoint::Interrupted; // Error.
287 // Send touch event to accessibility adaptor.
288 TouchPoint point( 0, touchPointState, static_cast< float >( info.startX ), static_cast< float >( info.startY ) );
290 // Perform actions based on received gestures.
291 // Note: This is seperated from the reading so we can have other input readers without changing the below code.
292 switch( info.gestureValue )
294 case 0: // OneFingerHover
297 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
300 case 1: // TwoFingersHover
302 // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
303 accessibilityAdaptor->HandleActionScrollEvent( point, GetCurrentMilliSeconds() );
306 case 2: // ThreeFingersHover
308 // Read from top item on screen continuously.
309 accessibilityAdaptor->HandleActionReadFromTopEvent();
312 case 3: // OneFingerFlickLeft
314 // Move to previous item.
315 accessibilityAdaptor->HandleActionReadPreviousEvent();
318 case 4: // OneFingerFlickRight
320 // Move to next item.
321 accessibilityAdaptor->HandleActionReadNextEvent();
324 case 5: // OneFingerFlickUp
326 // Move to previous item.
327 accessibilityAdaptor->HandleActionPreviousEvent();
330 case 6: // OneFingerFlickDown
332 // Move to next item.
333 accessibilityAdaptor->HandleActionNextEvent();
336 case 7: // TwoFingersFlickUp
338 // Scroll up the list.
339 accessibilityAdaptor->HandleActionScrollUpEvent();
342 case 8: // TwoFingersFlickDown
344 // Scroll down the list.
345 accessibilityAdaptor->HandleActionScrollDownEvent();
348 case 9: // TwoFingersFlickLeft
350 // Scroll left to the previous page
351 accessibilityAdaptor->HandleActionPageLeftEvent();
354 case 10: // TwoFingersFlickRight
356 // Scroll right to the next page
357 accessibilityAdaptor->HandleActionPageRightEvent();
360 case 11: // ThreeFingersFlickLeft
365 case 12: // ThreeFingersFlickRight
370 case 13: // ThreeFingersFlickUp
375 case 14: // ThreeFingersFlickDown
380 case 15: // OneFingerSingleTap
383 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
386 case 16: // OneFingerDoubleTap
388 // Activate selected item / active edit mode.
389 accessibilityAdaptor->HandleActionActivateEvent();
392 case 17: // OneFingerTripleTap
395 accessibilityAdaptor->HandleActionZoomEvent();
398 case 18: // TwoFingersSingleTap
400 // Pause/Resume current speech
401 accessibilityAdaptor->HandleActionReadPauseResumeEvent();
404 case 19: // TwoFingersDoubleTap
406 // Start/Stop current action
407 accessibilityAdaptor->HandleActionStartStopEvent();
410 case 20: // TwoFingersTripleTap
412 // Read information from indicator
416 case 21: // ThreeFingersSingleTap
418 // Read from top item on screen continuously.
419 accessibilityAdaptor->HandleActionReadFromTopEvent();
422 case 22: // ThreeFingersDoubleTap
424 // Read from next item continuously.
425 accessibilityAdaptor->HandleActionReadFromNextEvent();
428 case 23: // ThreeFingersTripleTap
433 case 24: // OneFingerFlickLeftReturn
435 // Scroll up to the previous page
436 accessibilityAdaptor->HandleActionPageUpEvent();
439 case 25: // OneFingerFlickRightReturn
441 // Scroll down to the next page
442 accessibilityAdaptor->HandleActionPageDownEvent();
445 case 26: // OneFingerFlickUpReturn
447 // Move to the first item on screen
448 accessibilityAdaptor->HandleActionMoveToFirstEvent();
451 case 27: // OneFingerFlickDownReturn
453 // Move to the last item on screen
454 accessibilityAdaptor->HandleActionMoveToLastEvent();
457 case 28: // TwoFingersFlickLeftReturn
462 case 29: // TwoFingersFlickRightReturn
467 case 30: // TwoFingersFlickUpReturn
472 case 31: // TwoFingersFlickDownReturn
477 case 32: // ThreeFingersFlickLeftReturn
482 case 33: // ThreeFingersFlickRightReturn
487 case 34: // ThreeFingersFlickUpReturn
492 case 35: // ThreeFingersFlickDownReturn
501 void EventHandler::AddObserver( Observer& observer )
503 ObserverContainer::iterator match ( find(mObservers.begin(), mObservers.end(), &observer) );
505 if ( match == mObservers.end() )
507 mObservers.push_back( &observer );
511 void EventHandler::RemoveObserver( Observer& observer )
513 ObserverContainer::iterator match ( find(mObservers.begin(), mObservers.end(), &observer) );
515 if ( match != mObservers.end() )
517 mObservers.erase( match );
521 } // namespace Adaptor
523 } // namespace Internal