@@ -69,8 +69,8 @@ class VncClient extends Events {
69
69
encodings . pseudoDesktopSize
70
70
] ;
71
71
72
- this . _audioChannels = options . audioChannels || 2 ;
73
- this . _audioFrequency = options . audioFrequency || 22050 ;
72
+ this . _audioChannels = options . audioChannels || 2 ;
73
+ this . _audioFrequency = options . audioFrequency || 22050 ;
74
74
75
75
this . _rects = 0 ;
76
76
this . _decoders = { } ;
@@ -553,12 +553,12 @@ class VncClient extends Events {
553
553
rect . height = this . _socketBuffer . readUInt16BE ( ) ;
554
554
rect . encoding = this . _socketBuffer . readInt32BE ( ) ;
555
555
556
- if ( rect . encoding === encodings . pseudoQemuAudio ) {
557
- this . sendAudio ( true ) ;
558
- this . sendAudioConfig ( this . _audioChannels , this . _audioFrequency ) ; //todo: future: setFrequency(...) to update mid thing
559
- } else if ( rect . encoding === encodings . pseudoQemuPointerMotionChange ) {
560
- this . _relativePointer = rect . x == 0 ;
561
- } else if ( rect . encoding === encodings . pseudoCursor ) {
556
+ if ( rect . encoding === encodings . pseudoQemuAudio ) {
557
+ this . sendAudio ( true ) ;
558
+ this . sendAudioConfig ( this . _audioChannels , this . _audioFrequency ) ; //todo: future: setFrequency(...) to update mid thing
559
+ } else if ( rect . encoding === encodings . pseudoQemuPointerMotionChange ) {
560
+ this . _relativePointer = rect . x == 0 ;
561
+ } else if ( rect . encoding === encodings . pseudoCursor ) {
562
562
const dataSize = rect . width * rect . height * ( this . pixelFormat . bitsPerPixel / 8 ) ;
563
563
const bitmaskSize = Math . floor ( ( rect . width + 7 ) / 8 ) * rect . height ;
564
564
this . _cursor . width = rect . width ;
@@ -644,18 +644,18 @@ class VncClient extends Events {
644
644
async _handleQemuAudio ( ) {
645
645
this . _socketBuffer . setOffset ( 2 ) ;
646
646
let operation = this . _socketBuffer . readUInt16BE ( ) ;
647
- if ( operation == 2 ) {
648
- const length = this . _socketBuffer . readUInt32BE ( ) ;
647
+ if ( operation == 2 ) {
648
+ const length = this . _socketBuffer . readUInt32BE ( ) ;
649
649
650
- //this._log(`Audio received. Length: ${length}.`);
650
+ //this._log(`Audio received. Length: ${length}.`);
651
651
652
- await this . _socketBuffer . waitBytes ( length ) ;
652
+ await this . _socketBuffer . waitBytes ( length ) ;
653
653
654
- let audioBuffer = [ ] ;
655
- for ( let i = 0 ; i < length / 2 ; i ++ ) audioBuffer . push ( this . _socketBuffer . readUInt16BE ( ) ) ;
654
+ let audioBuffer = [ ] ;
655
+ for ( let i = 0 ; i < length / 2 ; i ++ ) audioBuffer . push ( this . _socketBuffer . readUInt16BE ( ) ) ;
656
656
657
- this . _audioData = audioBuffer ;
658
- }
657
+ this . _audioData = audioBuffer ;
658
+ }
659
659
660
660
this . emit ( 'audioStream' , this . _audioData ) ;
661
661
this . _socketBuffer . flush ( ) ;
@@ -684,8 +684,8 @@ class VncClient extends Events {
684
684
685
685
this . _password = '' ;
686
686
687
- this . _audioChannels = 2 ;
688
- this . _audioFrequency = 22050 ;
687
+ this . _audioChannels = 2 ;
688
+ this . _audioFrequency = 22050 ;
689
689
690
690
this . _handshaked = false ;
691
691
@@ -731,9 +731,54 @@ class VncClient extends Events {
731
731
x : 0 ,
732
732
y : 0 ,
733
733
cursorPixels : null ,
734
- bitmask : null
734
+ bitmask : null ,
735
+ posX : 0 ,
736
+ posY : 0
737
+ }
738
+
739
+ }
740
+
741
+ /**
742
+ * Get the frame buffer with de cursor printed to it if using Cursor Pseudo Encoding
743
+ * @returns {null|Buffer|* }
744
+ */
745
+ getFb ( ) {
746
+ if ( ! this . _cursor . width ) {
747
+ // If there is no cursor, just return de framebuffer
748
+ return this . fb ;
749
+ } else {
750
+ // If there is a cursor, draw a cursor on the framebuffer and return the final result
751
+ const tempFb = new Buffer . from ( this . fb ) ;
752
+ for ( let h = 0 ; h < this . _cursor . height ; h ++ ) {
753
+ for ( let w = 0 ; w < this . _cursor . width ; w ++ ) {
754
+ const fbBytePosOffset = this . _getPixelBytePos ( this . _cursor . posX + w , this . _cursor . posY + h , this . clientWidth , this . clientHeight ) ;
755
+ const cursorBytePosOffset = this . _getPixelBytePos ( w , h , this . _cursor . width , this . _cursor . height ) ;
756
+ const bitmapByte = this . _cursor . bitmask . slice ( Math . floor ( cursorBytePosOffset / 4 / 8 ) , Math . floor ( cursorBytePosOffset / 4 / 8 ) + 1 ) ;
757
+ const bitmapBit = ( cursorBytePosOffset / 4 ) % 8 ;
758
+ const activePixel = bitmapBit === 0 ? bitmapByte [ 0 ] & 128 : bitmapBit === 1 ? bitmapByte [ 0 ] & 64 :
759
+ bitmapBit === 2 ? bitmapByte [ 0 ] & 32 : bitmapBit === 3 ? bitmapByte [ 0 ] & 16 :
760
+ bitmapBit === 4 ? bitmapByte [ 0 ] & 8 : bitmapBit === 5 ? bitmapByte [ 0 ] & 4 :
761
+ bitmapBit === 6 ? bitmapByte [ 0 ] & 2 : bitmapByte [ 0 ] & 1 ;
762
+
763
+ if ( activePixel ) {
764
+ if ( this . pixelFormat . bitsPerPixel === 8 ) {
765
+ const index = this . _cursor . cursorPixels . readUInt8 ( cursorBytePosOffset ) ;
766
+ const color = this . _colorMap [ index ] ;
767
+ tempFb . writeIntBE ( color , fbBytePosOffset , 4 ) ;
768
+ } else {
769
+ const bytesLength = this . pixelFormat . bitsPerPixel / 8 ;
770
+ const color = this . _cursor . cursorPixels . readIntBE ( cursorBytePosOffset , bytesLength ) ;
771
+ tempFb . writeIntBE ( color , fbBytePosOffset , bytesLength ) ;
772
+ }
773
+ }
774
+ }
775
+ }
776
+ return tempFb ;
735
777
}
778
+ }
736
779
780
+ _getPixelBytePos ( x , y , width , height ) {
781
+ return ( ( y * width ) + x ) * 4 ;
737
782
}
738
783
739
784
/**
@@ -744,7 +789,7 @@ class VncClient extends Events {
744
789
sendKeyEvent ( key , down = false ) {
745
790
746
791
const message = new Buffer ( 8 ) ;
747
- message . writeUInt8 ( 4 ) ; // Message type
792
+ message . writeUInt8 ( clientMsgTypes . keyEvent ) ; // Message type
748
793
message . writeUInt8 ( down ? 1 : 0 , 1 ) ; // Down flag
749
794
message . writeUInt8 ( 0 , 2 ) ; // Padding
750
795
message . writeUInt8 ( 0 , 3 ) ; // Padding
@@ -784,11 +829,14 @@ class VncClient extends Events {
784
829
buttonMask += button1 ? 1 : 0 ;
785
830
786
831
const message = new Buffer ( 6 ) ;
787
- message . writeUInt8 ( 5 ) ; // Message type
832
+ message . writeUInt8 ( clientMsgTypes . pointerEvent ) ; // Message type
788
833
message . writeUInt8 ( buttonMask , 1 ) ; // Button Mask
789
- const reladd = this . _relativePointer ?0x7FFF :0 ;
790
- message . writeUInt16BE ( xPosition + reladd , 2 ) ; // X Position
791
- message . writeUInt16BE ( yPosition + reladd , 4 ) ; // Y Position
834
+ const reladd = this . _relativePointer ? 0x7FFF : 0 ;
835
+ message . writeUInt16BE ( xPosition + reladd , 2 ) ; // X Position
836
+ message . writeUInt16BE ( yPosition + reladd , 4 ) ; // Y Position
837
+
838
+ this . _cursor . posX = xPosition ;
839
+ this . _cursor . posY = yPosition ;
792
840
793
841
this . sendData ( message , false ) ;
794
842
@@ -802,7 +850,7 @@ class VncClient extends Events {
802
850
803
851
const textBuffer = new Buffer . from ( text , 'latin1' ) ;
804
852
const message = new Buffer ( 8 + textBuffer . length ) ;
805
- message . writeUInt8 ( 6 ) ; // Message type
853
+ message . writeUInt8 ( clientMsgTypes . cutText ) ; // Message type
806
854
message . writeUInt8 ( 0 , 1 ) ; // Padding
807
855
message . writeUInt8 ( 0 , 2 ) ; // Padding
808
856
message . writeUInt8 ( 0 , 3 ) ; // Padding
@@ -817,8 +865,8 @@ class VncClient extends Events {
817
865
const message = new Buffer ( 4 ) ;
818
866
message . writeUInt8 ( clientMsgTypes . qemuAudio ) ; // Message type
819
867
message . writeUInt8 ( 1 , 1 ) ; // Submessage Type
820
- message . writeUInt16BE ( enable ? 0 : 1 , 2 ) ; // Operation
821
- this . _connection . write ( message ) ;
868
+ message . writeUInt16BE ( enable ? 0 : 1 , 2 ) ; // Operation
869
+ this . sendData ( message ) ;
822
870
}
823
871
824
872
sendAudioConfig ( channels , frequency ) {
@@ -829,7 +877,7 @@ class VncClient extends Events {
829
877
message . writeUInt8 ( 0 /*U8*/ , 4 ) ; // Sample Format
830
878
message . writeUInt8 ( channels , 5 ) ; // Number of Channels
831
879
message . writeUInt32BE ( frequency , 6 ) ; // Frequency
832
- this . _connection . write ( message ) ;
880
+ this . sendData ( message ) ;
833
881
}
834
882
835
883
/**
0 commit comments