Skip to content

Commit

Permalink
generate inlined code for msg decode (breaking)
Browse files Browse the repository at this point in the history
- fix float32 request args
- better error propogation
  • Loading branch information
rajveermalviya committed Sep 12, 2021
1 parent 79db33a commit d1e1a0a
Show file tree
Hide file tree
Showing 27 changed files with 1,070 additions and 875 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/examples/imageviewer",
"args": ["${workspaceFolder}/examples/imageviewer/image.webp"]
}
]
}
112 changes: 92 additions & 20 deletions cmd/go-wayland-scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ func writeRequest(w io.Writer, ifaceName string, opcode int, r Request) {
} else {
canBeConst = false
if protocol.Name == "wayland" {
fmt.Fprintf(w, "ifaceLen := StringPaddedLen(iface)\n")
fmt.Fprintf(w, "ifaceLen := PaddedLen(len(iface)+1)\n")
} else {
fmt.Fprintf(w, "ifaceLen := client.StringPaddedLen(iface)\n")
fmt.Fprintf(w, "ifaceLen := client.PaddedLen(len(iface)+1)\n")
}
sizes = append(sizes, "(4 + ifaceLen)", "4", "4")
}
Expand All @@ -380,9 +380,9 @@ func writeRequest(w io.Writer, ifaceName string, opcode int, r Request) {
case "string":
canBeConst = false
if protocol.Name == "wayland" {
fmt.Fprintf(w, "%sLen := StringPaddedLen(%s)\n", argNameLower, argNameLower)
fmt.Fprintf(w, "%sLen := PaddedLen(len(%s)+1)\n", argNameLower, argNameLower)
} else {
fmt.Fprintf(w, "%sLen := client.StringPaddedLen(%s)\n", argNameLower, argNameLower)
fmt.Fprintf(w, "%sLen := client.PaddedLen(len(%s)+1)\n", argNameLower, argNameLower)
}
sizes = append(sizes, fmt.Sprintf("(4 + %sLen)", argNameLower))
}
Expand Down Expand Up @@ -471,14 +471,22 @@ func writeRequest(w io.Writer, ifaceName string, opcode int, r Request) {
fmt.Fprintf(w, "l += 4\n")
}

case "int", "uint", "fixed":
case "int", "uint":
if protocol.Name == "wayland" {
fmt.Fprintf(w, "PutUint32(r[l:l+4], uint32(%s))\n", argNameLower)
} else {
fmt.Fprintf(w, "client.PutUint32(r[l:l+4], uint32(%s))\n", argNameLower)
}
fmt.Fprintf(w, "l += 4\n")

case "fixed":
if protocol.Name == "wayland" {
fmt.Fprintf(w, "PutFloat32(r[l:l+4], %s)\n", argNameLower)
} else {
fmt.Fprintf(w, "client.PutFloat32(r[l:l+4], %s)\n", argNameLower)
}
fmt.Fprintf(w, "l += 4\n")

