d60807d267f431a38acee717f9e3d4580c4e35f4
[apps/livebox/data-provider-master.git] / src / setting.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <errno.h>
25
26 #include <vconf.h>
27 #include <dlog.h>
28 #include <heynoti.h>
29
30 #include <Eina.h>
31
32 #include "client_life.h"
33 #include "setting.h"
34 #include "util.h"
35 #include "debug.h"
36 #include "slave_life.h"
37 #include "critical_log.h"
38 #include "xmonitor.h"
39 #include "conf.h"
40
41 int errno;
42
43 static struct {
44         int heyfd;
45 } s_info = {
46         .heyfd = -1,
47 };
48
49 static void lcd_state_cb(keynode_t *node, void *user_data)
50 {
51         if (!node)
52                 return;
53
54         xmonitor_handle_state_changes();
55 }
56
57 HAPI int setting_is_lcd_off(void)
58 {
59         int state;
60
61         if (vconf_get_int(VCONFKEY_PM_STATE, &state) != 0) {
62                 ErrPrint("Idle lock state is not valid\n");
63                 state = VCONFKEY_PM_STATE_NORMAL; /* UNLOCK */
64         }
65
66         DbgPrint("State: %d, (%d:lcdoff, %d:sleep)\n", state, VCONFKEY_PM_STATE_LCDOFF, VCONFKEY_PM_STATE_SLEEP);
67         return state == VCONFKEY_PM_STATE_LCDOFF || state == VCONFKEY_PM_STATE_SLEEP;
68 }
69
70 static void power_off_cb(void *data)
71 {
72         CRITICAL_LOG("Terminated(heynoti)\n");
73
74         if (creat("/tmp/.stop.provider", 0644) < 0)
75                 ErrPrint("Failed to create .stop.provider [%s]\n", strerror(errno));
76
77         exit(0);
78 }
79
80 HAPI int setting_init(void)
81 {
82         int ret;
83
84         ret = vconf_notify_key_changed(VCONFKEY_PM_STATE, lcd_state_cb, NULL);
85         if (ret < 0)
86                 ErrPrint("Failed to add vconf for lock state\n");
87
88         s_info.heyfd = heynoti_init();
89         if (s_info.heyfd < 0) {
90                 CRITICAL_LOG("Failed to set poweroff heynoti [%d]\n", s_info.heyfd);
91                 return 0;
92         }
93
94         ret = heynoti_subscribe(s_info.heyfd, "power_off_start", power_off_cb, NULL);
95         if (ret < 0)
96                 CRITICAL_LOG("Failed to subscribe heynoti for power off [%d]\n", ret);
97
98         ret = heynoti_attach_handler(s_info.heyfd);
99         if (ret < 0)
100                 CRITICAL_LOG("Failed to attach heynoti handler [%d]\n", ret);
101
102         return ret;
103 }
104
105 HAPI int setting_fini(void)
106 {
107         int ret;
108         ret = vconf_ignore_key_changed(VCONFKEY_PM_STATE, lcd_state_cb);
109         return ret;
110 }
111
112 /* End of a file */