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->FocusChangedSignal().Connect( this, &EventHandler::OnFocusChanged );
123 windowBase->RotationSignal().Connect( this, &EventHandler::SendRotationPrepareEvent );
124 windowBase->TouchEventSignal().Connect( this, &EventHandler::OnTouchEvent );
125 windowBase->WheelEventSignal().Connect( this, &EventHandler::OnWheelEvent );
126 windowBase->KeyEventSignal().Connect( this, &EventHandler::OnKeyEvent );
127 windowBase->SelectionDataSendSignal().Connect( this, &EventHandler::OnSelectionDataSend );
128 windowBase->SelectionDataReceivedSignal().Connect( this, &EventHandler::OnSelectionDataReceived );
129 windowBase->StyleChangedSignal().Connect( this, &EventHandler::OnStyleChanged );
130 windowBase->AccessibilitySignal().Connect( this, &EventHandler::OnAccessibilityNotification );
134 EventHandler::~EventHandler()
136 mGestureManager.Stop();
139 void EventHandler::SendEvent(Integration::Point& point, unsigned long timeStamp)
143 timeStamp = GetCurrentMilliSeconds();
146 ConvertTouchPosition( point );
148 Integration::TouchEvent touchEvent;
149 Integration::HoverEvent hoverEvent;
150 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
151 if(type != Integration::TouchEventCombiner::DispatchNone )
153 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);
155 // First the touch and/or hover event & related gesture events are queued
156 if(type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth)
158 mCoreEventInterface.QueueCoreEvent( touchEvent );
159 mGestureManager.SendEvent(touchEvent);
162 if(type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth)
164 mCoreEventInterface.QueueCoreEvent( hoverEvent );
167 // Next the events are processed with a single call into Core
168 mCoreEventInterface.ProcessCoreEvents();
172 void EventHandler::SendEvent(Integration::KeyEvent& keyEvent)
174 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
175 if ( physicalKeyboard )
177 if ( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
179 GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
183 // Create send KeyEvent to Core.
184 mCoreEventInterface.QueueCoreEvent( keyEvent );
185 mCoreEventInterface.ProcessCoreEvents();
188 void EventHandler::SendWheelEvent( WheelEvent& wheelEvent )
190 // Create WheelEvent and send to Core.
191 Integration::WheelEvent event( static_cast< Integration::WheelEvent::Type >(wheelEvent.type), wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp );
192 mCoreEventInterface.QueueCoreEvent( event );
193 mCoreEventInterface.ProcessCoreEvents();
196 void EventHandler::SendEvent( StyleChange::Type styleChange )
198 DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
199 GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
202 void EventHandler::SendEvent( const DamageArea& area )
204 mDamageObserver.OnDamaged( area );
207 void EventHandler::SendRotationPrepareEvent( const RotationEvent& event )
209 if( mRotationObserver != NULL )
211 mRotationAngle = event.angle;
212 mWindowWidth = event.width;
213 mWindowHeight = event.height;
215 mRotationObserver->OnRotationPrepare( event );
216 mRotationObserver->OnRotationRequest();
220 void EventHandler::SendRotationRequestEvent( )
222 // No need to separate event into prepare and request
225 void EventHandler::FeedTouchPoint( TouchPoint& point, int timeStamp)
227 Integration::Point convertedPoint( point );
228 SendEvent(convertedPoint, timeStamp);
231 void EventHandler::FeedWheelEvent( WheelEvent& wheelEvent )
233 SendWheelEvent( wheelEvent );
236 void EventHandler::FeedKeyEvent( KeyEvent& event )
238 Integration::KeyEvent convertedEvent( event );
239 SendEvent( convertedEvent );
242 void EventHandler::FeedEvent( Integration::Event& event )
244 mCoreEventInterface.QueueCoreEvent( event );
245 mCoreEventInterface.ProcessCoreEvents();
248 void EventHandler::Reset()
252 // Any touch listeners should be told of the interruption.
253 Integration::TouchEvent event;
254 Integration::Point point;
255 point.SetState( PointState::INTERRUPTED );
256 event.AddPoint( point );
258 // First the touch event & related gesture events are queued
259 mCoreEventInterface.QueueCoreEvent( event );
260 mGestureManager.SendEvent( event );
262 // Next the events are processed with a single call into Core
263 mCoreEventInterface.ProcessCoreEvents();
266 void EventHandler::Pause()
272 void EventHandler::Resume()
278 void EventHandler::SetDragAndDropDetector( DragAndDropDetectorPtr detector )
280 mDragAndDropDetector = detector;
283 void EventHandler::SetRotationObserver( RotationObserver* observer )
285 mRotationObserver = observer;
288 void EventHandler::OnTouchEvent( Integration::Point& point, unsigned long timeStamp )
290 SendEvent( point, timeStamp );
293 void EventHandler::OnWheelEvent( WheelEvent& wheelEvent )
295 SendWheelEvent( wheelEvent );
298 void EventHandler::OnKeyEvent( Integration::KeyEvent& keyEvent )
300 SendEvent( keyEvent );
303 void EventHandler::OnFocusChanged( bool focusIn )
305 // If the window gains focus and we hid the keyboard then show it again.
308 Dali::Clipboard clipboard = Clipboard::Get();
311 clipboard.HideClipboard();
316 // Hiding clipboard event will be ignored once because window focus out event is always received on showing clipboard
317 Dali::Clipboard clipboard = Clipboard::Get();
320 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
321 clipBoardImpl.HideClipboard(true);
326 void EventHandler::OnWindowDamaged( const DamageArea& area )
331 void EventHandler::OnSelectionDataSend( void* event )
333 Dali::Clipboard clipboard = Clipboard::Get();
336 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
337 clipBoardImpl.ExcuteBuffered( true, event );
341 void EventHandler::OnSelectionDataReceived( void* event )
343 // We have got the selected content, inform the clipboard event listener (if we have one).
344 Dali::Clipboard clipboard = Clipboard::Get();
345 char* selectionData = NULL;
348 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
349 selectionData = clipBoardImpl.ExcuteBuffered( false, event );
352 if( selectionData && mClipboardEventNotifier )
354 ClipboardEventNotifier& clipboardEventNotifier( ClipboardEventNotifier::GetImplementation( mClipboardEventNotifier ) );
355 std::string content( selectionData, strlen( selectionData ) );
357 clipboardEventNotifier.SetContent( content );
358 clipboardEventNotifier.EmitContentSelectedSignal();
360 DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d): %s\n" , strlen(selectionData), selectionData );
364 void EventHandler::OnStyleChanged( StyleChange::Type styleChange )
366 SendEvent( styleChange );
369 void EventHandler::OnAccessibilityNotification( const WindowBase::AccessibilityInfo& info )
371 #ifdef DALI_ELDBUS_AVAILABLE
377 if( !mAccessibilityAdaptor )
379 DALI_LOG_ERROR( "Invalid accessibility adaptor\n" );
383 AccessibilityAdaptor* accessibilityAdaptor( &AccessibilityAdaptor::GetImplementation( mAccessibilityAdaptor ) );
384 if( !accessibilityAdaptor )
386 DALI_LOG_ERROR( "Cannot access accessibility adaptor\n" );
390 // Create a touch point object.
391 TouchPoint::State touchPointState( TouchPoint::Down );
392 if( info.state == 0 )
394 touchPointState = TouchPoint::Down; // Mouse down.
396 else if( info.state == 1 )
398 touchPointState = TouchPoint::Motion; // Mouse move.
400 else if( info.state == 2 )
402 touchPointState = TouchPoint::Up; // Mouse up.
406 touchPointState = TouchPoint::Interrupted; // Error.
409 // Send touch event to accessibility adaptor.
410 TouchPoint point( 0, touchPointState, static_cast< float >( info.startX ), static_cast< float >( info.startY ) );
412 // Perform actions based on received gestures.
413 // Note: This is seperated from the reading so we can have other input readers without changing the below code.
414 switch( info.gestureValue )
416 case 0: // OneFingerHover
419 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
422 case 1: // TwoFingersHover
424 // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
425 accessibilityAdaptor->HandleActionScrollEvent( point, GetCurrentMilliSeconds() );
428 case 2: // ThreeFingersHover
430 // Read from top item on screen continuously.
431 accessibilityAdaptor->HandleActionReadFromTopEvent();
434 case 3: // OneFingerFlickLeft
436 // Move to previous item.
437 accessibilityAdaptor->HandleActionReadPreviousEvent();
440 case 4: // OneFingerFlickRight
442 // Move to next item.
443 accessibilityAdaptor->HandleActionReadNextEvent();
446 case 5: // OneFingerFlickUp
448 // Move to previous item.
449 accessibilityAdaptor->HandleActionPreviousEvent();
452 case 6: // OneFingerFlickDown
454 // Move to next item.
455 accessibilityAdaptor->HandleActionNextEvent();
458 case 7: // TwoFingersFlickUp
460 // Scroll up the list.
461 accessibilityAdaptor->HandleActionScrollUpEvent();
464 case 8: // TwoFingersFlickDown
466 // Scroll down the list.
467 accessibilityAdaptor->HandleActionScrollDownEvent();
470 case 9: // TwoFingersFlickLeft
472 // Scroll left to the previous page
473 accessibilityAdaptor->HandleActionPageLeftEvent();
476 case 10: // TwoFingersFlickRight
478 // Scroll right to the next page
479 accessibilityAdaptor->HandleActionPageRightEvent();
482 case 11: // ThreeFingersFlickLeft
487 case 12: // ThreeFingersFlickRight
492 case 13: // ThreeFingersFlickUp
497 case 14: // ThreeFingersFlickDown
502 case 15: // OneFingerSingleTap
505 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
508 case 16: // OneFingerDoubleTap
510 // Activate selected item / active edit mode.
511 accessibilityAdaptor->HandleActionActivateEvent();
514 case 17: // OneFingerTripleTap
517 accessibilityAdaptor->HandleActionZoomEvent();
520 case 18: // TwoFingersSingleTap
522 // Pause/Resume current speech
523 accessibilityAdaptor->HandleActionReadPauseResumeEvent();
526 case 19: // TwoFingersDoubleTap
528 // Start/Stop current action
529 accessibilityAdaptor->HandleActionStartStopEvent();
532 case 20: // TwoFingersTripleTap
534 // Read information from indicator
535 accessibilityAdaptor->HandleActionReadIndicatorInformationEvent();
538 case 21: // ThreeFingersSingleTap
540 // Read from top item on screen continuously.
541 accessibilityAdaptor->HandleActionReadFromTopEvent();
544 case 22: // ThreeFingersDoubleTap
546 // Read from next item continuously.
547 accessibilityAdaptor->HandleActionReadFromNextEvent();
550 case 23: // ThreeFingersTripleTap
555 case 24: // OneFingerFlickLeftReturn
557 // Scroll up to the previous page
558 accessibilityAdaptor->HandleActionPageUpEvent();
561 case 25: // OneFingerFlickRightReturn
563 // Scroll down to the next page
564 accessibilityAdaptor->HandleActionPageDownEvent();
567 case 26: // OneFingerFlickUpReturn
569 // Move to the first item on screen
570 accessibilityAdaptor->HandleActionMoveToFirstEvent();
573 case 27: // OneFingerFlickDownReturn
575 // Move to the last item on screen
576 accessibilityAdaptor->HandleActionMoveToLastEvent();
579 case 28: // TwoFingersFlickLeftReturn
584 case 29: // TwoFingersFlickRightReturn
589 case 30: // TwoFingersFlickUpReturn
594 case 31: // TwoFingersFlickDownReturn
599 case 32: // ThreeFingersFlickLeftReturn
604 case 33: // ThreeFingersFlickRightReturn
609 case 34: // ThreeFingersFlickUpReturn
614 case 35: // ThreeFingersFlickDownReturn
623 void EventHandler::ConvertTouchPosition( Integration::Point& point )
625 Vector2 position = point.GetScreenPosition();
626 Vector2 convertedPosition;
628 switch( mRotationAngle )
632 convertedPosition.x = mWindowWidth - position.y;
633 convertedPosition.y = position.x;
638 convertedPosition.x = mWindowWidth - position.x;
639 convertedPosition.y = mWindowHeight - position.y;
644 convertedPosition.x = position.y;
645 convertedPosition.y = mWindowHeight - position.x;
650 convertedPosition = position;
655 point.SetScreenPosition( convertedPosition );
658 } // namespace Adaptor
660 } // namespace Internal