Module: System::Power

Defined in:
lib/system_control/power.rb,
ext/system_control/power.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.no_sleep_closeObject

Cleans up the non-sleep. This method is used by no_sleep.

See Also:

  • no_sleep


# File 'ext/system_control/power.c'

/*
 * Cleans up the non-sleep. This method is used by no_sleep.
 *
 * @see
 *   System::Power.no_sleep
 */
static VALUE
rb_sys_no_sleep_close(VALUE obj, VALUE arg)
{
    IOPMAssertionRelease(FIX2INT(arg));
}

.no_sleep_openObject

Prepares the non-sleep. This method is used by no_sleep.

See Also:

  • no_sleep


# File 'ext/system_control/power.c'

/*
 * Prepares the non-sleep. This method is used by no_sleep.
 *
 * @see
 *   System::Power.no_sleep
 */
static VALUE
rb_sys_no_sleep_open(VALUE obj)
{
    IOPMAssertionID id;
    IOReturn ret = IOPMAssertionCreate(kIOPMAssertionTypeNoDisplaySleep,
                       kIOPMAssertionLevelOn, &id);
    if (ret != kIOReturnSuccess) {
    rb_raise(rb_eRuntimeError, "no sleep failed");
    }

    return INT2FIX((int)id);
}

.sleepObject

Sleeps the machine.

Examples:

System::Power.sleep


# File 'ext/system_control/power.c'

/*
 * Sleeps the machine.
 *
 * @example
 *   System::Power.sleep
 */
static VALUE
rb_sys_sleep(VALUE obj)
{
    mach_port_t  port;
    io_connect_t manage;

    if (IOMasterPort(bootstrap_port, &port) != kIOReturnSuccess) {
    rb_raise(rb_eRuntimeError, "Failed to get master port");
    }

    manage = IOPMFindPowerManagement(port);
    if (!manage) {
    rb_raise(rb_eRuntimeError, "Failed to find power management");
    }

    if (IOPMSleepSystem(manage) != kIOReturnSuccess) {
    rb_raise(rb_eRuntimeError, "Failed to sleep");
    }

    IOServiceClose(manage);
    return Qnil;
}

.sleep_displayObject

Sleeps the only display.

Examples:

System::Power.sleep_display


# File 'ext/system_control/power.c'

/*
 * Sleeps the only display.
 *
 * @example
 *   System::Power.sleep_display
 */
static VALUE
rb_sys_sleep_display(VALUE obj)
{
    io_registry_entry_t regist =
    IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler");
    if(!regist) {
    return Qnil;
    }

    IORegistryEntrySetCFProperty(regist, CFSTR("IORequestIdle"), kCFBooleanTrue);
    IOObjectRelease(regist);
    return Qnil;
}

Instance Method Details

#no_sleep(&block) ⇒ Object

While this method processes given block, the system does not sleep.

Examples:

System::Power.no_sleep {
   # Add the work you need to do without
   # the system sleeping here.
}


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/system_control/power.rb', line 17

def no_sleep(&block)
  if !block_given?
    raise ArgumentError, "need block"
  end

  begin
    id = no_sleep_open()
    block.call
  ensure
    no_sleep_close(id)
  end
end