Cooldown popup: Change the format of cooldown popup
[platform/core/system/system-popup.git] / src / launcher / launcher.c
1 /*
2  * popup-launcher
3  *
4  * Copyright (c) 2016 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 "launcher.h"
21
22 #define TERMINATE_TIMEOUT  5
23
24 static E_DBus_Connection *edbus_conn;
25 static DBusPendingCall *edbus_request_name;
26 static Ecore_Timer *term_timer = NULL;
27
28 extern DBusMessage *activate_notification_no_param(E_DBus_Object *obj, DBusMessage *msg, int type);
29 extern DBusMessage *launch_system_servant_app(E_DBus_Object *obj,       DBusMessage *msg, char **argv);
30 extern DBusMessage *activate_notification_single_param(E_DBus_Object *obj, DBusMessage *msg, int type);
31 extern DBusMessage *activate_notification_double_param(E_DBus_Object *obj, DBusMessage *msg, int type);
32 extern DBusMessage *deactivate_notification(E_DBus_Object *obj, DBusMessage *msg);
33 extern DBusMessage *update_notification_double_param(E_DBus_Object *obj, DBusMessage *msg, int type);
34
35 static Eina_Bool exit_idler_cb(void *data)
36 {
37         e_dbus_connection_close(edbus_conn);
38         e_dbus_shutdown();
39
40         ecore_main_loop_quit();
41         return ECORE_CALLBACK_CANCEL;
42 }
43
44 static Eina_Bool terminate_launcher(void *data)
45 {
46         if (term_timer)
47                 ecore_timer_del(term_timer);
48
49         if (ecore_idler_add(exit_idler_cb, NULL))
50                 return ECORE_CALLBACK_CANCEL;
51
52         exit_idler_cb(NULL);
53         return ECORE_CALLBACK_CANCEL;
54 }
55
56 static void set_timer_to_terminate(void)
57 {
58         if (term_timer)
59                 ecore_timer_reset(term_timer);
60         else {
61                 term_timer = ecore_timer_add(TERMINATE_TIMEOUT, terminate_launcher, NULL);
62                 if (!term_timer)
63                         terminate_launcher(NULL);
64         }
65 }
66
67 /* Basic popups */
68 static DBusMessage *system_popup(E_DBus_Object *obj, DBusMessage *msg)
69 {
70         set_timer_to_terminate();
71         return launch_popup(obj, msg, SYSTEM_SYSPOPUP);
72 }
73
74 static const struct edbus_method
75 dbus_system_methods[] = {
76         { "PopupLaunch", "a{ss}", "i", system_popup },
77         /* Add methods here */
78 };
79
80 /* Powerkey popup */
81 static DBusMessage *powerkey_popup(E_DBus_Object *obj, DBusMessage *msg)
82 {
83         set_timer_to_terminate();
84         return launch_powerkey_popup(obj, msg, POWERKEY_SYSPOPUP);
85 }
86
87 /* Overheat popup */
88 static DBusMessage *overheat_popup(E_DBus_Object *obj, DBusMessage *msg)
89 {
90         set_timer_to_terminate();
91         return launch_overheat_popup(obj, msg, OVERHEAT_SYSPOPUP);
92 }
93
94 /* Crash popup */
95 static DBusMessage *crash_popup(E_DBus_Object *obj, DBusMessage *msg)
96 {
97         set_timer_to_terminate();
98         return launch_popup(obj, msg, CRASH_SYSPOPUP);
99 }
100
101 /* Battery notifications */
102 static DBusMessage *battery_full_noti_on(E_DBus_Object *obj, DBusMessage *msg)
103 {
104         set_timer_to_terminate();
105         return activate_notification_no_param(obj, msg, BATTERY_FULL);
106 }
107
108 static DBusMessage *battery_charge_noti_on(E_DBus_Object *obj, DBusMessage *msg)
109 {
110         char param[2];
111         char *args[3];
112
113         set_timer_to_terminate();
114
115         args[0] = SERVANT_APP_NAME;
116         snprintf(param, sizeof(param), "%d", CHARGER_CONNECTION);
117         args[1] = param;
118         args[2] = NULL;
119         return launch_system_servant_app(obj, msg, args);
120 }
121
122 /* Notification Off */
123 static DBusMessage *noti_off(E_DBus_Object *obj, DBusMessage *msg)
124 {
125         set_timer_to_terminate();
126         return deactivate_notification(obj, msg);
127 }
128
129 /* LED */
130 static DBusMessage *led_torch_noti_on(E_DBus_Object *obj, DBusMessage *msg)
131 {
132         set_timer_to_terminate();
133         return activate_notification_no_param(obj, msg, LED_TORCH);
134 }
135
136 /* USB host notifications */
137 static DBusMessage *usb_storage_noti_on(E_DBus_Object *obj, DBusMessage *msg)
138 {
139         set_timer_to_terminate();
140         return activate_notification_single_param(obj, msg, USB_STORAGE);
141 }
142
143 /* Cooldown notification */
144 static DBusMessage *cooldown_noti_on(E_DBus_Object *obj, DBusMessage *msg)
145 {
146         set_timer_to_terminate();
147         return activate_notification_no_param(obj, msg, TEMP_COOLDOWN);
148 }
149
150 static DBusMessage *usb_storage_ro_noti_on(E_DBus_Object *obj, DBusMessage *msg)
151 {
152         set_timer_to_terminate();
153         return activate_notification_single_param(obj, msg, USB_STORAGE_RO);
154 }
155
156 static DBusMessage *usb_device_noti_on(E_DBus_Object *obj, DBusMessage *msg)
157 {
158         set_timer_to_terminate();
159         return activate_notification_double_param(obj, msg, USB_DEVICE);
160 }
161
162 static DBusMessage *usb_device_noti_update(E_DBus_Object *obj, DBusMessage *msg)
163 {
164         set_timer_to_terminate();
165         return update_notification_double_param(obj, msg, USB_DEVICE);
166 }
167
168 static DBusMessage *media_device_noti_on(E_DBus_Object *obj, DBusMessage *msg)
169 {
170         set_timer_to_terminate();
171         return activate_notification_no_param(obj, msg, MEDIA_DEVICE);
172 }
173
174 static const struct edbus_method
175 dbus_powerkey_methods[] = {
176         { "PopupLaunch", NULL, "i", powerkey_popup },
177         /* Add methods here */
178 };
179
180 static const struct edbus_method
181 dbus_overheat_methods[] = {
182         { "PopupLaunch", "a{ss}", "i", overheat_popup },
183         /* Add methods here */
184 };
185
186 static const struct edbus_method
187 dbus_crash_methods[] = {
188         { "PopupLaunch", "a{ss}", "i", crash_popup      },
189         /* Add methods here */
190 };
191
192 static const struct edbus_method
193 dbus_noti_methods[] = {
194         /* LED */
195         { "LedTorchNotiOn"              , NULL          , "i"   , led_torch_noti_on             },
196         { "LedTorhNotiOff"              , "i"           , "i"   , noti_off                      },
197         /* USB storage */
198         { "UsbStorageNotiOn"            , "s"           , "i"   , usb_storage_noti_on           },
199         { "UsbStorageRoNotiOn"          , "s"           , "i"   , usb_storage_ro_noti_on        },
200         { "UsbStorageNotiOff"           , "i"           , "i"   , noti_off                      },
201         { "UsbDeviceNotiOn"             , "ss"          , "i"   , usb_device_noti_on            },
202         { "UsbDeviceNotiUpdate"         , "isss"        , "i"   , usb_device_noti_update        },
203         { "UsbDeviceNotiOff"            , "i"           , "i"   , noti_off                      },
204         /* usb connection */
205         { "MediaDeviceNotiOn"           , NULL          , "i"   , media_device_noti_on          },
206         { "MediaDeviceNotiOff"          , "i"           , "i"   , noti_off                      },
207         /* Battery */
208         { "BatteryFullNotiOn"           , NULL          , "i"   , battery_full_noti_on          },
209         { "BatteryFullNotiOff"          , "i"           , "i"   , noti_off                      },
210         { "BatteryChargeNotiOn"         , NULL          , "i"   , battery_charge_noti_on        },
211         /* Temperature */
212         { "TempCooldownNotiOn"          , NULL          , "i"   , cooldown_noti_on              },
213         { "TempCooldownNotiOff"         , "i"           , "i"   , noti_off                      },
214         /* Add notifications here */
215 };
216
217 static struct edbus_object
218 edbus_objects[] = {
219         { POPUP_PATH_SYSTEM             , POPUP_IFACE_SYSTEM    , NULL  , NULL  ,
220                 dbus_system_methods     , ARRAY_SIZE(dbus_system_methods)               },
221         { POPUP_PATH_POWERKEY           , POPUP_IFACE_POWERKEY  , NULL  , NULL  ,
222                 dbus_powerkey_methods   , ARRAY_SIZE(dbus_powerkey_methods)             },
223         { POPUP_PATH_OVERHEAT           , POPUP_IFACE_OVERHEAT  , NULL  , NULL  ,
224                 dbus_overheat_methods   , ARRAY_SIZE(dbus_overheat_methods)             },
225         { POPUP_PATH_NOTI               , POPUP_IFACE_NOTI      , NULL  , NULL  ,
226                 dbus_noti_methods       , ARRAY_SIZE(dbus_noti_methods)                 },
227         { POPUP_PATH_CRASH              , POPUP_IFACE_CRASH     , NULL  , NULL  ,
228                 dbus_crash_methods      , ARRAY_SIZE(dbus_crash_methods)                },
229         /* Add new object & interface here*/
230 };
231
232 static int init_methods(void)
233 {
234         int ret;
235         int i, j;
236
237
238         for (i = 0; i < ARRAY_SIZE(edbus_objects); i++) {
239                 for (j = 0; j < edbus_objects[i].methods_len; j++) {
240                         ret = e_dbus_interface_method_add(edbus_objects[i].iface,
241                                         edbus_objects[i].methods[j].member,
242                                         edbus_objects[i].methods[j].signature,
243                                         edbus_objects[i].methods[j].reply_signature,
244                                         edbus_objects[i].methods[j].func);
245                         if (!ret) {
246                                 _E("fail to add method %s!", edbus_objects[i].methods[j].member);
247                                 return -ECONNREFUSED;
248                         }
249                 }
250         }
251         return 0;
252 }
253
254 static int register_dbus(void)
255 {
256         DBusError error;
257         int retry, ret, i;
258
259         dbus_error_init(&error);
260
261         retry = 0;
262         do {
263                 if (e_dbus_init())
264                         break;
265                 if (++retry == RETRY_MAX) {
266                         _E("fail to init edbus");
267                         return -ECONNREFUSED;
268                 }
269         } while (retry <= RETRY_MAX);
270
271         retry = 0;
272         do {
273                 edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
274                 if (edbus_conn)
275                         break;
276                 if (++retry == RETRY_MAX) {
277                         _E("fail to get edbus");
278                         ret = -ECONNREFUSED;
279                         goto out1;
280                 }
281         } while (retry <= RETRY_MAX);
282
283         retry = 0;
284         do {
285                 edbus_request_name = e_dbus_request_name(edbus_conn, BUS_NAME, 0, NULL, NULL);
286                 if (edbus_request_name)
287                         break;
288                 if (++retry == RETRY_MAX) {
289                         _E("fail to request edbus name");
290                         ret = -ECONNREFUSED;
291                         goto out2;
292                 }
293         } while (retry <= RETRY_MAX);
294
295         for (i = 0; i < ARRAY_SIZE(edbus_objects); i++) {
296                 edbus_objects[i].obj = e_dbus_object_add(edbus_conn, edbus_objects[i].path, NULL);
297                 if (!(edbus_objects[i].obj)) {
298                         _E("fail to add edbus obj");
299                         ret = -ECONNREFUSED;
300                         goto out2;
301                 }
302
303                 edbus_objects[i].iface = e_dbus_interface_new(edbus_objects[i].interface);
304                 if (!(edbus_objects[i].iface)) {
305                         _E("fail to add edbus interface");
306                         ret = -ECONNREFUSED;
307                         goto out2;
308                 }
309
310                 e_dbus_object_interface_attach(edbus_objects[i].obj, edbus_objects[i].iface);
311         }
312
313         return 0;
314
315 out2:
316         e_dbus_connection_close(edbus_conn);
317 out1:
318         e_dbus_shutdown();
319
320         return ret;
321 }
322
323 int main(int argc, char *argv[])
324 {
325         int ret;
326
327         ecore_init();
328
329         ret = register_dbus();
330         if (ret < 0)
331                 return ret;
332
333         ret = init_methods();
334         if (ret < 0)
335                 return ret;
336
337         ecore_main_loop_begin();
338         ecore_shutdown();
339         return 0;
340 }