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)


617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'ext/accessibility/extras/extras.c', line 617

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)


584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'ext/accessibility/extras/extras.c', line 584

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)


693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'ext/accessibility/extras/extras.c', line 693

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)


662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'ext/accessibility/extras/extras.c', line 662

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);
}