2 * Copyright (c) 2019 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>
33 #include <dali/integration-api/scene.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 uint32_t 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 static_cast<uint32_t>( (tp.tv_sec * 1000 ) + (tp.tv_nsec / 1000000L) );
94 gettimeofday(&tv, NULL);
95 return static_cast<uint32_t>( (tv.tv_sec * 1000 ) + (tv.tv_usec / 1000) );
98 } // unnamed namespace
100 EventHandler::EventHandler( Dali::Integration::Scene scene, CoreEventInterface& coreEventInterface, DamageObserver& damageObserver )
102 mCoreEventInterface( coreEventInterface ),
103 mStyleMonitor( StyleMonitor::Get() ),
104 mDamageObserver( damageObserver ),
105 mRotationObserver( NULL ),
106 mAccessibilityAdaptor( AccessibilityAdaptor::Get() ),
107 mClipboardEventNotifier( ClipboardEventNotifier::Get() ),
108 mClipboard( Clipboard::Get() ),
114 // this code only works with the WindowRenderSurface so need to downcast
115 WindowRenderSurface* windowRenderSurface = static_cast< WindowRenderSurface* >( scene.GetSurface() );
116 if( windowRenderSurface )
118 WindowBase* windowBase = windowRenderSurface->GetWindowBase();
121 windowBase->WindowDamagedSignal().Connect( this, &EventHandler::OnWindowDamaged );
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()
138 void EventHandler::SendEvent( Integration::Point& point, uint32_t timeStamp )
142 timeStamp = GetCurrentMilliSeconds();
145 ConvertTouchPosition( point );
147 Integration::TouchEvent touchEvent;
148 Integration::HoverEvent hoverEvent;
149 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
150 if( type != Integration::TouchEventCombiner::DispatchNone )
152 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 );
153 // First the touch and/or hover event & related gesture events are queued
154 if( type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth )
156 mScene.QueueEvent( touchEvent );
159 if( type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth )
161 mScene.QueueEvent( hoverEvent );
164 // Next the events are processed with a single call into Core
165 mCoreEventInterface.ProcessCoreEvents();
169 void EventHandler::SendEvent(Integration::KeyEvent& keyEvent)
171 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
172 if( physicalKeyboard )
174 if( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
176 GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
180 // Create send KeyEvent to Core.
181 mScene.QueueEvent( keyEvent );
182 mCoreEventInterface.ProcessCoreEvents();
185 void EventHandler::SendWheelEvent( WheelEvent& wheelEvent )
187 // Create WheelEvent and send to Core.
188 Integration::WheelEvent event( static_cast< Integration::WheelEvent::Type >(wheelEvent.type), wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp );
190 mScene.QueueEvent( event );
191 mCoreEventInterface.ProcessCoreEvents();
194 void EventHandler::SendEvent( StyleChange::Type styleChange )
196 DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
197 GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
200 void EventHandler::SendEvent( const DamageArea& area )
202 mDamageObserver.OnDamaged( area );
205 void EventHandler::SendRotationPrepareEvent( const RotationEvent& event )
207 if( mRotationObserver != NULL )
209 mRotationAngle = event.angle;
210 mWindowWidth = event.width;
211 mWindowHeight = event.height;
213 mRotationObserver->OnRotationPrepare( event );
214 mRotationObserver->OnRotationRequest();
218 void EventHandler::SendRotationRequestEvent( )
220 // No need to separate event into prepare and request
223 void EventHandler::FeedTouchPoint( TouchPoint& point, uint32_t timeStamp)
225 Integration::Point convertedPoint( point );
226 SendEvent( convertedPoint, timeStamp );
229 void EventHandler::FeedWheelEvent( WheelEvent& wheelEvent )
231 SendWheelEvent( wheelEvent );
234 void EventHandler::FeedKeyEvent( KeyEvent& event )
236 Integration::KeyEvent convertedEvent( event );
237 SendEvent( convertedEvent );
240 void EventHandler::FeedEvent( Integration::Event& event )
242 mScene.QueueEvent( event );
243 mCoreEventInterface.ProcessCoreEvents();
246 void EventHandler::Reset()
250 // Any touch listeners should be told of the interruption.
251 Integration::TouchEvent event;
252 Integration::Point point;
253 point.SetState( PointState::INTERRUPTED );
254 event.AddPoint( point );
256 // First the touch event & related gesture events are queued
257 mScene.QueueEvent( event );
259 // Next the events are processed with a single call into Core
260 mCoreEventInterface.ProcessCoreEvents();
263 void EventHandler::Pause()
269 void EventHandler::Resume()
275 void EventHandler::SetRotationObserver( RotationObserver* observer )
277 mRotationObserver = observer;
280 void EventHandler::OnTouchEvent( Integration::Point& point, uint32_t timeStamp )
282 SendEvent( point, timeStamp );
285 void EventHandler::OnWheelEvent( WheelEvent& wheelEvent )
287 SendWheelEvent( wheelEvent );
290 void EventHandler::OnKeyEvent( Integration::KeyEvent& keyEvent )
292 SendEvent( keyEvent );
295 void EventHandler::OnFocusChanged( bool focusIn )
297 // If the window gains focus and we hid the keyboard then show it again.
300 Dali::Clipboard clipboard = Clipboard::Get();
303 clipboard.HideClipboard();
308 // Hiding clipboard event will be ignored once because window focus out event is always received on showing clipboard
309 Dali::Clipboard clipboard = Clipboard::Get();
312 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
313 clipBoardImpl.HideClipboard(true);
318 void EventHandler::OnWindowDamaged( const DamageArea& area )
323 void EventHandler::OnSelectionDataSend( void* event )
325 Dali::Clipboard clipboard = Clipboard::Get();
328 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
329 clipBoardImpl.ExcuteBuffered( true, event );
333 void EventHandler::OnSelectionDataReceived( void* event )
335 // We have got the selected content, inform the clipboard event listener (if we have one).
336 Dali::Clipboard clipboard = Clipboard::Get();
337 char* selectionData = NULL;
340 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
341 selectionData = clipBoardImpl.ExcuteBuffered( false, event );
344 if( selectionData && mClipboardEventNotifier )
346 ClipboardEventNotifier& clipboardEventNotifier( ClipboardEventNotifier::GetImplementation( mClipboardEventNotifier ) );
347 std::string content( selectionData, strlen( selectionData ) );
349 clipboardEventNotifier.SetContent( content );
350 clipboardEventNotifier.EmitContentSelectedSignal();
352 DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d): %s\n" , strlen(selectionData), selectionData );
356 void EventHandler::OnStyleChanged( StyleChange::Type styleChange )
358 SendEvent( styleChange );
361 void EventHandler::OnAccessibilityNotification( const WindowBase::AccessibilityInfo& info )
363 #ifdef DALI_ELDBUS_AVAILABLE
369 if( !mAccessibilityAdaptor )
371 DALI_LOG_ERROR( "Invalid accessibility adaptor\n" );
375 AccessibilityAdaptor* accessibilityAdaptor( &AccessibilityAdaptor::GetImplementation( mAccessibilityAdaptor ) );
376 if( !accessibilityAdaptor )
378 DALI_LOG_ERROR( "Cannot access accessibility adaptor\n" );
382 // Create a touch point object.
383 TouchPoint::State touchPointState( TouchPoint::Down );
384 if( info.state == 0 )
386 touchPointState = TouchPoint::Down; // Mouse down.
388 else if( info.state == 1 )
390 touchPointState = TouchPoint::Motion; // Mouse move.
392 else if( info.state == 2 )
394 touchPointState = TouchPoint::Up; // Mouse up.
398 touchPointState = TouchPoint::Interrupted; // Error.
401 // Send touch event to accessibility adaptor.
402 TouchPoint point( 0, touchPointState, static_cast< float >( info.startX ), static_cast< float >( info.startY ) );
404 // Perform actions based on received gestures.
405 // Note: This is seperated from the reading so we can have other input readers without changing the below code.
406 switch( info.gestureValue )
408 case 0: // OneFingerHover
411 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
414 case 1: // TwoFingersHover
416 // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
417 accessibilityAdaptor->HandleActionScrollEvent( point, GetCurrentMilliSeconds() );
420 case 2: // ThreeFingersHover
422 // Read from top item on screen continuously.
423 accessibilityAdaptor->HandleActionReadFromTopEvent();
426 case 3: // OneFingerFlickLeft
428 // Move to previous item.
429 accessibilityAdaptor->HandleActionReadPreviousEvent();
432 case 4: // OneFingerFlickRight
434 // Move to next item.
435 accessibilityAdaptor->HandleActionReadNextEvent();
438 case 5: // OneFingerFlickUp
440 // Move to previous item.
441 accessibilityAdaptor->HandleActionPreviousEvent();
444 case 6: // OneFingerFlickDown
446 // Move to next item.
447 accessibilityAdaptor->HandleActionNextEvent();
450 case 7: // TwoFingersFlickUp
452 // Scroll up the list.
453 accessibilityAdaptor->HandleActionScrollUpEvent();
456 case 8: // TwoFingersFlickDown
458 // Scroll down the list.
459 accessibilityAdaptor->HandleActionScrollDownEvent();
462 case 9: // TwoFingersFlickLeft
464 // Scroll left to the previous page
465 accessibilityAdaptor->HandleActionPageLeftEvent();
468 case 10: // TwoFingersFlickRight
470 // Scroll right to the next page
471 accessibilityAdaptor->HandleActionPageRightEvent();
474 case 11: // ThreeFingersFlickLeft
479 case 12: // ThreeFingersFlickRight
484 case 13: // ThreeFingersFlickUp
489 case 14: // ThreeFingersFlickDown
494 case 15: // OneFingerSingleTap
497 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
500 case 16: // OneFingerDoubleTap
502 // Activate selected item / active edit mode.
503 accessibilityAdaptor->HandleActionActivateEvent();
506 case 17: // OneFingerTripleTap
509 accessibilityAdaptor->HandleActionZoomEvent();
512 case 18: // TwoFingersSingleTap
514 // Pause/Resume current speech
515 accessibilityAdaptor->HandleActionReadPauseResumeEvent();
518 case 19: // TwoFingersDoubleTap
520 // Start/Stop current action
521 accessibilityAdaptor->HandleActionStartStopEvent();
524 case 20: // TwoFingersTripleTap
526 // Read information from indicator
530 case 21: // ThreeFingersSingleTap
532 // Read from top item on screen continuously.
533 accessibilityAdaptor->HandleActionReadFromTopEvent();
536 case 22: // ThreeFingersDoubleTap
538 // Read from next item continuously.
539 accessibilityAdaptor->HandleActionReadFromNextEvent();
542 case 23: // ThreeFingersTripleTap
547 case 24: // OneFingerFlickLeftReturn
549 // Scroll up to the previous page
550 accessibilityAdaptor->HandleActionPageUpEvent();
553 case 25: // OneFingerFlickRightReturn
555 // Scroll down to the next page
556 accessibilityAdaptor->HandleActionPageDownEvent();
559 case 26: // OneFingerFlickUpReturn
561 // Move to the first item on screen
562 accessibilityAdaptor->HandleActionMoveToFirstEvent();
565 case 27: // OneFingerFlickDownReturn
567 // Move to the last item on screen
568 accessibilityAdaptor->HandleActionMoveToLastEvent();
571 case 28: // TwoFingersFlickLeftReturn
576 case 29: // TwoFingersFlickRightReturn
581 case 30: // TwoFingersFlickUpReturn
586 case 31: // TwoFingersFlickDownReturn
591 case 32: // ThreeFingersFlickLeftReturn
596 case 33: // ThreeFingersFlickRightReturn
601 case 34: // ThreeFingersFlickUpReturn
606 case 35: // ThreeFingersFlickDownReturn
615 void EventHandler::ConvertTouchPosition( Integration::Point& point )
617 Vector2 position = point.GetScreenPosition();
618 Vector2 convertedPosition;
620 switch( mRotationAngle )
624 convertedPosition.x = static_cast<float>( mWindowWidth ) - position.y;
625 convertedPosition.y = position.x;
630 convertedPosition.x = static_cast<float>( mWindowWidth ) - position.x;
631 convertedPosition.y = static_cast<float>( mWindowHeight ) - position.y;
636 convertedPosition.x = position.y;
637 convertedPosition.y = static_cast<float>( mWindowHeight ) - position.x;
642 convertedPosition = position;
647 point.SetScreenPosition( convertedPosition );
650 } // namespace Adaptor
652 } // namespace Internal