Refactor appcore legacy api to cpp
[platform/core/appfw/app-core.git] / legacy / src / legacy / appcore_base.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "appcore_base.h"
18
19 #include <aul.h>
20 #include <aul_app_lifecycle.h>
21 #include <dlog.h>
22
23 #include <memory>
24 #include <string>
25
26 #include "app_core_base.hh"
27
28 #undef EXPORT_API
29 #define EXPORT_API __attribute__ ((visibility("default")))
30
31 #undef DEPRECATED
32 #define DEPRECATED __attribute__((__deprecated__))
33
34 namespace tizen_cpp {
35
36 namespace {
37
38 class AppCore : public AppCoreBase {
39  public:
40   class AppCoreEvent : public AppCoreBase::EventBase {
41    public:
42     AppCoreEvent(appcore_base_event type, appcore_base_event_cb cb, void* data)
43         : EventBase(static_cast<Type>(type)),
44           cb_(cb),
45           data_(data) {}
46
47     void OnEvent(const std::string& value) override {
48       if (cb_ != nullptr)
49         cb_(reinterpret_cast<void*>(const_cast<char*>(value.c_str())), data_);
50     }
51
52     void OnEvent(int value) override {
53       if (cb_ != nullptr)
54         cb_(&value, data_);
55     }
56
57    private:
58     appcore_base_event_cb cb_;
59     void* data_;
60   };
61
62   AppCore(appcore_base_ops ops, void* data) : ops_(ops), data_(data) {}
63
64   void OnLoopInit(int argc, char** argv) override {
65     if (ops_.init)
66       ops_.init(argc, argv, data_);
67   }
68
69   void OnLoopFinish() override {
70     if (ops_.finish)
71       ops_.finish();
72   }
73
74   void OnLoopRun() override {
75     if (ops_.run)
76       ops_.run(data_);
77   }
78
79   void OnLoopExit() override {
80     if (ops_.exit)
81       ops_.exit(data_);
82   }
83
84   int OnReceive(aul_type type, tizen_base::Bundle b) override {
85     if (ops_.receive)
86       return ops_.receive(type, b.GetHandle(), data_);
87     return -1;
88   }
89
90   int OnCreate() override {
91     if (ops_.create)
92       return ops_.create(data_);
93     return -1;
94   }
95
96   int OnControl(tizen_base::Bundle b) override {
97     if (ops_.control)
98       return ops_.control(b.GetHandle(), data_);
99     return -1;
100   }
101
102   int OnTerminate() override {
103     if (ops_.terminate)
104       return ops_.terminate(data_);
105     return -1;
106   }
107
108   int OnSetI18n() override {
109     if (ops_.set_i18n)
110       return ops_.set_i18n(data_);
111     return -1;
112   }
113
114   int OnSetEvent(IEvent::Type event) override {
115     if (!ops_.set_event)
116       return -1;
117
118     ops_.set_event((appcore_base_event)event, data_);
119     return 0;
120   }
121
122   int OnUnsetEvent(IEvent::Type event) override {
123     if (!ops_.unset_event)
124       return -1;
125
126     ops_.unset_event((appcore_base_event)event, data_);
127     return 0;
128   }
129
130   int OnTrimMemory() override {
131     if (!ops_.trim_memory)
132       return -1;
133
134     ops_.trim_memory(data_);
135     return 0;
136   }
137
138  private:
139   appcore_base_ops ops_;
140   void* data_;
141 };
142
143 std::unique_ptr<AppCore> __context;
144
145 int __on_control(bundle* b, void* data) {
146   return appcore_base_on_control(b);
147 }
148
149 int __on_set_i18n(void* data) {
150   return appcore_base_on_set_i18n();
151 }
152
153 void __on_set_event(enum appcore_base_event event, void* data) {
154   appcore_base_on_set_event(event);
155 }
156
157 void __on_unset_event(enum appcore_base_event event, void* data) {
158   appcore_base_on_unset_event(event);
159 }
160
161 void __on_trim_memory(void* data) {
162   appcore_base_on_trim_memory();
163 }
164
165 int __on_receive(aul_type type, bundle* b, void* data) {
166   return appcore_base_on_receive(type, b);
167 }
168
169 int __on_create(void* data) {
170   return appcore_base_on_create();
171 }
172
173 int __on_terminate(void* data) {
174   return appcore_base_on_terminate();
175 }
176
177 }  // namespace
178 }  // namespace tizen_cpp
179
180 using namespace tizen_cpp;
181
182 extern "C" EXPORT_API int appcore_base_on_receive(aul_type type, bundle* b) {
183   if (__context.get() == nullptr)
184     return -1;
185   return __context->AppCoreBase::OnReceive(type, tizen_base::Bundle(b));
186 }
187
188 extern "C" EXPORT_API int appcore_base_on_create(void) {
189   if (__context.get() == nullptr)
190     return -1;
191   return __context->AppCoreBase::OnCreate();
192 }
193
194 extern "C" EXPORT_API int appcore_base_on_control(bundle* b) {
195   if (__context.get() == nullptr)
196     return -1;
197   return __context->AppCoreBase::OnControl(
198       b ? tizen_base::Bundle(b) : tizen_base::Bundle());
199 }
200
201 extern "C" EXPORT_API int appcore_base_on_terminate(void) {
202   if (__context.get() == nullptr)
203     return -1;
204   return __context->AppCoreBase::OnTerminate();
205 }
206
207 extern "C" EXPORT_API int appcore_base_on_set_i18n(void) {
208   if (__context.get() == nullptr)
209     return -1;
210   return __context->AppCoreBase::OnSetI18n();
211 }
212
213 extern "C" EXPORT_API void appcore_base_on_set_event(
214     enum appcore_base_event event) {
215   if (__context.get() == nullptr)
216     return;
217   __context->AppCoreBase::OnSetEvent(
218       static_cast<AppCoreBase::IEvent::Type>(event));
219 }
220
221 extern "C" EXPORT_API void appcore_base_on_unset_event(
222     enum appcore_base_event event) {
223   if (__context.get() == nullptr)
224     return;
225   __context->AppCoreBase::OnUnsetEvent(
226       static_cast<AppCoreBase::IEvent::Type>(event));
227 }
228
229 extern "C" EXPORT_API int appcore_base_on_trim_memory(void) {
230   if (__context.get() == nullptr)
231     return -1;
232   return __context->AppCoreBase::OnTrimMemory();
233 }
234
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));
238   try {
239     __context->Init(argc, argv);
240   } catch (const std::runtime_error&) {
241     return -1;
242   }
243
244   return 0;
245 }
246
247 extern "C" EXPORT_API void appcore_base_fini(void) {
248   if (__context.get() == nullptr)
249     return;
250   __context->Fini();
251   __context.reset();
252 }
253
254 extern "C" EXPORT_API appcore_base_ops appcore_base_get_default_ops(void) {
255   appcore_base_ops ops;
256
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;
262   ops.init = nullptr;
263   ops.finish = nullptr;
264   ops.run = nullptr;
265   ops.exit = nullptr;
266   ops.set_event = __on_set_event;
267   ops.unset_event = __on_unset_event;
268   ops.trim_memory = __on_trim_memory;
269
270   return ops;
271 }
272
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,
276     void* data) {
277   if (__context == nullptr)
278     return nullptr;
279
280   auto* ev = new std::shared_ptr<AppCore::AppCoreEvent>(
281       std::make_shared<AppCore::AppCoreEvent>(event, cb, data));
282   if (ev == nullptr)
283     return nullptr;
284
285   __context->AddEvent(*ev);
286   return static_cast<appcore_base_event_h>(ev);
287 }
288
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)
293     return -1;
294
295   bool ret = __context->RemoveEvent(*ev);
296   delete ev;
297   return ret ? 0 : -1;
298 }
299
300 extern "C" DEPRECATED EXPORT_API int appcore_base_raise_event(
301     void* event,
302     enum appcore_base_event type) {
303   LOGE("This api was deprecated since 7.0");
304   if (__context == nullptr)
305     return -1;
306
307   return 0;
308 }
309
310 extern "C" EXPORT_API int appcore_base_flush_memory(void) {
311   if (__context == nullptr)
312     return -1;
313
314   __context->FlushMemory();
315   return 0;
316 }
317
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)
322     return -1;
323
324   return 0;
325 }
326
327 extern "C" EXPORT_API bool appcore_base_is_bg_allowed(void) {
328   if (__context == nullptr)
329     return false;
330
331   return __context->IsBgAllowed();
332 }
333
334 extern "C" EXPORT_API bool appcore_base_is_suspended(void) {
335   if (__context == nullptr)
336     return false;
337
338   return __context->IsSuspended();
339 }
340
341 extern "C" EXPORT_API void appcore_base_toggle_suspended_state(void) {
342   if (__context == nullptr)
343     return;
344
345   __context->ToggleSuspendedState();
346 }
347
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)
351     return -1;
352
353   return __context->SetI18n(domain_name, dir_name);
354 }
355
356 extern "C" EXPORT_API void appcore_base_exit(void) {
357   aul_status_update(STATUS_DYING);
358   if (__context == nullptr)
359     return;
360
361   __context->Exit();
362 }
363
364 extern "C" EXPORT_API void appcore_base_add_suspend_timer(void) {
365   if (__context == nullptr)
366     return;
367
368   __context->AddSuspendTimer();
369 }
370
371 extern "C" EXPORT_API void appcore_base_remove_suspend_timer(void) {
372   if (__context == nullptr)
373     return;
374
375   __context->RemoveSuspendTimer();
376 }
377
378 extern "C" EXPORT_API void appcore_base_set_display_state(int display_state) {
379   if (__context == nullptr)
380     return;
381
382   __context->SetDisplayState(
383       static_cast<AppCoreBase::DisplayState>(display_state));
384 }
385
386 extern "C" EXPORT_API int appcore_base_get_display_state(void) {
387   if (__context == nullptr)
388     return -1;
389
390   return __context->GetDisplayState();
391 }