1
/* $Id: plugin_apm.c 840 2007-09-09 12:17:42Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_apm.c $
4
* plugin for APM (battery status)
6
* Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* based on the old 'battery.c' which is
10
* Copyright (C) 2001 Leopold T�tsch <lt@toetsch.at>
12
* This file is part of LCD4Linux.
14
* LCD4Linux is free software; you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation; either version 2, or (at your option)
19
* LCD4Linux is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
24
* You should have received a copy of the GNU General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33
* int plugin_init_apm (void)
46
#include <sys/ioctl.h>
47
#include <sys/types.h>
48
#include <asm/types.h>
57
/* from /usr/src/linux/arch/i386/kernel/apm.c:
59
* Arguments, with symbols from linux/apm_bios.h. Information is
60
* from the Get Power Status (0x0a) call unless otherwise noted.
62
* 0) Linux driver version (this will change if format changes)
63
* 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
64
* 2) APM flags from APM Installation Check (0x00):
65
* bit 0: APM_16_BIT_SUPPORT
66
* bit 1: APM_32_BIT_SUPPORT
67
* bit 2: APM_IDLE_SLOWS_CLOCK
68
* bit 3: APM_BIOS_DISABLED
69
* bit 4: APM_BIOS_DISENGAGED
73
* 0x02: On backup power (BIOS >= 1.1 only)
80
* 0x04: Selected battery not present (BIOS >= 1.2 only)
87
* bit 7: No system battery
89
* 6) Remaining battery life (percentage of charge):
92
* 7) Remaining battery life (time units):
93
* Number of remaining minutes or seconds
95
* 8) min = minutes; sec = seconds
97
* p+= sprintf(p, "%s %d.%d 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
99
* (apm_info.bios.version >> 8) & 0xff,
100
* apm_info.bios.version & 0xff,
101
* apm_info.bios.flags,
111
static int parse_proc_apm(void)
113
char *key[] = { "driver_version",
124
char buffer[128], *beg, *end;
127
/* reread every 10 msec only */
128
age = hash_age(&APM, NULL);
129
if (age > 0 && age <= 10)
133
fd = open("/proc/apm", O_RDONLY | O_NDELAY);
135
error("open(/proc/apm) failed: %s", strerror(errno));
140
if (lseek(fd, 0L, SEEK_SET) != 0) {
141
error("lseek(/proc/apm) failed: %s", strerror(errno));
146
if (read(fd, &buffer, sizeof(buffer) - 1) == -1) {
147
error("read(/proc/apm) failed: %s", strerror(errno));
153
for (i = 0; i < 9 && beg != NULL; i++) {
156
if ((end = strpbrk(beg, " \n")))
158
hash_put(&APM, key[i], beg);
159
beg = end ? end + 1 : NULL;
166
static void my_apm(RESULT * result, RESULT * arg1)
170
if (parse_proc_apm() < 0) {
171
SetResult(&result, R_STRING, "");
175
val = hash_get(&APM, R2S(arg1), NULL);
179
SetResult(&result, R_STRING, val);
182
int plugin_init_apm(void)
186
AddFunction("apm", 1, my_apm);
191
void plugin_exit_apm(void)