display: Relocate ambient-mode file to core
[platform/core/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 "ambient-mode.h"
26 #include "core.h"
27 #include "util.h"
28 #include "setting.h"
29 #include "display-ops.h"
30 #include "shared/eventsystem.h"
31 #include "shared/plugin.h"
32
33 #define LCD_DIM_RATIO           0.3
34 #define LCD_MAX_DIM_TIMEOUT     7000
35 #define LCD_MIN_DIM_TIMEOUT     500
36
37 static const char *setting_keys[SETTING_GET_END] = {
38         [SETTING_TO_NORMAL] = VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL,
39         [SETTING_BRT_LEVEL] = VCONFKEY_SETAPPL_LCD_BRIGHTNESS,
40         [SETTING_LOCK_SCREEN] = VCONFKEY_IDLE_LOCK_STATE,
41         [SETTING_POWER_CUSTOM_BRIGHTNESS] = VCONFKEY_PM_CUSTOM_BRIGHTNESS_STATUS,
42 };
43
44 static struct display_plugin *disp_plgn;
45 static int lock_screen_state = VCONFKEY_IDLE_UNLOCK;
46 static bool lock_screen_bg_state = false;
47 static int force_lcdtimeout = 0;
48 static int custom_on_timeout = 0;
49 static int custom_normal_timeout = 0;
50 static int custom_dim_timeout = 0;
51
52 int (*update_pm_setting) (int key_idx, int val);
53
54 static gboolean display_state_send_system_event(gpointer data)
55 {
56         const char *str;
57         int state = (intptr_t)data;
58
59         if (state == S_NORMAL)
60                 str = EVT_VAL_DISPLAY_NORMAL;
61         else if (state == S_LCDDIM)
62                 str = EVT_VAL_DISPLAY_DIM;
63         else if (state == S_LCDOFF)
64                 str = EVT_VAL_DISPLAY_OFF;
65         else
66                 return G_SOURCE_REMOVE;
67
68         _I("Set pmstate of eventsystem : [%s]", str);
69
70         event_system_send(SYS_EVENT_DISPLAY_STATE, EVT_KEY_DISPLAY_STATE, str);
71
72         return G_SOURCE_REMOVE;
73 }
74
75 int set_force_lcdtimeout(int timeout)
76 {
77         if (timeout < 0)
78                 return -EINVAL;
79
80         force_lcdtimeout = timeout;
81
82         return 0;
83 }
84
85 int __get_lock_screen_state(void)
86 {
87         return lock_screen_state;
88 }
89
90 void set_lock_screen_state(int state)
91 {
92         switch (state) {
93         case VCONFKEY_IDLE_LOCK:
94         case VCONFKEY_IDLE_UNLOCK:
95                 lock_screen_state = state;
96                 break;
97         default:
98                 lock_screen_state = VCONFKEY_IDLE_UNLOCK;
99         }
100 }
101
102 int get_lock_screen_bg_state(void)
103 {
104         return lock_screen_bg_state;
105 }
106
107 void set_lock_screen_bg_state(bool state)
108 {
109         _I("state is %d", state);
110         lock_screen_bg_state = state;
111 }
112
113 int get_lowbatt_status(int *val)
114 {
115         return vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, val);
116 }
117
118 int get_usb_status(int *val)
119 {
120         return vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, val);
121 }
122
123 int set_setting_pmstate(int val)
124 {
125         static int prev = S_NORMAL;
126
127         if (prev == val)
128                 return 0;
129
130         prev = val;
131
132         g_idle_add(display_state_send_system_event, (gpointer)((intptr_t)val));
133         return vconf_set_int(VCONFKEY_PM_STATE, val);
134 }
135
136 int get_setting_brightness(int *level)
137 {
138         return vconf_get_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, level);
139 }
140
141 void get_dim_timeout(int *dim_timeout)
142 {
143         int vconf_timeout, on_timeout, val, ret;
144
145         assert(g_display_plugin.config);
146
147         if (custom_dim_timeout > 0) {
148                 *dim_timeout = custom_dim_timeout;
149                 return;
150         }
151
152         if (!g_display_plugin.config->dimming || ambient_get_condition()) {
153                 *dim_timeout = LCD_MIN_DIM_TIMEOUT;
154                 return;
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         val = (double)on_timeout * LCD_DIM_RATIO;
169         if (val > LCD_MAX_DIM_TIMEOUT)
170                 val = LCD_MAX_DIM_TIMEOUT;
171
172         *dim_timeout = val;
173 }
174
175 void get_run_timeout(int *timeout)
176 {
177         int dim_timeout = -1;
178         int vconf_timeout = -1;
179         int on_timeout;
180         int ret;
181
182         if (custom_normal_timeout > 0) {
183                 *timeout = custom_normal_timeout;
184                 return;
185         }
186
187         ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
188         if (ret < 0) {
189                 _E("Failed to get vconf value for setting timeout: %d", vconf_get_ext_errno());
190                 vconf_timeout = DEFAULT_NORMAL_TIMEOUT;
191         }
192
193         if (force_lcdtimeout > 0)
194                 on_timeout = SEC_TO_MSEC(force_lcdtimeout);
195         else
196                 on_timeout = SEC_TO_MSEC(vconf_timeout);
197
198         if (on_timeout == 0) {
199                 *timeout = on_timeout;
200                 return;
201         }
202
203         get_dim_timeout(&dim_timeout);
204         *timeout = on_timeout - dim_timeout;
205 }
206
207 int set_custom_lcdon_timeout(int timeout)
208 {
209         int changed = (custom_on_timeout == timeout ? false : true);
210
211         custom_on_timeout = timeout;
212
213         if (timeout <= 0) {
214                 custom_normal_timeout = 0;
215                 custom_dim_timeout = 0;
216                 return changed;
217         }
218
219         if (g_display_plugin.config && g_display_plugin.config->dimming)
220                 custom_dim_timeout = (double)timeout * LCD_DIM_RATIO;
221         else
222                 custom_dim_timeout = LCD_MIN_DIM_TIMEOUT;
223
224         custom_normal_timeout = timeout - custom_dim_timeout;
225
226         _I("custom normal(%d), dim(%d)", custom_normal_timeout,
227             custom_dim_timeout);
228
229         return changed;
230 }
231
232 static int setting_cb(keynode_t *key_nodes, void *data)
233 {
234         keynode_t *tmp = key_nodes;
235         int index;
236
237         index = (int)((intptr_t)data);
238         if (index > SETTING_END) {
239                 _E("Unknown setting key: %s, idx=%d",
240                        vconf_keynode_get_name(tmp), index);
241                 return -1;
242         }
243         if (disp_plgn->update_pm_setting)
244                 disp_plgn->update_pm_setting(index, vconf_keynode_get_int(tmp));
245
246         return 0;
247 }
248
249 int init_setting(int (*func) (int key_idx, int val))
250 {
251         int i;
252
253         if (func != NULL)
254                 disp_plgn->update_pm_setting = func;
255
256         for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
257                 /*
258                  * To pass an index data through the vconf infratstructure
259                  * without memory allocation, an index data becomes typecast
260                  * to proper pointer size on each architecture.
261                  */
262                 vconf_notify_key_changed(setting_keys[i], (void *)setting_cb,
263                                          (void *)((intptr_t)i));
264         }
265
266         disp_plgn->get_lock_screen_state = __get_lock_screen_state;
267
268         return 0;
269 }
270
271 int exit_setting(void)
272 {
273         int i;
274         for (i = SETTING_BEGIN; i < SETTING_GET_END; i++)
275                 vconf_ignore_key_changed(setting_keys[i], (void *)setting_cb);
276
277         return 0;
278 }
279
280 static void __CONSTRUCTOR__ initialize(void)
281 {
282         disp_plgn = get_var_display_plugin();
283         if (!disp_plgn) {
284                 _E("Failed to get display plugin variable.");
285         }
286 }