|
| 1 | +/** |
| 2 | + * com.discord.streamdeck — plugin.cjs |
| 3 | + * |
| 4 | + * Runs as a child process forked by the host app. |
| 5 | + * Controls Discord via xdotool keyboard shortcuts. |
| 6 | + * |
| 7 | + * Each button stores a `hotkey` in its settings (e.g. "ctrl+shift+m"). |
| 8 | + * On keyDown the plugin fires `xdotool key <hotkey>`. |
| 9 | + * Push-to-Talk and Push-to-Mute use `xdotool keydown` / `xdotool keyup` |
| 10 | + * so the key is held for the duration of the button press. |
| 11 | + * |
| 12 | + * Requirements: xdotool must be installed (sudo apt install xdotool). |
| 13 | + */ |
| 14 | + |
| 15 | +'use strict' |
| 16 | + |
| 17 | +const { spawnSync, execFileSync } = require('child_process') |
| 18 | + |
| 19 | +const pluginUUID = process.env.PLUGIN_UUID || 'com.discord.streamdeck' |
| 20 | + |
| 21 | +// Actions that use hold-and-release rather than a single tap |
| 22 | +const HOLD_ACTIONS = new Set([ |
| 23 | + 'com.discord.streamdeck.ptt', |
| 24 | + 'com.discord.streamdeck.ptm', |
| 25 | +]) |
| 26 | + |
| 27 | +// Verify xdotool is available at startup |
| 28 | +let xdotoolAvailable = false |
| 29 | +try { |
| 30 | + execFileSync('which', ['xdotool'], { stdio: 'ignore' }) |
| 31 | + xdotoolAvailable = true |
| 32 | + console.log(`[${pluginUUID}] xdotool found — ready`) |
| 33 | +} catch { |
| 34 | + console.warn(`[${pluginUUID}] WARNING: xdotool not found. Install it with: sudo apt install xdotool`) |
| 35 | +} |
| 36 | + |
| 37 | +function xdotoolKey(hotkey) { |
| 38 | + if (!xdotoolAvailable) return |
| 39 | + const result = spawnSync('xdotool', ['key', hotkey], { stdio: 'pipe' }) |
| 40 | + if (result.status !== 0) { |
| 41 | + console.warn(`[${pluginUUID}] xdotool key failed for "${hotkey}":`, result.stderr?.toString().trim()) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function xdotoolKeyDown(hotkey) { |
| 46 | + if (!xdotoolAvailable) return |
| 47 | + const result = spawnSync('xdotool', ['keydown', hotkey], { stdio: 'pipe' }) |
| 48 | + if (result.status !== 0) { |
| 49 | + console.warn(`[${pluginUUID}] xdotool keydown failed for "${hotkey}":`, result.stderr?.toString().trim()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function xdotoolKeyUp(hotkey) { |
| 54 | + if (!xdotoolAvailable) return |
| 55 | + const result = spawnSync('xdotool', ['keyup', hotkey], { stdio: 'pipe' }) |
| 56 | + if (result.status !== 0) { |
| 57 | + console.warn(`[${pluginUUID}] xdotool keyup failed for "${hotkey}":`, result.stderr?.toString().trim()) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +process.on('message', (msg) => { |
| 62 | + if (!msg?.event) return |
| 63 | + |
| 64 | + const hotkey = msg.settings?.hotkey |
| 65 | + const actionUUID = msg.actionUUID || '' |
| 66 | + |
| 67 | + if (msg.event === 'keyDown') { |
| 68 | + if (!hotkey) { |
| 69 | + console.warn(`[${pluginUUID}] keyDown on ${actionUUID} — no hotkey configured`) |
| 70 | + return |
| 71 | + } |
| 72 | + console.log(`[${pluginUUID}] keyDown ${actionUUID} → ${HOLD_ACTIONS.has(actionUUID) ? 'keydown' : 'key'} "${hotkey}"`) |
| 73 | + if (HOLD_ACTIONS.has(actionUUID)) { |
| 74 | + xdotoolKeyDown(hotkey) |
| 75 | + } else { |
| 76 | + xdotoolKey(hotkey) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (msg.event === 'keyUp') { |
| 81 | + if (!hotkey) return |
| 82 | + if (HOLD_ACTIONS.has(actionUUID)) { |
| 83 | + console.log(`[${pluginUUID}] keyUp ${actionUUID} → keyup "${hotkey}"`) |
| 84 | + xdotoolKeyUp(hotkey) |
| 85 | + } |
| 86 | + } |
| 87 | +}) |
| 88 | + |
| 89 | +// Keep the process alive |
| 90 | +setInterval(() => {}, 60_000) |
0 commit comments