ruihe774 / brightness-tray

0 stars 0 forks source link

[Feature Request] Basic implementation of wmi brightness #3

Closed rp1231 closed 10 months ago

rp1231 commented 10 months ago

If a basic version of wmi for brightness is implemented, I can use this app as my daily driver.

ruihe774 commented 10 months ago

Sorry but as mentioned in https://github.com/ruihe774/brightness-tray/issues/1#issuecomment-1826804866, currently I have no WMI monitor (e.g. laptop monitors) so I have no way to implement and debug WMI support. I'd like to implement it when I get a laptop someday. Any help is appreciated. BTW, this issue should be kept open.

rp1231 commented 10 months ago

I can help with the testing of the code if it's possible to develop in that way. You can create a branch for wmi and I can test the code?

ruihe774 commented 10 months ago

@rp1231 You can have a try a45d66dea154efc5506034633a381f1894a17643

rp1231 commented 10 months ago

Thanks for the implementation. But currently it doesn't work properly. It initializes a slider for the internal monitor and reports the brightness properly (I tested at multiple brightness levels)

brightness-tray_UbvhHZ4q7v

But I am not able to change the brightness of the slider, it just turns black like this:

brightness-tray_f34MP1Da5M

This is a screenshot of the errors in the console window:

msedgewebview2_1tR0giZ60P

Let me know if anything else related to debugging is needed from my end.

rp1231 commented 10 months ago

@ruihe774 I changed the if statement to if true { and it got rid of the Failed to load resource: net::ERR_CONNECTION_REFUSED error in the console. But the slider still isn't working. I also ended up installing vue dev tools and found out that the powerState and name are undefined for the internal laptop display.

electron_oqXHVVAcxM

maybe that's causing the problem?

ruihe774 commented 10 months ago

@rp1231 I mistakenly make error silent in some previous commit. Please try latest commit. ❤️

rp1231 commented 10 months ago

This is the console output:

msedgewebview2_Wi4gL5XzfI
ruihe774 commented 10 months ago

This is the console output: msedgewebview2_Wi4gL5XzfI

It is an error from Windows. I have no idea how it happens. 😿

rp1231 commented 10 months ago

Oh ok I see..... Would any of these crates help? https://crates.io/crates/wmi https://crates.io/crates/brightness-windows Mentioning it just in case.

rp1231 commented 10 months ago

Also posting the expanded error message in case it gives a clue:

Screenshot 2023-12-19 233516
rp1231 commented 10 months ago

Ok nevermind, After going through the code I realized that you're dealing with things at a much more advanced level. I just found these powershell commands that work for me to get the brightness and set the brightness. They use the same classes that you've used, so maybe they give you some sort of clue....

This is the powershell line that enables me to get the brightness:

Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness

You're also accessing the same class for retrieving the brightness property.....

This is the powershell script that enables me to set the brightness:

$monitor = Get-WmiObject -ns root/wmi -class WmiMonitorBrightnessMethods
$monitor.WmiSetBrightness(0,100)

But the timeout doesn't seem to work, only the brightness level gets set properly.

This is the article that I got these commands from: https://devblogs.microsoft.com/scripting/use-powershell-to-report-and-set-monitor-brightness/

ruihe774 commented 10 months ago

I've looked through twinkle-tray's implementation. They first try invoking WMI in code (in "wmi-bridge"). If this fails, they will try using PowerShell commands. I think calling WMI through API and PowerShell should be literally same. Maybe there is security or permission problem? This needs investigation (when I get a laptop).

rp1231 commented 10 months ago

@ruihe774 I was able to get and set the brightness of the laptop display (and the external displays) using this crate: https://crates.io/crates/brightness Could you consider using this crate or looking through how they've implemented the wmi feature?

This is the code that I used to test it out:

use brightness::Brightness;
use futures::TryStreamExt;

async fn show_brightness() -> Result<(), brightness::Error> {
    brightness::brightness_devices()
        .try_for_each(|mut dev| async move {
            let name = dev.device_name().await?;
            let value = dev.get().await?;
            println!("Brightness of device {} is {}%", name, value);
            dev.set(50).await?;
            Ok(())
        })
        .await
}

#[tokio::main]
async fn main() {
    println!("Hello, world!");
    if let Err(e) = show_brightness().await {
        eprintln!("Error: {}", e);
    }
}
ruihe774 commented 10 months ago

@rp1231 I've copied some code from brightness crate and you can have a try on ce0acad

rp1231 commented 10 months ago

@ruihe774 Yes it works now! Thanks