tizen 2.3 release
[framework/system/deviced.git] / src / display / setting.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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 <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <stdbool.h>
24
25 #include "core.h"
26 #include "util.h"
27 #include "setting.h"
28 #include "weaks.h"
29
30 #define LCD_DIM_RATIO           0.3
31 #define LCD_MAX_DIM_TIMEOUT     7000
32 #define LCD_MIN_DIM_TIMEOUT     500
33
34 static const char *setting_keys[SETTING_GET_END] = {
35         [SETTING_TO_NORMAL] = VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL,
36         [SETTING_BRT_LEVEL] = VCONFKEY_SETAPPL_LCD_BRIGHTNESS,
37         [SETTING_LOCK_SCREEN] = VCONFKEY_IDLE_LOCK_STATE,
38         [SETTING_SMART_STAY] = VCONFKEY_SETAPPL_SMARTSCREEN_SMARTSTAY_STATUS,
39         [SETTING_BOOT_POWER_ON_STATUS] = VCONFKEY_DEVICED_BOOT_POWER_ON_STATUS,
40         [SETTING_POWER_CUSTOM_BRIGHTNESS] = VCONFKEY_PM_CUSTOM_BRIGHTNESS_STATUS,
41         [SETTING_ACCESSIBILITY_TTS] = VCONFKEY_SETAPPL_ACCESSIBILITY_TTS,
42 };
43
44 static int lock_screen_state = VCONFKEY_IDLE_UNLOCK;
45 static bool lock_screen_bg_state = false;
46 static int force_lcdtimeout = 0;
47 static int custom_on_timeout = 0;
48 static int custom_normal_timeout = 0;
49 static int custom_dim_timeout = 0;
50
51 int (*update_pm_setting) (int key_idx, int val);
52
53 int set_force_lcdtimeout(int timeout)
54 {
55         if (timeout < 0)
56                 return -EINVAL;
57
58         force_lcdtimeout = timeout;
59
60         return 0;
61 }
62
63 int get_lock_screen_state(void)
64 {
65         return lock_screen_state;
66 }
67
68 void set_lock_screen_state(int state)
69 {
70         switch (state) {
71         case VCONFKEY_IDLE_LOCK:
72         case VCONFKEY_IDLE_UNLOCK:
73                 lock_screen_state = state;
74                 break;
75         default:
76                 lock_screen_state = VCONFKEY_IDLE_UNLOCK;
77         }
78 }
79
80 int get_lock_screen_bg_state(void)
81 {
82         return lock_screen_bg_state;
83 }
84
85 void set_lock_screen_bg_state(bool state)
86 {
87         _I("state is %d", state);
88         lock_screen_bg_state = state;
89 }
90
91 int get_charging_status(int *val)
92 {
93         return vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, val);
94 }
95
96 int get_lowbatt_status(int *val)
97 {
98         return vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, val);
99 }
100
101 int get_usb_status(int *val)
102 {
103         return vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, val);
104 }
105
106 int set_setting_pmstate(int val)
107 {
108         return vconf_set_int(VCONFKEY_PM_STATE, val);
109 }
110
111 int get_setting_brightness(int *level)
112 {
113         return vconf_get_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, level);
114 }
115
116 int get_dim_timeout(int *dim_timeout)
117 {
118         int vconf_timeout, on_timeout, val, ret;
119
120         if (custom_dim_timeout > 0) {
121                 *dim_timeout = custom_dim_timeout;
122                 return 0;
123         }
124
125         if (get_ambient_mode != NULL &&
126             get_ambient_mode() == true) {
127                 *dim_timeout = LCD_MIN_DIM_TIMEOUT;
128                 return 0;
129         }
130
131         ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
132         if (ret != 0) {
133                 _E("Failed ro get setting timeout!");
134                 vconf_timeout = DEFAULT_NORMAL_TIMEOUT;
135         }
136
137         if (force_lcdtimeout > 0)
138                 on_timeout = SEC_TO_MSEC(force_lcdtimeout);
139         else
140                 on_timeout = SEC_TO_MSEC(vconf_timeout);
141
142         val = (double)on_timeout * LCD_DIM_RATIO;
143         if (val > LCD_MAX_DIM_TIMEOUT)
144                 val = LCD_MAX_DIM_TIMEOUT;
145
146         *dim_timeout = val;
147
148         return 0;
149 }
150
151 int get_run_timeout(int *timeout)
152 {
153         int dim_timeout = -1;
154         int vconf_timeout = -1;
155         int on_timeout;
156         int ret;
157
158         if (custom_normal_timeout > 0) {
159                 *timeout = custom_normal_timeout;
160                 return 0;
161         }
162
163         ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
164         if (ret != 0) {
165                 _E("Failed ro get setting timeout!");
166                 vconf_timeout = DEFAULT_NORMAL_TIMEOUT;
167         }
168
169         if (force_lcdtimeout > 0)
170                 on_timeout = SEC_TO_MSEC(force_lcdtimeout);
171         else
172                 on_timeout = SEC_TO_MSEC(vconf_timeout);
173
174         if (on_timeout < 0)
175                 return -ERANGE;
176
177         if (on_timeout == 0) {
178                 *timeout = on_timeout;
179                 return 0;
180         }
181
182         get_dim_timeout(&dim_timeout);
183         *timeout = on_timeout - dim_timeout;
184         return 0;
185 }
186
187 int set_custom_lcdon_timeout(int timeout)
188 {
189         int changed = (custom_on_timeout == timeout ? false : true);
190
191         custom_on_timeout = timeout;
192
193         if (timeout <= 0) {
194                 custom_normal_timeout = 0;
195                 custom_dim_timeout = 0;
196                 return changed;
197         }
198
199         custom_dim_timeout = (double)timeout * LCD_DIM_RATIO;
200         custom_normal_timeout = timeout - custom_dim_timeout;
201
202         _I("custom normal(%d), dim(%d)", custom_normal_timeout,
203             custom_dim_timeout);
204
205         return changed;
206 }
207
208 static int setting_cb(keynode_t *key_nodes, void *data)
209 {
210         keynode_t *tmp = key_nodes;
211
212         if ((int)data > SETTING_END) {
213                 _E("Unknown setting key: %s, idx=%d",
214                        vconf_keynode_get_name(tmp), (int)data);
215                 return -1;
216         }
217         if (update_pm_setting != NULL) {
218                 switch((int)data) {
219                         case SETTING_ACCESSIBILITY_TTS:
220                                 update_pm_setting((int)data, vconf_keynode_get_bool(tmp));
221                                 break;
222                         default:
223                                 update_pm_setting((int)data, vconf_keynode_get_int(tmp));
224                                 break;
225                 }
226         }
227
228         return 0;
229 }
230
231 int init_setting(int (*func) (int key_idx, int val))
232 {
233         int i;
234
235         if (func != NULL)
236                 update_pm_setting = func;
237
238         for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
239                 vconf_notify_key_changed(setting_keys[i], (void *)setting_cb,
240                                          (void *)i);
241         }
242
243         return 0;
244 }
245
246 int exit_setting(void)
247 {
248         int i;
249         for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
250                 vconf_ignore_key_changed(setting_keys[i], (void *)setting_cb);
251         }
252
253         return 0;
254 }
255