UNPKG

@blackmagic-controller/node

Version:

An npm module for interfacing with the Blackmagic usb/bluetooth controllers in node

98 lines 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeviceModelId = exports.VENDOR_ID = void 0; exports.listBlackmagicControllers = listBlackmagicControllers; exports.getBlackmagicControllerDeviceInfo = getBlackmagicControllerDeviceInfo; exports.getBlackmagicControllerInfo = getBlackmagicControllerInfo; exports.openBlackmagicController = openBlackmagicController; const core_1 = require("@blackmagic-controller/core"); const HID = require("node-hid"); const hid_device_js_1 = require("./hid-device.js"); const wrapper_js_1 = require("./wrapper.js"); var core_2 = require("@blackmagic-controller/core"); Object.defineProperty(exports, "VENDOR_ID", { enumerable: true, get: function () { return core_2.VENDOR_ID; } }); Object.defineProperty(exports, "DeviceModelId", { enumerable: true, get: function () { return core_2.DeviceModelId; } }); /** * Scan for and list detected devices */ async function listBlackmagicControllers() { const devices = {}; for (const dev of await HID.devicesAsync()) { if (dev.path && !devices[dev.path]) { const info = getBlackmagicControllerDeviceInfo(dev); if (info) devices[dev.path] = info; } } return Object.values(devices); } /** * If the provided device is a supported blackmagic controller, get the info about it */ function getBlackmagicControllerDeviceInfo(dev) { const model = core_1.DEVICE_MODELS.find((m) => m.productIds.includes(dev.productId)); if (model && dev.vendorId === core_1.VENDOR_ID && dev.path) { return { model: model.id, path: dev.path, serialNumber: dev.serialNumber, }; } else { return null; } } /** * Get the info of a device if the given path is a supported blackmagic controller */ async function getBlackmagicControllerInfo(path) { const allDevices = await listBlackmagicControllers(); return allDevices.find((dev) => dev.path === path); } /** * Open a supported blackmagic controller * @param devicePath The path of the device to open. * @param userOptions Options to customise the device behvaiour */ async function openBlackmagicController(devicePath, userOptions) { // const options: Required<OpenBlackmagicControllerOptions> = { // ...userOptions, // } let hidDevice; let model; try { hidDevice = await HID.HIDAsync.open(devicePath); const deviceInfo = await hidDevice.getDeviceInfo(); model = core_1.DEVICE_MODELS.find((m) => deviceInfo.vendorId === core_1.VENDOR_ID && m.productIds.includes(deviceInfo.productId)); if (!model) { throw new Error(`Device path at "${devicePath}" is not a supported Blackmagic controller.`); } } catch (e) { if (hidDevice) await hidDevice.close().catch(() => null); // Suppress error throw e; } let device; try { device = new hid_device_js_1.NodeHIDDevice(hidDevice); // Perform authentication if requried by the model let nextAuthMaxDelay = null; if (model.authenticate) { nextAuthMaxDelay = await model.authenticate(device); } const fullOptions = { // ...options, nextAuthMaxDelay, authenticate: model.authenticate ?? null, }; const rawSteamdeck = model.factory(device, fullOptions); return new wrapper_js_1.BlackmagicControllerNode(rawSteamdeck, userOptions?.clearOnClose ?? false); } catch (e) { if (device) await device.close().catch(() => null); // Suppress error throw e; } } //# sourceMappingURL=index.js.map