procon_bypass_man 0.1.2 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,6 @@ module ProconBypassMan
10
10
 
11
11
  # アクティブなバケットは1つだけ
12
12
  def record(event_name)
13
- return unless $is_stable
14
13
  key = Time.now.strftime("%S").to_i
15
14
  if table[key].nil?
16
15
  self.previous_table = table.values.first
@@ -58,13 +57,6 @@ module ProconBypassMan
58
57
  next
59
58
  end
60
59
 
61
- s_to_p = list.detect { |x| x.label == "switch -> procon" }
62
- previous_table = s_to_p&.previous_table.dup
63
- if previous_table && previous_table.dig(:eagain_wait_readable_on_read) && previous_table.dig(:eagain_wait_readable_on_read) > 300
64
- # ProconBypassMan.logger.debug { "接続の確立ができません" }
65
- # Process.kill("USR1", Process.ppid)
66
- end
67
-
68
60
  line = list.map { |counter|
69
61
  "#{counter.label}(#{counter.formated_previous_table})"
70
62
  }.join(", ")
@@ -1,14 +1,16 @@
1
1
  class ProconBypassMan::Procon
2
+ require "procon_bypass_man/procon/data"
2
3
  require "procon_bypass_man/procon/mode_registry"
3
4
  require "procon_bypass_man/procon/macro_registry"
4
5
  require "procon_bypass_man/procon/layer_changeable"
5
6
  require "procon_bypass_man/procon/button_collection"
6
7
  require "procon_bypass_man/procon/pressed_button_helper"
7
8
  require "procon_bypass_man/procon/user_operation"
9
+ require "procon_bypass_man/procon/flip_cache"
8
10
 
9
11
  attr_accessor :user_operation
10
12
 
11
- def self.reset_cvar!
13
+ def self.reset!
12
14
  @@status = {
13
15
  buttons: {},
14
16
  current_layer_key: :up,
@@ -16,7 +18,6 @@ class ProconBypassMan::Procon
16
18
  ongoing_mode: ModeRegistry.load(:manual),
17
19
  }
18
20
  end
19
- def self.reset!; reset_cvar!; end
20
21
  reset!
21
22
 
22
23
  def initialize(binary)
@@ -51,15 +52,21 @@ class ProconBypassMan::Procon
51
52
  when :manual
52
53
  @@status[:ongoing_mode] = ModeRegistry.load(:manual)
53
54
  current_layer.flip_buttons.each do |button, options|
54
- unless options[:if_pressed]
55
- status[button] = !status[button]
55
+ if !options[:if_pressed]
56
+ FlipCache.fetch(key: button, expires_in: options[:flip_interval]) do
57
+ status[button] = !status[button]
58
+ end
56
59
  next
57
60
  end
58
61
 
59
62
  if options[:if_pressed] && options[:if_pressed].all? { |b| user_operation.pressed_button?(b) }
60
- status[button] = !status[button]
63
+ FlipCache.fetch(key: button, expires_in: options[:flip_interval]) do
64
+ status[button] = !status[button]
65
+ end
61
66
  else
62
- status[button] = false
67
+ FlipCache.fetch(key: button, expires_in: options[:flip_interval]) do
68
+ status[button] = false
69
+ end
63
70
  end
64
71
  end
65
72
  else
@@ -99,18 +106,20 @@ class ProconBypassMan::Procon
99
106
  if !status[button]
100
107
  user_operation.unpress_button(button)
101
108
  end
102
- if options[:force_neutral] && user_operation.pressed_button?(options[:force_neutral])
103
- button = options[:force_neutral]
104
- user_operation.unpress_button(button)
109
+
110
+ options[:force_neutral]&.each do |force_neutral_button|
111
+ user_operation.pressed_button?(force_neutral_button) && user_operation.unpress_button(force_neutral_button)
105
112
  end
106
113
  end
107
114
  end
108
115
 
