Skip to content

Commit

Permalink
Implemented wmi-bridge test at app startup
Browse files Browse the repository at this point in the history
  • Loading branch information
xanderfrangos committed Jul 10, 2022
1 parent dc61650 commit c0364fc
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/Monitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ process.on('message', (data) => {
setVCP(data.monitor, data.code, data.value)
} else if (data.type === "flushvcp") {
vcpCache = [];
} else if (data.type === "wmi-bridge-ok") {
canUseWmiBridge = true
}
} catch(e) {
console.log(e)
Expand All @@ -47,6 +49,7 @@ let monitorsWin32 = {}

let settings = { order: [] }
let localization = {}
let canUseWmiBridge = false

let busyLevel = 0
refreshMonitors = async (fullRefresh = false, ddcciType = "default", alwaysSendUpdate = false) => {
Expand Down Expand Up @@ -99,7 +102,7 @@ refreshMonitors = async (fullRefresh = false, ddcciType = "default", alwaysSendU
}

// WMI
if(!wmiFailed && wmicUnavailable) {
if(canUseWmiBridge && !wmiFailed && wmicUnavailable) {
try {
const wmiBrightness = await getBrightnessWMI()
if (wmiBrightness) {
Expand Down Expand Up @@ -152,7 +155,7 @@ getAllMonitors = async () => {
}

// List via WMI
if(!wmiFailed && wmicUnavailable) {
if(canUseWmiBridge && !wmiFailed && wmicUnavailable) {
try {
const monitorsWMI = await getMonitorsWMI()
console.log(`getMonitorsWMI() Total: ${(startTime - process.hrtime.bigint()) / BigInt(-1000000)}ms`)
Expand Down Expand Up @@ -240,7 +243,7 @@ getAllMonitors = async () => {
}

// WMI Brightness
if(!wmiFailed && wmicUnavailable) {
if(canUseWmiBridge && !wmiFailed && wmicUnavailable) {
try {
startTime = process.hrtime.bigint()
const wmiBrightness = await getBrightnessWMI()
Expand Down Expand Up @@ -569,7 +572,7 @@ function setBrightness(brightness, id) {
let monitor = Object.values(monitors).find(mon => mon.type == "wmi")
monitor.brightness = brightness
monitor.brightnessRaw = brightness
if(wmiFailed) {
if(!canUseWmiBridge || wmiFailed) {
// If native WMI is disabled, fall back to old method
exec(`powershell.exe -NoProfile (Get-WmiObject -Namespace root\\wmi -Class WmiMonitorBrightnessMethods).wmisetbrightness(0, ${brightness})"`)
} else {
Expand Down
32 changes: 31 additions & 1 deletion src/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,35 @@ function startMonitorThread() {
startMonitorThread()



// Test if wmi-bridge works properly on user's system
let monitorsThreadTest
function startMonitorTestThread() {
monitorsThreadTest = fork(path.join(__dirname, 'wmi-bridge-test.js'), ["--isdev=" + isDev, "--apppath=" + app.getAppPath()], { silent: false })
monitorsThreadTest.on("message", (data) => {
if(data?.type === "ready") {
console.log("WMI-BRIDGE TEST: READY")
}
if(data?.type === "ok") {
console.log("WMI-BRIDGE TEST: OK")
monitorsThread.send({
type: "wmi-bridge-ok"
})
}
})
// Close after timeout
setTimeout(() => {
try {
if(monitorsThreadTest.connected) {
console.log("WMI-BRIDGE TEST: Killing thread")
monitorsThreadTest.kill()
}
} catch(e) { console.log(e) }
}, 2000)
}
startMonitorTestThread()


// Mouse wheel scrolling
let mouseEventsActive = false
let bounds
Expand Down Expand Up @@ -2601,14 +2630,15 @@ function handleAccentChange() {
let skipFirstMonChange = false
let handleChangeTimeout
function handleMonitorChange(e, d) {
console.log("Hardware change detected.")

// Skip event that happens at startup
if (!skipFirstMonChange) {
skipFirstMonChange = true
return false
}

console.log("Hardware change detected.")

// Defer actions for a moment just in case of repeat events
if (handleChangeTimeout) {
clearTimeout(handleChangeTimeout)
Expand Down
69 changes: 69 additions & 0 deletions src/wmi-bridge-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
console.log("\x1b[45mRunning wmi-bridge test...\x1b[0m")
const wmibridge = require("wmi-bridge");
require("os").setPriority(0, require("os").constants.priority.PRIORITY_BELOW_NORMAL)

function readInstanceName(insName) {
return (insName ? insName.replace(/&/g, '&').split("\\") : undefined)
}

// For testing timeouts
function wait4s() {
return new Promise(resolve => {
setTimeout(() => {
resolve(true);
}, 4000);
});
}

getMonitorsWMI = () => {
return new Promise(async (resolve, reject) => {
const foundMonitors = {}
try {
const wmiMonitors = await wmibridge.getMonitors();

if (wmiMonitors.failed) {
// Something went wrong
console.log("\x1b[41m" + "wmi-bridge-test: Recieved FAILED response from getMonitors()" + "\x1b[0m")
resolve(foundMonitor)
} else {
// Sort through results
for (let monitorHWID in wmiMonitors) {
const monitor = wmiMonitors[monitorHWID]

if (!monitor.InstanceName) continue;

let hwid = readInstanceName(monitor.InstanceName)
hwid[2] = hwid[2].split("_")[0]

const wmiInfo = {
id: `\\\\?\\${hwid[0]}#${hwid[1]}#${hwid[2]}`,
key: hwid[2],
hwid: hwid,
serial: monitor.SerialNumberID
}

if (monitor.UserFriendlyName !== null && monitor.UserFriendlyName !== "") {
wmiInfo.name = monitor.UserFriendlyName
}

foundMonitors[hwid[2]] = wmiInfo
}
}
} catch (e) {
console.log(`wmi-bridge-test: Failed to get all monitors.`)
console.log(e)
}
resolve(foundMonitors)
})
}



process.send({
type: 'ready'
})

//wait4s().then(() => { })
getMonitorsWMI().then(() => {
process.send({ type: 'ok' })
})

0 comments on commit c0364fc

Please sign in to comment.