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

: (?libinput_options: Array, ?commands: Hash[untyped, untyped]) -> void



9
10
11
12
13
14
# File 'lib/fusuma/libinput_command.rb', line 9

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

: (StringIO) -> Array

Returns:

  • (Integer)

    return a latest line libinput debug-events



57
58
59
# File 'lib/fusuma/libinput_command.rb', line 57

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

: () -> String



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fusuma/libinput_command.rb', line 93

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

: () -> String



106
107
108
109
# File 'lib/fusuma/libinput_command.rb', line 106

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)


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

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

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

: () { (String) -> void } -> void

Yield Parameters:

  • gives (String)

    a line in libinput list-devices output to the block



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fusuma/libinput_command.rb', line 40

def list_devices(&block)
  cmd = list_devices_command
  unless @logged_list_devices
    MultiLogger.debug(list_devices: cmd)
    @logged_list_devices = true
  end
  o, _, s = Open3.capture3(cmd)

  unless s.success?
    raise "libinput list-devices failed with output: #{o}"
  end

  o.each_line(&block)
end

#list_devices_commandObject

: () -> String



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fusuma/libinput_command.rb', line 80

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

: () -> bool

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

: () -> String?

Returns:



33
34
35
36
# File 'lib/fusuma/libinput_command.rb', line 33

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

#version_commandString

: () -> String?

Returns:

Raises:

  • (SystemExit)


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

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