Merge "Emit language changed signal" into tizen
[platform/core/uifw/dali-adaptor.git] / adaptors / common / application-impl.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 "application-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <style-monitor.h>
26 #include <command-line-options.h>
27 #include <common/adaptor-impl.h>
28 #include <singleton-service-impl.h>
29 #include <lifecycle-controller-impl.h>
30
31 namespace Dali
32 {
33
34 namespace TizenPlatform
35 {
36 class TizenPlatformAbstraction;
37 }
38
39 namespace Integration
40 {
41 class Core;
42 }
43
44 namespace Internal
45 {
46
47 namespace Adaptor
48 {
49
50 ApplicationPtr Application::New(
51   int* argc,
52   char **argv[],
53   const std::string& name,
54   const DeviceLayout& baseLayout,
55   Dali::Application::WINDOW_MODE windowMode)
56 {
57   ApplicationPtr application ( new Application (argc, argv, name, baseLayout, windowMode ) );
58   return application;
59 }
60
61 Application::Application( int* argc, char** argv[], const std::string& name, const DeviceLayout& baseLayout, Dali::Application::WINDOW_MODE windowMode)
62 : mInitSignal(),
63   mTerminateSignal(),
64   mPauseSignal(),
65   mResumeSignal(),
66   mResetSignal(),
67   mResizeSignal(),
68   mAppControlSignal(),
69   mLanguageChangedSignal(),
70   mRegionChangedSignal(),
71   mBatteryLowSignal(),
72   mMemoryLowSignal(),
73   mEventLoop( NULL ),
74   mFramework( NULL ),
75   mCommandLineOptions( NULL ),
76   mSingletonService( SingletonService::New() ),
77   mAdaptor( NULL ),
78   mWindow(),
79   mWindowMode( windowMode ),
80   mName( name ),
81   mInitialized( false ),
82   mBaseLayout( baseLayout ),
83   mSlotDelegate( this )
84 {
85   mCommandLineOptions = new CommandLineOptions(argc, argv);
86
87   mFramework = new Framework(*this, argc, argv, name);
88 }
89
90 Application::~Application()
91 {
92   mSingletonService.UnregisterAll();
93
94   delete mFramework;
95   delete mCommandLineOptions;
96   delete mAdaptor;
97   mWindow.Reset();
98 }
99
100 void Application::CreateWindow()
101 {
102   PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
103
104   if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
105   {
106     // let the command line options over ride
107     windowPosition = PositionSize( 0, 0, mCommandLineOptions->stageWidth, mCommandLineOptions->stageHeight );
108   }
109
110   mWindow = Dali::Window::New( windowPosition, mName, mWindowMode == Dali::Application::TRANSPARENT );
111 }
112
113 void Application::CreateAdaptor()
114 {
115   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
116
117   mAdaptor = &Dali::Adaptor::New( mWindow, mBaseLayout, mContextLossConfiguration );
118
119   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
120 }
121
122 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
123 {
124   mContextLossConfiguration = configuration;
125
126   // Run the application
127   mFramework->Run();
128 }
129
130 void Application::Lower()
131 {
132   // Lower the application without quitting it.
133   mWindow.Lower();
134 }
135
136 void Application::Quit()
137 {
138   // Actually quit the application.
139   AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
140 }
141
142 void Application::QuitFromMainLoop()
143 {
144   mAdaptor->Stop();
145
146   Dali::Application application(this);
147   mTerminateSignal.Emit( application );
148
149   mFramework->Quit();
150   // This will trigger OnTerminate(), below, after the main loop has completed.
151   mInitialized = false;
152 }
153
154 void Application::OnInit()
155 {
156   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
157
158   CreateWindow();
159   CreateAdaptor();
160
161   // Run the adaptor
162   mAdaptor->Start();
163
164   // Check if user requires no vsyncing and set on X11 Adaptor
165   if (mCommandLineOptions->noVSyncOnRender)
166   {
167     mAdaptor->SetUseHardwareVSync(false);
168   }
169
170   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
171   if( mCommandLineOptions->viewMode != 0 )
172   {
173     ViewMode viewMode = MONO;
174     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
175     {
176       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
177     }
178     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
179   }
180
181   mInitialized = true;
182
183   // Wire up the LifecycleController
184   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
185
186   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
187   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
188   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
189   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
190   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
191   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
192   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
193
194   Dali::Application application(this);
195   mInitSignal.Emit( application );
196
197   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetCore().SceneCreated();
198 }
199
200 void Application::OnTerminate()
201 {
202   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
203   // delete the window as ecore_x has been destroyed by AppCore
204
205   mWindow.Reset();
206   mInitialized = false;
207 }
208
209 void Application::OnPause()
210 {
211   mAdaptor->Pause();
212   Dali::Application application(this);
213   mPauseSignal.Emit( application );
214 }
215
216 void Application::OnResume()
217 {
218   mAdaptor->Resume();
219   Dali::Application application(this);
220   mResumeSignal.Emit( application );
221 }
222
223 void Application::OnReset()
224 {
225   /*
226    * usually, reset callback was called when a caller request to launch this application via aul.
227    * because Application class already handled initialization in OnInit(), OnReset do nothing.
228    */
229   Dali::Application application(this);
230   mResetSignal.Emit( application );
231
232   mWindow.Raise();
233 }
234
235 void Application::OnAppControl(void *data)
236 {
237   Dali::Application application(this);
238   mAppControlSignal.Emit( application , data );
239 }
240
241 void Application::OnLanguageChanged()
242 {
243   mAdaptor->NotifyLanguageChanged();
244   Dali::Application application(this);
245   mLanguageChangedSignal.Emit( application );
246 }
247
248 void Application::OnRegionChanged()
249 {
250   Dali::Application application(this);
251   mRegionChangedSignal.Emit( application );
252 }
253
254 void Application::OnBatteryLow()
255 {
256   Dali::Application application(this);
257   mBatteryLowSignal.Emit( application );
258 }
259
260 void Application::OnMemoryLow()
261 {
262   Dali::Application application(this);
263   mMemoryLowSignal.Emit( application );
264 }
265
266 void Application::OnResize(Dali::Adaptor& adaptor)
267 {
268   Dali::Application application(this);
269   mResizeSignal.Emit( application );
270 }
271
272 bool Application::AddIdle( CallbackBase* callback )
273 {
274   return mAdaptor->AddIdle( callback );
275 }
276
277 Dali::Adaptor& Application::GetAdaptor()
278 {
279   return *mAdaptor;
280 }
281
282 Dali::Window Application::GetWindow()
283 {
284   return mWindow;
285 }
286
287 const std::string& Application::GetTheme()
288 {
289   return Dali::StyleMonitor::Get().GetTheme();
290 }
291
292 void Application::SetTheme(const std::string& themeFilePath)
293 {
294   return Dali::StyleMonitor::Get().SetTheme(themeFilePath);
295 }
296
297 // Stereoscopy
298
299 void Application::SetViewMode( ViewMode viewMode )
300 {
301   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
302 }
303
304 ViewMode Application::GetViewMode() const
305 {
306   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
307 }
308
309 void Application::SetStereoBase( float stereoBase )
310 {
311   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
312 }
313
314 float Application::GetStereoBase() const
315 {
316   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
317 }
318
319
320 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
321 {
322   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
323   Window& windowImpl = GetImplementation(newWindow);
324   windowImpl.SetAdaptor(*mAdaptor);
325   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
326   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
327
328   Any nativeWindow = newWindow.GetNativeHandle();
329   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
330   mWindow = newWindow;
331 }
332
333 } // namespace Adaptor
334
335 } // namespace Internal
336
337 } // namespace Dali