Added #ifdefs to functions that are only works in X
[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         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 /* X specific */
209 #if HAVE_X
210 /* this func is to exit taskmanager after launching application */
211 static Eina_Bool __climsg_cb(void *data, int type, void *event)
212 {
213         static Atom a_deact;
214         pid_t pid_a, pid_d;
215
216         struct appdata *ad = (struct appdata *)data;
217         Ecore_X_Event_Client_Message *ev = event;
218
219         if(ev == NULL) {
220                 _E("Invalid argument: event is NULL\n");
221                 ecore_timer_add(0.3, _exit_cb, NULL);
222                 return ECORE_CALLBACK_CANCEL;
223         }
224
225         pid_a = ev->data.l[1];
226         pid_d = ev->data.l[3];
227         a_deact = ecore_x_atom_get("_X_ILLUME_DEACTIVATE_WINDOW");
228
229         /* when pid_a == pid_d, this is useless data */
230         if (pid_a == pid_d) {
231                 return ECORE_CALLBACK_RENEW;
232         }
233
234         if (ev->message_type == a_deact) {
235                 _D("exit after 1.0 sec\n");
236                 ad->exit_timer = ecore_timer_add(0.3, _exit_cb, ad);
237                 return ECORE_CALLBACK_CANCEL;
238         } else {
239                 _D("messagre is act\n");
240
241         }
242
243         return ECORE_CALLBACK_CANCEL;
244 }
245 #endif
246
247 static Eina_Bool _ask_kill_process(void *data)
248 {
249         struct appdata *ad = data;
250
251         _del_progressbar(ad);
252
253         ad->mode *= 2;
254         /* why? check enum in taskmgr.h */
255
256         if (ad->popup_ask) {
257                 evas_object_del(ad->popup_ask);
258                 ad->popup_ask = NULL;
259         }
260         ad->popup_ask = _add_popup_ask(ad->win,
261                         "It might be an invalid process. Do you want to kill this proceess anyway?",
262                         ad);
263
264         return ECORE_CALLBACK_CANCEL;
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                         ad->popup_timer = ecore_timer_add(7.0, _ask_kill_process, ad);
282                         _show_progressbar(ad);
283                         response_end_inuse(ad);
284                         _restart_pthread(ad);
285                         break;
286
287                 case MODE_END_ALL_INUSE:
288                         _D("end all inuse\n");
289                         _del_popup_timer(ad);
290                         ad->popup_timer = ecore_timer_add(7.0, _ask_kill_process, ad);
291                         _show_progressbar(ad);
292                         response_end_all_inuse(ad);
293                         break;
294
295                 case MODE_DEL_HISTORY:
296                         _D("del inuse\n");
297                         _del_popup_timer(ad);
298                         ad->popup_timer = ecore_timer_add(5.0, _ask_kill_process, ad);
299                         response_del_history(ad);
300                         break;
301
302                 case MODE_DEL_ALL_HISTORY:
303                         _D("del all inuse\n");
304                         _del_popup_timer(ad);
305                         ad->popup_timer = ecore_timer_add(5.0, _ask_kill_process, ad);
306                         response_del_all_history(ad);
307                         break;
308
309                 case MODE_KILL_INUSE:
310                         _D("kill all inuse\n");
311                         response_kill_inuse(ad);
312                         break;
313
314                 case MODE_KILL_ALL_INUSE:
315                         _D("kill all inuse\n");
316                         response_kill_all_inuse(ad);
317                         break;
318
319                 default:
320                         printf("[Wanning] taskmanager: check mode [%d]\n",
321                                         ad->mode);
322                         break;
323         }
324 }
325
326 void _cancel_response_cb(void *data, Evas_Object *obj, void *event_info)
327 {
328         struct appdata *ad = (struct appdata *)data;
329
330         retm_if(data == NULL, "Invalid argument: appdata is NULL\n");
331         if (ad->popup_ask) {
332                 evas_object_del(ad->popup_ask);
333                 ad->popup_ask = NULL;
334         }
335 }
336
337 Eina_Bool _create_idler_cb(void *data)
338 {
339         Evas_Object *pu, *bt1, *bt2;
340         struct appdata *ad = (struct appdata *)data;
341         retvm_if(ad == NULL, ECORE_CALLBACK_CANCEL, "Invalid argument\n");
342
343         evas_object_show(ad->win);
344
345 #if HAVE_X
346         _key_grab(ad);
347 #endif
348
349         _init_pthread();
350         _get_win_geometry(ad);
351         _set_vconf_noti(ad);
352 #if HAVE_X
353         ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __climsg_cb, ad);
354 #endif
355
356         return ECORE_CALLBACK_CANCEL;
357 }
358
359
360