display: Move force_brightness getter and setter from plugin to core display module
[platform/core/system/deviced.git] / plugins / wearable / display / powersaver.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2014 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 <vconf.h>
21
22 #include "shared/common.h"
23 #include "shared/devices.h"
24 #include "shared/device-notifier.h"
25 #include "core/log.h"
26 #include "display/core.h"
27 #include "display/device-interface.h"
28 #include "display/display-backlight.h"
29 #include "display/display-ops.h"
30 #include "display-info.h"
31 #include "powersaver.h"
32 #include "setting.h"
33
34 static struct _backlight_ops *backlight_ops;
35
36 static int set_powersaver_mode(int mode)
37 {
38         int timeout;
39         const struct display_ops *hbm_ops = NULL;
40         struct hbmsetstate hss;
41         bool pmqos = false;
42
43         if (mode == POWERSAVER_ENHANCED)
44                 pmqos = true;
45         _D("Powersaver mode %d pmqos %d.", mode, pmqos);
46         device_notify(DEVICE_NOTIFIER_ULTRAPOWERSAVING, (void *)&pmqos);
47
48         display_backlight_set_force_brightness(0);
49         set_force_lcdtimeout(0);
50
51         FIND_DISPLAY(hbm_ops, "hbm");
52         if (hbm_ops && hbm_ops->func(HBM_GET_STATE, 0)) {
53                 hss.hbm = false;
54                 hss.timeout = 0;
55                 hbm_ops->func(HBM_SET_TIMEOUT_STATE, &hss);
56         }
57
58         display_backlight_update_by_default_brightness();
59         get_run_timeout(&timeout);
60         state_st(S_NORMAL)->timeout = timeout;
61         state_st(get_pm_cur_state())->trans(EVENT_INPUT);
62
63         return 0;
64 }
65
66 static void powersaver_status_changed(keynode_t *key_nodes, void *data)
67 {
68         int status, mode, ret;
69
70         if (key_nodes == NULL) {
71                 _E("Wrong parameter, key_nodes is null.");
72                 return;
73         }
74
75         status = vconf_keynode_get_int(key_nodes);
76         if (status == SETTING_PSMODE_NORMAL)
77                 mode = POWERSAVER_OFF;
78         else if (status == SETTING_PSMODE_WEARABLE)
79                 mode = POWERSAVER_BASIC;
80         else if (status == SETTING_PSMODE_WEARABLE_ENHANCED)
81                 mode = POWERSAVER_ENHANCED;
82         else
83                 return;
84
85         ret = set_powersaver_mode(mode);
86         if (ret < 0)
87                 _E("Failed to update powersaver state %d.", ret);
88 }
89
90 static int delayed_init_done(void *data)
91 {
92         static int done;
93         int ret, status;
94         int mode = POWERSAVER_OFF;
95         bool pmqos = false;
96
97         if (data == NULL)
98                 goto out;
99         done = *(int *)data;
100         if (!done)
101                 goto out;
102         _I("Booting done.");
103
104
105         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
106             powersaver_status_changed, NULL);
107         if (ret != 0)
108                 _E("Failed to vconf_notify_key_changed.");
109
110         ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &status);
111         if (ret != 0) {
112                 _E("Failed to vconf get bool.");
113                 goto out;
114         }
115
116         switch (status) {
117         case SETTING_PSMODE_WEARABLE:
118                 mode = POWERSAVER_BASIC;
119                 break;
120         case SETTING_PSMODE_WEARABLE_ENHANCED:
121                 mode = POWERSAVER_ENHANCED;
122                 pmqos = true;
123                 break;
124         default:
125                 goto out;
126         }
127
128         _D("Powersaver mode on, %d.", mode);
129         device_notify(DEVICE_NOTIFIER_PMQOS_ULTRAPOWERSAVING, (void *)&pmqos);
130         ret = set_powersaver_mode(mode);
131         if (ret < 0)
132                 _E("Failed to update powersaver state, %d.", ret);
133 out:
134         return done;
135 }
136
137 static void powersaver_init(void *data)
138 {
139         register_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
140 }
141
142 static void powersaver_exit(void *data)
143 {
144         int ret;
145
146         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
147             powersaver_status_changed);
148         if (ret != 0)
149                 _E("Failed to vconf_ignore_key_changed.");
150 }
151
152 static const struct device_ops powersaver_device_ops = {
153         DECLARE_NAME_LEN("powersaver"),
154         .init     = powersaver_init,
155         .exit     = powersaver_exit,
156 };
157
158 DEVICE_OPS_REGISTER(&powersaver_device_ops)
159
160 static void __CONSTRUCTOR__ initialize(void)
161 {
162         backlight_ops = get_var_backlight_ops();
163         if (!backlight_ops)
164                 _E("Failed to get backlight operator variable.");
165 }