[4.0] Create Widget Application
[platform/core/uifw/dali-adaptor.git] / adaptors / common / widget-application-impl.cpp
1 /*
2  * Copyright (c) 2017 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 "widget-application-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <widget_base.h>
23
24 // INTERNAL INCLUDES
25 #include <style-monitor.h>
26 #include <command-line-options.h>
27 #include <common/adaptor-impl.h>
28 #include <common/framework.h>
29 #include <singleton-service-impl.h>
30 #include <window-impl.h>
31
32 namespace Dali
33 {
34
35 namespace TizenPlatform
36 {
37 class TizenPlatformAbstraction;
38 }
39
40 namespace Integration
41 {
42 class Core;
43 }
44
45 namespace Internal
46 {
47
48 namespace Adaptor
49 {
50
51 WidgetApplicationPtr WidgetApplication::New(
52   int* argc,
53   char **argv[],
54   const std::string& stylesheet)
55 {
56   WidgetApplicationPtr widgetApplication( new WidgetApplication (argc, argv, stylesheet ) );
57   return widgetApplication;
58 }
59
60 WidgetApplication::WidgetApplication( int* argc, char** argv[], const std::string& stylesheet )
61 : mInitSignal(),
62   mTerminateSignal(),
63   mLanguageChangedSignal(),
64   mRegionChangedSignal(),
65   mLowBatterySignal(),
66   mLowMemorySignal(),
67   mFramework( NULL ),
68   mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
69   mCommandLineOptions( NULL ),
70   mSingletonService( SingletonService::New() ),
71   mAdaptor( NULL ),
72   mName(),
73   mStylesheet( stylesheet ),
74   mEnvironmentOptions(),
75   mSlotDelegate( this )
76 {
77   // Get mName from environment options
78   mName = mEnvironmentOptions.GetWindowName();
79   if( mName.empty() && argc && ( *argc > 0 ) )
80   {
81     // Set mName from command-line args if environment option not set
82     mName = (*argv)[0];
83   }
84
85   mCommandLineOptions = new CommandLineOptions(argc, argv);
86   mFramework = new Framework( *this, argc, argv, Framework::WIDGET );
87 }
88
89 WidgetApplication::~WidgetApplication()
90 {
91   mSingletonService.UnregisterAll();
92
93   delete mAdaptor;
94   delete mCommandLineOptions;
95   delete mFramework;
96 }
97
98 void WidgetApplication::CreateAdaptor()
99 {
100   mAdaptor = Dali::Internal::Adaptor::Adaptor::New( mWindow, mContextLossConfiguration, &mEnvironmentOptions );
101
102   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( true );
103 }
104
105 void WidgetApplication::MainLoop()
106 {
107   // Run the application
108   mFramework->Run();
109 }
110
111 void WidgetApplication::Quit()
112 {
113   // Actually quit the application.
114   AddIdle( MakeCallback( this, &WidgetApplication::QuitFromMainLoop ) );
115 }
116
117 void WidgetApplication::QuitFromMainLoop()
118 {
119   mAdaptor->Stop();
120
121   mFramework->Quit();
122   // This will trigger OnTerminate(), below, after the main loop has completed.
123 }
124
125 void WidgetApplication::DoInit()
126 {
127   mWindow = Dali::Window::New( PositionSize(), "", mEnvironmentOptions.GetWindowClassName(), 1 );
128   // Quit the application when the window is closed
129   mWindow.ShowIndicator(Dali::Window::IndicatorVisibleMode::INVISIBLE);
130   GetImplementation( mWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &WidgetApplication::Quit );
131
132   CreateAdaptor();
133   // Run the adaptor
134   mAdaptor->Start();
135
136   // Check if user requires no vsyncing and set Adaptor
137   if (mCommandLineOptions->noVSyncOnRender)
138   {
139     mAdaptor->SetUseHardwareVSync(false);
140   }
141
142   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
143
144   if( ! mStylesheet.empty() )
145   {
146     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
147   }
148 }
149
150 void WidgetApplication::DoStart()
151 {
152   mAdaptor->NotifySceneCreated();
153 }
154
155 void WidgetApplication::DoTerminate()
156 {
157   if( mAdaptor )
158   {
159     // Ensure that the render-thread is not using the surface(window) after we delete it
160     mAdaptor->Stop();
161   }
162
163   mWindow.Reset();
164 }
165
166 void WidgetApplication::DoLanguageChange()
167 {
168   mAdaptor->NotifyLanguageChanged();
169 }
170
171 void WidgetApplication::OnInit()
172 {
173   mFramework->AddAbortCallback( MakeCallback( this, &WidgetApplication::QuitFromMainLoop ) );
174   DoInit();
175
176   Dali::WidgetApplication widgetApplication(this);
177   mInitSignal.Emit( widgetApplication );
178
179   DoStart();
180 }
181
182 void WidgetApplication::OnTerminate()
183 {
184   Dali::WidgetApplication widgetApplication(this);
185   mTerminateSignal.Emit( widgetApplication );
186
187   DoTerminate();
188 }
189
190 void WidgetApplication::OnLanguageChanged()
191 {
192   DoLanguageChange();
193   Dali::WidgetApplication widgetApplication(this);
194   mLanguageChangedSignal.Emit( widgetApplication );
195 }
196
197 void WidgetApplication::OnRegionChanged()
198 {
199   Dali::WidgetApplication widgetApplication(this);
200   mRegionChangedSignal.Emit( widgetApplication );
201 }
202
203 void WidgetApplication::OnBatteryLow( Dali::DeviceStatus::Battery::Status status )
204 {
205   Dali::WidgetApplication widgetApplication(this);
206   mLowBatterySignal.Emit( status );
207 }
208
209 void WidgetApplication::OnMemoryLow( Dali::DeviceStatus::Memory::Status status )
210 {
211   Dali::WidgetApplication widgetApplication(this);
212   mLowMemorySignal.Emit( status );
213 }
214
215 bool WidgetApplication::AddIdle( CallbackBase* callback )
216 {
217   return mAdaptor->AddIdle( callback );
218 }
219
220 Dali::Adaptor& WidgetApplication::GetAdaptor()
221 {
222   return *mAdaptor;
223 }
224
225 std::string WidgetApplication::GetResourcePath()
226 {
227   return Internal::Adaptor::Framework::GetResourcePath();
228 }
229
230 std::string WidgetApplication::GetRegion()
231 {
232   return mFramework->GetRegion();
233 }
234
235 std::string WidgetApplication::GetLanguage()
236 {
237   return mFramework->GetLanguage();
238 }
239
240 } // namespace Adaptor
241
242 } // namespace Internal
243
244 } // namespace Dali