2 * Copyright (c) 2022 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/integration-api/trace.h>
25 #include <dali/public-api/object/object-registry.h>
28 #include <dali/devel-api/adaptor-framework/accessibility-bridge.h>
29 #include <dali/devel-api/adaptor-framework/environment-variable.h>
30 #include <dali/devel-api/adaptor-framework/style-monitor.h>
31 #include <dali/devel-api/atspi-interfaces/accessible.h>
32 #include <dali/devel-api/text-abstraction/font-client.h>
33 #include <dali/internal/adaptor/common/adaptor-impl.h>
34 #include <dali/internal/adaptor/common/framework.h>
35 #include <dali/internal/adaptor/common/lifecycle-controller-impl.h>
36 #include <dali/internal/system/common/command-line-options.h>
37 #include <dali/internal/system/common/environment-variables.h>
38 #include <dali/internal/window-system/common/render-surface-factory.h>
39 #include <dali/internal/window-system/common/window-impl.h>
40 #include <dali/internal/window-system/common/window-render-surface.h>
41 #include <dali/internal/window-system/common/window-system.h>
43 // To disable a macro with the same name from one of OpenGL headers
48 namespace TizenPlatform
50 class TizenPlatformAbstraction;
62 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_APPLICATION, true);
64 ApplicationPtr Application::gPreInitializedApplication(NULL);
66 ApplicationPtr Application::New(
69 const std::string& stylesheet,
70 Dali::Application::WINDOW_MODE windowMode,
71 const PositionSize& positionSize,
72 Framework::Type applicationType,
76 ApplicationPtr application(new Application(argc, argv, stylesheet, windowMode, positionSize, applicationType, type, useUiThread));
80 void Application::PreInitialize(int* argc, char** argv[])
82 if(!gPreInitializedApplication)
84 Dali::TextAbstraction::FontClientPreInitialize();
86 gPreInitializedApplication = new Application(argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL, WindowType::NORMAL, false);
87 gPreInitializedApplication->CreateWindow(); // Only create window
88 gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
92 Application::Application(int* argc, char** argv[], const std::string& stylesheet, Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType, WindowType type, bool useUiThread)
99 mLanguageChangedSignal(),
100 mRegionChangedSignal(),
103 mCommandLineOptions(nullptr),
104 mAdaptorBuilder(nullptr),
107 mMainWindowMode(windowMode),
109 mStylesheet(stylesheet),
110 mEnvironmentOptions(),
111 mWindowPositionSize(positionSize),
112 mLaunchpadState(Launchpad::NONE),
113 mDefaultWindowType(type),
114 mUseUiThread(useUiThread),
117 // Get mName from environment options
118 mMainWindowName = mEnvironmentOptions.GetWindowName();
119 if(mMainWindowName.empty() && argc && (*argc > 0))
121 // Set mName from command-line args if environment option not set
122 mMainWindowName = (*argv)[0];
125 const char* uiThreadEnabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_ENABLE_UI_THREAD);
126 if(uiThreadEnabled && std::atoi(uiThreadEnabled) != 0)
131 mCommandLineOptions = new CommandLineOptions(argc, argv);
132 mFramework = new Framework(*this, *this, argc, argv, applicationType, mUseUiThread);
133 mUseRemoteSurface = (applicationType == Framework::WATCH);
136 Application::~Application()
138 SingletonService service = SingletonService::Get();
139 // Note this can be false i.e. if Application has never created a Core instance
142 service.UnregisterAll();
147 delete mAdaptorBuilder;
148 delete mCommandLineOptions;
152 void Application::StoreWindowPositionSize(PositionSize positionSize)
154 mWindowPositionSize = positionSize;
157 void Application::ChangePreInitializedWindowSize()
159 // The real screen size may be different from the value of the preinitialized state. Update it.
160 Dali::Internal::Adaptor::WindowSystem::UpdateScreenSize();
162 int screenWidth, screenHeight;
163 Dali::Internal::Adaptor::WindowSystem::GetScreenSize(screenWidth, screenHeight);
165 if(mWindowPositionSize != PositionSize(0, 0, 0, 0))
167 Dali::DevelWindow::SetPositionSize(mMainWindow, mWindowPositionSize);
169 else if(mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
171 // Command line options override environment options and full screen
172 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
173 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
174 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
176 else if(mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight())
178 // Environment options override full screen functionality if command line arguments not provided
179 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
180 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
181 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
183 else if(screenWidth != mWindowPositionSize.width || screenHeight != mWindowPositionSize.height)
185 // Some apps can receive screen size differently after launching by specifying size in manifest.
186 mWindowPositionSize.width = screenWidth;
187 mWindowPositionSize.height = screenHeight;
188 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
192 void Application::CreateWindow()
194 if(mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0)
196 if(mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
198 // Command line options override environment options and full screen
199 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
200 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
202 else if(mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight())
204 // Environment options override full screen functionality if command line arguments not provided
205 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
206 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
210 const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
212 Internal::Adaptor::Window* window = Internal::Adaptor::Window::New(mWindowPositionSize, mMainWindowName, windowClassName, mDefaultWindowType, mMainWindowMode == Dali::Application::TRANSPARENT);
213 mMainWindow = Dali::Window(window);
215 // Quit the application when the window is closed
216 GetImplementation(mMainWindow).DeleteRequestSignal().Connect(mSlotDelegate, &Application::Quit);
219 void Application::CreateAdaptor()
221 DALI_ASSERT_ALWAYS(mMainWindow && "Window required to create adaptor");
223 auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
225 Integration::SceneHolder sceneHolder = Integration::SceneHolder(&Dali::GetImplementation(mMainWindow));
227 mAdaptor = Adaptor::New(graphicsFactory, sceneHolder, &mEnvironmentOptions);
229 Adaptor::GetImplementation(*mAdaptor).SetUseRemoteSurface(mUseRemoteSurface);
232 void Application::CreateAdaptorBuilder()
234 mAdaptorBuilder = new AdaptorBuilder(mEnvironmentOptions);
237 void Application::MainLoop()
239 // Run the application
243 void Application::Lower()
245 // Lower the application without quitting it.
249 void Application::Quit()
251 // Actually quit the application.
252 // Force a call to Quit even if adaptor is not running.
253 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle(MakeCallback(this, &Application::QuitFromMainLoop), false, true);
256 void Application::QuitFromMainLoop()
258 Accessibility::Bridge::GetCurrentBridge()->Terminate();
263 // This will trigger OnTerminate(), below, after the main loop has completed.
266 void Application::OnInit()
268 mFramework->AddAbortCallback(MakeCallback(this, &Application::QuitFromMainLoop));
270 CreateAdaptorBuilder();
271 // If an application was pre-initialized, a window was made in advance
272 if(mLaunchpadState == Launchpad::NONE)
279 if(mLaunchpadState == Launchpad::PRE_INITIALIZED)
281 ChangePreInitializedWindowSize();
285 DALI_TRACE_BEGIN(gTraceFilter, "DALI_APP_ADAPTOR_START");
287 DALI_TRACE_END(gTraceFilter, "DALI_APP_ADAPTOR_START");
288 Accessibility::Accessible::SetObjectRegistry(mAdaptor->GetObjectRegistry());
290 if(!mStylesheet.empty())
292 Dali::StyleMonitor::Get().SetTheme(mStylesheet);
295 // Wire up the LifecycleController
296 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
298 InitSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnInit);
299 TerminateSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnTerminate);
300 PauseSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnPause);
301 ResumeSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnResume);
302 ResetSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnReset);
303 LanguageChangedSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnLanguageChanged);
305 Dali::Application application(this);
307 DALI_TRACE_BEGIN(gTraceFilter, "DALI_APP_EMIT_INIT_SIGNAL");
308 mInitSignal.Emit(application);
309 DALI_TRACE_END(gTraceFilter, "DALI_APP_EMIT_INIT_SIGNAL");
311 mAdaptor->NotifySceneCreated();
314 void Application::OnTerminate()
316 // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
317 // delete the window as ecore_x has been destroyed by AppCore
319 Dali::Application application(this);
320 mTerminateSignal.Emit(application);
324 // Ensure that the render-thread is not using the surface(window) after we delete it
328 mMainWindow.Reset(); // This only resets (clears) the default Window
331 void Application::OnPause()
333 // A DALi app should handle Pause/Resume events.
334 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
335 // Pausing DALi core only occurs on the Window Hidden framework event
336 Dali::Application application(this);
337 mPauseSignal.Emit(application);
340 void Application::OnResume()
342 // Emit the signal first so the application can queue any messages before we do an update/render
343 // This ensures we do not just redraw the last frame before pausing if that's not required
344 Dali::Application application(this);
345 mResumeSignal.Emit(application);
347 // DALi just delivers the framework Resume event to the application.
348 // Resuming DALi core only occurs on the Window Show framework event
350 // Trigger processing of events queued up while paused
351 CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation(GetAdaptor());
352 coreEventInterface.ProcessCoreEvents();
355 void Application::OnReset()
358 * usually, reset callback was called when a caller request to launch this application via aul.
359 * because Application class already handled initialization in OnInit(), OnReset do nothing.
361 Dali::Application application(this);
362 mResetSignal.Emit(application);
365 void Application::OnAppControl(void* data)
367 Dali::Application application(this);
368 mAppControlSignal.Emit(application, data);
371 void Application::OnLanguageChanged()
373 mAdaptor->NotifyLanguageChanged();
374 Dali::Application application(this);
375 mLanguageChangedSignal.Emit(application);
378 void Application::OnRegionChanged()
380 Dali::Application application(this);
381 mRegionChangedSignal.Emit(application);
384 void Application::OnBatteryLow(Dali::DeviceStatus::Battery::Status status)
386 Dali::Application application(this);
387 mLowBatterySignal.Emit(status);
390 void Application::OnMemoryLow(Dali::DeviceStatus::Memory::Status status)
392 Dali::Application application(this);
393 mLowMemorySignal.Emit(status);
396 void Application::OnDeviceOrientationChanged(Dali::DeviceStatus::Orientation::Status status)
398 Dali::Application application(this);
399 mDeviceOrientationChangedSignal.Emit(status);
402 void Application::OnSurfaceCreated(Any newSurface)
404 void* newWindow = AnyCast<void*>(newSurface);
405 void* oldWindow = AnyCast<void*>(mMainWindow.GetNativeHandle());
406 if(oldWindow != newWindow)
408 auto renderSurfaceFactory = Dali::Internal::Adaptor::GetRenderSurfaceFactory();
409 std::unique_ptr<WindowRenderSurface> newSurfacePtr = renderSurfaceFactory->CreateWindowRenderSurface(PositionSize(), newSurface, true);
411 mAdaptor->ReplaceSurface(mMainWindow, *newSurfacePtr.release());
415 void Application::OnSurfaceDestroyed(Any surface)
419 void Application::OnTaskInit()
421 Dali::Application application(this);
422 mTaskInitSignal.Emit(application);
425 void Application::OnTaskTerminate()
427 Dali::Application application(this);
428 mTaskTerminateSignal.Emit(application);
431 void Application::OnTaskAppControl(void* data)
433 Dali::Application application(this);
434 mTaskAppControlSignal.Emit(application, data);
437 void Application::OnTaskLanguageChanged()
439 Dali::Application application(this);
440 mTaskLanguageChangedSignal.Emit(application);
443 void Application::OnTaskRegionChanged()
445 Dali::Application application(this);
446 mTaskRegionChangedSignal.Emit(application);
449 void Application::OnTaskBatteryLow(Dali::DeviceStatus::Battery::Status status)
451 Dali::Application application(this);
452 mTaskLowBatterySignal.Emit(status);
455 void Application::OnTaskMemoryLow(Dali::DeviceStatus::Memory::Status status)
457 Dali::Application application(this);
458 mTaskLowMemorySignal.Emit(status);
461 void Application::OnTaskDeviceOrientationChanged(Dali::DeviceStatus::Orientation::Status status)
463 Dali::Application application(this);
464 mTaskDeviceOrientationChangedSignal.Emit(status);
467 bool Application::AddIdle(CallbackBase* callback, bool hasReturnValue)
469 return mAdaptor->AddIdle(callback, hasReturnValue);
472 std::string Application::GetRegion() const
474 return mFramework->GetRegion();
477 std::string Application::GetLanguage() const
479 return mFramework->GetLanguage();
482 Dali::ObjectRegistry Application::GetObjectRegistry() const
484 Dali::ObjectRegistry objectRegistry;
487 objectRegistry = mAdaptor->GetObjectRegistry();
489 return objectRegistry;
492 Dali::Adaptor& Application::GetAdaptor()
497 Dali::Window Application::GetWindow()
502 std::string Application::GetResourcePath()
504 return Internal::Adaptor::Framework::GetResourcePath();
507 std::string Application::GetDataPath()
509 return Internal::Adaptor::Framework::GetDataPath();
512 void Application::SetStyleSheet(const std::string& stylesheet)
514 mStylesheet = stylesheet;
517 void Application::SetCommandLineOptions(int* argc, char** argv[])
519 delete mCommandLineOptions;
521 mCommandLineOptions = new CommandLineOptions(argc, argv);
523 mFramework->SetCommandLineOptions(argc, argv);
526 void Application::SetDefaultWindowType(WindowType type)
528 mDefaultWindowType = type;
529 mMainWindow.SetType(type);
532 ApplicationPtr Application::GetPreInitializedApplication()
534 // Reset the handle to decrease the reference count
535 ApplicationPtr application = gPreInitializedApplication;
536 gPreInitializedApplication.Reset();
541 } // namespace Adaptor
543 } // namespace Internal