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
13
# File 'lib/fusuma/libinput_command.rb', line 8

def initialize(libinput_options: [], commands: {})
  @libinput_command = commands[:libinput_command]
  @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



50
51
52
# File 'lib/fusuma/libinput_command.rb', line 50

def debug_events(writer)
  Open3.pipeline_start([debug_events_with_options], ["grep -v POINTER_ --line-buffered"], out: writer, in: "/dev/null")
end

#debug_events_commandObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fusuma/libinput_command.rb', line 83

def debug_events_command
  if @libinput_command
    @libinput_command + " debug-events"
  elsif @debug_events_command
    @debug_events_command
  elsif new_cli_option_available?
    "libinput debug-events"
  else
    "libinput-debug-events"
  end
end

#debug_events_with_optionsObject



95
96
97
98
# File 'lib/fusuma/libinput_command.rb', line 95

def debug_events_with_options
  prefix = "stdbuf -oL --"
  "#{prefix} #{debug_events_command} #{@libinput_options.join(" ")}".strip
end

#libinput_1_27_0_or_later?Boolean

Returns:

  • (Boolean)


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

def libinput_1_27_0_or_later?
  Gem::Version.new(version) >= Gem::Version.new("1.27.0")
end

#list_devices {|gives| ... } ⇒ Object

Yield Parameters:

  • gives (String)

    a line in libinput list-devices output to the block



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fusuma/libinput_command.rb', line 36

def list_devices(&block)
  cmd = list_devices_command
  MultiLogger.debug(list_devices: cmd)
  o, e, s = Open3.capture3(cmd)

  unless s.success?
    MultiLogger.error("libinput list-devices failed with output: #{o}")
    return
  end

  o.each_line(&block)
end

#list_devices_commandObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fusuma/libinput_command.rb', line 71

def list_devices_command
  if @libinput_command
    @libinput_command + " list-devices"
  elsif @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)


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

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

#versionString

Returns:



30
31
32
33
# File 'lib/fusuma/libinput_command.rb', line 30

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

#version_commandString

Returns command.

Returns:

Raises:

  • (SystemExit)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fusuma/libinput_command.rb', line 56

def version_command
  if @libinput_command
    "#{@libinput_command} --version"
  elsif @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