Skip to content

Commit d8062dd

Browse files
committed
feat: support libinput 1.27.0 or later
- Add `libinput_1_27_0_or_later?` method to `LibinputCommand` - Add `parse_line_1_27_0_or_later` method to `LibinputGestureParser`
1 parent 7f077f8 commit d8062dd

File tree

3 files changed

+151
-26
lines changed

3 files changed

+151
-26
lines changed

lib/fusuma/libinput_command.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def new_cli_option_available?
2020
Gem::Version.new(version) >= Gem::Version.new(NEW_CLI_OPTION_VERSION)
2121
end
2222

23+
# @return [Boolean]
24+
def libinput_1_27_0_or_later?
25+
Gem::Version.new(version) >= Gem::Version.new("1.27.0")
26+
end
27+
2328
# @return [String]
2429
def version
2530
# version_command prints "1.6.3\n"

lib/fusuma/plugin/parsers/libinput_gesture_parser.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative "../events/records/record"
44
require_relative "../events/records/gesture_record"
5+
require_relative "../../libinput_command"
56

67
module Fusuma
78
module Plugin
@@ -29,6 +30,20 @@ def parse_record(record)
2930
private
3031

3132
def parse_libinput(line)
33+
if libinput_1_27_0_or_later?
34+
parse_line_1_27_0_or_later(line)
35+
else
36+
parse_line(line)
37+
end
38+
end
39+
40+
def libinput_1_27_0_or_later?
41+
return @libinput_1_27_0_or_later if defined?(@libinput_1_27_0_or_later)
42+
43+
@libinput_1_27_0_or_later = Inputs::LibinputCommandInput.new.command.libinput_1_27_0_or_later?
44+
end
45+
46+
def parse_line(line)
3247
_device, event_name, _time, other = line.strip.split(nil, 4)
3348
finger, other = other.split(nil, 2)
3449

@@ -39,6 +54,22 @@ def parse_libinput(line)
3954
[gesture, status, finger, delta]
4055
end
4156

57+
def parse_line_1_27_0_or_later(line)
58+
_device, event_name, other = line.strip.split(nil, 3)
59+
60+
if other[0] != "+"
61+
_seq, other = other.split(nil, 2)
62+
end
63+
64+
_time, finger, other = other.split(nil, 3)
65+
66+
gesture, status = *detect_gesture(event_name)
67+
68+
status = "cancelled" if gesture == "hold" && status == "end" && other == "cancelled"
69+
delta = parse_delta(other)
70+
[gesture, status, finger, delta]
71+
end
72+
4273
def detect_gesture(event_name)
4374
event_name =~ /GESTURE_(SWIPE|PINCH|HOLD)_(BEGIN|UPDATE|END)/
4475
gesture = Regexp.last_match(1).downcase

spec/fusuma/plugin/parsers/libinput_gesture_parser_spec.rb

Lines changed: 115 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "spec_helper"
44
require "./lib/fusuma/plugin/parsers/parser"
5-
require "./lib/fusuma/plugin/events/event"
5+
require "./lib/fusuma/plugin/inputs/input"
66

77
module Fusuma
88
module Plugin
@@ -36,39 +36,128 @@ module Parsers
3636
it { expect(parser.parse(event)).to eq event }
3737
end
3838

39-
context "with libinput_command_input event" do
40-
let(:event) { Events::Event.new(tag: "libinput_command_input", record: record) }
39+
context "with libinput version 1.27.0 or later" do
40+
before do
41+
allow_any_instance_of(LibinputCommand).to receive(:libinput_1_27_0_or_later?).and_return(true)
42+
end
43+
4144
context "with swipe gestures" do
42-
# event10 GESTURE_SWIPE_BEGIN +0.728s 3
43-
# event10 GESTURE_SWIPE_UPDATE +0.948s 3 0.23/ 0.00 ( 0.29/ 0.00 unaccelerated)
44-
# event10 GESTURE_SWIPE_END +0.989s 3
45-
let(:record) { "event10 GESTURE_SWIPE_BEGIN +0.728s 3" }
46-
it { expect(parser.parse(event).record).to be_a Events::Records::GestureRecord }
47-
it { expect(parser.parse(event).record.status).to eq "begin" }
45+
before do
46+
@debug_events = <<~EVENTS
47+
event4 GESTURE_SWIPE_BEGIN +19.410s 3
48+
event4 GESTURE_SWIPE_UPDATE +19.410s 3 19.02/ 1.00 ( 4.17/ 0.22 unaccelerated)
49+
event4 GESTURE_SWIPE_UPDATE 2 +19.417s 3 21.24/ 0.00 ( 4.61/ 0.00 unaccelerated)
50+
event4 GESTURE_SWIPE_UPDATE 3 +19.424s 3 6.36/-0.22 ( 6.36/-0.22 unaccelerated)
51+
event4 GESTURE_SWIPE_END +19.614s 3
52+
EVENTS
53+
.split("\n")
54+
end
55+
56+
let(:event) {
57+
-> {
58+
record = @debug_events.shift
59+
Events::Event.new(tag: "libinput_command_input", record: record)
60+
}
61+
}
62+
63+
it "has a gesture record" do
64+
expect(parser.parse(event.call).record).to be_a Events::Records::GestureRecord
65+
end
66+
67+
it "has a gesture record that it has a status" do
68+
expect(parser.parse(event.call).record.status).to eq "begin"
69+
expect(parser.parse(event.call).record.status).to eq "update"
70+
expect(parser.parse(event.call).record.status).to eq "update"
71+
expect(parser.parse(event.call).record.status).to eq "update"
72+
expect(parser.parse(event.call).record.status).to eq "end"
73+
end
74+
75+
it "has a gesture record that it has finger num" do
76+
expect(parser.parse(event.call).record.finger).to eq 3
77+
expect(parser.parse(event.call).record.finger).to eq 3
78+
expect(parser.parse(event.call).record.finger).to eq 3
79+
expect(parser.parse(event.call).record.finger).to eq 3
80+
expect(parser.parse(event.call).record.finger).to eq 3
81+
end
4882
end
83+
end
4984

