Add battery tooltip support

This adds a new config option 'battery_tooltip' (enabled
by default), which can be used to enable a tooltip for
the battery widget providing details for all installed
batteries.
This commit is contained in:
Sebastian Reichel 2015-08-05 02:15:06 +02:00
parent ae375ae526
commit ffd659208a
4 changed files with 105 additions and 0 deletions

View file

@ -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;
@ -135,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;
@ -218,6 +220,13 @@ void init_battery()
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)
{
@ -242,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;
}

View file

@ -51,6 +51,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 +63,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();
@ -83,6 +98,7 @@ void battery_action(int button);
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

View file

@ -284,4 +284,76 @@ void update_linux_batteries(enum chargestate *state, gint64 *energy_now, gint64
}
}
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

View file

@ -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) {