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.
17 #include "appcore_base.h"
20 #include <aul_app_lifecycle.h>
27 #include "app_core_base.hh"
30 #define EXPORT_API __attribute__ ((visibility("default")))
33 #define DEPRECATED __attribute__((__deprecated__))
39 class AppCore : public AppCoreBase {
41 class AppCoreEvent : public AppCoreBase::EventBase {
43 AppCoreEvent(appcore_base_event type, appcore_base_event_cb cb, void* data)
44 : EventBase(static_cast<Type>(type)),
48 void OnEvent(const std::string& value) override {
50 cb_(reinterpret_cast<void*>(const_cast<char*>(value.c_str())), data_);
53 void OnEvent(int value) override {
59 appcore_base_event_cb cb_;
63 AppCore(appcore_base_ops ops, void* data) : ops_(ops), data_(data) {}
65 void OnLoopInit(int argc, char** argv) override {
67 ops_.init(argc, argv, data_);
70 void OnLoopFinish() override {
75 void OnLoopRun() override {
80 void OnLoopExit() override {
85 int OnReceive(aul_type type, tizen_base::Bundle b) override {
87 return ops_.receive(type, b.GetHandle(), data_);
91 int OnCreate() override {
93 return ops_.create(data_);
97 int OnControl(tizen_base::Bundle b) override {
99 return ops_.control(b.GetHandle(), data_);
103 int OnTerminate() override {
105 return ops_.terminate(data_);
109 int OnSetI18n() override {
111 return ops_.set_i18n(data_);
115 int OnSetEvent(IEvent::Type event) override {
119 ops_.set_event((appcore_base_event)event, data_);
123 int OnUnsetEvent(IEvent::Type event) override {
124 if (!ops_.unset_event)
127 ops_.unset_event((appcore_base_event)event, data_);
131 int OnTrimMemory() override {
132 if (!ops_.trim_memory)
135 ops_.trim_memory(data_);
140 appcore_base_ops ops_;
144 std::list<std::shared_ptr<AppCoreBase::EventBase>> __event_list;
146 int __on_control(bundle* b, void* data) {
147 return appcore_base_on_control(b);
150 int __on_set_i18n(void* data) {
151 return appcore_base_on_set_i18n();
154 void __on_set_event(enum appcore_base_event event, void* data) {
155 appcore_base_on_set_event(event);
158 void __on_unset_event(enum appcore_base_event event, void* data) {
159 appcore_base_on_unset_event(event);
162 void __on_trim_memory(void* data) {
163 appcore_base_on_trim_memory();
166 int __on_receive(aul_type type, bundle* b, void* data) {
167 return appcore_base_on_receive(type, b);
170 int __on_create(void* data) {
171 return appcore_base_on_create();
174 int __on_terminate(void* data) {
175 return appcore_base_on_terminate();
179 } // namespace tizen_cpp
181 using namespace tizen_cpp;
183 extern "C" EXPORT_API int appcore_base_on_receive(aul_type type, bundle* b) {
184 auto* context = AppCoreBase::GetContext();
188 return context->AppCoreBase::OnReceive(type, tizen_base::Bundle(b));
191 extern "C" EXPORT_API int appcore_base_on_create(void) {
192 auto* context = AppCoreBase::GetContext();
196 return context->AppCoreBase::OnCreate();
199 extern "C" EXPORT_API int appcore_base_on_control(bundle* b) {
200 auto* context = AppCoreBase::GetContext();
204 return context->AppCoreBase::OnControl(
205 b ? tizen_base::Bundle(b) : tizen_base::Bundle());
208 extern "C" EXPORT_API int appcore_base_on_terminate(void) {
209 auto* context = AppCoreBase::GetContext();
213 return context->AppCoreBase::OnTerminate();
216 extern "C" EXPORT_API int appcore_base_on_set_i18n(void) {
217 auto* context = AppCoreBase::GetContext();
221 return context->AppCoreBase::OnSetI18n();
224 extern "C" EXPORT_API void appcore_base_on_set_event(
225 enum appcore_base_event event) {
226 auto* context = AppCoreBase::GetContext();
230 context->AppCoreBase::OnSetEvent(
231 static_cast<AppCoreBase::IEvent::Type>(event));
234 extern "C" EXPORT_API void appcore_base_on_unset_event(
235 enum appcore_base_event event) {
236 auto* context = AppCoreBase::GetContext();
240 context->AppCoreBase::OnUnsetEvent(
241 static_cast<AppCoreBase::IEvent::Type>(event));
244 extern "C" EXPORT_API int appcore_base_on_trim_memory(void) {
245 auto* context = AppCoreBase::GetContext();
249 return context->AppCoreBase::OnTrimMemory();
252 static std::unique_ptr<AppCore> context;
254 extern "C" EXPORT_API int appcore_base_init(appcore_base_ops ops,
255 int argc, char** argv, void* data) {
256 context.reset(new AppCore(ops, data));
258 for (auto& event : __event_list)
259 context->AddEvent(std::move(event));
261 __event_list.clear();
262 context->Init(argc, argv);
263 } catch (const std::runtime_error&) {
270 extern "C" EXPORT_API void appcore_base_fini(void) {
277 extern "C" EXPORT_API appcore_base_ops appcore_base_get_default_ops(void) {
278 appcore_base_ops ops;
280 ops.create = __on_create;
281 ops.control = __on_control;
282 ops.terminate = __on_terminate;
283 ops.receive = __on_receive;
284 ops.set_i18n = __on_set_i18n;
286 ops.finish = nullptr;
289 ops.set_event = __on_set_event;
290 ops.unset_event = __on_unset_event;
291 ops.trim_memory = __on_trim_memory;
296 extern "C" EXPORT_API appcore_base_event_h appcore_base_add_event(
297 enum appcore_base_event event,
298 appcore_base_event_cb cb,
300 auto* ev = new std::shared_ptr<AppCore::AppCoreEvent>(
301 std::make_shared<AppCore::AppCoreEvent>(event, cb, data));
305 auto* context = AppCoreBase::GetContext();
307 __event_list.push_back(*ev);
309 context->AddEvent(*ev);
312 return static_cast<appcore_base_event_h>(ev);
315 extern "C" EXPORT_API int appcore_base_remove_event(
316 appcore_base_event_h handle) {
317 auto* ev = static_cast<std::shared_ptr<AppCoreBase::EventBase>*>(handle);
322 auto* context = AppCoreBase::GetContext();
326 for (auto it = __event_list.begin(); it != __event_list.end(); ++it) {
328 it = __event_list.erase(it);
334 ret = (context->RemoveEvent(*ev) ? 0 : -1);
341 extern "C" DEPRECATED EXPORT_API int appcore_base_raise_event(
343 enum appcore_base_event type) {
344 LOGE("This api was deprecated since 7.0");
345 auto* context = AppCoreBase::GetContext();
352 extern "C" EXPORT_API int appcore_base_flush_memory(void) {
353 auto* context = AppCoreBase::GetContext();
357 context->FlushMemory();
361 extern "C" DEPRECATED EXPORT_API int appcore_base_get_rotation_state(
362 enum appcore_base_rm* curr) {
363 LOGE("This api was deprecated since 7.0");
364 auto* context = AppCoreBase::GetContext();
371 extern "C" EXPORT_API bool appcore_base_is_bg_allowed(void) {
372 auto* context = AppCoreBase::GetContext();
376 return context->IsBgAllowed();
379 extern "C" EXPORT_API bool appcore_base_is_suspended(void) {
380 auto* context = AppCoreBase::GetContext();
384 return context->IsSuspended();
387 extern "C" EXPORT_API void appcore_base_toggle_suspended_state(void) {
388 auto* context = AppCoreBase::GetContext();
392 context->ToggleSuspendedState();
395 extern "C" EXPORT_API int appcore_base_set_i18n(const char* domain_name,
396 const char* dir_name) {
397 auto* context = AppCoreBase::GetContext();
398 if (!context || domain_name == nullptr || dir_name == nullptr)
401 return context->SetI18n(domain_name, dir_name);
404 extern "C" EXPORT_API void appcore_base_exit(void) {
405 aul_status_update(STATUS_DYING);
406 auto* context = AppCoreBase::GetContext();
413 extern "C" EXPORT_API void appcore_base_add_suspend_timer(void) {
414 auto* context = AppCoreBase::GetContext();
418 context->AddSuspendTimer();
421 extern "C" EXPORT_API void appcore_base_remove_suspend_timer(void) {
422 auto* context = AppCoreBase::GetContext();
426 context->RemoveSuspendTimer();
429 extern "C" EXPORT_API void appcore_base_set_display_state(int display_state) {
430 auto* context = AppCoreBase::GetContext();
434 context->SetDisplayState(
435 static_cast<AppCoreBase::DisplayState>(display_state));
438 extern "C" EXPORT_API int appcore_base_get_display_state(void) {
439 auto* context = AppCoreBase::GetContext();
443 return context->GetDisplayState();