Fix typo
[platform/core/uifw/widget-viewer-dali.git] / internal / widget_view / widget_view_impl.cpp
index 166da2d..f1bfea4 100644 (file)
@@ -23,6 +23,7 @@
 // EXTERNAL INCLUDES
 #include <dali/public-api/common/stage.h>
 #include <dali/public-api/events/touch-data.h>
+#include <dali/public-api/events/key-event.h>
 #include <dali/public-api/events/wheel-event.h>
 #include <dali/public-api/images/native-image.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali-toolkit/public-api/visuals/visual-properties.h>
 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
 #include <dali/integration-api/debug.h>
+#include <dali/devel-api/events/touch-event-devel.h>
+#include <dali/devel-api/events/key-event-devel.h>
 #include <string.h>
+#include <Ecore_Wayland.h>
 #include <Ecore_Input.h>
 #include <widget_service.h>
 #include <widget_instance.h>
@@ -39,6 +43,7 @@
 #include <tbm_surface_internal.h>
 #include <unistd.h>
 #include <libintl.h>
+#include <xkbcommon/xkbcommon.h>
 
 namespace Dali
 {
@@ -113,6 +118,74 @@ const char* const TEXT_POINT_SIZE( "textPointSize" );
 const char* const TEXT_COLOR( "textColor" );
 const char* const TEXT_VISIBLE( "textVisible" ); //ToDo: it should be removed after retry text property is public one
 
+// ToDo: Now dali provides only dali key codes.
+// This funtion will be used to get platform specific key codes from key name
+struct KeyCodeMap
+{
+  xkb_keysym_t keySym;
+  xkb_keycode_t keyCode;
+  bool isKeyCode;
+};
+
+static void FindKeyCode( struct xkb_keymap* keyMap, xkb_keycode_t key, void* data )
+{
+  KeyCodeMap* foundKeyCode = static_cast< KeyCodeMap* >( data );
+  if( foundKeyCode->isKeyCode )
+  {
+    return;
+  }
+
+  xkb_keysym_t keySym = foundKeyCode->keySym;
+  int nsyms = 0;
+  const xkb_keysym_t* symsOut = NULL;
+
+  nsyms = xkb_keymap_key_get_syms_by_level( keyMap, key, 0, 0, &symsOut );
+
+  if( nsyms && symsOut )
+  {
+    if( *symsOut == keySym )
+    {
+      foundKeyCode->keyCode = key;
+      foundKeyCode->isKeyCode = true;
+    }
+  }
+}
+
+static bool GetKeyCode( std::string keyName, int32_t& keyCode )
+{
+  xkb_keymap* keyMap = NULL;
+  Ecore_Wl_Input* ecoreWlInput = NULL;
+  xkb_keysym_t sym = XKB_KEY_NoSymbol;
+  KeyCodeMap foundKeyCode;
+
+  ecoreWlInput = ecore_wl_input_get();
+  if( !ecoreWlInput )
+  {
+    DALI_LOG_ERROR( "Failed to get Ecore_Wl_Input in WidgetView\n" );
+    return false;
+  }
+
+  keyMap = ecore_wl_input_keymap_get( ecoreWlInput );
+  if( !keyMap )
+  {
+    DALI_LOG_ERROR( "Failed to get keymap in WidgetView\n" );
+    return false;
+  }
+
+  sym = xkb_keysym_from_name( keyName.c_str(), XKB_KEYSYM_NO_FLAGS );
+  if( sym == XKB_KEY_NoSymbol )
+  {
+    DALI_LOG_ERROR( "Failed to get keysym in WidgetView\n" );
+    return false;
+  }
+
+  foundKeyCode.keySym = sym;
+  foundKeyCode.isKeyCode = false;
+  xkb_keymap_key_for_each( keyMap, FindKeyCode, &foundKeyCode );
+  keyCode = static_cast< int32_t >( foundKeyCode.keyCode );
+  return true;
+}
+
 static void OnBufferUpdated( struct tizen_remote_surface *surface, uint32_t type, struct wl_buffer *buffer,
                               int32_t img_file_fd, uint32_t img_file_size, uint32_t time, struct wl_array *keys,
                               const char *appid, const char *instance_id, const int pid, void *data)
@@ -233,7 +306,7 @@ bool WidgetView::PauseWidget()
   int ret = widget_instance_pause( mInstanceId.c_str() );
   if( ret < 0 )
   {
-    DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::PauseWidget: Fail to pause widget(%s, %s) [%d]\n", mWidgetId.c_str(), mInstanceId.c_str(), ret );
+    DALI_LOG_ERROR( "WidgetView::PauseWidget: Fail to pause widget(%s, %s) [%d]\n", mWidgetId.c_str(), mInstanceId.c_str(), ret );
     return false;
   }
 
@@ -247,7 +320,7 @@ bool WidgetView::ResumeWidget()
   int ret = widget_instance_resume( mInstanceId.c_str() );
   if( ret < 0 )
   {
-    DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ResumeWidget: Fail to resume widget(%s, %s) [%d]\n", mWidgetId.c_str(), mInstanceId.c_str(), ret );
+    DALI_LOG_ERROR( "WidgetView::ResumeWidget: Fail to resume widget(%s, %s) [%d]\n", mWidgetId.c_str(), mInstanceId.c_str(), ret );
     return false;
   }
 
@@ -348,7 +421,7 @@ void WidgetView::SetLoadingTextVisible( bool visible )
 
     if( visible )
     {
-      mLoadingText.SetProperty( Toolkit::TextLabel::Property::TEXT, 
+      mLoadingText.SetProperty( Toolkit::TextLabel::Property::TEXT,
                                 ( mLoadingTextString.empty() )? GET_LOCALE_TEXT( "IDS_ST_POP_LOADING_ING" ) : mLoadingTextString );
     }
     mLoadingText.SetVisible( visible );
@@ -408,7 +481,7 @@ void WidgetView::ActivateFaultedWidget()
     ops.updated_cb = OnBufferUpdated;
     ops.removed_cb = OnSurfaceRemoved;
     ops.added_cb = OnBufferAdded;
-    mWatcherHandle = screen_connector_toolkit_add(&ops, (char *)mInstanceId.c_str(), SCREEN_CONNECTOR_SCREEEN_TYPE_WIDGET, this);
+    mWatcherHandle = screen_connector_toolkit_add(&ops, (char *)mInstanceId.c_str(), SCREEN_CONNECTOR_SCREEN_TYPE_WIDGET, this);
 
     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
   }
@@ -891,7 +964,7 @@ void WidgetView::OnInitialize()
   ops.updated_cb = OnBufferUpdated;
   ops.removed_cb = OnSurfaceRemoved;
   ops.added_cb = OnBufferAdded;
-  mWatcherHandle = screen_connector_toolkit_add(&ops, (char *)instanceId, SCREEN_CONNECTOR_SCREEEN_TYPE_WIDGET, this);
+  mWatcherHandle = screen_connector_toolkit_add(&ops, (char *)instanceId, SCREEN_CONNECTOR_SCREEN_TYPE_WIDGET, this);
   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
 
 }
