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>
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( WheelEvent& wheelEvent )
154 Integration::WheelEvent event( static_cast< Integration::WheelEvent::Type >(wheelEvent.type), wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp );
156 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
158 (*iter)->OnWheelEvent( event );
162 void EventHandler::OnKeyEvent( Integration::KeyEvent& keyEvent )
164 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
166 (*iter)->OnKeyEvent( keyEvent );
170 void EventHandler::OnFocusChanged( bool focusIn )
172 // If the window gains focus and we hid the keyboard then show it again.
175 Dali::Clipboard clipboard = Clipboard::Get();
178 clipboard.HideClipboard();
183 // Hiding clipboard event will be ignored once because window focus out event is always received on showing clipboard
184 Dali::Clipboard clipboard = Clipboard::Get();
187 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
188 clipBoardImpl.HideClipboard(true);
193 void EventHandler::OnRotation( const RotationEvent& event )
195 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
197 (*iter)->OnRotation( event );
201 void EventHandler::OnWindowDamaged( const DamageArea& area )
206 void EventHandler::OnSelectionDataSend( void* event )
208 Dali::Clipboard clipboard = Clipboard::Get();
211 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
212 clipBoardImpl.ExcuteBuffered( true, event );
216 void EventHandler::OnSelectionDataReceived( void* event )
218 // We have got the selected content, inform the clipboard event listener (if we have one).
219 Dali::Clipboard clipboard = Clipboard::Get();
220 char* selectionData = NULL;
223 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
224 selectionData = clipBoardImpl.ExcuteBuffered( false, event );
227 if( selectionData && mClipboardEventNotifier )
229 ClipboardEventNotifier& clipboardEventNotifier( ClipboardEventNotifier::GetImplementation( mClipboardEventNotifier ) );
230 std::string content( selectionData, strlen( selectionData ) );
232 clipboardEventNotifier.SetContent( content );
233 clipboardEventNotifier.EmitContentSelectedSignal();
235 DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d): %s\n" , strlen(selectionData), selectionData );
239 void EventHandler::OnStyleChanged( StyleChange::Type styleChange )
241 SendEvent( styleChange );
244 void EventHandler::OnAccessibilityNotification( const WindowBase::AccessibilityInfo& info )
246 #ifdef DALI_ELDBUS_AVAILABLE
252 if( !mAccessibilityAdaptor )
254 DALI_LOG_ERROR( "Invalid accessibility adaptor\n" );
258 AccessibilityAdaptor* accessibilityAdaptor( &AccessibilityAdaptor::GetImplementation( mAccessibilityAdaptor ) );
259 if( !accessibilityAdaptor )
261 DALI_LOG_ERROR( "Cannot access accessibility adaptor\n" );
265 // Create a touch point object.
266 TouchPoint::State touchPointState( TouchPoint::Down );
267 if( info.state == 0 )
269 touchPointState = TouchPoint::Down; // Mouse down.
271 else if( info.state == 1 )
273 touchPointState = TouchPoint::Motion; // Mouse move.
275 else if( info.state == 2 )
277 touchPointState = TouchPoint::Up; // Mouse up.
281 touchPointState = TouchPoint::Interrupted; // Error.
284 // Send touch event to accessibility adaptor.
285 TouchPoint point( 0, touchPointState, static_cast< float >( info.startX ), static_cast< float >( info.startY ) );
287 // Perform actions based on received gestures.
288 // Note: This is seperated from the reading so we can have other input readers without changing the below code.
289 switch( info.gestureValue )
291 case 0: // OneFingerHover
294 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
297 case 1: // TwoFingersHover
299 // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
300 accessibilityAdaptor->HandleActionScrollEvent( point, GetCurrentMilliSeconds() );
303 case 2: // ThreeFingersHover
305 // Read from top item on screen continuously.
306 accessibilityAdaptor->HandleActionReadFromTopEvent();
309 case 3: // OneFingerFlickLeft
311 // Move to previous item.
312 accessibilityAdaptor->HandleActionReadPreviousEvent();
315 case 4: // OneFingerFlickRight
317 // Move to next item.
318 accessibilityAdaptor->HandleActionReadNextEvent();
321 case 5: // OneFingerFlickUp
323 // Move to previous item.
324 accessibilityAdaptor->HandleActionPreviousEvent();
327 case 6: // OneFingerFlickDown
329 // Move to next item.
330 accessibilityAdaptor->HandleActionNextEvent();
333 case 7: // TwoFingersFlickUp
335 // Scroll up the list.
336 accessibilityAdaptor->HandleActionScrollUpEvent();
339 case 8: // TwoFingersFlickDown
341 // Scroll down the list.
342 accessibilityAdaptor->HandleActionScrollDownEvent();
345 case 9: // TwoFingersFlickLeft
347 // Scroll left to the previous page
348 accessibilityAdaptor->HandleActionPageLeftEvent();
351 case 10: // TwoFingersFlickRight
353 // Scroll right to the next page
354 accessibilityAdaptor->HandleActionPageRightEvent();
357 case 11: // ThreeFingersFlickLeft
362 case 12: // ThreeFingersFlickRight
367 case 13: // ThreeFingersFlickUp
372 case 14: // ThreeFingersFlickDown
377 case 15: // OneFingerSingleTap
380 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
383 case 16: // OneFingerDoubleTap
385 // Activate selected item / active edit mode.
386 accessibilityAdaptor->HandleActionActivateEvent();
389 case 17: // OneFingerTripleTap
392 accessibilityAdaptor->HandleActionZoomEvent();
395 case 18: // TwoFingersSingleTap
397 // Pause/Resume current speech
398 accessibilityAdaptor->HandleActionReadPauseResumeEvent();
401 case 19: // TwoFingersDoubleTap
403 // Start/Stop current action
404 accessibilityAdaptor->HandleActionStartStopEvent();
407 case 20: // TwoFingersTripleTap
409 // Read information from indicator
413 case 21: // ThreeFingersSingleTap
415 // Read from top item on screen continuously.
416 accessibilityAdaptor->HandleActionReadFromTopEvent();
419 case 22: // ThreeFingersDoubleTap
421 // Read from next item continuously.
422 accessibilityAdaptor->HandleActionReadFromNextEvent();
425 case 23: // ThreeFingersTripleTap
430 case 24: // OneFingerFlickLeftReturn
432 // Scroll up to the previous page
433 accessibilityAdaptor->HandleActionPageUpEvent();
436 case 25: // OneFingerFlickRightReturn
438 // Scroll down to the next page
439 accessibilityAdaptor->HandleActionPageDownEvent();
442 case 26: // OneFingerFlickUpReturn
444 // Move to the first item on screen
445 accessibilityAdaptor->HandleActionMoveToFirstEvent();
448 case 27: // OneFingerFlickDownReturn
450 // Move to the last item on screen
451 accessibilityAdaptor->HandleActionMoveToLastEvent();
454 case 28: // TwoFingersFlickLeftReturn
459 case 29: // TwoFingersFlickRightReturn
464 case 30: // TwoFingersFlickUpReturn
469 case 31: // TwoFingersFlickDownReturn
474 case 32: // ThreeFingersFlickLeftReturn
479 case 33: // ThreeFingersFlickRightReturn
484 case 34: // ThreeFingersFlickUpReturn
489 case 35: // ThreeFingersFlickDownReturn
498 void EventHandler::AddObserver( Observer& observer )
500 ObserverContainer::iterator match ( find(mObservers.begin(), mObservers.end(), &observer) );
502 if ( match == mObservers.end() )
504 mObservers.push_back( &observer );
508 void EventHandler::RemoveObserver( Observer& observer )
510 ObserverContainer::iterator match ( find(mObservers.begin(), mObservers.end(), &observer) );
512 if ( match != mObservers.end() )
514 mObservers.erase( match );
518 } // namespace Adaptor
520 } // namespace Internal