Class: Rkremap::EvdevList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rkremap/evdev_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_files, auto_detect: false, detect_mouse: false, exclude: nil) ⇒ EvdevList

Returns a new instance of EvdevList.

Parameters:

  • device_files (Array<String>)

    path list (/dev/input/event*)

  • auto_detect (Boolean) (defaults to: false)
  • detect_mouse (Boolean) (defaults to: false)
  • exclude (Regexp) (defaults to: nil)


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_listObject (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

#grabObject

Returns the value of attribute grab.



8
9
10
# File 'lib/rkremap/evdev_list.rb', line 8

def grab
  @grab
end

Instance Method Details

#detectObject



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_loopObject



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_eventObject



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

Parameters:

  • evdev (path)


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

Parameters:

  • timeout (Numeric)

Returns:



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