update for beta release
[framework/uifw/e17.git] / src / modules / battery / e_mod_openbsd.c
1
2 #include <err.h>
3
4 #include <sys/param.h>
5 #include <sys/sysctl.h>
6 #include <sys/sensors.h>
7
8 #include "e.h"
9 #include "e_mod_main.h"
10
11 static Eina_Bool _battery_openbsd_battery_update_poll(void *data);
12 static void _battery_openbsd_battery_update();
13
14 extern Eina_List *device_batteries;
15 extern Eina_List *device_ac_adapters;
16 extern double init_time;
17
18 Ac_Adapter *ac;
19 Battery *bat;
20
21 int 
22 _battery_openbsd_start(void)
23 {
24    Eina_List *devices;
25    int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0};
26    int devn;
27    struct sensordev snsrdev;
28    size_t sdlen = sizeof(struct sensordev);
29    struct sensor s;
30    size_t slen = sizeof(struct sensor);
31
32    for (devn = 0 ; ; devn++) {
33         mib[2] = devn;
34         if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1) {
35                 if (errno == ENXIO)
36                         continue;
37                 if (errno == ENOENT)
38                         break;
39         }
40
41         if (!strcmp("acpibat0", snsrdev.xname)) {
42                 if (!(bat = E_NEW(Battery, 1)))
43                         return 0;
44                 bat->udi = eina_stringshare_add("acpibat0"),
45                 bat->mib = malloc(sizeof(int) * 5);
46                 bat->mib[0] = mib[0];
47                 bat->mib[1] = mib[1];
48                 bat->mib[2] = mib[2];   
49                 bat->technology = eina_stringshare_add("Unknow");
50                 bat->model = eina_stringshare_add("Unknow");
51                 bat->vendor = eina_stringshare_add("Unknow");
52                 bat->last_update = ecore_time_get();
53                 bat->poll = ecore_poller_add(ECORE_POLLER_CORE, 
54                                 battery_config->poll_interval, 
55                                 _battery_openbsd_battery_update_poll, NULL);
56                 device_batteries = eina_list_append(device_batteries, bat);
57         }
58         else if (!strcmp("acpiac0", snsrdev.xname)) {
59                 if (!(ac = E_NEW(Ac_Adapter, 1)))
60                    return 0;
61                 ac->udi = eina_stringshare_add("acpiac0");
62                 ac->mib = malloc(sizeof(int) * 5);
63                 ac->mib[0] = mib[0];
64                 ac->mib[1] = mib[1];
65                 ac->mib[2] = mib[2];    
66                 device_ac_adapters = eina_list_append(device_ac_adapters, ac);
67         }
68    }
69
70    _battery_openbsd_battery_update();
71
72    init_time = ecore_time_get();
73    return 1;
74 }
75
76 void 
77 _battery_openbsd_stop(void)
78 {
79    if (ac)
80       free(ac);
81    if (bat) {
82       eina_stringshare_del(bat->udi);
83       eina_stringshare_del(bat->technology);
84       eina_stringshare_del(bat->model);
85       eina_stringshare_del(bat->vendor);
86       ecore_poller_del(bat->poll);
87       free(bat->mib);
88       free(bat);
89    }
90 }
91
92 static Eina_Bool 
93 _battery_openbsd_battery_update_poll(void *data)
94 {
95         _battery_openbsd_battery_update();
96         return EINA_TRUE;
97 }
98
99 static void 
100 _battery_openbsd_battery_update()
101 {
102         double time, charge;
103         struct sensor s;
104         size_t slen = sizeof(struct sensor);
105    
106         /* update the poller interval */
107         ecore_poller_poller_interval_set(bat->poll,
108             battery_config->poll_interval);
109
110         /* last full capacity */
111         bat->mib[3] = 8;
112         bat->mib[4] = 0;
113         if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) != -1) {
114                 bat->last_full_charge = (double)s.value;
115         }
116                 
117         /* remaining capcity */
118         bat->mib[3] = 8;
119         bat->mib[4] = 3;
120         if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) != -1) {
121                 charge = (double)s.value;
122         }
123
124         time = ecore_time_get();
125         if ((bat->got_prop) && (charge != bat->current_charge))
126           bat->charge_rate = ((charge - bat->current_charge) / (time - bat->last_update));
127         bat->last_update = time;
128         bat->current_charge = charge;
129         bat->percent = 100 * (bat->current_charge / bat->last_full_charge);
130         if (bat->got_prop)
131           {
132              if (bat->charge_rate > 0)
133                {
134                   if (battery_config->fuzzy && (++battery_config->fuzzcount <= 10) && (bat->time_full > 0))
135                     bat->time_full = (((bat->last_full_charge - bat->current_charge) / bat->charge_rate) + bat->time_full) / 2;
136                   else
137                     bat->time_full = (bat->last_full_charge - bat->current_charge) / bat->charge_rate;
138                   bat->time_left = -1;
139                }
140              else
141                {
142                   if (battery_config->fuzzy && (battery_config->fuzzcount <= 10) && (bat->time_left > 0))
143                     bat->time_left = (((0 - bat->current_charge) / bat->charge_rate) + bat->time_left) / 2;
144                   else
145                     bat->time_left = (0 - bat->current_charge) / bat->charge_rate;
146                   bat->time_full = -1;
147                }
148           }
149         else
150           {
151              bat->time_full = -1;
152              bat->time_left = -1;
153           }
154
155         /* battery state 1: discharge, 2: charge */
156         bat->mib[3] = 10;
157         bat->mib[4] = 0;
158         if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) == -1) {
159                 if (s.value == 2)
160                         bat->charging = 1;
161                 else
162                         bat->charging = 0;
163         }
164
165         /* AC State */
166         ac->mib[3] = 9;
167         ac->mib[4] = 0;
168         if (sysctl(ac->mib, 5, &s, &slen, NULL, 0) == -1) {
169                 if (s.value)
170                         ac->present = 1;
171                 else
172                         ac->present = 0;
173         }
174
175         
176
177         if (bat->got_prop)
178                 _battery_device_update();
179         bat->got_prop = 1;
180 }