Add capi for C# binding
[platform/core/uifw/libscl-ui-nui.git] / capi / src / cscl-ui-nui.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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 <dlog.h>
18 #include <sclui.h>
19 #include <Elementary.h>
20 #include "cscl-ui-nui.h"
21 #include "cscl-ui-graphics-backend.h"
22 #include "cscl-ui-window-backend.h"
23 #include "cscl-ui-event-callback.h"
24
25 using namespace std;
26 using namespace scl;
27
28 #ifdef LOG_TAG
29 #undef LOG_TAG
30 #endif
31 #define LOG_TAG "CSCLUINUI"
32
33 #define USE_ELM_WIN 1
34
35 #ifdef USE_ELM_WIN
36 static Evas_Object *g_win = NULL;
37 #endif
38
39 static CSCLUI *g_ui = NULL;
40
41 static scl_nui_draw_text_cb g_draw_text_cb;
42 static void *g_draw_text_cb_data = NULL;
43
44 static scl_nui_draw_image_cb g_draw_image_cb = NULL;
45 static void *g_draw_image_cb_data = NULL;
46
47 static scl_nui_draw_rectangle_cb g_draw_rectangle_cb = NULL;
48 static void *g_draw_rectangle_cb_data = NULL;
49
50 static scl_nui_update_window_cb g_update_window_cb = NULL;
51 static void *g_update_window_cb_data = NULL;
52
53 void CUIGraphicsBackendCallback::on_draw_text(const SclFontInfo& font_info, const SclColor& color, const char *str, int pos_x, int pos_y, int w, int h,
54                                       SCLLabelAlignment align, int padding_x, int padding_y, int inner_width, int inner_height, void* user_data)
55                       //const char *str, int x, int y, int w, int h, int fontsize, void* user_data)
56 {
57     LOGD("draw text : %p", g_draw_text_cb);
58     if (g_draw_text_cb) {
59         //g_draw_text_cb(str, pos_x, pos_y, w, h, font_info.font_size, user_data);
60         /*
61         FontInfo fi;
62         strcpy(fi.font_name, font_info.font_name);
63         fi.font_size = font_info.font_size;
64         fi.is_bold = font_info.is_bold;
65         fi.is_italic = font_info.is_italic;
66
67         Color ci;
68         ci.a = color.a;
69         ci.b = color.b;
70         ci.g = color.g;
71         ci.r = color.r;
72         */
73
74         /*
75         g_draw_text_cb(fi, ci, str, pos_x, pos_y, w, h,
76                        (LabelAlignment)align, padding_x, padding_y, inner_width, inner_height, user_data);
77         */
78         g_draw_text_cb(font_info.font_name, font_info.font_size, font_info.is_bold, font_info.is_italic,
79                        color.r, color.g, color.b, color.a,
80                        str, pos_x, pos_y, w, h,
81                        (LabelAlignment)align, padding_x, padding_y, inner_width, inner_height, user_data);
82     }
83 }
84
85 void CUIGraphicsBackendCallback::on_draw_image(const char *image_path, int dest_x, int dest_y, int dest_weight, int dest_height, int src_x, int src_y, int src_width, int src_height, void* user_data)
86 {
87     LOGD("draw image : %p", g_draw_image_cb);
88     if (g_draw_image_cb) {
89         g_draw_image_cb(image_path, dest_x, dest_y, dest_weight, dest_height, src_x, src_y, src_width, src_height, user_data);
90     }
91 }
92
93 void CUIGraphicsBackendCallback::on_draw_rectangle(int pos_x, int pos_y, int width, int height, bool fill, int fill_color_r, int fill_color_g, int fill_color_b, int fill_color_a, void* user_data)
94 {
95     LOGD("draw rectangle : %p", g_draw_rectangle_cb);
96     if (g_draw_rectangle_cb) {
97         g_draw_rectangle_cb(pos_x, pos_y, width, height,
98                             fill, fill_color_r, fill_color_g, fill_color_b, fill_color_a, user_data);
99     }
100 }
101
102 void CUIWindowBackendCallback::update_window(int x, int y, int width, int height, void* user_data)
103 {
104     LOGD("update_window : %p", g_update_window_cb);
105     if (g_update_window_cb) {
106         g_update_window_cb(x, y, width, height, user_data);
107     }
108 }
109
110 SCLEventReturnType CUIEventCallback::on_event_key_clicked(SclUIEventDesc event_desc)
111 {
112     LOGI("");
113     return SCL_EVENT_PASS_ON;
114 }
115
116 SCLEventReturnType CUIEventCallback::on_event_notification(SCLUINotiType noti_type, SclNotiDesc *etc_info)
117 {
118     LOGI("");
119     return SCL_EVENT_PASS_ON;
120 }
121
122 SCLEventReturnType CUIEventCallback::on_event_drag_state_changed(SclUIEventDesc event_desc)
123 {
124     LOGI("");
125     return SCL_EVENT_PASS_ON;
126 }
127
128 static CUIGraphicsBackendCallback callback;
129
130 static CUIWindowBackendCallback window_callback;
131
132 static CUIEventCallback event_callback;
133
134 Evas_Object *get_main_window()
135 {
136     return g_win;
137 }
138
139 EXPORT_API int scl_nui_init(const char *entry_filepath)
140 {
141 #ifdef USE_ELM_WIN
142     elm_init(0, NULL);
143 #endif
144
145     //LOGI("################## Hello ################");
146
147     //sleep(10);
148
149     if (!g_ui) {
150         g_ui = new CSCLUI;
151     }
152
153     if (!g_ui)
154         return 1;
155
156 #ifdef USE_ELM_WIN
157     g_win = elm_win_add(NULL, "test", ELM_WIN_UTILITY);
158 #endif
159
160     g_ui->init(g_win, SCL_PARSER_TYPE_XML, entry_filepath);
161
162     g_ui->set_graphics_backend_callback(&callback, NULL);
163
164     g_ui->set_window_backend_callback(&window_callback, NULL);
165
166     /* Default ISE callback */
167     g_ui->set_ui_event_callback(&event_callback);
168
169     return 0;
170 }
171
172 EXPORT_API int scl_nui_fini()
173 {
174     int ret = 0;
175
176 #ifdef USE_ELM_WIN
177     if (g_win)
178         evas_object_del(g_win);
179
180     g_win = NULL;
181 #endif
182
183     if (g_ui)
184         g_ui->fini();
185     else
186         ret = 1;
187
188 #ifdef USE_ELM_WIN
189     elm_shutdown();
190 #endif
191
192     return ret;
193 }
194
195 EXPORT_API int scl_nui_set_input_mode(const char *input_mode)
196 {
197     if (!g_ui)
198         return 1;
199
200     g_ui->set_input_mode(input_mode);
201
202     return 0;
203 }
204
205 EXPORT_API int scl_nui_set_update_pending(bool pend)
206 {
207     if (!g_ui)
208         return 1;
209
210     g_ui->set_update_pending(pend);
211     return 0;
212 }
213
214 EXPORT_API int scl_nui_set_draw_text_cb(scl_nui_draw_text_cb callback, void *user_data)
215 {
216     g_draw_text_cb = callback;
217     g_draw_text_cb_data = user_data;
218
219     return 0;
220 }
221
222 EXPORT_API int scl_nui_set_draw_image_cb(scl_nui_draw_image_cb callback, void *user_data)
223 {
224     g_draw_image_cb = callback;
225     g_draw_image_cb_data = user_data;
226
227     return 0;
228 }
229
230 EXPORT_API int scl_nui_set_draw_rectangle_cb(scl_nui_draw_rectangle_cb callback, void *user_data)
231 {
232     g_draw_rectangle_cb = callback;
233     g_draw_rectangle_cb_data = user_data;
234
235     return 0;
236 }
237
238 EXPORT_API int scl_nui_set_cur_sublayout(const char *sub_layout_name)
239 {
240     if (!g_ui)
241         return 1;
242
243     LOGD("cur sublayout : %s", sub_layout_name);
244
245     g_ui->set_cur_sublayout(sub_layout_name);
246
247     return 0;
248 }
249
250 EXPORT_API int scl_nui_set_string_substitution(const char *original, const char *substitute)
251 {
252     if (!g_ui)
253         return 1;
254
255     LOGD("original : %s, substitute : %s", original, substitute);
256
257     g_ui->set_string_substitution(original, substitute);
258
259     return 0;
260 }
261
262 EXPORT_API int scl_nui_set_update_window_cb(scl_nui_update_window_cb callback, void *user_data)
263 {
264     g_update_window_cb = callback;
265     g_update_window_cb_data = user_data;
266
267     return 0;
268 }