2 * Copyright (c) 2022 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/drag-and-drop/tizen-wayland/drag-and-drop-impl-ecore-wl2.h>
22 #include <dali/devel-api/common/singleton-service.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/internal/adaptor/tizen-wayland/dali-ecore-wl2.h>
27 ///////////////////////////////////////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////////////////////////////////////
37 static bool IsIntersection(int px, int py, int tx, int ty, int tw, int th)
39 if(px > tx && py > ty && px < (tx + tw) && py < (ty + th))
46 static Eina_Bool EcoreEventDataSend(void* data, int type, void* event)
48 DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
49 dndImpl->SendData(event);
51 return ECORE_CALLBACK_PASS_ON;
54 static Eina_Bool EcoreEventDataSourceEnd(void* data, int type, void* event)
56 Ecore_Wl2_Event_Data_Source_End *ev = reinterpret_cast<Ecore_Wl2_Event_Data_Source_End*>(event);
57 DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
60 dndImpl->CallSourceEvent(Dali::DragAndDrop::SourceEventType::CANCEL);
64 dndImpl->CallSourceEvent(Dali::DragAndDrop::SourceEventType::ACCEPT);
67 return ECORE_CALLBACK_PASS_ON;
70 static Eina_Bool EcoreEventDataSourceDrop(void* data, int type, void* event)
72 DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
73 dndImpl->CallSourceEvent(Dali::DragAndDrop::SourceEventType::FINISH);
74 return ECORE_CALLBACK_PASS_ON;
77 static Eina_Bool EcoreEventOfferDataReady(void* data, int type, void* event)
79 DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
80 dndImpl->ReceiveData(event);
82 return ECORE_CALLBACK_PASS_ON;
85 static Eina_Bool EcoreEventDataMotion(void* data, int type, void* event)
87 DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
88 dndImpl->CalculateDragEvent(event);
90 return ECORE_CALLBACK_PASS_ON;
93 static Eina_Bool EcoreEventDataDrop(void* data, int type, void* event)
95 DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
96 dndImpl->CalculateViewRegion(event);
98 return ECORE_CALLBACK_PASS_ON;
101 static Eina_Bool EcoreEventDataEnter(void* data, int type, void* event)
103 Ecore_Wl2_Event_Dnd_Enter* ev = reinterpret_cast<Ecore_Wl2_Event_Dnd_Enter*>(event);
105 // Set default offer is reject
106 ecore_wl2_offer_accept(ev->offer, NULL);
107 return ECORE_CALLBACK_PASS_ON;
110 static Eina_Bool EcoreEventDataLeave(void* data, int type, void* event)
112 DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
113 dndImpl->ResetDropTargets();
115 return ECORE_CALLBACK_PASS_ON;
118 Dali::DragAndDrop GetDragAndDrop()
120 Dali::DragAndDrop dnd;
122 Dali::SingletonService service(SingletonService::Get());
125 // Check whether the singleton is already created
126 Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::DragAndDrop));
129 // If so, downcast the handle
130 dnd = Dali::DragAndDrop(dynamic_cast<DragAndDrop*>(handle.GetObjectPtr()));
134 // Create a singleon instance
135 DragAndDropEcoreWl* dndImpl = new DragAndDropEcoreWl();
137 dnd = Dali::DragAndDrop(dndImpl);
138 service.Register(typeid(Dali::DragAndDrop), dnd);
145 DragAndDropEcoreWl::DragAndDropEcoreWl()
148 mSendHandler = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_SEND, EcoreEventDataSend, this);
149 mSourceEndHandler = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_END, EcoreEventDataSourceEnd, this);
150 mSourceDropHandler = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_DROP, EcoreEventDataSourceDrop, this);
153 mReceiveHandler = ecore_event_handler_add(ECORE_WL2_EVENT_OFFER_DATA_READY, EcoreEventOfferDataReady, this);
154 mMotionHandler = ecore_event_handler_add(ECORE_WL2_EVENT_DND_MOTION, EcoreEventDataMotion, this);
155 mDropHandler = ecore_event_handler_add(ECORE_WL2_EVENT_DND_DROP, EcoreEventDataDrop, this);
156 mEnterHandler = ecore_event_handler_add(ECORE_WL2_EVENT_DND_ENTER, EcoreEventDataEnter, this);
157 mLeaveHandler = ecore_event_handler_add(ECORE_WL2_EVENT_DND_LEAVE, EcoreEventDataLeave, this);
160 DragAndDropEcoreWl::~DragAndDropEcoreWl()
163 ecore_event_handler_del(mSendHandler);
164 ecore_event_handler_del(mSourceEndHandler);
165 ecore_event_handler_del(mSourceDropHandler);
168 ecore_event_handler_del(mReceiveHandler);
169 ecore_event_handler_del(mMotionHandler);
170 ecore_event_handler_del(mDropHandler);
171 ecore_event_handler_del(mEnterHandler);
174 bool DragAndDropEcoreWl::StartDragAndDrop(Dali::Actor source, Dali::Window shadowWindow, const Dali::DragAndDrop::DragData& data, Dali::DragAndDrop::SourceFunction callback)
177 auto parent = Dali::DevelWindow::Get(source);
179 // Set Drag Source Data
180 mMimeType = data.GetMimeType();
181 mData = data.GetData();
184 mSourceCallback = callback;
187 mDragWindow = shadowWindow;
189 // Start Drag and Drop
190 Ecore_Wl2_Window* parentWindow = AnyCast<Ecore_Wl2_Window*>(parent.GetNativeHandle());
191 Ecore_Wl2_Window* dragWindow = AnyCast<Ecore_Wl2_Window*>(mDragWindow.GetNativeHandle());
192 Ecore_Wl2_Display* display = ecore_wl2_connected_display_get(NULL);
193 Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get(display);
195 // Disable Default Cursor
196 ecore_wl2_input_pointer_set(input, NULL, 0, 0);
198 // Set mime type for drag and drop
199 const char* mimeTypes[2];
200 mimeTypes[0] = mMimeType.c_str();
204 ecore_wl2_dnd_drag_types_set(input, (const char**)mimeTypes);
206 // Start wayland drag and drop
207 mSerial = ecore_wl2_dnd_drag_start(input, parentWindow, dragWindow);
210 CallSourceEvent(Dali::DragAndDrop::SourceEventType::START);
215 bool DragAndDropEcoreWl::AddListener(Dali::Actor target, Dali::DragAndDrop::DragAndDropFunction callback)
217 std::vector<DropTarget>::iterator itr;
218 for(itr = mDropTargets.begin(); itr < mDropTargets.end(); itr++)
220 if((*itr).target == target)
226 DropTarget targetData;
227 targetData.target = target;
228 targetData.callback = callback;
229 targetData.inside = false;
231 mDropTargets.push_back(targetData);
236 bool DragAndDropEcoreWl::RemoveListener(Dali::Actor target)
238 std::vector<DropTarget>::iterator itr;
239 for(itr = mDropTargets.begin(); itr < mDropTargets.end(); itr++)
241 if((*itr).target == target)
243 mDropTargets.erase(itr);
251 void DragAndDropEcoreWl::CallSourceEvent(Dali::DragAndDrop::SourceEventType type)
255 mSourceCallback(type);
259 void DragAndDropEcoreWl::ResetDropTargets()
261 for(std::size_t i = 0; i < mDropTargets.size(); i++)
263 if(mDropTargets[i].inside)
265 Dali::DragAndDrop::DragEvent dragEvent;
266 dragEvent.SetAction(Dali::DragAndDrop::DragType::LEAVE);
267 // -1 means the default position when dragging is cancelled
268 Dali::Vector2 position(-1, -1);
269 dragEvent.SetPosition(position);
270 mDropTargets[i].callback(dragEvent);
272 mDropTargets[i].inside = false;
277 void DragAndDropEcoreWl::SendData(void* event)
279 Ecore_Wl2_Event_Data_Source_Send* ev = reinterpret_cast<Ecore_Wl2_Event_Data_Source_Send*>(event);
280 if(ev->serial != mSerial)
285 int dataLength = strlen(mData.c_str());
286 int bufferSize = dataLength;
287 if((mMimeType.find("text") != std::string::npos) ||
288 (mMimeType.find("markup") != std::string::npos) ||
289 (mMimeType.find("image") != std::string::npos))
294 char* buffer = new char[bufferSize];
300 memcpy(buffer, mData.c_str(), dataLength);
301 buffer[dataLength] = '\0';
303 auto ret = write(ev->fd, buffer, bufferSize);
304 if(DALI_UNLIKELY(ret != bufferSize))
306 DALI_LOG_ERROR("write(ev->fd) return %d! Pleacse check it\n", static_cast<int>(ret));
319 void DragAndDropEcoreWl::ReceiveData(void* event)
321 Ecore_Wl2_Event_Offer_Data_Ready* ev = reinterpret_cast<Ecore_Wl2_Event_Offer_Data_Ready*>(event);
323 if(mTargetIndex != -1)
325 Dali::DragAndDrop::DragEvent dragEvent(Dali::DragAndDrop::DragType::DROP, mPosition, ev->mimetype, ev->data);
326 mDropTargets[mTargetIndex].callback(dragEvent);
327 mDropTargets[mTargetIndex].inside = false;
328 ecore_wl2_offer_finish(ev->offer);
333 bool DragAndDropEcoreWl::CalculateDragEvent(void* event)
335 Ecore_Wl2_Event_Dnd_Motion* ev = reinterpret_cast<Ecore_Wl2_Event_Dnd_Motion*>(event);
337 Dali::DragAndDrop::DragEvent dragEvent;
338 Dali::Vector2 curPosition(ev->x, ev->y);
340 for(std::size_t i = 0; i < mDropTargets.size(); i++)
342 Vector2 position = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::POSITION);
343 Vector2 size = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SIZE);
344 bool currentInside = IsIntersection(ev->x, ev->y, position.x, position.y, size.width, size.height);
346 // Calculate Drag Enter, Leave, Move Event
347 if(currentInside && !mDropTargets[i].inside)
349 mDropTargets[i].inside = true;
351 dragEvent.SetAction(Dali::DragAndDrop::DragType::ENTER);
352 dragEvent.SetPosition(curPosition);
353 mDropTargets[i].callback(dragEvent);
355 ecore_wl2_offer_mimes_set(ev->offer, ecore_wl2_offer_mimes_get(ev->offer));
357 else if(!currentInside && mDropTargets[i].inside)
359 mDropTargets[i].inside = false;
361 dragEvent.SetAction(Dali::DragAndDrop::DragType::LEAVE);
362 dragEvent.SetPosition(curPosition);
363 mDropTargets[i].callback(dragEvent);
365 ecore_wl2_offer_accept(ev->offer, NULL);
367 else if(currentInside && mDropTargets[i].inside)
370 dragEvent.SetAction(Dali::DragAndDrop::DragType::MOVE);
371 dragEvent.SetPosition(curPosition);
372 mDropTargets[i].callback(dragEvent);
379 bool DragAndDropEcoreWl::CalculateViewRegion(void* event)
381 Ecore_Wl2_Event_Dnd_Drop* ev = reinterpret_cast<Ecore_Wl2_Event_Dnd_Drop*>(event);
383 // Check the target object region
386 for(std::size_t i = 0; i < mDropTargets.size(); i++)
388 Vector2 position = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::POSITION);
389 Vector2 size = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SIZE);
390 // If the drop position is in the target object region, request drop data to the source object
391 if(IsIntersection(ev->x, ev->y, position.x, position.y, size.width, size.height))
394 mPosition = position;
395 Dali::Window window = Dali::DevelWindow::Get(mDropTargets[i].target);
397 char* mimetype = (char*)eina_array_data_get(ecore_wl2_offer_mimes_get(ev->offer), 0);
400 ecore_wl2_offer_receive(ev->offer, mimetype);
401 Ecore_Wl2_Display* display = ecore_wl2_connected_display_get(NULL);
402 Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get(display);
403 ecore_wl2_display_flush(ecore_wl2_input_display_get(input));
412 } // namespace Adaptor
414 } // namespace Internal