Modify AppCoreUiThreadBase class
[platform/core/appfw/app-core.git] / tizen-cpp / app-core-ui-cpp / app_core_ui_base_legacy.cc
1 /*
2  * Copyright (c) 2021 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 "app_core_ui_base.hh"
18
19 #include <stdexcept>
20
21 #include "appcore_ui_base.h"
22
23 #undef EXPORT_API
24 #define EXPORT_API __attribute__ ((visibility("default")))
25
26 namespace tizen_cpp {
27
28 namespace {
29
30 class AppCoreUi : public AppCoreUiBase {
31  public:
32   AppCoreUi(unsigned int hint, appcore_ui_base_ops ops, void* data)
33       : AppCoreUiBase(hint), ops_(ops), data_(data) {}
34
35   void OnLoopInit(int argc, char** argv) override {
36     if (ops_.base.init)
37       ops_.base.init(argc, argv, data_);
38   }
39
40   void OnLoopFinish() override {
41     if (ops_.base.finish)
42       ops_.base.finish(data_);
43   }
44
45   void OnLoopRun() override {
46     if (ops_.base.run)
47       ops_.base.run(data_);
48   }
49
50   void OnLoopExit() override {
51     if (ops_.base.exit)
52       ops_.base.exit(data_);
53   }
54
55   int OnReceive(aul_type type, tizen_base::Bundle b) override {
56     if (ops_.base.receive)
57       return ops_.base.receive(type, b.GetHandle(), data_);
58     return -1;
59   }
60
61   int OnCreate() override {
62     if (ops_.base.create)
63       return ops_.base.create(data_);
64     return -1;
65   }
66
67   int OnControl(tizen_base::Bundle b) override {
68     if (ops_.base.control)
69       return ops_.base.control(b.GetHandle(), data_);
70     return -1;
71   }
72
73   int OnTerminate() override {
74     if (ops_.base.terminate)
75       return ops_.base.terminate(data_);
76     return -1;
77   }
78
79   int OnSetI18n() override {
80     if (ops_.base.set_i18n)
81       return ops_.base.set_i18n(data_);
82     return -1;
83   }
84
85   int OnSetEvent(IEvent::Type event) override {
86     if (!ops_.base.set_event)
87       return -1;
88
89     ops_.base.set_event((appcore_base_event)event, data_);
90     return 0;
91   }
92
93   int OnUnsetEvent(IEvent::Type event) override {
94     if (!ops_.base.unset_event)
95       return -1;
96
97     ops_.base.unset_event((appcore_base_event)event, data_);
98     return 0;
99   }
100
101   int OnTrimMemory() override {
102     if (!ops_.base.trim_memory)
103       return -1;
104
105     ops_.base.trim_memory(data_);
106     return 0;
107   }
108
109   int OnPause() override {
110     if (ops_.pause)
111       return ops_.pause(data_);
112     return -1;
113   }
114
115   int OnResume() override {
116     if (ops_.resume)
117       return ops_.resume(data_);
118     return -1;
119   }
120
121   void OnShow(int type, void* event) override {
122     if (ops_.window.show)
123       ops_.window.show(type, event, data_);
124   }
125
126   void OnHide(int type, void* event) override {
127     if (ops_.window.hide)
128       ops_.window.hide(type, event, data_);
129   }
130
131   void OnLower(int type, void* event) override {
132     if (ops_.window.lower)
133       ops_.window.lower(type, event, data_);
134   }
135
136   void OnVisibility(int type, void* event) override {
137     if (ops_.window.visibility)
138       ops_.window.visibility(type, event, data_);
139   }
140
141   void OnPreVisibility(int type, void* event) override {
142     if (ops_.window.pre_visibility)
143       ops_.window.pre_visibility(type, event, data_);
144   }
145
146   void OnAuxMessage(int type, void* event) override {
147     if (ops_.window.aux_message)
148       ops_.window.aux_message(type, event, data_);
149   }
150
151  private:
152   appcore_ui_base_ops ops_;
153   void* data_;
154 };
155
156 std::unique_ptr<AppCoreUi> __context;
157
158 int __on_control(bundle* b, void* data) {
159   return appcore_ui_base_on_control(b);
160 }
161
162 int __on_set_i18n(void* data) {
163   return appcore_ui_base_on_set_i18n();
164 }
165
166 void __on_set_event(enum appcore_base_event event, void* data) {
167   appcore_ui_base_on_set_event(event);
168 }
169
170 void __on_unset_event(enum appcore_base_event event, void* data) {
171   appcore_ui_base_on_unset_event(event);
172 }
173
174 void __on_trim_memory(void* data) {
175   appcore_ui_base_on_trim_memory();
176 }
177
178 int __on_receive(aul_type type, bundle* b, void* data) {
179   return appcore_ui_base_on_receive(type, b);
180 }
181
182 int __on_create(void* data) {
183   return appcore_ui_base_on_create();
184 }
185
186 int __on_terminate(void* data) {
187   return appcore_ui_base_on_terminate();
188 }
189
190 int __on_pause(void* data) {
191   return appcore_ui_base_on_pause();
192 }
193
194 int __on_resume(void* data) {
195   return appcore_ui_base_on_resume();
196 }
197
198 void __window_on_show(int type, void* event, void* data) {
199   appcore_ui_base_window_on_show(type, event);
200 }
201
202 void __window_on_hide(int type, void* event, void* data) {
203   appcore_ui_base_window_on_hide(type, event);
204 }
205
206 void __window_on_lower(int type, void* event, void* data) {
207   appcore_ui_base_window_on_lower(type, event);
208 }
209
210 void __window_on_visibility(int type, void* event, void* data) {
211   appcore_ui_base_window_on_visibility(type, event);
212 }
213
214 void __window_on_pre_visibility(int type, void* event, void* data) {
215   appcore_ui_base_window_on_pre_visibility(type, event);
216 }
217
218 void __window_on_aux_message(int type, void* event, void* data) {
219   appcore_ui_base_window_on_aux_message(type, event);
220 }
221
222 }  // namespace
223 }  // namespace tizen_cpp
224
225 using namespace tizen_cpp;
226
227 extern "C" EXPORT_API int appcore_ui_base_on_receive(aul_type type, bundle*b) {
228   if (__context.get() == nullptr)
229     return -1;
230   return __context->AppCoreUiBase::OnReceive(type, tizen_base::Bundle(b));
231 }
232
233 extern "C" EXPORT_API int appcore_ui_base_on_create(void) {
234   if (__context.get() == nullptr)
235     return -1;
236   return __context->AppCoreUiBase::OnCreate();
237 }
238
239 extern "C" EXPORT_API int appcore_ui_base_on_terminate(void) {
240   if (__context.get() == nullptr)
241     return -1;
242   return __context->AppCoreUiBase::OnTerminate();
243 }
244
245 extern "C" EXPORT_API int appcore_ui_base_on_pause(void) {
246   if (__context.get() == nullptr)
247     return -1;
248   return __context->AppCoreUiBase::OnPause();
249 }
250
251 extern "C" EXPORT_API int appcore_ui_base_on_resume(void) {
252   if (__context.get() == nullptr)
253     return -1;
254   return __context->AppCoreUiBase::OnResume();
255 }
256
257 extern "C" EXPORT_API int appcore_ui_base_on_control(bundle* b) {
258   if (__context.get() == nullptr)
259     return -1;
260   return __context->AppCoreUiBase::OnControl(
261       b ? tizen_base::Bundle(b) : tizen_base::Bundle());
262 }
263
264 extern "C" EXPORT_API int appcore_ui_base_on_trim_memory(void) {
265   if (__context.get() == nullptr)
266     return -1;
267   return __context->AppCoreUiBase::OnTrimMemory();
268 }
269
270 extern "C" EXPORT_API void appcore_ui_base_window_on_show(int type,
271     void* event) {
272   if (__context.get() == nullptr)
273     return;
274   __context->AppCoreUiBase::OnShow(type, event);
275 }
276
277 extern "C" EXPORT_API void appcore_ui_base_window_on_hide(int type,
278     void* event) {
279   if (__context.get() == nullptr)
280     return;
281   __context->AppCoreUiBase::OnHide(type, event);
282 }
283
284 extern "C" EXPORT_API void appcore_ui_base_window_on_lower(int type,
285     void* event) {
286   if (__context.get() == nullptr)
287     return;
288   __context->AppCoreUiBase::OnLower(type, event);
289 }
290
291 extern "C" EXPORT_API void appcore_ui_base_window_on_visibility(int type,
292     void* event) {
293   if (__context.get() == nullptr)
294     return;
295   __context->AppCoreUiBase::OnVisibility(type, event);
296 }
297
298 extern "C" EXPORT_API void appcore_ui_base_window_on_pre_visibility(int type,
299     void* event) {
300   if (__context.get() == nullptr)
301     return;
302   __context->AppCoreUiBase::OnPreVisibility(type, event);
303 }
304
305 extern "C" EXPORT_API void appcore_ui_base_window_on_aux_message(int type,
306     void* event) {
307   if (__context.get() == nullptr)
308     return;
309   __context->AppCoreUiBase::OnAuxMessage(type, event);
310 }
311
312 extern "C" EXPORT_API int appcore_ui_base_on_set_i18n(void) {
313   if (__context.get() == nullptr)
314     return -1;
315   return __context->AppCoreUiBase::OnSetI18n();
316 }
317
318 extern "C" EXPORT_API void appcore_ui_base_on_set_event(
319     enum appcore_base_event event) {
320   if (__context.get() == nullptr)
321     return;
322   __context->AppCoreUiBase::OnSetEvent(
323       static_cast<AppCoreBase::IEvent::Type>(event));
324 }
325
326 extern "C" EXPORT_API void appcore_ui_base_on_unset_event(
327     enum appcore_base_event event) {
328   if (__context.get() == nullptr)
329     return;
330   __context->AppCoreUiBase::OnUnsetEvent(
331       static_cast<AppCoreBase::IEvent::Type>(event));
332 }
333
334 extern "C" EXPORT_API int appcore_ui_base_init(appcore_ui_base_ops ops,
335     int argc, char** argv, void* data, unsigned int hint) {
336   __context.reset(new AppCoreUi(hint, ops, data));
337   try {
338     __context->Init(argc, argv);
339   } catch (const std::runtime_error&) {
340     return -1;
341   }
342
343   return 0;
344 }
345
346 extern "C" EXPORT_API void appcore_ui_base_fini(void) {
347   if (__context.get() == nullptr)
348     return;
349   __context->Fini();
350   __context.reset();
351 }
352
353 extern "C" EXPORT_API appcore_ui_base_ops
354 appcore_ui_base_get_default_ops(void) {
355   appcore_ui_base_ops ops;
356
357   ops.base.create = __on_create;
358   ops.base.control = __on_control;
359   ops.base.terminate = __on_terminate;
360   ops.base.receive = __on_receive;
361   ops.base.set_i18n = __on_set_i18n;
362   ops.base.init = nullptr;
363   ops.base.finish = nullptr;
364   ops.base.run = nullptr;
365   ops.base.exit = nullptr;
366   ops.base.set_event = __on_set_event;
367   ops.base.unset_event = __on_unset_event;
368   ops.base. trim_memory = __on_trim_memory;
369   ops.pause = __on_pause;
370   ops.resume = __on_resume;
371   ops.window.show = __window_on_show;
372   ops.window.hide = __window_on_hide;
373   ops.window.lower = __window_on_lower;
374   ops.window.visibility = __window_on_visibility;
375   ops.window.pre_visibility = __window_on_pre_visibility;
376   ops.window.aux_message = __window_on_aux_message;
377
378   return ops;
379 }
380
381 extern "C" EXPORT_API void appcore_ui_base_pause(void) {
382   if (__context.get() == nullptr)
383     return;
384   __context->Pause();
385 }
386
387 extern "C" EXPORT_API void appcore_ui_base_resume(void) {
388   if (__context.get() == nullptr)
389     return;
390   __context->Resume();
391 }
392
393 extern "C" EXPORT_API bool appcore_ui_base_is_resumed(void) {
394   if (__context.get() == nullptr)
395     return false;
396   return __context->IsResumed();
397 }
398
399 extern "C" EXPORT_API void appcore_ui_base_exit(void) {
400   if (__context.get() == nullptr)
401     return;
402   __context->Exit();
403 }
404
405 extern "C" EXPORT_API int appcore_ui_base_group_add() {
406   if (__context.get() == nullptr)
407     return -1;
408   return __context->GroupAdd();
409 }
410
411 extern "C" EXPORT_API void appcore_ui_base_group_remove() {
412   if (__context.get() == nullptr)
413     return;
414   __context->GroupRemove();
415 }
416
417 extern "C" EXPORT_API unsigned int appcore_ui_base_get_main_window(void) {
418   if (__context.get() == nullptr)
419     return 0;
420   return __context->GetMainWindow();
421 }
422
423 extern "C" EXPORT_API unsigned int appcore_ui_base_get_main_surface(void) {
424   if (__context.get() == nullptr)
425     return 0;
426   return __context->GetMainSurface();
427 }
428
429 extern "C" EXPORT_API int appcore_ui_base_get_hint(void) {
430   if (__context.get() == nullptr)
431     return 0;
432   return __context->GetHint();
433 }
434
435 extern "C" EXPORT_API bool appcore_ui_base_get_bg_state(void) {
436   if (__context.get() == nullptr)
437     return false;
438   return __context->GetBgState();
439 }
440
441 extern "C" EXPORT_API void appcore_ui_base_set_bg_state(bool bg_state) {
442   if (__context.get() == nullptr)
443     return;
444   __context->SetBgState(bg_state);
445 }
446
447 extern "C" EXPORT_API void appcore_ui_base_set_system_resource_reclaiming(
448     bool enable) {
449   if (__context.get() == nullptr)
450     return;
451   __context->SetSystemResourceReclaiming(enable);
452 }