2024-10-16 14:38:57 +09:00
|
|
|
package system
|
2024-09-22 00:29:36 +09:00
|
|
|
|
2024-12-20 23:21:19 +09:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-09-22 00:29:36 +09:00
|
|
|
type (
|
|
|
|
// Enablement represents an optional system resource
|
|
|
|
Enablement uint8
|
|
|
|
// Enablements represents optional system resources to share
|
|
|
|
Enablements uint64
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-10-16 14:38:57 +09:00
|
|
|
EWayland Enablement = iota
|
|
|
|
EX11
|
|
|
|
EDBus
|
|
|
|
EPulse
|
2024-09-22 00:29:36 +09:00
|
|
|
)
|
|
|
|
|
2024-10-16 14:38:57 +09:00
|
|
|
var enablementString = [...]string{
|
|
|
|
EWayland: "Wayland",
|
|
|
|
EX11: "X11",
|
|
|
|
EDBus: "D-Bus",
|
|
|
|
EPulse: "PulseAudio",
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
|
2024-10-16 14:38:57 +09:00
|
|
|
const ELen = len(enablementString)
|
|
|
|
|
2024-09-22 00:29:36 +09:00
|
|
|
func (e Enablement) String() string {
|
2024-10-17 14:31:13 +09:00
|
|
|
if int(e) >= ELen {
|
|
|
|
return "<invalid enablement>"
|
|
|
|
}
|
2024-09-22 00:29:36 +09:00
|
|
|
return enablementString[e]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Enablement) Mask() Enablements {
|
|
|
|
return 1 << e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has returns whether a feature is enabled
|
|
|
|
func (es *Enablements) Has(e Enablement) bool {
|
|
|
|
return *es&e.Mask() != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set enables a feature
|
|
|
|
func (es *Enablements) Set(e Enablement) {
|
|
|
|
if es.Has(e) {
|
|
|
|
panic("enablement " + e.String() + " set twice")
|
|
|
|
}
|
|
|
|
|
|
|
|
*es |= e.Mask()
|
|
|
|
}
|
2024-12-20 23:21:19 +09:00
|
|
|
|
|
|
|
func (es *Enablements) String() string {
|
|
|
|
buf := new(strings.Builder)
|
|
|
|
for i := Enablement(0); i < Enablement(ELen); i++ {
|
|
|
|
if es.Has(i) {
|
|
|
|
buf.WriteString(", " + i.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf.Len() == 0 {
|
|
|
|
buf.WriteString("(No enablements)")
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimPrefix(buf.String(), ", ")
|
|
|
|
}
|