Class: Rkremap::Evdev

Inherits:
Object
  • Object
show all
Defined in:
lib/rkremap/evdev.rb

Overview

Constant Summary collapse

EVIOCGBIT_ANY =

EVIOCGBIT(0, 1)

2147566880
EVIOCGBIT_EV_KEY =

EVIOCGBIT(EV_KEY, (KEY_CNT-1)/8+1)

2153792801
EVIOCGRAB =
1074021776
EVIOCGNAME =

EVIOCGNAME(256)

2164278534

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Evdev

Returns a new instance of Evdev.

Parameters:

  • path (String)


13
14
15
16
17
18
19
# File 'lib/rkremap/evdev.rb', line 13

def initialize(path)
  @path = path
  @io = nil
  @capa = nil
  @uinput = nil
  @grab = false
end

Instance Attribute Details

#pathString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/rkremap/evdev.rb', line 10

def path
  @path
end

Instance Method Details

#capable?(key) ⇒ Boolean

Parameters:

  • key (Integer)

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
# File 'lib/rkremap/evdev.rb', line 49

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

#closeObject



43
44
45
# File 'lib/rkremap/evdev.rb', line 43

def close
  @io&.close
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/rkremap/evdev.rb', line 25

def eql?(other)
  other.is_a?(Evdev) && self.path == other.path
end

#grabObject



83
84
85
86
# File 'lib/rkremap/evdev.rb', line 83

def grab
  io.ioctl(EVIOCGRAB, 1)
  @grab = true
end

#grab?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/rkremap/evdev.rb', line 89

def grab?
  @grab
end

#inspectObject



21
22
23
# File 'lib/rkremap/evdev.rb', line 21

def inspect
  "#<Rkremap::Evdev: #{name} (#{path})>"
end

#ioIO

Returns:

  • (IO)


30
31
32
33
# File 'lib/rkremap/evdev.rb', line 30

def io
  return @io if @io
  @io = File.open(@path)
end

#keyboard?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rkremap/evdev.rb', line 59

def keyboard?
  return @is_keyboard unless @is_keyboard.nil?
  @is_keyboard = false
  return false if name =~ /\Arkremap\0*\z/

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

#match?(pattern) ⇒ Boolean

Parameters:

  • pattern (Symbol, Regexp)

Returns:

  • (Boolean)


108
109
110
111
112
113
114
# File 'lib/rkremap/evdev.rb', line 108

def match?(pattern)
  return true if pattern == true
  return true if pattern == :keyboard && keyboard?
  return true if pattern == :mouse && mouse?
  return true if pattern.is_a?(Regexp) && pattern =~ name
  return false
end

#mouse?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
# File 'lib/rkremap/evdev.rb', line 72

def mouse?
  return @is_mouse unless @is_mouse.nil?
  @is_mouse = false
  return false if name =~ /\Arkremap\0*\z/

  buf = ''
  io.ioctl(EVIOCGBIT_ANY, buf)
  return false if buf.unpack1('C')[EV_KEY] == 0
  @is_mouse = capable?(KeyCode::BTN_MOUSE)
end

#nameString

Returns:

  • (String)


36
37
38
39
40
41
# File 'lib/rkremap/evdev.rb', line 36

def name
  return @name if @name
  buf = ''
  io.ioctl(EVIOCGNAME, buf)
  @name = buf.sub(/\0+$/, '')
end

#read_eventRkremap::Event

struct input_event

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

;

Returns:



100
101
102
103
104
105
# File 'lib/rkremap/evdev.rb', line 100

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)
  Event.new(self, time, type, code, value)
end