Upload Tizen2.0 source
[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
31 static GSource *src;
32
33 static gboolean __aul_glib_check(GSource *src);
34 static gboolean __aul_glib_dispatch(GSource *src, GSourceFunc callback,
35                                   gpointer data);
36 static gboolean __aul_glib_prepare(GSource *src, gint *timeout);
37 static gboolean __app_start_internal(gpointer data);
38
39 static gboolean __aul_glib_check(GSource *src)
40 {
41         GSList *fd_list;
42         GPollFD *tmp;
43
44         fd_list = src->poll_fds;
45         do {
46                 tmp = (GPollFD *) fd_list->data;
47                 if ((tmp->revents & (POLLIN | POLLPRI)))
48                         return TRUE;
49                 fd_list = fd_list->next;
50         } while (fd_list);
51
52         return FALSE;
53 }
54
55 static gboolean __aul_glib_dispatch(GSource *src, GSourceFunc callback,
56                                   gpointer data)
57 {
58         callback(data);
59         return TRUE;
60 }
61
62 static gboolean __aul_glib_prepare(GSource *src, gint *timeout)
63 {
64         return FALSE;
65 }
66
67 GSourceFuncs funcs = {
68         .prepare = __aul_glib_prepare,
69         .check = __aul_glib_check,
70         .dispatch = __aul_glib_dispatch,
71         .finalize = NULL
72 };
73
74 gboolean __aul_glib_handler(gpointer data)
75 {
76         GPollFD *gpollfd = (GPollFD *) data;
77         aul_sock_handler(gpollfd->fd);
78         return TRUE;
79 }
80
81 static gboolean __app_start_internal(gpointer data)
82 {
83         bundle *kb;
84
85         kb = (bundle *) data;
86         app_start(kb);
87         bundle_free(kb);
88
89         return 0;
90 }
91
92 SLPAPI int aul_launch_init(
93         int (*aul_handler) (aul_type type, bundle *, void *), void *data)
94 {
95         int fd;
96         GPollFD *gpollfd;
97         int ret;
98
99         if (aul_handler != NULL)
100                 aul_register_init_callback(aul_handler, data);
101
102         fd = aul_initialize();
103         if (fd < 0)
104                 return fd;
105
106         src = g_source_new(&funcs, sizeof(GSource));
107
108         gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
109         gpollfd->events = POLLIN;
110         gpollfd->fd = fd;
111
112         g_source_add_poll(src, gpollfd);
113         g_source_set_callback(src, (GSourceFunc) __aul_glib_handler,
114                               (gpointer) gpollfd, NULL);
115         g_source_set_priority(src, G_PRIORITY_DEFAULT);
116
117         ret = g_source_attach(src, NULL);
118         if (ret == 0)
119                 return AUL_R_ERROR;
120
121         g_source_unref(src);
122
123         return AUL_R_OK;
124 }
125
126 SLPAPI int aul_launch_fini()
127 {
128         g_source_destroy(src);
129 }
130
131 SLPAPI int aul_launch_argv_handler(int argc, char **argv)
132 {
133         bundle *b;
134
135         if (!aul_is_initialized())
136                 return AUL_R_ENOINIT;
137
138         b = bundle_import_from_argv(argc, argv);
139         if (b == NULL) {
140                 _E("bundle for APP_START is NULL");
141         }
142         if (g_idle_add(__app_start_internal, b) > 0)
143                 return AUL_R_OK;
144         else
145                 return AUL_R_ERROR;
146 }
147
148 SLPAPI int aul_launch_local(bundle *b)
149 {
150         if (!aul_is_initialized())
151                 return AUL_R_ENOINIT;
152
153         if (b == NULL) {
154                 _E("bundle for APP_START is NULL");
155         }
156         if (g_idle_add(__app_start_internal, b) > 0)
157                 return AUL_R_OK;
158         else
159                 return AUL_R_ERROR;
160 }
161