Remove trivial unnecessary build dependency
[apps/core/preloaded/lockscreen.git] / src / battery.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #include "log.h"
18 #include "battery.h"
19
20 #include <device/battery.h>
21 #include <device/callback.h>
22 #include <runtime_info.h>
23
24 #include <Ecore.h>
25
26 static bool is_connected, is_charging;
27 static int level, init_count;
28 int LOCKSCREEN_EVENT_BATTERY_CHANGED;
29
30 int _battery_status_update()
31 {
32         bool model_changed = false;
33
34         int percent, ret;
35         bool charging, connected;
36
37         ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_CHARGER_CONNECTED, &connected);
38         if (ret != RUNTIME_INFO_ERROR_NONE) {
39                 ERR("runtime_info_get_value_bool failed: %s", get_error_message(ret));
40                 return -1;
41         }
42         if (is_connected != connected) {
43                 is_connected = connected;
44                 model_changed = true;
45         }
46
47         ret = device_battery_is_charging(&charging);
48         if (ret != DEVICE_ERROR_NONE) {
49                 ERR("device_battery_is_charging failed: %s", get_error_message(ret));
50                 return -1;
51         }
52         if (is_charging != charging) {
53                 is_charging = charging;
54                 model_changed = true;
55         }
56
57         ret = device_battery_get_percent(&percent);
58         if (ret != DEVICE_ERROR_NONE) {
59                 ERR("device_battery_get_percent failed: %s", get_error_message(ret));
60                 return -1;
61         }
62         if (level != percent) {
63                 level = percent;
64                 model_changed = true;
65         }
66
67         if (model_changed)
68                 ecore_event_add(LOCKSCREEN_EVENT_BATTERY_CHANGED, NULL, NULL, NULL);
69
70         return 0;
71 }
72
73 static void _battery_changed_cb(device_callback_e type, void *value, void *user_data)
74 {
75         if (type == DEVICE_CALLBACK_BATTERY_LEVEL || type == DEVICE_CALLBACK_BATTERY_CAPACITY)
76                 _battery_status_update();
77 }
78
79 static void _battery_charger_changed_cb(runtime_info_key_e key, void *data)
80 {
81         if (key == RUNTIME_INFO_KEY_CHARGER_CONNECTED)
82                 _battery_status_update();
83 }
84
85 int lockscreen_battery_init()
86 {
87         if (!init_count) {
88                 LOCKSCREEN_EVENT_BATTERY_CHANGED = ecore_event_type_new();
89                 int ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED, _battery_charger_changed_cb, NULL);
90                 if (ret != RUNTIME_INFO_ERROR_NONE) {
91                         ERR("runtime_info_set_changed_cb failed: %s", get_error_message(ret));
92                         return -1;
93                 }
94
95                 ret = device_add_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, _battery_changed_cb, NULL);
96                 if (ret != DEVICE_ERROR_NONE) {
97                         ERR("device_add_callback failed: %s", get_error_message(ret));
98                         runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED);
99                         return -1;
100                 }
101
102                 ret = device_add_callback(DEVICE_CALLBACK_BATTERY_CHARGING, _battery_changed_cb, NULL);
103                 if (ret != DEVICE_ERROR_NONE) {
104                         ERR("device_add_callback failed: %s", get_error_message(ret));
105                         device_remove_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, _battery_changed_cb);
106                         runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED);
107                         return -1;
108                 }
109
110                 _battery_status_update();
111         }
112         init_count++;
113         return 0;
114 }
115
116 void lockscreen_battery_shutdown(void)
117 {
118         if (init_count) {
119                 init_count--;
120                 if (!init_count)
121                         runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED);
122         }
123 }
124
125 bool lockscreen_battery_is_charging(void)
126 {
127         return is_charging;
128 }
129
130 bool lockscreen_battery_is_connected(void)
131 {
132         return is_connected;
133 }
134
135 int lockscreen_battery_level_get(void)
136 {
137         return level;
138 }