2017-06-12 14:11:23 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-06-13 01:52:07 +03:00
|
|
|
"image"
|
2017-06-12 14:11:23 +03:00
|
|
|
"log"
|
2017-06-13 01:52:07 +03:00
|
|
|
"math"
|
2017-06-12 14:11:23 +03:00
|
|
|
"net"
|
2017-06-13 01:52:07 +03:00
|
|
|
"time"
|
2017-06-12 14:11:23 +03:00
|
|
|
|
2018-12-18 00:31:40 +03:00
|
|
|
vnc "github.com/unistack-org/go-rfb"
|
2017-06-12 14:11:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-01-21 07:57:19 +03:00
|
|
|
ln, err := net.Listen("tcp", ":6900")
|
2017-06-12 14:11:23 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error listen. %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chServer := make(chan vnc.ClientMessage)
|
|
|
|
chClient := make(chan vnc.ServerMessage)
|
|
|
|
|
2017-06-13 01:52:07 +03:00
|
|
|
im := image.NewRGBA(image.Rect(0, 0, width, height))
|
2020-01-21 07:57:19 +03:00
|
|
|
tick := time.NewTicker(time.Millisecond * 2)
|
2017-06-13 01:52:07 +03:00
|
|
|
defer tick.Stop()
|
2020-01-21 07:57:19 +03:00
|
|
|
connected := false
|
2017-06-13 01:52:07 +03:00
|
|
|
|
2017-06-12 14:11:23 +03:00
|
|
|
cfg := &vnc.ServerConfig{
|
2020-01-21 07:57:19 +03:00
|
|
|
Width: width,
|
|
|
|
Height: height,
|
2018-12-18 00:31:40 +03:00
|
|
|
Handlers: vnc.DefaultServerHandlers,
|
|
|
|
SecurityHandlers: []vnc.SecurityHandler{&vnc.ClientAuthNone{}},
|
|
|
|
Encodings: []vnc.Encoding{&vnc.RawEncoding{}},
|
|
|
|
PixelFormat: vnc.PixelFormat32bit,
|
|
|
|
ClientMessageCh: chServer,
|
|
|
|
ServerMessageCh: chClient,
|
|
|
|
Messages: vnc.DefaultClientMessages,
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
go vnc.Serve(context.Background(), ln, cfg)
|
2020-01-21 07:57:19 +03:00
|
|
|
anim := 0
|
2017-06-12 14:11:23 +03:00
|
|
|
// Process messages coming in on the ClientMessage channel.
|
|
|
|
for {
|
2017-06-13 01:52:07 +03:00
|
|
|
select {
|
|
|
|
case <-tick.C:
|
2020-01-21 07:57:19 +03:00
|
|
|
if !connected {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
drawImage(im, anim*2)
|
|
|
|
anim++
|
|
|
|
colors := make([]vnc.Color, 0, 0)
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
r, g, b, a := im.At(x, y).RGBA()
|
2020-01-23 02:04:27 +03:00
|
|
|
clr := rgbaToColor(cfg.PixelFormat, r, g, b, a)
|
2020-01-21 07:57:19 +03:00
|
|
|
colors = append(colors, *clr)
|
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2020-01-21 07:57:19 +03:00
|
|
|
cfg.ServerMessageCh <- &vnc.FramebufferUpdate{
|
|
|
|
NumRect: 1,
|
|
|
|
Rects: []*vnc.Rectangle{
|
|
|
|
&vnc.Rectangle{
|
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
|
|
|
Width: width,
|
|
|
|
Height: height,
|
|
|
|
EncType: vnc.EncRaw,
|
|
|
|
Enc: &vnc.RawEncoding{
|
|
|
|
Colors: colors,
|
|
|
|
},
|
|
|
|
}}}
|
|
|
|
/*
|
|
|
|
case msg := <-chClient:
|
|
|
|
switch msg.Type() {
|
|
|
|
case vnc.FramebufferUpdateMsgType:
|
|
|
|
connected = true
|
|
|
|
log.Printf("11 Received message type:%v msg:%v\n", msg.Type(), msg)
|
|
|
|
default:
|
|
|
|
log.Printf("11 Received message type:%v msg:%v\n", msg.Type(), msg)
|
|
|
|
}*/
|
2017-06-13 01:52:07 +03:00
|
|
|
case msg := <-chServer:
|
|
|
|
switch msg.Type() {
|
2020-01-21 07:57:19 +03:00
|
|
|
case vnc.FramebufferUpdateRequestMsgType:
|
|
|
|
connected = true
|
2017-06-13 01:52:07 +03:00
|
|
|
default:
|
|
|
|
log.Printf("22 Received message type:%v msg:%v\n", msg.Type(), msg)
|
|
|
|
}
|
2020-01-21 07:57:19 +03:00
|
|
|
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
width = 800
|
|
|
|
height = 600
|
|
|
|
)
|
|
|
|
|
2020-01-21 07:57:19 +03:00
|
|
|
func rgbaToColor(pf *vnc.PixelFormat, r uint32, g uint32, b uint32, a uint32) *vnc.Color {
|
|
|
|
// fix converting rbga to rgb http://marcodiiga.github.io/rgba-to-rgb-conversion
|
|
|
|
clr := vnc.NewColor(pf, nil)
|
|
|
|
clr.R = uint16(r / 257)
|
|
|
|
clr.G = uint16(g / 257)
|
|
|
|
clr.B = uint16(b / 257)
|
|
|
|
return clr
|
|
|
|
}
|
|
|
|
|
2017-06-13 01:52:07 +03:00
|
|
|
func drawImage(im *image.RGBA, anim int) {
|
|
|
|
pos := 0
|
|
|
|
const border = 50
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
var r, g, b uint8
|
|
|
|
switch {
|
|
|
|
case x < border*2.5 && x < int((1.1+math.Sin(float64(y+anim*2)/40))*border):
|
|
|
|
r = 255
|
|
|
|
case x > width-border*2.5 && x > width-int((1.1+math.Sin(math.Pi+float64(y+anim*2)/40))*border):
|
|
|
|
g = 255
|
|
|
|
case y < border*2.5 && y < int((1.1+math.Sin(float64(x+anim*2)/40))*border):
|
|
|
|
r, g = 255, 255
|
|
|
|
case y > height-border*2.5 && y > height-int((1.1+math.Sin(math.Pi+float64(x+anim*2)/40))*border):
|
|
|
|
b = 255
|
|
|
|
default:
|
|
|
|
r, g, b = uint8(x+anim), uint8(y+anim), uint8(x+y+anim*3)
|
|
|
|
}
|
|
|
|
im.Pix[pos] = r
|
|
|
|
im.Pix[pos+1] = g
|
|
|
|
im.Pix[pos+2] = b
|
|
|
|
pos += 4 // skipping alpha
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|