aboutsummaryrefslogtreecommitdiffhomepage
path: root/hst
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-09-29 06:32:15 +0900
committerOphestra <cat@gensokyo.uk>2025-09-29 06:34:29 +0900
commit44ba7a5f02b7575a706a22aac53c8ac608d18f23 (patch)
treec08890b09a42c38bdf24b976421704ce83a74fad /hst
parentdc467493d8a1461ea321ccf5ca3a2b037f880088 (diff)
hst/enablement: move bits from system
This is part of the hst API, should not be in the implementation package. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'hst')
-rw-r--r--hst/enablement.go77
-rw-r--r--hst/enablement_test.go49
-rw-r--r--hst/hst.go3
3 files changed, 102 insertions, 27 deletions
diff --git a/hst/enablement.go b/hst/enablement.go
index 021288b3..deaacb9d 100644
--- a/hst/enablement.go
+++ b/hst/enablement.go
@@ -2,15 +2,56 @@ package hst
import (
"encoding/json"
+ "fmt"
+ "strings"
"syscall"
+)
+
+// Enablement represents an optional host service to export to the target user.
+type Enablement byte
- "hakurei.app/system"
+const (
+ EWayland Enablement = 1 << iota
+ EX11
+ EDBus
+ EPulse
+
+ EM
)
-// NewEnablements returns the address of [system.Enablement] as [Enablements].
-func NewEnablements(e system.Enablement) *Enablements { return (*Enablements)(&e) }
+func (e Enablement) String() string {
+ switch e {
+ case 0:
+ return "(no enablements)"
+ case EWayland:
+ return "wayland"
+ case EX11:
+ return "x11"
+ case EDBus:
+ return "dbus"
+ case EPulse:
+ return "pulseaudio"
+ default:
+ buf := new(strings.Builder)
+ buf.Grow(32)
+
+ for i := Enablement(1); i < EM; i <<= 1 {
+ if e&i != 0 {
+ buf.WriteString(", " + i.String())
+ }
+ }
+
+ if buf.Len() == 0 {
+ return fmt.Sprintf("e%x", byte(e))
+ }
+ return strings.TrimPrefix(buf.String(), ", ")
+ }
+}
+
+// NewEnablements returns the address of [Enablement] as [Enablements].
+func NewEnablements(e Enablement) *Enablements { return (*Enablements)(&e) }
-// enablementsJSON is the [json] representation of the [system.Enablement] bit field.
+// enablementsJSON is the [json] representation of the [Enablement] bit field.
type enablementsJSON struct {
Wayland bool `json:"wayland,omitempty"`
X11 bool `json:"x11,omitempty"`
@@ -18,15 +59,15 @@ type enablementsJSON struct {
Pulse bool `json:"pulse,omitempty"`
}
-// Enablements is the [json] adapter for [system.Enablement].
-type Enablements system.Enablement
+// Enablements is the [json] adapter for [Enablement].
+type Enablements Enablement
-// Unwrap returns the underlying [system.Enablement].
-func (e *Enablements) Unwrap() system.Enablement {
+// Unwrap returns the underlying [Enablement].
+func (e *Enablements) Unwrap() Enablement {
if e == nil {
return 0
}
- return system.Enablement(*e)
+ return Enablement(*e)
}
func (e *Enablements) MarshalJSON() ([]byte, error) {
@@ -34,10 +75,10 @@ func (e *Enablements) MarshalJSON() ([]byte, error) {
return nil, syscall.EINVAL
}
return json.Marshal(&enablementsJSON{
- Wayland: system.Enablement(*e)&system.EWayland != 0,
- X11: system.Enablement(*e)&system.EX11 != 0,
- DBus: system.Enablement(*e)&system.EDBus != 0,
- Pulse: system.Enablement(*e)&system.EPulse != 0,
+ Wayland: Enablement(*e)&EWayland != 0,
+ X11: Enablement(*e)&EX11 != 0,
+ DBus: Enablement(*e)&EDBus != 0,
+ Pulse: Enablement(*e)&EPulse != 0,
})
}
@@ -51,18 +92,18 @@ func (e *Enablements) UnmarshalJSON(data []byte) error {
return err
}
- var ve system.Enablement
+ var ve Enablement
if v.Wayland {
- ve |= system.EWayland
+ ve |= EWayland
}
if v.X11 {
- ve |= system.EX11
+ ve |= EX11
}
if v.DBus {
- ve |= system.EDBus
+ ve |= EDBus
}
if v.Pulse {
- ve |= system.EPulse
+ ve |= EPulse
}
*e = Enablements(ve)
return nil
diff --git a/hst/enablement_test.go b/hst/enablement_test.go
index c132dd37..82059aa9 100644
--- a/hst/enablement_test.go
+++ b/hst/enablement_test.go
@@ -7,9 +7,44 @@ import (
"testing"
"hakurei.app/hst"
- "hakurei.app/system"
)
+func TestEnablementString(t *testing.T) {
+ testCases := []struct {
+ flags hst.Enablement
+ want string
+ }{
+ {0, "(no enablements)"},
+ {hst.EWayland, "wayland"},
+ {hst.EX11, "x11"},
+ {hst.EDBus, "dbus"},
+ {hst.EPulse, "pulseaudio"},
+ {hst.EWayland | hst.EX11, "wayland, x11"},
+ {hst.EWayland | hst.EDBus, "wayland, dbus"},
+ {hst.EWayland | hst.EPulse, "wayland, pulseaudio"},
+ {hst.EX11 | hst.EDBus, "x11, dbus"},
+ {hst.EX11 | hst.EPulse, "x11, pulseaudio"},
+ {hst.EDBus | hst.EPulse, "dbus, pulseaudio"},
+ {hst.EWayland | hst.EX11 | hst.EDBus, "wayland, x11, dbus"},
+ {hst.EWayland | hst.EX11 | hst.EPulse, "wayland, x11, pulseaudio"},
+ {hst.EWayland | hst.EDBus | hst.EPulse, "wayland, dbus, pulseaudio"},
+ {hst.EX11 | hst.EDBus | hst.EPulse, "x11, dbus, pulseaudio"},
+ {hst.EWayland | hst.EX11 | hst.EDBus | hst.EPulse, "wayland, x11, dbus, pulseaudio"},
+
+ {1 << 5, "e20"},
+ {1 << 6, "e40"},
+ {1 << 7, "e80"},
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.want, func(t *testing.T) {
+ if got := tc.flags.String(); got != tc.want {
+ t.Errorf("String: %q, want %q", got, tc.want)
+ }
+ })
+ }
+}
+
func TestEnablements(t *testing.T) {
testCases := []struct {
name string
@@ -19,11 +54,11 @@ func TestEnablements(t *testing.T) {
}{
{"nil", nil, "null", `{"value":null,"magic":3236757504}`},
{"zero", hst.NewEnablements(0), `{}`, `{"value":{},"magic":3236757504}`},
- {"wayland", hst.NewEnablements(system.EWayland), `{"wayland":true}`, `{"value":{"wayland":true},"magic":3236757504}`},
- {"x11", hst.NewEnablements(system.EX11), `{"x11":true}`, `{"value":{"x11":true},"magic":3236757504}`},
- {"dbus", hst.NewEnablements(system.EDBus), `{"dbus":true}`, `{"value":{"dbus":true},"magic":3236757504}`},
- {"pulse", hst.NewEnablements(system.EPulse), `{"pulse":true}`, `{"value":{"pulse":true},"magic":3236757504}`},
- {"all", hst.NewEnablements(system.EWayland | system.EX11 | system.EDBus | system.EPulse), `{"wayland":true,"x11":true,"dbus":true,"pulse":true}`, `{"value":{"wayland":true,"x11":true,"dbus":true,"pulse":true},"magic":3236757504}`},
+ {"wayland", hst.NewEnablements(hst.EWayland), `{"wayland":true}`, `{"value":{"wayland":true},"magic":3236757504}`},
+ {"x11", hst.NewEnablements(hst.EX11), `{"x11":true}`, `{"value":{"x11":true},"magic":3236757504}`},
+ {"dbus", hst.NewEnablements(hst.EDBus), `{"dbus":true}`, `{"value":{"dbus":true},"magic":3236757504}`},
+ {"pulse", hst.NewEnablements(hst.EPulse), `{"pulse":true}`, `{"value":{"pulse":true},"magic":3236757504}`},
+ {"all", hst.NewEnablements(hst.EWayland | hst.EX11 | hst.EDBus | hst.EPulse), `{"wayland":true,"x11":true,"dbus":true,"pulse":true}`, `{"value":{"wayland":true,"x11":true,"dbus":true,"pulse":true},"magic":3236757504}`},
}
for _, tc := range testCases {
@@ -88,7 +123,7 @@ func TestEnablements(t *testing.T) {
})
t.Run("val", func(t *testing.T) {
- if got := hst.NewEnablements(system.EWayland | system.EPulse).Unwrap(); got != system.EWayland|system.EPulse {
+ if got := hst.NewEnablements(hst.EWayland | hst.EPulse).Unwrap(); got != hst.EWayland|hst.EPulse {
t.Errorf("Unwrap: %v", got)
}
})
diff --git a/hst/hst.go b/hst/hst.go
index c9a23d7a..5399cba0 100644
--- a/hst/hst.go
+++ b/hst/hst.go
@@ -8,7 +8,6 @@ import (
"hakurei.app/container"
"hakurei.app/container/seccomp"
- "hakurei.app/system"
"hakurei.app/system/dbus"
)
@@ -71,7 +70,7 @@ func Template() *Config {
"--ozone-platform=wayland",
},
- Enablements: NewEnablements(system.EWayland | system.EDBus | system.EPulse),
+ Enablements: NewEnablements(EWayland | EDBus | EPulse),
SessionBus: &dbus.Config{
See: nil,