50-
context "with hold gestures" do
51-
# -event10 GESTURE_HOLD_BEGIN +2.125s 3
52-
# event10 GESTURE_HOLD_END +3.274s 3
53-
# event10 GESTURE_HOLD_BEGIN +5.573s 4
54-
# event10 GESTURE_HOLD_END +6.462s 4 cancelled
55-
context "with begin" do
56-
let(:record) { "-event10 GESTURE_HOLD_BEGIN +2.125s 3" }
57-
it { expect(parser.parse(event).record).to be_a Events::Records::GestureRecord }
58-
it { expect(parser.parse(event).record.status).to eq "begin" }
85+
context "with libinput version 1.26.0 or earlier" do
86+
before do
87+
allow_any_instance_of(LibinputCommand).to receive(:libinput_1_27_0_or_later?).and_return(false)
88+
end
89+
90+
context "with swipe gestures" do
91+
before do
92+
@debug_events = <<~EVENTS
93+
event10 GESTURE_SWIPE_BEGIN +0.728s 3
94+
event10 GESTURE_SWIPE_UPDATE +0.948s 3 0.23/ 0.00 ( 0.29/ 0.00 unaccelerated)
95+
event10 GESTURE_SWIPE_END +0.989s 3
96+
EVENTS
97+
.split("\n")
5998
end
60-
context "with end" do
61-
let(:record) { " event10 GESTURE_HOLD_END +3.274s 3" }
62-
it { expect(parser.parse(event).record).to be_a Events::Records::GestureRecord }
63-
it { expect(parser.parse(event).record.status).to eq "end" }
99+
100+
let(:event) {
101+
-> {
102+
record = @debug_events.shift
103+
Events::Event.new(tag: "libinput_command_input", record: record)
104+
}
105+
}
106+
107+
it "has a gesture record" do
108+
expect(parser.parse(event.call).record).to be_a Events::Records::GestureRecord
109+
end
110+
111+
it "has a gesture record that it has a status" do
112+
expect(parser.parse(event.call).record.status).to eq "begin"
113+
expect(parser.parse(event.call).record.status).to eq "update"
114+
expect(parser.parse(event.call).record.status).to eq "end"
64115
end
65-
context "with end(cancelled)" do
66-
let(:record) { " event10 GESTURE_HOLD_END +6.462s 4 cancelled" }
67-
it { expect(parser.parse(event).record).to be_a Events::Records::GestureRecord }
68-
it { expect(parser.parse(event).record.status).to eq "cancelled" }
116+
117+
it "has a gesture record that it has finger num" do
118+
expect(parser.parse(event.call).record.finger).to eq 3
119+
expect(parser.parse(event.call).record.finger).to eq 3
120+
expect(parser.parse(event.call).record.finger).to eq 3
69121
end
70122
end
71123
end
124+
125+
context "with hold gestures" do
126+
before do
127+
@debug_events = <<~EVENTS
128+
-event10 GESTURE_HOLD_BEGIN +2.125s 3
129+
event10 GESTURE_HOLD_END +3.274s 3
130+
event10 GESTURE_HOLD_BEGIN +5.573s 4
131+
event10 GESTURE_HOLD_END +6.462s 4 cancelled
132+
EVENTS
133+
.split("\n")
134+
end
135+
136+
let(:event) {
137+
-> {
138+
record = @debug_events.shift
139+
Events::Event.new(tag: "libinput_command_input", record: record)
140+
}
141+
}
142+
143+
it "has a gesture record" do
144+
expect(parser.parse(event.call).record).to be_a Events::Records::GestureRecord
145+
end
146+
147+
it "has a gesture record that it has a status" do
148+
expect(parser.parse(event.call).record.status).to eq "begin"
149+
expect(parser.parse(event.call).record.status).to eq "end"
150+
expect(parser.parse(event.call).record.status).to eq "begin"
151+
expect(parser.parse(event.call).record.status).to eq "cancelled"
152+
end
153+
154+
it "has a gesture record that it has finger num" do
155+
expect(parser.parse(event.call).record.finger).to eq 3
156+
expect(parser.parse(event.call).record.finger).to eq 3
157+
expect(parser.parse(event.call).record.finger).to eq 4
158+
expect(parser.parse(event.call).record.finger).to eq 4
159+
end
160+
end
72161
end
73162
end
74163
end

0 commit comments

Comments
 (0)