ruinput 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ruinput/uinput_device.rb +87 -0
- data/lib/ruinput/version.rb +1 -1
- data/lib/ruinput.rb +2 -0
- data/sample/input_j +28 -0
- data/sample/through_uinput +71 -0
- data/test/test_uinput_device.rb +54 -0
- metadata +8 -3
@@ -0,0 +1,87 @@
|
|
1
|
+
# -*- coding:utf-8; mode:ruby; -*-
|
2
|
+
|
3
|
+
require "revdev"
|
4
|
+
|
5
|
+
module Ruinput
|
6
|
+
|
7
|
+
# a class for /dev/uinput
|
8
|
+
class UinputDevice < Revdev::EventDevice
|
9
|
+
attr_reader :is_created
|
10
|
+
|
11
|
+
DEFUALT_LOCATION = "/dev/uinput"
|
12
|
+
DEFAULT_DEVICE_NAME = "ruinput"
|
13
|
+
DEFAULT_INPUT_ID = Revdev::InputId.new({ :bustype => Revdev::BUS_VIRTUAL,
|
14
|
+
:vendor => 1,
|
15
|
+
:product => 1,
|
16
|
+
:version => 1 })
|
17
|
+
|
18
|
+
def initialize location=nil
|
19
|
+
@is_created = false
|
20
|
+
location ||= DEFUALT_LOCATION
|
21
|
+
super location
|
22
|
+
end
|
23
|
+
|
24
|
+
# create virtual event divece
|
25
|
+
# _name_ :: device name
|
26
|
+
# _id_ :: InputId ("struct input_id" on input.h)
|
27
|
+
def create name = DEFAULT_DEVICE_NAME, id = DEFAULT_INPUT_ID
|
28
|
+
if not name.kind_of? String
|
29
|
+
raise ArgumentError, "1st arg expect String"
|
30
|
+
elsif not id.kind_of? Revdev::InputId
|
31
|
+
raise ArgumentError, "2nd arg expect Revdev::InputId"
|
32
|
+
end
|
33
|
+
|
34
|
+
recognize_as_keyboard
|
35
|
+
#recognize_as_mouse
|
36
|
+
|
37
|
+
uud = UinputUserDev.new({ :name => name, :id => id,
|
38
|
+
:ff_effects_max => 0, :absmax => [20],
|
39
|
+
:absmin => [30], :absfuzz => [4],
|
40
|
+
:absflat => [5] })
|
41
|
+
@file.syswrite uud.to_byte_string
|
42
|
+
|
43
|
+
@file.ioctl UI_DEV_CREATE, nil
|
44
|
+
@is_created = true
|
45
|
+
end
|
46
|
+
|
47
|
+
def destroy
|
48
|
+
if not @is_created
|
49
|
+
raise Exception, "invalid method call: this uinput is not created"
|
50
|
+
end
|
51
|
+
@file.ioctl UI_DEV_DESTROY, nil
|
52
|
+
@file.close
|
53
|
+
end
|
54
|
+
|
55
|
+
def recognize_as_keyboard
|
56
|
+
if @is_created
|
57
|
+
raise Exception, "invalid method call: this uinput is already created"
|
58
|
+
end
|
59
|
+
@file.ioctl UI_SET_EVBIT, Revdev::EV_KEY
|
60
|
+
Revdev::KEY_CNT.times do |i|
|
61
|
+
@file.ioctl UI_SET_KEYBIT, i
|
62
|
+
end
|
63
|
+
|
64
|
+
@file.ioctl UI_SET_EVBIT, Revdev::EV_MSC
|
65
|
+
Revdev::MSC_CNT.times do |i|
|
66
|
+
@file.ioctl UI_SET_MSCBIT, i
|
67
|
+
end
|
68
|
+
|
69
|
+
@file.ioctl UI_SET_EVBIT, Revdev::EV_REP
|
70
|
+
end
|
71
|
+
|
72
|
+
def recognize_as_mouse
|
73
|
+
if @is_created
|
74
|
+
raise Exception, "invalid method call: this uinput is already created"
|
75
|
+
end
|
76
|
+
@file.ioctl UI_SET_EVBIT, Revdev::EV_KEY
|
77
|
+
Revdev::KEY_CNT.times do |i|
|
78
|
+
@file.ioctl UI_SET_KEYBIT, i
|
79
|
+
end
|
80
|
+
@file.ioctl UI_SET_EVBIT, Revdev::EV_REL
|
81
|
+
Revdev::REL_CNT.times do |i|
|
82
|
+
@file.ioctl UI_SET_RELBIT, i
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
data/lib/ruinput/version.rb
CHANGED
data/lib/ruinput.rb
CHANGED
data/sample/input_j
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- coding:utf-8; mode:ruby; -*-
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "revdev"
|
5
|
+
require "ruinput"
|
6
|
+
|
7
|
+
def main
|
8
|
+
|
9
|
+
include Revdev
|
10
|
+
$uinput = Ruinput::UinputDevice.new
|
11
|
+
def $uinput.send type, code, value
|
12
|
+
write_input_event InputEvent.new nil, EV_MSC, MSC_SCAN, code
|
13
|
+
write_input_event InputEvent.new nil, type, code, value
|
14
|
+
write_input_event InputEvent.new nil, EV_SYN, SYN_REPORT, 0
|
15
|
+
end
|
16
|
+
|
17
|
+
$uinput.create
|
18
|
+
sleep 0.5
|
19
|
+
|
20
|
+
$uinput.send EV_KEY, KEY_J, 1
|
21
|
+
$uinput.send EV_KEY, KEY_J, 0
|
22
|
+
|
23
|
+
sleep 0.5
|
24
|
+
$uinput.destroy
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
main
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# -*- coding:utf-8; mode:ruby; -*-
|
2
|
+
|
3
|
+
USAGE = <<__USAGE
|
4
|
+
usage: #{$0} devent_device
|
5
|
+
create virtual device with uinput and grab the argument event device
|
6
|
+
|
7
|
+
example: #{$0} /dev/input/event2
|
8
|
+
|
9
|
+
# if key inputs is mismatch of the displayed chars,
|
10
|
+
# you should check keyboad layouts of both system default and
|
11
|
+
# your xsession settings.
|
12
|
+
__USAGE
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "revdev"
|
16
|
+
require "ruinput"
|
17
|
+
|
18
|
+
def main
|
19
|
+
|
20
|
+
if ARGV.length != 1
|
21
|
+
STDERR.puts USAGE
|
22
|
+
exit false
|
23
|
+
end
|
24
|
+
|
25
|
+
include Revdev
|
26
|
+
$evdev = EventDevice.new ARGV.shift
|
27
|
+
$uinput = Ruinput::UinputDevice.new "/dev/uinput"
|
28
|
+
|
29
|
+
destroy = lambda do
|
30
|
+
begin
|
31
|
+
$evdev.ungrab
|
32
|
+
puts "ungrab"
|
33
|
+
rescue => e
|
34
|
+
puts e.backtrace.join "\n\t"
|
35
|
+
end
|
36
|
+
begin
|
37
|
+
$uinput.destroy
|
38
|
+
puts "destroy"
|
39
|
+
rescue => e
|
40
|
+
puts e.backtrace.join "\n\t"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
trap :INT, &destroy
|
45
|
+
trap :TERM, &destroy
|
46
|
+
|
47
|
+
begin
|
48
|
+
$uinput.create "foo", $evdev.device_id
|
49
|
+
$evdev.grab
|
50
|
+
caps = true
|
51
|
+
while true
|
52
|
+
ie = $evdev.read_input_event
|
53
|
+
len = $uinput.write_input_event ie
|
54
|
+
|
55
|
+
# toggle capslock LED
|
56
|
+
if ie.code == KEY_CAPSLOCK and ie.value == 1
|
57
|
+
ie = InputEvent.new nil, EV_LED, LED_CAPSL, caps ? 1 : 0
|
58
|
+
$evdev.write_input_event ie
|
59
|
+
puts "## light #{ie.value}"
|
60
|
+
caps = (not caps)
|
61
|
+
end
|
62
|
+
|
63
|
+
puts "type:#{ie.hr_type}(#{ie.type})\tcode:#{ie.hr_code}(#{ie.code})\tvalue:#{ie.value} (#{len})"
|
64
|
+
end
|
65
|
+
rescue => e
|
66
|
+
puts e.backtrace.join "\n\t"
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
main
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# -*- coding:utf-8; mode:ruby; -*-
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'revdev'
|
6
|
+
require 'ruinput'
|
7
|
+
|
8
|
+
class UinputDeviceTest < Test::Unit::TestCase
|
9
|
+
include Ruinput
|
10
|
+
|
11
|
+
FILE = "/tmp/foo"
|
12
|
+
FileUtils.touch FILE
|
13
|
+
|
14
|
+
def test_init
|
15
|
+
UinputDevice.new FILE
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_create
|
19
|
+
$file = File.new FILE
|
20
|
+
$ioctls = []
|
21
|
+
def $file.ioctl cmd, val=nil
|
22
|
+
$ioctls << cmd
|
23
|
+
end
|
24
|
+
$syswrites = ""
|
25
|
+
def $file.syswrite arg
|
26
|
+
$syswrites << arg
|
27
|
+
end
|
28
|
+
|
29
|
+
ud = UinputDevice.new $file
|
30
|
+
assert_nothing_raised do
|
31
|
+
ud.create
|
32
|
+
end
|
33
|
+
assert_equal UI_DEV_CREATE, $ioctls.last
|
34
|
+
assert 0 < $syswrites.length
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_create
|
38
|
+
$file = File.new FILE
|
39
|
+
$ioctls = []
|
40
|
+
def $file.ioctl cmd, val=nil
|
41
|
+
$ioctls << cmd
|
42
|
+
end
|
43
|
+
def $file.syswrite arg
|
44
|
+
end
|
45
|
+
|
46
|
+
ud = UinputDevice.new $file
|
47
|
+
assert_nothing_raised do
|
48
|
+
ud.create
|
49
|
+
ud.destroy
|
50
|
+
end
|
51
|
+
assert_equal UI_DEV_DESTROY, $ioctls.last
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.2
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Keiichiro Ui
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-04-
|
18
|
+
date: 2012-04-21 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: revdev
|
@@ -79,10 +79,14 @@ files:
|
|
79
79
|
- ext/ruinput/extconf.rb
|
80
80
|
- ext/ruinput/ruinput.c
|
81
81
|
- lib/ruinput.rb
|
82
|
+
- lib/ruinput/uinput_device.rb
|
82
83
|
- lib/ruinput/uinput_user_dev.rb
|
83
84
|
- lib/ruinput/version.rb
|
84
85
|
- ruinput.gemspec
|
86
|
+
- sample/input_j
|
87
|
+
- sample/through_uinput
|
85
88
|
- test/test_ruinput.rb
|
89
|
+
- test/test_uinput_device.rb
|
86
90
|
- test/test_uinput_user_dev.rb
|
87
91
|
homepage: https://rubygems.org/gems/ruinput
|
88
92
|
licenses: []
|
@@ -119,4 +123,5 @@ specification_version: 3
|
|
119
123
|
summary: ruinput is a ruby binding for uinput.h.
|
120
124
|
test_files:
|
121
125
|
- test/test_ruinput.rb
|
126
|
+
- test/test_uinput_device.rb
|
122
127
|
- test/test_uinput_user_dev.rb
|