4 * Copyright (c) 2020 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.
20 #include "power-doze.h"
22 #include "shared/device-notifier.h"
25 #define DOZE_DISABLED 0
26 #define DOZE_ENABLED 1
27 static int doze_mode; /* doze from setting */
30 DOZE_LEVEL_0 = 0, /* doze off mode */
34 static enum doze_level doze_level;
35 static int display_state;
36 static bool doze_initialized = false;
38 int doze_pmqos_request(int req)
42 ret = gdbus_call_async(PASS_BUS_NAME,
45 PASS_METHOD_SET_SCENARIO,
46 g_variant_new("(si)", "Doze", req));
48 _E("Failed to request pmqos: %d", ret);
53 static int change_doze_level(enum doze_level level)
57 if (level < DOZE_LEVEL_0 || level >= DOZE_LEVEL_MAX) {
58 _E("Invalid doze level %d.", level);
62 if (doze_level == level)
65 _D("Change doze level, %d -> %d.", doze_level, level);
68 ret = vconf_set_int(VCONFKEY_SYSMAN_DOZE_MODE_LEVEL, doze_level);
70 _E("Failed to set doze level, %d.", vconf_get_ext_errno());
77 static void _enter_doze(void)
79 /* already in doze mode */
80 if (doze_level >= DOZE_LEVEL_1)
83 _D("Entering doze mode.");
84 change_doze_level(DOZE_LEVEL_1);
85 doze_pmqos_request(DOZE_PMQOS_START);
88 static void _leave_doze(void)
90 /* already not in doze mode */
91 if (doze_level == DOZE_LEVEL_0)
94 _D("Leaving doze mode.");
95 change_doze_level(DOZE_LEVEL_0);
96 doze_pmqos_request(DOZE_PMQOS_STOP);
101 if (!doze_initialized)
104 if (doze_mode == DOZE_DISABLED)
110 void leave_doze(void)
112 if (!doze_initialized)
115 if (doze_mode == DOZE_DISABLED)
121 static int display_changed_cb(void *data)
123 display_state = *(int *)data;
128 static void setting_doze_mode_cb(keynode_t *key, void *data)
132 val = vconf_keynode_get_bool(key);
135 _E("Failed to get vconf value for doze, %d.", vconf_get_ext_errno());
139 if (doze_mode == val)
144 if (!doze_initialized)
147 if (doze_mode == DOZE_DISABLED) {
149 _D("Doze mode disabled.");
150 } else if (doze_mode == DOZE_ENABLED) {
151 _D("Doze mode enabled.");
152 if (display_state == DEVICED_DISPLAY_STATE_OFF || display_state == DEVICED_DISPLAY_STATE_SLEEP)
162 ret = vconf_get_bool(VCONFKEY_SETAPPL_DOZE_MODE, &doze_mode);
164 _E("Failed to get vconf value for doze, %d.", vconf_get_ext_errno());
168 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DOZE_MODE, setting_doze_mode_cb, NULL);
170 _E("Failed to register vconf notify.");
174 doze_level = DOZE_LEVEL_0;
176 syscommon_notifier_subscribe_notify(DEVICE_NOTIFIER_LCD, display_changed_cb);
178 doze_initialized = true;
179 _D("Doze mode initialized. doze_mode: %d.", doze_mode);
186 doze_initialized = false;
188 syscommon_notifier_unsubscribe_notify(DEVICE_NOTIFIER_LCD, display_changed_cb);