Skip to content

Commit

Permalink
encode & decode fixed-point numbers correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rajveermalviya committed Sep 13, 2021
1 parent f755c86 commit fbfc5d1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 66 deletions.
36 changes: 13 additions & 23 deletions cmd/go-wayland-scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,25 +200,15 @@ func fmtFile(b []byte) []byte {
return b
}

var (
typeToGoTypeMap map[string]string = map[string]string{
"int": "int32",
"uint": "uint32",
"fixed": "float32",
"string": "string",
"object": "Proxy",
"array": "[]int32",
"fd": "uintptr",
}
typeToGetterMap map[string]string = map[string]string{
"int": "Int32",
"uint": "Uint32",
"fixed": "Float32",
"string": "String",
"array": "Array",
"fd": "FD",
}
)
var typeToGoTypeMap map[string]string = map[string]string{
"int": "int32",
"uint": "uint32",
"fixed": "float64",
"string": "string",
"object": "Proxy",
"array": "[]int32",
"fd": "uintptr",
}

func writeInterface(w io.Writer, v Interface) {
ifaceName := toCamel(v.Name)
Expand Down Expand Up @@ -481,9 +471,9 @@ func writeRequest(w io.Writer, ifaceName string, opcode int, r Request) {

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

Expand Down Expand Up @@ -702,9 +692,9 @@ func writeEventDispatcher(w io.Writer, ifaceName string, v Interface) {

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

Expand Down
64 changes: 32 additions & 32 deletions wayland/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2683,8 +2683,8 @@ func (i *DataDevice) RemoveDataOfferHandler(h DataDeviceDataOfferHandler) {
type DataDeviceEnterEvent struct {
Serial uint32
Surface *Surface
X float32
Y float32
X float64
Y float64
ID *DataOffer
}

Expand Down Expand Up @@ -2746,8 +2746,8 @@ func (i *DataDevice) RemoveLeaveHandler(h DataDeviceLeaveHandler) {
// coordinates.
type DataDeviceMotionEvent struct {
Time uint32
X float32
Y float32
X float64
Y float64
}

type DataDeviceMotionHandler interface {
Expand Down Expand Up @@ -2872,9 +2872,9 @@ func (i *DataDevice) Dispatch(opcode uint16, fd uintptr, data []byte) {
l += 4
e.Surface = i.Context().GetProxy(Uint32(data[l : l+4])).(*Surface)
l += 4
e.X = Float32(data[l : l+4])
e.X = Fixed(data[l : l+4])
l += 4
e.Y = Float32(data[l : l+4])
e.Y = Fixed(data[l : l+4])
l += 4
e.ID = i.Context().GetProxy(Uint32(data[l : l+4])).(*DataOffer)
l += 4
Expand All @@ -2897,9 +2897,9 @@ func (i *DataDevice) Dispatch(opcode uint16, fd uintptr, data []byte) {
l := 0
e.Time = Uint32(data[l : l+4])
l += 4
e.X = Float32(data[l : l+4])
e.X = Fixed(data[l : l+4])
l += 4
e.Y = Float32(data[l : l+4])
e.Y = Fixed(data[l : l+4])
l += 4
for _, h := range i.motionHandlers {
h.HandleDataDeviceMotion(e)
Expand Down Expand Up @@ -5202,8 +5202,8 @@ func (e PointerAxisSource) String() string {
type PointerEnterEvent struct {
Serial uint32
Surface *Surface
SurfaceX float32
SurfaceY float32
SurfaceX float64
SurfaceY float64
}

type PointerEnterHandler interface {
Expand Down Expand Up @@ -5269,8 +5269,8 @@ func (i *Pointer) RemoveLeaveHandler(h PointerLeaveHandler) {
// focused surface.
type PointerMotionEvent struct {
Time uint32
SurfaceX float32
SurfaceY float32
SurfaceX float64
SurfaceY float64
}

type PointerMotionHandler interface {
Expand Down Expand Up @@ -5361,7 +5361,7 @@ func (i *Pointer) RemoveButtonHandler(h PointerButtonHandler) {
type PointerAxisEvent struct {
Time uint32
Axis uint32
Value float32
Value float64
}

type PointerAxisHandler interface {
Expand Down Expand Up @@ -5608,9 +5608,9 @@ func (i *Pointer) Dispatch(opcode uint16, fd uintptr, data []byte) {
l += 4
e.Surface = i.Context().GetProxy(Uint32(data[l : l+4])).(*Surface)
l += 4
e.SurfaceX = Float32(data[l : l+4])
e.SurfaceX = Fixed(data[l : l+4])
l += 4
e.SurfaceY = Float32(data[l : l+4])
e.SurfaceY = Fixed(data[l : l+4])
l += 4
for _, h := range i.enterHandlers {
h.HandlePointerEnter(e)
Expand All @@ -5636,9 +5636,9 @@ func (i *Pointer) Dispatch(opcode uint16, fd uintptr, data []byte) {
l := 0
e.Time = Uint32(data[l : l+4])
l += 4
e.SurfaceX = Float32(data[l : l+4])
e.SurfaceX = Fixed(data[l : l+4])
l += 4
e.SurfaceY = Float32(data[l : l+4])
e.SurfaceY = Fixed(data[l : l+4])
l += 4
for _, h := range i.motionHandlers {
h.HandlePointerMotion(e)
Expand Down Expand Up @@ -5670,7 +5670,7 @@ func (i *Pointer) Dispatch(opcode uint16, fd uintptr, data []byte) {
l += 4
e.Axis = Uint32(data[l : l+4])
l += 4
e.Value = Float32(data[l : l+4])
e.Value = Fixed(data[l : l+4])
l += 4
for _, h := range i.axisHandlers {
h.HandlePointerAxis(e)
Expand Down Expand Up @@ -6224,8 +6224,8 @@ type TouchDownEvent struct {
Time uint32
Surface *Surface
ID int32
X float32
Y float32
X float64
Y float64
}

type TouchDownHandler interface {
Expand Down Expand Up @@ -6289,8 +6289,8 @@ func (i *Touch) RemoveUpHandler(h TouchUpHandler) {
type TouchMotionEvent struct {
Time uint32
ID int32
X float32
Y float32
X float64
Y float64
}

type TouchMotionHandler interface {
Expand Down Expand Up @@ -6408,8 +6408,8 @@ func (i *Touch) RemoveCancelHandler(h TouchCancelHandler) {
// shape if it did not receive this event.
type TouchShapeEvent struct {
ID int32
Major float32
Minor float32
Major float64
Minor float64
}

type TouchShapeHandler interface {
Expand Down Expand Up @@ -6461,7 +6461,7 @@ func (i *Touch) RemoveShapeHandler(h TouchShapeHandler) {
// orientation reports.
type TouchOrientationEvent struct {
ID int32
Orientation float32
Orientation float64
}

type TouchOrientationHandler interface {
Expand Down Expand Up @@ -6502,9 +6502,9 @@ func (i *Touch) Dispatch(opcode uint16, fd uintptr, data []byte) {
l += 4
e.ID = int32(Uint32(data[l : l+4]))
l += 4
e.X = Float32(data[l : l+4])
e.X = Fixed(data[l : l+4])
l += 4
e.Y = Float32(data[l : l+4])
e.Y = Fixed(data[l : l+4])
l += 4
for _, h := range i.downHandlers {
h.HandleTouchDown(e)
Expand Down Expand Up @@ -6534,9 +6534,9 @@ func (i *Touch) Dispatch(opcode uint16, fd uintptr, data []byte) {
l += 4
e.ID = int32(Uint32(data[l : l+4]))
l += 4
e.X = Float32(data[l : l+4])
e.X = Fixed(data[l : l+4])
l += 4
e.Y = Float32(data[l : l+4])
e.Y = Fixed(data[l : l+4])
l += 4
for _, h := range i.motionHandlers {
h.HandleTouchMotion(e)
Expand Down Expand Up @@ -6565,9 +6565,9 @@ func (i *Touch) Dispatch(opcode uint16, fd uintptr, data []byte) {
l := 0
e.ID = int32(Uint32(data[l : l+4]))
l += 4
e.Major = Float32(data[l : l+4])
e.Major = Fixed(data[l : l+4])
l += 4
e.Minor = Float32(data[l : l+4])
e.Minor = Fixed(data[l : l+4])
l += 4
for _, h := range i.shapeHandlers {
h.HandleTouchShape(e)
Expand All @@ -6580,7 +6580,7 @@ func (i *Touch) Dispatch(opcode uint16, fd uintptr, data []byte) {
l := 0
e.ID = int32(Uint32(data[l : l+4]))
l += 4
e.Orientation = Float32(data[l : l+4])
e.Orientation = Fixed(data[l : l+4])
l += 4
for _, h := range i.orientationHandlers {
h.HandleTouchOrientation(e)
Expand Down
4 changes: 2 additions & 2 deletions wayland/client/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func String(src []byte) string {
return string(bytes.TrimRight(src, "\x00"))
}

func Float32(src []byte) float32 {
func Fixed(src []byte) float64 {
i := int32(byteorder.NativeEndian.Uint32(src))
return float32(fixedToFloat64(i))
return fixedToFloat64(i)
}

func Array(src []byte) []int32 {
Expand Down
4 changes: 2 additions & 2 deletions wayland/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func PutUint32(dst []byte, v uint32) {
byteorder.NativeEndian.PutUint32(dst, v)
}

func PutFloat32(dst []byte, f float32) {
fx := float64ToFixed(float64(f))
func PutFixed(dst []byte, f float64) {
fx := fixedFromfloat64(f)
byteorder.NativeEndian.PutUint32(dst, uint32(fx))
}

Expand Down
4 changes: 2 additions & 2 deletions wayland/client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import "math"
// From wayland/wayland-util.h

func fixedToFloat64(f int32) float64 {
u_i := int64((1023+44)<<52) + (1 << 51) + int64(f)
u_i := (1023+44)<<52 + (1 << 51) + int64(f)
u_d := math.Float64frombits(uint64(u_i))
return u_d - (3 << 43)
}

func float64ToFixed(d float64) int32 {
func fixedFromfloat64(d float64) int32 {
u_d := d + (3 << (51 - 8))
u_i := int64(math.Float64bits(u_d))
return int32(u_i)
Expand Down
10 changes: 5 additions & 5 deletions wayland/stable/viewporter/viewporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (i *Viewport) Destroy() error {
// y: source rectangle y
// width: source rectangle width
// height: source rectangle height
func (i *Viewport) SetSource(x, y, width, height float32) error {
func (i *Viewport) SetSource(x, y, width, height float64) error {
const opcode = 1
const rLen = 8 + 4 + 4 + 4 + 4
r := make([]byte, rLen)
Expand All @@ -313,13 +313,13 @@ func (i *Viewport) SetSource(x, y, width, height float32) error {
l += 4
client.PutUint32(r[l:l+4], uint32(rLen<<16|opcode&0x0000ffff))
l += 4
client.PutFloat32(r[l:l+4], x)
client.PutFixed(r[l:l+4], x)
l += 4
client.PutFloat32(r[l:l+4], y)
client.PutFixed(r[l:l+4], y)
l += 4
client.PutFloat32(r[l:l+4], width)
client.PutFixed(r[l:l+4], width)
l += 4
client.PutFloat32(r[l:l+4], height)
client.PutFixed(r[l:l+4], height)
l += 4
err := i.Context().WriteMsg(r, nil)
return err
Expand Down

0 comments on commit fbfc5d1

Please # to comment.