Release version 1.5.0
[platform/core/appfw/appcore-widget.git] / include / widget_app.hpp
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 #ifndef TIZEN_APPFW_WIDGET_APP_HPP_
18 #define TIZEN_APPFW_WIDGET_APP_HPP_
19
20 #include <widget_app.h>
21 #include <widget_app_efl.h>
22
23 #include <ctime>
24 #include <list>
25 #include <memory>
26
27 #include <app_common.hpp>
28 #include <bundle_cpp.h>
29
30 namespace tizen_appfw {
31
32 class WidgetAppBase : public app_common::AppBase<
33     decltype(widget_app_add_event_handler)*,
34     decltype(widget_app_remove_event_handler)*> {
35  public:
36   using Remover = decltype(widget_app_remove_event_handler)*;
37
38   WidgetAppBase()
39       : AppBase(widget_app_add_event_handler,
40                 widget_app_remove_event_handler) {}
41
42   virtual void OnCreate() {}
43   virtual void OnTerminate() {}
44
45   class InstanceBase {
46    public:
47     class Factory {
48      public:
49       ~Factory() = default;
50       virtual std::unique_ptr<InstanceBase> Create(widget_context_h h) = 0;
51     };
52
53     enum class DestroyType {
54       PERMANENT = 0x00,
55       TEMPORARY = 0x01,
56     };
57
58     InstanceBase(widget_context_h h) : handle_(h) {}
59     virtual ~InstanceBase() = default;
60
61     virtual void OnCreate(const tizen_base::Bundle& content, int w, int h) = 0;
62     virtual void OnDestroy(DestroyType reason, tizen_base::Bundle* content) = 0;
63     virtual void OnPause() = 0;
64     virtual void OnResume() = 0;
65     virtual void OnResize(int w, int h) = 0;
66     virtual void OnUpdate(const tizen_base::Bundle& content, bool force) = 0;
67     widget_context_h GetHandle() const {
68       return handle_;
69     }
70
71     void Finish() {
72       widget_app_terminate_context(handle_);
73     }
74
75     std::string GetId() const {
76       return std::string(widget_app_get_id(handle_));
77     }
78
79     void SetContextInfo(const tizen_base::Bundle& info) {
80       widget_app_context_set_content_info(handle_, info.GetHandle());
81     }
82
83     void SetTitle(const std::string& title) {
84       widget_app_context_set_title(handle_, title.c_str());
85     }
86
87     Evas_Object* GetElmWin() const {
88       Evas_Object* win = nullptr;
89       widget_app_get_elm_win(handle_, &win);
90       return win;
91     }
92
93    private:
94     widget_context_h handle_;
95   };
96
97   int Run(int argc, char** argv,
98       std::unique_ptr<InstanceBase::Factory> factory) {
99     factory_ = std::move(factory);
100     widget_app_lifecycle_callback_s callback = {
101       create : [](void *user_data) -> widget_class_h {
102         WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
103         b->OnCreate();
104         widget_instance_lifecycle_callback_s callback = {
105           create : [](widget_context_h context, bundle* content, int w, int h,
106               void* user_data) -> int {
107             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
108             auto i = b->factory_->Create(context);
109             i->OnCreate(tizen_base::Bundle(content, false, false), w, h);
110             b->instances_.push_back(std::move(i));
111             return 0;
112           },
113           destroy : [](widget_context_h context,
114               widget_app_destroy_type_e reason, bundle* content,
115               void* user_data) -> int {
116             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
117
118             for (auto& i : b->instances_) {
119               if (i->GetHandle() == context) {
120                 tizen_base::Bundle bd(content, false, false);
121                 i->OnDestroy(
122                     static_cast<InstanceBase::DestroyType>(reason), &bd);
123                 b->instances_.remove(i);
124                 break;
125               }
126             }
127
128             return 0;
129           },
130           pause : [](widget_context_h context, void* user_data) -> int {
131             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
132             auto* i = b->FindInst(context);
133             if (!i)
134               return -1;
135             i->OnPause();
136             return 0;
137           },
138           resume : [](widget_context_h context, void* user_data) -> int {
139             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
140             auto* i = b->FindInst(context);
141             if (!i)
142               return -1;
143             i->OnResume();
144             return 0;
145           },
146           resize : [](widget_context_h context, int w, int h,
147               void* user_data) -> int {
148             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
149             auto* i = b->FindInst(context);
150             if (!i)
151               return -1;
152             i->OnResize(w, h);
153             return 0;
154           },
155           update : [](widget_context_h context, bundle* content, int force,
156               void* user_data) -> int {
157             WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
158             auto* i = b->FindInst(context);
159             if (!i)
160               return -1;
161             i->OnUpdate(tizen_base::Bundle(content), force);
162             return 0;
163           }
164         };
165         auto h = widget_app_class_create(callback, user_data);
166         return h;
167       },
168       terminate : [](void* user_data) {
169         WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
170         b->OnTerminate();
171       }
172     };
173
174     return widget_app_main(argc, argv, &callback, this);
175   }
176
177   void Exit() noexcept {
178     widget_app_exit();
179   }
180
181  private:
182   InstanceBase* FindInst(widget_context_h h) {
183     for (auto& i : instances_) {
184       if (i->GetHandle() == h) {
185         return i.get();
186       }
187     }
188
189     return nullptr;
190   }
191
192   std::unique_ptr<InstanceBase::Factory> factory_;
193   std::list<std::unique_ptr<InstanceBase>> instances_;
194 };
195
196 }  // namespace tizen_appfw
197
198 #endif  // TIZEN_APPFW_WIDGET_APP_HPP_