Class: Net::VNC
- Inherits:
-
Object
- Object
- Net::VNC
- Defined in:
- lib/net/vnc.rb,
lib/net/vnc/version.rb
Overview
The VNC class provides for simple rfb-protocol based control of a VNC server. This can be used, eg, to automate applications.
Sample usage:
# launch xclock on localhost. note that there is an xterm in the top-left
require 'net/vnc'
Net::VNC.open 'localhost:0', :shared => true, :password => 'mypass' do |vnc|
vnc.pointer_move 10, 10
vnc.type 'xclock'
vnc.key_press :return
end
TODO
-
The server read loop seems a bit iffy. Not sure how best to do it.
-
Should probably be changed to be more of a lower-level protocol wrapping thing, with the actual VNCClient sitting on top of that. all it should do is read/write the packets over the socket.
Defined Under Namespace
Classes: PointerState
Constant Summary collapse
- BASE_PORT =
5900- CHALLENGE_SIZE =
16- DEFAULT_OPTIONS =
{ shared: false, wait: 0.1, pix_fmt: :BGRA, encoding: :RAW }
- KEY_MAP =
YAML.load_file(keys_file).inject({}) { |h, (k, v)| h.update k.to_sym => v }
- VERSION =
'1.2.0'.freeze
- BUTTON_MAP =
{ left: 0 }
Instance Attribute Summary collapse
-
#desktop_name ⇒ Object
readonly
Returns the value of attribute desktop_name.
-
#display ⇒ Object
readonly
Returns the value of attribute display.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#pointer ⇒ Object
readonly
Returns the value of attribute pointer.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Class Method Summary collapse
Instance Method Summary collapse
- #button_down(which = :left, options = {}) ⇒ Object
- #button_press(button = :left, options = {}) ⇒ Object
- #button_up(which = :left, options = {}) ⇒ Object
- #clipboard ⇒ Object
- #clipboard=(text) ⇒ Object
- #close ⇒ Object
- #connect ⇒ Object
-
#initialize(display = ':0', options = {}) ⇒ VNC
constructor
A new instance of VNC.
- #key_down(which, options = {}) ⇒ Object
-
#key_press(*args) ⇒ Object
this takes an array of keys, and successively holds each down then lifts them up in reverse order.
- #key_up(which, options = {}) ⇒ Object
- #pointer_move(x, y, options = {}) ⇒ Object
- #port ⇒ Object
- #reconnect ⇒ Object
-
#take_screenshot(dest = nil) ⇒ String
take screenshot as PNG image.
-
#type(text, options = {}) ⇒ Object
this types
texton the server. - #wait(options = {}) ⇒ Object
Constructor Details
#initialize(display = ':0', options = {}) ⇒ VNC
Returns a new instance of VNC.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/net/vnc.rb', line 78 def initialize(display = ':0', = {}) @server = 'localhost' if display =~ /^(.*)(:\d+)$/ @server = Regexp.last_match(1) display = Regexp.last_match(2) end @display = display[1..-1].to_i @desktop_name = nil @options = DEFAULT_OPTIONS.merge @clipboard = nil @fb = nil @pointer = PointerState.new self @mutex = Mutex.new connect @packet_reading_state = nil @packet_reading_thread = Thread.new { packet_reading_thread } end |
Instance Attribute Details
#desktop_name ⇒ Object (readonly)
Returns the value of attribute desktop_name.
76 77 78 |
# File 'lib/net/vnc.rb', line 76 def desktop_name @desktop_name end |
#display ⇒ Object (readonly)
Returns the value of attribute display.
76 77 78 |
# File 'lib/net/vnc.rb', line 76 def display @display end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
76 77 78 |
# File 'lib/net/vnc.rb', line 76 def @options end |
#pointer ⇒ Object (readonly)
Returns the value of attribute pointer.
76 77 78 |
# File 'lib/net/vnc.rb', line 76 def pointer @pointer end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
76 77 78 |
# File 'lib/net/vnc.rb', line 76 def server @server end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
76 77 78 |
# File 'lib/net/vnc.rb', line 76 def socket @socket end |
Class Method Details
.open(display = ':0', options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/net/vnc.rb', line 96 def self.open(display = ':0', = {}) vnc = new display, if block_given? begin yield vnc ensure vnc.close end else vnc end end |
Instance Method Details
#button_down(which = :left, options = {}) ⇒ Object
247 248 249 250 251 252 253 |
# File 'lib/net/vnc.rb', line 247 def (which = :left, = {}) = BUTTON_MAP[which] || which raise ArgumentError, 'Invalid button - %p' % which unless (0..2).include?() pointer. |= 1 << wait end |
#button_press(button = :left, options = {}) ⇒ Object
240 241 242 243 244 245 |
# File 'lib/net/vnc.rb', line 240 def ( = :left, = {}) , yield if block_given? ensure , end |
#button_up(which = :left, options = {}) ⇒ Object
255 256 257 258 259 260 261 |
# File 'lib/net/vnc.rb', line 255 def (which = :left, = {}) = BUTTON_MAP[which] || which raise ArgumentError, 'Invalid button - %p' % which unless (0..2).include?() pointer. &= ~(1 << ) wait end |
#clipboard ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/net/vnc.rb', line 300 def clipboard if block_given? @clipboard = nil yield 60.times do clipboard = @mutex.synchronize { @clipboard } return clipboard if clipboard sleep 0.5 end warn 'clipboard still empty after 30s' nil else @mutex.synchronize { @clipboard } end end |
#clipboard=(text) ⇒ Object
317 318 319 320 321 322 323 324 325 326 |
# File 'lib/net/vnc.rb', line 317 def clipboard=(text) text = text.to_s.gsub(/\R/, "\n") # eol of ClientCutText's text is LF byte_size = text.to_s.bytes.size packet = 0.chr * (8 + byte_size) packet[0] = 6.chr # message-type: 6 (ClientCutText) packet[4, 4] = [byte_size].pack('N') # length packet[8, byte_size] = text socket.write(packet) @clipboard = text end |
#close ⇒ Object
276 277 278 279 280 281 282 283 284 285 |
# File 'lib/net/vnc.rb', line 276 def close # destroy packet reading thread if @packet_reading_state == :loop @packet_reading_state = :stop while @packet_reading_state # do nothing end end socket.close end |
#connect ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/net/vnc.rb', line 113 def connect @socket = TCPSocket.open(server, port) raise 'invalid server response' unless socket.read(12) =~ /^RFB (\d{3}.\d{3})\n$/ @server_version = Regexp.last_match(1) socket.write "RFB 003.003\n" data = socket.read(4) auth = data.to_s.unpack1('N') case auth when 0, nil raise 'connection failed' when 1 # ok... when 2 password = @options[:password] or raise 'Need to authenticate but no password given' challenge = socket.read CHALLENGE_SIZE response = Cipher::VNCDES.new(password).encrypt(challenge) socket.write response ok = socket.read(4).to_s.unpack1('N') raise 'Unable to authenticate - %p' % ok unless ok == 0 else raise 'Unknown authentication scheme - %d' % auth end # ClientInitialisation socket.write(([:shared] ? 1 : 0).chr) # ServerInitialisation @framebuffer_width = socket.read(2).to_s.unpack1('n').to_i @framebuffer_height = socket.read(2).to_s.unpack1('n').to_i # TODO: parse this. _pixel_format = socket.read(16) # read the name in byte chunks of 20 name_length = socket.read(4).to_s.unpack1('N') @desktop_name = [].tap do |it| while name_length > 0 len = [20, name_length].min it << socket.read(len) name_length -= len end end.join _load_frame_buffer end |
#key_down(which, options = {}) ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/net/vnc.rb', line 210 def key_down(which, = {}) packet = 0.chr * 8 packet[0] = 4.chr key_code = get_key_code which packet[4, 4] = [key_code].pack('N') packet[1] = 1.chr socket.write packet wait end |
#key_press(*args) ⇒ Object
this takes an array of keys, and successively holds each down then lifts them up in reverse order. FIXME: should wait. can’t recurse in that case.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/net/vnc.rb', line 177 def key_press(*args) = args.last.is_a?(Hash) ? args.pop : {} keys = args raise ArgumentError, 'Must have at least one key argument' if keys.empty? begin key_down keys.first if keys.length == 1 yield if block_given? else key_press(*(keys[1..-1] + [])) end ensure key_up keys.first end end |
#key_up(which, options = {}) ⇒ Object
220 221 222 223 224 225 226 227 228 |
# File 'lib/net/vnc.rb', line 220 def key_up(which, = {}) packet = 0.chr * 8 packet[0] = 4.chr key_code = get_key_code which packet[4, 4] = [key_code].pack('N') packet[1] = 0.chr socket.write packet wait end |
#pointer_move(x, y, options = {}) ⇒ Object
230 231 232 233 234 |
# File 'lib/net/vnc.rb', line 230 def pointer_move(x, y, = {}) # options[:relative] pointer.update x, y wait end |
#port ⇒ Object
109 110 111 |
# File 'lib/net/vnc.rb', line 109 def port BASE_PORT + @display end |
#reconnect ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/net/vnc.rb', line 287 def reconnect 60.times do if @packet_reading_state.nil? connect @packet_reading_thread = Thread.new { packet_reading_thread } return true end sleep 0.5 end warn 'reconnect failed because packet reading state had not been stopped for 30 seconds.' false end |
#take_screenshot(dest = nil) ⇒ String
take screenshot as PNG image
267 268 269 270 |
# File 'lib/net/vnc.rb', line 267 def take_screenshot(dest = nil) fb = _load_frame_buffer # on-demand loading fb.save_pixel_data_as_png dest end |
#type(text, options = {}) ⇒ Object
this types text on the server
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/net/vnc.rb', line 161 def type(text, = {}) packet = 0.chr * 8 packet[0] = 4.chr text.split(//).each do |char| packet[7] = char[0] packet[1] = 1.chr socket.write packet packet[1] = 0.chr socket.write packet end wait end |
#wait(options = {}) ⇒ Object
272 273 274 |
# File 'lib/net/vnc.rb', line 272 def wait( = {}) sleep [:wait] || @options[:wait] end |