evdev 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6db3c1910ac57b43eba5481021d04ff0035eb694
4
+ data.tar.gz: b2ba03ea15a327dba67f96d3bec5faa706dd5f68
5
+ SHA512:
6
+ metadata.gz: 4687f24d2dfa42270ff8c9406d8dc084cf94bc9787ddd6b1b38dd525f8e908839affb9e2b530de94a958f8fafd88d355872bc04d3e41580080f7b59b36bb1202
7
+ data.tar.gz: ac2701a0dfca11d5fd9f0066dcd1f7a66f0b920887e6a741cbfdccddf9d10fd7b9961182725ad53a9a07e1ebf9ee562800e395e1de6a33920d9eb2949ee2f686
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rakeTasks ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build evdev-0.1.0.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Build and install evdev-0.1.0.gem into system gems" fullCmd="install" taksId="install" /><RakeTask description="Create tag v0.1.0 and build and push evdev-0.1.0.gem to Rubygems" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper.rb
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Christopher Aue
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Evdev
2
+
3
+ A ruby wrapper around [Libevdev](https://github.com/christopheraue/ruby-libevdev)
4
+ for a nice and easy access to linux's event devices like keyboards and mice.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'evdev'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install evdev
21
+
22
+ ## Usage
23
+
24
+ Let's have a look at my keyboard:
25
+
26
+ ### Initializing a device
27
+
28
+ ```ruby
29
+ require 'evdev'
30
+ keyboard = Evdev.new('/dev/input/event0')
31
+ ```
32
+
33
+ ### Querying the device
34
+
35
+ ```ruby
36
+ keyboard.name # => "Logitech USB Receiver"
37
+ keyboard.phys # => "usb-0000:00:1d.2-1/input0"
38
+ keyboard.uniq # => ""
39
+ keyboard.vendor_id # => 1133
40
+ keyboard.product_id # => 50473
41
+ keyboard.bustype # => :USB
42
+ keyboard.version # => 273
43
+ keyboard.driver_version # => 65537
44
+ keyboard.has_property? :pointer # => false
45
+ ```
46
+
47
+ ### Grabbing a device
48
+
49
+ ```ruby
50
+ keyboard.grab # => true if successful, else false
51
+ keyboard.ungrab # => true if successful, else false
52
+ ```
53
+
54
+ ### Event handling
55
+
56
+ Evdev lumps event type and code together. You just need to give it the name of
57
+ the code and it derives the implied type from it internally.
58
+
59
+ ```ruby
60
+ keyboard.supports_event? :KEY_A # => true
61
+ keyboard.supports_event? :ABS_X # => false
62
+
63
+ key_a_handler = keyboard.on(:KEY_A) do |value|
64
+ puts "key 'a' #{%w(up down repeat)[value]}"
65
+ end
66
+
67
+ loop do
68
+ Kernel.select([keyboard.event_channel])
69
+ keyboard.handle_event
70
+ # => puts "key 'a' down" and "key 'a' up" on a single KEY_A stroke
71
+ end
72
+ ```
73
+
74
+ Removing the handler:
75
+
76
+ ```ruby
77
+ keyboard.off(:KEY_A, key_a_handler)
78
+ ```
79
+
80
+ Evdev includes the [CallbacksAttachable](https://github.com/christopheraue/ruby-callbacks_attachable)
81
+ mixin to attach and detach callbacks. Have a look at its API in its own documentation.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/evdev.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'evdev/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "evdev"
8
+ spec.version = Evdev::VERSION
9
+ spec.authors = ["Christopher Aue"]
10
+ spec.email = ["mail@christopheraue.net"]
11
+
12
+ spec.summary = %q{A ruby object wrapper around libevdev bindings.}
13
+ spec.homepage = "https://github.com/christopheraue/ruby-evdev"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "libevdev", "~> 1.0"
21
+ spec.add_runtime_dependency "callbacks_attachable", "~> 0.1"
22
+ spec.add_development_dependency "bundler", "~> 1.8"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.2.0"
25
+ spec.add_development_dependency "rspec-its"
26
+ spec.add_development_dependency "rspec-mocks-matchers-send_message"
27
+ end
data/lib/evdev.rb ADDED
@@ -0,0 +1,111 @@
1
+ require "bundler/setup"
2
+ require "callbacks_attachable"
3
+ require "libevdev"
4
+ require "evdev/version"
5
+ require "evdev/converter"
6
+ require "evdev/abs_axis"
7
+
8
+ class Evdev
9
+ include CallbacksAttachable
10
+
11
+ class << self
12
+ def finalize(device)
13
+ proc { Libevdev.free(device) }
14
+ end
15
+
16
+ def all(file_paths = '/dev/input/event*')
17
+ Dir[file_paths].map{ |file_path| new(file_path) }
18
+ end
19
+ end
20
+
21
+ def initialize(file_path)
22
+ @file = File.open(file_path)
23
+ device_ptr = FFI::MemoryPointer.new :pointer
24
+ Libevdev.new_from_fd(@file.fileno, device_ptr)
25
+ @device = device_ptr.read_pointer
26
+
27
+ ObjectSpace.define_finalizer(self, self.class.finalize(@device))
28
+ end
29
+
30
+ attr_reader :file
31
+ alias_method :event_channel, :file
32
+
33
+ def name
34
+ Libevdev.get_name(@device)
35
+ end
36
+
37
+ def phys
38
+ Libevdev.get_phys(@device)
39
+ end
40
+
41
+ def uniq
42
+ Libevdev.get_uniq(@device)
43
+ end
44
+
45
+ def vendor_id
46
+ Libevdev.get_id_vendor(@device)
47
+ end
48
+
49
+ def product_id
50
+ Libevdev.get_id_product(@device)
51
+ end
52
+
53
+ def bustype
54
+ Libevdev.get_id_bustype(@device)
55
+ end
56
+
57
+ def version
58
+ Libevdev.get_id_version(@device)
59
+ end
60
+
61
+ def driver_version
62
+ Libevdev.get_driver_version(@device)
63
+ end
64
+
65
+ def has_property?(property)
66
+ 1 == Libevdev.has_property(@device, Converter.property_to_int(property))
67
+ end
68
+
69
+ def abs_axis(code)
70
+ AbsAxis.new(@device, Converter.code_to_int(code))
71
+ end
72
+
73
+ def grab
74
+ 0 == Libevdev.grab(@device, Libevdev::GRAB)
75
+ end
76
+
77
+ def ungrab
78
+ 0 == Libevdev.grab(@device, Libevdev::UNGRAB)
79
+ end
80
+
81
+ def supports_event?(event)
82
+ type = Converter.code_to_type(event)
83
+ handles_event_type?(type) and handles_event_code?(type, event)
84
+ end
85
+
86
+ def handle_event(mode = :blocking)
87
+ event = LinuxInput::InputEvent.new
88
+ Libevdev.next_event(@device, Libevdev.const_get(:"READ_FLAG_#{mode.upcase}"), event.pointer)
89
+ trigger(Converter.int_to_name(event[:type], event[:code]), event[:value])
90
+ end
91
+
92
+ def events_pending?
93
+ 1 == Libevdev.has_event_pending(@device)
94
+ end
95
+
96
+ def event_value(event)
97
+ return unless supports_event?(event)
98
+ type = Converter.code_to_type(event)
99
+ Libevdev.get_event_value(@device, Converter.type_to_int(type), Converter.code_to_int(event))
100
+ end
101
+
102
+ private
103
+
104
+ def handles_event_type?(type)
105
+ 1 == Libevdev.has_event_type(@device, Converter.type_to_int(type))
106
+ end
107
+
108
+ def handles_event_code?(type, code)
109
+ 1 == Libevdev.has_event_code(@device, Converter.type_to_int(type), Converter.code_to_int(code))
110
+ end
111
+ end
@@ -0,0 +1,28 @@
1
+ class Evdev
2
+ class AbsAxis
3
+ def initialize(device, code)
4
+ @device = device
5
+ @code = code
6
+ end
7
+
8
+ def maximum
9
+ Libevdev.get_abs_maximum(@device, @code)
10
+ end
11
+
12
+ def minimum
13
+ Libevdev.get_abs_minimum(@device, @code)
14
+ end
15
+
16
+ def fuzz
17
+ Libevdev.get_abs_fuzz(@device, @code)
18
+ end
19
+
20
+ def flat
21
+ Libevdev.get_abs_flat(@device, @code)
22
+ end
23
+
24
+ def resolution
25
+ Libevdev.get_abs_resolution(@device, @code)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,39 @@
1
+ class Evdev
2
+ module Converter; end
3
+ class << Converter
4
+ def code_to_type(code)
5
+ :"EV_#{code.to_s.split('_').first}"
6
+ end
7
+
8
+ def property_to_int(property)
9
+ LinuxInput.const_get(:"INPUT_PROP_#{property.upcase}")
10
+ end
11
+
12
+ def type_to_int(type)
13
+ LinuxInput.const_get(type.upcase)
14
+ end
15
+
16
+ def code_to_int(code)
17
+ LinuxInput.const_get(code.upcase)
18
+ end
19
+
20
+ def int_to_name(type_int, code_int)
21
+ @event_names ||= {}
22
+ @event_names[type_int] ||= consts_starting_with("#{int_to_type(type_int)}_")
23
+ @event_names[type_int][code_int]
24
+ end
25
+
26
+ private
27
+
28
+ def int_to_type(int)
29
+ @event_types ||= consts_starting_with('EV_')
30
+ @event_types[int][3..-1]
31
+ end
32
+
33
+ def consts_starting_with(prefix)
34
+ LinuxInput.constants(false).map do |const|
35
+ [LinuxInput.const_get(const), const] if const.to_s.start_with? prefix
36
+ end.compact.to_h
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ class Evdev
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evdev
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Aue
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: libevdev
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: callbacks_attachable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.2.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.2.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-mocks-matchers-send_message
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - mail@christopheraue.net
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rakeTasks"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - evdev.gemspec
127
+ - lib/evdev.rb
128
+ - lib/evdev/abs_axis.rb
129
+ - lib/evdev/converter.rb
130
+ - lib/evdev/version.rb
131
+ homepage: https://github.com/christopheraue/ruby-evdev
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.4.8
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: A ruby object wrapper around libevdev bindings.
155
+ test_files: []