]> git.sur5r.net Git - i3/i3status/blob - src/get_battery_info.c
Remove unneeded struct
[i3/i3status] / src / get_battery_info.c
1 // vim:ts=8:expandtab
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 #include "i3status.h"
7
8 /*
9  * Get battery information from /sys. Note that it uses the design capacity to
10  * calculate the percentage, not the last full capacity, so you can see how
11  * worn off your battery is.
12  *
13  */
14 const char *get_battery_info(struct battery *bat) {
15         char buf[1024];
16         static char part[512];
17         char *walk, *last;
18         int full_design = -1,
19             remaining = -1,
20             present_rate = -1;
21         charging_status_t status = CS_DISCHARGING;
22
23         slurp(bat->path, buf, sizeof(buf));
24
25         for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
26                 if (*walk == '\n') {
27                         last = walk+1;
28                         continue;
29                 }
30
31                 if (*walk != '=')
32                         continue;
33
34                 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
35                     BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
36                         remaining = atoi(walk+1);
37                 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
38                         present_rate = atoi(walk+1);
39                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
40                         status = CS_CHARGING;
41                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
42                         status = CS_FULL;
43                 else {
44                         /* The only thing left is the full capacity */
45                         if (bat->use_last_full) {
46                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
47                                     !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
48                                         continue;
49                         } else {
50                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
51                                     !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
52                                         continue;
53                         }
54
55                         full_design = atoi(walk+1);
56                 }
57         }
58
59         if ((full_design == 1) || (remaining == -1))
60                 return part;
61
62         if (present_rate > 0) {
63                 float remaining_time;
64                 int seconds, hours, minutes;
65                 if (status == CS_CHARGING)
66                         remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
67                 else if (status == CS_DISCHARGING)
68                         remaining_time = ((float)remaining / (float)present_rate);
69                 else remaining_time = 0;
70
71                 seconds = (int)(remaining_time * 3600.0);
72                 hours = seconds / 3600;
73                 seconds -= (hours * 3600);
74                 minutes = seconds / 60;
75                 seconds -= (minutes * 60);
76
77                 (void)snprintf(part, sizeof(part), "%s %.02f%% %02d:%02d:%02d",
78                         (status == CS_CHARGING ? "CHR" :
79                          (status == CS_DISCHARGING ? "BAT" : "FULL")),
80                         (((float)remaining / (float)full_design) * 100),
81                         max(hours, 0), max(minutes, 0), max(seconds, 0));
82         } else {
83                 (void)snprintf(part, sizeof(part), "%s %.02f%%",
84                         (status == CS_CHARGING ? "CHR" :
85                          (status == CS_DISCHARGING ? "BAT" : "FULL")),
86                         (((float)remaining / (float)full_design) * 100));
87         }
88         return part;
89 }