Skip to content

Commit

Permalink
cmd/occamy-gui: add gio client
Browse files Browse the repository at this point in the history
Updates #47
  • Loading branch information
changkun committed Dec 7, 2021
1 parent 44d2251 commit 0e88923
Show file tree
Hide file tree
Showing 12 changed files with 1,394 additions and 3 deletions.
10 changes: 10 additions & 0 deletions cmd/occamy-gui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Occamy GUI

This is a minimum example of building an occamy client.

It is based on the [bring](https://github.com/deluan/bring) implementation of the Guacamole protocol.

Note that `bring` is a software renderer that does not utilize Gio's GPU backend.
Hence there is a lot of improvements for building this application.

Later coding will revise the code into a gio accelerated rendering.
114 changes: 114 additions & 0 deletions cmd/occamy-gui/occamy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2021 Changkun Ou. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package main

import (
"image"
"log"
"os"

"changkun.de/x/occamy/internal/guac"

"gioui.org/app"
"gioui.org/io/event"
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/io/system"
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/unit"
)

func main() {
if len(os.Args) < 3 {
log.Fatal("Usage: occamy-gui <vnc|rdp> host:port")
return
}

a, err := NewApp(os.Args[1], os.Args[2])
if err != nil {
log.Fatalf("cannot create Occamy client: %v", err)
}
go a.Run()
app.Main()
}

type App struct {
client *guac.Client
win *app.Window
}

func NewApp(protocol, addr string) (a *App, err error) {
log.SetPrefix("occamy: ")
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)

a = &App{}
a.win = app.NewWindow(app.Title("Occamy GUI Client"))
a.client, err = guac.NewClient("0.0.0.0:5636", map[string]string{
"host": addr,
"protocol": protocol,
"username": "",
"password": "vncpassword",
}, a.win)
if err != nil {
return nil, err
}
w, h := 1280*2, 1024*2
a.win.Option(
app.Size(unit.Px(float32(w)), unit.Px(float32(h))),
app.MaxSize(unit.Px(float32(w)), unit.Px(float32(h))),
app.MinSize(unit.Px(float32(w)), unit.Px(float32(h))),
)
return a, nil
}

func (a *App) Run() {
for e := range a.win.Events() {
switch e := e.(type) {
case system.DestroyEvent:
log.Println(e.Err)
os.Exit(0)
case system.FrameEvent:
ops := &op.Ops{}
a.updateScreen(ops, e.Queue)
e.Frame(ops)
case pointer.Event:
if err := a.client.SendMouse(
image.Point{X: int(e.Position.X), Y: int(e.Position.Y)},
guac.MouseToGioButton[e.Buttons]); err != nil {
log.Println(err)
}
a.win.Invalidate()
case key.Event:
log.Printf("%+v, %+v", e.Name, e.Modifiers)

// TODO: keyboard seems problematic, yet.
// See https://todo.sr.ht/~eliasnaur/gio/319
// var keycode guac.KeyCode
// switch {
// case e.Modifiers.Contain(key.ModCtrl):
// keycode = guac.KeyCode(guac.KeyLeftControl)
// case e.Modifiers.Contain(key.ModCommand):
// keycode = guac.KeyCode(guac.KeyLeftControl)
// case e.Modifiers.Contain(key.ModShift):
// keycode = guac.KeyCode(guac.KeyLeftShift)
// case e.Modifiers.Contain(key.ModAlt):
// keycode = guac.KeyCode(guac.KeyLeftAlt)
// case e.Modifiers.Contain(key.ModSuper):
// keycode = guac.KeyCode(guac.KeySuper)
// }
// err := a.client.SendKey(keycode, e.State == key.Press)
// if err != nil {
// log.Println(err)
// }
}
}
}

func (a *App) updateScreen(ops *op.Ops, q event.Queue) {
img, _ := a.client.Screen()
paint.NewImageOp(img).Add(ops)
paint.PaintOp{}.Add(ops)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module changkun.de/x/occamy
go 1.15

require (
gioui.org v0.0.0-20211202105001-872b4ba41be0
gioui.org v0.0.0-20211207114553-03016f0c69b7
github.com/appleboy/gin-jwt/v2 v2.6.2
github.com/gin-gonic/gin v1.4.0
github.com/gorilla/websocket v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20211126213425-227c5a132be6 h1:I/VXI7iHRGYHVqkhLBOxw43zvDH+uzANxKhCj2DNbms=
gioui.org v0.0.0-20211126213425-227c5a132be6/go.mod h1:yoWOxPng6WkDpsud+NRmkoftmyWn3rkKsYGEcWHpjTI=
gioui.org v0.0.0-20211202105001-872b4ba41be0 h1:rXO+2zdXvX6G19M5oF1fs1U7kmUWb4uXCKWa+WHZSpA=
gioui.org v0.0.0-20211202105001-872b4ba41be0/go.mod h1:yoWOxPng6WkDpsud+NRmkoftmyWn3rkKsYGEcWHpjTI=
gioui.org v0.0.0-20211207114553-03016f0c69b7 h1:iJza27sQfbK41uk2Jlx8MpsrlWqMj7n5vCg0GGcC9DM=
gioui.org v0.0.0-20211207114553-03016f0c69b7/go.mod h1:yoWOxPng6WkDpsud+NRmkoftmyWn3rkKsYGEcWHpjTI=
gioui.org/cpu v0.0.0-20210808092351-bfe733dd3334/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ=
gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2 h1:AGDDxsJE1RpcXTAxPG2B4jrwVUJGFDjINIPi1jtO6pc=
gioui.org/cpu v0.0.0-20210817075930-8d6a761490d2/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ=
Expand Down
Loading

0 comments on commit 0e88923

Please sign in to comment.