Re-write the driver to use getopt(), add the new display suspend

function of the kernel, and other assorted misc. cleanups.

Submitted by:	nate & HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
This commit is contained in:
nate 1997-01-06 06:04:42 +00:00
parent 61076a3cf5
commit 54b49f1564
2 changed files with 122 additions and 101 deletions

View File

@ -16,11 +16,8 @@
.Nd control the APM BIOS and display its information
.Sh SYNOPSIS
.Nm apm
.Op Fl z
.Op Fl b
.Op Fl a
.Op Fl l
.Op Fl s
.Op Fl ablsz
.Op Fl d Ar 1|0
.Pp
.Nm zzz
.Sh DESCRIPTION
@ -39,10 +36,13 @@ If no options are specified,
.Nm apm
displays information and current status of APM in verbose mode.
.Bl -tag -width indent
.It Fl z
Suspend the system. It is equivalent to
.Nm zzz.
.Pp
.It Fl a
Display the current AC-line status as an integer value. The values
0 and 1 correspond to the
.Dq off-line
state or
.Dq on-line
state, respectively.
.It Fl b
Display an integer value reflecting the current battery status.
The values 0, 1, 2, 3, correspond to the
@ -54,19 +54,16 @@ status,
status,
.Dq charging
status respectively.
.Pp
.It Fl a
Display the current AC-line status as an integer value. The values
0 and 1 correspond to the
.Dq off-line
state or
.Dq on-line
state, respectively.
.Pp
.It Fl d
Disable/enable suspending of the display separately from a normal suspend
using using the values
.Ar 1
or
.Ar 0
respectively.
.It Fl l
Display the remaining battery percentage. If your laptop does not
support this function, 255 is displayed.
.Pp
.It Fl s
Display the status of the APM support as an integer value. The values
0 and 1 correspond to the
@ -74,6 +71,9 @@ Display the status of the APM support as an integer value. The values
state or
.Dq disabled
state respecitively
.It Fl z
Suspend the system. It is equivalent to
.Nm zzz.
.Sh BUGS
Some APM implementations do not support parameters needed by
.Nm apm.

View File

@ -1,7 +1,7 @@
/*
* LP (Laptop Package)
* apm / zzz APM BIOS utility for FreeBSD
*
* Copyright (C) 1994 by HOSOKAWA Tatasumi <hosokawa@mt.cs.keio.ac.jp>
* Copyright (C) 1994-1996 by HOSOKAWA Tatasumi <hosokawa@mt.cs.keio.ac.jp>
*
* This software may be used, modified, copied, distributed, and sold,
* in both source and binary form provided that the above copyright and
@ -14,153 +14,174 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <machine/apm_bios.h>
#define APMDEV "/dev/apm"
int main_argc;
char **main_argv;
static char *cmdname;
void apm_suspend(int fd)
void
usage()
{
fprintf(stderr, "usage: %s [-ablsz] [-d 1|0]\n", cmdname);
exit(1);
}
void
apm_suspend(int fd)
{
if (ioctl(fd, APMIO_SUSPEND, NULL) == -1) {
fprintf(stderr, "%s: ioctl APMIO_SUSPEND failed.\n", main_argv[0]);
perror(cmdname);
exit(1);
}
}
void apm_getinfo(int fd, apm_info_t aip)
void
apm_getinfo(int fd, apm_info_t aip)
{
if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
fprintf(stderr, "%s: ioctl APMIO_GETINFO failed.\n", main_argv[0]);
perror(cmdname);
exit(1);
}
}
void print_all_info(apm_info_t aip)
void
print_all_info(apm_info_t aip)
{
printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
printf("APM Managment: %s\n", (aip->ai_status ? "Enabled" : "Disabled"));
printf("AC Line status: ");
if (aip->ai_acline == 255) {
if (aip->ai_acline == 255)
printf("unknown");
}
else if (aip->ai_acline > 1) {
else if (aip->ai_acline > 1)
printf("invalid value (0x%x)", aip->ai_acline);
}
else {
static char messages[][10] = {"off-line", "on-line"};
printf("%s", messages[aip->ai_acline]);
}
printf("\n");
printf("Battery status: ");
if (aip->ai_batt_stat == 255) {
if (aip->ai_batt_stat == 255)
printf("unknown");
}
else if (aip->ai_batt_stat > 3) {
else if (aip->ai_batt_stat > 3)
printf("invalid value (0x%x)", aip->ai_batt_stat);
}
else {
static char messages[][10] = {"high", "low", "critical", "charging"};
char messages[][10] = {"high", "low", "critical", "charging"};
printf("%s", messages[aip->ai_batt_stat]);
}
printf("\n");
printf("Remaining battery life: ");
if (aip->ai_batt_life == 255) {
if (aip->ai_batt_life == 255)
printf("unknown");
}
else if (aip->ai_batt_life <= 100) {
else if (aip->ai_batt_life <= 100)
printf("%d%%", aip->ai_batt_life);
}
else {
else
printf("invalid value (0x%x)", aip->ai_batt_life);
}
printf("\n");
}
int main(int argc, char *argv[])
{
int i, j, fd;
int sleep = 0, all_info = 1, apm_status = 0, batt_status = 0, batt_life = 0, ac_status = 0;
char *cmdname;
main_argc = argc;
main_argv = argv;
if ((cmdname = strrchr(argv[0], '/')) != NULL) {
/*
* 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[])
{
int c, fd;
int sleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
int display = 0, batt_life = 0, ac_status = 0;
if ((cmdname = strrchr(argv[0], '/')) != NULL)
cmdname++;
}
else {
else
cmdname = argv[0];
}
if (strcmp(cmdname, "zzz") == 0) {
sleep = 1;
all_info = 0;
goto finish_option;
}
for (i = argc - 1; i >= 1; i--) {
if (argv[i][0] != '-') {
fprintf(stderr, "%s: Unknown option '%s'.\n", argv[0], argv[i]);
exit(1);
}
for (j = 1; argv[i][j]; j++) {
switch (argv[i][j]) {
case 'z':
sleep = 1;
while ((c = getopt(argc, argv, "ablszd:")) != EOF) {
switch (c) {
case 'a':
ac_status = 1;
all_info = 0;
break;
case 'b':
batt_status = 1;
all_info = 0;
break;
case 'a':
ac_status = 1;
case 'd':
display = *optarg - '0';
if (display < 0 || display > 1) {
fprintf(stderr, "%s: Argument of option '-%c' is invalid.\n", cmdname, c);
usage();
}
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:
fprintf(stderr, "%s Unknown option '%s'.\n", argv[0], argv[i]);
exit(1);
}
usage();
}
argc -= optind;
argv += optind;
}
finish_option:
fd = open(APMDEV, O_RDWR);
if (fd == -1) {
fprintf(stderr, "%s: Can't open %s.\n", argv[0], APMDEV);
fprintf(stderr, "%s: Can't open %s.\n", cmdname, APMDEV);
return 1;
}
if (sleep) {
if (sleep)
apm_suspend(fd);
} else {
else {
struct apm_info info;
apm_getinfo(fd, &info);
if (all_info) {
if (all_info)
print_all_info(&info);
}
if (batt_status) {
if (batt_status)
printf("%d\n", info.ai_batt_stat);
}
if (batt_life) {
if (batt_life)
printf("%d\n", info.ai_batt_life);
}
if (ac_status) {
if (ac_status)
printf("%d\n", info.ai_acline);
}
if (apm_status) {
if (apm_status)
printf("%d\n", info.ai_status);
}
if (display)
apm_display(fd, display - 1);
}
close(fd);
return 0;