actualy handle acpi properly :)

SVN revision: 7932
This commit is contained in:
Carsten Haitzler 2003-11-23 22:32:35 +00:00
parent 87e13fb40f
commit 3ca49ec205
1 changed files with 160 additions and 2 deletions

View File

@ -1,4 +1,14 @@
#include "epplet.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
/* Modified by Attila ZIMLER <hijaszu@hlfslinux.hu>, 2003/11/16
Added ACPI power management support.
*/
/* Length of explain strings in /proc/acpi/battery/BAT0 data files */
#define DATA_EXPLAIN_STR_LEN 25
int prev_bat_val = 110;
int bat_val = 0;
@ -9,7 +19,9 @@ int prev_up[16] =
int prev_count = 0;
Epplet_gadget b_close, b_suspend, b_sleep, b_help, image, label;
static void cb_timer(void *data);
static void cb_timer(void* data);
static void cb_timer_acpi(void *data);
static void cb_timer_apm(void *data);
static void cb_close(void *data);
static void cb_in(void *data, Window w);
static void cb_out(void *data, Window w);
@ -17,6 +29,152 @@ static void cb_help(void *data);
static void
cb_timer(void *data)
{
struct stat st;
if ((stat("/proc/apm", &st) > -1) && S_ISREG(st.st_mode))
cb_timer_apm(data);
else if ( (stat("/proc/acpi", &st) > -1) && S_ISDIR(st.st_mode))
cb_timer_acpi(data);
}
static void
cb_timer_acpi(void *data)
{
/* We don't have any data from the remaining percentage, and time directly,
so we have to calculate and measure them.
(Measure the time and calculate the percentage.)
*/
static int prev_bat_drain = 1;
FILE *f;
DIR *dirp;
struct dirent *dp;
int bat_max = 0;
int bat_filled = 0;
int bat_level = 0;
int bat_drain = 1;
int bat_val = 0;
char current_status[256];
char *line = 0;
size_t lsize = 0;
int discharging = 0;
int charging = 0;
int battery = 0;
int hours, minutes;
/* Read some information on first run. */
dirp = opendir("/proc/acpi/battery");
if (dirp)
{
while ((dp = readdir(dirp)))
{
char buf[4096];
if ((!strcmp(dp->d_name, ".")) || (!strcmp(dp->d_name, "..")))
continue;
snprintf(buf, sizeof(buf), "/proc/acpi/battery/%s/info",
dp->d_name);
f = fopen(buf, "r");
if (f)
{
int design_cap = 0;
int last_full = 0;
getline(&line, &lsize, f);
getline(&line, &lsize, f);
sscanf(line, "%*[^:]: %i %*s", &design_cap);
getline(&line, &lsize, f);
sscanf(line, "%*[^:]: %i %*s", &last_full);
fclose(f);
bat_max += design_cap;
bat_filled += last_full;
}
snprintf(buf, sizeof(buf), "/proc/acpi/battery/%s/state",
dp->d_name);
f = fopen(buf, "r");
if (f)
{
char present[256];
char capacity_state[256];
char charging_state[256];
int rate = 1;
int level = 0;
getline(&line, &lsize, f);
sscanf(line, "%*[^:]: %250s", present);
getline(&line, &lsize, f);
sscanf(line, "%*[^:]: %250s", capacity_state);
getline(&line, &lsize, f);
sscanf(line, "%*[^:]: %250s", charging_state);
getline(&line, &lsize, f);
sscanf(line, "%*[^:]: %i %*s", &rate);
getline(&line, &lsize, f);
sscanf(line, "%*[^:]: %i %*s", &level);
fclose(f);
if (!strcmp(present, "yes")) battery++;
if (!strcmp(charging_state, "discharging")) discharging++;
if (!strcmp(charging_state, "charging")) charging++;
bat_drain += rate;
bat_level += level;
}
}
}
if (prev_bat_drain < 1) prev_bat_drain = 1;
if (bat_drain < 1) bat_drain = prev_bat_drain;
prev_bat_drain = bat_drain;
if (bat_filled > 0)
bat_val = (100 * bat_level) / bat_filled;
else
bat_val = 100;
if (discharging)
minutes = (60 * bat_level) / bat_drain;
else
{
if (bat_filled > 0)
minutes = (60 * (bat_filled - bat_level)) / bat_drain;
else
minutes = 0;
}
hours = minutes / 60;
minutes -= (hours * 60);
if (charging)
snprintf(current_status, sizeof(current_status),
"%i%% PWR\n"
"%02i:%02i",
bat_val, hours, minutes);
else if (discharging)
snprintf(current_status, sizeof(current_status),
"%i%%\n"
"%02i:%02i",
bat_val, hours, minutes);
else if (!battery)
snprintf(current_status, sizeof(current_status), "No Bat");
else
snprintf(current_status, sizeof(current_status), "Full");
/* Display current status */
Epplet_change_label(label, current_status);
sprintf(current_status, EROOT"/epplet_data/E-Power//E-Power-Bat-%i.png",
((bat_val + 5) / 10) * 10);
Epplet_change_image(image, 44, 24, current_status);
Epplet_timer(cb_timer, NULL, 5.0, "TIMER");
/* Final steps before ending the status update. */
data = NULL;
if ( lsize )
free( line );
}
static void
cb_timer_apm(void *data)
{
static FILE *f;
@ -192,7 +350,7 @@ main(int argc, char **argv)
EROOT"/epplet_data/E-Power/E-Power-Bat-100.png"));
Epplet_gadget_show(label =
Epplet_create_label
(2, 28, "APM not\nin Kernel", 1));
(2, 28, "APM, ACPI\nmissing", 1));
Epplet_register_focus_in_handler(cb_in, NULL);
Epplet_register_focus_out_handler(cb_out, NULL);
cb_timer(NULL);