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
|
|
|
*
|
1998-02-20 07:17:48 +00:00
|
|
|
* Copyright (C) 1994-1996 by Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
|
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)
|
|
|
|
*/
|
|
|
|
|
2002-10-15 16:28:21 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1997-09-02 06:36:39 +00:00
|
|
|
|
1999-07-20 15:31:23 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <machine/apm_bios.h>
|
|
|
|
|
1997-09-02 06:36:39 +00:00
|
|
|
#include <err.h>
|
1994-10-01 03:23:33 +00:00
|
|
|
#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>
|
1998-09-04 16:08:54 +00:00
|
|
|
#include <time.h>
|
1999-07-20 15:31:23 +00:00
|
|
|
#include <unistd.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
|
|
|
|
2004-06-30 21:00:07 +00:00
|
|
|
#define APM_UNKNOWN 255
|
|
|
|
|
1998-09-04 16:08:54 +00:00
|
|
|
#define xh(a) (((a) & 0xff00) >> 8)
|
|
|
|
#define xl(a) ((a) & 0xff)
|
|
|
|
#define APMERR(a) xh(a)
|
|
|
|
|
|
|
|
int cmos_wall = 0; /* True when wall time is in cmos clock, else UTC */
|
|
|
|
|
2004-05-27 19:23:27 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
1997-01-06 06:04:42 +00:00
|
|
|
{
|
1997-09-02 06:36:39 +00:00
|
|
|
fprintf(stderr, "%s\n%s\n",
|
2000-01-22 18:11:58 +00:00
|
|
|
"usage: apm [-ablstzZ] [-d enable ] [ -e enable ] "
|
|
|
|
"[ -h enable ] [-r delta]",
|
1997-09-02 06:36:39 +00:00
|
|
|
" zzz");
|
1997-01-06 06:04:42 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2000-01-22 18:11:58 +00:00
|
|
|
/*
|
|
|
|
* Return 1 for boolean true, and 0 for false, according to the
|
|
|
|
* interpretation of the string argument given.
|
|
|
|
*/
|
2004-05-27 19:23:27 +00:00
|
|
|
static int
|
2004-06-14 16:53:20 +00:00
|
|
|
is_true(const char *boolean)
|
|
|
|
{
|
2000-01-22 18:11:58 +00:00
|
|
|
char *endp;
|
|
|
|
long val;
|
|
|
|
|
|
|
|
val = strtoul(boolean, &endp, 0);
|
|
|
|
if (*endp == '\0')
|
|
|
|
return (val != 0 ? 1 : 0);
|
|
|
|
if (strcasecmp(boolean, "true") == 0 ||
|
|
|
|
strcasecmp(boolean, "yes") == 0 ||
|
|
|
|
strcasecmp(boolean, "enable") == 0)
|
|
|
|
return (1);
|
|
|
|
if (strcasecmp(boolean, "false") == 0 ||
|
|
|
|
strcasecmp(boolean, "no") == 0 ||
|
|
|
|
strcasecmp(boolean, "disable") == 0)
|
|
|
|
return (0);
|
|
|
|
/* Well, I have no idea what the user wants, so... */
|
|
|
|
warnx("invalid boolean argument \"%s\"", boolean);
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
2004-05-27 19:23:27 +00:00
|
|
|
|
|
|
|
return (0);
|
2000-01-22 18:11:58 +00:00
|
|
|
}
|
|
|
|
|
2004-05-27 19:23:27 +00:00
|
|
|
static int
|
1998-09-04 16:08:54 +00:00
|
|
|
int2bcd(int i)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
int base = 0;
|
|
|
|
|
|
|
|
if (i >= 10000)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (i) {
|
|
|
|
retval |= (i % 10) << base;
|
|
|
|
i /= 10;
|
|
|
|
base += 4;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2004-05-27 19:23:27 +00:00
|
|
|
static int
|
1998-09-04 16:08:54 +00:00
|
|
|
bcd2int(int bcd)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
1999-07-20 15:37:28 +00:00
|
|
|
int place = 1;
|
1998-09-04 16:08:54 +00:00
|
|
|
|
|
|
|
if (bcd > 0x9999)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (bcd) {
|
1999-07-20 15:37:28 +00:00
|
|
|
retval += (bcd & 0xf) * place;
|
|
|
|
bcd >>= 4;
|
|
|
|
place *= 10;
|
1998-09-04 16:08:54 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2004-05-27 19:23:27 +00:00
|
|
|
static void
|
1997-01-06 06:04:42 +00:00
|
|
|
apm_suspend(int fd)
|
1994-10-01 03:23:33 +00:00
|
|
|
{
|
1997-09-02 06:36:39 +00:00
|
|
|
if (ioctl(fd, APMIO_SUSPEND, NULL) == -1)
|
2000-01-22 18:11:58 +00:00
|
|
|
err(1, "ioctl(APMIO_SUSPEND)");
|
1994-10-01 03:23:33 +00:00
|
|
|
}
|
|
|
|
|
2004-05-27 19:23:27 +00:00
|
|
|
static void
|
1998-09-04 16:08:54 +00:00
|
|
|
apm_standby(int fd)
|
|
|
|
{
|
|
|
|
if (ioctl(fd, APMIO_STANDBY, NULL) == -1)
|
2000-01-22 18:11:58 +00:00
|
|
|
err(1, "ioctl(APMIO_STANDBY)");
|
1998-09-04 16:08:54 +00:00
|
|
|
}
|
|
|
|
|
2004-05-27 19:23:27 +00:00
|
|
|
static void
|
1997-01-06 06:04:42 +00:00
|
|
|
apm_getinfo(int fd, apm_info_t aip)
|
1994-10-01 03:23:33 +00:00
|
|
|
{
|
1997-09-02 06:36:39 +00:00
|
|
|
if (ioctl(fd, APMIO_GETINFO, aip) == -1)
|
2000-01-22 18:11:58 +00:00
|
|
|
err(1, "ioctl(APMIO_GETINFO)");
|
1994-10-01 03:23:33 +00:00
|
|
|
}
|
|
|
|
|
2004-05-27 19:23:27 +00:00
|
|
|
static void
|
2004-06-14 16:53:20 +00:00
|
|
|
apm_enable(int fd, int enable)
|
|
|
|
{
|
2000-01-22 18:11:58 +00:00
|
|
|
if (enable) {
|
|
|
|
if (ioctl(fd, APMIO_ENABLE) == -1)
|
|
|
|
err(1, "ioctl(APMIO_ENABLE)");
|
|
|
|
} else {
|
|
|
|
if (ioctl(fd, APMIO_DISABLE) == -1)
|
|
|
|
err(1, "ioctl(APMIO_DISABLE)");
|
|
|
|
}
|
2000-01-16 20:25:57 +00:00
|
|
|
}
|
|
|
|
|
2004-06-14 16:53:20 +00:00
|
|
|
static void
|
|
|
|
print_batt_time(int batt_time)
|
|
|
|
{
|
|
|
|
printf("Remaining battery time: ");
|
|
|
|
if (batt_time == -1)
|
|
|
|
printf("unknown\n");
|
|
|
|
else {
|
|
|
|
int h, m, s;
|
|
|
|
|
|
|
|
h = batt_time;
|
|
|
|
s = h % 60;
|
|
|
|
h /= 60;
|
|
|
|
m = h % 60;
|
|
|
|
h /= 60;
|
|
|
|
printf("%2d:%02d:%02d\n", h, m, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_batt_life(u_int batt_life)
|
|
|
|
{
|
|
|
|
printf("Remaining battery life: ");
|
2004-06-30 21:00:07 +00:00
|
|
|
if (batt_life == APM_UNKNOWN)
|
2004-06-14 16:53:20 +00:00
|
|
|
printf("unknown\n");
|
|
|
|
else if (batt_life <= 100)
|
|
|
|
printf("%d%%\n", batt_life);
|
|
|
|
else
|
|
|
|
printf("invalid value (0x%x)\n", batt_life);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_batt_stat(u_int batt_stat)
|
|
|
|
{
|
|
|
|
const char *batt_msg[] = { "high", "low", "critical", "charging" };
|
|
|
|
|
|
|
|
printf("Battery Status: ");
|
2004-06-30 21:00:07 +00:00
|
|
|
if (batt_stat == APM_UNKNOWN)
|
2004-06-14 16:53:20 +00:00
|
|
|
printf("unknown\n");
|
|
|
|
else if (batt_stat > 3)
|
|
|
|
printf("invalid value (0x%x)\n", batt_stat);
|
|
|
|
else
|
|
|
|
printf("%s\n", batt_msg[batt_stat]);
|
|
|
|
}
|
|
|
|
|
2004-05-27 19:23:27 +00:00
|
|
|
static void
|
2000-08-04 20:57:31 +00:00
|
|
|
print_all_info(int fd, apm_info_t aip, int bioscall_available)
|
1994-10-01 03:23:33 +00:00
|
|
|
{
|
1998-09-04 16:08:54 +00:00
|
|
|
struct apm_bios_arg args;
|
|
|
|
int apmerr;
|
2005-05-30 18:44:43 +00:00
|
|
|
const char *line_msg[] = { "off-line", "on-line" , "backup power"};
|
1998-09-04 16:08:54 +00:00
|
|
|
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
|
2004-05-27 19:23:27 +00:00
|
|
|
printf("APM Management: %s\n", aip->ai_status ? "Enabled" : "Disabled");
|
1994-10-01 03:23:33 +00:00
|
|
|
printf("AC Line status: ");
|
2004-06-30 21:00:07 +00:00
|
|
|
if (aip->ai_acline == APM_UNKNOWN)
|
2000-01-22 18:11:58 +00:00
|
|
|
printf("unknown\n");
|
2005-05-30 18:44:43 +00:00
|
|
|
else if (aip->ai_acline > 2)
|
2004-06-14 16:53:20 +00:00
|
|
|
printf("invalid value (0x%x)\n", aip->ai_acline);
|
1997-01-06 06:04:42 +00:00
|
|
|
else
|
2004-06-14 16:53:20 +00:00
|
|
|
printf("%s\n", line_msg[aip->ai_acline]);
|
|
|
|
|
|
|
|
print_batt_stat(aip->ai_batt_stat);
|
|
|
|
print_batt_life(aip->ai_batt_life);
|
|
|
|
print_batt_time(aip->ai_batt_time);
|
|
|
|
|
1998-09-04 16:08:54 +00:00
|
|
|
if (aip->ai_infoversion >= 1) {
|
|
|
|
printf("Number of batteries: ");
|
2004-06-30 21:00:07 +00:00
|
|
|
if (aip->ai_batteries == ~0U)
|
1998-09-04 16:08:54 +00:00
|
|
|
printf("unknown\n");
|
2000-08-13 17:05:27 +00:00
|
|
|
else {
|
2004-05-27 19:23:27 +00:00
|
|
|
u_int i;
|
2000-08-13 17:05:27 +00:00
|
|
|
struct apm_pwstatus aps;
|
|
|
|
|
1998-09-04 16:08:54 +00:00
|
|
|
printf("%d\n", aip->ai_batteries);
|
2000-08-13 17:05:27 +00:00
|
|
|
for (i = 0; i < aip->ai_batteries; ++i) {
|
|
|
|
bzero(&aps, sizeof(aps));
|
|
|
|
aps.ap_device = PMDV_BATT0 + i;
|
|
|
|
if (ioctl(fd, APMIO_GETPWSTATUS, &aps) == -1)
|
|
|
|
continue;
|
|
|
|
printf("Battery %d:\n", i);
|
2004-06-30 21:00:07 +00:00
|
|
|
if (aps.ap_batt_flag & APM_BATT_NOT_PRESENT) {
|
2000-08-13 17:05:27 +00:00
|
|
|
printf("not present\n");
|
|
|
|
continue;
|
|
|
|
}
|
2004-06-16 19:14:21 +00:00
|
|
|
printf("\t");
|
2004-06-14 16:53:20 +00:00
|
|
|
print_batt_stat(aps.ap_batt_stat);
|
2004-06-16 19:14:21 +00:00
|
|
|
printf("\t");
|
2004-06-14 16:53:20 +00:00
|
|
|
print_batt_life(aps.ap_batt_life);
|
2004-06-16 19:14:21 +00:00
|
|
|
printf("\t");
|
2004-06-14 16:53:20 +00:00
|
|
|
print_batt_time(aps.ap_batt_time);
|
2000-08-13 17:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
1998-09-04 16:08:54 +00:00
|
|
|
}
|
1994-10-01 03:23:33 +00:00
|
|
|
|
2000-08-04 20:57:31 +00:00
|
|
|
if (bioscall_available) {
|
|
|
|
/*
|
|
|
|
* try to get the suspend timer
|
|
|
|
*/
|
|
|
|
bzero(&args, sizeof(args));
|
|
|
|
args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
|
|
|
|
args.ebx = PMDV_APMBIOS;
|
|
|
|
args.ecx = 0x0001;
|
|
|
|
if (ioctl(fd, APMIO_BIOS, &args)) {
|
|
|
|
printf("Resume timer: unknown\n");
|
|
|
|
} else {
|
|
|
|
apmerr = APMERR(args.eax);
|
|
|
|
if (apmerr == 0x0d || apmerr == 0x86)
|
|
|
|
printf("Resume timer: disabled\n");
|
|
|
|
else if (apmerr)
|
2002-10-15 16:28:21 +00:00
|
|
|
warnx(
|
|
|
|
"failed to get the resume timer: APM error0x%x", apmerr);
|
2000-08-04 20:57:31 +00:00
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* OK. We have the time (all bcd).
|
|
|
|
* CH - seconds
|
|
|
|
* DH - hours
|
|
|
|
* DL - minutes
|
|
|
|
* xh(SI) - month (1-12)
|
|
|
|
* xl(SI) - day of month (1-31)
|
|
|
|
* DI - year
|
|
|
|
*/
|
|
|
|
struct tm tm;
|
|
|
|
char buf[1024];
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
tm.tm_sec = bcd2int(xh(args.ecx));
|
|
|
|
tm.tm_min = bcd2int(xl(args.edx));
|
|
|
|
tm.tm_hour = bcd2int(xh(args.edx));
|
|
|
|
tm.tm_mday = bcd2int(xl(args.esi));
|
|
|
|
tm.tm_mon = bcd2int(xh(args.esi)) - 1;
|
|
|
|
tm.tm_year = bcd2int(args.edi) - 1900;
|
|
|
|
if (cmos_wall)
|
|
|
|
t = mktime(&tm);
|
|
|
|
else
|
|
|
|
t = timegm(&tm);
|
2003-07-13 20:06:41 +00:00
|
|
|
if (t != -1) {
|
|
|
|
tm = *localtime(&t);
|
|
|
|
strftime(buf, sizeof(buf), "%c", &tm);
|
|
|
|
printf("Resume timer: %s\n", buf);
|
|
|
|
} else
|
|
|
|
printf("Resume timer: unknown\n");
|
2000-08-04 20:57:31 +00:00
|
|
|
}
|
1998-09-04 16:08:54 +00:00
|
|
|
}
|
|
|
|
|
2000-08-04 20:57:31 +00:00
|
|
|
/*
|
|
|
|
* Get the ring indicator resume state
|
|
|
|
*/
|
|
|
|
bzero(&args, sizeof(args));
|
|
|
|
args.eax = (APM_BIOS) << 8 | APM_RESUMEONRING;
|
|
|
|
args.ebx = PMDV_APMBIOS;
|
|
|
|
args.ecx = 0x0002;
|
|
|
|
if (ioctl(fd, APMIO_BIOS, &args) == 0) {
|
|
|
|
printf("Resume on ring indicator: %sabled\n",
|
2002-08-25 06:15:50 +00:00
|
|
|
args.ecx ? "en" : "dis");
|
2000-08-04 20:57:31 +00:00
|
|
|
}
|
1998-09-04 16:08:54 +00:00
|
|
|
}
|
2000-08-04 20:57:31 +00:00
|
|
|
|
1998-09-04 16:08:54 +00:00
|
|
|
if (aip->ai_infoversion >= 1) {
|
|
|
|
if (aip->ai_capabilities == 0xff00)
|
2004-06-30 21:00:07 +00:00
|
|
|
return;
|
|
|
|
printf("APM Capabilities:\n");
|
1998-09-04 16:08:54 +00:00
|
|
|
if (aip->ai_capabilities & 0x01)
|
|
|
|
printf("\tglobal standby state\n");
|
|
|
|
if (aip->ai_capabilities & 0x02)
|
|
|
|
printf("\tglobal suspend state\n");
|
|
|
|
if (aip->ai_capabilities & 0x04)
|
|
|
|
printf("\tresume timer from standby\n");
|
|
|
|
if (aip->ai_capabilities & 0x08)
|
|
|
|
printf("\tresume timer from suspend\n");
|
|
|
|
if (aip->ai_capabilities & 0x10)
|
|
|
|
printf("\tRI resume from standby\n");
|
|
|
|
if (aip->ai_capabilities & 0x20)
|
|
|
|
printf("\tRI resume from suspend\n");
|
|
|
|
if (aip->ai_capabilities & 0x40)
|
|
|
|
printf("\tPCMCIA RI resume from standby\n");
|
|
|
|
if (aip->ai_capabilities & 0x80)
|
|
|
|
printf("\tPCMCIA RI resume from suspend\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 :-).
|
|
|
|
*/
|
2004-05-27 19:23:27 +00:00
|
|
|
static void
|
1997-01-06 06:04:42 +00:00
|
|
|
apm_display(int fd, int newstate)
|
|
|
|
{
|
1997-09-02 06:36:39 +00:00
|
|
|
if (ioctl(fd, APMIO_DISPLAY, &newstate) == -1)
|
2000-01-22 18:11:58 +00:00
|
|
|
err(1, "ioctl(APMIO_DISPLAY)");
|
1997-01-06 06:04:42 +00:00
|
|
|
}
|
|
|
|
|
2004-05-27 19:23:27 +00:00
|
|
|
static void
|
2004-06-14 16:53:20 +00:00
|
|
|
apm_haltcpu(int fd, int enable)
|
|
|
|
{
|
2000-01-22 18:11:58 +00:00
|
|
|
if (enable) {
|
|
|
|
if (ioctl(fd, APMIO_HALTCPU, NULL) == -1)
|
|
|
|
err(1, "ioctl(APMIO_HALTCPU)");
|
|
|
|
} else {
|
|
|
|
if (ioctl(fd, APMIO_NOTHALTCPU, NULL) == -1)
|
|
|
|
err(1, "ioctl(APMIO_NOTHALTCPU)");
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 06:04:42 +00:00
|
|
|
|
2004-05-27 19:23:27 +00:00
|
|
|
static void
|
1998-09-04 16:08:54 +00:00
|
|
|
apm_set_timer(int fd, int delta)
|
|
|
|
{
|
|
|
|
time_t tmr;
|
|
|
|
struct tm *tm;
|
|
|
|
struct apm_bios_arg args;
|
|
|
|
|
|
|
|
tmr = time(NULL) + delta;
|
|
|
|
if (cmos_wall)
|
|
|
|
tm = localtime(&tmr);
|
|
|
|
else
|
|
|
|
tm = gmtime(&tmr);
|
|
|
|
bzero(&args, sizeof(args));
|
|
|
|
args.eax = (APM_BIOS) << 8 | APM_RESUMETIMER;
|
|
|
|
args.ebx = PMDV_APMBIOS;
|
|
|
|
if (delta > 0) {
|
|
|
|
args.ecx = (int2bcd(tm->tm_sec) << 8) | 0x02;
|
|
|
|
args.edx = (int2bcd(tm->tm_hour) << 8) | int2bcd(tm->tm_min);
|
|
|
|
args.esi = (int2bcd(tm->tm_mon + 1) << 8) | int2bcd(tm->tm_mday);
|
|
|
|
args.edi = int2bcd(tm->tm_year + 1900);
|
|
|
|
} else {
|
|
|
|
args.ecx = 0x0000;
|
|
|
|
}
|
|
|
|
if (ioctl(fd, APMIO_BIOS, &args)) {
|
2002-10-15 16:28:21 +00:00
|
|
|
err(1,"set resume timer");
|
1998-09-04 16:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-01-06 06:04:42 +00:00
|
|
|
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;
|
2004-05-27 19:23:27 +00:00
|
|
|
int dosleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
|
2000-01-22 18:11:58 +00:00
|
|
|
int display = -1, batt_life = 0, ac_status = 0, standby = 0;
|
|
|
|
int batt_time = 0, delta = 0, enable = -1, haltcpu = -1;
|
1997-09-02 06:36:39 +00:00
|
|
|
char *cmdname;
|
2000-08-04 20:57:31 +00:00
|
|
|
int bioscall_available = 0;
|
1999-07-20 15:31:23 +00:00
|
|
|
size_t cmos_wall_len = sizeof(cmos_wall);
|
1997-09-02 06:36:39 +00:00
|
|
|
|
1999-07-20 15:31:23 +00:00
|
|
|
if (sysctlbyname("machdep.wall_cmos_clock", &cmos_wall, &cmos_wall_len,
|
|
|
|
NULL, 0) == -1)
|
|
|
|
err(1, "sysctlbyname(machdep.wall_cmos_clock)");
|
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) {
|
2004-05-27 19:23:27 +00:00
|
|
|
dosleep = 1;
|
1994-10-01 03:23:33 +00:00
|
|
|
all_info = 0;
|
|
|
|
goto finish_option;
|
|
|
|
}
|
2000-01-22 18:11:58 +00:00
|
|
|
while ((c = getopt(argc, argv, "abe:h:lRr:stzd:Z")) != -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':
|
2000-01-22 18:11:58 +00:00
|
|
|
display = is_true(optarg);
|
1997-01-06 06:04:42 +00:00
|
|
|
all_info = 0;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
batt_life = 1;
|
|
|
|
all_info = 0;
|
|
|
|
break;
|
1998-09-04 16:08:54 +00:00
|
|
|
case 'R':
|
|
|
|
delta = -1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
delta = atoi(optarg);
|
|
|
|
break;
|
1997-01-06 06:04:42 +00:00
|
|
|
case 's':
|
|
|
|
apm_status = 1;
|
|
|
|
all_info = 0;
|
|
|
|
break;
|
2000-01-16 20:25:57 +00:00
|
|
|
case 'e':
|
2000-01-22 18:11:58 +00:00
|
|
|
enable = is_true(optarg);
|
2002-07-26 13:43:47 +00:00
|
|
|
all_info = 0;
|
2000-01-22 18:11:58 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
haltcpu = is_true(optarg);
|
2002-07-26 13:43:47 +00:00
|
|
|
all_info = 0;
|
2000-01-16 20:25:57 +00:00
|
|
|
break;
|
1997-11-12 04:16:23 +00:00
|
|
|
case 't':
|
|
|
|
batt_time = 1;
|
|
|
|
all_info = 0;
|
|
|
|
break;
|
1997-01-06 06:04:42 +00:00
|
|
|
case 'z':
|
2004-05-27 19:23:27 +00:00
|
|
|
dosleep = 1;
|
1997-01-06 06:04:42 +00:00
|
|
|
all_info = 0;
|
|
|
|
break;
|
1998-09-04 16:08:54 +00:00
|
|
|
case 'Z':
|
|
|
|
standby = 1;
|
|
|
|
all_info = 0;
|
|
|
|
break;
|
1997-01-06 06:04:42 +00:00
|
|
|
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:
|
2004-05-27 19:23:27 +00:00
|
|
|
if (haltcpu != -1 || enable != -1 || display != -1 || delta || dosleep
|
|
|
|
|| standby) {
|
2000-07-19 06:34:02 +00:00
|
|
|
fd = open(APMDEV, O_RDWR);
|
2000-08-04 20:57:31 +00:00
|
|
|
bioscall_available = 1;
|
|
|
|
} else if ((fd = open(APMDEV, O_RDWR)) >= 0)
|
|
|
|
bioscall_available = 1;
|
2000-07-19 06:34:02 +00:00
|
|
|
else
|
|
|
|
fd = open(APMDEV, O_RDONLY);
|
2000-01-22 18:11:58 +00:00
|
|
|
if (fd == -1)
|
|
|
|
err(1, "can't open %s", APMDEV);
|
|
|
|
if (enable != -1)
|
|
|
|
apm_enable(fd, enable);
|
|
|
|
if (haltcpu != -1)
|
|
|
|
apm_haltcpu(fd, haltcpu);
|
1998-09-04 16:08:54 +00:00
|
|
|
if (delta)
|
|
|
|
apm_set_timer(fd, delta);
|
2004-05-27 19:23:27 +00:00
|
|
|
if (dosleep)
|
1994-10-01 03:23:33 +00:00
|
|
|
apm_suspend(fd);
|
1998-09-04 16:08:54 +00:00
|
|
|
else if (standby)
|
|
|
|
apm_standby(fd);
|
|
|
|
else if (delta == 0) {
|
1997-01-06 06:04:42 +00:00
|
|
|
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)
|
2000-08-04 20:57:31 +00:00
|
|
|
print_all_info(fd, &info, bioscall_available);
|
1997-11-12 04:16:23 +00:00
|
|
|
if (ac_status)
|
|
|
|
printf("%d\n", info.ai_acline);
|
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 (apm_status)
|
1996-03-13 00:48:06 +00:00
|
|
|
printf("%d\n", info.ai_status);
|
1997-11-12 04:16:23 +00:00
|
|
|
if (batt_time)
|
|
|
|
printf("%d\n", info.ai_batt_time);
|
2000-01-22 18:11:58 +00:00
|
|
|
if (display != -1)
|
|
|
|
apm_display(fd, display);
|
1994-10-01 03:23:33 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
2000-01-22 18:11:58 +00:00
|
|
|
exit(0);
|
1994-10-01 03:23:33 +00:00
|
|
|
}
|