Пример #1
0
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 {
	}
}
Пример #2
0
func (t *TransportDbus) export(obj ObjectDbus, p dbus.ObjectPath) error {
	n := obj.Introspect()

	for _, i := range n.Interfaces {
		if err := t.conn.Export(obj, p, i.Name); err != nil {
			return err
		}
	}

	if err := t.conn.Export(introspect.NewIntrospectable(n), p, "org.freedesktop.DBus.Introspectable"); err != nil {
		return err
	}

	return nil
}
Пример #3
0
func init() {
	var err error
	flag.Parse()
	if *logFile != "" {
		f, err := os.Open(*logFile)
		defer f.Close()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		l = log.New(f, "", log.LstdFlags)
	} else {
		l = log.New(os.Stderr, "screenwatchd ", log.LstdFlags)
	}

	conn, err = dbus.SystemBus()
	if err != nil {
		l.Fatalln(err)
	}

	xrandr, err = exec.LookPath("xrandr")
	if err != nil {
		l.Fatalf("couldn't find xrandr: %v\n", err)
	}

	if *verbose {
		l.Printf("parsing '%s'\n", *positionalArgs)
	}
	s = swatch{
		introspect.NewIntrospectable(screenwatch.Introspect),
		make(map[string][]string),
		make(map[string]bool),
	}
	for _, a := range strings.Split(*positionalArgs, ",") {
		arg := strings.Split(a, ":")
		if _, exist := s.pos[arg[0]]; exist {
			l.Printf("duplicate display argument '%s:%s', discarding\n", arg[0], arg[1])
			continue
		}
		if *verbose {
			l.Printf("watching for %s(%s)\n", arg[0], mangle(arg[0]))
		}
		s.pos[arg[0]] = strings.Fields(arg[1])
	}
	for d, _ := range s.pos {
		var status string
		f, err := os.Open(fmt.Sprint("/sys/class/drm/", d, "/status"))
		if err != nil {
			l.Fatalln(err)
		}
		_, err = fmt.Fscanf(f, "%s\n", &status)
		if err != nil {
			l.Fatalln(err)
		}
		switch status {
		case "connected":
			s.state[d] = true
		case "disconnected":
			s.state[d] = false
		default:
			l.Fatalf("unknown status '%s', bailing\n", status)
		}
	}
	if *verbose {
		fmt.Printf("positional arguments: %v\n", s.pos)
	}
}