DragAndDrop : Add Window AddListener
[platform/core/uifw/dali-adaptor.git] / dali / internal / drag-and-drop / tizen-wayland / drag-and-drop-impl-ecore-wl2.cpp
index dd020c0..49c45ff 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 // EXTERNAL INCLUDES
 #include <dali/devel-api/common/singleton-service.h>
 #include <dali/integration-api/debug.h>
-#include <dali/internal/adaptor/tizen-wayland/dali-ecore-wl2.h>
 #include <unistd.h>
 
+// INTERNAL INCLUDES
+#include <dali/internal/adaptor/tizen-wayland/dali-ecore-wl2.h>
+#include <dali/internal/window-system/common/window-system.h>
+#include <dali/internal/window-system/common/window-impl.h>
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // DragAndDrop
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -34,6 +38,12 @@ namespace Internal
 {
 namespace Adaptor
 {
+namespace
+{
+static constexpr int32_t DEFAULT_POSITION            = -1;
+static constexpr int32_t INVALID_ECORE_WL2_WINDOW_ID = -1;
+} // namespace
+
 static bool IsIntersection(int px, int py, int tx, int ty, int tw, int th)
 {
   if(px > tx && py > ty && px < (tx + tw) && py < (ty + th))
@@ -51,6 +61,29 @@ static Eina_Bool EcoreEventDataSend(void* data, int type, void* event)
   return ECORE_CALLBACK_PASS_ON;
 }
 
+static Eina_Bool EcoreEventDataSourceEnd(void* data, int type, void* event)
+{
+  Ecore_Wl2_Event_Data_Source_End* ev      = reinterpret_cast<Ecore_Wl2_Event_Data_Source_End*>(event);
+  DragAndDropEcoreWl*              dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
+  if(ev->cancelled)
+  {
+    dndImpl->CallSourceEvent(Dali::DragAndDrop::SourceEventType::CANCEL);
+  }
+  else
+  {
+    dndImpl->CallSourceEvent(Dali::DragAndDrop::SourceEventType::ACCEPT);
+  }
+
+  return ECORE_CALLBACK_PASS_ON;
+}
+
+static Eina_Bool EcoreEventDataSourceDrop(void* data, int type, void* event)
+{
+  DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
+  dndImpl->CallSourceEvent(Dali::DragAndDrop::SourceEventType::FINISH);
+  return ECORE_CALLBACK_PASS_ON;
+}
+
 static Eina_Bool EcoreEventOfferDataReady(void* data, int type, void* event)
 {
   DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
@@ -75,6 +108,23 @@ static Eina_Bool EcoreEventDataDrop(void* data, int type, void* event)
   return ECORE_CALLBACK_PASS_ON;
 }
 
+static Eina_Bool EcoreEventDataEnter(void* data, int type, void* event)
+{
+  Ecore_Wl2_Event_Dnd_Enter* ev = reinterpret_cast<Ecore_Wl2_Event_Dnd_Enter*>(event);
+
+  // Set default offer is reject
+  ecore_wl2_offer_accept(ev->offer, NULL);
+  return ECORE_CALLBACK_PASS_ON;
+}
+
+static Eina_Bool EcoreEventDataLeave(void* data, int type, void* event)
+{
+  DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
+  dndImpl->ResetDropTargets();
+
+  return ECORE_CALLBACK_PASS_ON;
+}
+
 Dali::DragAndDrop GetDragAndDrop()
 {
   Dali::DragAndDrop dnd;
@@ -104,21 +154,34 @@ Dali::DragAndDrop GetDragAndDrop()
 
 DragAndDropEcoreWl::DragAndDropEcoreWl()
 {
-  mSendHandler    = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_SEND, EcoreEventDataSend, this);
+  // Source Events
+  mSendHandler       = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_SEND, EcoreEventDataSend, this);
+  mSourceEndHandler  = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_END, EcoreEventDataSourceEnd, this);
+  mSourceDropHandler = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_DROP, EcoreEventDataSourceDrop, this);
+
+  // Target Events
   mReceiveHandler = ecore_event_handler_add(ECORE_WL2_EVENT_OFFER_DATA_READY, EcoreEventOfferDataReady, this);
   mMotionHandler  = ecore_event_handler_add(ECORE_WL2_EVENT_DND_MOTION, EcoreEventDataMotion, this);
   mDropHandler    = ecore_event_handler_add(ECORE_WL2_EVENT_DND_DROP, EcoreEventDataDrop, this);
