sycn with master
[platform/framework/web/data-provider-slave.git] / src / client.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 <stdio.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <malloc.h>
21
22 #include <Elementary.h>
23 #include <Eina.h>
24 #include <Ecore.h>
25 #include <Ecore_X.h>
26
27 #include <dlog.h>
28 #include <aul.h>
29 #include <sqlite3.h>
30
31 #include <provider.h>
32
33 #include "critical_log.h"
34 #include "conf.h"
35 #include "debug.h"
36 #include "client.h"
37 #include "so_handler.h"
38 #include "lb.h"
39 #include "util.h"
40
41 static struct info {
42         Ecore_Timer *ping_timer;
43 } s_info = {
44         .ping_timer = NULL,
45 };
46
47 static int method_new(struct event_arg *arg, int *width, int *height, double *priority, void *data)
48 {
49         int ret;
50         DbgPrint("Create: pkgname[%s], id[%s], content[%s], timeout[%d], has_script[%d], period[%lf], cluster[%s], category[%s], skip[%d], abi[%s], size: %dx%d\n",
51                 arg->pkgname,
52                 arg->id,
53                 arg->info.lb_create.content,
54                 arg->info.lb_create.timeout,
55                 arg->info.lb_create.has_script,
56                 arg->info.lb_create.period,
57                 arg->info.lb_create.cluster, arg->info.lb_create.category,
58                 arg->info.lb_create.skip_need_to_create,
59                 arg->info.lb_create.abi,
60                 arg->info.lb_create.width,
61                 arg->info.lb_create.height);
62
63         ret = lb_create(arg->pkgname, arg->id,
64                         arg->info.lb_create.content,
65                         arg->info.lb_create.timeout,
66                         arg->info.lb_create.has_script,
67                         arg->info.lb_create.period,
68                         arg->info.lb_create.cluster,
69                         arg->info.lb_create.category,
70                         width, height, priority,
71                         arg->info.lb_create.skip_need_to_create,
72                         arg->info.lb_create.abi,
73                         &arg->info.lb_create.out_content,
74                         &arg->info.lb_create.out_title);
75
76         if (ret == 0) {
77                 if (arg->info.lb_create.width > 0 && arg->info.lb_create.height > 0) {
78                         if (*width != arg->info.lb_create.width || *height != arg->info.lb_create.height) {
79                                 int tmp;
80                                 tmp = lb_resize(arg->pkgname, arg->id, arg->info.lb_create.width, arg->info.lb_create.height);
81                                 DbgPrint("Resize[%dx%d] returns: %d\n", arg->info.lb_create.width, arg->info.lb_create.height, tmp);
82                         }
83                 }
84
85                 arg->info.lb_create.out_is_pinned_up = (lb_is_pinned_up(arg->pkgname, arg->id) == 1);
86         }
87
88         return ret;
89 }
90
91 static int method_renew(struct event_arg *arg, void *data)
92 {
93          int ret;
94          int w;
95          int h;
96          double priority;
97
98         DbgPrint("Re-create: pkgname[%s], id[%s], content[%s], timeout[%d], has_script[%d], period[%lf], cluster[%s], category[%s], abi[%s]\n",
99                 arg->pkgname, arg->id,
100                 arg->info.lb_recreate.content,
101                 arg->info.lb_recreate.timeout,
102                 arg->info.lb_recreate.has_script,
103                 arg->info.lb_recreate.period,
104                 arg->info.lb_recreate.cluster,
105                 arg->info.lb_recreate.category,
106                 arg->info.lb_recreate.abi);
107
108          ret = lb_create(arg->pkgname, arg->id,
109                         arg->info.lb_recreate.content,
110                         arg->info.lb_recreate.timeout,
111                         arg->info.lb_recreate.has_script,
112                         arg->info.lb_recreate.period,
113                         arg->info.lb_recreate.cluster,
114                         arg->info.lb_recreate.category,
115                         &w, &h, &priority,
116                         1,
117                         arg->info.lb_recreate.abi,
118                         &arg->info.lb_recreate.out_content,
119                         &arg->info.lb_recreate.out_title);
120         if (ret == 0) {
121                 if (w != arg->info.lb_recreate.width || h != arg->info.lb_recreate.height) {
122                         int tmp;
123                         tmp = lb_resize(arg->pkgname, arg->id, arg->info.lb_recreate.width, arg->info.lb_recreate.height);
124                         DbgPrint("Resize[%dx%d] returns: %d\n", arg->info.lb_recreate.width, arg->info.lb_recreate.height, tmp);
125                 } else {
126                         DbgPrint("No need to change the size: %dx%d\n", w, h);
127                 }
128
129                 arg->info.lb_recreate.out_is_pinned_up = (lb_is_pinned_up(arg->pkgname, arg->id) == 1);
130         }
131
132         return ret;
133 }
134
135 static int method_delete(struct event_arg *arg, void *data)
136 {
137         int ret;
138         DbgPrint("pkgname[%s] id[%s]\n", arg->pkgname, arg->id);
139         ret = lb_destroy(arg->pkgname, arg->id);
140         return ret;
141 }
142
143 static int method_content_event(struct event_arg *arg, void *data)
144 {
145         int ret;
146         struct event_info info;
147
148         info = arg->info.content_event.info;
149
150         ret = lb_script_event(arg->pkgname, arg->id,
151                                 arg->info.content_event.emission, arg->info.content_event.source,
152                                 &info);
153         return ret;
154 }
155
156 static int method_clicked(struct event_arg *arg, void *data)
157 {
158         int ret;
159
160         DbgPrint("pkgname[%s] id[%s] event[%s] timestamp[%lf] x[%lf] y[%lf]\n",
161                                                                 arg->pkgname, arg->id,
162                                                                 arg->info.clicked.event, arg->info.clicked.timestamp,
163                                                                 arg->info.clicked.x, arg->info.clicked.y);
164         ret = lb_clicked(arg->pkgname, arg->id,
165                         arg->info.clicked.event,
166                         arg->info.clicked.timestamp, arg->info.clicked.x, arg->info.clicked.y);
167
168         return ret;
169 }
170
171 static int method_text_signal(struct event_arg *arg, void *data)
172 {
173         int ret;
174         struct event_info info;
175
176         info = arg->info.text_signal.info;
177
178         DbgPrint("pkgname[%s] id[%s] emission[%s] source[%s]\n", arg->pkgname, arg->id, arg->info.text_signal.emission, arg->info.text_signal.source);
179         ret = lb_script_event(arg->pkgname, arg->id,
180                                 arg->info.text_signal.emission, arg->info.text_signal.source,
181                                 &info);
182
183         return ret;
184 }
185
186 static int method_resize(struct event_arg *arg, void *data)
187 {
188         int ret;
189
190         DbgPrint("pkgname[%s] id[%s] w[%d] h[%d]\n", arg->pkgname, arg->id, arg->info.resize.w, arg->info.resize.h);
191         ret = lb_resize(arg->pkgname, arg->id, arg->info.resize.w, arg->info.resize.h);
192
193         return ret;
194 }
195
196 static int method_set_period(struct event_arg *arg, void *data)
197 {
198         int ret;
199         DbgPrint("pkgname[%s] id[%s] period[%lf]\n", arg->pkgname, arg->id, arg->info.set_period.period);
200         ret = lb_set_period(arg->pkgname, arg->id, arg->info.set_period.period);
201         return ret;
202 }
203
204 static int method_change_group(struct event_arg *arg, void *data)
205 {
206         int ret;
207         DbgPrint("pkgname[%s] id[%s] cluster[%s] category[%s]\n", arg->pkgname, arg->id, arg->info.change_group.cluster, arg->info.change_group.category);
208         ret = lb_change_group(arg->pkgname, arg->id, arg->info.change_group.cluster, arg->info.change_group.category);
209         return ret;
210 }
211
212 static int method_pinup(struct event_arg *arg, void *data)
213 {
214         DbgPrint("pkgname[%s] id[%s] state[%d]\n", arg->pkgname, arg->id, arg->info.pinup.state);
215         arg->info.pinup.content_info = lb_pinup(arg->pkgname, arg->id, arg->info.pinup.state);
216         return arg->info.pinup.content_info ? 0 : -ENOTSUP;
217 }
218
219 static int method_update_content(struct event_arg *arg, void *data)
220 {
221         int ret;
222
223         if (!arg->id || !strlen(arg->id)) {
224                 DbgPrint("pkgname[%s] cluster[%s] category[%s]\n", arg->pkgname, arg->info.update_content.cluster, arg->info.update_content.category);
225                 ret = lb_update_all(arg->pkgname, arg->info.update_content.cluster, arg->info.update_content.category);
226         } else {
227                 DbgPrint("Update [%s]\n", arg->id);
228                 ret = lb_update(arg->pkgname, arg->id);
229         }
230
231         return ret;
232 }
233
234 static int method_pause(struct event_arg *arg, void *data)
235 {
236         lb_pause_all();
237         if (s_info.ping_timer)
238                 ecore_timer_freeze(s_info.ping_timer);
239
240         sqlite3_release_memory(SQLITE_FLUSH_MAX);
241         malloc_trim(0);
242         return 0;
243 }
244
245 static int method_resume(struct event_arg *arg, void *data)
246 {
247         lb_resume_all();
248         if (s_info.ping_timer)
249                 ecore_timer_thaw(s_info.ping_timer);
250         return 0;
251 }
252
253 static Eina_Bool send_ping_cb(void *data)
254 {
255         provider_send_ping();
256         return ECORE_CALLBACK_RENEW;
257 }
258
259 static int method_disconnected(struct event_arg *arg, void *data)
260 {
261         if (s_info.ping_timer) {
262                 ecore_timer_del(s_info.ping_timer);
263                 s_info.ping_timer = NULL;
264         }
265
266         elm_exit();
267         return 0;
268 }
269
270 static int method_connected(struct event_arg *arg, void *data)
271 {
272         int ret;
273         ret = provider_send_hello();
274         if (ret == 0) {
275                 s_info.ping_timer = ecore_timer_add(DEFAULT_PING_TIME, send_ping_cb, NULL);
276                 if (!s_info.ping_timer)
277                         ErrPrint("Failed to add a ping timer\n");
278         }
279
280         return 0;
281 }
282
283 static int method_pd_created(struct event_arg *arg, void *data)
284 {
285         int ret;
286
287         ret = lb_open_pd(arg->pkgname);
288         DbgPrint("%s Open PD: %d\n", arg->pkgname, ret);
289
290         return 0;
291 }
292
293 static int method_pd_destroyed(struct event_arg *arg, void *data)
294 {
295         int ret;
296
297         ret = lb_close_pd(arg->pkgname);
298         DbgPrint("%s Close PD: %d\n", arg->pkgname, ret);
299
300         return 0;
301 }
302
303 static int method_pd_move(struct event_arg *arg, void *data)
304 {
305         int ret;
306         struct event_info info;
307
308         memset(&info, 0, sizeof(info));
309         info.pointer.x = arg->info.pd_move.x;
310         info.pointer.y = arg->info.pd_move.y;
311         info.pointer.down = 0;
312
313         ret = lb_script_event(arg->pkgname, arg->id,
314                                 "pd,move", util_uri_to_path(arg->id), &info);
315         return ret;
316 }
317
318 static int method_lb_pause(struct event_arg *arg, void *data)
319 {
320         return lb_pause(arg->pkgname, arg->id);
321 }
322
323 static int method_lb_resume(struct event_arg *arg, void *data)
324 {
325         return lb_resume(arg->pkgname, arg->id);
326 }
327
328 HAPI int client_init(const char *name)
329 {
330         struct event_handler table = {
331                 .lb_create = method_new,
332                 .lb_recreate = method_renew,
333                 .lb_destroy = method_delete,
334                 .content_event = method_content_event,
335                 .clicked = method_clicked,
336                 .text_signal = method_text_signal,
337                 .resize = method_resize,
338                 .set_period = method_set_period,
339                 .change_group = method_change_group,
340                 .pinup = method_pinup,
341                 .update_content = method_update_content,
342                 .pause = method_pause,
343                 .resume = method_resume,
344                 .disconnected = method_disconnected,
345                 .connected = method_connected,
346                 .pd_create = method_pd_created,
347                 .pd_destroy = method_pd_destroyed,
348                 .pd_move = method_pd_move,
349                 .lb_pause = method_lb_pause,
350                 .lb_resume = method_lb_resume,
351                 .pd_access = NULL,
352                 .lb_access = NULL,
353         };
354
355         return provider_init(ecore_x_display_get(), name, &table, NULL);
356 }
357
358 HAPI int client_fini(void)
359 {
360         (void)provider_fini();
361         return 0;
362 }
363
364 /* End of a file */
365