Class: Rkremap::EvdevList
- Inherits:
-
Object
- Object
- Rkremap::EvdevList
- Includes:
- Enumerable
- Defined in:
- lib/rkremap/evdev_list.rb
Instance Attribute Summary collapse
-
#device_list ⇒ Object
readonly
Returns the value of attribute device_list.
-
#grab ⇒ Object
Returns the value of attribute grab.
Instance Method Summary collapse
- #detect ⇒ Object
- #detect_loop ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(device_files, auto_detect: false, detect_mouse: false, exclude: nil) ⇒ EvdevList
constructor
A new instance of EvdevList.
- #read_event ⇒ Object
- #remove_device(path) ⇒ Object
- #select(timeout) ⇒ Evdev
Constructor Details
#initialize(device_files, auto_detect: false, detect_mouse: false, exclude: nil) ⇒ EvdevList
Returns a new instance of EvdevList.
14 15 16 17 18 19 20 21 22 |
# File 'lib/rkremap/evdev_list.rb', line 14 def initialize(device_files, auto_detect: false, detect_mouse: false, exclude: nil) @device_files = device_files @auto_detect = auto_detect @detect_mouse = detect_mouse @exclude = exclude @io2evdev = {} # {IO => Evdev} @device_list = {} # {path => Evdev} @prev_devices = [] end |
Instance Attribute Details
#device_list ⇒ Object (readonly)
Returns the value of attribute device_list.
7 8 9 |
# File 'lib/rkremap/evdev_list.rb', line 7 def device_list @device_list end |
#grab ⇒ Object
Returns the value of attribute grab.
8 9 10 |
# File 'lib/rkremap/evdev_list.rb', line 8 def grab @grab end |
Instance Method Details
#detect ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rkremap/evdev_list.rb', line 38 def detect devs = Dir.glob("/dev/input/event*") (@device_list.keys - devs).each{|path| remove_device(path)} new_devs = devs - @prev_devices @prev_devices = devs new_devs &= @device_files unless @auto_detect new_devs -= @device_list.keys new_devs.each do |path| ev = Evdev.new(path) stat = nil if @exclude && ev.name =~ @exclude stat = 'excluded' elsif ev.keyboard? || (@detect_mouse && ev.mouse?) ev.grab if ev.match?(@grab) @io2evdev[ev.io] = ev @device_list[path] = ev stat = ev.grab? ? 'grabbed' : 'not grabbed' else stat = 'ignored' end puts "detect: #{ev.name} (#{ev.path}) #{stat}" if $VERBOSE end end |
#detect_loop ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/rkremap/evdev_list.rb', line 30 def detect_loop detect timer = Tmtms::Timer.new timer.repeat(3) do detect end end |
#each(&block) ⇒ Object
24 25 26 27 28 |
# File 'lib/rkremap/evdev_list.rb', line 24 def each(&block) @device_list.each_value do |evdev| block.call evdev end end |
#read_event ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rkremap/evdev_list.rb', line 78 def read_event while true evdev = select(3) next unless evdev begin return evdev.read_event rescue Errno::ENODEV remove_device(evdev.path) end end end |
#remove_device(path) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/rkremap/evdev_list.rb', line 63 def remove_device(path) evdev = @device_list[path] puts "disconnect: #{evdev.name} (#{evdev.path})" if $VERBOSE @io2evdev.delete evdev.io evdev.close @device_list.delete path end |
#select(timeout) ⇒ Evdev
73 74 75 76 |
# File 'lib/rkremap/evdev_list.rb', line 73 def select(timeout) r, = IO.select(@device_list.values.map(&:io), nil, nil, timeout) r && @io2evdev[r[0]] end |