109
- current_layer.remaps.each do |from_button, to_button|
116
+ current_layer.remaps.each do |from_button, to_buttons|
110
117
  if user_operation.pressed_button?(from_button)
111
118
  user_operation.unpress_button(from_button)
112
119
  # TODO 2重でpressしないようにしたい
113
- user_operation.press_button(to_button) unless user_operation.pressed_button?(to_button)
120
+ to_buttons[:to].each do |to_button|
121
+ user_operation.press_button(to_button) unless user_operation.pressed_button?(to_button)
122
+ end
114
123
  end
115
124
  end
116
125
 
@@ -24,7 +24,7 @@ class ProconBypassMan::Procon::ButtonCollection
24
24
  3 => [:zr, :r, :sr, :sl, :a, :b, :x, :y],
25
25
  4 => [:grip, :_undefined_key, :cap, :home, :thumbl, :thumbr, :plus, :minus],
26
26
  5 => [:zl, :l, :sl, :sr, :left, :right, :up, :down],
27
- }
27
+ }.freeze
28
28
 
29
29
  BUTTONS_MAP = BYTES_MAP.reduce({}) { |acc, value|
30
30
  next acc if value[1].nil?
@@ -32,7 +32,8 @@ class ProconBypassMan::Procon::ButtonCollection
32
32
  acc[button] = { byte_position: value[0], bit_position: index }
33
33
  end
34
34
  acc
35
- }
35
+ }.freeze
36
+ BUTTONS = ProconBypassMan::Procon::ButtonCollection::BUTTONS_MAP.keys.freeze
36
37
 
37
38
  def self.load(button_key)
38
39
  Button.new(button_key)
@@ -0,0 +1,22 @@
1
+ class ProconBypassMan::Procon
2
+ class FlipCache
3
+ def self.fetch(key: , expires_in: , &block)
4
+ if expires_in.nil?
5
+ block.call
6
+ else
7
+ @@previous_flips_at_table[key] ||= Time.now
8
+ if @@previous_flips_at_table[key] < Time.now
9
+ @@previous_flips_at_table[key] = Time.now + expires_in
10
+ block.call
11
+ end
12
+ end
13
+ end
14
+
15
+ # for testing
16
+ def self.reset!
17
+ @@previous_flips_at_table = {}
18
+ end
19
+
20
+ reset!
21
+ end
22
+ end
@@ -13,7 +13,7 @@ module ProconBypassMan::Procon::PushedButtonHelper
13
13
  @@compiled = false
14
14
  def compile_if_not_compile_yet!
15
15
  unless @@compiled
16
- ::ProconBypassMan::Procon::ButtonCollection::BUTTONS_MAP.each do |button, value|
16
+ ::ProconBypassMan::Procon::ButtonCollection::BUTTONS_MAP.each do |button, _value|
17
17
  define_method "pressed_#{button}?" do
18
18
  pressed_button?(button)
19
19
  end
@@ -2,19 +2,15 @@ require_relative "io_monitor"
2
2
 
3
3
  class ProconBypassMan::Runner
4
4
  class InterruptForRestart < StandardError; end
5
- class InterruptForCouldNotConnect < StandardError; end
6
-
7
- def initialize(gadget: , procon: )
8
- @gadget = gadget
9
- @procon = procon
10
5
 
6
+ def initialize
11
7
  $will_interval_0_0_0_5 = 0
12
8
  $will_interval_1_6 = 0
13
9
  end
14
10
 
15
11
  def run
16
12
  first_negotiation
17
- $is_stable = false
13
+ print_booted_message
18
14
 
19
15
  self_read, self_write = IO.pipe
20
16
  %w(TERM INT USR1 USR2).each do |sig|
@@ -23,27 +19,19 @@ class ProconBypassMan::Runner
23
19
  self_write.puts(sig)
24
20
  end
25
21
  rescue ArgumentError
