E-Power: Cosmetics.

This commit is contained in:
Kim Woelders 2013-07-12 19:47:06 +02:00
parent f1c8766cca
commit 2a2b07b2df
1 changed files with 112 additions and 137 deletions

View File

@ -8,43 +8,112 @@
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;
int time_val = 0;
int prev_up[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
int prev_count = 0;
Epplet_gadget b_close, b_suspend, b_sleep, b_help, image, label;
static Epplet_gadget b_close, b_suspend, b_sleep, b_help, image, label;
static void cb_timer(void *data);
static void cb_timer_acpi(void *data);
static void cb_timer_apm(void *data);
static void cb_timer_sys(void *data);
static void cb_close(void *data);
static void cb_in(void *data, Window w);
static void cb_out(void *data, Window w);
static void cb_help(void *data);
static void
cb_timer(void *data)
cb_timer_apm(void)
{
struct stat st;
static int prev_bat_val = 110;
static int bat_val = 0;
static int time_val = 0;
if ((stat("/proc/apm", &st) > -1) && S_ISREG(st.st_mode))
cb_timer_apm(data);
else if ((stat("/proc/acpi/battery", &st) > -1) && S_ISDIR(st.st_mode))
cb_timer_acpi(data);
else if ((stat("/sys/class/power_supply", &st) > -1) && S_ISDIR(st.st_mode))
cb_timer_sys(data);
static int prev_up[16] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static int prev_count = 0;
FILE *f;
f = fopen("/proc/apm", "r");
if (f)
{
char s[256], s1[32], s2[32], s3[32];
int apm_flags, ac_stat, bat_stat, bat_flags;
int i, hours, minutes, up, up2;
char *s_ptr;
fgets(s, 255, f);
sscanf(s, "%*s %*s %x %x %x %x %s %s %s", &apm_flags, &ac_stat,
&bat_stat, &bat_flags, s1, s2, s3);
s1[strlen(s1) - 1] = 0;
bat_val = atoi(s1);
if (!strcmp(s3, "sec"))
time_val = atoi(s2);
else if (!strcmp(s3, "min"))
time_val = atoi(s2) * 60;
fclose(f);
up = bat_val - prev_bat_val;
up2 = up;
for (i = 0; i < 16; i++)
up2 = +prev_up[i];
up2 = (up2 * 60) / 17;
prev_up[prev_count] = up;
prev_count++;
if (prev_count >= 16)
prev_count = 0;
s_ptr = s;
if (bat_flags != 0xff && bat_flags & 0x80)
{
s_ptr += sprintf(s_ptr, "no battery");
}
else
{
if (bat_val > 0)
s_ptr += sprintf(s_ptr, "%i%%", bat_val);
switch (bat_stat)
{
case 0:
s_ptr += sprintf(s_ptr, ", high");
break;
case 1:
s_ptr += sprintf(s_ptr, ", low");
break;
case 2:
s_ptr += sprintf(s_ptr, ", crit.");
break;
case 3:
s_ptr += sprintf(s_ptr, ", charge");
break;
}
}
s_ptr += sprintf(s_ptr, "\n");
if (ac_stat == 1)
{
s_ptr += sprintf(s_ptr, "AC on-line");
}
else
{
hours = time_val / 3600;
minutes = (time_val / 60) % 60;
if (up2 > 0)
s_ptr += sprintf(s_ptr, "(%i:%02i)\n%i:%02i",
(((100 - bat_val) * 2 * 60) / up2) / 60,
(((100 - bat_val) * 2 * 60) / up2) % 60,
hours, minutes);
else
s_ptr += sprintf(s_ptr, "%i:%02i", hours, minutes);
}
Epplet_change_label(label, s);
sprintf(s, "E-Power-Bat-%i.png", ((bat_val + 5) / 10) * 10);
Epplet_change_image(image, 44, 24, s);
Epplet_timer(cb_timer, NULL, 30.0, "TIMER");
prev_bat_val = bat_val;
}
}
static void
cb_timer_acpi(void *data)
cb_timer_acpi(void)
{
/* We don't have any data from the remaining percentage, and time directly,
* so we have to calculate and measure them.
@ -214,105 +283,12 @@ cb_timer_acpi(void *data)
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;
f = fopen("/proc/apm", "r");
if (f)
{
char s[256], s1[32], s2[32], s3[32];
int apm_flags, ac_stat, bat_stat, bat_flags;
int i, hours, minutes, up, up2;
char *s_ptr;
fgets(s, 255, f);
sscanf(s, "%*s %*s %x %x %x %x %s %s %s", &apm_flags, &ac_stat,
&bat_stat, &bat_flags, s1, s2, s3);
s1[strlen(s1) - 1] = 0;
bat_val = atoi(s1);
if (!strcmp(s3, "sec"))
time_val = atoi(s2);
else if (!strcmp(s3, "min"))
time_val = atoi(s2) * 60;
fclose(f);
up = bat_val - prev_bat_val;
up2 = up;
for (i = 0; i < 16; i++)
up2 = +prev_up[i];
up2 = (up2 * 60) / 17;
prev_up[prev_count] = up;
prev_count++;
if (prev_count >= 16)
prev_count = 0;
s_ptr = s;
if (bat_flags != 0xff && bat_flags & 0x80)
{
s_ptr += sprintf(s_ptr, "no battery");
}
else
{
if (bat_val > 0)
s_ptr += sprintf(s_ptr, "%i%%", bat_val);
switch (bat_stat)
{
case 0:
s_ptr += sprintf(s_ptr, ", high");
break;
case 1:
s_ptr += sprintf(s_ptr, ", low");
break;
case 2:
s_ptr += sprintf(s_ptr, ", crit.");
break;
case 3:
s_ptr += sprintf(s_ptr, ", charge");
break;
}
}
s_ptr += sprintf(s_ptr, "\n");
if (ac_stat == 1)
{
s_ptr += sprintf(s_ptr, "AC on-line");
}
else
{
hours = time_val / 3600;
minutes = (time_val / 60) % 60;
if (up2 > 0)
s_ptr += sprintf(s_ptr, "(%i:%02i)\n%i:%02i",
(((100 - bat_val) * 2 * 60) / up2) / 60,
(((100 - bat_val) * 2 * 60) / up2) % 60,
hours, minutes);
else
s_ptr += sprintf(s_ptr, "%i:%02i", hours, minutes);
}
Epplet_change_label(label, s);
sprintf(s, "E-Power-Bat-%i.png", ((bat_val + 5) / 10) * 10);
Epplet_change_image(image, 44, 24, s);
Epplet_timer(cb_timer, NULL, 30.0, "TIMER");
prev_bat_val = bat_val;
}
data = NULL;
}
static void
cb_timer_sys(void *data)
cb_timer_sys(void)
{
/* We don't have any data from the remaining percentage, and time directly,
* so we have to calculate and measure them.
@ -365,7 +341,6 @@ cb_timer_sys(void *data)
int last_full = 0;
char present[256];
char key[256];
char capacity_state[256];
char charging_state[256];
char name[256];
int rate = 1;
@ -482,16 +457,28 @@ cb_timer_sys(void *data)
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(void *data)
{
struct stat st;
if ((stat("/proc/apm", &st) > -1) && S_ISREG(st.st_mode))
cb_timer_apm();
else if ((stat("/proc/acpi/battery", &st) > -1) && S_ISDIR(st.st_mode))
cb_timer_acpi();
else if ((stat("/sys/class/power_supply", &st) > -1) && S_ISDIR(st.st_mode))
cb_timer_sys();
}
static void
cb_close(void *data)
{
Epplet_unremember();
Esync();
data = NULL;
exit(0);
}
@ -502,9 +489,6 @@ cb_in(void *data, Window w)
Epplet_gadget_show(b_suspend);
Epplet_gadget_show(b_sleep);
Epplet_gadget_show(b_help);
return;
data = NULL;
w = (Window) 0;
}
static void
@ -514,33 +498,24 @@ cb_out(void *data, Window w)
Epplet_gadget_hide(b_suspend);
Epplet_gadget_hide(b_sleep);
Epplet_gadget_hide(b_help);
return;
data = NULL;
w = (Window) 0;
}
static void
cb_help(void *data)
{
Epplet_show_about("E-Power");
return;
data = NULL;
}
static void
cb_suspend(void *data)
{
system("/usr/bin/apm -s");
return;
data = NULL;
}
static void
cb_sleep(void *data)
{
system("/usr/bin/apm -S");
return;
data = NULL;
}
int