4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include "ambient-mode.h"
29 #include "display-ops.h"
30 #include "shared/eventsystem.h"
31 #include "shared/plugin.h"
33 #define LCD_DIM_RATIO 0.3
34 #define LCD_MAX_DIM_TIMEOUT 7000
35 #define LCD_MIN_DIM_TIMEOUT 500
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,
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;
52 int (*update_pm_setting) (int key_idx, int val);
54 static gboolean display_state_send_system_event(gpointer data)
57 int state = (intptr_t)data;
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;
66 return G_SOURCE_REMOVE;
68 _I("Set pmstate of eventsystem : [%s]", str);
70 event_system_send(SYS_EVENT_DISPLAY_STATE, EVT_KEY_DISPLAY_STATE, str);
72 return G_SOURCE_REMOVE;
75 int set_force_lcdtimeout(int timeout)
80 force_lcdtimeout = timeout;
85 int __get_lock_screen_state(void)
87 return lock_screen_state;
90 void set_lock_screen_state(int state)
93 case VCONFKEY_IDLE_LOCK:
94 case VCONFKEY_IDLE_UNLOCK:
95 lock_screen_state = state;
98 lock_screen_state = VCONFKEY_IDLE_UNLOCK;
102 int get_lock_screen_bg_state(void)
104 return lock_screen_bg_state;
107 void set_lock_screen_bg_state(bool state)
109 _I("state is %d", state);
110 lock_screen_bg_state = state;
113 int get_lowbatt_status(int *val)
115 return vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, val);
118 int get_usb_status(int *val)
120 return vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, val);
123 int set_setting_pmstate(int val)
125 static int prev = S_NORMAL;
132 g_idle_add(display_state_send_system_event, (gpointer)((intptr_t)val));
133 return vconf_set_int(VCONFKEY_PM_STATE, val);
136 int get_setting_brightness(int *level)
138 return vconf_get_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, level);
141 void get_dim_timeout(int *dim_timeout)
143 int vconf_timeout, on_timeout, val, ret;
145 assert(g_display_plugin.config);
147 if (custom_dim_timeout > 0) {
148 *dim_timeout = custom_dim_timeout;
152 if (!g_display_plugin.config->dimming || ambient_get_condition()) {
153 *dim_timeout = LCD_MIN_DIM_TIMEOUT;
157 ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
159 _E("Failed ro get setting timeout!");
160 vconf_timeout = DEFAULT_NORMAL_TIMEOUT;
163 if (force_lcdtimeout > 0)
164 on_timeout = SEC_TO_MSEC(force_lcdtimeout);
166 on_timeout = SEC_TO_MSEC(vconf_timeout);
168 val = (double)on_timeout * LCD_DIM_RATIO;
169 if (val > LCD_MAX_DIM_TIMEOUT)
170 val = LCD_MAX_DIM_TIMEOUT;
175 void get_run_timeout(int *timeout)
177 int dim_timeout = -1;
178 int vconf_timeout = -1;
182 if (custom_normal_timeout > 0) {
183 *timeout = custom_normal_timeout;
187 ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
189 _E("Failed to get vconf value for setting timeout: %d", vconf_get_ext_errno());
190 vconf_timeout = DEFAULT_NORMAL_TIMEOUT;
193 if (force_lcdtimeout > 0)
194 on_timeout = SEC_TO_MSEC(force_lcdtimeout);
196 on_timeout = SEC_TO_MSEC(vconf_timeout);
198 if (on_timeout == 0) {
199 *timeout = on_timeout;
203 get_dim_timeout(&dim_timeout);
204 *timeout = on_timeout - dim_timeout;
207 int set_custom_lcdon_timeout(int timeout)
209 int changed = (custom_on_timeout == timeout ? false : true);
211 custom_on_timeout = timeout;
214 custom_normal_timeout = 0;
215 custom_dim_timeout = 0;
219 if (g_display_plugin.config && g_display_plugin.config->dimming)
220 custom_dim_timeout = (double)timeout * LCD_DIM_RATIO;
222 custom_dim_timeout = LCD_MIN_DIM_TIMEOUT;
224 custom_normal_timeout = timeout - custom_dim_timeout;
226 _I("custom normal(%d), dim(%d)", custom_normal_timeout,
232 static int setting_cb(keynode_t *key_nodes, void *data)
234 keynode_t *tmp = key_nodes;
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);
243 if (disp_plgn->update_pm_setting)
244 disp_plgn->update_pm_setting(index, vconf_keynode_get_int(tmp));
249 int init_setting(int (*func) (int key_idx, int val))
254 disp_plgn->update_pm_setting = func;
256 for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
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.
262 vconf_notify_key_changed(setting_keys[i], (void *)setting_cb,
263 (void *)((intptr_t)i));
266 disp_plgn->get_lock_screen_state = __get_lock_screen_state;
271 int exit_setting(void)
274 for (i = SETTING_BEGIN; i < SETTING_GET_END; i++)
275 vconf_ignore_key_changed(setting_keys[i], (void *)setting_cb);
280 static void __CONSTRUCTOR__ initialize(void)
282 disp_plgn = get_var_display_plugin();
284 _E("Failed to get display plugin variable.");