diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-10-27 20:13:13 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-10-27 20:13:13 +0900 |
| commit | ea29e6bad114306b78820cb674863a9bf3041fa5 (patch) | |
| tree | da87b09d5b3b57d6744db90b697124c5d4569e8d /internal/pipewire/pod.go | |
| parent | 672cf9abcac6f25abfdd2541cae6455c6da38fc3 (diff) | |
internal/pipewire: re-format for readability
This package was the first somewhat large piece of code that was
primarily written using the built-in display of my laptop. The
experience had been deeply unpleasant due to the awkward 3:2 aspect
ratio not working well with my setup, which almost always involved
having the implementation and tests side-by-side. This experience is
what provoked me to re-instate the 80-column limit that I had long
resented and rejected. This package however predates the formal
re-instating of this limit, and was never updated.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/pipewire/pod.go')
| -rw-r--r-- | internal/pipewire/pod.go | 124 |
1 files changed, 82 insertions, 42 deletions
diff --git a/internal/pipewire/pod.go b/internal/pipewire/pod.go index e085af3d..3a03540b 100644 --- a/internal/pipewire/pod.go +++ b/internal/pipewire/pod.go @@ -12,7 +12,8 @@ import ( type ( // A Word is a 32-bit unsigned integer. // - // Values internal to a message appear to always be aligned to 32-bit boundary. + // Values internal to a message appear to always be aligned to 32-bit + // boundary. Word = uint32 // A Bool is a boolean value representing SPA_TYPE_Bool. @@ -40,9 +41,11 @@ const ( // SizeAlign is the boundary which POD starts are always aligned to. SizeAlign = 8 - // SizeSPrefix is the fixed, unpadded size of the fixed-size prefix encoding POD wire size. + // SizeSPrefix is the fixed, unpadded size of the fixed-size prefix encoding + // POD wire size. SizeSPrefix = 4 - // SizeTPrefix is the fixed, unpadded size of the fixed-size prefix encoding POD value type. + // SizeTPrefix is the fixed, unpadded size of the fixed-size prefix encoding + // POD value type. SizeTPrefix = 4 // SizePrefix is the fixed, unpadded size of the fixed-size POD prefix. SizePrefix = SizeSPrefix + SizeTPrefix @@ -76,11 +79,11 @@ func Size[W Word | int](wireSize W) W { return SizePrefix + PaddedSize(wireSize) // SizeString returns prefixed and padded size corresponding to a string. func SizeString[W Word | int](s string) W { return Size(W(len(s)) + 1) } -// PODMarshaler is the interface implemented by an object that can -// marshal itself into PipeWire POD encoding. +// PODMarshaler is the interface implemented by an object that can marshal +// itself into PipeWire POD encoding. type PODMarshaler interface { - // MarshalPOD encodes the receiver into PipeWire POD encoding, - // appends it to data, and returns the result. + // MarshalPOD encodes the receiver into PipeWire POD encoding, appends it to + // data, and returns the result. MarshalPOD(data []byte) ([]byte, error) } @@ -90,9 +93,9 @@ type UnsupportedTypeError struct{ Type reflect.Type } func (e *UnsupportedTypeError) Error() string { return "unsupported type " + e.Type.String() } -// An UnsupportedSizeError is returned by [Marshal] when attempting -// to encode a value with its encoded size exceeding what could be -// represented by the format. +// An UnsupportedSizeError is returned by [Marshal] when attempting to encode a +// value with its encoded size exceeding what could be represented by the +// format. type UnsupportedSizeError int func (e UnsupportedSizeError) Error() string { return "size " + strconv.Itoa(int(e)) + " out of range" } @@ -111,8 +114,8 @@ func MarshalAppend(data []byte, v any) ([]byte, error) { return marshalValueAppend(data, reflect.ValueOf(v)) } -// appendInner calls f and handles size prefix and padding around the appended data. -// f must only append to data. +// appendInner calls f and handles size prefix and padding around the appended +// data. f must only append to data. func appendInner(data []byte, f func(data []byte) ([]byte, error)) ([]byte, error) { data = append(data, make([]byte, SizeSPrefix)...) @@ -146,7 +149,8 @@ func marshalValueAppend(data []byte, v reflect.Value) ([]byte, error) { return appendInner(data, func(data []byte) ([]byte, error) { return marshalValueAppendRaw(data, v) }) } -// marshalValueAppendRaw implements [MarshalAppend] on [reflect.Value] without the size prefix. +// marshalValueAppendRaw implements [MarshalAppend] on [reflect.Value] without +// the size prefix. func marshalValueAppendRaw(data []byte, v reflect.Value) ([]byte, error) { if v.CanInterface() { switch c := v.Interface().(type) { @@ -202,10 +206,11 @@ func marshalValueAppendRaw(data []byte, v reflect.Value) ([]byte, error) { } } -// PODUnmarshaler is the interface implemented by an object that can -// unmarshal a PipeWire POD encoding representation of itself. +// PODUnmarshaler is the interface implemented by an object that can unmarshal a +// PipeWire POD encoding representation of itself. type PODUnmarshaler interface { // UnmarshalPOD must be able to decode the form generated by MarshalPOD. + // // UnmarshalPOD must copy the data if it wishes to retain the data // after returning. UnmarshalPOD(data []byte) (Word, error) @@ -226,15 +231,16 @@ func (e *InvalidUnmarshalError) Error() string { return "attempting to unmarshal to nil " + e.Type.String() } -// UnexpectedEOFError describes an unexpected EOF encountered in the middle of decoding POD data. +// UnexpectedEOFError describes an unexpected EOF encountered in the middle of +// decoding POD data. type UnexpectedEOFError uintptr const ( - // ErrEOFPrefix is returned when unexpectedly encountering EOF - // decoding the fixed-size POD prefix. + // ErrEOFPrefix is returned when unexpectedly encountering EOF decoding the + // fixed-size POD prefix. ErrEOFPrefix UnexpectedEOFError = iota - // ErrEOFData is returned when unexpectedly encountering EOF - // establishing POD data bounds. + // ErrEOFData is returned when unexpectedly encountering EOF establishing + // POD data bounds. ErrEOFData // ErrEOFDataString is returned when unexpectedly encountering EOF // establishing POD [String] bounds. @@ -259,9 +265,9 @@ func (e UnexpectedEOFError) Error() string { return "unexpected EOF " + suffix } -// Unmarshal parses the PipeWire POD encoded data and stores the result -// in the value pointed to by v. If v is nil or not a pointer, -// Unmarshal returns an [InvalidUnmarshalError]. +// Unmarshal parses the PipeWire POD encoded data and stores the result in the +// value pointed to by v. If v is nil or not a pointer, Unmarshal returns an +// [InvalidUnmarshalError]. func Unmarshal(data []byte, v any) error { if n, err := UnmarshalNext(data, v); err != nil { return err @@ -291,8 +297,8 @@ type UnmarshalSetError struct{ Type reflect.Type } func (u *UnmarshalSetError) Error() string { return "cannot set " + u.Type.String() } -// A TrailingGarbageError describes extra bytes after decoding -// has completed during [Unmarshal]. +// A TrailingGarbageError describes extra bytes after decoding has completed +// during [Unmarshal]. type TrailingGarbageError []byte func (e TrailingGarbageError) Error() string { @@ -310,7 +316,8 @@ func (e StringTerminationError) Error() string { return "got byte " + strconv.Itoa(int(e)) + " instead of NUL" } -// unmarshalValue implements [Unmarshal] on [reflect.Value] without compensating for prefix and padding size. +// unmarshalValue implements [Unmarshal] on [reflect.Value] without compensating +// for prefix and padding size. func unmarshalValue(data []byte, v reflect.Value, wireSizeP *Word) error { if !v.CanSet() { return &UnmarshalSetError{v.Type()} @@ -330,7 +337,11 @@ func unmarshalValue(data []byte, v reflect.Value, wireSizeP *Word) error { switch v.Interface().(type) { case Fd: *wireSizeP = SizeFd - if err := unmarshalCheckTypeBounds(&data, SPA_TYPE_Fd, wireSizeP); err != nil { + if err := unmarshalCheckTypeBounds( + &data, + SPA_TYPE_Fd, + wireSizeP, + ); err != nil { return err } v.SetInt(int64(binary.NativeEndian.Uint64(data))) @@ -341,7 +352,11 @@ func unmarshalValue(data []byte, v reflect.Value, wireSizeP *Word) error { switch v.Kind() { case reflect.Uint32: *wireSizeP = SizeId - if err := unmarshalCheckTypeBounds(&data, SPA_TYPE_Id, wireSizeP); err != nil { + if err := unmarshalCheckTypeBounds( + &data, + SPA_TYPE_Id, + wireSizeP, + ); err != nil { return err } v.SetUint(uint64(binary.NativeEndian.Uint32(data))) @@ -349,7 +364,11 @@ func unmarshalValue(data []byte, v reflect.Value, wireSizeP *Word) error { case reflect.Int32: *wireSizeP = SizeInt - if err := unmarshalCheckTypeBounds(&data, SPA_TYPE_Int, wireSizeP); err != nil { + if err := unmarshalCheckTypeBounds( + &data, + SPA_TYPE_Int, + wireSizeP, + ); err != nil { return err } v.SetInt(int64(binary.NativeEndian.Uint32(data))) @@ -357,7 +376,11 @@ func unmarshalValue(data []byte, v reflect.Value, wireSizeP *Word) error { case reflect.Int64: *wireSizeP = SizeLong - if err := unmarshalCheckTypeBounds(&data, SPA_TYPE_Long, wireSizeP); err != nil { + if err := unmarshalCheckTypeBounds( + &data, + SPA_TYPE_Long, + wireSizeP, + ); err != nil { return err } v.SetInt(int64(binary.NativeEndian.Uint64(data))) @@ -365,7 +388,11 @@ func unmarshalValue(data []byte, v reflect.Value, wireSizeP *Word) error { case reflect.Struct: *wireSizeP = 0 - if err := unmarshalCheckTypeBounds(&data, SPA_TYPE_Struct, wireSizeP); err != nil { + if err := unmarshalCheckTypeBounds( + &data, + SPA_TYPE_Struct, + wireSizeP, + ); err != nil { return err } @@ -396,7 +423,11 @@ func unmarshalValue(data []byte, v reflect.Value, wireSizeP *Word) error { case reflect.String: *wireSizeP = 0 - if err := unmarshalCheckTypeBounds(&data, SPA_TYPE_String, wireSizeP); err != nil { + if err := unmarshalCheckTypeBounds( + &data, + SPA_TYPE_String, + wireSizeP, + ); err != nil { return err } @@ -419,8 +450,9 @@ func unmarshalValue(data []byte, v reflect.Value, wireSizeP *Word) error { } } -// unmarshalHandleNone establishes prefix bounds and, for a value of type [SPA_TYPE_None], -// validates its size and skips the header. This is for unmarshalling values that can be nil. +// unmarshalHandleNone establishes prefix bounds and, for a value of type +// [SPA_TYPE_None], validates its size and skips the header. This is for +// unmarshalling values that can be nil. func unmarshalHandleNone(data *[]byte, wireSizeP *Word) (bool, error) { if len(*data) < SizePrefix { return false, ErrEOFPrefix @@ -451,15 +483,17 @@ func (e InconsistentSizeError) Error() string { " for a " + strconv.Itoa(int(e.Expect)) + "-byte long segment" } -// An UnexpectedTypeError describes an unexpected type encountered -// in data passed to [Unmarshal]. +// An UnexpectedTypeError describes an unexpected type encountered in data +// passed to [Unmarshal]. type UnexpectedTypeError struct{ Type, Expect SPAKind } func (e UnexpectedTypeError) Error() string { return "received " + e.Type.String() + " for a value of type " + e.Expect.String() } -// unmarshalCheckTypeBounds performs bounds checks on data and validates the type and size prefixes. +// unmarshalCheckTypeBounds performs bounds checks on data and validates the +// type and size prefixes. +// // An expected size of zero skips further bounds checks. func unmarshalCheckTypeBounds(data *[]byte, t SPAKind, sizeP *Word) error { if len(*data) < SizePrefix { @@ -486,8 +520,8 @@ func unmarshalCheckTypeBounds(data *[]byte, t SPAKind, sizeP *Word) error { return nil } -// The Footer contains additional messages, not directed to -// the destination object defined by the Id field. +// The Footer contains additional messages, not directed to the destination +// object defined by the Id field. type Footer[P KnownSize] struct { // The footer opcode. Opcode Id `json:"opcode"` @@ -595,7 +629,8 @@ func (d *SPADict) UnmarshalPOD(data []byte) (Word, error) { return wireSize, nil } -// A Message is a value that can be transmitted as a message over PipeWire protocol native. +// A Message is a value that can be transmitted as a message over PipeWire +// protocol native. type Message interface { // Opcode returns the opcode of this message. Opcode() byte @@ -617,8 +652,13 @@ func (m MessageEncoder) SizeMessage(footer KnownSize) (size Word) { return } -// AppendMessage appends the protocol native encoding of Message to dst and returns the appended slice. -func (m MessageEncoder) AppendMessage(dst []byte, Id, sequence Int, footer KnownSize) (data []byte, err error) { +// AppendMessage appends the protocol native encoding of Message to dst and +// returns the appended slice. +func (m MessageEncoder) AppendMessage( + dst []byte, + Id, sequence Int, + footer KnownSize, +) (data []byte, err error) { size := m.SizeMessage(footer) if size&^SizeMax != 0 { return dst, ErrSizeRange |
