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/devel-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( WindowBase* windowBase, DamageObserver& damageObserver )
99 : mStyleMonitor( StyleMonitor::Get() ),
100 mDamageObserver( damageObserver ),
101 mAccessibilityAdaptor( AccessibilityAdaptor::Get() ),
102 mClipboardEventNotifier( ClipboardEventNotifier::Get() ),
103 mClipboard( Clipboard::Get() ),
107 windowBase->WindowDamagedSignal().Connect( this, &EventHandler::OnWindowDamaged );
108 windowBase->FocusChangedSignal().Connect( this, &EventHandler::OnFocusChanged );
109 windowBase->RotationSignal().Connect( this, &EventHandler::OnRotation );
110 windowBase->TouchEventSignal().Connect( this, &EventHandler::OnTouchEvent );
111 windowBase->WheelEventSignal().Connect( this, &EventHandler::OnWheelEvent );
112 windowBase->KeyEventSignal().Connect( this, &EventHandler::OnKeyEvent );
113 windowBase->SelectionDataSendSignal().Connect( this, &EventHandler::OnSelectionDataSend );
114 windowBase->SelectionDataReceivedSignal().Connect( this, &EventHandler::OnSelectionDataReceived );
115 windowBase->StyleChangedSignal().Connect( this, &EventHandler::OnStyleChanged );
116 windowBase->AccessibilitySignal().Connect( this, &EventHandler::OnAccessibilityNotification );
119 EventHandler::~EventHandler()
123 void EventHandler::SendEvent( StyleChange::Type styleChange )
125 DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
126 GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
129 void EventHandler::SendEvent( const DamageArea& area )
131 mDamageObserver.OnDamaged( area );
134 void EventHandler::Pause()
139 void EventHandler::Resume()
144 void EventHandler::OnTouchEvent( Integration::Point& point, uint32_t timeStamp )
146 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
148 (*iter)->OnTouchPoint( point, timeStamp );
152 void EventHandler::OnWheelEvent( Integration::WheelEvent& wheelEvent )
154 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
156 (*iter)->OnWheelEvent( wheelEvent );
160 void EventHandler::OnKeyEvent( Integration::KeyEvent& keyEvent )
162 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
164 (*iter)->OnKeyEvent( keyEvent );
168 void EventHandler::OnFocusChanged( bool focusIn )
170 // If the window gains focus and we hid the keyboard then show it again.
173 Dali::Clipboard clipboard = Clipboard::Get();
176 clipboard.HideClipboard();
181 // Hiding clipboard event will be ignored once because window focus out event is always received on showing clipboard
182 Dali::Clipboard clipboard = Clipboard::Get();
185 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
186 clipBoardImpl.HideClipboard(true);
191 void EventHandler::OnRotation( const RotationEvent& event )
193 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
195 (*iter)->OnRotation( event );
199 void EventHandler::OnWindowDamaged( const DamageArea& area )
204 void EventHandler::OnSelectionDataSend( void* event )
206 Dali::Clipboard clipboard = Clipboard::Get();
209 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
210 clipBoardImpl.ExcuteBuffered( true, event );
214 void EventHandler::OnSelectionDataReceived( void* event )
216 // We have got the selected content, inform the clipboard event listener (if we have one).
217 Dali::Clipboard clipboard = Clipboard::Get();
218 char* selectionData = NULL;
221 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
222 selectionData = clipBoardImpl.ExcuteBuffered( false, event );
225 if( selectionData && mClipboardEventNotifier )
227 ClipboardEventNotifier& clipboardEventNotifier( ClipboardEventNotifier::GetImplementation( mClipboardEventNotifier ) );
228 std::string content( selectionData, strlen( selectionData ) );
230 clipboardEventNotifier.SetContent( content );
231 clipboardEventNotifier.EmitContentSelectedSignal();
233 DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d): %s\n" , strlen(selectionData), selectionData );
237 void EventHandler::OnStyleChanged( StyleChange::Type styleChange )
239 SendEvent( styleChange );
242 void EventHandler::OnAccessibilityNotification( const WindowBase::AccessibilityInfo& info )
244 #ifdef DALI_ELDBUS_AVAILABLE
250 if( !mAccessibilityAdaptor )
252 DALI_LOG_ERROR( "Invalid accessibility adaptor\n" );
256 AccessibilityAdaptor* accessibilityAdaptor( &AccessibilityAdaptor::GetImplementation( mAccessibilityAdaptor ) );
257 if( !accessibilityAdaptor )
259 DALI_LOG_ERROR( "Cannot access accessibility adaptor\n" );
263 // Create a touch point object.
264 PointState::Type touchPointState( PointState::DOWN );
265 if( info.state == 0 )
267 touchPointState = PointState::DOWN; // Mouse down.
269 else if( info.state == 1 )
271 touchPointState = PointState::MOTION; // Mouse move.
273 else if( info.state == 2 )
275 touchPointState = PointState::UP; // Mouse up.
279 touchPointState = PointState::INTERRUPTED; // Error.
282 // Send touch event to accessibility adaptor.
283 TouchPoint point( 0, touchPointState, static_cast< float >( info.startX ), static_cast< float >( info.startY ) );
285 // Perform actions based on received gestures.
286 // Note: This is seperated from the reading so we can have other input readers without changing the below code.
287 switch( info.gestureValue )
289 case 0: // OneFingerHover
292 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
295 case 1: // TwoFingersHover
297 // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
298 accessibilityAdaptor->HandleActionScrollEvent( point, GetCurrentMilliSeconds() );
301 case 2: // ThreeFingersHover
303 // Read from top item on screen continuously.
304 accessibilityAdaptor->HandleActionReadFromTopEvent();
307 case 3: // OneFingerFlickLeft
309 // Move to previous item.
310 accessibilityAdaptor->HandleActionReadPreviousEvent();
313 case 4: // OneFingerFlickRight
315 // Move to next item.
316 accessibilityAdaptor->HandleActionReadNextEvent();
319 case 5: // OneFingerFlickUp
321 // Move to previous item.
322 accessibilityAdaptor->HandleActionPreviousEvent();
325 case 6: // OneFingerFlickDown
327 // Move to next item.
328 accessibilityAdaptor->HandleActionNextEvent();
331 case 7: // TwoFingersFlickUp
333 // Scroll up the list.
334 accessibilityAdaptor->HandleActionScrollUpEvent();
337 case 8: // TwoFingersFlickDown
339 // Scroll down the list.
340 accessibilityAdaptor->HandleActionScrollDownEvent();
343 case 9: // TwoFingersFlickLeft
345 // Scroll left to the previous page
346 accessibilityAdaptor->HandleActionPageLeftEvent();
349 case 10: // TwoFingersFlickRight
351 // Scroll right to the next page
352 accessibilityAdaptor->HandleActionPageRightEvent();
355 case 11: // ThreeFingersFlickLeft
360 case 12: // ThreeFingersFlickRight
365 case 13: // ThreeFingersFlickUp
370 case 14: // ThreeFingersFlickDown
375 case 15: // OneFingerSingleTap
378 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
381 case 16: // OneFingerDoubleTap
383 // Activate selected item / active edit mode.
384 accessibilityAdaptor->HandleActionActivateEvent();
387 case 17: // OneFingerTripleTap
390 accessibilityAdaptor->HandleActionZoomEvent();
393 case 18: // TwoFingersSingleTap
395 // Pause/Resume current speech
396 accessibilityAdaptor->HandleActionReadPauseResumeEvent();
399 case 19: // TwoFingersDoubleTap
401 // Start/Stop current action
402 accessibilityAdaptor->HandleActionStartStopEvent();
405 case 20: // TwoFingersTripleTap
407 // Read information from indicator
411 case 21: // ThreeFingersSingleTap
413 // Read from top item on screen continuously.
414 accessibilityAdaptor->HandleActionReadFromTopEvent();
417 case 22: // ThreeFingersDoubleTap
419 // Read from next item continuously.
420 accessibilityAdaptor->HandleActionReadFromNextEvent();
423 case 23: // ThreeFingersTripleTap
428 case 24: // OneFingerFlickLeftReturn
430 // Scroll up to the previous page
431 accessibilityAdaptor->HandleActionPageUpEvent();
434 case 25: // OneFingerFlickRightReturn
436 // Scroll down to the next page
437 accessibilityAdaptor->HandleActionPageDownEvent();
440 case 26: // OneFingerFlickUpReturn
442 // Move to the first item on screen
443 accessibilityAdaptor->HandleActionMoveToFirstEvent();
446 case 27: // OneFingerFlickDownReturn
448 // Move to the last item on screen
449 accessibilityAdaptor->HandleActionMoveToLastEvent();
452 case 28: // TwoFingersFlickLeftReturn
457 case 29: // TwoFingersFlickRightReturn
462 case 30: // TwoFingersFlickUpReturn
467 case 31: // TwoFingersFlickDownReturn
472 case 32: // ThreeFingersFlickLeftReturn
477 case 33: // ThreeFingersFlickRightReturn
482 case 34: // ThreeFingersFlickUpReturn
487 case 35: // ThreeFingersFlickDownReturn
496 void EventHandler::AddObserver( Observer& observer )
498 ObserverContainer::iterator match ( find(mObservers.begin(), mObservers.end(), &observer) );
500 if ( match == mObservers.end() )
502 mObservers.push_back( &observer );
506 void EventHandler::RemoveObserver( Observer& observer )
508 ObserverContainer::iterator match ( find(mObservers.begin(), mObservers.end(), &observer) );
510 if ( match != mObservers.end() )
512 mObservers.erase( match );
516 } // namespace Adaptor
518 } // namespace Internal