graphics() rewrite windows

This commit is contained in:
Sebastian Hildebrandt 2019-06-12 09:07:09 +02:00
parent 9679420639
commit e06a192b06

View File

@ -13,6 +13,7 @@
// 7. Graphics (controller, display) // 7. Graphics (controller, display)
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
const os = require('os');
const exec = require('child_process').exec; const exec = require('child_process').exec;
const execSync = require('child_process').execSync; const execSync = require('child_process').execSync;
const util = require('./util'); const util = require('./util');
@ -32,6 +33,27 @@ let _resolutiony = 0;
let _pixeldepth = 0; let _pixeldepth = 0;
let _refreshrate = 0; let _refreshrate = 0;
const videoTypes = {
'-2': 'UNINITIALIZED',
'-1': 'OTHER',
'0': 'HD15',
'1': 'SVIDEO',
'2': 'COMPOSITE_VIDEO',
'3': 'COMPONENT_VIDEO',
'4': 'DVI',
'5': 'HDMI',
'6': 'LVDS',
'8': 'D_JPN',
'9': 'SDI',
'10': 'DISPLAYPORT_EXTERNAL',
'11': 'DISPLAYPORT_EMBEDDED',
'12': 'UDI_EXTERNAL',
'13': 'UDI_EMBEDDED',
'14': 'SDTVDONGLE',
'15': 'MIRACAST',
'0x80000000': 'INTERNAL'
};
function graphics(callback) { function graphics(callback) {
function parseLinesDarwin(lines) { function parseLinesDarwin(lines) {
@ -515,16 +537,42 @@ function graphics(callback) {
resolve(result); resolve(result);
} }
if (_windows) { if (_windows) {
// https://blogs.technet.microsoft.com/heyscriptingguy/2013/10/03/use-powershell-to-discover-multi-monitor-information/ // https://blogs.technet.microsoft.com/heyscriptingguy/2013/10/03/use-powershell-to-discover-multi-monitor-information/
// https://devblogs.microsoft.com/scripting/use-powershell-to-discover-multi-monitor-information/
try { try {
util.wmic('path win32_VideoController get /value').then((stdout, error) => { const workload = [];
if (!error) { workload.push(util.wmic('path win32_VideoController get /value'));
let csections = stdout.split(/\n\s*\n/); workload.push(util.wmic('path win32_desktopmonitor get /value'));
workload.push(util.powerShell('Get-CimInstance -Namespace root\\wmi -ClassName WmiMonitorBasicDisplayParams'));
workload.push(util.powerShell('Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Screen]::AllScreens'));
workload.push(util.powerShell('Get-CimInstance -Namespace root\\wmi -ClassName WmiMonitorConnectionParams'));
Promise.all(
workload
).then(data => {
// controller
let csections = data[0].split(/\n\s*\n/);
result.controllers = parseLinesWindowsControllers(csections); result.controllers = parseLinesWindowsControllers(csections);
util.wmic('path win32_desktopmonitor get /value').then((stdout, error) => {
let dsections = stdout.split(/\n\s*\n/); // displays
if (!error) { let dsections = data[1].split(/\n\s*\n/);
result.displays = parseLinesWindowsDisplays(dsections); // result.displays = parseLinesWindowsDisplays(dsections);
// monitor (powershell)
let msections = data[2].split('Active ');
msections.shift();
// forms.screens (powershell)
let ssections = data[3].split('BitsPerPixel ');
ssections.shift();
result.displays = parseLinesWindowsDisplaysPowershell(ssections, msections, dsections);
// connection params (powershell) - video type
let tsections = data[4].split(/\n\s*\n/);
// tsections.shift();
console.log(tsections);
if (result.controllers.length === 1 && result.displays.length === 1) { if (result.controllers.length === 1 && result.displays.length === 1) {
if (_resolutionx) { if (_resolutionx) {
result.displays[0].currentResX = _resolutionx; result.displays[0].currentResX = _resolutionx;
@ -545,16 +593,17 @@ function graphics(callback) {
result.displays[0].currentRefreshRate = _refreshrate; result.displays[0].currentRefreshRate = _refreshrate;
} }
} }
}
if (callback) { if (callback) {
callback(result); callback(result);
} }
resolve(result); resolve(result);
}); })
} else { .catch(() => {
if (callback) { callback(result); } if (callback) {
resolve(result); callback(result);
} }
resolve(result);
}); });
} catch (e) { } catch (e) {
if (callback) { callback(result); } if (callback) { callback(result); }
@ -588,32 +637,74 @@ function graphics(callback) {
return controllers; return controllers;
} }
function parseLinesWindowsDisplays(sections) { // function parseLinesWindowsDisplays(sections) {
// let displays = [];
// for (let i in sections) {
// if (sections.hasOwnProperty(i)) {
// if (sections[i].trim() !== '') {
// let lines = sections[i].trim().split('\r\n');
// displays.push({
// vendor: util.getValue(lines, 'MonitorManufacturer', '='),
// model: util.getValue(lines, 'Name', '='),
// main: false,
// builtin: false,
// connection: '',
// sizex: -1,
// sizey: -1,
// pixeldepth: -1,
// resolutionx: util.toInt(util.getValue(lines, 'ScreenWidth', '=')),
// resolutiony: util.toInt(util.getValue(lines, 'ScreenHeight', '=')),
// });
// }
// }
// }
// return displays;
// }
function parseLinesWindowsDisplaysPowershell(ssections, msections, dsections) {
let displays = []; let displays = [];
for (let i in sections) { let vendor = '';
if (sections.hasOwnProperty(i)) { let model = '';
if (sections[i].trim() !== '') { let deviceID = '';
let lines = sections[i].trim().split('\r\n'); if (dsections && dsections.length) {
let linesDsplay = dsections[0].split(os.EOL);
vendor = util.getValue(linesDsplay, 'MonitorManufacturer', '=');
model = util.getValue(linesDsplay, 'Name', '=');
deviceID = util.getValue(linesDsplay, 'PNPDeviceID', '=');
}
for (let i = 0; i < ssections.length; i++) {
if (ssections[i].trim() !== '') {
ssections[i] = 'BitsPerPixel ' + ssections[i];
msections[i] = 'Active ' + msections[i];
let linesScreen = ssections[i].split(os.EOL);
let linesMonitor = msections[i].split(os.EOL);
const bitsPerPixel = util.getValue(linesScreen, 'BitsPerPixel');
const bounds = util.getValue(linesScreen, 'Bounds').replace('{', '').replace('}', '').split(',');
const primary = util.getValue(linesScreen, 'Primary');
const sizex = util.getValue(linesMonitor, 'MaxHorizontalImageSize');
const sizey = util.getValue(linesMonitor, 'MaxVerticalImageSize');
const instanceName = util.getValue(linesMonitor, 'InstanceName');
displays.push({ displays.push({
vendor: util.getValue(lines, 'MonitorManufacturer', '='), vendor: instanceName === deviceID ? vendor : '',
model: util.getValue(lines, 'Name', '='), model: instanceName === deviceID ? model : '',
main: false, main: primary.toLowerCase() === 'true',
builtin: false, builtin: false,
connection: '', connection: '',
sizex: -1, resolutionx: util.toInt(util.getValue(bounds, 'Width', '=')),
sizey: -1, resolutiony: util.toInt(util.getValue(bounds, 'Height', '=')),
pixeldepth: -1, sizex: sizex ? parseInt(sizex, 10) : -1,
resolutionx: util.toInt(util.getValue(lines, 'ScreenWidth', '=')), sizey: sizey ? parseInt(sizey, 10) : -1,
resolutiony: util.toInt(util.getValue(lines, 'ScreenHeight', '=')), pixeldepth: bitsPerPixel,
currentResX: -1, currentResX: util.toInt(util.getValue(bounds, 'Width', '=')),
currentResY: -1, currentResY: util.toInt(util.getValue(bounds, 'Width', '=')),
currentRefreshRate: -1
}); });
} }
} }
}
return displays; return displays;
} }
} }
exports.graphics = graphics; exports.graphics = graphics;