Merge branch 'multi-battery-support' into 'master'
Multi battery support Hi , I have a couple of patches for the battery widget making it actually useful on my ThinkPad X250. -- Sebastian See merge request !6
This commit is contained in:
commit
aa47642161
8 changed files with 429 additions and 184 deletions
|
@ -78,7 +78,7 @@ set( SOURCES src/config.c
|
|||
src/util/window.c )
|
||||
|
||||
if( ENABLE_BATTERY )
|
||||
set( SOURCES ${SOURCES} src/battery/battery.c )
|
||||
set( SOURCES ${SOURCES} src/battery/battery.c src/battery/linux.c)
|
||||
add_definitions( -DENABLE_BATTERY )
|
||||
endif( ENABLE_BATTERY )
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
*
|
||||
* Tint2 : battery
|
||||
*
|
||||
* Copyright (C) 2009 Sebastian Reichel <elektranox@gmail.com>
|
||||
* Copyright (C) 2009-2015 Sebastian Reichel <sre@ring0.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License version 2
|
||||
|
@ -47,6 +47,7 @@ PangoFontDescription *bat1_font_desc;
|
|||
PangoFontDescription *bat2_font_desc;
|
||||
struct batstate battery_state;
|
||||
int battery_enabled;
|
||||
int battery_tooltip_enabled;
|
||||
int percentage_hide;
|
||||
static timeout* battery_timeout;
|
||||
|
||||
|
@ -61,10 +62,6 @@ char *battery_mclick_command;
|
|||
char *battery_rclick_command;
|
||||
char *battery_uwheel_command;
|
||||
char *battery_dwheel_command;
|
||||
gchar *path_energy_now;
|
||||
gchar *path_energy_full;
|
||||
gchar *path_current_now;
|
||||
gchar *path_status;
|
||||
int battery_found;
|
||||
|
||||
#if defined(__OpenBSD__) || defined(__NetBSD__)
|
||||
|
@ -139,6 +136,7 @@ void update_battery_tick(void* arg)
|
|||
void default_battery()
|
||||
{
|
||||
battery_enabled = 0;
|
||||
battery_tooltip_enabled = 1;
|
||||
battery_found = 0;
|
||||
percentage_hide = 101;
|
||||
battery_low_cmd_sent = 0;
|
||||
|
@ -151,10 +149,6 @@ void default_battery()
|
|||
battery_rclick_command = NULL;
|
||||
battery_uwheel_command = NULL;
|
||||
battery_dwheel_command = NULL;
|
||||
path_energy_now = NULL;
|
||||
path_energy_full = NULL;
|
||||
path_current_now = NULL;
|
||||
path_status = NULL;
|
||||
battery_state.percentage = 0;
|
||||
battery_state.time.hours = 0;
|
||||
battery_state.time.minutes = 0;
|
||||
|
@ -171,14 +165,6 @@ void cleanup_battery()
|
|||
bat1_font_desc = NULL;
|
||||
pango_font_description_free(bat2_font_desc);
|
||||
bat2_font_desc = NULL;
|
||||
g_free(path_energy_now);
|
||||
path_energy_now = NULL;
|
||||
g_free(path_energy_full);
|
||||
path_energy_full = NULL;
|
||||
g_free(path_current_now);
|
||||
path_current_now = NULL;
|
||||
g_free(path_status);
|
||||
path_status = NULL;
|
||||
free(battery_low_cmd);
|
||||
battery_low_cmd = NULL;
|
||||
free(battery_lclick_command);
|
||||
|
@ -199,6 +185,8 @@ void cleanup_battery()
|
|||
if ((apm_fd != -1) && (close(apm_fd) == -1))
|
||||
warn("cannot close /dev/apm");
|
||||
apm_fd = -1;
|
||||
#elif defined(__linux)
|
||||
free_linux_batteries();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -224,95 +212,21 @@ void init_battery()
|
|||
battery_found = (sysctlbyname("hw.acpi.battery.state", &sysctl_out, &len, NULL, 0) == 0) ||
|
||||
(sysctlbyname("hw.acpi.battery.time", &sysctl_out, &len, NULL, 0) == 0) ||
|
||||
(sysctlbyname("hw.acpi.battery.life", &sysctl_out, &len, NULL, 0) == 0);
|
||||
#else // Linux
|
||||
GDir *directory = 0;
|
||||
GError *error = NULL;
|
||||
const char *entryname;
|
||||
gchar *battery_dir = 0;
|
||||
|
||||
directory = g_dir_open("/sys/class/power_supply", 0, &error);
|
||||
if (error) {
|
||||
g_error_free(error);
|
||||
} else {
|
||||
while ((entryname = g_dir_read_name(directory))) {
|
||||
if (strncmp(entryname, "AC", 2) == 0)
|
||||
continue;
|
||||
|
||||
gchar *path1 = g_build_filename("/sys/class/power_supply", entryname, "present", NULL);
|
||||
if (g_file_test(path1, G_FILE_TEST_EXISTS)) {
|
||||
g_free(path1);
|
||||
battery_dir = g_build_filename("/sys/class/power_supply", entryname, NULL);
|
||||
break;
|
||||
}
|
||||
g_free(path1);
|
||||
}
|
||||
}
|
||||
if (directory)
|
||||
g_dir_close(directory);
|
||||
if (!battery_dir) {
|
||||
fprintf(stderr, "ERROR: battery applet cannot find any battery\n");
|
||||
battery_found = 0;
|
||||
} else {
|
||||
battery_found = 1;
|
||||
|
||||
g_free(path_energy_now);
|
||||
path_energy_now = g_build_filename(battery_dir, "energy_now", NULL);
|
||||
if (!g_file_test(path_energy_now, G_FILE_TEST_EXISTS)) {
|
||||
g_free(path_energy_now);
|
||||
path_energy_now = g_build_filename(battery_dir, "charge_now", NULL);
|
||||
}
|
||||
if (!g_file_test(path_energy_now, G_FILE_TEST_EXISTS)) {
|
||||
fprintf(stderr, "ERROR: battery applet cannot find energy_now nor charge_now\n");
|
||||
g_free(path_energy_now);
|
||||
path_energy_now = NULL;
|
||||
}
|
||||
|
||||
g_free(path_energy_full);
|
||||
path_energy_full = g_build_filename(battery_dir, "energy_full", NULL);
|
||||
if (!g_file_test(path_energy_full, G_FILE_TEST_EXISTS)) {
|
||||
g_free(path_energy_full);
|
||||
path_energy_full = g_build_filename(battery_dir, "charge_full", NULL);
|
||||
}
|
||||
if (!g_file_test(path_energy_full, G_FILE_TEST_EXISTS)) {
|
||||
fprintf(stderr, "ERROR: battery applet cannot find energy_now nor charge_now\n");
|
||||
g_free(path_energy_full);
|
||||
path_energy_full = NULL;
|
||||
}
|
||||
|
||||
g_free(path_current_now);
|
||||
path_current_now = g_build_filename(battery_dir, "power_now", NULL);
|
||||
if (!g_file_test(path_current_now, G_FILE_TEST_EXISTS)) {
|
||||
g_free(path_current_now);
|
||||
path_current_now = g_build_filename(battery_dir, "current_now", NULL);
|
||||
}
|
||||
if (!g_file_test(path_current_now, G_FILE_TEST_EXISTS)) {
|
||||
fprintf(stderr, "ERROR: battery applet cannot find power_now nor current_now\n");
|
||||
g_free(path_current_now);
|
||||
path_current_now = NULL;
|
||||
}
|
||||
|
||||
g_free(path_status);
|
||||
path_status = g_build_filename(battery_dir, "status", NULL);
|
||||
if (!g_file_test(path_status, G_FILE_TEST_EXISTS)) {
|
||||
fprintf(stderr, "ERROR: battery applet cannot find battery status\n");
|
||||
g_free(path_status);
|
||||
path_status = NULL;
|
||||
}
|
||||
|
||||
g_free(battery_dir);
|
||||
battery_dir = NULL;
|
||||
}
|
||||
|
||||
if (!path_status) {
|
||||
battery_found = 0;
|
||||
fprintf(stderr, "ERROR: battery applet cannot find any batteries\n");
|
||||
}
|
||||
#elif defined(__linux)
|
||||
battery_found = init_linux_batteries();
|
||||
#endif
|
||||
|
||||
if (!battery_timeout)
|
||||
battery_timeout = add_timeout(10, 30000, update_battery_tick, 0, &battery_timeout);
|
||||
}
|
||||
|
||||
const char* battery_get_tooltip(void* obj) {
|
||||
#if defined(__linux)
|
||||
return linux_batteries_get_tooltip();
|
||||
#else
|
||||
return g_strdup("No tooltip support for this OS!");
|
||||
#endif
|
||||
}
|
||||
|
||||
void init_battery_panel(void *p)
|
||||
{
|
||||
|
@ -337,6 +251,9 @@ void init_battery_panel(void *p)
|
|||
battery->area._resize = resize_battery;
|
||||
battery->area.on_screen = 1;
|
||||
battery->area.resize = 1;
|
||||
|
||||
if (battery_tooltip_enabled)
|
||||
battery->area._get_tooltip_text = battery_get_tooltip;
|
||||
}
|
||||
|
||||
|
||||
|
@ -420,82 +337,7 @@ int update_battery() {
|
|||
else
|
||||
new_percentage = sysctl_out;
|
||||
#else
|
||||
FILE *fp = NULL;
|
||||
char tmp[25] = "";
|
||||
int64_t current_now = 0;
|
||||
if (path_status) {
|
||||
fp = fopen(path_status, "r");
|
||||
if (fp != NULL) {
|
||||
if (fgets(tmp, sizeof(tmp), fp)) {
|
||||
if (strcasecmp(tmp, "Charging\n") == 0)
|
||||
battery_state.state = BATTERY_CHARGING;
|
||||
if (strcasecmp(tmp, "Discharging\n") == 0)
|
||||
battery_state.state = BATTERY_DISCHARGING;
|
||||
if (strcasecmp(tmp, "Full\n") == 0)
|
||||
battery_state.state = BATTERY_FULL;
|
||||
}
|
||||
fclose(fp);
|
||||
} else {
|
||||
errors = 1;
|
||||
}
|
||||
} else {
|
||||
errors = 1;
|
||||
}
|
||||
|
||||
if (path_energy_now) {
|
||||
fp = fopen(path_energy_now, "r");
|
||||
if (fp != NULL) {
|
||||
if (fgets(tmp, sizeof tmp, fp))
|
||||
energy_now = atoi(tmp);
|
||||
fclose(fp);
|
||||
} else {
|
||||
errors = 1;
|
||||
}
|
||||
} else {
|
||||
errors = 1;
|
||||
}
|
||||
|
||||
if (path_energy_full) {
|
||||
fp = fopen(path_energy_full, "r");
|
||||
if (fp != NULL) {
|
||||
if (fgets(tmp, sizeof tmp, fp))
|
||||
energy_full = atoi(tmp);
|
||||
fclose(fp);
|
||||
} else {
|
||||
errors = 1;
|
||||
}
|
||||
} else {
|
||||
errors = 1;
|
||||
}
|
||||
|
||||
if (path_current_now) {
|
||||
fp = fopen(path_current_now, "r");
|
||||
if (fp != NULL) {
|
||||
if (fgets(tmp, sizeof tmp, fp))
|
||||
current_now = atoi(tmp);
|
||||
fclose(fp);
|
||||
} else {
|
||||
errors = 1;
|
||||
}
|
||||
} else {
|
||||
errors = 1;
|
||||
}
|
||||
|
||||
if (current_now > 0) {
|
||||
switch (battery_state.state) {
|
||||
case BATTERY_CHARGING:
|
||||
seconds = 3600 * (energy_full - energy_now) / current_now;
|
||||
break;
|
||||
case BATTERY_DISCHARGING:
|
||||
seconds = 3600 * energy_now / current_now;
|
||||
break;
|
||||
default:
|
||||
seconds = 0;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
seconds = 0;
|
||||
}
|
||||
update_linux_batteries(&battery_state.state, &energy_now, &energy_full, &seconds);
|
||||
#endif
|
||||
|
||||
battery_state.time.hours = seconds / 3600;
|
||||
|
@ -616,4 +458,4 @@ void battery_action(int button)
|
|||
break;
|
||||
}
|
||||
tint_exec(command);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
/**************************************************************************
|
||||
* Copyright (C) 2009 Sebastian Reichel <elektranox@gmail.com>
|
||||
* Copyright (C) 2009-2015 Sebastian Reichel <sre@ring0.de>
|
||||
*
|
||||
* Battery with functional data (percentage, time to life) and drawing data
|
||||
* (area, font, ...). Each panel use his own drawing data.
|
||||
* Need kernel > 2.6.23.
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
|
@ -51,6 +50,7 @@ extern struct batstate battery_state;
|
|||
extern PangoFontDescription *bat1_font_desc;
|
||||
extern PangoFontDescription *bat2_font_desc;
|
||||
extern int battery_enabled;
|
||||
extern int battery_tooltip_enabled;
|
||||
extern int percentage_hide;
|
||||
|
||||
extern int8_t battery_low_status;
|
||||
|
@ -62,6 +62,20 @@ extern char *battery_rclick_command;
|
|||
extern char *battery_uwheel_command;
|
||||
extern char *battery_dwheel_command;
|
||||
|
||||
static inline gchar* chargestate2str(enum chargestate state) {
|
||||
switch(state) {
|
||||
case BATTERY_CHARGING:
|
||||
return "Charging";
|
||||
case BATTERY_DISCHARGING:
|
||||
return "Discharging";
|
||||
case BATTERY_FULL:
|
||||
return "Full";
|
||||
case BATTERY_UNKNOWN:
|
||||
default:
|
||||
return "Unknown";
|
||||
};
|
||||
}
|
||||
|
||||
// default global data
|
||||
void default_battery();
|
||||
|
||||
|
@ -79,4 +93,11 @@ int resize_battery(void *obj);
|
|||
|
||||
void battery_action(int button);
|
||||
|
||||
#ifdef __linux
|
||||
gboolean init_linux_batteries();
|
||||
void free_linux_batteries();
|
||||
void update_linux_batteries(enum chargestate *state, gint64 *energy_now, gint64 *energy_full, int *seconds);
|
||||
const char* linux_batteries_get_tooltip();
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
359
src/battery/linux.c
Normal file
359
src/battery/linux.c
Normal file
|
@ -0,0 +1,359 @@
|
|||
/**************************************************************************
|
||||
*
|
||||
* Tint2 : Linux battery
|
||||
*
|
||||
* Copyright (C) 2015 Sebastian Reichel <sre@ring0.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License version 2
|
||||
* or any later version as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**************************************************************************/
|
||||
|
||||
#ifdef __linux
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "battery.h"
|
||||
|
||||
struct psy_battery {
|
||||
/* generic properties */
|
||||
gchar* name;
|
||||
/* sysfs files */
|
||||
gchar* path_present;
|
||||
gchar* path_energy_now;
|
||||
gchar* path_energy_full;
|
||||
gchar* path_power_now;
|
||||
gchar* path_status;
|
||||
/* sysfs hints */
|
||||
gboolean energy_in_uamp;
|
||||
gboolean power_in_uamp;
|
||||
/* values */
|
||||
gboolean present;
|
||||
gint energy_now;
|
||||
gint energy_full;
|
||||
gint power_now;
|
||||
enum chargestate status;
|
||||
};
|
||||
|
||||
#define RETURN_ON_ERROR(err) if(error) { g_error_free(err); return FALSE; }
|
||||
|
||||
static GList *batteries = NULL;
|
||||
|
||||
static gboolean power_supply_is_battery(const gchar *entryname) {
|
||||
gchar *path_type = g_build_filename("/sys/class/power_supply", entryname, "type", NULL);
|
||||
GError *error = NULL;
|
||||
gchar *type;
|
||||
gsize typelen;
|
||||
|
||||
g_file_get_contents(path_type, &type, &typelen, &error);
|
||||
g_free(path_type);
|
||||
RETURN_ON_ERROR(error);
|
||||
|
||||
if(g_strcmp0(type, "Battery\n")) {
|
||||
g_free(type);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
g_free(type);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean init_linux_battery(struct psy_battery *bat) {
|
||||
const gchar *entryname = bat->name;
|
||||
|
||||
bat->energy_in_uamp = FALSE;
|
||||
bat->power_in_uamp = FALSE;
|
||||
|
||||
bat->path_present = g_build_filename("/sys/class/power_supply", entryname, "present", NULL);
|
||||
if (!g_file_test(bat->path_present, G_FILE_TEST_EXISTS)) {
|
||||
goto err0;
|
||||
}
|
||||
|
||||
bat->path_energy_now = g_build_filename("/sys/class/power_supply", entryname, "energy_now", NULL);
|
||||
if (!g_file_test(bat->path_energy_now, G_FILE_TEST_EXISTS)) {
|
||||
g_free(bat->path_energy_now);
|
||||
bat->path_energy_now = g_build_filename("/sys/class/power_supply", entryname, "charge_now", NULL);
|
||||
bat->energy_in_uamp = TRUE;
|
||||
}
|
||||
if (!g_file_test(bat->path_energy_now, G_FILE_TEST_EXISTS)) {
|
||||
goto err1;
|
||||
}
|
||||
|
||||
if(!bat->energy_in_uamp) {
|
||||
bat->path_energy_full = g_build_filename("/sys/class/power_supply", entryname, "energy_full", NULL);
|
||||
if (!g_file_test(bat->path_energy_full, G_FILE_TEST_EXISTS))
|
||||
goto err2;
|
||||
} else {
|
||||
bat->path_energy_full = g_build_filename("/sys/class/power_supply", entryname, "charge_full", NULL);
|
||||
if (!g_file_test(bat->path_energy_full, G_FILE_TEST_EXISTS))
|
||||
goto err2;
|
||||
}
|
||||
|
||||
bat->path_power_now = g_build_filename("/sys/class/power_supply", entryname, "power_now", NULL);
|
||||
if (!g_file_test(bat->path_power_now, G_FILE_TEST_EXISTS)) {
|
||||
g_free(bat->path_power_now);
|
||||
bat->path_power_now = g_build_filename("/sys/class/power_supply", entryname, "current_now", NULL);
|
||||
bat->power_in_uamp = TRUE;
|
||||
}
|
||||
if (!g_file_test(bat->path_power_now, G_FILE_TEST_EXISTS)) {
|
||||
goto err3;
|
||||
}
|
||||
|
||||
bat->path_status = g_build_filename("/sys/class/power_supply", entryname, "status", NULL);
|
||||
if (!g_file_test(bat->path_status, G_FILE_TEST_EXISTS)) {
|
||||
goto err4;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
|
||||
err4:
|
||||
g_free(bat->path_status);
|
||||
err3:
|
||||
g_free(bat->path_power_now);
|
||||
err2:
|
||||
g_free(bat->path_energy_full);
|
||||
err1:
|
||||
g_free(bat->path_energy_now);
|
||||
err0:
|
||||
g_free(bat->path_present);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void free_linux_batteries() {
|
||||
GList *l = batteries;
|
||||
|
||||
while (l != NULL) {
|
||||
GList *next = l->next;
|
||||
struct psy_battery *bat = l->data;
|
||||
|
||||
g_free(bat->name);
|
||||
g_free(bat->path_status);
|
||||
g_free(bat->path_power_now);
|
||||
g_free(bat->path_energy_full);
|
||||
g_free(bat->path_energy_now);
|
||||
g_free(bat->path_present);
|
||||
|
||||
batteries = g_list_delete_link(batteries, l);
|
||||
l = next;
|
||||
}
|
||||
|
||||
batteries = NULL;
|
||||
}
|
||||
|
||||
gboolean init_linux_batteries() {
|
||||
GDir *directory = 0;
|
||||
GError *error = NULL;
|
||||
const char *entryname;
|
||||
|
||||
free_linux_batteries();
|
||||
|
||||
directory = g_dir_open("/sys/class/power_supply", 0, &error);
|
||||
RETURN_ON_ERROR(error);
|
||||
|
||||
while ((entryname = g_dir_read_name(directory))) {
|
||||
if(!power_supply_is_battery(entryname))
|
||||
continue;
|
||||
|
||||
struct psy_battery *bat = g_malloc0(sizeof(*bat));
|
||||
bat->name = g_strdup(entryname);
|
||||
if(init_linux_battery(bat)) {
|
||||
batteries = g_list_append(batteries, bat);
|
||||
fprintf(stdout, "found battery \"%s\"\n", bat->name);
|
||||
} else {
|
||||
g_free(bat);
|
||||
fprintf(stderr, RED "failed to initialize battery \"%s\"\n" RESET, entryname);
|
||||
}
|
||||
}
|
||||
|
||||
g_dir_close(directory);
|
||||
|
||||
return batteries != NULL;
|
||||
}
|
||||
|
||||
static gboolean update_linux_battery(struct psy_battery *bat) {
|
||||
GError *error = NULL;
|
||||
gchar *data;
|
||||
gsize datalen;
|
||||
|
||||
/* reset values */
|
||||
bat->present = 0;
|
||||
bat->status = BATTERY_UNKNOWN;
|
||||
bat->energy_now = 0;
|
||||
bat->energy_full = 0;
|
||||
bat->power_now = 0;
|
||||
|
||||
/* present */
|
||||
g_file_get_contents(bat->path_present, &data, &datalen, &error);
|
||||
RETURN_ON_ERROR(error);
|
||||
bat->present = (atoi(data) == 1);
|
||||
g_free(data);
|
||||
|
||||
/* we are done, if battery is not present */
|
||||
if(!bat->present)
|
||||
return TRUE;
|
||||
|
||||
/* status */
|
||||
bat->status = BATTERY_UNKNOWN;
|
||||
g_file_get_contents(bat->path_status, &data, &datalen, &error);
|
||||
RETURN_ON_ERROR(error);
|
||||
if (!g_strcmp0(data, "Charging\n")) {
|
||||
bat->status = BATTERY_CHARGING;
|
||||
} else if (!g_strcmp0(data, "Discharging\n")) {
|
||||
bat->status = BATTERY_DISCHARGING;
|
||||
} else if (!g_strcmp0(data, "Full\n")) {
|
||||
bat->status = BATTERY_FULL;
|
||||
}
|
||||
g_free(data);
|
||||
|
||||
/* energy now */
|
||||
g_file_get_contents(bat->path_energy_now, &data, &datalen, &error);
|
||||
RETURN_ON_ERROR(error);
|
||||
bat->energy_now = atoi(data);
|
||||
g_free(data);
|
||||
|
||||
/* energy full */
|
||||
g_file_get_contents(bat->path_energy_full, &data, &datalen, &error);
|
||||
RETURN_ON_ERROR(error);
|
||||
bat->energy_full = atoi(data);
|
||||
g_free(data);
|
||||
|
||||
/* power now */
|
||||
g_file_get_contents(bat->path_power_now, &data, &datalen, &error);
|
||||
RETURN_ON_ERROR(error);
|
||||
bat->power_now = atoi(data);
|
||||
g_free(data);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void update_linux_batteries(enum chargestate *state, gint64 *energy_now, gint64 *energy_full, int *seconds) {
|
||||
GList *l;
|
||||
|
||||
gint64 total_energy_now = 0;
|
||||
gint64 total_energy_full = 0;
|
||||
gint64 total_power_now = 0;
|
||||
|
||||
gboolean charging = FALSE;
|
||||
gboolean discharging = FALSE;
|
||||
gboolean full = FALSE;
|
||||
|
||||
for (l = batteries; l != NULL; l = l->next) {
|
||||
struct psy_battery *bat = l->data;
|
||||
update_linux_battery(bat);
|
||||
|
||||
total_energy_now += bat->energy_now;
|
||||
total_energy_full += bat->energy_full;
|
||||
total_power_now += bat->power_now;
|
||||
|
||||
charging |= (bat->status == BATTERY_CHARGING);
|
||||
discharging |= (bat->status == BATTERY_DISCHARGING);
|
||||
full |= (bat->status == BATTERY_FULL);
|
||||
}
|
||||
|
||||
/* global energy stats */
|
||||
*energy_now = total_energy_now;
|
||||
*energy_full = total_energy_full;
|
||||
|
||||
/* build global state */
|
||||
*state = BATTERY_UNKNOWN;
|
||||
if (charging && !discharging)
|
||||
*state = BATTERY_CHARGING;
|
||||
else if (!charging && discharging)
|
||||
*state = BATTERY_DISCHARGING;
|
||||
else if (!charging && !discharging && full)
|
||||
*state = BATTERY_FULL;
|
||||
|
||||
/* calculate seconds */
|
||||
*seconds = 0;
|
||||
if (total_power_now > 0) {
|
||||
if(*state == BATTERY_CHARGING)
|
||||
*seconds = 3600 * (total_energy_full - total_energy_now) / total_power_now;
|
||||
else if(*state == BATTERY_DISCHARGING)
|
||||
*seconds = 3600 * total_energy_now / total_power_now;
|
||||
}
|
||||
}
|
||||
|
||||
static gchar* energy_human_readable(struct psy_battery *bat) {
|
||||
gint now = bat->energy_now;
|
||||
gint full = bat->energy_full;
|
||||
gchar unit = bat->energy_in_uamp ? 'A' : 'W';
|
||||
|
||||
if (full >= 1000000) {
|
||||
return g_strdup_printf("%d.%d / %d.%d %ch",
|
||||
now / 1000000, (now % 1000000) / 100000,
|
||||
full / 1000000, (full % 1000000) / 100000,
|
||||
unit);
|
||||
} else if (full >= 1000) {
|
||||
return g_strdup_printf("%d.%d / %d.%d m%ch",
|
||||
now / 1000, (now % 1000) / 100,
|
||||
full / 1000, (full % 1000) / 100,
|
||||
unit);
|
||||
} else {
|
||||
return g_strdup_printf("%d / %d µ%ch", now, full, unit);
|
||||
}
|
||||
}
|
||||
|
||||
static gchar* power_human_readable(struct psy_battery *bat) {
|
||||
gint power = bat->power_now;
|
||||
gchar unit = bat->power_in_uamp ? 'A' : 'W';
|
||||
|
||||
if (power >= 1000000) {
|
||||
return g_strdup_printf("%d.%d %c", power / 1000000, (power % 1000000) / 100000, unit);
|
||||
} else if (power >= 1000) {
|
||||
return g_strdup_printf("%d.%d m%c", power / 1000, (power % 1000) / 100, unit);
|
||||
} else if (power > 0) {
|
||||
return g_strdup_printf("%d µ%c", power, unit);
|
||||
} else {
|
||||
return g_strdup_printf("0 %c", unit);
|
||||
}
|
||||
}
|
||||
|
||||
const char* linux_batteries_get_tooltip() {
|
||||
GList *l;
|
||||
GString *tooltip = g_string_new("");
|
||||
gchar *result;
|
||||
|
||||
for (l = batteries; l != NULL; l = l->next) {
|
||||
struct psy_battery *bat = l->data;
|
||||
|
||||
if (tooltip->len)
|
||||
g_string_append_c(tooltip, '\n');
|
||||
|
||||
g_string_append_printf(tooltip, "%s\n", bat->name);
|
||||
|
||||
if (!bat->present) {
|
||||
g_string_append_printf(tooltip, "\tnot connected");
|
||||
continue;
|
||||
}
|
||||
|
||||
gchar *power = power_human_readable(bat);
|
||||
gchar *energy = energy_human_readable(bat);
|
||||
gchar *state = (bat->status == BATTERY_UNKNOWN) ? "Level" : chargestate2str(bat->status);
|
||||
|
||||
guint percentage = 0.5 + ((bat->energy_now <= bat->energy_full ? bat->energy_now : bat->energy_full) * 100.0) / bat->energy_full;
|
||||
|
||||
g_string_append_printf(tooltip, "\t%s: %s (%u %%)\n\tPower: %s",
|
||||
state, energy, percentage, power);
|
||||
|
||||
g_free(power);
|
||||
g_free(energy);
|
||||
}
|
||||
|
||||
result = tooltip->str;
|
||||
g_string_free(tooltip, FALSE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -434,6 +434,11 @@ void add_entry (char *key, char *value)
|
|||
percentage_hide = 101;
|
||||
#endif
|
||||
}
|
||||
else if (strcmp (key, "battery_tooltip") == 0) {
|
||||
#ifdef ENABLE_BATTERY
|
||||
battery_tooltip_enabled = atoi(value);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Clock */
|
||||
else if (strcmp (key, "time1_format") == 0) {
|
||||
|
|
|
@ -92,6 +92,7 @@ GtkWidget *clock_background;
|
|||
GtkWidget *battery_hide_if_higher, *battery_alert_if_lower, *battery_alert_cmd;
|
||||
GtkWidget *battery_padding_x, *battery_padding_y, *battery_font_line1, *battery_font_line2, *battery_font_color;
|
||||
GtkWidget *battery_background;
|
||||
GtkWidget *battery_tooltip;
|
||||
GtkWidget *battery_left_command, *battery_mclick_command, *battery_right_command, *battery_uwheel_command, *battery_dwheel_command;
|
||||
|
||||
// systray
|
||||
|
@ -3829,6 +3830,20 @@ void create_battery(GtkWidget *parent)
|
|||
gtk_table_set_col_spacings(GTK_TABLE(table), COL_SPACING);
|
||||
row = 0, col = 2;
|
||||
|
||||
label = gtk_label_new(_("Tooltips"));
|
||||
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
|
||||
gtk_widget_show(label);
|
||||
gtk_table_attach(GTK_TABLE(table), label, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
|
||||
col++;
|
||||
|
||||
battery_tooltip = gtk_check_button_new();
|
||||
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(battery_tooltip), 1);
|
||||
gtk_widget_show(battery_tooltip);
|
||||
gtk_table_attach(GTK_TABLE(table), battery_tooltip, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
|
||||
col++;
|
||||
gtk_tooltips_set_tip(tooltips, battery_tooltip, _("If enabled, shows a tooltip with detailed battery information when the mouse is moved over the battery widget."), NULL);
|
||||
|
||||
row++, col = 2;
|
||||
label = gtk_label_new(_("Left click command"));
|
||||
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
|
||||
gtk_widget_show(label);
|
||||
|
|
|
@ -95,6 +95,7 @@ extern GtkWidget *clock_background;
|
|||
extern GtkWidget *battery_hide_if_higher, *battery_alert_if_lower, *battery_alert_cmd;
|
||||
extern GtkWidget *battery_padding_x, *battery_padding_y, *battery_font_line1, *battery_font_line2, *battery_font_color;
|
||||
extern GtkWidget *battery_background;
|
||||
extern GtkWidget *battery_tooltip;
|
||||
extern GtkWidget *battery_left_command, *battery_mclick_command, *battery_right_command, *battery_uwheel_command, *battery_dwheel_command;
|
||||
|
||||
// systray
|
||||
|
|
|
@ -560,6 +560,7 @@ void config_write_battery(FILE *fp)
|
|||
fprintf(fp, "#-------------------------------------\n");
|
||||
fprintf(fp, "# Battery\n");
|
||||
|
||||
fprintf(fp, "battery_tooltip = %d\n", gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(battery_tooltip)) ? 1 : 0);
|
||||
fprintf(fp, "battery_low_status = %g\n", gtk_spin_button_get_value(GTK_SPIN_BUTTON(battery_alert_if_lower)));
|
||||
fprintf(fp, "battery_low_cmd = %s\n", gtk_entry_get_text(GTK_ENTRY(battery_alert_cmd)));
|
||||
fprintf(fp, "bat1_font = %s\n", gtk_font_button_get_font_name(GTK_FONT_BUTTON(battery_font_line1)));
|
||||
|
@ -898,12 +899,13 @@ void add_entry(char *key, char *value)
|
|||
}
|
||||
|
||||
/* Battery */
|
||||
else if (strcmp(key, "systray") == 0) {
|
||||
else if (strcmp(key, "battery") == 0) {
|
||||
// Obsolete option
|
||||
config_has_battery = 1;
|
||||
config_battery_enabled = atoi(value);
|
||||
}
|
||||
else if (strcmp(key, "battery_low_status") == 0) {
|
||||
} else if (strcmp(key, "battery_tooltip") == 0) {
|
||||
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(battery_tooltip), atoi(value));
|
||||
} else if (strcmp(key, "battery_low_status") == 0) {
|
||||
gtk_spin_button_set_value(GTK_SPIN_BUTTON(battery_alert_if_lower), atof(value));
|
||||
}
|
||||
else if (strcmp(key, "battery_low_cmd") == 0) {
|
||||
|
@ -1464,4 +1466,4 @@ char *get_action(GtkWidget *combo)
|
|||
if (gtk_combo_box_get_active(GTK_COMBO_BOX(combo)) == 10)
|
||||
return "prev_task";
|
||||
return "none";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue