Class: Vigilem::Evdev::VTYContextFilter

Inherits:
Object
  • Object
show all
Includes:
ContextFilter
Defined in:
lib/vigilem/evdev/vty_context_filter.rb

Overview

filters events if the VTY differs from the one of this process

Constant Summary collapse

KeyMap =
Support::KeyMap

Constants included from ContextFilter::States

ContextFilter::States::OFF, ContextFilter::States::ON, ContextFilter::States::UNKNOWN

Instance Attribute Summary collapse

Attributes included from ContextFilter

#last_known_state

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContextFilter

included, #on_change, #state_hash, #was_off?, #was_on?

Constructor Details

#initialize(keymap = Evdev.key_map) ⇒ Object

Parameters:

  • keymap (KeyMap) (defaults to: Evdev.key_map)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 25

def initialize(keymap=Evdev.key_map)
  @key_map = keymap unless keymap.eql? Evdev.key_map
  
  @current_console_num = `fgconsole`.rstrip.to_i
  curr_con_keys, oth_con_keys = 
    self.class.console_keys(keymap).partition {|syms,v| v == "Console_#{@current_console_num}" }
  @current_console_keys, @other_console_keys = KeyMap[curr_con_keys], KeyMap[oth_con_keys]
  
  @current_console_keys.left_side_aliases(:keycode, :keycodes)
  @current_console_keys.right_side_aliases(:keysym, :keysyms)
  
  @other_console_keys.left_side_aliases(:keycode, :keycodes)
  @other_console_keys.right_side_aliases(:keysym, :keysyms)
  
  @current_keys = []
end

Instance Attribute Details

#console_keysObject (readonly)

TODO:

make these class_methods



19
20
21
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 19

def console_keys
  @console_keys
end

#current_console_keysObject (readonly)

TODO:

make these class_methods



19
20
21
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 19

def current_console_keys
  @current_console_keys
end

#current_console_numObject (readonly)

TODO:

make these class_methods



19
20
21
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 19

def current_console_num
  @current_console_num
end

#other_console_keysObject (readonly)

TODO:

make these class_methods



19
20
21
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 19

def other_console_keys
  @other_console_keys
end

Class Method Details

.console_keys(keymap) ⇒ KeyMap

Parameters:

Returns:



45
46
47
48
49
50
51
52
53
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 45

def self.console_keys(keymap)
  @console_keys = if keymap.[:loader].class.name =~ /KmapLoader/
      keymap.select {|k,v| v =~ /console_\d+/i }
    else
      console_syms = keymap.info.keysyms.select {|k,v| v =~ /console_\d+/i }
      tmp = keymap.select {|k,v| v =~ /^#{console_syms.keys.join('|')}$/ } 
      tmp.each {|k,v| tmp[k] = console_syms[v] }
    end
end

Instance Method Details

#add_key(keycode_or_keysym) ⇒ Array

Parameters:

  • key

Returns:

  • (Array)


102
103
104
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 102

def add_key(keycode_or_keysym)
  current_keys << keycode_or_keysym
end

#changed_vty?TrueClass || FalseClass

Returns:

  • (TrueClass || FalseClass)


157
158
159
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 157

def changed_vty?
  was_off? and other_console_keys.keysym(current_keys_downcase)
end

#current_keysArray

Returns:

  • (Array)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 63

def current_keys
  if @current_keys and current_keys_changed? 
    max_and_one = KeyMap.mod_weights.values.max + 1
    @current_keys.sort_by! {|ksym| KeyMap.mod_weights[ksym.downcase] || max_and_one }
    @current_keys_hash_cache = @current_keys.hash
    @current_keys
  else
    if @current_keys.nil?
      @current_keys = []
      @current_keys_hash_cache = @current_keys.hash
    end
    @current_keys
  end
end

#key_mapKeyMap

Returns:



57
58
59
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 57

def key_map
  @key_map || Evdev.key_map
end

#keysym(keycode) ⇒ String

TODO:

move to KeyMapUtils, or something like that

Parameters:

  • key_num (Integer)

Returns:

  • (String)


128
129
130
131
132
133
134
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 128

def keysym(keycode)
  if key_map.[:loader].class.name =~ /KmapLoader/
    key_map.keysym(keycode)
  else
    key_map.info.keysyms[key_map.char_ref(keycode)]
  end
end

#keysym_or_keycode(key_num) ⇒ String

TODO:

name

Returns:

  • (String)


116
117
118
119
120
121
122
123
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 116

def keysym_or_keycode(key_num)
  sym = keysym(keycode = "keycode#{key_num}")
  if sym =~ /shift|alt|c(on)?tro?l|capsshift/i
    sym
  else
    keycode
  end
end

#off?TrueClass || FalseClass

Returns:

  • (TrueClass || FalseClass)


163
164
165
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 163

def off?
  not on?
end

#on?TrueClass || FalseClass || NilClass

Returns:

  • (TrueClass || FalseClass || NilClass)


138
139
140
141
142
143
144
145
146
147
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 138

def on?
  if changed_vty?
    self.filter_ommisions = current_keys_downcase
    on_change(ON)
  elsif reverted_vty?
    self.filter_ommisions = current_keys_downcase
    on_change(OFF)
  end
  was_on?
end

#process(*events) ⇒ Array

Parameters:

  • events (Array<System::InputEvent>)

Returns:

  • (Array)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 81

def process(*events)
  events.select do |event|
    off_status = off?
    key_check = if event.type == System::Input::EV_KEY
      if event.value == 1
        add_key(keysym_or_keycode(event.code))
        off_status
      elsif event.value == 0
        kork = keysym_or_keycode(event.code)
        release_status = allow_release?(kork)
        remove_key(kork)
        release_status
      end
    end
    if key_check.nil? then off_status else key_check end
  end
end

#remove_key(keycode_or_keysym) ⇒ Object

Parameters:

  • key_num (Fixnum)

Returns:



109
110
111
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 109

def remove_key(keycode_or_keysym)
  current_keys.delete(keycode_or_keysym)
end

#reverted_vty?TrueClass || FalseClass

Returns:

  • (TrueClass || FalseClass)


151
152
153
# File 'lib/vigilem/evdev/vty_context_filter.rb', line 151

def reverted_vty?
  was_on? and current_console_keys.keysym(current_keys_downcase)
end