class Fusuma::LibinputCommand

Execute libinput command

Constants

DEFAULT_WAIT_TIME
NEW_CLI_OPTION_VERSION

`libinput-list-devices` and `libinput-debug-events` are deprecated, use `libinput list-devices` and `libinput debug-events` from 1.8.

TIMEOUT_MESSAGE

Public Class Methods

new(libinput_options: [], commands: {}) click to toggle source
# File lib/fusuma/libinput_command.rb, line 8
def initialize(libinput_options: [], commands: {})
  @debug_events_command = commands[:debug_events_command]
  @list_devices_command = commands[:list_devices_command]
  @libinput_options = libinput_options
end

Public Instance Methods

debug_events(writer) click to toggle source

@return [Integer] return a latest line libinput debug-events

# File lib/fusuma/libinput_command.rb, line 44
def debug_events(writer)
  @debug_events ||= begin
    pid = Process.spawn(debug_events_with_options, out: writer,
                                                   in: '/dev/null')
    Process.detach(pid)
    pid
  end
end
debug_events_command() click to toggle source
# File lib/fusuma/libinput_command.rb, line 78
def debug_events_command
  if @debug_events_command
    @debug_events_command
  elsif new_cli_option_available?
    'libinput debug-events'
  else
    'libinput-debug-events'
  end
end
debug_events_with_options() click to toggle source
# File lib/fusuma/libinput_command.rb, line 88
def debug_events_with_options
  prefix = 'stdbuf -oL --'
  "#{prefix} #{debug_events_command} #{@libinput_options.join(' ')}".strip
end
list_devices(&block) click to toggle source

@yieldparam [String] gives a line in libinput list-devices output to the block

# File lib/fusuma/libinput_command.rb, line 33
def list_devices(&block)
  cmd = list_devices_command
  MultiLogger.debug(list_devices: cmd)
  i, o, e, _w = Open3.popen3(cmd)
  MultiLogger.error(e.read) if o.eof?
  i.close
  e.close
  o.each(&block)
end
list_devices_command() click to toggle source
# File lib/fusuma/libinput_command.rb, line 68
def list_devices_command
  if @list_devices_command
    @list_devices_command
  elsif new_cli_option_available?
    'libinput list-devices'
  else
    'libinput-list-devices'
  end
end
new_cli_option_available?() click to toggle source

@return [Boolean]

# File lib/fusuma/libinput_command.rb, line 22
def new_cli_option_available?
  Gem::Version.new(version) >= Gem::Version.new(NEW_CLI_OPTION_VERSION)
end
version() click to toggle source

@return [String]

# File lib/fusuma/libinput_command.rb, line 27
def version
  # versiom_command prints "1.6.3\n"
  @version ||= `#{version_command}`.strip
end
version_command() click to toggle source

@return [String] command @raise [SystemExit]

# File lib/fusuma/libinput_command.rb, line 55
def version_command
  if @debug_events_command && @list_devices_command
    "#{@list_devices_command} --version"
  elsif which('libinput')
    'libinput --version'
  elsif which('libinput-list-devices')
    'libinput-list-devices --version'
  else
    MultiLogger.error 'Please install libinput-tools'
    exit 1
  end
end

Private Instance Methods

wait_time() click to toggle source
# File lib/fusuma/libinput_command.rb, line 95
def wait_time
  DEFAULT_WAIT_TIME
end
which(command) click to toggle source

which in ruby: Checking if program exists in $PATH from ruby (stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby) Cross-platform way of finding an executable in the $PATH.

which('ruby') #=> /usr/bin/ruby

@return [String, nil]

# File lib/fusuma/libinput_command.rb, line 105
def which(command)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{command}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end