Xinqi Bao's Git
99e09d531f95f5b38f4893b4da82ff3ba2fa983e
   1 /* See LICENSE file for copyright and license details. */ 
  11         #define CHARGE_NOW "/sys/class/power_supply/%s/charge_now" 
  12         #define ENERGY_NOW "/sys/class/power_supply/%s/energy_now" 
  13         #define CURRENT_NOW "/sys/class/power_supply/%s/current_now" 
  14         #define POWER_NOW "/sys/class/power_supply/%s/power_now" 
  17         pick(const char *bat
, const char *f1
, const char *f2
, char *path
, 
  20                 if (esnprintf(path
, length
, f1
, bat
) > 0 && 
  21                     access(path
, R_OK
) == 0) { 
  25                 if (esnprintf(path
, length
, f2
, bat
) > 0 && 
  26                     access(path
, R_OK
) == 0) { 
  34         battery_perc(const char *bat
) 
  39                 if (esnprintf(path
, sizeof(path
), 
  40                               "/sys/class/power_supply/%s/capacity", 
  44                 if (pscanf(path
, "%d", &perc
) != 1) { 
  48                 return bprintf("%d", perc
); 
  52         battery_state(const char *bat
) 
  59                         { "Discharging", "-" }, 
  62                 char path
[PATH_MAX
], state
[12]; 
  64                 if (esnprintf(path
, sizeof(path
), 
  65                               "/sys/class/power_supply/%s/status", 
  69                 if (pscanf(path
, "%12s", state
) != 1) { 
  73                 for (i 
= 0; i 
< LEN(map
); i
++) { 
  74                         if (!strcmp(map
[i
].state
, state
)) { 
  78                 return (i 
== LEN(map
)) ? "?" : map
[i
].symbol
; 
  82         battery_remaining(const char *bat
) 
  84                 int charge_now
, current_now
, m
, h
; 
  86                 char path
[PATH_MAX
], state
[12]; 
  88                 if (esnprintf(path
, sizeof(path
), 
  89                               "/sys/class/power_supply/%s/status", 
  93                 if (pscanf(path
, "%12s", state
) != 1) { 
  97                 if (!pick(bat
, CHARGE_NOW
, ENERGY_NOW
, path
, sizeof(path
)) || 
  98                     pscanf(path
, "%d", &charge_now
) < 0) { 
 102                 if (!strcmp(state
, "Discharging")) { 
 103                         if (!pick(bat
, CURRENT_NOW
, POWER_NOW
, path
, 
 105                             pscanf(path
, "%d", ¤t_now
) < 0) { 
 109                         if (current_now 
== 0) { 
 113                         timeleft 
= (double)charge_now 
/ (double)current_now
; 
 115                         m 
= (timeleft 
- (double)h
) * 60; 
 117                         return bprintf("%dh %dm", h
, m
); 
 122 #elif defined(__OpenBSD__) 
 124         #include <machine/apmvar.h> 
 125         #include <sys/ioctl.h> 
 129         load_apm_power_info(struct apm_power_info 
*apm_info
) 
 133                 fd 
= open("/dev/apm", O_RDONLY
); 
 135                         warn("open '/dev/apm':"); 
 139                 memset(apm_info
, 0, sizeof(struct apm_power_info
)); 
 140                 if (ioctl(fd
, APM_IOC_GETPOWER
, apm_info
) < 0) { 
 141                         warn("ioctl 'APM_IOC_GETPOWER':"); 
 149         battery_perc(const char *unused
) 
 151                 struct apm_power_info apm_info
; 
 153                 if (load_apm_power_info(&apm_info
)) { 
 154                         return bprintf("%d", apm_info
.battery_life
); 
 161         battery_state(const char *unused
) 
 170                 struct apm_power_info apm_info
; 
 173                 if (load_apm_power_info(&apm_info
)) { 
 174                         for (i 
= 0; i 
< LEN(map
); i
++) { 
 175                                 if (map
[i
].state 
== apm_info
.ac_state
) { 
 179                         return (i 
== LEN(map
)) ? "?" : map
[i
].symbol
; 
 186         battery_remaining(const char *unused
) 
 188                 struct apm_power_info apm_info
; 
 190                 if (load_apm_power_info(&apm_info
)) { 
 191                         if (apm_info
.ac_state 
!= APM_AC_ON
) { 
 192                                 return bprintf("%uh %02um", 
 193                                                apm_info
.minutes_left 
/ 60, 
 194                                                apm_info
.minutes_left 
% 60);