Class: Fusuma::Plugin::Buffers::GestureBuffer
- Inherits:
-
Buffer
- Object
- Fusuma::Plugin::Base
- Buffer
- Fusuma::Plugin::Buffers::GestureBuffer
- Defined in:
- lib/fusuma/plugin/buffers/gesture_buffer.rb
Overview
manage events and generate command
Defined Under Namespace
Classes: CacheEntry
Constant Summary collapse
- DEFAULT_SOURCE =
"libinput_gesture_parser"- DEFAULT_SECONDS_TO_KEEP =
100
Instance Attribute Summary
Attributes inherited from Buffer
Instance Method Summary collapse
-
#avg_attrs(attr) ⇒ Float
: (Symbol) -> Float.
-
#buffer(event) ⇒ Buffer, FalseClass
: (Fusuma::Plugin::Events::Event) -> Fusuma::Plugin::Buffers::GestureBuffer?.
-
#clear ⇒ Object
: () -> Hash[untyped, untyped].
-
#clear_expired(current_time: Time.now) ⇒ Object
: (?current_time: Time) -> void.
-
#config_param_types ⇒ Object
: () -> Hash[Symbol, Array | Class].
-
#ended? ⇒ Boolean
: () -> bool.
-
#finger ⇒ Object
return [Integer] : () -> Integer.
- #gesture ⇒ String
-
#initialize(*args) ⇒ GestureBuffer
constructor
: (*nil | Array) -> void.
-
#select_by_type(type) ⇒ Object
: (String) -> Fusuma::Plugin::Buffers::GestureBuffer.
-
#select_from_last_begin ⇒ Object
: () -> Fusuma::Plugin::Buffers::GestureBuffer.
-
#sum_attrs(attr) ⇒ Float
: (Symbol) -> Float.
-
#sum_last10_attrs(attr) ⇒ Float
: (Symbol) -> Float.
-
#updating_events ⇒ Object
: () -> Array.
Methods inherited from Buffer
Methods inherited from Fusuma::Plugin::Base
#config_index, #config_params, inherited, plugins, #shutdown
Constructor Details
#initialize(*args) ⇒ GestureBuffer
: (*nil | Array) -> void
15 16 17 18 19 20 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 15 def initialize(*args) super @cache = {} @cache_select_by = {} @cache_sum10 = {} end |
Instance Method Details
#avg_attrs(attr) ⇒ Float
: (Symbol) -> Float
123 124 125 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 123 def avg_attrs(attr) sum_attrs(attr).to_f / updating_events.length end |
#buffer(event) ⇒ Buffer, FalseClass
: (Fusuma::Plugin::Events::Event) -> Fusuma::Plugin::Buffers::GestureBuffer?
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 41 def buffer(event) # TODO: buffering events into buffer plugins # - gesture event buffer # - window event buffer # - other event buffer return if event&.tag != source @events.push(event) self end |
#clear ⇒ Object
: () -> Hash[untyped, untyped]
23 24 25 26 27 28 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 23 def clear super.clear @cache = {} @cache_select_by = {} @cache_sum10 = {} end |
#clear_expired(current_time: Time.now) ⇒ Object
: (?current_time: Time) -> void
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 53 def clear_expired(current_time: Time.now) clear if ended? @seconds_to_keep ||= config_params(:seconds_to_keep) || DEFAULT_SECONDS_TO_KEEP @events.each do |e| break if current_time - e.time < @seconds_to_keep MultiLogger.debug("#{self.class.name}##{__method__}") @events.delete(e) @cache = {} @cache_select_by = {} @cache_sum10 = {} end end |
#config_param_types ⇒ Object
: () -> Hash[Symbol, Array | Class]
31 32 33 34 35 36 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 31 def config_param_types { source: [String], seconds_to_keep: [Float, Integer] } end |
#ended? ⇒ Boolean
: () -> bool
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 70 def ended? return false if empty? case @events.last.record.status when "end", "cancelled" true else false end end |
#finger ⇒ Object
return [Integer] : () -> Integer
129 130 131 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 129 def finger @events.last.record.finger.to_i end |
#gesture ⇒ String
137 138 139 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 137 def gesture @events.last.record.gesture end |
#select_by_type(type) ⇒ Object
: (String) -> Fusuma::Plugin::Buffers::GestureBuffer
142 143 144 145 146 147 148 149 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 142 def select_by_type(type) cache_entry = (@cache_select_by[type] ||= CacheEntry.new(0, self.class.new([]))) cache_entry.checked.upto(@events.length - 1).each do |i| (cache_entry.value.events << @events[i]) if @events[i].record.gesture == type end cache_entry.checked = @events.length cache_entry.value end |
#select_from_last_begin ⇒ Object
: () -> Fusuma::Plugin::Buffers::GestureBuffer
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 152 def select_from_last_begin return self if empty? cache_entry = (@cache[:last_begin] ||= CacheEntry.new(0, nil)) cache_entry.value = (@events.length - 1).downto(cache_entry.checked).find do |i| @events[i].record.status == "begin" end || cache_entry.value cache_entry.checked = @events.length return self if cache_entry.value == 0 return GestureBuffer.new([]) if cache_entry.value.nil? GestureBuffer.new(@events[cache_entry.value..-1]) end |
#sum_attrs(attr) ⇒ Float
: (Symbol) -> Float
84 85 86 87 88 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 84 def sum_attrs(attr) updating_events.map do |gesture_event| gesture_event.record.delta[attr].to_f end.reduce(:+) end |
#sum_last10_attrs(attr) ⇒ Float
: (Symbol) -> Float
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 93 def sum_last10_attrs(attr) # sums last 10 values of attr (or all if length < 10) cache_entry = (@cache_sum10[attr] ||= CacheEntry.new(0, 0)) upd_ev = updating_events if upd_ev.length > cache_entry.checked + 1 cache_entry.value = upd_ev.last(10).map do |gesture_event| gesture_event.record.delta[attr].to_f end.reduce(:+) elsif upd_ev.length > cache_entry.checked cache_entry.value = cache_entry.value + upd_ev[-1].record.delta[attr].to_f - ((upd_ev.length > 10) ? upd_ev[-11].record.delta[attr].to_f : 0) else return cache_entry.value end cache_entry.checked = upd_ev.length cache_entry.value end |
#updating_events ⇒ Object
: () -> Array
111 112 113 114 115 116 117 118 |
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 111 def updating_events cache_entry = (@cache[:updating_events] ||= CacheEntry.new(0, [])) cache_entry.checked.upto(@events.length - 1).each do |i| (cache_entry.value << @events[i]) if @events[i].record.status == "update" end cache_entry.checked = @events.length cache_entry.value end |