Battery: refactoring

This commit is contained in:
o9000 2015-11-23 16:12:42 +01:00
parent 8df91bd475
commit 33645f9b5a
6 changed files with 36 additions and 30 deletions

View file

@ -33,7 +33,7 @@
PangoFontDescription *bat1_font_desc;
PangoFontDescription *bat2_font_desc;
struct batstate battery_state;
struct BatteryState battery_state;
gboolean battery_enabled;
gboolean battery_tooltip_enabled;
int percentage_hide;
@ -43,7 +43,7 @@ static char buf_bat_percentage[10];
static char buf_bat_time[20];
int8_t battery_low_status;
unsigned char battery_low_cmd_sent;
gboolean battery_low_cmd_sent;
char *ac_connected_cmd;
char *ac_disconnected_cmd;
char *battery_low_cmd;
@ -52,14 +52,14 @@ char *battery_mclick_command;
char *battery_rclick_command;
char *battery_uwheel_command;
char *battery_dwheel_command;
int battery_found;
gboolean battery_found;
void update_battery_tick(void *arg)
{
if (!battery_enabled)
return;
int old_found = battery_found;
gboolean old_found = battery_found;
int old_percentage = battery_state.percentage;
gboolean old_ac_connected = battery_state.ac_connected;
int16_t old_hours = battery_state.time.hours;
@ -106,7 +106,7 @@ void update_battery_tick(void *arg)
panel_refresh = TRUE;
}
} else {
if (panels[i].battery.area.on_screen == 0) {
if (!panels[i].battery.area.on_screen) {
show(&panels[i].battery.area);
panel_refresh = TRUE;
}
@ -125,11 +125,11 @@ void update_battery_tick(void *arg)
void default_battery()
{
battery_enabled = 0;
battery_tooltip_enabled = 1;
battery_found = 0;
battery_enabled = FALSE;
battery_tooltip_enabled = TRUE;
battery_found = FALSE;
percentage_hide = 101;
battery_low_cmd_sent = 0;
battery_low_cmd_sent = FALSE;
battery_timeout = NULL;
bat1_font_desc = NULL;
bat2_font_desc = NULL;
@ -172,7 +172,7 @@ void cleanup_battery()
ac_disconnected_cmd = NULL;
stop_timeout(battery_timeout);
battery_timeout = NULL;
battery_found = 0;
battery_found = FALSE;
battery_os_free();
}
@ -239,7 +239,7 @@ int update_battery()
battery_state.state = BATTERY_UNKNOWN;
battery_state.percentage = 0;
battery_state.ac_connected = FALSE;
batstate_set_time(&battery_state, 0);
battery_state_set_time(&battery_state, 0);
err = battery_os_update(&battery_state);

View file

@ -26,22 +26,27 @@ typedef struct Battery {
int bat2_posy;
} Battery;
enum chargestate { BATTERY_UNKNOWN, BATTERY_CHARGING, BATTERY_DISCHARGING, BATTERY_FULL };
typedef enum ChargeState {
BATTERY_UNKNOWN = 0,
BATTERY_CHARGING,
BATTERY_DISCHARGING,
BATTERY_FULL,
} ChargeState;
typedef struct battime {
typedef struct BatteryTime {
int16_t hours;
int8_t minutes;
int8_t seconds;
} battime;
} BatteryTime;
typedef struct batstate {
typedef struct BatteryState {
int percentage;
struct battime time;
enum chargestate state;
BatteryTime time;
ChargeState state;
gboolean ac_connected;
} batstate;
} BatteryState;
extern struct batstate battery_state;
extern struct BatteryState battery_state;
extern PangoFontDescription *bat1_font_desc;
extern PangoFontDescription *bat2_font_desc;
extern gboolean battery_enabled;
@ -60,7 +65,7 @@ extern char *battery_rclick_command;
extern char *battery_uwheel_command;
extern char *battery_dwheel_command;
static inline gchar *chargestate2str(enum chargestate state)
static inline gchar *chargestate2str(ChargeState state)
{
switch (state) {
case BATTERY_CHARGING:
@ -75,7 +80,7 @@ static inline gchar *chargestate2str(enum chargestate state)
};
}
static inline void batstate_set_time(struct batstate *state, int seconds)
static inline void battery_state_set_time(BatteryState *state, int seconds)
{
state->time.hours = seconds / 3600;
seconds -= 3600 * state->time.hours;
@ -106,7 +111,7 @@ void battery_action(int button);
/* operating system specific functions */
gboolean battery_os_init();
void battery_os_free();
int battery_os_update(struct batstate *state);
int battery_os_update(BatteryState *state);
char *battery_os_tooltip();
#endif

View file

@ -33,7 +33,7 @@ void battery_os_free()
return;
}
int battery_os_update(struct batstate *state)
int battery_os_update(BatteryState *state)
{
return -1;
}

View file

@ -39,7 +39,7 @@ void battery_os_free()
return;
}
int battery_os_update(struct batstate *state)
int battery_os_update(BatteryState *state)
{
int sysctl_out = 0;
size_t len = sizeof(sysctl_out);
@ -63,7 +63,7 @@ int battery_os_update(struct batstate *state)
}
if (sysctlbyname("hw.acpi.battery.time", &sysctl_out, &len, NULL, 0) == 0)
batstate_set_time(state, sysctl_out * 60);
battery_state_set_time(state, sysctl_out * 60);
else
err = -1;

View file

@ -34,6 +34,7 @@ enum psy_type {
struct psy_battery {
/* generic properties */
gchar *name;
/* monotonic time, in microseconds */
gint64 timestamp;
/* sysfs files */
gchar *path_present;
@ -49,7 +50,7 @@ struct psy_battery {
gint energy_now;
gint energy_full;
gint power_now;
enum chargestate status;
ChargeState status;
};
struct psy_mains {
@ -382,7 +383,7 @@ static gboolean update_linux_mains(struct psy_mains *ac)
return TRUE;
}
int battery_os_update(struct batstate *state)
int battery_os_update(BatteryState *state)
{
GList *l;
@ -430,7 +431,7 @@ int battery_os_update(struct batstate *state)
else if (state->state == BATTERY_DISCHARGING)
seconds = 3600 * total_energy_now / total_power_now;
}
batstate_set_time(state, seconds);
battery_state_set_time(state, seconds);
/* calculate percentage */
state->percentage = energy_to_percent(total_energy_now, total_energy_full);

View file

@ -51,7 +51,7 @@ void battery_os_free()
apm_fd = -1;
}
int battery_os_update(struct batstate *state)
int battery_os_update(BatteryState *state)
{
struct apm_power_info info;
@ -73,7 +73,7 @@ int battery_os_update(struct batstate *state)
state->percentage = info.battery_life;
if (info.minutes_left != -1)
batstate_set_time(state, info.minutes_left * 60);
battery_state_set_time(state, info.minutes_left * 60);
state->ac_connected = info.ac_state == APM_AC_ON;
} else {