@@ -921,8 +994,14 @@ bool WidgetView::IsWidgetImageView()
 
 void WidgetView::UpdateImageSource( tbm_surface_h source )
 {
-  mImageSource->SetSource( source );
-  Dali::Stage::GetCurrent().KeepRendering( 0.0f );
+  mImageSource = Dali::NativeImageSource::New( source );
+  Dali::NativeImage image = Dali::NativeImage::New( *mImageSource );
+
+  if( mWidgetImageView )
+  {
+    mWidgetImageView.SetImage( image );
+    mWidgetImageView.SetProperty( Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true );
+  }
 }
 
 void WidgetView::ConnectSignal( tizen_remote_surface* surface )
@@ -934,78 +1013,31 @@ void WidgetView::ConnectSignal( tizen_remote_surface* surface )
     Self().TouchSignal().Connect( this, &WidgetView::OnTouch );
     Self().WheelEventSignal().Connect( this, &WidgetView::OnWheelEvent );
   }
-
 }
 
 bool WidgetView::OnTouch( Dali::Actor actor, const Dali::TouchData& event )
 {
-  tizen_remote_surface_event_type type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_NONE;
-
   if( event.GetPointCount() == 0 )
   {
     return false;
   }
 
-  switch( event.GetState( 0 ) )
-  {
-    case Dali::PointState::UP:
-    {
-      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_UP;
-
-      if( mRetryState )
-      {
-        ActivateFaultedWidget();
-        return false;
-      }
-
-      break;
-    }
-    case Dali::PointState::DOWN:
-    {
-      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_DOWN;
-      break;
-    }
-    case Dali::PointState::MOTION:
-    {
-      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_MOVE;
-      break;
-    }
-    default:
-    {
-      return false;
-    }
-  }
-
   if( mRemoteSurface == NULL )
   {
     return false;
   }
 
-  int button = 1;
-
-  if( type == TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_MOVE )
+  DevelDevice::Class::Type deviceType = DevelTouchData::GetDeviceClass( event, 0 );
+  if( deviceType == DevelDevice::Class::MOUSE )
   {
-    button = 0 ;
+    return MouseEvent( event );
+  }
+  else if( deviceType == DevelDevice::Class::TOUCH )
+  {
+    return TouchEvent( event );
   }
 
-  Vector2 localPos = event.GetLocalPosition( 0 );
-
-  tizen_remote_surface_transfer_mouse_event( mRemoteSurface,
-                                             type,
-                                             0,
-                                             button,
-                                             (int)localPos.x,
-                                             (int)localPos.y,
-                                             wl_fixed_from_double( event.GetEllipseRadius( 0 ).x ),
-                                             wl_fixed_from_double( event.GetEllipseRadius( 0 ).y ),
-                                             wl_fixed_from_double( event.GetPressure( 0 ) ),
-                                             wl_fixed_from_double( event.GetAngle( 0 ).degree ),
-                                             TIZEN_INPUT_DEVICE_CLAS_TOUCHSCREEN,
-                                             TIZEN_INPUT_DEVICE_SUBCLAS_NONE,
-                                             "",
-                                             event.GetTime()
-                                           );
-  return true;
+  return false;
 }
 
 bool WidgetView::OnWheelEvent( Dali::Actor actor, const Dali::WheelEvent& event )
