Sha256: 99534da4245b652e968733b4597cfa945edb5193364b03f04ed1552c63e86eaf

Contents?: true

Size: 1.68 KB

Versions: 20

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require_relative '../events/records/record.rb'
require_relative '../events/records/gesture_record.rb'

module Fusuma
  module Plugin
    module Parsers
      # parse libinput and generate gesture record
      class LibinputGestureParser < Parser
        DEFAULT_SOURCE = 'libinput_command_input'

        # @param record [String]
        # @return [Records::GestureRecord, nil]
        def parse_record(record)
          case line = record.to_s
          when /GESTURE_SWIPE|GESTURE_PINCH/
            gesture, status, finger, direction = parse_libinput(line)
          else
            return
          end

          Events::Records::GestureRecord.new(status: status,
                                             gesture: gesture,
                                             finger: finger,
                                             direction: direction)
        end

        private

        def parse_libinput(line)
          _device, event_name, _time, other = line.strip.split(nil, 4)
          finger, other = other.split(nil, 2)

          direction = parse_direction(other)
          [*detect_gesture(event_name), finger, direction]
        end

        def detect_gesture(event_name)
          event_name =~ /GESTURE_(SWIPE|PINCH)_(BEGIN|UPDATE|END)/
          [Regexp.last_match(1).downcase, Regexp.last_match(2).downcase]
        end

        def parse_direction(line)
          return if line.nil?

          move_x, move_y, _, _, _, zoom, _, rotate = line.tr('/|(|)', ' ').split
          Events::Records::GestureRecord::Delta.new(move_x.to_f, move_y.to_f,
                                                    zoom.to_f, rotate.to_f)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
fusuma-2.0.0.pre lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.11.1 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.10.2 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.10.1 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.10.0 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.9.0 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.8.0 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.7.0 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.6.0 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.5.0 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.4.1 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.4.0 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.3.3 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.3.2 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.3.1 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.3.0 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.2.1 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.2 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.1 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb
fusuma-1.0 lib/fusuma/plugin/parsers/libinput_gesture_parser.rb