2 * Copyright (c) 2023 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/devel-api/adaptor-framework/clipboard-event-notifier.h>
20 #include <dali/internal/clipboard/common/clipboard-impl.h>
23 #include <dali/internal/system/linux/dali-ecore.h>
26 #include <dali/internal/adaptor/tizen-wayland/dali-ecore-wl2.h>
28 #include <dali/internal/adaptor/tizen-wayland/dali-ecore-wayland.h>
31 #include <dali/devel-api/common/singleton-service.h>
32 #include <dali/integration-api/debug.h>
33 #include <dali/public-api/object/any.h>
34 #include <dali/public-api/object/type-registry.h>
37 #ifdef DALI_ELDBUS_AVAILABLE
39 #endif // DALI_ELDBUS_AVAILABLE
41 #define CBHM_DBUS_OBJPATH "/org/tizen/cbhm/dbus"
42 #ifndef CBHM_DBUS_INTERFACE
43 #define CBHM_DBUS_INTERFACE "org.tizen.cbhm.dbus"
44 #endif /* CBHM_DBUS_INTERFACE */
45 #define CBHM_COUNT_ALL 0 // ATOM_INDEX_CBHM_COUNT_ALL
47 ///////////////////////////////////////////////////////////////////////////////////////////////////
49 ///////////////////////////////////////////////////////////////////////////////////////////////////
57 struct Clipboard::Impl
61 #ifdef DALI_ELDBUS_AVAILABLE
62 Eldbus_Object* eldbus_obj;
64 cbhm_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
65 eldbus_obj = eldbus_object_get(cbhm_conn, CBHM_DBUS_INTERFACE, CBHM_DBUS_OBJPATH);
66 eldbus_proxy = eldbus_proxy_get(eldbus_obj, CBHM_DBUS_INTERFACE);
67 eldbus_name_owner_changed_callback_add(cbhm_conn, CBHM_DBUS_INTERFACE, NULL, cbhm_conn, EINA_TRUE);
68 eldbus_proxy_signal_handler_add(eldbus_proxy, "ItemClicked", _on_item_clicked, this);
69 #endif // DALI_ELDBUS_AVAILABLE
71 mIsFirstTimeHidden = true;
76 #ifdef DALI_ELDBUS_AVAILABLE
78 eldbus_connection_unref(cbhm_conn);
80 #endif // DALI_ELDBUS_AVAILABLE
83 #ifdef DALI_ELDBUS_AVAILABLE
84 Eldbus_Proxy* cbhm_proxy_get()
89 Eldbus_Connection* cbhm_connection_get()
93 #endif // DALI_ELDBUS_AVAILABLE
95 void SetItem(const std::string& itemData)
97 const char* types[10] = {
102 if(itemData.length() == 0)
106 mSendBuffer = itemData;
108 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
109 types[++i] = "CLIPBOARD_BEGIN";
111 types[++i] = "text/plain;charset=utf-8";
113 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
114 types[++i] = "CLIPBOARD_END";
116 #ifdef ECORE_WAYLAND2
117 Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get(ecore_wl2_connected_display_get(NULL));
118 mSerial = ecore_wl2_dnd_selection_set(input, types);
120 mSerial = ecore_wl_dnd_selection_set(ecore_wl_input_get(), types);
126 #ifdef ECORE_WAYLAND2
127 Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get(ecore_wl2_connected_display_get(NULL));
128 ecore_wl2_dnd_selection_get(input);
130 const char* types[10] = {
135 types[++i] = "text/plain;charset=utf-8";
136 ecore_wl_dnd_selection_get(ecore_wl_input_get(), *types);
139 Dali::ClipboardEventNotifier clipboardEventNotifier(Dali::ClipboardEventNotifier::Get());
140 if(clipboardEventNotifier)
142 clipboardEventNotifier.SetContent(mSendBuffer);
143 clipboardEventNotifier.EmitContentSelectedSignal();
147 char* ExcuteSend(void* event)
149 #ifdef ECORE_WAYLAND2
150 Ecore_Wl2_Event_Data_Source_Send* ev = reinterpret_cast<Ecore_Wl2_Event_Data_Source_Send*>(event);
152 Ecore_Wl_Event_Data_Source_Send* ev = reinterpret_cast<Ecore_Wl_Event_Data_Source_Send*>(event);
155 if(ev->serial != mSerial)
160 int len_buf = mSendBuffer.length();
161 int len_remained = len_buf;
162 int len_written = 0, ret;
163 const char* buf = mSendBuffer.c_str();
165 while(len_written < len_buf)
167 ret = write(ev->fd, buf, len_remained);
177 char* ExcuteReceive(void* event)
179 #ifdef ECORE_WAYLAND2
180 Ecore_Wl2_Event_Selection_Data_Ready* ev = reinterpret_cast<Ecore_Wl2_Event_Selection_Data_Ready*>(event);
182 Ecore_Wl_Event_Selection_Data_Ready* ev = reinterpret_cast<Ecore_Wl_Event_Selection_Data_Ready*>(event);
185 return reinterpret_cast<char*>(ev->data);
190 #ifdef DALI_ELDBUS_AVAILABLE
191 Eldbus_Message *reply, *req;
192 const char * errname = NULL, *errmsg = NULL;
193 #endif // DALI_ELDBUS_AVAILABLE
196 #ifdef DALI_ELDBUS_AVAILABLE
197 if(!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
199 DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
203 eldbus_message_ref(req);
204 eldbus_message_arguments_append(req, "i", CBHM_COUNT_ALL);
205 reply = eldbus_proxy_send_and_block(eldbus_proxy, req, 100);
206 if(!reply || eldbus_message_error_get(reply, &errname, &errmsg))
208 DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
211 eldbus_message_unref(req);
214 eldbus_message_unref(reply);
219 if(!eldbus_message_arguments_get(reply, "i", &count))
221 DALI_LOG_ERROR("Cannot get arguments from eldbus");
222 eldbus_message_unref(req);
223 eldbus_message_unref(reply);
227 eldbus_message_unref(req);
228 eldbus_message_unref(reply);
229 DALI_LOG_ERROR("cbhm item count(%d)", count);
230 #endif // DALI_ELDBUS_AVAILABLE
236 #ifdef DALI_ELDBUS_AVAILABLE
237 eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
238 #endif // DALI_ELDBUS_AVAILABLE
239 mIsFirstTimeHidden = true;
243 void HideClipboard(bool skipFirstHide)
245 if(skipFirstHide && mIsFirstTimeHidden)
247 mIsFirstTimeHidden = false;
250 #ifdef DALI_ELDBUS_AVAILABLE
251 eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
252 #endif // DALI_ELDBUS_AVAILABLE
253 mIsFirstTimeHidden = false;
257 bool IsVisible() const
262 #ifdef DALI_ELDBUS_AVAILABLE
263 static void _on_item_clicked(void* data, const Eldbus_Message* msg EINA_UNUSED)
265 static_cast<Clipboard::Impl*>(data)->RequestItem();
268 Eldbus_Proxy* eldbus_proxy;
269 Eldbus_Connection* cbhm_conn;
270 #endif // DALI_ELDBUS_AVAILABLE
272 std::string mSendBuffer;
274 bool mIsFirstTimeHidden;
275 uint32_t mSerial{0u};
278 Clipboard::Clipboard(Impl* impl)
283 Clipboard::~Clipboard()
288 Dali::Clipboard Clipboard::Get()
290 Dali::Clipboard clipboard;
292 Dali::SingletonService service(SingletonService::Get());
295 // Check whether the singleton is already created
296 Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::Clipboard));
299 // If so, downcast the handle
300 clipboard = Dali::Clipboard(dynamic_cast<Clipboard*>(handle.GetObjectPtr()));
304 Clipboard::Impl* impl(new Clipboard::Impl());
305 clipboard = Dali::Clipboard(new Clipboard(impl));
306 service.Register(typeid(Dali::Clipboard), clipboard);
313 bool Clipboard::IsAvailable()
315 Dali::SingletonService service(SingletonService::Get());
318 Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::Clipboard));
327 bool Clipboard::SetItem(const std::string& itemData)
329 mImpl->SetItem(itemData);
334 * Request clipboard service to give an item
336 void Clipboard::RequestItem()
338 mImpl->RequestItem();
342 * Get number of items in clipboard
344 unsigned int Clipboard::NumberOfItems()
346 int count = mImpl->GetCount();
347 return (count < 0 ? 0 : count);
350 void Clipboard::ShowClipboard()
352 mImpl->ShowClipboard();
355 void Clipboard::HideClipboard(bool skipFirstHide)
357 mImpl->HideClipboard(skipFirstHide);
360 bool Clipboard::IsVisible() const
362 return mImpl->IsVisible();
365 char* Clipboard::ExcuteBuffered(bool type, void* event)
367 return (type ? mImpl->ExcuteSend(event) : mImpl->ExcuteReceive(event));
370 } // namespace Adaptor
372 } // namespace Internal