Fix build warning
[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_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_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 static DBusMessage *battery_low_noti_on(E_DBus_Object *obj, DBusMessage *msg)
123 {
124         set_timer_to_terminate();
125         return activate_notification_no_param(obj, msg, BATTERY_LOW);
126 }
127
128 static DBusMessage *battery_critical_noti_on(E_DBus_Object *obj, DBusMessage *msg)
129 {
130         set_timer_to_terminate();
131         return activate_notification_no_param(obj, msg, BATTERY_CRITICAL);
132 }
133
134 static DBusMessage *battery_low_actnoti_on(E_DBus_Object *obj, DBusMessage *msg)
135 {
136         set_timer_to_terminate();
137         return activate_notification_single_param(obj, msg, BATTERY_LOW_ACT);
138 }
139
140 static DBusMessage *battery_critical_actnoti_on(E_DBus_Object *obj, DBusMessage *msg)
141 {
142         set_timer_to_terminate();
143         return activate_notification_single_param(obj, msg, BATTERY_CRITICAL_ACT);
144 }
145
146 static DBusMessage *battery_low_actnoti_usb_on(E_DBus_Object *obj, DBusMessage *msg)
147 {
148         set_timer_to_terminate();
149         return activate_notification_no_param(obj, msg, BATTERY_LOW_USB_ACT);
150 }
151
152 static DBusMessage *battery_critical_actnoti_usb_on(E_DBus_Object *obj, DBusMessage *msg)
153 {
154         set_timer_to_terminate();
155         return activate_notification_no_param(obj, msg, BATTERY_CRITICAL_USB_ACT);
156 }
157
158 static DBusMessage *battery_low_actnoti_chg_on(E_DBus_Object *obj, DBusMessage *msg)
159 {
160         set_timer_to_terminate();
161         return activate_notification_no_param(obj, msg, BATTERY_LOW_CHG_ACT);
162 }
163
164 static DBusMessage *battery_critical_actnoti_chg_on(E_DBus_Object *obj, DBusMessage *msg)
165 {
166         set_timer_to_terminate();
167         return activate_notification_no_param(obj, msg, BATTERY_CRITICAL_CHG_ACT);
168 }
169
170 /* Datausage */
171 static DBusMessage *data_warning_noti_on(E_DBus_Object *obj, DBusMessage *msg)
172 {
173         set_timer_to_terminate();
174         return activate_notification_no_param(obj, msg, DATAUSAGE_WARNING);
175 }
176
177 static DBusMessage *data_disabled_noti_on(E_DBus_Object *obj, DBusMessage *msg)
178 {
179         set_timer_to_terminate();
180         return activate_notification_no_param(obj, msg, DATAUSAGE_DISABLED);
181 }
182
183 /* Notification Off */
184 static DBusMessage *noti_off(E_DBus_Object *obj, DBusMessage *msg)
185 {
186         set_timer_to_terminate();
187         return deactivate_notification(obj, msg);
188 }
189
190 /* SD Card */
191 static DBusMessage *sdcard_noti(E_DBus_Object *obj, DBusMessage *msg)
192 {
193         set_timer_to_terminate();
194         return activate_notification_single_param(obj, msg, SDCARD_DEVICE);
195 }
196
197 /* LED */
198 static DBusMessage *led_torch_noti_on(E_DBus_Object *obj, DBusMessage *msg)
199 {
200         set_timer_to_terminate();
201         return activate_notification_no_param(obj, msg, LED_TORCH);
202 }
203
204 /* USB host notifications */
205 static DBusMessage *usb_storage_noti_on(E_DBus_Object *obj, DBusMessage *msg)
206 {
207         set_timer_to_terminate();
208         return activate_notification_single_param(obj, msg, USB_STORAGE);
209 }
210
211 /* Cooldown notification */
212 static DBusMessage *cooldown_noti_on(E_DBus_Object *obj, DBusMessage *msg)
213 {
214         set_timer_to_terminate();
215         return activate_notification_no_param(obj, msg, TEMP_COOLDOWN);
216 }
217
218 static DBusMessage *usb_storage_ro_noti_on(E_DBus_Object *obj, DBusMessage *msg)
219 {
220         set_timer_to_terminate();
221         return activate_notification_single_param(obj, msg, USB_STORAGE_RO);
222 }
223
224 static DBusMessage *usb_device_noti_on(E_DBus_Object *obj, DBusMessage *msg)
225 {
226         set_timer_to_terminate();
227         return activate_notification_double_param(obj, msg, USB_DEVICE);
228 }
229
230 static DBusMessage *usb_device_noti_update(E_DBus_Object *obj, DBusMessage *msg)
231 {
232         set_timer_to_terminate();
233         return update_notification_double_param(obj, msg, USB_DEVICE);
234 }
235
236 static DBusMessage *media_device_noti_on(E_DBus_Object *obj, DBusMessage *msg)
237 {
238         set_timer_to_terminate();
239         return activate_notification_no_param(obj, msg, MEDIA_DEVICE);
240 }
241
242 static const struct edbus_method
243 dbus_powerkey_methods[] = {
244         { "PopupLaunch", "a{ss}", "i", powerkey_popup },
245         /* Add methods here */
246 };
247
248 static const struct edbus_method
249 dbus_overheat_methods[] = {
250         { "PopupLaunch", "a{ss}", "i", overheat_popup },
251         /* Add methods here */
252 };
253
254 static const struct edbus_method
255 dbus_crash_methods[] = {
256         { "PopupLaunch", "a{ss}", "i", crash_popup      },
257         /* Add methods here */
258 };
259
260 static const struct edbus_method
261 dbus_noti_methods[] = {
262         /* LED */
263         { "LedTorchNotiOn"              , NULL          , "i"   , led_torch_noti_on             },
264         { "LedTorchNotiOff"             , "i"           , "i"   , noti_off                      },
265         /* USB storage */
266         { "UsbStorageNotiOn"            , "s"           , "i"   , usb_storage_noti_on           },
267         { "UsbStorageRoNotiOn"          , "s"           , "i"   , usb_storage_ro_noti_on        },
268         { "UsbStorageNotiOff"           , "i"           , "i"   , noti_off                      },
269         { "UsbDeviceNotiOn"             , "ss"          , "i"   , usb_device_noti_on            },
270         { "UsbDeviceNotiUpdate"         , "isss"        , "i"   , usb_device_noti_update        },
271         { "UsbDeviceNotiOff"            , "i"           , "i"   , noti_off                      },
272         /* usb connection */
273         { "MediaDeviceNotiOn"           , NULL          , "i"   , media_device_noti_on  },
274         { "MediaDeviceNotiOff"          , "i"           , "i"   , noti_off      },
275         /* Battery Event noti*/
276         { "BatteryFullNotiOn"           , NULL          , "i"   , battery_full_noti_on          },
277         { "BatteryFullNotiOff"          , "i"           , "i"   , noti_off                      },
278         { "BatteryChargeNotiOn"         , NULL          , "i"   , battery_charge_noti_on        },
279         { "BatteryLowNotiOn"            , NULL          , "i"   , battery_low_noti_on   },
280         { "BatteryCriticalNotiOn"       , NULL          , "i"   , battery_critical_noti_on      },
281         { "BatteryLowCriticalNotiOff"   , "i"           , "i"   , noti_off      },
282         /* Battery Active noti*/
283         { "BatteryLowActNotiOn"         , "s"           , "i"   , battery_low_actnoti_on        },
284         { "BatteryCriticalActNotiOn",   "s"             , "i"   , battery_critical_actnoti_on   },
285         { "BatteryLowActNotiUsbOn"      , NULL          , "i"   , battery_low_actnoti_usb_on    },
286         { "BatteryCriticalActNotiUsbOn" , NULL  , "i"   , battery_critical_actnoti_usb_on       },
287         { "BatteryLowActNotiChgOn"      , NULL          , "i"   , battery_low_actnoti_chg_on    },
288         { "BatteryCriticalActNotiChgOn" , NULL  , "i"   , battery_critical_actnoti_chg_on       },
289         /* Temperature */
290         { "TempCooldownNotiOn"          , NULL          , "i"   , cooldown_noti_on              },
291         { "TempCooldownNotiOff"         , "i"           , "i"   , noti_off                      },
292         /* SD Card */
293         { "SDcardNoti"                  , "s"           , "i"   , sdcard_noti   },
294         /* Data Usage */
295         { "DataWarningNotiOn"           , NULL          , "i"   , data_warning_noti_on          },
296         { "DataWarningNotiOff"          , "i"           , "i"   , noti_off                      },
297         { "DataDisabledNotiOn"          , NULL          , "i"   , data_disabled_noti_on },
298         { "DataDisabledNotiOff"         , "i"           , "i"   , noti_off              },
299         /* Add notifications here */
300 };
301
302 static struct edbus_object
303 edbus_objects[] = {
304         { POPUP_PATH_SYSTEM             , POPUP_IFACE_SYSTEM    , NULL  , NULL  ,
305                 dbus_system_methods     , ARRAY_SIZE(dbus_system_methods)               },
306         { POPUP_PATH_POWERKEY           , POPUP_IFACE_POWERKEY  , NULL  , NULL  ,
307                 dbus_powerkey_methods   , ARRAY_SIZE(dbus_powerkey_methods)             },
308         { POPUP_PATH_OVERHEAT           , POPUP_IFACE_OVERHEAT  , NULL  , NULL  ,
309                 dbus_overheat_methods   , ARRAY_SIZE(dbus_overheat_methods)             },
310         { POPUP_PATH_NOTI               , POPUP_IFACE_NOTI      , NULL  , NULL  ,
311                 dbus_noti_methods       , ARRAY_SIZE(dbus_noti_methods)                 },
312         { POPUP_PATH_CRASH              , POPUP_IFACE_CRASH     , NULL  , NULL  ,
313                 dbus_crash_methods      , ARRAY_SIZE(dbus_crash_methods)                },
314         /* Add new object & interface here*/
315 };
316
317 static int init_methods(void)
318 {
319         int ret;
320         int i, j;
321
322
323         for (i = 0; i < ARRAY_SIZE(edbus_objects); i++) {
324                 for (j = 0; j < edbus_objects[i].methods_len; j++) {
325                         ret = e_dbus_interface_method_add(edbus_objects[i].iface,
326                                         edbus_objects[i].methods[j].member,
327                                         edbus_objects[i].methods[j].signature,
328                                         edbus_objects[i].methods[j].reply_signature,
329                                         edbus_objects[i].methods[j].func);
330                         if (!ret) {
331                                 _E("fail to add method %s!", edbus_objects[i].methods[j].member);
332                                 return -ECONNREFUSED;
333                         }
334                 }
335         }
336         return 0;
337 }
338
339 static int register_dbus(void)
340 {
341         DBusError error;
342         int retry, ret, i;
343
344         dbus_error_init(&error);
345
346         retry = 0;
347         do {
348                 if (e_dbus_init())
349                         break;
350                 if (++retry == RETRY_MAX) {
351                         _E("fail to init edbus");
352                         return -ECONNREFUSED;
353                 }
354         } while (retry <= RETRY_MAX);
355
356         retry = 0;
357         do {
358                 edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
359                 if (edbus_conn)
360                         break;
361                 if (++retry == RETRY_MAX) {
362                         _E("fail to get edbus");
363                         ret = -ECONNREFUSED;
364                         goto out1;
365                 }
366         } while (retry <= RETRY_MAX);
367
368         retry = 0;
369         do {
370                 edbus_request_name = e_dbus_request_name(edbus_conn, BUS_NAME, 0, NULL, NULL);
371                 if (edbus_request_name)
372                         break;
373                 if (++retry == RETRY_MAX) {
374                         _E("fail to request edbus name");
375                         ret = -ECONNREFUSED;
376                         goto out2;
377                 }
378         } while (retry <= RETRY_MAX);
379
380         for (i = 0; i < ARRAY_SIZE(edbus_objects); i++) {
381                 edbus_objects[i].obj = e_dbus_object_add(edbus_conn, edbus_objects[i].path, NULL);
382                 if (!(edbus_objects[i].obj)) {
383                         _E("fail to add edbus obj");
384                         ret = -ECONNREFUSED;
385                         goto out2;
386                 }
387
388                 edbus_objects[i].iface = e_dbus_interface_new(edbus_objects[i].interface);
389                 if (!(edbus_objects[i].iface)) {
390                         _E("fail to add edbus interface");
391                         ret = -ECONNREFUSED;
392                         goto out2;
393                 }
394
395                 e_dbus_object_interface_attach(edbus_objects[i].obj, edbus_objects[i].iface);
396         }
397
398         return 0;
399
400 out2:
401         e_dbus_connection_close(edbus_conn);
402 out1:
403         e_dbus_shutdown();
404
405         return ret;
406 }
407
408 int main(int argc, char *argv[])
409 {
410         int ret;
411
412         ecore_init();
413
414         ret = register_dbus();
415         if (ret < 0)
416                 return ret;
417
418         ret = init_methods();
419         if (ret < 0)
420                 return ret;
421
422         ecore_main_loop_begin();
423         ecore_shutdown();
424         return 0;
425 }