apply FSL(Flora Software License)
[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 #include <appcore-common.h>
19 #include <aul.h>
20 #include <Ecore_X.h>
21 #include <vconf.h>
22
23 #include "taskmanager.h"
24 #include "_util_log.h"
25 #include "_util_efl.h"
26 #include "_genlist.h"
27 #include "_eina.h"
28 #include "_progressbar.h"
29 #include "_info.h"
30
31
32 int _dead_cb(int pid, void *data)
33 {
34 _D("func\n");
35         /* redraw list */
36         struct appdata *ad = (struct appdata *)data;
37         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
38
39         _fini_pthread();
40         if (ad->update_timer) {
41                 ecore_timer_del(ad->update_timer);
42                 ad->update_timer = NULL;
43         }
44         _subt_einalist_item(ad, pid);
45
46         _D("mode(%d) count(%d)\n", ad->mode, ad->endcnt);
47         switch (ad->mode) {
48         default:
49         case MODE_END_INUSE:
50         case MODE_DEL_HISTORY:
51         case MODE_DEL_ALL_HISTORY:
52         case MODE_KILL_INUSE:
53                 _D("aa\n");
54                 _del_popup_timer(ad);
55                 _del_progressbar(ad);
56                 refresh_app_info(ad);
57                 _restart_pthread(ad);
58                 break;
59
60         case MODE_END_ALL_INUSE:
61         case MODE_KILL_ALL_INUSE:
62                 _D("bb\n");
63                 if (ad->endcnt <= 1) {
64                         _D("count set 0\n");
65                         _del_popup_timer(ad);
66                         _del_progressbar(ad);
67                         refresh_app_info(ad);
68                         _restart_pthread(ad);
69
70                 } else {
71                         ad->endcnt--;
72                 }
73                 break;
74         }
75 }
76
77 static void _back_cb(void *data, Evas_Object *obj, void *event_info)
78 {
79         struct appdata *ad = (struct appdata *)data;
80         /*
81         _unset_notification_level(ad->win);
82         */
83         elm_exit();
84
85 }
86
87 static void _drag_start_cb(void *data, Evas_Object *obj, void *event_info)
88 {
89         _D("func\n");
90         struct appdata *ad = (struct appdata *)data;
91         if (ad->update_timer) {
92                 ecore_timer_del(ad->update_timer);
93                 ad->update_timer = NULL;
94         }
95 }
96
97 static void _drag_stop_cb(void *data, Evas_Object *obj, void *event_info)
98 {
99         _D("func\n");
100         struct appdata *ad = (struct appdata *)data;
101         if (ad->update_timer) {
102                 ecore_timer_del(ad->update_timer);
103                 ad->update_timer = NULL;
104         }
105         ad->update_timer = ecore_timer_add(2.0, _update_list, ad);
106 }
107
108
109 static void _anim_start_cb(void *data, Evas_Object *obj, void *event_info)
110 {
111         _D("func\n");
112         struct appdata *ad = (struct appdata *)data;
113         if (ad->update_timer) {
114                 ecore_timer_del(ad->update_timer);
115                 ad->update_timer = NULL;
116         }
117 }
118
119 static void _anim_stop_cb(void *data, Evas_Object *obj, void *event_info)
120 {
121         _D("func\n");
122         struct appdata *ad = (struct appdata *)data;
123         if (ad->update_timer) {
124                 ecore_timer_del(ad->update_timer);
125                 ad->update_timer = NULL;
126         }
127         ad->update_timer = ecore_timer_add(2.0, _update_list, ad);
128 }
129
130 static void _moved_cb(void *data, Evas_Object *obj, void *event_info)
131 {
132         _D("func\n");
133 }
134
135 int _app_create(struct appdata *ad)
136 {
137         Evas_Object *ly, *bg, *nv, *bt, *gl;
138
139         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
140         ad->ending = EINA_FALSE;
141
142         ly = _add_layout_main(ad->win, EINA_TRUE, EINA_FALSE);
143         retvm_if(ly == NULL, -1, "Failed to add layout main\n");
144
145         bg = _add_bg(ad->win, "group_list");
146         retvm_if(bg == NULL, -1, "Failed to add bg\n");
147         elm_object_part_content_set(ly, "elm.swallow.bg", bg);
148
149         nv = _add_naviframe(ly);
150         retvm_if(nv == NULL, -1, "Failed to add naviframe\n");
151         evas_object_show(nv);
152         ad->nv = nv;
153
154         ly = _add_layout(ad->nv, EDJ_NAME, GRP_TM);
155         retvm_if(ly == NULL, -1, "Failed to add layout\n");
156         ad->ly = ly;
157
158         /* Load default content (running task) */
159         gl = _add_genlist(ly);
160         retvm_if(gl == NULL, -1, "Failed to add genlist\n");
161         elm_genlist_block_count_set(gl, 20);
162         evas_object_data_set(gl, "appdata", ad);
163         elm_object_part_content_set(ly, "list", gl);
164         evas_object_smart_callback_add(gl, "scroll,drag,start", _drag_start_cb, ad);
165         evas_object_smart_callback_add(gl, "scroll,drag,stop", _drag_stop_cb, ad);
166         evas_object_smart_callback_add(gl, "scroll,anim,start", _anim_start_cb, ad);
167         evas_object_smart_callback_add(gl, "scroll,anim,stop", _anim_stop_cb, ad);
168 //      evas_object_smart_callback_add(gl, "edge,bottom", _moved_cb, ad);
169         evas_object_show(gl);
170         ad->gl = gl;
171
172         bt = elm_button_add(nv);
173         retvm_if(bt == NULL, -1, "Failed to add button\n");
174         elm_object_style_set(bt, "naviframe/end_btn/default");
175         evas_object_smart_callback_add(bt, "clicked", _back_cb, ad);
176
177         elm_naviframe_item_push(nv,
178                         _("IDS_TASKMGR_HEADER_TASK_SWITCHER"),
179                         bt, NULL, ly, NULL);
180
181         return 0;
182 }
183
184 static void _get_win_geometry(struct appdata *ad)
185 {
186         Ecore_X_Window focus_win;
187         Ecore_X_Window root_win;
188
189         focus_win = ecore_x_window_focus_get();
190         root_win = ecore_x_window_root_get(focus_win);
191         ecore_x_window_size_get(root_win, &ad->root_w, &ad->root_h);
192 }
193
194 static void _vconf_noti_cb(keynode_t *node, void *data)
195 {
196         elm_exit();
197 }
198
199 static void _set_vconf_noti(void *data)
200 {
201         vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
202                         _vconf_noti_cb, NULL);
203 }
204
205 /* this func is to exit taskmanager after launching application */
206 static Eina_Bool __climsg_cb(void *data, int type, void *event)
207 {
208         static Atom a_deact;
209         pid_t pid_a, pid_d;
210
211         Ecore_X_Event_Client_Message *ev = event;
212
213         if(ev == NULL) {
214                 _E("Invalid argument: event is NULL\n");
215                 ecore_timer_add(1.5, _exit_cb, NULL);
216                 return ECORE_CALLBACK_CANCEL;
217         }
218
219         pid_a = ev->data.l[1];
220         pid_d = ev->data.l[3];
221         a_deact = ecore_x_atom_get("_X_ILLUME_DEACTIVATE_WINDOW");
222
223         /* when pid_a == pid_d, this is useless data */
224         if (pid_a == pid_d) {
225                 return ECORE_CALLBACK_RENEW;
226         }
227
228         if (ev->message_type == a_deact) {
229                 _D("exit after 1.0 sec\n");
230                 ecore_timer_add(1.5, _exit_cb, NULL);
231                 return ECORE_CALLBACK_CANCEL;
232         } else {
233                 _D("messagre is act\n");
234
235         }
236
237         return ECORE_CALLBACK_CANCEL;
238 }
239
240 static Eina_Bool _ask_kill_process(void *data)
241 {
242         struct appdata *ad = data;
243
244         _del_progressbar(ad);
245
246         ad->mode *= 2;
247         /* why? check enum in taskmgr.h */
248
249         switch (ad->mode) {
250                 case MODE_KILL_INUSE:
251                         _D("kill all inuse\n");
252                         response_kill_inuse(ad);
253                         break;
254
255                 case MODE_KILL_ALL_INUSE:
256                         _D("kill all inuse\n");
257                         response_kill_all_inuse(ad);
258                         break;
259         }
260 /*
261         if (ad->popup_ask) {
262                 evas_object_del(ad->popup_ask);
263                 ad->popup_ask = NULL;
264         }
265         ad->popup_ask = _add_popup_ask(ad->win,
266                         "It might be an invalid process. Do you want to kill this proceess anyway?",
267                         ad);
268 */
269         return ECORE_CALLBACK_CANCEL;
270 }
271
272 void _ok_response_cb(void *data, Evas_Object *obj, void *event_info)
273 {
274         struct appdata *ad = (struct appdata *)data;
275
276         retm_if(data == NULL, "Invalid argument: appdata is NULL\n");
277         if (ad->popup_ask) {
278                 evas_object_del(ad->popup_ask);
279                 ad->popup_ask = NULL;
280         }
281
282         switch (ad->mode) {
283                 case MODE_END_INUSE:
284                         _D("end inuse\n");
285                         _del_popup_timer(ad);
286                         ad->popup_timer = ecore_timer_add(7.0, _ask_kill_process, ad);
287                         _show_progressbar(ad);
288                         response_end_inuse(ad);
289                         _restart_pthread(ad);
290                         break;
291
292                 case MODE_END_ALL_INUSE:
293                         _D("end all inuse\n");
294                         _del_popup_timer(ad);
295                         ad->popup_timer = ecore_timer_add(7.0, _ask_kill_process, ad);
296                         _show_progressbar(ad);
297                         response_end_all_inuse(ad);
298                         break;
299
300                 case MODE_DEL_HISTORY:
301                         _D("del inuse\n");
302                         _del_popup_timer(ad);
303                         ad->popup_timer = ecore_timer_add(5.0, _ask_kill_process, ad);
304                         response_del_history(ad);
305                         break;
306
307                 case MODE_DEL_ALL_HISTORY:
308                         _D("del all inuse\n");
309                         _del_popup_timer(ad);
310                         ad->popup_timer = ecore_timer_add(5.0, _ask_kill_process, ad);
311                         response_del_all_history(ad);
312                         break;
313
314                 case MODE_KILL_INUSE:
315                         _D("kill all inuse\n");
316                         response_kill_inuse(ad);
317                         break;
318
319                 case MODE_KILL_ALL_INUSE:
320                         _D("kill all inuse\n");
321                         response_kill_all_inuse(ad);
322                         break;
323
324                 default:
325                         printf("[Wanning] taskmanager: check mode [%d]\n",
326                                         ad->mode);
327                         break;
328         }
329 }
330
331 void _cancel_response_cb(void *data, Evas_Object *obj, void *event_info)
332 {
333         struct appdata *ad = (struct appdata *)data;
334
335         retm_if(data == NULL, "Invalid argument: appdata is NULL\n");
336         if (ad->popup_ask) {
337                 evas_object_del(ad->popup_ask);
338                 ad->popup_ask = NULL;
339         }
340 }
341
342 Eina_Bool _create_idler_cb(void *data)
343 {
344         Evas_Object *pu, *bt1, *bt2;
345         struct appdata *ad = (struct appdata *)data;
346         retvm_if(ad == NULL, ECORE_CALLBACK_CANCEL, "Invalid argument\n");
347
348         evas_object_show(ad->win);
349
350         _key_grab(ad);
351
352         _init_pthread();
353         _get_win_geometry(ad);
354         _set_vconf_noti(ad);
355         ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __climsg_cb, NULL);
356
357         return ECORE_CALLBACK_CANCEL;
358 }
359
360
361