@@ -390,25 +390,46 @@ void Platform_getBattery(BatteryInfo* info) {
390390 bool found = findDevice ("acpibat0" , mib , & snsrdev , & sdlen );
391391
392392 if (found ) {
393+ bool haveTotalFull = false;
394+ bool haveTotalRemain = false;
395+
396+ int64_t totalFull = 0 ;
397+ int64_t totalRemain = 0 ;
398+
393399 /* See "sys/dev/acpi/acpibat.c" of OpenBSD source code for the indices
394400 of the last field. */
395401 mib [3 ] = SENSOR_WATTHOUR ;
396402 mib [4 ] = 0 ; /* "last full capacity" */
397- double last_full_capacity = 0 ;
403+ bool haveBatteryFull = false;
404+ int64_t batteryFull = 0 ;
398405 if (sysctl (mib , 5 , & s , & slen , NULL , 0 ) != -1 )
399- last_full_capacity = s .value ;
400- if (last_full_capacity > 0 ) {
406+ batteryFull = s .value ;
407+
408+ if (batteryFull > 0 )
409+ haveBatteryFull = true;
410+
411+ if (haveBatteryFull ) {
401412 mib [3 ] = SENSOR_WATTHOUR ;
402413 mib [4 ] = 3 ; /* "remaining capacity" */
403414 if (sysctl (mib , 5 , & s , & slen , NULL , 0 ) != -1 ) {
404- double charge = s .value ;
405- info -> percent = 100 * (charge / last_full_capacity );
406- if (charge >= last_full_capacity )
407- info -> percent = 100 ;
408- info -> energyCurr = charge ;
409- info -> energyFull = last_full_capacity ;
415+ int64_t batteryRemain = s .value ;
416+ if (batteryRemain >= 0 ) {
417+ totalRemain += batteryRemain ;
418+ totalFull += batteryFull ;
419+ haveTotalRemain = true;
420+ haveTotalFull = true;
421+ }
410422 }
411423 }
424+
425+ if (haveTotalRemain && haveTotalFull && totalFull > 0 ) {
426+ info -> percent = ((double ) totalRemain * 100.0 ) / (double ) totalFull ;
427+ if (totalRemain >= totalFull )
428+ info -> percent = 100 ;
429+
430+ info -> energyCurr = (double ) totalRemain / 1000000.0 ;
431+ info -> energyFull = (double ) totalFull / 1000000.0 ;
432+ }
412433 }
413434
414435 found = findDevice ("acpiac0" , mib , & snsrdev , & sdlen );
0 commit comments