Class: Fusuma::Plugin::Buffers::GestureBuffer

Inherits:
Buffer show all
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

#events

Instance Method Summary collapse

Methods inherited from Buffer

#empty?, #source, #type

Methods inherited from Fusuma::Plugin::Base

#config_index, #config_params, inherited, plugins, #shutdown

Constructor Details

#initialize(*args) ⇒ GestureBuffer

Returns a new instance of GestureBuffer.



14
15
16
17
18
19
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 14

def initialize(*args)
  super
  @cache = {}
  @cache_select_by = {}
  @cache_sum10 = {}
end

Instance Method Details

#avg_attrs(attr) ⇒ Float

Parameters:

  • attr (Symbol)

Returns:

  • (Float)


113
114
115
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 113

def avg_attrs(attr)
  sum_attrs(attr).to_f / updating_events.length
end

#buffer(event) ⇒ Buffer, FalseClass

Parameters:

  • event (Event)

Returns:



37
38
39
40
41
42
43
44
45
46
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 37

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

#clearObject



21
22
23
24
25
26
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 21

def clear
  super.clear
  @cache = {}
  @cache_select_by = {}
  @cache_sum10 = {}
end

#clear_expired(current_time: Time.now) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 48

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_typesObject



28
29
30
31
32
33
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 28

def config_param_types
  {
    source: [String],
    seconds_to_keep: [Float, Integer]
  }
end

#ended?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 64

def ended?
  return false if empty?

  case @events.last.record.status
  when "end", "cancelled"
    true
  else
    false
  end
end

#fingerObject

return [Integer]



118
119
120
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 118

def finger
  @events.last.record.finger.to_i
end

#gestureString

Examples:

event_buffer.gesture
=> 'swipe'

Returns:



126
127
128
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 126

def gesture
  @events.last.record.gesture
end

#select_by_events(&block) ⇒ Object



130
131
132
133
134
135
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 130

def select_by_events(&block)
  return enum_for(:select_by_events) unless block

  events = @events.select(&block)
  self.class.new events
end

#select_by_type(type) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 137

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_beginObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 146

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

Parameters:

  • attr (Symbol)

Returns:

  • (Float)


77
78
79
80
81
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 77

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

Parameters:

  • attr (Symbol)

Returns:

  • (Float)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 85

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_eventsObject



102
103
104
105
106
107
108
109
# File 'lib/fusuma/plugin/buffers/gesture_buffer.rb', line 102

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