2 * Copyright (c) 2018 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/input/common/gesture-manager.h>
36 #include <dali/internal/clipboard/common/clipboard-impl.h>
37 #include <dali/internal/input/common/key-impl.h>
38 #include <dali/internal/input/common/physical-keyboard-impl.h>
39 #include <dali/internal/styling/common/style-monitor-impl.h>
40 #include <dali/internal/window-system/common/window-render-surface.h>
51 #if defined(DEBUG_ENABLED)
54 Integration::Log::Filter* gTouchEventLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_TOUCH");
55 Integration::Log::Filter* gSelectionEventLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_SELECTION");
56 } // unnamed namespace
62 // Copied from x server
63 static unsigned int GetCurrentMilliSeconds(void)
68 static clockid_t clockid;
72 #ifdef CLOCK_MONOTONIC_COARSE
73 if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
74 (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
76 clockid = CLOCK_MONOTONIC_COARSE;
80 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
82 clockid = CLOCK_MONOTONIC;
89 if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
91 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
94 gettimeofday(&tv, NULL);
95 return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
98 } // unnamed namespace
100 EventHandler::EventHandler( RenderSurface* surface, CoreEventInterface& coreEventInterface, GestureManager& gestureManager, DamageObserver& damageObserver, DragAndDropDetectorPtr dndDetector )
101 : mCoreEventInterface( coreEventInterface ),
102 mGestureManager( gestureManager ),
103 mStyleMonitor( StyleMonitor::Get() ),
104 mDamageObserver( damageObserver ),
105 mRotationObserver( NULL ),
106 mDragAndDropDetector( dndDetector ),
107 mAccessibilityAdaptor( AccessibilityAdaptor::Get() ),
108 mClipboardEventNotifier( ClipboardEventNotifier::Get() ),
109 mClipboard( Clipboard::Get() ),
115 // this code only works with the WindowRenderSurface so need to downcast
116 WindowRenderSurface* windowRenderSurface = static_cast< WindowRenderSurface* >( surface );
117 if( windowRenderSurface )
119 WindowBase* windowBase = windowRenderSurface->GetWindowBase();
122 windowBase->WindowDamagedSignal().Connect( this, &EventHandler::OnWindowDamaged );
123 windowBase->FocusChangedSignal().Connect( this, &EventHandler::OnFocusChanged );
124 windowBase->RotationSignal().Connect( this, &EventHandler::SendRotationPrepareEvent );
125 windowBase->TouchEventSignal().Connect( this, &EventHandler::OnTouchEvent );
126 windowBase->WheelEventSignal().Connect( this, &EventHandler::OnWheelEvent );
127 windowBase->KeyEventSignal().Connect( this, &EventHandler::OnKeyEvent );
128 windowBase->SelectionDataSendSignal().Connect( this, &EventHandler::OnSelectionDataSend );
129 windowBase->SelectionDataReceivedSignal().Connect( this, &EventHandler::OnSelectionDataReceived );
130 windowBase->StyleChangedSignal().Connect( this, &EventHandler::OnStyleChanged );
131 windowBase->AccessibilitySignal().Connect( this, &EventHandler::OnAccessibilityNotification );
135 EventHandler::~EventHandler()
137 mGestureManager.Stop();
140 void EventHandler::SendEvent(Integration::Point& point, unsigned long timeStamp)
144 timeStamp = GetCurrentMilliSeconds();
147 ConvertTouchPosition( point );
149 Integration::TouchEvent touchEvent;
150 Integration::HoverEvent hoverEvent;
151 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
152 if(type != Integration::TouchEventCombiner::DispatchNone )
154 DALI_LOG_INFO(gTouchEventLogFilter, Debug::General, "%d: Device %d: Button state %d (%.2f, %.2f)\n", timeStamp, point.GetDeviceId(), point.GetState(), point.GetScreenPosition().x, point.GetScreenPosition().y);
156 // First the touch and/or hover event & related gesture events are queued
157 if(type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth)
159 mCoreEventInterface.QueueCoreEvent( touchEvent );
160 mGestureManager.SendEvent(touchEvent);
163 if(type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth)
165 mCoreEventInterface.QueueCoreEvent( hoverEvent );
168 // Next the events are processed with a single call into Core
169 mCoreEventInterface.ProcessCoreEvents();
173 void EventHandler::SendEvent(Integration::KeyEvent& keyEvent)
175 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
176 if ( physicalKeyboard )
178 if ( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
180 GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
184 // Create send KeyEvent to Core.
185 mCoreEventInterface.QueueCoreEvent( keyEvent );
186 mCoreEventInterface.ProcessCoreEvents();
189 void EventHandler::SendWheelEvent( WheelEvent& wheelEvent )
191 // Create WheelEvent and send to Core.
192 Integration::WheelEvent event( static_cast< Integration::WheelEvent::Type >(wheelEvent.type), wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp );
193 mCoreEventInterface.QueueCoreEvent( event );
194 mCoreEventInterface.ProcessCoreEvents();
197 void EventHandler::SendEvent( StyleChange::Type styleChange )
199 DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
200 GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
203 void EventHandler::SendEvent( const DamageArea& area )
205 mDamageObserver.OnDamaged( area );
208 void EventHandler::SendRotationPrepareEvent( const RotationEvent& event )
210 if( mRotationObserver != NULL )
212 mRotationAngle = event.angle;
213 mWindowWidth = event.width;
214 mWindowHeight = event.height;
216 mRotationObserver->OnRotationPrepare( event );
217 mRotationObserver->OnRotationRequest();
221 void EventHandler::SendRotationRequestEvent( )
223 // No need to separate event into prepare and request
226 void EventHandler::FeedTouchPoint( TouchPoint& point, int timeStamp)
228 Integration::Point convertedPoint( point );
229 SendEvent(convertedPoint, timeStamp);
232 void EventHandler::FeedWheelEvent( WheelEvent& wheelEvent )
234 SendWheelEvent( wheelEvent );
237 void EventHandler::FeedKeyEvent( KeyEvent& event )
239 Integration::KeyEvent convertedEvent( event );
240 SendEvent( convertedEvent );
243 void EventHandler::FeedEvent( Integration::Event& event )
245 mCoreEventInterface.QueueCoreEvent( event );
246 mCoreEventInterface.ProcessCoreEvents();
249 void EventHandler::Reset()
253 // Any touch listeners should be told of the interruption.
254 Integration::TouchEvent event;
255 Integration::Point point;
256 point.SetState( PointState::INTERRUPTED );
257 event.AddPoint( point );
259 // First the touch event & related gesture events are queued
260 mCoreEventInterface.QueueCoreEvent( event );
261 mGestureManager.SendEvent( event );
263 // Next the events are processed with a single call into Core
264 mCoreEventInterface.ProcessCoreEvents();
267 void EventHandler::Pause()
273 void EventHandler::Resume()
279 void EventHandler::SetDragAndDropDetector( DragAndDropDetectorPtr detector )
281 mDragAndDropDetector = detector;
284 void EventHandler::SetRotationObserver( RotationObserver* observer )
286 mRotationObserver = observer;
289 void EventHandler::OnTouchEvent( Integration::Point& point, unsigned long timeStamp )
291 SendEvent( point, timeStamp );
294 void EventHandler::OnWheelEvent( WheelEvent& wheelEvent )
296 SendWheelEvent( wheelEvent );
299 void EventHandler::OnKeyEvent( Integration::KeyEvent& keyEvent )
301 SendEvent( keyEvent );
304 void EventHandler::OnFocusChanged( bool focusIn )
306 // If the window gains focus and we hid the keyboard then show it again.
309 Dali::Clipboard clipboard = Clipboard::Get();
312 clipboard.HideClipboard();
317 // Hiding clipboard event will be ignored once because window focus out event is always received on showing clipboard
318 Dali::Clipboard clipboard = Clipboard::Get();
321 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
322 clipBoardImpl.HideClipboard(true);
327 void EventHandler::OnWindowDamaged( const DamageArea& area )
332 void EventHandler::OnSelectionDataSend( void* event )
334 Dali::Clipboard clipboard = Clipboard::Get();
337 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
338 clipBoardImpl.ExcuteBuffered( true, event );
342 void EventHandler::OnSelectionDataReceived( void* event )
344 // We have got the selected content, inform the clipboard event listener (if we have one).
345 Dali::Clipboard clipboard = Clipboard::Get();
346 char* selectionData = NULL;
349 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
350 selectionData = clipBoardImpl.ExcuteBuffered( false, event );
353 if( selectionData && mClipboardEventNotifier )
355 ClipboardEventNotifier& clipboardEventNotifier( ClipboardEventNotifier::GetImplementation( mClipboardEventNotifier ) );
356 std::string content( selectionData, strlen( selectionData ) );
358 clipboardEventNotifier.SetContent( content );
359 clipboardEventNotifier.EmitContentSelectedSignal();
361 DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d): %s\n" , strlen(selectionData), selectionData );
365 void EventHandler::OnStyleChanged( StyleChange::Type styleChange )
367 SendEvent( styleChange );
370 void EventHandler::OnAccessibilityNotification( const WindowBase::AccessibilityInfo& info )
372 #ifdef DALI_ELDBUS_AVAILABLE
378 if( !mAccessibilityAdaptor )
380 DALI_LOG_ERROR( "Invalid accessibility adaptor\n" );
384 AccessibilityAdaptor* accessibilityAdaptor( &AccessibilityAdaptor::GetImplementation( mAccessibilityAdaptor ) );
385 if( !accessibilityAdaptor )
387 DALI_LOG_ERROR( "Cannot access accessibility adaptor\n" );
391 // Create a touch point object.
392 TouchPoint::State touchPointState( TouchPoint::Down );
393 if( info.state == 0 )
395 touchPointState = TouchPoint::Down; // Mouse down.
397 else if( info.state == 1 )
399 touchPointState = TouchPoint::Motion; // Mouse move.
401 else if( info.state == 2 )
403 touchPointState = TouchPoint::Up; // Mouse up.
407 touchPointState = TouchPoint::Interrupted; // Error.
410 // Send touch event to accessibility adaptor.
411 TouchPoint point( 0, touchPointState, static_cast< float >( info.startX ), static_cast< float >( info.startY ) );
413 // Perform actions based on received gestures.
414 // Note: This is seperated from the reading so we can have other input readers without changing the below code.
415 switch( info.gestureValue )
417 case 0: // OneFingerHover
420 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
423 case 1: // TwoFingersHover
425 // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
426 accessibilityAdaptor->HandleActionScrollEvent( point, GetCurrentMilliSeconds() );
429 case 2: // ThreeFingersHover
431 // Read from top item on screen continuously.
432 accessibilityAdaptor->HandleActionReadFromTopEvent();
435 case 3: // OneFingerFlickLeft
437 // Move to previous item.
438 accessibilityAdaptor->HandleActionReadPreviousEvent();
441 case 4: // OneFingerFlickRight
443 // Move to next item.
444 accessibilityAdaptor->HandleActionReadNextEvent();
447 case 5: // OneFingerFlickUp
449 // Move to previous item.
450 accessibilityAdaptor->HandleActionPreviousEvent();
453 case 6: // OneFingerFlickDown
455 // Move to next item.
456 accessibilityAdaptor->HandleActionNextEvent();
459 case 7: // TwoFingersFlickUp
461 // Scroll up the list.
462 accessibilityAdaptor->HandleActionScrollUpEvent();
465 case 8: // TwoFingersFlickDown
467 // Scroll down the list.
468 accessibilityAdaptor->HandleActionScrollDownEvent();
471 case 9: // TwoFingersFlickLeft
473 // Scroll left to the previous page
474 accessibilityAdaptor->HandleActionPageLeftEvent();
477 case 10: // TwoFingersFlickRight
479 // Scroll right to the next page
480 accessibilityAdaptor->HandleActionPageRightEvent();
483 case 11: // ThreeFingersFlickLeft
488 case 12: // ThreeFingersFlickRight
493 case 13: // ThreeFingersFlickUp
498 case 14: // ThreeFingersFlickDown
503 case 15: // OneFingerSingleTap
506 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
509 case 16: // OneFingerDoubleTap
511 // Activate selected item / active edit mode.
512 accessibilityAdaptor->HandleActionActivateEvent();
515 case 17: // OneFingerTripleTap
518 accessibilityAdaptor->HandleActionZoomEvent();
521 case 18: // TwoFingersSingleTap
523 // Pause/Resume current speech
524 accessibilityAdaptor->HandleActionReadPauseResumeEvent();
527 case 19: // TwoFingersDoubleTap
529 // Start/Stop current action
530 accessibilityAdaptor->HandleActionStartStopEvent();
533 case 20: // TwoFingersTripleTap
535 // Read information from indicator
536 accessibilityAdaptor->HandleActionReadIndicatorInformationEvent();
539 case 21: // ThreeFingersSingleTap
541 // Read from top item on screen continuously.
542 accessibilityAdaptor->HandleActionReadFromTopEvent();
545 case 22: // ThreeFingersDoubleTap
547 // Read from next item continuously.
548 accessibilityAdaptor->HandleActionReadFromNextEvent();
551 case 23: // ThreeFingersTripleTap
556 case 24: // OneFingerFlickLeftReturn
558 // Scroll up to the previous page
559 accessibilityAdaptor->HandleActionPageUpEvent();
562 case 25: // OneFingerFlickRightReturn
564 // Scroll down to the next page
565 accessibilityAdaptor->HandleActionPageDownEvent();
568 case 26: // OneFingerFlickUpReturn
570 // Move to the first item on screen
571 accessibilityAdaptor->HandleActionMoveToFirstEvent();
574 case 27: // OneFingerFlickDownReturn
576 // Move to the last item on screen
577 accessibilityAdaptor->HandleActionMoveToLastEvent();
580 case 28: // TwoFingersFlickLeftReturn
585 case 29: // TwoFingersFlickRightReturn
590 case 30: // TwoFingersFlickUpReturn
595 case 31: // TwoFingersFlickDownReturn
600 case 32: // ThreeFingersFlickLeftReturn
605 case 33: // ThreeFingersFlickRightReturn
610 case 34: // ThreeFingersFlickUpReturn
615 case 35: // ThreeFingersFlickDownReturn
624 void EventHandler::ConvertTouchPosition( Integration::Point& point )
626 Vector2 position = point.GetScreenPosition();
627 Vector2 convertedPosition;
629 switch( mRotationAngle )
633 convertedPosition.x = mWindowWidth - position.y;
634 convertedPosition.y = position.x;
639 convertedPosition.x = mWindowWidth - position.x;
640 convertedPosition.y = mWindowHeight - position.y;
645 convertedPosition.x = position.y;
646 convertedPosition.y = mWindowHeight - position.x;
651 convertedPosition = position;
656 point.SetScreenPosition( convertedPosition );
659 } // namespace Adaptor
661 } // namespace Internal