2022-11-21 19:59:54 +00:00
|
|
|
package utils
|
2021-11-24 19:36:36 +00:00
|
|
|
|
2022-05-11 20:24:12 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2021-11-24 19:36:36 +00:00
|
|
|
|
2022-05-11 20:24:12 +00:00
|
|
|
"github.com/godbus/dbus/v5"
|
|
|
|
)
|
|
|
|
|
2022-11-21 19:59:54 +00:00
|
|
|
func NewSystemBusConn(ctx context.Context) (*dbus.Conn, error) {
|
2021-11-24 19:36:36 +00:00
|
|
|
// Connect to dbus session bus
|
2022-05-11 20:24:12 +00:00
|
|
|
conn, err := dbus.SystemBusPrivate(dbus.WithContext(ctx))
|
2021-11-24 19:36:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = conn.Auth(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = conn.Hello()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 19:59:54 +00:00
|
|
|
func NewSessionBusConn(ctx context.Context) (*dbus.Conn, error) {
|
2021-11-24 19:36:36 +00:00
|
|
|
// Connect to dbus session bus
|
2022-05-11 20:24:12 +00:00
|
|
|
conn, err := dbus.SessionBusPrivate(dbus.WithContext(ctx))
|
2021-11-24 19:36:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = conn.Auth(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = conn.Hello()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|