tizen 2.4 release
[framework/appfw/aul-1.git] / src / status.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 <stdio.h>
24 #include <stdlib.h>
25 #include <aul.h>
26 #include <bundle.h>
27 #include <bundle_internal.h>
28
29 #include "aul_util.h"
30 #include "app_sock.h"
31 #include "aul_api.h"
32 #include "launch.h"
33 #include "simple_util.h"
34
35 typedef struct _app_status_cb_info_t {
36         int (*handler) (int status, void *data);
37         void *data;
38         struct _app_status_cb_info_t *next;
39 } app_status_cb_info_t;
40
41 app_status_cb_info_t *app_status_cb = NULL;
42
43 SLPAPI int aul_status_update(int status)
44 {
45         int ret;
46
47         ret = __app_send_raw_with_noreply(AUL_UTIL_PID, APP_STATUS_UPDATE, (unsigned char *)&status, sizeof(status));
48
49         if (!ret)
50                 aul_invoke_status_local_cb(status);
51
52         return ret;
53 }
54
55 SLPAPI  int aul_app_get_status_bypid(int pid)
56 {
57         int ret;
58
59         ret = __app_send_raw(AUL_UTIL_PID, APP_GET_STATUS, (unsigned char *)&pid, sizeof(pid));
60
61         return ret;
62 }
63
64 SLPAPI int aul_add_status_local_cb(int (*func)(int status, void *data), void *data)
65 {
66         app_status_cb_info_t *cb = app_status_cb;
67
68         if (func == NULL)
69                 return -1;
70
71         // check known callback
72         while (cb) {
73                 if (cb && cb->handler == func && cb->data == data) {
74                         // already in list
75                         return 0;
76                 }
77                 cb = cb->next;
78         }
79
80         cb = (app_status_cb_info_t *)malloc(sizeof(app_status_cb_info_t));
81         if (cb == NULL)
82                 return -1;
83
84         cb->handler = func;
85         cb->data = data;
86
87         cb->next = app_status_cb;
88         app_status_cb = cb;
89
90         return 0;
91 }
92
93 SLPAPI int aul_remove_status_local_cb(int (*func)(int status, void *data), void *data)
94 {
95         app_status_cb_info_t *cb = app_status_cb;
96         app_status_cb_info_t *tmp = NULL;
97
98         if (app_status_cb
99                  && app_status_cb->handler == func
100                  && app_status_cb->data == data) {
101                 cb = app_status_cb->next;
102                 free(app_status_cb);
103                 app_status_cb = cb;
104                 return 0;
105         }
106
107         while (cb) {
108                 if (cb->next
109                          && cb->next->handler == func
110                          && cb->next->data == data) {
111                         tmp = cb->next->next;
112                         free(cb->next);
113                         cb->next = tmp;
114                         return 0;
115                 }
116
117                 cb = cb->next;
118         }
119
120         return -1;
121 }
122
123 SLPAPI int aul_invoke_status_local_cb(int status)
124 {
125         app_status_cb_info_t *cb = app_status_cb;
126
127         while (cb) {
128                 if (cb->handler) {
129                         if (cb->handler(status, cb->data) < 0)
130                                 aul_remove_status_local_cb(cb->handler, cb->data);
131                 }
132
133                 cb = cb->next;
134         }
135
136         return 0;
137 }
138
139 SLPAPI int aul_running_list_update(char *appid, char *app_path, char *pid)
140 {
141         int ret;
142         bundle *kb;
143
144         kb = bundle_create();
145
146         bundle_add(kb, AUL_K_APPID, appid);
147         bundle_add(kb, AUL_K_EXEC, app_path);
148         bundle_add(kb, AUL_K_PID, pid);
149
150         ret = app_send_cmd(AUL_UTIL_PID, APP_RUNNING_LIST_UPDATE, kb);
151
152         if (kb != NULL)
153                         bundle_free(kb);
154
155         return ret;
156 }
157
158 SLPAPI int aul_set_process_group(int owner_pid, int child_pid)
159 {
160         int ret = -1;
161         bundle *kb = NULL;
162         char pid_buf[MAX_PID_STR_BUFSZ] = {0,};
163
164         kb = bundle_create();
165
166         snprintf(pid_buf, MAX_PID_STR_BUFSZ, "%d", owner_pid);
167         bundle_add(kb, AUL_K_OWNER_PID, pid_buf);
168         snprintf(pid_buf, MAX_PID_STR_BUFSZ, "%d", child_pid);
169         bundle_add(kb, AUL_K_CHILD_PID, pid_buf);
170
171         ret = app_send_cmd(AUL_UTIL_PID, APP_SET_PROCESS_GROUP, kb);
172
173         if (kb != NULL)
174                         bundle_free(kb);
175
176         return ret;
177 }