cba5971aa8738bfee28ba91bfdfbbfe98eb4cea9
[platform/core/system/system-popup.git] / src / poweroff / poweroff.c
1 /*
2  *  system-popup
3  *
4  * Copyright (c) 2014 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 "popup-common.h"
21
22 #define SYSTEMD_STOP_POWER_OFF 4
23
24 #define POWER_BUS_NAME        "org.tizen.system.deviced"
25 #define POWER_OBJECT_PATH     "/Org/Tizen/System/DeviceD/Power"
26 #define POWER_INTERFACE_NAME  POWER_BUS_NAME".power"
27
28 #define POWER_METHOD            "reboot"
29 #define POWER_OPERATION_OFF     "poweroff"
30
31 static void remove_popup(const struct popup_ops *ops);
32 static void pm_state_changed(keynode_t *key, void *data);
33 static void event_back_key_up(void *data, Evas_Object *obj, void *event_info);
34 static void register_handlers(const struct popup_ops *ops);
35 static void unregister_handlers(const struct popup_ops *ops);
36 static int poweroff_launch(bundle *b, const struct popup_ops *ops);
37 static void poweroff_terminate(const struct popup_ops *ops);
38 static void poweroff_clicked(const struct popup_ops *ops);
39 static __attribute__ ((constructor)) void poweroff_register_popup(void);
40
41 static void remove_popup(const struct popup_ops *ops)
42 {
43         static bool terminating = false;
44
45         if (terminating)
46                 return;
47
48         terminating = true;
49
50         unload_simple_popup(ops);
51         popup_terminate();
52 }
53
54 static void pm_state_changed(keynode_t *key, void *data)
55 {
56         const struct popup_ops *ops = data;
57
58         if (!key)
59                 return;
60
61         if (vconf_keynode_get_int(key) != VCONFKEY_PM_STATE_LCDOFF)
62                 return;
63
64         remove_popup(ops);
65 }
66
67 static void event_back_key_up(void *data, Evas_Object *obj, void *event_info)
68 {
69         const struct popup_ops *ops = data;
70         remove_popup(ops);
71 }
72
73 static void register_handlers(const struct popup_ops *ops)
74 {
75         Evas_Object *win;
76
77         if (vconf_notify_key_changed(
78                                 VCONFKEY_PM_STATE,
79                                 pm_state_changed,
80                                 (void *)ops) != 0)
81                 _E("Failed to register vconf");
82
83         win = get_window();
84         if (win)
85                 eext_object_event_callback_add(win, EEXT_CALLBACK_BACK, event_back_key_up, (void*)ops);
86 }
87
88 static void unregister_handlers(const struct popup_ops *ops)
89 {
90         Evas_Object *win;
91
92         vconf_ignore_key_changed(VCONFKEY_PM_STATE, pm_state_changed);
93
94         win = get_window();
95         if (win)
96                 eext_object_event_callback_del(win, EEXT_CALLBACK_BACK, event_back_key_up);
97 }
98
99 static int poweroff_launch(bundle *b, const struct popup_ops *ops)
100 {
101         register_handlers(ops);
102         return 0;
103 }
104
105 static void poweroff_terminate(const struct popup_ops *ops)
106 {
107         unregister_handlers(ops);
108 }
109
110 static void poweroff_clicked(const struct popup_ops *ops)
111 {
112         Evas_Object *rect, *win;
113         Evas_Coord w, h, size;
114         static int bPowerOff = 0;
115         char *param[2];
116         char data[8];
117         int ret;
118
119         if (bPowerOff == 1)
120                 return;
121         bPowerOff = 1;
122
123         unload_simple_popup(ops);
124
125         win = get_window();
126         if (!win)
127                 popup_terminate();
128
129         rect = evas_object_rectangle_add(evas_object_evas_get(win));
130         evas_object_geometry_get(win, NULL, NULL, &w, &h);
131         size = max(w, h);
132         evas_object_resize(rect, size, size);
133         evas_object_color_set(rect, 0, 0, 0, 255);
134         evas_object_show(rect);
135
136         if (vconf_set_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, SYSTEMD_STOP_POWER_OFF) != 0)
137                 _E("Failed to request poweroff to deviced");
138
139         param[0] = POWER_OPERATION_OFF;
140         snprintf(data, sizeof(data), "0");
141         param[1] = data;
142         ret = popup_dbus_method_sync(POWER_BUS_NAME,
143                         POWER_OBJECT_PATH,
144                         POWER_INTERFACE_NAME,
145                         POWER_METHOD,
146                         "si", param);
147         if (ret < 0)
148                 _E("Failed to request poweroff to deviced (%d)", ret);
149 }
150
151 static const struct popup_ops poweroff_ops = {
152         .name           = "poweroff",
153         .show           = load_simple_popup,
154         .title          = "IDS_ST_BODY_POWER_OFF",
155         .content        = "IDS_TPLATFORM_BODY_POWER_OFF_THE_DEVICE_Q",
156         .left_text      = "IDS_COM_SK_CANCEL",
157         .right_text     = "IDS_HS_BUTTON_POWER_OFF_ABB2",
158         .right          = poweroff_clicked,
159         .pre            = poweroff_launch,
160         .terminate      = poweroff_terminate,
161 };
162
163 static __attribute__ ((constructor)) void poweroff_register_popup(void)
164 {
165         register_popup(&poweroff_ops);
166 }