diff --git a/src/index.js b/src/index.js index 52c2582..0458d86 100755 --- a/src/index.js +++ b/src/index.js @@ -1,84 +1,11 @@ #!/bin/env node const { execSync, spawnSync } = require("child_process"); -const chroma = require("chroma-js"); const suncalc = require("suncalc"); -const util = require("util"); -const dbus = require("dbus"); -const bus = dbus.getBus("session"); -const SERVICE_NAME = "org.razer"; -const OBJECT_PATH_DEVICES = "/org/razer"; -const OBJECT_PATH_DEVICE = "/org/razer/device/"; -const DEVICE = "IO2033F28201257"; +const dbus = require("./dbus"); +const { colors, brightness } = require("./colors"); const DANGEROUS_VOLUME = 40; -const BASE_COLOR = [186, 81, 0]; -const WARNING_COLOR = [150, 30, 30]; -const ALERT_COLOR = [250, 10, 10]; -const SAFE_COLOR = [10, 220, 30]; -const BLUE = [10, 20, 200]; const AUDIO_NAME = "analog-surround-51"; const MIC_NAME = "C-Media_Electronics_Inc._USB_PnP_Sound_Device"; -const INTERFACES = { - DEVICES: { - iface: "razer.devices", - methods: ["getDevices"], - path: OBJECT_PATH_DEVICES, - }, - BRIGHTNESS: { - iface: "razer.device.lighting.brightness", - methods: ["setBrightness", "getBrightness"], - path: OBJECT_PATH_DEVICE + DEVICE, - }, - CHROMA: { - iface: "razer.device.lighting.chroma", - methods: [ - "setBreathDual", - "setBreathRandom", - "setBreathSingle", - "setCustom", - "setKeyRow", - "setNone", - "setReactive", - "setSpectrum", - "setStarlightDual", - "setStarlightRandom", - "setStarlightSingle", - "setStatic", - "setWave", - ], - path: OBJECT_PATH_DEVICE + DEVICE, - }, - CUSTOM: { - iface: "razer.device.lighting.custom", - methods: ["setRipple", "setRippleRandomColour"], - path: OBJECT_PATH_DEVICE + DEVICE, - }, - MACRO: { - iface: "razer.device.macro", - methods: ["addMacro", "deleteMacro", "getMacros"], - path: OBJECT_PATH_DEVICE + DEVICE, - }, - MISC: { - iface: "razer.device.misc", - methods: [ - "getDeviceMode", - "getDeviceName", - "getDeviceType", - "getDriverVersion", - "getFirmware", - "getKeyboardLayout", - "getMatrixDimensions", - "getRazerUrls", - "getSerial", - "getVidPid", - "hasDedicatedMacroKeys", - "hasMatrix", - "resumeDevice", - "setDeviceMode", - "suspendDevice", - ], - path: OBJECT_PATH_DEVICE + DEVICE, - }, -}; function isNight() { const now = new Date(); @@ -87,52 +14,19 @@ function isNight() { return isnight; } -const getInterface = util.promisify(bus.getInterface).bind(bus); -const interfaces = {}; -function bindPromisify(obj, ...fn) { - fn.forEach(fn => (obj[fn] = util.promisify(obj[fn]).bind(obj))); -} -async function initializeDeviceInterfaces() { - const p = Object.entries(INTERFACES).map( - async ([label, { iface, path, methods }]) => { - const interface = await getInterface(SERVICE_NAME, path, iface); - bindPromisify(interface, ...methods); - interfaces[label.toLowerCase()] = interface; - }, - ); - await Promise.all(p); -} -async function initialize() { - await initializeDeviceInterfaces(); -} - -function staticMatrix(r, g, b, rows, cols) { - const color = [r, g, b]; - const row = [].concat(00, 0x15, ...new Array(cols).fill(color)); - const matrix = []; - for (let i = 0; i < rows; ++i) { - matrix.push(...[i, ...row]); - } - matrix.rows = rows; - matrix.cols = cols; - return matrix; -} -function setKey(matrix, row, col, r, g, b) { - const idx = row * (col + 1) * 3 + (col + 1) * 3; - matrix[idx] = r; - matrix[idx + 1] = g; - matrix[idx + 2] = b; -} - function setPlaying(matrix) { const playing = !spawnSync("mpc | grep playing", { shell: true }).status; [18, 19, 20].forEach(x => - setKey(matrix, 0, x, ...(playing ? ALERT_COLOR : BASE_COLOR)), + dbus.setKey( + matrix, + 0, + x, + ...(playing ? colors.ALERT_COLOR.rgb() : colors.BASE_COLOR.rgb()), + ), ); } function getBrightness() { - console.log(isNight()); return isNight() ? 60 : 100; } const volume = @@ -152,8 +46,14 @@ async function setMutedMic() { `pacmd dump | grep ^set-source-mute.*${MIC_NAME}.*yes`, { shell: true }, ).status; - await interfaces.brightness.setBrightness(muted ? getBrightness() : 100); - return staticMatrix(...(muted ? BASE_COLOR : ALERT_COLOR), 6, 22); + const b = muted ? getBrightness() : 100; + return dbus.staticMatrix( + ...(muted + ? brightness(colors.BASE_COLOR, b).rgb() + : brightness(colors.ALERT_COLOR, b).rgb()), + 6, + 22, + ); } async function setAudio(matrix) { @@ -163,9 +63,11 @@ async function setAudio(matrix) { { shell: true }, ).status; if (muted) { - keys.forEach(x => setKey(matrix, 0, x, ...SAFE_COLOR)); + keys.forEach(x => + dbus.setKey(matrix, 0, x, ...colors.SAFE_COLOR.rgb()), + ); } else if (volume === 0) { - keys.forEach(x => setKey(matrix, 0, x, ...BLUE)); + keys.forEach(x => dbus.setKey(matrix, 0, x, ...colors.BLUE.rgb())); } else { const volume = parseInt( spawnSync( @@ -173,21 +75,25 @@ async function setAudio(matrix) { { shell: true }, ).stdout, ); - const c = chroma(volume < DANGEROUS_VOLUME ? BASE_COLOR : ALERT_COLOR) - .set("hsv.v", volume / 50) - .rgb(); - keys.forEach(x => setKey(matrix, 0, x, ...c)); + const c = brightness( + volume < DANGEROUS_VOLUME + ? colors.BASE_COLOR.rgb() + : colors.ALERT_COLOR.rgb(), + volume / 50, + ); + keys.forEach(x => dbus.setKey(matrix, 0, x, ...c.rgb())); } } async function run() { - await initialize(); + const interfaces = await dbus.connect(); const matrix = await setMutedMic(); setAudio(matrix); setPlaying(matrix); + await interfaces.brightness.setBrightness(100); await interfaces.chroma.setKeyRow(matrix); - bus.disconnect(); + dbus.disconnect(); } run();