func main()

in cmd/vncviewer/main.go [210:293]


func main() {
	if len(os.Args) != 2 {
		log.Fatalf("Usage: %v <vnc address>", path.Base(os.Args[0]))
	}

	fmt.Println("creating session")
	s := gymvnc.NewVNCSession("", gymvnc.VNCSessionConfig{
		Address:  os.Args[1],
		Password: "openai",
		Encoding: "tight",
	})

	gl.StartDriver(func(driver gxui.Driver) {
		theme := flags.CreateTheme(driver)
		img := theme.CreateImage()
		window := theme.CreateWindow(1000, 1000, "go-vncdriver vnc viewer")
		window.AddChild(img)
		window.OnClose(driver.Terminate)
		var events struct {
			sync.Mutex
			slice []gymvnc.VNCEvent
		}
		onEvent := func(e gymvnc.VNCEvent) {
			events.Lock()
			defer events.Unlock()
			events.slice = append(events.slice, e)
		}
		window.OnKeyDown(func(e gxui.KeyboardEvent) {
			onEvent(gymvnc.KeyEvent{
				Keysym: mapKey(e.Key),
				Down:   true,
			})
		})
		window.OnKeyUp(func(e gxui.KeyboardEvent) {
			onEvent(gymvnc.KeyEvent{
				Keysym: mapKey(e.Key),
				Down:   false,
			})
		})
		handleMouseEvent := func(e gxui.MouseEvent) {
			var btnMask vncclient.ButtonMask
			if e.State.IsDown(gxui.MouseButtonLeft) {
				btnMask |= vncclient.ButtonLeft
			}
			if e.State.IsDown(gxui.MouseButtonMiddle) {
				btnMask |= vncclient.ButtonMiddle
			}
			if e.State.IsDown(gxui.MouseButtonRight) {
				btnMask |= vncclient.ButtonRight
			}
			onEvent(gymvnc.PointerEvent{
				X:    uint16(e.Point.X),
				Y:    uint16(e.Point.Y),
				Mask: btnMask,
			})
		}
		img.OnMouseDown(handleMouseEvent)
		img.OnMouseUp(handleMouseEvent)
		img.OnMouseMove(handleMouseEvent)

		go func() {
			for {
				fmt.Println("tick")
				time.Sleep(time.Second / 60)
				events.Lock()
				screen, updates, err := s.Step(events.slice)
				events.slice = events.slice[:0]
				events.Unlock()
				if err != nil {
					log.Fatal(err)
				}
				if len(updates) == 0 {
					continue
				}
				source := toImage(screen)
				rgba := image.NewRGBA(source.Bounds())
				draw.Draw(rgba, source.Bounds(), source, image.ZP, draw.Src)
				driver.Call(func() {
					img.SetTexture(driver.CreateTexture(rgba, 1))
				})
			}
		}()
	})
}