migrate to Tizen 3.0 SDK
[apps/core/preloaded/taskmanager.git] / src / main.c
1 /*
2  *  Task Manager
3  *
4  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <app.h>
21 #include <Elementary.h>
22 #include <malloc.h>
23
24 #include "conf.h"
25 #include "item.h"
26 #include "list.h"
27 #include "log.h"
28 #include "main.h"
29 #include "scroller.h"
30 #include "util.h"
31
32 #define KEY_BACK "XF86Back"
33 #define PRIVATE_DATA_KEY_LIST_TIMER "pri_list_tm"
34
35 static main_s main_info = {
36         .e = NULL,
37         .win = NULL,
38         .layout = NULL,
39         .scroller = NULL,
40         .box = NULL,
41         .pkg_list = NULL,
42
43         .root_w = 0,
44         .root_h = 0,
45 };
46 //main_h main_info_h = &main_info;
47
48
49
50 main_h main_get_info(void)
51 {
52         return &main_info;
53 }
54
55
56
57 static Eina_Bool _back_key_pressed(void *data, Evas_Object *obj, Evas_Object *src, Evas_Callback_Type type, void *event_info)
58 {
59         _D("");
60         Evas_Event_Key_Down *ev = event_info;
61
62         if (type == EVAS_CALLBACK_KEY_DOWN && strncmp(KEY_BACK, ev->key, strlen(KEY_BACK)) == 0) {
63                 _D("KEY PRESSED: %s", ev->key);
64
65                 elm_exit();
66                 return EINA_TRUE;
67         } else {
68                 return EINA_FALSE;
69         }
70 }
71
72
73
74 static task_mgr_error_e _create_layout(Evas_Object *parent)
75 {
76         _D("");
77         Evas_Object *layout = NULL;
78         Eina_Bool ret = EINA_FALSE;
79
80         // create layout
81         layout = elm_layout_add(parent);
82         goto_if (!layout, ERROR);
83
84         ret = elm_layout_file_set(layout, util_get_file_path(APP_DIR_RESOURCE, LAYOUT_EDJ), "layout");
85         goto_if(EINA_FALSE == ret, ERROR);
86
87         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
88         evas_object_move(layout, 0, 0);
89         evas_object_resize(layout, main_info.root_w, main_info.root_h);
90         evas_object_show(layout);
91
92         main_info.layout = layout;
93
94         // create scroller
95         main_info.scroller = scroller_create(layout);
96         goto_if(!main_info.scroller, ERROR);
97
98         return TASK_MGR_ERROR_NONE;
99
100 ERROR:
101         if (layout) evas_object_del(layout);
102         evas_object_del(main_info.win);
103         return TASK_MGR_ERROR_FAIL;
104 }
105
106
107
108 static void _destroy_layout(void)
109 {
110         if (main_info.layout) {
111                 evas_object_del(main_info.layout);
112                 main_info.layout = NULL;
113         }
114 }
115
116
117
118 static Eina_Bool _list_timer_cb(void *data)
119 {
120         _D("");
121         Evas_Object *clear_item = NULL;
122         task_mgr_error_e ret = TASK_MGR_ERROR_NONE;
123
124         if (main_info.pkg_list) {
125                 _D("Already loaded");
126                 goto END;
127         }
128
129         clear_item = item_clear_all_create(main_info.scroller);
130         if (!clear_item) {
131                 _E("Fail to create clear all button");
132                 return ECORE_CALLBACK_CANCEL;
133         }
134         scroller_push_item(main_info.scroller, clear_item);
135
136         ret = list_create(&main_info.pkg_list);
137         elm_object_part_text_set(main_info.layout, "no,apps,txt", _("IDS_TASKMGR_NPBODY_NO_APPLICATIONS_ABB2"));
138
139         if (TASK_MGR_ERROR_NO_DATA == ret) {
140                 _D("There is no application");
141                 item_clear_set_disable(main_info.scroller);
142         } else if (TASK_MGR_ERROR_NONE != ret) {
143                 _E("Fail to create pkglist");
144                 goto END;
145         }
146
147         goto_if(TASK_MGR_ERROR_NONE != scroller_push_all_item(main_info.scroller, main_info.pkg_list), END);
148
149 END:
150         evas_object_data_del(main_info.win, PRIVATE_DATA_KEY_LIST_TIMER);
151         return ECORE_CALLBACK_CANCEL;
152 }
153
154
155
156 static bool _create_cb(void *data)
157 {
158         _D("");
159
160         Ecore_Timer *timer = NULL;
161
162         main_info.win = elm_win_add(NULL, "Task-mgr", ELM_WIN_BASIC);
163         retv_if(!main_info.win, false);
164
165         elm_win_screen_size_get(main_info.win, NULL, NULL, &main_info.root_w, &main_info.root_h);
166         _D("screen size is (%d, %d)", main_info.root_w, main_info.root_h);
167
168         elm_object_event_callback_add(main_info.win, _back_key_pressed, NULL);
169
170         elm_win_indicator_mode_set(main_info.win, ELM_WIN_INDICATOR_SHOW);
171         elm_win_indicator_opacity_set(main_info.win, ELM_WIN_INDICATOR_TRANSPARENT);
172
173         elm_win_borderless_set(main_info.win, EINA_TRUE);
174         elm_win_alpha_set(main_info.win, EINA_TRUE);
175         evas_object_show(main_info.win);
176
177         main_info.e = evas_object_evas_get(main_info.win);
178
179         if (_create_layout(main_info.win) != TASK_MGR_ERROR_NONE) {
180                 _E("Failed to create a layout");
181                 return false;
182         }
183
184         //create the list
185         timer = evas_object_data_get(main_info.win, PRIVATE_DATA_KEY_LIST_TIMER);
186         if (timer) {
187                 ecore_timer_del(timer);
188         }
189
190         timer = ecore_timer_add(0.001f, _list_timer_cb, NULL);
191         if (timer) {
192                 evas_object_data_set(main_info.win, PRIVATE_DATA_KEY_LIST_TIMER, timer);
193         } else {
194                 _E("Cannot add a create list timer");
195         }
196
197         return true;
198 }
199
200
201
202 static void _terminate_cb(void *data)
203 {
204         _D("");
205
206         /* list destroy */
207         list_destroy(main_info.pkg_list);
208         evas_object_data_del(main_info.win, PRIVATE_DATA_KEY_LIST_TIMER);
209
210         elm_cache_all_flush();
211         malloc_trim(0);
212
213         scroller_destroy(main_info.layout);
214         _destroy_layout();
215
216         /**
217          * Even though the window is deleted automatically,
218          * It is good habit to delete window explicitly by your hands.
219          */
220
221         if (main_info.win) {
222                 evas_object_del(main_info.win);
223                 main_info.win = NULL;
224         }
225 }
226
227
228
229 static void _app_control(app_control_h service, void *data)
230 {
231         _D("");
232
233         return;
234 }
235
236
237
238 static void _pause_cb(void *data)
239 {
240         _D("");
241
242         elm_exit();
243         return;
244 }
245
246
247
248 static void _language_changed(app_event_info_h event_info, void *data)
249 {
250         _D("");
251 }
252
253
254
255 int main(int argc, char **argv)
256 {
257         int ret;
258         ui_app_lifecycle_callback_s lifecycle_callback = {0, };
259         app_event_handler_h event_handlers[5] = {NULL, };
260
261         lifecycle_callback.create = _create_cb;
262         lifecycle_callback.terminate = _terminate_cb;
263         lifecycle_callback.pause = _pause_cb;
264         lifecycle_callback.resume = NULL;
265         lifecycle_callback.app_control = _app_control;
266
267         ui_app_add_event_handler(&event_handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, NULL, NULL);
268         ui_app_add_event_handler(&event_handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, NULL, NULL);
269         ui_app_add_event_handler(&event_handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, NULL, NULL);
270         ui_app_add_event_handler(&event_handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, _language_changed, NULL);
271         ui_app_add_event_handler(&event_handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, NULL, NULL);
272
273         ret = ui_app_main(argc, argv, &lifecycle_callback, &main_info);
274
275         return ret;
276 }
277
278 /* End of a file */