apm.c revision 14609
1109998Smarkm/* 2296465Sdelphij * LP (Laptop Package) 3296465Sdelphij * 4296465Sdelphij * Copyright (C) 1994 by HOSOKAWA Tatasumi <hosokawa@mt.cs.keio.ac.jp> 5109998Smarkm * 6109998Smarkm * This software may be used, modified, copied, distributed, and sold, 7109998Smarkm * in both source and binary form provided that the above copyright and 8109998Smarkm * these terms are retained. Under no circumstances is the author 9109998Smarkm * responsible for the proper functioning of this software, nor does 10109998Smarkm * the author assume any responsibility for damages incurred with its 11109998Smarkm * use. 12109998Smarkm * 13109998Smarkm * Sep., 1994 Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD) 14296465Sdelphij */ 15109998Smarkm 16109998Smarkm#include <stdio.h> 17109998Smarkm#include <string.h> 18109998Smarkm#include <sys/file.h> 19109998Smarkm#include <sys/ioctl.h> 20109998Smarkm#include <machine/apm_bios.h> 21109998Smarkm 22109998Smarkm#define APMDEV "/dev/apm0" 23109998Smarkm 24109998Smarkmint main_argc; 25109998Smarkmchar **main_argv; 26109998Smarkm 27109998Smarkmvoid apm_suspend(int fd) 28109998Smarkm{ 29109998Smarkm if (ioctl(fd, APMIO_SUSPEND, NULL) == -1) { 30109998Smarkm fprintf(stderr, "%s: ioctl APMIO_SUSPEND failed.\n", main_argv[0]); 31109998Smarkm exit(1); 32109998Smarkm } 33109998Smarkm} 34109998Smarkm 35109998Smarkmvoid apm_getinfo(int fd, apm_info_t aip) 36109998Smarkm{ 37109998Smarkm if (ioctl(fd, APMIO_GETINFO, aip) == -1) { 38109998Smarkm fprintf(stderr, "%s: ioctl APMIO_GETINFO failed.\n", main_argv[0]); 39109998Smarkm exit(1); 40109998Smarkm } 41109998Smarkm} 42109998Smarkm 43109998Smarkmvoid print_all_info(apm_info_t aip) 44109998Smarkm{ 45109998Smarkm printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor); 46109998Smarkm printf("APM Managment: %s\n", (aip->ai_status ? "Enabled": "Disabled")); 47109998Smarkm printf("AC Line status: "); 48109998Smarkm if (aip->ai_acline == 255) { 49109998Smarkm printf("unknown"); 50109998Smarkm } 51109998Smarkm else if (aip->ai_acline > 1) { 52109998Smarkm printf("invalid value (0x%x)", aip->ai_acline); 53109998Smarkm } 54109998Smarkm else { 55109998Smarkm static char messages[][10] = {"off-line", "on-line"}; 56109998Smarkm printf("%s", messages[aip->ai_acline]); 57109998Smarkm } 58109998Smarkm printf("\n"); 59109998Smarkm printf("Battery status: "); 60109998Smarkm if (aip->ai_batt_stat == 255) { 61109998Smarkm printf("unknown"); 62109998Smarkm } 63109998Smarkm else if (aip->ai_batt_stat > 3) { 64296465Sdelphij printf("invalid value (0x%x)", aip->ai_batt_stat); 65109998Smarkm } 66109998Smarkm else { 67109998Smarkm static char messages[][10] = {"high", "low", "critical", "charging"}; 68194206Ssimon printf("%s", messages[aip->ai_batt_stat]); 69296465Sdelphij } 70296465Sdelphij printf("\n"); 71109998Smarkm printf("Remaining battery life: "); 72296465Sdelphij if (aip->ai_batt_life == 255) { 73296465Sdelphij printf("unknown"); 74109998Smarkm } 75296465Sdelphij else if (aip->ai_batt_life <= 100) { 76296465Sdelphij printf("%d%%", aip->ai_batt_life); 77296465Sdelphij } 78296465Sdelphij else { 79296465Sdelphij printf("invalid value (0x%x)", aip->ai_batt_life); 80296465Sdelphij } 81296465Sdelphij printf("\n"); 82296465Sdelphij} 83296465Sdelphij 84296465Sdelphijint main(int argc, char *argv[]) 85296465Sdelphij{ 86296465Sdelphij int i, j, fd; 87296465Sdelphij int sleep = 0, all_info = 1, apm_status = 0, batt_status = 0, batt_life = 0, ac_status = 0; 88296465Sdelphij char *cmdname; 89296465Sdelphij 90296465Sdelphij main_argc = argc; 91296465Sdelphij main_argv = argv; 92296465Sdelphij if ((cmdname = strrchr(argv[0], '/')) != NULL) { 93109998Smarkm cmdname++; 94109998Smarkm } 95109998Smarkm else { 96296465Sdelphij cmdname = argv[0]; 97296465Sdelphij } 98296465Sdelphij 99109998Smarkm if (strcmp(cmdname, "zzz") == 0) { 100109998Smarkm sleep = 1; 101296465Sdelphij all_info = 0; 102296465Sdelphij goto finish_option; 103109998Smarkm } 104296465Sdelphij 105296465Sdelphij for (i = argc - 1; i >= 1; i--) { 106296465Sdelphij if (argv[i][0] != '-') { 107296465Sdelphij fprintf(stderr, "%s: Unknown option '%s'.\n", argv[0], argv[i]); 108296465Sdelphij exit(1); 109296465Sdelphij } 110296465Sdelphij for (j = 1; argv[i][j]; j++) { 111109998Smarkm switch (argv[i][j]) { 112296465Sdelphij case 'z': 113296465Sdelphij sleep = 1; 114109998Smarkm all_info = 0; 115296465Sdelphij break; 116296465Sdelphij case 'b': 117296465Sdelphij batt_status = 1; 118296465Sdelphij all_info = 0; 119109998Smarkm break; 120296465Sdelphij case 'a': 121296465Sdelphij ac_status = 1; 122109998Smarkm all_info = 0; 123296465Sdelphij break; 124296465Sdelphij case 'l': 125296465Sdelphij batt_life = 1; 126109998Smarkm all_info = 0; 127296465Sdelphij case 's': 128296465Sdelphij apm_status = 1; 129109998Smarkm all_info = 0; 130109998Smarkm break; 131296465Sdelphij default: 132296465Sdelphij fprintf(stderr, "%s Unknown option '%s'.\n", argv[0], argv[i]); 133296465Sdelphij exit(1); 134296465Sdelphij } 135296465Sdelphij } 136296465Sdelphij } 137296465Sdelphijfinish_option: 138296465Sdelphij fd = open(APMDEV, O_RDWR); 139296465Sdelphij if (fd == -1) { 140296465Sdelphij fprintf(stderr, "%s: Can't open %s.\n", argv[0], APMDEV); 141296465Sdelphij return 1; 142296465Sdelphij } 143296465Sdelphij if (sleep) { 144296465Sdelphij apm_suspend(fd); 145296465Sdelphij } else { 146109998Smarkm struct apm_info info; 147296465Sdelphij 148296465Sdelphij apm_getinfo(fd, &info); 149296465Sdelphij if (all_info) { 150296465Sdelphij print_all_info(&info); 151296465Sdelphij } 152296465Sdelphij if (batt_status) { 153296465Sdelphij printf("%d\n", info.ai_batt_stat); 154296465Sdelphij } 155296465Sdelphij if (batt_life) { 156296465Sdelphij printf("%d\n", info.ai_batt_life); 157296465Sdelphij } 158296465Sdelphij if (ac_status) { 159296465Sdelphij printf("%d\n", info.ai_acline); 160296465Sdelphij } 161296465Sdelphij if (apm_status) { 162296465Sdelphij printf("%d\n", info.ai_status); 163296465Sdelphij } 164296465Sdelphij } 165296465Sdelphij close(fd); 166296465Sdelphij return 0; 167296465Sdelphij} 168296465Sdelphij