[Task-mgr] Fixed scale and long size name issues
[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 <appcore-efl.h>
22 #include <Elementary.h>
23 #include <aul.h>
24 #include <malloc.h>
25
26 #include "conf.h"
27 #include "item.h"
28 #include "list.h"
29 #include "log.h"
30 #include "main.h"
31 #include "scroller.h"
32 #include "util.h"
33
34 #define KEY_BACK "XF86Back"
35 #define PRIVATE_DATA_KEY_LIST_TIMER "pri_list_tm"
36
37 static main_s main_info = {
38         .e = NULL,
39         .win = NULL,
40         .layout = NULL,
41         .scroller = NULL,
42         .box = NULL,
43         .pkg_list = NULL,
44
45         .root_w = 0,
46         .root_h = 0,
47 };
48 //main_h main_info_h = &main_info;
49
50
51
52 extern main_h main_get_info(void)
53 {
54         return &main_info;
55 }
56
57
58
59 static Eina_Bool _back_key_pressed(void *data, Evas_Object *obj, Evas_Object *src, Evas_Callback_Type type, void *event_info)
60 {
61         _D("");
62         Evas_Event_Key_Down *ev = event_info;
63
64         if (type == EVAS_CALLBACK_KEY_DOWN && strncmp(KEY_BACK, ev->key, strlen(KEY_BACK)) == 0) {
65                 _D("KEY PRESSED: %s", ev->key);
66
67                 elm_exit();
68                 return EINA_TRUE;
69         } else {
70                 return EINA_FALSE;
71         }
72 }
73
74
75
76 static task_mgr_error_e _create_layout(Evas_Object *parent)
77 {
78         _D("");
79         Evas_Object *layout = NULL;
80         Evas_Object *rect = NULL;
81         Eina_Bool ret = EINA_FALSE;
82
83         // create layout
84         layout = elm_layout_add(parent);
85         goto_if (!layout, ERROR);
86
87         ret = elm_layout_file_set(layout, LAYOUT, "layout");
88         goto_if(EINA_FALSE == ret, ERROR);
89
90         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
91         evas_object_move(layout, 0, 0);
92         evas_object_resize(layout, main_info.root_w, main_info.root_h);
93         evas_object_show(layout);
94
95         main_info.layout = layout;
96
97         // create layout bg
98         rect = evas_object_rectangle_add(evas_object_evas_get(layout));
99         evas_object_size_hint_min_set(rect, main_info.root_w, main_info.root_h);
100         evas_object_size_hint_weight_set(rect, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
101         evas_object_resize(rect, main_info.root_w, main_info.root_h);
102         evas_object_color_set(rect, 0, 0, 0, BG_COLOR);
103         evas_object_show(rect);
104         elm_object_part_content_set(layout, BG_PART_NAME, rect);
105
106         // create scroller
107         main_info.scroller = scroller_create(layout);
108         goto_if(!main_info.scroller, ERROR);
109
110         return TASK_MGR_ERROR_NONE;
111
112 ERROR:
113         if (layout) evas_object_del(layout);
114         evas_object_del(main_info.win);
115         return TASK_MGR_ERROR_FAIL;
116 }
117
118
119
120 static void _destroy_layout(void)
121 {
122         Evas_Object *rect = NULL;
123
124         if (main_info.layout) {
125                 rect = elm_object_part_content_unset(main_info.layout, BG_PART_NAME);
126                 if (rect) {
127                         evas_object_del(rect);
128                 }
129                 evas_object_del(main_info.layout);
130                 main_info.layout = NULL;
131         }
132 }
133
134
135
136 static Eina_Bool _list_timer_cb(void *data)
137 {
138         _D("");
139         Evas_Object *clear_item = NULL;
140         task_mgr_error_e ret = TASK_MGR_ERROR_NONE;
141
142         if (main_info.pkg_list) {
143                 _D("Already loaded");
144                 goto END;
145         }
146
147         clear_item = item_clear_all_create(main_info.scroller);
148         if (!clear_item) {
149                 _E("Fail to create clear all button");
150                 return ECORE_CALLBACK_CANCEL;
151         }
152         scroller_push_item(main_info.scroller, clear_item);
153
154         ret = list_create(&main_info.pkg_list);
155                 elm_object_part_text_set(main_info.layout, "no,apps,txt", _("IDS_TASKMGR_NPBODY_NO_APPLICATIONS_ABB2"));
156         if (TASK_MGR_ERROR_NO_DATA == ret) {
157                 _D("There is no application");
158                 item_clear_set_disable(main_info.scroller);
159         } else if (TASK_MGR_ERROR_NONE != ret) {
160                 _E("Fail to create pkglist");
161                 goto END;
162         }
163
164         goto_if(TASK_MGR_ERROR_NONE != scroller_push_all_item(main_info.scroller, main_info.pkg_list), END);
165
166 END:
167         evas_object_data_del(main_info.win, PRIVATE_DATA_KEY_LIST_TIMER);
168         return ECORE_CALLBACK_CANCEL;
169 }
170
171
172
173 static bool _create_cb(void *data)
174 {
175         _D("");
176
177         Ecore_Timer *timer = NULL;
178
179         main_info.win = elm_win_add(NULL, "Task-mgr", ELM_WIN_BASIC);
180         retv_if(!main_info.win, false);
181
182         elm_app_base_scale_set(2.6);
183         elm_win_screen_size_get(main_info.win, NULL, NULL, &main_info.root_w, &main_info.root_h);
184         _D("screen size is (%d, %d)", main_info.root_w, main_info.root_h);
185
186         elm_object_event_callback_add(main_info.win, _back_key_pressed, NULL);
187
188         elm_win_alpha_set(main_info.win, EINA_TRUE);
189         elm_win_indicator_mode_set(main_info.win, ELM_WIN_INDICATOR_SHOW);
190         elm_win_indicator_opacity_set(main_info.win, ELM_WIN_INDICATOR_TRANSPARENT);
191
192         elm_win_borderless_set(main_info.win, EINA_TRUE);
193         evas_object_show(main_info.win);
194
195         main_info.e = evas_object_evas_get(main_info.win);
196
197         if (_create_layout(main_info.win) != TASK_MGR_ERROR_NONE) {
198                 _E("Failed to create a layout");
199                 return false;
200         }
201
202         //create the list
203         timer = evas_object_data_get(main_info.win, PRIVATE_DATA_KEY_LIST_TIMER);
204         if (timer) {
205                 ecore_timer_del(timer);
206         }
207
208         timer = ecore_timer_add(0.001f, _list_timer_cb, NULL);
209         if (timer) {
210                 evas_object_data_set(main_info.win, PRIVATE_DATA_KEY_LIST_TIMER, timer);
211         } else {
212                 _E("Cannot add a create list timer");
213         }
214
215         return true;
216 }
217
218
219
220 static void _terminate_cb(void *data)
221 {
222         _D("");
223
224         /* list destroy */
225         list_destroy(main_info.pkg_list);
226         evas_object_data_del(main_info.win, PRIVATE_DATA_KEY_LIST_TIMER);
227
228         elm_cache_all_flush();
229         malloc_trim(0);
230         appcore_flush_memory();
231
232         scroller_destroy(main_info.scroller);
233         _destroy_layout();
234
235         /**
236          * Even though the window is deleted automatically,
237          * It is good habit to delete window explicitly by your hands.
238          */
239
240         if (main_info.win) {
241                 evas_object_del(main_info.win);
242                 main_info.win = NULL;
243         }
244 }
245
246
247
248 static void _app_control(app_control_h service, void *data)
249 {
250         _D("");
251
252         return;
253 }
254
255
256
257 static void _pause_cb(void *data)
258 {
259         _D("");
260
261         elm_exit();
262         return;
263 }
264
265
266
267 static void _language_changed(app_event_info_h event_info, void *data)
268 {
269         _D("");
270 }
271
272
273
274 int main(int argc, char **argv)
275 {
276         int ret;
277         ui_app_lifecycle_callback_s lifecycle_callback = {0, };
278         app_event_handler_h event_handlers[5] = {NULL, };
279
280         lifecycle_callback.create = _create_cb;
281         lifecycle_callback.terminate = _terminate_cb;
282         lifecycle_callback.pause = _pause_cb;
283         lifecycle_callback.resume = NULL;
284         lifecycle_callback.app_control = _app_control;
285
286         ui_app_add_event_handler(&event_handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, NULL, NULL);
287         ui_app_add_event_handler(&event_handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, NULL, NULL);
288         ui_app_add_event_handler(&event_handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, NULL, NULL);
289         ui_app_add_event_handler(&event_handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, _language_changed, NULL);
290         ui_app_add_event_handler(&event_handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, NULL, NULL);
291
292         ret = ui_app_main(argc, argv, &lifecycle_callback, &main_info);
293
294         return ret;
295 }
296
297 /* End of a file */