2 * Copyright (c) 2017 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 "clipboard-impl.h"
22 // Ecore is littered with C style cast
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wold-style-cast"
26 #include <Ecore_Wayland.h>
27 #include <dali/public-api/object/any.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/integration-api/debug.h>
32 #ifdef DALI_ELDBUS_AVAILABLE
34 #endif // DALI_ELDBUS_AVAILABLE
37 #include <singleton-service-impl.h>
39 #define CBHM_DBUS_OBJPATH "/org/tizen/cbhm/dbus"
40 #ifndef CBHM_DBUS_INTERFACE
41 #define CBHM_DBUS_INTERFACE "org.tizen.cbhm.dbus"
42 #endif /* CBHM_DBUS_INTERFACE */
43 #define CBHM_COUNT_ALL 0 // ATOM_INDEX_CBHM_COUNT_ALL
45 ///////////////////////////////////////////////////////////////////////////////////////////////////
47 ///////////////////////////////////////////////////////////////////////////////////////////////////
58 struct Clipboard::Impl
62 Eldbus_Object *eldbus_obj;
63 cbhm_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
64 eldbus_obj = eldbus_object_get(cbhm_conn, CBHM_DBUS_INTERFACE, CBHM_DBUS_OBJPATH);
65 eldbus_proxy = eldbus_proxy_get(eldbus_obj, CBHM_DBUS_INTERFACE);
66 eldbus_name_owner_changed_callback_add(cbhm_conn, CBHM_DBUS_INTERFACE, NULL, cbhm_conn, EINA_TRUE);
67 eldbus_proxy_signal_handler_add(eldbus_proxy, "ItemClicked", _on_item_clicked, this);
69 mIsFirstTimeHidden = true;
75 eldbus_connection_unref(cbhm_conn);
78 Eldbus_Proxy* cbhm_proxy_get()
83 Eldbus_Connection* cbhm_connection_get()
88 void SetItem( const std::string &itemData )
90 const char *types[10] = {0, };
93 if (itemData.length() == 0)
97 mSendBuffer = itemData;
99 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
100 types[++i] = "CLIPBOARD_BEGIN";
102 types[++i] = "text/plain;charset=utf-8";
104 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
105 types[++i] = "CLIPBOARD_END";
106 ecore_wl_dnd_selection_set(ecore_wl_input_get(), types);
111 const char *types[10] = {0, };
114 types[++i] = "text/plain;charset=utf-8";
115 ecore_wl_dnd_selection_get(ecore_wl_input_get(), *types);
118 char *ExcuteSend( void *event )
120 Ecore_Wl_Event_Data_Source_Send *ev = (Ecore_Wl_Event_Data_Source_Send *)event;
121 int len_buf = mSendBuffer.length();
122 int len_remained = len_buf;
123 int len_written = 0, ret;
124 const char *buf = mSendBuffer.c_str();
126 while (len_written < len_buf)
128 ret = write(ev->fd, buf, len_remained);
129 if (ret == -1) break;
138 char *ExcuteReceive( void *event )
140 Ecore_Wl_Event_Selection_Data_Ready *ev = (Ecore_Wl_Event_Selection_Data_Ready *)event;
142 return (char *)ev->data;
147 Eldbus_Message *reply, *req;
148 const char *errname = NULL, *errmsg = NULL;
151 if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
153 DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
157 eldbus_message_ref(req);
158 eldbus_message_arguments_append(req, "i", CBHM_COUNT_ALL) ;
159 reply = eldbus_proxy_send_and_block(eldbus_proxy, req, 100);
160 if (!reply || eldbus_message_error_get(reply, &errname, &errmsg))
162 DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
164 eldbus_message_unref(req);
167 eldbus_message_unref(reply);
172 if (!eldbus_message_arguments_get(reply, "i", &count))
174 DALI_LOG_ERROR("Cannot get arguments from eldbus");
175 eldbus_message_unref(req);
176 eldbus_message_unref(reply);
180 eldbus_message_unref(req);
181 eldbus_message_unref(reply);
182 DALI_LOG_ERROR("cbhm item count(%d)", count);
188 eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
189 mIsFirstTimeHidden = true;
193 void HideClipboard( bool skipFirstHide )
195 if ( skipFirstHide && mIsFirstTimeHidden )
197 mIsFirstTimeHidden = false;
200 eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
201 mIsFirstTimeHidden = false;
205 bool IsVisible() const
210 static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
212 static_cast<Clipboard::Impl*>(data)->RequestItem();
215 Eldbus_Proxy *eldbus_proxy;
216 Eldbus_Connection *cbhm_conn;
218 std::string mSendBuffer;
220 bool mIsFirstTimeHidden;
223 Clipboard::Clipboard(Impl* impl)
228 Clipboard::~Clipboard()
233 Dali::Clipboard Clipboard::Get()
235 Dali::Clipboard clipboard;
237 Dali::SingletonService service( SingletonService::Get() );
240 // Check whether the singleton is already created
241 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
244 // If so, downcast the handle
245 clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
249 Clipboard::Impl* impl( new Clipboard::Impl() );
250 clipboard = Dali::Clipboard( new Clipboard(impl) );
251 service.Register( typeid(Dali::Clipboard), clipboard );
258 bool Clipboard::SetItem(const std::string &itemData )
260 mImpl->SetItem( itemData );
265 * Request clipboard service to give an item
267 void Clipboard::RequestItem()
269 mImpl->RequestItem();
273 * Get number of items in clipboard
275 unsigned int Clipboard::NumberOfItems()
277 return mImpl->GetCount();
280 void Clipboard::ShowClipboard()
282 mImpl->ShowClipboard();
285 void Clipboard::HideClipboard(bool skipFirstHide)
287 mImpl->HideClipboard(skipFirstHide);
290 bool Clipboard::IsVisible() const
292 return mImpl->IsVisible();
295 char* Clipboard::ExcuteBuffered( bool type, void *event )
297 return (type ? mImpl->ExcuteSend( event ) : mImpl->ExcuteReceive( event ));
300 } // namespace Adaptor
302 } // namespace Internal
306 #pragma GCC diagnostic pop