fusuma-plugin-sendkey 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cdbd450a3576a7faf3c331d4a79aa78e9d3d806ac91e884adf83828d479af826
4
- data.tar.gz: faf882164080733d4c019590dff0052d4dcbb42780870fb616e3714ef29cfc19
3
+ metadata.gz: 4cf085cf00f878f5262bd4ef48d3e8ab49354bea02823c85c11e6edea79e9854
4
+ data.tar.gz: 718effb43a443bbb46852c851bf4716688a3e5bab413fa8a13ddda0f1cca1058
5
5
  SHA512:
6
- metadata.gz: f78dde78d718f38b385f96301eaa23923d3225581473e4e523f93622142a33a64c24b228549c6b906a12917d7824b5162ed868e681c50e4fd7ca4143d6aea2d3
7
- data.tar.gz: a3e6dcf360a8303faa9bbf89b7e3122571231fa614ef965189fd0a4b62de05a90fd6d826e74c2acc94aaed3de8d151c578d3eaee31bece54144f131492e7634c
6
+ metadata.gz: ea348132422cb7bab174e93eccabcd6df2ab358e5d282b619bd380e1a50452bc9c17021e046881cb256f9cb7ea3b30b2c752ba60f7e9be8b5bbe263ed9af7f04
7
+ data.tar.gz: 2a0e4eed20e056eff6708a7ce67c7015fd16ab85626c4729321328baaf7d6e27b25cc6509263a5c98630d37b41bb8a7a2d3c4a2e47f6225cfdf1bd2982030b6b
@@ -21,6 +21,8 @@ Gem::Specification.new do |spec|
21
21
  spec.bindir = 'exe'
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
+ spec.required_ruby_version = '>= 2.5.1' # https://packages.ubuntu.com/search?keywords=ruby&searchon=names&exact=1&suite=all&section=main
25
+ # support bionic (18.04LTS) 2.5.1
24
26
 
25
27
  spec.add_dependency 'fusuma', '~> 2.0.0'
26
28
  spec.add_dependency 'revdev'
@@ -34,10 +34,7 @@ module Fusuma
34
34
  # @param event [Event]
35
35
  # @return [nil]
36
36
  def _execute(event)
37
- keyboard.type(
38
- param: search_param(event),
39
- keep: search_keypress(event)
40
- )
37
+ keyboard.type(param: search_param(event))
41
38
  end
42
39
 
43
40
  # check executable
@@ -62,15 +59,6 @@ module Fusuma
62
59
  index = Config::Index.new([*event.record.index.keys, :sendkey])
63
60
  Config.search(index)
64
61
  end
65
-
66
- # @param event [Event]
67
- # @return [String]
68
- def search_keypress(event)
69
- keys = event.record.index.keys
70
- keypress_index = keys.find_index { |k| k.symbol == :keypress }
71
- code = keypress_index && keys[keypress_index + 1].symbol
72
- code.to_s
73
- end
74
62
  end
75
63
  end
76
64
  end
@@ -10,9 +10,25 @@ module Fusuma
10
10
  module Sendkey
11
11
  # Emulate Keyboard
12
12
  class Keyboard
