summaryrefslogtreecommitdiff
path: root/src/display/setting.c
blob: f45c40e820c62a3f08d22cfcb3ad50f2aab70a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * deviced
 *
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdbool.h>
#include <bundle.h>
#include <eventsystem.h>

#include "core.h"
#include "util.h"
#include "setting.h"

#define LCD_DIM_RATIO		0.3
#define LCD_MAX_DIM_TIMEOUT	7000
#define LCD_MIN_DIM_TIMEOUT	500

static const char *setting_keys[SETTING_GET_END] = {
	[SETTING_TO_NORMAL] = VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL,
	[SETTING_BRT_LEVEL] = VCONFKEY_SETAPPL_LCD_BRIGHTNESS,
	[SETTING_LOCK_SCREEN] = VCONFKEY_IDLE_LOCK_STATE,
	[SETTING_POWER_CUSTOM_BRIGHTNESS] = VCONFKEY_PM_CUSTOM_BRIGHTNESS_STATUS,
};

static int lock_screen_state = VCONFKEY_IDLE_UNLOCK;
static bool lock_screen_bg_state = false;
static int force_lcdtimeout = 0;
static int custom_on_timeout = 0;
static int custom_normal_timeout = 0;
static int custom_dim_timeout = 0;

int (*update_pm_setting) (int key_idx, int val);

static void display_state_send_system_event(int state)
{
	bundle *b;
	const char *str;

	if (state == S_NORMAL)
		str = EVT_VAL_DISPLAY_NORMAL;
	else if (state == S_LCDDIM)
		str = EVT_VAL_DISPLAY_DIM;
	else if (state == S_LCDOFF)
		str = EVT_VAL_DISPLAY_OFF;
	else
		return;

	_I("eventsystem (%s)", str);

	b = bundle_create();
	bundle_add_str(b, EVT_KEY_DISPLAY_STATE, str);
	eventsystem_send_system_event(SYS_EVENT_DISPLAY_STATE, b);
	bundle_free(b);
}

int set_force_lcdtimeout(int timeout)
{
	if (timeout < 0)
		return -EINVAL;

	force_lcdtimeout = timeout;

	return 0;
}

int get_lock_screen_state(void)
{
	return lock_screen_state;
}

void set_lock_screen_state(int state)
{
	switch (state) {
	case VCONFKEY_IDLE_LOCK:
	case VCONFKEY_IDLE_UNLOCK:
		lock_screen_state = state;
		break;
	default:
		lock_screen_state = VCONFKEY_IDLE_UNLOCK;
	}
}

int get_lock_screen_bg_state(void)
{
	return lock_screen_bg_state;
}

void set_lock_screen_bg_state(bool state)
{
	_I("state is %d", state);
	lock_screen_bg_state = state;
}

int get_charging_status(int *val)
{
	return vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, val);
}

int get_lowbatt_status(int *val)
{
	return vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, val);
}

int get_usb_status(int *val)
{
	return vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, val);
}

int set_setting_pmstate(int val)
{
	static int old = -1;

	if (old == val)
		return 0;
	old = val;

	display_state_send_system_event(val);
	return vconf_set_int(VCONFKEY_PM_STATE, val);
}

int get_setting_brightness(int *level)
{
	return vconf_get_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, level);
}

void get_dim_timeout(int *dim_timeout)
{
	int vconf_timeout, on_timeout, val, ret;

	if (custom_dim_timeout > 0) {
		*dim_timeout = custom_dim_timeout;
		return;
	}

	ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
	if (ret != 0) {
		_E("Failed ro get setting timeout!");
		vconf_timeout = DEFAULT_NORMAL_TIMEOUT;
	}

	if (force_lcdtimeout > 0)
		on_timeout = SEC_TO_MSEC(force_lcdtimeout);
	else
		on_timeout = SEC_TO_MSEC(vconf_timeout);

	val = (double)on_timeout * LCD_DIM_RATIO;
	if (val > LCD_MAX_DIM_TIMEOUT)
		val = LCD_MAX_DIM_TIMEOUT;

	*dim_timeout = val;
}

void get_run_timeout(int *timeout)
{
	int dim_timeout = -1;
	int vconf_timeout = -1;
	int on_timeout;
	int ret;

	if (custom_normal_timeout > 0) {
		*timeout = custom_normal_timeout;
		return;
	}

	ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
	if (ret != 0) {
		_E("Failed ro get setting timeout!");
		vconf_timeout = DEFAULT_NORMAL_TIMEOUT;
	}

	if (force_lcdtimeout > 0)
		on_timeout = SEC_TO_MSEC(force_lcdtimeout);
	else
		on_timeout = SEC_TO_MSEC(vconf_timeout);

	if (on_timeout == 0) {
		*timeout = on_timeout;
		return;
	}

	get_dim_timeout(&dim_timeout);
	*timeout = on_timeout - dim_timeout;
}

int set_custom_lcdon_timeout(int timeout)
{
	int changed = (custom_on_timeout == timeout ? false : true);

	custom_on_timeout = timeout;

	if (timeout <= 0) {
		custom_normal_timeout = 0;
		custom_dim_timeout = 0;
		return changed;
	}

	custom_dim_timeout = (double)timeout * LCD_DIM_RATIO;
	custom_normal_timeout = timeout - custom_dim_timeout;

	_I("custom normal(%d), dim(%d)", custom_normal_timeout,
	    custom_dim_timeout);

	return changed;
}

static int setting_cb(keynode_t *key_nodes, void *data)
{
	keynode_t *tmp = key_nodes;
	int index;

	index = (int)((intptr_t)data);
	if (index > SETTING_END) {
		_E("Unknown setting key: %s, idx=%d",
		       vconf_keynode_get_name(tmp), index);
		return -1;
	}
	if (update_pm_setting != NULL)
		update_pm_setting(index, vconf_keynode_get_int(tmp));

	return 0;
}

int init_setting(int (*func) (int key_idx, int val))
{
	int i;

	if (func != NULL)
		update_pm_setting = func;

	for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
		/*
		 * To pass an index data through the vconf infratstructure
		 * without memory allocation, an index data becomes typecast
		 * to proper pointer size on each architecture.
		 */
		vconf_notify_key_changed(setting_keys[i], (void *)setting_cb,
					 (void *)((intptr_t)i));
	}

	return 0;
}

int exit_setting(void)
{
	int i;
	for (i = SETTING_BEGIN; i < SETTING_GET_END; i++)
		vconf_ignore_key_changed(setting_keys[i], (void *)setting_cb);

	return 0;
}