N_SE-9808 : App terminate issue
[apps/core/preloaded/taskmanager.git] / src / _logic.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  * 
4  * Licensed under the Flora License, Version 1.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *     http://www.tizenopensource.org/license
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19
20 #include <appcore-common.h>
21 #include <aul.h>
22 #include <Ecore_X.h>
23 #include <vconf.h>
24
25 #include "taskmanager.h"
26 #include "_util_log.h"
27 #include "_util_efl.h"
28 #include "_genlist.h"
29 #include "_eina.h"
30 #include "_progressbar.h"
31 #include "_info.h"
32
33 int _dead_cb(int pid, void *data)
34 {
35 _D("func\n");
36         /* redraw list */
37         struct appdata *ad = (struct appdata *)data;
38         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
39
40         int ret = -1;
41
42         _fini_pthread();
43         if (ad->update_timer) {
44                 ecore_timer_del(ad->update_timer);
45                 ad->update_timer = NULL;
46         }
47
48         ret = _subt_einalist_item(ad, pid);
49         _D("mode(%d) count(%d) pid(%d) \n", ad->mode, ad->endcnt, pid);
50
51         if (ret != -1) {
52         switch (ad->mode) {
53         default:
54         case MODE_END_INUSE:
55         case MODE_DEL_HISTORY:
56         case MODE_DEL_ALL_HISTORY:
57         case MODE_KILL_INUSE:
58                 _D("aa\n");
59                 _del_popup_timer(ad);
60                 _del_progressbar(ad);
61                 refresh_app_info(ad);
62                 _restart_pthread(ad);
63                 break;
64
65         case MODE_END_ALL_INUSE:
66         case MODE_KILL_ALL_INUSE:
67                 _D("bb\n");
68                 if (ad->endcnt <= 1) {
69                         _D("count set 0\n");
70                         _del_popup_timer(ad);
71                         _del_progressbar(ad);
72                         refresh_app_info(ad);
73                         _restart_pthread(ad);
74
75                 } else {
76                         ad->endcnt--;
77                 }
78                 break;
79         }
80 }
81 }
82
83 static void _back_cb(void *data, Evas_Object *obj, void *event_info)
84 {
85         struct appdata *ad = (struct appdata *)data;
86         /*
87         _unset_notification_level(ad->win);
88         */
89         elm_exit();
90
91 }
92
93 static void _drag_start_cb(void *data, Evas_Object *obj, void *event_info)
94 {
95         _D("func\n");
96         struct appdata *ad = (struct appdata *)data;
97         if (ad->update_timer) {
98                 ecore_timer_del(ad->update_timer);
99                 ad->update_timer = NULL;
100         }
101 }
102
103 static void _drag_stop_cb(void *data, Evas_Object *obj, void *event_info)
104 {
105         _D("func\n");
106         struct appdata *ad = (struct appdata *)data;
107         if (ad->update_timer) {
108                 ecore_timer_del(ad->update_timer);
109                 ad->update_timer = NULL;
110         }
111         ad->update_timer = ecore_timer_add(2.0, _update_list, ad);
112 }
113
114
115 static void _anim_start_cb(void *data, Evas_Object *obj, void *event_info)
116 {
117         _D("func\n");
118         struct appdata *ad = (struct appdata *)data;
119         if (ad->update_timer) {
120                 ecore_timer_del(ad->update_timer);
121                 ad->update_timer = NULL;
122         }
123 }
124
125 static void _anim_stop_cb(void *data, Evas_Object *obj, void *event_info)
126 {
127         _D("func\n");
128         struct appdata *ad = (struct appdata *)data;
129         if (ad->update_timer) {
130                 ecore_timer_del(ad->update_timer);
131                 ad->update_timer = NULL;
132         }
133         ad->update_timer = ecore_timer_add(2.0, _update_list, ad);
134 }
135
136 static void _moved_cb(void *data, Evas_Object *obj, void *event_info)
137 {
138         _D("func\n");
139 }
140
141 int _app_create(struct appdata *ad)
142 {
143         Evas_Object *ly, *bg, *nv, *bt, *gl;
144
145         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
146         ad->ending = EINA_FALSE;
147
148         ly = _add_layout_main(ad->win, EINA_TRUE, EINA_FALSE);
149         retvm_if(ly == NULL, -1, "Failed to add layout main\n");
150
151         bg = _add_bg(ad->win, "group_list");
152         retvm_if(bg == NULL, -1, "Failed to add bg\n");
153         elm_object_part_content_set(ly, "elm.swallow.bg", bg);
154
155         nv = _add_naviframe(ly);
156         retvm_if(nv == NULL, -1, "Failed to add naviframe\n");
157         ad->nv = nv;
158
159         ly = _add_layout(ad->nv, EDJ_NAME, GRP_TM);
160         retvm_if(ly == NULL, -1, "Failed to add layout\n");
161         ad->ly = ly;
162
163         /* Load default content (running task) */
164         gl = _add_genlist(ly);
165         retvm_if(gl == NULL, -1, "Failed to add genlist\n");
166         elm_genlist_block_count_set(gl, 20);
167         evas_object_data_set(gl, "appdata", ad);
168         elm_object_part_content_set(ly, "list", gl);
169         evas_object_smart_callback_add(gl, "scroll,drag,start", _drag_start_cb, ad);
170         evas_object_smart_callback_add(gl, "scroll,drag,stop", _drag_stop_cb, ad);
171         evas_object_smart_callback_add(gl, "scroll,anim,start", _anim_start_cb, ad);
172         evas_object_smart_callback_add(gl, "scroll,anim,stop", _anim_stop_cb, ad);
173         ad->gl = gl;
174
175         bt = elm_button_add(nv);
176         retvm_if(bt == NULL, -1, "Failed to add button\n");
177         elm_object_style_set(bt, "naviframe/end_btn/default");
178         evas_object_smart_callback_add(bt, "clicked", _back_cb, ad);
179
180         elm_naviframe_item_push(nv,
181                         _("IDS_TASKMGR_HEADER_TASK_SWITCHER"),
182                         bt, NULL, ly, NULL);
183
184         return 0;
185 }
186
187 static void _get_win_geometry(struct appdata *ad)
188 {
189         Ecore_X_Window focus_win;
190         Ecore_X_Window root_win;
191
192         focus_win = ecore_x_window_focus_get();
193         root_win = ecore_x_window_root_get(focus_win);
194         ecore_x_window_size_get(root_win, &ad->root_w, &ad->root_h);
195 }
196
197 static void _vconf_noti_cb(keynode_t *node, void *data)
198 {
199         elm_exit();
200 }
201
202 static void _set_vconf_noti(void *data)
203 {
204         vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
205                         _vconf_noti_cb, NULL);
206 }
207
208 /* this func is to exit taskmanager after launching application */
209 static Eina_Bool __climsg_cb(void *data, int type, void *event)
210 {
211         static Atom a_deact;
212         pid_t pid_a, pid_d;
213
214         struct appdata *ad = (struct appdata *)data;
215         Ecore_X_Event_Client_Message *ev = event;
216
217         if(ev == NULL) {
218                 _E("Invalid argument: event is NULL\n");
219                 ecore_timer_add(0.3, _exit_cb, NULL);
220                 return ECORE_CALLBACK_CANCEL;
221         }
222
223         pid_a = ev->data.l[1];
224         pid_d = ev->data.l[3];
225         a_deact = ecore_x_atom_get("_X_ILLUME_DEACTIVATE_WINDOW");
226
227         /* when pid_a == pid_d, this is useless data */
228         if (pid_a == pid_d) {
229                 return ECORE_CALLBACK_RENEW;
230         }
231
232         if (ev->message_type == a_deact) {
233                 _D("exit after 1.0 sec\n");
234                 ad->exit_timer = ecore_timer_add(0.3, _exit_cb, ad);
235                 return ECORE_CALLBACK_CANCEL;
236         } else {
237                 _D("messagre is act\n");
238
239         }
240
241         return ECORE_CALLBACK_CANCEL;
242 }
243
244 static Eina_Bool _ask_kill_process(void *data)
245 {
246         struct appdata *ad = data;
247
248         _del_progressbar(ad);
249
250         ad->mode *= 2;
251         /* why? check enum in taskmgr.h */
252
253         if (ad->popup_ask) {
254                 evas_object_del(ad->popup_ask);
255                 ad->popup_ask = NULL;
256         }
257         ad->popup_ask = _add_popup_ask(ad->win,
258                         "It might be an invalid process. Do you want to kill this proceess anyway?",
259                         ad);
260
261         return ECORE_CALLBACK_CANCEL;
262 }
263
264 void _ok_response_cb(void *data, Evas_Object *obj, void *event_info)
265 {
266         struct appdata *ad = (struct appdata *)data;
267
268         retm_if(data == NULL, "Invalid argument: appdata is NULL\n");
269         if (ad->popup_ask) {
270                 evas_object_del(ad->popup_ask);
271                 ad->popup_ask = NULL;
272         }
273
274         switch (ad->mode) {
275                 case MODE_END_INUSE:
276                         _D("end inuse\n");
277                         _del_popup_timer(ad);
278                         ad->popup_timer = ecore_timer_add(7.0, _ask_kill_process, ad);
279                         _show_progressbar(ad);
280                         response_end_inuse(ad);
281                         _restart_pthread(ad);
282                         break;
283
284                 case MODE_END_ALL_INUSE:
285                         _D("end all inuse\n");
286                         _del_popup_timer(ad);
287                         ad->popup_timer = ecore_timer_add(7.0, _ask_kill_process, ad);
288                         _show_progressbar(ad);
289                         response_end_all_inuse(ad);
290                         break;
291
292                 case MODE_DEL_HISTORY:
293                         _D("del inuse\n");
294                         _del_popup_timer(ad);
295                         ad->popup_timer = ecore_timer_add(5.0, _ask_kill_process, ad);
296                         response_del_history(ad);
297                         break;
298
299                 case MODE_DEL_ALL_HISTORY:
300                         _D("del all inuse\n");
301                         _del_popup_timer(ad);
302                         ad->popup_timer = ecore_timer_add(5.0, _ask_kill_process, 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         Evas_Object *pu, *bt1, *bt2;
337         struct appdata *ad = (struct appdata *)data;
338         retvm_if(ad == NULL, ECORE_CALLBACK_CANCEL, "Invalid argument\n");
339
340         evas_object_show(ad->win);
341
342         _key_grab(ad);
343
344         _init_pthread();
345         _get_win_geometry(ad);
346         _set_vconf_noti(ad);
347         ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __climsg_cb, ad);
348
349         return ECORE_CALLBACK_CANCEL;
350 }
351
352
353