class Rkremap::Evdev

www.kernel.org/doc/html/v4.12/input/input_uapi.html

Constants

EVIOCGBIT_ANY
EVIOCGBIT_EV_KEY
EVIOCGNAME
EVIOCGRAB

Attributes

path[R]

@return [String]

Public Class Methods

detect() click to toggle source

@return [Array<Evdev>]

# File lib/rkremap/evdev.rb, line 21
def self.detect
  devs = []
  new_devs = Dir.children("/dev/input").map do |dev|
    if dev =~ /\Aevent\d+\z/
      path = "/dev/input/#{dev}"
      devs.push path
      next if @device_list[path]
      @device_list[path] = true
      Evdev.new(path)
    end
  end.compact
  (@device_list.keys - devs).each do |path|
    @device_list.delete path
  end
  new_devs
end
io2evdev() click to toggle source
# File lib/rkremap/evdev.rb, line 14
def self.io2evdev
  @io2evdev
end
new(path) click to toggle source

@param path [String]

# File lib/rkremap/evdev.rb, line 55
def initialize(path)
  @path = path
  @io = nil
  @capa = nil
  @uinput = nil
end
remove_device(evdev) click to toggle source
# File lib/rkremap/evdev.rb, line 38
def self.remove_device(evdev)
  @io2evdev.delete evdev.io
  @device_list.delete evdev.path
end
select(inputs, timeout) click to toggle source

@param inputs [Array<Evdev>] @param timeout [Numeric] @return [Evdev]

# File lib/rkremap/evdev.rb, line 46
def self.select(inputs, timeout)
  r, = IO.select(inputs.map(&:io), nil, nil, timeout)
  r && @io2evdev[r[0]]
end

Public Instance Methods

capable?(key) click to toggle source

@param key [Integer] @return [Boolean]

# File lib/rkremap/evdev.rb, line 81
def capable?(key)
  unless @capa
    buf = ' ' * ((KeyCode::KEY_MAX-1)/8+1)
    io.ioctl(EVIOCGBIT_EV_KEY, buf)
    @capa = buf.unpack('C*')
  end
  @capa[key/8][key%8] != 0
end
close() click to toggle source
# File lib/rkremap/evdev.rb, line 74
def close
  @io&.close
  self.class.remove_device(self)
end
eql?(other) click to toggle source
# File lib/rkremap/evdev.rb, line 62
def eql?(other)
  other.is_a?(Evdev) && self.path == other.path
end
grab() click to toggle source
# File lib/rkremap/evdev.rb, line 102
def grab
  io.ioctl(EVIOCGRAB, 1)
end
io() click to toggle source

@return [IO]

# File lib/rkremap/evdev.rb, line 67
def io
  return @io if @io
  @io = File.open(@path)
  self.class.io2evdev[@io] = self
  @io
end
keyboard?() click to toggle source

@return [Boolean]

# File lib/rkremap/evdev.rb, line 91
def keyboard?
  buf = ' '
  io.ioctl(EVIOCGNAME, buf)
  return false if buf =~ /\Arkremap\0*\z/

  io.ioctl(EVIOCGBIT_ANY, buf)
  return false if buf.unpack1('C')[EV_KEY] == 0
  capable?(KeyCode::KEY_0) && capable?(KeyCode::KEY_9) &&
    capable?(KeyCode::KEY_A) && capable?(KeyCode::KEY_Z) && capable?(KeyCode::KEY_SPACE)
end
read_event() click to toggle source

struct input_event {

struct timeval time;
unsigned short type;
unsigned short code;
unsigned int value;

}; @return [Array<Time, Integer, Integer, Integer>]

# File lib/rkremap/evdev.rb, line 113
def read_event
  raw = io.sysread(24)  # sizeof(struct input_event)
  sec, usec, type, code, value = raw.unpack('Q!Q!SSl')
  time = Time.at(sec, usec)
  return time, type, code, value
end