Class: Evdev

Inherits:
Object
  • Object
show all
Includes:
CallbacksAttachable
Defined in:
lib/evdev.rb,
lib/evdev/version.rb,
lib/evdev/abs_axis.rb,
lib/evdev/converter.rb

Defined Under Namespace

Modules: AwaitEvent, Converter Classes: AbsAxis

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Evdev

Returns a new instance of Evdev.



23
24
25
26
27
28
29
30
# File 'lib/evdev.rb', line 23

def initialize(file_path)
  @file = File.open(file_path)
  device_ptr = FFI::MemoryPointer.new :pointer
  Libevdev.new_from_fd(@file.fileno, device_ptr)
  @device = device_ptr.read_pointer

  ObjectSpace.define_finalizer(self, self.class.finalize(@device))
end

Instance Attribute Details

#fileObject (readonly) Also known as: event_channel

Returns the value of attribute file.



32
33
34
# File 'lib/evdev.rb', line 32

def file
  @file
end

Class Method Details

.all(file_paths = '/dev/input/event*') ⇒ Object



18
19
20
# File 'lib/evdev.rb', line 18

def all(file_paths = '/dev/input/event*')
  Dir[file_paths].map{ |file_path| new(file_path) }
end

.finalize(device) ⇒ Object



14
15
16
# File 'lib/evdev.rb', line 14

def finalize(device)
  proc { Libevdev.free(device) }
end

Instance Method Details

#abs_axis(code) ⇒ Object



71
72
73
# File 'lib/evdev.rb', line 71

def abs_axis(code)
  AbsAxis.new(@device, Converter.code_to_int(code))
end

#bustypeObject



55
56
57
# File 'lib/evdev.rb', line 55

def bustype
  Libevdev.get_id_bustype(@device)
end

#driver_versionObject



63
64
65
# File 'lib/evdev.rb', line 63

def driver_version
  Libevdev.get_driver_version(@device)
end

#event_value(event) ⇒ Object



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

def event_value(event)
  return unless supports_event?(event)
  type = Converter.code_to_type(event)
  Libevdev.get_event_value(@device, Converter.type_to_int(type), Converter.code_to_int(event))
end

#events_pending?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/evdev.rb', line 97

def events_pending?
  1 == Libevdev.has_event_pending(@device)
end

#grabObject



75
76
77
# File 'lib/evdev.rb', line 75

def grab
  0 == Libevdev.grab(@device, Libevdev::GRAB)
end

#handle_event(mode = :blocking) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/evdev.rb', line 88

def handle_event(mode = :blocking)
  event = LinuxInput::InputEvent.new
  Libevdev.next_event(@device, Libevdev.const_get(:"READ_FLAG_#{mode.upcase}"), event.pointer)
  event_name = Converter.int_to_name(event[:type], event[:code])
  trigger(event_name, event[:value], event_name)
rescue Errno::EAGAIN => e
  raise e.extend AwaitEvent
end

#has_property?(property) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/evdev.rb', line 67

def has_property?(property)
  1 == Libevdev.has_property(@device, Converter.property_to_int(property))
end

#nameObject



35
36
37
# File 'lib/evdev.rb', line 35

def name
  Libevdev.get_name(@device)
end

#physObject



39
40
41
# File 'lib/evdev.rb', line 39

def phys
  Libevdev.get_phys(@device)
end

#product_idObject



51
52
53
# File 'lib/evdev.rb', line 51

def product_id
  Libevdev.get_id_product(@device)
end

#supports_event?(event) ⇒ Boolean

Returns:

  • (Boolean)


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

def supports_event?(event)
  type = Converter.code_to_type(event)
  handles_event_type?(type) and handles_event_code?(type, event)
end

#ungrabObject



79
80
81
# File 'lib/evdev.rb', line 79

def ungrab
  0 == Libevdev.grab(@device, Libevdev::UNGRAB)
end

#uniqObject



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

def uniq
  Libevdev.get_uniq(@device)
end

#vendor_idObject



47
48
49
# File 'lib/evdev.rb', line 47

def vendor_id
  Libevdev.get_id_vendor(@device)
end

#versionObject



59
60
61
# File 'lib/evdev.rb', line 59

def version
  Libevdev.get_id_version(@device)
end