1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "runtime/runtime.h"
7 #include <ewk_chromium.h>
11 #include "common/logger.h"
12 #include "common/command_line.h"
13 #include "common/app_control.h"
14 #include "common/application_data.h"
15 #include "runtime/native_app_window.h"
21 const char* kTextLocalePath = "/usr/share/locale";
22 const char* kTextDomainWrt = "wrt";
24 static NativeWindow* CreateNativeWindow() {
25 // TODO(wy80.choi) : consider other type of native window.
26 NativeWindow* window = new NativeAppWindow();
35 native_window_(NULL) {
43 delete native_window_;
47 bool Runtime::OnCreate() {
48 std::string appid = CommandLine::ForCurrentProcess()->appid();
50 // Process First Launch
51 std::unique_ptr<ApplicationData> appdata(new ApplicationData(appid));
52 if (!appdata->LoadManifestData()) {
56 native_window_ = CreateNativeWindow();
57 application_ = new WebApplication(native_window_, std::move(appdata));
58 application_->set_terminator([](){ ui_app_exit(); });
60 setlocale(LC_ALL, "");
61 bindtextdomain(kTextDomainWrt, kTextLocalePath);
66 void Runtime::OnTerminate() {
69 void Runtime::OnPause() {
70 if (application_->launched()) {
71 application_->Suspend();
75 void Runtime::OnResume() {
76 if (application_->launched()) {
77 application_->Resume();
81 void Runtime::OnAppControl(app_control_h app_control) {
82 std::unique_ptr<AppControl> appcontrol(new AppControl(app_control));
83 if (application_->launched()) {
84 application_->AppControl(std::move(appcontrol));
86 application_->Launch(std::move(appcontrol));
90 void Runtime::OnLanguageChanged(const std::string& language) {
92 application_->OnLanguageChanged();
93 elm_language_set(language.c_str());
97 void Runtime::OnLowMemory() {
99 application_->OnLowMemory();
103 int Runtime::Exec(int argc, char* argv[]) {
104 ui_app_lifecycle_callback_s ops = {NULL, NULL, NULL, NULL, NULL};
107 ops.create = [](void* data) -> bool {
108 Runtime* runtime = reinterpret_cast<Runtime*>(data);
110 LOGGER(ERROR) << "Runtime has not been created.";
113 return runtime->OnCreate();
117 ops.terminate = [](void* data) -> void {
118 Runtime* runtime = reinterpret_cast<Runtime*>(data);
120 LOGGER(ERROR) << "Runtime has not been created.";
123 runtime->OnTerminate();
127 ops.pause = [](void* data) -> void {
128 Runtime* runtime = reinterpret_cast<Runtime*>(data);
130 LOGGER(ERROR) << "Runtime has not been created.";
137 ops.resume = [](void* data) -> void {
138 Runtime* runtime = reinterpret_cast<Runtime*>(data);
140 LOGGER(ERROR) << "Runtime has not been created.";
147 ops.app_control = [](app_control_h app_control, void* data) -> void {
148 Runtime* runtime = reinterpret_cast<Runtime*>(data);
150 LOGGER(ERROR) << "Runtime has not been created.";
153 runtime->OnAppControl(app_control);
156 // language changed callback
157 auto language_changed = [](app_event_info_h event_info, void* user_data) {
159 if (app_event_get_language(event_info, &str) == 0 && str != NULL) {
160 std::string language = std::string(str);
162 Runtime* runtime = reinterpret_cast<Runtime*>(user_data);
163 runtime->OnLanguageChanged(language);
166 auto low_memory = [](app_event_info_h /*event_info*/, void* user_data) {
167 Runtime* runtime = reinterpret_cast<Runtime*>(user_data);
168 runtime->OnLowMemory();
170 app_event_handler_h ev_handle;
171 ui_app_add_event_handler(&ev_handle,
172 APP_EVENT_LANGUAGE_CHANGED,
175 ui_app_add_event_handler(&ev_handle,
176 APP_EVENT_LOW_MEMORY,
180 return ui_app_main(argc, argv, &ops, this);