wmauda: Fix installation dir
[dockapps.git] / wmpower-0.4.3 / src / power_management / apm / libapm.c
blobea95ae055488b2106bf8459a4ffe94adfdbe1cef
1 /***************************************************************************
2 libapm.c - description
3 -------------------
4 begin : Feb 10 2003
5 copyright : (C) 2003 by Noberasco Michele
6 e-mail : 2001s098@educ.disi.unige.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 * *
26 ***************************************************************************/
28 /***************************************************************************
29 Originally written by Filippo Panessa for his 'wmab' program
30 ***************************************************************************/
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
37 #include "libapm.h"
39 int apm_read(struct_apm_data *i);
40 int apm_exists();
42 /* Check to see if /proc/apm exists... */
43 int apm_exists()
45 if (!access(APMDEV, R_OK)) return 1;
47 return 0;
50 /* Read in the information found in /proc/apm... */
51 int apm_read(struct_apm_data *i)
53 FILE *str;
54 char units[10];
55 char buffer[100];
57 /* Open /proc/apm for reading */
58 if (!(str = fopen(APMDEV, "r"))) return 0;
60 /* Scan in the information.... */
61 fgets(buffer, sizeof(buffer) - 1, str);
62 buffer[sizeof(buffer) - 1] = '\0';
63 sscanf(buffer, "%s %d.%d %x %x %x %x %d%% %d %s\n",
64 (char *)i->driver_version,
65 &i->apm_version_major,
66 &i->apm_version_minor,
67 (unsigned int *) &i->apm_flags,
68 (unsigned int *) &i->ac_line_status,
69 (unsigned int *) &i->battery_status,
70 (unsigned int *) &i->battery_flags,
71 &i->battery_percentage,
72 &i->battery_time,
73 units
75 i->using_minutes = !strncmp(units, "min", 3) ? 1 : 0;
76 /* Old Style */
77 if (i->driver_version[0] == 'B')
79 strcpy((char *)i->driver_version, "pre-0.7");
80 i->apm_version_major = 0;
81 i->apm_version_minor = 0;
82 i->apm_flags = 0;
83 i->ac_line_status = 0xff;
84 i->battery_status = 0xff;
85 i->battery_flags = 0xff;
86 i->battery_percentage = -1;
87 i->battery_time = -1;
88 i->using_minutes = 1;
89 sscanf(buffer, "BIOS version: %d.%d", &i->apm_version_major, &i->apm_version_minor);
90 fgets(buffer, sizeof(buffer) - 1, str);
91 sscanf(buffer, "Flags: 0x%02x", (unsigned int *) &i->apm_flags);
92 if (i->apm_flags & APM_32_BIT_SUPPORT)
94 fgets(buffer, sizeof(buffer) - 1, str);
95 fgets(buffer, sizeof(buffer) - 1, str);
96 if (buffer[0] != 'P')
98 if (!strncmp(buffer+4, "off line", 8)) i->ac_line_status = 0;
99 else if (!strncmp(buffer+4, "on line", 7)) i->ac_line_status = 1;
100 else if (!strncmp(buffer+4, "on back", 7)) i->ac_line_status = 2;
101 fgets(buffer, sizeof(buffer) - 1, str);
102 if (!strncmp(buffer+16, "high", 4)) i->battery_status = 0;
103 else if (!strncmp(buffer+16, "low", 3)) i->battery_status = 1;
104 else if (!strncmp(buffer+16, "crit", 4)) i->battery_status = 2;
105 else if (!strncmp(buffer+16, "charg", 5)) i->battery_status = 3;
106 fgets(buffer, sizeof(buffer) - 1, str);
107 if (strncmp(buffer+14, "unknown", 7)) i->battery_percentage = atoi(buffer + 14);
108 if (i->apm_version_major >= 1 && i->apm_version_minor >= 1)
110 fgets(buffer, sizeof(buffer) - 1, str);
111 sscanf(buffer, "Battery flag: 0x%02x", (unsigned int *) &i->battery_flags);
112 fgets(buffer, sizeof(buffer) - 1, str);
113 if (strncmp(buffer+14, "unknown", 7)) i->battery_time = atoi(buffer + 14);
119 /* Take care of battery percentages > 100% */
120 if (i->battery_percentage > 100) i->battery_percentage = -1;
121 fclose(str);
123 return 1;