+  mEnterHandler   = ecore_event_handler_add(ECORE_WL2_EVENT_DND_ENTER, EcoreEventDataEnter, this);
+  mLeaveHandler   = ecore_event_handler_add(ECORE_WL2_EVENT_DND_LEAVE, EcoreEventDataLeave, this);
 }
 
 DragAndDropEcoreWl::~DragAndDropEcoreWl()
 {
+  // Source Events
   ecore_event_handler_del(mSendHandler);
+  ecore_event_handler_del(mSourceEndHandler);
+  ecore_event_handler_del(mSourceDropHandler);
+
+  // Target Events
   ecore_event_handler_del(mReceiveHandler);
   ecore_event_handler_del(mMotionHandler);
   ecore_event_handler_del(mDropHandler);
+  ecore_event_handler_del(mEnterHandler);
 }
 
-bool DragAndDropEcoreWl::StartDragAndDrop(Dali::Actor source, Dali::Actor shadow, const Dali::DragAndDrop::DragData& data)
+bool DragAndDropEcoreWl::StartDragAndDrop(Dali::Actor source, Dali::Window shadowWindow, const Dali::DragAndDrop::DragData& data, Dali::DragAndDrop::SourceFunction callback)
 {
   // Get Parent Window
   auto parent = Dali::DevelWindow::Get(source);
@@ -127,16 +190,11 @@ bool DragAndDropEcoreWl::StartDragAndDrop(Dali::Actor source, Dali::Actor shadow
   mMimeType = data.GetMimeType();
   mData     = data.GetData();
 
-  // Apply Shadow Property
-  shadow.SetProperty(Dali::Actor::Property::SIZE, Vector2(150, 150));
-  shadow.SetProperty(Dali::Actor::Property::OPACITY, 0.9f);
+  // Set Source Event
+  mSourceCallback = callback;
 
-  // Create Drag Window
-  mDragWindow = Dali::Window::New(Dali::PositionSize(0, 0, 150, 150), "DragWindow", "class", true);
-  mDragWindow.SetTransparency(true);
-  mDragWindow.SetSize(Dali::Window::WindowSize(150, 150));
-  mDragWindow.SetBackgroundColor(Color::TRANSPARENT);
-  mDragWindow.Add(shadow);
+  // Set Drag Window
+  mDragWindow = shadowWindow;
 
   // Start Drag and Drop
   Ecore_Wl2_Window*  parentWindow = AnyCast<Ecore_Wl2_Window*>(parent.GetNativeHandle());
@@ -144,9 +202,6 @@ bool DragAndDropEcoreWl::StartDragAndDrop(Dali::Actor source, Dali::Actor shadow
   Ecore_Wl2_Display* display      = ecore_wl2_connected_display_get(NULL);
   Ecore_Wl2_Input*   input        = ecore_wl2_input_default_input_get(display);
 
-  // Disable Default Cursor
-  ecore_wl2_input_pointer_set(input, NULL, 0, 0);
-
   // Set mime type for drag and drop
   const char* mimeTypes[2];
   mimeTypes[0] = mMimeType.c_str();
@@ -158,6 +213,9 @@ bool DragAndDropEcoreWl::StartDragAndDrop(Dali::Actor source, Dali::Actor shadow
   // Start wayland drag and drop
   mSerial = ecore_wl2_dnd_drag_start(input, parentWindow, dragWindow);
 
+  // Call Start Event
+  CallSourceEvent(Dali::DragAndDrop::SourceEventType::START);
+
   return true;
 }
 
@@ -172,12 +230,64 @@ bool DragAndDropEcoreWl::AddListener(Dali::Actor target, Dali::DragAndDrop::Drag
     }
   }
 
+  auto window = Dali::DevelWindow::Get(target);
+
+  int parentWindowId = INVALID_ECORE_WL2_WINDOW_ID;
+
+  if(!window)
+  {
+    // Target is stil not scene-on
+    // Add dummy target data, and wait until target is on scene.
+    target.OnSceneSignal().Connect(this, &DragAndDropEcoreWl::DropTargetSceneOn);
+  }
+  else
+  {
+    Ecore_Wl2_Window* parentWindow = AnyCast<Ecore_Wl2_Window*>(window.GetNativeHandle());
+    if(parentWindow == nullptr)
+    {
+      return false;
+    }
+    parentWindowId = ecore_wl2_window_id_get(parentWindow);
+  }
+
   DropTarget targetData;
+  targetData.target         = target;
+  targetData.callback       = callback;
+  targetData.inside         = false;
+  targetData.parentWindowId = parentWindowId;
+
+  mDropTargets.push_back(targetData);
+
+  return true;
+}
+
+bool DragAndDropEcoreWl::AddListener(Dali::Window target, Dali::DragAndDrop::DragAndDropFunction callback)
+{
+  std::vector<DropWindowTarget>::iterator itr;
+  for(itr = mDropWindowTargets.begin(); itr < mDropWindowTargets.end(); itr++)
+  {
+    if((*itr).target == target)
+    {
+      return false;
+    }
+  }
+
+  int windowId = INVALID_ECORE_WL2_WINDOW_ID;
+
+  Ecore_Wl2_Window* window = AnyCast<Ecore_Wl2_Window*>(target.GetNativeHandle());
+  if(window == nullptr)
+  {
+    return false;
+  }
+  windowId = ecore_wl2_window_id_get(window);
+
+  DropWindowTarget targetData;
   targetData.target   = target;
   targetData.callback = callback;
   targetData.inside   = false;
+  targetData.windowId = windowId;
 
-  mDropTargets.push_back(targetData);
+  mDropWindowTargets.push_back(targetData);
 
   return true;
 }
@@ -197,6 +307,62 @@ bool DragAndDropEcoreWl::RemoveListener(Dali::Actor target)
   return true;
 }
 
