xref: /freebsd/usr.sbin/apm/apm.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
1 /*
2  * apm / zzz	APM BIOS utility for FreeBSD
3  *
4  * Copyright (C) 1994-1996 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 <stdlib.h>
18 #include <string.h>
19 #include <sys/file.h>
20 #include <sys/ioctl.h>
21 #include <unistd.h>
22 #include <machine/apm_bios.h>
23 
24 #define APMDEV	"/dev/apm"
25 
26 static char *cmdname;
27 
28 void
29 usage()
30 {
31 	fprintf(stderr, "usage: %s [-ablsz] [-d 1|0]\n", cmdname);
32 	exit(1);
33 }
34 
35 void
36 apm_suspend(int fd)
37 {
38 	if (ioctl(fd, APMIO_SUSPEND, NULL) == -1) {
39 		perror(cmdname);
40 		exit(1);
41 	}
42 }
43 
44 void
45 apm_getinfo(int fd, apm_info_t aip)
46 {
47 	if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
48 		perror(cmdname);
49 		exit(1);
50 	}
51 }
52 
53 void
54 print_all_info(apm_info_t aip)
55 {
56 	printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
57 	printf("APM Managment: %s\n", (aip->ai_status ? "Enabled" : "Disabled"));
58 	printf("AC Line status: ");
59 	if (aip->ai_acline == 255)
60 		printf("unknown");
61 	else if (aip->ai_acline > 1)
62 		printf("invalid value (0x%x)", aip->ai_acline);
63 	else {
64 		char messages[][10] = {"off-line", "on-line"};
65 		printf("%s", messages[aip->ai_acline]);
66 	}
67 	printf("\n");
68 	printf("Battery status: ");
69 	if (aip->ai_batt_stat == 255)
70 		printf("unknown");
71 	else if (aip->ai_batt_stat > 3)
72 			printf("invalid value (0x%x)", aip->ai_batt_stat);
73 	else {
74 		char messages[][10] = {"high", "low", "critical", "charging"};
75 		printf("%s", messages[aip->ai_batt_stat]);
76 	}
77 	printf("\n");
78 	printf("Remaining battery life: ");
79 	if (aip->ai_batt_life == 255)
80 		printf("unknown");
81 	else if (aip->ai_batt_life <= 100)
82 			printf("%d%%", aip->ai_batt_life);
83 	else
84 		printf("invalid value (0x%x)", aip->ai_batt_life);
85 	printf("\n");
86 }
87 
88 
89 /*
90  * currently, it can turn off the display, but the display never comes
91  * back until the machine suspend/resumes :-).
92  */
93 void
94 apm_display(int fd, int newstate)
95 {
96 	if (ioctl(fd, APMIO_DISPLAY, &newstate) == -1) {
97 		perror(cmdname);
98 		exit(1);
99 	}
100 }
101 
102 
103 extern char *optarg;
104 extern int optind;
105 
106 int
107 main(int argc, char *argv[])
108 {
109 	int	c, fd;
110 	int     sleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
111 	int     display = 0, batt_life = 0, ac_status = 0;
112 
113 	if ((cmdname = strrchr(argv[0], '/')) != NULL)
114 		cmdname++;
115 	else
116 		cmdname = argv[0];
117 
118 	if (strcmp(cmdname, "zzz") == 0) {
119 		sleep = 1;
120 		all_info = 0;
121 		goto finish_option;
122 	}
123 	while ((c = getopt(argc, argv, "ablszd:")) != -1) {
124 		switch (c) {
125 		case 'a':
126 			ac_status = 1;
127 			all_info = 0;
128 			break;
129 		case 'b':
130 			batt_status = 1;
131 			all_info = 0;
132 			break;
133 		case 'd':
134 			display = *optarg - '0';
135 			if (display < 0 || display > 1) {
136 				fprintf(stderr, "%s: Argument of option '-%c' is invalid.\n", cmdname, c);
137 				usage();
138 			}
139 			display++;
140 			all_info = 0;
141 			break;
142 		case 'l':
143 			batt_life = 1;
144 			all_info = 0;
145 			break;
146 		case 's':
147 			apm_status = 1;
148 			all_info = 0;
149 			break;
150 		case 'z':
151 			sleep = 1;
152 			all_info = 0;
153 			break;
154 		case '?':
155 		default:
156 			usage();
157 		}
158 		argc -= optind;
159 		argv += optind;
160 	}
161 finish_option:
162 	fd = open(APMDEV, O_RDWR);
163 	if (fd == -1) {
164 		fprintf(stderr, "%s: Can't open %s.\n", cmdname, APMDEV);
165 		return 1;
166 	}
167 	if (sleep)
168 		apm_suspend(fd);
169 	else {
170 		struct apm_info info;
171 
172 		apm_getinfo(fd, &info);
173 		if (all_info)
174 			print_all_info(&info);
175 		if (batt_status)
176 			printf("%d\n", info.ai_batt_stat);
177 		if (batt_life)
178 			printf("%d\n", info.ai_batt_life);
179 		if (ac_status)
180 			printf("%d\n", info.ai_acline);
181 		if (apm_status)
182 			printf("%d\n", info.ai_status);
183 		if (display)
184 			apm_display(fd, display - 1);
185 	}
186 	close(fd);
187 	return 0;
188 }
189