26
- ProconBypassMan.logger.info("Signal #{sig} not supported")
22
+ ProconBypassMan.logger.error("Signal #{sig} not supported")
27
23
  end
28
24
  end
29
25
 
30
- FileUtils.mkdir_p "tmp"
31
- File.write "tmp/pid", $$
32
-
33
26
  loop do
34
27
  $will_terminate_token = false
35
28
  main_loop_pid = fork { main_loop }
36
29
 
37
30
  begin
38
- while readable_io = IO.select([self_read])
31
+ while(readable_io = IO.select([self_read]))
39
32
  signal = readable_io.first[0].gets.strip
40
33
  handle_signal(signal)
41
34
  end
42
- rescue InterruptForCouldNotConnect
43
- $will_terminate_token = true
44
- Process.kill("TERM", main_loop_pid)
45
- Process.wait
46
- raise ProconBypassMan::CouldNotConnectDeviceError
47
35
  rescue InterruptForRestart
48
36
  $will_terminate_token = true
49
37
  Process.kill("TERM", main_loop_pid)
@@ -62,6 +50,8 @@ class ProconBypassMan::Runner
62
50
  Process.wait
63
51
  @gadget&.close
64
52
  @procon&.close
53
+ FileUtils.rm_rf(ProconBypassMan.pid_path)
54
+ FileUtils.rm_rf(ProconBypassMan.digest_path)
65
55
  exit 1
66
56
  end
67
57
  end
@@ -72,10 +62,9 @@ class ProconBypassMan::Runner
72
62
  def main_loop
73
63
  # TODO 接続確立完了をswitchを読み取るようにして、この暫定で接続完了sleepを消す
74
64
  Thread.new do
75
- sleep(10)
65
+ sleep(5)
76
66
  $will_interval_0_0_0_5 = 0.005
77
67
  $will_interval_1_6 = 1.6
78
- $is_stable = true
79
68
  end
80
69
 
81
70
  ProconBypassMan::IOMonitor.start!
@@ -107,8 +96,11 @@ class ProconBypassMan::Runner
107
96
  loop do
108
97
  break if $will_terminate_token
109
98
  bypass.send_procon_to_gadget!
99
+ rescue EOFError => e
100
+ ProconBypassMan.logger.error "Proconと通信ができませんでした.終了処理を開始します"
101
+ Process.kill "TERM", Process.ppid
110
102
  rescue Errno::EIO, Errno::ENODEV, Errno::EPROTO, IOError => e
111
- ProconBypassMan.logger.error "Proconが切断されました.終了処理を開始します"
103
+ ProconBypassMan.logger.error "Proconが切断されました。終了処理を開始します"
112
104
  Process.kill "TERM", Process.ppid
113
105
  end
114
106
  ProconBypassMan.logger.info "Thread2を終了します"
@@ -128,7 +120,7 @@ class ProconBypassMan::Runner
128
120
 
129
121
  ProconBypassMan.logger.info "子プロセスでgraceful shutdownの準備ができました"
130
122
  begin
131
- while readable_io = IO.select([self_read])
123
+ while(readable_io = IO.select([self_read]))
132
124
  signal = readable_io.first[0].gets.strip
133
125
  handle_signal(signal)
134
126
  end
@@ -142,30 +134,39 @@ class ProconBypassMan::Runner
142
134
  end
143
135
 
144
136
  def first_negotiation
145
- loop do
146
- begin
147
- input = @gadget.read_nonblock(128)
148
- ProconBypassMan.logger.debug { ">>> #{input.unpack("H*")}" }
149
- @procon.write_nonblock(input)
150
- if input[0] == "\x80".b && input[1] == "\x01".b
151
- ProconBypassMan.logger.info("first negotiation is over")
152
- break
153
- end
154
- break if $will_terminate_token
155
- rescue IO::EAGAINWaitReadable
156
- end
157
- end
137
+ return if $will_terminate_token
138
+
139
+ @gadget, @procon = ProconBypassMan::DeviceConnector.connect
140
+ rescue ProconBypassMan::Timer::Timeout
141
+ ::ProconBypassMan.logger.error "デバイスとの通信でタイムアウトが起きて接続ができませんでした。"
142
+ @gadget&.close
143
+ @procon&.close
144
+ raise ::ProconBypassMan::EternalConnectionError
158
145
  end
