2009-05-15 20:48:55 +00:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
2015-08-07 02:31:31 +00:00
|
|
|
* Tint2 : Generic battery
|
2009-05-15 20:48:55 +00:00
|
|
|
*
|
2015-08-05 00:25:54 +00:00
|
|
|
* Copyright (C) 2009-2015 Sebastian Reichel <sre@ring0.de>
|
2009-05-15 20:48:55 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <cairo.h>
|
|
|
|
#include <cairo-xlib.h>
|
|
|
|
#include <pango/pangocairo.h>
|
|
|
|
|
|
|
|
#include "window.h"
|
|
|
|
#include "server.h"
|
2009-10-18 17:54:09 +00:00
|
|
|
#include "panel.h"
|
2009-05-15 20:48:55 +00:00
|
|
|
#include "battery.h"
|
2009-11-15 16:55:50 +00:00
|
|
|
#include "timer.h"
|
2010-06-11 18:47:26 +00:00
|
|
|
#include "common.h"
|
2009-05-15 20:48:55 +00:00
|
|
|
|
2010-04-18 12:07:36 +00:00
|
|
|
PangoFontDescription *bat1_font_desc;
|
|
|
|
PangoFontDescription *bat2_font_desc;
|
2009-05-15 20:48:55 +00:00
|
|
|
struct batstate battery_state;
|
2009-10-28 23:01:32 +00:00
|
|
|
int battery_enabled;
|
2015-08-05 00:15:06 +00:00
|
|
|
int battery_tooltip_enabled;
|
2010-04-16 18:50:03 +00:00
|
|
|
int percentage_hide;
|
2010-04-18 12:07:36 +00:00
|
|
|
static timeout* battery_timeout;
|
2009-05-15 20:48:55 +00:00
|
|
|
|
|
|
|
static char buf_bat_percentage[10];
|
|
|
|
static char buf_bat_time[20];
|
|
|
|
|
|
|
|
int8_t battery_low_status;
|
2015-04-09 09:11:57 +00:00
|
|
|
unsigned char battery_low_cmd_sent;
|
2015-08-07 03:20:24 +00:00
|
|
|
char *ac_connected_cmd;
|
|
|
|
char *ac_disconnected_cmd;
|
2010-04-18 12:07:36 +00:00
|
|
|
char *battery_low_cmd;
|
2015-07-13 03:16:02 +00:00
|
|
|
char *battery_lclick_command;
|
|
|
|
char *battery_mclick_command;
|
|
|
|
char *battery_rclick_command;
|
|
|
|
char *battery_uwheel_command;
|
|
|
|
char *battery_dwheel_command;
|
2015-03-28 18:08:44 +00:00
|
|
|
int battery_found;
|
2009-05-31 12:40:40 +00:00
|
|
|
|
2015-03-28 18:08:44 +00:00
|
|
|
void update_battery_tick(void* arg)
|
2009-11-15 16:55:50 +00:00
|
|
|
{
|
2015-03-28 18:08:44 +00:00
|
|
|
if (!battery_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int old_found = battery_found;
|
2010-09-21 22:07:11 +00:00
|
|
|
int old_percentage = battery_state.percentage;
|
2015-08-07 06:07:51 +00:00
|
|
|
gboolean old_ac_connected = battery_state.ac_connected;
|
2010-09-21 22:07:11 +00:00
|
|
|
int16_t old_hours = battery_state.time.hours;
|
|
|
|
int8_t old_minutes = battery_state.time.minutes;
|
|
|
|
|
2015-05-08 21:35:44 +00:00
|
|
|
if (!battery_found) {
|
|
|
|
init_battery();
|
2015-08-08 04:39:45 +00:00
|
|
|
old_ac_connected = battery_state.ac_connected;
|
2015-05-08 21:35:44 +00:00
|
|
|
}
|
2015-03-28 18:08:44 +00:00
|
|
|
if (update_battery() != 0) {
|
2015-08-08 04:39:45 +00:00
|
|
|
// Try to reconfigure on failed update
|
2015-03-28 18:08:44 +00:00
|
|
|
init_battery();
|
|
|
|
}
|
2015-08-07 03:20:24 +00:00
|
|
|
|
|
|
|
if (old_ac_connected != battery_state.ac_connected) {
|
2015-08-08 06:51:44 +00:00
|
|
|
if (battery_state.ac_connected)
|
2015-08-07 03:20:24 +00:00
|
|
|
tint_exec(ac_connected_cmd);
|
|
|
|
else
|
|
|
|
tint_exec(ac_disconnected_cmd);
|
|
|
|
}
|
|
|
|
|
2015-03-28 18:08:44 +00:00
|
|
|
if (old_found == battery_found &&
|
|
|
|
old_percentage == battery_state.percentage &&
|
|
|
|
old_hours == battery_state.time.hours &&
|
|
|
|
old_minutes == battery_state.time.minutes) {
|
2010-09-21 22:07:11 +00:00
|
|
|
return;
|
2015-03-28 18:08:44 +00:00
|
|
|
}
|
2010-09-21 22:07:11 +00:00
|
|
|
|
2015-04-09 09:11:57 +00:00
|
|
|
if (battery_state.percentage < battery_low_status &&
|
|
|
|
battery_state.state == BATTERY_DISCHARGING &&
|
|
|
|
!battery_low_cmd_sent) {
|
|
|
|
tint_exec(battery_low_cmd);
|
|
|
|
battery_low_cmd_sent = 1;
|
|
|
|
}
|
|
|
|
if (battery_state.percentage > battery_low_status &&
|
|
|
|
battery_state.state == BATTERY_CHARGING &&
|
|
|
|
battery_low_cmd_sent) {
|
|
|
|
battery_low_cmd_sent = 0;
|
|
|
|
}
|
|
|
|
|
2010-09-21 22:07:11 +00:00
|
|
|
int i;
|
2015-03-28 18:08:44 +00:00
|
|
|
for (i = 0; i < nb_panel; i++) {
|
2015-05-08 21:35:44 +00:00
|
|
|
if (!battery_found) {
|
|
|
|
if (panel1[i].battery.area.on_screen == 1) {
|
|
|
|
hide(&panel1[i].battery.area);
|
|
|
|
panel_refresh = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (battery_state.percentage >= percentage_hide) {
|
|
|
|
if (panel1[i].battery.area.on_screen == 1) {
|
|
|
|
hide(&panel1[i].battery.area);
|
|
|
|
panel_refresh = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (panel1[i].battery.area.on_screen == 0) {
|
|
|
|
show(&panel1[i].battery.area);
|
|
|
|
panel_refresh = 1;
|
|
|
|
}
|
|
|
|
}
|
2010-01-19 19:29:28 +00:00
|
|
|
}
|
2010-09-18 14:58:00 +00:00
|
|
|
if (panel1[i].battery.area.on_screen == 1) {
|
|
|
|
panel1[i].battery.area.resize = 1;
|
|
|
|
panel_refresh = 1;
|
|
|
|
}
|
2010-01-19 19:29:28 +00:00
|
|
|
}
|
2009-11-15 16:55:50 +00:00
|
|
|
}
|
|
|
|
|
2010-04-18 12:07:36 +00:00
|
|
|
void default_battery()
|
|
|
|
{
|
|
|
|
battery_enabled = 0;
|
2015-08-05 00:15:06 +00:00
|
|
|
battery_tooltip_enabled = 1;
|
2015-03-28 18:08:44 +00:00
|
|
|
battery_found = 0;
|
2010-04-18 12:07:36 +00:00
|
|
|
percentage_hide = 101;
|
2015-04-09 09:11:57 +00:00
|
|
|
battery_low_cmd_sent = 0;
|
2015-04-11 09:51:10 +00:00
|
|
|
battery_timeout = NULL;
|
|
|
|
bat1_font_desc = NULL;
|
|
|
|
bat2_font_desc = NULL;
|
|
|
|
battery_low_cmd = NULL;
|
2015-07-13 03:16:02 +00:00
|
|
|
battery_lclick_command = NULL;
|
|
|
|
battery_mclick_command = NULL;
|
|
|
|
battery_rclick_command = NULL;
|
|
|
|
battery_uwheel_command = NULL;
|
|
|
|
battery_dwheel_command = NULL;
|
2010-09-21 22:07:11 +00:00
|
|
|
battery_state.percentage = 0;
|
|
|
|
battery_state.time.hours = 0;
|
|
|
|
battery_state.time.minutes = 0;
|
2015-04-11 09:51:10 +00:00
|
|
|
battery_state.time.seconds = 0;
|
|
|
|
battery_state.state = BATTERY_UNKNOWN;
|
2010-04-18 12:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup_battery()
|
|
|
|
{
|
2015-03-28 18:08:44 +00:00
|
|
|
pango_font_description_free(bat1_font_desc);
|
2015-04-11 09:51:10 +00:00
|
|
|
bat1_font_desc = NULL;
|
2015-03-28 18:08:44 +00:00
|
|
|
pango_font_description_free(bat2_font_desc);
|
2015-04-11 09:51:10 +00:00
|
|
|
bat2_font_desc = NULL;
|
|
|
|
free(battery_low_cmd);
|
|
|
|
battery_low_cmd = NULL;
|
2015-07-13 03:16:02 +00:00
|
|
|
free(battery_lclick_command);
|
|
|
|
battery_lclick_command = NULL;
|
|
|
|
free(battery_mclick_command);
|
|
|
|
battery_mclick_command = NULL;
|
|
|
|
free(battery_rclick_command);
|
|
|
|
battery_rclick_command = NULL;
|
|
|
|
free(battery_uwheel_command);
|
|
|
|
battery_uwheel_command = NULL;
|
|
|
|
free(battery_dwheel_command);
|
|
|
|
battery_dwheel_command = NULL;
|
2015-09-12 02:31:12 +00:00
|
|
|
free(ac_connected_cmd);
|
|
|
|
ac_connected_cmd = NULL;
|
|
|
|
free(ac_disconnected_cmd);
|
|
|
|
ac_disconnected_cmd = NULL;
|
2015-03-28 18:08:44 +00:00
|
|
|
stop_timeout(battery_timeout);
|
2015-04-11 09:51:10 +00:00
|
|
|
battery_timeout = NULL;
|
2015-03-28 18:08:44 +00:00
|
|
|
battery_found = 0;
|
2010-05-15 23:35:57 +00:00
|
|
|
|
2015-08-07 02:31:31 +00:00
|
|
|
battery_os_free();
|
2010-04-18 12:07:36 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 20:48:55 +00:00
|
|
|
void init_battery()
|
|
|
|
{
|
2015-03-28 18:08:44 +00:00
|
|
|
if (!battery_enabled)
|
|
|
|
return;
|
2010-05-15 23:35:57 +00:00
|
|
|
|
2015-08-07 02:31:31 +00:00
|
|
|
battery_found = battery_os_init();
|
2009-11-15 16:55:50 +00:00
|
|
|
|
2015-04-17 20:17:25 +00:00
|
|
|
if (!battery_timeout)
|
2015-05-08 21:35:44 +00:00
|
|
|
battery_timeout = add_timeout(10, 30000, update_battery_tick, 0, &battery_timeout);
|
2015-08-08 04:39:45 +00:00
|
|
|
|
|
|
|
update_battery();
|
2009-10-18 17:54:09 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 00:03:06 +00:00
|
|
|
char* battery_get_tooltip(void* obj) {
|
2015-08-07 02:31:31 +00:00
|
|
|
return battery_os_tooltip();
|
2015-08-05 00:15:06 +00:00
|
|
|
}
|
2009-10-18 17:54:09 +00:00
|
|
|
|
|
|
|
void init_battery_panel(void *p)
|
|
|
|
{
|
|
|
|
Panel *panel = (Panel*)p;
|
|
|
|
Battery *battery = &panel->battery;
|
2009-05-15 20:48:55 +00:00
|
|
|
|
2009-10-28 23:01:32 +00:00
|
|
|
if (!battery_enabled)
|
|
|
|
return;
|
2009-05-15 20:48:55 +00:00
|
|
|
|
2015-04-11 09:51:10 +00:00
|
|
|
if (!bat1_font_desc)
|
|
|
|
bat1_font_desc = pango_font_description_from_string(DEFAULT_FONT);
|
|
|
|
if (!bat2_font_desc)
|
|
|
|
bat2_font_desc = pango_font_description_from_string(DEFAULT_FONT);
|
|
|
|
|
2011-02-05 08:32:11 +00:00
|
|
|
if (battery->area.bg == 0)
|
|
|
|
battery->area.bg = &g_array_index(backgrounds, Background, 0);
|
|
|
|
|
2009-10-18 17:54:09 +00:00
|
|
|
battery->area.parent = p;
|
|
|
|
battery->area.panel = p;
|
|
|
|
battery->area._draw_foreground = draw_battery;
|
2010-08-08 14:06:15 +00:00
|
|
|
battery->area.size_mode = SIZE_BY_CONTENT;
|
2009-10-18 17:54:09 +00:00
|
|
|
battery->area._resize = resize_battery;
|
2010-09-23 18:09:30 +00:00
|
|
|
battery->area.on_screen = 1;
|
|
|
|
battery->area.resize = 1;
|
2015-08-05 00:15:06 +00:00
|
|
|
|
|
|
|
if (battery_tooltip_enabled)
|
|
|
|
battery->area._get_tooltip_text = battery_get_tooltip;
|
2009-05-15 20:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-28 18:08:44 +00:00
|
|
|
int update_battery() {
|
2015-08-07 02:31:31 +00:00
|
|
|
int err;
|
2015-03-28 18:08:44 +00:00
|
|
|
|
2015-08-07 02:31:31 +00:00
|
|
|
/* reset */
|
2015-03-28 18:08:44 +00:00
|
|
|
battery_state.state = BATTERY_UNKNOWN;
|
2015-08-07 02:31:31 +00:00
|
|
|
battery_state.percentage = 0;
|
2015-08-07 03:20:24 +00:00
|
|
|
battery_state.ac_connected = FALSE;
|
2015-08-07 02:31:31 +00:00
|
|
|
batstate_set_time(&battery_state, 0);
|
2009-06-18 20:26:40 +00:00
|
|
|
|
2015-08-07 02:31:31 +00:00
|
|
|
err = battery_os_update(&battery_state);
|
2009-10-28 10:58:12 +00:00
|
|
|
|
|
|
|
// clamp percentage to 100 in case battery is misreporting that its current charge is more than its max
|
2015-03-28 18:08:44 +00:00
|
|
|
if (battery_state.percentage > 100) {
|
2009-10-28 10:58:12 +00:00
|
|
|
battery_state.percentage = 100;
|
|
|
|
}
|
2015-03-28 18:08:44 +00:00
|
|
|
|
2015-08-07 02:31:31 +00:00
|
|
|
return err;
|
2009-06-18 20:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-09 00:11:01 +00:00
|
|
|
void draw_battery (void *obj, cairo_t *c)
|
2009-05-15 20:48:55 +00:00
|
|
|
{
|
|
|
|
Battery *battery = obj;
|
|
|
|
PangoLayout *layout;
|
|
|
|
|
|
|
|
layout = pango_cairo_create_layout (c);
|
|
|
|
|
|
|
|
// draw layout
|
|
|
|
pango_layout_set_font_description(layout, bat1_font_desc);
|
|
|
|
pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
|
|
|
|
pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
|
|
|
|
pango_layout_set_text(layout, buf_bat_percentage, strlen(buf_bat_percentage));
|
|
|
|
|
|
|
|
cairo_set_source_rgba(c, battery->font.color[0], battery->font.color[1], battery->font.color[2], battery->font.alpha);
|
|
|
|
|
|
|
|
pango_cairo_update_layout(c, layout);
|
2015-04-18 13:22:07 +00:00
|
|
|
draw_text(layout, c, 0, battery->bat1_posy, &battery->font, ((Panel*)battery->area.panel)->font_shadow);
|
2009-05-15 20:48:55 +00:00
|
|
|
|
|
|
|
pango_layout_set_font_description(layout, bat2_font_desc);
|
|
|
|
pango_layout_set_indent(layout, 0);
|
|
|
|
pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time));
|
|
|
|
pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
|
|
|
|
|
|
|
|
pango_cairo_update_layout(c, layout);
|
2015-04-18 13:22:07 +00:00
|
|
|
draw_text(layout, c, 0, battery->bat2_posy, &battery->font, ((Panel*)battery->area.panel)->font_shadow);
|
2009-05-15 20:48:55 +00:00
|
|
|
pango_cairo_show_layout(c, layout);
|
|
|
|
|
|
|
|
g_object_unref(layout);
|
|
|
|
}
|
|
|
|
|
2009-06-08 18:49:50 +00:00
|
|
|
|
2010-09-16 23:24:25 +00:00
|
|
|
int resize_battery(void *obj)
|
2009-05-15 20:48:55 +00:00
|
|
|
{
|
2009-09-07 21:41:21 +00:00
|
|
|
Battery *battery = obj;
|
2010-09-21 22:07:11 +00:00
|
|
|
Panel *panel = battery->area.panel;
|
|
|
|
int bat_percentage_height, bat_percentage_width, bat_percentage_height_ink;
|
|
|
|
int bat_time_height, bat_time_width, bat_time_height_ink;
|
|
|
|
int ret = 0;
|
2009-05-15 20:48:55 +00:00
|
|
|
|
|
|
|
battery->area.redraw = 1;
|
2010-09-21 22:07:11 +00:00
|
|
|
|
2009-05-15 20:48:55 +00:00
|
|
|
snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
|
2015-03-28 18:08:44 +00:00
|
|
|
if (battery_state.state == BATTERY_FULL) {
|
2009-10-28 10:58:12 +00:00
|
|
|
strcpy(buf_bat_time, "Full");
|
|
|
|
} else {
|
|
|
|
snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
|
|
|
|
}
|
2015-03-28 18:08:44 +00:00
|
|
|
get_text_size2(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, &bat_percentage_width,
|
|
|
|
panel->area.height, panel->area.width, buf_bat_percentage, strlen(buf_bat_percentage));
|
|
|
|
get_text_size2(bat2_font_desc, &bat_time_height_ink, &bat_time_height, &bat_time_width,
|
|
|
|
panel->area.height, panel->area.width, buf_bat_time, strlen(buf_bat_time));
|
2009-05-15 20:48:55 +00:00
|
|
|
|
2010-09-21 22:07:11 +00:00
|
|
|
if (panel_horizontal) {
|
|
|
|
int new_size = (bat_percentage_width > bat_time_width) ? bat_percentage_width : bat_time_width;
|
2015-03-28 18:08:44 +00:00
|
|
|
new_size += 2 * battery->area.paddingxlr + 2 * battery->area.bg->border.width;
|
|
|
|
if (new_size > battery->area.width ||
|
|
|
|
new_size < battery->area.width - 2) {
|
2010-09-21 22:07:11 +00:00
|
|
|
// we try to limit the number of resize
|
2015-03-28 18:08:44 +00:00
|
|
|
battery->area.width = new_size;
|
|
|
|
battery->bat1_posy = (battery->area.height - bat_percentage_height - bat_time_height) / 2;
|
2011-01-08 13:56:26 +00:00
|
|
|
battery->bat2_posy = battery->bat1_posy + bat_percentage_height;
|
2010-09-21 22:07:11 +00:00
|
|
|
ret = 1;
|
|
|
|
}
|
2015-03-28 18:08:44 +00:00
|
|
|
} else {
|
|
|
|
int new_size = bat_percentage_height + bat_time_height +
|
|
|
|
(2 * (battery->area.paddingxlr + battery->area.bg->border.width));
|
|
|
|
if (new_size > battery->area.height ||
|
|
|
|
new_size < battery->area.height - 2) {
|
2010-09-21 22:07:11 +00:00
|
|
|
battery->area.height = new_size;
|
2015-03-28 18:08:44 +00:00
|
|
|
battery->bat1_posy = (battery->area.height - bat_percentage_height - bat_time_height - 2) / 2;
|
2011-01-08 13:56:26 +00:00
|
|
|
battery->bat2_posy = battery->bat1_posy + bat_percentage_height + 2;
|
2010-09-21 22:07:11 +00:00
|
|
|
ret = 1;
|
|
|
|
}
|
2009-05-15 20:48:55 +00:00
|
|
|
}
|
2010-09-16 23:24:25 +00:00
|
|
|
return ret;
|
2009-05-15 20:48:55 +00:00
|
|
|
}
|
2015-07-13 03:16:02 +00:00
|
|
|
|
|
|
|
void battery_action(int button)
|
|
|
|
{
|
|
|
|
char *command = 0;
|
|
|
|
switch (button) {
|
|
|
|
case 1:
|
|
|
|
command = battery_lclick_command;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
command = battery_mclick_command;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
command = battery_rclick_command;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
command = battery_uwheel_command;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
command = battery_dwheel_command;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tint_exec(command);
|
2015-08-04 22:17:32 +00:00
|
|
|
}
|