wmacpi: Bump to version 1.99r4.
[dockapps.git] / wmacpi / acpi.c
blob23649fb5889124f40b121a38cf9abd68c6c0ffab
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 #define ACPI_VER "1.99"
31 global_t *globals;
33 void usage(char *name)
35 printf("%s: query battery status on ACPI enabled systems.\n"
36 "Usage:\n"
37 "%s [-h] [-a samples]\n"
38 " h - display this help information\n"
39 " a - average remaining time over some number of samples\n"
40 " much more accurate than using a single sample\n"
41 " v - increase verbosity\n",
42 name, name);
45 void print_version(void)
47 printf("acpi version %s\n", ACPI_VER);
48 printf(" Using libacpi version %s\n", LIBACPI_VER);
51 int main(int argc, char *argv[])
53 int i, j, ch;
54 int sleep_time = 0;
55 int samples = 1;
56 battery_t *binfo;
57 adapter_t *ap;
59 while((ch = getopt(argc, argv, "hvVa:")) != EOF) {
60 switch(ch) {
61 case 'h':
62 usage(argv[0]);
63 return 0;
64 case 'v':
65 verbosity++;
66 break;
67 case 'V':
68 print_version();
69 return 0;
70 case 'a':
71 if(optarg != NULL) {
72 samples = atoi(optarg);
73 if(samples > 1000 || samples <= 0) {
74 fprintf(stderr, "Please specify a reasonable number of samples\n");
75 exit(1);
78 pinfo("samples: %d\n", samples);
79 sleep_time = 1000000/samples;
80 break;
81 default:
82 usage(argv[0]);
83 return 1;
87 globals = (global_t *) malloc(sizeof(global_t));
89 power_init();
90 /* we want to acquire samples over some period of time, so . . . */
91 for(i = 0; i < samples + 2; i++) {
92 for(j = 0; j < batt_count; j++)
93 acquire_batt_info(j);
94 acquire_global_info();
95 usleep(sleep_time);
98 ap = &globals->adapter;
99 if(ap->power == AC) {
100 printf("On AC Power");
101 for(i = 0; i < batt_count; i++) {
102 binfo = &batteries[i];
103 if(binfo->present && (binfo->charge_state == CHARGE)) {
104 printf("; Battery %s charging", binfo->name);
105 printf(", currently at %2d%%", binfo->percentage);
106 if(binfo->charge_time >= 0)
107 printf(", %2d:%02d remaining",
108 binfo->charge_time/60,
109 binfo->charge_time%60);
112 printf("\n");
113 } else if(ap->power == BATT) {
114 printf("On Battery");
115 for(i = 0; i < batt_count; i++) {
116 binfo = &batteries[i];
117 if(binfo->present && (binfo->percentage >= 0))
118 printf(", Battery %s at %d%%", binfo->name,
119 binfo->percentage);
121 if(globals->rtime >= 0)
122 printf("; %d:%02d remaining", globals->rtime/60,
123 globals->rtime%60);
124 printf("\n");
126 return 0;