-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix for libinput version 1.8 or later
- Loading branch information
1 parent
867514c
commit c399e7a
Showing
6 changed files
with
252 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
module Fusuma | ||
# libinput commands wrapper | ||
class LibinputCommands | ||
def initialize(*options) | ||
@options = options | ||
end | ||
|
||
# `libinput-list-devices` and `libinput-debug-events` are deprecated, | ||
# use `libinput list-devices` and `libinput debug-events` from 1.8. | ||
NEW_CLI_OPTION_VERSION = 1.8 | ||
|
||
# @return [Boolean] | ||
def new_cli_option_available? | ||
Gem::Version.new(version) >= Gem::Version.new(NEW_CLI_OPTION_VERSION) | ||
end | ||
|
||
# @return [String] | ||
def version | ||
# versiom_command prints "1.6.3\n" | ||
@version ||= `#{version_command}`.strip | ||
end | ||
|
||
# @yield [line] gives a line in libinput list-devices output to the block | ||
def list_devices | ||
cmd = list_devices_command | ||
MultiLogger.debug(debug_events: cmd) | ||
Open3.popen3(cmd) do |_i, o, _e, _w| | ||
o.each { |line| yield(line) } | ||
end | ||
end | ||
|
||
# @yield [line] gives a line in libinput debug-events output to the block | ||
def debug_events | ||
prefix = 'stdbuf -oL --' | ||
options = [*@options, device_option] | ||
cmd = "#{prefix} #{debug_events_command} #{options.join(' ')}".strip | ||
MultiLogger.debug(debug_events: cmd) | ||
Open3.popen3(cmd) do |_i, o, _e, _w| | ||
o.each { |line| yield(line) } | ||
end | ||
end | ||
|
||
# @return [String] command | ||
# @raise [SystemExit] | ||
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 | ||
|
||
def list_devices_command | ||
if new_cli_option_available? | ||
'libinput list-devices' | ||
else | ||
'libinput-list-devices' | ||
end | ||
end | ||
|
||
def debug_events_command | ||
if new_cli_option_available? | ||
'libinput debug-events' | ||
else | ||
'libinput-debug-events' | ||
end | ||
end | ||
|
||
private | ||
|
||
def device_option | ||
"--device /dev/input/#{Device.names.first}" if Device.names.size == 1 | ||
end | ||
|
||
# which in ruby: Checking if program exists in $PATH from ruby | ||
# (https://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby) | ||
# Cross-platform way of finding an executable in the $PATH. | ||
# | ||
# which('ruby') #=> /usr/bin/ruby | ||
# @return [String, nil] | ||
def which(command) | ||
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] | ||
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| | ||
exts.each do |ext| | ||
exe = File.join(path, "#{command}#{ext}") | ||
return exe if File.executable?(exe) && !File.directory?(exe) | ||
end | ||
end | ||
nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Fusuma | ||
VERSION = '0.7.1'.freeze | ||
VERSION = '0.7.2'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
require 'spec_helper' | ||
module Fusuma | ||
describe LibinputCommands do | ||
let(:libinput_commands) { described_class.new } | ||
describe '#version' do | ||
subject { libinput_commands.version } | ||
|
||
context 'when libinput-list-device is available' do | ||
before do | ||
allow(libinput_commands).to receive('which') | ||
.with('libinput') { false } | ||
allow(libinput_commands).to receive('which') | ||
.with('libinput-list-devices') { true } | ||
allow_any_instance_of(Kernel).to receive(:`) | ||
.with('libinput-list-devices --version') { "1.6.3\n" } | ||
end | ||
|
||
it { is_expected.to eq '1.6.3' } | ||
it { expect(libinput_commands.new_cli_option_available?).to be false } | ||
end | ||
|
||
context 'when libinput is available' do | ||
before do | ||
allow(libinput_commands).to receive('which') | ||
.with('libinput') { true } | ||
allow(libinput_commands).to receive('which') | ||
.with('libinput-list-devices') { false } | ||
allow_any_instance_of(Kernel).to receive(:`) | ||
.with('libinput --version') { "1.8\n" } | ||
end | ||
|
||
it { is_expected.to eq '1.8' } | ||
it { expect(libinput_commands.new_cli_option_available?).to be true } | ||
end | ||
|
||
context 'when libinput command is not found' do | ||
before do | ||
allow(libinput_commands).to receive('which') | ||
.with('libinput') { false } | ||
allow(libinput_commands).to receive('which') | ||
.with('libinput-list-devices') { false } | ||
end | ||
|
||
it 'shold print error and exit 1' do | ||
expect(MultiLogger).to receive(:error) | ||
expect { subject }.to raise_error(SystemExit) | ||
end | ||
end | ||
end | ||
|
||
describe '#new_cli_option_available?' do | ||
subject { libinput_commands.new_cli_option_available? } | ||
context 'with NEW_CLI_OPTION_VERSION' do | ||
before do | ||
allow(libinput_commands).to receive(:version) | ||
.and_return(LibinputCommands::NEW_CLI_OPTION_VERSION) | ||
end | ||
it { is_expected.to eq true } | ||
end | ||
context 'without NEW_CLI_OPTION_VERSION' do | ||
before do | ||
allow(libinput_commands).to receive(:version) | ||
.and_return(LibinputCommands::NEW_CLI_OPTION_VERSION - 0.1) | ||
end | ||
it { is_expected.to eq false } | ||
end | ||
end | ||
|
||
describe 'list_devices' do | ||
subject { libinput_commands.list_devices } | ||
after { subject } | ||
|
||
context 'with new cli version' do | ||
before do | ||
allow(libinput_commands).to receive(:new_cli_option_available?) | ||
.and_return(true) | ||
end | ||
|
||
it 'call `libinput list-devices`' do | ||
command = 'libinput list-devices' | ||
expect(Open3).to receive(:popen3) | ||
.with("#{command}") | ||
end | ||
end | ||
context 'with old cli version' do | ||
before do | ||
allow(libinput_commands).to receive(:new_cli_option_available?) | ||
.and_return(false) | ||
end | ||
|
||
it 'call `libinput-list-devices`' do | ||
command = 'libinput-list-devices' | ||
expect(Open3).to receive(:popen3) | ||
.with("#{command}") | ||
end | ||
end | ||
end | ||
|
||
describe 'debug_events' do | ||
subject { libinput_commands.debug_events } | ||
|
||
before do | ||
allow(libinput_commands).to receive(:device_option) | ||
.and_return('--device stub_device') | ||
end | ||
|
||
after { subject } | ||
|
||
context 'with new cli version' do | ||
before do | ||
allow(libinput_commands).to receive(:new_cli_option_available?) | ||
.and_return(true) | ||
end | ||
|
||
it 'call `libinput debug-events`' do | ||
command = 'libinput debug-events' | ||
expect(Open3).to receive(:popen3) | ||
.with("stdbuf -oL -- #{command} --device stub_device") | ||
.and_return 'stub message' | ||
end | ||
end | ||
|
||
context 'with old cli version' do | ||
before do | ||
allow(libinput_commands).to receive(:new_cli_option_available?) | ||
.and_return(false) | ||
end | ||
|
||
it 'call `libinput-debug-events`' do | ||
command = 'libinput-debug-events' | ||
expect(Open3).to receive(:popen3) | ||
.with("stdbuf -oL -- #{command} --device stub_device") | ||
.and_return 'stub message' | ||
end | ||
end | ||
end | ||
end | ||
end |