Class: Fusuma::Plugin::Inputs::LibinputCommandInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fusuma/plugin/inputs/libinput_command_input.rb

Overview

libinput commands wrapper

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

Methods inherited from Input

#event, #tag

Methods inherited from Base

#config_index, #config_params, inherited, plugins

Instance Method Details

#config_param_typesObject



11
12
13
14
15
16
17
18
# File 'lib/fusuma/plugin/inputs/libinput_command_input.rb', line 11

def config_param_types
  {
    'device': [String],
    'enable-dwt': [TrueClass, FalseClass],
    'enable-tap': [TrueClass, FalseClass],
    'show-keycodes': [TrueClass, FalseClass]
  }
end

#debug_events {|line| ... } ⇒ Object

Yields:

  • (line)

    gives a line in libinput debug-events output to the block



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fusuma/plugin/inputs/libinput_command_input.rb', line 51

def debug_events
  prefix = 'stdbuf -oL --'
  options = [*libinput_options]
  cmd = "#{prefix} #{debug_events_command} #{options.join(' ')}".strip
  MultiLogger.debug(debug_events: cmd)
  Open3.popen3(cmd) do |_i, o, _e, _w|
    o.each do |line|
      yield(line.chomp)
    end
  end
end

#debug_events_commandObject



84
85
86
87
88
89
90
# File 'lib/fusuma/plugin/inputs/libinput_command_input.rb', line 84

def debug_events_command
  if new_cli_option_available?
    'libinput debug-events'
  else
    'libinput-debug-events'
  end
end

#libinput_optionsObject



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

def libinput_options
  device = ("--device='#{config_params(:device)}'" if config_params(:device))
  enable_tap = '--enable-tap' if config_params(:'enable-tap')
  enable_dwt = '--enable-dwt' if config_params(:'enable-dwt')
  show_keycodes = '--show-keycodes' if config_params(:'show-keycodes')
  [
    device,
    enable_dwt,
    enable_tap,
    show_keycodes
  ].compact
end

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

Yields:

  • (line)

    gives a line in libinput list-devices output to the block



42
43
44
45
46
47
48
# File 'lib/fusuma/plugin/inputs/libinput_command_input.rb', line 42

def list_devices
  cmd = list_devices_command
  MultiLogger.debug(list_devices: cmd)
  Open3.popen3(cmd) do |_i, o, _e, _w|
    o.each { |line| yield(line) }
  end
end

#list_devices_commandObject



76
77
78
79
80
81
82
# File 'lib/fusuma/plugin/inputs/libinput_command_input.rb', line 76

def list_devices_command
  if new_cli_option_available?
    'libinput list-devices'
  else
    'libinput-list-devices'
  end
end

#new_cli_option_available?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/fusuma/plugin/inputs/libinput_command_input.rb', line 31

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

#runObject



20
21
22
23
24
# File 'lib/fusuma/plugin/inputs/libinput_command_input.rb', line 20

def run
  debug_events do |line|
    yield event(record: line)
  end
end

#versionString

Returns:



36
37
38
39
# File 'lib/fusuma/plugin/inputs/libinput_command_input.rb', line 36

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

#version_commandString

Returns command.

Returns:

Raises:

  • (SystemExit)


65
66
67
68
69
70
71
72
73
74
# File 'lib/fusuma/plugin/inputs/libinput_command_input.rb', line 65

def version_command
  if which('libinput')
    'libinput --version'
  elsif which('libinput-list-devices')
    'libinput-list-devices --version'
  else
    MultiLogger.error 'install libinput-tools'
    exit 1
  end
end