~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to plugin_apm.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
Import upstream version 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 $
 
3
 *
 
4
 * plugin for APM (battery status)
 
5
 *
 
6
 * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
 
7
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * based on the old 'battery.c' which is 
 
10
 * Copyright (C) 2001 Leopold T�tsch <lt@toetsch.at>
 
11
 * 
 
12
 * This file is part of LCD4Linux.
 
13
 *
 
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)
 
17
 * any later version.
 
18
 *
 
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.
 
23
 *
 
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.
 
27
 *
 
28
 */
 
29
 
 
30
/* 
 
31
 * exported functions:
 
32
 *
 
33
 * int plugin_init_apm (void)
 
34
 *  adds apm() function
 
35
 *
 
36
 */
 
37
 
 
38
#include "config.h"
 
39
 
 
40
#include <stdlib.h>
 
41
#include <stdio.h>
 
42
#include <fcntl.h>
 
43
#include <unistd.h>
 
44
#include <string.h>
 
45
#include <errno.h>
 
46
#include <sys/ioctl.h>
 
47
#include <sys/types.h>
 
48
#include <asm/types.h>
 
49
 
 
50
#include "debug.h"
 
51
#include "plugin.h"
 
52
#include "hash.h"
 
53
 
 
54
static int fd = -2;
 
55
static HASH APM;
 
56
 
 
57
/* from /usr/src/linux/arch/i386/kernel/apm.c:
 
58
 *
 
59
 * Arguments, with symbols from linux/apm_bios.h.  Information is
 
60
 * from the Get Power Status (0x0a) call unless otherwise noted.
 
61
 *
 
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
 
70
 * 3) AC line status
 
71
 *    0x00: Off-line
 
72
 *    0x01: On-line
 
73
 *    0x02: On backup power (BIOS >= 1.1 only)
 
74
 *    0xff: Unknown
 
75
 * 4) Battery status
 
76
 *    0x00: High
 
77
 *    0x01: Low
 
78
 *    0x02: Critical
 
79
 *    0x03: Charging
 
80
 *    0x04: Selected battery not present (BIOS >= 1.2 only)
 
81
 *    0xff: Unknown
 
82
 * 5) Battery flag
 
83
 *    bit 0: High
 
84
 *    bit 1: Low
 
85
 *    bit 2: Critical
 
86
 *    bit 3: Charging
 
87
 *    bit 7: No system battery
 
88
 *    0xff: Unknown
 
89
 * 6) Remaining battery life (percentage of charge):
 
90
 *    0-100: valid
 
91
 *    -1: Unknown
 
92
 * 7) Remaining battery life (time units):
 
93
 *    Number of remaining minutes or seconds
 
94
 *    -1: Unknown
 
95
 * 8) min = minutes; sec = seconds
 
96
 *
 
97
 * p+= sprintf(p, "%s %d.%d 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
 
98
 *             driver_version,
 
99
 *             (apm_info.bios.version >> 8) & 0xff,
 
100
 *             apm_info.bios.version & 0xff,
 
101
 *             apm_info.bios.flags,
 
102
 *             ac_line_status,
 
103
 *             battery_status,
 
104
 *             battery_flag,
 
105
 *             percentage,
 
106
 *             time_units,
 
107
 *             units);
 
108
 */
 
109
 
 
110
 
 
111
static int parse_proc_apm(void)
 
112
{
 
113
    char *key[] = { "driver_version",
 
114
        "bios_version",
 
115
        "bios_flags",
 
116
        "line_status",
 
117
        "battery_status",
 
118
        "battery_flag",
 
119
        "battery_percent",
 
120
        "battery_remaining",
 
121
        "time_units"
 
122
    };
 
123
 
 
124
    char buffer[128], *beg, *end;
 
125
    int age, i;
 
126
 
 
127
    /* reread every 10 msec only */
 
128
    age = hash_age(&APM, NULL);
 
129
    if (age > 0 && age <= 10)
 
130
        return 0;
 
131
 
 
132
    if (fd == -2) {
 
133
        fd = open("/proc/apm", O_RDONLY | O_NDELAY);
 
134
        if (fd == -1) {
 
135
            error("open(/proc/apm) failed: %s", strerror(errno));
 
136
            return -1;
 
137
        }
 
138
    }
 
139
 
 
140
    if (lseek(fd, 0L, SEEK_SET) != 0) {
 
141
        error("lseek(/proc/apm) failed: %s", strerror(errno));
 
142
        fd = -1;
 
143
        return -1;
 
144
    }
 
145
 
 
146
    if (read(fd, &buffer, sizeof(buffer) - 1) == -1) {
 
147
        error("read(/proc/apm) failed: %s", strerror(errno));
 
148
        fd = -1;
 
149
        return -1;
 
150
    }
 
151
 
 
152
    beg = buffer;
 
153
    for (i = 0; i < 9 && beg != NULL; i++) {
 
154
        while (*beg == ' ')
 
155
            beg++;
 
156
        if ((end = strpbrk(beg, " \n")))
 
157
            *end = '\0';
 
158
        hash_put(&APM, key[i], beg);
 
159
        beg = end ? end + 1 : NULL;
 
160
    }
 
161
 
 
162
    return 0;
 
163
}
 
164
 
 
165
 
 
166
static void my_apm(RESULT * result, RESULT * arg1)
 
167
{
 
168
    char *val;
 
169
 
 
170
    if (parse_proc_apm() < 0) {
 
171
        SetResult(&result, R_STRING, "");
 
172
        return;
 
173
    }
 
174
 
 
175
    val = hash_get(&APM, R2S(arg1), NULL);
 
176
    if (val == NULL)
 
177
        val = "";
 
178
 
 
179
    SetResult(&result, R_STRING, val);
 
180
}
 
181
 
 
182
int plugin_init_apm(void)
 
183
{
 
184
    hash_create(&APM);
 
185
 
 
186
    AddFunction("apm", 1, my_apm);
 
187
 
 
188
    return 0;
 
189
}
 
190
 
 
191
void plugin_exit_apm(void)
 
192
{
 
193
    if (fd > -1) {
 
194
        close(fd);
 
195
    }
 
196
    fd = -2;
 
197
 
 
198
    hash_destroy(&APM);
 
199
}