Class: XInputWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/xinput_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(device: '3', host: 'sps', port: '59000', topic: 'input/keyboard', verbose: true, lookup: {}, interval: nil) ⇒ XInputWrapper

Returns a new instance of XInputWrapper.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/xinput_wrapper.rb', line 12

def initialize(device: '3', host: 'sps', port: '59000', 
           topic: 'input/keyboard', verbose: true, lookup: {}, interval: nil)

  @lookup = {
    37 => :control,
    50 => :lshift,
    133 => :super
  }.merge(lookup)

  @device, @topic, @verbose = device, topic, verbose
  @sps = SPSPub.new host: host, port: port
  @sk = SecretKnock.new short_delay: 0.25, long_delay: 0.5, 
                            external: self, verbose: verbose
  @interval = interval
  @time = Time.now if interval
  
end

Instance Method Details

#knockObject



30
31
32
# File 'lib/xinput_wrapper.rb', line 30

def knock()
  puts 'knock' if @verbose
end

#listenObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/xinput_wrapper.rb', line 43

def listen()

  command = "xinput test-xi2 --root #{@device}"
  @sk.detect timeout: 0.7
  sk = @sk
  type = 0

  IO.popen(command).each_line do |x|
 
    #print "GOT ", x
    raw_type = x[/EVENT type (\d+)/,1]

    type = raw_type.to_i unless raw_type.nil?
    
    # type = 13 means a key has been pressed
    if type == 13 then

      keycode = x[/detail: (\d+)/,1].to_i

      puts 'keycode: ' + keycode.to_s if keycode > 0 and @verbose
      
      if @interval then
        
        if Time.now > @time + @interval then
          @sps.notice "%s: key pressed" % [@topic] 
          @time = Time.now
        end
      end

      case @lookup[keycode]
      when :lshift
        puts 'left shift' if @verbose
      when :control
        puts 'control' if @verbose
        sk.knock
      when :super
        puts 'super key pressed'  if @verbose
        message 'super key pressed' 
      end

    end
  end
end

#message(msg) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/xinput_wrapper.rb', line 34

def message(msg)
  
  puts ':: ' + msg.inspect if @verbose        
  
  return if msg.strip.empty?
  
  @sps.notice "%s: %s" % [@topic, msg]
end