@@ -1023,7 +1055,56 @@ bool WidgetView::OnWheelEvent( Dali::Actor actor, const Dali::WheelEvent& event
                                              "",
                                              event.timeStamp
                                            );
-  return true;
+  return false;
+}
+
+bool WidgetView::OnKeyEvent( const Dali::KeyEvent& event )
+{
+  if( mRemoteSurface == NULL )
+  {
+    return false;
+  }
+
+  tizen_remote_surface_event_type type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_NONE;
+
+  switch( event.state )
+  {
+    case Dali::KeyEvent::Down:
+    {
+      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_KEY_DOWN;
+      break;
+    }
+    case Dali::KeyEvent::Up:
+    {
+      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_KEY_UP;
+      break;
+    }
+    case Dali::KeyEvent::Last:
+    {
+      break;
+    }
+    default:
+    {
+      return false;
+    }
+  }
+
+  int32_t keyCode;
+  if( GetKeyCode( event.keyPressedName, keyCode ) == false )
+  {
+    return false;
+  }
+
+  tizen_remote_surface_transfer_key_event( mRemoteSurface,
+                                           type,
+                                           keyCode,
+                                           Dali::DevelKeyEvent::GetDeviceClass( event ),
+                                           Dali::DevelKeyEvent::GetDeviceSubclass( event ),
+                                           "",
+                                           static_cast< uint32_t >( event.time )
+                                         );
+
+  return false;
 }
 
 Vector3 WidgetView::GetNaturalSize()
@@ -1186,7 +1267,6 @@ void WidgetView::SetRetryTextPropertyMap( Property::Map map )
 
     Property::Value* textColor = map.Find( TEXT_COLOR );
     Vector4 color;
-
     if( textColor && textColor->Get( color ) )
     {
       mRetryText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, color );
@@ -1218,6 +1298,167 @@ float WidgetView::TextPixelToPointSize( int pixelSize )
   return (pixelSize * 72.0f) / meanDpi;
 }
 
