Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 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 | //#define DEBUG_UEVENTS |
| 18 | #define CHARGER_KLOG_LEVEL 6 |
| 19 | |
| 20 | #include <dirent.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <linux/input.h> |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 24 | #include <stdbool.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/poll.h> |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/un.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | |
Elliott Hughes | b77d3d7 | 2012-09-11 18:52:40 -0700 | [diff] [blame] | 35 | #include <sys/socket.h> |
| 36 | #include <linux/netlink.h> |
| 37 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 38 | #include <cutils/android_reboot.h> |
| 39 | #include <cutils/klog.h> |
| 40 | #include <cutils/list.h> |
Dima Zavin | 823ebc4 | 2011-09-29 17:20:07 -0700 | [diff] [blame] | 41 | #include <cutils/misc.h> |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 42 | #include <cutils/uevent.h> |
| 43 | |
Iliyan Malchev | 40156b8 | 2012-12-06 17:06:36 -0800 | [diff] [blame] | 44 | #ifdef CHARGER_ENABLE_SUSPEND |
choongryeol.lee | 9255713 | 2012-11-15 17:03:03 -0800 | [diff] [blame] | 45 | #include <suspend/autosuspend.h> |
Iliyan Malchev | 40156b8 | 2012-12-06 17:06:36 -0800 | [diff] [blame] | 46 | #endif |
choongryeol.lee | 9255713 | 2012-11-15 17:03:03 -0800 | [diff] [blame] | 47 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 48 | #include "minui/minui.h" |
| 49 | |
Colin Cross | ca0e504 | 2014-02-13 12:25:21 -0800 | [diff] [blame] | 50 | char *locale; |
| 51 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 52 | #ifndef max |
| 53 | #define max(a,b) ((a) > (b) ? (a) : (b)) |
| 54 | #endif |
| 55 | |
| 56 | #ifndef min |
| 57 | #define min(a,b) ((a) < (b) ? (a) : (b)) |
| 58 | #endif |
| 59 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 60 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
| 61 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 62 | #define MSEC_PER_SEC (1000LL) |
| 63 | #define NSEC_PER_MSEC (1000000LL) |
| 64 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 65 | #define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC) |
Dima Zavin | 92312a5 | 2011-09-16 13:15:47 -0700 | [diff] [blame] | 66 | #define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 67 | #define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC) |
| 68 | |
Dima Zavin | 1a5ca61 | 2011-09-26 14:14:19 -0700 | [diff] [blame] | 69 | #define BATTERY_FULL_THRESH 95 |
| 70 | |
Dima Zavin | 823ebc4 | 2011-09-29 17:20:07 -0700 | [diff] [blame] | 71 | #define LAST_KMSG_PATH "/proc/last_kmsg" |
| 72 | #define LAST_KMSG_MAX_SZ (32 * 1024) |
| 73 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 74 | #define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0) |
| 75 | #define LOGI(x...) do { KLOG_INFO("charger", x); } while (0) |
| 76 | #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) |
| 77 | |
| 78 | struct key_state { |
| 79 | bool pending; |
| 80 | bool down; |
| 81 | int64_t timestamp; |
| 82 | }; |
| 83 | |
| 84 | struct power_supply { |
| 85 | struct listnode list; |
| 86 | char name[256]; |
| 87 | char type[32]; |
| 88 | bool online; |
| 89 | bool valid; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 90 | char cap_path[PATH_MAX]; |
| 91 | }; |
| 92 | |
| 93 | struct frame { |
| 94 | const char *name; |
| 95 | int disp_time; |
| 96 | int min_capacity; |
Dima Zavin | 9ec3f3e | 2011-10-31 16:53:41 -0700 | [diff] [blame] | 97 | bool level_only; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 98 | |
| 99 | gr_surface surface; |
| 100 | }; |
| 101 | |
| 102 | struct animation { |
| 103 | bool run; |
| 104 | |
| 105 | struct frame *frames; |
| 106 | int cur_frame; |
| 107 | int num_frames; |
| 108 | |
| 109 | int cur_cycle; |
| 110 | int num_cycles; |
| 111 | |
| 112 | /* current capacity being animated */ |
| 113 | int capacity; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | struct charger { |
| 117 | int64_t next_screen_transition; |
| 118 | int64_t next_key_check; |
| 119 | int64_t next_pwr_check; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 120 | |
| 121 | struct key_state keys[KEY_MAX + 1]; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 122 | int uevent_fd; |
| 123 | |
| 124 | struct listnode supplies; |
| 125 | int num_supplies; |
| 126 | int num_supplies_online; |
| 127 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 128 | struct animation *batt_anim; |
| 129 | gr_surface surf_unknown; |
| 130 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 131 | struct power_supply *battery; |
| 132 | }; |
| 133 | |
| 134 | struct uevent { |
| 135 | const char *action; |
| 136 | const char *path; |
| 137 | const char *subsystem; |
| 138 | const char *ps_name; |
| 139 | const char *ps_type; |
| 140 | const char *ps_online; |
| 141 | }; |
| 142 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 143 | static struct frame batt_anim_frames[] = { |
| 144 | { |
| 145 | .name = "charger/battery_0", |
| 146 | .disp_time = 750, |
| 147 | .min_capacity = 0, |
| 148 | }, |
| 149 | { |
| 150 | .name = "charger/battery_1", |
| 151 | .disp_time = 750, |
| 152 | .min_capacity = 20, |
| 153 | }, |
| 154 | { |
| 155 | .name = "charger/battery_2", |
| 156 | .disp_time = 750, |
| 157 | .min_capacity = 40, |
| 158 | }, |
| 159 | { |
| 160 | .name = "charger/battery_3", |
| 161 | .disp_time = 750, |
| 162 | .min_capacity = 60, |
| 163 | }, |
| 164 | { |
| 165 | .name = "charger/battery_4", |
| 166 | .disp_time = 750, |
| 167 | .min_capacity = 80, |
Dima Zavin | 9ec3f3e | 2011-10-31 16:53:41 -0700 | [diff] [blame] | 168 | .level_only = true, |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 169 | }, |
Dima Zavin | 1a5ca61 | 2011-09-26 14:14:19 -0700 | [diff] [blame] | 170 | { |
| 171 | .name = "charger/battery_5", |
| 172 | .disp_time = 750, |
| 173 | .min_capacity = BATTERY_FULL_THRESH, |
| 174 | }, |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static struct animation battery_animation = { |
| 178 | .frames = batt_anim_frames, |
| 179 | .num_frames = ARRAY_SIZE(batt_anim_frames), |
| 180 | .num_cycles = 3, |
| 181 | }; |
| 182 | |
| 183 | static struct charger charger_state = { |
| 184 | .batt_anim = &battery_animation, |
| 185 | }; |
| 186 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 187 | static int char_width; |
| 188 | static int char_height; |
| 189 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 190 | /* current time in milliseconds */ |
| 191 | static int64_t curr_time_ms(void) |
| 192 | { |
| 193 | struct timespec tm; |
| 194 | clock_gettime(CLOCK_MONOTONIC, &tm); |
| 195 | return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC); |
| 196 | } |
| 197 | |
| 198 | static void clear_screen(void) |
| 199 | { |
| 200 | gr_color(0, 0, 0, 255); |
| 201 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 202 | }; |
| 203 | |
Dima Zavin | 823ebc4 | 2011-09-29 17:20:07 -0700 | [diff] [blame] | 204 | #define MAX_KLOG_WRITE_BUF_SZ 256 |
| 205 | |
| 206 | static void dump_last_kmsg(void) |
| 207 | { |
| 208 | char *buf; |
| 209 | char *ptr; |
| 210 | unsigned sz = 0; |
| 211 | int len; |
| 212 | |
| 213 | LOGI("\n"); |
| 214 | LOGI("*************** LAST KMSG ***************\n"); |
| 215 | LOGI("\n"); |
| 216 | buf = load_file(LAST_KMSG_PATH, &sz); |
| 217 | if (!buf || !sz) { |
| 218 | LOGI("last_kmsg not found. Cold reset?\n"); |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | len = min(sz, LAST_KMSG_MAX_SZ); |
| 223 | ptr = buf + (sz - len); |
| 224 | |
| 225 | while (len > 0) { |
| 226 | int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ); |
| 227 | char yoink; |
| 228 | char *nl; |
| 229 | |
| 230 | nl = memrchr(ptr, '\n', cnt - 1); |
| 231 | if (nl) |
| 232 | cnt = nl - ptr + 1; |
| 233 | |
| 234 | yoink = ptr[cnt]; |
| 235 | ptr[cnt] = '\0'; |
Dima Zavin | d11e1a0 | 2011-10-12 16:13:56 -0700 | [diff] [blame] | 236 | klog_write(6, "<6>%s", ptr); |
Dima Zavin | 823ebc4 | 2011-09-29 17:20:07 -0700 | [diff] [blame] | 237 | ptr[cnt] = yoink; |
| 238 | |
| 239 | len -= cnt; |
| 240 | ptr += cnt; |
| 241 | } |
| 242 | |
| 243 | free(buf); |
| 244 | |
| 245 | out: |
| 246 | LOGI("\n"); |
| 247 | LOGI("************* END LAST KMSG *************\n"); |
| 248 | LOGI("\n"); |
| 249 | } |
| 250 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 251 | static int read_file(const char *path, char *buf, size_t sz) |
| 252 | { |
| 253 | int fd; |
| 254 | size_t cnt; |
| 255 | |
| 256 | fd = open(path, O_RDONLY, 0); |
| 257 | if (fd < 0) |
| 258 | goto err; |
| 259 | |
| 260 | cnt = read(fd, buf, sz - 1); |
| 261 | if (cnt <= 0) |
| 262 | goto err; |
| 263 | buf[cnt] = '\0'; |
| 264 | if (buf[cnt - 1] == '\n') { |
| 265 | cnt--; |
| 266 | buf[cnt] = '\0'; |
| 267 | } |
| 268 | |
| 269 | close(fd); |
| 270 | return cnt; |
| 271 | |
| 272 | err: |
| 273 | if (fd >= 0) |
| 274 | close(fd); |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | static int read_file_int(const char *path, int *val) |
| 279 | { |
| 280 | char buf[32]; |
| 281 | int ret; |
| 282 | int tmp; |
| 283 | char *end; |
| 284 | |
| 285 | ret = read_file(path, buf, sizeof(buf)); |
| 286 | if (ret < 0) |
| 287 | return -1; |
| 288 | |
| 289 | tmp = strtol(buf, &end, 0); |
| 290 | if (end == buf || |
| 291 | ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0'))) |
| 292 | goto err; |
| 293 | |
| 294 | *val = tmp; |
| 295 | return 0; |
| 296 | |
| 297 | err: |
| 298 | return -1; |
| 299 | } |
| 300 | |
Dima Zavin | 1a5ca61 | 2011-09-26 14:14:19 -0700 | [diff] [blame] | 301 | static int get_battery_capacity(struct charger *charger) |
| 302 | { |
| 303 | int ret; |
| 304 | int batt_cap = -1; |
| 305 | |
| 306 | if (!charger->battery) |
| 307 | return -1; |
| 308 | |
| 309 | ret = read_file_int(charger->battery->cap_path, &batt_cap); |
| 310 | if (ret < 0 || batt_cap > 100) { |
| 311 | batt_cap = -1; |
| 312 | } |
| 313 | |
| 314 | return batt_cap; |
| 315 | } |
| 316 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 317 | static struct power_supply *find_supply(struct charger *charger, |
| 318 | const char *name) |
| 319 | { |
| 320 | struct listnode *node; |
| 321 | struct power_supply *supply; |
| 322 | |
| 323 | list_for_each(node, &charger->supplies) { |
| 324 | supply = node_to_item(node, struct power_supply, list); |
| 325 | if (!strncmp(name, supply->name, sizeof(supply->name))) |
| 326 | return supply; |
| 327 | } |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | static struct power_supply *add_supply(struct charger *charger, |
| 332 | const char *name, const char *type, |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 333 | const char *path, bool online) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 334 | { |
| 335 | struct power_supply *supply; |
| 336 | |
| 337 | supply = calloc(1, sizeof(struct power_supply)); |
| 338 | if (!supply) |
| 339 | return NULL; |
| 340 | |
| 341 | strlcpy(supply->name, name, sizeof(supply->name)); |
| 342 | strlcpy(supply->type, type, sizeof(supply->type)); |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 343 | snprintf(supply->cap_path, sizeof(supply->cap_path), |
| 344 | "/sys/%s/capacity", path); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 345 | supply->online = online; |
| 346 | list_add_tail(&charger->supplies, &supply->list); |
| 347 | charger->num_supplies++; |
| 348 | LOGV("... added %s %s %d\n", supply->name, supply->type, online); |
| 349 | return supply; |
| 350 | } |
| 351 | |
| 352 | static void remove_supply(struct charger *charger, struct power_supply *supply) |
| 353 | { |
| 354 | if (!supply) |
| 355 | return; |
| 356 | list_remove(&supply->list); |
| 357 | charger->num_supplies--; |
| 358 | free(supply); |
| 359 | } |
| 360 | |
choongryeol.lee | 9255713 | 2012-11-15 17:03:03 -0800 | [diff] [blame] | 361 | #ifdef CHARGER_ENABLE_SUSPEND |
| 362 | static int request_suspend(bool enable) |
| 363 | { |
| 364 | if (enable) |
| 365 | return autosuspend_enable(); |
| 366 | else |
| 367 | return autosuspend_disable(); |
| 368 | } |
| 369 | #else |
| 370 | static int request_suspend(bool enable) |
| 371 | { |
| 372 | return 0; |
| 373 | } |
| 374 | #endif |
| 375 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 376 | static void parse_uevent(const char *msg, struct uevent *uevent) |
| 377 | { |
| 378 | uevent->action = ""; |
| 379 | uevent->path = ""; |
| 380 | uevent->subsystem = ""; |
| 381 | uevent->ps_name = ""; |
| 382 | uevent->ps_online = ""; |
| 383 | uevent->ps_type = ""; |
| 384 | |
| 385 | /* currently ignoring SEQNUM */ |
| 386 | while (*msg) { |
| 387 | #ifdef DEBUG_UEVENTS |
| 388 | LOGV("uevent str: %s\n", msg); |
| 389 | #endif |
| 390 | if (!strncmp(msg, "ACTION=", 7)) { |
| 391 | msg += 7; |
| 392 | uevent->action = msg; |
| 393 | } else if (!strncmp(msg, "DEVPATH=", 8)) { |
| 394 | msg += 8; |
| 395 | uevent->path = msg; |
| 396 | } else if (!strncmp(msg, "SUBSYSTEM=", 10)) { |
| 397 | msg += 10; |
| 398 | uevent->subsystem = msg; |
| 399 | } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) { |
| 400 | msg += 18; |
| 401 | uevent->ps_name = msg; |
| 402 | } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) { |
| 403 | msg += 20; |
| 404 | uevent->ps_online = msg; |
| 405 | } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) { |
| 406 | msg += 18; |
| 407 | uevent->ps_type = msg; |
| 408 | } |
| 409 | |
| 410 | /* advance to after the next \0 */ |
| 411 | while (*msg++) |
| 412 | ; |
| 413 | } |
| 414 | |
| 415 | LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n", |
| 416 | uevent->action, uevent->path, uevent->subsystem, |
| 417 | uevent->ps_name, uevent->ps_type, uevent->ps_online); |
| 418 | } |
| 419 | |
| 420 | static void process_ps_uevent(struct charger *charger, struct uevent *uevent) |
| 421 | { |
| 422 | int online; |
| 423 | char ps_type[32]; |
| 424 | struct power_supply *supply = NULL; |
| 425 | int i; |
| 426 | bool was_online = false; |
| 427 | bool battery = false; |
| 428 | |
| 429 | if (uevent->ps_type[0] == '\0') { |
| 430 | char *path; |
| 431 | int ret; |
| 432 | |
| 433 | if (uevent->path[0] == '\0') |
| 434 | return; |
| 435 | ret = asprintf(&path, "/sys/%s/type", uevent->path); |
| 436 | if (ret <= 0) |
| 437 | return; |
| 438 | ret = read_file(path, ps_type, sizeof(ps_type)); |
| 439 | free(path); |
| 440 | if (ret < 0) |
| 441 | return; |
| 442 | } else { |
| 443 | strlcpy(ps_type, uevent->ps_type, sizeof(ps_type)); |
| 444 | } |
| 445 | |
| 446 | if (!strncmp(ps_type, "Battery", 7)) |
| 447 | battery = true; |
| 448 | |
| 449 | online = atoi(uevent->ps_online); |
| 450 | supply = find_supply(charger, uevent->ps_name); |
| 451 | if (supply) { |
| 452 | was_online = supply->online; |
| 453 | supply->online = online; |
| 454 | } |
| 455 | |
| 456 | if (!strcmp(uevent->action, "add")) { |
| 457 | if (!supply) { |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 458 | supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path, |
| 459 | online); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 460 | if (!supply) { |
| 461 | LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name, |
| 462 | uevent->ps_type, online); |
| 463 | return; |
| 464 | } |
| 465 | /* only pick up the first battery for now */ |
| 466 | if (battery && !charger->battery) |
| 467 | charger->battery = supply; |
| 468 | } else { |
| 469 | LOGE("supply '%s' already exists..\n", uevent->ps_name); |
| 470 | } |
| 471 | } else if (!strcmp(uevent->action, "remove")) { |
| 472 | if (supply) { |
| 473 | if (charger->battery == supply) |
| 474 | charger->battery = NULL; |
| 475 | remove_supply(charger, supply); |
| 476 | supply = NULL; |
| 477 | } |
| 478 | } else if (!strcmp(uevent->action, "change")) { |
| 479 | if (!supply) { |
| 480 | LOGE("power supply '%s' not found ('%s' %d)\n", |
| 481 | uevent->ps_name, ps_type, online); |
| 482 | return; |
| 483 | } |
| 484 | } else { |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | /* allow battery to be managed in the supply list but make it not |
| 489 | * contribute to online power supplies. */ |
| 490 | if (!battery) { |
| 491 | if (was_online && !online) |
| 492 | charger->num_supplies_online--; |
| 493 | else if (supply && !was_online && online) |
| 494 | charger->num_supplies_online++; |
| 495 | } |
| 496 | |
| 497 | LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n", |
| 498 | uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline", |
| 499 | uevent->action, charger->num_supplies_online, charger->num_supplies); |
| 500 | } |
| 501 | |
| 502 | static void process_uevent(struct charger *charger, struct uevent *uevent) |
| 503 | { |
| 504 | if (!strcmp(uevent->subsystem, "power_supply")) |
| 505 | process_ps_uevent(charger, uevent); |
| 506 | } |
| 507 | |
| 508 | #define UEVENT_MSG_LEN 1024 |
| 509 | static int handle_uevent_fd(struct charger *charger, int fd) |
| 510 | { |
| 511 | char msg[UEVENT_MSG_LEN+2]; |
| 512 | int n; |
| 513 | |
| 514 | if (fd < 0) |
| 515 | return -1; |
| 516 | |
| 517 | while (true) { |
| 518 | struct uevent uevent; |
| 519 | |
| 520 | n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN); |
| 521 | if (n <= 0) |
| 522 | break; |
| 523 | if (n >= UEVENT_MSG_LEN) /* overflow -- discard */ |
| 524 | continue; |
| 525 | |
| 526 | msg[n] = '\0'; |
| 527 | msg[n+1] = '\0'; |
| 528 | |
| 529 | parse_uevent(msg, &uevent); |
| 530 | process_uevent(charger, &uevent); |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static int uevent_callback(int fd, short revents, void *data) |
| 537 | { |
| 538 | struct charger *charger = data; |
| 539 | |
| 540 | if (!(revents & POLLIN)) |
| 541 | return -1; |
| 542 | return handle_uevent_fd(charger, fd); |
| 543 | } |
| 544 | |
| 545 | /* force the kernel to regenerate the change events for the existing |
| 546 | * devices, if valid */ |
| 547 | static void do_coldboot(struct charger *charger, DIR *d, const char *event, |
| 548 | bool follow_links, int max_depth) |
| 549 | { |
| 550 | struct dirent *de; |
| 551 | int dfd, fd; |
| 552 | |
| 553 | dfd = dirfd(d); |
| 554 | |
| 555 | fd = openat(dfd, "uevent", O_WRONLY); |
| 556 | if (fd >= 0) { |
| 557 | write(fd, event, strlen(event)); |
| 558 | close(fd); |
| 559 | handle_uevent_fd(charger, charger->uevent_fd); |
| 560 | } |
| 561 | |
| 562 | while ((de = readdir(d)) && max_depth > 0) { |
| 563 | DIR *d2; |
| 564 | |
| 565 | LOGV("looking at '%s'\n", de->d_name); |
| 566 | |
| 567 | if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) || |
| 568 | de->d_name[0] == '.') { |
| 569 | LOGV("skipping '%s' type %d (depth=%d follow=%d)\n", |
| 570 | de->d_name, de->d_type, max_depth, follow_links); |
| 571 | continue; |
| 572 | } |
| 573 | LOGV("can descend into '%s'\n", de->d_name); |
| 574 | |
| 575 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 576 | if (fd < 0) { |
| 577 | LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name, |
| 578 | errno, strerror(errno)); |
| 579 | continue; |
| 580 | } |
| 581 | |
| 582 | d2 = fdopendir(fd); |
| 583 | if (d2 == 0) |
| 584 | close(fd); |
| 585 | else { |
| 586 | LOGV("opened '%s'\n", de->d_name); |
| 587 | do_coldboot(charger, d2, event, follow_links, max_depth - 1); |
| 588 | closedir(d2); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | static void coldboot(struct charger *charger, const char *path, |
| 594 | const char *event) |
| 595 | { |
| 596 | char str[256]; |
| 597 | |
| 598 | LOGV("doing coldboot '%s' in '%s'\n", event, path); |
| 599 | DIR *d = opendir(path); |
| 600 | if (d) { |
| 601 | snprintf(str, sizeof(str), "%s\n", event); |
| 602 | do_coldboot(charger, d, str, true, 1); |
| 603 | closedir(d); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | static int draw_text(const char *str, int x, int y) |
| 608 | { |
| 609 | int str_len_px = gr_measure(str); |
| 610 | |
| 611 | if (x < 0) |
| 612 | x = (gr_fb_width() - str_len_px) / 2; |
| 613 | if (y < 0) |
| 614 | y = (gr_fb_height() - char_height) / 2; |
Doug Zongker | 12c45fb | 2013-03-07 13:35:23 -0800 | [diff] [blame] | 615 | gr_text(x, y, str, 0); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 616 | |
| 617 | return y + char_height; |
| 618 | } |
| 619 | |
| 620 | static void android_green(void) |
| 621 | { |
| 622 | gr_color(0xa4, 0xc6, 0x39, 255); |
| 623 | } |
| 624 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 625 | /* returns the last y-offset of where the surface ends */ |
| 626 | static int draw_surface_centered(struct charger *charger, gr_surface surface) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 627 | { |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 628 | int w; |
| 629 | int h; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 630 | int x; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 631 | int y; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 632 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 633 | w = gr_get_width(surface); |
| 634 | h = gr_get_height(surface); |
| 635 | x = (gr_fb_width() - w) / 2 ; |
| 636 | y = (gr_fb_height() - h) / 2 ; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 637 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 638 | LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y); |
| 639 | gr_blit(surface, 0, 0, w, h, x, y); |
| 640 | return y + h; |
| 641 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 642 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 643 | static void draw_unknown(struct charger *charger) |
| 644 | { |
| 645 | int y; |
| 646 | if (charger->surf_unknown) { |
| 647 | draw_surface_centered(charger, charger->surf_unknown); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 648 | } else { |
| 649 | android_green(); |
| 650 | y = draw_text("Charging!", -1, -1); |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 651 | draw_text("?\?/100", -1, y + 25); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 652 | } |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 653 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 654 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 655 | static void draw_battery(struct charger *charger) |
| 656 | { |
| 657 | struct animation *batt_anim = charger->batt_anim; |
| 658 | struct frame *frame = &batt_anim->frames[batt_anim->cur_frame]; |
| 659 | |
| 660 | if (batt_anim->num_frames != 0) { |
| 661 | draw_surface_centered(charger, frame->surface); |
| 662 | LOGV("drawing frame #%d name=%s min_cap=%d time=%d\n", |
| 663 | batt_anim->cur_frame, frame->name, frame->min_capacity, |
| 664 | frame->disp_time); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 665 | } |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 666 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 667 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 668 | static void redraw_screen(struct charger *charger) |
| 669 | { |
| 670 | struct animation *batt_anim = charger->batt_anim; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 671 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 672 | clear_screen(); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 673 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 674 | /* try to display *something* */ |
| 675 | if (batt_anim->capacity < 0 || batt_anim->num_frames == 0) |
| 676 | draw_unknown(charger); |
| 677 | else |
| 678 | draw_battery(charger); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 679 | gr_flip(); |
| 680 | } |
| 681 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 682 | static void kick_animation(struct animation *anim) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 683 | { |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 684 | anim->run = true; |
| 685 | } |
| 686 | |
| 687 | static void reset_animation(struct animation *anim) |
| 688 | { |
| 689 | anim->cur_cycle = 0; |
| 690 | anim->cur_frame = 0; |
| 691 | anim->run = false; |
| 692 | } |
| 693 | |
| 694 | static void update_screen_state(struct charger *charger, int64_t now) |
| 695 | { |
| 696 | struct animation *batt_anim = charger->batt_anim; |
| 697 | int cur_frame; |
| 698 | int disp_time; |
| 699 | |
| 700 | if (!batt_anim->run || now < charger->next_screen_transition) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 701 | return; |
| 702 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 703 | /* animation is over, blank screen and leave */ |
| 704 | if (batt_anim->cur_cycle == batt_anim->num_cycles) { |
| 705 | reset_animation(batt_anim); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 706 | charger->next_screen_transition = -1; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 707 | gr_fb_blank(true); |
| 708 | LOGV("[%lld] animation done\n", now); |
Devin Kim | fd8e650 | 2012-12-07 09:25:06 -0800 | [diff] [blame] | 709 | if (charger->num_supplies_online > 0) |
| 710 | request_suspend(true); |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 711 | return; |
| 712 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 713 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 714 | disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time; |
| 715 | |
| 716 | /* animation starting, set up the animation */ |
| 717 | if (batt_anim->cur_frame == 0) { |
| 718 | int batt_cap; |
| 719 | int ret; |
| 720 | |
| 721 | LOGV("[%lld] animation starting\n", now); |
Dima Zavin | 1a5ca61 | 2011-09-26 14:14:19 -0700 | [diff] [blame] | 722 | batt_cap = get_battery_capacity(charger); |
| 723 | if (batt_cap >= 0 && batt_anim->num_frames != 0) { |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 724 | int i; |
| 725 | |
| 726 | /* find first frame given current capacity */ |
| 727 | for (i = 1; i < batt_anim->num_frames; i++) { |
| 728 | if (batt_cap < batt_anim->frames[i].min_capacity) |
| 729 | break; |
| 730 | } |
| 731 | batt_anim->cur_frame = i - 1; |
| 732 | |
| 733 | /* show the first frame for twice as long */ |
| 734 | disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2; |
| 735 | } |
| 736 | |
| 737 | batt_anim->capacity = batt_cap; |
| 738 | } |
| 739 | |
| 740 | /* unblank the screen on first cycle */ |
| 741 | if (batt_anim->cur_cycle == 0) |
| 742 | gr_fb_blank(false); |
| 743 | |
| 744 | /* draw the new frame (@ cur_frame) */ |
| 745 | redraw_screen(charger); |
| 746 | |
| 747 | /* if we don't have anim frames, we only have one image, so just bump |
| 748 | * the cycle counter and exit |
| 749 | */ |
| 750 | if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) { |
| 751 | LOGV("[%lld] animation missing or unknown battery status\n", now); |
| 752 | charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME; |
| 753 | batt_anim->cur_cycle++; |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | /* schedule next screen transition */ |
| 758 | charger->next_screen_transition = now + disp_time; |
| 759 | |
| 760 | /* advance frame cntr to the next valid frame |
| 761 | * if necessary, advance cycle cntr, and reset frame cntr |
| 762 | */ |
| 763 | batt_anim->cur_frame++; |
Dima Zavin | 9ec3f3e | 2011-10-31 16:53:41 -0700 | [diff] [blame] | 764 | |
| 765 | /* if the frame is used for level-only, that is only show it when it's |
| 766 | * the current level, skip it during the animation. |
| 767 | */ |
| 768 | while (batt_anim->cur_frame < batt_anim->num_frames && |
| 769 | batt_anim->frames[batt_anim->cur_frame].level_only) |
| 770 | batt_anim->cur_frame++; |
| 771 | if (batt_anim->cur_frame >= batt_anim->num_frames) { |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 772 | batt_anim->cur_cycle++; |
| 773 | batt_anim->cur_frame = 0; |
| 774 | |
| 775 | /* don't reset the cycle counter, since we use that as a signal |
| 776 | * in a test above to check if animation is over |
| 777 | */ |
| 778 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Dima Zavin | 2471a6a | 2011-10-12 16:16:05 -0700 | [diff] [blame] | 781 | static int set_key_callback(int code, int value, void *data) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 782 | { |
Dima Zavin | 2471a6a | 2011-10-12 16:16:05 -0700 | [diff] [blame] | 783 | struct charger *charger = data; |
| 784 | int64_t now = curr_time_ms(); |
| 785 | int down = !!value; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 786 | |
Dima Zavin | 2471a6a | 2011-10-12 16:16:05 -0700 | [diff] [blame] | 787 | if (code > KEY_MAX) |
| 788 | return -1; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 789 | |
Dima Zavin | 2d978c0 | 2011-10-12 16:18:23 -0700 | [diff] [blame] | 790 | /* ignore events that don't modify our state */ |
| 791 | if (charger->keys[code].down == down) |
Dima Zavin | 471157a | 2011-10-13 13:04:20 -0700 | [diff] [blame] | 792 | return 0; |
Dima Zavin | 2d978c0 | 2011-10-12 16:18:23 -0700 | [diff] [blame] | 793 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 794 | /* only record the down even timestamp, as the amount |
| 795 | * of time the key spent not being pressed is not useful */ |
| 796 | if (down) |
Dima Zavin | 2471a6a | 2011-10-12 16:16:05 -0700 | [diff] [blame] | 797 | charger->keys[code].timestamp = now; |
| 798 | charger->keys[code].down = down; |
| 799 | charger->keys[code].pending = true; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 800 | if (down) { |
Dima Zavin | 2471a6a | 2011-10-12 16:16:05 -0700 | [diff] [blame] | 801 | LOGV("[%lld] key[%d] down\n", now, code); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 802 | } else { |
Dima Zavin | 2471a6a | 2011-10-12 16:16:05 -0700 | [diff] [blame] | 803 | int64_t duration = now - charger->keys[code].timestamp; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 804 | int64_t secs = duration / 1000; |
| 805 | int64_t msecs = duration - secs * 1000; |
| 806 | LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now, |
Dima Zavin | 2471a6a | 2011-10-12 16:16:05 -0700 | [diff] [blame] | 807 | code, secs, msecs); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 808 | } |
Dima Zavin | 2471a6a | 2011-10-12 16:16:05 -0700 | [diff] [blame] | 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | static void update_input_state(struct charger *charger, |
| 814 | struct input_event *ev) |
| 815 | { |
| 816 | if (ev->type != EV_KEY) |
| 817 | return; |
| 818 | set_key_callback(ev->code, ev->value, charger); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | static void set_next_key_check(struct charger *charger, |
| 822 | struct key_state *key, |
| 823 | int64_t timeout) |
| 824 | { |
| 825 | int64_t then = key->timestamp + timeout; |
| 826 | |
| 827 | if (charger->next_key_check == -1 || then < charger->next_key_check) |
| 828 | charger->next_key_check = then; |
| 829 | } |
| 830 | |
| 831 | static void process_key(struct charger *charger, int code, int64_t now) |
| 832 | { |
| 833 | struct key_state *key = &charger->keys[code]; |
| 834 | int64_t next_key_check; |
| 835 | |
| 836 | if (code == KEY_POWER) { |
| 837 | if (key->down) { |
| 838 | int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME; |
| 839 | if (now >= reboot_timeout) { |
| 840 | LOGI("[%lld] rebooting\n", now); |
| 841 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 842 | } else { |
| 843 | /* if the key is pressed but timeout hasn't expired, |
| 844 | * make sure we wake up at the right-ish time to check |
| 845 | */ |
| 846 | set_next_key_check(charger, key, POWER_ON_KEY_TIME); |
| 847 | } |
| 848 | } else { |
| 849 | /* if the power key got released, force screen state cycle */ |
choongryeol.lee | 9255713 | 2012-11-15 17:03:03 -0800 | [diff] [blame] | 850 | if (key->pending) { |
| 851 | request_suspend(false); |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 852 | kick_animation(charger->batt_anim); |
choongryeol.lee | 9255713 | 2012-11-15 17:03:03 -0800 | [diff] [blame] | 853 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
| 857 | key->pending = false; |
| 858 | } |
| 859 | |
| 860 | static void handle_input_state(struct charger *charger, int64_t now) |
| 861 | { |
| 862 | process_key(charger, KEY_POWER, now); |
| 863 | |
| 864 | if (charger->next_key_check != -1 && now > charger->next_key_check) |
| 865 | charger->next_key_check = -1; |
| 866 | } |
| 867 | |
| 868 | static void handle_power_supply_state(struct charger *charger, int64_t now) |
| 869 | { |
| 870 | if (charger->num_supplies_online == 0) { |
choongryeol.lee | 9255713 | 2012-11-15 17:03:03 -0800 | [diff] [blame] | 871 | request_suspend(false); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 872 | if (charger->next_pwr_check == -1) { |
| 873 | charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME; |
| 874 | LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n", |
| 875 | now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check); |
| 876 | } else if (now >= charger->next_pwr_check) { |
| 877 | LOGI("[%lld] shutting down\n", now); |
| 878 | android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
| 879 | } else { |
| 880 | /* otherwise we already have a shutdown timer scheduled */ |
| 881 | } |
| 882 | } else { |
| 883 | /* online supply present, reset shutdown timer if set */ |
| 884 | if (charger->next_pwr_check != -1) { |
| 885 | LOGI("[%lld] device plugged in: shutdown cancelled\n", now); |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 886 | kick_animation(charger->batt_anim); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 887 | } |
| 888 | charger->next_pwr_check = -1; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | static void wait_next_event(struct charger *charger, int64_t now) |
| 893 | { |
| 894 | int64_t next_event = INT64_MAX; |
| 895 | int64_t timeout; |
| 896 | struct input_event ev; |
| 897 | int ret; |
| 898 | |
| 899 | LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now, |
| 900 | charger->next_screen_transition, charger->next_key_check, |
| 901 | charger->next_pwr_check); |
| 902 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 903 | if (charger->next_screen_transition != -1) |
| 904 | next_event = charger->next_screen_transition; |
| 905 | if (charger->next_key_check != -1 && charger->next_key_check < next_event) |
| 906 | next_event = charger->next_key_check; |
| 907 | if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event) |
| 908 | next_event = charger->next_pwr_check; |
| 909 | |
| 910 | if (next_event != -1 && next_event != INT64_MAX) |
| 911 | timeout = max(0, next_event - now); |
| 912 | else |
| 913 | timeout = -1; |
| 914 | LOGV("[%lld] blocking (%lld)\n", now, timeout); |
| 915 | ret = ev_wait((int)timeout); |
| 916 | if (!ret) |
| 917 | ev_dispatch(); |
| 918 | } |
| 919 | |
| 920 | static int input_callback(int fd, short revents, void *data) |
| 921 | { |
| 922 | struct charger *charger = data; |
| 923 | struct input_event ev; |
| 924 | int ret; |
| 925 | |
| 926 | ret = ev_get_input(fd, revents, &ev); |
| 927 | if (ret) |
| 928 | return -1; |
Dima Zavin | 2471a6a | 2011-10-12 16:16:05 -0700 | [diff] [blame] | 929 | update_input_state(charger, &ev); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | static void event_loop(struct charger *charger) |
| 934 | { |
| 935 | int ret; |
| 936 | |
| 937 | while (true) { |
| 938 | int64_t now = curr_time_ms(); |
| 939 | |
| 940 | LOGV("[%lld] event_loop()\n", now); |
| 941 | handle_input_state(charger, now); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 942 | handle_power_supply_state(charger, now); |
| 943 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 944 | /* do screen update last in case any of the above want to start |
| 945 | * screen transitions (animations, etc) |
| 946 | */ |
| 947 | update_screen_state(charger, now); |
| 948 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 949 | wait_next_event(charger, now); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | int main(int argc, char **argv) |
| 954 | { |
| 955 | int ret; |
| 956 | struct charger *charger = &charger_state; |
| 957 | int64_t now = curr_time_ms() - 1; |
| 958 | int fd; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 959 | int i; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 960 | |
| 961 | list_init(&charger->supplies); |
| 962 | |
| 963 | klog_init(); |
| 964 | klog_set_level(CHARGER_KLOG_LEVEL); |
| 965 | |
Dima Zavin | 823ebc4 | 2011-09-29 17:20:07 -0700 | [diff] [blame] | 966 | dump_last_kmsg(); |
| 967 | |
| 968 | LOGI("--------------- STARTING CHARGER MODE ---------------\n"); |
| 969 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 970 | gr_init(); |
| 971 | gr_font_size(&char_width, &char_height); |
| 972 | |
| 973 | ev_init(input_callback, charger); |
| 974 | |
| 975 | fd = uevent_open_socket(64*1024, true); |
| 976 | if (fd >= 0) { |
| 977 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 978 | ev_add_fd(fd, uevent_callback, charger); |
| 979 | } |
| 980 | charger->uevent_fd = fd; |
| 981 | coldboot(charger, "/sys/class/power_supply", "add"); |
| 982 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 983 | ret = res_create_surface("charger/battery_fail", &charger->surf_unknown); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 984 | if (ret < 0) { |
| 985 | LOGE("Cannot load image\n"); |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 986 | charger->surf_unknown = NULL; |
| 987 | } |
| 988 | |
| 989 | for (i = 0; i < charger->batt_anim->num_frames; i++) { |
| 990 | struct frame *frame = &charger->batt_anim->frames[i]; |
| 991 | |
| 992 | ret = res_create_surface(frame->name, &frame->surface); |
| 993 | if (ret < 0) { |
| 994 | LOGE("Cannot load image %s\n", frame->name); |
| 995 | /* TODO: free the already allocated surfaces... */ |
| 996 | charger->batt_anim->num_frames = 0; |
| 997 | charger->batt_anim->num_cycles = 1; |
| 998 | break; |
| 999 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Dima Zavin | 2471a6a | 2011-10-12 16:16:05 -0700 | [diff] [blame] | 1002 | ev_sync_key_state(set_key_callback, charger); |
| 1003 | |
Dima Zavin | 209c7b0 | 2012-10-12 15:13:52 -0700 | [diff] [blame] | 1004 | #ifndef CHARGER_DISABLE_INIT_BLANK |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 1005 | gr_fb_blank(true); |
Dima Zavin | 209c7b0 | 2012-10-12 15:13:52 -0700 | [diff] [blame] | 1006 | #endif |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 1007 | |
| 1008 | charger->next_screen_transition = now - 1; |
| 1009 | charger->next_key_check = -1; |
| 1010 | charger->next_pwr_check = -1; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame] | 1011 | reset_animation(charger->batt_anim); |
| 1012 | kick_animation(charger->batt_anim); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 1013 | |
| 1014 | event_loop(charger); |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |