DragAndDrop : support multiwindow dnd on single process
[platform/core/uifw/dali-adaptor.git] / dali / internal / drag-and-drop / tizen-wayland / drag-and-drop-impl-ecore-wl2.cpp
index 8f8d979..476715b 100644 (file)
@@ -51,6 +51,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 +98,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,39 +144,47 @@ Dali::DragAndDrop GetDragAndDrop()
 
 DragAndDropEcoreWl::DragAndDropEcoreWl()
 {
-  mSendHandler    = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_SEND, EcoreEventDataSend, this);
-  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);
+  // 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);
 
   // Set Drag Source Data
   mMimeType = data.GetMimeType();
-  mData = data.GetData();
+  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,18 +192,23 @@ 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();
   mimeTypes[1] = NULL;
 
-
   // Set mime type
   ecore_wl2_dnd_drag_types_set(input, (const char**)mimeTypes);
 
   // Start wayland drag and drop
   mSerial = ecore_wl2_dnd_drag_start(input, parentWindow, dragWindow);
 
+  // Call Start Event
+  CallSourceEvent(Dali::DragAndDrop::SourceEventType::START);
+
   return true;
 }
 
@@ -170,10 +223,18 @@ bool DragAndDropEcoreWl::AddListener(Dali::Actor target, Dali::DragAndDrop::Drag
     }
   }
 
+  auto parent = Dali::DevelWindow::Get(target);
+  Ecore_Wl2_Window*  parentWindow = AnyCast<Ecore_Wl2_Window*>(parent.GetNativeHandle());
+  if(parentWindow == nullptr)
+  {
+    return false;
+  }
+
   DropTarget targetData;
   targetData.target   = target;
   targetData.callback = callback;
   targetData.inside   = false;
+  targetData.parentWindowId = ecore_wl2_window_id_get(parentWindow);
 
   mDropTargets.push_back(targetData);
 
@@ -195,6 +256,23 @@ bool DragAndDropEcoreWl::RemoveListener(Dali::Actor target)
   return true;
 }
 
+void DragAndDropEcoreWl::CallSourceEvent(Dali::DragAndDrop::SourceEventType type)
+{
+  if(mSourceCallback)
+  {
+    mSourceCallback(type);
+  }
+}
+
+void DragAndDropEcoreWl::ResetDropTargets()
+{
+  for(std::size_t i = 0; i < mDropTargets.size(); i++)
+  {
+     mDropTargets[i].inside = false;
+  }
+}
+
+
 void DragAndDropEcoreWl::SendData(void* event)
 {
   Ecore_Wl2_Event_Data_Source_Send* ev = reinterpret_cast<Ecore_Wl2_Event_Data_Source_Send*>(event);
@@ -209,7 +287,7 @@ void DragAndDropEcoreWl::SendData(void* event)
      (mMimeType.find("markup") != std::string::npos) ||
      (mMimeType.find("image") != std::string::npos))
   {
-     bufferSize += 1;
+    bufferSize += 1;
   }
 
   char* buffer = new char[bufferSize];
@@ -221,7 +299,12 @@ void DragAndDropEcoreWl::SendData(void* event)
   memcpy(buffer, mData.c_str(), dataLength);
   buffer[dataLength] = '\0';
 
-  write(ev->fd, buffer, bufferSize);
+  auto ret = write(ev->fd, buffer, bufferSize);
+  if(DALI_UNLIKELY(ret != bufferSize))
+  {
+    DALI_LOG_ERROR("write(ev->fd) return %d! Pleacse check it\n", static_cast<int>(ret));
+  }
+
   close(ev->fd);
 
   if(mDragWindow)
@@ -229,7 +312,7 @@ void DragAndDropEcoreWl::SendData(void* event)
     mDragWindow.Hide();
   }
 
-  delete [] buffer;
+  delete[] buffer;
 }
 
 void DragAndDropEcoreWl::ReceiveData(void* event)
@@ -241,6 +324,7 @@ void DragAndDropEcoreWl::ReceiveData(void* event)
     Dali::DragAndDrop::DragEvent dragEvent(Dali::DragAndDrop::DragType::DROP, mPosition, ev->mimetype, ev->data);
     mDropTargets[mTargetIndex].callback(dragEvent);
     mDropTargets[mTargetIndex].inside = false;
+    ecore_wl2_offer_finish(ev->offer);
   }
   mTargetIndex = -1;
 }
@@ -252,8 +336,13 @@ bool DragAndDropEcoreWl::CalculateDragEvent(void* event)
   Dali::DragAndDrop::DragEvent dragEvent;
   Dali::Vector2                curPosition(ev->x, ev->y);
 
-  for(int i = 0; i < mDropTargets.size(); i++)
+  for(std::size_t i = 0; i < mDropTargets.size(); i++)
   {
+    if(ev->win != mDropTargets[i].parentWindowId)
+    {
+      continue;
+    }
+
     Vector2 position      = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::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);
@@ -266,6 +355,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)
     {
@@ -274,6 +365,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)
     {
@@ -294,8 +387,13 @@ bool DragAndDropEcoreWl::CalculateViewRegion(void* event)
   // Check the target object region
   mTargetIndex = -1;
 
-  for(int i = 0; i < mDropTargets.size(); i++)
+  for(std::size_t i = 0; i < mDropTargets.size(); i++)
   {
+    if(ev->win != mDropTargets[i].parentWindowId)
+    {
+      continue;
+    }
+
     Vector2 position = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::POSITION);
     Vector2 size     = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SIZE);
     // If the drop position is in the target object region, request drop data to the source object
@@ -308,7 +406,6 @@ 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);