Module: Battery

Defined in:
ext/accessibility/extras/extras.c,
ext/accessibility/extras/extras.c

Overview

Utility methods for getting information about the system battery (if present).

Instance Method Summary collapse

Instance Method Details

#levelFloat Also known as: charge_level

Returns the batteries charge level as a percentage from 0 to 1

A special value of -1.0 is returned when there is no battery present.

Returns:

  • (Float)


603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'ext/accessibility/extras/extras.c', line 603

static
VALUE
rb_battery_level(VALUE self)
{
  // constant global strings (like ruby symbols, or lisp atoms, NXAtom, etc)
  // so we do not need to release it later (unless you really want to)
  CFStringRef capacity_key     = CFSTR(kIOPSCurrentCapacityKey);
  CFStringRef max_capacity_key = CFSTR(kIOPSMaxCapacityKey);

  double         level = -1.0;
  CFDictionaryRef info = battery_info();

  if (info) {
    CFNumberRef current_cap = CFDictionaryGetValue(info, capacity_key);
    CFNumberRef     max_cap = CFDictionaryGetValue(info, max_capacity_key);

    if (current_cap && max_cap) {
      int current = 0;
      int     max = 0;

      CFNumberGetValue(current_cap, kCFNumberIntType, &current);
      CFNumberGetValue(max_cap,     kCFNumberIntType, &max);

      level = ((double)current)/((double)max);
    }

    CFRelease(info);
  }

  return DBL2NUM(level);
}

#stateSymbol

Returns the current battery state

The state will be one of:

  • :not_installed
  • :charged
  • :charging
  • :discharging

Returns:

  • (Symbol)


570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'ext/accessibility/extras/extras.c', line 570

static
VALUE
rb_battery_state(VALUE self)
{
  // constant global strings (like ruby symbols, or lisp atoms, NXAtom, etc)
  // so we do not need to release it later (unless you really want to)
  CFStringRef charged_key  = CFSTR(kIOPSIsChargedKey);
  CFStringRef charging_key = CFSTR(kIOPSIsChargingKey);

  VALUE              state = battery_not_installed;
  CFDictionaryRef     info = battery_info();

  if (info) {
    if (CFDictionaryGetValue(info, charged_key) == kCFBooleanTrue)
      state = battery_charged;
    else if (CFDictionaryGetValue(info, charging_key) == kCFBooleanTrue)
      state = battery_charging;
    else
      state = battery_discharging;

    CFRelease(info);
  }

  return state;
}

#time_to_chargedFixnum Also known as: time_to_full_charge

Returns the estimated number of minutes until the battery is fully charged

A special value of -1 indicates that the value is currently being estimated and you should try again later.

Returns:

  • (Fixnum)


679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'ext/accessibility/extras/extras.c', line 679

static
VALUE
rb_battery_time_full_charge(VALUE self)
{
  CFStringRef ttfull_key = CFSTR(kIOPSTimeToFullChargeKey);
  int                time = -1;
  CFDictionaryRef    info = battery_info();

  if (info) {
    CFNumberRef current_time = CFDictionaryGetValue(info, ttfull_key);
    if (current_time)
      CFNumberGetValue(current_time, kCFNumberIntType, &time);

    CFRelease(info);
  }

  return INT2FIX(time);
}

#time_to_dischargedFixnum Also known as: time_to_empty

Returns the estimated number of minutes until the battery is fully discharged

A special value of -1 indicates that the value is currently being estimated and you should try again later.

A special value of 0 indicates that the battery is not discharging, which usually means that the battery does not exist or is in a charging/charged state.

Returns:

  • (Fixnum)


648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'ext/accessibility/extras/extras.c', line 648

static
VALUE
rb_battery_time_to_empty(VALUE self)
{
  CFStringRef ttempty_key = CFSTR(kIOPSTimeToEmptyKey);
  int                time = -1;
  CFDictionaryRef    info = battery_info();

  if (info) {
    CFNumberRef current_time = CFDictionaryGetValue(info, ttempty_key);
    if (current_time)
      CFNumberGetValue(current_time, kCFNumberIntType, &time);

    CFRelease(info);
  }

  if (time)
    return INT2FIX(time);
  else
    return INT2FIX(0);
}