2 * Copyright (c) 2016 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 <pepper-dali/internal/compositor-impl.h>
22 #include <dali/integration-api/debug.h>
23 #include <wayland-server.h>
24 #include <tizen-extension-client-protocol.h>
25 #include <Ecore_Wayland.h>
39 #if defined(DEBUG_ENABLED)
40 Integration::Log::Filter* gPepperCompositorLogging = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_PEPPER_COMPOSITOR" );
43 static void EmbeddedCompositorGetSocket( void* data, struct tizen_embedded_compositor* embeddedCompositor, int fd )
45 int* socketFd = (int*)data;
51 static const struct tizen_embedded_compositor_listener tizenEmbeddedCompositorListener =
53 EmbeddedCompositorGetSocket
56 } // unnamed namespace
58 CompositorPtr Compositor::New( Application application, const std::string& name )
60 CompositorPtr impl = new Compositor();
62 // Second-phase init of the implementation
63 impl->Initialize( application, name );
68 Compositor::Compositor()
69 : mCompositor( NULL ),
81 Compositor::~Compositor()
85 wayland_tbm_server_deinit( mTbmServer );
90 pepper_compositor_destroy( mCompositor );
95 ecore_main_fd_handler_del( mFdHandler );
99 void* Compositor::GetCompositorHandle()
101 return static_cast< void* >( mCompositor );
104 void Compositor::Initialize( Application application, const std::string& name )
106 // Get socket fd from server
107 int socketFd = GetSocketFdFromServer();
110 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: socket fd is invalid.\n" );
115 mCompositor = pepper_compositor_create_fd( name.c_str(), socketFd );
118 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: pepper_compositor_create_fd is failed. [%d]\n", socketFd );
122 mDisplay = pepper_compositor_get_display( mCompositor );
125 mShell = Pepper::Shell::New( Pepper::Compositor( this ) );
128 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: Fail to create shell.\n" );
129 pepper_compositor_destroy( mCompositor );
134 mTbmServer = wayland_tbm_server_init( mDisplay, NULL, -1, 0 );
137 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: wayland_tbm_server_init is failed.\n" );
138 pepper_compositor_destroy( mCompositor );
144 mInput = Pepper::Input::New( Pepper::Compositor( this ) );
147 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: Fail to create input.\n" );
148 wayland_tbm_server_deinit( mTbmServer );
150 pepper_compositor_destroy( mCompositor );
155 mSocketName = pepper_compositor_get_socket_name( mCompositor );
157 mEventLoop = wl_display_get_event_loop( mDisplay );
159 int loopFd = wl_event_loop_get_fd( mEventLoop );
161 mFdHandler = ecore_main_fd_handler_add( loopFd, (Ecore_Fd_Handler_Flags)(ECORE_FD_READ | ECORE_FD_ERROR), Compositor::FdReadCallback, this, NULL, NULL );
163 ecore_main_fd_handler_prepare_callback_set( mFdHandler, Compositor::FdPrepareCallback, this );
166 mOutput = Pepper::Output::New( Pepper::Compositor( this ), application, mInput );
169 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::Initialize: Fail to create output.\n" );
171 ecore_main_fd_handler_del( mFdHandler );
173 wayland_tbm_server_deinit( mTbmServer );
175 pepper_compositor_destroy( mCompositor );
180 mOutput.ObjectViewAddedSignal().Connect( this, &Compositor::OnObjectViewAdded );
181 mOutput.ObjectViewDeletedSignal().Connect( this, &Compositor::OnObjectViewDeleted );
183 DALI_LOG_INFO( gPepperCompositorLogging, Debug::Verbose, "Compositor::Initialize: success. socket name = %s\n", mSocketName.c_str() );
186 setenv("WAYLAND_DISPLAY", mSocketName.c_str(), 1);
189 int Compositor::GetSocketFdFromServer()
193 Ecore_Wl_Global* global;
194 struct tizen_embedded_compositor* embeddedCompositor = NULL;
197 list = ecore_wl_globals_get();
200 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::GetSocketFdFromServer: ecore_wl_globals_get returns NULL.\n" );
204 EINA_INLIST_FOREACH_SAFE( list, tmp, global )
206 if( !strcmp( global->interface, "tizen_embedded_compositor" ) )
208 embeddedCompositor = static_cast< struct tizen_embedded_compositor* >( wl_registry_bind( ecore_wl_registry_get(), global->id, &tizen_embedded_compositor_interface, 1 ) );
210 tizen_embedded_compositor_add_listener( embeddedCompositor, &tizenEmbeddedCompositorListener, &fd );
215 if( !embeddedCompositor )
217 DALI_LOG_INFO( gPepperCompositorLogging, Debug::General, "Compositor::GetSocketFdFromServer: tizen_embedded_compositor is not supported.\n" );
221 tizen_embedded_compositor_get_socket( embeddedCompositor );
224 tizen_embedded_compositor_destroy( embeddedCompositor );
229 Pepper::Compositor::CompositorSignalType& Compositor::ObjectViewAddedSignal()
231 return mObjectViewAddedSignal;
234 Pepper::Compositor::CompositorSignalType& Compositor::ObjectViewDeletedSignal()
236 return mObjectViewDeletedSignal;
239 void Compositor::OnObjectViewAdded( Pepper::Output output, Pepper::ObjectView objectView )
241 Pepper::Compositor handle( this );
242 mObjectViewAddedSignal.Emit( handle, objectView );
244 DALI_LOG_INFO( gPepperCompositorLogging, Debug::Verbose, "Compositor::ObjectViewAddedSignal: ObjectView is added!\n" );
247 void Compositor::OnObjectViewDeleted( Pepper::Output output, Pepper::ObjectView objectView )
249 Pepper::Compositor handle( this );
250 mObjectViewDeletedSignal.Emit( handle, objectView );
252 DALI_LOG_INFO( gPepperCompositorLogging, Debug::Verbose, "Compositor::ObjectViewDeletedSignal: ObjectView is deleted!\n" );
255 Eina_Bool Compositor::FdReadCallback( void* data, Ecore_Fd_Handler* handler )
257 Compositor* compositor = static_cast< Compositor* >( data );
259 wl_event_loop_dispatch( compositor->mEventLoop, 0 );
261 return ECORE_CALLBACK_RENEW;
264 void Compositor::FdPrepareCallback( void* data, Ecore_Fd_Handler* handler )
266 Compositor* compositor = static_cast< Compositor* >( data );
268 wl_display_flush_clients( compositor->mDisplay );
271 } // namespace Internal
273 } // namespace Pepper