cbatticon.c (cbatticon-1.6.12) | : | cbatticon.c (cbatticon-1.6.13) | ||
---|---|---|---|---|
skipping to change at line 24 | skipping to change at line 24 | |||
* | * | |||
* This program is distributed in the hope that it will be useful, | * This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | * GNU General Public License for more details. | |||
* | * | |||
* You should have received a copy of the GNU General Public License along | * You should have received a copy of the GNU General Public License along | |||
* with this program. If not, see <http://www.gnu.org/licenses/>. | * with this program. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | */ | |||
#define CBATTICON_VERSION_NUMBER 1.6.12 | #define CBATTICON_VERSION_NUMBER 1.6.13 | |||
#define CBATTICON_VERSION_STRING "1.6.12" | #define CBATTICON_VERSION_STRING "1.6.13" | |||
#define CBATTICON_STRING "cbatticon" | #define CBATTICON_STRING "cbatticon" | |||
#include <glib.h> | #include <glib.h> | |||
#include <glib/gi18n.h> | #include <glib/gi18n.h> | |||
#include <glib/gprintf.h> | #include <glib/gprintf.h> | |||
#include <glib/gstdio.h> | #include <glib/gstdio.h> | |||
#include <gtk/gtk.h> | #include <gtk/gtk.h> | |||
#ifdef WITH_NOTIFY | #ifdef WITH_NOTIFY | |||
#include <libnotify/notify.h> | #include <libnotify/notify.h> | |||
#endif | #endif | |||
skipping to change at line 57 | skipping to change at line 57 | |||
static gboolean get_sysattr_string (gchar *path, gchar *attribute, gchar **value ); | static gboolean get_sysattr_string (gchar *path, gchar *attribute, gchar **value ); | |||
static gboolean get_sysattr_double (gchar *path, gchar *attribute, gdouble *valu e); | static gboolean get_sysattr_double (gchar *path, gchar *attribute, gdouble *valu e); | |||
static gboolean get_ac_online (gchar *path, gboolean *online); | static gboolean get_ac_online (gchar *path, gboolean *online); | |||
static gboolean get_battery_present (gchar *path, gboolean *present); | static gboolean get_battery_present (gchar *path, gboolean *present); | |||
static gboolean get_battery_status (gint *status); | static gboolean get_battery_status (gint *status); | |||
static gboolean get_battery_full_capacity (gboolean *use_charge, gdouble *capaci ty); | static gboolean get_battery_full_capacity (gboolean *use_charge, gdouble *capaci ty); | |||
static gboolean get_battery_remaining_capacity (gboolean use_charge, gdouble *ca pacity); | static gboolean get_battery_remaining_capacity (gboolean use_charge, gdouble *ca pacity); | |||
static gboolean get_battery_remaining_capacity_pct (gdouble *capacity); | ||||
static gboolean get_battery_current_rate (gboolean use_charge, gdouble *rate); | static gboolean get_battery_current_rate (gboolean use_charge, gdouble *rate); | |||
static gboolean get_battery_charge (gboolean remaining, gint *percentage, gint * time); | static gboolean get_battery_charge (gboolean remaining, gint *percentage, gint * time); | |||
static gboolean get_battery_time_estimation (gdouble remaining_capacity, gdouble y, gint *time); | static gboolean get_battery_time_estimation (gdouble remaining_capacity, gdouble y, gint *time); | |||
static void reset_battery_time_estimation (void); | static void reset_battery_time_estimation (void); | |||
static void create_tray_icon (void); | static void create_tray_icon (void); | |||
static gboolean update_tray_icon (GtkStatusIcon *tray_icon); | static gboolean update_tray_icon (GtkStatusIcon *tray_icon); | |||
static void update_tray_icon_status (GtkStatusIcon *tray_icon); | static void update_tray_icon_status (GtkStatusIcon *tray_icon); | |||
static void on_tray_icon_click (GtkStatusIcon *tray_icon, gpointer user_data); | static void on_tray_icon_click (GtkStatusIcon *tray_icon, gpointer user_data); | |||
skipping to change at line 113 | skipping to change at line 114 | |||
CRITICAL_LEVEL | CRITICAL_LEVEL | |||
}; | }; | |||
struct configuration { | struct configuration { | |||
gboolean display_version; | gboolean display_version; | |||
gboolean debug_output; | gboolean debug_output; | |||
gint update_interval; | gint update_interval; | |||
gint icon_type; | gint icon_type; | |||
gint low_level; | gint low_level; | |||
gint critical_level; | gint critical_level; | |||
gchar *command_low_level; | ||||
gchar *command_critical_level; | gchar *command_critical_level; | |||
gchar *command_left_click; | gchar *command_left_click; | |||
#ifdef WITH_NOTIFY | #ifdef WITH_NOTIFY | |||
gboolean hide_notification; | gboolean hide_notification; | |||
#endif | #endif | |||
gboolean list_icon_types; | gboolean list_icon_types; | |||
gboolean list_power_supplies; | gboolean list_power_supplies; | |||
} configuration = { | } configuration = { | |||
FALSE, | FALSE, | |||
FALSE, | FALSE, | |||
skipping to change at line 167 | skipping to change at line 169 | |||
gchar *icon_type_string = NULL; | gchar *icon_type_string = NULL; | |||
GOptionContext *option_context; | GOptionContext *option_context; | |||
GOptionEntry option_entries[] = { | GOptionEntry option_entries[] = { | |||
{ "version" , 'v', 0, G_OPTION_ARG_NONE , &configuration. display_version , N_("Display the version") , NULL }, | { "version" , 'v', 0, G_OPTION_ARG_NONE , &configuration. display_version , N_("Display the version") , NULL }, | |||
{ "debug" , 'd', 0, G_OPTION_ARG_NONE , &configuration. debug_output , N_("Display debug information") , NULL }, | { "debug" , 'd', 0, G_OPTION_ARG_NONE , &configuration. debug_output , N_("Display debug information") , NULL }, | |||
{ "update-interval" , 'u', 0, G_OPTION_ARG_INT , &configuration. update_interval , N_("Set update interval (in seconds)") , NULL }, | { "update-interval" , 'u', 0, G_OPTION_ARG_INT , &configuration. update_interval , N_("Set update interval (in seconds)") , NULL }, | |||
{ "icon-type" , 'i', 0, G_OPTION_ARG_STRING, &icon_type_stri ng , N_("Set icon type ('standard', 'notification' or 'symbol ic')") , NULL }, | { "icon-type" , 'i', 0, G_OPTION_ARG_STRING, &icon_type_stri ng , N_("Set icon type ('standard', 'notification' or 'symbol ic')") , NULL }, | |||
{ "low-level" , 'l', 0, G_OPTION_ARG_INT , &configuration. low_level , N_("Set low battery level (in percent)") , NULL }, | { "low-level" , 'l', 0, G_OPTION_ARG_INT , &configuration. low_level , N_("Set low battery level (in percent)") , NULL }, | |||
{ "critical-level" , 'r', 0, G_OPTION_ARG_INT , &configuration. critical_level , N_("Set critical battery level (in percent)") , NULL }, | { "critical-level" , 'r', 0, G_OPTION_ARG_INT , &configuration. critical_level , N_("Set critical battery level (in percent)") , NULL }, | |||
{ "command-low-level" , 'o', 0, G_OPTION_ARG_STRING, &configuration. command_low_level , N_("Command to execute when low battery level is reached ") , NULL }, | ||||
{ "command-critical-level", 'c', 0, G_OPTION_ARG_STRING, &configuration. command_critical_level, N_("Command to execute when critical battery level is re ached"), NULL }, | { "command-critical-level", 'c', 0, G_OPTION_ARG_STRING, &configuration. command_critical_level, N_("Command to execute when critical battery level is re ached"), NULL }, | |||
{ "command-left-click" , 'x', 0, G_OPTION_ARG_STRING, &configuration. command_left_click , N_("Command to execute when left clicking on tray icon") , NULL }, | { "command-left-click" , 'x', 0, G_OPTION_ARG_STRING, &configuration. command_left_click , N_("Command to execute when left clicking on tray icon") , NULL }, | |||
#ifdef WITH_NOTIFY | #ifdef WITH_NOTIFY | |||
{ "hide-notification" , 'n', 0, G_OPTION_ARG_NONE , &configuration. hide_notification , N_("Hide the notification popups") , NULL }, | { "hide-notification" , 'n', 0, G_OPTION_ARG_NONE , &configuration. hide_notification , N_("Hide the notification popups") , NULL }, | |||
#endif | #endif | |||
{ "list-icon-types" , 't', 0, G_OPTION_ARG_NONE , &configuration. list_icon_types , N_("List available icon types") , NULL }, | { "list-icon-types" , 't', 0, G_OPTION_ARG_NONE , &configuration. list_icon_types , N_("List available icon types") , NULL }, | |||
{ "list-power-supplies" , 'p', 0, G_OPTION_ARG_NONE , &configuration. list_power_supplies , N_("List available power supplies (battery and AC)") , NULL }, | { "list-power-supplies" , 'p', 0, G_OPTION_ARG_NONE , &configuration. list_power_supplies , N_("List available power supplies (battery and AC)") , NULL }, | |||
{ NULL } | { NULL } | |||
}; | }; | |||
skipping to change at line 593 | skipping to change at line 596 | |||
{ | { | |||
g_return_val_if_fail (capacity != NULL, FALSE); | g_return_val_if_fail (capacity != NULL, FALSE); | |||
if (use_charge == FALSE) { | if (use_charge == FALSE) { | |||
return get_sysattr_double (battery_path, "energy_now", capacity); | return get_sysattr_double (battery_path, "energy_now", capacity); | |||
} else { | } else { | |||
return get_sysattr_double (battery_path, "charge_now", capacity); | return get_sysattr_double (battery_path, "charge_now", capacity); | |||
} | } | |||
} | } | |||
static gboolean get_battery_remaining_capacity_pct (gdouble *capacity) | ||||
{ | ||||
g_return_val_if_fail (capacity != NULL, FALSE); | ||||
return get_sysattr_double (battery_path, "capacity", capacity); | ||||
} | ||||
static gboolean get_battery_current_rate (gboolean use_charge, gdouble *rate) | static gboolean get_battery_current_rate (gboolean use_charge, gdouble *rate) | |||
{ | { | |||
if (use_charge == FALSE) { | if (use_charge == FALSE) { | |||
return get_sysattr_double (battery_path, "power_now", rate); | return get_sysattr_double (battery_path, "power_now", rate); | |||
} else { | } else { | |||
return get_sysattr_double (battery_path, "current_now", rate); | return get_sysattr_double (battery_path, "current_now", rate); | |||
} | } | |||
} | } | |||
/* | /* | |||
skipping to change at line 622 | skipping to change at line 632 | |||
if (get_battery_full_capacity (&use_charge, &full_capacity) == FALSE) { | if (get_battery_full_capacity (&use_charge, &full_capacity) == FALSE) { | |||
if (configuration.debug_output == TRUE) { | if (configuration.debug_output == TRUE) { | |||
g_printf ("full capacity: %s\n", "unavailable"); | g_printf ("full capacity: %s\n", "unavailable"); | |||
} | } | |||
return FALSE; | return FALSE; | |||
} | } | |||
if (get_battery_remaining_capacity (use_charge, &remaining_capacity) == FALS E) { | if (get_battery_remaining_capacity (use_charge, &remaining_capacity) == FALS E) { | |||
if (configuration.debug_output == TRUE) { | if (get_battery_remaining_capacity_pct (&remaining_capacity) == FALSE) { | |||
g_printf ("remaining capacity: %s\n", "unavailable"); | if (configuration.debug_output == TRUE) { | |||
g_printf ("remaining capacity: %s\n", "unavailable"); | ||||
} | ||||
return FALSE; | ||||
} | } | |||
return FALSE; | /* remaining capacity is percentage, compute the actual remaining capaci | |||
ty */ | ||||
remaining_capacity *= full_capacity / 100.0; | ||||
} | } | |||
*percentage = (gint)fmin (floor (remaining_capacity / full_capacity * 100.0) , 100.0); | *percentage = (gint)fmin (floor (remaining_capacity / full_capacity * 100.0) , 100.0); | |||
if (time == NULL) { | if (time == NULL) { | |||
return TRUE; | return TRUE; | |||
} | } | |||
if (estimation_needed == TRUE) { | if (estimation_needed == TRUE) { | |||
if (remaining == TRUE) { | if (remaining == TRUE) { | |||
skipping to change at line 738 | skipping to change at line 753 | |||
static gint old_battery_status = -1; | static gint old_battery_status = -1; | |||
/* battery statuses: */ | /* battery statuses: */ | |||
/* not present => ac_only, battery_missing */ | /* not present => ac_only, battery_missing */ | |||
/* present => charging, charged, discharging, unknown */ | /* present => charging, charged, discharging, unknown */ | |||
/* (present and not present are exclusive) */ | /* (present and not present are exclusive) */ | |||
static gboolean ac_only = FALSE; | static gboolean ac_only = FALSE; | |||
static gboolean battery_low = FALSE; | static gboolean battery_low = FALSE; | |||
static gboolean battery_critical = FALSE; | static gboolean battery_critical = FALSE; | |||
static gboolean spawn_command_low = FALSE; | ||||
static gboolean spawn_command_critical = FALSE; | static gboolean spawn_command_critical = FALSE; | |||
gint percentage, time; | gint percentage, time; | |||
gchar *battery_string, *time_string; | gchar *battery_string, *time_string; | |||
#ifdef WITH_NOTIFY | #ifdef WITH_NOTIFY | |||
static NotifyNotification *notification = NULL; | static NotifyNotification *notification = NULL; | |||
#endif | #endif | |||
/* update power supplies */ | /* update power supplies */ | |||
if (changed_power_supplies () == TRUE) | if (changed_power_supplies () == TRUE) | |||
{ | { | |||
get_power_supplies (); | get_power_supplies (); | |||
old_battery_status = -1; | old_battery_status = -1; | |||
ac_only = FALSE; | ac_only = FALSE; | |||
battery_low = FALSE; | battery_low = FALSE; | |||
battery_critical = FALSE; | battery_critical = FALSE; | |||
spawn_command_low = FALSE; | ||||
spawn_command_critical = FALSE; | spawn_command_critical = FALSE; | |||
} | } | |||
/* update tray icon for AC only */ | /* update tray icon for AC only */ | |||
if (battery_path == NULL) { | if (battery_path == NULL) { | |||
if (ac_only == FALSE) { | if (ac_only == FALSE) { | |||
ac_only = TRUE; | ac_only = TRUE; | |||
NOTIFY_MESSAGE (¬ification, _("AC only, no battery!"), NULL, NOTI FY_EXPIRES_NEVER, NOTIFY_URGENCY_NORMAL); | NOTIFY_MESSAGE (¬ification, _("AC only, no battery!"), NULL, NOTI FY_EXPIRES_NEVER, NOTIFY_URGENCY_NORMAL); | |||
skipping to change at line 864 | skipping to change at line 881 | |||
battery_string = get_battery_string (battery_status, percentage); | battery_string = get_battery_string (battery_status, percentage); | |||
time_string = get_time_string (time); | time_string = get_time_string (time); | |||
if (old_battery_status != DISCHARGING) { | if (old_battery_status != DISCHARGING) { | |||
old_battery_status = DISCHARGING; | old_battery_status = DISCHARGING; | |||
NOTIFY_MESSAGE (¬ification, battery_string, time_string, NOTI FY_EXPIRES_DEFAULT, NOTIFY_URGENCY_NORMAL); | NOTIFY_MESSAGE (¬ification, battery_string, time_string, NOTI FY_EXPIRES_DEFAULT, NOTIFY_URGENCY_NORMAL); | |||
battery_low = FALSE; | battery_low = FALSE; | |||
battery_critical = FALSE; | battery_critical = FALSE; | |||
spawn_command_low = FALSE; | ||||
spawn_command_critical = FALSE; | spawn_command_critical = FALSE; | |||
} | } | |||
if (battery_low == FALSE && percentage <= configuration.low_level) { | if (battery_low == FALSE && percentage <= configuration.low_level) { | |||
battery_low = TRUE; | battery_low = TRUE; | |||
battery_string = get_battery_string (LOW_LEVEL, percentage); | battery_string = get_battery_string (LOW_LEVEL, percentage); | |||
NOTIFY_MESSAGE (¬ification, battery_string, time_string, NOTI FY_EXPIRES_NEVER, NOTIFY_URGENCY_NORMAL); | NOTIFY_MESSAGE (¬ification, battery_string, time_string, NOTI FY_EXPIRES_NEVER, NOTIFY_URGENCY_NORMAL); | |||
spawn_command_low = TRUE; | ||||
} | } | |||
if (battery_critical == FALSE && percentage <= configuration.critica l_level) { | if (battery_critical == FALSE && percentage <= configuration.critica l_level) { | |||
battery_critical = TRUE; | battery_critical = TRUE; | |||
battery_string = get_battery_string (CRITICAL_LEVEL, percentage) ; | battery_string = get_battery_string (CRITICAL_LEVEL, percentage) ; | |||
NOTIFY_MESSAGE (¬ification, battery_string, time_string, NOTI FY_EXPIRES_NEVER, NOTIFY_URGENCY_CRITICAL); | NOTIFY_MESSAGE (¬ification, battery_string, time_string, NOTI FY_EXPIRES_NEVER, NOTIFY_URGENCY_CRITICAL); | |||
spawn_command_critical = TRUE; | spawn_command_critical = TRUE; | |||
} | } | |||
gtk_status_icon_set_tooltip_text (tray_icon, get_tooltip_string (bat tery_string, time_string)); | gtk_status_icon_set_tooltip_text (tray_icon, get_tooltip_string (bat tery_string, time_string)); | |||
gtk_status_icon_set_from_icon_name (tray_icon, get_icon_name (batter y_status, percentage)); | gtk_status_icon_set_from_icon_name (tray_icon, get_icon_name (batter y_status, percentage)); | |||
if (spawn_command_low == TRUE) { | ||||
spawn_command_low = FALSE; | ||||
if (configuration.command_low_level != NULL) { | ||||
syslog (LOG_CRIT, _("Spawning low battery level command in 5 | ||||
seconds: %s"), configuration.command_low_level); | ||||
g_usleep (G_USEC_PER_SEC * 5); | ||||
if (get_battery_status (&battery_status) == TRUE) { | ||||
if (battery_status != DISCHARGING && battery_status != N | ||||
OT_CHARGING) { | ||||
syslog (LOG_NOTICE, _("Skipping low battery level co | ||||
mmand, no longer discharging")); | ||||
return; | ||||
} | ||||
} | ||||
if (g_spawn_command_line_async (configuration.command_low_le | ||||
vel, &error) == FALSE) { | ||||
syslog (LOG_CRIT, _("Cannot spawn low battery level comm | ||||
and: %s\n"), error->message); | ||||
g_printerr (_("Cannot spawn low battery level command: % | ||||
s\n"), error->message); | ||||
g_error_free (error); error = NULL; | ||||
#ifdef WITH_NOTIFY | ||||
static NotifyNotification *spawn_notification = NULL; | ||||
NOTIFY_MESSAGE (&spawn_notification, _("Cannot spawn low | ||||
battery level command!"), configuration.command_low_level, NOTIFY_EXPIRES_NEVER | ||||
, NOTIFY_URGENCY_CRITICAL); | ||||
#endif | ||||
} | ||||
} | ||||
} | ||||
if (spawn_command_critical == TRUE) { | if (spawn_command_critical == TRUE) { | |||
spawn_command_critical = FALSE; | spawn_command_critical = FALSE; | |||
if (configuration.command_critical_level != NULL) { | if (configuration.command_critical_level != NULL) { | |||
syslog (LOG_CRIT, _("Spawning critical battery level command in 30 seconds: %s"), configuration.command_critical_level); | syslog (LOG_CRIT, _("Spawning critical battery level command in 30 seconds: %s"), configuration.command_critical_level); | |||
g_usleep (G_USEC_PER_SEC * 30); | g_usleep (G_USEC_PER_SEC * 30); | |||
if (get_battery_status (&battery_status) == TRUE) { | if (get_battery_status (&battery_status) == TRUE) { | |||
if (battery_status != DISCHARGING && battery_status != N OT_CHARGING) { | if (battery_status != DISCHARGING && battery_status != N OT_CHARGING) { | |||
syslog (LOG_NOTICE, _("Skipping critical battery lev el command, no longer discharging")); | syslog (LOG_NOTICE, _("Skipping critical battery lev el command, no longer discharging")); | |||
End of changes. 12 change blocks. | ||||
5 lines changed or deleted | 62 lines changed or added |