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)


565
566
567
568
569
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
595
# File 'ext/accessibility/extras/extras.c', line 565

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)


532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'ext/accessibility/extras/extras.c', line 532

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)


641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'ext/accessibility/extras/extras.c', line 641

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)


610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'ext/accessibility/extras/extras.c', line 610

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