kwo pushed a commit to branch master.
commit f126801893e95c049afeb1dd4ef97ea224fd156d
Author: Kim Woelders <[email protected]>
Date: Fri Jul 12 20:13:32 2013 +0200
E-Power: Some refactoring.
---
epplets/E-Power.c | 470 +++++++++++++++++++++++-------------------------------
1 file changed, 200 insertions(+), 270 deletions(-)
diff --git a/epplets/E-Power.c b/epplets/E-Power.c
index 43b8d0f..8173123 100644
--- a/epplets/E-Power.c
+++ b/epplets/E-Power.c
@@ -8,6 +8,25 @@
Added ACPI power management support.
*/
+typedef struct
+{
+ char design_cap_unknown;
+ char last_full_unknown;
+ char rate_unknown;
+ char level_unknown;
+
+ char discharging;
+ char charging;
+ char battery;
+
+ int bat_max;
+ int bat_filled;
+ int bat_level;
+ int bat_drain;
+} bat_info_t;
+
+typedef void (bi_fetch_f) (bat_info_t * bi);
+
static Epplet_gadget b_close, b_suspend, b_sleep, b_help, image, label;
static void
@@ -110,181 +129,185 @@ cb_timer_apm(void)
}
static void
-cb_timer_acpi(void)
+_bat_info_fetch_acpi(bat_info_t * bi)
{
- /* 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;
+ char *line = NULL;
size_t lsize = 0;
- int discharging = 0;
- int charging = 0;
- int battery = 0;
-
- int design_cap_unknown = 0;
- int last_full_unknown = 0;
- int rate_unknown = 0;
- int level_unknown = 0;
-
- int hours, minutes;
/* Read some information on first run. */
dirp = opendir("/proc/acpi/battery");
- if (dirp)
+ if (!dirp)
+ return;
+
+ while ((dp = readdir(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)
{
- 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, "%*[^:]: %250s %*s", buf);
- if (!strcmp(buf, "unknown"))
- design_cap_unknown = 1;
- else
- sscanf(line, "%*[^:]: %i %*s", &design_cap);
- getline(&line, &lsize, f);
- sscanf(line, "%*[^:]: %250s %*s", buf);
- if (!strcmp(buf, "unknown"))
- last_full_unknown = 1;
- else
- 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, "%*[^:]: %250s %*s", buf);
- if (!strcmp(buf, "unknown"))
- rate_unknown = 1;
- else
- sscanf(line, "%*[^:]: %i %*s", &rate);
- getline(&line, &lsize, f);
- sscanf(line, "%*[^:]: %250s %*s", buf);
- if (!strcmp(buf, "unknown"))
- level_unknown = 1;
- else
- 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;
- }
+ int design_cap = 0;
+ int last_full = 0;
+
+ getline(&line, &lsize, f);
+ getline(&line, &lsize, f);
+ sscanf(line, "%*[^:]: %250s %*s", buf);
+ if (!strcmp(buf, "unknown"))
+ bi->design_cap_unknown = 1;
+ else
+ sscanf(line, "%*[^:]: %i %*s", &design_cap);
+ getline(&line, &lsize, f);
+ sscanf(line, "%*[^:]: %250s %*s", buf);
+ if (!strcmp(buf, "unknown"))
+ bi->last_full_unknown = 1;
+ else
+ sscanf(line, "%*[^:]: %i %*s", &last_full);
+ fclose(f);
+ bi->bat_max += design_cap;
+ bi->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, "%*[^:]: %250s %*s", buf);
+ if (!strcmp(buf, "unknown"))
+ bi->rate_unknown = 1;
+ else
+ sscanf(line, "%*[^:]: %i %*s", &rate);
+ getline(&line, &lsize, f);
+ sscanf(line, "%*[^:]: %250s %*s", buf);
+ if (!strcmp(buf, "unknown"))
+ bi->level_unknown = 1;
+ else
+ sscanf(line, "%*[^:]: %i %*s", &level);
+ fclose(f);
+ if (!strcmp(present, "yes"))
+ bi->battery++;
+ if (!strcmp(charging_state, "discharging"))
+ bi->discharging++;
+ if (!strcmp(charging_state, "charging"))
+ bi->charging++;
+ bi->bat_drain += rate;
+ bi->bat_level += level;
}
- closedir(dirp);
}
- if (prev_bat_drain < 1)
- prev_bat_drain = 1;
- if (bat_drain < 1)
- bat_drain = prev_bat_drain;
- prev_bat_drain = bat_drain;
+ closedir(dirp);
+ free(line);
+}
- if (bat_filled > 0)
- bat_val = (100 * bat_level) / bat_filled;
- else
- bat_val = 100;
+static void
+_bat_info_fetch_sys(bat_info_t * bi)
+{
+ DIR *dirp;
+ struct dirent *dp;
+ FILE *f;
+ char *line = NULL;
+ size_t lsize = 0;
- 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);
+ /* Read some information on first run. */
+ dirp = opendir("/sys/class/power_supply/");
+ if (!dirp)
+ return;
- if (charging)
+ while ((dp = readdir(dirp)))
{
- if (level_unknown)
- snprintf(current_status, sizeof(current_status),
- "Level ???\n" "Bad Driver");
- else if (rate_unknown)
- snprintf(current_status, sizeof(current_status),
- "%i%% PWR\n" "Time ???", bat_val);
- else
- snprintf(current_status, sizeof(current_status),
- "%i%% PWR\n" "%02i:%02i", bat_val, hours, minutes);
- }
- else if (discharging)
- {
- if (level_unknown)
- snprintf(current_status, sizeof(current_status),
- "Level ???\n" "Bad Driver");
- else if (rate_unknown)
- snprintf(current_status, sizeof(current_status),
- "%i%%\n" "Time ???", bat_val);
- else
- 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");
+ char buf[4096];
- /* Display current status */
- Epplet_change_label(label, current_status);
- sprintf(current_status, "E-Power-Bat-%i.png", ((bat_val + 5) / 10) * 10);
- Epplet_change_image(image, 44, 24, current_status);
+ if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") ||
+ !strstr(dp->d_name, "BAT"))
+ continue;
- /* Final steps before ending the status update. */
- if (lsize)
- free(line);
+ snprintf(buf, sizeof(buf), "/sys/class/power_supply/%s/uevent",
+ dp->d_name);
+ f = fopen(buf, "r");
+ if (f)
+ {
+ int design_cap = 0;
+ int last_full = 0;
+ char present[256];
+ char key[256];
+ char charging_state[256];
+ char name[256];
+ int rate = 1;
+ int level = 0;
+
+ while (getline(&line, &lsize, f) != -1)
+ {
+ sscanf(line, "%[^=]= %250s", key, name);
+ if (strcmp(key, "POWER_SUPPLY_NAME") == 0)
+ {
+ }
+ else if (strcmp(key, "POWER_SUPPLY_STATUS") == 0)
+ {
+ sscanf(line, "%*[^=]= %250s", charging_state);
+ if (!strcmp(charging_state, "Discharging"))
+ bi->discharging++;
+ if (!strcmp(charging_state, "Charging"))
+ bi->charging++;
+ }
+ else if (strcmp(key, "POWER_SUPPLY_PRESENT") == 0)
+ {
+ sscanf(line, "%*[^=]= %250s", present);
+ if (!strcmp(present, "1"))
+ bi->battery++;
+ }
+ else if (strcmp(key, "POWER_SUPPLY_CURRENT_NOW") == 0)
+ {
+ sscanf(line, "%*[^=]= %i %*s", &rate);
+ bi->rate_unknown = 0;
+ bi->bat_drain += rate;
+ }
+ else if (strcmp(key, "POWER_SUPPLY_CHARGE_FULL_DESIGN") == 0)
+ {
+ sscanf(line, "%*[^=]=%i", &design_cap);
+ bi->design_cap_unknown = 0;
+ bi->bat_max += design_cap;
+ }
+ else if (strcmp(key, "POWER_SUPPLY_CHARGE_FULL") == 0)
+ {
+ sscanf(line, "%*[^=]= %i", &last_full);
+ bi->last_full_unknown = 0;
+ bi->bat_filled += last_full;
+ }
+ else if (strcmp(key, "POWER_SUPPLY_CHARGE_NOW") == 0)
+ {
+ sscanf(line, "%*[^=]= %i", &level);
+ bi->level_unknown = 0;
+ bi->bat_level += level;
+ }
+ }
+ fclose(f);
+ }
+ }
+ closedir(dirp);
+ free(line);
}
static void
-cb_timer_sys(void)
+cb_timer_gen(bi_fetch_f * bi_fetch)
{
/* We don't have any data from the remaining percentage, and time directly,
* so we have to calculate and measure them.
@@ -292,156 +315,67 @@ cb_timer_sys(void)
*/
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;
+ bat_info_t bi;
- int bat_val = 0;
+ int bat_val;
char current_status[256];
- char *line = 0;
- size_t lsize = 0;
- int discharging = 0;
- int charging = 0;
- int battery = 0;
-
- int design_cap_unknown = 1;
- int last_full_unknown = 1;
- int rate_unknown = 1;
- int level_unknown = 1;
int hours, minutes;
- /* Read some information on first run. */
- dirp = opendir("/sys/class/power_supply/");
- if (dirp)
- {
- while ((dp = readdir(dirp)))
- {
- char buf[4096];
-
- if ((!strcmp(dp->d_name, ".")) || (!strcmp(dp->d_name, ".."))
- || (!strstr(dp->d_name, "BAT")))
- continue;
- snprintf(buf, sizeof(buf), "/sys/class/power_supply/%s/uevent",
- dp->d_name);
- f = fopen(buf, "r");
- if (f)
- {
- int design_cap = 0;
- int last_full = 0;
- char present[256];
- char key[256];
- char charging_state[256];
- char name[256];
- int rate = 1;
- int level = 0;
-
- while (getline(&line, &lsize, f) != -1)
- {
- sscanf(line, "%[^=]= %250s", key, name);
- if (strcmp(key, "POWER_SUPPLY_NAME") == 0)
- {
- }
- else if (strcmp(key, "POWER_SUPPLY_STATUS") == 0)
- {
- sscanf(line, "%*[^=]= %250s", charging_state);
- if (!strcmp(charging_state, "Discharging"))
- discharging++;
- if (!strcmp(charging_state, "Charging"))
- charging++;
- }
- else if (strcmp(key, "POWER_SUPPLY_PRESENT") == 0)
- {
- sscanf(line, "%*[^=]= %250s", present);
- if (!strcmp(present, "1"))
- battery++;
- }
- else if (strcmp(key, "POWER_SUPPLY_CURRENT_NOW") == 0)
- {
- sscanf(line, "%*[^=]= %i %*s", &rate);
- rate_unknown = 0;
- bat_drain += (rate);
- }
- else if (strcmp(key, "POWER_SUPPLY_CHARGE_FULL_DESIGN")
- == 0)
- {
- sscanf(line, "%*[^=]=%i", &design_cap);
- design_cap_unknown = 0;
- bat_max += design_cap;
- }
- else if (strcmp(key, "POWER_SUPPLY_CHARGE_FULL") == 0)
- {
- sscanf(line, "%*[^=]= %i", &last_full);
- last_full_unknown = 0;
- bat_filled += last_full;
- }
- else if (strcmp(key, "POWER_SUPPLY_CHARGE_NOW") == 0)
- {
- sscanf(line, "%*[^=]= %i", &level);
- level_unknown = 0;
- bat_level += level;
- }
- }
- fclose(f);
- }
- }
- closedir(dirp);
- }
+ memset(&bi, 0, sizeof(bi));
+ bi.bat_drain = 1;
+
+ bi_fetch(&bi);
if (prev_bat_drain < 1)
prev_bat_drain = 1;
- if (bat_drain < 1)
- bat_drain = prev_bat_drain;
- prev_bat_drain = bat_drain;
+ if (bi.bat_drain < 1)
+ bi.bat_drain = prev_bat_drain;
+ prev_bat_drain = bi.bat_drain;
- if (bat_filled > 0)
- bat_val = (100 * bat_level) / bat_filled;
+ if (bi.bat_filled > 0)
+ bat_val = (100 * bi.bat_level) / bi.bat_filled;
else
bat_val = 100;
- if (discharging)
- minutes = (60 * bat_level) / bat_drain;
+ if (bi.discharging)
+ minutes = (60 * bi.bat_level) / bi.bat_drain;
else
{
- if (bat_filled > 0)
- minutes = (60 * (bat_filled - bat_level)) / bat_drain;
+ if (bi.bat_filled > 0)
+ minutes = (60 * (bi.bat_filled - bi.bat_level)) / bi.bat_drain;
else
minutes = 0;
}
hours = minutes / 60;
minutes -= (hours * 60);
- if (charging)
+ if (bi.charging)
{
- if (level_unknown)
+ if (bi.level_unknown)
snprintf(current_status, sizeof(current_status),
"Level ???\n" "Bad Driver");
- else if (rate_unknown)
+ else if (bi.rate_unknown)
snprintf(current_status, sizeof(current_status),
"%i%% PWR\n" "Time ???", bat_val);
else
snprintf(current_status, sizeof(current_status),
"%i%% PWR\n" "%02i:%02i", bat_val, hours, minutes);
}
- else if (discharging)
+ else if (bi.discharging)
{
- if (level_unknown)
+ if (bi.level_unknown)
snprintf(current_status, sizeof(current_status),
"Level ???\n" "Bad Driver");
- else if (rate_unknown)
+ else if (bi.rate_unknown)
snprintf(current_status, sizeof(current_status),
"%i%%\n" "Time ???", bat_val);
else
snprintf(current_status, sizeof(current_status),
"%i%%\n" "%02i:%02i", bat_val, hours, minutes);
}
- else if (!battery)
+ else if (!bi.battery)
snprintf(current_status, sizeof(current_status), "No Bat");
else
snprintf(current_status, sizeof(current_status), "Full");
@@ -450,10 +384,6 @@ cb_timer_sys(void)
Epplet_change_label(label, current_status);
sprintf(current_status, "E-Power-Bat-%i.png", ((bat_val + 5) / 10) * 10);
Epplet_change_image(image, 44, 24, current_status);
-
- /* Final steps before ending the status update. */
- if (lsize)
- free(line);
}
static void
@@ -464,9 +394,9 @@ cb_timer(void *data)
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();
+ cb_timer_gen(_bat_info_fetch_acpi);
else if ((stat("/sys/class/power_supply", &st) > -1) && S_ISDIR(st.st_mode))
- cb_timer_sys();
+ cb_timer_gen(_bat_info_fetch_sys);
Epplet_timer(cb_timer, NULL, 10.0, "TIMER");
}
--
------------------------------------------------------------------------------
Get your SQL database under version control now!
Version control is standard for application code, but databases havent
caught up. So what steps can you take to put your SQL databases under
version control? Why should you start doing it? Read more to find out.
http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk