wmail: refactored MakePathName: one malloc; one snprintf instead of two memcpys.
[dockapps.git] / wmacpi / wmacpi-cli.c
bloba3fbae035aafe432276d393a16db3ccabd43b1dc
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 "2.3"
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 " v - print version information\n",
43 name, name);
46 void print_version(void)
48 printf("wmacpi-cli version %s\n", ACPI_VER);
49 printf(" Using libacpi version %s\n", LIBACPI_VER);
52 int main(int argc, char *argv[])
54 int i, j, ch;
55 int sleep_time = 0;
56 int samples = 1;
57 battery_t *binfo;
58 adapter_t *ap;
60 while((ch = getopt(argc, argv, "hvVa:")) != EOF) {
61 switch(ch) {
62 case 'h':
63 usage(argv[0]);
64 return 0;
65 case 'V':
66 verbosity++;
67 break;
68 case 'v':
69 print_version();
70 return 0;
71 case 'a':
72 if(optarg != NULL) {
73 samples = atoi(optarg);
74 if(samples > 1000 || samples <= 0) {
75 fprintf(stderr, "Please specify a reasonable number of samples\n");
76 exit(1);
79 pinfo("samples: %d\n", samples);
80 sleep_time = 1000000/samples;
81 break;
82 default:
83 usage(argv[0]);
84 return 1;
88 globals = (global_t *) malloc(sizeof(global_t));
90 power_init(globals);
91 /* we want to acquire samples over some period of time, so . . . */
92 for(i = 0; i < samples + 2; i++) {
93 for(j = 0; j < globals->battery_count; j++)
94 acquire_batt_info(globals, j);
95 acquire_global_info(globals);
96 usleep(sleep_time);
99 ap = &globals->adapter;
100 if(ap->power == AC) {
101 printf("On AC Power");
102 for(i = 0; i < globals->battery_count; i++) {
103 binfo = &batteries[i];
104 if(binfo->present && (binfo->charge_state == CHARGE)) {
105 printf("; Battery %s charging", binfo->name);
106 printf(", currently at %2d%%", binfo->percentage);
107 if(binfo->charge_time >= 0)
108 printf(", %2d:%02d remaining",
109 binfo->charge_time/60,
110 binfo->charge_time%60);
113 printf("\n");
114 } else if(ap->power == BATT) {
115 printf("On Battery");
116 for(i = 0; i < globals->battery_count; i++) {
117 binfo = &batteries[i];
118 if(binfo->present && (binfo->percentage >= 0))
119 printf(", Battery %s at %d%%", binfo->name,
120 binfo->percentage);
122 if(globals->rtime >= 0)
123 printf("; %d:%02d remaining", globals->rtime/60,
124 globals->rtime%60);
125 printf("\n");
127 return 0;