I have a really annoying SONY android TV. The device just does not want to turn off when there is no signal via HDMI. I have also a mini PC connected to it and in majority of time the TV just works as a monitor. Now when the PC goes to sleep, the TV keeps the screen on and has a message there - no signal...
(Yes I know I should buy another TV, but this is not an option).
I decided to take some actions, and at least turn the TV off when the PC goes to sleep.
My first idea was to go with the DDC/CI way, but the TV does not have a setting to enable it, and does not react to the actual commands.
So since it is an android TV i got an idea to connect via adb via WiFi and send a shutdown command:
adb shell input keyevent KEYCODE_POWER
Tried this manually from command line - so far so good, the TV accepts the command and turns off.
But here comes the harder part, on Windows 10 I'm (only) able to detect user presence via:
User32.dll and GetLastInputInfo()
But this is not enough as I may watch a video (vlc, chrome, etc) In this case there is no user input, but the system is kept awake by the video player. So I need to know when something is blocking the Windows from sleep.
I searched and I found:
- some are wanting to know if the TV as hardware is turned on or off - I don't need this!
- see if the monitor is on / off in a manner that windows is sending a signal or not via
Win32_DesktopMonitor
andManagementObject
but accessing properties field throws an exception. I would need to accessresult[n].Properties["Availability"]
. - some suggest to hook onto system power change notifications.
This is not a good idea in my case as when my app receives the 'going to sleep' event from the OS there is only a little time to act, and I'm afraid the adb won't be so quick to actually send the commands (connect, doublecheck, turn off). - some suggest to hook onto monitor power change notifications.
Which might be a good idea actually, but also some other people warn about these notification being not 100% reliable. (I haven't tried this for now, but might fall back to this if other trials fail.)
But later I came across this:
https://learn.microsoft.com/en-us/windows-hardware/design/device-experiences/display--sleep--and-hibernate-idle-timers
which suggest that 'Turn off the display timer' does exist. Although I'm unable to find any example how to access it. I also got across that if you actually want windows to prevent from sleep you need to use SetThreadExecutionState
. OK but how to read this ? I actually tried to list all the processes and search for such flags, but either I'm unable to find them or they are not there at all.
Then I came across a command line command: powercfg /requests
which provides me with the info what i need, but this would mean string parsing and more importantly to run the app under admin rights which I would like to avoid.
So my questions:
- does anybody know if and how to read the 'Turn off the display timer' (preferably in c#) ? (which I assume is being reset by the SetThreadExecutionState)
- does anybody know how to get the same info that powercfg /requests provides in more code friendly way preferably without the need of admin privileges ?
- just by chance does anybody know if this could be solved purely on the TV - by an android app maybe ?
Thank You very much if you managed to read through, and even more if you are able to provide some clues.
Update:
I found out that the 2) is probably impossible: Possible to find all Windows processes preventing automatic sleep, w/o admin rights?