+bool DragAndDropEcoreWl::RemoveListener(Dali::Window target)
+{
+  std::vector<DropWindowTarget>::iterator itr;
+  for(itr = mDropWindowTargets.begin(); itr < mDropWindowTargets.end(); itr++)
+  {
+    if((*itr).target == target)
+    {
+      mDropWindowTargets.erase(itr);
+      break;
+    }
+  }
+
+  return true;
+}
+
+void DragAndDropEcoreWl::CallSourceEvent(Dali::DragAndDrop::SourceEventType type)
+{
+  if(mSourceCallback)
+  {
+    mSourceCallback(type);
+    if(type != Dali::DragAndDrop::SourceEventType::START)
+    {
+       mDragWindow.Reset();
+    }
+  }
+}
+
+void DragAndDropEcoreWl::ResetDropTargets()
+{
+  for(std::size_t i = 0; i < mDropTargets.size(); i++)
+  {
+    if(mDropTargets[i].inside)
+    {
+      Dali::DragAndDrop::DragEvent dragEvent;
+      dragEvent.SetAction(Dali::DragAndDrop::DragType::LEAVE);
+      Dali::Vector2 position(DEFAULT_POSITION, DEFAULT_POSITION);
+      dragEvent.SetPosition(position);
+      mDropTargets[i].callback(dragEvent);
+    }
+    mDropTargets[i].inside = false;
+  }
+
+  for(std::size_t i = 0; i < mDropWindowTargets.size(); i++)
+  {
+    if(mDropWindowTargets[i].inside)
+    {
+      Dali::DragAndDrop::DragEvent dragEvent;
+      dragEvent.SetAction(Dali::DragAndDrop::DragType::LEAVE);
+      Dali::Vector2 position(DEFAULT_POSITION, DEFAULT_POSITION);
+      dragEvent.SetPosition(position);
+      mDropWindowTargets[i].callback(dragEvent);
+    }
+    mDropWindowTargets[i].inside = false;
+  }
+}
+
 void DragAndDropEcoreWl::SendData(void* event)
 {
   Ecore_Wl2_Event_Data_Source_Send* ev = reinterpret_cast<Ecore_Wl2_Event_Data_Source_Send*>(event);
@@ -250,6 +416,51 @@ void DragAndDropEcoreWl::ReceiveData(void* event)
     mDropTargets[mTargetIndex].inside = false;
   }
   mTargetIndex = -1;
+
+  if(mWindowTargetIndex != -1)
+  {
+    Dali::DragAndDrop::DragEvent dragEvent(Dali::DragAndDrop::DragType::DROP, mWindowPosition, ev->mimetype, ev->data);
+    mDropWindowTargets[mWindowTargetIndex].callback(dragEvent);
+    mDropWindowTargets[mWindowTargetIndex].inside = false;
+  }
+  mWindowTargetIndex = -1;
+
+}
+
+Vector2 DragAndDropEcoreWl::RecalculatePositionByOrientation(int x, int y, Dali::Window window)
+{
+  int screenWidth, screenHeight;
+  Internal::Adaptor::WindowSystem::GetScreenSize(screenWidth, screenHeight);
+  int angle = DevelWindow::GetPhysicalOrientation(window);
+
+  int newX, newY;
+  Dali::Vector2 newPosition;
+
+  if(angle == 90)
+  {
+    newX = screenHeight - y;
+    newY = x;
+  }
+  else if(angle == 180)
+  {
+    newX = screenWidth - x;
+    newY = screenHeight - y;
+  }
+  else if(angle == 270)
+  {
+    newX = y;
+    newY = screenWidth - x;
+  }
+  else
+  {
+    newX = x;
+    newY = y;
+  }
+
+  newPosition.x = newX;
+  newPosition.y = newY;
+
+  return newPosition;
 }
 
 bool DragAndDropEcoreWl::CalculateDragEvent(void* event)
