Fix wrong log formats
[platform/core/appfw/ui-gadget-1.git] / src / ug.c
1 /*
2  *  UI Gadget
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <stdio.h>
26
27 #include "ug.h"
28 #include "ug-module.h"
29 #include "ug-manager.h"
30 #include "ug-dbg.h"
31
32 #ifndef UG_API
33 #define UG_API __attribute__ ((visibility("default")))
34 #endif
35
36 /* LCOV_EXCL_START */
37 ui_gadget_h ug_root_create(void)
38 {
39         ui_gadget_h ug;
40
41         ug = calloc(1, sizeof(struct ui_gadget_s));
42         if (!ug) {
43                 _ERR("ug root create failed: Memory allocation failed");
44                 return NULL;
45         }
46
47         ug->mode = UG_MODE_FULLVIEW;
48         ug->state = UG_STATE_RUNNING;
49         ug->children = NULL;
50
51         return ug;
52 }
53 /* LCOV_EXCL_STOP */
54
55 /* LCOV_EXCL_START */
56 int ug_free(ui_gadget_h ug)
57 {
58         if (!ug) {
59                 _ERR("ug free failed: Invalid ug");
60                 errno = EINVAL;
61                 return -1;
62         }
63
64         if (ug->module)
65                 ug_module_unload(ug->module);
66         if (ug->name) {
67                 free((void *)ug->name);
68                 ug->name = NULL;
69         }
70         if (ug->app_control) {
71                 app_control_destroy(ug->app_control);
72                 ug->app_control = NULL;
73         }
74         free(ug);
75         ug = NULL;
76         return 0;
77 }
78 /* LCOV_EXCL_STOP */
79
80 UG_API ui_gadget_h ug_create(ui_gadget_h parent,
81                                    const char *name,
82                                    enum ug_mode mode,
83                                    app_control_h app_control, struct ug_cbs *cbs)
84 {
85         if (!name) {
86                 _ERR("ug_create() failed: Invalid name");
87                 errno = EINVAL;
88                 return NULL;
89         }
90
91         if (mode < UG_MODE_FULLVIEW || mode >= UG_MODE_INVALID) {
92                 _ERR("ug_create() failed: Invalid mode");
93                 errno = EINVAL;
94                 return NULL;
95         }
96
97         return ugman_ug_load(parent, name, mode, app_control, cbs);
98 }
99
100 #ifndef UG_WAYLAND
101 UG_API int ug_init(void *disp, unsigned long xid, void *win, enum ug_option opt)
102 {
103         if (!win || !xid || !disp) {
104                 _ERR("ug_init() failed: Invalid arguments");
105                 return -1;
106         }
107
108         if (opt < UG_OPT_INDICATOR_ENABLE || opt >= UG_OPT_MAX) {
109                 _ERR("ug_init() failed: Invalid option");
110                 return -1;
111         }
112
113         return ugman_init((Display *)disp, (Window)xid, win, opt);
114 }
115 #else
116
117 /* LCOV_EXCL_START */
118 UG_API int ug_init(void *disp, unsigned long xid, void *win, enum ug_option opt)
119 {
120         if (opt < UG_OPT_INDICATOR_ENABLE || opt >= UG_OPT_MAX) {
121                 _ERR("ug_init() failed: Invalid option");
122                 return -1;
123         }
124
125         return ugman_init(win, opt);
126 }
127 /* LCOV_EXCL_STOP */
128 #endif
129
130 /* LCOV_EXCL_START */
131 UG_API int UG_INIT_EFL(void *win, enum ug_option opt)
132 {
133         return ug_init_efl((Evas_Object *)win, opt);
134 }
135 /* LCOV_EXCL_STOP */
136
137 UG_API int ug_init_efl(Evas_Object *win, enum ug_option opt)
138 {
139         if (!win) {
140                 _ERR("ug_init_efl() failed: Invalid arguments");
141                 return -1;
142         }
143
144         if (opt < UG_OPT_INDICATOR_ENABLE || opt >= UG_OPT_MAX) {
145                 _ERR("ug_init_efl() failed: Invalid option");
146                 return -1;
147         }
148
149         return ugman_init_efl(win, opt);
150 }
151
152 UG_API int ug_pause(void)
153 {
154         return ugman_pause();
155 }
156
157 UG_API int ug_pause_ug(ui_gadget_h ug)
158 {
159         if (!ug || !ugman_ug_exist(ug)) {
160                 _ERR("ug_pause_ug() failed: Invalid ug");
161                 errno = EINVAL;
162                 return -1;
163         }
164
165         return ugman_pause_ug(ug);
166 }
167
168 UG_API int ug_resume(void)
169 {
170         return ugman_resume();
171 }
172
173 UG_API int ug_resume_ug(ui_gadget_h ug)
174 {
175         if (!ug || !ugman_ug_exist(ug)) {
176                 _ERR("ug_resume_ug() failed: Invalid ug");
177                 errno = EINVAL;
178                 return -1;
179         }
180
181         return ugman_resume_ug(ug);
182 }
183
184 UG_API int ug_destroy(ui_gadget_h ug)
185 {
186         return ugman_ug_del(ug);
187 }
188
189 UG_API int ug_destroy_all(void)
190 {
191         return ugman_ug_del_all();
192 }
193
194 UG_API int ug_destroy_me(ui_gadget_h ug)
195 {
196         if (!ug || !ugman_ug_exist(ug)) {
197                 _ERR("ug_destroy_me() failed: Invalid ug");
198                 errno = EINVAL;
199                 return -1;
200         }
201
202         if (ug->state == UG_STATE_DESTROYING) {
203                 _ERR("ug_destory_me() failed:ug(%p) is already on destroying", ug);
204                 return -1;
205         }
206
207         if (!ug->cbs.destroy_cb) {
208                 _ERR("ug_destroy_me() failed: destroy callback does not "
209                         "exist");
210                 return -1;
211         }
212
213         ug->cbs.destroy_cb(ug, ug->cbs.priv);
214         return 0;
215 }
216
217 UG_API void *ug_get_layout(ui_gadget_h ug)
218 {
219         if (!ug || !ugman_ug_exist(ug)) {
220                 _ERR("ug_get_layout() failed: Invalid ug");
221                 errno = EINVAL;
222                 return NULL;
223         }
224         return ug->layout;
225 }
226
227 UG_API void *ug_get_parent_layout(ui_gadget_h ug)
228 {
229         ui_gadget_h parent;
230         if (!ug || !ugman_ug_exist(ug)) {
231                 _ERR("ug_get_parent_layout() failed: Invalid ug");
232                 errno = EINVAL;
233                 return NULL;
234         }
235
236         parent = ug->parent;
237
238         if (parent)
239                 return parent->layout;
240         return NULL;
241 }
242
243 UG_API enum ug_mode ug_get_mode(ui_gadget_h ug)
244 {
245         if (!ug || !ugman_ug_exist(ug)) {
246                 _ERR("ug_get_mode() failed: Invalid ug");
247                 errno = EINVAL;
248                 return UG_MODE_INVALID;
249         }
250
251         return ug->mode;
252 }
253
254 UG_API void *ug_get_window(void)
255 {
256         return ugman_get_window();
257 }
258
259 UG_API void *ug_get_conformant(void)
260 {
261         return ugman_get_conformant();
262 }
263
264 UG_API int ug_send_event(enum ug_event event)
265 {
266         if (event <= UG_EVENT_NONE || event >= UG_EVENT_MAX) {
267                 _ERR("ug_send_event() failed: Invalid event");
268                 return -1;
269         }
270
271         return ugman_send_event(event);
272 }
273
274 UG_API int ug_send_key_event(enum ug_key_event event)
275 {
276         if (event <= UG_KEY_EVENT_NONE || event >= UG_KEY_EVENT_MAX) {
277                 _ERR("ug_send_key_event() failed: Invalid event");
278                 return -1;
279         }
280
281         return ugman_send_key_event(event);
282 }
283
284 UG_API int ug_send_result(ui_gadget_h ug, app_control_h send)
285 {
286         app_control_h send_dup = NULL;
287
288         if (!ug || !ugman_ug_exist(ug)) {
289                 _ERR("ug_send_result() failed: Invalid ug");
290                 errno = EINVAL;
291                 return -1;
292         }
293
294         if (!ug->cbs.result_cb) {
295                 _ERR("ug_send_result() failed: result callback does not exist");
296                 return -1;
297         }
298
299         if (send) {
300                 app_control_clone(&send_dup, send);
301                 if (!send_dup) {
302                         _ERR("ug_send_result() failed: app_control_destroy failed");
303                         return -1;
304                 }
305         }
306
307         ug->cbs.result_cb(ug, send_dup, ug->cbs.priv);
308
309         if (send_dup)
310                 app_control_destroy(send_dup);
311
312         return 0;
313 }
314
315 UG_API int ug_send_result_full(ui_gadget_h ug, app_control_h send, app_control_result_e result)
316 {
317         app_control_h send_dup = NULL;
318         char tmp_result[4] = {0,};
319
320         if (!ug || !ugman_ug_exist(ug)) {
321                 _ERR("ug_send_result() failed: Invalid ug");
322                 errno = EINVAL;
323                 return -1;
324         }
325
326         if (!ug->cbs.result_cb) {
327                 _ERR("ug_send_result() failed: result callback does not exist");
328                 return -1;
329         }
330
331         if (send) {
332                 app_control_clone(&send_dup, send);
333                 if (!send_dup) {
334                         _ERR("ug_send_result() failed: app_control_destroy failed");
335                         return -1;
336                 }
337         }
338
339         snprintf(tmp_result, 4, "%d", result);
340
341         app_control_add_extra_data(send_dup, UG_APP_CONTROL_DATA_RESULT, (const char*)tmp_result);
342
343         ug->cbs.result_cb(ug, send_dup, ug->cbs.priv);
344
345         if (send_dup)
346                 app_control_destroy(send_dup);
347
348         return 0;
349 }
350
351 UG_API int ug_send_message(ui_gadget_h ug, app_control_h msg)
352 {
353         int r;
354
355         app_control_h msg_dup = NULL;
356         if (msg) {
357                 app_control_clone(&msg_dup, msg);
358                 if (!msg_dup) {
359                         _ERR("ug_send_message() failed: app_control_destroy failed");
360                         return -1;
361                 }
362         }
363
364         r = ugman_send_message(ug, msg_dup);
365
366         if (msg_dup)
367                 app_control_destroy(msg_dup);
368
369         return r;
370 }
371
372 UG_API int ug_disable_effect(ui_gadget_h ug)
373 {
374         if (!ug) {
375                 _ERR("ug input param is null");
376                 return -1;
377         }
378         if (ug->layout_state != UG_LAYOUT_INIT) {
379                 _ERR("ug_disable_effect() failed: ug has already been shown");
380                 return -1;
381         }
382         ug->layout_state = UG_LAYOUT_NOEFFECT;
383
384         return 0;
385 }
386
387 UG_API int ug_is_installed(const char *name)
388 {
389         int ret = 1;
390         char *ug_file_path = NULL;
391
392         if (name == NULL) {
393                 _ERR("name is null");
394                 return -1;
395         }
396
397         if (ug_module_get_file_path(name, &ug_file_path) < 0)
398                 ret = 0;
399
400         if (ug_file_path)
401                 free(ug_file_path);
402
403         return ret;
404 }
405
406 #ifdef ENABLE_UG_CREATE_CB
407 UG_API int ug_create_cb(void (*create_cb)(char *, char *, char *, void *), void *user_data)
408 {
409         int ret;
410
411         ret = ugman_create_cb(create_cb, user_data);
412         if (ret == -1)
413                 _ERR("trace cb register fail");
414
415         return ret;
416 }
417 #endif