2014-08-18 22:56:10 +00:00
|
|
|
/*
|
|
|
|
* acpi-ng: command line acpi battery status tool.
|
|
|
|
*
|
|
|
|
* Written by Simon Fowler <simon@dreamcraft.com.au>, 2003-06-20.
|
|
|
|
* Copyright 2003-06-20 Dreamcraft Pty Ltd.
|
|
|
|
*
|
|
|
|
* This file is distributed under the GNU General Public License,
|
|
|
|
* version 2. Please see the COPYING file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 2003-06-20.
|
|
|
|
* I'm getting sick of not having a convenient way to query battery
|
|
|
|
* status on the command line, so I'm hacking up this - a quick little
|
|
|
|
* command line tool to display current battery status, using the same
|
|
|
|
* libacpi code as wmacpi-ng.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "libacpi.h"
|
|
|
|
|
2014-08-18 22:56:14 +00:00
|
|
|
#define ACPI_VER "1.99"
|
2014-08-18 22:56:10 +00:00
|
|
|
|
2014-08-18 22:56:13 +00:00
|
|
|
global_t *globals;
|
2014-08-18 22:56:10 +00:00
|
|
|
|
|
|
|
void usage(char *name)
|
|
|
|
{
|
|
|
|
printf("%s: query battery status on ACPI enabled systems.\n"
|
|
|
|
"Usage:\n"
|
2014-08-18 22:56:12 +00:00
|
|
|
"%s [-h] [-a samples]\n"
|
2014-08-18 22:56:10 +00:00
|
|
|
" h - display this help information\n"
|
|
|
|
" a - average remaining time over some number of samples\n"
|
|
|
|
" much more accurate than using a single sample\n"
|
|
|
|
" v - increase verbosity\n",
|
|
|
|
name, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_version(void)
|
|
|
|
{
|
2014-08-18 22:56:14 +00:00
|
|
|
printf("acpi version %s\n", ACPI_VER);
|
2014-08-18 22:56:10 +00:00
|
|
|
printf(" Using libacpi version %s\n", LIBACPI_VER);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i, j, ch;
|
|
|
|
int sleep_time = 0;
|
|
|
|
int samples = 1;
|
2014-08-18 22:56:11 +00:00
|
|
|
battery_t *binfo;
|
|
|
|
adapter_t *ap;
|
2014-08-18 22:56:10 +00:00
|
|
|
|
2014-08-18 22:56:12 +00:00
|
|
|
while((ch = getopt(argc, argv, "hvVa:")) != EOF) {
|
2014-08-18 22:56:10 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 'v':
|
|
|
|
verbosity++;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
print_version();
|
|
|
|
return 0;
|
|
|
|
case 'a':
|
2014-08-18 22:56:12 +00:00
|
|
|
if(optarg != NULL) {
|
2014-08-18 22:56:10 +00:00
|
|
|
samples = atoi(optarg);
|
2014-08-18 22:56:12 +00:00
|
|
|
if(samples > 1000 || samples <= 0) {
|
2014-08-18 22:56:15 +00:00
|
|
|
fprintf(stderr, "Please specify a reasonable number of samples\n");
|
2014-08-18 22:56:12 +00:00
|
|
|
exit(1);
|
2014-08-18 22:56:15 +00:00
|
|
|
}
|
2014-08-18 22:56:10 +00:00
|
|
|
}
|
2014-08-18 22:56:15 +00:00
|
|
|
pinfo("samples: %d\n", samples);
|
2014-08-18 22:56:10 +00:00
|
|
|
sleep_time = 1000000/samples;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:13 +00:00
|
|
|
globals = (global_t *) malloc(sizeof(global_t));
|
2014-08-18 22:56:10 +00:00
|
|
|
|
2014-08-18 22:56:21 +00:00
|
|
|
power_init(globals);
|
2014-08-18 22:56:10 +00:00
|
|
|
/* we want to acquire samples over some period of time, so . . . */
|
|
|
|
for(i = 0; i < samples + 2; i++) {
|
2014-08-18 22:56:19 +00:00
|
|
|
for(j = 0; j < globals->battery_count; j++)
|
2014-08-18 22:56:21 +00:00
|
|
|
acquire_batt_info(globals, j);
|
|
|
|
acquire_global_info(globals);
|
2014-08-18 22:56:10 +00:00
|
|
|
usleep(sleep_time);
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:56:13 +00:00
|
|
|
ap = &globals->adapter;
|
2014-08-18 22:56:11 +00:00
|
|
|
if(ap->power == AC) {
|
2014-08-18 22:56:10 +00:00
|
|
|
printf("On AC Power");
|
2014-08-18 22:56:19 +00:00
|
|
|
for(i = 0; i < globals->battery_count; i++) {
|
2014-08-18 22:56:10 +00:00
|
|
|
binfo = &batteries[i];
|
2014-08-18 22:56:11 +00:00
|
|
|
if(binfo->present && (binfo->charge_state == CHARGE)) {
|
2014-08-18 22:56:10 +00:00
|
|
|
printf("; Battery %s charging", binfo->name);
|
2014-08-18 22:56:12 +00:00
|
|
|
printf(", currently at %2d%%", binfo->percentage);
|
2014-08-18 22:56:11 +00:00
|
|
|
if(binfo->charge_time >= 0)
|
|
|
|
printf(", %2d:%02d remaining",
|
|
|
|
binfo->charge_time/60,
|
|
|
|
binfo->charge_time%60);
|
2014-08-18 22:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
2014-08-18 22:56:11 +00:00
|
|
|
} else if(ap->power == BATT) {
|
2014-08-18 22:56:10 +00:00
|
|
|
printf("On Battery");
|
2014-08-18 22:56:19 +00:00
|
|
|
for(i = 0; i < globals->battery_count; i++) {
|
2014-08-18 22:56:10 +00:00
|
|
|
binfo = &batteries[i];
|
2014-08-18 22:56:11 +00:00
|
|
|
if(binfo->present && (binfo->percentage >= 0))
|
2014-08-18 22:56:10 +00:00
|
|
|
printf(", Battery %s at %d%%", binfo->name,
|
|
|
|
binfo->percentage);
|
|
|
|
}
|
2014-08-18 22:56:13 +00:00
|
|
|
if(globals->rtime >= 0)
|
|
|
|
printf("; %d:%02d remaining", globals->rtime/60,
|
|
|
|
globals->rtime%60);
|
2014-08-18 22:56:11 +00:00
|
|
|
printf("\n");
|
2014-08-18 22:56:10 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|