@@ -261,9 +472,19 @@ bool DragAndDropEcoreWl::CalculateDragEvent(void* event)
 
   for(std::size_t i = 0; i < mDropTargets.size(); i++)
   {
-    Vector2 position      = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::POSITION);
+    if(ev->win != mDropTargets[i].parentWindowId)
+    {
+      continue;
+    }
+
+    Vector2 position      = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SCREEN_POSITION);
     Vector2 size          = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SIZE);
-    bool    currentInside = IsIntersection(ev->x, ev->y, position.x, position.y, size.width, size.height);
+
+    // Recalculate Cursor by Orientation
+    Dali::Window window = Dali::DevelWindow::Get(mDropTargets[i].target);
+    Dali::Vector2 cursor = RecalculatePositionByOrientation(ev->x, ev->y, window);
+
+    bool currentInside = IsIntersection(cursor.x, cursor.y, position.x, position.y, size.width, size.height);
 
     // Calculate Drag Enter, Leave, Move Event
     if(currentInside && !mDropTargets[i].inside)
@@ -273,6 +494,8 @@ bool DragAndDropEcoreWl::CalculateDragEvent(void* event)
       dragEvent.SetAction(Dali::DragAndDrop::DragType::ENTER);
       dragEvent.SetPosition(curPosition);
       mDropTargets[i].callback(dragEvent);
+      // Accept Offer
+      ecore_wl2_offer_mimes_set(ev->offer, ecore_wl2_offer_mimes_get(ev->offer));
     }
     else if(!currentInside && mDropTargets[i].inside)
     {
@@ -281,6 +504,8 @@ bool DragAndDropEcoreWl::CalculateDragEvent(void* event)
       dragEvent.SetAction(Dali::DragAndDrop::DragType::LEAVE);
       dragEvent.SetPosition(curPosition);
       mDropTargets[i].callback(dragEvent);
+      // Reject Offer
+      ecore_wl2_offer_accept(ev->offer, NULL);
     }
     else if(currentInside && mDropTargets[i].inside)
     {
@@ -291,6 +516,50 @@ bool DragAndDropEcoreWl::CalculateDragEvent(void* event)
     }
   }
 
+  for(std::size_t i = 0; i < mDropWindowTargets.size(); i++)
+  {
+    if(ev->win != mDropWindowTargets[i].windowId)
+    {
+      continue;
+    }
+
+    // Recalculate Cursor by Orientation
+    Dali::Window window = mDropWindowTargets[i].target;
+    Dali::Window::WindowPosition position = window.GetPosition();
+    Dali::Window::WindowSize size = window.GetSize();
+
+    bool currentInside = IsIntersection(ev->x + position.GetX(), ev->y + position.GetY(), position.GetX(), position.GetY(), size.GetWidth(), size.GetHeight());
+
+    // Calculate Drag Enter, Leave, Move Event
+    if(currentInside && !mDropWindowTargets[i].inside)
+    {
+      mDropWindowTargets[i].inside = true;
+      // Call Enter Event
+      dragEvent.SetAction(Dali::DragAndDrop::DragType::ENTER);
+      dragEvent.SetPosition(curPosition);
+      mDropWindowTargets[i].callback(dragEvent);
+      // Accept Offer
+      ecore_wl2_offer_mimes_set(ev->offer, ecore_wl2_offer_mimes_get(ev->offer));
+    }
+    else if(!currentInside && mDropWindowTargets[i].inside)
+    {
+      mDropWindowTargets[i].inside = false;
+      // Call Leave Event
+      dragEvent.SetAction(Dali::DragAndDrop::DragType::LEAVE);
+      dragEvent.SetPosition(curPosition);
+      mDropWindowTargets[i].callback(dragEvent);
+      // Reject Offer
+      ecore_wl2_offer_accept(ev->offer, NULL);
+    }
+    else if(currentInside && mDropWindowTargets[i].inside)
+    {
+      // Call Move Event
+      dragEvent.SetAction(Dali::DragAndDrop::DragType::MOVE);
+      dragEvent.SetPosition(curPosition);
+      mDropWindowTargets[i].callback(dragEvent);
+    }
+  }
+
   return true;
 }
 
