apply FSL(Flora Software License)
[framework/system/power-manager.git] / pm_setting.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "pm_setting.h"
21 #include "pm_conf.h"
22 #include "util.h"
23
24 static const char *setting_keys[SETTING_GET_END] = {
25         [SETTING_TO_NORMAL] = VCONFKEY_SYSMAN_LCD_TIMEOUT_NORMAL,
26         [SETTING_LOW_BATT] = VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
27         [SETTING_CHARGING] = VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
28         [SETTING_BRT_LEVEL] = VCONFKEY_SETAPPL_LCD_BRIGHTNESS,
29         [SETTING_LOCK_SCREEN] = VCONFKEY_IDLE_LOCK_STATE,
30 };
31
32 static int (*update_setting) (int key_idx, int val);
33
34 int get_charging_status(int *val)
35 {
36         return vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, val);
37 }
38
39 int get_lowbatt_status(int *val)
40 {
41         return vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, val);
42 }
43
44 int get_usb_status(int *val)
45 {
46         return vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, val);
47 }
48
49 int set_setting_pmstate(int val)
50 {
51         return vconf_set_int(VCONFKEY_PM_STATE, val);
52 }
53
54 int get_setting_brightness(int *level)
55 {
56         return vconf_get_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, level);
57 }
58
59 int get_run_timeout(int *timeout)
60 {
61         int dim_timeout = -1, vconf_timeout = -1, ret;
62         get_dim_timeout(&dim_timeout);
63
64         if(dim_timeout < 0) {
65                 LOGERR("Can not get dim timeout. set default 5 seconds");
66                 dim_timeout = 5;
67         }
68
69         ret = vconf_get_int(setting_keys[SETTING_TO_NORMAL], &vconf_timeout);
70         *timeout = vconf_timeout - dim_timeout;
71         return ret;
72
73 }
74
75 int get_dim_timeout(int *timeout)
76 {
77         char buf[255];
78         /* TODO if needed */
79         *timeout = 5;           /* default timeout */
80         get_env("PM_TO_LCDDIM", buf, sizeof(buf));
81         LOGDBG("Get lcddim timeout [%s]", buf);
82         *timeout = atoi(buf);
83         return 0;
84 }
85
86 int get_off_timeout(int *timeout)
87 {
88         char buf[255];
89         /* TODO if needed */
90         *timeout = 5;           /* default timeout */
91         get_env("PM_TO_LCDOFF", buf, sizeof(buf));
92         LOGDBG("Get lcdoff timeout [%s]", buf);
93         *timeout = atoi(buf);
94         return 0;
95 }
96
97 static int setting_cb(keynode_t *key_nodes, void *data)
98 {
99         keynode_t *tmp = key_nodes;
100
101         if ((int)data > SETTING_END) {
102                 LOGERR("Unknown setting key: %s, idx= %d",
103                        vconf_keynode_get_name, (int)data);
104                 return -1;
105         }
106         if (update_setting != NULL) {
107                 update_setting((int)data, vconf_keynode_get_int(tmp));
108         }
109
110         return 0;
111 }
112
113 int init_setting(int (*func) (int key_idx, int val))
114 {
115         int i;
116
117         if (func != NULL)
118                 update_setting = func;
119
120         for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
121                 vconf_notify_key_changed(setting_keys[i], (void *)setting_cb,
122                                          (void *)i);
123         }
124
125         return 0;
126 }
127
128 int exit_setting()
129 {
130         int i;
131         for (i = SETTING_BEGIN; i < SETTING_GET_END; i++) {
132                 vconf_ignore_key_changed(setting_keys[i], (void *)setting_cb);
133         }
134
135         return 0;
136 }