diff -urN --exclude *Po --exclude Makefile xfce4-battery-plugin-0.5.0.orig/panel-plugin/battery.c xfce4-battery-plugin-0.5.0/panel-plugin/battery.c --- xfce4-battery-plugin-0.5.0.orig/panel-plugin/battery.c Wed Jan 17 23:56:51 2007 +++ xfce4-battery-plugin-0.5.0/panel-plugin/battery.c Tue May 22 11:52:28 2007 @@ -25,8 +25,25 @@ #include #endif +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + #ifdef __FreeBSD__ +#include #include + +#define APMDEVICE "/dev/apm" #elif __OpenBSD__ #include #include @@ -39,97 +56,9 @@ #include #endif -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include #include "libacpi.h" - -#include -#include - -#define BORDER 8 -#define HIGH_COLOR "#00ff00" -#define LOW_COLOR "#ffff00" -#define CRITICAL_COLOR "#ff0000" -#define AVERAGING_CYCLE 5 -#define PLUGIN_WEBSITE "http://goodies.xfce.org/projects/panel-plugins/xfce4-battery-plugin" - -typedef struct -{ - gboolean display_label; /* Options */ - gboolean display_icon; /* Options */ - gboolean display_power; /* Options */ - gboolean display_percentage; /* Options */ - gboolean display_time; - gboolean hide_when_full; - gboolean tooltip_display_percentage; - gboolean tooltip_display_time; - int low_percentage; - int critical_percentage; - int action_on_low; - int action_on_critical; - char *command_on_low; char *command_on_critical; - float hsize; - float vsize; -} t_battmon_options; - -typedef struct -{ - XfcePanelPlugin *plugin; - - GtkTooltips *tips; - GtkWidget *vbox; /* Widgets */ - GtkWidget *ebox; - GtkWidget *battstatus; - int timeoutid; /* To update apm status */ - int method; - gboolean flag; - gboolean low; - gboolean critical; - t_battmon_options options; - GdkColor colorH; - GdkColor colorL; - GdkColor colorC; - GtkLabel *label; - GtkLabel *charge; - GtkLabel *rtime; - GtkLabel *alt_rtime; - GtkLabel *acfan; - GtkLabel *temp; - GtkWidget *image; -} t_battmon; - -typedef struct -{ - GtkWidget *cb_disp_power; - GtkWidget *cb_disp_label; - GtkWidget *cb_disp_percentage; - GtkWidget *cb_disp_time; - GtkWidget *cb_hide_when_full; - GtkWidget *cb_disp_tooltip_percentage; - GtkWidget *cb_disp_tooltip_time; - GtkWidget *cb_disp_icon; - GtkWidget *sb_low_percentage; - GtkWidget *sb_critical_percentage; - GtkWidget *om_action_low; - GtkWidget *om_action_critical; - GtkWidget *en_command_low; - GtkWidget *en_command_critical; - t_battmon *battmon; -} t_battmon_dialog; - -enum {BM_DO_NOTHING, BM_MESSAGE, BM_COMMAND, BM_COMMAND_TERM}; -enum {BM_BROKEN, BM_USE_ACPI, BM_USE_APM}; +#include "battery.h" static void @@ -155,49 +84,46 @@ gboolean detect_battery_info(t_battmon *battmon) { -#ifdef __FreeBSD__ - /* This is how I read the information from the APM subsystem under - FreeBSD. Each time this functions is called (once every second) - the APM device is opened, read from and then closed. - - except that is does not work on FreeBSD - - */ - struct apm_info apm; - int fd; + battmon->avail_methods = BM_METHOD_NONE; + battmon->method = BM_METHOD_NONE; +#ifdef __FreeBSD__ /* First check to see if ACPI is available */ if (check_acpi() == 0) { int i; /* ACPI detected */ - battmon->method = BM_USE_ACPI; + battmon->method = BM_METHOD_ACPI; + battmon->avail_methods |= BM_METHOD_ACPI; /* consider battery 0 first... */ + /* FIXME: doesn't matter how many batteries is... there is only one of acpiinfo and acpistate + variables... for more battetries support there should be fixed + */ + + /* FIXME: hm... there is no difference between check_acpi & read_acpi_info + for sysctl method except storing batt_count into acpiinfo->present + variable + */ for (i=0;imethod = BM_BROKEN; -#ifdef APMDEVICE - fd = open(APMDEVICE, O_RDONLY); - if (fd == -1) return FALSE; + struct apm_info apm; + int fd; - if (ioctl(fd, APMIO_GETINFO, &apm) == -1) { - close(fd); - return FALSE; + fd = open(APMDEVICE, O_RDONLY); + if (fd != -1) { + if (ioctl(fd, APMIO_GETINFO, &apm) != -1) { + battmon->method = BM_METHOD_APM; + battmon->avail_methods |= BM_METHOD_APM; + } + close(fd); } - close(fd); - battmon->method = BM_USE_APM; -#endif - return TRUE; #elif defined(__OpenBSD__) || defined(__NetBSD__) /* Code for OpenBSD by Joe Ammond . Using the same procedure as for FreeBSD. @@ -206,15 +132,16 @@ struct apm_power_info apm; int fd; - battmon->method = BM_BROKEN; + battmon->method = BM_METHOD_NONE; fd = open(APMDEVICE, O_RDONLY); if (fd == -1) return FALSE; - + if (ioctl(fd, APM_IOC_GETPOWER, &apm) == -1) { + if (ioctl(fd, APM_IOC_GETPOWER, &apm) == -1) { close(fd); - return FALSE; - } + return FALSE; + } close(fd); - battmon->method = BM_USE_APM; + battmon->method = BM_METHOD_APM; + battmon->avail_methods |= BM_METHOD_APM; return TRUE; #elif __linux__ @@ -224,124 +151,95 @@ if(check_acpi()==0) { /* ACPI detected */ int i; - battmon->method = BM_USE_ACPI; - for (i=0;imethod = BM_METHOD_ACPI; + battmon->avail_methods |= BM_METHOD_ACPI; + for (i = 0; i < batt_count; i++) { + read_acpi_info(i); if (read_acpi_state(i)) break; } - if (batt_count){ - apm.battery_percentage=acpistate->percentage; - apm.battery_time=acpistate->rtime; - } - return TRUE; } if(apm_read(&apm) == 0) { /* ACPI not detected, but APM works */ - battmon->method = BM_USE_APM; - return TRUE; + battmon->method = BM_METHOD_APM; + battmon->avail_methods |= BM_METHOD_APM; } - - /* Neither ACPI or APM detected/working */ - battmon->method = BM_BROKEN; - - return FALSE; #endif + /* use ACPI by default */ + switch (battmon->avail_methods) { + case BM_METHOD_BOTH: + case BM_METHOD_ACPI: { battmon->method = BM_METHOD_ACPI; break; } + case BM_METHOD_APM: { battmon->method = BM_METHOD_APM; break; } + case BM_METHOD_NONE: + default: { + battmon->method = BM_METHOD_NONE; + return FALSE; + } + } + + return TRUE; } + static gboolean -battmon_time_labels_fits(t_battmon *battmon) +toggle_widget(gboolean isShow, GtkWidget *widget) { - int plugin_size, labels_size; - - GtkRequisition widget_size; - gtk_widget_size_request( GTK_WIDGET(battmon->plugin), &widget_size ); - plugin_size = widget_size.height; - - labels_size = 0; - gtk_widget_size_request( GTK_WIDGET(battmon->charge), &widget_size ); - labels_size += widget_size.height; - gtk_widget_size_request( GTK_WIDGET(battmon->rtime), &widget_size ); - labels_size += widget_size.height; + if(isShow){ + gtk_widget_show((GtkWidget *)widget); + } else { + gtk_widget_hide((GtkWidget *)widget); + } - return labels_size <= plugin_size; + return 0; } + static gboolean -update_apm_status(t_battmon *battmon) +update_status(t_battmon *battmon) { - int charge=0, rate; + int charge = 0; + int time_remaining=0; + int batt_status = BM_BATT_NOT_PRESENT; - int lcapacity, ccapacity; - gboolean fan=FALSE; + gboolean acline = FALSE; + gboolean fan = FALSE; const char *temp; - int time_remaining=0; - gboolean acline; gchar buffer[128]; - static int update_time = AVERAGING_CYCLE; - static int sum_lcapacity = 0; - static int sum_ccapacity = 0; - static int sum_rate = 0; - - static int last_ccapacity = 0; - static int last_lcapacity = 0; - static int last_rate = 0; - static int last_acline = 0; - -#if defined(__OpenBSD__) || defined(__NetBSD__) - /* Code for OpenBSD by Joe Ammond . Using the same - procedure as for FreeBSD. - Made to work on NetBSD by Stefan Sperling - */ - struct apm_power_info apm; - int fd; - - battmon->method = BM_BROKEN; - fd = open(APMDEVICE, O_RDONLY); - if (fd == -1) return TRUE; - if (ioctl(fd, APM_IOC_GETPOWER, &apminfo) == -1) - return TRUE; - close(fd); - charge = apm.battery_life; - time_remaining = apm.minutes_left; - acline = apm.ac_state ? TRUE : FALSE; - -#else - struct apm_info apm; - DBG ("Updating battery status..."); - - if(battmon->method == BM_BROKEN) { + /* try to make some detection if polling method is unknown */ + if(battmon->method == BM_METHOD_NONE) { /* See if ACPI or APM support has been enabled yet */ - if(!detect_battery_info(battmon)) return TRUE; - if(battmon->timeoutid != 0) g_source_remove(battmon->timeoutid); - /* Poll only once per minute if using ACPI due to a bug */ -#ifdef TUTTLE_UPDATES - /* what bug? I don't see any bug here. */ - if(battmon->method == BM_USE_ACPI) { - battmon->timeoutid = g_timeout_add(60 * 1024, - (GSourceFunc) update_apm_status, battmon); - } - else -#endif - battmon->timeoutid = g_timeout_add(2 * 1024, - (GSourceFunc) update_apm_status, battmon); - } - - /* Show initial state if using ACPI rather than waiting a minute */ - if(battmon->flag) { - g_source_remove(battmon->timeoutid); - battmon->timeoutid = g_timeout_add(2 * 1024, - (GSourceFunc) update_apm_status, battmon); + if(!detect_battery_info(battmon)) + return TRUE; + if(!battmon->timeoutid) + battmon->timeoutid = g_timeout_add(5 * 1024, + (GSourceFunc) update_status, battmon); } - if(battmon->method == BM_USE_ACPI) { + /* using ACPI method */ + if(battmon->method == BM_METHOD_ACPI) { int i; acline = read_acad_state(); + +#ifdef __linux__ + int rate; + int lcapacity, ccapacity; + + static int update_time = AVERAGING_CYCLE; + static int sum_lcapacity = 0; + static int sum_ccapacity = 0; + static int sum_rate = 0; + + static int last_ccapacity = 0; + static int last_lcapacity = 0; + static int last_rate = 0; + static int last_acline = 0; + lcapacity = rate = ccapacity = 0; + + /* FIXME: optimize this */ + for (i=0;ilast_full_capacity; @@ -349,12 +247,6 @@ rate += acpistate->prate; } - if ( battmon->flag ) { - last_ccapacity = ccapacity; - last_lcapacity = lcapacity; - last_rate = rate; - } - sum_lcapacity += lcapacity; sum_ccapacity += ccapacity; sum_rate += rate; @@ -389,128 +281,167 @@ time_remaining = 0; last_acline = acline; + batt_status = (acline ? BM_BATT_ONLINE : BM_BATT_DISCHARG); - } +#elif __FreeBSD__ + for (i = 0; i < batt_count; i++) { + /* FIXME: is it really needed to reread info (call read_acpi_info) during each polling? */ + /* read_acpi_info(i); */ + + /* FIXME: it works correctly for one battery only... */ + if (read_acpi_state(i)) + break; + } + + charge = acpistate->percentage; + time_remaining = acpistate->rtime; + switch (acpistate->state) { + case 0: { batt_status = BM_BATT_ONLINE; break; } + case ACPI_BATT_STAT_DISCHARG: { batt_status = BM_BATT_DISCHARG; break; } + case ACPI_BATT_STAT_CHARGING: { batt_status = BM_BATT_CHARGING; break; } + case ACPI_BATT_STAT_NOT_PRESENT: + default: { + batt_status = BM_BATT_NOT_PRESENT; + break; + } + } +#endif /* __FreeBSD__ || __linux__ */ + } else if(battmon->method == BM_METHOD_APM) { /* using APM method */ #ifdef __linux__ - else { - DBG ("Trying apm_read()..."); - apm_read(&apm); /* not broken and not using ACPI, assume APM */ + struct apm_info apm; + + apm_read(&apm); + charge = apm.battery_percentage; time_remaining = apm.battery_time; acline = apm.ac_line_status ? TRUE : FALSE; - - } + batt_status = (acline ? BM_BATT_ONLINE : BM_BATT_DISCHARG); #elif __FreeBSD__ - else { - /* This is how I read the information from the APM subsystem under - FreeBSD. Each time this functions is called (once every second) - the APM device is opened, read from and then closed. - - except it don't work with 5.x: -battmon.c: In function `update_apm_status': -battmon.c:241: `APMDEVICE' undeclared (first use in this function) -battmon.c:241: (Each undeclared identifier is reported only once -battmon.c:241: for each function it appears in.) -*** Error code 1 - - */ -#ifdef APMDEVICE - int fd; - - battmon->method = BM_BROKEN; - fd = open(APMDEVICE, O_RDONLY); - if (fd == -1) return TRUE; - - if (ioctl(fd, APMIO_GETINFO, &apm) == -1) { - close(fd); - return TRUE; - } + struct apm_info apm; + int fd; - close(fd); + fd = open(APMDEVICE, O_RDONLY); + if (fd == -1) return TRUE; - acline = apm.ai_acline ? TRUE : FALSE; - time_remaining = apm.ai_batt_time; - time_remaining = time_remaining / 60; /* convert from seconds to minutes */ - charge = apm.ai_batt_life; -#else - /* FIXME: apm stuff needs fix for 5.x kernels */ - acline=0; - time_remaining=0; - charge=0; -#endif + if (ioctl(fd, APMIO_GETINFO, &apm) == -1) { + close(fd); + return TRUE; + } + + close(fd); + + acline = apm.ai_acline ? TRUE : FALSE; + time_remaining = apm.ai_batt_time / 60; + charge = apm.ai_batt_life; + switch (apm.ai_batt_stat) { + case 0: { batt_status = BM_BATT_ONLINE; break; } + case APM_BATT_HIGH: + case APM_BATT_LOW: + case APM_BATT_CRITICAL: { batt_status = BM_BATT_DISCHARG; break; } + case APM_BATT_CHARGING: { batt_status = BM_BATT_CHARGING; break; } + case APM_BATT_NOT_PRESENT: + default: { + batt_status = BM_BATT_NOT_PRESENT; + break; + } + } +#elif defined(__OpenBSD__) || defined(__NetBSD__) + /* Code for OpenBSD by Joe Ammond . Using the same + procedure as for FreeBSD. + Made to work on NetBSD by Stefan Sperling + */ + struct apm_power_info apm; + int fd; + + battmon->method = BM_METHOD_NONE; + fd = open(APMDEVICE, O_RDONLY); + if (fd == -1) return TRUE; + if (ioctl(fd, APM_IOC_GETPOWER, &apminfo) == -1) + return TRUE; + close(fd); + charge = apm.battery_life; + time_remaining = apm.minutes_left; + acline = apm.ac_state ? TRUE : FALSE; + if (acline) { + batt_state = BM_BATT_ONLINE; + } else { + switch (apm.battery_state) { + case APM_BATT_HIGH: + case APM_BATT_LOW: + case APM_BATT_CRITICAL: { batt_status = BM_BATT_DISCHARG; break; } + case APM_BATT_CHARGING: { batt_status = BM_BATT_CHARGING; break; } + case APM_BATT_UNKNOWN: + default: { + batt_status = BM_BATT_NOT_PRESENT; + break; + } + } + } +#elif defined(__OpenBSD__) || defined(__NetBSD__) +#endif + } else { + return TRUE; } -#endif -#endif - battmon->flag = FALSE; charge = CLAMP (charge, 0, 100); gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(battmon->battstatus), charge / 100.0); - if(battmon->options.display_label){ - gtk_widget_show((GtkWidget *)battmon->label); - } else { - gtk_widget_hide((GtkWidget *)battmon->label); - } - - if(battmon->options.display_icon){ - gtk_widget_show(battmon->image); - } else { - gtk_widget_hide(battmon->image); - } - - if(battmon->options.display_percentage && !(battmon->options.hide_when_full && acline && charge >= 99)){ - gtk_widget_show((GtkWidget *)battmon->charge); + if(battmon->options.display_percentage) { g_snprintf(buffer, sizeof(buffer),"%d%% ", charge); gtk_label_set_text(battmon->charge,buffer); - } else { - gtk_widget_hide((GtkWidget *)battmon->charge); + + if(battmon->options.hide_when_full && batt_status == BM_BATT_ONLINE){ + if (GTK_WIDGET_VISIBLE((GtkWidget *)battmon->charge)) + gtk_widget_hide((GtkWidget *)battmon->charge); + } else { + if (!GTK_WIDGET_VISIBLE((GtkWidget *)battmon->charge)) + gtk_widget_show((GtkWidget *)battmon->charge); + } } - if (battmon->options.display_time && !(battmon->options.hide_when_full && acline && charge >= 99 )){ - GtkLabel *active_label; - if ( battmon_time_labels_fits( battmon ) ) { - active_label = battmon->rtime; - gtk_widget_hide( (GtkWidget*)battmon->alt_rtime ); + if (battmon->options.display_time){ + if (acline) { + g_snprintf(buffer, sizeof(buffer),"AC "); + } else { + g_snprintf(buffer, sizeof(buffer),"%02d:%02d ",time_remaining/60,time_remaining%60); + } + gtk_label_set_text(battmon->rtime,buffer); + + if (battmon->options.hide_when_full && batt_status == BM_BATT_ONLINE){ + if (GTK_WIDGET_VISIBLE((GtkWidget *)battmon->rtime)) + gtk_widget_hide((GtkWidget *)battmon->rtime); } else { - active_label = battmon->alt_rtime; - gtk_widget_hide( (GtkWidget*)battmon->rtime ); + if (!GTK_WIDGET_VISIBLE((GtkWidget *)battmon->rtime)) + gtk_widget_show((GtkWidget *)battmon->rtime); } - - gtk_widget_show((GtkWidget *)active_label); - g_snprintf(buffer, sizeof(buffer),"%02d:%02d ",time_remaining/60,time_remaining%60); - gtk_label_set_text(active_label,buffer); - - } else { - gtk_widget_hide((GtkWidget *)battmon->rtime); - gtk_widget_hide((GtkWidget *)battmon->alt_rtime); } + /* Setting tooltip phrase */ if(acline) { - char *t=(charge<99.9)?_("(Charging from AC)"):_("(AC on-line)"); + char *t_prefix = (batt_status == BM_BATT_CHARGING)?_("Charging from AC"):_("AC on-line"); + char *t_suffix = (batt_status == BM_BATT_CHARGING)?_("is complete"):_("of full charge"); if(battmon->options.tooltip_display_percentage) { - g_snprintf(buffer, sizeof(buffer), "%d%% %s", charge,t); + g_snprintf(buffer, sizeof(buffer), "%s. %d%% %s", t_prefix, charge, t_suffix); } else - g_snprintf(buffer, sizeof(buffer), "%s",t); + g_snprintf(buffer, sizeof(buffer), "%s",t_prefix); } else { if(battmon->options.tooltip_display_percentage && battmon->options.tooltip_display_time) - g_snprintf(buffer, sizeof(buffer), _("%d%% (%02d:%02d) remaining"), charge, time_remaining / 60, time_remaining % 60); + g_snprintf(buffer, sizeof(buffer), _("Cable is unplugged. %d%% (%02d:%02d) remaining"), charge, time_remaining / 60, time_remaining % 60); else if(battmon->options.tooltip_display_time) - g_snprintf(buffer, sizeof(buffer), _("%02d:%02d remaining"),time_remaining / 60, time_remaining % 60); + g_snprintf(buffer, sizeof(buffer), _("Cable is unplugged. %02d:%02d remaining"),time_remaining / 60, time_remaining % 60); else if(battmon->options.tooltip_display_percentage) - g_snprintf(buffer, sizeof(buffer), _("%d%% remaining"), charge); + g_snprintf(buffer, sizeof(buffer), _("Cable is unplugged. %d%% remaining"), charge); else - g_snprintf(buffer, sizeof(buffer), _("AC off-line")); + g_snprintf(buffer, sizeof(buffer), _("Cable is unplugged")); } gtk_tooltips_set_tip (battmon->tips, battmon->ebox, buffer, NULL); + /* FIXME: is it really needed in this plugin? */ if(battmon->options.display_power){ - gtk_widget_show((GtkWidget *)battmon->acfan); - gtk_widget_show((GtkWidget *)battmon->temp); - fan=get_fan_status(); if(acline && fan) gtk_label_set_text(battmon->acfan,"AC FAN"); @@ -531,10 +462,7 @@ gtk_label_set_text(battmon->temp,""); gtk_widget_hide((GtkWidget *)battmon->temp); } - } else { - gtk_widget_hide((GtkWidget *)battmon->acfan); - gtk_widget_hide((GtkWidget *)battmon->temp); - } + } gtk_progress_bar_set_text(GTK_PROGRESS_BAR(battmon->battstatus), NULL); @@ -607,7 +535,7 @@ orientation = xfce_panel_plugin_get_orientation (battmon->plugin); size = xfce_panel_plugin_get_size (battmon->plugin) - 6; - /* icon size is 41x64px */ + /* icon size is 41x64px */ if (orientation == GTK_ORIENTATION_HORIZONTAL) { height = size; @@ -665,8 +593,8 @@ /* init hide the widget */ gtk_widget_hide(battmon->image); - battmon->label = (GtkLabel *)gtk_label_new(_("Battery")); - gtk_box_pack_start(GTK_BOX(box),GTK_WIDGET(battmon->label),FALSE, FALSE, 0); + battmon->label = (GtkLabel *)gtk_label_new(_("Battery")); + gtk_box_pack_start(GTK_BOX(box),GTK_WIDGET(battmon->label),FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(battmon->battstatus), FALSE, FALSE, 2); @@ -675,21 +603,14 @@ /* percent + rtime */ gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(vbox), FALSE, FALSE, 0); - battmon->charge = (GtkLabel *)gtk_label_new("50%%"); - gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(battmon->charge),TRUE, TRUE, 0); + battmon->charge = (GtkLabel *)gtk_label_new("50%%"); + gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(battmon->charge),TRUE, TRUE, 0); - battmon->rtime = (GtkLabel *)gtk_label_new("01:00"); + battmon->rtime = (GtkLabel *)gtk_label_new("01:00"); gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(battmon->rtime),TRUE, TRUE, 0); - vbox=gtk_vbox_new(FALSE, 0); - gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(vbox), FALSE, FALSE, 0); - - battmon->alt_rtime = (GtkLabel *)gtk_label_new("01:00"); - gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(battmon->alt_rtime),TRUE, TRUE, 0); - - vbox=gtk_vbox_new(FALSE, 0); - /* ac-fan-temp */ + vbox=gtk_vbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(vbox), FALSE, FALSE, 0); battmon->acfan = (GtkLabel *)gtk_label_new("AC FAN"); @@ -712,13 +633,6 @@ } if (!battmon->options.display_time){ gtk_widget_hide((GtkWidget *)battmon->rtime); - gtk_widget_hide((GtkWidget *)battmon->alt_rtime); - } else { - if ( battmon_time_labels_fits(battmon) ) { - gtk_widget_hide((GtkWidget*)battmon->alt_rtime); - } else { - gtk_widget_hide((GtkWidget*)battmon->rtime); - } } gtk_container_add(GTK_CONTAINER(battmon->ebox),GTK_WIDGET(battmon->vbox)); @@ -738,8 +652,8 @@ if (battmon->timeoutid) g_source_remove(battmon->timeoutid); gtk_container_remove(GTK_CONTAINER(battmon->ebox), GTK_WIDGET(battmon->vbox)); setup_battmon(battmon,orientation); - update_apm_status( battmon ); - battmon->timeoutid = g_timeout_add(1 * 1024, (GSourceFunc) update_apm_status, battmon); + update_status( battmon ); + battmon->timeoutid = g_timeout_add(5 * 1024, (GSourceFunc) update_status, battmon); return TRUE; } @@ -759,7 +673,6 @@ battmon->ebox = gtk_event_box_new(); setup_battmon(battmon, xfce_panel_plugin_get_orientation (plugin)); battmon->timeoutid = 0; - battmon->flag = FALSE; battmon->tips = gtk_tooltips_new (); g_object_ref (G_OBJECT (battmon->tips)); gtk_object_sink (GTK_OBJECT (battmon->tips)); @@ -885,6 +798,7 @@ battmon_set_size(XfcePanelPlugin *plugin, int size, t_battmon *battmon) { GdkPixbuf *icon; + int font_size; if (xfce_panel_plugin_get_orientation (plugin) == GTK_ORIENTATION_HORIZONTAL) @@ -914,6 +828,20 @@ g_object_unref (G_OBJECT (icon)); } + /* calculating font size for labels */ + GtkStyle *labelStyle = gtk_widget_get_style((GtkWidget *)battmon->rtime); + + font_size = (size -6) /2; + font_size = (font_size < 6 ? 6 : (font_size > 12 ? 12 : font_size)); + + pango_font_description_set_absolute_size(labelStyle->font_desc, font_size *PANGO_SCALE); + + gtk_widget_modify_font ((GtkWidget *)battmon->charge, labelStyle->font_desc); + gtk_widget_modify_font ((GtkWidget *)battmon->rtime, labelStyle->font_desc); + gtk_widget_modify_font ((GtkWidget *)battmon->acfan, labelStyle->font_desc); + gtk_widget_modify_font ((GtkWidget *)battmon->temp, labelStyle->font_desc); + gtk_widget_modify_font ((GtkWidget *)battmon->label, labelStyle->font_desc); + return TRUE; } @@ -952,7 +880,8 @@ t_battmon *battmon = dialog->battmon; battmon->options.display_percentage = gtk_toggle_button_get_active(tb); - update_apm_status(dialog->battmon); + + toggle_widget( (battmon->options.display_percentage && !battmon->options.hide_when_full), (GtkWidget *)battmon->charge); } static void @@ -961,7 +890,8 @@ t_battmon *battmon = dialog->battmon; battmon->options.display_time = gtk_toggle_button_get_active(tb); - update_apm_status(dialog->battmon); + + toggle_widget( (battmon->options.display_time && !battmon->options.hide_when_full), (GtkWidget *)battmon->rtime); } static void @@ -970,7 +900,6 @@ t_battmon *battmon = dialog->battmon; battmon->options.hide_when_full = gtk_toggle_button_get_active(tb); - update_apm_status(dialog->battmon); } static void @@ -986,7 +915,9 @@ t_battmon *battmon = dialog->battmon; battmon->options.display_power = gtk_toggle_button_get_active(tb); - update_apm_status(dialog->battmon); + + toggle_widget(battmon->options.display_power, (GtkWidget *)battmon->acfan); + toggle_widget(battmon->options.display_power, (GtkWidget *)battmon->temp); } static void set_disp_label(GtkToggleButton *tb, t_battmon_dialog *dialog) @@ -994,7 +925,8 @@ t_battmon *battmon = dialog->battmon; battmon->options.display_label = gtk_toggle_button_get_active(tb); - update_apm_status(dialog->battmon); + + toggle_widget(battmon->options.display_label, (GtkWidget *)battmon->label); } static void @@ -1003,7 +935,8 @@ t_battmon *battmon = dialog->battmon; battmon->options.display_icon = gtk_toggle_button_get_active(tb); - update_apm_status(dialog->battmon); + + toggle_widget(battmon->options.display_icon, (GtkWidget *)battmon->image); } static void @@ -1020,7 +953,7 @@ t_battmon *battmon = dialog->battmon; battmon->options.low_percentage = gtk_spin_button_get_value_as_int(sb); - update_apm_status(dialog->battmon); + /* update_status(dialog->battmon); */ } static void @@ -1029,7 +962,7 @@ t_battmon *battmon = dialog->battmon; battmon->options.critical_percentage = gtk_spin_button_get_value_as_int(sb); - update_apm_status(dialog->battmon); + /* update_status(dialog->battmon); */ } static void @@ -1040,7 +973,7 @@ battmon->options.action_on_low = gtk_option_menu_get_history(om); gtk_widget_set_sensitive(dialog->en_command_low, (gtk_option_menu_get_history(om) > 1) ? 1 : 0); - update_apm_status(dialog->battmon); + /* update_status(dialog->battmon); */ } static void @@ -1051,7 +984,7 @@ battmon->options.action_on_critical = gtk_option_menu_get_history(om); gtk_widget_set_sensitive(dialog->en_command_critical, (gtk_option_menu_get_history(om) > 1) ? 1 : 0); - update_apm_status(dialog->battmon); + /* update_status(dialog->battmon); */ } static gboolean @@ -1063,7 +996,7 @@ g_free(battmon->options.command_on_low); temp = gtk_entry_get_text(en); battmon->options.command_on_low = g_strdup(temp); - update_apm_status(dialog->battmon); + /* update_status(dialog->battmon); */ /* Prevents a GTK crash */ return FALSE; @@ -1078,7 +1011,7 @@ g_free(battmon->options.command_on_critical); temp = gtk_entry_get_text(en); battmon->options.command_on_critical = g_strdup(temp); - update_apm_status(dialog->battmon); + /* update_status(dialog->battmon); */ /* Prevents a GTK crash */ return FALSE; @@ -1376,6 +1309,9 @@ battmon_read_config (plugin, battmon); + toggle_widget(battmon->options.display_label, (GtkWidget *)battmon->label); + toggle_widget(battmon->options.display_icon, (GtkWidget *)battmon->image); + g_signal_connect (plugin, "free-data", G_CALLBACK (battmon_free), battmon); g_signal_connect (plugin, "save", G_CALLBACK (battmon_write_config), battmon); @@ -1394,19 +1330,12 @@ xfce_panel_plugin_add_action_widget (plugin, battmon->battstatus); /* Determine what facility to use and initialize reading */ - battmon->method = BM_BROKEN; - update_apm_status(battmon); + battmon->method = BM_METHOD_NONE; + update_status(battmon); /* If neither ACPI nor APM are enabled, check for either every 60 seconds */ if(battmon->timeoutid == 0) - battmon->timeoutid = g_timeout_add(60 * 1024, (GSourceFunc) update_apm_status, battmon); - - /* Required for the percentage and tooltip to be initially displayed due to the long timeout for ACPI */ - if(battmon->method == BM_USE_ACPI) { - battmon->flag = TRUE; - g_source_remove(battmon->timeoutid); - battmon->timeoutid = g_timeout_add(1000, (GSourceFunc) update_apm_status, battmon); - } + battmon->timeoutid = g_timeout_add(60 * 1024, (GSourceFunc) update_status, battmon); } /* register the plugin */ diff -urN --exclude *Po --exclude Makefile xfce4-battery-plugin-0.5.0.orig/panel-plugin/battery.h xfce4-battery-plugin-0.5.0/panel-plugin/battery.h --- xfce4-battery-plugin-0.5.0.orig/panel-plugin/battery.h Thu Jan 1 07:00:00 1970 +++ xfce4-battery-plugin-0.5.0/panel-plugin/battery.h Tue May 22 11:01:52 2007 @@ -0,0 +1,110 @@ +/* $Id: battery.h 2394 2007-01-17 17:40:47Z nick $ + * + * Copyright (c) 2003 Nicholas Penwarden + * Copyright (c) 2003 Benedikt Meurer + * Copyright (c) 2003 Edscott Wilson Garcia + * Copyright (c) 2005 Eduard Roccatello + * Copyright (c) 2006 Nick Schermer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * 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., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _BATTMON_BATTERY_H +#define _BATTMON_BATTERY_H + +#define BORDER 8 +#define HIGH_COLOR "#00ff00" +#define LOW_COLOR "#ffff00" +#define CRITICAL_COLOR "#ff0000" +#define AVERAGING_CYCLE 5 +#define PLUGIN_WEBSITE "http://goodies.xfce.org/projects/panel-plugins/xfce4-battery-plugin" + +#define BM_METHOD_NONE 0x00 +#define BM_METHOD_ACPI 0x01 +#define BM_METHOD_APM 0x02 +#define BM_METHOD_BOTH 0x03 + +#define BM_BATT_ONLINE 0x00 +#define BM_BATT_DISCHARG 0x01 +#define BM_BATT_CHARGING 0x02 +#define BM_BATT_NOT_PRESENT 0x07 + +typedef struct +{ + gboolean display_label; /* Options */ + gboolean display_icon; /* Options */ + gboolean display_power; /* Options */ + gboolean display_percentage; /* Options */ + gboolean display_time; + gboolean hide_when_full; + gboolean tooltip_display_percentage; + gboolean tooltip_display_time; + int low_percentage; + int critical_percentage; + int action_on_low; + int action_on_critical; + char *command_on_low; + char *command_on_critical; + float hsize; + float vsize; +} t_battmon_options; + +typedef struct +{ + XfcePanelPlugin *plugin; + + GtkTooltips *tips; + GtkWidget *vbox; /* Widgets */ + GtkWidget *ebox; + GtkWidget *battstatus; + int timeoutid; /* To update apm status */ + int avail_methods; + int method; + gboolean low; + gboolean critical; + t_battmon_options options; + GdkColor colorH; + GdkColor colorL; + GdkColor colorC; + GtkLabel *label; + GtkLabel *charge; + GtkLabel *rtime; + GtkLabel *acfan; + GtkLabel *temp; + GtkWidget *image; +} t_battmon; + +typedef struct +{ + GtkWidget *cb_disp_power; + GtkWidget *cb_disp_label; + GtkWidget *cb_disp_percentage; + GtkWidget *cb_disp_time; + GtkWidget *cb_hide_when_full; + GtkWidget *cb_disp_tooltip_percentage; + GtkWidget *cb_disp_tooltip_time; + GtkWidget *cb_disp_icon; + GtkWidget *sb_low_percentage; + GtkWidget *sb_critical_percentage; + GtkWidget *om_action_low; + GtkWidget *om_action_critical; + GtkWidget *en_command_low; + GtkWidget *en_command_critical; + t_battmon *battmon; +} t_battmon_dialog; + +enum {BM_DO_NOTHING, BM_MESSAGE, BM_COMMAND, BM_COMMAND_TERM}; + +#endif diff -urN --exclude *Po --exclude Makefile xfce4-battery-plugin-0.5.0.orig/panel-plugin/libacpi.c xfce4-battery-plugin-0.5.0/panel-plugin/libacpi.c --- xfce4-battery-plugin-0.5.0.orig/panel-plugin/libacpi.c Wed Jan 17 23:56:51 2007 +++ xfce4-battery-plugin-0.5.0/panel-plugin/libacpi.c Sun May 20 20:44:00 2007 @@ -45,18 +45,22 @@ #include #include +#include + #endif #include "libacpi.h" +#ifdef __linux #define ACBASE "/proc/acpi/ac_adapter" - static char batteries[MAXBATT][128]; static char battinfo[MAXBATT][128]; +#endif + #ifndef __linux__ #if HAVE_SYSCTL - static int +static int name2oid(char *name, int *oidp) { int oid[2]; @@ -103,16 +107,19 @@ static int get_var(int *oid, int nlen) { - int retval=0; + int retval=0; u_char buf[BUFSIZ], *val, *p; char name[BUFSIZ], *fmt, *sep; int qoid[CTL_MAXNAME+2]; int i; size_t j, len; u_int kind; -/* int (*func)(int, void *);*/ qoid[0] = 0; + + if (nlen < 0) + return (0); + memcpy(qoid + 2, oid, nlen * sizeof(int)); qoid[1] = 1; @@ -138,9 +145,13 @@ val[len] = '\0'; fmt = buf; oidfmt(oid, nlen, fmt, &kind); + + if ((kind & CTLTYPE) == CTLTYPE_NODE) { + return (0); + } + p = val; - switch (*fmt) { - case 'I': + if (*fmt == 'I') { #ifdef DEBUG printf("I:%s%s", name, sep); #endif @@ -165,29 +176,30 @@ } return (retval); - default: - printf("%s%s", name, sep); +/* } else { + + printf("%s%s", name, sep); printf("Format:%s Length:%d Dump:0x", fmt, len); while (len-- && (p < val + 16)) printf("%02x", *p++); if (len > 16) printf("..."); return (0); +*/ } return (0); } - -#endif -#endif +#endif // HAVE_SYSCTL +#endif // ifndef __linux__ /* see if we have ACPI support */ int check_acpi(void) { +#ifdef __linux__ DIR *battdir; struct dirent *batt; char *name; -#ifdef __linux__ FILE *acpi; if (!(acpi = fopen ("/proc/acpi/info", "r"))) @@ -241,25 +253,17 @@ { static char buf[BUFSIZ]; char *bufp=buf; - char fmt[BUFSIZ]; - void *oldp=(void *)buf; - size_t oldlenp=BUFSIZ; int len,mib[CTL_MAXNAME]; - u_int kind; -/* snprintf(buf, BUFSIZ, "%s", "hw.acpi.battery.time");*/ + snprintf(buf, BUFSIZ, "%s", "hw.acpi.battery.units"); len = name2oid(bufp, mib); - if (len <=0) return 1; - if (oidfmt(mib, len, fmt, &kind)) return 1; - if ((kind & CTLTYPE) == CTLTYPE_NODE) return 1; - batt_count=get_var(mib, len); - + batt_count = get_var(mib, len); } return 0; #else return 1; -#endif -#endif +#endif // HAVE_SYSCTL +#endif // __linux__ } int read_acad_state(void) @@ -326,34 +330,24 @@ #else #ifdef HAVE_SYSCTL static char buf[BUFSIZ]; - char fmt[BUFSIZ]; - void *oldp=(void *)buf; char *bufp=buf; - size_t oldlenp=BUFSIZ; int len,mib[CTL_MAXNAME]; - u_int kind; int retval; + snprintf(buf, BUFSIZ, "%s", "hw.acpi.acline"); len = name2oid(bufp, mib); - if (len <= 0) return(-1); - if (oidfmt(mib, len, fmt, &kind)) - err(1, "couldn't find format of oid '%s'", bufp); - if (len < 0) errx(1, "unknown oid '%s'", bufp); - if ((kind & CTLTYPE) == CTLTYPE_NODE) { - printf("oh-oh...\n"); - } else { - retval=get_var(mib, len); + retval = get_var(mib, len); #ifdef DEBUG - printf("retval=%d\n",retval); -#endif - } + printf("retval=%d\n",retval); +#endif // DEBUG return retval; #else return 0; -#endif -#endif +#endif // HAVE_SYSCTL +#endif // __linux__ } + int read_acpi_info(int battery) { #ifdef __linux__ @@ -480,31 +474,25 @@ static char buf[BUFSIZ]; char *bufp=buf; int len,mib[CTL_MAXNAME]; - char fmt[BUFSIZ]; - u_int kind; int retval; - if (!acpiinfo) acpiinfo=(ACPIinfo *)malloc(sizeof(ACPIinfo)); - acpiinfo->present = 0; - acpiinfo->design_capacity = 0; - acpiinfo->last_full_capacity = 0; - acpiinfo->battery_technology = 0; - acpiinfo->design_voltage = 0; - acpiinfo->design_capacity_warning = 0; - acpiinfo->design_capacity_low = 0; + + if (!acpiinfo) + acpiinfo=(ACPIinfo *)malloc(sizeof(ACPIinfo)); + acpiinfo->present = 0; + acpiinfo->design_capacity = 0; + acpiinfo->last_full_capacity = 0; + acpiinfo->battery_technology = 0; + acpiinfo->design_voltage = 0; + acpiinfo->design_capacity_warning = 0; + acpiinfo->design_capacity_low = 0; + snprintf(buf, BUFSIZ, "%s", "hw.acpi.battery.units"); len = name2oid(bufp, mib); - if (len <= 0) return(-1); - if (oidfmt(mib, len, fmt, &kind)) - err(1, "couldn't find format of oid '%s'", bufp); - if (len < 0) errx(1, "unknown oid '%s'", bufp); - if ((kind & CTLTYPE) == CTLTYPE_NODE) { - printf("oh-oh...\n"); - } else { - retval=get_var(mib, len); + retval = get_var(mib, len); #ifdef DEBUG printf("retval=%d\n",retval); #endif - } + acpiinfo->present = retval; return 1; #else @@ -609,7 +597,7 @@ else /* Battery not present */ { acpistate->present = 0; - acpistate->state = UNKNOW; + acpistate->state = ACPI_BATT_STAT_NOT_PRESENT; acpistate->prate = 0; acpistate->rcapacity = 0; acpistate->pvoltage = 0; @@ -622,18 +610,13 @@ return 1; #else #ifdef HAVE_SYSCTL - char *string; static char buf[BUFSIZ]; - char fmt[BUFSIZ]; char *bufp=buf; - void *oldp=(void *)buf; - size_t oldlenp=BUFSIZ; int len,mib[CTL_MAXNAME]; int retval; - u_int kind; if (!acpistate) acpistate=(ACPIstate *)malloc(sizeof(ACPIstate)); acpistate->present = 0; - acpistate->state = UNKNOW; + acpistate->state = ACPI_BATT_STAT_NOT_PRESENT; acpistate->prate = 0; acpistate->rcapacity = 0; acpistate->pvoltage = 0; @@ -642,35 +625,17 @@ snprintf(buf, BUFSIZ, "%s", "hw.acpi.battery.time"); len = name2oid(bufp, mib); - if (len <= 0) return(-1); - if (oidfmt(mib, len, fmt, &kind)) - err(1, "couldn't find format of oid '%s'", bufp); - if (len < 0) errx(1, "unknown oid '%s'", bufp); - if ((kind & CTLTYPE) == CTLTYPE_NODE) { - printf("oh-oh...\n"); - } else { - retval=get_var(mib, len); -#ifdef DEBUG - printf("retval=%d\n",retval); -#endif - } + retval = get_var(mib, len); acpistate->rtime =(retval<0)?0:retval; snprintf(buf, BUFSIZ, "%s", "hw.acpi.battery.life"); len = name2oid(bufp, mib); - if (len <= 0) return(-1); - if (oidfmt(mib, len, fmt, &kind)) - err(1, "couldn't find format of oid '%s'", bufp); - if (len < 0) errx(1, "unknown oid '%s'", bufp); - if ((kind & CTLTYPE) == CTLTYPE_NODE) { - printf("oh-oh...\n"); - } else { - retval=get_var(mib, len); -#ifdef DEBUG - printf("retval=%d\n",retval); -#endif - } - acpistate->percentage =retval; + acpistate->percentage = get_var(mib, len); + + snprintf(buf, BUFSIZ, "%s", "hw.acpi.battery.state"); + len = name2oid(bufp, mib); + acpistate->state = get_var(mib, len); + return 1; #else return 0; @@ -722,27 +687,17 @@ #else #ifdef HAVE_SYSCTL static char buf[BUFSIZ]; - char fmt[BUFSIZ]; char *bufp=buf; - void *oldp=(void *)buf; - size_t oldlenp=BUFSIZ; int len,mib[CTL_MAXNAME]; int retval; - u_int kind; + snprintf(buf, BUFSIZ, "%s", "hw.acpi.thermal.tz0.temperature"); len = name2oid(bufp, mib); - if (len <= 0) return(NULL); - if (oidfmt(mib, len, fmt, &kind)) - err(1, "couldn't find format of oid '%s'", bufp); - if (len < 0) errx(1, "unknown oid '%s'", bufp); - if ((kind & CTLTYPE) == CTLTYPE_NODE) { - printf("oh-oh...\n"); - } else { - retval=get_var(mib, len); + retval = get_var(mib, len); #ifdef DEBUG printf("retval=%d\n",retval); #endif - } + snprintf(buf, BUFSIZ, "%d C",(retval-2735)/10); return (const char *)buf; #else diff -urN --exclude *Po --exclude Makefile xfce4-battery-plugin-0.5.0.orig/panel-plugin/libacpi.h xfce4-battery-plugin-0.5.0/panel-plugin/libacpi.h --- xfce4-battery-plugin-0.5.0.orig/panel-plugin/libacpi.h Wed Jan 17 23:56:51 2007 +++ xfce4-battery-plugin-0.5.0/panel-plugin/libacpi.h Sun May 20 20:44:21 2007 @@ -21,19 +21,10 @@ #define MAXBATT 8 -typedef enum -{ - POWER, /* on AC, Battery charged */ - DISCHARGING, /* on Battery, Discharging */ - CHARGING, /* on AC, Charging */ - UNKNOW /* unknow */ -} -Charging; - typedef struct { int present; /* 1 if present, 0 if no battery */ - Charging state; /* charging state enum */ + int state; /* charging state enum */ int prate; /* present rate */ int rcapacity; /* rameining capacity */ int pvoltage; /* present voltage */