dbus-swift

Github Release GitHub Actions Workflow Status GitHub License Swift Version Compatibility Platform Compatibility

D-Bus 的 Swift 绑定。

文档

API 文档 可在 Swift Package Index 上找到。

示例

客户端

let connection = try Connection(type: .session)
try connection.setupDispatch(with: DispatchQueue.main)
let object = ObjectProxy(
  connection: connection,
  destination: "com.example.Foo",
  path: "/com/example/Foo"
)

// Call methods
let methods = object.methods(interface: "com.example.Foo")
// Using `dynamic member lookup` and `callAsFunction`, this is equivalent to
// `try await methods[dynamicMember: "Foo"].callAsFunction("one", 2, 3.0) as Void`
try await methods.Foo("one", 2 as Int32, 3.0) as Void  // no return value
let foo: [String] = try await methods.Bar("one", 2 as Int32, 3.0)  // single return value
let (bar, baz): (String, Int32) = try await methods.Baz("one", 2 as Int32, 3.0)  // multiple return values

// Get properties
let properties = object.properties(interface: "com.example.Foo")
let foo: String = try await properties.Foo.get()

// Set properties
try await properties.Foo.set("bar")

// Observe property changes
_ = try properties.Foo.observe { (newValue: String) in
  // ...
}

服务端

let connection = try Connection(type: .session, private: true)
try connection.setupDispatch(with: DispatchQueue.main)
let bus = Bus(connection: connection)
_ = try await bus.requestName("com.example.Foo", .doNotQueue)
let object = ObjectProxy(connection: connection, path: "/com/example/Foo")

// Handle method calls
let methods = object.methods(interface: "com.example.Foo")
func foo(a: String, b: Int32, c: Double) throws(DBus.Error) {
  // ...
}
_ = try methods.Foo.delegate(to: foo)
// or use a closure
_ = try methods.Bar.delegate {
  (a: String, b: Int32, c: Double) throws(DBus.Error) -> [String] in
  return ["qux", "quux"]
}
_ = try methods.Baz.delegate {
  (a: String, b: Int32, c: Double) throws(DBus.Error) -> (String, Int32) in
  return ("qux", 0)
}

// Provide properties
let properties = object.properties(interface: "com.example.Interface")
var foo: String = "foo"
_ = try properties.Foo.delegate(
  get: { foo },
  set: { newValue in foo = newValue }
)

// Notify property changes
try properties.Foo.didChange("bar")

信号

let connection = try Connection(type: .session)
try connection.setupDispatch(with: DispatchQueue.main)
let object = ObjectProxy(connection: connection, path: "/com/example/Foo")

// Emit signals
let signals = object.signals(interface: "com.example.Foo")
try signals.Foo.emit("one", 2 as Int32)

// Connect to signals
_ = try signals.Foo.connect { (a: String, b: Int32) in
  // ...
}

数据类型

遵循 Argument 协议,以下 Swift 类型可以用作 D-Bus 参数

Swift 类型 D-Bus 类型 D-Bus 签名 说明
Swift.UInt8 BYTE y
Swift.Bool BOOLEAN b
Swift.Int16 INT16 n
Swift.UInt16 UINT16 q
Swift.Int32 INT32 i
Swift.UInt32 UINT32 u
Swift.Int64 INT64 x
Swift.UInt64 UINT64 t
Swift.Double DOUBLE d
Swift.String STRING s
DBus.ObjectPath OBJECT_PATH o
DBus.Signature SIGNATURE g
Swift.Array ARRAY a Array.Element 是某种 Argument
DBus.DictEntry DICT_ENTRY {kv}
Swift.Dictionary ARRAY of DICT_ENTRY a{kv} Dictionary.Key/Value 是某种 Argument
DBus.Struct STRUCT (...)
DBus.AnyStruct STRUCT (...)
DBus.Variant VARIANT v
DBus.FileDescriptor UNIX_FD h

自定义类型可以通过遵循 Argument 协议用作 D-Bus 参数。

有时,我们无法在编译时知道参数类型,请考虑使用 DBus.AnyArgument

CDBus

CDBus 目标是 libdbus 的 C 模块。它被 DBus 目标用于提供 D-Bus 的 Swift 绑定。

默认情况下,CDBus 使用 vendored libdbus 源代码。您还可以通过将 CDBUS_SYSTEM 定义添加到构建设置来使用系统 libdbus

swift build -Xswiftc -DCDBUS_SYSTEM

许可证

MIT 许可证