E17: fix crash of the battery module on OpenBSD if the system has no AC adapter or battery sensor

SVN revision: 79630
This commit is contained in:
Vincent Torri 2012-11-24 10:19:55 +00:00
parent 4ff82885e8
commit 8058b50c82
1 changed files with 83 additions and 74 deletions

View File

@ -15,8 +15,8 @@ extern Eina_List *device_batteries;
extern Eina_List *device_ac_adapters;
extern double init_time;
Ac_Adapter *ac;
Battery *bat;
Ac_Adapter *ac = NULL;
Battery *bat = NULL;
int
_battery_openbsd_start(void)
@ -106,80 +106,89 @@ _battery_openbsd_battery_update()
struct sensor s;
size_t slen = sizeof(struct sensor);
/* update the poller interval */
ecore_poller_poller_interval_set(bat->poll,
battery_config->poll_interval);
/* last full capacity */
bat->mib[3] = 8;
bat->mib[4] = 0;
if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) != -1)
if (bat)
{
bat->last_full_charge = (double)s.value;
}
/* update the poller interval */
ecore_poller_poller_interval_set(bat->poll,
battery_config->poll_interval);
/* last full capacity */
bat->mib[3] = 8;
bat->mib[4] = 0;
if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) != -1)
{
bat->last_full_charge = (double)s.value;
}
/* remaining capcity */
bat->mib[3] = 8;
bat->mib[4] = 3;
if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) != -1)
{
charge = (double)s.value;
}
_time = ecore_time_get();
if ((bat->got_prop) && (charge != bat->current_charge))
bat->charge_rate = ((charge - bat->current_charge) / (_time - bat->last_update));
bat->last_update = _time;
bat->current_charge = charge;
bat->percent = 100 * (bat->current_charge / bat->last_full_charge);
if (bat->got_prop)
{
if (bat->charge_rate > 0)
{
if (battery_config->fuzzy && (++battery_config->fuzzcount <= 10) && (bat->time_full > 0))
bat->time_full = (((bat->last_full_charge - bat->current_charge) / bat->charge_rate) + bat->time_full) / 2;
else
bat->time_full = (bat->last_full_charge - bat->current_charge) / bat->charge_rate;
bat->time_left = -1;
}
else
{
if (battery_config->fuzzy && (battery_config->fuzzcount <= 10) && (bat->time_left > 0))
bat->time_left = (((0 - bat->current_charge) / bat->charge_rate) + bat->time_left) / 2;
else
bat->time_left = (0 - bat->current_charge) / bat->charge_rate;
bat->time_full = -1;
}
}
else
{
bat->time_full = -1;
bat->time_left = -1;
}
/* battery state 1: discharge, 2: charge */
bat->mib[3] = 10;
bat->mib[4] = 0;
if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) == -1)
{
if (s.value == 2)
bat->charging = 1;
else
bat->charging = 0;
}
}
/* remaining capcity */
bat->mib[3] = 8;
bat->mib[4] = 3;
if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) != -1)
if (ac)
{
charge = (double)s.value;
}
/* AC State */
ac->mib[3] = 9;
ac->mib[4] = 0;
if (sysctl(ac->mib, 5, &s, &slen, NULL, 0) == -1)
{
if (s.value)
ac->present = 1;
else
ac->present = 0;
}
}
_time = ecore_time_get();
if ((bat->got_prop) && (charge != bat->current_charge))
bat->charge_rate = ((charge - bat->current_charge) / (_time - bat->last_update));
bat->last_update = _time;
bat->current_charge = charge;
bat->percent = 100 * (bat->current_charge / bat->last_full_charge);
if (bat->got_prop)
{
if (bat->charge_rate > 0)
{
if (battery_config->fuzzy && (++battery_config->fuzzcount <= 10) && (bat->time_full > 0))
bat->time_full = (((bat->last_full_charge - bat->current_charge) / bat->charge_rate) + bat->time_full) / 2;
else
bat->time_full = (bat->last_full_charge - bat->current_charge) / bat->charge_rate;
bat->time_left = -1;
}
else
{
if (battery_config->fuzzy && (battery_config->fuzzcount <= 10) && (bat->time_left > 0))
bat->time_left = (((0 - bat->current_charge) / bat->charge_rate) + bat->time_left) / 2;
else
bat->time_left = (0 - bat->current_charge) / bat->charge_rate;
bat->time_full = -1;
}
}
else
{
bat->time_full = -1;
bat->time_left = -1;
}
/* battery state 1: discharge, 2: charge */
bat->mib[3] = 10;
bat->mib[4] = 0;
if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) == -1)
{
if (s.value == 2)
bat->charging = 1;
else
bat->charging = 0;
}
/* AC State */
ac->mib[3] = 9;
ac->mib[4] = 0;
if (sysctl(ac->mib, 5, &s, &slen, NULL, 0) == -1)
{
if (s.value)
ac->present = 1;
else
ac->present = 0;
}
if (bat->got_prop)
_battery_device_update();
bat->got_prop = 1;
if (bat)
{
if (bat->got_prop)
_battery_device_update();
bat->got_prop = 1;
}
}