Initialize Tizen 2.3
[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
29 #define LCD_DIM_RATIO           0.2
30 #define LCD_MAX_DIM_TIMEOUT     7000
31
32 static const char *setting_keys[SETTING_GET_END] = {
33         [SETTING_TO_NORMAL] = VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL,
34         [SETTING_BRT_LEVEL] = VCONFKEY_SETAPPL_LCD_BRIGHTNESS,
35         [SETTING_LOCK_SCREEN] = VCONFKEY_IDLE_LOCK_STATE,
36         [SETTING_POWER_SAVING] = VCONFKEY_SETAPPL_PWRSV_SYSMODE_STATUS,
37         [SETTING_POWER_SAVING_DISPLAY] = VCONFKEY_SETAPPL_PWRSV_CUSTMODE_DISPLAY,
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         ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
126         if (ret != 0) {
127                 _E("Failed ro get setting timeout!");
128                 vconf_timeout = DEFAULT_NORMAL_TIMEOUT;
129         }
130
131         if (force_lcdtimeout > 0)
132                 on_timeout = SEC_TO_MSEC(force_lcdtimeout);
133         else
134                 on_timeout = SEC_TO_MSEC(vconf_timeout);
135
136         val = (double)on_timeout * LCD_DIM_RATIO;
137         if (val > LCD_MAX_DIM_TIMEOUT)
138                 val = LCD_MAX_DIM_TIMEOUT;
139
140         *dim_timeout = val;
141
142         return 0;
143 }
144
145 int get_run_timeout(int *timeout)
146 {
147         int dim_timeout = -1;
148         int vconf_timeout = -1;
149         int on_timeout;
150         int ret;
151
152         if (custom_normal_timeout > 0) {
153                 *timeout = custom_normal_timeout;
154                 return 0;
155         }
156
157         ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
158         if (ret != 0) {
159                 _E("Failed ro get setting timeout!");
160                 vconf_timeout = DEFAULT_NORMAL_TIMEOUT;
161         }
162
163         if (force_lcdtimeout > 0)
164                 on_timeout = SEC_TO_MSEC(force_lcdtimeout);
165         else
166                 on_timeout = SEC_TO_MSEC(vconf_timeout);
167
168         get_dim_timeout(&dim_timeout);
169
170         if (on_timeout <= 0)
171                 ret = -ERANGE;
172         else
173                 *timeout = on_timeout - dim_timeout;
174
175         return ret;
176 }
177
178 int set_custom_lcdon_timeout(int timeout)
179 {
180         int changed = (custom_on_timeout == timeout ? false : true);
181
182         custom_on_timeout = timeout;
183
184         if (timeout <= 0) {
185                 custom_normal_timeout = 0;
186                 custom_dim_timeout = 0;
187                 return changed;
188         }
189
190         custom_dim_timeout = (double)timeout * LCD_DIM_RATIO;
191         custom_normal_timeout = timeout - custom_dim_timeout;
192
193         _I("custom normal(%d), dim(%d)", custom_normal_timeout,
194             custom_dim_timeout);
195
196         return changed;
197 }
198
199 static int setting_cb(keynode_t *key_nodes, void *data)
200 {
201         keynode_t *tmp = key_nodes;
202
203         if ((int)data > SETTING_END) {
204                 _E("Unknown setting key: %s, idx=%d",
205                        vconf_keynode_get_name(tmp), (int)data);
206                 return -1;
207         }
208         if (update_pm_setting != NULL) {
209                 switch((int)data) {
210                         case SETTING_POWER_SAVING:
211                         case SETTING_POWER_SAVING_DISPLAY:
212                         case SETTING_ACCESSIBILITY_TTS:
213                                 update_pm_setting((int)data, vconf_keynode_get_bool(tmp));
214                                 break;
215                         default:
216                                 update_pm_setting((int)data, vconf_keynode_get_int(tmp));
217                                 break;
218                 }
219         }
220
221         return 0;
222 }
223
224 int init_setting(int (*func) (int key_idx, int val))
225 {
226         int i;
227
228         if (func != NULL)
229                 update_pm_setting = func;
230
231         for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
232                 vconf_notify_key_changed(setting_keys[i], (void *)setting_cb,
233                                          (void *)i);
234         }
235
236         return 0;
237 }
238
239 int exit_setting(void)
240 {
241         int i;
242         for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
243                 vconf_ignore_key_changed(setting_keys[i], (void *)setting_cb);
244         }
245
246         return 0;
247 }
248