Libevdev

1:1 FFI ruby bindings for libevdev.

For a nice object wrapper around the raw bindings have a look at the evdev gem.

Installation

Add this line to your application's Gemfile:

gem 'libevdev'

And then execute:

$ bundle

Or install it yourself as:

$ gem install libevdev

Usage

require 'libevdev'

All libevdev constants and methods are available under the Libevdev namespace. The LIBEVDEV_ and libevdev_ prefixes of the C API have been dropped. Besides that, all names were left unchanged.

Accessing constants

Libevdev::READ_FLAG_SYNC
Libevdev::GRAB
Libevdev::READ_STATUS_SUCCESS
# an so on …

Initializing a new libevdev device

file = File.open('/dev/input/event0')

# pointer to the pointer of the to be created libevdev struct
evdev_ptr = FFI::MemoryPointer.new :pointer

Libevdev.new_from_fd(file.fileno, evdev_ptr)

# get the pointer to the actual opaque libevdev struct
evdev = evdev_ptr.read_pointer

Listening to events

Libevdev also loads the LinuxInput gem for its structs and constants.

event = LinuxInput::InputEvent.new
loop do
    Libevdev.next_event(evdev, Libevdev::READ_FLAG_NORMAL, event.pointer)
    puts event[:type]
    puts event[:code]
    puts event[:value]
end

Freeing the device

Libevdev.free(evdev)