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 <dali/public-api/dali-core.h>
24 #include <dali/integration-api/debug.h>
28 #include <dali/public-api/object/any.h>
29 #include <adaptor-impl.h>
30 #include <ecore-x-window-interface.h>
32 namespace //unnamed namespace
34 const char* const CBHM_WINDOW = "CBHM_XWIN";
35 const char* const CBHM_MSG = "CBHM_MSG";
36 const char* const CBHM_ITEM = "CBHM_ITEM";
37 const char* const CBHM_cCOUNT = "CBHM_cCOUNT";
38 const char* const CBHM_ERROR = "CBHM_ERROR";
39 const char* const SET_ITEM = "SET_ITEM";
40 const char* const SHOW = "show0";
41 const char* const HIDE = "cbhm_hide";
44 ///////////////////////////////////////////////////////////////////////////////////////////////////
46 ///////////////////////////////////////////////////////////////////////////////////////////////////
61 BaseHandle handle( Clipboard::Get() );
63 if ( !handle && Adaptor::IsAvailable() )
65 Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
67 // The Ecore_X_Window needs to use the Clipboard.
68 // Only when the render surface is window, we can get the Ecore_X_Window.
69 Ecore_X_Window ecoreXwin( 0 );
70 Dali::RenderSurface& surface( adaptorImpl.GetSurface() );
71 if( surface.GetType() == Dali::RenderSurface::WINDOW )
73 ecoreXwin = AnyCast< Ecore_X_Window >( adaptorImpl.GetSurface().GetSurface() );
76 // If we fail to get Ecore_X_Window, we can't use the Clipboard correctly.
77 // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
78 // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
79 Dali::Clipboard clipboard = Dali::Clipboard( new Clipboard( ecoreXwin ) );
80 adaptorImpl.RegisterSingleton( typeid( clipboard ), clipboard );
86 TypeRegistration CLIPBOARD_TYPE( typeid(Dali::Clipboard), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
88 } // unnamed namespace
90 Clipboard::Clipboard( Ecore_X_Window ecoreXwin)
92 mApplicationWindow = ecoreXwin;
95 Clipboard::~Clipboard()
99 Dali::Clipboard Clipboard::Get()
101 Dali::Clipboard clipboard;
103 if ( Adaptor::IsAvailable() )
105 // Check whether the singleton is already created
106 Dali::BaseHandle handle = Adaptor::Get().GetSingleton( typeid( Dali::Clipboard ) );
109 // If so, downcast the handle
110 clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
116 bool Clipboard::SetItem(const std::string &itemData )
118 Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
119 Ecore_X_Atom atomCbhmItem = ecore_x_atom_get( CBHM_ITEM );
120 Ecore_X_Atom dataType = ECORE_X_ATOM_STRING;
122 // Set item (property) to send
123 ecore_x_window_prop_property_set( cbhmWin, atomCbhmItem, dataType, 8, const_cast<char*>( itemData.c_str() ), itemData.length() + 1 );
126 // Trigger sending of item (property)
127 Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
128 ECore::WindowInterface::SendXEvent(ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, SET_ITEM );
133 * Get string at given index of clipboard
135 std::string Clipboard::GetItem( unsigned int index ) // change string to a Dali::Text object.
137 if ( index >= NumberOfItems() )
142 std::string emptyString( "" );
145 snprintf( sendBuf, 20, "%s%d", CBHM_ITEM, index );
146 Ecore_X_Atom xAtomCbhmItem = ecore_x_atom_get( sendBuf );
147 Ecore_X_Atom xAtomItemType = 0;
149 std::string clipboardString( ECore::WindowInterface::GetWindowProperty(xAtomCbhmItem, &xAtomItemType, index ) );
150 if ( !clipboardString.empty() )
152 Ecore_X_Atom xAtomCbhmError = ecore_x_atom_get( CBHM_ERROR );
153 if ( xAtomItemType != xAtomCbhmError )
155 return clipboardString;
162 * Get number of items in clipboard
164 unsigned int Clipboard::NumberOfItems()
166 Ecore_X_Atom xAtomCbhmCountGet = ecore_x_atom_get( CBHM_cCOUNT );
168 std::string ret( ECore::WindowInterface::GetWindowProperty( xAtomCbhmCountGet, NULL, 0 ) );
173 count = atoi( ret.c_str() );
180 * Show clipboard window
181 * Function to send message to show the Clipboard (cbhm) as no direct API available
182 * Reference elementary/src/modules/ctxpopup_copypasteUI/cbhm_helper.c
184 void Clipboard::ShowClipboard()
186 // Claim the ownership of the SECONDARY selection.
187 ecore_x_selection_secondary_set(mApplicationWindow, "", 1);
188 Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
190 // Launch the clipboard window
191 Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
192 ECore::WindowInterface::SendXEvent( ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, SHOW );
195 void Clipboard::HideClipboard()
197 Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
198 // Launch the clipboard window
199 Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
200 ECore::WindowInterface::SendXEvent( ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, HIDE );
202 // release the ownership of SECONDARY selection
203 ecore_x_selection_secondary_clear();
207 } // namespace Adaptor
209 } // namespace Internal