492210c2ad8a55edbc207c8d5754024e7f805552
[platform/core/uifw/dali-adaptor.git] / dali / internal / drag-and-drop / tizen-wayland / drag-and-drop-impl-ecore-wl2.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/drag-and-drop/tizen-wayland/drag-and-drop-impl-ecore-wl2.h>
20
21 // EXTERNAL INCLUDES
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>
25 #include <unistd.h>
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////
28 // DragAndDrop
29 ///////////////////////////////////////////////////////////////////////////////////////////////////
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace Adaptor
36 {
37 static bool IsIntersection(int px, int py, int tx, int ty, int tw, int th)
38 {
39   if(px > tx && py > ty && px < (tx + tw) && py < (ty + th))
40   {
41     return true;
42   }
43   return false;
44 }
45
46 static Eina_Bool EcoreEventDataSend(void* data, int type, void* event)
47 {
48   DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
49   dndImpl->SendData(event);
50
51   return ECORE_CALLBACK_PASS_ON;
52 }
53
54 static Eina_Bool EcoreEventOfferDataReady(void* data, int type, void* event)
55 {
56   DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
57   dndImpl->ReceiveData(event);
58
59   return ECORE_CALLBACK_PASS_ON;
60 }
61
62 static Eina_Bool EcoreEventDataMotion(void* data, int type, void* event)
63 {
64   DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
65   dndImpl->CalculateDragEvent(event);
66
67   return ECORE_CALLBACK_PASS_ON;
68 }
69
70 static Eina_Bool EcoreEventDataDrop(void* data, int type, void* event)
71 {
72   DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
73   dndImpl->CalculateViewRegion(event);
74
75   return ECORE_CALLBACK_PASS_ON;
76 }
77
78 Dali::DragAndDrop GetDragAndDrop()
79 {
80   Dali::DragAndDrop dnd;
81
82   Dali::SingletonService service(SingletonService::Get());
83   if(service)
84   {
85     // Check whether the singleton is already created
86     Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::DragAndDrop));
87     if(handle)
88     {
89       // If so, downcast the handle
90       dnd = Dali::DragAndDrop(dynamic_cast<DragAndDrop*>(handle.GetObjectPtr()));
91     }
92     else
93     {
94       // Create a singleon instance
95       DragAndDropEcoreWl* dndImpl = new DragAndDropEcoreWl();
96
97       dnd = Dali::DragAndDrop(dndImpl);
98       service.Register(typeid(Dali::DragAndDrop), dnd);
99     }
100   }
101
102   return dnd;
103 }
104
105 DragAndDropEcoreWl::DragAndDropEcoreWl()
106 {
107   mSendHandler    = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_SEND, EcoreEventDataSend, this);
108   mReceiveHandler = ecore_event_handler_add(ECORE_WL2_EVENT_OFFER_DATA_READY, EcoreEventOfferDataReady, this);
109   mMotionHandler  = ecore_event_handler_add(ECORE_WL2_EVENT_DND_MOTION, EcoreEventDataMotion, this);
110   mDropHandler    = ecore_event_handler_add(ECORE_WL2_EVENT_DND_DROP, EcoreEventDataDrop, this);
111 }
112
113 DragAndDropEcoreWl::~DragAndDropEcoreWl()
114 {
115   ecore_event_handler_del(mSendHandler);
116   ecore_event_handler_del(mReceiveHandler);
117   ecore_event_handler_del(mMotionHandler);
118   ecore_event_handler_del(mDropHandler);
119 }
120
121 bool DragAndDropEcoreWl::StartDragAndDrop(Dali::Actor source, Dali::Actor shadow, const Dali::DragAndDrop::DragData& data)
122 {
123   // Get Parent Window
124   auto parent = Dali::DevelWindow::Get(source);
125
126   // Set Drag Source Data
127   mMimeType = data.GetMimeType();
128   mData = data.GetData();
129
130   // Apply Shadow Property
131   shadow.SetProperty(Dali::Actor::Property::SIZE, Vector2(150, 150));
132   shadow.SetProperty(Dali::Actor::Property::OPACITY, 0.9f);
133
134   // Create Drag Window
135   mDragWindow = Dali::Window::New(Dali::PositionSize(0, 0, 150, 150), "DragWindow", "class", true);
136   mDragWindow.SetTransparency(true);
137   mDragWindow.SetSize(Dali::Window::WindowSize(150, 150));
138   mDragWindow.SetBackgroundColor(Color::TRANSPARENT);
139   mDragWindow.Add(shadow);
140
141   // Start Drag and Drop
142   Ecore_Wl2_Window*  parentWindow = AnyCast<Ecore_Wl2_Window*>(parent.GetNativeHandle());
143   Ecore_Wl2_Window*  dragWindow   = AnyCast<Ecore_Wl2_Window*>(mDragWindow.GetNativeHandle());
144   Ecore_Wl2_Display* display      = ecore_wl2_connected_display_get(NULL);
145   Ecore_Wl2_Input*   input        = ecore_wl2_input_default_input_get(display);
146
147   // Disable Default Cursor
148   ecore_wl2_input_pointer_set(input, NULL, 0, 0);
149
150   // Set mime type for drag and drop
151   const char* mimeTypes[2];
152   mimeTypes[0] = mMimeType.c_str();
153   mimeTypes[1] = NULL;
154
155
156   // Set mime type
157   ecore_wl2_dnd_drag_types_set(input, (const char**)mimeTypes);
158
159   // Start wayland drag and drop
160   mSerial = ecore_wl2_dnd_drag_start(input, parentWindow, dragWindow);
161
162   return true;
163 }
164
165 bool DragAndDropEcoreWl::AddListener(Dali::Actor target, Dali::DragAndDrop::DragAndDropFunction callback)
166 {
167   std::vector<DropTarget>::iterator itr;
168   for(itr = mDropTargets.begin(); itr < mDropTargets.end(); itr++)
169   {
170     if((*itr).target == target)
171     {
172       return false;
173     }
174   }
175
176   DropTarget targetData;
177   targetData.target   = target;
178   targetData.callback = callback;
179   targetData.inside   = false;
180
181   mDropTargets.push_back(targetData);
182
183   return true;
184 }
185
186 bool DragAndDropEcoreWl::RemoveListener(Dali::Actor target)
187 {
188   std::vector<DropTarget>::iterator itr;
189   for(itr = mDropTargets.begin(); itr < mDropTargets.end(); itr++)
190   {
191     if((*itr).target == target)
192     {
193       mDropTargets.erase(itr);
194       break;
195     }
196   }
197
198   return true;
199 }
200
201 void DragAndDropEcoreWl::SendData(void* event)
202 {
203   Ecore_Wl2_Event_Data_Source_Send* ev = reinterpret_cast<Ecore_Wl2_Event_Data_Source_Send*>(event);
204   if(ev->serial != mSerial)
205   {
206     return;
207   }
208
209   int dataLength = strlen(mData.c_str());
210   int bufferSize = dataLength;
211   if((mMimeType.find("text") != std::string::npos) ||
212      (mMimeType.find("markup") != std::string::npos) ||
213      (mMimeType.find("image") != std::string::npos))
214   {
215      bufferSize += 1;
216   }
217
218   char* buffer = new char[bufferSize];
219   if(!buffer)
220   {
221     return;
222   }
223
224   memcpy(buffer, mData.c_str(), dataLength);
225   buffer[dataLength] = '\0';
226
227   write(ev->fd, buffer, bufferSize);
228   close(ev->fd);
229
230   if(mDragWindow)
231   {
232     mDragWindow.Hide();
233   }
234
235   delete [] buffer;
236 }
237
238 void DragAndDropEcoreWl::ReceiveData(void* event)
239 {
240   Ecore_Wl2_Event_Offer_Data_Ready* ev = reinterpret_cast<Ecore_Wl2_Event_Offer_Data_Ready*>(event);
241
242   if(mTargetIndex != -1)
243   {
244     Dali::DragAndDrop::DragEvent dragEvent(Dali::DragAndDrop::DragType::DROP, mPosition, ev->mimetype, ev->data);
245     mDropTargets[mTargetIndex].callback(dragEvent);
246     mDropTargets[mTargetIndex].inside = false;
247   }
248   mTargetIndex = -1;
249 }
250
251 bool DragAndDropEcoreWl::CalculateDragEvent(void* event)
252 {
253   Ecore_Wl2_Event_Dnd_Motion* ev = reinterpret_cast<Ecore_Wl2_Event_Dnd_Motion*>(event);
254
255   Dali::DragAndDrop::DragEvent dragEvent;
256   Dali::Vector2                curPosition(ev->x, ev->y);
257
258   for(int i = 0; i < mDropTargets.size(); i++)
259   {
260     Vector2 position      = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::POSITION);
261     Vector2 size          = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SIZE);
262     bool    currentInside = IsIntersection(ev->x, ev->y, position.x, position.y, size.width, size.height);
263
264     // Calculate Drag Enter, Leave, Move Event
265     if(currentInside && !mDropTargets[i].inside)
266     {
267       mDropTargets[i].inside = true;
268       // Call Enter Event
269       dragEvent.SetAction(Dali::DragAndDrop::DragType::ENTER);
270       dragEvent.SetPosition(curPosition);
271       mDropTargets[i].callback(dragEvent);
272     }
273     else if(!currentInside && mDropTargets[i].inside)
274     {
275       mDropTargets[i].inside = false;
276       // Call Leave Event
277       dragEvent.SetAction(Dali::DragAndDrop::DragType::LEAVE);
278       dragEvent.SetPosition(curPosition);
279       mDropTargets[i].callback(dragEvent);
280     }
281     else if(currentInside && mDropTargets[i].inside)
282     {
283       // Call Move Event
284       dragEvent.SetAction(Dali::DragAndDrop::DragType::MOVE);
285       dragEvent.SetPosition(curPosition);
286       mDropTargets[i].callback(dragEvent);
287     }
288   }
289
290   return true;
291 }
292
293 bool DragAndDropEcoreWl::CalculateViewRegion(void* event)
294 {
295   Ecore_Wl2_Event_Dnd_Drop* ev = reinterpret_cast<Ecore_Wl2_Event_Dnd_Drop*>(event);
296
297   // Check the target object region
298   mTargetIndex = -1;
299
300   for(int i = 0; i < mDropTargets.size(); i++)
301   {
302     Vector2 position = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::POSITION);
303     Vector2 size     = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SIZE);
304     // If the drop position is in the target object region, request drop data to the source object
305     if(IsIntersection(ev->x, ev->y, position.x, position.y, size.width, size.height))
306     {
307       mTargetIndex        = i;
308       mPosition           = position;
309       Dali::Window window = Dali::DevelWindow::Get(mDropTargets[i].target);
310
311       char* mimetype = (char*)eina_array_data_get(ecore_wl2_offer_mimes_get(ev->offer), 0);
312       if(mimetype)
313       {
314         ecore_wl2_offer_accept(ev->offer, mimetype);
315         ecore_wl2_offer_receive(ev->offer, mimetype);
316         Ecore_Wl2_Display* display = ecore_wl2_connected_display_get(NULL);
317         Ecore_Wl2_Input*   input   = ecore_wl2_input_default_input_get(display);
318         ecore_wl2_display_flush(ecore_wl2_input_display_get(input));
319       }
320       return true;
321     }
322   }
323
324   return false;
325 }
326
327 } // namespace Adaptor
328
329 } // namespace Internal
330
331 } // namespace Dali