Implement WatchApplication class
[platform/core/uifw/dali-adaptor.git] / adaptors / common / application-impl.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 "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& stylesheet,
54   Dali::Application::WINDOW_MODE windowMode,
55   Framework::Type applicationType)
56 {
57   ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, applicationType ) );
58   return application;
59 }
60
61 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
62   Dali::Application::WINDOW_MODE windowMode, Framework::Type applicationType )
63 : mInitSignal(),
64   mTerminateSignal(),
65   mPauseSignal(),
66   mResumeSignal(),
67   mResetSignal(),
68   mResizeSignal(),
69   mAppControlSignal(),
70   mLanguageChangedSignal(),
71   mRegionChangedSignal(),
72   mBatteryLowSignal(),
73   mMemoryLowSignal(),
74   mEventLoop( NULL ),
75   mFramework( NULL ),
76   mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
77   mCommandLineOptions( NULL ),
78   mSingletonService( SingletonService::New() ),
79   mAdaptor( NULL ),
80   mWindow(),
81   mWindowMode( windowMode ),
82   mName(),
83   mStylesheet( stylesheet ),
84   mEnvironmentOptions(),
85   mSlotDelegate( this )
86 {
87   // Get mName from environment options
88   mName = mEnvironmentOptions.GetWindowName();
89   if( mName.empty() && argc && ( *argc > 0 ) )
90   {
91     // Set mName from command-line args if environment option not set
92     mName = (*argv)[0];
93   }
94
95   mCommandLineOptions = new CommandLineOptions(argc, argv);
96   mFramework = new Framework( *this, argc, argv, applicationType );
97 }
98
99 Application::~Application()
100 {
101   mSingletonService.UnregisterAll();
102
103   delete mFramework;
104   delete mCommandLineOptions;
105   delete mAdaptor;
106   mWindow.Reset();
107 }
108
109 void Application::CreateWindow()
110 {
111   PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
112
113   if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
114   {
115     // Command line options override environment options and full screen
116     windowPosition = PositionSize( 0, 0, mCommandLineOptions->stageWidth, mCommandLineOptions->stageHeight );
117   }
118   else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
119   {
120     // Environment options override full screen functionality if command line arguments not provided
121     windowPosition = PositionSize( 0, 0, mEnvironmentOptions.GetWindowWidth(), mEnvironmentOptions.GetWindowHeight() );
122   }
123
124   const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
125   mWindow = Dali::Window::New( windowPosition, mName, windowClassName, mWindowMode == Dali::Application::TRANSPARENT );
126
127   // Quit the application when the window is closed
128   GetImplementation( mWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
129 }
130
131 void Application::CreateAdaptor()
132 {
133   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
134
135   mAdaptor = Dali::Internal::Adaptor::Adaptor::New( mWindow, mContextLossConfiguration, &mEnvironmentOptions );
136
137   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
138 }
139
140 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
141 {
142   mContextLossConfiguration = configuration;
143
144   // Run the application
145   mFramework->Run();
146 }
147
148 void Application::Lower()
149 {
150   // Lower the application without quitting it.
151   mWindow.Lower();
152 }
153
154 void Application::Quit()
155 {
156   // Actually quit the application.
157   AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
158 }
159
160 void Application::QuitFromMainLoop()
161 {
162   mAdaptor->Stop();
163
164   mFramework->Quit();
165   // This will trigger OnTerminate(), below, after the main loop has completed.
166 }
167
168 void Application::OnInit()
169 {
170   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
171
172   CreateWindow();
173   CreateAdaptor();
174
175   // Run the adaptor
176   mAdaptor->Start();
177
178   // Check if user requires no vsyncing and set on X11 Adaptor
179   if (mCommandLineOptions->noVSyncOnRender)
180   {
181     mAdaptor->SetUseHardwareVSync(false);
182   }
183
184   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
185   if( mCommandLineOptions->viewMode != 0 )
186   {
187     ViewMode viewMode = MONO;
188     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
189     {
190       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
191     }
192     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
193   }
194
195   if( ! mStylesheet.empty() )
196   {
197     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
198   }
199
200   // Wire up the LifecycleController
201   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
202
203   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
204   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
205   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
206   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
207   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
208   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
209   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
210
211   Dali::Application application(this);
212   mInitSignal.Emit( application );
213
214   mAdaptor->NotifySceneCreated();
215 }
216
217 void Application::OnTerminate()
218 {
219   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
220   // delete the window as ecore_x has been destroyed by AppCore
221
222   Dali::Application application(this);
223   mTerminateSignal.Emit( application );
224
225   if( mAdaptor )
226   {
227     // Ensure that the render-thread is not using the surface(window) after we delete it
228     mAdaptor->Stop();
229   }
230
231   mWindow.Reset();
232 }
233
234 void Application::OnPause()
235 {
236   mAdaptor->Pause();
237   Dali::Application application(this);
238   mPauseSignal.Emit( application );
239 }
240
241 void Application::OnResume()
242 {
243   // Emit the signal first so the application can queue any messages before we do an update/render
244   // This ensures we do not just redraw the last frame before pausing if that's not required
245   Dali::Application application(this);
246   mResumeSignal.Emit( application );
247   mAdaptor->Resume();
248 }
249
250 void Application::OnReset()
251 {
252   /*
253    * usually, reset callback was called when a caller request to launch this application via aul.
254    * because Application class already handled initialization in OnInit(), OnReset do nothing.
255    */
256   Dali::Application application(this);
257   mResetSignal.Emit( application );
258 }
259
260 void Application::OnAppControl(void *data)
261 {
262   Dali::Application application(this);
263   mAppControlSignal.Emit( application , data );
264 }
265
266 void Application::OnLanguageChanged()
267 {
268   mAdaptor->NotifyLanguageChanged();
269   Dali::Application application(this);
270   mLanguageChangedSignal.Emit( application );
271 }
272
273 void Application::OnRegionChanged()
274 {
275   Dali::Application application(this);
276   mRegionChangedSignal.Emit( application );
277 }
278
279 void Application::OnBatteryLow()
280 {
281   Dali::Application application(this);
282   mBatteryLowSignal.Emit( application );
283 }
284
285 void Application::OnMemoryLow()
286 {
287   Dali::Application application(this);
288   mMemoryLowSignal.Emit( application );
289 }
290
291 void Application::OnResize(Dali::Adaptor& adaptor)
292 {
293   Dali::Application application(this);
294   mResizeSignal.Emit( application );
295 }
296
297 bool Application::AddIdle( CallbackBase* callback )
298 {
299   return mAdaptor->AddIdle( callback );
300 }
301
302 Dali::Adaptor& Application::GetAdaptor()
303 {
304   return *mAdaptor;
305 }
306
307 Dali::Window Application::GetWindow()
308 {
309   return mWindow;
310 }
311
312 // Stereoscopy
313
314 void Application::SetViewMode( ViewMode viewMode )
315 {
316   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
317 }
318
319 ViewMode Application::GetViewMode() const
320 {
321   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
322 }
323
324 void Application::SetStereoBase( float stereoBase )
325 {
326   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
327 }
328
329 float Application::GetStereoBase() const
330 {
331   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
332 }
333
334
335 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
336 {
337   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
338   Window& windowImpl = GetImplementation(newWindow);
339   windowImpl.SetAdaptor(*mAdaptor);
340   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
341   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
342
343   Any nativeWindow = newWindow.GetNativeHandle();
344   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
345   mWindow = newWindow;
346 }
347
348 } // namespace Adaptor
349
350 } // namespace Internal
351
352 } // namespace Dali