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( WindowRenderSurface* surface, DamageObserver& damageObserver )
99 : mStyleMonitor( StyleMonitor::Get() ),
100 mDamageObserver( damageObserver ),
101 mAccessibilityAdaptor( AccessibilityAdaptor::Get() ),
102 mClipboardEventNotifier( ClipboardEventNotifier::Get() ),
103 mClipboard( Clipboard::Get() ),
108 WindowBase* windowBase = surface->GetWindowBase();
111 windowBase->WindowDamagedSignal().Connect( this, &EventHandler::OnWindowDamaged );
112 windowBase->FocusChangedSignal().Connect( this, &EventHandler::OnFocusChanged );
113 windowBase->RotationSignal().Connect( this, &EventHandler::OnRotation );
114 windowBase->TouchEventSignal().Connect( this, &EventHandler::OnTouchEvent );
115 windowBase->WheelEventSignal().Connect( this, &EventHandler::OnWheelEvent );
116 windowBase->KeyEventSignal().Connect( this, &EventHandler::OnKeyEvent );
117 windowBase->SelectionDataSendSignal().Connect( this, &EventHandler::OnSelectionDataSend );
118 windowBase->SelectionDataReceivedSignal().Connect( this, &EventHandler::OnSelectionDataReceived );
119 windowBase->StyleChangedSignal().Connect( this, &EventHandler::OnStyleChanged );
120 windowBase->AccessibilitySignal().Connect( this, &EventHandler::OnAccessibilityNotification );
124 EventHandler::~EventHandler()
128 void EventHandler::SendEvent( StyleChange::Type styleChange )
130 DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
131 GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
134 void EventHandler::SendEvent( const DamageArea& area )
136 mDamageObserver.OnDamaged( area );
139 void EventHandler::Pause()
144 void EventHandler::Resume()
149 void EventHandler::OnTouchEvent( Integration::Point& point, uint32_t timeStamp )
151 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
153 (*iter)->OnTouchPoint( point, timeStamp );
157 void EventHandler::OnWheelEvent( WheelEvent& wheelEvent )
159 Integration::WheelEvent event( static_cast< Integration::WheelEvent::Type >(wheelEvent.type), wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp );
161 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
163 (*iter)->OnWheelEvent( event );
167 void EventHandler::OnKeyEvent( Integration::KeyEvent& keyEvent )
169 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
171 (*iter)->OnKeyEvent( keyEvent );
175 void EventHandler::OnFocusChanged( bool focusIn )
177 // If the window gains focus and we hid the keyboard then show it again.
180 Dali::Clipboard clipboard = Clipboard::Get();
183 clipboard.HideClipboard();
188 // Hiding clipboard event will be ignored once because window focus out event is always received on showing clipboard
189 Dali::Clipboard clipboard = Clipboard::Get();
192 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
193 clipBoardImpl.HideClipboard(true);
198 void EventHandler::OnRotation( const RotationEvent& event )
200 for ( ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter )
202 (*iter)->OnRotation( event );
206 void EventHandler::OnWindowDamaged( const DamageArea& area )
211 void EventHandler::OnSelectionDataSend( void* event )
213 Dali::Clipboard clipboard = Clipboard::Get();
216 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
217 clipBoardImpl.ExcuteBuffered( true, event );
221 void EventHandler::OnSelectionDataReceived( void* event )
223 // We have got the selected content, inform the clipboard event listener (if we have one).
224 Dali::Clipboard clipboard = Clipboard::Get();
225 char* selectionData = NULL;
228 Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
229 selectionData = clipBoardImpl.ExcuteBuffered( false, event );
232 if( selectionData && mClipboardEventNotifier )
234 ClipboardEventNotifier& clipboardEventNotifier( ClipboardEventNotifier::GetImplementation( mClipboardEventNotifier ) );
235 std::string content( selectionData, strlen( selectionData ) );
237 clipboardEventNotifier.SetContent( content );
238 clipboardEventNotifier.EmitContentSelectedSignal();
240 DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d): %s\n" , strlen(selectionData), selectionData );
244 void EventHandler::OnStyleChanged( StyleChange::Type styleChange )
246 SendEvent( styleChange );
249 void EventHandler::OnAccessibilityNotification( const WindowBase::AccessibilityInfo& info )
251 #ifdef DALI_ELDBUS_AVAILABLE
257 if( !mAccessibilityAdaptor )
259 DALI_LOG_ERROR( "Invalid accessibility adaptor\n" );
263 AccessibilityAdaptor* accessibilityAdaptor( &AccessibilityAdaptor::GetImplementation( mAccessibilityAdaptor ) );
264 if( !accessibilityAdaptor )
266 DALI_LOG_ERROR( "Cannot access accessibility adaptor\n" );
270 // Create a touch point object.
271 TouchPoint::State touchPointState( TouchPoint::Down );
272 if( info.state == 0 )
274 touchPointState = TouchPoint::Down; // Mouse down.
276 else if( info.state == 1 )
278 touchPointState = TouchPoint::Motion; // Mouse move.
280 else if( info.state == 2 )
282 touchPointState = TouchPoint::Up; // Mouse up.
286 touchPointState = TouchPoint::Interrupted; // Error.
289 // Send touch event to accessibility adaptor.
290 TouchPoint point( 0, touchPointState, static_cast< float >( info.startX ), static_cast< float >( info.startY ) );
292 // Perform actions based on received gestures.
293 // Note: This is seperated from the reading so we can have other input readers without changing the below code.
294 switch( info.gestureValue )
296 case 0: // OneFingerHover
299 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
302 case 1: // TwoFingersHover
304 // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
305 accessibilityAdaptor->HandleActionScrollEvent( point, GetCurrentMilliSeconds() );
308 case 2: // ThreeFingersHover
310 // Read from top item on screen continuously.
311 accessibilityAdaptor->HandleActionReadFromTopEvent();
314 case 3: // OneFingerFlickLeft
316 // Move to previous item.
317 accessibilityAdaptor->HandleActionReadPreviousEvent();
320 case 4: // OneFingerFlickRight
322 // Move to next item.
323 accessibilityAdaptor->HandleActionReadNextEvent();
326 case 5: // OneFingerFlickUp
328 // Move to previous item.
329 accessibilityAdaptor->HandleActionPreviousEvent();
332 case 6: // OneFingerFlickDown
334 // Move to next item.
335 accessibilityAdaptor->HandleActionNextEvent();
338 case 7: // TwoFingersFlickUp
340 // Scroll up the list.
341 accessibilityAdaptor->HandleActionScrollUpEvent();
344 case 8: // TwoFingersFlickDown
346 // Scroll down the list.
347 accessibilityAdaptor->HandleActionScrollDownEvent();
350 case 9: // TwoFingersFlickLeft
352 // Scroll left to the previous page
353 accessibilityAdaptor->HandleActionPageLeftEvent();
356 case 10: // TwoFingersFlickRight
358 // Scroll right to the next page
359 accessibilityAdaptor->HandleActionPageRightEvent();
362 case 11: // ThreeFingersFlickLeft
367 case 12: // ThreeFingersFlickRight
372 case 13: // ThreeFingersFlickUp
377 case 14: // ThreeFingersFlickDown
382 case 15: // OneFingerSingleTap
385 accessibilityAdaptor->HandleActionReadEvent( static_cast< unsigned int >( info.startX ), static_cast< unsigned int >( info.startY ), true /* allow read again */ );
388 case 16: // OneFingerDoubleTap
390 // Activate selected item / active edit mode.
391 accessibilityAdaptor->HandleActionActivateEvent();
394 case 17: // OneFingerTripleTap
397 accessibilityAdaptor->HandleActionZoomEvent();
400 case 18: // TwoFingersSingleTap
402 // Pause/Resume current speech
403 accessibilityAdaptor->HandleActionReadPauseResumeEvent();
406 case 19: // TwoFingersDoubleTap
408 // Start/Stop current action
409 accessibilityAdaptor->HandleActionStartStopEvent();
412 case 20: // TwoFingersTripleTap
414 // Read information from indicator
418 case 21: // ThreeFingersSingleTap
420 // Read from top item on screen continuously.
421 accessibilityAdaptor->HandleActionReadFromTopEvent();
424 case 22: // ThreeFingersDoubleTap
426 // Read from next item continuously.
427 accessibilityAdaptor->HandleActionReadFromNextEvent();
430 case 23: // ThreeFingersTripleTap
435 case 24: // OneFingerFlickLeftReturn
437 // Scroll up to the previous page
438 accessibilityAdaptor->HandleActionPageUpEvent();
441 case 25: // OneFingerFlickRightReturn
443 // Scroll down to the next page
444 accessibilityAdaptor->HandleActionPageDownEvent();
447 case 26: // OneFingerFlickUpReturn
449 // Move to the first item on screen
450 accessibilityAdaptor->HandleActionMoveToFirstEvent();
453 case 27: // OneFingerFlickDownReturn
455 // Move to the last item on screen
456 accessibilityAdaptor->HandleActionMoveToLastEvent();
459 case 28: // TwoFingersFlickLeftReturn
464 case 29: // TwoFingersFlickRightReturn
469 case 30: // TwoFingersFlickUpReturn
474 case 31: // TwoFingersFlickDownReturn
479 case 32: // ThreeFingersFlickLeftReturn
484 case 33: // ThreeFingersFlickRightReturn
489 case 34: // ThreeFingersFlickUpReturn
494 case 35: // ThreeFingersFlickDownReturn
503 void EventHandler::AddObserver( Observer& observer )
505 ObserverContainer::iterator match ( find(mObservers.begin(), mObservers.end(), &observer) );
507 if ( match == mObservers.end() )
509 mObservers.push_back( &observer );
513 void EventHandler::RemoveObserver( Observer& observer )
515 ObserverContainer::iterator match ( find(mObservers.begin(), mObservers.end(), &observer) );
517 if ( match != mObservers.end() )
519 mObservers.erase( match );
523 } // namespace Adaptor
525 } // namespace Internal