Use ecore_evas_screen_geometry_get() to retrieve desktop size
[profile/ivi/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         Evas_Object *eo;
190         Ecore_Evas *ee;
191  
192         ee = ecore_evas_ecore_evas_get(evas_object_evas_get(ad->win));
193         ecore_evas_screen_geometry_get(ee, 0, 0, &ad->root_w, &ad->root_h);
194 }
195
196 static void _vconf_noti_cb(keynode_t *node, void *data)
197 {
198         elm_exit();
199 }
200
201 static void _set_vconf_noti(void *data)
202 {
203         vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
204                         _vconf_noti_cb, NULL);
205 }
206
207 /* X specific */
208 #if HAVE_X
209 /* this func is to exit taskmanager after launching application */
210 static Eina_Bool __climsg_cb(void *data, int type, void *event)
211 {
212         static Atom a_deact;
213         pid_t pid_a, pid_d;
214
215         struct appdata *ad = (struct appdata *)data;
216         Ecore_X_Event_Client_Message *ev = event;
217
218         if(ev == NULL) {
219                 _E("Invalid argument: event is NULL\n");
220                 ecore_timer_add(0.3, _exit_cb, NULL);
221                 return ECORE_CALLBACK_CANCEL;
222         }
223
224         pid_a = ev->data.l[1];
225         pid_d = ev->data.l[3];
226         a_deact = ecore_x_atom_get("_X_ILLUME_DEACTIVATE_WINDOW");
227
228         /* when pid_a == pid_d, this is useless data */
229         if (pid_a == pid_d) {
230                 return ECORE_CALLBACK_RENEW;
231         }
232
233         if (ev->message_type == a_deact) {
234                 _D("exit after 1.0 sec\n");
235                 ad->exit_timer = ecore_timer_add(0.3, _exit_cb, ad);
236                 return ECORE_CALLBACK_CANCEL;
237         } else {
238                 _D("messagre is act\n");
239
240         }
241
242         return ECORE_CALLBACK_CANCEL;
243 }
244 #endif
245
246 static Eina_Bool _ask_kill_process(void *data)
247 {
248         struct appdata *ad = data;
249
250         _del_progressbar(ad);
251
252         ad->mode *= 2;
253         /* why? check enum in taskmgr.h */
254
255         if (ad->popup_ask) {
256                 evas_object_del(ad->popup_ask);
257                 ad->popup_ask = NULL;
258         }
259         ad->popup_ask = _add_popup_ask(ad->win,
260                         "It might be an invalid process. Do you want to kill this proceess anyway?",
261                         ad);
262
263         return ECORE_CALLBACK_CANCEL;
264 }
265
266 void _ok_response_cb(void *data, Evas_Object *obj, void *event_info)
267 {
268         struct appdata *ad = (struct appdata *)data;
269
270         retm_if(data == NULL, "Invalid argument: appdata is NULL\n");
271         if (ad->popup_ask) {
272                 evas_object_del(ad->popup_ask);
273                 ad->popup_ask = NULL;
274         }
275
276         switch (ad->mode) {
277                 case MODE_END_INUSE:
278                         _D("end inuse\n");
279                         _del_popup_timer(ad);
280                         ad->popup_timer = ecore_timer_add(7.0, _ask_kill_process, 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                         ad->popup_timer = ecore_timer_add(7.0, _ask_kill_process, ad);
290                         _show_progressbar(ad);
291                         response_end_all_inuse(ad);
292                         break;
293
294                 case MODE_DEL_HISTORY:
295                         _D("del inuse\n");
296                         _del_popup_timer(ad);
297                         ad->popup_timer = ecore_timer_add(5.0, _ask_kill_process, ad);
298                         response_del_history(ad);
299                         break;
300
301                 case MODE_DEL_ALL_HISTORY:
302                         _D("del all inuse\n");
303                         _del_popup_timer(ad);
304                         ad->popup_timer = ecore_timer_add(5.0, _ask_kill_process, ad);
305                         response_del_all_history(ad);
306                         break;
307
308                 case MODE_KILL_INUSE:
309                         _D("kill all inuse\n");
310                         response_kill_inuse(ad);
311                         break;
312
313                 case MODE_KILL_ALL_INUSE:
314                         _D("kill all inuse\n");
315                         response_kill_all_inuse(ad);
316                         break;
317
318                 default:
319                         printf("[Wanning] taskmanager: check mode [%d]\n",
320                                         ad->mode);
321                         break;
322         }
323 }
324
325 void _cancel_response_cb(void *data, Evas_Object *obj, void *event_info)
326 {
327         struct appdata *ad = (struct appdata *)data;
328
329         retm_if(data == NULL, "Invalid argument: appdata is NULL\n");
330         if (ad->popup_ask) {
331                 evas_object_del(ad->popup_ask);
332                 ad->popup_ask = NULL;
333         }
334 }
335
336 Eina_Bool _create_idler_cb(void *data)
337 {
338         Evas_Object *pu, *bt1, *bt2;
339         struct appdata *ad = (struct appdata *)data;
340         retvm_if(ad == NULL, ECORE_CALLBACK_CANCEL, "Invalid argument\n");
341
342         evas_object_show(ad->win);
343
344 #if HAVE_X
345         _key_grab(ad);
346 #endif
347
348         _init_pthread();
349         _get_win_geometry(ad);
350         _set_vconf_noti(ad);
351 #if HAVE_X
352         ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __climsg_cb, ad);
353 #endif
354
355         return ECORE_CALLBACK_CANCEL;
356 }
357
358
359