159
146
 
160
147
  def handle_signal(sig)
161
148
  ProconBypassMan.logger.info "#{$$}で#{sig}を受け取りました"
162
149
  case sig
163
- when 'USR1'
164
- raise InterruptForCouldNotConnect
165
150
  when 'USR2'
166
151
  raise InterruptForRestart
167
152
  when 'INT', 'TERM'
168
153
  raise Interrupt
169
154
  end
170
155
  end
156
+
157
+ # @return [void]
158
+ def print_booted_message
159
+ booted_message = <<~EOF
160
+ ----
161
+ RUBY_VERSION: #{RUBY_VERSION}
162
+ ProconBypassMan: #{ProconBypassMan::VERSION}
163
+ pid: #{$$}
164
+ root: #{ProconBypassMan.root}
165
+ pid_path: #{ProconBypassMan.pid_path}
166
+ setting_path: #{ProconBypassMan::Configuration.instance.setting_path}
167
+ ----
168
+ EOF
169
+ ProconBypassMan.logger.info(booted_message)
170
+ puts booted_message
171
+ end
171
172
  end
@@ -0,0 +1,14 @@
1
+ module ProconBypassMan
2
+ class Timer
3
+ class Timeout < StandardError; end
4
+
5
+ # 5秒後がタイムアウト
6
+ def initialize(timeout: Time.now + 5)
7
+ @timeout = timeout
8
+ end
9
+
10
+ def throw_if_timeout!
11
+ raise Timeout if @timeout < Time.now
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ProconBypassMan
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.6"
5
5
  end
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["jiikko"]
9
9
  spec.email = ["n905i.1214@gmail.com"]
10
10
 
11
- spec.summary = "rasberrypi's software for procon"
11
+ spec.summary = "extension for Nintendo Switch Pro Controller"
12
12
  spec.description = spec.summary
13
- spec.homepage = "https://github.com/splaspla-hacker/procon_bypass_man"
13
+ spec.homepage = "https://github.com/splaplapla/procon_bypass_man"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
16
 
metadata CHANGED
@@ -1,27 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: procon_bypass_man
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - jiikko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-26 00:00:00.000000000 Z
11
+ date: 2021-08-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: rasberrypi's software for procon
13
+ description: extension for Nintendo Switch Pro Controller
14
14
  email:
15
15
  - n905i.1214@gmail.com
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - ".circleci/config.yml"
20
21
  - ".gitignore"
21
22
  - ".rspec"
23
+ - ".rubocop.yml"
22
24
  - ".ruby-version"
23
25
  - CHANGELOG.md
24
- - CODE_OF_CONDUCT.md
25
26
  - Gemfile
26
27
  - Gemfile.lock
27
28
  - LICENSE.txt
@@ -31,6 +32,7 @@ files:
31
32
  - bin/setup
32
33
  - docs/how_to_connect_procon.md
33
34
  - docs/setup_raspi.md
35
+ - examples/pbm.service
34
36
  - examples/practical/app.rb
35
37
  - examples/practical/setting.yml
36
38
  - examples/simple.rb
@@ -40,26 +42,28 @@ files:
40
42
  - lib/procon_bypass_man/configuration/layer.rb
41
43
  - lib/procon_bypass_man/configuration/loader.rb
42
44
  - lib/procon_bypass_man/configuration/validator.rb
43
- - lib/procon_bypass_man/device_registry.rb
45
+ - lib/procon_bypass_man/device_connector.rb
44
46
  - lib/procon_bypass_man/io_monitor.rb
