class Fidget::Platform

Constants

ES_AWAYMODE_REQUIRED
ES_CONTINUOUS
ES_DISPLAY_REQUIRED
ES_SYSTEM_REQUIRED
KB_EVENT_KEYPRESS
KB_EVENT_KEYUP
KB_KEY_F24

Public Class Methods

allow_sleep() click to toggle source
# File lib/fidget/platform/darwin.rb, line 22
def self.allow_sleep
  return unless @@pids

  @@pids.each do |pid|
    Process.kill('TERM', pid)
  end

  @@pids = nil
end
current_process(options) click to toggle source
# File lib/fidget/platform/darwin.rb, line 2
def self.current_process(options)
  pid = spawn("caffeinate #{arguments(options)}")

  at_exit do
    Process.kill('TERM', pid)
  end
end
prevent_sleep(options) { || ... } click to toggle source
# File lib/fidget/platform/darwin.rb, line 10
def self.prevent_sleep(options)
  pid = spawn("caffeinate #{arguments(options)}")

  if block_given?
    yield
    Process.kill('TERM', pid)
  else
    @@pids ||= []
    @@pids << pid
  end
end
simulate() click to toggle source
# File lib/fidget/platform/darwin.rb, line 32
def self.simulate
  # osx needs accessibilty access to do this, along with a big scary prompt.
  # But it's not really needed anyway. Caffeinate works really well.
  # Should we want to at some point..., https://github.com/AXElements/AXElements
end

Private Class Methods

arguments(options) click to toggle source
# File lib/fidget/platform/darwin.rb, line 39
def self.arguments(options)
  options.flatten!
  options.compact!
  return '-u'    if options.empty?
  return '-dism' if options == [:all]

  terms = {
    :display => 'd',
    :idle    => 'i',
    :disk    => 'm',
    :sleep   => 's',
    :user    => 'u',
  }
  options.each do |opt|
    STDERR.puts "Fidget: option #{opt} is not supported on OS X" unless terms.include? opt
  end

  args = options.map { |element| terms[element] }.compact.join

  return "-#{args}" unless args.empty?
end
dbus_screensaver() click to toggle source
# File lib/fidget/platform/posix.rb, line 104
def self.dbus_screensaver
  session = DBus.session_bus
  service = session['org.freedesktop.ScreenSaver']
  object  = service['/ScreenSaver']
  object.introspect
  object['org.freedesktop.ScreenSaver']
end
munge(options) click to toggle source
# File lib/fidget/platform/posix.rb, line 37
def self.munge(options)
  options.flatten!
  options.compact!
  options = [:display] if options.empty?
  options = [:display, :blanking] if options == [:all]
  options.each do |opt|
    STDERR.puts "Fidget: option {opt} is not supported on Linux" unless [:display, :blanking].include? opt
  end
  options
end
required_binaries(*list) click to toggle source
# File lib/fidget/platform/posix.rb, line 113
def self.required_binaries(*list)
  list = ['xset', 'xdg-screensaver', 'xwininfo'] if list.empty?
  found = true
  list.each do |command|
    unless system("which #{command} > /dev/null 2>&1")
      found = false
      Fidget.debug "Fidget: required binary (#{command}) not found."
    end
  end
  found
end
resume(options) click to toggle source
# File lib/fidget/platform/posix.rb, line 88
def self.resume(options)
  if options.include? :display
    system("xdg-screensaver resume #{root_win}")
    system('xset +dpms')
    system('xset s on')

    # if we have a cookie, we can trust that DBus works
    dbus_screensaver.Uninhibit(@@cookie) if @@cookie
  end

  if options.include? :blanking
    system("setterm -blank #{@@blanking} -powerdown #{@@blanking}")
  end
end
root_win() click to toggle source
# File lib/fidget/platform/posix.rb, line 49
def self.root_win
  ids = `xwininfo -root`.each_line.collect do |line|
    next unless line =~ /Window id: (0x\h+)/
    $1
  end.compact
  raise "Parsing xwininfo failed" unless ids.size == 1
  ids.first
end
set_state(options) click to toggle source

Set thread execution state, using information from msdn.microsoft.com/en-us/library/aa373208(VS.85).aspx stackoverflow.com/questions/4126136/stop-a-windows-7-pc-from-going-to-sleep-while-a-ruby-program-executes

# File lib/fidget/platform/windows.rb, line 46
def self.set_state(options)
  options.flatten!
  options.compact!
  options = [:display, :sleep] if options.empty?
  options = [:display, :sleep, :simulate, :away] if options == [:all]

  terms = {
    :display => ES_DISPLAY_REQUIRED,
    :sleep   => ES_SYSTEM_REQUIRED,
    :away    => ES_AWAYMODE_REQUIRED,
  }
  options.each do |opt|
    STDERR.puts "Fidget: option #{opt} is not supported on Windows" unless terms.include? opt
  end

  # translate options to constants, then OR them all together
  mode = options.map { |element| terms[element] }.reduce { |memo, element| memo|element } || 0

  state = Win32API.new('kernel32','SetThreadExecutionState','L')
  state.call(ES_CONTINUOUS|mode)

end
suspend(options) click to toggle source
# File lib/fidget/platform/posix.rb, line 59
def self.suspend(options)
  if options.include? :display
    # I'm sure that if we tried, we could find yet another way to do this
    system("xdg-screensaver suspend #{root_win}")
    system('xset s off')
    system('xset s noblank')
    system('xset -dpms')

    # xdg-screensaver doesn't actually seem to work, but making DBus calls ourself does.
    # This is possibly because the inhibit expires when the dbus-session command terminates
    # I don't know if this will work in other distros though. Yay for consistency. *\o/*
    begin
      dbus_screensaver.Inhibit(root_win, 'Administratively disabled') do |res|
        @@cookie = res
      end
    rescue => e
      STDERR.puts 'Fidget: DBus action failed.'
      STDERR.puts e.message
      STDERR.puts e.backtrace.first
    end
  end

  if options.include? :blanking
    @@blanking ||= File.read('/sys/module/kernel/parameters/consoleblank').strip
    system('setterm -blank 0 -powerdown 0')
  end
end