+void WidgetView::MouseIn( const Dali::TouchData& event )
+{
+  Vector2 localPos = event.GetLocalPosition( 0 );
+
+  tizen_remote_surface_transfer_mouse_event( mRemoteSurface,
+                                             TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_IN,
+                                             0,
+                                             0,
+                                             (int)localPos.x,
+                                             (int)localPos.y,
+                                             0,
+                                             0,
+                                             0,
+                                             0,
+                                             TIZEN_INPUT_DEVICE_CLAS_MOUSE,
+                                             DevelTouchData::GetDeviceSubclass( event, 0 ),
+                                             "",
+                                             event.GetTime()
+                                           );
+}
+
+void WidgetView::MouseOut( const Dali::TouchData& event )
+{
+  tizen_remote_surface_transfer_mouse_event( mRemoteSurface,
+                                             TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_OUT,
+                                             0,
+                                             0,
+                                             0,
+                                             0,
+                                             0,
+                                             0,
+                                             0,
+                                             0,
+                                             TIZEN_INPUT_DEVICE_CLAS_MOUSE,
+                                             DevelTouchData::GetDeviceSubclass( event, 0 ),
+                                             "",
+                                             event.GetTime()
+                                           );
+}
+
+bool WidgetView::MouseEvent( const Dali::TouchData& event )
+{
+  tizen_remote_surface_event_type type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_NONE;
+  int button = 1;
+
+  switch( event.GetState( 0 ) )
+  {
+    case Dali::PointState::UP:
+    {
+      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_UP;
+
+      if( mRetryState )
+      {
+        ActivateFaultedWidget();
+        return false;
+      }
+
+      break;
+    }
+    case Dali::PointState::DOWN:
+    {
+      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_DOWN;
+      MouseIn( event );
+
+      break;
+    }
+    case Dali::PointState::MOTION:
+    {
+      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_MOVE;
+      button = 0 ;
+      break;
+    }
+    default:
+    {
+      return false;
+    }
+  }
+
+  Vector2 localPos = event.GetLocalPosition( 0 );
+
+  tizen_remote_surface_transfer_mouse_event( mRemoteSurface,
+                                             type,
+                                             0,
+                                             button,
+                                             (int)localPos.x,
+                                             (int)localPos.y,
+                                             wl_fixed_from_double( event.GetEllipseRadius( 0 ).x ),
+                                             wl_fixed_from_double( event.GetEllipseRadius( 0 ).y ),
+                                             wl_fixed_from_double( event.GetPressure( 0 ) ),
+                                             wl_fixed_from_double( event.GetAngle( 0 ).degree ),
+                                             TIZEN_INPUT_DEVICE_CLAS_MOUSE,
+                                             DevelTouchData::GetDeviceSubclass( event, 0 ),
+                                             "",
+                                             event.GetTime()
+                                           );
+
+  if( event.GetState( 0 ) == Dali::PointState::UP)
+  {
+    MouseOut( event );
+  }
+
+  return true;
+}
+
+bool WidgetView::TouchEvent( const Dali::TouchData& event )
+{
+  tizen_remote_surface_event_type type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_NONE;
+  int button = 1;
+
+  switch( event.GetState( 0 ) )
+  {
+    case Dali::PointState::UP:
+    {
+      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_TOUCH_UP;
+
+      if( mRetryState )
+      {
+        ActivateFaultedWidget();
+        return false;
+      }
+
+      break;
+    }
+    case Dali::PointState::DOWN:
+    {
+      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_TOUCH_DOWN;
+
+      break;
+    }
+    case Dali::PointState::MOTION:
+    {
+      type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_TOUCH_MOVE;
+      button = 0;
+      break;
+    }
+    default:
+    {
+      return false;
+    }
+  }
+
+  Vector2 localPos = event.GetLocalPosition( 0 );
+
+  tizen_remote_surface_transfer_touch_event( mRemoteSurface,
+                                             type,
+                                             0,
+                                             button,
+                                             (int)localPos.x,
+                                             (int)localPos.y,
+                                             wl_fixed_from_double( event.GetEllipseRadius( 0 ).x ),
+                                             wl_fixed_from_double( event.GetEllipseRadius( 0 ).y ),
+                                             wl_fixed_from_double( event.GetPressure( 0 ) ),
+                                             wl_fixed_from_double( event.GetAngle( 0 ).degree ),
+                                             TIZEN_INPUT_DEVICE_CLAS_TOUCHSCREEN,
+                                             DevelTouchData::GetDeviceSubclass( event, 0 ),
+                                             "",
+                                             event.GetTime()
+                                           );
+  return true;
+}
+
 } // namespace Internal
 
 } // namespace WidgetView