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'

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_events(writer) ⇒ Integer

Return a latest line libinput debug-events

Returns:

  • (Integer)

    return a latest line libinput debug-events



41
42
43
44
45
46
47
48
# File 'lib/fusuma/libinput_command.rb', line 41

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_commandObject



75
76
77
78
79
80
81
82
83
# File 'lib/fusuma/libinput_command.rb', line 75

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



85
86
87
88
# File 'lib/fusuma/libinput_command.rb', line 85

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



30
31
32
33
34
35
36
37
38
# File 'lib/fusuma/libinput_command.rb', line 30

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_commandObject



65
66
67
68
69
70
71
72
73
# File 'lib/fusuma/libinput_command.rb', line 65

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)


19
20
21
# File 'lib/fusuma/libinput_command.rb', line 19

def new_cli_option_available?
  Gem::Version.new(version) >= Gem::Version.new(NEW_CLI_OPTION_VERSION)
end

#versionString

Returns:



24
25
26
27
# File 'lib/fusuma/libinput_command.rb', line 24

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

#version_commandString

Returns command.

Returns:

Raises:

  • (SystemExit)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fusuma/libinput_command.rb', line 52

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