Merge "Calculate EncodedImageBuffer's hash in generate time" into devel/master
[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 EcoreEventDataSourceEnd(void* data, int type, void* event)
55 {
56   Ecore_Wl2_Event_Data_Source_End *ev = reinterpret_cast<Ecore_Wl2_Event_Data_Source_End*>(event);
57   DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
58   if(ev->cancelled)
59   {
60     dndImpl->CallSourceEvent(Dali::DragAndDrop::SourceEventType::CANCEL);
61   }
62   else
63   {
64     dndImpl->CallSourceEvent(Dali::DragAndDrop::SourceEventType::ACCEPT);
65   }
66
67   return ECORE_CALLBACK_PASS_ON;
68 }
69
70 static Eina_Bool EcoreEventDataSourceDrop(void* data, int type, void* event)
71 {
72   DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
73   dndImpl->CallSourceEvent(Dali::DragAndDrop::SourceEventType::FINISH);
74   return ECORE_CALLBACK_PASS_ON;
75 }
76
77 static Eina_Bool EcoreEventOfferDataReady(void* data, int type, void* event)
78 {
79   DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
80   dndImpl->ReceiveData(event);
81
82   return ECORE_CALLBACK_PASS_ON;
83 }
84
85 static Eina_Bool EcoreEventDataMotion(void* data, int type, void* event)
86 {
87   DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
88   dndImpl->CalculateDragEvent(event);
89
90   return ECORE_CALLBACK_PASS_ON;
91 }
92
93 static Eina_Bool EcoreEventDataDrop(void* data, int type, void* event)
94 {
95   DragAndDropEcoreWl* dndImpl = reinterpret_cast<DragAndDropEcoreWl*>(data);
96   dndImpl->CalculateViewRegion(event);
97
98   return ECORE_CALLBACK_PASS_ON;
99 }
100
101 static Eina_Bool EcoreEventDataEnter(void* data, int type, void* event)
102 {
103   Ecore_Wl2_Event_Dnd_Enter* ev = reinterpret_cast<Ecore_Wl2_Event_Dnd_Enter*>(event);
104
105   // Set default offer is reject
106   ecore_wl2_offer_accept(ev->offer, NULL);
107
108   return ECORE_CALLBACK_PASS_ON;
109 }
110
111 Dali::DragAndDrop GetDragAndDrop()
112 {
113   Dali::DragAndDrop dnd;
114
115   Dali::SingletonService service(SingletonService::Get());
116   if(service)
117   {
118     // Check whether the singleton is already created
119     Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::DragAndDrop));
120     if(handle)
121     {
122       // If so, downcast the handle
123       dnd = Dali::DragAndDrop(dynamic_cast<DragAndDrop*>(handle.GetObjectPtr()));
124     }
125     else
126     {
127       // Create a singleon instance
128       DragAndDropEcoreWl* dndImpl = new DragAndDropEcoreWl();
129
130       dnd = Dali::DragAndDrop(dndImpl);
131       service.Register(typeid(Dali::DragAndDrop), dnd);
132     }
133   }
134
135   return dnd;
136 }
137
138 DragAndDropEcoreWl::DragAndDropEcoreWl()
139 {
140   // Source Events
141   mSendHandler       = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_SEND, EcoreEventDataSend, this);
142   mSourceEndHandler  = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_END, EcoreEventDataSourceEnd, this);
143   mSourceDropHandler = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_DROP, EcoreEventDataSourceDrop, this);
144
145   // Target Events
146   mReceiveHandler    = ecore_event_handler_add(ECORE_WL2_EVENT_OFFER_DATA_READY, EcoreEventOfferDataReady, this);
147   mMotionHandler     = ecore_event_handler_add(ECORE_WL2_EVENT_DND_MOTION, EcoreEventDataMotion, this);
148   mDropHandler       = ecore_event_handler_add(ECORE_WL2_EVENT_DND_DROP, EcoreEventDataDrop, this);
149   mEnterHandler      = ecore_event_handler_add(ECORE_WL2_EVENT_DND_ENTER, EcoreEventDataEnter, this);
150 }
151
152 DragAndDropEcoreWl::~DragAndDropEcoreWl()
153 {
154   // Source Events
155   ecore_event_handler_del(mSendHandler);
156   ecore_event_handler_del(mSourceEndHandler);
157   ecore_event_handler_del(mSourceDropHandler);
158
159   // Target Events
160   ecore_event_handler_del(mReceiveHandler);
161   ecore_event_handler_del(mMotionHandler);
162   ecore_event_handler_del(mDropHandler);
163   ecore_event_handler_del(mEnterHandler);
164 }
165
166 bool DragAndDropEcoreWl::StartDragAndDrop(Dali::Actor source, Dali::Window shadowWindow, const Dali::DragAndDrop::DragData& data, Dali::DragAndDrop::SourceFunction callback)
167 {
168   // Get Parent Window
169   auto parent = Dali::DevelWindow::Get(source);
170
171   // Set Drag Source Data
172   mMimeType = data.GetMimeType();
173   mData     = data.GetData();
174
175   // Set Source Event
176   mSourceCallback = callback;
177
178   // Set Drag Window
179   mDragWindow = shadowWindow;
180
181   // Start Drag and Drop
182   Ecore_Wl2_Window*  parentWindow = AnyCast<Ecore_Wl2_Window*>(parent.GetNativeHandle());
183   Ecore_Wl2_Window*  dragWindow   = AnyCast<Ecore_Wl2_Window*>(mDragWindow.GetNativeHandle());
184   Ecore_Wl2_Display* display      = ecore_wl2_connected_display_get(NULL);
185   Ecore_Wl2_Input*   input        = ecore_wl2_input_default_input_get(display);
186
187   // Disable Default Cursor
188   ecore_wl2_input_pointer_set(input, NULL, 0, 0);
189
190   // Set mime type for drag and drop
191   const char* mimeTypes[2];
192   mimeTypes[0] = mMimeType.c_str();
193   mimeTypes[1] = NULL;
194
195   // Set mime type
196   ecore_wl2_dnd_drag_types_set(input, (const char**)mimeTypes);
197
198   // Start wayland drag and drop
199   mSerial = ecore_wl2_dnd_drag_start(input, parentWindow, dragWindow);
200
201   // Call Start Event
202   CallSourceEvent(Dali::DragAndDrop::SourceEventType::START);
203
204   return true;
205 }
206
207 bool DragAndDropEcoreWl::AddListener(Dali::Actor target, Dali::DragAndDrop::DragAndDropFunction callback)
208 {
209   std::vector<DropTarget>::iterator itr;
210   for(itr = mDropTargets.begin(); itr < mDropTargets.end(); itr++)
211   {
212     if((*itr).target == target)
213     {
214       return false;
215     }
216   }
217
218   DropTarget targetData;
219   targetData.target   = target;
220   targetData.callback = callback;
221   targetData.inside   = false;
222
223   mDropTargets.push_back(targetData);
224
225   return true;
226 }
227
228 bool DragAndDropEcoreWl::RemoveListener(Dali::Actor target)
229 {
230   std::vector<DropTarget>::iterator itr;
231   for(itr = mDropTargets.begin(); itr < mDropTargets.end(); itr++)
232   {
233     if((*itr).target == target)
234     {
235       mDropTargets.erase(itr);
236       break;
237     }
238   }
239
240   return true;
241 }
242
243 void DragAndDropEcoreWl::CallSourceEvent(Dali::DragAndDrop::SourceEventType type)
244 {
245   if(mSourceCallback)
246   {
247     mSourceCallback(type);
248   }
249 }
250
251 void DragAndDropEcoreWl::SendData(void* event)
252 {
253   Ecore_Wl2_Event_Data_Source_Send* ev = reinterpret_cast<Ecore_Wl2_Event_Data_Source_Send*>(event);
254   if(ev->serial != mSerial)
255   {
256     return;
257   }
258
259   int dataLength = strlen(mData.c_str());
260   int bufferSize = dataLength;
261   if((mMimeType.find("text") != std::string::npos) ||
262      (mMimeType.find("markup") != std::string::npos) ||
263      (mMimeType.find("image") != std::string::npos))
264   {
265     bufferSize += 1;
266   }
267
268   char* buffer = new char[bufferSize];
269   if(!buffer)
270   {
271     return;
272   }
273
274   memcpy(buffer, mData.c_str(), dataLength);
275   buffer[dataLength] = '\0';
276
277   auto ret = write(ev->fd, buffer, bufferSize);
278   if(DALI_UNLIKELY(ret != bufferSize))
279   {
280     DALI_LOG_ERROR("write(ev->fd) return %d! Pleacse check it\n", static_cast<int>(ret));
281   }
282
283   close(ev->fd);
284
285   if(mDragWindow)
286   {
287     mDragWindow.Hide();
288   }
289
290   delete[] buffer;
291 }
292
293 void DragAndDropEcoreWl::ReceiveData(void* event)
294 {
295   Ecore_Wl2_Event_Offer_Data_Ready* ev = reinterpret_cast<Ecore_Wl2_Event_Offer_Data_Ready*>(event);
296
297   if(mTargetIndex != -1)
298   {
299     Dali::DragAndDrop::DragEvent dragEvent(Dali::DragAndDrop::DragType::DROP, mPosition, ev->mimetype, ev->data);
300     mDropTargets[mTargetIndex].callback(dragEvent);
301     mDropTargets[mTargetIndex].inside = false;
302     ecore_wl2_offer_finish(ev->offer);
303   }
304   mTargetIndex = -1;
305 }
306
307 bool DragAndDropEcoreWl::CalculateDragEvent(void* event)
308 {
309   Ecore_Wl2_Event_Dnd_Motion* ev = reinterpret_cast<Ecore_Wl2_Event_Dnd_Motion*>(event);
310
311   Dali::DragAndDrop::DragEvent dragEvent;
312   Dali::Vector2                curPosition(ev->x, ev->y);
313
314   for(std::size_t i = 0; i < mDropTargets.size(); i++)
315   {
316     Vector2 position      = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::POSITION);
317     Vector2 size          = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SIZE);
318     bool    currentInside = IsIntersection(ev->x, ev->y, position.x, position.y, size.width, size.height);
319
320     // Calculate Drag Enter, Leave, Move Event
321     if(currentInside && !mDropTargets[i].inside)
322     {
323       mDropTargets[i].inside = true;
324       // Call Enter Event
325       dragEvent.SetAction(Dali::DragAndDrop::DragType::ENTER);
326       dragEvent.SetPosition(curPosition);
327       mDropTargets[i].callback(dragEvent);
328       // Accept Offer
329       ecore_wl2_offer_mimes_set(ev->offer, ecore_wl2_offer_mimes_get(ev->offer));
330     }
331     else if(!currentInside && mDropTargets[i].inside)
332     {
333       mDropTargets[i].inside = false;
334       // Call Leave Event
335       dragEvent.SetAction(Dali::DragAndDrop::DragType::LEAVE);
336       dragEvent.SetPosition(curPosition);
337       mDropTargets[i].callback(dragEvent);
338       // Reject Offer
339       ecore_wl2_offer_accept(ev->offer, NULL);
340     }
341     else if(currentInside && mDropTargets[i].inside)
342     {
343       // Call Move Event
344       dragEvent.SetAction(Dali::DragAndDrop::DragType::MOVE);
345       dragEvent.SetPosition(curPosition);
346       mDropTargets[i].callback(dragEvent);
347     }
348   }
349
350   return true;
351 }
352
353 bool DragAndDropEcoreWl::CalculateViewRegion(void* event)
354 {
355   Ecore_Wl2_Event_Dnd_Drop* ev = reinterpret_cast<Ecore_Wl2_Event_Dnd_Drop*>(event);
356
357   // Check the target object region
358   mTargetIndex = -1;
359
360   for(std::size_t i = 0; i < mDropTargets.size(); i++)
361   {
362     Vector2 position = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::POSITION);
363     Vector2 size     = mDropTargets[i].target.GetProperty<Vector2>(Dali::Actor::Property::SIZE);
364     // If the drop position is in the target object region, request drop data to the source object
365     if(IsIntersection(ev->x, ev->y, position.x, position.y, size.width, size.height))
366     {
367       mTargetIndex        = i;
368       mPosition           = position;
369       Dali::Window window = Dali::DevelWindow::Get(mDropTargets[i].target);
370
371       char* mimetype = (char*)eina_array_data_get(ecore_wl2_offer_mimes_get(ev->offer), 0);
372       if(mimetype)
373       {
374         ecore_wl2_offer_receive(ev->offer, mimetype);
375         Ecore_Wl2_Display* display = ecore_wl2_connected_display_get(NULL);
376         Ecore_Wl2_Input*   input   = ecore_wl2_input_default_input_get(display);
377         ecore_wl2_display_flush(ecore_wl2_input_display_get(input));
378       }
379       return true;
380     }
381   }
382
383   return false;
384 }
385
386 } // namespace Adaptor
387
388 } // namespace Internal
389
390 } // namespace Dali