feat(display): add display_control CoreGraphics binary, replace displayplacer as primary path

Derived from OSIT MasterKey app (LegacyUtilities.m, Ian Kohl 2019).
Uses CGConfigureDisplayMirrorOfDisplay — native macOS API, no Homebrew dependency.
Handler now tries display_control first; falls back to displayplacer for missing binary
or per-device configStr overrides. Build the binary via scripts/build-display-control.sh
on a Mac (requires Xcode CLT only), then commit resources/bin/display_control.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Scott Idem
2026-05-20 16:48:19 -04:00
parent 9df9d884e5
commit a14c7c7a3f
6 changed files with 267 additions and 51 deletions

View File

@@ -318,34 +318,48 @@ export function registerSystemHandlers() {
return { success: false, error: 'Unknown action' };
});
// 6. Set Display Layout (Displayplacer)
// Auto-detects connected displays via `displayplacer list` when no explicit configStr is given.
// mirror: secondary display(s) mirror the primary.
// extend: displays shown side-by-side; un-mirrors if currently mirrored.
// 6. Set Display Layout
// Primary path: display_control (native CoreGraphics, no external deps).
// Build from scripts/display_control.m via scripts/build-display-control.sh on a Mac.
// Commit the resulting resources/bin/display_control binary to the repo.
// Fallback: displayplacer (requires: brew install displayplacer on each venue Mac).
// Also supports per-device configStr override (displayplacer format).
ipcMain.handle('native:set-display-layout', async (event, { mode, configStr }) => {
if (os.platform() !== 'darwin') return { success: false, error: 'Display control only supported on macOS' };
// Try bundled binary first; fall back to common Homebrew/system locations.
// Install on a dev/venue Mac via: brew install displayplacer
const _bin_candidates = app.isPackaged
// Primary: display_control — native CoreGraphics, no Homebrew dependency.
// Derived from OSIT MasterKey app (LegacyUtilities.m). No configStr support needed —
// CoreGraphics auto-detects all connected displays.
const dc_bin = app.isPackaged
? path.join(process.resourcesPath, 'bin', 'display_control')
: path.join(__dirname, '../../resources/bin/display_control');
if (fs.existsSync(dc_bin) && !configStr) {
const dc_cmd = mode === 'mirror' ? 'mirror' : 'extend';
return await runExec(`"${dc_bin}" ${dc_cmd}`);
}
// Fallback: displayplacer — required when display_control binary is not built yet,
// or when a per-device configStr override is set (displayplacer-format string from event_device.data_json).
// Install: brew install displayplacer
const _dp_candidates = app.isPackaged
? [path.join(process.resourcesPath, 'bin', 'displayplacer')]
: [
path.join(__dirname, '../../resources/bin/displayplacer'),
'/opt/homebrew/bin/displayplacer', // Apple Silicon Homebrew
'/usr/local/bin/displayplacer', // Intel Homebrew
];
const binPath = _bin_candidates.find(p => fs.existsSync(p)) ?? _bin_candidates[0];
const dpPath = _dp_candidates.find(p => fs.existsSync(p)) ?? _dp_candidates[0];
// Explicit config string always takes priority — allows manual override per device.
// Explicit configStr takes priority — allows manual per-device override.
if (configStr) {
return await runExec(`"${binPath}" ${configStr}`);
return await runExec(`"${dpPath}" ${configStr}`);
}
// Auto-detect: `displayplacer list` emits a ready-to-run command line at the bottom.
// We parse the quoted display strings from that line and modify them for the requested mode.
const list_result = await runExec(`"${binPath}" list`);
// Auto-detect via `displayplacer list`.
const list_result = await runExec(`"${dpPath}" list`);
if (!list_result.success || !list_result.stdout) {
return { success: false, error: `displayplacer list failed: ${list_result.error ?? 'no output'}` };
return { success: false, error: `displayplacer not available. Build display_control from scripts/build-display-control.sh or run: brew install displayplacer` };
}
// The command line looks like: displayplacer "id:xxx res:... origin:(0,0) ..." "id:yyy ..."
@@ -366,24 +380,21 @@ export function registerSystemHandlers() {
}
const primary_id = primary_id_match[1];
// Primary display unchanged; secondary display(s) get mirror_of_display:<primary_id>.
const mirror_args = display_strings.map((s, i) => {
if (i === 0) return `"${s}"`;
const without_existing_mirror = s.replace(/\s*mirror_of_display:\S+/g, '').trim();
return `"${without_existing_mirror} mirror_of_display:${primary_id}"`;
}).join(' ');
return await runExec(`"${binPath}" ${mirror_args}`);
return await runExec(`"${dpPath}" ${mirror_args}`);
}
if (mode === 'extend') {
const any_mirrored = display_strings.some(s => /\bmirror_of_display:\S+/.test(s));
if (!any_mirrored) {
// Already extended — re-apply current layout to ensure it is active.
return await runExec(`"${binPath}" ${display_strings.map(s => `"${s}"`).join(' ')}`);
return await runExec(`"${dpPath}" ${display_strings.map(s => `"${s}"`).join(' ')}`);
}
// Remove mirror keys and compute side-by-side origins from each display's resolution.
let x_offset = 0;
const extend_args = display_strings.map((s) => {
const without_mirror = s.replace(/\s*mirror_of_display:\S+/g, '').trim();
@@ -394,7 +405,7 @@ export function registerSystemHandlers() {
return `"${updated}"`;
}).join(' ');
return await runExec(`"${binPath}" ${extend_args}`);
return await runExec(`"${dpPath}" ${extend_args}`);
}
return { success: false, error: `Unsupported display mode: ${mode}` };