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