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>
28 #include "app_core_base.hh"
31 #define EXPORT_API __attribute__ ((visibility("default")))
34 #define DEPRECATED __attribute__((__deprecated__))
40 class AppCore : public AppCoreBase {
42 class AppCoreEvent : public AppCoreBase::EventBase {
44 AppCoreEvent(appcore_base_event type, appcore_base_event_cb cb, void* data)
45 : EventBase(static_cast<Type>(type)),
49 void OnEvent(const std::string& value) override {
51 cb_(reinterpret_cast<void*>(const_cast<char*>(value.c_str())), data_);
54 void OnEvent(int value) override {
60 appcore_base_event_cb cb_;
64 AppCore(appcore_base_ops ops, void* data) : ops_(ops), data_(data) {}
66 void OnLoopInit(int argc, char** argv) override {
68 ops_.init(argc, argv, data_);
71 void OnLoopFinish() override {
76 void OnLoopRun() override {
81 void OnLoopExit() override {
86 int OnReceive(aul_type type, tizen_base::Bundle b) override {
88 return ops_.receive(type, b.GetHandle(), data_);
92 int OnCreate() override {
94 return ops_.create(data_);
98 int OnControl(tizen_base::Bundle b) override {
100 return ops_.control(b.GetHandle(), data_);
104 int OnTerminate() override {
106 return ops_.terminate(data_);
110 int OnSetI18n() override {
112 return ops_.set_i18n(data_);
116 int OnSetEvent(IEvent::Type event) override {
120 ops_.set_event((appcore_base_event)event, data_);
124 int OnUnsetEvent(IEvent::Type event) override {
125 if (!ops_.unset_event)
128 ops_.unset_event((appcore_base_event)event, data_);
132 int OnTrimMemory() override {
133 if (!ops_.trim_memory)
136 ops_.trim_memory(data_);
141 appcore_base_ops ops_;
145 std::list<std::shared_ptr<AppCoreBase::EventBase>> __event_list;
147 int __on_control(bundle* b, void* data) {
148 return appcore_base_on_control(b);
151 int __on_set_i18n(void* data) {
152 return appcore_base_on_set_i18n();
155 void __on_set_event(enum appcore_base_event event, void* data) {
156 appcore_base_on_set_event(event);
159 void __on_unset_event(enum appcore_base_event event, void* data) {
160 appcore_base_on_unset_event(event);
163 void __on_trim_memory(void* data) {
164 appcore_base_on_trim_memory();
167 int __on_receive(aul_type type, bundle* b, void* data) {
168 return appcore_base_on_receive(type, b);
171 int __on_create(void* data) {
172 return appcore_base_on_create();
175 int __on_terminate(void* data) {
176 return appcore_base_on_terminate();
180 } // namespace tizen_cpp
182 using namespace tizen_cpp;
184 extern "C" EXPORT_API int appcore_base_on_receive(aul_type type, bundle* b) {
185 auto* context = AppCoreBase::GetContext();
189 return context->AppCoreBase::OnReceive(type, tizen_base::Bundle(b));
192 extern "C" EXPORT_API int appcore_base_on_create(void) {
193 auto* context = AppCoreBase::GetContext();
197 return context->AppCoreBase::OnCreate();
200 extern "C" EXPORT_API int appcore_base_on_control(bundle* b) {
201 auto* context = AppCoreBase::GetContext();
205 return context->AppCoreBase::OnControl(
206 b ? tizen_base::Bundle(b) : tizen_base::Bundle());
209 extern "C" EXPORT_API int appcore_base_on_terminate(void) {
210 auto* context = AppCoreBase::GetContext();
214 return context->AppCoreBase::OnTerminate();
217 extern "C" EXPORT_API int appcore_base_on_set_i18n(void) {
218 auto* context = AppCoreBase::GetContext();
222 return context->AppCoreBase::OnSetI18n();
225 extern "C" EXPORT_API void appcore_base_on_set_event(
226 enum appcore_base_event event) {
227 auto* context = AppCoreBase::GetContext();
231 context->AppCoreBase::OnSetEvent(
232 static_cast<AppCoreBase::IEvent::Type>(event));
235 extern "C" EXPORT_API void appcore_base_on_unset_event(
236 enum appcore_base_event event) {
237 auto* context = AppCoreBase::GetContext();
241 context->AppCoreBase::OnUnsetEvent(
242 static_cast<AppCoreBase::IEvent::Type>(event));
245 extern "C" EXPORT_API int appcore_base_on_trim_memory(void) {
246 auto* context = AppCoreBase::GetContext();
250 return context->AppCoreBase::OnTrimMemory();
253 static std::unique_ptr<AppCore> context;
255 extern "C" EXPORT_API int appcore_base_init(appcore_base_ops ops,
256 int argc, char** argv, void* data) {
257 context.reset(new AppCore(ops, data));
259 for (auto& event : __event_list)
260 context->AddEvent(std::move(event));
262 __event_list.clear();
263 context->Init(argc, argv);
264 } catch (const std::runtime_error&) {
271 extern "C" EXPORT_API void appcore_base_fini(void) {
278 extern "C" EXPORT_API appcore_base_ops appcore_base_get_default_ops(void) {
279 appcore_base_ops ops;
281 ops.create = __on_create;
282 ops.control = __on_control;
283 ops.terminate = __on_terminate;
284 ops.receive = __on_receive;
285 ops.set_i18n = __on_set_i18n;
287 ops.finish = nullptr;
290 ops.set_event = __on_set_event;
291 ops.unset_event = __on_unset_event;
292 ops.trim_memory = __on_trim_memory;
297 extern "C" EXPORT_API appcore_base_event_h appcore_base_add_event(
298 enum appcore_base_event event,
299 appcore_base_event_cb cb,
301 auto* ev = new std::shared_ptr<AppCore::AppCoreEvent>(
302 std::make_shared<AppCore::AppCoreEvent>(event, cb, data));
306 auto* context = AppCoreBase::GetContext();
308 __event_list.push_back(*ev);
310 context->AddEvent(*ev);
313 return static_cast<appcore_base_event_h>(ev);
316 extern "C" EXPORT_API int appcore_base_remove_event(
317 appcore_base_event_h handle) {
318 auto* ev = static_cast<std::shared_ptr<AppCoreBase::EventBase>*>(handle);
323 auto* context = AppCoreBase::GetContext();
327 for (auto it = __event_list.begin(); it != __event_list.end(); ++it) {
329 it = __event_list.erase(it);
335 ret = (context->RemoveEvent(*ev) ? 0 : -1);
342 extern "C" DEPRECATED EXPORT_API int appcore_base_raise_event(
344 enum appcore_base_event type) {
345 LOGE("This api was deprecated since 7.0");
346 auto* context = AppCoreBase::GetContext();
353 extern "C" EXPORT_API int appcore_base_flush_memory(void) {
354 auto* context = AppCoreBase::GetContext();
358 context->FlushMemory();
362 extern "C" DEPRECATED EXPORT_API int appcore_base_get_rotation_state(
363 enum appcore_base_rm* curr) {
364 LOGE("This api was deprecated since 7.0");
365 auto* context = AppCoreBase::GetContext();
372 extern "C" EXPORT_API bool appcore_base_is_bg_allowed(void) {
373 auto* context = AppCoreBase::GetContext();
377 return context->IsBgAllowed();
380 extern "C" EXPORT_API bool appcore_base_is_suspended(void) {
381 auto* context = AppCoreBase::GetContext();
385 return context->IsSuspended();
388 extern "C" EXPORT_API void appcore_base_toggle_suspended_state(void) {
389 auto* context = AppCoreBase::GetContext();
393 context->ToggleSuspendedState();
396 extern "C" EXPORT_API int appcore_base_set_i18n(const char* domain_name,
397 const char* dir_name) {
398 auto* context = AppCoreBase::GetContext();
399 if (!context || domain_name == nullptr || dir_name == nullptr)
402 return context->SetI18n(domain_name, dir_name);
405 extern "C" EXPORT_API void appcore_base_exit(void) {
406 aul_status_update(STATUS_DYING);
407 auto* context = AppCoreBase::GetContext();
414 extern "C" EXPORT_API void appcore_base_add_suspend_timer(void) {
415 auto* context = AppCoreBase::GetContext();
419 context->AddSuspendTimer();
422 extern "C" EXPORT_API void appcore_base_remove_suspend_timer(void) {
423 auto* context = AppCoreBase::GetContext();
427 context->RemoveSuspendTimer();
430 extern "C" EXPORT_API void appcore_base_set_display_state(int display_state) {
431 auto* context = AppCoreBase::GetContext();
435 context->SetDisplayState(
436 static_cast<AppCoreBase::DisplayState>(display_state));
439 extern "C" EXPORT_API int appcore_base_get_display_state(void) {
440 auto* context = AppCoreBase::GetContext();
444 return context->GetDisplayState();