Documentation
¶
Overview ¶
Package notifications is a pure-Go (CGO_ENABLED=0) implementation of the freedesktop.org Desktop Notifications Specification -- the D-Bus service org.freedesktop.Notifications that desktop applications call (via notify-send, libnotify, GLib.Notification, ...) to post transient notification bubbles.
The package is split into a platform-neutral core (this file plus notification.go, hints.go and image.go) that models a Notification and decodes the wire-level hint dictionary and image payloads, a Linux D-Bus server (server_linux.go) that exports the four spec methods and emits the two spec signals over the owned pure-Go github.com/go-freedesktop/dbus, and a pure bridge (./toast) that turns a decoded Notification into a github.com/go-widgets Toast widget so a go-widgets desktop can act as the notification daemon.
Specification: https://specifications.freedesktop.org/notification-spec/latest/
Index ¶
Constants ¶
const ( // BusName is the well-known bus name a notification daemon owns. BusName = "org.freedesktop.Notifications" // ObjectPath is the object path the service is exported at. ObjectPath = "/org/freedesktop/Notifications" // Interface is the D-Bus interface the four methods and two signals // belong to. Interface = "org.freedesktop.Notifications" )
Well-known D-Bus name, object path and interface of the notification service, as mandated by the specification.
const ( MethodNotify = "Notify" MethodCloseNotification = "CloseNotification" MethodGetCapabilities = "GetCapabilities" MethodGetServerInfo = "GetServerInformation" SignalNotificationClosed = "NotificationClosed" SignalActionInvoked = "ActionInvoked" )
The four method member names of the interface.
const ( ServerName = "go-widgets-notifyd" VendorName = "go-freedesktop" Version = "0.2.0" SpecVersion = "1.2" )
Server-information values reported by GetServerInformation. SpecVersion is the version of the notification specification the server implements.
Variables ¶
var ( // ErrBadImageData is returned by decodeImageData when the inline // image-data hint is not a well-formed (iiibiiay) payload. ErrBadImageData = errors.New("notifications: malformed image-data hint") // ErrVectorIcon is returned by LoadImagePath for a scalable (SVG) path: // this pure-raster decoder does not rasterise vector art, so the caller // should fall back to an icon-theme raster lookup or drop the image. ErrVectorIcon = errors.New("notifications: vector (svg) icon path not rasterised") )
Errors returned by the image decoders.
var ErrNameTaken = errors.New("notifications: another daemon already owns " + BusName)
ErrNameTaken is returned by Export when another process already owns the org.freedesktop.Notifications name (a notification daemon is already running).
Functions ¶
func Capabilities ¶
func Capabilities() []string
Capabilities returns the notification capabilities this implementation advertises through GetCapabilities. The list is deliberately TRUTHFUL: it names only what the ./toast bridge actually renders --
- "body" : the notification carries a body text distinct from the summary (rendered as a second Toast line);
- "actions" : action buttons are rendered and ActionInvoked is emitted when one is clicked;
- "body-markup" : the body may contain the small hypertext-subset markup the spec defines (the bridge strips the tags to plain text);
- "icon-static" : a static (non-animated) icon or image is rendered beside the text;
- "persistence" : a notification with an infinite / resident lifetime stays on screen until dismissed (a sticky Toast).
Capabilities we do NOT render (sound, action-icons, icon-multi, ...) are omitted so a client is never misled about what the daemon can do.
func DecodeHints ¶
func DecodeHints(n *Notification, hints map[string]dbus.Variant)
DecodeHints folds the Notify hint dictionary into n. Every field is decoded defensively: a missing key leaves the corresponding field at its zero value, and a value of the wrong D-Bus type is ignored rather than propagated as an error, so a malformed client can never break decoding. A nil map is a no-op.
func LoadImagePath ¶
LoadImagePath decodes an on-disk image referenced by a notification "image-path" hint (or a resolved app_icon path) into an *image.RGBA. A "file://" URI prefix is accepted and stripped. Raster formats (PNG, JPEG, GIF) are decoded through the standard library; a ".svg" path returns ErrVectorIcon because this decoder is deliberately pure-raster and drags in no SVG rasteriser. A missing or undecodable file returns the underlying error.
Types ¶
type Action ¶
Action is one entry of the Notify "actions" array: a machine Key the client keys ActionInvoked on, plus the human-readable Label rendered on the button. The reserved key "default" marks the action taken on a plain activation (a click on the notification body rather than a specific button).
type CloseReason ¶
type CloseReason uint32
CloseReason is the reason code carried by the NotificationClosed signal, as enumerated by the specification.
const ( // ReasonExpired: the notification's timeout elapsed. ReasonExpired CloseReason = 1 // ReasonDismissed: the user dismissed the notification. ReasonDismissed CloseReason = 2 // ReasonClosed: the notification was closed by a CloseNotification call. ReasonClosed CloseReason = 3 // ReasonUndefined: closed for some other/undefined reason. ReasonUndefined CloseReason = 4 )
func (CloseReason) String ¶
func (r CloseReason) String() string
String names the close reason for diagnostics.
type Handler ¶
type Handler interface {
OnNotify(n *Notification) uint32
OnClose(id uint32, reason CloseReason)
}
Handler is the application-side callback set a Server drives. OnNotify is invoked for each incoming Notify (the Notification already carries its server-assigned, non-zero ID); it renders the notification and returns the id the caller should see (normally n.ID). OnClose is invoked when a notification is closed -- by a CloseNotification call, an expiry or a dismissal -- so the handler can retire the on-screen surface.
type Notification ¶
type Notification struct {
// ID is the notification id assigned by the server (0 until assigned).
ID uint32
// AppName is the optional application name (first Notify argument).
AppName string
// ReplacesID is the id of a notification this one replaces (0 = none).
ReplacesID uint32
// AppIcon is the app_icon argument: an icon name (to resolve through an
// icon theme) or a file/URI path.
AppIcon string
// Summary is the single-line title (required by the spec).
Summary string
// Body is the optional multi-line body; it may carry the spec's
// hypertext-subset markup.
Body string
// Actions are the parsed action pairs, in order.
Actions []Action
// ExpireMS is the requested timeout in milliseconds: -1 = server
// default, 0 = never expire (persist until dismissed / closed).
ExpireMS int32
// Urgency is the decoded "urgency" hint (defaults to UrgencyNormal).
Urgency Urgency
// Category is the decoded "category" hint (may be empty).
Category string
// DesktopEntry is the decoded "desktop-entry" hint (may be empty).
DesktopEntry string
// Resident is the decoded "resident" hint: the notification is not
// removed after an action is invoked.
Resident bool
// Transient is the decoded "transient" hint: bypass any persistence.
Transient bool
// Image is the inline image supplied through an "image-data" /
// "image_data" / "icon_data" hint (nil when none was supplied).
Image *image.RGBA
// ImagePath is the decoded "image-path" / "image_path" hint: a path or
// icon name pointing at an image on disk (may be empty).
ImagePath string
}
Notification is a decoded org.freedesktop.Notifications.Notify request: the spec's positional arguments plus everything unpacked from its hint dictionary. It is the platform-neutral value the server hands to a Handler and the ./toast bridge turns into a Toast.
func Decode ¶
func Decode(appName string, replacesID uint32, appIcon, summary, body string, actions []string, hints map[string]dbus.Variant, expireMS int32) *Notification
Decode assembles a Notification from the raw positional arguments of a Notify call and its hint dictionary. It never fails: malformed hints are ignored field by field (see DecodeHints) so a hostile or buggy client can never break the daemon.
func (*Notification) Sticky ¶
func (n *Notification) Sticky() bool
Sticky reports whether the notification should persist on screen until the user acts, rather than auto-expiring: an explicit zero timeout, a resident hint, or critical urgency all make it sticky (unless it is transient).
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server exports the org.freedesktop.Notifications service on a D-Bus connection and forwards decoded requests to a Handler. It is safe for concurrent use: the id allocator is mutex-guarded.
The three fallible D-Bus operations the server performs -- exporting the method object, claiming the well-known name, and emitting a signal -- are indirected through function fields (exportFn, requestNameFn, emitFn) so the method handlers can be unit-tested as plain Go calls, with injected fakes covering every branch, without ever bringing up a bus or a connection.
func NewServer ¶
NewServer returns a Server that will export the notification service on conn and forward requests to h.
func (*Server) EmitActionInvoked ¶
EmitActionInvoked emits the ActionInvoked signal binding action key to id.
func (*Server) EmitClosed ¶
func (s *Server) EmitClosed(id uint32, reason CloseReason) error
EmitClosed emits the NotificationClosed signal for id with the given reason.
func (*Server) Export ¶
Export publishes the four notification methods on conn at ObjectPath and claims the well-known BusName. It returns ErrNameTaken if the name is already owned, or the underlying error if exporting or the name request fails.
The org.freedesktop.DBus.Introspectable interface is served automatically by the connection (it reflects the exported methods into an introspection document), so no introspection node is registered here.
type Urgency ¶
type Urgency byte
Urgency is the freedesktop "urgency" hint: a byte ranking how much the notification demands attention. The spec defines exactly three levels.
const ( // UrgencyLow is background information the user need not act on. UrgencyLow Urgency = 0 // UrgencyNormal is the default level for ordinary notifications. UrgencyNormal Urgency = 1 // UrgencyCritical must stay visible until the user acts (it maps to a // resident, error-coloured Toast). UrgencyCritical Urgency = 2 )
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
notifyd
command
Command notifyd is the reference go-widgets notification daemon: it owns the org.freedesktop.Notifications name on the session bus and renders each incoming notification as a go-widgets Toast, driven by the github.com/go-freedesktop/notifications server and its ./toast bridge.
|
Command notifyd is the reference go-widgets notification daemon: it owns the org.freedesktop.Notifications name on the session bus and renders each incoming notification as a go-widgets Toast, driven by the github.com/go-freedesktop/notifications server and its ./toast bridge. |
|
Package toast bridges a decoded freedesktop github.com/go-freedesktop/notifications.Notification onto a github.com/go-widgets/toolkit.Toast widget, so a go-widgets desktop can render notification bubbles and act as the notification daemon.
|
Package toast bridges a decoded freedesktop github.com/go-freedesktop/notifications.Notification onto a github.com/go-widgets/toolkit.Toast widget, so a go-widgets desktop can render notification bubbles and act as the notification daemon. |