Merge branch 'devel/master (1.1.16)' into tizen
[platform/core/uifw/dali-adaptor.git] / adaptors / x11 / clipboard-impl-x.cpp
1 /*
2  * Copyright (c) 2014 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 "clipboard-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <Ecore_X.h>
23 #include <dali/public-api/object/any.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
28 #include <adaptor-impl.h>
29 #include <ecore-x-window-interface.h>
30 #include <singleton-service-impl.h>
31
32 namespace //unnamed namespace
33 {
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";
42 }
43
44 ///////////////////////////////////////////////////////////////////////////////////////////////////
45 // Clipboard
46 ///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 namespace Dali
49 {
50
51 namespace Internal
52 {
53
54 namespace Adaptor
55 {
56
57 struct Clipboard::Impl
58 {
59   Impl( Ecore_X_Window ecoreXwin )
60   {
61     mApplicationWindow = ecoreXwin;
62   }
63
64   Ecore_X_Window mApplicationWindow;
65 };
66
67 namespace // unnamed namespace
68 {
69
70 BaseHandle Create()
71 {
72   BaseHandle handle( Clipboard::Get() );
73
74   if ( !handle && Adaptor::IsAvailable() )
75   {
76     Dali::SingletonService service( SingletonService::Get() );
77     if ( service )
78     {
79       Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
80       Any nativewindow = adaptorImpl.GetNativeWindowHandle();
81
82       // The Ecore_X_Window needs to use the Clipboard.
83       // Only when the render surface is window, we can get the Ecore_X_Window.
84       Ecore_X_Window ecoreXwin( AnyCast<Ecore_X_Window>(nativewindow) );
85       if (ecoreXwin)
86       {
87         // If we fail to get Ecore_X_Window, we can't use the Clipboard correctly.
88         // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
89         // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
90         Clipboard::Impl* impl( new Clipboard::Impl( ecoreXwin ) );
91         Dali::Clipboard clipboard = Dali::Clipboard( new Clipboard( impl ) );
92         service.Register( typeid( clipboard ), clipboard );
93         handle = clipboard;
94       }
95       else
96       {
97         DALI_LOG_ERROR("Failed to get native window handle");
98       }
99     }
100   }
101
102   return handle;
103 }
104
105 TypeRegistration CLIPBOARD_TYPE( typeid(Dali::Clipboard), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
106
107 } // unnamed namespace
108
109 Clipboard::Clipboard(Impl* impl)
110 : mImpl( impl )
111 {
112 }
113
114 Clipboard::~Clipboard()
115 {
116   delete mImpl;
117 }
118
119 Dali::Clipboard Clipboard::Get()
120 {
121   Dali::Clipboard clipboard;
122
123   Dali::SingletonService service( SingletonService::Get() );
124   if ( service )
125   {
126     // Check whether the singleton is already created
127     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
128     if(handle)
129     {
130       // If so, downcast the handle
131       clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
132     }
133   }
134
135   return clipboard;
136 }
137 bool Clipboard::SetItem(const std::string &itemData )
138 {
139   Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
140   Ecore_X_Atom atomCbhmItem = ecore_x_atom_get( CBHM_ITEM );
141   Ecore_X_Atom dataType = ECORE_X_ATOM_STRING;
142
143   // Set item (property) to send
144   ecore_x_window_prop_property_set( cbhmWin, atomCbhmItem, dataType, 8, const_cast<char*>( itemData.c_str() ), itemData.length() + 1 );
145   ecore_x_sync();
146
147   // Trigger sending of item (property)
148   Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
149   ECore::WindowInterface::SendXEvent(ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, SET_ITEM );
150   return true;
151 }
152
153 /*
154  * Get string at given index of clipboard
155  */
156 std::string Clipboard::GetItem( unsigned int index )  // change string to a Dali::Text object.
157 {
158   if ( index >= NumberOfItems() )
159   {
160     return "";
161   }
162
163   std::string emptyString( "" );
164   char sendBuf[20];
165
166   snprintf( sendBuf, 20,  "%s%d", CBHM_ITEM, index );
167   Ecore_X_Atom xAtomCbhmItem = ecore_x_atom_get( sendBuf );
168   Ecore_X_Atom xAtomItemType = 0;
169
170   std::string clipboardString( ECore::WindowInterface::GetWindowProperty(xAtomCbhmItem, &xAtomItemType, index ) );
171   if ( !clipboardString.empty() )
172   {
173     Ecore_X_Atom xAtomCbhmError = ecore_x_atom_get( CBHM_ERROR );
174     if ( xAtomItemType != xAtomCbhmError )
175     {
176       return clipboardString;
177     }
178    }
179    return emptyString;
180 }
181
182 /*
183  * Get number of items in clipboard
184  */
185 unsigned int Clipboard::NumberOfItems()
186 {
187   Ecore_X_Atom xAtomCbhmCountGet = ecore_x_atom_get( CBHM_cCOUNT );
188
189   std::string ret( ECore::WindowInterface::GetWindowProperty( xAtomCbhmCountGet, NULL, 0 ) );
190   int count = 0;
191
192   if ( !ret.empty() )
193   {
194     count = atoi( ret.c_str() );
195   }
196
197   return count;
198 }
199
200 /**
201  * Show clipboard window
202  * Function to send message to show the Clipboard (cbhm) as no direct API available
203  * Reference elementary/src/modules/ctxpopup_copypasteUI/cbhm_helper.c
204  */
205 void Clipboard::ShowClipboard()
206 {
207   // Claim the ownership of the SECONDARY selection.
208   ecore_x_selection_secondary_set(mImpl->mApplicationWindow, "", 1);
209   Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
210
211   // Launch the clipboard window
212   Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
213   ECore::WindowInterface::SendXEvent( ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, SHOW );
214 }
215
216 void Clipboard::HideClipboard()
217 {
218   Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
219   // Launch the clipboard window
220   Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
221   ECore::WindowInterface::SendXEvent( ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, HIDE );
222
223   // release the ownership of SECONDARY selection
224   ecore_x_selection_secondary_clear();
225 }
226
227
228 } // namespace Adaptor
229
230 } // namespace Internal
231
232 } // namespace Dali