tizen 2.3.1 release
[framework/appfw/aul-1.git] / src / launch_glib.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@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
23 #include <glib.h>
24 #include <poll.h>
25 #include <bundle.h>
26 #include "aul.h"
27 #include "aul_api.h"
28 #include "launch.h"
29 #include "simple_util.h"
30 #include <Ecore.h>
31
32 static GSource *src;
33
34 static gboolean __aul_glib_check(GSource *src);
35 static gboolean __aul_glib_dispatch(GSource *src, GSourceFunc callback,
36                                   gpointer data);
37 static gboolean __aul_glib_prepare(GSource *src, gint *timeout);
38 static gboolean __app_start_internal(gpointer data);
39
40 static void __aul_glib_finalize(GSource *src)
41 {
42         GSList *fd_list;
43         GPollFD *tmp;
44
45         fd_list = src->poll_fds;
46         do {
47                 tmp = (GPollFD *) fd_list->data;
48                 g_free(tmp);
49
50                 fd_list = fd_list->next;
51         } while (fd_list);
52
53         return;
54 }
55
56 static gboolean __aul_glib_check(GSource *src)
57 {
58         GSList *fd_list;
59         GPollFD *tmp;
60
61         fd_list = src->poll_fds;
62         do {
63                 tmp = (GPollFD *) fd_list->data;
64                 if ((tmp->revents & (POLLIN | POLLPRI)))
65                         return TRUE;
66                 fd_list = fd_list->next;
67         } while (fd_list);
68
69         return FALSE;
70 }
71
72 static gboolean __aul_glib_dispatch(GSource *src, GSourceFunc callback,
73                                   gpointer data)
74 {
75         callback(data);
76         return TRUE;
77 }
78
79 static gboolean __aul_glib_prepare(GSource *src, gint *timeout)
80 {
81         return FALSE;
82 }
83
84 GSourceFuncs funcs = {
85         .prepare = __aul_glib_prepare,
86         .check = __aul_glib_check,
87         .dispatch = __aul_glib_dispatch,
88         .finalize = __aul_glib_finalize
89 };
90
91 gboolean __aul_glib_handler(gpointer data)
92 {
93         GPollFD *gpollfd = (GPollFD *) data;
94         aul_sock_handler(gpollfd->fd);
95         return TRUE;
96 }
97
98 static gboolean __app_start_internal(gpointer data)
99 {
100         bundle *kb;
101
102         kb = (bundle *) data;
103         app_start(kb);
104         bundle_free(kb);
105
106         return 0;
107 }
108
109 SLPAPI int aul_launch_init(
110         int (*aul_handler) (aul_type type, bundle *, void *), void *data)
111 {
112         int fd;
113         GPollFD *gpollfd;
114         int ret;
115
116         if (aul_handler != NULL)
117                 aul_register_init_callback(aul_handler, data);
118
119         fd = aul_initialize();
120         if (fd < 0)
121                 return fd;
122
123         src = g_source_new(&funcs, sizeof(GSource));
124
125         gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
126         if (gpollfd == NULL) {
127                 _E("out of memory");
128                 g_source_unref(src);
129                 close(fd);
130                 return AUL_R_ERROR;
131         }
132
133         gpollfd->events = POLLIN;
134         gpollfd->fd = fd;
135
136         g_source_add_poll(src, gpollfd);
137         g_source_set_callback(src, (GSourceFunc) __aul_glib_handler,
138                               (gpointer) gpollfd, NULL);
139         g_source_set_priority(src, G_PRIORITY_DEFAULT);
140
141         ret = g_source_attach(src, NULL);
142         if (ret == 0)
143                 return AUL_R_ERROR;
144
145         g_source_unref(src);
146
147         return AUL_R_OK;
148 }
149
150 SLPAPI int aul_launch_fini()
151 {
152         g_source_destroy(src);
153         return AUL_R_OK;
154 }
155
156 SLPAPI int aul_launch_argv_handler(int argc, char **argv)
157 {
158         bundle *b;
159
160         if (!aul_is_initialized())
161                 return AUL_R_ENOINIT;
162
163         b = bundle_import_from_argv(argc, argv);
164         if (b == NULL) {
165                 _E("bundle for APP_START is NULL");
166         }
167         if (g_idle_add(__app_start_internal, b) > 0)
168                 return AUL_R_OK;
169         else
170                 return AUL_R_ERROR;
171 }
172
173 SLPAPI int aul_launch_argv_handler_for_efl(int argc, char **argv)
174 {
175         bundle *b = NULL;
176
177         Ecore_Idler *idler = NULL;
178
179         if (!aul_is_initialized())
180                 return AUL_R_ENOINIT;
181
182         b = bundle_import_from_argv(argc, argv);
183         if (b == NULL) {
184                 _E("bundle for APP_START is NULL");
185         }
186
187         idler = ecore_idler_add(__app_start_internal, b);
188         if (idler)
189                 return AUL_R_OK;
190         else
191                 return AUL_R_ERROR;
192 }
193
194 SLPAPI int aul_launch_local(bundle *b)
195 {
196         if (!aul_is_initialized())
197                 return AUL_R_ENOINIT;
198
199         if (b == NULL) {
200                 _E("bundle for APP_START is NULL");
201         }
202         if (g_idle_add(__app_start_internal, b) > 0)
203                 return AUL_R_OK;
204         else
205                 return AUL_R_ERROR;
206 }
207