case "string":
if protocol.Name == "wayland" {
fmt.Fprintf(w, "PutString(r[l:l+(4 + %sLen)], %s, %sLen)\n", argNameLower, argNameLower, argNameLower)
Expand Down Expand Up @@ -625,26 +633,30 @@ func writeEventDispatcher(w io.Writer, ifaceName string, v Interface) {
return
}

if protocol.Name != "wayland" {
fmt.Fprintf(w, "func (i *%s) Dispatch(event *client.Event) {\n", ifaceName)
} else {
fmt.Fprintf(w, "func (i *%s) Dispatch(event *Event) {\n", ifaceName)
}
fmt.Fprintf(w, "switch event.Opcode {\n")
fmt.Fprintf(w, "func (i *%s) Dispatch(opcode uint16, fd uintptr, data []byte) {\n", ifaceName)
fmt.Fprintf(w, "switch opcode {\n")
for i, e := range v.Events {
eventName := toCamel(e.Name)
eventNameLower := toLowerCamel(e.Name)

fmt.Fprintf(w, "case %d:\n", i)
fmt.Fprintf(w, "if len(i.%sHandlers) == 0 {\n", eventNameLower)
fmt.Fprintf(w, "break\n")
fmt.Fprintf(w, "return\n")
fmt.Fprintf(w, "}\n")
fmt.Fprintf(w, "e := %s%sEvent{\n", ifaceName, eventName)

fmt.Fprintf(w, "var e %s%sEvent\n", ifaceName, eventName)

if len(e.Args) > 0 && (len(e.Args) != 1 || e.Args[0].Type != "fd") {
fmt.Fprintf(w, "l := 0\n")
}

for _, arg := range e.Args {
argName := toCamel(arg.Name)
argNameLower := toLowerCamel(arg.Name)

switch arg.Type {
case "object", "new_id":
if arg.Interface != "" {
argName := toCamel(arg.Name)
argIface := toCamel(arg.Interface)

if !isLocalInterface(arg.Interface) {
Expand All @@ -655,17 +667,77 @@ func writeEventDispatcher(w io.Writer, ifaceName string, v Interface) {
}
}

fmt.Fprintf(w, "%s: event.Proxy(i.Context()).(*%s),\n", argName, argIface)
if protocol.Name == "wayland" {
fmt.Fprintf(w, "e.%s = i.Context().GetProxy(Uint32(data[l :l+4])).(*%s)\n", argName, argIface)
} else {
fmt.Fprintf(w, "e.%s = i.Context().GetProxy(client.Uint32(data[l :l+4])).(*%s)\n", argName, argIface)
}
} else {
if protocol.Name == "wayland" {
fmt.Fprintf(w, "e.%s = i.Context().GetProxy(Uint32(data[l :l+4]))\n", argName)
} else {
fmt.Fprintf(w, "e.%s = i.Context().GetProxy(client.Uint32(data[l :l+4]))\n", argName)
}
}
fmt.Fprintf(w, "l += 4\n")

case "fd":
fmt.Fprintf(w, "e.%s = fd\n", argName)

case "uint":
if protocol.Name == "wayland" {
fmt.Fprintf(w, "e.%s = Uint32(data[l : l+4])\n", argName)
} else {
fmt.Fprintf(w, "%s: event.Proxy(i.Context()),\n", toCamel(arg.Name))
fmt.Fprintf(w, "e.%s = client.Uint32(data[l : l+4])\n", argName)
}
fmt.Fprintf(w, "l += 4\n")

case "int", "uint", "fixed",
"string", "array", "fd":
fmt.Fprintf(w, "%s: event.%s(),\n", toCamel(arg.Name), typeToGetterMap[arg.Type])
case "int":
if protocol.Name == "wayland" {
fmt.Fprintf(w, "e.%s = int32(Uint32(data[l : l+4]))\n", argName)
} else {
fmt.Fprintf(w, "e.%s = int32(client.Uint32(data[l : l+4]))\n", argName)
}
fmt.Fprintf(w, "l += 4\n")

case "fixed":
if protocol.Name == "wayland" {
fmt.Fprintf(w, "e.%s = Float32(data[l : l+4])\n", argName)
} else {
fmt.Fprintf(w, "e.%s = client.Float32(data[l : l+4])\n", argName)
}
fmt.Fprintf(w, "l += 4\n")

case "string":
if protocol.Name == "wayland" {
fmt.Fprintf(w, "%sLen := PaddedLen(int(Uint32(data[l : l+4])))\n", argNameLower)
} else {
fmt.Fprintf(w, "%sLen := client.PaddedLen(int(client.Uint32(data[l : l+4])))\n", argNameLower)
}
fmt.Fprintf(w, "l += 4\n")
if protocol.Name == "wayland" {
fmt.Fprintf(w, "e.%s = String(data[l : l+%sLen])\n", argName, argNameLower)
} else {
fmt.Fprintf(w, "e.%s = client.String(data[l : l+%sLen])\n", argName, argNameLower)
}
fmt.Fprintf(w, "l += %sLen\n", argNameLower)

case "array":
if protocol.Name == "wayland" {
fmt.Fprintf(w, "%sLen := int(Uint32(data[l : l+4]))\n", argNameLower)
} else {
fmt.Fprintf(w, "%sLen := int(client.Uint32(data[l : l+4]))\n", argNameLower)
}
fmt.Fprintf(w, "l += 4\n")
if protocol.Name == "wayland" {
fmt.Fprintf(w, "e.%s = Array(data[l : l+%sLen])\n", argName, argNameLower)
} else {
fmt.Fprintf(w, "e.%s = client.Array(data[l : l+%sLen])\n", argName, argNameLower)
}
fmt.Fprintf(w, "l += %sLen\n", argNameLower)
}
}
fmt.Fprintf(w, "}\n\n")

fmt.Fprintf(w, "for _, h := range i.%sHandlers {\n", eventNameLower)
fmt.Fprintf(w, "h.Handle%s%s(e)\n\n", ifaceName, eventName)
fmt.Fprintf(w, "}\n")
Expand Down
2 changes: 2 additions & 0 deletions examples/imageviewer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ require (
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0
)

replace github.com/rajveermalviya/go-wayland/wayland => ../../wayland/
2 changes: 0 additions & 2 deletions examples/imageviewer/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/rajveermalviya/go-wayland/wayland v0.0.0-20210911205150-77423f857c3d h1:N3r9LE88xnbpXMLs2/dQEDDlZpreXqz8v/63mkKbbDo=
github.com/rajveermalviya/go-wayland/wayland v0.0.0-20210911205150-77423f857c3d/go.mod h1:tTq1RgEsAtaurLrBeFzf3clsqah1vXXv4UcXhesV5Sk=
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d h1:RNPAfi2nHY7C2srAV8A49jpsYr0ADedCk1wq6fTMTvs=
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 h1:xrCZDmdtoloIiooiA9q0OQb9r8HejIHYoHGhGCe1pGg=
Expand Down
Binary file added examples/imageviewer/image.webp
Binary file not shown.
6 changes: 3 additions & 3 deletions examples/imageviewer/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ func (app *appState) attachKeyboard() {

keyboard.AddKeyHandler(app)

log.Print("keyboard interface registered")
logPrintln("keyboard interface registered")
}

func (app *appState) releaseKeyboard() {
app.keyboard.RemoveKeyHandler(app)

if err := app.keyboard.Release(); err != nil {
log.Println("unable to release keyboard interface")
logPrintln("unable to release keyboard interface")
}
app.keyboard = nil

log.Print("keyboard interface released")
logPrintln("keyboard interface released")
}

func (app *appState) HandleKeyboardKey(e client.KeyboardKeyEvent) {
Expand Down
24 changes: 24 additions & 0 deletions examples/imageviewer/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"log"
"os"
)

var logDisabled = os.Getenv("LOG_DISABLED") == "1"

func logPrintln(v ...interface{}) {
if !logDisabled {
log.Println(v...)
}
}

func logPrintf(format string, v ...interface{}) {
if !logDisabled {
log.Printf(format, v...)
}
}

func logFatalf(format string, v ...interface{}) {
log.Fatalf(format, v...)
}
Loading

0 comments on commit d1e1a0a

Please # to comment.