@@ -300,13 +569,24 @@ bool DragAndDropEcoreWl::CalculateViewRegion(void* event)
 
   // Check the target object region
   mTargetIndex = -1;
+  mWindowTargetIndex = -1;
 
   for(std::size_t i = 0; i < mDropTargets.size(); i++)
   {
-    Vector2 position = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::POSITION);
+    if(ev->win != mDropTargets[i].parentWindowId)
+    {
+      continue;
+    }
+
+    Vector2 position = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SCREEN_POSITION);
     Vector2 size     = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SIZE);
+
+    // Recalculate Cursor by Orientation
+    Dali::Window window = Dali::DevelWindow::Get(mDropTargets[i].target);
+    Dali::Vector2 cursor = RecalculatePositionByOrientation(ev->x, ev->y, window);
+
     // If the drop position is in the target object region, request drop data to the source object
-    if(IsIntersection(ev->x, ev->y, position.x, position.y, size.width, size.height))
+    if(IsIntersection(cursor.x, cursor.y, position.x, position.y, size.width, size.height))
     {
       mTargetIndex        = i;
       mPosition           = position;
@@ -315,7 +595,36 @@ bool DragAndDropEcoreWl::CalculateViewRegion(void* event)
       char* mimetype = (char*)eina_array_data_get(ecore_wl2_offer_mimes_get(ev->offer), 0);
       if(mimetype)
       {
-        ecore_wl2_offer_accept(ev->offer, mimetype);
+        ecore_wl2_offer_receive(ev->offer, mimetype);
+        Ecore_Wl2_Display* display = ecore_wl2_connected_display_get(NULL);
+        Ecore_Wl2_Input*   input   = ecore_wl2_input_default_input_get(display);
+        ecore_wl2_display_flush(ecore_wl2_input_display_get(input));
+      }
+      return true;
+    }
+  }
+
+  for(std::size_t i = 0; i < mDropWindowTargets.size(); i++)
+  {
+    if(ev->win != mDropWindowTargets[i].windowId)
+    {
+      continue;
+    }
+
+    // Recalculate Cursor by Orientation
+    Dali::Window window = mDropWindowTargets[i].target;
+    Dali::Window::WindowPosition position = window.GetPosition();
+    Dali::Window::WindowSize size = window.GetSize();
+
+    // If the drop position is in the target object region, request drop data to the source object
+    if(IsIntersection(ev->x + position.GetX(), ev->y + position.GetY(), position.GetX(), position.GetY(), size.GetWidth(), size.GetHeight()))
+    {
+      mWindowTargetIndex = i;
+      mWindowPosition  = Dali::Vector2(position.GetX(), position.GetY());
+
+      char* mimetype = (char*)eina_array_data_get(ecore_wl2_offer_mimes_get(ev->offer), 0);
+      if(mimetype)
+      {
         ecore_wl2_offer_receive(ev->offer, mimetype);
         Ecore_Wl2_Display* display = ecore_wl2_connected_display_get(NULL);
         Ecore_Wl2_Input*   input   = ecore_wl2_input_default_input_get(display);
@@ -328,6 +637,29 @@ bool DragAndDropEcoreWl::CalculateViewRegion(void* event)
   return false;
 }
 
+void DragAndDropEcoreWl::DropTargetSceneOn(Dali::Actor target)
+{
+  // Disconnect scene on signal
+  target.OnSceneSignal().Disconnect(this, &DragAndDropEcoreWl::DropTargetSceneOn);
+
+  for(auto iter = mDropTargets.begin(), iterEnd = mDropTargets.end(); iter != iterEnd; iter++)
+  {
+    if((*iter).target == target)
+    {
+      auto window = Dali::DevelWindow::Get(target);
+
+      Ecore_Wl2_Window* parentWindow = AnyCast<Ecore_Wl2_Window*>(window.GetNativeHandle());
+      if(parentWindow == nullptr)
+      {
+        return;
+      }
+
+      (*iter).parentWindowId = ecore_wl2_window_id_get(parentWindow);
+      break;
+    }
+  }
+}
+
 } // namespace Adaptor
 
 } // namespace Internal