class Bandshell::ScreenControl

Public Class Methods

control_availability() click to toggle source

Returns a boolean and an explanatory string indicating whether the screen can be controlled by DPMS.

# File lib/bandshell/screen_control.rb, line 47
def control_availability
  dpms_availability
end
default_display() click to toggle source

Only used if no display is set already.

# File lib/bandshell/screen_control.rb, line 9
def default_display
  ":0"
end
enforce_screen_state(state) click to toggle source

Ensures that the display is in the specified state by enforcing each of a number of parameters, passed as the “state” hash. Valid keys:

:on => (boolean value: true for on, false for off)
# File lib/bandshell/screen_control.rb, line 17
def enforce_screen_state(state)
  if !state.is_a? Hash
    raise "enforce_screen_state: did not receive a hash!"
  end
  if state.has_key? :on
    if state[:on] == true
      force_screen_on unless screen_is_on? == true
    elsif state[:on] == false
      force_screen_off unless screen_is_on? == false
    else
      raise "enforce_screen_state: Invalid value for :on!"
    end
  end
end
force_screen_off() click to toggle source
# File lib/bandshell/screen_control.rb, line 36
def force_screen_off
  dpms_force_screen_off
end
force_screen_on() click to toggle source
# File lib/bandshell/screen_control.rb, line 32
def force_screen_on
  dpms_force_screen_on
end
screen_is_on?() click to toggle source

true, false, or unknown

# File lib/bandshell/screen_control.rb, line 41
def screen_is_on?
  dpms_screen_is_on?
end

Private Class Methods

dpms_availability() click to toggle source
# File lib/bandshell/screen_control.rb, line 132
def dpms_availability
  if ENV['DISPLAY'].nil? or ENV['DISPLAY'].empty?
    ENV['DISPLAY'] = default_display
  end

  begin
    result = `xset -q 2>&1`
  rescue Errno::ENOENT
    return [false, "Can't access the xset command to control DPMS."]
  end
  if ($?.exitstatus == 127)
    return [false, "Can't access the xset command to control DPMS."]
  elsif ($?.exitstatus != 0)
    # xset returns 1 and a message if the display is not specified or
    # invalid.
    return [false, "Problem running xset: "+result.chomp]
  end
  if result.include? "DPMS is Disabled"
    return [false, "DPMS is disabled."]
  elsif result.include? "DPMS is Enabled"
    return [true, ""]
  else
    return [false, "Error parsing xset output."]
  end
end
dpms_force_screen_off() click to toggle source

true on success, false on failure.

# File lib/bandshell/screen_control.rb, line 115
def dpms_force_screen_off
  if ENV['DISPLAY'].nil? or ENV['DISPLAY'].empty?
    ENV['DISPLAY'] = default_display
  end

  begin
    `xset dpms force off`
  rescue Errno::ENOENT
    return false
  end
  if $?.exitstatus == 0
    return true
  else
    return false
  end
end
dpms_force_screen_on() click to toggle source

true on success, false on failure.

# File lib/bandshell/screen_control.rb, line 88
def dpms_force_screen_on
  if ENV['DISPLAY'].nil? or ENV['DISPLAY'].empty?
    ENV['DISPLAY'] = default_display
  end

  begin
    `xset dpms force on`
  rescue Errno::ENOENT
    return false
  end
  if $?.exitstatus != 0
    return false
  end

  # Required if the screen was turned off with DPMS and the
  # screensaver has not been disabled:
  begin
    `xset s reset`
  rescue Errno::ENOENT #unlikely, but still...
    return false
  end
  if $?.exitstatus != 0
    return false
  end
end
dpms_screen_is_on?() click to toggle source

true, false, or :unknown

# File lib/bandshell/screen_control.rb, line 65
def dpms_screen_is_on?
  if ENV['DISPLAY'].nil? or ENV['DISPLAY'].empty?
    ENV['DISPLAY'] = default_display
  end

  begin
    result = `xset -q 2>&1`
  rescue Errno::ENOENT
    return :unknown
  end
  if ($?.exitstatus != 0)
    return :unknown
  end
  if result.include? "Monitor is On"
    return true
  elsif result.include? "Monitor is Off"
    return false
  else
    return :unknown
  end
end