patch by Christian Ruppert, configure option for battery

git-svn-id: http://tint2.googlecode.com/svn/trunk@115 121b4492-b84c-0410-8b4c-0d4edfb3f3cc
This commit is contained in:
lorthiois@bbsoft.fr 2009-06-18 20:26:40 +00:00
parent 81930a6861
commit ce50e9c159
12 changed files with 160 additions and 119 deletions

View file

@ -1,3 +1,6 @@
2009-06-18
- patch by Christian Ruppert, configure option for battery
2009-06-14
- revert r78, fixed issue 100

View file

@ -60,6 +60,15 @@ AC_FUNC_FORK
AC_FUNC_MALLOC
AC_CHECK_FUNCS([gettimeofday memset select setlocale strcasecmp strchr strdup])
AC_ARG_ENABLE([battery],
[AS_HELP_STRING([--enable-battery=no], [Disable battery status, only useful for notebooks [default=yes]])],
[case "${enableval}" in
yes) battery=true ;;
no) battery=false ;;
*) AC_MSG_ERROR([bad value ${enableval} for --enable-battery]) ;;
esac], [battery=true])
AM_CONDITIONAL([ENABLE_BATTERY], [test x$battery = xtrue])
AC_ARG_ENABLE([examples],
[AS_HELP_STRING([--enable-examples], [Install additional $(PACKAGE_NAME)rc examples])],
[case "${enableval}" in

View file

@ -3,10 +3,10 @@ AM_CFLAGS += @PANGOCAIRO_CFLAGS@ @PANGO_CFLAGS@ @CAIRO_CFLAGS@ @GLIB2_CFLAGS@ @G
AM_LDFLAGS = -Wl,--as-needed
bin_PROGRAMS = tint2
LIBS = @PANGOCAIRO_LIBS@ @PANGO_LIBS@ @CAIRO_LIBS@ @GLIB2_LIBS@ @GOBJECT2_LIBS@ @X11_LIBS@ @XINERAMA_LIBS@ @IMLIB2_LIBS@
#noinst_LIBRARIES
INCLUDES = -Iutil -Ibattery -Iclock -Itaskbar -Isystray
INCLUDES = -Iutil -Iclock -Itaskbar -Isystray
bin_PROGRAMS = tint2
tint2_SOURCES = config.c \
util/area.h \
util/common.h \
@ -26,9 +26,15 @@ tint2_SOURCES = config.c \
taskbar/taskbar.c \
taskbar/task.c \
taskbar/taskbar.h \
taskbar/task.h \
battery/battery.c \
battery/battery.h
taskbar/task.h
if ENABLE_BATTERY
AM_CFLAGS += -DENABLE_BATTERY
INCLUDES += -Ibattery
tint2_SOURCES += battery/battery.c \
battery/battery.h
endif
DISTCLEANFILES = Makefile.in

View file

@ -44,76 +44,6 @@ char *battery_low_cmd;
char *path_energy_now, *path_energy_full, *path_current_now, *path_status;
void update_battery(struct batstate *data) {
FILE *fp;
char tmp[25];
int64_t energy_now = 0, energy_full = 0, current_now = 0;
int seconds = 0;
int8_t new_percentage = 0;
fp = fopen(path_energy_now, "r");
if(fp != NULL) {
fgets(tmp, sizeof tmp, fp);
energy_now = atoi(tmp);
fclose(fp);
}
fp = fopen(path_energy_full, "r");
if(fp != NULL) {
fgets(tmp, sizeof tmp, fp);
energy_full = atoi(tmp);
fclose(fp);
}
fp = fopen(path_current_now, "r");
if(fp != NULL) {
fgets(tmp, sizeof tmp, fp);
current_now = atoi(tmp);
fclose(fp);
}
fp = fopen(path_status, "r");
if(fp != NULL) {
fgets(tmp, sizeof tmp, fp);
fclose(fp);
}
data->state = BATTERY_UNKNOWN;
if(strcasecmp(tmp, "Charging\n")==0) data->state = BATTERY_CHARGING;
if(strcasecmp(tmp, "Discharging\n")==0) data->state = BATTERY_DISCHARGING;
if(current_now > 0) {
switch(data->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;
data->time.hours = seconds / 3600;
seconds -= 3600 * data->time.hours;
data->time.minutes = seconds / 60;
seconds -= 60 * data->time.minutes;
data->time.seconds = seconds;
if(energy_full > 0)
new_percentage = (energy_now*100)/energy_full;
if(battery_low_status != 0 && battery_low_status == new_percentage && data->percentage > new_percentage) {
printf("battery low, executing: %s\n", battery_low_cmd);
if(battery_low_cmd) system(battery_low_cmd);
}
data->percentage = new_percentage;
}
void init_battery()
{
// check battery
@ -172,13 +102,14 @@ void init_battery()
panel = &panel1[i];
battery = &panel->battery;
if (battery_dir == 0) battery->area.on_screen = 0;
if (!battery->area.on_screen) continue;
battery->area.parent = panel;
battery->area.panel = panel;
battery->area._draw_foreground = draw_battery;
battery->area._resize = resize_battery;
if (battery_dir == 0) panel->battery.area.on_screen = 0;
if (!battery->area.on_screen) continue;
if((fp = fopen(path_energy_now, "r")) == NULL) {
fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
panel->battery.area.on_screen = 0;
@ -224,6 +155,76 @@ void init_battery()
}
void update_battery() {
FILE *fp;
char tmp[25];
int64_t energy_now = 0, energy_full = 0, current_now = 0;
int seconds = 0;
int8_t new_percentage = 0;
fp = fopen(path_energy_now, "r");
if(fp != NULL) {
fgets(tmp, sizeof tmp, fp);
energy_now = atoi(tmp);
fclose(fp);
}
fp = fopen(path_energy_full, "r");
if(fp != NULL) {
fgets(tmp, sizeof tmp, fp);
energy_full = atoi(tmp);
fclose(fp);
}
fp = fopen(path_current_now, "r");
if(fp != NULL) {
fgets(tmp, sizeof tmp, fp);
current_now = atoi(tmp);
fclose(fp);
}
fp = fopen(path_status, "r");
if(fp != NULL) {
fgets(tmp, sizeof tmp, fp);
fclose(fp);
}
battery_state.state = BATTERY_UNKNOWN;
if(strcasecmp(tmp, "Charging\n")==0) battery_state.state = BATTERY_CHARGING;
if(strcasecmp(tmp, "Discharging\n")==0) battery_state.state = BATTERY_DISCHARGING;
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;
battery_state.time.hours = seconds / 3600;
seconds -= 3600 * battery_state.time.hours;
battery_state.time.minutes = seconds / 60;
seconds -= 60 * battery_state.time.minutes;
battery_state.time.seconds = seconds;
if(energy_full > 0)
new_percentage = (energy_now*100)/energy_full;
if(battery_low_status != 0 && battery_low_status == new_percentage && battery_state.percentage > new_percentage) {
printf("battery low, executing: %s\n", battery_low_cmd);
if(battery_low_cmd) system(battery_low_cmd);
}
battery_state.percentage = new_percentage;
}
void draw_battery (void *obj, cairo_t *c, int active)
{
Battery *battery = obj;

View file

@ -54,7 +54,7 @@ extern char *path_energy_now, *path_energy_full, *path_current_now, *path_status
// initialize clock : y position, ...
void update_battery(struct batstate *data);
void update_battery();
void init_battery();

View file

@ -185,7 +185,9 @@ void resize_clock (void *obj)
// resize other objects on panel
panel->area.resize = 1;
#ifdef ENABLE_BATTERY
panel->battery.area.resize = 1;
#endif
systray.area.resize = 1;
panel_refresh = 1;
}

View file

@ -40,11 +40,15 @@
#include "taskbar.h"
#include "systraybar.h"
#include "clock.h"
#include "battery.h"
#include "panel.h"
#include "config.h"
#include "window.h"
#ifdef ENABLE_BATTERY
#include "battery.h"
#endif
// --------------------------------------------------
// backward compatibility
static int save_file_config;
@ -54,8 +58,10 @@ static char *old_time1_font;
static char *old_time2_font;
static Area *area_task, *area_task_active;
#ifdef ENABLE_BATTERY
static char *old_bat1_font;
static char *old_bat2_font;
#endif
// temporary panel
static Panel *panel_config = 0;
@ -75,8 +81,8 @@ void init_config()
list_back = g_slist_append(0, calloc(1, sizeof(Area)));
panel_config = calloc(1, sizeof(Panel));
// window manager's menu default value == true
wm_menu = 1;
// window manager's menu default value == false
wm_menu = 0;
}
@ -314,6 +320,7 @@ void add_entry (char *key, char *value)
wm_menu = atoi (value);
/* Battery */
#ifdef ENABLE_BATTERY
else if (strcmp (key, "battery") == 0) {
if(atoi(value) == 1)
panel_config->battery.area.on_screen = 1;
@ -356,6 +363,11 @@ void add_entry (char *key, char *value)
memcpy(&panel_config->battery.area.pix.back, &a->pix.back, sizeof(Color));
memcpy(&panel_config->battery.area.pix.border, &a->pix.border, sizeof(Border));
}
#else
else if ((strcmp (key, "battery") == 0) || (strcmp (key, "battery_low_status") == 0) || (strcmp (key, "battery_low_cmd") == 0) || (strcmp (key, "bat1_font") == 0) || (strcmp (key, "bat2_font") == 0) || (strcmp (key, "battery_font_color") == 0) || (strcmp (key, "battery_padding") == 0) || (strcmp (key, "battery_background_id") == 0)) {
printf("tint2 is build without battery support\n");
}
#endif
/* Clock */
else if (strcmp (key, "time1_format") == 0) {
@ -646,7 +658,9 @@ void config_finish ()
// clock and systray before taskbar because resize(clock) can resize others object ??
init_panel();
init_clock();
#ifdef ENABLE_BATTERY
init_battery();
#endif
init_systray();
init_taskbar();
visible_object();
@ -850,6 +864,7 @@ void save_config ()
fputs("clock_padding = 2 2\n", fp);
fputs("clock_background_id = 0\n", fp);
#ifdef ENABLE_BATTERY
fputs("\n#---------------------------------------------\n", fp);
fputs("# BATTERY\n", fp);
fputs("#---------------------------------------------\n", fp);
@ -861,6 +876,7 @@ void save_config ()
fprintf(fp, "battery_font_color = #%02x%02x%02x %d\n", (int)(panel_config->battery.font.color[0]*255), (int)(panel_config->battery.font.color[1]*255), (int)(panel_config->battery.font.color[2]*255), (int)(panel_config->battery.font.alpha*100));
fputs("battery_padding = 2 2\n", fp);
fputs("battery_background_id = 0\n", fp);
#endif
fputs("\n#---------------------------------------------\n", fp);
fputs("# MOUSE ACTION ON TASK\n", fp);

View file

@ -73,12 +73,13 @@ void init_panel()
// add childs
if (p->clock.area.on_screen)
p->area.list = g_slist_append(p->area.list, &p->clock);
#ifdef ENABLE_BATTERY
if (p->battery.area.on_screen)
p->area.list = g_slist_append(p->area.list, &p->battery);
if (systray.area.on_screen && i == 0) {
// systray only on first panel
#endif
// systray only on first panel
if (systray.area.on_screen && i == 0)
p->area.list = g_slist_append(p->area.list, &systray);
}
// detect panel size
if (p->pourcentx)
@ -179,8 +180,10 @@ void resize_panel(void *obj)
taskbar_width = panel->area.width - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
if (panel->clock.area.on_screen && panel->clock.area.width)
taskbar_width -= (panel->clock.area.width + panel->area.paddingx);
#ifdef ENABLE_BATTERY
if (panel->battery.area.on_screen && panel->battery.area.width)
taskbar_width -= (panel->battery.area.width + panel->area.paddingx);
#endif
// TODO : systray only on first panel. search better implementation !
if (systray.area.on_screen && systray.area.width && panel == &panel1[0])
taskbar_width -= (systray.area.width + panel->area.paddingx);

View file

@ -14,13 +14,15 @@
#include <pango/pangocairo.h>
#include <sys/time.h>
#include "battery.h"
#include "common.h"
#include "clock.h"
#include "task.h"
#include "taskbar.h"
#include "systraybar.h"
#ifdef ENABLE_BATTERY
#include "battery.h"
#endif
extern int signal_pending;
@ -82,8 +84,9 @@ typedef struct {
// --------------------------------------------------
// battery
#ifdef ENABLE_BATTERY
Battery battery;
#endif
} Panel;

View file

@ -267,6 +267,8 @@ next:
}
// detect number of desktops
// wait 15s to leave some time for window manager startup
// FIXME: We should also provide a small warning about not respected NETWM specs.
for (i=0 ; i < 15 ; i++) {
server.nb_desktop = server_get_number_of_desktop ();
if (server.nb_desktop > 0) break;

View file

@ -66,8 +66,10 @@ void init_systray()
systray.area.posx = panel->area.width - panel->area.paddingxlr - panel->area.pix.border.width - systray.area.width;
if (panel->clock.area.on_screen)
systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
#ifdef ENABLE_BATTERY
if (panel->battery.area.on_screen)
systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
#endif
systray.area.redraw = 1;
}
@ -145,8 +147,10 @@ void resize_systray(void *obj)
systray.area.posx = panel->area.width - panel->area.pix.border.width - panel->area.paddingxlr - systray.area.width;
if (panel->clock.area.on_screen)
systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
#ifdef ENABLE_BATTERY
if (panel->battery.area.on_screen)
systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
#endif
systray.area.redraw = 1;
@ -257,29 +261,28 @@ void fix_geometry()
gboolean error;
int window_error_handler(Display *d, XErrorEvent *e)
{
d=d;e=e;
if (e->error_code == BadWindow) {
error = TRUE;
} else {
printf("error_handler %d\n", e->error_code);
abort();
}
return 0;
d=d;e=e;
error = TRUE;
if (e->error_code != BadWindow) {
printf("error_handler %d\n", e->error_code);
}
return 0;
}
gboolean icon_swallow(Window id)
{
XErrorHandler old;
Panel *panel = systray.area.panel;
XErrorHandler old;
Panel *panel = systray.area.panel;
error = FALSE;
old = XSetErrorHandler(window_error_handler);
XReparentWindow(server.dsp, id, panel->main_win, 0, 0);
XSync(server.dsp, False);
XSetErrorHandler(old);
error = FALSE;
old = XSetErrorHandler(window_error_handler);
XReparentWindow(server.dsp, id, panel->main_win, 0, 0);
printf("icon_swallow %lx %lx\n", id, panel->main_win);
XSync(server.dsp, False);
XSetErrorHandler(old);
return !error;
return !error;
}
@ -306,19 +309,6 @@ gboolean add_icon(Window id)
panel->area.resize = 1;
panel_refresh = 1;
// => calcul x, y, width, height dans resize
/*
// find the positon for the systray app window
int count = g_slist_length(icons);
traywin->x = border + ((width % icon_size) / 2) +
(count % (width / icon_size)) * icon_size;
traywin->y = border + ((height % icon_size) / 2) +
(count / (height / icon_size)) * icon_size;
// add the new icon to the list
icons = g_slist_append(icons, traywin);
*/
return TRUE;
}

View file

@ -50,8 +50,9 @@ void init ()
{
// Set signal handler
signal(SIGUSR1, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGHUP, signal_handler);
// set global data
memset(&server, 0, sizeof(Server_global));
@ -92,6 +93,7 @@ void cleanup()
if (time2_font_desc) pango_font_description_free(time2_font_desc);
if (time1_format) g_free(time1_format);
if (time2_format) g_free(time2_format);
#ifdef ENABLE_BATTERY
if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
if (battery_low_cmd) g_free(battery_low_cmd);
@ -99,6 +101,7 @@ void cleanup()
if (path_energy_full) g_free(path_energy_full);
if (path_current_now) g_free(path_current_now);
if (path_status) g_free(path_status);
#endif
if (clock_lclick_command) g_free(clock_lclick_command);
if (clock_rclick_command) g_free(clock_rclick_command);
@ -513,11 +516,13 @@ void event_timer()
}
// update battery
#ifdef ENABLE_BATTERY
if (panel1[0].battery.area.on_screen) {
update_battery(&battery_state);
update_battery();
for (i=0 ; i < nb_panel ; i++)
panel1[i].battery.area.resize = 1;
}
#endif
// update clock
if (time1_format) {
@ -626,6 +631,7 @@ load_config:
goto load_config;
case SIGINT:
case SIGTERM:
case SIGHUP:
cleanup ();
return 0;
}