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