From f6acfdfdad844a293a5670d8f719cb3ec5c31368 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 14 Jul 2017 02:01:24 +0300 Subject: [PATCH] add ability to check supported status Signed-off-by: Vasiliy Tolstov --- encoding.go | 1 + encoding_atenhermon.go | 19 ++++++++++++++ encoding_copyrect.go | 6 ++++- encoding_cursor.go | 4 +++ encoding_desktopname.go | 4 +++ encoding_desktopsize.go | 4 +++ encoding_raw.go | 4 +++ encoding_tight.go | 4 +++ encoding_tightpng.go | 4 +++ encoding_xcursor.go | 4 +++ image.go | 11 ++++---- messages.go | 42 +++++++++++++++++++++++++++++++ messages_aten.go | 56 +++++++++++++++++++++++++++++++++++++++++ 13 files changed, 157 insertions(+), 6 deletions(-) diff --git a/encoding.go b/encoding.go index 734ccae..55d7b29 100644 --- a/encoding.go +++ b/encoding.go @@ -83,6 +83,7 @@ type Encoding interface { Type() EncodingType Read(Conn, *Rectangle) error Write(Conn, *Rectangle) error + Supported(Conn) bool } func setBit(n uint8, pos uint8) uint8 { diff --git a/encoding_atenhermon.go b/encoding_atenhermon.go index 21a62ac..b7b440e 100644 --- a/encoding_atenhermon.go +++ b/encoding_atenhermon.go @@ -28,6 +28,10 @@ type AtenHermonSubrect struct { Data []byte } +func (*AtenHermon) Supported(Conn) bool { + return false +} + func (*AtenHermon) Type() EncodingType { return EncAtenHermon } func (enc *AtenHermon) Read(c Conn, rect *Rectangle) error { @@ -113,6 +117,14 @@ func (enc *AtenHermon) Read(c Conn, rect *Rectangle) error { } func (enc *AtenHermon) Write(c Conn, rect *Rectangle) error { + if !enc.Supported(c) { + for _, ew := range enc.Encodings { + if err := ew.Write(c, rect); err != nil { + return err + } + } + return nil + } var pad4 [4]byte if err := binary.Write(c, binary.BigEndian, pad4); err != nil { @@ -148,6 +160,10 @@ func (enc *AtenHermon) Write(c Conn, rect *Rectangle) error { return nil } +func (*AtenHermonSubrect) Supported(Conn) bool { + return false +} + func (enc *AtenHermonSubrect) Type() EncodingType { return EncAtenHermonSubrect } @@ -173,6 +189,9 @@ func (enc *AtenHermonSubrect) Read(c Conn, rect *Rectangle) error { } func (enc *AtenHermonSubrect) Write(c Conn, rect *Rectangle) error { + if !enc.Supported(c) { + return nil + } if err := binary.Write(c, binary.BigEndian, enc.A); err != nil { return err } diff --git a/encoding_copyrect.go b/encoding_copyrect.go index c73ded1..9dd510f 100644 --- a/encoding_copyrect.go +++ b/encoding_copyrect.go @@ -6,7 +6,11 @@ type CopyRectEncoding struct { SX, SY uint16 } -func (CopyRectEncoding) Type() EncodingType { return EncCopyRect } +func (*CopyRectEncoding) Supported(Conn) bool { + return true +} + +func (*CopyRectEncoding) Type() EncodingType { return EncCopyRect } func (enc *CopyRectEncoding) Read(c Conn, rect *Rectangle) error { if err := binary.Read(c, binary.BigEndian, &enc.SX); err != nil { diff --git a/encoding_cursor.go b/encoding_cursor.go index 5d35d28..c09c24a 100644 --- a/encoding_cursor.go +++ b/encoding_cursor.go @@ -7,6 +7,10 @@ type CursorPseudoEncoding struct { BitMask []byte } +func (*CursorPseudoEncoding) Supported(Conn) bool { + return true +} + func (*CursorPseudoEncoding) Type() EncodingType { return EncCursorPseudo } func (enc *CursorPseudoEncoding) Read(c Conn, rect *Rectangle) error { diff --git a/encoding_desktopname.go b/encoding_desktopname.go index dc8e620..20be716 100644 --- a/encoding_desktopname.go +++ b/encoding_desktopname.go @@ -7,6 +7,10 @@ type DesktopNamePseudoEncoding struct { Name []byte } +func (*DesktopNamePseudoEncoding) Supported(Conn) bool { + return true +} + func (*DesktopNamePseudoEncoding) Type() EncodingType { return EncDesktopNamePseudo } // Read implements the Encoding interface. diff --git a/encoding_desktopsize.go b/encoding_desktopsize.go index d8a44a1..f2db3c2 100644 --- a/encoding_desktopsize.go +++ b/encoding_desktopsize.go @@ -3,6 +3,10 @@ package vnc // DesktopSizePseudoEncoding represents a desktop size message from the server. type DesktopSizePseudoEncoding struct{} +func (*DesktopSizePseudoEncoding) Supported(Conn) bool { + return true +} + func (*DesktopSizePseudoEncoding) Type() EncodingType { return EncDesktopSizePseudo } // Read implements the Encoding interface. diff --git a/encoding_raw.go b/encoding_raw.go index 29acce2..ab7d6c6 100644 --- a/encoding_raw.go +++ b/encoding_raw.go @@ -4,6 +4,10 @@ type RawEncoding struct { Colors []Color } +func (*RawEncoding) Supported(Conn) bool { + return true +} + func (enc *RawEncoding) Write(c Conn, rect *Rectangle) error { var err error diff --git a/encoding_tight.go b/encoding_tight.go index 4447203..9b199ce 100644 --- a/encoding_tight.go +++ b/encoding_tight.go @@ -28,6 +28,10 @@ const ( type TightEncoding struct{} +func (*TightEncoding) Supported(Conn) bool { + return true +} + func (*TightEncoding) Type() EncodingType { return EncTight } func (enc *TightEncoding) Write(c Conn, rect *Rectangle) error { diff --git a/encoding_tightpng.go b/encoding_tightpng.go index fde88ad..a3c3185 100644 --- a/encoding_tightpng.go +++ b/encoding_tightpng.go @@ -11,6 +11,10 @@ import ( "io" ) +func (*TightPngEncoding) Supported(Conn) bool { + return true +} + func (enc *TightPngEncoding) Write(c Conn, rect *Rectangle) error { if err := writeTightCC(c, enc.TightCC); err != nil { return err diff --git a/encoding_xcursor.go b/encoding_xcursor.go index 796ca34..310450e 100644 --- a/encoding_xcursor.go +++ b/encoding_xcursor.go @@ -16,6 +16,10 @@ type XCursorPseudoEncoding struct { Bitmask []byte } +func (*XCursorPseudoEncoding) Supported(Conn) bool { + return true +} + func (*XCursorPseudoEncoding) Type() EncodingType { return EncXCursorPseudo } // Read implements the Encoding interface. diff --git a/image.go b/image.go index 3246d5d..1e345f4 100644 --- a/image.go +++ b/image.go @@ -50,15 +50,16 @@ func NewRectangle() *Rectangle { // Write marshal color to conn func (clr *Color) Write(c Conn) error { var err error - order := clr.pf.order() + pf := c.PixelFormat() + order := pf.order() pixel := clr.cmIndex if clr.pf.TrueColor != 0 { - pixel = uint32(clr.R) << clr.pf.RedShift - pixel |= uint32(clr.G) << clr.pf.GreenShift - pixel |= uint32(clr.B) << clr.pf.BlueShift + pixel = uint32(clr.R) << pf.RedShift + pixel |= uint32(clr.G) << pf.GreenShift + pixel |= uint32(clr.B) << pf.BlueShift } - switch clr.pf.BPP { + switch pf.BPP { case 8: err = binary.Write(c, order, byte(pixel)) case 16: diff --git a/messages.go b/messages.go index ff191c1..28ba9ae 100644 --- a/messages.go +++ b/messages.go @@ -70,6 +70,7 @@ type ClientMessage interface { Type() ClientMessageType Read(Conn) (ClientMessage, error) Write(Conn) error + Supported(Conn) bool } type ServerMessage interface { @@ -77,6 +78,7 @@ type ServerMessage interface { Type() ServerMessageType Read(Conn) (ServerMessage, error) Write(Conn) error + Supported(Conn) bool } // FramebufferUpdate holds a FramebufferUpdate wire format message. @@ -91,6 +93,10 @@ func (msg *FramebufferUpdate) String() string { return fmt.Sprintf("rects %d rectangle[]: { %v }", msg.NumRect, msg.Rects) } +func (msg *FramebufferUpdate) Supported(c Conn) bool { + return true +} + // Type return MessageType func (*FramebufferUpdate) Type() ServerMessageType { return FramebufferUpdateMsgType @@ -144,6 +150,10 @@ type ServerCutText struct { Text []byte } +func (msg *ServerCutText) Supported(c Conn) bool { + return true +} + // String returns string func (msg *ServerCutText) String() string { return fmt.Sprintf("lenght: %d text: %s", msg.Length, msg.Text) @@ -200,6 +210,10 @@ func (msg *ServerCutText) Write(c Conn) error { // Bell server message type Bell struct{} +func (*Bell) Supported(c Conn) bool { + return true +} + // String return string func (*Bell) String() string { return fmt.Sprintf("bell") @@ -231,6 +245,10 @@ type SetColorMapEntries struct { Colors []Color } +func (msg *SetColorMapEntries) Supported(c Conn) bool { + return true +} + // String returns string func (msg *SetColorMapEntries) String() string { return fmt.Sprintf("first color: %d, numcolors: %d, colors[]: { %v }", msg.FirstColor, msg.ColorsNum, msg.Colors) @@ -308,6 +326,10 @@ type SetPixelFormat struct { PF PixelFormat // pixel-format } +func (msg *SetPixelFormat) Supported(c Conn) bool { + return true +} + // String returns string func (msg *SetPixelFormat) String() string { return fmt.Sprintf("%s", msg.PF) @@ -353,6 +375,10 @@ type SetEncodings struct { Encodings []EncodingType } +func (msg *SetEncodings) Supported(c Conn) bool { + return true +} + // String return string func (msg *SetEncodings) String() string { return fmt.Sprintf("encnum: %d, encodings[]: { %v }", msg.EncNum, msg.Encodings) @@ -417,6 +443,10 @@ type FramebufferUpdateRequest struct { Width, Height uint16 // width, height } +func (msg *FramebufferUpdateRequest) Supported(c Conn) bool { + return true +} + // String returns string func (msg *FramebufferUpdateRequest) String() string { return fmt.Sprintf("incremental: %d, x: %d, y: %d, width: %d, height: %d", msg.Inc, msg.X, msg.Y, msg.Width, msg.Height) @@ -454,6 +484,10 @@ type KeyEvent struct { Key Key // key } +func (msg *KeyEvent) Supported(c Conn) bool { + return true +} + // String returns string func (msg *KeyEvent) String() string { return fmt.Sprintf("down: %d, key: %v", msg.Down, msg.Key) @@ -490,6 +524,10 @@ type PointerEvent struct { X, Y uint16 // x-, y-position } +func (msg *PointerEvent) Supported(c Conn) bool { + return true +} + // String returns string func (msg *PointerEvent) String() string { return fmt.Sprintf("mask %d, x: %d, y: %d", msg.Mask, msg.X, msg.Y) @@ -527,6 +565,10 @@ type ClientCutText struct { Text []byte } +func (msg *ClientCutText) Supported(c Conn) bool { + return true +} + // String returns string func (msg *ClientCutText) String() string { return fmt.Sprintf("length: %d, text: %s", msg.Length, msg.Text) diff --git a/messages_aten.go b/messages_aten.go index 93c02ea..d8bb800 100644 --- a/messages_aten.go +++ b/messages_aten.go @@ -39,6 +39,10 @@ type AteniKVMPointerEvent struct { _ [11]byte // padding } +func (msg *AteniKVMPointerEvent) Supported(c Conn) bool { + return false +} + func (msg *AteniKVMPointerEvent) String() string { return fmt.Sprintf("mask: %d, x:%d, y:%d", msg.Mask, msg.X, msg.Y) } @@ -56,6 +60,9 @@ func (*AteniKVMPointerEvent) Read(c Conn) (ClientMessage, error) { } func (msg *AteniKVMPointerEvent) Write(c Conn) error { + if !msg.Supported(c) { + return nil + } if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil { return err } @@ -65,6 +72,10 @@ func (msg *AteniKVMPointerEvent) Write(c Conn) error { return c.Flush() } +func (msg *AteniKVMKeyEvent) Supported(c Conn) bool { + return false +} + func (msg *AteniKVMKeyEvent) String() string { return fmt.Sprintf("down:%d, key:%s", msg.Down, msg.Key) } @@ -82,6 +93,9 @@ func (*AteniKVMKeyEvent) Read(c Conn) (ClientMessage, error) { } func (msg *AteniKVMKeyEvent) Write(c Conn) error { + if !msg.Supported(c) { + return nil + } if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil { return err } @@ -96,6 +110,10 @@ type AteniKVMFrontGroundEvent struct { _ [20]byte } +func (msg *AteniKVMFrontGroundEvent) Supported(c Conn) bool { + return false +} + // String return string representation func (msg *AteniKVMFrontGroundEvent) String() string { return fmt.Sprintf("%s", msg.Type()) @@ -118,6 +136,9 @@ func (*AteniKVMFrontGroundEvent) Read(c Conn) (ServerMessage, error) { // Write marshal message to conn func (msg *AteniKVMFrontGroundEvent) Write(c Conn) error { + if !msg.Supported(c) { + return nil + } var pad [20]byte if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil { return err @@ -133,6 +154,10 @@ type AteniKVMKeepAliveEvent struct { _ [1]byte } +func (msg *AteniKVMKeepAliveEvent) Supported(c Conn) bool { + return false +} + // String return string representation func (msg *AteniKVMKeepAliveEvent) String() string { return fmt.Sprintf("%s", msg.Type()) @@ -155,6 +180,9 @@ func (*AteniKVMKeepAliveEvent) Read(c Conn) (ServerMessage, error) { // Write marshal message to conn func (msg *AteniKVMKeepAliveEvent) Write(c Conn) error { + if !msg.Supported(c) { + return nil + } var pad [1]byte if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil { return err @@ -170,6 +198,10 @@ type AteniKVMVideoGetInfo struct { _ [20]byte } +func (msg *AteniKVMVideoGetInfo) Supported(c Conn) bool { + return false +} + // String return string representation func (msg *AteniKVMVideoGetInfo) String() string { return fmt.Sprintf("%s", msg.Type()) @@ -192,6 +224,9 @@ func (*AteniKVMVideoGetInfo) Read(c Conn) (ServerMessage, error) { // Write marshal message to conn func (msg *AteniKVMVideoGetInfo) Write(c Conn) error { + if !msg.Supported(c) { + return nil + } var pad [4]byte if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil { return err @@ -207,6 +242,10 @@ type AteniKVMMouseGetInfo struct { _ [2]byte } +func (msg *AteniKVMMouseGetInfo) Supported(c Conn) bool { + return false +} + // String return string representation func (msg *AteniKVMMouseGetInfo) String() string { return fmt.Sprintf("%s", msg.Type()) @@ -229,6 +268,9 @@ func (*AteniKVMMouseGetInfo) Read(c Conn) (ServerMessage, error) { // Write marshal message to conn func (msg *AteniKVMMouseGetInfo) Write(c Conn) error { + if !msg.Supported(c) { + return nil + } var pad [2]byte if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil { return err @@ -244,6 +286,10 @@ type AteniKVMSessionMessage struct { _ [264]byte } +func (msg *AteniKVMSessionMessage) Supported(c Conn) bool { + return false +} + // String return string representation func (msg *AteniKVMSessionMessage) String() string { return fmt.Sprintf("%s", msg.Type()) @@ -266,6 +312,9 @@ func (*AteniKVMSessionMessage) Read(c Conn) (ServerMessage, error) { // Write marshal message to conn func (msg *AteniKVMSessionMessage) Write(c Conn) error { + if !msg.Supported(c) { + return nil + } var pad [264]byte if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil { return err @@ -281,6 +330,10 @@ type AteniKVMGetViewerLang struct { _ [8]byte } +func (msg *AteniKVMGetViewerLang) Supported(c Conn) bool { + return false +} + // String return string representation func (msg *AteniKVMGetViewerLang) String() string { return fmt.Sprintf("%s", msg.Type()) @@ -303,6 +356,9 @@ func (*AteniKVMGetViewerLang) Read(c Conn) (ServerMessage, error) { // Write marshal message to conn func (msg *AteniKVMGetViewerLang) Write(c Conn) error { + if !msg.Supported(c) { + return nil + } var pad [8]byte if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil { return err