13
+ MODIFIER_KEY_CODES = %w[
14
+ KEY_CAPSLOCK
15
+ KEY_LEFTALT
16
+ KEY_LEFTCTRL
17
+ KEY_LEFTMETA
18
+ KEY_LEFTSHIFT
19
+ KEY_RIGHTALT
20
+ KEY_RIGHTCTRL
21
+ KEY_RIGHTSHIFT
22
+ KEY_RIGHTMETA
23
+ ].freeze
24
+
25
+ def self.find_device(name_pattern:)
26
+ Fusuma::Device.all.find { |d| d.name.match(/#{name_pattern}/) }
27
+ end
28
+
13
29
  def initialize(name_pattern: nil)
14
30
  name_pattern ||= 'keyboard|Keyboard|KEYBOARD'
15
- device = find_device(name_pattern: name_pattern)
31
+ device = Keyboard.find_device(name_pattern: name_pattern)
16
32
 
17
33
  if device.nil?
18
34
  warn "sendkey: Keyboard: /#{name_pattern}/ is not found"
@@ -26,16 +42,14 @@ module Fusuma
26
42
 
27
43
  # @param param [String]
28
44
  # @param keep [String]
29
- def type(param:, keep: '')
45
+ def type(param:)
30
46
  return unless param.is_a?(String)
31
47
 
32
- keep_keycodes = split_param(keep)
33
- keycodes = split_param(param) - keep_keycodes
34
-
35
- clear_modifiers(keep_keycodes)
36
- keycodes.each { |keycode| key_event(keycode: keycode, press: true) }
48
+ param_keycodes = split_param(param)
49
+ clear_modifiers(MODIFIER_KEY_CODES - param_keycodes)
50
+ param_keycodes.each { |keycode| key_event(keycode: keycode, press: true) }
37
51
  key_sync(press: true)
38
- keycodes.reverse.map { |keycode| key_event(keycode: keycode, press: false) }
52
+ param_keycodes.reverse.each { |keycode| key_event(keycode: keycode, press: false) }
39
53
  key_sync(press: false)
40
54
  end
41
55
 
@@ -101,19 +115,13 @@ module Fusuma
101
115
  Object.const_get "LinuxInput::#{keycode}"
102
116
  end
103
117
 
118
+ # @param [Array<String>] keycodes to be released
104
119
  def clear_modifiers(keycodes)
105
- modifiers = %w[ CAPSLOCK LEFTALT LEFTCTRL LEFTMETA
106
- LEFTSHIFT RIGHTALT RIGHTCTRL RIGHTSHIFT ]
107
- .map { |code| key_prefix(code) }
108
- (modifiers - keycodes).each { |code| key_event(keycode: code, press: false) }
120
+ keycodes.each { |code| key_event(keycode: code, press: false) }
109
121
  end
110
122
 
111
123
  private
112
124
 
113
- def find_device(name_pattern:)
114
- Fusuma::Device.all.find { |d| d.name.match(/#{name_pattern}/) }
115
- end
116
-
117
125
  def split_param(param)
118
126
  param.split('+').map { |code| key_prefix(code) }
119
127
  end
@@ -3,7 +3,7 @@
3
3
  module Fusuma
4
4
  module Plugin
5
5
  module Sendkey
6
- VERSION = '0.6.1'
6
+ VERSION = '0.6.2'
7
7
  end
8
8
  end
9
9
  end
@@ -18,9 +18,6 @@ module Fusuma
18
18
  1:
19
19
  direction:
20
20
  sendkey: KEY_CODE
21
- keypress:
22
- LEFTSHIFT:
23
- sendkey: KEY_CODE_WITH_KEYPRESS
24
21
 
25
22
  plugin:
26
23
  executors:
@@ -37,9 +34,9 @@ module Fusuma
37
34
  index = Config::Index.new([:dummy, 1, :direction])
38
35
  record = Events::Records::IndexRecord.new(index: index)
39
36
  @event = Events::Event.new(tag: 'dummy_detector', record: record)
40
- @executor = SendkeyExecutor.new
37
+ @executor = described_class.new
41
38
 
42
- @keyboard = double(Sendkey::Keyboard)
39
+ @keyboard = instance_double(Sendkey::Keyboard)
43
40
 
44
41
  allow(@executor).to receive(:keyboard).and_return @keyboard
45
42
  end
@@ -49,6 +46,7 @@ module Fusuma
49
46
  allow(Process).to receive(:daemon).with(true)
50
47
  allow(Process).to receive(:detach).with(anything)
51
48
  end
49
+
52
50
  it 'fork' do
53
51
  expect(@executor).to receive(:fork).and_yield do |block_context|
54
52
  expect(block_context).to receive(:_execute).with(@event)
@@ -59,27 +57,10 @@ module Fusuma
59
57
  end
60
58
 
61
59
  describe '#_execute' do
62
- after do
63
- @executor._execute(@event)
64
- end
65
60
  it 'send KEY_CODE message to keybard' do
66
- expect(@executor).to receive(:search_param).with(@event).and_return('KEY_CODE')
67
- expect(@executor).to receive(:search_keypress).with(@event).and_return(nil)
68
- expect(@keyboard).to receive(:type).with(param: 'KEY_CODE', keep: nil)
69
- end
70
-
71
- context 'with keypress' do
72
- before do
73
- index_with_keypress = Config::Index.new(
74
- [:dummy, 1, :direction, :keypress, :LEFTSHIFT]
75
- )
76
- record = Events::Records::IndexRecord.new(index: index_with_keypress)
77
- @event = Events::Event.new(tag: 'dummy_detector', record: record)
78
- end
79
- it 'send KEY_CODE_WITH_KEYPRESS message to keybard' do
80
- expect(@keyboard).to receive(:type)
81
- .with(param: 'KEY_CODE_WITH_KEYPRESS', keep: 'LEFTSHIFT')
82
- end
61
+ allow(@executor).to receive(:search_param).with(@event).and_return('KEY_CODE')
62
+ expect(@keyboard).to receive(:type).with(param: 'KEY_CODE')
63
+ @executor._execute(@event)
83
64
  end
84
65
  end
85
66
 
@@ -92,15 +73,17 @@ module Fusuma
92
73
  allow(@keyboard).to receive(:valid?).with(param: 'INVALID_CODE')
93
74
  .and_return false
94
75
  end
76
+
95
77
  context 'when given valid event tagged as xxxx_detector' do
96
- it { expect(@executor.executable?(@event)).to be_truthy }
78
+ it { expect(@executor).to be_executable(@event) }
97
79
  end
98
80
 
99
81
  context 'when given INVALID event tagged as invalid_tag' do
100
82
  before do
101
83
  @event.tag = 'invalid_tag'
102
84
  end
103
- it { expect(@executor.executable?(@event)).to be_falsey }
85
+
86
+ it { expect(@executor).not_to be_executable(@event) }
104
87
  end
105
88
 
106
89
  context "when sendkey: 'MODIFIER_CODE+KEY_CODE'" do
@@ -121,8 +104,8 @@ module Fusuma
121
104
  Config.custom_path = nil
122
105
  end
123
106
 
124
- it 'should return true' do
125
- expect(@executor.executable?(@event)).to be_truthy
107
+ it 'returns true' do
108
+ expect(@executor).to be_executable(@event)
126
109
  end
127
110
  end
128
111
 
@@ -143,6 +126,10 @@ module Fusuma
143
126
 
144
127
  Config.custom_path = nil
145
128
  end
129
+
130
+ it 'returns true' do
131
+ expect(@executor).not_to be_executable(@event)
132
+ end
146
133
  end
147
134
  end
148
135
  end
@@ -9,62 +9,62 @@ module Fusuma
9
9
  module Sendkey
10
10
  RSpec.describe Keyboard do
11
11
  describe '#new' do
12
- context 'keyboard is found' do
12
+ context 'when keyboard is found' do
13
13
  before do
14
14
  dummy_keyboard = Fusuma::Device.new(name: 'dummy keyboard')
15
- allow_any_instance_of(Sendkey::Keyboard)
15
+ allow(described_class)
16
16
  .to receive(:find_device)
17
17
  .and_return(dummy_keyboard)
18
18
  allow(Sendkey::Device).to receive(:new).and_return('dummy')
19
19
  end
20
20
 
21
- it 'should not raise error' do
22
- expect { Keyboard.new }.not_to raise_error
21
+ it 'does not raise error' do
22
+ expect { described_class.new }.not_to raise_error
23
23
  end
24
24
  end
25
25
 
26
- context 'keyboard is not found' do
26
+ context 'when keyboard is not found' do
27
27
  before do
28
- allow_any_instance_of(Sendkey::Keyboard)
28
+ allow(described_class)
29
29
  .to receive(:find_device)
30
30
  .and_return(nil)
31
31
  end
32
32
 
33
- it 'should not raise error' do
34
- expect { Keyboard.new }.to raise_error SystemExit
33
+ it 'does not raise error' do
34
+ expect { described_class.new }.to raise_error SystemExit
35
35
  end
36
36
  end
37
37
 
38
- context 'detected device name is Keyboard (Capitarized)' do
38
+ context 'when detected device name is Keyboard (Capitarized)' do
39
39
  before do
40
40
  other_device = Fusuma::Device.new(name: 'Keyboard', id: 'dummy')
41
41
 
42
- allow_any_instance_of(Sendkey::Keyboard)
42
+ allow(described_class)
43
43
  .to receive(:find_device)
44
44
  .and_return(other_device)
45
45
  allow(Sendkey::Device).to receive(:new).and_return('dummy')
46
46
  end
47
47
 
48
- it 'should not raise error' do
49
- expect { Keyboard.new }.not_to raise_error
48
+ it 'does not raise error' do
49
+ expect { described_class.new }.not_to raise_error
50
50
  end
51
51
  end
52
52
 
53
- context 'detected device name is KEYBOARD (Upper case)' do
53
+ context 'when detected device name is KEYBOARD (Upper case)' do
54
54
  before do
55
55
  other_device = Fusuma::Device.new(name: 'KEYBOARD', id: 'dummy')
56
- allow_any_instance_of(Sendkey::Keyboard)
56
+ allow(described_class)
57
57
  .to receive(:find_device)
58
58
  .and_return(other_device)
59
59
  allow(Sendkey::Device).to receive(:new).and_return('dummy')
60
60
  end
61
61
 
62
- it 'should not raise error' do
63
- expect { Keyboard.new }.not_to raise_error
62
+ it 'does not raise error' do
63
+ expect { described_class.new }.not_to raise_error
64
64
  end
65
65
  end
66
66
 
67
- context 'given name pattern' do
67
+ context 'with given name pattern' do
68
68
  before do
69
69
  specified_device = Fusuma::Device.new(
70
70
  name: 'Awesome KEY/BOARD input device',
@@ -74,18 +74,19 @@ module Fusuma
74
74
  allow(Sendkey::Device).to receive(:new).and_return('dummy')
75
75
  end
76
76
 
77
- it 'should not raise error' do
78
- expect { Keyboard.new(name_pattern: 'Awesome KEY/BOARD') }.not_to raise_error
77
+ it 'does not raise error' do
78
+ expect { described_class.new(name_pattern: 'Awesome KEY/BOARD') }.not_to raise_error
79
79
  end
80
80
  end
81
81
 
82
- context 'not given name pattern (use default)' do
83
- subject { -> { Keyboard.new(name_pattern: nil) } }
82
+ context 'when name pattern (use default) is not given' do
83
+ subject { -> { described_class.new(name_pattern: nil) } }
84
+
84
85
  before do
85
86
  allow(Sendkey::Device).to receive(:new).and_return('dummy')
86
87
  end
87
88
 
88
- context 'exist device named keyboard' do
89
+ context 'when exist device named keyboard(lower-case)' do
89
90
  before do
90
91
  specified_device = Fusuma::Device.new(
91
92
  name: 'keyboard',
@@ -97,7 +98,7 @@ module Fusuma
97
98
  it { is_expected.not_to raise_error }
98
99
  end
99
100
 
100
- context 'exist device named Keyboard' do
101
+ context 'when exist device named Keyboard(Capital-case)' do
101
102
  before do
102
103
  specified_device = Fusuma::Device.new(
103
104
  name: 'Keyboard',
@@ -109,7 +110,7 @@ module Fusuma
109
110
  it { is_expected.not_to raise_error }
110
111
  end
111
112
 
112
- context 'exist device named KEYBOARD' do
113
+ context 'when exist device named KEYBOARD(UPPER case)' do
113
114
  before do
114
115
  specified_device = Fusuma::Device.new(
115
116
  name: 'KEYBOARD',
@@ -121,7 +122,7 @@ module Fusuma
121
122
  it { is_expected.not_to raise_error }
122
123
  end
123
124
 
124
- context 'exist no device named keyboard|Keyboard|KEYBOARD' do
125
+ context 'when exist no device named keyboard|Keyboard|KEYBOARD' do
125
126
  before do
126
127
  specified_device = Fusuma::Device.new(
127
128
  name: 'KEY-BOARD',
@@ -137,20 +138,20 @@ module Fusuma
137
138
 
138
139
  describe '#type' do
139
140
  before do
140
- allow_any_instance_of(Sendkey::Keyboard)
141
+ allow(described_class)
141
142
  .to receive(:find_device)
142
143
  .and_return(Fusuma::Device.new(name: 'dummy keyboard'))
143
144
 
144
- @device = double(Sendkey::Device)
145
+ @device = instance_double(Sendkey::Device)
145
146
  allow(@device).to receive(:write_event).with(anything)
146
147
  # allow(@device).to receive(:valid?).with(param: 'KEY_A')
147
148
 
148
149
  allow(Sendkey::Device).to receive(:new).and_return(@device)
149
150
 
150
- @keyboard = Keyboard.new
151
+ @keyboard = described_class.new
151
152
  end
152
153
 
153
- it 'should press key KEY_A and release KEY_A' do
154
+ it 'presses key KEY_A and release KEY_A' do
154
155
  expect(@keyboard).to receive(:clear_modifiers).ordered
155
156
  expect(@keyboard).to receive(:key_event).with(keycode: 'KEY_A', press: true).ordered
156
157
  expect(@keyboard).to receive(:key_event).with(keycode: 'KEY_A', press: false).ordered
@@ -162,7 +163,12 @@ module Fusuma
162
163
  @keys = 'LEFTSHIFT+A'
163
164
  end
164
165
 
165
- it 'should type AB' do
166
+ it 'clear all modifier keys except parameter of sendkey' do
167
+ expect(@keyboard).to receive(:clear_modifiers).with(Keyboard::MODIFIER_KEY_CODES - ['KEY_LEFTSHIFT']).ordered
168
+ @keyboard.type(param: @keys)
169
+ end
170
+
171
+ it 'types (Shift)A' do
166
172
  expect(@keyboard).to receive(:clear_modifiers).ordered
167
173
  expect(@keyboard).to receive(:key_event).with(keycode: 'KEY_LEFTSHIFT', press: true).ordered
168
174
  expect(@keyboard).to receive(:key_event).with(keycode: 'KEY_A', press: true).ordered
@@ -171,12 +177,13 @@ module Fusuma
171
177
  @keyboard.type(param: @keys)
172
178
  end
173
179
  end
180
+
174
181
  context 'with multiple keys' do
175
182
  before do
176
183
  @keys = 'A+B'
177
184
  end
178
185
 
179
- it 'should type AB' do
186
+ it 'types AB' do
180
187
  expect(@keyboard).to receive(:clear_modifiers).ordered
181
188
  expect(@keyboard).to receive(:key_event).with(keycode: 'KEY_A', press: true).ordered
182
189
  expect(@keyboard).to receive(:key_event).with(keycode: 'KEY_B', press: true).ordered
@@ -185,18 +192,6 @@ module Fusuma
185
192
  @keyboard.type(param: @keys)
186
193
  end
187
194
  end
188
- context 'with keypress' do
189
- before do
190
- @keys = 'LEFTSHIFT+A'
191
- @keypress_keys = 'LEFTSHIFT'
192
- end
193
- it 'should type A (without LEFTSHIFT key pressing by user)' do
194
- expect(@keyboard).to receive(:clear_modifiers).ordered
195
- expect(@keyboard).to receive(:key_event).with(keycode: 'KEY_A', press: true).ordered
196
- expect(@keyboard).to receive(:key_event).with(keycode: 'KEY_A', press: false).ordered
197
- @keyboard.type(param: @keys, keep: @keypress_keys)
198
- end
199
- end
200
195
  end
201
196
  end
202
197
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'tempfile'
4
- require 'fusuma/config.rb'
4
+ require 'fusuma/config'
5
5
 
6
6
  module Fusuma
7
7
  module ConfigHelper
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'bundler/setup'
4
- require 'helpers/config_helper.rb'
4
+ require 'helpers/config_helper'
5
5
 
6
6
  RSpec.configure do |config|
7
7
  # Enable flags like --only-failures and --next-failure
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fusuma-plugin-sendkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - iberianpig
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-13 00:00:00.000000000 Z
11
+ date: 2021-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fusuma
@@ -74,7 +74,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - ">="
76
76
  - !ruby/object:Gem::Version
77
- version: '0'
77
+ version: 2.5.1
78
78
  required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="