Gentoo Archives: gentoo-commits

From: "Christoph Mende (angelos)" <angelos@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] gentoo-x86 commit in xfce-extra/xfce4-battery/files: xfce4-battery-0.5.0-sysfs.patch
Date: Thu, 21 Aug 2008 21:35:55
Message-Id: E1KWHou-0001LE-Co@stork.gentoo.org
1 angelos 08/08/21 21:35:52
2
3 Added: xfce4-battery-0.5.0-sysfs.patch
4 Log:
5 Using sysfs instead of proc, bug 219690
6 (Portage version: 2.2_rc8/cvs/Linux 2.6.26-gentoo x86_64)
7
8 Revision Changes Path
9 1.1 xfce-extra/xfce4-battery/files/xfce4-battery-0.5.0-sysfs.patch
10
11 file : http://sources.gentoo.org/viewcvs.py/gentoo-x86/xfce-extra/xfce4-battery/files/xfce4-battery-0.5.0-sysfs.patch?rev=1.1&view=markup
12 plain: http://sources.gentoo.org/viewcvs.py/gentoo-x86/xfce-extra/xfce4-battery/files/xfce4-battery-0.5.0-sysfs.patch?rev=1.1&content-type=text/plain
13
14 Index: xfce4-battery-0.5.0-sysfs.patch
15 ===================================================================
16 diff -ru xfce4-battery-plugin-0.5.0.orig/panel-plugin/libacpi.c xfce4-battery-plugin-0.5.0-fixed/panel-plugin/libacpi.c
17 --- xfce4-battery-plugin-0.5.0.orig/panel-plugin/libacpi.c 2008-04-06 02:56:10.502679698 +0200
18 +++ xfce4-battery-plugin-0.5.0-fixed/panel-plugin/libacpi.c 2008-04-06 03:15:11.306678469 +0200
19 @@ -54,6 +54,10 @@
20
21 static char batteries[MAXBATT][128];
22 static char battinfo[MAXBATT][128];
23 +/* path to AC adapter because not all AC adapter are listed
24 +in /sys/class/power_supply/AC/ this obviously only supports one AC adapter. */
25 +static char sysfsacdir[128];
26 +
27 #ifndef __linux__
28 #if HAVE_SYSCTL
29 static int
30 @@ -181,9 +185,76 @@
31 #endif
32 #endif
33
34 +int check_acpi_sysfs(void)
35 +{
36 + DIR *sysfs;
37 + struct dirent *batt;
38 + char *name;
39 + FILE *typefile;
40 + char typepath[128];
41 + char tmptype[8];
42 +
43 + sysfs = opendir("/sys/class/power_supply");
44 + if (sysfs == 0)
45 + {
46 + #ifdef DEBUG
47 + printf("DBG:No acpi support for sysfs. Trying procfs...\n");
48 + #endif
49 + return 2;
50 + }
51 +
52 + while ((batt = readdir(sysfs)))
53 + {
54 + name = batt->d_name;
55 + if (!strncmp(".", name, 1)) continue;
56 + /* check whether /sys/class/power_supply/$name/type exists and
57 + contains "Battery" or "Mains" */
58 + sprintf(typepath, "/sys/class/power_supply/%s/type",name);
59 + if(!(typefile = fopen(typepath, "r"))) continue;
60 + fgets(tmptype, 8, typefile);
61 + fclose(typefile);
62 + if(strncmp("Battery", tmptype, 7)==0)
63 + {
64 + sprintf(batteries[batt_count], "/sys/class/power_supply/%s", name);
65 + #ifdef DEBUG
66 + printf("DBG:battery number %d at:\n",batt_count);
67 + printf("DBG:sysfs dir->%s\n",batteries[batt_count]);
68 + printf("DBG:------------------------\n");
69 + #endif
70 + batt_count++;
71 + }
72 + /* I guess that the type of the AC adapter is always "Mains" (?) */
73 + else if(strncmp("Mains", tmptype, 5)==0){
74 + sprintf(sysfsacdir, "/sys/class/power_supply/%s", name);
75 + #ifdef DEBUG
76 + printf("DBG:sysfs AC dir->%s\n",sysfsacdir);
77 + printf("DBG:------------------------\n");
78 + #endif
79 + }
80 + }
81 + closedir(sysfs);
82 + if ( batt_count == 0 )
83 + {
84 +#ifdef DEBUG
85 + printf("DBG:No acpi support for sysfs. Trying procfs...\n");
86 +#endif
87 + acpi_sysfs = 0;
88 + return 2;
89 + }
90 + else
91 + {
92 + acpi_sysfs = 1;
93 + return 0;
94 + }
95 +}
96 +
97 /* see if we have ACPI support */
98 int check_acpi(void)
99 {
100 +#ifdef __linux__
101 + if ( check_acpi_sysfs() == 0 )
102 + return 0;
103 +#endif
104 DIR *battdir;
105 struct dirent *batt;
106 char *name;
107 @@ -262,9 +333,82 @@
108 #endif
109 }
110
111 +int read_sysfs_int(char* filename)
112 +{
113 + FILE* f;
114 + f = fopen(filename,"r");
115 + if ( !f )
116 + {
117 +#ifdef DEBUG
118 + printf("DBG:Could not open %s\n",filename);
119 +#endif
120 + return 0;
121 + }
122 + int out;
123 + fscanf(f,"%d",&out);
124 + fclose(f);
125 + return out;
126 +}
127 +
128 +char* read_sysfs_string(char* filename)
129 +{
130 + FILE* f;
131 + f = fopen(filename,"r");
132 + if ( !f )
133 + {
134 +#ifdef DEBUG
135 + printf("DBG:Could not open %s\n",filename);
136 +#endif
137 + return NULL;
138 + }
139 + fscanf(f,"%s",buf2);
140 + fclose(f);
141 + return buf2;
142 +}
143 +
144 +int read_acad_state_sysfs(void)
145 +{
146 + DIR *sysfs;
147 + struct dirent *propety;
148 + char *name;
149 + char onlinefilepath[128];
150 +
151 + sysfs = opendir(sysfsacdir);
152 + if (sysfs == 0)
153 + {
154 + #ifdef DEBUG
155 + printf("DBG:Can't open %s",sysfsacdir);
156 + #endif
157 + return 0;
158 + }
159 + closedir(sysfs);
160 +
161 + if (!acadstate) acadstate=(ACADstate *)malloc(sizeof(ACADstate));
162 + /* this code doesn't make much sense.. why look at the whole directory?!
163 + while ((propety = readdir(sysfs)))
164 + {
165 + name = propety->d_name;
166 + if (!strncmp(".", name, 1) || !strncmp("..", name, 2)) continue;
167 +
168 + if (strcmp(name,"online") == 0)
169 + {
170 + acadstate->state = ( read_sysfs_int("/sys/class/power_supply/AC/online") == 1 ) ;
171 + }
172 + }
173 + */
174 + sprintf(onlinefilepath, "%s/online", sysfsacdir);
175 + /* if onlinefilepath doesn't exist read_sysfs_int() will return 0
176 + so acadstate->state will be 0, that should be ok */
177 + acadstate->state = ( read_sysfs_int(onlinefilepath) == 1 );
178 +
179 + return acadstate->state;
180 +}
181 +
182 int read_acad_state(void)
183 {
184 #ifdef __linux__
185 + if (acpi_sysfs)
186 + return read_acad_state_sysfs();
187 FILE *acpi;
188 char *ptr;
189 char stat;
190 @@ -354,20 +498,83 @@
191 #endif
192 }
193
194 +int read_acpi_info_sysfs(int battery)
195 +{
196 + DIR *sysfs;
197 + struct dirent *propety;
198 + char *name;
199 +
200 + sysfs = opendir(batteries[battery]);
201 + if (sysfs == 0)
202 + {
203 + #ifdef DEBUG
204 + printf("DBG:Can't open %s!\n", batteries[battery]);
205 + #endif
206 + return 0;
207 + }
208 + /* malloc.. might explain the random battery level values on 2.6.24
209 + systems (energe_full is called charge_full so the value isn't initialised
210 + and some random data from the heap is displayed..)
211 + if (!acpiinfo) acpiinfo=(ACPIinfo *)malloc(sizeof(ACPIinfo));
212 + */
213 + if (!acpiinfo) acpiinfo=(ACPIinfo *)calloc(1, sizeof(ACPIinfo));
214 +
215 + while ((propety = readdir(sysfs)))
216 + {
217 + name = propety->d_name;
218 + if (!strncmp(".", name, 1) || !strncmp("..", name, 2)) continue;
219 + /* at least on my system this is called charge_full */
220 + if ((strcmp(name,"energy_full") == 0) || (strcmp(name,"charge_full") == 0))
221 + {
222 + sprintf(buf,"%s/%s",batteries[battery], name);
223 + acpiinfo->last_full_capacity = read_sysfs_int(buf);
224 + }
225 + if ((strcmp(name,"energy_full_design") == 0) || (strcmp(name,"charge_full_design") == 0))
226 + {
227 + sprintf(buf,"%s/%s",batteries[battery], name);
228 + acpiinfo->design_capacity = read_sysfs_int(buf);
229 + }
230 + if (strcmp(name,"technology") == 0)
231 + {
232 + char *tmp;
233 + sprintf(buf,"%s/%s",batteries[battery], name);
234 + tmp = read_sysfs_string(buf);
235 + if (tmp != NULL)
236 + {
237 + if (strcmp(tmp,"Li-ion") == 0)
238 + acpiinfo->battery_technology = 1;
239 + else
240 + acpiinfo->battery_technology = 0;
241 + }
242 + }
243 + if (strcmp(name,"present") == 0)
244 + {
245 + sprintf(buf,"%s/%s",batteries[battery], name);
246 + acpiinfo->present = read_sysfs_int(buf);
247 + }
248 + }
249 + closedir(sysfs);
250 + return acpiinfo->present;
251 +}
252 +
253 int read_acpi_info(int battery)
254 {
255 #ifdef __linux__
256 - FILE *acpi;
257 - char *ptr;
258 - char stat;
259 - int temp;
260 -
261 if (battery > MAXBATT) {
262 #ifdef DEBUG
263 printf("DBG: error, battery > MAXBATT (%d)\n",MAXBATT);
264 #endif
265 return 0;
266 }
267 +
268 + if (acpi_sysfs)
269 + return read_acpi_info_sysfs(battery);
270 +
271 + FILE *acpi;
272 + char *ptr;
273 + char stat;
274 + int temp;
275 +
276 if (!(acpi = fopen (battinfo[battery], "r"))) {
277 #ifdef DEBUG
278 printf("DBG:cannot open %s for read!\n",battinfo[battery]);
279 @@ -514,9 +721,80 @@
280
281 }
282
283 +int read_acpi_state_sysfs(int battery)
284 +{
285 + DIR *sysfs;
286 + struct dirent *propety;
287 + char *name;
288 +
289 + sysfs = opendir(batteries[battery]);
290 + if (sysfs == 0)
291 + {
292 + #ifdef DEBUG
293 + printf("DBG:Can't open %s!\n", batteries[battery]);
294 + #endif
295 + return 0;
296 + }
297 +
298 + /* again it might be better to use calloc
299 + if (!acpistate) acpistate=(ACPIstate *)malloc(sizeof(ACPIstate));
300 + */
301 + if (!acpistate) acpistate=(ACPIstate *)calloc(1, sizeof(ACPIstate));
302 +
303 + while ((propety = readdir(sysfs)))
304 + {
305 + name = propety->d_name;
306 + if (!strncmp(".", name, 1) || !strncmp("..", name, 2)) continue;
307 +
308 + if (strcmp(name,"status") == 0)
309 + {
310 + char *tmp;
311 + sprintf(buf,"%s/%s",batteries[battery], name);
312 + tmp = read_sysfs_string(buf);
313 + if ( tmp != NULL )
314 + {
315 + if (strcmp(tmp,"Charging") == 0)
316 + acpistate->state = CHARGING;
317 + else if (strcmp(tmp,"Discharging") == 0)
318 + acpistate->state = DISCHARGING;
319 + else if (strcmp(tmp,"Full") == 0)
320 + acpistate->state = POWER;
321 + else
322 + acpistate->state = UNKNOW;
323 + }
324 + }
325 + /* on my system this is called charge_now */
326 + if ((strcmp(name,"energy_now") == 0) || (strcmp(name,"charge_now") == 0))
327 + {
328 + sprintf(buf,"%s/%s",batteries[battery], name);
329 + acpistate->rcapacity = read_sysfs_int(buf);
330 + acpistate->percentage = (((float) acpistate->rcapacity)/acpiinfo->last_full_capacity) * 100;
331 + }
332 + if (strcmp(name,"current_now") == 0)
333 + {
334 + sprintf(buf,"%s/%s",batteries[battery], name);
335 + acpistate->prate = read_sysfs_int(buf);
336 + if ( acpistate->prate < 0 )
337 + acpistate->prate = 0;
338 + if ( acpistate->prate > 0 )
339 + acpistate->rtime = (((float) acpistate->rcapacity) / acpistate->prate) * 60;
340 + }
341 + if (strcmp(name,"voltage_now") == 0)
342 + {
343 + sprintf(buf,"%s/%s",batteries[battery], name);
344 + acpistate->pvoltage = read_sysfs_int(buf);
345 + }
346 + }
347 + closedir(sysfs);
348 + return acpiinfo->present;
349 +}
350 +
351 int read_acpi_state(int battery)
352 {
353 #ifdef __linux__
354 + if (acpi_sysfs)
355 + return read_acpi_state_sysfs(battery);
356 +
357 FILE *acpi;
358 char *ptr;
359 char stat;
360 diff -ru xfce4-battery-plugin-0.5.0.orig/panel-plugin/libacpi.h xfce4-battery-plugin-0.5.0-fixed/panel-plugin/libacpi.h
361 --- xfce4-battery-plugin-0.5.0.orig/panel-plugin/libacpi.h 2007-01-17 18:56:51.000000000 +0100
362 +++ xfce4-battery-plugin-0.5.0-fixed/panel-plugin/libacpi.h 2008-04-05 22:13:55.522679792 +0200
363 @@ -80,6 +80,8 @@
364 int batt_count;
365 /* temp buffer */
366 char buf[512];
367 +char buf2[512];
368 +int acpi_sysfs;
369 #else
370 extern int batt_count;
371 extern ACPIstate *acpistate;