2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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.
18 #include "runtime/runtime.h"
20 #include <ewk_chromium.h>
24 #include "common/logger.h"
25 #include "common/command_line.h"
26 #include "common/app_control.h"
27 #include "common/application_data.h"
28 #include "runtime/native_app_window.h"
34 const char* kTextLocalePath = "/usr/share/locale";
35 const char* kTextDomainWrt = "wrt";
37 static NativeWindow* CreateNativeWindow() {
38 // TODO(wy80.choi) : consider other type of native window.
39 NativeWindow* window = new NativeAppWindow();
48 native_window_(NULL) {
56 delete native_window_;
60 bool Runtime::OnCreate() {
61 std::string appid = CommandLine::ForCurrentProcess()->appid();
63 // Process First Launch
64 std::unique_ptr<ApplicationData> appdata(new ApplicationData(appid));
65 if (!appdata->LoadManifestData()) {
69 native_window_ = CreateNativeWindow();
70 application_ = new WebApplication(native_window_, std::move(appdata));
71 application_->set_terminator([](){ ui_app_exit(); });
73 setlocale(LC_ALL, "");
74 bindtextdomain(kTextDomainWrt, kTextLocalePath);
79 void Runtime::OnTerminate() {
82 void Runtime::OnPause() {
83 if (application_->launched()) {
84 application_->Suspend();
88 void Runtime::OnResume() {
89 if (application_->launched()) {
90 application_->Resume();
94 void Runtime::OnAppControl(app_control_h app_control) {
95 std::unique_ptr<AppControl> appcontrol(new AppControl(app_control));
96 if (application_->launched()) {
97 application_->AppControl(std::move(appcontrol));
99 application_->Launch(std::move(appcontrol));
103 void Runtime::OnLanguageChanged(const std::string& language) {
105 application_->OnLanguageChanged();
106 elm_language_set(language.c_str());
110 void Runtime::OnLowMemory() {
112 application_->OnLowMemory();
116 int Runtime::Exec(int argc, char* argv[]) {
117 ui_app_lifecycle_callback_s ops = {NULL, NULL, NULL, NULL, NULL};
120 ops.create = [](void* data) -> bool {
121 Runtime* runtime = reinterpret_cast<Runtime*>(data);
123 LOGGER(ERROR) << "Runtime has not been created.";
126 return runtime->OnCreate();
130 ops.terminate = [](void* data) -> void {
131 Runtime* runtime = reinterpret_cast<Runtime*>(data);
133 LOGGER(ERROR) << "Runtime has not been created.";
136 runtime->OnTerminate();
140 ops.pause = [](void* data) -> void {
141 Runtime* runtime = reinterpret_cast<Runtime*>(data);
143 LOGGER(ERROR) << "Runtime has not been created.";
150 ops.resume = [](void* data) -> void {
151 Runtime* runtime = reinterpret_cast<Runtime*>(data);
153 LOGGER(ERROR) << "Runtime has not been created.";
160 ops.app_control = [](app_control_h app_control, void* data) -> void {
161 Runtime* runtime = reinterpret_cast<Runtime*>(data);
163 LOGGER(ERROR) << "Runtime has not been created.";
166 runtime->OnAppControl(app_control);
169 // language changed callback
170 auto language_changed = [](app_event_info_h event_info, void* user_data) {
172 if (app_event_get_language(event_info, &str) == 0 && str != NULL) {
173 std::string language = std::string(str);
175 Runtime* runtime = reinterpret_cast<Runtime*>(user_data);
176 runtime->OnLanguageChanged(language);
179 auto low_memory = [](app_event_info_h /*event_info*/, void* user_data) {
180 Runtime* runtime = reinterpret_cast<Runtime*>(user_data);
181 runtime->OnLowMemory();
183 app_event_handler_h ev_handle;
184 ui_app_add_event_handler(&ev_handle,
185 APP_EVENT_LANGUAGE_CHANGED,
188 ui_app_add_event_handler(&ev_handle,
189 APP_EVENT_LOW_MEMORY,
193 return ui_app_main(argc, argv, &ops, this);