Class: FingerPoken::Target::VNC

Inherits:
FingerPoken::Target
  • Object
show all
Defined in:
lib/fingerpoken/vnc.rb

Defined Under Namespace

Classes: VNCClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ VNC

Returns a new instance of VNC.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fingerpoken/vnc.rb', line 12

def initialize(config)
  super(config)
  # TODO(sissel): eventmachine-vnc needs to suppore more auth mechanisms
  @user = config[:user]
  @password = (config[:password] or config[:user])
  @host = config[:host]
  @port = (config[:port] or 5900)
  @ready = false
  @recenter = config[:recenter]

  # For eventmachine-vnc
  ENV["VNCPASS"] = @password

  if @host == nil
    raise "#{self.class.name}: No host given to connect to"
  end

  @vnc = EventMachine::connect(@host, @port, VNCClient, self)
  @x = 0
  @y = 0
  @buttonmask = 0
end

Instance Attribute Details

#buttonmaskObject

Returns the value of attribute buttonmask.



10
11
12
# File 'lib/fingerpoken/vnc.rb', line 10

def buttonmask
  @buttonmask
end

#screen_xObject

Returns the value of attribute screen_x.



8
9
10
# File 'lib/fingerpoken/vnc.rb', line 8

def screen_x
  @screen_x
end

#screen_yObject

Returns the value of attribute screen_y.



9
10
11
# File 'lib/fingerpoken/vnc.rb', line 9

def screen_y
  @screen_y
end

#xObject

Returns the value of attribute x.



6
7
8
# File 'lib/fingerpoken/vnc.rb', line 6

def x
  @x
end

#yObject

Returns the value of attribute y.



7
8
9
# File 'lib/fingerpoken/vnc.rb', line 7

def y
  @y
end

Instance Method Details

#keypress(key) ⇒ Object

TODO(sissel): Add keyboard support. VNC uses the same keysym values as X11, so that’s a win. We can likely leverage xdo’s char-to-keysym magic with VNC.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fingerpoken/vnc.rb', line 92

def keypress(key)
  puts "Got key: #{key} (#{key.class})"
  if key.is_a?(String)
    if key.length == 1
      # Assume letter
      @vnc.keyevent(key.chr, true)
      @vnc.keyevent(key.chr, false)
    else
      # Assume keysym
      puts "I don't know how to type '#{key}'"
      return { :action => "status", :status => "I don't know how to type '#{key}'" }
    end
  else
    # type printables, key others.
    if 32.upto(127).include?(key)
      @vnc.keyevent(key, true)
      @vnc.keyevent(key, false)
    else
      case key
        when 8 
          @vnc.keyevent(0xff08, true)
          @vnc.keyevent(0xff08, false)
        when 13
          @vnc.keyevent(0xff0D, true)
          @vnc.keyevent(0xff0D, false)
        else
          puts "I don't know how to type web keycode '#{key}'"
          return { :action => "status", :status => "I don't know how to type '#{key}'" }
        end # case key
    end # if 32.upto(127).include?(key)
  end # if key.is_a?String
  return nil
end

#mousedown(button) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/fingerpoken/vnc.rb', line 73

def mousedown(button)
  button = (1 << (button.to_i - 1))
  return if @buttonmask & button != 0
  @buttonmask |= button
  update_mouse
  return nil
end

#mousemove_absolute(px, py) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/fingerpoken/vnc.rb', line 63

def mousemove_absolute(px, py)
  # Edges may be hard to hit on some devices, so inflate things a bit.
  xbuf = @screen_x * 0.1
  ybuf = @screen_y * 0.1
  @x = (((@screen_x + xbuf) * px) - (xbuf / 2)).to_i
  @y = (((@screen_y + ybuf) * py) - (ybuf / 2)).to_i
  update_mouse
  return nil
end

#mousemove_relative(x, y) ⇒ Object



56
57
58
59
60
61
# File 'lib/fingerpoken/vnc.rb', line 56

def mousemove_relative(x, y)
  @x += x
  @y += y
  update_mouse
  return nil
end

#mouseup(button) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/fingerpoken/vnc.rb', line 81

def mouseup(button)
  button = (1 << (button.to_i - 1))
  return if @buttonmask & button == 0
  @buttonmask &= (~button)
  update_mouse
  return nil
end

#readyObject



51
52
53
54
# File 'lib/fingerpoken/vnc.rb', line 51

def ready
  @ready = true
  return { "action" => "status", "status" => "VNC READY!" }
end

#update_mouseObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fingerpoken/vnc.rb', line 35

def update_mouse
  if !@ready
    @logger.warn("VNC connection is not ready. Ignoring update.")
    return { "action" => "status", "status" => "VNC connection not ready, yet" }
  end
  @vnc.pointerevent(@x, @y, @buttonmask)

  # TODO(sissel): Hack to make it work in TF2.
  # Mouse movement is always "from center"
  # So after each move, center the cursor.
  if @recenter
    @x = (@vnc.screen_width / 2).to_i
    @y = (@vnc.screen_height / 2).to_i
  end
end