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>
26 #include "app_core_base.hh"
29 #define EXPORT_API __attribute__ ((visibility("default")))
32 #define DEPRECATED __attribute__((__deprecated__))
38 class AppCore : public AppCoreBase {
40 class AppCoreEvent : public AppCoreBase::EventBase {
42 AppCoreEvent(appcore_base_event type, appcore_base_event_cb cb, void* data)
43 : EventBase(static_cast<Type>(type)),
47 void OnEvent(const std::string& value) override {
49 cb_(reinterpret_cast<void*>(const_cast<char*>(value.c_str())), data_);
52 void OnEvent(int value) override {
58 appcore_base_event_cb cb_;
62 AppCore(appcore_base_ops ops, void* data) : ops_(ops), data_(data) {}
64 void OnLoopInit(int argc, char** argv) override {
66 ops_.init(argc, argv, data_);
69 void OnLoopFinish() override {
74 void OnLoopRun() override {
79 void OnLoopExit() override {
84 int OnReceive(aul_type type, tizen_base::Bundle b) override {
86 return ops_.receive(type, b.GetHandle(), data_);
90 int OnCreate() override {
92 return ops_.create(data_);
96 int OnControl(tizen_base::Bundle b) override {
98 return ops_.control(b.GetHandle(), data_);
102 int OnTerminate() override {
104 return ops_.terminate(data_);
108 int OnSetI18n() override {
110 return ops_.set_i18n(data_);
114 int OnSetEvent(IEvent::Type event) override {
118 ops_.set_event((appcore_base_event)event, data_);
122 int OnUnsetEvent(IEvent::Type event) override {
123 if (!ops_.unset_event)
126 ops_.unset_event((appcore_base_event)event, data_);
130 int OnTrimMemory() override {
131 if (!ops_.trim_memory)
134 ops_.trim_memory(data_);
139 appcore_base_ops ops_;
143 std::unique_ptr<AppCore> __context;
145 int __on_control(bundle* b, void* data) {
146 return appcore_base_on_control(b);
149 int __on_set_i18n(void* data) {
150 return appcore_base_on_set_i18n();
153 void __on_set_event(enum appcore_base_event event, void* data) {
154 appcore_base_on_set_event(event);
157 void __on_unset_event(enum appcore_base_event event, void* data) {
158 appcore_base_on_unset_event(event);
161 void __on_trim_memory(void* data) {
162 appcore_base_on_trim_memory();
165 int __on_receive(aul_type type, bundle* b, void* data) {
166 return appcore_base_on_receive(type, b);
169 int __on_create(void* data) {
170 return appcore_base_on_create();
173 int __on_terminate(void* data) {
174 return appcore_base_on_terminate();
178 } // namespace tizen_cpp
180 using namespace tizen_cpp;
182 extern "C" EXPORT_API int appcore_base_on_receive(aul_type type, bundle* b) {
183 if (__context.get() == nullptr)
185 return __context->AppCoreBase::OnReceive(type, tizen_base::Bundle(b));
188 extern "C" EXPORT_API int appcore_base_on_create(void) {
189 if (__context.get() == nullptr)
191 return __context->AppCoreBase::OnCreate();
194 extern "C" EXPORT_API int appcore_base_on_control(bundle* b) {
195 if (__context.get() == nullptr)
197 return __context->AppCoreBase::OnControl(
198 b ? tizen_base::Bundle(b) : tizen_base::Bundle());
201 extern "C" EXPORT_API int appcore_base_on_terminate(void) {
202 if (__context.get() == nullptr)
204 return __context->AppCoreBase::OnTerminate();
207 extern "C" EXPORT_API int appcore_base_on_set_i18n(void) {
208 if (__context.get() == nullptr)
210 return __context->AppCoreBase::OnSetI18n();
213 extern "C" EXPORT_API void appcore_base_on_set_event(
214 enum appcore_base_event event) {
215 if (__context.get() == nullptr)
217 __context->AppCoreBase::OnSetEvent(
218 static_cast<AppCoreBase::IEvent::Type>(event));
221 extern "C" EXPORT_API void appcore_base_on_unset_event(
222 enum appcore_base_event event) {
223 if (__context.get() == nullptr)
225 __context->AppCoreBase::OnUnsetEvent(
226 static_cast<AppCoreBase::IEvent::Type>(event));
229 extern "C" EXPORT_API int appcore_base_on_trim_memory(void) {
230 if (__context.get() == nullptr)
232 return __context->AppCoreBase::OnTrimMemory();
235 extern "C" EXPORT_API int appcore_base_init(appcore_base_ops ops,
236 int argc, char** argv, void* data) {
237 __context.reset(new AppCore(ops, data));
239 __context->Init(argc, argv);
240 } catch (const std::runtime_error&) {
247 extern "C" EXPORT_API void appcore_base_fini(void) {
248 if (__context.get() == nullptr)
254 extern "C" EXPORT_API appcore_base_ops appcore_base_get_default_ops(void) {
255 appcore_base_ops ops;
257 ops.create = __on_create;
258 ops.control = __on_control;
259 ops.terminate = __on_terminate;
260 ops.receive = __on_receive;
261 ops.set_i18n = __on_set_i18n;
263 ops.finish = nullptr;
266 ops.set_event = __on_set_event;
267 ops.unset_event = __on_unset_event;
268 ops.trim_memory = __on_trim_memory;
273 extern "C" EXPORT_API appcore_base_event_h appcore_base_add_event(
274 enum appcore_base_event event,
275 appcore_base_event_cb cb,
277 if (__context == nullptr)
280 auto* ev = new std::shared_ptr<AppCore::AppCoreEvent>(
281 std::make_shared<AppCore::AppCoreEvent>(event, cb, data));
285 __context->AddEvent(*ev);
286 return static_cast<appcore_base_event_h>(ev);
289 extern "C" EXPORT_API int appcore_base_remove_event(
290 appcore_base_event_h handle) {
291 auto* ev = static_cast<std::shared_ptr<AppCoreBase::EventBase>*>(handle);
292 if (ev == nullptr || __context == nullptr)
295 bool ret = __context->RemoveEvent(*ev);
300 extern "C" DEPRECATED EXPORT_API int appcore_base_raise_event(
302 enum appcore_base_event type) {
303 LOGE("This api was deprecated since 7.0");
304 if (__context == nullptr)
310 extern "C" EXPORT_API int appcore_base_flush_memory(void) {
311 if (__context == nullptr)
314 __context->FlushMemory();
318 extern "C" DEPRECATED EXPORT_API int appcore_base_get_rotation_state(
319 enum appcore_base_rm* curr) {
320 LOGE("This api was deprecated since 7.0");
321 if (__context == nullptr)
327 extern "C" EXPORT_API bool appcore_base_is_bg_allowed(void) {
328 if (__context == nullptr)
331 return __context->IsBgAllowed();
334 extern "C" EXPORT_API bool appcore_base_is_suspended(void) {
335 if (__context == nullptr)
338 return __context->IsSuspended();
341 extern "C" EXPORT_API void appcore_base_toggle_suspended_state(void) {
342 if (__context == nullptr)
345 __context->ToggleSuspendedState();
348 extern "C" EXPORT_API int appcore_base_set_i18n(const char* domain_name,
349 const char* dir_name) {
350 if (__context == nullptr || domain_name == nullptr || dir_name == nullptr)
353 return __context->SetI18n(domain_name, dir_name);
356 extern "C" EXPORT_API void appcore_base_exit(void) {
357 aul_status_update(STATUS_DYING);
358 if (__context == nullptr)
364 extern "C" EXPORT_API void appcore_base_add_suspend_timer(void) {
365 if (__context == nullptr)
368 __context->AddSuspendTimer();
371 extern "C" EXPORT_API void appcore_base_remove_suspend_timer(void) {
372 if (__context == nullptr)
375 __context->RemoveSuspendTimer();
378 extern "C" EXPORT_API void appcore_base_set_display_state(int display_state) {
379 if (__context == nullptr)
382 __context->SetDisplayState(
383 static_cast<AppCoreBase::DisplayState>(display_state));
386 extern "C" EXPORT_API int appcore_base_get_display_state(void) {
387 if (__context == nullptr)
390 return __context->GetDisplayState();