pooliot-client
Version:
118 lines (97 loc) • 3.27 kB
JavaScript
/* eslint-disable no-use-before-define */
import Logger from 'nightingale';
import { runAsyncAsUser, runSyncAsUser } from '../utils/exec';
import availableDisplays, { webBrowsers } from '../utils/availableDisplays';
const logger = new Logger('app:commands:display');
let currentDisplay;
let currentUrl;
let displayProcess;
let unclutterProcess;
const isBrowser = display => webBrowsers.includes(display);
export const supports = (display = currentDisplay) => availableDisplays.includes(display);
export const refresh = () => {
if (displayProcess) {
logger.log('refresh');
return runSyncAsUser(`./${currentDisplay}.sh`, ['refresh']);
} else {
logger.warn('display stopped');
}
};
export const update = (config: Object, autoStart = true) => {
logger.info('update');
if (config.display !== currentDisplay) {
if (autoStart) stop();
currentDisplay = config.display;
currentUrl = config.url;
if (autoStart) return start();
} else if (config.url !== currentUrl) {
if (autoStart) stop(true);
currentUrl = config.url;
if (autoStart) return start();
}
};
const startDisplay = () => {
logger.info('starting child');
if (displayProcess) {
throw new Error('child process already started');
}
logger.info('start', { display: currentDisplay, url: currentUrl });
displayProcess = runAsyncAsUser(`./${currentDisplay}.sh`, ['start', currentUrl]);
const thisDisplayProcess = displayProcess;
displayProcess.on('exit', (code, signal) => {
const currentDisplayProcess = displayProcess;
const sameChildProcess = () => thisDisplayProcess === currentDisplayProcess;
displayProcess = null;
logger.error('child process exited', { code, signal });
if (sameChildProcess()) {
process.nextTick(() => {
if (sameChildProcess()) start();
});
}
});
if (!unclutterProcess) {
const unclutterIdle = isBrowser(currentDisplay) ? 5 : 0;
unclutterProcess = runAsyncAsUser('unclutter', ['-display', ':0', '-idle', unclutterIdle]);
unclutterProcess.on('exit', (code, signal) => {
logger.info('unclutter exited', { code, signal });
unclutterProcess = null;
});
}
};
export const openboxStarted = () => {
logger.success('openbox started');
startDisplay();
};
export const start = () => {
if (displayProcess) {
logger.warn('start: already started');
return;
}
if (!supports(currentDisplay)) return;
logger.info('starting...', { display: currentDisplay });
// } else if (startOpenBox() !== 'started') {
// logger.info('openbox not yet started');
// } else {
startDisplay();
// }
};
export const restart = () => {
logger.info('restarting...');
stop(true);
return start();
};
const kill = process => {
process.removeAllListeners();
process.kill();
return null;
};
export const stop = (onlyDisplay: ?boolean = false) => {
logger.info('stop', { display: currentDisplay });
if (displayProcess) displayProcess = kill(displayProcess);
if (!onlyDisplay && unclutterProcess) unclutterProcess = kill(unclutterProcess);
runSyncAsUser('./display.sh', ['stop']);
if (currentDisplay === 'omxplayer') {
runSyncAsUser('./display.sh', ['restart']);
runSyncAsUser('./screen.sh', ['waitForX']);
}
};