pooliot-client
Version:
131 lines (103 loc) • 3.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.stop = exports.restart = exports.start = exports.openboxStarted = exports.update = exports.refresh = exports.supports = undefined;
var _nightingale = require('nightingale');
var _nightingale2 = _interopRequireDefault(_nightingale);
var _exec = require('../utils/exec');
var _availableDisplays = require('../utils/availableDisplays');
var _availableDisplays2 = _interopRequireDefault(_availableDisplays);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const logger = new _nightingale2.default('app:commands:display'); /* eslint-disable no-use-before-define */
let currentDisplay;
let currentUrl;
let displayProcess;
let unclutterProcess;
const supports = exports.supports = (display = currentDisplay) => _availableDisplays2.default.includes(display);
const refresh = exports.refresh = () => {
if (displayProcess) {
logger.log('refresh');
return (0, _exec.runSyncAsUser)(`./${currentDisplay}.sh`, ['refresh']);
} else {
logger.warn('display stopped');
}
};
const update = exports.update = (config, 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 = (0, _exec.runAsyncAsUser)(`./${currentDisplay}.sh`, ['start', currentUrl]);
const thisDisplayProcess = displayProcess;
displayProcess.on('exit', (code, signal) => {
const sameChildProcess = () => thisDisplayProcess === displayProcess;
displayProcess = null;
logger.error('child process exited', { code, signal });
if (sameChildProcess()) {
process.nextTick(() => {
if (sameChildProcess()) start();
});
}
});
if (!unclutterProcess) {
const unclutterIdle = _availableDisplays.webBrowsers.includes(currentDisplay) ? 5 : 0;
unclutterProcess = (0, _exec.runAsyncAsUser)('unclutter', ['-display', ':0', '-idle', unclutterIdle]);
unclutterProcess.on('exit', (code, signal) => {
logger.info('unclutter exited', { code, signal });
unclutterProcess = null;
});
}
};
const openboxStarted = exports.openboxStarted = () => {
logger.success('openbox started');
startDisplay();
};
const start = exports.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();
// }
};
const restart = exports.restart = () => {
logger.info('restarting...');
stop(true);
return start();
};
const kill = process => {
process.removeAllListeners();
process.kill();
return null;
};
const stop = exports.stop = (onlyDisplay = false) => {
logger.info('stop', { display: currentDisplay });
if (displayProcess) displayProcess = kill(displayProcess);
if (!onlyDisplay && unclutterProcess) unclutterProcess = kill(unclutterProcess);
(0, _exec.runSyncAsUser)('./display.sh', ['stop']);
if (currentDisplay === 'omxplayer') {
(0, _exec.runSyncAsUser)('./display.sh', ['restart']);
(0, _exec.runSyncAsUser)('./screen.sh', ['waitForX']);
}
};
//# sourceMappingURL=display.js.map