File: prop.go

package info (click to toggle)
golang-dbus 5.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, bookworm-proposed-updates, forky, sid, trixie
  • size: 592 kB
  • sloc: makefile: 2
file content (102 lines) | stat: -rw-r--r-- 2,317 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main

import (
	"fmt"
	"os"

	"github.com/godbus/dbus/v5"
	"github.com/godbus/dbus/v5/introspect"
	"github.com/godbus/dbus/v5/prop"
)

type foo string

func (f foo) Foo() (string, *dbus.Error) {
	fmt.Println(f)
	return string(f), nil
}

type Foo struct {
	Id    int
	Value string
}

func main() {
	conn, err := dbus.ConnectSessionBus()
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	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
				},
			},
			"FooStruct": {
				Foo{Id: 1, Value: "First"},
				true,
				prop.EmitTrue,
				func(c *prop.Change) *dbus.Error {
					var foo Foo
					err := dbus.Store([]interface{}{c.Value}, &foo)
					if err != nil {
						_, _ = fmt.Fprintf(os.Stderr, "dbus.Store foo failed: %v\n", err)
					}
					fmt.Println(c.Name, "changed to", foo)
					return nil
				},
			},
		},
	}
	f := foo("Bar")
	err = conn.Export(f, "/com/github/guelfey/Demo", "com.github.guelfey.Demo")
	if err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "export f failed: %v\n", err)
		os.Exit(1)
	}
	props, err := prop.Export(conn, "/com/github/guelfey/Demo", propsSpec)
	if err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "export propsSpec failed: %v\n", err)
		os.Exit(1)
	}
	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"),
			},
		},
	}
	err = conn.Export(introspect.NewIntrospectable(n), "/com/github/guelfey/Demo",
		"org.freedesktop.DBus.Introspectable")
	if err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "export introspect failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("Listening on com.github.guelfey.Demo / /com/github/guelfey/Demo ...")

	c := make(chan *dbus.Signal)
	conn.Signal(c)
	for range c {
	}
}