2 * Copyright (c) 2021 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 <dali/internal/adaptor/common/application-impl.h>
22 #include <dali/devel-api/common/singleton-service.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/object/object-registry.h>
27 #include <dali/devel-api/adaptor-framework/accessibility-impl.h>
28 #include <dali/devel-api/adaptor-framework/style-monitor.h>
29 #include <dali/devel-api/text-abstraction/font-client.h>
30 #include <dali/internal/adaptor/common/adaptor-impl.h>
31 #include <dali/internal/adaptor/common/framework.h>
32 #include <dali/internal/adaptor/common/lifecycle-controller-impl.h>
33 #include <dali/internal/system/common/command-line-options.h>
34 #include <dali/internal/window-system/common/render-surface-factory.h>
35 #include <dali/internal/window-system/common/window-impl.h>
36 #include <dali/internal/window-system/common/window-render-surface.h>
37 #include <dali/internal/window-system/common/window-system.h>
39 // To disable a macro with the same name from one of OpenGL headers
44 namespace TizenPlatform
46 class TizenPlatformAbstraction;
58 ApplicationPtr Application::gPreInitializedApplication(NULL);
60 ApplicationPtr Application::New(
63 const std::string& stylesheet,
64 Dali::Application::WINDOW_MODE windowMode,
65 const PositionSize& positionSize,
66 Framework::Type applicationType,
69 ApplicationPtr application(new Application(argc, argv, stylesheet, windowMode, positionSize, applicationType, type));
73 void Application::PreInitialize(int* argc, char** argv[])
75 if(!gPreInitializedApplication)
77 Dali::TextAbstraction::FontClientPreInitialize();
79 gPreInitializedApplication = new Application(argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL, WindowType::NORMAL);
80 gPreInitializedApplication->CreateWindow(); // Only create window
81 gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
85 Application::Application(int* argc, char** argv[], const std::string& stylesheet, Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType, WindowType type)
92 mLanguageChangedSignal(),
93 mRegionChangedSignal(),
96 mCommandLineOptions(nullptr),
97 mAdaptorBuilder(nullptr),
100 mMainWindowMode(windowMode),
102 mStylesheet(stylesheet),
103 mEnvironmentOptions(),
104 mWindowPositionSize(positionSize),
105 mLaunchpadState(Launchpad::NONE),
106 mDefaultWindowType(type),
109 // Get mName from environment options
110 mMainWindowName = mEnvironmentOptions.GetWindowName();
111 if(mMainWindowName.empty() && argc && (*argc > 0))
113 // Set mName from command-line args if environment option not set
114 mMainWindowName = (*argv)[0];
117 mCommandLineOptions = new CommandLineOptions(argc, argv);
118 mFramework = new Framework(*this, argc, argv, applicationType);
119 mUseRemoteSurface = (applicationType == Framework::WATCH);
122 Application::~Application()
124 SingletonService service = SingletonService::Get();
125 // Note this can be false i.e. if Application has never created a Core instance
128 service.UnregisterAll();
133 delete mAdaptorBuilder;
134 delete mCommandLineOptions;
138 void Application::StoreWindowPositionSize(PositionSize positionSize)
140 mWindowPositionSize = positionSize;
143 void Application::ChangePreInitializedWindowSize()
145 int screenWidth, screenHeight;
146 Dali::Internal::Adaptor::WindowSystem::GetScreenSize(screenWidth, screenHeight);
148 if(mWindowPositionSize != PositionSize(0, 0, 0, 0))
150 Dali::DevelWindow::SetPositionSize(mMainWindow, mWindowPositionSize);
152 else if(mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
154 // Command line options override environment options and full screen
155 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
156 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
157 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
159 else if(mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight())
161 // Environment options override full screen functionality if command line arguments not provided
162 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
163 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
164 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
166 else if(screenWidth != mWindowPositionSize.width || screenHeight != mWindowPositionSize.height)
168 //Some apps can receive screen size differently after launching by specifying size in manifest.
169 mWindowPositionSize.width = screenWidth;
170 mWindowPositionSize.height = screenHeight;
171 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
175 void Application::CreateWindow()
177 if(mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0)
179 if(mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
181 // Command line options override environment options and full screen
182 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
183 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
185 else if(mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight())
187 // Environment options override full screen functionality if command line arguments not provided
188 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
189 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
193 const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
195 Internal::Adaptor::Window* window = Internal::Adaptor::Window::New(mWindowPositionSize, mMainWindowName, windowClassName, mDefaultWindowType, mMainWindowMode == Dali::Application::TRANSPARENT);
196 mMainWindow = Dali::Window(window);
198 // Quit the application when the window is closed
199 GetImplementation(mMainWindow).DeleteRequestSignal().Connect(mSlotDelegate, &Application::Quit);
202 void Application::CreateAdaptor()
204 DALI_ASSERT_ALWAYS(mMainWindow && "Window required to create adaptor");
206 auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
208 Integration::SceneHolder sceneHolder = Integration::SceneHolder(&Dali::GetImplementation(mMainWindow));
210 mAdaptor = Adaptor::New(graphicsFactory, sceneHolder, &mEnvironmentOptions);
212 Adaptor::GetImplementation(*mAdaptor).SetUseRemoteSurface(mUseRemoteSurface);
215 void Application::CreateAdaptorBuilder()
217 mAdaptorBuilder = new AdaptorBuilder(mEnvironmentOptions);
220 void Application::MainLoop()
222 // Run the application
226 void Application::Lower()
228 // Lower the application without quitting it.
232 void Application::Quit()
234 // Actually quit the application.
235 // Force a call to Quit even if adaptor is not running.
236 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle(MakeCallback(this, &Application::QuitFromMainLoop), false, true);
239 void Application::QuitFromMainLoop()
241 Accessibility::Bridge::GetCurrentBridge()->Terminate();
246 // This will trigger OnTerminate(), below, after the main loop has completed.
249 void Application::OnInit()
251 mFramework->AddAbortCallback(MakeCallback(this, &Application::QuitFromMainLoop));
253 CreateAdaptorBuilder();
254 // If an application was pre-initialized, a window was made in advance
255 if(mLaunchpadState == Launchpad::NONE)
262 if(mLaunchpadState == Launchpad::PRE_INITIALIZED)
264 ChangePreInitializedWindowSize();
269 Accessibility::Accessible::SetObjectRegistry(mAdaptor->GetObjectRegistry());
271 if(!mStylesheet.empty())
273 Dali::StyleMonitor::Get().SetTheme(mStylesheet);
276 // Wire up the LifecycleController
277 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
279 InitSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnInit);
280 TerminateSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnTerminate);
281 PauseSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnPause);
282 ResumeSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnResume);
283 ResetSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnReset);
284 LanguageChangedSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnLanguageChanged);
286 Dali::Application application(this);
287 mInitSignal.Emit(application);
289 mAdaptor->NotifySceneCreated();
292 void Application::OnTerminate()
294 // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
295 // delete the window as ecore_x has been destroyed by AppCore
297 Dali::Application application(this);
298 mTerminateSignal.Emit(application);
302 // Ensure that the render-thread is not using the surface(window) after we delete it
306 mMainWindow.Reset(); // This only resets (clears) the default Window
309 void Application::OnPause()
311 // A DALi app should handle Pause/Resume events.
312 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
313 // Pausing DALi core only occurs on the Window Hidden framework event
314 Dali::Application application(this);
315 mPauseSignal.Emit(application);
318 void Application::OnResume()
320 // Emit the signal first so the application can queue any messages before we do an update/render
321 // This ensures we do not just redraw the last frame before pausing if that's not required
322 Dali::Application application(this);
323 mResumeSignal.Emit(application);
325 // DALi just delivers the framework Resume event to the application.
326 // Resuming DALi core only occurs on the Window Show framework event
328 // Trigger processing of events queued up while paused
329 CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation(GetAdaptor());
330 coreEventInterface.ProcessCoreEvents();
333 void Application::OnReset()
336 * usually, reset callback was called when a caller request to launch this application via aul.
337 * because Application class already handled initialization in OnInit(), OnReset do nothing.
339 Dali::Application application(this);
340 mResetSignal.Emit(application);
343 void Application::OnAppControl(void* data)
345 Dali::Application application(this);
346 mAppControlSignal.Emit(application, data);
349 void Application::OnLanguageChanged()
351 mAdaptor->NotifyLanguageChanged();
352 Dali::Application application(this);
353 mLanguageChangedSignal.Emit(application);
356 void Application::OnRegionChanged()
358 Dali::Application application(this);
359 mRegionChangedSignal.Emit(application);
362 void Application::OnBatteryLow(Dali::DeviceStatus::Battery::Status status)
364 Dali::Application application(this);
365 mLowBatterySignal.Emit(status);
368 void Application::OnMemoryLow(Dali::DeviceStatus::Memory::Status status)
370 Dali::Application application(this);
371 mLowMemorySignal.Emit(status);
374 void Application::OnSurfaceCreated(Any newSurface)
376 void* newWindow = AnyCast<void*>(newSurface);
377 void* oldWindow = AnyCast<void*>(mMainWindow.GetNativeHandle());
378 if(oldWindow != newWindow)
380 auto renderSurfaceFactory = Dali::Internal::Adaptor::GetRenderSurfaceFactory();
381 std::unique_ptr<WindowRenderSurface> newSurfacePtr = renderSurfaceFactory->CreateWindowRenderSurface(PositionSize(), newSurface, true);
383 mAdaptor->ReplaceSurface(mMainWindow, *newSurfacePtr.release());
387 void Application::OnSurfaceDestroyed(Any surface)
391 bool Application::AddIdle(CallbackBase* callback, bool hasReturnValue)
393 return mAdaptor->AddIdle(callback, hasReturnValue);
396 std::string Application::GetRegion() const
398 return mFramework->GetRegion();
401 std::string Application::GetLanguage() const
403 return mFramework->GetLanguage();
406 Dali::ObjectRegistry Application::GetObjectRegistry() const
408 Dali::ObjectRegistry objectRegistry;
411 objectRegistry = mAdaptor->GetObjectRegistry();
413 return objectRegistry;
416 Dali::Adaptor& Application::GetAdaptor()
421 Dali::Window Application::GetWindow()
426 std::string Application::GetResourcePath()
428 return Internal::Adaptor::Framework::GetResourcePath();
431 std::string Application::GetDataPath()
433 return Internal::Adaptor::Framework::GetDataPath();
436 void Application::SetStyleSheet(const std::string& stylesheet)
438 mStylesheet = stylesheet;
441 void Application::SetCommandLineOptions(int* argc, char** argv[])
443 delete mCommandLineOptions;
445 mCommandLineOptions = new CommandLineOptions(argc, argv);
447 mFramework->SetCommandLineOptions(argc, argv);
450 void Application::SetDefaultWindowType(WindowType type)
452 mDefaultWindowType = type;
453 mMainWindow.SetType(type);
456 ApplicationPtr Application::GetPreInitializedApplication()
458 return gPreInitializedApplication;
461 Graphics::Controller& Application::GetController()
463 auto& adaptor = Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor);
464 return adaptor.GetGraphicsInterface().GetController();
467 } // namespace Adaptor
469 } // namespace Internal