43 lines
1.1 KiB
TypeScript

import Gio from "gi://Gio";
import GObject, { register } from "astal/gobject";
import { getDbusXml } from "@/utils";
let dbusConnectionResolve: ((connection: any) => void) | null = null;
export const Connection: Promise<any> = new Promise<any>(
(resolve) => (dbusConnectionResolve = resolve),
);
const ownerId = Gio.bus_own_name(
Gio.BusType.SESSION,
"dev.ezri.VoidShell",
Gio.BusNameOwnerFlags.NONE,
(connection) => {
dbusConnectionResolve?.(connection);
},
() => {},
() => {},
);
@register()
export class DBusObject extends GObject.Object {
protected dbusObj: Gio.DBusExportedObject | null = null;
#objectPath: string;
constructor(iface: string, objectPath: string) {
super();
this.#objectPath = objectPath;
getDbusXml(iface).then(async (xml) => {
try {
this.dbusObj = Gio.DBusExportedObject.wrapJSObject(xml, this);
this.dbusObj.export(await Connection, objectPath);
} catch (e) {
console.error(`Error exporting to D-Bus: ${e}`);
}
});
}
get objectPath(): string {
return this.#objectPath;
}
}