Keep compatible with Wayland
[profile/ivi/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 ui_gadget_h ug_root_create(void)
37 {
38         ui_gadget_h ug;
39
40         ug = calloc(1, sizeof(struct ui_gadget_s));
41         if (!ug) {
42                 _ERR("ug root create failed: Memory allocation failed\n");
43                 return NULL;
44         }
45
46         ug->mode = UG_MODE_FULLVIEW;
47         ug->state = UG_STATE_RUNNING;
48         ug->children = NULL;
49
50         return ug;
51 }
52
53 int ug_free(ui_gadget_h ug)
54 {
55         if (!ug) {
56                 _ERR("ug free failed: Invalid ug\n");
57                 errno = EINVAL;
58                 return -1;
59         }
60
61         if (ug->module)
62                 ug_module_unload(ug->module);
63         if (ug->name)
64                 free((void *)ug->name);
65         if (ug->service)
66                 service_destroy(ug->service);
67         free(ug);
68         return 0;
69 }
70
71 UG_API ui_gadget_h ug_create(ui_gadget_h parent,
72                                    const char *name,
73                                    enum ug_mode mode,
74                                    service_h service, struct ug_cbs *cbs)
75 {
76         if (!name) {
77                 _ERR("ug_create() failed: Invalid name\n");
78                 errno = EINVAL;
79                 return NULL;
80         }
81
82         if (mode < UG_MODE_FULLVIEW || mode >= UG_MODE_INVALID) {
83                 _ERR("ug_create() failed: Invalid mode\n");
84                 errno = EINVAL;
85                 return NULL;
86         }
87
88         return ugman_ug_load(parent, name, mode, service, cbs);
89 }
90
91 UG_API int ug_init(Display *disp, Window xid, void *win, enum ug_option opt)
92 {
93         /* XXX Start Hooks for Wayland */
94         char *d = getenv("DISPLAY");
95         /* xid and disp are not needed by wayland instead of only by X.*/
96         if (!win || (d && (!xid || !disp))) {
97                 _ERR("ug_init() failed: Invalid arguments\n");
98                 return -1;
99         }
100         /* XXX End Hooks for Wayland */
101
102         if (opt < UG_OPT_INDICATOR_ENABLE || opt >= UG_OPT_MAX) {
103                 _ERR("ug_init() failed: Invalid option\n");
104                 return -1;
105         }
106
107         return ugman_init(disp, xid, win, opt);
108 }
109
110 UG_API int ug_pause(void)
111 {
112         return ugman_pause();
113 }
114
115 UG_API int ug_resume(void)
116 {
117         return ugman_resume();
118 }
119
120 UG_API int ug_destroy(ui_gadget_h ug)
121 {
122         return ugman_ug_del(ug);
123 }
124
125 UG_API int ug_destroy_all(void)
126 {
127         return ugman_ug_del_all();
128 }
129
130 UG_API int ug_destroy_me(ui_gadget_h ug)
131 {
132         if (!ug || !ugman_ug_exist(ug)) {
133                 _ERR("ug_destroy_me() failed: Invalid ug\n");
134                 errno = EINVAL;
135                 return -1;
136         }
137
138         if (ug->state == UG_STATE_DESTROYING) {
139                 _ERR("ug_destory_me() failed:ug is alreay on destroying");
140                 return -1;
141         }
142
143         if (!ug->cbs.destroy_cb) {
144                 _ERR("ug_destroy_me() failed: destroy callback does not "
145                         "exist\n");
146                 return -1;
147         }
148
149         ug->cbs.destroy_cb(ug, ug->cbs.priv);
150         return 0;
151 }
152
153 UG_API void *ug_get_layout(ui_gadget_h ug)
154 {
155         if (!ug || !ugman_ug_exist(ug)) {
156                 _ERR("ug_get_layout() failed: Invalid ug\n");
157                 errno = EINVAL;
158                 return NULL;
159         }
160         return ug->layout;
161 }
162
163 UG_API void *ug_get_parent_layout(ui_gadget_h ug)
164 {
165         ui_gadget_h parent;
166         if (!ug || !ugman_ug_exist(ug)) {
167                 _ERR("ug_get_parent_layout() failed: Invalid ug\n");
168                 errno = EINVAL;
169                 return NULL;
170         }
171
172         parent = ug->parent;
173
174         if (parent)
175                 return parent->layout;
176         return NULL;
177 }
178
179 UG_API enum ug_mode ug_get_mode(ui_gadget_h ug)
180 {
181         if (!ug || !ugman_ug_exist(ug)) {
182                 _ERR("ug_get_mode() failed: Invalid ug\n");
183                 errno = EINVAL;
184                 return UG_MODE_INVALID;
185         }
186
187         return ug->mode;
188 }
189
190 UG_API void *ug_get_window(void)
191 {
192         return ugman_get_window();
193 }
194
195 UG_API int ug_send_event(enum ug_event event)
196 {
197         if (event <= UG_EVENT_NONE || event >= UG_EVENT_MAX) {
198                 _ERR("ug_send_event() failed: Invalid event\n");
199                 return -1;
200         }
201
202         return ugman_send_event(event);
203 }
204
205 UG_API int ug_send_key_event(enum ug_key_event event)
206 {
207         if (event <= UG_KEY_EVENT_NONE || event >= UG_KEY_EVENT_MAX) {
208                 _ERR("ug_send_key_event() failed: Invalid event\n");
209                 return -1;
210         }
211
212         return ugman_send_key_event(event);
213 }
214
215 UG_API int ug_send_result(ui_gadget_h ug, service_h result)
216 {
217         service_h result_dup = NULL;
218
219         if (!ug || !ugman_ug_exist(ug)) {
220                 _ERR("ug_send_result() failed: Invalid ug\n");
221                 errno = EINVAL;
222                 return -1;
223         }
224
225         if (!ug->cbs.result_cb) {
226                 _ERR("ug_send_result() failed: result callback does not exist\n");
227                 return -1;
228         }
229
230         if (result) {
231                 service_clone(&result_dup, result);
232                 if (!result_dup) {
233                         _ERR("ug_send_result() failed: service_destroy failed\n");
234                         return -1;
235                 }
236         }
237
238         ug->cbs.result_cb(ug, result_dup, ug->cbs.priv);
239
240         if (result_dup)
241                 service_destroy(result_dup);
242
243         return 0;
244 }
245
246 UG_API int ug_send_message(ui_gadget_h ug, service_h msg)
247 {
248         int r;
249
250         service_h msg_dup = NULL;
251         if (msg) {
252                 service_clone(&msg_dup, msg);
253                 if (!msg_dup) {
254                         _ERR("ug_send_message() failed: service_destroy failed\n");
255                         return -1;
256                 }
257         }
258
259         r = ugman_send_message(ug, msg_dup);
260
261         if (msg_dup)
262                 service_destroy(msg_dup);
263
264         return r;
265 }
266
267 UG_API int ug_disable_effect(ui_gadget_h ug)
268 {
269         if (ug->layout_state != UG_LAYOUT_INIT) {
270                 _ERR("ug_disable_effect() failed: ug has already been shown\n");
271                 return -1;
272         }
273         ug->layout_state = UG_LAYOUT_NOEFFECT;
274
275         return 0;
276 }