UNPKG

pooliot-client

Version:
115 lines (92 loc) 2.9 kB
/* eslint-disable no-use-before-define */ import Logger from 'nightingale'; import { get as getConfig } from '../config'; import { runAsyncAsUser, runSyncAsUser } from './exec'; import availableDisplays from '../utils/availableDisplays'; const logger = new Logger('app:commands:display'); let currentDisplay; let autoRestart; let displayProcess; let unclutterProcess; const displaysCouldUseCursor = ['chromium', 'firefox']; export const supports = config => availableDisplays.includes(config.display); export const refresh = () => { if (displayProcess) { logger.log('refresh'); const config = getConfig(); return runSyncAsUser(`./${config.display}.sh`, ['refresh']); } else { logger.warn('display stopped'); } }; export const update = (config: Object) => { logger.info('update'); return restart(); }; function startDisplay() { runSyncAsUser('./display.sh', ['stop']); logger.info('starting child'); if (displayProcess) { throw new Error('child process already started'); } autoRestart = true; const config = getConfig(); currentDisplay = config.display; logger.info('start', { display: currentDisplay, url: config.url }); displayProcess = runAsyncAsUser(`./${currentDisplay}.sh`, ['start', config.url]); const thisDisplayProcess = displayProcess; displayProcess.on('exit', (code, signal) => { const sameChildProcess = thisDisplayProcess === displayProcess; displayProcess = null; logger.error('child process exited', { code, signal }); if (autoRestart && sameChildProcess) { process.nextTick(() => start()); } }); const unclutterIdle = displaysCouldUseCursor.includes(currentDisplay) ? 5 : 0; if (!unclutterProcess) { unclutterProcess = runAsyncAsUser('unclutter', ['-display', ':0', '-idle', unclutterIdle]); unclutterProcess.on('exit', (code, signal) => { logger.info('unclutter exited', { code, signal }); unclutterProcess = null; }); } } export function openboxStarted() { logger.success('openbox started'); startDisplay(); } export function start() { if (displayProcess) { logger.warn('start: already started'); return; } const config = getConfig(); if (!supports(config)) return; logger.info('starting...', { config }); // } else if (startOpenBox() !== 'started') { // logger.info('openbox not yet started'); // } else { startDisplay(); // } } export function restart() { logger.info('restarting...'); stop(); return start(); } export function stop() { autoRestart = false; if (displayProcess) { displayProcess.removeAllListeners(); displayProcess.kill(); } displayProcess = null; if (unclutterProcess) { unclutterProcess.removeAllListeners(); unclutterProcess.kill(); } unclutterProcess = null; logger.info('stop', { display: currentDisplay }); runSyncAsUser('./display.sh', ['stop']); }