blib/lib/Net/VNC.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Net::VNC;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Crypt::DES;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use Image::Imlib2;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use IO::Socket::INET;
8 use bytes;
9 __PACKAGE__->mk_accessors(
10 qw(hostname port username password socket name width height depth save_bandwidth
11 hide_cursor server_endian
12 _pixinfo _colourmap _framebuffer _cursordata _rfb_version
13 _bpp _true_colour _big_endian _image_format
14 )
15 );
16 our $VERSION = '0.40';
17  
18 my $MAX_PROTOCOL_VERSION = 'RFB 003.008' . chr(0x0a); # Max version supported
19  
20 # Precompute booleans for specific Image::Imlib2 features
21 my $CAN_CREATE_RAW_IMAGE = Image::Imlib2->can('new_using_data');
22 my $CAN_CHANGE_BLEND = Image::Imlib2->can('will_blend');
23  
24 # This line comes from perlport.pod
25 my $AM_BIG_ENDIAN = unpack( 'h*', pack( 's', 1 ) ) =~ /01/ ? 1 : 0;
26  
27 # The numbers in the hashes below were acquired from the VNC source code
28 my %supported_depths = (
29 '24' => {
30 bpp => 32,
31 true_colour => 1,
32 red_max => 255,
33 green_max => 255,
34 blue_max => 255,
35 red_shift => 16,
36 green_shift => 8,
37 blue_shift => 0,
38 },
39 '16' => {
40 bpp => 16,
41 true_colour => 1,
42 red_max => 31,
43 green_max => 31,
44 blue_max => 31,
45 red_shift => 10,
46 green_shift => 5,
47 blue_shift => 0,
48 },
49 '8' => {
50 bpp => 8,
51 true_colour => 0,
52 red_max => 255,
53 green_max => 255,
54 blue_max => 255,
55 red_shift => 16,
56 green_shift => 8,
57 blue_shift => 0,
58 },
59  
60 # Unused right now, but supportable
61 '8t' => {
62 bpp => 8,
63 true_colour => 1, #!!!
64 red_max => 7,
65 green_max => 7,
66 blue_max => 3,
67 red_shift => 0,
68 green_shift => 3,
69 blue_shift => 6,
70 },
71 );
72  
73 my @encodings = (
74  
75 # These ones are defined in rfbproto.pdf
76 { num => 0,
77 name => 'Raw',
78 supported => 1,
79 },
80 { num => 1,
81 name => 'CopyRect',
82 supported => 1,
83 },
84 { num => 2,
85 name => 'RRE',
86 supported => 1,
87 },
88 { num => 4,
89 name => 'CoRRE',
90 supported => 1,
91 },
92 { num => 5,
93 name => 'Hextile',
94 supported => 1,
95 bandwidth => 1,
96 },
97 { num => 16,
98 name => 'ZRLE',
99 supported => 0,
100 bandwidth => 1,
101 },
102 { num => -239,
103 name => 'Cursor',
104 supported => 1,
105 cursor => 1,
106 },
107 { num => -223,
108 name => 'DesktopSize',
109 supported => 0,
110 },
111  
112 # Learned about these from cvs://cotvnc.sf.net/cotvnc/Source/rfbproto.h
113 # None of them are currently used
114 map( { { num => -256 + $_,
115 name => 'CompressLevel' . $_,
116 supported => 0,
117 compress => 1,
118 }
119 } 0 .. 9 ),
120 { num => -240,
121 name => 'XCursor',
122 supported => 0,
123 cursor => 1,
124 },
125 { num => -224,
126 name => 'LastRect',
127 supported => 0,
128 },
129 map( { { num => -32 + $_,
130 name => 'QualityLevel' . $_,
131 supported => 0,
132 quality => 1,
133 }
134 } 0 .. 9 ),
135  
136 # Learned about this one from pyvnc2swf/rfb.py, but I don't understand where it comes from
137 # It doesn't seem to be documented in CotVNC or VNC 4.1.1 source code
138 { num => -232,
139 name => 'CursorPos',
140 supported => 1,
141 cursor => 1,
142 },
143 );
144  
145 sub list_encodings {
146 my $pkg_or_self = shift;
147  
148 my %encmap = map { $_->{num} => $_->{name} } @encodings;
149 return %encmap;
150 }
151  
152 sub login {
153 my $self = shift;
154 my $hostname = $self->hostname;
155 my $port = $self->port;
156 my $socket = IO::Socket::INET->new(
157 PeerAddr => $hostname || 'localhost',
158 PeerPort => $port || '5900',
159 Proto => 'tcp',
160 ) || die "Error connecting to $hostname: $@";
161 $socket->timeout(15);
162 $self->socket($socket);
163  
164 eval {
165 $self->_handshake_protocol_version();
166 $self->_handshake_security();
167 $self->_client_initialization();
168 $self->_server_initialization();
169 };
170 my $error = $@; # store so it doesn't get overwritten
171 if ($error) {
172  
173 # clean up so socket can be garbage collected
174 $self->socket(undef);
175 die $error;
176 }
177 }
178  
179 sub _handshake_protocol_version {
180 my $self = shift;
181  
182 my $socket = $self->socket;
183 $socket->read( my $protocol_version, 12 ) || die 'unexpected end of data';
184  
185 # warn "prot: $protocol_version";
186  
187 my $protocol_pattern = qr/\A RFB [ ] (\d{3}\.\d{3}) \s* \z/xms;
188 if ( $protocol_version !~ m/$protocol_pattern/xms ) {
189 die 'Malformed RFB protocol: ' . $protocol_version;
190 }
191 $self->_rfb_version($1);
192  
193 if ( $protocol_version gt $MAX_PROTOCOL_VERSION ) {
194 $protocol_version = $MAX_PROTOCOL_VERSION;
195  
196 # Repeat with the changed version
197 if ( $protocol_version !~ m/$protocol_pattern/xms ) {
198 die 'Malformed RFB protocol';
199 }
200 $self->_rfb_version($1);
201 }
202  
203 if ( $self->_rfb_version lt '003.003' ) {
204 die 'RFB protocols earlier than v3.3 are not supported';
205 }
206  
207 # let's use the same version of the protocol, or the max, whichever's lower
208 $socket->print($protocol_version);
209 }
210  
211 sub _handshake_security {
212 my $self = shift;
213  
214 my $socket = $self->socket;
215  
216 # Retrieve list of security options
217 my $security_type;
218 if ( $self->_rfb_version ge '003.007' ) {
219 $socket->read( my $number_of_security_types, 1 )
220 || die 'unexpected end of data';
221 $number_of_security_types = unpack( 'C', $number_of_security_types );
222  
223 # warn "types: $number_of_security_types";
224  
225 if ( $number_of_security_types == 0 ) {
226 die 'Error authenticating';
227 }
228  
229 my @security_types;
230 foreach ( 1 .. $number_of_security_types ) {
231 $socket->read( my $security_type, 1 )
232 || die 'unexpected end of data';
233 $security_type = unpack( 'C', $security_type );
234  
235 # warn "sec: $security_type";
236 push @security_types, $security_type;
237 }
238  
239 my @pref_types = ( 1, 2 );
240 @pref_types = ( 30, 1, 2 ) if $self->username;
241  
242 for my $preferred_type (@pref_types) {
243 if ( 0 < grep { $_ == $preferred_type } @security_types ) {
244 $security_type = $preferred_type;
245 last;
246 }
247 }
248 } else {
249  
250 # In RFB 3.3, the server dictates the security type
251 $socket->read( $security_type, 4 ) || die 'unexpected end of data';
252 $security_type = unpack( 'N', $security_type );
253 }
254  
255 if ( !$security_type ) {
256  
257 die 'Connection failed';
258  
259 } elsif ( $security_type == 2 ) {
260  
261 # DES-encrypted challenge/response
262  
263 if ( $self->_rfb_version ge '003.007' ) {
264 $socket->print( pack( 'C', 2 ) );
265 }
266  
267 $socket->read( my $challenge, 16 ) || die 'unexpected end of data';
268  
269 # warn "chal: " . unpack('h*', $challenge) . "\n";
270  
271 # the RFB protocol only uses the first 8 characters of a password
272 my $key = substr( $self->password, 0, 8 );
273 $key = '' if ( !defined $key );
274 $key .= pack( 'C', 0 ) until ( length($key) % 8 ) == 0;
275  
276 my $realkey;
277  
278 # warn unpack('b*', $key);
279 foreach my $byte ( split //, $key ) {
280 $realkey .= pack( 'b8', scalar reverse unpack( 'b8', $byte ) );
281 }
282  
283 # warn unpack('b*', $realkey);
284  
285 my $cipher = Crypt::DES->new($realkey);
286 my $response;
287 my $i = 0;
288 while ( $i < 16 ) {
289 my $word = substr( $challenge, $i, 8 );
290  
291 # warn "$i: " . length($word);
292 $response .= $cipher->encrypt($word);
293 $i += 8;
294 }
295  
296 # warn "resp: " . unpack('h*', $response) . "\n";
297  
298 $socket->print($response);
299  
300 } elsif ( $security_type == 1 ) {
301  
302 # No authorization needed!
303 if ( $self->_rfb_version ge '003.007' ) {
304 $socket->print( pack( 'C', 1 ) );
305 }
306  
307 } elsif ( $security_type == 30 ) {
308  
309 require Crypt::GCrypt::MPI;
310 require Crypt::Random;
311  
312 # ARD - Apple Remote Desktop - authentication
313 $socket->print( pack( 'C', 30 ) ); # use ARD
314 $socket->read( my $gen, 2 ) || die 'unexpected end of data';
315 $socket->read( my $len, 2 ) || die 'unexpected end of data';
316 my $keylen = $self->_bin_int($len);
317 $socket->read( my $mod, $keylen ) || die 'unexpected end of data';
318 $socket->read( my $resp, $keylen ) || die 'unexpected end of data';
319  
320 my $genmpi = Crypt::GCrypt::MPI::new(
321 secure => 0,
322 value => $self->_bin_int($gen),
323 format => Crypt::GCrypt::MPI::FMT_USG()
324 );
325 my $modmpi = Crypt::GCrypt::MPI::new(
326 secure => 0,
327 value => $mod,
328 format => Crypt::GCrypt::MPI::FMT_USG()
329 );
330 my $respmpi = Crypt::GCrypt::MPI::new(
331 secure => 0,
332 value => $resp,
333 format => Crypt::GCrypt::MPI::FMT_USG()
334 );
335 my $privmpi = $self->_mpi_randomize($keylen);
336  
337 my $pubmpi = $genmpi->copy()->powm( $privmpi, $modmpi );
338 my $keympi = $respmpi->copy()->powm( $privmpi, $modmpi );
339 my $pub = $self->_mpi_2_bytes( $pubmpi, $keylen );
340 my $key = $self->_mpi_2_bytes( $keympi, $keylen );
341 my $md5 = Crypt::GCrypt->new( type => 'digest', algorithm => 'md5' );
342 $md5->write($key);
343 my $shared = $md5->read();
344 my $passlen = length( $self->password ) + 1;
345 my $userlen = length( $self->username ) + 1;
346 $passlen = 64 if ( $passlen > 64 );
347 my $passpad = 64 - $passlen;
348 $userlen = 64 if ( $userlen > 64 );
349 my $userpad = 64 - $userlen;
350 my $up = Crypt::Random::makerandom_octet(
351 Length => $userpad,
352 Strength => 1
353 );
354 my $pp = Crypt::Random::makerandom_octet(
355 Length => $passpad,
356 Strength => 1
357 );
358 my $userpass = pack "a*xa*a*xa*", $self->username, $up,
359 $self->password, $pp;
360 my $aes = Crypt::GCrypt->new(
361 type => 'cipher',
362 algorithm => 'aes',
363 mode => 'ecb'
364 );
365 $aes->start('encrypting');
366 $aes->setkey($shared);
367 my $cyptxt = $aes->encrypt($userpass);
368 $cyptxt .= $aes->finish;
369 $socket->write( $cyptxt, 128 ); # appears to be only writing 16 bytes
370 $socket->write( $pub, $keylen ); # appears to be only writing 16 bytes
371 $socket->read( my $security_result, 4 )
372 || die 'unexpected end of data';
373 $security_result = $self->_bin_int($security_result);
374  
375 if ( $security_result == 1 ) {
376 $socket->read( my $len, 4 ) || die 'unexpected end of data';
377 $socket->read( my $msg, $self->_bin_int($len) )
378 || die 'unexpected end of data';
379 die "VNC Authentication Failed: $msg";
380 } elsif ( $security_result == 2 ) {
381  
382 # too many
383 die "VNC Authentication Failed - too many tries";
384 }
385 } else {
386  
387 die "no supported vnc authentication mechanism";
388  
389 }
390  
391 # the RFB protocol always returns a result for type 2,
392 # but type 1, only for 003.008 and up
393 if ( ( $self->_rfb_version ge '003.008' && $security_type == 1 )
394 || $security_type == 2 )
395 {
396 $socket->read( my $security_result, 4 )
397 || die 'unexpected end of data';
398 $security_result = unpack( 'I', $security_result );
399  
400 # warn $security_result;
401 die 'login failed' if $security_result;
402 } elsif ( !$socket->connected ) {
403 die 'login failed';
404 }
405 }
406  
407 sub _mpi_randomize {
408 my ( $self, $l ) = @_;
409 my $bits = int( $l / 8 ) * 8;
410 my $bytes = int( $bits / 8 );
411 my $r
412 = Crypt::Random::makerandom_octet( Length => $bytes, Strength => 1 );
413 my @ra = unpack( "C*", $r );
414 my $mpi = Crypt::GCrypt::MPI::new( secure => 0, value => 0 );
415 my $tfs = Crypt::GCrypt::MPI::new( secure => 0, value => 256 );
416 for ( my $i = 0; $i < $bytes; $i++ ) {
417 $mpi = $mpi->mul($tfs);
418 my $n = $ra[$i];
419 $mpi = $mpi->add(
420 Crypt::GCrypt::MPI::new( secure => 0, value => $n ) );
421 }
422 return $mpi;
423 }
424  
425 sub _mpi_2_bytes {
426 my ( $self, $mpi, $sz ) = @_;
427 my $s = $mpi->print( Crypt::GCrypt::MPI::FMT_USG() );
428 my $pad = $sz - length($s);
429 return pack( "x[$pad]a*", $s );
430 }
431  
432 sub _bin_int {
433 my ( $self, $s ) = @_;
434 my @a = unpack( "C*", $s );
435 my $r = 0;
436 for ( my $i = 0; $i < @a; $i++ ) {
437 $r = 256 * $r;
438 $r += $a[$i];
439 }
440 return $r;
441 }
442  
443 sub _client_initialization {
444 my $self = shift;
445  
446 my $socket = $self->socket;
447  
448 $socket->print( pack( 'C', 1 ) ); # share
449 }
450  
451 sub _server_initialization {
452 my $self = shift;
453  
454 my $socket = $self->socket;
455 $socket->read( my $server_init, 24 ) || die 'unexpected end of data';
456  
457 my ( $framebuffer_width, $framebuffer_height, $bits_per_pixel, $depth,
458 $big_endian_flag, $true_colour_flag, %pixinfo, $name_length );
459 ( $framebuffer_width, $framebuffer_height, $bits_per_pixel,
460 $depth, $big_endian_flag, $true_colour_flag,
461 $pixinfo{red_max}, $pixinfo{green_max}, $pixinfo{blue_max},
462 $pixinfo{red_shift}, $pixinfo{green_shift}, $pixinfo{blue_shift},
463 $name_length
464 ) = unpack 'nnCCCCnnnCCCxxxN', $server_init;
465  
466 # warn "$framebuffer_width x $framebuffer_height";
467  
468 # warn "$bits_per_pixel bpp / depth $depth / $big_endian_flag be / $true_colour_flag tc / $pixinfo{red_max},$pixinfo{green_max},$pixinfo{blue_max} / $pixinfo{red_shift},$pixinfo{green_shift},$pixinfo{blue_shift}";
469  
470 # warn $name_length;
471  
472 if ( !$self->depth ) {
473  
474 # client did not express a depth preference, so check if the server's preference is OK
475 if ( !$supported_depths{$depth} ) {
476 die 'Unsupported depth ' . $depth;
477 }
478 if ( $bits_per_pixel != $supported_depths{$depth}->{bpp} ) {
479 die 'Unsupported bits-per-pixel value ' . $bits_per_pixel;
480 }
481 if ($true_colour_flag
482 ? !$supported_depths{$depth}->{true_colour}
483 : $supported_depths{$depth}->{true_colour}
484 )
485 {
486 die 'Unsupported true colour flag';
487 }
488 $self->depth($depth);
489  
490 # Use server's values for *_max and *_shift
491  
492 } elsif ( $depth != $self->depth ) {
493 for my $key (
494 qw(red_max green_max blue_max red_shift green_shift blue_shift))
495 {
496 $pixinfo{$key} = $supported_depths{ $self->depth }->{$key};
497 }
498 }
499  
500 if ( !$self->width ) {
501 $self->width($framebuffer_width);
502 }
503 if ( !$self->height ) {
504 $self->height($framebuffer_height);
505 }
506 $self->_pixinfo( \%pixinfo );
507 $self->_bpp( $supported_depths{ $self->depth }->{bpp} );
508 $self->_true_colour( $supported_depths{ $self->depth }->{true_colour} );
509 $self->_big_endian(
510 $self->server_endian ? $big_endian_flag : $AM_BIG_ENDIAN );
511  
512 $socket->read( my $name_string, $name_length )
513 || die 'unexpected end of data';
514 $self->name($name_string);
515  
516 # warn $name_string;
517  
518 # setpixelformat
519 $socket->print(
520 pack(
521 'CCCCCCCCnnnCCCCCC',
522 0, # message_type
523 0, # padding
524 0, # padding
525 0, # padding
526 $self->_bpp,
527 $self->depth,
528 $self->_big_endian,
529 $self->_true_colour,
530 $pixinfo{red_max},
531 $pixinfo{green_max},
532 $pixinfo{blue_max},
533 $pixinfo{red_shift},
534 $pixinfo{green_shift},
535 $pixinfo{blue_shift},
536 0, # padding
537 0, # padding
538 0, # padding
539 )
540 );
541  
542 # set encodings
543  
544 my @encs = grep { $_->{supported} } @encodings;
545  
546 # Prefer the higher-numbered encodings
547 @encs = reverse sort { $a->{num} <=> $b->{num} } @encs;
548  
549 if ( !$self->save_bandwidth ) {
550 @encs = grep { !$_->{bandwidth} } @encs;
551 }
552 if ( $self->hide_cursor ) {
553 @encs = grep { !$_->{cursor} } @encs;
554 }
555  
556 $socket->print(
557 pack(
558 'CCn',
559 2, # message_type
560 0, # padding
561 scalar @encs, # number_of_encodings
562 )
563 );
564 for my $enc (@encs) {
565  
566 # Make a big-endian, signed 32-bit value
567 # method:
568 # pack as own-endian, signed e.g. -239
569 # unpack as own-endian, unsigned e.g. 4294967057
570 # pack as big-endian
571 my $num = pack 'N', unpack 'L', pack 'l', $enc->{num};
572 $socket->print($num);
573 }
574 }
575  
576 sub capture {
577 my $self = shift;
578 my $socket = $self->socket;
579  
580 $self->_send_update_request();
581 while ( ( my $message_type = $self->_receive_message() ) != 0 ) {
582  
583 # warn $message_type;
584 }
585  
586 return $self->_image_plus_cursor;
587 }
588  
589 sub _image_plus_cursor {
590 my $self = shift;
591  
592 my $image = $self->_framebuffer;
593 my $cursor = $self->_cursordata;
594 if ( !$self->hide_cursor
595 && $cursor
596 && $cursor->{image}
597 && defined $cursor->{x} )
598 {
599  
600 #$cursor->{image}->save('cursor.png'); # temporary -- debugging
601 $image
602 = $image->clone(); # make a duplicate so we can overlay the cursor
603 $image->blend(
604 $cursor->{image},
605 1, # don't modify destination alpha
606 0, 0, $cursor->{width}, $cursor->{height}, # source dimensions
607 $cursor->{x}, $cursor->{y}, $cursor->{width},
608 $cursor->{height}, # destination dimensions
609 );
610 }
611 return $image;
612 }
613  
614 sub _send_key_event {
615 my ( $self, $down_flag, $key ) = @_;
616  
617 # A key press or release. Down-flag is non-zero (true) if the key is now pressed, zero
618 # (false) if it is now released. The key itself is specified using the “keysym” values
619 # defined by the X Window System.
620  
621 my $socket = $self->socket;
622 $socket->print(
623 pack(
624 'CCnN',
625 4, # message_type
626 $down_flag, # down-flag
627 0, # padding
628 $key, # key
629 )
630 );
631 }
632  
633 sub send_key_event_down {
634 my ( $self, $key ) = @_;
635 $self->_send_key_event( 1, $key );
636 }
637  
638 sub send_key_event_up {
639 my ( $self, $key ) = @_;
640 $self->_send_key_event( 0, $key );
641 }
642  
643 sub send_key_event {
644 my ( $self, $key ) = @_;
645 $self->send_key_event_down($key);
646 $self->send_key_event_up($key);
647 }
648  
649 sub send_key_event_string {
650 my ( $self, $string ) = @_;
651 foreach my $key ( map {ord} split //, $string ) {
652 warn $key;
653 $self->send_key_event($key);
654 }
655 }
656  
657 sub send_pointer_event {
658 my ( $self, $button_mask, $x, $y ) = @_;
659  
660 $self->socket->print(
661 pack(
662 'CCnn',
663 5, # message type
664 $button_mask, # button-mask
665 $x, # x-position
666 $y, # y-position
667 )
668 );
669 }
670  
671 sub _send_update_request {
672 my $self = shift;
673  
674 # frame buffer update request
675 my $socket = $self->socket;
676 my $incremental = $self->_framebuffer ? 1 : 0;
677 $socket->print(
678 pack(
679 'CCnnnn',
680 3, # message_type
681 $incremental, # incremental
682 0, # x
683 0, # y
684 $self->width,
685 $self->height,
686 )
687 );
688 }
689  
690 sub _receive_message {
691 my $self = shift;
692  
693 my $socket = $self->socket;
694 $socket->read( my $message_type, 1 ) || die 'unexpected end of data';
695 $message_type = unpack( 'C', $message_type );
696  
697 # warn $message_type;
698  
699 # This result is unused. It's meaning is different for the different methods
700 my $result
701 = !defined $message_type ? die 'bad message type received'
702 : $message_type == 0 ? $self->_receive_update()
703 : $message_type == 1 ? $self->_receive_colour_map()
704 : $message_type == 2 ? $self->_receive_bell()
705 : $message_type == 3 ? $self->_receive_cut_text()
706 : die 'unsupported message type received';
707  
708 return $message_type;
709 }
710  
711 sub _receive_update {
712 my $self = shift;
713  
714 my $image = $self->_framebuffer;
715 if ( !$image ) {
716 $self->_framebuffer( $image
717 = Image::Imlib2->new( $self->width, $self->height ) );
718 if ( $self->_image_format ) {
719 $image->image_set_format( $self->_image_format );
720 }
721 if ($CAN_CREATE_RAW_IMAGE) {
722  
723 # We're going to be splatting pixels, so make sure every pixel is opaque
724 $image->set_colour( 0, 0, 0, 255 );
725 $image->fill_rectangle( 0, 0, $self->width, $self->height );
726 }
727 }
728  
729 my $socket = $self->socket;
730 $socket->read( my $header, 3 ) || die 'unexpected end of data';
731 my $number_of_rectangles = unpack( 'xn', $header );
732  
733 # warn $number_of_rectangles;
734  
735 my $depth = $self->depth;
736  
737 my $big_endian = $self->_big_endian;
738 my $read_and_set_colour
739 = $depth == 24
740 ? (
741 $big_endian
742 ? \&_read_and_set_colour_24_be
743 : \&_read_and_set_colour_24_le
744 )
745 : $depth == 16 ? (
746 $big_endian
747 ? \&_read_and_set_colour_16_be
748 : \&_read_and_set_colour_16_le
749 )
750 : $depth == 8 ? \&_read_and_set_colour_8
751 : die 'unsupported depth';
752  
753 foreach ( 1 .. $number_of_rectangles ) {
754 $socket->read( my $data, 12 ) || die 'unexpected end of data';
755 my ( $x, $y, $w, $h, $encoding_type ) = unpack 'nnnnN', $data;
756  
757 # unsigned -> signed conversion
758 $encoding_type = unpack 'l', pack 'L', $encoding_type;
759  
760 # warn "$x,$y $w x $h $encoding_type";
761  
762 ### Raw encoding ###
763 if ( $encoding_type == 0 ) {
764  
765 if ( $CAN_CREATE_RAW_IMAGE
766 && $depth == 24
767 && $AM_BIG_ENDIAN == $self->_big_endian )
768 {
769  
770 # Performance boost: splat raw pixels into the image
771 $socket->read( my $data, $w * $h * 4 );
772 my $raw = Image::Imlib2->new_using_data( $w, $h, $data );
773 $raw->has_alpha(0);
774 $image->blend( $raw, 0, 0, 0, $w, $h, $x, $y, $w, $h );
775  
776 } else {
777  
778 for my $py ( $y .. $y + $h - 1 ) {
779 for my $px ( $x .. $x + $w - 1 ) {
780 $self->$read_and_set_colour();
781 $image->draw_point( $px, $py );
782 }
783 }
784  
785 }
786  
787 ### CopyRect encooding ###
788 } elsif ( $encoding_type == 1 ) {
789  
790 $socket->read( my $srcpos, 4 ) || die 'unexpected end of data';
791 my ( $srcx, $srcy ) = unpack 'nn', $srcpos;
792  
793 my $copy = $image->crop( $srcx, $srcy, $w, $h );
794 $image->blend( $copy, 0, 0, 0, $w, $h, $x, $y, $w, $h );
795  
796 ### RRE and CoRRE encodings ###
797 } elsif ( $encoding_type == 2 || $encoding_type == 4 ) {
798  
799 $socket->read( my $num_sub_rects, 4 )
800 || die 'unexpected end of data';
801 $num_sub_rects = unpack 'N', $num_sub_rects;
802  
803 $self->$read_and_set_colour();
804 $image->fill_rectangle( $x, $y, $w, $h );
805  
806 # RRE is U16, CoRRE is U8
807 my $geombytes = $encoding_type == 2 ? 8 : 4;
808 my $format = $encoding_type == 2 ? 'nnnn' : 'CCCC';
809  
810 for my $i ( 1 .. $num_sub_rects ) {
811  
812 $self->$read_and_set_colour();
813 $socket->read( my $subrect, $geombytes )
814 || die 'unexpected end of data';
815 my ( $sx, $sy, $sw, $sh ) = unpack $format, $subrect;
816 $image->fill_rectangle( $x + $sx, $y + $sy, $sw, $sh );
817  
818 }
819  
820 ### Hextile encoding ###
821 } elsif ( $encoding_type == 5 ) {
822  
823 my $maxx = $x + $w;
824 my $maxy = $y + $h;
825 my $background;
826 my $foreground;
827  
828 # Step over 16x16 tiles in the target rectangle
829 for ( my $ry = $y; $ry < $maxy; $ry += 16 ) {
830 my $rh = $maxy - $ry > 16 ? 16 : $maxy - $ry;
831 for ( my $rx = $x; $rx < $maxx; $rx += 16 ) {
832 my $rw = $maxx - $rx > 16 ? 16 : $maxx - $rx;
833 $socket->read( my $mask, 1 )
834 || die 'unexpected end of data';
835 $mask = unpack 'C', $mask;
836  
837 if ( $mask & 0x1 ) { # Raw tile
838 for my $py ( $ry .. $ry + $rh - 1 ) {
839 for my $px ( $rx .. $rx + $rw - 1 ) {
840 $self->$read_and_set_colour();
841 $image->draw_point( $px, $py );
842 }
843 }
844  
845 } else {
846  
847 if ( $mask & 0x2 ) { # background set
848 $background = $self->$read_and_set_colour();
849 }
850 if ( $mask & 0x4 ) { # foreground set
851 $foreground = $self->$read_and_set_colour();
852 }
853 if ( $mask & 0x8 ) { # has subrects
854  
855 $socket->read( my $nsubrects, 1 )
856 || die 'unexpected end of data';
857 $nsubrects = unpack 'C', $nsubrects;
858  
859 if ( !$mask & 0x10 ) { # use foreground colour
860 $image->set_colour( @{$foreground} );
861 }
862 for my $i ( 1 .. $nsubrects ) {
863 if ( $mask & 0x10 ) { # use per-subrect colour
864 $self->$read_and_set_colour();
865 }
866 $socket->read( my $pos, 1 )
867 || die 'unexpected end of data';
868 $pos = unpack 'C', $pos;
869 $socket->read( my $size, 1 )
870 || die 'unexpected end of data';
871 $size = unpack 'C', $size;
872 my $sx = $pos >> 4;
873 my $sy = $pos & 0xff;
874 my $sw = 1 + ( $size >> 4 );
875 my $sh = 1 + ( $size & 0xff );
876 $image->fill_rectangle( $rx + $sx, $ry + $sy,
877 $sw, $sh );
878 }
879  
880 } else { # no subrects
881 $image->set_colour( @{$background} );
882 $image->fill_rectangle( $rx, $ry, $rw, $rh );
883 }
884 }
885 }
886 }
887  
888 ### Cursor ###
889 } elsif ( $encoding_type == -239 ) {
890  
891 # realvnc 3.3 sends empty cursor messages, so skip
892 next unless $w || $h;
893  
894 my $cursordata = $self->_cursordata;
895 if ( !$cursordata ) {
896 $self->_cursordata( $cursordata = {} );
897 }
898 $cursordata->{image} = Image::Imlib2->new( $w, $h );
899 $cursordata->{hotspotx} = $x;
900 $cursordata->{hotspoty} = $y;
901 $cursordata->{width} = $w;
902 $cursordata->{height} = $h;
903  
904 my $cursor = $cursordata->{image}
905 || die "Failed to create cursor buffer $w x $h";
906 $cursor->has_alpha(1);
907  
908 my @pixbuf;
909 for my $i ( 1 .. $w * $h ) {
910 push @pixbuf, $self->$read_and_set_colour();
911 }
912 my $masksize = int( ( $w + 7 ) / 8 ) * $h;
913 my $maskrowsize = int( ( $w + 7 ) / 8 ) * 8;
914 $socket->read( my $mask, $masksize )
915 || die 'unexpected end of data';
916 $mask = unpack 'B*', $mask;
917  
918 #print "masksize: $masksize\n";
919 #print "maskrowsize: $maskrowsize\n";
920 #print "mask: $mask\n";
921  
922 #open my $fh, '>', $ENV{HOME}.'/Desktop/cursor.txt';
923 $cursor->will_blend(0) if ($CAN_CHANGE_BLEND);
924 for my $cy ( 0 .. $h - 1 ) {
925 for my $cx ( 0 .. $w - 1 ) {
926 my $pixel = shift @pixbuf;
927 $pixel || die 'not enough pixels';
928 if ( !substr( $mask, $cx + $cy * $maskrowsize, 1 ) ) {
929 @{$pixel} = ( 0, 0, 0, 0 );
930 }
931  
932 #print "$cx, $cy: @$pixel\n";
933 #print $fh "$cx, $cy: @$pixel\n";
934 $cursor->set_colour( @{$pixel} );
935 $cursor->draw_point( $cx, $cy );
936 }
937 }
938 $cursor->will_blend(1) if ($CAN_CHANGE_BLEND);
939  
940 #$cursor->save('vnccursor.png');
941 #print "wrote cursor\n";
942  
943 ### CursorPos ###
944 } elsif ( $encoding_type == -232 ) {
945  
946 my $cursordata = $self->_cursordata;
947 if ( !$cursordata ) {
948 $self->_cursordata( $cursordata = {} );
949 }
950 $cursordata->{x} = $x;
951 $cursordata->{y} = $y;
952  
953 #print "Cursor pos: $x, $y\n";
954  
955 } else {
956 die 'unsupported update encoding ' . $encoding_type;
957  
958 }
959 }
960  
961 return $number_of_rectangles;
962 }
963  
964 sub _read_and_set_colour_8 {
965 my $self = shift;
966  
967 $self->socket->read( my $pixel, 1 ) || die 'unexpected end of data';
968  
969 my $colours = $self->_colourmap;
970 my $index = unpack( 'C', $pixel );
971 my $colour = $colours->[$index];
972 my @colour = ( $colour->{r}, $colour->{g}, $colour->{b}, 255 );
973 $self->_framebuffer->set_colour(@colour);
974  
975 return \@colour;
976 }
977  
978 sub _read_and_set_colour_16_le {
979 my $self = shift;
980  
981 $self->socket->read( my $pixel, 2 ) || die 'unexpected end of data';
982 my $colour = unpack 'v', $pixel;
983 my @colour = (
984 ( $colour >> 10 & 31 ) << 3,
985 ( $colour >> 5 & 31 ) << 3,
986 ( $colour & 31 ) << 3, 255
987 );
988 $self->_framebuffer->set_colour(@colour);
989  
990 return \@colour;
991 }
992  
993 sub _read_and_set_colour_16_be {
994 my $self = shift;
995  
996 $self->socket->read( my $pixel, 2 ) || die 'unexpected end of data';
997 my $colour = unpack 'n', $pixel;
998 my @colour = (
999 ( $colour >> 10 & 31 ) << 3,
1000 ( $colour >> 5 & 31 ) << 3,
1001 ( $colour & 31 ) << 3, 255
1002 );
1003 $self->_framebuffer->set_colour(@colour);
1004  
1005 return \@colour;
1006 }
1007  
1008 sub _read_and_set_colour_24_le {
1009 my $self = shift;
1010  
1011 $self->socket->read( my $pixel, 4 ) || die 'unexpected end of data';
1012 my $colour = unpack 'V', $pixel;
1013 my @colour
1014 = ( $colour >> 16 & 255, $colour >> 8 & 255, $colour & 255, 255, );
1015 $self->_framebuffer->set_colour(@colour);
1016  
1017 return \@colour;
1018 }
1019  
1020 sub _read_and_set_colour_24_be {
1021 my $self = shift;
1022  
1023 $self->socket->read( my $pixel, 4 ) || die 'unexpected end of data';
1024 my $colour = unpack 'N', $pixel;
1025 my @colour
1026 = ( $colour >> 16 & 255, $colour >> 8 & 255, $colour & 255, 255, );
1027 $self->_framebuffer->set_colour(@colour);
1028  
1029 return \@colour;
1030 }
1031  
1032 # The following is the full version that supports all 8, 16, and 32
1033 # bpp and arbitrary pixel formats. This version is only used when one
1034 # of the faster functions declared above cannot be used due to
1035 # specific VNC settings.
1036  
1037 sub _read_and_set_colour {
1038 my $self = shift;
1039 my $pixel = shift;
1040  
1041 my $colours = $self->_colourmap;
1042 my $bytes_per_pixel = $self->_bpp / 8;
1043 if ( !$pixel ) {
1044 $self->socket->read( $pixel, $bytes_per_pixel )
1045 || die 'unexpected end of data';
1046 }
1047 my @colour;
1048 if ($colours) { # indexed colour, depth is 8
1049 my $index = unpack( 'C', $pixel );
1050 my $colour = $colours->[$index];
1051 @colour = ( $colour->{r}, $colour->{g}, $colour->{b}, 255 );
1052 } else { # true colour, depth is 24 or 16
1053 my $pixinfo = $self->_pixinfo;
1054 my $format
1055 = $bytes_per_pixel == 4 ? ( $self->_big_endian ? 'N' : 'V' )
1056 : $bytes_per_pixel == 2 ? ( $self->_big_endian ? 'n' : 'v' )
1057 : die 'Unsupported bits-per-pixel value';
1058 my $colour = unpack $format, $pixel;
1059 my $r = $colour >> $pixinfo->{red_shift} & $pixinfo->{red_max};
1060 my $g = $colour >> $pixinfo->{green_shift} & $pixinfo->{green_max};
1061 my $b = $colour >> $pixinfo->{blue_shift} & $pixinfo->{blue_max};
1062 if ( $bytes_per_pixel == 4 ) {
1063 @colour = ( $r, $g, $b, 255 );
1064 } else {
1065 @colour = (
1066 $r * 255 / $pixinfo->{red_max},
1067 $g * 255 / $pixinfo->{green_max},
1068 $b * 255 / $pixinfo->{blue_max}, 255
1069 );
1070 }
1071 }
1072 $self->_framebuffer->set_colour(@colour);
1073 return \@colour;
1074 }
1075  
1076 sub _receive_colour_map {
1077 my $self = shift;
1078  
1079 # set colour map entries
1080 my $socket = $self->socket;
1081 $socket->read( my $padding, 1 ) || die 'unexpected end of data';
1082 $socket->read( my $first_colour, 2 ) || die 'unexpected end of data';
1083 $first_colour = unpack( 'n', $first_colour );
1084 $socket->read( my $number_of_colours, 2 ) || die 'unexpected end of data';
1085 $number_of_colours = unpack( 'n', $number_of_colours );
1086  
1087 # warn "colours: $first_colour.. ($number_of_colours)";
1088  
1089 my @colours;
1090 foreach my $i ( $first_colour .. $first_colour + $number_of_colours - 1 )
1091 {
1092 $socket->read( my $r, 2 ) || die 'unexpected end of data';
1093 $r = unpack( 'n', $r );
1094 $socket->read( my $g, 2 ) || die 'unexpected end of data';
1095 $g = unpack( 'n', $g );
1096 $socket->read( my $b, 2 ) || die 'unexpected end of data';
1097 $b = unpack( 'n', $b );
1098  
1099 # warn "$i $r/$g/$b";
1100  
1101 # The 8-bit colours are in the top byte of each field
1102 $colours[$i] = { r => $r >> 8, g => $g >> 8, b => $b >> 8 };
1103 }
1104 $self->_colourmap( \@colours );
1105 return 1;
1106 }
1107  
1108 sub _receive_bell {
1109 my $self = shift;
1110  
1111 # And discard it...
1112  
1113 return 1;
1114 }
1115  
1116 sub _receive_cut_text {
1117 my $self = shift;
1118  
1119 my $socket = $self->socket;
1120 $socket->read( my $cut_msg, 7 ) || die 'unexpected end of data';
1121 my $cut_length = unpack 'xxxN', $cut_msg;
1122 $socket->read( my $cut_string, $cut_length )
1123 || die 'unexpected end of data';
1124  
1125 # And discard it...
1126  
1127 return 1;
1128 }
1129  
1130 sub mouse_move_to {
1131 my ( $self, $x, $y ) = @_;
1132 $self->send_pointer_event( 0, $x, $y );
1133  
1134 my $cursordata = $self->_cursordata;
1135 if ( !$cursordata ) {
1136 $self->_cursordata( $cursordata = {} );
1137 }
1138 $cursordata->{x} = $x;
1139 $cursordata->{y} = $y;
1140 }
1141  
1142 sub mouse_click {
1143 my ($self) = @_;
1144  
1145 my $cursordata = $self->_cursordata;
1146 if ( !$cursordata ) {
1147 $self->_cursordata( $cursordata = { x => 0, y => 0 } );
1148 }
1149  
1150 $self->send_pointer_event( 1, $cursordata->{x}, $cursordata->{y} );
1151 $self->send_pointer_event( 0, $cursordata->{x}, $cursordata->{y} );
1152 }
1153  
1154 sub mouse_right_click {
1155 my ($self) = @_;
1156  
1157 my $cursordata = $self->_cursordata;
1158 if ( !$cursordata ) {
1159 $self->_cursordata( $cursordata = { x => 0, y => 0 } );
1160 }
1161  
1162 $self->send_pointer_event( 4, $cursordata->{x}, $cursordata->{y} );
1163 $self->send_pointer_event( 0, $cursordata->{x}, $cursordata->{y} );
1164 }
1165  
1166 1;
1167  
1168 __END__
↑ Summary