func main() { conn, err := dbus.SessionBus() if err != nil { panic(err) } reply, err := conn.RequestName("com.github.guelfey.Demo", dbus.NameFlagDoNotQueue) if err != nil { panic(err) } if reply != dbus.RequestNameReplyPrimaryOwner { fmt.Fprintln(os.Stderr, "name already taken") os.Exit(1) } propsSpec := map[string]map[string]*prop.Prop{ "com.github.guelfey.Demo": { "SomeInt": { int32(0), true, prop.EmitTrue, func(c *prop.Change) *dbus.Error { fmt.Println(c.Name, "changed to", c.Value) return nil }, }, }, } f := foo("Bar") conn.Export(f, "/com/github/guelfey/Demo", "com.github.guelfey.Demo") props := prop.New(conn, "/com/github/guelfey/Demo", propsSpec) n := &introspect.Node{ Name: "/com/github/guelfey/Demo", Interfaces: []introspect.Interface{ introspect.IntrospectData, prop.IntrospectData, { Name: "com.github.guelfey.Demo", Methods: introspect.Methods(f), Properties: props.Introspection("com.github.guelfey.Demo"), }, }, } conn.Export(introspect.NewIntrospectable(n), "/com/github/guelfey/Demo", "org.freedesktop.DBus.Introspectable") fmt.Println("Listening on com.github.guelfey.Demo / /com/github/guelfey/Demo ...") c := make(chan *dbus.Signal) conn.Signal(c) for _ = range c { } }
// Introspect provides introspection data for the DBus service to start. // // propsSpec example: // // var propsSpec = map[string]map[string]*prop.Prop{ // SrvObj: { // "Restart": { // int32(0), // true, // prop.EmitTrue, // func(c *prop.Change) *dbus.Error { // fmt.Println(c.Name, "changed to", c.Value) // return nil // }, // }, // }, // } // func (load *Server) Introspect(obj interface{}, propsSpec map[string]map[string]*prop.Prop) *introspect.Node { var props []introspect.Property if propsSpec != nil { tmp := prop.New(load.Conn, dbus.ObjectPath(load.srvPath), propsSpec) props = tmp.Introspection(load.srvObj) } return &introspect.Node{ Name: load.srvPath, Interfaces: []introspect.Interface{ prop.IntrospectData, { Name: load.srvObj, Methods: introspect.Methods(obj), Properties: props, }, }, } }
func Run(p *player.Player) error { conn, err := dbus.SessionBus() if err != nil { return fmt.Errorf("Could not get session bus: %s", err) } reply, err := conn.RequestName(bus_name, dbus.NameFlagDoNotQueue) if err != nil { return fmt.Errorf("Could not request name: %s", err) } if reply != dbus.RequestNameReplyPrimaryOwner { return fmt.Errorf("Name already take") } bus_props_spec := map[string]map[string]*prop.Prop{ bus_interface_player: { "PlaybackStatus": { p.Status.String(), false, prop.EmitTrue, nil, }, "LoopStatus": { "none", true, prop.EmitTrue, SetLoopStatus, }, "TrackMetadata": { map[string]string{}, false, prop.EmitTrue, nil, }, "TrackPath": { "", false, prop.EmitTrue, nil, }, "TrackLength": { float64(0), false, prop.EmitTrue, nil, }, "TrackTitle": { "", false, prop.EmitTrue, nil, }, "Tracks": { []string{}, false, prop.EmitTrue, nil, }, "Volume": { 0.0, true, prop.EmitTrue, SetVolume, }, }, } bus = &Bus{ player: p, props: prop.New(conn, bus_path, bus_props_spec), } conn.Export(bus, bus_path, bus_interface_player) p.HandleStatusChange = HandleStatusChange p.HandleTrackChange = HandleTrackChange p.HandleTracksChange = HandleTracksChange p.HandleVolumeChange = HandleVolumeChange introspect := introspect.Introspectable(bus_introspection) conn.Export(introspect, bus_path, bus_interface_introspect) return nil }