Class: Fusuma::LibinputCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/fusuma/libinput_command.rb

Overview

Execute libinput command

Constant Summary collapse

NEW_CLI_OPTION_VERSION =

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

1.8
DEFAULT_WAIT_TIME =
0.3
TIMEOUT_MESSAGE =
'LIBINPUT TIMEOUT'

Instance Method Summary collapse

Constructor Details

#initialize(libinput_options: [], commands: {}) ⇒ LibinputCommand

Returns a new instance of LibinputCommand.



8
9
10
11
12
# 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

Instance Method Details

#debug_eventsInteger, IO

Return a latest line libinput debug-events

Returns:

  • (Integer, IO)

    return a latest line libinput debug-events



45
46
47
48
49
50
51
# File 'lib/fusuma/libinput_command.rb', line 45

def debug_events
  @debug_events = begin
    p, i, o, _e = POSIX::Spawn.popen4(debug_events_with_options)
    i.close
    [p, o]
  end
end

#debug_events_commandObject



78
79
80
81
82
83
84
85
86
# 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_optionsObject



88
89
90
91
# 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 {|gives| ... } ⇒ Object

Yield Parameters:

  • gives (String)

    a line in libinput list-devices output to the block



33
34
35
36
37
38
39
40
41
42
# File 'lib/fusuma/libinput_command.rb', line 33

def list_devices(&block)
  cmd = list_devices_command
  MultiLogger.debug(list_devices: cmd)
  p, i, o, e = POSIX::Spawn.popen4(cmd)
  i.close
  o.each(&block)
ensure
  [i, o, e].each { |io| io.close unless io.closed? }
  Process.waitpid(p)
end

#list_devices_commandObject



68
69
70
71
72
73
74
75
76
# 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?Boolean

Returns:

  • (Boolean)


22
23
24
# 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

#versionString

Returns:



27
28
29
30
# File 'lib/fusuma/libinput_command.rb', line 27

def version
  # versiom_command prints "1.6.3\n"
  @version ||= `#{version_command}`.strip
end

#version_commandString

Returns command.

Returns:

Raises:

  • (SystemExit)


55
56
57
58
59
60
61
62
63
64
65
66
# 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