Class: Fusuma::Plugin::Remap::KeyboardRemapper::KeyboardSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/fusuma/plugin/remap/keyboard_remapper.rb

Overview

Devices to detect key presses and releases

Instance Method Summary collapse

Constructor Details

#initialize(names) ⇒ KeyboardSelector

Returns a new instance of KeyboardSelector.



652
653
654
# File 'lib/fusuma/plugin/remap/keyboard_remapper.rb', line 652

def initialize(names)
  @names = names
end

Instance Method Details

#selectArray<Revdev::EventDevice>

Select devices that match the name If no device is found, it will wait for 3 seconds and try again

Returns:



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/fusuma/plugin/remap/keyboard_remapper.rb', line 659

def select
  logged_no_device = false
  loop do
    keyboards = try_open_devices

    if keyboards.empty?
      unless logged_no_device
        MultiLogger.warn "No keyboard found: #{@names}"
        logged_no_device = true
      end

      wait_for_device
    else
      return keyboards
    end
  end
end

#try_open_devicesObject



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/fusuma/plugin/remap/keyboard_remapper.rb', line 677

def try_open_devices
  Fusuma::Device.reset # reset cache to get the latest device information
  devices = Fusuma::Device.all.select do |d|
    next if d.name == VIRTUAL_KEYBOARD_NAME

    Array(@names).any? { |name| d.name =~ /#{name}/ }
  end

  devices.filter_map do |d|
    Revdev::EventDevice.new("/dev/input/#{d.id}")
  rescue Errno::ENOENT, Errno::ENODEV, Errno::EACCES => e
    MultiLogger.warn "Failed to open #{d.name} (/dev/input/#{d.id}): #{e.message}"
    nil
  end
end