|
| 1 | +import { DBusConfig } from "./index"; |
| 2 | +import * as dbus from "dbus-next"; |
| 3 | +import { MessageBus, ProxyObject, ClientInterface, Variant } from "dbus-next"; |
| 4 | + |
| 5 | +export class DBusClient { |
| 6 | + public readonly session: MessageBus; |
| 7 | + public readonly system: MessageBus; |
| 8 | + |
| 9 | + constructor(config: DBusConfig) { |
| 10 | + this.session = dbus.sessionBus(config); |
| 11 | + this.system = dbus.systemBus(); |
| 12 | + } |
| 13 | + |
| 14 | + public async proxy<T>(config: DBusProxyConfig<T>): Promise<T> { |
| 15 | + const proxy = await (config.system ? this.system : this.session).getProxyObject(config.iface, config.path); |
| 16 | + return config.create(this, proxy); |
| 17 | + } |
| 18 | + |
| 19 | + static createClient(config: DBusConfig): DBusClient { |
| 20 | + return new DBusClient(config); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// Required, so we don't have circular imports. |
| 25 | +export interface DBusProxyConfig<T> { |
| 26 | + iface: string; |
| 27 | + path: string; |
| 28 | + system: boolean; |
| 29 | + create(client: DBusClient, proxy: ProxyObject): T; |
| 30 | +} |
| 31 | + |
| 32 | +export class DBusObject { |
| 33 | + protected readonly client: DBusClient; |
| 34 | + protected readonly proxy: ProxyObject; |
| 35 | + private readonly properties: RatBagPropertyInterface; |
| 36 | + |
| 37 | + protected constructor(client: DBusClient, proxy: ProxyObject) { |
| 38 | + this.client = client; |
| 39 | + this.proxy = proxy; |
| 40 | + this.properties = proxy.getInterface("org.freedesktop.DBus.Properties") as RatBagPropertyInterface; |
| 41 | + } |
| 42 | + |
| 43 | + async getProperty(iface: string, name: string): Promise<Variant> { |
| 44 | + return await this.properties.Get(iface, name); |
| 45 | + } |
| 46 | + |
| 47 | + async setProperty(iface: string, name: string, value: Variant): Promise<void> { |
| 48 | + return await this.properties.Set(iface, name, value); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +type RatBagPropertyInterface = ClientInterface & { |
| 53 | + Get(iface: string, path: string): Promise<Variant>; |
| 54 | + Set(iface: string, path: string, value: Variant): Promise<void>; |
| 55 | +}; |
0 commit comments