changing type of calling exit_cb function to timer.
[apps/core/preloaded/taskmanager.git] / src / _logic.c
1 /*
2  * org.tizen.taskmgr
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Flora License, Version 1.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://floralicense.org/license/
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an AS IS BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18
19
20
21 #include <appcore-common.h>
22 #include <ail.h>
23 #include <aul.h>
24 #include <Ecore_X.h>
25 #include <vconf.h>
26
27 #include "taskmanager.h"
28 #include "_util_log.h"
29 #include "_util_efl.h"
30 #include "_genlist.h"
31 #include "_eina.h"
32 #include "_progressbar.h"
33 #include "_info.h"
34
35 int _dead_cb(int pid, void *data)
36 {
37 _D("func\n");
38         /* redraw list */
39         struct appdata *ad = (struct appdata *)data;
40         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
41
42         int ret = -1;
43
44         if (ad->update_timer) {
45                 ecore_timer_del(ad->update_timer);
46                 ad->update_timer = NULL;
47         }
48
49         ret = _subt_einalist_item(ad, pid);
50         _D("mode(%d) count(%d) pid(%d) \n", ad->mode, ad->endcnt, pid);
51
52         if (ret != -1) {
53                 switch (ad->mode) {
54                 default:
55                 case MODE_END_INUSE:
56                 case MODE_DEL_HISTORY:
57                 case MODE_DEL_ALL_HISTORY:
58                 case MODE_KILL_INUSE:
59                         _D("aa\n");
60                         _del_popup_timer(ad);
61                         _del_progressbar(ad);
62                         refresh_app_info(ad);
63                         //_restart_pthread(ad);
64                         break;
65
66                 case MODE_END_ALL_INUSE:
67                 case MODE_KILL_ALL_INUSE:
68                         _D("bb\n");
69                         if (ad->endcnt <= 1) {
70                                 _D("count set 0\n");
71
72                                 if(ad->killall_timer)
73                                 {
74                                         ecore_timer_del(ad->killall_timer);
75                                         ad->killall_timer = NULL;
76                                 }
77
78                                 _del_popup_timer(ad);
79                                 _del_progressbar(ad);
80                                 refresh_app_info(ad);
81
82                         } else {
83                                 ad->endcnt--;
84                         }
85                         break;
86                 }
87         }
88
89         return ret;
90 }
91
92 static void _back_cb(void *data, Evas_Object *obj, void *event_info)
93 {
94         elm_exit();
95 }
96
97 int _app_create(struct appdata *ad)
98 {
99         Evas_Object *ly, *bg, *nv, *bt, *gl;
100
101         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
102         ad->ending = EINA_FALSE;
103
104         ly = _add_layout_main(ad->win, EINA_TRUE, EINA_FALSE);
105         retvm_if(ly == NULL, -1, "Failed to add layout main\n");
106
107         bg = _add_bg(ad->win, "group_list");
108         retvm_if(bg == NULL, -1, "Failed to add bg\n");
109         elm_object_part_content_set(ly, "elm.swallow.bg", bg);
110
111         nv = _add_naviframe(ly);
112         retvm_if(nv == NULL, -1, "Failed to add naviframe\n");
113         ad->nv = nv;
114
115         ly = _add_layout(ad->nv, EDJ_NAME, GRP_TM);
116         retvm_if(ly == NULL, -1, "Failed to add layout\n");
117         ad->ly = ly;
118
119         /* Load default content (running task) */
120         gl = _add_genlist(ly);
121         retvm_if(gl == NULL, -1, "Failed to add genlist\n");
122         elm_genlist_block_count_set(gl, 20);
123         evas_object_data_set(gl, "appdata", ad);
124         elm_object_part_content_set(ly, "list", gl);
125         ad->gl = gl;
126
127         bt = elm_button_add(nv);
128         retvm_if(bt == NULL, -1, "Failed to add button\n");
129         elm_object_style_set(bt, "naviframe/end_btn/default");
130         evas_object_smart_callback_add(bt, "clicked", _back_cb, ad);
131
132         elm_naviframe_item_push(nv,
133                         T_("IDS_TASKMGR_HEADER_TASK_SWITCHER"),
134                         bt, NULL, ly, NULL);
135
136         return 0;
137 }
138
139 static void _get_win_geometry(struct appdata *ad)
140 {
141         Ecore_X_Window focus_win;
142         Ecore_X_Window root_win;
143
144         focus_win = ecore_x_window_focus_get();
145         root_win = ecore_x_window_root_get(focus_win);
146         ecore_x_window_size_get(root_win, &ad->root_w, &ad->root_h);
147 }
148
149 /* this func is to exit taskmanager after launching application */
150 static Eina_Bool __climsg_cb(void *data, int type, void *event)
151 {
152 _D("%s\n", __func__);
153         static Atom a_deact;
154         pid_t pid_a, pid_d;
155
156         struct appdata *ad = (struct appdata *)data;
157         Ecore_X_Event_Client_Message *ev = event;
158
159         if(ev == NULL) {
160                 _E("Invalid argument: event is NULL\n");
161                 ad->exit_timer = ecore_timer_add(0.3, _exit_cb, ad);
162                 return ECORE_CALLBACK_CANCEL;
163         }
164
165         pid_a = ev->data.l[1];
166         pid_d = ev->data.l[3];
167         a_deact = ecore_x_atom_get("_X_ILLUME_DEACTIVATE_WINDOW");
168
169         /* when pid_a == pid_d, this is useless data */
170         if (pid_a == pid_d) {
171                 return ECORE_CALLBACK_RENEW;
172         }
173
174         if (ev->message_type == a_deact) {
175                 ad->exit_timer = ecore_timer_add(0.3, _exit_cb, ad);
176                 return ECORE_CALLBACK_CANCEL;
177         } else {
178                 _D("messagre is act\n");
179
180         }
181
182         return ECORE_CALLBACK_CANCEL;
183 }
184
185 static int runapp_count = 0;
186
187 int _runapp_info_get_count(const aul_app_info *ainfo, void *data)
188 {
189         ail_appinfo_h handle;
190         ail_error_e ret;
191         bool is_taskmanage;
192
193         retvm_if(ainfo == NULL, -1, "Invalid argument: ainfo is NULL\n");
194
195         retvm_if(ainfo->pid <= 0, -1, "Invalid pid(%u)\n", ainfo->pid);
196
197         /* filtering */
198         if (ainfo->pid == getpid())
199         {
200                 return 0;
201         }
202
203         retvm_if(ainfo->pkg_name == NULL, 0, "Invalid pkg_name(%s)\n", ainfo->pkg_name);
204
205         ret = ail_package_get_appinfo(ainfo->pkg_name, &handle);
206         retvm_if(ret != AIL_ERROR_OK, -1,
207                         "Failed to get appinfo, pkg_name:%s\n", ainfo->pkg_name);
208
209         ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_TASKMANAGE_BOOL, &is_taskmanage);
210         if (is_taskmanage == 0) {
211                 ret = ail_package_destroy_appinfo(handle);
212                 retvm_if(ret != AIL_ERROR_OK, -1, "Failed to destroy appinfo\n");
213                 return 0;
214         }
215
216         ++runapp_count;
217         _D("running(%s)\n", ainfo->pkg_name);
218         _D("runapp count : %d\n", runapp_count);
219
220         ret = ail_package_destroy_appinfo(handle);
221         retvm_if(ret != AIL_ERROR_OK, -1, "Failed to destroy appinfo\n");
222         return 0;
223 }
224
225 Eina_Bool _kill_all_timer_cb(void *data)
226 {
227         _D("func\n");
228
229         struct appdata *ad = data;
230
231         int ret = AUL_R_ERROR;
232         int retry_cnt = 0;
233         int sleep_value = 500;
234
235         runapp_count = 0;
236
237         while(ret != AUL_R_OK && retry_cnt < 5)
238         {
239                 usleep(sleep_value);
240                 ret = aul_app_get_running_app_info(_runapp_info_get_count, ad);
241
242                 if(ret != AUL_R_OK)
243                 {
244                         _D("Fail to get running app information\n");
245                 }
246
247                 retry_cnt++;
248                 sleep_value *= 2;
249         }
250         _D("runapp count : %d\n", runapp_count);
251
252         /* count inuse app number */
253         /** if(count == 0) dead_cb */
254         if(runapp_count == 0)
255         {
256                 _D("runapp_count == 0\n");
257                 _del_popup_timer(ad);
258                 _del_progressbar(ad);
259                 refresh_app_info(ad);
260                 //_restart_pthread(ad);
261                 return ECORE_CALLBACK_CANCEL;
262         }
263
264         return ECORE_CALLBACK_RENEW;
265 }
266
267 void _ok_response_cb(void *data, Evas_Object *obj, void *event_info)
268 {
269         struct appdata *ad = (struct appdata *)data;
270
271         retm_if(data == NULL, "Invalid argument: appdata is NULL\n");
272         if (ad->popup_ask) {
273                 evas_object_del(ad->popup_ask);
274                 ad->popup_ask = NULL;
275         }
276
277         switch (ad->mode) {
278                 case MODE_END_INUSE:
279                         _D("end inuse\n");
280                         _del_popup_timer(ad);
281                         _show_progressbar(ad);
282                         response_end_inuse(ad);
283                         //_restart_pthread(ad);
284                         break;
285
286                 case MODE_END_ALL_INUSE:
287                         _D("end all inuse\n");
288                         _del_popup_timer(ad);
289                         _show_progressbar(ad);
290                         response_end_all_inuse(ad);
291                         ad->killall_timer = ecore_timer_add(2.0, _kill_all_timer_cb, ad);
292                         break;
293
294                 case MODE_DEL_HISTORY:
295                         _D("del inuse\n");
296                         _del_popup_timer(ad);
297                         response_del_history(ad);
298                         break;
299
300                 case MODE_DEL_ALL_HISTORY:
301                         _D("del all inuse\n");
302                         _del_popup_timer(ad);
303                         response_del_all_history(ad);
304                         break;
305
306                 case MODE_KILL_INUSE:
307                         _D("kill all inuse\n");
308                         response_kill_inuse(ad);
309                         break;
310
311                 case MODE_KILL_ALL_INUSE:
312                         _D("kill all inuse\n");
313                         response_kill_all_inuse(ad);
314                         break;
315
316                 default:
317                         printf("[Wanning] taskmanager: check mode [%d]\n",
318                                         ad->mode);
319                         break;
320         }
321 }
322
323 void _cancel_response_cb(void *data, Evas_Object *obj, void *event_info)
324 {
325         struct appdata *ad = (struct appdata *)data;
326
327         retm_if(data == NULL, "Invalid argument: appdata is NULL\n");
328         if (ad->popup_ask) {
329                 evas_object_del(ad->popup_ask);
330                 ad->popup_ask = NULL;
331         }
332 }
333
334 Eina_Bool _create_idler_cb(void *data)
335 {
336         struct appdata *ad = (struct appdata *)data;
337         retvm_if(ad == NULL, ECORE_CALLBACK_CANCEL, "Invalid argument\n");
338
339         evas_object_show(ad->win);
340
341         _key_grab(ad);
342
343         _get_win_geometry(ad);
344         ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __climsg_cb, ad);
345
346         return ECORE_CALLBACK_CANCEL;
347 }
348
349
350