2 * Copyright (c) 2014 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"
23 #include <Ecore_Wayland.h>
24 #include <dali/public-api/object/any.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/integration-api/debug.h>
29 #ifdef DALI_ELDBUS_AVAILABLE
31 #endif // DALI_ELDBUS_AVAILABLE
34 #include <singleton-service-impl.h>
36 #define CBHM_DBUS_OBJPATH "/org/tizen/cbhm/dbus"
37 #ifndef CBHM_DBUS_INTERFACE
38 #define CBHM_DBUS_INTERFACE "org.tizen.cbhm.dbus"
39 #endif /* CBHM_DBUS_INTERFACE */
40 #define CBHM_COUNT_ALL 0 // ATOM_INDEX_CBHM_COUNT_ALL
42 ///////////////////////////////////////////////////////////////////////////////////////////////////
44 ///////////////////////////////////////////////////////////////////////////////////////////////////
55 struct Clipboard::Impl
59 Eldbus_Object *eldbus_obj;
60 cbhm_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
61 eldbus_obj = eldbus_object_get(cbhm_conn, CBHM_DBUS_INTERFACE, CBHM_DBUS_OBJPATH);
62 eldbus_proxy = eldbus_proxy_get(eldbus_obj, CBHM_DBUS_INTERFACE);
63 eldbus_name_owner_changed_callback_add(cbhm_conn, CBHM_DBUS_INTERFACE, NULL, cbhm_conn, EINA_TRUE);
64 eldbus_proxy_signal_handler_add(eldbus_proxy, "ItemClicked", _on_item_clicked, this);
66 mIsFirstTimeHidden = true;
72 eldbus_connection_unref(cbhm_conn);
75 Eldbus_Proxy* cbhm_proxy_get()
80 Eldbus_Connection* cbhm_connection_get()
85 void SetItem( const std::string &itemData )
87 const char *types[10] = {0, };
90 if (itemData.length() == 0)
94 mSendBuffer = itemData;
96 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
97 types[++i] = "CLIPBOARD_BEGIN";
99 types[++i] = "text/plain;charset=utf-8";
101 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
102 types[++i] = "CLIPBOARD_END";
103 ecore_wl_dnd_selection_set(ecore_wl_input_get(), types);
108 const char *types[10] = {0, };
111 types[++i] = "text/plain;charset=utf-8";
112 ecore_wl_dnd_selection_get(ecore_wl_input_get(), *types);
115 char *ExcuteSend( void *event )
117 Ecore_Wl_Event_Data_Source_Send *ev = (Ecore_Wl_Event_Data_Source_Send *)event;
118 int len_buf = mSendBuffer.length();
119 int len_remained = len_buf;
120 int len_written = 0, ret;
121 const char *buf = mSendBuffer.c_str();
123 while (len_written < len_buf)
125 ret = write(ev->fd, buf, len_remained);
126 if (ret == -1) break;
135 char *ExcuteReceive( void *event )
137 Ecore_Wl_Event_Selection_Data_Ready *ev = (Ecore_Wl_Event_Selection_Data_Ready *)event;
139 return (char *)ev->data;
144 Eldbus_Message *reply, *req;
145 const char *errname = NULL, *errmsg = NULL;
148 if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
150 DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
154 eldbus_message_ref(req);
155 eldbus_message_arguments_append(req, "i", CBHM_COUNT_ALL) ;
156 reply = eldbus_proxy_send_and_block(eldbus_proxy, req, 100);
157 if (!reply || eldbus_message_error_get(reply, &errname, &errmsg))
159 DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
161 eldbus_message_unref(req);
165 if (!eldbus_message_arguments_get(reply, "i", &count))
167 DALI_LOG_ERROR("Cannot get arguments from eldbus");
168 eldbus_message_unref(req);
172 eldbus_message_unref(req);
173 DALI_LOG_ERROR("cbhm item count(%d)", count);
179 eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
180 mIsFirstTimeHidden = true;
184 void HideClipboard( bool skipFirstHide )
186 if ( skipFirstHide && mIsFirstTimeHidden )
188 mIsFirstTimeHidden = false;
191 eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
192 mIsFirstTimeHidden = false;
196 bool IsVisible() const
201 static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
203 static_cast<Clipboard::Impl*>(data)->RequestItem();
206 Eldbus_Proxy *eldbus_proxy;
207 Eldbus_Connection *cbhm_conn;
209 std::string mSendBuffer;
211 bool mIsFirstTimeHidden;
214 Clipboard::Clipboard(Impl* impl)
219 Clipboard::~Clipboard()
223 Dali::Clipboard Clipboard::Get()
225 Dali::Clipboard clipboard;
227 Dali::SingletonService service( SingletonService::Get() );
230 // Check whether the singleton is already created
231 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
234 // If so, downcast the handle
235 clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
239 Clipboard::Impl* impl( new Clipboard::Impl() );
240 clipboard = Dali::Clipboard( new Clipboard(impl) );
241 service.Register( typeid(Dali::Clipboard), clipboard );
248 bool Clipboard::SetItem(const std::string &itemData )
250 mImpl->SetItem( itemData );
255 * Request clipboard service to give an item
257 void Clipboard::RequestItem()
259 mImpl->RequestItem();
263 * Get number of items in clipboard
265 unsigned int Clipboard::NumberOfItems()
267 return mImpl->GetCount();
270 void Clipboard::ShowClipboard()
272 mImpl->ShowClipboard();
275 void Clipboard::HideClipboard(bool skipFirstHide)
277 mImpl->HideClipboard(skipFirstHide);
280 bool Clipboard::IsVisible() const
282 return mImpl->IsVisible();
285 char* Clipboard::ExcuteBuffered( bool type, void *event )
287 return (type ? mImpl->ExcuteSend( event ) : mImpl->ExcuteReceive( event ));
290 } // namespace Adaptor
292 } // namespace Internal