@homebridge/dbus-native
Version:
D-bus protocol implementation in native javascript
56 lines (52 loc) • 1.58 kB
JavaScript
// read dbus address from X11 window selection
// Requires the optional `x11` peer dependency.
const fs = require('fs');
const os = require('os');
function getDbusAddress(callback) {
var x11;
try {
x11 = require('x11');
} catch (e) {
return callback(
new Error(
"address-x11 requires the optional 'x11' package; install it with `npm install x11`"
)
);
}
// read machine uuid
fs.readFile('/var/lib/dbus/machine-id', 'ascii', function (err, uuid) {
if (err) return callback(err);
var hostname = os.hostname().split('-')[0];
x11.createClient(function (err, display) {
var X = display.client;
var selectionName = `_DBUS_SESSION_BUS_SELECTION_${hostname}_${uuid.trim()}`;
X.InternAtom(false, selectionName, function (err, id) {
if (err) return callback(err);
X.GetSelectionOwner(id, function (err, win) {
if (err) return callback(err);
X.InternAtom(
false,
'_DBUS_SESSION_BUS_ADDRESS',
function (err, propId) {
if (err) return callback(err);
win = display.screen[0].root;
X.GetProperty(
0,
win,
propId,
0,
0,
10000000,
function (err, val) {
if (err) return callback(err);
callback(null, val.data.toString());
}
);
}
);
});
});
});
});
}
module.exports = getDbusAddress;