Index: usr.bin/top/display.h =================================================================== --- usr.bin/top/display.h +++ usr.bin/top/display.h @@ -24,6 +24,7 @@ void i_swap(int *stats); void i_timeofday(time_t *tod); void i_uptime(struct timeval *bt, time_t *tod); +void i_battery(int nbat, int batt); void new_message(int type, const char *msgfmt, ...); int readline(char *buffer, int size, int numeric); char *trim_header(const char *text); Index: usr.bin/top/display.c =================================================================== --- usr.bin/top/display.c +++ usr.bin/top/display.c @@ -1322,6 +1322,14 @@ } } +void +i_battery(int nbat, int batt) +{ + if (nbat > 0) { + printf("; battery: %d%%", batt); + } +} + #define SETUPBUFFER_MIN_SCREENWIDTH 80 #define SETUPBUFFER_REQUIRED_ADDBUFSIZ 2 Index: usr.bin/top/machine.h =================================================================== --- usr.bin/top/machine.h +++ usr.bin/top/machine.h @@ -31,6 +31,7 @@ const char * const *swap_names; const char * const *order_names; int ncpus; + int nbatteries; }; /* @@ -51,6 +52,7 @@ int *swap; struct timeval boottime; int ncpus; + int battery; }; /* Index: usr.bin/top/machine.c =================================================================== --- usr.bin/top/machine.c +++ usr.bin/top/machine.c @@ -211,6 +211,10 @@ static long *pcpu_cp_diff; static int *pcpu_cpu_states; +/* Battery units and states */ +static int battery_units; +static int battery_life; + static int compare_swap(const void *a, const void *b); static int compare_jid(const void *a, const void *b); static int compare_pid(const void *a, const void *b); @@ -373,6 +377,12 @@ pcpu_cpu_states = calloc(ncpus * CPUSTATES, sizeof(int)); statics->ncpus = ncpus; + /* Allocate state of battery units. */ + battery_units = 0; + size = sizeof(int); + sysctlbyname("hw.acpi.battery.units", &battery_units, &size, NULL, 0); + statics->nbatteries = battery_units; + update_layout(); /* all done! */ @@ -579,6 +589,12 @@ } else { si->boottime.tv_sec = -1; } + + battery_life = 0; + if (battery_units > 0) { + GETSYSCTL("hw.acpi.battery.life", battery_life); + } + si->battery = battery_life; } #define NOPROC ((void *)-1) Index: usr.bin/top/top.c =================================================================== --- usr.bin/top/top.c +++ usr.bin/top/top.c @@ -619,6 +619,9 @@ (*d_loadave)(system_info.last_pid, system_info.load_avg); + /* display the battery info (if any) */ + i_battery(statics.nbatteries, system_info.battery); + /* display the current time */ /* this method of getting the time SHOULD be fairly portable */ time(&curr_time);