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 REBOOT_OBJECT_PATH "/Org/Tizen/System/DeviceD/Reboot"
26 #define REBOOT_INTERFACE_NAME DEVICED_BUS_NAME".reboot"
28 #define REBOOT_METHOD "reboot"
29 #define REBOOT_OPERATION_OFF "poweroff"
30 #define REBOOT_OPERATION_RESTART "reboot"
31 #define TIMEOUT_POWEROFF 5 /* seconds */
33 #define POWEROFF_KEY "XF86PowerOff"
34 #define BACK_KEY "XF86Back"
35 #define HOME_KEY "XF86Home"
37 int clicked_index = 0;
39 static void pm_state_changed(keynode_t *key, void *data);
40 static void register_handlers(const struct popup_ops *ops);
41 static void unregister_handlers(const struct popup_ops *ops);
42 static int poweroff_launch(bundle *b, const struct popup_ops *ops);
43 static void poweroff_terminate(const struct popup_ops *ops);
44 static void powerkey_terminate(const struct popup_ops *ops);
45 static void poweroff_clicked(const struct popup_ops *ops);
46 static __attribute__ ((constructor)) void powerkey_register_popup(void);
47 static const struct popup_ops powerkey_ops;
48 static const struct popup_ops poweroff_ops;
49 static const struct popup_ops restart_ops;
51 static char *items[] = {
52 "IDS_ST_BODY_POWER_OFF",
53 "IDS_COM_SK_RESTART_ABB"
56 static void remove_other_powerkey_popups(const struct popup_ops *ops)
58 unload_simple_popup(&powerkey_ops);
59 unload_simple_popup(&poweroff_ops);
60 unload_simple_popup(&restart_ops);
63 static int restart_launch(bundle *b, const struct popup_ops *ops)
65 remove_other_powerkey_popups(ops);
69 static void restart_terminate(const struct popup_ops *ops)
74 static void restart_clicked(const struct popup_ops *ops)
76 static int bReset = 0;
81 if (bReset == 1) return;
84 unload_simple_popup(ops);
86 param[0] = REBOOT_OPERATION_RESTART;
87 snprintf(data, sizeof(data), "0");
90 ret = popup_dbus_method_sync(DEVICED_BUS_NAME,
92 REBOOT_INTERFACE_NAME,
96 if (ret < 0) _E("Failed to request restart to deviced (%d)", ret);
99 static char* gl_text_get_cb(void *data, Evas_Object *obj, const char *part)
101 int index = (int) data;
105 if (!strncmp("elm.text", part, sizeof("elm.text")))
106 return strdup(_(items[index]));
111 if (!strncmp("elm.text", part, sizeof("elm.text")))
112 return strdup(_(items[index]));
120 static void gl_sel_cb(void *data, Evas_Object *obj, void *event_info)
122 Elm_Object_Item *item = event_info;
125 clicked_index = (int)elm_object_item_data_get(item);
126 unload_simple_popup(data);
128 if (clicked_index == 0) {
129 _D("poweroff is chosen");
130 load_simple_popup(b, &poweroff_ops);
131 } else if (clicked_index == 1) {
132 _D("restart is chosen");
133 load_simple_popup(b, &restart_ops);
135 _E("Wrong button is pressed");
138 static Evas_Object* gl_image_get_cb(void *data, Evas_Object *obj, const char *part)
140 Evas_Object *img = elm_image_add(obj);
142 int index = (int) data;
152 if (!strncmp(part, "elm.swallow.icon", sizeof("elm.swallow.icon"))) {
153 elm_image_file_set(img, RESDIR"/core_power_off.png", NULL);
154 elm_image_object_size_get(img, &w, &h);
155 evas_object_size_hint_min_set(img, w, h);
158 } else if (!strncmp(part, "elm.swallow.icon.end", sizeof("elm.swallow.icon.end"))) {
159 check = elm_check_add(obj);
165 if (!strncmp(part, "elm.swallow.icon", sizeof("elm.swallow.icon"))) {
166 elm_image_file_set(img, RESDIR"/core_restart.png", NULL);
167 elm_image_object_size_get(img, &w, &h);
168 evas_object_size_hint_min_set(img, w, h);
171 } else if (!strncmp(part, "elm.swallow.icon.end", sizeof("elm.swallow.icon.end"))) {
172 check = elm_check_add(obj);
183 Eina_Bool KeyUpCb(void *data, int type, void *event)
185 Ecore_Event_Key *e = event;
186 const struct popup_ops *ops = data;
188 if (!strncmp(e->key, BACK_KEY, sizeof(BACK_KEY)) || !strncmp(e->key, HOME_KEY, sizeof(HOME_KEY))) {
189 _D("HOME_KEY or BACK KEY is pressed");
191 unload_simple_popup(ops);
192 terminate_if_no_popup();
195 // Let the event continue to other callbacks.
196 return ECORE_CALLBACK_PASS_ON;
199 int powerkey_list(bundle *b, const struct popup_ops *ops)
201 Elm_Genlist_Item_Class *itc;
202 Evas_Object *popup = NULL;
203 Evas_Object *box = NULL;
204 Evas_Object *genlist = NULL;
205 struct object_ops *obj;
210 if (!ops) return -EINVAL;
212 ret = get_object_by_ops(ops, &obj);
220 evas_object_show(win);
222 popup = elm_popup_add(win);
226 elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
227 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
229 elm_object_style_set(genlist, "default");
232 box = elm_box_add(popup);
233 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
235 /* back & home key */
236 ecore_event_handler_add(ECORE_EVENT_KEY_UP, KeyUpCb, (void*)ops);
239 genlist = elm_genlist_add(box);
240 evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
241 evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
243 itc = elm_genlist_item_class_new();
244 itc->item_style = "type1";
245 itc->func.text_get = gl_text_get_cb;
246 itc->func.content_get = gl_image_get_cb;
247 itc->func.state_get = NULL;
248 itc->func.del = NULL;
250 for (i = 0; i < 2; i++)
251 elm_genlist_item_append(genlist, itc, (void *) i, NULL, ELM_GENLIST_ITEM_NONE, gl_sel_cb, ops);
253 evas_object_show(genlist);
254 elm_box_pack_end(box, genlist);
255 elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
256 elm_scroller_content_min_limit(genlist, EINA_FALSE, EINA_TRUE);
257 elm_object_content_set(popup, box);
259 evas_object_show(popup);
262 elm_genlist_item_class_free(itc);
266 static void pm_state_changed(keynode_t *key, void *data)
268 const struct popup_ops *ops = data;
271 if (vconf_keynode_get_int(key) != VCONFKEY_PM_STATE_LCDOFF) return;
273 unload_simple_popup(ops);
276 static void register_handlers(const struct popup_ops *ops)
278 if (vconf_notify_key_changed(
282 _E("Failed to register vconf");
285 static void unregister_handlers(const struct popup_ops *ops)
287 vconf_ignore_key_changed(VCONFKEY_PM_STATE, pm_state_changed);
290 static int powerkey_list_launch(bundle *b, const struct popup_ops *ops)
292 unregister_handlers(ops);
293 remove_other_powerkey_popups(ops);
294 register_handlers(ops);
298 static int poweroff_launch(bundle *b, const struct popup_ops *ops)
300 remove_other_powerkey_popups(ops);
301 register_handlers(ops);
305 static void poweroff_terminate(const struct popup_ops *ops)
307 unregister_handlers(ops);
310 static void powerkey_terminate(const struct popup_ops *ops)
312 unregister_handlers(ops);
315 static void poweroff_clicked(const struct popup_ops *ops)
317 static int bPowerOff = 0;
322 if (bPowerOff == 1) return;
325 unload_simple_popup(ops);
327 param[0] = REBOOT_OPERATION_OFF;
328 snprintf(data, sizeof(data), "0");
330 ret = popup_dbus_method_sync(DEVICED_BUS_NAME,
332 REBOOT_INTERFACE_NAME,
335 if (ret < 0) _E("Failed to request poweroff to deviced (%d)", ret);
338 static const struct popup_ops restart_ops = {
340 .show = load_simple_popup,
341 .title = "IDS_COM_SK_RESTART_ABB",
342 .content = "IDS_ST_POP_DEVICE_WILL_RESTART",
343 .left_text = "IDS_COM_SK_CANCEL",
344 .right_text = "IDS_IDLE_BUTTON_RESTART_ABB3",
345 .right = restart_clicked,
346 .pre = restart_launch,
347 .terminate = restart_terminate,
350 static const struct popup_ops poweroff_ops = {
352 .show = load_simple_popup,
353 .title = "IDS_ST_BODY_POWER_OFF",
354 .content = "IDS_IDLE_POP_YOUR_PHONE_WILL_POWER_OFF",
355 .left_text = "IDS_COM_SK_CANCEL",
356 .right_text = "IDS_HS_BUTTON_POWER_OFF_ABB2",
357 .right = poweroff_clicked,
358 .pre = poweroff_launch,
359 .terminate = poweroff_terminate,
362 static const struct popup_ops powerkey_ops = {
364 .show = powerkey_list,
365 .pre = powerkey_list_launch,
366 .terminate = powerkey_terminate,
369 static __attribute__ ((constructor)) void powerkey_register_popup(void)
371 register_popup(&powerkey_ops);
372 register_popup(&poweroff_ops);
373 register_popup(&restart_ops);