4 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "popup-common.h"
22 #define SYSTEMD_STOP_POWER_OFF 4
24 #define DEVICED_BUS_NAME "org.tizen.system.deviced"
25 #define POWER_OBJECT_PATH "/Org/Tizen/System/DeviceD/Power"
26 #define POWER_INTERFACE_NAME DEVICED_BUS_NAME".power"
28 #define POWER_METHOD "reboot"
29 #define POWER_OPERATION_OFF "poweroff"
30 #define POWER_OPERATION_RESTART "reboot"
31 #define TIMEOUT_POWEROFF 5 /* seconds */
33 #define DEVICED_OBJECT_PATH "/Org/Tizen/System/DeviceD"
34 #define DEVICED_INTERFACE_NAME DEVICED_BUS_NAME
36 int clicked_index = 0;
38 static void remove_popup(const struct popup_ops *ops);
39 static void pm_state_changed(keynode_t *key, void *data);
40 static void event_back_key_up(void *data, Evas_Object *obj, void *event_info);
41 static void register_handlers(const struct popup_ops *ops);
42 static void unregister_handlers(const struct popup_ops *ops);
43 static int poweroff_launch(bundle *b, const struct popup_ops *ops);
44 static void poweroff_terminate(const struct popup_ops *ops);
45 static void powerkey_terminate(const struct popup_ops *ops);
46 static void poweroff_clicked(const struct popup_ops *ops);
47 static __attribute__ ((constructor)) void powerkey_register_popup(void);
48 static const struct popup_ops powerkey_ops;
49 static const struct popup_ops poweroff_ops;
50 static const struct popup_ops restart_ops;
52 static char *items[] = {
53 "IDS_ST_BODY_POWER_OFF",
54 "IDS_COM_SK_RESTART_ABB"
57 static int restart_launch(bundle *b, const struct popup_ops *ops)
62 static void restart_terminate(const struct popup_ops *ops)
67 static void restart_clicked(const struct popup_ops *ops)
69 Evas_Object *rect, *win;
70 Evas_Coord w, h, size;
71 static int bReset = 0;
76 if (bReset == 1) return;
79 unload_simple_popup(ops);
82 if (!win) popup_terminate();
84 rect = evas_object_rectangle_add(evas_object_evas_get(win));
85 evas_object_geometry_get(win, NULL, NULL, &w, &h);
87 evas_object_resize(rect, size, size);
88 evas_object_color_set(rect, 0, 0, 0, 255);
89 evas_object_show(rect);
91 param[0] = POWER_OPERATION_RESTART;
92 snprintf(data, sizeof(data), "0");
95 ret = popup_dbus_method_sync(DEVICED_BUS_NAME,
101 if (ret < 0) _E("Failed to request restart to deviced (%d)", ret);
104 static char* gl_text_get_cb(void *data, Evas_Object *obj, const char *part)
106 int index = (int) data;
110 if (!strncmp("elm.text", part, sizeof("elm.text")))
111 return strdup(_(items[index]));
116 if (!strncmp("elm.text", part, sizeof("elm.text")))
117 return strdup(_(items[index]));
125 static void gl_sel_cb(void *data, Evas_Object *obj, void *event_info)
127 Elm_Object_Item *item = event_info;
130 clicked_index = (int)elm_object_item_data_get(item);
131 unload_simple_popup(data);
133 if (clicked_index == 0) {
134 _D("poweroff is chosen");
135 load_simple_popup(b, &poweroff_ops);
136 } else if (clicked_index == 1) {
137 _D("restart is chosen");
138 load_simple_popup(b, &restart_ops);
140 _E("Wrong button is pressed");
143 static Evas_Object* gl_image_get_cb(void *data, Evas_Object *obj, const char *part)
145 Evas_Object *img = elm_image_add(obj);
147 int index = (int) data;
156 if (!strncmp(part, "elm.swallow.icon", sizeof("elm.swallow.icon"))) {
157 elm_image_file_set(img, RESDIR"/core_power_off.png", NULL);
158 evas_object_size_hint_min_set(img, ELM_SCALE_SIZE(50), ELM_SCALE_SIZE(50));
161 } else if (!strncmp(part, "elm.swallow.icon.end", sizeof("elm.swallow.icon.end"))) {
162 check = elm_check_add(obj);
168 if (!strncmp(part, "elm.swallow.icon", sizeof("elm.swallow.icon"))) {
169 elm_image_file_set(img, RESDIR"/core_restart.png", NULL);
170 evas_object_size_hint_min_set(img, ELM_SCALE_SIZE(50), ELM_SCALE_SIZE(50));
173 } else if (!strncmp(part, "elm.swallow.icon.end", sizeof("elm.swallow.icon.end"))) {
174 check = elm_check_add(obj);
185 int powerkey_list(bundle *b, const struct popup_ops *ops)
187 Elm_Genlist_Item_Class *itc = elm_genlist_item_class_new();
188 Evas_Object *popup = NULL;
189 Evas_Object *box = NULL;
190 Evas_Object *genlist = NULL;
191 struct object_ops *obj;
196 if (!ops) return -EINVAL;
198 ret = get_object_by_ops(ops, &obj);
199 if (ret < 0) return -EINVAL;
202 if (!win) return -ENOMEM;
204 evas_object_show(win);
206 popup = elm_popup_add(win);
207 if (!popup) return -ENOMEM;
209 elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
210 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
212 elm_object_style_set(genlist, "default");
215 box = elm_box_add(popup);
216 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
219 genlist = elm_genlist_add(box);
220 evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
221 evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
223 itc->item_style = "type1";
224 itc->func.text_get = gl_text_get_cb;
225 itc->func.content_get = gl_image_get_cb;
226 itc->func.state_get = NULL;
227 itc->func.del = NULL;
229 for (i = 0; i < 2; i++)
230 elm_genlist_item_append(genlist, itc, (void *) i, NULL, ELM_GENLIST_ITEM_NONE, gl_sel_cb, ops);
232 evas_object_show(genlist);
233 elm_box_pack_end(box, genlist);
234 evas_object_size_hint_min_set(box, -1, 350);
235 elm_object_content_set(popup, box);
237 evas_object_show(popup);
240 elm_genlist_item_class_free(itc);
244 static void remove_popup(const struct popup_ops *ops)
246 static bool terminating = false;
248 if (terminating) return;
251 unload_simple_popup(ops);
255 static void pm_state_changed(keynode_t *key, void *data)
257 const struct popup_ops *ops = data;
260 if (vconf_keynode_get_int(key) != VCONFKEY_PM_STATE_LCDOFF) return;
265 static void event_back_key_up(void *data, Evas_Object *obj, void *event_info)
267 const struct popup_ops *ops = data;
269 if (ops) remove_popup(ops);
270 terminate_if_no_popup();
273 static void register_handlers(const struct popup_ops *ops)
277 if (vconf_notify_key_changed(
281 _E("Failed to register vconf");
284 if (win) eext_object_event_callback_add(win, EEXT_CALLBACK_BACK, event_back_key_up, (void*)ops);
287 static void unregister_handlers(const struct popup_ops *ops)
291 vconf_ignore_key_changed(VCONFKEY_PM_STATE, pm_state_changed);
294 if (win) eext_object_event_callback_del(win, EEXT_CALLBACK_BACK, event_back_key_up);
297 static int powerkey_list_launch(bundle *b, const struct popup_ops *ops)
299 register_handlers(ops);
303 static int poweroff_launch(bundle *b, const struct popup_ops *ops)
305 register_handlers(ops);
309 static void poweroff_terminate(const struct popup_ops *ops)
311 unregister_handlers(ops);
314 static void powerkey_terminate(const struct popup_ops *ops)
316 unregister_handlers(ops);
319 static void poweroff_clicked(const struct popup_ops *ops)
321 Evas_Object *rect, *win;
322 Evas_Coord w, h, size;
323 static int bPowerOff = 0;
328 if (bPowerOff == 1) return;
331 unload_simple_popup(ops);
334 if (!win) popup_terminate();
336 rect = evas_object_rectangle_add(evas_object_evas_get(win));
337 evas_object_geometry_get(win, NULL, NULL, &w, &h);
339 evas_object_resize(rect, size, size);
340 evas_object_color_set(rect, 0, 0, 0, 255);
341 evas_object_show(rect);
343 param[0] = POWER_OPERATION_OFF;
344 snprintf(data, sizeof(data), "0");
346 ret = popup_dbus_method_sync(DEVICED_BUS_NAME,
348 POWER_INTERFACE_NAME,
351 if (ret < 0) _E("Failed to request poweroff to deviced (%d)", ret);
354 static const struct popup_ops restart_ops = {
356 .show = load_simple_popup,
357 .title = "IDS_COM_SK_RESTART_ABB",
358 .content = "IDS_ST_POP_DEVICE_WILL_RESTART",
359 .left_text = "IDS_COM_SK_CANCEL",
360 .right_text = "IDS_IDLE_BUTTON_RESTART_ABB3",
361 .right = restart_clicked,
362 .pre = restart_launch,
363 .terminate = restart_terminate,
366 static const struct popup_ops poweroff_ops = {
368 .show = load_simple_popup,
369 .title = "IDS_ST_BODY_POWER_OFF",
370 .content = "IDS_TPLATFORM_BODY_POWER_OFF_THE_DEVICE_Q",
371 .left_text = "IDS_COM_SK_CANCEL",
372 .right_text = "IDS_HS_BUTTON_POWER_OFF_ABB2",
373 .right = poweroff_clicked,
374 .pre = poweroff_launch,
375 .terminate = poweroff_terminate,
378 static const struct popup_ops powerkey_ops = {
380 .show = powerkey_list,
381 .pre = powerkey_list_launch,
382 .terminate = powerkey_terminate,
385 static __attribute__ ((constructor)) void powerkey_register_popup(void)
387 register_popup(&powerkey_ops);
388 register_popup(&poweroff_ops);
389 register_popup(&restart_ops);