wmclockmon: update change-log
[dockapps.git] / wmacpi / wmacpi-cli.c
blob3c552d30f455b361fff975b80ff5ecc7cc121d58
1 /*
2 * acpi-ng: command line acpi battery status tool.
4 * Written by Simon Fowler <simon@dreamcraft.com.au>, 2003-06-20.
5 * Copyright 2003-06-20 Dreamcraft Pty Ltd.
7 * This file is distributed under the GNU General Public License,
8 * version 2. Please see the COPYING file for details.
9 */
12 * 2003-06-20.
13 * I'm getting sick of not having a convenient way to query battery
14 * status on the command line, so I'm hacking up this - a quick little
15 * command line tool to display current battery status, using the same
16 * libacpi code as wmacpi-ng.
19 #define _GNU_SOURCE
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <getopt.h>
25 #include <unistd.h>
27 #include "libacpi.h"
29 global_t *globals;
31 void usage(char *name)
33 printf("%s: query battery status on ACPI enabled systems.\n"
34 "Usage:\n"
35 "%s [-h] [-a samples]\n"
36 " h - display this help information\n"
37 " a - average remaining time over some number of samples\n"
38 " much more accurate than using a single sample\n"
39 " V - increase verbosity\n"
40 " v - print version information\n",
41 name, name);
44 void print_version(void)
46 printf("wmacpi-cli version %s\n", WMACPI_VER);
47 printf(" Using libacpi version %s\n", LIBACPI_VER);
50 int main(int argc, char *argv[])
52 int i, j, ch;
53 int sleep_time = 0;
54 int samples = 1;
55 battery_t *binfo;
56 adapter_t *ap;
58 while((ch = getopt(argc, argv, "hvVa:")) != EOF) {
59 switch(ch) {
60 case 'h':
61 usage(argv[0]);
62 return 0;
63 case 'V':
64 verbosity++;
65 break;
66 case 'v':
67 print_version();
68 return 0;
69 case 'a':
70 if(optarg != NULL) {
71 samples = atoi(optarg);
72 if(samples > 1000 || samples <= 0) {
73 fprintf(stderr, "Please specify a reasonable number of samples\n");
74 exit(1);
77 pinfo("samples: %d\n", samples);
78 sleep_time = 1000000/samples;
79 break;
80 default:
81 usage(argv[0]);
82 return 1;
86 globals = (global_t *) malloc(sizeof(global_t));
88 power_init(globals);
89 /* we want to acquire samples over some period of time, so . . . */
90 for(i = 0; i < samples + 2; i++) {
91 for(j = 0; j < globals->battery_count; j++)
92 acquire_batt_info(globals, j);
93 acquire_global_info(globals);
94 usleep(sleep_time);
97 ap = &globals->adapter;
98 if(ap->power == AC) {
99 printf("On AC Power");
100 for(i = 0; i < globals->battery_count; i++) {
101 binfo = &batteries[i];
102 if(binfo->present && (binfo->charge_state == CHARGE)) {
103 printf("; Battery %s charging", binfo->name);
104 printf(", currently at %2d%%", binfo->percentage);
105 if(binfo->charge_time >= 0)
106 printf(", %2d:%02d remaining",
107 binfo->charge_time/60,
108 binfo->charge_time%60);
111 printf("\n");
112 } else if(ap->power == BATT) {
113 printf("On Battery");
114 for(i = 0; i < globals->battery_count; i++) {
115 binfo = &batteries[i];
116 if(binfo->present && (binfo->percentage >= 0))
117 printf(", Battery %s at %d%%", binfo->name,
118 binfo->percentage);
120 if(globals->rtime >= 0)
121 printf("; %d:%02d remaining", globals->rtime/60,
122 globals->rtime%60);
123 printf("\n");
125 return 0;