[3.0] Add ivi profile
[platform/core/uifw/dali-adaptor.git] / adaptors / x11 / clipboard-impl-x.cpp
1 /*
2  * Copyright (c) 2016 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 Clipboard::Clipboard(Impl* impl)
68 : mImpl( impl )
69 {
70 }
71
72 Clipboard::~Clipboard()
73 {
74   delete mImpl;
75 }
76
77 Dali::Clipboard Clipboard::Get()
78 {
79   Dali::Clipboard clipboard;
80
81   Dali::SingletonService service( SingletonService::Get() );
82   if ( service )
83   {
84     // Check whether the singleton is already created
85     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
86     if(handle)
87     {
88       // If so, downcast the handle
89       clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
90     }
91     else
92     {
93       Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
94       Any nativewindow = adaptorImpl.GetNativeWindowHandle();
95
96       // The Ecore_X_Window needs to use the Clipboard.
97       // Only when the render surface is window, we can get the Ecore_X_Window.
98       Ecore_X_Window ecoreXwin( AnyCast<Ecore_X_Window>(nativewindow) );
99       if (ecoreXwin)
100       {
101         // If we fail to get Ecore_X_Window, we can't use the Clipboard correctly.
102         // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
103         // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
104         Clipboard::Impl* impl( new Clipboard::Impl( ecoreXwin ) );
105         clipboard = Dali::Clipboard( new Clipboard( impl ) );
106         service.Register( typeid( clipboard ), clipboard );
107       }
108     }
109   }
110
111   return clipboard;
112 }
113 bool Clipboard::SetItem(const std::string &itemData )
114 {
115   Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
116   Ecore_X_Atom atomCbhmItem = ecore_x_atom_get( CBHM_ITEM );
117   Ecore_X_Atom dataType = ECORE_X_ATOM_STRING;
118
119   // Set item (property) to send
120   ecore_x_window_prop_property_set( cbhmWin, atomCbhmItem, dataType, 8, const_cast<char*>( itemData.c_str() ), itemData.length() + 1 );
121   ecore_x_sync();
122
123   // Trigger sending of item (property)
124   Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
125   ECore::WindowInterface::SendXEvent(ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, SET_ITEM );
126   return true;
127 }
128
129 /*
130  * Get string at given index of clipboard
131  */
132 std::string Clipboard::GetItem( unsigned int index )  // change string to a Dali::Text object.
133 {
134   if ( index >= NumberOfItems() )
135   {
136     return "";
137   }
138
139   char sendBuf[20];
140   snprintf( sendBuf, 20,  "%s%d", CBHM_ITEM, index );
141   Ecore_X_Atom xAtomCbhmItem = ecore_x_atom_get( sendBuf );
142   Ecore_X_Atom xAtomItemType = 0;
143
144   std::string clipboardString( ECore::WindowInterface::GetWindowProperty(xAtomCbhmItem, &xAtomItemType, index ) );
145
146   // Only return the text string if the Atom type is text (Do not return a text string/URL for images).
147   if( !clipboardString.empty() &&
148       ( xAtomItemType == ECORE_X_ATOM_TEXT || xAtomItemType == ECORE_X_ATOM_COMPOUND_TEXT || xAtomItemType == ECORE_X_ATOM_STRING || xAtomItemType == ECORE_X_ATOM_UTF8_STRING ) )
149   {
150     Ecore_X_Atom xAtomCbhmError = ecore_x_atom_get( CBHM_ERROR );
151     if ( xAtomItemType != xAtomCbhmError )
152     {
153       return clipboardString;
154     }
155   }
156   return "";
157 }
158
159 /*
160  * Get number of items in clipboard
161  */
162 unsigned int Clipboard::NumberOfItems()
163 {
164   Ecore_X_Atom xAtomCbhmCountGet = ecore_x_atom_get( CBHM_cCOUNT );
165
166   std::string ret( ECore::WindowInterface::GetWindowProperty( xAtomCbhmCountGet, NULL, 0 ) );
167   int count = 0;
168
169   if ( !ret.empty() )
170   {
171     count = atoi( ret.c_str() );
172   }
173
174   return count;
175 }
176
177 /**
178  * Show clipboard window
179  * Function to send message to show the Clipboard (cbhm) as no direct API available
180  * Reference elementary/src/modules/ctxpopup_copypasteUI/cbhm_helper.c
181  */
182 void Clipboard::ShowClipboard()
183 {
184   // Claim the ownership of the SECONDARY selection.
185   ecore_x_selection_secondary_set(mImpl->mApplicationWindow, "", 1);
186   Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
187
188   // Launch the clipboard window
189   Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
190   ECore::WindowInterface::SendXEvent( ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, SHOW );
191 }
192
193 void Clipboard::HideClipboard()
194 {
195   Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
196   // Launch the clipboard window
197   Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
198   ECore::WindowInterface::SendXEvent( ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, HIDE );
199
200   // release the ownership of SECONDARY selection
201   ecore_x_selection_secondary_clear();
202 }
203
204
205 } // namespace Adaptor
206
207 } // namespace Internal
208
209 } // namespace Dali