45
47
  - lib/procon_bypass_man/processor.rb
46
48
  - lib/procon_bypass_man/procon.rb
47
49
  - lib/procon_bypass_man/procon/button_collection.rb
48
50
  - lib/procon_bypass_man/procon/data.rb
51
+ - lib/procon_bypass_man/procon/flip_cache.rb
49
52
  - lib/procon_bypass_man/procon/layer_changeable.rb
50
53
  - lib/procon_bypass_man/procon/macro_registry.rb
51
54
  - lib/procon_bypass_man/procon/mode_registry.rb
52
55
  - lib/procon_bypass_man/procon/pressed_button_helper.rb
53
56
  - lib/procon_bypass_man/procon/user_operation.rb
54
57
  - lib/procon_bypass_man/runner.rb
58
+ - lib/procon_bypass_man/timer.rb
55
59
  - lib/procon_bypass_man/version.rb
56
60
  - procon_bypass_man.gemspec
57
- homepage: https://github.com/splaspla-hacker/procon_bypass_man
61
+ homepage: https://github.com/splaplapla/procon_bypass_man
58
62
  licenses:
59
63
  - MIT
60
64
  metadata:
61
- homepage_uri: https://github.com/splaspla-hacker/procon_bypass_man
62
- source_code_uri: https://github.com/splaspla-hacker/procon_bypass_man
65
+ homepage_uri: https://github.com/splaplapla/procon_bypass_man
66
+ source_code_uri: https://github.com/splaplapla/procon_bypass_man
63
67
  post_install_message:
64
68
  rdoc_options: []
65
69
  require_paths:
@@ -78,5 +82,5 @@ requirements: []
78
82
  rubygems_version: 3.2.15
79
83
  signing_key:
80
84
  specification_version: 4
81
- summary: rasberrypi's software for procon
85
+ summary: extension for Nintendo Switch Pro Controller
82
86
  test_files: []
data/CODE_OF_CONDUCT.md DELETED
@@ -1,84 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
-
7
- We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
-
9
- ## Our Standards
10
-
11
- Examples of behavior that contributes to a positive environment for our community include:
12
-
13
- * Demonstrating empathy and kindness toward other people
14
- * Being respectful of differing opinions, viewpoints, and experiences
15
- * Giving and gracefully accepting constructive feedback
16
- * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
- * Focusing on what is best not just for us as individuals, but for the overall community
18
-
19
- Examples of unacceptable behavior include:
20
-
21
- * The use of sexualized language or imagery, and sexual attention or
22
- advances of any kind
23
- * Trolling, insulting or derogatory comments, and personal or political attacks
24
- * Public or private harassment
25
- * Publishing others' private information, such as a physical or email
26
- address, without their explicit permission
27
- * Other conduct which could reasonably be considered inappropriate in a
28
- professional setting
29
-
30
- ## Enforcement Responsibilities
31
-
32
- Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
-
34
- Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
-
36
- ## Scope
37
-
38
- This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
-
40
- ## Enforcement
41
-
42
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at n905i.1214@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
-
44
- All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
-
46
- ## Enforcement Guidelines
47
-
48
- Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
-
50
- ### 1. Correction
51
-
52
- **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
-
54
- **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
-
56
- ### 2. Warning
57
-
58
- **Community Impact**: A violation through a single incident or series of actions.
59
-
60
- **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
-
62
- ### 3. Temporary Ban
63
-
64
- **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
-
66
- **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
-
68
- ### 4. Permanent Ban
69
-
70
- **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
-
72
- **Consequence**: A permanent ban from any sort of public interaction within the community.
73
-
74
- ## Attribution
75
-
76
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
- available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
-
79
- Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
-
81
- [homepage]: https://www.contributor-covenant.org
82
-
83
- For answers to common questions about this code of conduct, see the FAQ at
84
- https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.