1994-10-01 03:23:33 +00:00
|
|
|
/*
|
1997-01-06 06:04:42 +00:00
|
|
|
* apm / zzz APM BIOS utility for FreeBSD
|
1994-10-01 03:23:33 +00:00
|
|
|
*
|
1997-01-06 06:04:42 +00:00
|
|
|
* Copyright (C) 1994-1996 by HOSOKAWA Tatasumi <hosokawa@mt.cs.keio.ac.jp>
|
1994-10-01 03:23:33 +00:00
|
|
|
*
|
|
|
|
* This software may be used, modified, copied, distributed, and sold,
|
|
|
|
* in both source and binary form provided that the above copyright and
|
1995-05-30 03:57:47 +00:00
|
|
|
* these terms are retained. Under no circumstances is the author
|
|
|
|
* responsible for the proper functioning of this software, nor does
|
|
|
|
* the author assume any responsibility for damages incurred with its
|
1994-10-01 03:23:33 +00:00
|
|
|
* use.
|
|
|
|
*
|
|
|
|
* Sep., 1994 Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1997-01-06 06:04:42 +00:00
|
|
|
#include <stdlib.h>
|
1994-10-01 03:23:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/ioctl.h>
|
1997-01-06 06:04:42 +00:00
|
|
|
#include <unistd.h>
|
1994-11-02 04:54:30 +00:00
|
|
|
#include <machine/apm_bios.h>
|
1994-10-01 03:23:33 +00:00
|
|
|
|
1996-03-18 20:34:29 +00:00
|
|
|
#define APMDEV "/dev/apm"
|
1994-10-01 03:23:33 +00:00
|
|
|
|
1997-01-06 06:04:42 +00:00
|
|
|
static char *cmdname;
|
1994-10-01 03:23:33 +00:00
|
|
|
|
1997-01-06 06:04:42 +00:00
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: %s [-ablsz] [-d 1|0]\n", cmdname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
apm_suspend(int fd)
|
1994-10-01 03:23:33 +00:00
|
|
|
{
|
|
|
|
if (ioctl(fd, APMIO_SUSPEND, NULL) == -1) {
|
1997-01-06 06:04:42 +00:00
|
|
|
perror(cmdname);
|
1994-10-01 03:23:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-01-06 06:04:42 +00:00
|
|
|
void
|
|
|
|
apm_getinfo(int fd, apm_info_t aip)
|
1994-10-01 03:23:33 +00:00
|
|
|
{
|
|
|
|
if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
|
1997-01-06 06:04:42 +00:00
|
|
|
perror(cmdname);
|
1994-10-01 03:23:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-01-06 06:04:42 +00:00
|
|
|
void
|
|
|
|
print_all_info(apm_info_t aip)
|
1994-10-01 03:23:33 +00:00
|
|
|
{
|
|
|
|
printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
|
1997-01-06 06:04:42 +00:00
|
|
|
printf("APM Managment: %s\n", (aip->ai_status ? "Enabled" : "Disabled"));
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("AC Line status: ");
|
1997-01-06 06:04:42 +00:00
|
|
|
if (aip->ai_acline == 255)
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("unknown");
|
1997-01-06 06:04:42 +00:00
|
|
|
else if (aip->ai_acline > 1)
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("invalid value (0x%x)", aip->ai_acline);
|
|
|
|
else {
|
1997-01-06 06:09:13 +00:00
|
|
|
char messages[][10] = {"off-line", "on-line"};
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("%s", messages[aip->ai_acline]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
printf("Battery status: ");
|
1997-01-06 06:04:42 +00:00
|
|
|
if (aip->ai_batt_stat == 255)
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("unknown");
|
1997-01-06 06:04:42 +00:00
|
|
|
else if (aip->ai_batt_stat > 3)
|
|
|
|
printf("invalid value (0x%x)", aip->ai_batt_stat);
|
1994-10-01 03:23:33 +00:00
|
|
|
else {
|
1997-01-06 06:04:42 +00:00
|
|
|
char messages[][10] = {"high", "low", "critical", "charging"};
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("%s", messages[aip->ai_batt_stat]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
printf("Remaining battery life: ");
|
1997-01-06 06:04:42 +00:00
|
|
|
if (aip->ai_batt_life == 255)
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("unknown");
|
1997-01-06 06:04:42 +00:00
|
|
|
else if (aip->ai_batt_life <= 100)
|
|
|
|
printf("%d%%", aip->ai_batt_life);
|
|
|
|
else
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("invalid value (0x%x)", aip->ai_batt_life);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
1997-01-06 06:04:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* currently, it can turn off the display, but the display never comes
|
|
|
|
* back until the machine suspend/resumes :-).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
apm_display(int fd, int newstate)
|
|
|
|
{
|
|
|
|
if (ioctl(fd, APMIO_DISPLAY, &newstate) == -1) {
|
|
|
|
perror(cmdname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
1994-10-01 03:23:33 +00:00
|
|
|
{
|
1997-01-06 06:04:42 +00:00
|
|
|
int c, fd;
|
|
|
|
int sleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
|
|
|
|
int display = 0, batt_life = 0, ac_status = 0;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1997-01-06 06:04:42 +00:00
|
|
|
if ((cmdname = strrchr(argv[0], '/')) != NULL)
|
1994-10-01 03:23:33 +00:00
|
|
|
cmdname++;
|
1997-01-06 06:04:42 +00:00
|
|
|
else
|
1994-10-01 03:23:33 +00:00
|
|
|
cmdname = argv[0];
|
|
|
|
|
|
|
|
if (strcmp(cmdname, "zzz") == 0) {
|
|
|
|
sleep = 1;
|
|
|
|
all_info = 0;
|
|
|
|
goto finish_option;
|
|
|
|
}
|
1997-03-31 05:11:47 +00:00
|
|
|
while ((c = getopt(argc, argv, "ablszd:")) != -1) {
|
1997-01-06 06:04:42 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'a':
|
|
|
|
ac_status = 1;
|
|
|
|
all_info = 0;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
batt_status = 1;
|
|
|
|
all_info = 0;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
display = *optarg - '0';
|
|
|
|
if (display < 0 || display > 1) {
|
|
|
|
fprintf(stderr, "%s: Argument of option '-%c' is invalid.\n", cmdname, c);
|
|
|
|
usage();
|
1994-10-01 03:23:33 +00:00
|
|
|
}
|
1997-01-06 06:04:42 +00:00
|
|
|
display++;
|
|
|
|
all_info = 0;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
batt_life = 1;
|
|
|
|
all_info = 0;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
apm_status = 1;
|
|
|
|
all_info = 0;
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
sleep = 1;
|
|
|
|
all_info = 0;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
1994-10-01 03:23:33 +00:00
|
|
|
}
|
1997-01-06 06:04:42 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1994-10-01 03:23:33 +00:00
|
|
|
}
|
|
|
|
finish_option:
|
|
|
|
fd = open(APMDEV, O_RDWR);
|
|
|
|
if (fd == -1) {
|
1997-01-06 06:04:42 +00:00
|
|
|
fprintf(stderr, "%s: Can't open %s.\n", cmdname, APMDEV);
|
1994-10-01 03:23:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1997-01-06 06:04:42 +00:00
|
|
|
if (sleep)
|
1994-10-01 03:23:33 +00:00
|
|
|
apm_suspend(fd);
|
1997-01-06 06:04:42 +00:00
|
|
|
else {
|
|
|
|
struct apm_info info;
|
1994-10-01 03:23:33 +00:00
|
|
|
|
|
|
|
apm_getinfo(fd, &info);
|
1997-01-06 06:04:42 +00:00
|
|
|
if (all_info)
|
1994-10-01 03:23:33 +00:00
|
|
|
print_all_info(&info);
|
1997-01-06 06:04:42 +00:00
|
|
|
if (batt_status)
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("%d\n", info.ai_batt_stat);
|
1997-01-06 06:04:42 +00:00
|
|
|
if (batt_life)
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("%d\n", info.ai_batt_life);
|
1997-01-06 06:04:42 +00:00
|
|
|
if (ac_status)
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("%d\n", info.ai_acline);
|
1997-01-06 06:04:42 +00:00
|
|
|
if (apm_status)
|
1996-03-13 00:48:06 +00:00
|
|
|
printf("%d\n", info.ai_status);
|
1997-01-06 06:04:42 +00:00
|
|
|
if (display)
|
|
|
|
apm_display(fd, display - 1);
|
1994-10-01 03:23:33 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|