dbusx

package
v0.3.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 12 Imported by: 6 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dbusx holds the D-Bus plumbing every lys process shares, on lys-lib's patched godbus: exporting interfaces at a path and under a well-known name the way zbus's object_server().at + request_name does, serving zbus-style #[zbus(property)] getters (computed on every read) and setters over org.freedesktop.DBus.Properties, introspection built on demand, watching a name for its owner and a peer for its signals, and dialing the system bus within a bound.

Index

Constants

View Source
const (
	ErrFailed          = "org.freedesktop.DBus.Error.Failed"
	ErrInvalidArgs     = "org.freedesktop.DBus.Error.InvalidArgs"
	ErrUnknownProperty = "org.freedesktop.DBus.Error.UnknownProperty"
	ErrUnknownIface    = "org.freedesktop.DBus.Error.UnknownInterface"
	ErrPropertyRO      = "org.freedesktop.DBus.Error.PropertyReadOnly"
)

Error names of the standard org.freedesktop.DBus errors (zbus's fdo::Error variants).

View Source
const ObjectManagerInterface = "org.freedesktop.DBus.ObjectManager"

ObjectManagerInterface is where an object manager announces the objects below it gaining and losing interfaces (InterfacesAdded, InterfacesRemoved).

View Source
const (
	PropertiesInterface = "org.freedesktop.DBus.Properties"
)

The interfaces Export serves itself at each path, for all of the path's interfaces. PropertiesInterface is also where an object announces its changed properties (PropertiesChanged).

Variables

View Source
var ErrDisconnected = errors.New("the bus connection closed")

ErrDisconnected is what Watch.Stop reports when the watch ended because its connection closed.

View Source
var ErrNameTaken = errors.New("name already owned")

ErrNameTaken is what Own and Serve wrap when another connection already owns the well-known name, so a caller can tell a taken name from a bus failure.

View Source
var InterfacesAdded = NewSignal[AddedInterfaces](ObjectManagerInterface, "InterfacesAdded")

InterfacesAdded is the signal an object manager sends when an object below it gains interfaces, or appears with them.

View Source
var InterfacesRemoved = NewSignal[RemovedInterfaces](ObjectManagerInterface, "InterfacesRemoved")

InterfacesRemoved is the signal an object manager sends when an object below it loses interfaces, or disappears with them.

View Source
var PropertiesChanged = NewSignal[ChangedProperties](PropertiesInterface, "PropertiesChanged")

PropertiesChanged is the signal an object sends when properties of one of its interfaces change.

Functions

func DialSession

func DialSession(ctx context.Context, timeout time.Duration) (*dbus.Conn, error)

DialSession connects to the session bus as DialSystem does to the system bus, bounded the same way.

func DialSystem

func DialSystem(ctx context.Context, timeout time.Duration) (*dbus.Conn, error)

DialSystem connects to the system bus, giving up when ctx ends or timeout has passed, whichever comes first: a bus that does not answer never hangs its caller. Once made, the connection outlives both and is the caller's to close.

func Dispatch

func Dispatch(sig *dbus.Signal, sender string, routes []Route) error

Dispatch hands sig to the route it belongs to, decoded, when sender, a unique name or the bus's, sent it; no signal is from the sender "". A route for a name's owner changing takes the bus's signal whoever sender is. It takes over the descriptors sig carries, which are the caller's own (each signal channel of a connection is handed duplicates of its own): a handler that gets the signal owns them, and a signal no handler gets, because it is of no route, another peer sent it or it does not decode, has them closed. The error is the handler's, or says why the signal was not decoded or a descriptor not closed.

func Export

func Export(conn *dbus.Conn, path dbus.ObjectPath, ifaces ...Interface) (unexport func(), err error)

Export puts ifaces at path on conn, with one Properties object serving all of their properties and introspection listing them all, as zbus's object_server().at does for each interface it mounts at a path. The returned unexport removes them again (a Request or Session object ending). Export is all or nothing: every interface is checked before any is exported, and a failure leaves none of them at path.

func Failed

func Failed(msg string) *dbus.Error

Failed is fdo::Error::Failed(msg).

func File

func File(fd dbus.UnixFD, name string) (*os.File, error)

File wraps a descriptor received over D-Bus; a negative one is an error, not a file.

func InvalidArgs

func InvalidArgs(msg string) *dbus.Error

InvalidArgs is fdo::Error::InvalidArgs(msg).

func NameOwner

func NameOwner(ctx context.Context, conn *dbus.Conn, name string) (string, error)

NameOwner returns the unique name of name's current owner; a name nobody owns is an error.

func NameOwnerChange

func NameOwnerChange(sig *dbus.Signal, name string) (oldOwner, newOwner string, ok bool)

NameOwnerChange reads a NameOwnerChanged signal the bus sent about name: the old and new owners ("" for none), and whether the signal is that one. Another peer sending NameOwnerChanged is not the bus, so its signal is not that one.

func Own

func Own(conn *dbus.Conn, name string) (func(), error)

Own takes a well-known name; the returned release drops it. A name already owned is ErrNameTaken, as zbus's request_name with DoNotQueue.

func OwnerMatch

func OwnerMatch(name string) []dbus.MatchOption

OwnerMatch matches the NameOwnerChanged signals the bus sends about name.

func RemoveSignal

func RemoveSignal(conn *dbus.Conn, signals chan *dbus.Signal) error

RemoveSignal takes signals off conn, as conn.RemoveSignal does, and closes the descriptors of the signals it was sent and nobody read: every signal channel owns those of what it is handed. Nothing is sent on signals once it returns.

func Serve

func Serve(conn *dbus.Conn, s Service) (func(), error)

Serve exports s on conn and takes its well-known name; the returned release drops the name and unexports s. A name already owned is ErrNameTaken, as zbus's request_name with DoNotQueue, and leaves nothing exported.

func WatchOwner

func WatchOwner(ctx context.Context, conn *dbus.Conn, name string, appeared func(announced dbus.Sequence)) (stop func() error, err error)

WatchOwner runs appeared, on the watch's goroutine, each time name gains an owner on conn: a service that starts (or restarts) after its client did. appeared gets the sequence of the bus's announcement on conn, which a caller compares with the sequence of a reply of its own (dbus.Call.ResponseSequence) to tell an owner that had already answered it from one that has not seen it. The watch holds a match rule and a signal channel on conn until ctx ends or stop is called, whichever comes first, and then removes both, so a connection shared with others is left as it was. stop returns once they are gone, with the error removing the rule met (none for a closed connection, whose rules the bus dropped); calling it again, or after ctx ended, waits for the same removal and reports the same error. stop must not be called from appeared.

Types

type AddedInterfaces

type AddedInterfaces struct {
	Object     dbus.ObjectPath                    `dbus:"object_path"`
	Interfaces map[string]map[string]dbus.Variant `dbus:"interfaces_and_properties"`
}

AddedInterfaces is what InterfacesAdded carries: the object that gained interfaces, and each of them with its properties. A route lends it to its handler: the descriptors in Interfaces are closed once the handler returns, so a handler that keeps one duplicates it.

type ChangedProperties

type ChangedProperties struct {
	Interface   string                  `dbus:"interface_name"`
	Changed     map[string]dbus.Variant `dbus:"changed_properties"`
	Invalidated []string                `dbus:"invalidated_properties"`
}

ChangedProperties is what PropertiesChanged carries: the interface the properties belong to, those that changed with their new values, and those that changed without one. A route lends it to its handler: the descriptors in Changed are closed once the handler returns, so a handler that keeps one duplicates it.

type Getters

type Getters map[string]func() any

Getters maps property names to their read functions. Each returns a value godbus can marshal; the D-Bus signature is taken from it.

type Interface

type Interface struct {
	Name       string
	Methods    any
	Properties Getters
	Setters    Setters
	Signals    []SignalDecl
	// Ordered are the methods whose calls run one at a time in arrival
	// order (dbus.Conn.DispatchInOrder), for a caller that does not wait
	// for each reply.
	Ordered []string
}

Interface is one D-Bus interface of an object: its methods (an object whose exported methods each end in a *dbus.Error result), its properties, the writers of those that are writable, and the signals it emits.

type OwnerChange

type OwnerChange struct {
	Name     string `dbus:"name"`
	OldOwner string `dbus:"old_owner"`
	NewOwner string `dbus:"new_owner"`
}

OwnerChange is what NameOwnerChanged carries: the name, and its old and new owners, "" for none.

type RemovedInterfaces

type RemovedInterfaces struct {
	Object     dbus.ObjectPath `dbus:"object_path"`
	Interfaces []string        `dbus:"interfaces"`
}

RemovedInterfaces is what InterfacesRemoved carries: the object that lost interfaces, and their names.

type Route

type Route struct {
	// contains filtered or unexported fields
}

Route is one signal of a watch: which signal, from which objects, and the handler it is decoded for. One made by OnOthers is every signal of an interface instead, and one made by OnNameOwnerChanged, OnOwnerChange or OnNameLost is the bus's whoever the watch's sender is.

func On

func On[T any](s Signal[T], path dbus.ObjectPath, handle func(T, dbus.Sequence) error) Route

On routes the signal s, sent from the object at path, to handle, which also gets the signal's sequence on its connection: every message a connection reads has one, in the order it was read, so a signal sequenced before a call's reply (dbus.Call.ResponseSequence) was sent before the peer answered. An error handle returns is reported as the watch reports a signal it could not decode. The signal may carry arguments after those s declares (Signal.Decode); their descriptors are closed, as are those of a ChangedProperties or an AddedInterfaces once handle returns.

func OnNameLost

func OnNameLost(lost func(name string, announced dbus.Sequence) error) Route

OnNameLost is OnNameOwnerChanged for a name losing its owner: a well-known name released, or the unique name of a peer that left the bus. The announcement's sequence orders the loss against a reply the name's owner sent (dbus.Call.ResponseSequence): an answer sequenced before it came from an owner that has left since.

func OnNameOwnerChanged

func OnNameOwnerChanged(handle func(OwnerChange, dbus.Sequence) error) Route

OnNameOwnerChanged routes the bus's announcement that a name changed owner to handle, with the announcement's sequence: every name's, unless WithArg0 or WithArg0Namespace narrows the route. The bus sends it whoever the watch's own signals are from, so a watch following the owner of a name (FromOwner) learns here, in order with that owner's signals, that the owner came, went or was replaced; it is not told of a change the bus had already announced when it answered who owns the name as the watch began.

func OnOthers

func OnOthers(iface string, path dbus.ObjectPath, handle func(*dbus.Signal) error) Route

OnOthers routes every signal of iface, sent from the object at path, that no route before it takes to handle, as it arrived: handle owns the descriptors it carries. A watch that must understand everything its peer says about an object lists it last and fails on what reaches it.

func OnOwnerChange

func OnOwnerChange(name string, handle func(old, new string, announced dbus.Sequence) error) Route

OnOwnerChange is OnNameOwnerChanged for name alone, handle getting its old and new owners ("" for none).

func OnUnder

func OnUnder[T any](s Signal[T], namespace dbus.ObjectPath, handle func(dbus.ObjectPath, T, dbus.Sequence) error) Route

OnUnder is On for every object in the path namespace: the object at namespace and every object below it, as the D-Bus match rule's path_namespace says. handle also gets the path of the object that sent the signal.

func (Route) WithArg0

func (r Route) WithArg0(value string) Route

WithArg0 narrows the route to signals whose first argument is the string value: the name a NameOwnerChanged is about, the interface of a PropertiesChanged.

func (Route) WithArg0Namespace

func (r Route) WithArg0Namespace(namespace string) Route

WithArg0Namespace narrows the route to signals whose first argument is the string namespace or a name in it, as the D-Bus match rule's arg0namespace says: org.mpris.MediaPlayer2 takes org.mpris.MediaPlayer2 and org.mpris.MediaPlayer2.vlc, not org.mpris.MediaPlayer2x.

type Sender

type Sender struct {
	// contains filtered or unexported fields
}

Sender is the peer whose signals a watch delivers. A signal anyone else sends in its name is dropped.

func FromBus

func FromBus() Sender

FromBus is the message bus itself, which alone sends as org.freedesktop.DBus.

func FromOwner

func FromOwner(name string) Sender

FromOwner is whichever connection owns the well-known name as each signal arrives. The watch follows the name from one owner to the next, so a service that restarts keeps being heard, and a name nobody owns yet does not fail it: its signals are delivered from the moment it gains an owner.

type Service

type Service struct {
	// Name is the well-known bus name and Path the object path.
	Name string
	Path dbus.ObjectPath
	// Interface is the D-Bus interface the methods are exported as.
	Interface string
	// Methods is the object whose exported methods (each ending in a
	// *dbus.Error result) form the interface.
	Methods any
	// Properties are the read-only properties.
	Properties Getters
	// Signals are the signals the interface emits.
	Signals []SignalDecl
}

Service is one exported interface.

type Setters

type Setters map[string]func(dbus.Variant) *dbus.Error

Setters maps writable property names to their write functions, which decode the variant themselves and refuse a wrong type.

type Signal

type Signal[T any] struct {
	// contains filtered or unexported fields
}

Signal is one signal of a D-Bus interface, carrying a T: a struct whose exported fields, except those tagged `dbus:"-"`, are the signal's arguments in order. A field's dbus tag names its argument in the introspection data; an untagged field's argument is unnamed. The same Signal declares it on an Interface, emits it and decodes it, so its name and signature exist once.

func NewSignal

func NewSignal[T any](iface, member string) Signal[T]

NewSignal declares the signal member of iface carrying a T. The declaration costs nothing, so a package-level one runs no code when its package is imported; the arguments are read off T when the signal is first declared on an Interface, emitted or decoded, which panics when T is not a struct or a field has no D-Bus type, as a declaration that cannot exist.

func (Signal[T]) Decode

func (s Signal[T]) Decode(sig *dbus.Signal) (T, error)

Decode reads a received signal as this one, refusing another signal and a body that does not start with its arguments. Arguments after them are not read: an interface grows by appending arguments, and a newer peer's signal is still the one declared, so a T without arguments decodes whatever the signal carries. The descriptors in those further arguments stay the signal's (dbus.Signal.CloseFDs); a watch's On route closes them. Arguments holding a struct arrive untyped, so within them Decode accepts what dbus.Store converts.

func (Signal[T]) Emit

func (s Signal[T]) Emit(conn *dbus.Conn, path dbus.ObjectPath, v T) error

Emit sends the signal from the object at path on conn, carrying v.

func (Signal[T]) Name

func (s Signal[T]) Name() string

Name is the signal's full name, interface and member, as a received dbus.Signal carries it.

type SignalDecl

type SignalDecl interface {
	// contains filtered or unexported methods
}

SignalDecl is a Signal as an Interface declares it, whatever it carries.

type Watch

type Watch struct {
	// contains filtered or unexported fields
}

Watch follows signals on a connection until its context ends, it is stopped, or the connection closes.

func WatchSignals

func WatchSignals(ctx context.Context, conn *dbus.Conn, from Sender, failed func(error), routes ...Route) (*Watch, error)

WatchSignals runs each route's handler, on the watch's goroutine and in arrival order, for the signals the routes name, when from sent them. failed is told, on the same goroutine, of each such signal a handler did not accept or that did not decode. A signal carrying descriptors reaches each watch of a connection with descriptors of its own: a handler that gets the signal owns them, and the watch closes those of every signal no handler gets and of the signals still unread when it ends. The watch holds match rules and a signal channel on conn until it ends, and then removes them, leaving a connection shared with others as it was.

func (*Watch) Done

func (w *Watch) Done() <-chan struct{}

Done is closed once the watch has ended and removed what it held.

func (*Watch) Stop

func (w *Watch) Stop() error

Stop ends the watch and returns once it has removed what it held: with ErrDisconnected when its connection had closed, with the error removing its match rules met, or nil. Calling it again, or after the watch's context ended, waits for the same end and reports the same error. Stop must not be called from a handler of the watch.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL