xref: /freebsd/usr.sbin/apm/apm.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*
2  * LP (Laptop Package)
3  *
4  * Copyright (C) 1994 by HOSOKAWA Tatasumi <hosokawa@mt.cs.keio.ac.jp>
5  *
6  * This software may be used, modified, copied, distributed, and sold,
7  * in both source and binary form provided that the above copyright and
8  * these terms are retained. Under no circumstances is the author
9  * responsible for the proper functioning of this software, nor does
10  * the author assume any responsibility for damages incurred with its
11  * use.
12  *
13  * Sep., 1994	Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
14  */
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <sys/file.h>
19 #include <sys/ioctl.h>
20 #include <machine/apm_bios.h>
21 
22 #define APMDEV	"/dev/apm"
23 
24 int main_argc;
25 char **main_argv;
26 
27 void apm_suspend(int fd)
28 {
29 	if (ioctl(fd, APMIO_SUSPEND, NULL) == -1) {
30 		fprintf(stderr, "%s: ioctl APMIO_SUSPEND failed.\n", main_argv[0]);
31 		exit(1);
32 	}
33 }
34 
35 void apm_getinfo(int fd, apm_info_t aip)
36 {
37 	if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
38 		fprintf(stderr, "%s: ioctl APMIO_GETINFO failed.\n", main_argv[0]);
39 		exit(1);
40 	}
41 }
42 
43 void print_all_info(apm_info_t aip)
44 {
45 	printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
46 	printf("APM Managment: %s\n", (aip->ai_status ? "Enabled": "Disabled"));
47 	printf("AC Line status: ");
48 	if (aip->ai_acline == 255) {
49 		printf("unknown");
50 	}
51 	else if (aip->ai_acline > 1) {
52 		printf("invalid value (0x%x)", aip->ai_acline);
53 	}
54 	else {
55 		static char messages[][10] = {"off-line", "on-line"};
56 		printf("%s", messages[aip->ai_acline]);
57 	}
58 	printf("\n");
59 	printf("Battery status: ");
60 	if (aip->ai_batt_stat == 255) {
61 		printf("unknown");
62 	}
63 	else if (aip->ai_batt_stat > 3) {
64 		printf("invalid value (0x%x)", aip->ai_batt_stat);
65 	}
66 	else {
67 		static char messages[][10] = {"high", "low", "critical", "charging"};
68 		printf("%s", messages[aip->ai_batt_stat]);
69 	}
70 	printf("\n");
71 	printf("Remaining battery life: ");
72 	if (aip->ai_batt_life == 255) {
73 		printf("unknown");
74 	}
75 	else if (aip->ai_batt_life <= 100) {
76 		printf("%d%%", aip->ai_batt_life);
77 	}
78 	else {
79 		printf("invalid value (0x%x)", aip->ai_batt_life);
80 	}
81 	printf("\n");
82 }
83 
84 int main(int argc, char *argv[])
85 {
86 	int i, j, fd;
87 	int sleep = 0, all_info = 1, apm_status = 0, batt_status = 0, batt_life = 0, ac_status = 0;
88 	char *cmdname;
89 
90 	main_argc = argc;
91 	main_argv = argv;
92 	if ((cmdname = strrchr(argv[0], '/')) != NULL) {
93 		cmdname++;
94 	}
95 	else {
96 		cmdname = argv[0];
97 	}
98 
99 	if (strcmp(cmdname, "zzz") == 0) {
100 		sleep = 1;
101 		all_info = 0;
102 		goto finish_option;
103 	}
104 
105 	for (i = argc - 1; i >= 1; i--) {
106 		if (argv[i][0] != '-') {
107 			fprintf(stderr, "%s: Unknown option '%s'.\n", argv[0], argv[i]);
108 			exit(1);
109 		}
110 		for (j = 1; argv[i][j]; j++) {
111 			switch (argv[i][j]) {
112 			case 'z':
113 				sleep = 1;
114 				all_info = 0;
115 				break;
116 			case 'b':
117 				batt_status = 1;
118 				all_info = 0;
119 				break;
120 			case 'a':
121 				ac_status = 1;
122 				all_info = 0;
123 				break;
124 			case 'l':
125 				batt_life = 1;
126 				all_info = 0;
127 			case 's':
128 				apm_status = 1;
129 				all_info = 0;
130 				break;
131 			default:
132 				fprintf(stderr, "%s Unknown option '%s'.\n", argv[0], argv[i]);
133 				exit(1);
134 			}
135 		}
136 	}
137 finish_option:
138 	fd = open(APMDEV, O_RDWR);
139 	if (fd == -1) {
140 		fprintf(stderr, "%s: Can't open %s.\n", argv[0], APMDEV);
141 		return 1;
142 	}
143 	if (sleep) {
144 		apm_suspend(fd);
145 	} else {
146 		struct apm_info	info;
147 
148 		apm_getinfo(fd, &info);
149 		if (all_info) {
150 			print_all_info(&info);
151 		}
152 		if (batt_status) {
153 			printf("%d\n", info.ai_batt_stat);
154 		}
155 		if (batt_life) {
156 			printf("%d\n", info.ai_batt_life);
157 		}
158 		if (ac_status) {
159 			printf("%d\n", info.ai_acline);
160 		}
161 		if (apm_status) {
162 			printf("%d\n", info.ai_status);
163 		}
164 	}
165 	close(fd);
166 	return 0;
167 }
168